Repository navigation
Expand file tree
/
Copy pathBitStream.js
More file actions
765 lines (687 loc) · 19.4 KB
/
Copy pathBitStream.js
File metadata and controls
765 lines (687 loc) · 19.4 KB
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
/**
* The BitStream class used for reading data from a Buffer.
*/
class BitStream {
/**
* Creates a new instance of the BitStream class and sets up the default values for some variables
* @param {Buffer} data
*/
constructor(data = undefined) {
if(data !== undefined) {
this.data = data;
}
else {
this.data = Buffer.alloc(0);
}
this._byteCount = this.data.length;
//for reading data
this._rBytePos = 0;
this._rBitPos = 7;
//for writing data
this._wBytePos = 0;
this._wBitPos = 7;
this._mask = [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80];
this._byte = this._byteCount ? this.data.readUInt8(0) : undefined;
}
/**
* Returns the length of this BitStream
* @returns {Number}
*/
length() {
return this._byteCount;
}
/**
* Gets the number of bits in this stream
* @returns {Number}
*/
bits() {
return this._wBytePos * 8 + (8 - this._wBitPos) - 1;
}
/**
* Returns true if we are at the end of the stream
* @returns {Boolean}
*/
allRead() {
return this._rBytePos * 8 + this._rBitPos >= this._byteCount * 8 - 1;
}
/**
* Reads a single bit from the Buffer
* @returns {Number}
*/
readBit() {
if (this._rBytePos >= this._byteCount) {
this._wBytePos = this._rBytePos;
this.writeByte(0);
}
let byte = this.data.readUInt8(this._rBytePos);
let bit = (byte & this._mask[this._rBitPos]) >> this._rBitPos;
if (--this._rBitPos === -1) {
this._rBitPos = 7;
this._rBytePos++;
}
return bit;
}
/**
* Writes a single bit to the buffer
* @param {boolean} b The bit to write
*/
writeBit(b) {
if(typeof(b) !== "boolean") {
throw new Error("BitStream writeBit was not passed a boolean");
}
//if the wBitPos is in the first bit position, we have rolled over
if(this._wBitPos === 7) {
//increase the buffer size...
let old = this.data;
this.data = Buffer.alloc(this._wBytePos + 1);
this._byteCount = this._wBytePos + 1;
old.copy(this.data);
this.data.writeUInt8(0, this._wBytePos);
}
//now we need to get the current byte...
let byte = this.data.readUInt8(this._wBytePos);
//set the bit
byte |= b << this._wBitPos;
//write the byte
this.data.writeUInt8(byte, this._wBytePos);
//move to next bit
this._wBitPos --;
//if we are done writing this byte, move to the next one
if(this._wBitPos < 0) {
this._wBitPos = 7;
this._wBytePos ++;
}
}
/**
* Reads multiple bits from the buffer
* @param {Number} n The number of bits to read
* @returns {Number}
*/
readBits(n) {
let val = 0;
if(this._rBytePos < this._byteCount) this._byte = this.data.readUInt8(this._rBytePos);
while (n--) {
let bit = this.readBit();
val = (val << 1) | bit;
}
return val;
}
/**
* Writes number to stream with a certain amount of bits
* @param {Number} n The number to write to the stream
* @param {Number} b The number of bits to write
*/
writeBits(n, b) {
for(let i = b; i > 0; i --) {
let temp = (n >> (i - 1)) & 0x01;
this.writeBit(temp === 1);
}
}
/**
* Reads bits in reverse
* @param {Number} n The number of bits to read
* @returns {Number}
*/
readBitsReversed(n) {
let val = 0;
// Don't know why I need this here, but I do
for (let i = 0; i < n; i ++) {
let bit = this.readBit();
val |= (bit << i);
}
return val;
}
/**
* Don't ask...
* @param ret
* @param n
* @param b
* @returns {*}
*/
readBitsStream(ret, n, b = true) {
if(n <= 0) return undefined;
if(this._rBytePos + Math.floor(n/8) > this._byteCount) return undefined;
let c = 0;
while(n > 0) {
if(n >= 8) {
ret.writeByteOffset(this.readByte(), c);
n -= 8;
c++;
} else {
let neg = n - 8;
if(neg < 0) {
if(b) {
ret.writeByteOffset(ret.readByteOffset(c) >> -neg, c);
this._rBytePos += 8 + neg;
} else {
this._rBytePos += 8;
}
}
n = 0;
}
}
return ret;
}
/**
* Reads a byte from the buffer
* @returns {Number}
*/
readByte() {
return this.readBits(8);
}
/**
* Read a byte at a given offset
* @param {Number} o Offset byte
* @returns {Number}
*/
readByteOffset(o) {
return this.data.readUInt8(o);
}
/**
* Read number of bytes to a new stream
* @param {Number} n Number of bytes
* @returns {BitStream}
*/
readBytes(n) {
let val = new BitStream();
while(n-- && this._rBytePos < this.length()) {
val.writeByte(this.readByte());
}
return val;
}
/**
* Writes a byte to the stream
* @param {Number} n The byte to write to the stream
*/
writeByte(n) {
//we have to build an array of true and false... or we can just left shift it and do it that way
for(let i = 0; i < 8; i++) {
let t = (n & 0x80) >>> 7; //get the leftmost bit and put it on the right
this.writeBit(t === 1);
n <<= 1; //move to next bit...
n &= 0xFF; //ensure we are only looking at a byte...
}
}
/**
* Writes a byte a offset
* @param {Number} n Byte to write
* @param {Number} o Offset to write at
*/
writeByteOffset(n, o) {
if(o + 1> this.length()) { //we are trying to write outside the current size... resizing to fix...
this.data = Buffer.alloc(o + 1, 0);
this._byteCount = this._wBytePos + 1;
for(let i = 0; i < this._wBytePos; i ++) {
this.data.writeUInt8(old.readUInt8(i), i); //copy over into new Buffer
}
this._byteCount = o + 1;
}
this.data.writeUInt8(n, o);
}
/**
* Reads a character from the stream
* @returns {Number}
*/
readChar() {
return this.readBits(8);
}
/**
* Writes a boolean to the stream
* @param {Boolean} n
*/
writeBoolean(n) {
if(n === true) {
this.writeByte(1);
} else {
this.writeByte(0);
}
}
/**
* Reads a boolean from the stream
* @returns {boolean}
*/
readBoolean() {
return this.readByte() === true;
}
/**
* Writes a character to the stream
* @param {Number} n Character to write
*/
writeChar(n) {
this.writeByte(n);
}
/**
* Reads a signed character from the stream
* @returns {Number}
*/
readSignedChar() {
if(this.readBit()) {
return -this.readBits(7)
}
return this.readBits(7);
}
/**
* Reads an unsigned short from the stream
* @returns {Number}
*/
readShort() {
return this.readByte() +
(this.readByte() << 8);
}
/**
* Writes an unsigned short to the stream
* @param {Number} n The number to write
*/
writeShort(n) {
this.writeByte(n & 0xff); //write the bottom byte
this.writeByte((n & 0xff00) >>> 8); //write the top byte
}
/**
* Writes a compressed short to this stream
* @param {Number} n The short to compress
*/
writeCompressedShort(n) {
this.writeCompressed(n, 2);
}
/**
* Reads a signed short from the stream
* @returns {Number}
*/
readSignedShort() {
let firstByte = this.readByte();
if(this.readBit()) {
return -(firstByte & (this.readBits(7) << 7));
}
return firstByte & (this.readBits(7) << 7);
}
/**
* Reads an unsigned long from the stream
* @returns {Number}
*/
readLong() {
return this.readByte() +
(this.readByte() << 8) +
(this.readByte() << 16) +
(this.readByte() * 16777216); // Had to do this because shifting it over 24 places causes it to return a signed value because JavsScript treats numbers in bitshift as 32bit
}
/**
* Writes an unsigned long to the stream
* @param {Number} n The number to write
*/
writeLong(n) {
this.writeShort(n & 0xffff); //write the lower two bytes...
this.writeShort((n & 0xffff0000) >>> 16); //write the top two bytes
}
/**
* Writes a compressed long to this stream
* @param {Number} n The long to compress and write to to this stream
*/
writeCompressedLong(n) {
this.writeCompressed(n, 4);
}
/**
* Currently not implemented
*/
readSignedLong() {
//lol no
}
/**
* Reads an unsigned long long from the stream
* @returns {Number}
*/
readLongLong() {
return this.readByte() +
(this.readByte() << 8) +
(this.readByte() << 16) +
(this.readByte() * 16777216) +
(this.readByte() * 4294967296) +
(this.readByte() * 1099511627776) +
(this.readByte() * 281474976710656) +
(this.readByte() * 72057594037927936);
}
/**
* Writes an unsigned long long to the stream
* @param {Number} top The top of the number
* @param {Number} bottom The bottom of the number
*/
writeLongLong(top, bottom) {
this.writeLong(bottom);
this.writeLong(top);
}
/**
* Reads a float from the stream
* @returns {Number}
*/
readFloat() {
let mantissa = this.readShort();
let exponent = this.readBit();
mantissa += (this.readBits(7) << 16);
let sign = this.readBit();
if(sign) {
sign = -1;
} else {
sign = 1;
}
exponent += (this.readBits(7) << 1);
exponent -= 127;
return Math.pow(2, exponent) * (mantissa * 1.1920928955078125e-7 + 1) * sign;
}
/**
* Writes a float to the stream
* @param {Number} n
*/
writeFloat(n) {
let sign = (n < 0);
let exponent = Math.floor(Math.log2(Math.abs(n)));
let mantissa = Math.ceil(((Math.abs(n) / Math.pow(2, exponent)) - 1) / 1.1920928955078125e-7); // 1.1920928955078125e-7 = 2^-23
exponent += 127;
this.writeByte(mantissa & 0xff);
this.writeByte((mantissa & 0xff00) >> 8);
this.writeBit((exponent & 0x01) === 1);
this.writeBits((mantissa & 0x7f0000) >> 16, 7);
this.writeBit(sign);
this.writeBits((exponent & 0xfe) >> 1, 7);
}
/**
* Writes a BitStream to this BitStream
* @param {BitStream} bs
*/
writeBitStream(bs) {
for(let i = 0; i < bs.bits(); i++) {
this.writeBit(bs.readBit() === 1);
}
}
/**
* Reads compressed data from the stream
* @param {Number} size The size of the data to read
* @returns {BitStream}
*/
readCompressed(size) {
let currentByte = size - 1;
let ret = new BitStream();
while(currentByte > 0) {
let b = this.readBit();
if(b) {
currentByte --;
} else {
for(let i = 0; i < size - currentByte - 1; i++) {
ret.writeByte(0);
}
for(let i = 0; i < currentByte + 1; i ++) {
ret.writeByte(this.readByte());
}
return ret;
}
}
let b = this.readBit();
if(b) {
ret.writeByte(this.readBits(4) << 4 && 0xF0);
} else {
ret.writeByte(this.readByte());
}
for(let i = 0; i < size - 1; i++) {
ret.writeByte(0);
}
return ret;
}
/**
*
* @param {Number} data The number to write
* @param {Number} size The number of bytes to write it in
*/
writeCompressed(data, size) {
let currentByte = size - 1;
let mask = [
0xFF,
0XFF00,
0xFF0000,
0xFF000000,
0xFF00000000,
0xFF0000000000,
0xFF000000000000,
0xFF00000000000000
];
while(currentByte > 0) {
let zero = (data & mask[currentByte]) === 0;
this.writeBit(zero);
if(!zero) {
// Now we write all the bits from beginning to the current byte
for(let i = 0; i < currentByte + 1; i ++) {
this.writeByte((data & mask[i]) >> i * 8);
}
return;
}
currentByte --;
}
let zero = (data & 0xF0) === 0;
this.writeBit(zero);
if(zero) {
this.writeBits(data & 0xF0 >> 4, 4);
} else {
this.writeByte(data & 0xFF);
}
}
/**
* Reads a string from the stream
* @param {Number} [size]
* @returns {string}
*/
readString(size) {
if(size === undefined) size = 33;
let text = "";
for(let i = 0; i < size; i++) {
text += String.fromCharCode(this.readByte());
}
return text;
}
/**
*
* @param {String} string
* @param {Number} [size]
*/
writeString(string, size) {
if(size === undefined) {
size = 33;
}
while(string.length < size) {
string += '\0';
}
for(let i = 0; i < string.length; i++) {
this.writeByte(string.charCodeAt(i));
}
}
/**
*
* @param {Number} [size]
* @returns {String}
*/
readWString(size) {
if(size === undefined) {
size = 33;
}
let write = true;
let text = "";
let temp = this.readShort();
write = temp !== 0;
for(let i = 0; i < size - 1; i ++) {
if(write) {
temp = String.fromCharCode(temp);
text += temp;
temp = this.readShort();
write = temp !== 0;
} else {
temp = this.readShort();
}
}
return text;
}
/**
*
* @param {String} string
* @param {Number} [size]
*/
writeWString(string, size) {
if(size === undefined) size = 33;
while(string.length < size) {
string += '\0';
}
for(let i = 0; i < size; i++) {
this.writeByte(string.charCodeAt(i));
this.writeByte(0);
}
}
/**
* Aligns the current reading bit to the next available byte
*/
alignRead() {
if(this._rBitPos !== 7) {
this._rBitPos = 7;
this._rBytePos ++;
}
}
/**
* Aligns the current writing bit to the next available byte
*/
alignWrite() {
if(this._wBitPos !== 7) {
this._wBitPos = 7;
this._wBytePos ++;
}
}
/**
* Adds a BitStream to the end of this BitStream
* @param {BitStream} bs The BitStream to add on
*/
concat(bs) {
for(let i = 0; i < bs.length; i ++) {
for(let j = 0; j < bs[i].length(); j ++) {
this.writeByte(bs[i].readByte());
}
}
}
/**
* Gets the binary string representation of this BitStream
* @returns {String}
*/
toBinaryString() {
let output = "";
let temp = [
'0000',
'0001',
'0010',
'0011',
'0100',
'0101',
'0110',
'0111',
'1000',
'1001',
'1010',
'1011',
'1100',
'1101',
'1110',
'1111'
];
for(let i = 0; i < this._byteCount; i ++) {
let byte = this.data.readUInt8(i);
let partone = (byte & 0xF0) >> 4;
let parttwo = byte & 0x0F;
if(i === this._rBytePos) {
for(let j = 7; j >= 0; j--) {
let bit = (byte & this._mask[j]) >> j;
if(j === this._rBitPos) {
output += " -> "
}
output += bit;
}
output += ' ';
} else if(i === this._wBytePos) {
for(let j = 7; j >= 0; j--) {
let bit = (byte & this._mask[j]) >> j;
if(j === this._wBitPos) {
output += " <- "
}
output += bit;
}
output += ' ';
} else {
output += temp[partone] + temp[parttwo] + ' ';
}
}
return output;
}
/**
* Returns the Hex representation of this BitStream
*/
toHexString() {
let output = "";
let temp = [
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F'
];
for(let i = 0; i < this._byteCount; i ++) {
let byte = this.data.readUInt8(i);
let partone = (byte & 0xF0) >> 4;
let parttwo = byte & 0x0F;
output += temp[partone] + temp[parttwo] + ' ';
}
return output;
}
}
/**
* Turns Unicode into a number
* @param {String} string
* @returns {Number}
*/
function ord (string) {
// discuss at: http://locutus.io/php/ord/
// original by: Kevin van Zonneveld (http://kvz.io)
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
// improved by: Brett Zamir (http://brett-zamir.me)
// input by: incidence
// example 1: ord('K')
// returns 1: 75
// example 2: ord('\uD800\uDC00'); // surrogate pair to create a single Unicode character
// returns 2: 65536
let str = string + '';
let code = str.charCodeAt(0);
if (code >= 0xD800 && code <= 0xDBFF) {
// High surrogate (could change last hex to 0xDB7F to treat
// high private surrogates as single characters)
let hi = code;
if (str.length === 1) {
// This is just a high surrogate with no following low surrogate,
// so we return its value;
return code
// we could also throw an error as it is not a complete character,
// but someone may want to know
}
let low = str.charCodeAt(1);
return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000
}
if (code >= 0xDC00 && code <= 0xDFFF) {
// Low surrogate
// This is just a low surrogate with no preceding high surrogate,
// so we return its value;
return code;
// we could also throw an error as it is not a complete character,
// but someone may want to know
}
return code
}
module.exports = BitStream;