-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyte_array.js
786 lines (713 loc) · 18 KB
/
byte_array.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/* Copyright (c) 2016, Markus Peloquin <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
'use strict';
const ByteArray = (() => {
function _splitBytes(val) {
const b3 = val & 0xff; val >>>= 8;
const b2 = val & 0xff; val >>>= 8;
const b1 = val & 0xff; val >>>= 8;
const b0 = val;
return [b0, b1, b2, b3];
}
class ByteArray {
// new ByteArray(size)
// new ByteArray(int[], size)
constructor(...args) {
if (args.length == 1) {
const [arg0] = args;
if (typeof arg0 === 'number')
this._buf = new ArrayBuffer(arg0);
else if (arg0.constructor == ArrayBuffer)
this._buf = arg0.slice();
else
throw new TypeError;
} else if (args.length == 2) {
const [buf, size] = args;
if (typeof size === 'number' &&
buf.constructor === Array) {
if (buf.length != (size + 3) >> 2)
throw new RuntimeError(
'invalid size');
const newbuf = this._buf =
new ArrayBuffer(size);
const view = new DataView(newbuf);
const words = size >> 2;
for (let i = 0, j = 0; j != words;
i += 4, j++)
view.setUint32(i, buf[j], false);
if (words != buf.length) {
const b = _splitBytes(
buf[buf.length - 1]);
for (let i = words << 2, j = 0;
i != size; i++, j++)
view.setUint8(i, b[j]);
}
} else
throw new TypeError;
} else
throw new RuntimeError('invalid parameters');
this._view = new DataView(this._buf);
}
size() {
return this._buf.byteLength;
}
fill(val=0) {
const _view = this._view;
const size = _view.byteLength;
const size4 = size & ~0x3;
const fillByte = val & 0xff;
let fillWord = fillByte | (fillByte << 8);
fillWord |= fillWord << 16;
for (let i = 0; i != size4; i += 4)
_view.setUint32(i, fillWord);
for (let i = size4; i != size; i++)
_view.setUint8(i, fillByte);
}
get(index) {
const _view = this._view;
if (index < 0 || index >= _view.byteLength)
throw new IndexError(index);
return _view.getUint8(index);
}
get16(index, littleEndian=false) {
const _view = this._view;
if (index < 0 || index >= _view.byteLength >>> 1)
throw new IndexError(index);
return _view.getUint16(index << 1, littleEndian);
}
get32(index, littleEndian=false) {
const _view = this._view;
if (index < 0 || index >= _view.byteLength >>> 2)
throw new IndexError(index);
return _view.getUint32(index << 2, littleEndian);
}
get64(index, littleEndian=false) {
const _view = this._view;
if (index < 0 || index >= _view.byteLength >>> 3)
throw new IndexError(index);
index <<= 3;
const hi = _view.getUint32(index, littleEndian);
const lo = _view.getUint32(index + 4, littleEndian);
return littleEndian ? new Long(lo, hi) : new Long(hi, lo);
}
set(index, val) {
const _view = this._view;
if (index < 0 || index >= _view.byteLength)
throw new IndexError(index);
_view.setUint8(index, val);
}
set16(index, val, littleEndian=false) {
const _view = this._view;
if (index < 0 || index >= _view.byteLength >>> 1)
throw new IndexError(index);
_view.setUint16(index << 1, val, littleEndian);
}
set32(index, val, littleEndian=false) {
const _view = this._view;
if (index < 0 || index >= _view.byteLength >>> 2)
throw new IndexError(index);
_view.setUint32(index << 2, val, littleEndian);
}
set64(index, val, littleEndian=false) {
const _view = this._view;
if (index < 0 || index >= _view.byteLength >>> 3)
throw new IndexError(index);
index <<= 3;
if (littleEndian) {
_view.setUint32(index, val.lo(), true);
_view.setUint32(index + 4, val.hi(), true);
} else {
_view.setUint32(index, val.hi(), false);
_view.setUint32(index + 4, val.lo(), false);
}
}
/** swap byte order in place
*
* if two arguments given, second interpreted as 'count'
* \param buf data buffer
* \param off (optional) index in buffer to start at
* \param count (optional) number of elements to swap
*/
swap16(off=0, count=-1) {
const _view = this._view;
let end;
if (count == -1)
end = (_view.byteLength >>> 1) - off;
else
end = off + count;
if (off < 0 || end > _view.byteLength >>> 1)
throw new IndexError;
for (let i = off, j = off << 1; i != end; i++, j += 2)
_view.setUint16(j, _view.getUint16(j, false), true);
}
swap32(off=0, count=-1) {
const _view = this._view;
let end;
if (count == -1)
end = (_view.byteLength >>> 2) - off;
else
end = off + count;
if (off < 0 || end > _view.byteLength >>> 2)
throw new IndexError;
for (let i = off, j = off << 2; i != end; i++, j += 4)
_view.setUint32(j, _view.getUint32(j, false), true);
}
swap64(off=0, count=-1) {
const _view = this._view;
let end;
if (count == -1)
end = (_view.byteLength >>> 3) - off;
else
end = off + count;
if (off < 0 || end > _view.byteLength >>> 3)
throw new IndexError;
for (let i = off, j = off << 3; i != end; i++, j += 8) {
const hi = _view.getUint32(j, false);
const lo = _view.getUint32(j + 4, false);
_view.setUint32(j, lo, true);
_view.setUint32(j + 4, hi, true);
}
}
/** Convert a buffer to a base64 string.
*
* buf.toBase64()
* buf.toBase64(sz)
* buf.toBase64(sz, off)
*
* \param sz [optional] number of bytes to copy
* \param off [optional] start offset
* \return base64 string
*/
toBase64(start=0, end=-1) {
const _view = this._view;
if (end == -1)
end = _view.byteLength;
else if (end < start)
throw new RuntimeError('end < start');
const [S_XXX, S_0XX, S_01X] = [0, 1, 2];
let accum = 0;
let state = S_XXX;
let res = '';
for (; start != end; start++) {
let x = _view.getUint8(start);
switch (state) {
case S_XXX:
res += B64_ALPHABET[x >>> 2]; // aaaaaa
accum = (x & 0x3) << 4; // aa0000
state = S_0XX;
break;
case S_0XX:
res += B64_ALPHABET[accum | (x >>> 4)]; // aabbbb
accum = (x & 0xf) << 2; // bbbb00
state = S_01X;
break;
case S_01X:
res += B64_ALPHABET[accum | (x >>> 6)]; // bbbbcc
res += B64_ALPHABET[x & 0x3f]; // cccccc
state = S_XXX;
}
}
switch (state) {
case S_0XX:
res += '==';
break;
case S_01X:
res += B64_ALPHABET[accum] + '='; // bbbb00
}
return res;
}
/** Convert a buffer to a hex string
*
* buf.toHex()
* buf.toHex(start)
* buf.toHex(start, end)
*
* \param start [optional]
* \param end [optional]
* \return hex string
*/
toHex(start=0, end=-1) {
const _view = this._view;
if (end == -1)
end = _view.byteLength;
else if (end < start)
throw new RuntimeError('end < start');
let res = '';
for (; start != end; start++) {
let x = _view.getUint8(start);
res += B16_ALPHABET[x >> 4] +
B16_ALPHABET[x & 0xf];
}
return res;
}
toText(enc=null) {
const _view = this._view;
if (enc == null) {
try {
return _toUtf8(_view);
} catch (e) {
if (!(e instanceof EncodingError))
throw e;
enc = 'latin1';
}
}
switch (enc) {
case 'l1':
case 'latin1':
case 'ISO-8859-1':
case 'ISO_8859-1':
case 'iso-8859-1':
case 'iso_8859-1':
return _toLatin(_view);
case 'UTF-8':
case 'utf-8':
return _toUtf8(_view);
case 'UTF-16BE':
case 'utf-16be':
return _toUtf16(_view, false);
case 'UTF-16LE':
case 'utf-16le':
return _toUtf16(_view, true);
default:
throw new EncodingError('invalid encoding: ' + enc);
}
}
/** convert a base64 string to bytes
* \param str base64 string
* \return the buffer
*/
static fromBase64(str) {
if (str.length & 3) throw new RuntimeError('invalid parameter');
// inclusive bounds
const maxBytes = (str.length >>> 2) * 3;
const minBytes = maxBytes - 2;
// [ABC] is some b64 character, [E] is '=', [X] is dont-know
const [S_XXXX, S_AXXX, S_ABXX, S_ABCX, S_ABEX, S_END] = [0, 1, 2, 3, 4, 5];
const bytes = [];
let accum = 0;
let state = S_XXXX;
for (const ch of str.split('')) {
const c = B64_ALPHABET_REV[ch.charCodeAt(0)];
if (c == null) throw new RuntimeError('invalid parameter');
switch (state) {
case S_XXXX:
if (c == -1) {
// ...[=***]...
throw new RuntimeException('invalid parameter');
}
accum = c << 2; // aaaaaa00
state = S_AXXX;
break;
case S_AXXX:
if (c == -1) {
// ...[a=**]...
throw new RuntimeException('invalid parameter');
}
bytes.push(accum | (c >> 4)); // aaaaaabb
accum = (c & 0xf) << 4; // bbbb0000
state = S_ABXX;
break;
case S_ABXX:
if (c == -1)
state = S_ABEX;
else {
bytes.push(accum | (c >> 2)); // bbbbcccc
accum = (c & 0x3) << 6; // cc000000
state = S_ABCX;
}
break;
case S_ABCX:
if (c == -1)
state = S_END;
else {
bytes.push(accum | c); // ccdddddd
state = S_XXXX;
}
break;
case S_ABEX:
if (c != -1) {
// ...[ab=c]...
throw new RuntimeException('invalid parameter');
}
state = S_END;
break;
case S_END:
// ...[ab*=][****]...
throw new RuntimeException('invalid parameter');
default:
throw new AssertionError('bad state ' + state);
}
}
if (bytes.length < minBytes || bytes.length > maxBytes)
throw new AssertionError('bad length');
// copy bytes into an ArrayBuffer
const buf = new ArrayBuffer(bytes.length);
const view = new DataView(buf);
for (let i = 0; i != bytes.length; i++)
view.setUint8(i, bytes[i]);
return new ByteArray(buf);
}
/** convert a hex string to bytes
* \param str hex string
* \return the buffer
*/
static fromHex(str) {
if (str.length & 1) throw new RuntimeError('invalid parameter');
const buf = new ArrayBuffer(str.length >> 1);
const view = new DataView(buf);
let pos = 0;
let accum = 0;
let accumSz = 0;
for (const ch of str.split('')) {
const c = B16_ALPHABET_REV[ch.charCodeAt(0)];
if (c == null) throw new RuntimeError('invalid parameter');
switch (accumSz) {
case 0:
accum = c << 4;
accumSz = 1;
break;
case 1:
view.setUint8(pos++, accum | c);
accumSz = 0;
}
}
return new ByteArray(buf);
}
/** convert a text string to bytes
* \param str text string
* \param enc how to encode characters
* \return the buffer
*/
static fromText(str, enc='utf-8') {
switch (enc) {
case 'l1':
case 'latin1':
case 'ISO-8859-1':
case 'ISO_8859-1':
case 'iso-8859-1':
case 'iso_8859-1':
return _fromLatin(str);
case 'UTF-8':
case 'utf-8':
return _fromUtf8(str);
case 'UTF-16BE':
case 'utf-16be':
return _fromUtf16(str, false);
case 'UTF-16LE':
case 'utf-16le':
return _fromUtf16(str, true);
default:
throw new EncodingError('invalid encoding: ' + enc);
}
}
static generateRandom(size) {
if (window.crypto === undefined) {
const arr = new Uint8Array(size);
for (let i = 0; i < size; i++)
arr[i] = (Math.random() * 0x100) ^ 0;
return new ByteArray(arr.buffer);
}
let tmp;
if (size & 1)
tmp = new Uint8Array(size);
else if (size & 3)
tmp = new Uint16Array(size >>> 1);
else
tmp = new Uint32Array(size >>> 2);
crypto.getRandomValues(tmp);
return new ByteArray(tmp.buffer);
}
}
const B16_ALPHABET = '0123456789abcdef'.split('');
const B64_ALPHABET = (
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'abcdefghijklmnopqrstuvwxyz' +
'0123456789+/').split('');
const B16_ALPHABET_REV = (() => {
const diff = 'a'.charCodeAt(0) - 'A'.charCodeAt(0);
const table = new Array('f'.charCodeAt(0) + 1);
let i = 0;
for (const ch of B16_ALPHABET) {
const code = ch.charCodeAt(0);
table[code] = i;
table[code - diff] = i;
i++;
}
return table;
})();
const B64_ALPHABET_REV = (() => {
const table = new Array('z'.charCodeAt(0) + 1);
let i = 0;
for (const ch of B64_ALPHABET)
table[ch.charCodeAt(0)] = i++;
table['='.charCodeAt(0)] = -1;
return table;
})();
function _swap16(n) {
return ((n << 8) & 0xff00) | (n >> 8);
}
class Buffer {
constructor() {
this._buf = [];
this._accum = 0;
this._accumSz = 0;
}
push8(val) {
switch (this._accumSz) {
case 0:
this._accum = val << 24;
this._accumSz = 1;
break;
case 1:
this._accum |= val << 16;
this._accumSz = 2;
break;
case 2:
this._accum |= val << 8;
this._accumSz = 3;
break;
default:
this._buf.push(this._accum | val);
this._accumSz = 0;
}
}
push16(val) {
switch (this._accumSz) {
case 0:
this._accum = val << 16;
this._accumSz = 2;
break;
case 1:
this._accum |= val << 8;
this._accumSz = 3;
break;
case 2:
this._buf.push(this._accum | val);
this._accumSz = 0;
break;
default:
this._buf.push(this._accum | (val >>> 8));
this._accum = val << 24;
this._accumSz = 1;
}
}
push24(val) {
switch (this._accumSz) {
case 0:
this._accum = val << 8;
this._accumSz = 3;
break;
case 1:
this._buf.push(this._accum | val);
this._accumSz = 0;
break;
case 2:
this._buf.push(this._accum | (val >>> 8));
this._accum = val << 24;
this._accumSz = 1;
break;
default:
this._buf.push(this._accum | (val >>> 16));
this._accum = val << 16;
this._accumSz = 2;
}
}
push32(val) {
switch (this._accumSz) {
case 0:
this._buf.push(val);
break;
case 1:
this._buf.push(this._accum | (val >>> 24));
this._accum = val << 8;
break;
case 2:
this._buf.push(this._accum | (val >>> 16));
this._accum = val << 16;
break;
default:
this._buf.push(this._accum | (val >>> 8));
this._accum = val << 24;
}
}
finalize() {
const out = this._buf;
const _accum = this._accum;
const _accumSz = this._accumSz;
let len = out.length << 2;
if (_accumSz) {
out.push(_accum);
len += _accumSz;
}
// reset
this._buf = [];
this._accumSz = 0;
return new ByteArray(out, len);
}
}
const _fromLatin = str => {
const buf = new Buffer;
for (const ch of str.split('')) {
const code = ch.charCodeAt(0);
if (code & ~0xff)
throw new RuntimeError('invalid Latin1');
buf.push8(code);
}
return buf.finalize();
}
const _fromUtf8 = str => {
const buf = new Buffer;
for (const ch of str.split('')) {
const code = ch.charCodeAt(0);
if (code < 0x80)
buf.push8(code);
else if (code < 0x800) {
let c = code;
const c1 = c & 0x3f; c >>= 6;
const c0 = c;
buf.push16(0xc080 | (c0 << 8) | c1);
} else if (code < 0x10000) {
if (0xd800 <= code && code < 0xe000 || 0x110000 <= code)
throw new EncodingError('invalid Unicode');
let c = code;
const c2 = c & 0x3f; c >>= 6;
const c1 = c & 0x3f; c >>= 6;
const c0 = c;
buf.push24(0xe08080 | (c0 << 16) | (c1 << 8) | c2);
} else if (code < 0x200000) {
let c = code;
const c3 = c & 0x3f; c >>= 6;
const c2 = c & 0x3f; c >>= 6;
const c1 = c & 0x3f; c >>= 6;
const c0 = c;
buf.push32(0xf0808080 | (c0 << 24) | (c1 << 16) | (c2 << 8) | c3);
} else
throw new EncodingError('invalid Unicode');
}
return buf.finalize();
}
const _fromUtf16 = (str, littleEndian) => {
const buf = [];
const swap = littleEndian ? _swap16 : (x => x);
for (const ch of str.split('')) {
const code = ch.charCodeAt(0);
if (code < 0xd800)
buf.push16(swap(code));
else if (code < 0x10000) {
if (code < 0xe000)
throw new EncodingError('invalid Unicode');
buf.push16(swap(code));
} else if (code < 0x110000) {
let c = code - 0x10000;
const c1 = 0xdc00 + (c & 0x3ff); c >>= 10;
const c0 = 0xd800 + c;
buf.push16(swap(c0));
buf.push16(swap(c1));
} else
throw new EncodingError('invalid Unicode');
}
return buf.finalize();
}
function _toLatin(view) {
const sz = view.byteLength;
let res = '';
for (let i = 0; i != sz; i++)
res += String.fromCharCode(view.getUint8(i));
return res;
}
function _toUtf8(view) {
const sz = view.byteLength;
let accum = 0;
let accumLeft = 0;
let res = '';
for (let i = 0; i != sz; i++) {
let x = view.getUint8(i);
if (accumLeft) {
// in other words (x < 0x80 || x >= 0xc0)
if ((x & 0xc0) != 0x80)
throw new EncodingError('invalid UTF-8');
x &= 0x3f;
switch (accumLeft) {
case 1:
accum |= x;
if (0xd800 <= accum && accum < 0xe000 ||
0x110000 <= accum)
throw new EncodingError(
'invalid UTF-8');
res += String.fromCharCode(accum);
accum = 0;
break;
case 2:
accum |= x << 6;
break;
case 3:
accum |= x << 12;
break;
default:
throw new AssertionError;
}
accumLeft--;
} else {
if (x < 0x80)
res += String.fromCharCode(x);
else if (x < 0xc0)
throw new EncodingError('invalid UTF-8');
else if (x < 0xe0) {
accum = (x & 0x1f) << 6;
accumLeft = 1;
} else if (x < 0xf0) {
accum = (x & 0xf) << 12;
accumLeft = 2;
} else if (x < 0xf8) {
accum = (x & 0x7) << 18;
accumLeft = 3;
} else
throw new EncodingError('invalid UTF-8');
}
}
return res;
}
function _toUtf16(view, littleEndian) {
const sz = view.byteLength;
if (sz & 1) throw new EncodingError('invalid UTF-16');
let accum = 0;
let accumLeft = false;
let res = '';
for (let i = 0; i != sz; i += 2) {
const x = view.getUint16(i, littleEndian);
if (accumLeft) {
if (x < 0xdc00)
throw new EncodingError('invalid UTF-16');
else if (x < 0xe000) {
const code = 0x10000 + (accum | (x - 0xdc00));
accumLeft = false;
res += String.fromCharCode(code);
} else
throw new EncodingError('invalid UTF-16');
} else {
if (x < 0xd800)
res += String.fromCharCode(x);
else if (x < 0xdc00) {
accum = (x - 0xd800) << 10;
accumLeft = true;
} else if (x < 0xe000)
throw new EncodingError('invalid UTF-16');
else
res += String.fromCharCode(x);
}
}
return res;
}
return ByteArray;
})();