This repository was archived by the owner on Feb 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathab-utils.js
129 lines (112 loc) · 3.26 KB
/
ab-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* eslint-env es6 */
/* global TextEncoder, TextDecoder */
import * as base64 from 'base64-js'
import 'text-encoder-lite'
// see jsperf.com/hex-conversion
const hexChars = '0123456789abcdef'
const hexEncodeArray = hexChars.split('')
export function ab2hex (ab) {
let arr = new Uint8Array(ab)
let s = ''
for (var i = 0, n = ab.byteLength; i < n; i++) {
const byte = arr[i]
s += hexEncodeArray[byte >>> 4]
s += hexEncodeArray[byte & 0x0f]
}
return s
}
function hexdigit (c) {
const i = hexChars.indexOf(c)
if (i < 0) {
throw new Error(`Character '${c}' is not a valid hex character: expected '${hexChars}'`)
}
return i
}
export function hex2ab (hexstr) {
const len = hexstr.length
if (len % 2 !== 0) {
throw new Error(`hex string '${hexstr} 'is expected to have even number of hex characters`)
}
const buflen = len >>> 1
const buf = new ArrayBuffer(buflen)
const view = new Uint8Array(buf)
let i = 0
let rest = hexstr
while (rest.length > 0) {
let [d1, d0, ... newrest] = rest // rest parameter must not be the same as already existing variable
// d0 === undefined should be handled by throw above.
let n = hexdigit(d1) * 16 + hexdigit(d0)
view[i++] = n
rest = newrest
}
return buf
}
// see devnotes.md for more on why we use TextEncoder(Lite) for UTF-8
// conversions.
export function str2utf8ab (str) {
return new TextEncoder('utf-8').encode(str)
}
export function abutf82str (ab) {
return new TextDecoder('utf-8').decode(ab)
}
export function str2ab (str) {
const strlen = str.length
let buf = new ArrayBuffer(strlen * 2)
let view = new Uint16Array(buf)
for (var i = 0; i < strlen; i++) {
view[i] = str.charCodeAt(i)
// don't use charAt(i) here.
}
return buf
}
export function ab2str (ab) {
return String.fromCharCode.apply(null, new Uint16Array(ab))
}
export function ascii2ab (str) {
const strlen = str.length
let buf = new ArrayBuffer(strlen)
let view = new Uint8Array(buf)
for (var i = 0; i < strlen; i++) {
const cc = str.charCodeAt(i)
if (cc > 127) {
throw new Error(`String contains non ascii characters: charCode = '${cc}' at index '${i}'`)
}
view[i] = cc
}
return buf
}
export function ab2ascii (ab) {
const view = new Uint8Array(ab)
const len = view.length
let s = ''
for (var i = 0; i < len; i++) {
const cc = ab[i]
if (cc > 127) {
throw new Error(`String contains non ascii characters: charCode = '${cc}' at index '${i}'`)
}
s += String.fromCharCode(cc)
}
return s
}
export function base64str2ab (base64str) {
return base64.toByteArray(base64str)
}
export function ab2base64str (buf) {
return base64.fromByteArray(new Uint8Array(buf))
}
export function base64urlstr2ab (base64urlstr) {
// Decode url-safe style base64: https://github.com/beatgammit/base64-js/pull/10
// however '=' padding characters must be added, if needed
let str = base64urlstr
let npad = 4 - str.length % 4
if (npad === 4) {
npad = 0
}
str = (str + '===').slice(0, str.length + npad)
return base64.toByteArray(str)
}
export function ab2base64urlstr (buf) {
const str = base64.fromByteArray(new Uint8Array(buf))
// '=' is percent encoded in an URL so strip this:
return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
}