bireader 1.0.19 → 1.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,20 +41,27 @@ export function skip(_this, bytes, bits) {
41
41
  const new_size = (((bytes || 0) + _this.offset) + Math.ceil((_this.bitoffset + (bits || 0)) / 8));
42
42
  _this.check_size(bytes || 0, bits || 0);
43
43
  if (new_size > _this.size) {
44
- _this.errorDump ? "\x1b[31m[Error]\x1b[0m hexdump:\n" + _this.hexdump() : "";
45
- throw new Error("\x1b[33m[Strict mode]\x1b[0m: Seek outside of size of data: " + _this.size);
44
+ if (_this.strict == false) {
45
+ _this.extendArray(new_size - _this.size);
46
+ }
47
+ else {
48
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m hexdump:\n" + _this.hexdump() : "";
49
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: Seek of range of data: seek " + new_size + " of " + _this.size);
50
+ }
46
51
  }
47
52
  _this.bitoffset += (bits || 0) % 8;
48
53
  _this.offset += (bytes || 0);
49
54
  }
50
55
  export function goto(_this, byte, bit) {
51
56
  const new_size = (byte + Math.ceil((bit || 0) / 8));
52
- if (new_size > _this.size && _this.strict == false) {
53
- _this.extendArray(new_size - _this.size);
54
- }
55
- else {
56
- _this.errorDump ? "\x1b[31m[Error]\x1b[0m hexdump:\n" + _this.hexdump() : "";
57
- throw new Error("\x1b[33m[Strict mode]\x1b[0m: Outside of range of data: goto " + new_size + " of " + _this.size);
57
+ if (new_size > _this.size) {
58
+ if (_this.strict == false) {
59
+ _this.extendArray(new_size - _this.size);
60
+ }
61
+ else {
62
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m hexdump:\n" + _this.hexdump() : "";
63
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: Goto utside of range of data: goto " + new_size + " of " + _this.size);
64
+ }
58
65
  }
59
66
  _this.offset = byte;
60
67
  _this.bitoffset = (bit || 0) % 8;
@@ -103,9 +110,11 @@ export function remove(_this, startOffset, endOffset, consume, remove, fillValue
103
110
  if (consume == true) {
104
111
  if (remove != true) {
105
112
  _this.offset = new_offset;
113
+ _this.bitoffset = 0;
106
114
  }
107
115
  else {
108
116
  _this.offset = new_start;
117
+ _this.bitoffset = 0;
109
118
  }
110
119
  }
111
120
  return data_removed;
@@ -126,8 +135,13 @@ export function addData(_this, data, consume, offset, repalce) {
126
135
  needed_size = offset || _this.offset + data.length;
127
136
  }
128
137
  if (needed_size > _this.size) {
129
- const dif = needed_size - _this.size;
130
- _this.extendArray(dif);
138
+ if (_this.strict == false) {
139
+ _this.extendArray(needed_size - _this.size);
140
+ }
141
+ else {
142
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
143
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + needed_size + " of " + _this.size);
144
+ }
131
145
  _this.size = _this.data.length;
132
146
  }
133
147
  if (repalce) {
@@ -160,6 +174,7 @@ export function addData(_this, data, consume, offset, repalce) {
160
174
  }
161
175
  if (consume) {
162
176
  _this.offset = needed_size;
177
+ _this.bitoffset = 0;
163
178
  }
164
179
  }
165
180
  export function hexDump(_this, options) {
@@ -344,7 +359,7 @@ export function hexDump(_this, options) {
344
359
  }
345
360
  console.log(rows.join("\n"));
346
361
  }
347
- export function AND(_this, xor_key, start, end, consume) {
362
+ export function AND(_this, and_key, start, end, consume) {
348
363
  const input = _this.data;
349
364
  if ((end || 0) > _this.size) {
350
365
  if (_this.strict == false) {
@@ -355,27 +370,29 @@ export function AND(_this, xor_key, start, end, consume) {
355
370
  throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
356
371
  }
357
372
  }
358
- if (typeof xor_key == "number") {
373
+ if (typeof and_key == "number") {
359
374
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
360
- input[i] = input[i] & (xor_key & 0xff);
375
+ input[i] = input[i] & (and_key & 0xff);
361
376
  if (consume) {
362
377
  _this.offset = i;
378
+ _this.bitoffset = 0;
363
379
  }
364
380
  }
365
381
  }
366
382
  else {
367
- if (_this.isBufferOrUint8Array(xor_key)) {
383
+ if (_this.isBufferOrUint8Array(and_key)) {
368
384
  let number = -1;
369
385
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
370
- if (number != xor_key.length - 1) {
386
+ if (number != and_key.length - 1) {
371
387
  number = number + 1;
372
388
  }
373
389
  else {
374
390
  number = 0;
375
391
  }
376
- input[i] = input[i] & xor_key[number];
392
+ input[i] = input[i] & and_key[number];
377
393
  if (consume) {
378
394
  _this.offset = i;
395
+ _this.bitoffset = 0;
379
396
  }
380
397
  }
381
398
  }
@@ -384,7 +401,7 @@ export function AND(_this, xor_key, start, end, consume) {
384
401
  }
385
402
  }
386
403
  }
387
- export function OR(_this, xor_key, start, end, consume) {
404
+ export function OR(_this, or_key, start, end, consume) {
388
405
  const input = _this.data;
389
406
  if ((end || 0) > _this.size) {
390
407
  if (_this.strict == false) {
@@ -395,27 +412,29 @@ export function OR(_this, xor_key, start, end, consume) {
395
412
  throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
396
413
  }
397
414
  }
398
- if (typeof xor_key == "number") {
415
+ if (typeof or_key == "number") {
399
416
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
400
- input[i] = input[i] | (xor_key & 0xff);
417
+ input[i] = input[i] | (or_key & 0xff);
401
418
  if (consume) {
402
419
  _this.offset = i;
420
+ _this.bitoffset = 0;
403
421
  }
404
422
  }
405
423
  }
406
424
  else {
407
- if (_this.isBufferOrUint8Array(xor_key)) {
425
+ if (_this.isBufferOrUint8Array(or_key)) {
408
426
  let number = -1;
409
427
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
410
- if (number != xor_key.length - 1) {
428
+ if (number != or_key.length - 1) {
411
429
  number = number + 1;
412
430
  }
413
431
  else {
414
432
  number = 0;
415
433
  }
416
- input[i] = input[i] | xor_key[number];
434
+ input[i] = input[i] | or_key[number];
417
435
  if (consume) {
418
436
  _this.offset = i;
437
+ _this.bitoffset = 0;
419
438
  }
420
439
  }
421
440
  }
@@ -440,6 +459,7 @@ export function XOR(_this, xor_key, start, end, consume) {
440
459
  input[i] = input[i] ^ (xor_key & 0xff);
441
460
  if (consume) {
442
461
  _this.offset = i;
462
+ _this.bitoffset = 0;
443
463
  }
444
464
  }
445
465
  }
@@ -456,6 +476,7 @@ export function XOR(_this, xor_key, start, end, consume) {
456
476
  input[i] = input[i] ^ xor_key[number];
457
477
  if (consume) {
458
478
  _this.offset = i;
479
+ _this.bitoffset = 0;
459
480
  }
460
481
  }
461
482
  }
@@ -478,10 +499,12 @@ export function NOT(_this, start, end, consume) {
478
499
  _this.data[i] = ~_this.data[i];
479
500
  if (consume) {
480
501
  _this.offset = i;
502
+ _this.bitoffset = 0;
481
503
  }
482
504
  }
483
505
  }
484
- export function LSHIFT(_this, value, start, end, consume) {
506
+ export function LSHIFT(_this, shift_key, start, end, consume) {
507
+ const input = _this.data;
485
508
  if ((end || 0) > _this.size) {
486
509
  if (_this.strict == false) {
487
510
  _this.extendArray((end || 0) - _this.size);
@@ -491,14 +514,39 @@ export function LSHIFT(_this, value, start, end, consume) {
491
514
  throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
492
515
  }
493
516
  }
494
- for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
495
- _this.data[i] = _this.data[i] << value;
496
- if (consume) {
497
- _this.offset = i;
517
+ if (typeof shift_key == "number") {
518
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
519
+ input[i] = input[i] << shift_key;
520
+ if (consume) {
521
+ _this.offset = i;
522
+ _this.bitoffset = 0;
523
+ }
524
+ }
525
+ }
526
+ else {
527
+ if (_this.isBufferOrUint8Array(shift_key)) {
528
+ let number = -1;
529
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
530
+ if (number != shift_key.length - 1) {
531
+ number = number + 1;
532
+ }
533
+ else {
534
+ number = 0;
535
+ }
536
+ input[i] = input[i] << shift_key[number];
537
+ if (consume) {
538
+ _this.offset = i;
539
+ _this.bitoffset = 0;
540
+ }
541
+ }
542
+ }
543
+ else {
544
+ throw new Error("XOR key must be a byte value, string, Uint8Array or Buffer");
498
545
  }
499
546
  }
500
547
  }
501
- export function RSHIFT(_this, value, start, end, consume) {
548
+ export function RSHIFT(_this, shift_key, start, end, consume) {
549
+ const input = _this.data;
502
550
  if ((end || 0) > _this.size) {
503
551
  if (_this.strict == false) {
504
552
  _this.extendArray((end || 0) - _this.size);
@@ -508,14 +556,39 @@ export function RSHIFT(_this, value, start, end, consume) {
508
556
  throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
509
557
  }
510
558
  }
511
- for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
512
- _this.data[i] = _this.data[i] >> value;
513
- if (consume) {
514
- _this.offset = i;
559
+ if (typeof shift_key == "number") {
560
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
561
+ input[i] = input[i] >> shift_key;
562
+ if (consume) {
563
+ _this.offset = i;
564
+ _this.bitoffset = 0;
565
+ }
566
+ }
567
+ }
568
+ else {
569
+ if (_this.isBufferOrUint8Array(shift_key)) {
570
+ let number = -1;
571
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
572
+ if (number != shift_key.length - 1) {
573
+ number = number + 1;
574
+ }
575
+ else {
576
+ number = 0;
577
+ }
578
+ input[i] = input[i] >> shift_key[number];
579
+ if (consume) {
580
+ _this.offset = i;
581
+ _this.bitoffset = 0;
582
+ }
583
+ }
584
+ }
585
+ else {
586
+ throw new Error("XOR key must be a byte value, string, Uint8Array or Buffer");
515
587
  }
516
588
  }
517
589
  }
518
- export function ADD(_this, value, start, end, consume) {
590
+ export function ADD(_this, add_key, start, end, consume) {
591
+ const input = _this.data;
519
592
  if ((end || 0) > _this.size) {
520
593
  if (_this.strict == false) {
521
594
  _this.extendArray((end || 0) - _this.size);
@@ -525,10 +598,34 @@ export function ADD(_this, value, start, end, consume) {
525
598
  throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
526
599
  }
527
600
  }
528
- for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
529
- _this.data[i] += value;
530
- if (consume) {
531
- _this.offset = i;
601
+ if (typeof add_key == "number") {
602
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
603
+ input[i] = input[i] + add_key;
604
+ if (consume) {
605
+ _this.offset = i;
606
+ _this.bitoffset = 0;
607
+ }
608
+ }
609
+ }
610
+ else {
611
+ if (_this.isBufferOrUint8Array(add_key)) {
612
+ let number = -1;
613
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
614
+ if (number != add_key.length - 1) {
615
+ number = number + 1;
616
+ }
617
+ else {
618
+ number = 0;
619
+ }
620
+ input[i] = input[i] + add_key[number];
621
+ if (consume) {
622
+ _this.offset = i;
623
+ _this.bitoffset = 0;
624
+ }
625
+ }
626
+ }
627
+ else {
628
+ throw new Error("XOR key must be a byte value, string, Uint8Array or Buffer");
532
629
  }
533
630
  }
534
631
  }
@@ -654,11 +751,13 @@ export function wbyte(_this, value, unsigned) {
654
751
  }
655
752
  _this.data[_this.offset] = (unsigned == undefined || unsigned == false) ? value : value & 0xFF;
656
753
  _this.offset += 1;
754
+ _this.bitoffset = 0;
657
755
  }
658
756
  export function rbyte(_this, unsigned) {
659
757
  _this.check_size(1);
660
758
  var read = _this.data[_this.offset];
661
759
  _this.offset += 1;
760
+ _this.bitoffset = 0;
662
761
  if (unsigned == true) {
663
762
  return read & 0xFF;
664
763
  }
@@ -691,6 +790,7 @@ export function wint16(_this, value, unsigned, endian) {
691
790
  _this.data[_this.offset + 1] = (unsigned == undefined || unsigned == false) ? value : value & 0xff;
692
791
  }
693
792
  _this.offset += 2;
793
+ _this.bitoffset = 0;
694
794
  }
695
795
  export function rint16(_this, unsigned, endian) {
696
796
  _this.check_size(2);
@@ -702,6 +802,7 @@ export function rint16(_this, unsigned, endian) {
702
802
  read = (_this.data[_this.offset] << 8) | _this.data[_this.offset + 1];
703
803
  }
704
804
  _this.offset += 2;
805
+ _this.bitoffset = 0;
705
806
  if (unsigned == undefined || unsigned == false) {
706
807
  return read & 0x8000 ? -(0x10000 - read) : read;
707
808
  }
@@ -737,6 +838,8 @@ export function rhalffloat(_this, endian) {
737
838
  // Normalized number
738
839
  floatValue = (sign === 0 ? 1 : -1) * Math.pow(2, exponent - 15) * (1 + fraction / 0x0400);
739
840
  }
841
+ _this.offset += 2;
842
+ _this.bitoffset = 0;
740
843
  return floatValue;
741
844
  }
742
845
  export function whalffloat(_this, value, endian) {
@@ -780,6 +883,7 @@ export function whalffloat(_this, value, endian) {
780
883
  _this.data[_this.offset + 1] = halfFloatBits & 0xFF;
781
884
  }
782
885
  _this.offset += 2;
886
+ _this.bitoffset = 0;
783
887
  }
784
888
  export function wint32(_this, value, unsigned, endian) {
785
889
  _this.check_size(4, 0);
@@ -810,6 +914,7 @@ export function wint32(_this, value, unsigned, endian) {
810
914
  _this.data[_this.offset + 3] = (unsigned == undefined || unsigned == false) ? value : value & 0xFF;
811
915
  }
812
916
  _this.offset += 4;
917
+ _this.bitoffset = 0;
813
918
  }
814
919
  export function rint32(_this, unsigned, endian) {
815
920
  _this.check_size(4);
@@ -821,6 +926,7 @@ export function rint32(_this, unsigned, endian) {
821
926
  read = (_this.data[_this.offset] << 24) | (_this.data[_this.offset + 1] << 16) | (_this.data[_this.offset + 2] << 8) | _this.data[_this.offset + 3];
822
927
  }
823
928
  _this.offset += 4;
929
+ _this.bitoffset = 0;
824
930
  if (unsigned == undefined || unsigned == false) {
825
931
  return read;
826
932
  }
@@ -850,6 +956,8 @@ export function rfloat(_this, endian) {
850
956
  // Normalized number
851
957
  floatValue = Math.pow(-1, isNegative) * Math.pow(2, exponent - 127) * (1 + fraction / Math.pow(2, 23));
852
958
  }
959
+ _this.offset += 4;
960
+ _this.bitoffset = 0;
853
961
  return floatValue;
854
962
  }
855
963
  export function wfloat(_this, value, endian) {
@@ -872,6 +980,7 @@ export function wfloat(_this, value, endian) {
872
980
  shift += 8;
873
981
  }
874
982
  _this.offset += 4;
983
+ _this.bitoffset = 0;
875
984
  }
876
985
  export function rint64(_this, unsigned, endian) {
877
986
  _this.check_size(8);
@@ -886,10 +995,6 @@ export function rint64(_this, unsigned, endian) {
886
995
  if (value & (BigInt(1) << BigInt(63))) {
887
996
  value -= BigInt(1) << BigInt(64);
888
997
  }
889
- return value;
890
- }
891
- else {
892
- return value;
893
998
  }
894
999
  }
895
1000
  else {
@@ -901,12 +1006,11 @@ export function rint64(_this, unsigned, endian) {
901
1006
  if (value & (BigInt(1) << BigInt(63))) {
902
1007
  value -= BigInt(1) << BigInt(64);
903
1008
  }
904
- return value;
905
- }
906
- else {
907
- return value;
908
1009
  }
909
1010
  }
1011
+ _this.offset += 8;
1012
+ _this.bitoffset = 0;
1013
+ return value;
910
1014
  }
911
1015
  export function wint64(_this, value, unsigned, endian) {
912
1016
  _this.check_size(8, 0);
@@ -960,6 +1064,7 @@ export function wint64(_this, value, unsigned, endian) {
960
1064
  }
961
1065
  }
962
1066
  _this.offset += 8;
1067
+ _this.bitoffset = 0;
963
1068
  }
964
1069
  export function wdfloat(_this, value, endian) {
965
1070
  _this.check_size(8, 0);
@@ -982,6 +1087,7 @@ export function wdfloat(_this, value, endian) {
982
1087
  }
983
1088
  }
984
1089
  _this.offset += 8;
1090
+ _this.bitoffset = 0;
985
1091
  }
986
1092
  export function rdfloat(_this, endian) {
987
1093
  _this.check_size(8);
@@ -1011,6 +1117,8 @@ export function rdfloat(_this, endian) {
1011
1117
  // Normalized number
1012
1118
  floatValue = (sign == 0n ? 1 : -1) * Math.pow(2, exponent) * (1 + fraction);
1013
1119
  }
1120
+ _this.offset += 8;
1121
+ _this.bitoffset = 0;
1014
1122
  return floatValue;
1015
1123
  }
1016
1124
  export function rstring(_this, options) {
@@ -1189,6 +1297,7 @@ export function wstring(_this, string, options) {
1189
1297
  }
1190
1298
  }
1191
1299
  _this.offset += totalLength;
1300
+ _this.bitoffset = 0;
1192
1301
  }
1193
1302
  else if (stringType == 'pascal' || stringType == 'wide-pascal') {
1194
1303
  if (encoding == undefined) {
@@ -1254,6 +1363,7 @@ export function wstring(_this, string, options) {
1254
1363
  }
1255
1364
  }
1256
1365
  _this.offset += totalLength;
1366
+ _this.bitoffset = 0;
1257
1367
  }
1258
1368
  else {
1259
1369
  throw new Error('Unsupported string type: ' + stringType);