-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffers.ts
263 lines (219 loc) · 7.08 KB
/
buffers.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/** Get buffer from string */
export function bufferFromString(string: string) {
const buffer = new ArrayBuffer(string.length)
const bufferView = new Uint8Array(buffer)
for (let i = 0; i < string.length; i++) {
bufferView[i] = string.charCodeAt(i)
}
return buffer
}
/** Get string from buffer */
export function stringFromBuffer(buffer: ArrayBuffer) {
const bufferView = new Uint8Array(buffer)
let string = ''
for (let i = 0; i < bufferView.byteLength; i++) {
string += String.fromCharCode(bufferView[i])
}
return string
}
/* eslint-disable max-depth, sonarjs/cognitive-complexity, @typescript-eslint/no-magic-numbers */
/**
* Get buffer from unicode string
*
* NOTE: based on `buffer` realization
* https://github.com/feross/buffer/blob/795bbb5bda1b39f1370ebd784bea6107b087e3a7/index.js#L1956-L2034
*/
export function bufferFromUnicode(string: string) {
const bytes = []
let codePoint
let units = Infinity
let leadSurrogate = null
for (let i = 0; i < string.length; ++i) {
codePoint = string.charCodeAt(i)
// is surrogate component
if (codePoint > 0xd7ff && codePoint < 0xe000) {
// last char was a lead
if (!leadSurrogate) {
// no lead yet
if (codePoint > 0xdbff || i + 1 === string.length) {
// unexpected trail
if ((units -= 3) > -1) bytes.push(0xef, 0xbf, 0xbd)
continue
}
// valid lead
leadSurrogate = codePoint
continue
}
// 2 leads in a row
if (codePoint < 0xdc00) {
if ((units -= 3) > -1) bytes.push(0xef, 0xbf, 0xbd)
leadSurrogate = codePoint
continue
}
// valid surrogate pair
codePoint =
(((leadSurrogate - 0xd800) << 10) | (codePoint - 0xdc00)) + 0x10000
// eslint-disable-next-line sonarjs/no-collapsible-if
} else if (leadSurrogate) {
// valid bmp char, but last char was a lead
if ((units -= 3) > -1) bytes.push(0xef, 0xbf, 0xbd)
}
leadSurrogate = null
// encode utf8
if (codePoint < 0x80) {
if ((units -= 1) < 0) break
bytes.push(codePoint)
} else if (codePoint < 0x800) {
if ((units -= 2) < 0) break
bytes.push((codePoint >> 0x6) | 0xc0, (codePoint & 0x3f) | 0x80)
} else if (codePoint < 0x10000) {
if ((units -= 3) < 0) break
bytes.push(
(codePoint >> 0xc) | 0xe0,
((codePoint >> 0x6) & 0x3f) | 0x80,
(codePoint & 0x3f) | 0x80
)
} else if (codePoint < 0x110000) {
if ((units -= 4) < 0) break
bytes.push(
(codePoint >> 0x12) | 0xf0,
((codePoint >> 0xc) & 0x3f) | 0x80,
((codePoint >> 0x6) & 0x3f) | 0x80,
(codePoint & 0x3f) | 0x80
)
} else {
throw new Error('Invalid code point')
}
}
const buffer = new ArrayBuffer(bytes.length)
const bufferView = new Uint8Array(buffer)
for (let i = 0; i < bytes.length; ++i) {
if (i >= bufferView.length || i >= bytes.length) {
break
}
bufferView[i] = bytes[i]
}
return buffer
}
// NOTE: based on http://stackoverflow.com/a/22747272/680742, the browser with
// the lowest limit is Chrome, with 0x10000 args.
// We go 1 magnitude less, for safety
const MAX_ARGUMENTS_LENGTH = 0x1000
/**
* Get unicode string from buffer
*
* NOTE: based on `buffer` realization
* https://github.com/feross/buffer/blob/795bbb5bda1b39f1370ebd784bea6107b087e3a7/index.js#L954-L1028
*/
export function unicodeFromBuffer(buffer: ArrayBuffer) {
const bufferView = new Uint8Array(buffer)
const codePoints = []
let i = 0
while (i < bufferView.length) {
const firstByte = bufferView[i]
let codePoint = null
let bytesPerSequence =
// eslint-disable-next-line no-nested-ternary
firstByte > 0xef ? 4 : firstByte > 0xdf ? 3 : firstByte > 0xbf ? 2 : 1
if (i + bytesPerSequence <= bufferView.length) {
let secondByte, thirdByte, fourthByte, tempCodePoint
switch (bytesPerSequence) {
case 1:
if (firstByte < 0x80) {
codePoint = firstByte
}
break
case 2:
secondByte = bufferView[i + 1]
if ((secondByte & 0xc0) === 0x80) {
tempCodePoint = ((firstByte & 0x1f) << 0x6) | (secondByte & 0x3f)
if (tempCodePoint > 0x7f) {
codePoint = tempCodePoint
}
}
break
case 3:
secondByte = bufferView[i + 1]
thirdByte = bufferView[i + 2]
if ((secondByte & 0xc0) === 0x80 && (thirdByte & 0xc0) === 0x80) {
tempCodePoint =
((firstByte & 0xf) << 0xc) |
((secondByte & 0x3f) << 0x6) |
(thirdByte & 0x3f)
if (
tempCodePoint > 0x7ff &&
(tempCodePoint < 0xd800 || tempCodePoint > 0xdfff)
) {
codePoint = tempCodePoint
}
}
break
case 4:
secondByte = bufferView[i + 1]
thirdByte = bufferView[i + 2]
fourthByte = bufferView[i + 3]
if (
(secondByte & 0xc0) === 0x80 &&
(thirdByte & 0xc0) === 0x80 &&
(fourthByte & 0xc0) === 0x80
) {
tempCodePoint =
((firstByte & 0xf) << 0x12) |
((secondByte & 0x3f) << 0xc) |
((thirdByte & 0x3f) << 0x6) |
(fourthByte & 0x3f)
if (tempCodePoint > 0xffff && tempCodePoint < 0x110000) {
codePoint = tempCodePoint
}
}
}
}
if (codePoint === null) {
// we did not generate a valid codePoint so insert a
// replacement char (U+FFFD) and advance only 1 byte
codePoint = 0xfffd
bytesPerSequence = 1
} else if (codePoint > 0xffff) {
// encode to utf16 (surrogate pair dance)
codePoint -= 0x10000
codePoints.push(((codePoint >>> 10) & 0x3ff) | 0xd800)
codePoint = 0xdc00 | (codePoint & 0x3ff)
}
codePoints.push(codePoint)
i += bytesPerSequence
}
if (codePoints.length <= MAX_ARGUMENTS_LENGTH) {
return String.fromCharCode(...codePoints) // avoid extra slice()
}
// Decode in chunks to avoid "call stack size exceeded".
let string = ''
let j = 0
while (j < codePoints.length) {
string += String.fromCharCode(
...codePoints.slice(j, (j += MAX_ARGUMENTS_LENGTH))
)
}
return string
}
/* eslint-enable max-depth, sonarjs/cognitive-complexity, @typescript-eslint/no-magic-numbers */
/** Get base64url from string */
export function base64urlFromString(string: string) {
return window
.btoa(string)
.replace(/=/g, '')
.replace(/\+/g, '-')
.replace(/\//g, '_')
}
/** Get string from base64url */
export function stringFromBase64Url(string: string) {
return window.atob(
string.replace(/-/g, '+').replace(/_/g, '/').replace(/\n/g, '')
)
}
/** Concatenate buffers */
export function concatBuffers(buffer1: ArrayBuffer, buffer2: ArrayBuffer) {
const result = new Uint8Array(buffer1.byteLength + buffer2.byteLength)
result.set(new Uint8Array(buffer1), 0)
result.set(new Uint8Array(buffer2), buffer1.byteLength)
return result.buffer
}