bireader 1.0.35 → 1.0.38

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.
package/lib/cjs/index.js CHANGED
@@ -1,22 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.biwriter = exports.bireader = exports.wstring = exports.rstring = exports.rdfloat = exports.wdfloat = exports.wint64 = exports.rint64 = exports.wfloat = exports.rfloat = exports.rint32 = exports.wint32 = exports.whalffloat = exports.rhalffloat = exports.rint16 = exports.wint16 = exports.rbyte = exports.wbyte = exports.rbit = exports.wbit = exports.ADD = exports.RSHIFT = exports.LSHIFT = exports.NOT = exports.XOR = exports.OR = exports.AND = exports.hexDump = exports.addData = exports.remove = exports.goto = exports.skip = exports.checkSize = exports.extendarray = exports.arraybuffcheck = exports.buffcheck = exports.check_size = exports.isBuffer = void 0;
3
+ exports.biwriter = exports.bireader = void 0;
4
4
  function isBuffer(obj) {
5
5
  return buffcheck(obj);
6
6
  }
7
- exports.isBuffer = isBuffer;
8
7
  function check_size(_this, write_bytes, write_bit, offset) {
9
8
  return checkSize(_this, write_bytes || 0, write_bit || 0, offset || _this.offset);
10
9
  }
11
- exports.check_size = check_size;
12
10
  function buffcheck(obj) {
13
11
  return (typeof Buffer !== 'undefined' && obj instanceof Buffer);
14
12
  }
15
- exports.buffcheck = buffcheck;
16
13
  function arraybuffcheck(_this, obj) {
17
14
  return obj instanceof Uint8Array || isBuffer(obj);
18
15
  }
19
- exports.arraybuffcheck = arraybuffcheck;
20
16
  function extendarray(_this, to_padd) {
21
17
  if ((typeof Buffer !== 'undefined' && _this.data instanceof Buffer)) {
22
18
  var paddbuffer = Buffer.alloc(to_padd);
@@ -27,7 +23,6 @@ function extendarray(_this, to_padd) {
27
23
  _this.data = new Uint8Array([..._this.data, ...addArray]);
28
24
  }
29
25
  }
30
- exports.extendarray = extendarray;
31
26
  function checkSize(_this, write_bytes, write_bit, offset) {
32
27
  const bits = (write_bit || 0) + _this.bitoffset;
33
28
  var new_off = (offset || _this.offset);
@@ -51,7 +46,6 @@ function checkSize(_this, write_bytes, write_bit, offset) {
51
46
  //start read location
52
47
  return new_off;
53
48
  }
54
- exports.checkSize = checkSize;
55
49
  function skip(_this, bytes, bits) {
56
50
  var new_size = (((bytes || 0) + _this.offset) + Math.ceil((_this.bitoffset + (bits || 0)) / 8));
57
51
  if (bits && bits < 0) {
@@ -66,16 +60,29 @@ function skip(_this, bytes, bits) {
66
60
  throw new Error("\x1b[33m[Strict mode]\x1b[0m: Seek of range of data: seek " + new_size + " of " + _this.size);
67
61
  }
68
62
  }
69
- if (bits && bits < 0) {
70
- _this.bitoffset = ((_this.bitoffset + (bits || 0) % 8) + 8) % 8;
71
- _this.offset = new_size;
63
+ // Adjust byte offset based on bit overflow
64
+ _this.offset += Math.floor((_this.bitoffset + (bits || 0)) / 8);
65
+ // Adjust bit offset
66
+ _this.bitoffset = (_this.bitoffset + (bits || 0) + 64) % 8;
67
+ // Adjust byte offset based on byte overflow
68
+ _this.offset += bytes;
69
+ // Ensure bit offset stays between 0-7
70
+ _this.bitoffset = Math.min(Math.max(_this.bitoffset, 0), 7);
71
+ // Ensure offset doesn't go negative
72
+ _this.offset = Math.max(_this.offset, 0);
73
+ }
74
+ function align(_this, n) {
75
+ var a = _this.offset % n;
76
+ if (a) {
77
+ _this.skip(n - a);
72
78
  }
73
- else {
74
- _this.bitoffset += (bits || 0) % 8;
75
- _this.offset += (bytes || 0);
79
+ }
80
+ function alignRev(_this, n) {
81
+ var a = _this.offset % n;
82
+ if (a) {
83
+ _this.skip(a * -1);
76
84
  }
77
85
  }
78
- exports.skip = skip;
79
86
  function goto(_this, byte, bit) {
80
87
  const new_size = (byte + Math.ceil((bit || 0) / 8));
81
88
  if (new_size > _this.size) {
@@ -90,7 +97,6 @@ function goto(_this, byte, bit) {
90
97
  _this.offset = byte;
91
98
  _this.bitoffset = (bit || 0) % 8;
92
99
  }
93
- exports.goto = goto;
94
100
  function remove(_this, startOffset, endOffset, consume, remove, fillValue) {
95
101
  const new_start = Math.abs(startOffset || 0);
96
102
  const new_offset = (endOffset || _this.offset);
@@ -144,7 +150,6 @@ function remove(_this, startOffset, endOffset, consume, remove, fillValue) {
144
150
  }
145
151
  return data_removed;
146
152
  }
147
- exports.remove = remove;
148
153
  function addData(_this, data, consume, offset, repalce) {
149
154
  if (_this.strict == true) {
150
155
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
@@ -203,7 +208,6 @@ function addData(_this, data, consume, offset, repalce) {
203
208
  _this.bitoffset = 0;
204
209
  }
205
210
  }
206
- exports.addData = addData;
207
211
  function hexDump(_this, options) {
208
212
  var length = options && options.length;
209
213
  var startByte = options && options.startByte;
@@ -386,7 +390,6 @@ function hexDump(_this, options) {
386
390
  }
387
391
  console.log(rows.join("\n"));
388
392
  }
389
- exports.hexDump = hexDump;
390
393
  function AND(_this, and_key, start, end, consume) {
391
394
  const input = _this.data;
392
395
  if ((end || 0) > _this.size) {
@@ -408,7 +411,7 @@ function AND(_this, and_key, start, end, consume) {
408
411
  }
409
412
  }
410
413
  else {
411
- if (_this.isBufferOrUint8Array(and_key)) {
414
+ if (arraybuffcheck(_this, and_key)) {
412
415
  let number = -1;
413
416
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
414
417
  if (number != and_key.length - 1) {
@@ -429,7 +432,6 @@ function AND(_this, and_key, start, end, consume) {
429
432
  }
430
433
  }
431
434
  }
432
- exports.AND = AND;
433
435
  function OR(_this, or_key, start, end, consume) {
434
436
  const input = _this.data;
435
437
  if ((end || 0) > _this.size) {
@@ -451,7 +453,7 @@ function OR(_this, or_key, start, end, consume) {
451
453
  }
452
454
  }
453
455
  else {
454
- if (_this.isBufferOrUint8Array(or_key)) {
456
+ if (arraybuffcheck(_this, or_key)) {
455
457
  let number = -1;
456
458
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
457
459
  if (number != or_key.length - 1) {
@@ -472,7 +474,6 @@ function OR(_this, or_key, start, end, consume) {
472
474
  }
473
475
  }
474
476
  }
475
- exports.OR = OR;
476
477
  function XOR(_this, xor_key, start, end, consume) {
477
478
  const input = _this.data;
478
479
  if ((end || 0) > _this.size) {
@@ -494,7 +495,7 @@ function XOR(_this, xor_key, start, end, consume) {
494
495
  }
495
496
  }
496
497
  else {
497
- if (_this.isBufferOrUint8Array(xor_key)) {
498
+ if (arraybuffcheck(_this, xor_key)) {
498
499
  let number = -1;
499
500
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
500
501
  if (number != xor_key.length - 1) {
@@ -515,7 +516,6 @@ function XOR(_this, xor_key, start, end, consume) {
515
516
  }
516
517
  }
517
518
  }
518
- exports.XOR = XOR;
519
519
  function NOT(_this, start, end, consume) {
520
520
  if ((end || 0) > _this.size) {
521
521
  if (_this.strict == false) {
@@ -534,7 +534,6 @@ function NOT(_this, start, end, consume) {
534
534
  }
535
535
  }
536
536
  }
537
- exports.NOT = NOT;
538
537
  function LSHIFT(_this, shift_key, start, end, consume) {
539
538
  const input = _this.data;
540
539
  if ((end || 0) > _this.size) {
@@ -556,7 +555,7 @@ function LSHIFT(_this, shift_key, start, end, consume) {
556
555
  }
557
556
  }
558
557
  else {
559
- if (_this.isBufferOrUint8Array(shift_key)) {
558
+ if (arraybuffcheck(_this, shift_key)) {
560
559
  let number = -1;
561
560
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
562
561
  if (number != shift_key.length - 1) {
@@ -577,7 +576,6 @@ function LSHIFT(_this, shift_key, start, end, consume) {
577
576
  }
578
577
  }
579
578
  }
580
- exports.LSHIFT = LSHIFT;
581
579
  function RSHIFT(_this, shift_key, start, end, consume) {
582
580
  const input = _this.data;
583
581
  if ((end || 0) > _this.size) {
@@ -599,7 +597,7 @@ function RSHIFT(_this, shift_key, start, end, consume) {
599
597
  }
600
598
  }
601
599
  else {
602
- if (_this.isBufferOrUint8Array(shift_key)) {
600
+ if (arraybuffcheck(_this, shift_key)) {
603
601
  let number = -1;
604
602
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
605
603
  if (number != shift_key.length - 1) {
@@ -620,7 +618,6 @@ function RSHIFT(_this, shift_key, start, end, consume) {
620
618
  }
621
619
  }
622
620
  }
623
- exports.RSHIFT = RSHIFT;
624
621
  function ADD(_this, add_key, start, end, consume) {
625
622
  const input = _this.data;
626
623
  if ((end || 0) > _this.size) {
@@ -642,7 +639,7 @@ function ADD(_this, add_key, start, end, consume) {
642
639
  }
643
640
  }
644
641
  else {
645
- if (_this.isBufferOrUint8Array(add_key)) {
642
+ if (arraybuffcheck(_this, add_key)) {
646
643
  let number = -1;
647
644
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
648
645
  if (number != add_key.length - 1) {
@@ -663,7 +660,6 @@ function ADD(_this, add_key, start, end, consume) {
663
660
  }
664
661
  }
665
662
  }
666
- exports.ADD = ADD;
667
663
  function wbit(_this, value, bits, unsigned, endian) {
668
664
  if (value == undefined) {
669
665
  throw new Error('Must supply value.');
@@ -724,7 +720,6 @@ function wbit(_this, value, bits, unsigned, endian) {
724
720
  _this.offset = _this.offset + Math.floor(((bits) + _this.bitoffset) / 8); //end byte
725
721
  _this.bitoffset = ((bits) + _this.bitoffset) % 8;
726
722
  }
727
- exports.wbit = wbit;
728
723
  function rbit(_this, bits, unsigned, endian) {
729
724
  if (bits == undefined || typeof bits != "number") {
730
725
  throw new Error("Enter number of bits to read");
@@ -769,7 +764,6 @@ function rbit(_this, bits, unsigned, endian) {
769
764
  }
770
765
  return value;
771
766
  }
772
- exports.rbit = rbit;
773
767
  function wbyte(_this, value, unsigned) {
774
768
  check_size(_this, 1, 0);
775
769
  if (unsigned == true) {
@@ -790,7 +784,6 @@ function wbyte(_this, value, unsigned) {
790
784
  _this.offset += 1;
791
785
  _this.bitoffset = 0;
792
786
  }
793
- exports.wbyte = wbyte;
794
787
  function rbyte(_this, unsigned) {
795
788
  check_size(_this, 1);
796
789
  var read = _this.data[_this.offset];
@@ -803,7 +796,6 @@ function rbyte(_this, unsigned) {
803
796
  return read > 127 ? read - 256 : read;
804
797
  }
805
798
  }
806
- exports.rbyte = rbyte;
807
799
  function wint16(_this, value, unsigned, endian) {
808
800
  check_size(_this, 2, 0);
809
801
  if (unsigned == true) {
@@ -831,7 +823,6 @@ function wint16(_this, value, unsigned, endian) {
831
823
  _this.offset += 2;
832
824
  _this.bitoffset = 0;
833
825
  }
834
- exports.wint16 = wint16;
835
826
  function rint16(_this, unsigned, endian) {
836
827
  check_size(_this, 2);
837
828
  var read;
@@ -850,7 +841,6 @@ function rint16(_this, unsigned, endian) {
850
841
  return read & 0xFFFF;
851
842
  }
852
843
  }
853
- exports.rint16 = rint16;
854
844
  function rhalffloat(_this, endian) {
855
845
  var uint16Value = _this.readInt16(true, (endian != undefined ? endian : _this.endian));
856
846
  const sign = (uint16Value & 0x8000) >> 15;
@@ -880,7 +870,6 @@ function rhalffloat(_this, endian) {
880
870
  }
881
871
  return floatValue;
882
872
  }
883
- exports.rhalffloat = rhalffloat;
884
873
  function whalffloat(_this, value, endian) {
885
874
  check_size(_this, 2, 0);
886
875
  const maxValue = 65504;
@@ -924,7 +913,6 @@ function whalffloat(_this, value, endian) {
924
913
  _this.offset += 2;
925
914
  _this.bitoffset = 0;
926
915
  }
927
- exports.whalffloat = whalffloat;
928
916
  function wint32(_this, value, unsigned, endian) {
929
917
  check_size(_this, 4, 0);
930
918
  if (unsigned == true) {
@@ -956,7 +944,6 @@ function wint32(_this, value, unsigned, endian) {
956
944
  _this.offset += 4;
957
945
  _this.bitoffset = 0;
958
946
  }
959
- exports.wint32 = wint32;
960
947
  function rint32(_this, unsigned, endian) {
961
948
  check_size(_this, 4);
962
949
  var read;
@@ -975,7 +962,6 @@ function rint32(_this, unsigned, endian) {
975
962
  return read >>> 0;
976
963
  }
977
964
  }
978
- exports.rint32 = rint32;
979
965
  function rfloat(_this, endian) {
980
966
  var uint32Value = _this.readInt32(true, (endian == undefined ? _this.endian : endian));
981
967
  // Check if the value is negative (i.e., the most significant bit is set)
@@ -999,7 +985,6 @@ function rfloat(_this, endian) {
999
985
  }
1000
986
  return floatValue;
1001
987
  }
1002
- exports.rfloat = rfloat;
1003
988
  function wfloat(_this, value, endian) {
1004
989
  check_size(_this, 4, 0);
1005
990
  const maxValue = 3.402823466e+38;
@@ -1022,7 +1007,6 @@ function wfloat(_this, value, endian) {
1022
1007
  _this.offset += 4;
1023
1008
  _this.bitoffset = 0;
1024
1009
  }
1025
- exports.wfloat = wfloat;
1026
1010
  function rint64(_this, unsigned, endian) {
1027
1011
  check_size(_this, 8);
1028
1012
  // Convert the byte array to a BigInt
@@ -1053,7 +1037,6 @@ function rint64(_this, unsigned, endian) {
1053
1037
  _this.bitoffset = 0;
1054
1038
  return value;
1055
1039
  }
1056
- exports.rint64 = rint64;
1057
1040
  function wint64(_this, value, unsigned, endian) {
1058
1041
  check_size(_this, 8, 0);
1059
1042
  if (unsigned == true) {
@@ -1108,7 +1091,6 @@ function wint64(_this, value, unsigned, endian) {
1108
1091
  _this.offset += 8;
1109
1092
  _this.bitoffset = 0;
1110
1093
  }
1111
- exports.wint64 = wint64;
1112
1094
  function wdfloat(_this, value, endian) {
1113
1095
  check_size(_this, 8, 0);
1114
1096
  const maxValue = 1.7976931348623158e308;
@@ -1132,7 +1114,6 @@ function wdfloat(_this, value, endian) {
1132
1114
  _this.offset += 8;
1133
1115
  _this.bitoffset = 0;
1134
1116
  }
1135
- exports.wdfloat = wdfloat;
1136
1117
  function rdfloat(_this, endian) {
1137
1118
  var uint64Value = _this.readInt64(true, (endian == undefined ? _this.endian : endian));
1138
1119
  const sign = (uint64Value & 0x8000000000000000n) >> 63n;
@@ -1162,7 +1143,6 @@ function rdfloat(_this, endian) {
1162
1143
  }
1163
1144
  return floatValue;
1164
1145
  }
1165
- exports.rdfloat = rdfloat;
1166
1146
  function rstring(_this, options) {
1167
1147
  var length = options && options.length;
1168
1148
  var stringType = options && options.stringType || 'utf-8';
@@ -1285,7 +1265,6 @@ function rstring(_this, options) {
1285
1265
  throw new Error('Unsupported string type: ' + stringType);
1286
1266
  }
1287
1267
  }
1288
- exports.rstring = rstring;
1289
1268
  function wstring(_this, string, options) {
1290
1269
  var length = options && options.length;
1291
1270
  var stringType = options && options.stringType || 'utf-8';
@@ -1412,7 +1391,6 @@ function wstring(_this, string, options) {
1412
1391
  throw new Error('Unsupported string type: ' + stringType);
1413
1392
  }
1414
1393
  }
1415
- exports.wstring = wstring;
1416
1394
  /**
1417
1395
  * Binary reader, includes bitfields and strings
1418
1396
  *
@@ -1540,6 +1518,26 @@ class bireader {
1540
1518
  // move from current position
1541
1519
  //
1542
1520
  /**
1521
+ * Aligns current byte position
1522
+ *
1523
+ * Note: Will extend array if strict mode is off and outside of max size
1524
+ *
1525
+ * @param {number} number - Byte to align
1526
+ */
1527
+ align(number) {
1528
+ return align(this, number);
1529
+ }
1530
+ /**
1531
+ * Reverse aligns current byte position
1532
+ *
1533
+ * Note: Will extend array if strict mode is off and outside of max size
1534
+ *
1535
+ * @param {number} number - Byte to align
1536
+ */
1537
+ alignRev(number) {
1538
+ return alignRev(this, number);
1539
+ }
1540
+ /**
1543
1541
  * Offset current byte or bit position
1544
1542
  * Note: Will extend array if strict mode is off and outside of max size
1545
1543
  *
@@ -4489,6 +4487,14 @@ class bireader {
4489
4487
  return wbyte(this, value, unsigned);
4490
4488
  }
4491
4489
  /**
4490
+ * Write unsigned byte
4491
+ *
4492
+ * @param {number} value - value as int
4493
+ */
4494
+ writeUByte(value) {
4495
+ return wbyte(this, value, true);
4496
+ }
4497
+ /**
4492
4498
  * Read byte
4493
4499
  *
4494
4500
  * @param {boolean} unsigned - if value is unsigned or not
@@ -4554,6 +4560,15 @@ class bireader {
4554
4560
  return wint16(this, value, unsigned, endian);
4555
4561
  }
4556
4562
  /**
4563
+ * Write unsigned int16
4564
+ *
4565
+ * @param {number} value - value as int
4566
+ * @param {string} endian - ``big`` or ``little`
4567
+ */
4568
+ writeUInt16(value, endian) {
4569
+ return wint16(this, value, true, endian);
4570
+ }
4571
+ /**
4557
4572
  * Read short
4558
4573
  *
4559
4574
  * @param {boolean} unsigned - if value is unsigned or not
@@ -4862,6 +4877,15 @@ class bireader {
4862
4877
  return wint32(this, value, unsigned, endian);
4863
4878
  }
4864
4879
  /**
4880
+ * Write unsigned int32
4881
+ *
4882
+ * @param {number} value - value as int
4883
+ * @param {string} endian - ``big`` or ``little``
4884
+ */
4885
+ writeUInt32(value, endian) {
4886
+ return wint32(this, value, true, endian);
4887
+ }
4888
+ /**
4865
4889
  * Read 32 bit integer
4866
4890
  *
4867
4891
  * @param {boolean} unsigned - if value is unsigned or not
@@ -5957,7 +5981,28 @@ class biwriter {
5957
5981
  // move from current position
5958
5982
  //
5959
5983
  /**
5984
+ * Aligns current byte position
5985
+ *
5986
+ * Note: Will extend array if strict mode is off and outside of max size
5987
+ *
5988
+ * @param {number} number - Byte to align
5989
+ */
5990
+ align(number) {
5991
+ return align(this, number);
5992
+ }
5993
+ /**
5994
+ * Reverse aligns current byte position
5995
+ *
5996
+ * Note: Will extend array if strict mode is off and outside of max size
5997
+ *
5998
+ * @param {number} number - Byte to align
5999
+ */
6000
+ alignRev(number) {
6001
+ return alignRev(this, number);
6002
+ }
6003
+ /**
5960
6004
  * Offset current byte or bit position
6005
+ *
5961
6006
  * Note: Will extend array if strict mode is off and outside of max size
5962
6007
  *
5963
6008
  * @param {number} bytes - Bytes to skip
@@ -9171,6 +9216,14 @@ class biwriter {
9171
9216
  return rbyte(this, unsigned);
9172
9217
  }
9173
9218
  /**
9219
+ * Read unsigned byte
9220
+ *
9221
+ * @returns number
9222
+ */
9223
+ readUByte() {
9224
+ return rbyte(this, true);
9225
+ }
9226
+ /**
9174
9227
  * Write byte
9175
9228
  *
9176
9229
  * @param {number} value - value as int