bireader 1.0.36 → 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,11 +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
- const total_bits = (((bytes || 0) + _this.offset) * 8) + (bits || 0) + _this.bitoffset;
70
- _this.bitoffset = total_bits % 8;
71
- _this.offset = Math.floor(total_bits / 8);
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);
78
+ }
79
+ }
80
+ function alignRev(_this, n) {
81
+ var a = _this.offset % n;
82
+ if (a) {
83
+ _this.skip(a * -1);
84
+ }
72
85
  }
73
- exports.skip = skip;
74
86
  function goto(_this, byte, bit) {
75
87
  const new_size = (byte + Math.ceil((bit || 0) / 8));
76
88
  if (new_size > _this.size) {
@@ -85,7 +97,6 @@ function goto(_this, byte, bit) {
85
97
  _this.offset = byte;
86
98
  _this.bitoffset = (bit || 0) % 8;
87
99
  }
88
- exports.goto = goto;
89
100
  function remove(_this, startOffset, endOffset, consume, remove, fillValue) {
90
101
  const new_start = Math.abs(startOffset || 0);
91
102
  const new_offset = (endOffset || _this.offset);
@@ -139,7 +150,6 @@ function remove(_this, startOffset, endOffset, consume, remove, fillValue) {
139
150
  }
140
151
  return data_removed;
141
152
  }
142
- exports.remove = remove;
143
153
  function addData(_this, data, consume, offset, repalce) {
144
154
  if (_this.strict == true) {
145
155
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
@@ -198,7 +208,6 @@ function addData(_this, data, consume, offset, repalce) {
198
208
  _this.bitoffset = 0;
199
209
  }
200
210
  }
201
- exports.addData = addData;
202
211
  function hexDump(_this, options) {
203
212
  var length = options && options.length;
204
213
  var startByte = options && options.startByte;
@@ -381,7 +390,6 @@ function hexDump(_this, options) {
381
390
  }
382
391
  console.log(rows.join("\n"));
383
392
  }
384
- exports.hexDump = hexDump;
385
393
  function AND(_this, and_key, start, end, consume) {
386
394
  const input = _this.data;
387
395
  if ((end || 0) > _this.size) {
@@ -403,7 +411,7 @@ function AND(_this, and_key, start, end, consume) {
403
411
  }
404
412
  }
405
413
  else {
406
- if (_this.isBufferOrUint8Array(and_key)) {
414
+ if (arraybuffcheck(_this, and_key)) {
407
415
  let number = -1;
408
416
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
409
417
  if (number != and_key.length - 1) {
@@ -424,7 +432,6 @@ function AND(_this, and_key, start, end, consume) {
424
432
  }
425
433
  }
426
434
  }
427
- exports.AND = AND;
428
435
  function OR(_this, or_key, start, end, consume) {
429
436
  const input = _this.data;
430
437
  if ((end || 0) > _this.size) {
@@ -446,7 +453,7 @@ function OR(_this, or_key, start, end, consume) {
446
453
  }
447
454
  }
448
455
  else {
449
- if (_this.isBufferOrUint8Array(or_key)) {
456
+ if (arraybuffcheck(_this, or_key)) {
450
457
  let number = -1;
451
458
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
452
459
  if (number != or_key.length - 1) {
@@ -467,7 +474,6 @@ function OR(_this, or_key, start, end, consume) {
467
474
  }
468
475
  }
469
476
  }
470
- exports.OR = OR;
471
477
  function XOR(_this, xor_key, start, end, consume) {
472
478
  const input = _this.data;
473
479
  if ((end || 0) > _this.size) {
@@ -489,7 +495,7 @@ function XOR(_this, xor_key, start, end, consume) {
489
495
  }
490
496
  }
491
497
  else {
492
- if (_this.isBufferOrUint8Array(xor_key)) {
498
+ if (arraybuffcheck(_this, xor_key)) {
493
499
  let number = -1;
494
500
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
495
501
  if (number != xor_key.length - 1) {
@@ -510,7 +516,6 @@ function XOR(_this, xor_key, start, end, consume) {
510
516
  }
511
517
  }
512
518
  }
513
- exports.XOR = XOR;
514
519
  function NOT(_this, start, end, consume) {
515
520
  if ((end || 0) > _this.size) {
516
521
  if (_this.strict == false) {
@@ -529,7 +534,6 @@ function NOT(_this, start, end, consume) {
529
534
  }
530
535
  }
531
536
  }
532
- exports.NOT = NOT;
533
537
  function LSHIFT(_this, shift_key, start, end, consume) {
534
538
  const input = _this.data;
535
539
  if ((end || 0) > _this.size) {
@@ -551,7 +555,7 @@ function LSHIFT(_this, shift_key, start, end, consume) {
551
555
  }
552
556
  }
553
557
  else {
554
- if (_this.isBufferOrUint8Array(shift_key)) {
558
+ if (arraybuffcheck(_this, shift_key)) {
555
559
  let number = -1;
556
560
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
557
561
  if (number != shift_key.length - 1) {
@@ -572,7 +576,6 @@ function LSHIFT(_this, shift_key, start, end, consume) {
572
576
  }
573
577
  }
574
578
  }
575
- exports.LSHIFT = LSHIFT;
576
579
  function RSHIFT(_this, shift_key, start, end, consume) {
577
580
  const input = _this.data;
578
581
  if ((end || 0) > _this.size) {
@@ -594,7 +597,7 @@ function RSHIFT(_this, shift_key, start, end, consume) {
594
597
  }
595
598
  }
596
599
  else {
597
- if (_this.isBufferOrUint8Array(shift_key)) {
600
+ if (arraybuffcheck(_this, shift_key)) {
598
601
  let number = -1;
599
602
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
600
603
  if (number != shift_key.length - 1) {
@@ -615,7 +618,6 @@ function RSHIFT(_this, shift_key, start, end, consume) {
615
618
  }
616
619
  }
617
620
  }
618
- exports.RSHIFT = RSHIFT;
619
621
  function ADD(_this, add_key, start, end, consume) {
620
622
  const input = _this.data;
621
623
  if ((end || 0) > _this.size) {
@@ -637,7 +639,7 @@ function ADD(_this, add_key, start, end, consume) {
637
639
  }
638
640
  }
639
641
  else {
640
- if (_this.isBufferOrUint8Array(add_key)) {
642
+ if (arraybuffcheck(_this, add_key)) {
641
643
  let number = -1;
642
644
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
643
645
  if (number != add_key.length - 1) {
@@ -658,7 +660,6 @@ function ADD(_this, add_key, start, end, consume) {
658
660
  }
659
661
  }
660
662
  }
661
- exports.ADD = ADD;
662
663
  function wbit(_this, value, bits, unsigned, endian) {
663
664
  if (value == undefined) {
664
665
  throw new Error('Must supply value.');
@@ -719,7 +720,6 @@ function wbit(_this, value, bits, unsigned, endian) {
719
720
  _this.offset = _this.offset + Math.floor(((bits) + _this.bitoffset) / 8); //end byte
720
721
  _this.bitoffset = ((bits) + _this.bitoffset) % 8;
721
722
  }
722
- exports.wbit = wbit;
723
723
  function rbit(_this, bits, unsigned, endian) {
724
724
  if (bits == undefined || typeof bits != "number") {
725
725
  throw new Error("Enter number of bits to read");
@@ -764,7 +764,6 @@ function rbit(_this, bits, unsigned, endian) {
764
764
  }
765
765
  return value;
766
766
  }
767
- exports.rbit = rbit;
768
767
  function wbyte(_this, value, unsigned) {
769
768
  check_size(_this, 1, 0);
770
769
  if (unsigned == true) {
@@ -785,7 +784,6 @@ function wbyte(_this, value, unsigned) {
785
784
  _this.offset += 1;
786
785
  _this.bitoffset = 0;
787
786
  }
788
- exports.wbyte = wbyte;
789
787
  function rbyte(_this, unsigned) {
790
788
  check_size(_this, 1);
791
789
  var read = _this.data[_this.offset];
@@ -798,7 +796,6 @@ function rbyte(_this, unsigned) {
798
796
  return read > 127 ? read - 256 : read;
799
797
  }
800
798
  }
801
- exports.rbyte = rbyte;
802
799
  function wint16(_this, value, unsigned, endian) {
803
800
  check_size(_this, 2, 0);
804
801
  if (unsigned == true) {
@@ -826,7 +823,6 @@ function wint16(_this, value, unsigned, endian) {
826
823
  _this.offset += 2;
827
824
  _this.bitoffset = 0;
828
825
  }
829
- exports.wint16 = wint16;
830
826
  function rint16(_this, unsigned, endian) {
831
827
  check_size(_this, 2);
832
828
  var read;
@@ -845,7 +841,6 @@ function rint16(_this, unsigned, endian) {
845
841
  return read & 0xFFFF;
846
842
  }
847
843
  }
848
- exports.rint16 = rint16;
849
844
  function rhalffloat(_this, endian) {
850
845
  var uint16Value = _this.readInt16(true, (endian != undefined ? endian : _this.endian));
851
846
  const sign = (uint16Value & 0x8000) >> 15;
@@ -875,7 +870,6 @@ function rhalffloat(_this, endian) {
875
870
  }
876
871
  return floatValue;
877
872
  }
878
- exports.rhalffloat = rhalffloat;
879
873
  function whalffloat(_this, value, endian) {
880
874
  check_size(_this, 2, 0);
881
875
  const maxValue = 65504;
@@ -919,7 +913,6 @@ function whalffloat(_this, value, endian) {
919
913
  _this.offset += 2;
920
914
  _this.bitoffset = 0;
921
915
  }
922
- exports.whalffloat = whalffloat;
923
916
  function wint32(_this, value, unsigned, endian) {
924
917
  check_size(_this, 4, 0);
925
918
  if (unsigned == true) {
@@ -951,7 +944,6 @@ function wint32(_this, value, unsigned, endian) {
951
944
  _this.offset += 4;
952
945
  _this.bitoffset = 0;
953
946
  }
954
- exports.wint32 = wint32;
955
947
  function rint32(_this, unsigned, endian) {
956
948
  check_size(_this, 4);
957
949
  var read;
@@ -970,7 +962,6 @@ function rint32(_this, unsigned, endian) {
970
962
  return read >>> 0;
971
963
  }
972
964
  }
973
- exports.rint32 = rint32;
974
965
  function rfloat(_this, endian) {
975
966
  var uint32Value = _this.readInt32(true, (endian == undefined ? _this.endian : endian));
976
967
  // Check if the value is negative (i.e., the most significant bit is set)
@@ -994,7 +985,6 @@ function rfloat(_this, endian) {
994
985
  }
995
986
  return floatValue;
996
987
  }
997
- exports.rfloat = rfloat;
998
988
  function wfloat(_this, value, endian) {
999
989
  check_size(_this, 4, 0);
1000
990
  const maxValue = 3.402823466e+38;
@@ -1017,7 +1007,6 @@ function wfloat(_this, value, endian) {
1017
1007
  _this.offset += 4;
1018
1008
  _this.bitoffset = 0;
1019
1009
  }
1020
- exports.wfloat = wfloat;
1021
1010
  function rint64(_this, unsigned, endian) {
1022
1011
  check_size(_this, 8);
1023
1012
  // Convert the byte array to a BigInt
@@ -1048,7 +1037,6 @@ function rint64(_this, unsigned, endian) {
1048
1037
  _this.bitoffset = 0;
1049
1038
  return value;
1050
1039
  }
1051
- exports.rint64 = rint64;
1052
1040
  function wint64(_this, value, unsigned, endian) {
1053
1041
  check_size(_this, 8, 0);
1054
1042
  if (unsigned == true) {
@@ -1103,7 +1091,6 @@ function wint64(_this, value, unsigned, endian) {
1103
1091
  _this.offset += 8;
1104
1092
  _this.bitoffset = 0;
1105
1093
  }
1106
- exports.wint64 = wint64;
1107
1094
  function wdfloat(_this, value, endian) {
1108
1095
  check_size(_this, 8, 0);
1109
1096
  const maxValue = 1.7976931348623158e308;
@@ -1127,7 +1114,6 @@ function wdfloat(_this, value, endian) {
1127
1114
  _this.offset += 8;
1128
1115
  _this.bitoffset = 0;
1129
1116
  }
1130
- exports.wdfloat = wdfloat;
1131
1117
  function rdfloat(_this, endian) {
1132
1118
  var uint64Value = _this.readInt64(true, (endian == undefined ? _this.endian : endian));
1133
1119
  const sign = (uint64Value & 0x8000000000000000n) >> 63n;
@@ -1157,7 +1143,6 @@ function rdfloat(_this, endian) {
1157
1143
  }
1158
1144
  return floatValue;
1159
1145
  }
1160
- exports.rdfloat = rdfloat;
1161
1146
  function rstring(_this, options) {
1162
1147
  var length = options && options.length;
1163
1148
  var stringType = options && options.stringType || 'utf-8';
@@ -1280,7 +1265,6 @@ function rstring(_this, options) {
1280
1265
  throw new Error('Unsupported string type: ' + stringType);
1281
1266
  }
1282
1267
  }
1283
- exports.rstring = rstring;
1284
1268
  function wstring(_this, string, options) {
1285
1269
  var length = options && options.length;
1286
1270
  var stringType = options && options.stringType || 'utf-8';
@@ -1407,7 +1391,6 @@ function wstring(_this, string, options) {
1407
1391
  throw new Error('Unsupported string type: ' + stringType);
1408
1392
  }
1409
1393
  }
1410
- exports.wstring = wstring;
1411
1394
  /**
1412
1395
  * Binary reader, includes bitfields and strings
1413
1396
  *
@@ -1535,6 +1518,26 @@ class bireader {
1535
1518
  // move from current position
1536
1519
  //
1537
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
+ /**
1538
1541
  * Offset current byte or bit position
1539
1542
  * Note: Will extend array if strict mode is off and outside of max size
1540
1543
  *
@@ -4484,6 +4487,14 @@ class bireader {
4484
4487
  return wbyte(this, value, unsigned);
4485
4488
  }
4486
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
+ /**
4487
4498
  * Read byte
4488
4499
  *
4489
4500
  * @param {boolean} unsigned - if value is unsigned or not
@@ -4549,6 +4560,15 @@ class bireader {
4549
4560
  return wint16(this, value, unsigned, endian);
4550
4561
  }
4551
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
+ /**
4552
4572
  * Read short
4553
4573
  *
4554
4574
  * @param {boolean} unsigned - if value is unsigned or not
@@ -4857,6 +4877,15 @@ class bireader {
4857
4877
  return wint32(this, value, unsigned, endian);
4858
4878
  }
4859
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
+ /**
4860
4889
  * Read 32 bit integer
4861
4890
  *
4862
4891
  * @param {boolean} unsigned - if value is unsigned or not
@@ -5952,7 +5981,28 @@ class biwriter {
5952
5981
  // move from current position
5953
5982
  //
5954
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
+ /**
5955
6004
  * Offset current byte or bit position
6005
+ *
5956
6006
  * Note: Will extend array if strict mode is off and outside of max size
5957
6007
  *
5958
6008
  * @param {number} bytes - Bytes to skip
@@ -9166,6 +9216,14 @@ class biwriter {
9166
9216
  return rbyte(this, unsigned);
9167
9217
  }
9168
9218
  /**
9219
+ * Read unsigned byte
9220
+ *
9221
+ * @returns number
9222
+ */
9223
+ readUByte() {
9224
+ return rbyte(this, true);
9225
+ }
9226
+ /**
9169
9227
  * Write byte
9170
9228
  *
9171
9229
  * @param {number} value - value as int