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/esm/index.mjs CHANGED
@@ -1,16 +1,16 @@
1
- export function isBuffer(obj) {
1
+ function isBuffer(obj) {
2
2
  return buffcheck(obj);
3
3
  }
4
- export function check_size(_this, write_bytes, write_bit, offset) {
4
+ function check_size(_this, write_bytes, write_bit, offset) {
5
5
  return checkSize(_this, write_bytes || 0, write_bit || 0, offset || _this.offset);
6
6
  }
7
- export function buffcheck(obj) {
7
+ function buffcheck(obj) {
8
8
  return (typeof Buffer !== 'undefined' && obj instanceof Buffer);
9
9
  }
10
- export function arraybuffcheck(_this, obj) {
10
+ function arraybuffcheck(_this, obj) {
11
11
  return obj instanceof Uint8Array || isBuffer(obj);
12
12
  }
13
- export function extendarray(_this, to_padd) {
13
+ function extendarray(_this, to_padd) {
14
14
  if ((typeof Buffer !== 'undefined' && _this.data instanceof Buffer)) {
15
15
  var paddbuffer = Buffer.alloc(to_padd);
16
16
  _this.data = Buffer.concat([_this.data, paddbuffer]);
@@ -20,7 +20,7 @@ export function extendarray(_this, to_padd) {
20
20
  _this.data = new Uint8Array([..._this.data, ...addArray]);
21
21
  }
22
22
  }
23
- export function checkSize(_this, write_bytes, write_bit, offset) {
23
+ function checkSize(_this, write_bytes, write_bit, offset) {
24
24
  const bits = (write_bit || 0) + _this.bitoffset;
25
25
  var new_off = (offset || _this.offset);
26
26
  var writesize = write_bytes || 0;
@@ -43,7 +43,7 @@ export function checkSize(_this, write_bytes, write_bit, offset) {
43
43
  //start read location
44
44
  return new_off;
45
45
  }
46
- export function skip(_this, bytes, bits) {
46
+ function skip(_this, bytes, bits) {
47
47
  var new_size = (((bytes || 0) + _this.offset) + Math.ceil((_this.bitoffset + (bits || 0)) / 8));
48
48
  if (bits && bits < 0) {
49
49
  new_size = Math.floor(((((bytes || 0) + _this.offset) * 8) + _this.bitoffset + (bits || 0)) / 8);
@@ -57,11 +57,30 @@ export function skip(_this, bytes, bits) {
57
57
  throw new Error("\x1b[33m[Strict mode]\x1b[0m: Seek of range of data: seek " + new_size + " of " + _this.size);
58
58
  }
59
59
  }
60
- const total_bits = (((bytes || 0) + _this.offset) * 8) + (bits || 0) + _this.bitoffset;
61
- _this.bitoffset = total_bits % 8;
62
- _this.offset = Math.floor(total_bits / 8);
60
+ // Adjust byte offset based on bit overflow
61
+ _this.offset += Math.floor((_this.bitoffset + (bits || 0)) / 8);
62
+ // Adjust bit offset
63
+ _this.bitoffset = (_this.bitoffset + (bits || 0) + 64) % 8;
64
+ // Adjust byte offset based on byte overflow
65
+ _this.offset += bytes;
66
+ // Ensure bit offset stays between 0-7
67
+ _this.bitoffset = Math.min(Math.max(_this.bitoffset, 0), 7);
68
+ // Ensure offset doesn't go negative
69
+ _this.offset = Math.max(_this.offset, 0);
63
70
  }
64
- export function goto(_this, byte, bit) {
71
+ function align(_this, n) {
72
+ var a = _this.offset % n;
73
+ if (a) {
74
+ _this.skip(n - a);
75
+ }
76
+ }
77
+ function alignRev(_this, n) {
78
+ var a = _this.offset % n;
79
+ if (a) {
80
+ _this.skip(a * -1);
81
+ }
82
+ }
83
+ function goto(_this, byte, bit) {
65
84
  const new_size = (byte + Math.ceil((bit || 0) / 8));
66
85
  if (new_size > _this.size) {
67
86
  if (_this.strict == false) {
@@ -75,7 +94,7 @@ export function goto(_this, byte, bit) {
75
94
  _this.offset = byte;
76
95
  _this.bitoffset = (bit || 0) % 8;
77
96
  }
78
- export function remove(_this, startOffset, endOffset, consume, remove, fillValue) {
97
+ function remove(_this, startOffset, endOffset, consume, remove, fillValue) {
79
98
  const new_start = Math.abs(startOffset || 0);
80
99
  const new_offset = (endOffset || _this.offset);
81
100
  if (new_offset > _this.size) {
@@ -128,7 +147,7 @@ export function remove(_this, startOffset, endOffset, consume, remove, fillValue
128
147
  }
129
148
  return data_removed;
130
149
  }
131
- export function addData(_this, data, consume, offset, repalce) {
150
+ function addData(_this, data, consume, offset, repalce) {
132
151
  if (_this.strict == true) {
133
152
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
134
153
  throw new Error(`\x1b[33m[Strict mode]\x1b[0m: Can not insert data in strict mode. Use unrestrict() to enable.`);
@@ -186,7 +205,7 @@ export function addData(_this, data, consume, offset, repalce) {
186
205
  _this.bitoffset = 0;
187
206
  }
188
207
  }
189
- export function hexDump(_this, options) {
208
+ function hexDump(_this, options) {
190
209
  var length = options && options.length;
191
210
  var startByte = options && options.startByte;
192
211
  var supressUnicode = options && options.supressUnicode || false;
@@ -368,7 +387,7 @@ export function hexDump(_this, options) {
368
387
  }
369
388
  console.log(rows.join("\n"));
370
389
  }
371
- export function AND(_this, and_key, start, end, consume) {
390
+ function AND(_this, and_key, start, end, consume) {
372
391
  const input = _this.data;
373
392
  if ((end || 0) > _this.size) {
374
393
  if (_this.strict == false) {
@@ -389,7 +408,7 @@ export function AND(_this, and_key, start, end, consume) {
389
408
  }
390
409
  }
391
410
  else {
392
- if (_this.isBufferOrUint8Array(and_key)) {
411
+ if (arraybuffcheck(_this, and_key)) {
393
412
  let number = -1;
394
413
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
395
414
  if (number != and_key.length - 1) {
@@ -410,7 +429,7 @@ export function AND(_this, and_key, start, end, consume) {
410
429
  }
411
430
  }
412
431
  }
413
- export function OR(_this, or_key, start, end, consume) {
432
+ function OR(_this, or_key, start, end, consume) {
414
433
  const input = _this.data;
415
434
  if ((end || 0) > _this.size) {
416
435
  if (_this.strict == false) {
@@ -431,7 +450,7 @@ export function OR(_this, or_key, start, end, consume) {
431
450
  }
432
451
  }
433
452
  else {
434
- if (_this.isBufferOrUint8Array(or_key)) {
453
+ if (arraybuffcheck(_this, or_key)) {
435
454
  let number = -1;
436
455
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
437
456
  if (number != or_key.length - 1) {
@@ -452,7 +471,7 @@ export function OR(_this, or_key, start, end, consume) {
452
471
  }
453
472
  }
454
473
  }
455
- export function XOR(_this, xor_key, start, end, consume) {
474
+ function XOR(_this, xor_key, start, end, consume) {
456
475
  const input = _this.data;
457
476
  if ((end || 0) > _this.size) {
458
477
  if (_this.strict == false) {
@@ -473,7 +492,7 @@ export function XOR(_this, xor_key, start, end, consume) {
473
492
  }
474
493
  }
475
494
  else {
476
- if (_this.isBufferOrUint8Array(xor_key)) {
495
+ if (arraybuffcheck(_this, xor_key)) {
477
496
  let number = -1;
478
497
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
479
498
  if (number != xor_key.length - 1) {
@@ -494,7 +513,7 @@ export function XOR(_this, xor_key, start, end, consume) {
494
513
  }
495
514
  }
496
515
  }
497
- export function NOT(_this, start, end, consume) {
516
+ function NOT(_this, start, end, consume) {
498
517
  if ((end || 0) > _this.size) {
499
518
  if (_this.strict == false) {
500
519
  _this.extendArray((end || 0) - _this.size);
@@ -512,7 +531,7 @@ export function NOT(_this, start, end, consume) {
512
531
  }
513
532
  }
514
533
  }
515
- export function LSHIFT(_this, shift_key, start, end, consume) {
534
+ function LSHIFT(_this, shift_key, start, end, consume) {
516
535
  const input = _this.data;
517
536
  if ((end || 0) > _this.size) {
518
537
  if (_this.strict == false) {
@@ -533,7 +552,7 @@ export function LSHIFT(_this, shift_key, start, end, consume) {
533
552
  }
534
553
  }
535
554
  else {
536
- if (_this.isBufferOrUint8Array(shift_key)) {
555
+ if (arraybuffcheck(_this, shift_key)) {
537
556
  let number = -1;
538
557
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
539
558
  if (number != shift_key.length - 1) {
@@ -554,7 +573,7 @@ export function LSHIFT(_this, shift_key, start, end, consume) {
554
573
  }
555
574
  }
556
575
  }
557
- export function RSHIFT(_this, shift_key, start, end, consume) {
576
+ function RSHIFT(_this, shift_key, start, end, consume) {
558
577
  const input = _this.data;
559
578
  if ((end || 0) > _this.size) {
560
579
  if (_this.strict == false) {
@@ -575,7 +594,7 @@ export function RSHIFT(_this, shift_key, start, end, consume) {
575
594
  }
576
595
  }
577
596
  else {
578
- if (_this.isBufferOrUint8Array(shift_key)) {
597
+ if (arraybuffcheck(_this, shift_key)) {
579
598
  let number = -1;
580
599
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
581
600
  if (number != shift_key.length - 1) {
@@ -596,7 +615,7 @@ export function RSHIFT(_this, shift_key, start, end, consume) {
596
615
  }
597
616
  }
598
617
  }
599
- export function ADD(_this, add_key, start, end, consume) {
618
+ function ADD(_this, add_key, start, end, consume) {
600
619
  const input = _this.data;
601
620
  if ((end || 0) > _this.size) {
602
621
  if (_this.strict == false) {
@@ -617,7 +636,7 @@ export function ADD(_this, add_key, start, end, consume) {
617
636
  }
618
637
  }
619
638
  else {
620
- if (_this.isBufferOrUint8Array(add_key)) {
639
+ if (arraybuffcheck(_this, add_key)) {
621
640
  let number = -1;
622
641
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
623
642
  if (number != add_key.length - 1) {
@@ -638,7 +657,7 @@ export function ADD(_this, add_key, start, end, consume) {
638
657
  }
639
658
  }
640
659
  }
641
- export function wbit(_this, value, bits, unsigned, endian) {
660
+ function wbit(_this, value, bits, unsigned, endian) {
642
661
  if (value == undefined) {
643
662
  throw new Error('Must supply value.');
644
663
  }
@@ -698,7 +717,7 @@ export function wbit(_this, value, bits, unsigned, endian) {
698
717
  _this.offset = _this.offset + Math.floor(((bits) + _this.bitoffset) / 8); //end byte
699
718
  _this.bitoffset = ((bits) + _this.bitoffset) % 8;
700
719
  }
701
- export function rbit(_this, bits, unsigned, endian) {
720
+ function rbit(_this, bits, unsigned, endian) {
702
721
  if (bits == undefined || typeof bits != "number") {
703
722
  throw new Error("Enter number of bits to read");
704
723
  }
@@ -742,7 +761,7 @@ export function rbit(_this, bits, unsigned, endian) {
742
761
  }
743
762
  return value;
744
763
  }
745
- export function wbyte(_this, value, unsigned) {
764
+ function wbyte(_this, value, unsigned) {
746
765
  check_size(_this, 1, 0);
747
766
  if (unsigned == true) {
748
767
  if (value < 0 || value > 255) {
@@ -762,7 +781,7 @@ export function wbyte(_this, value, unsigned) {
762
781
  _this.offset += 1;
763
782
  _this.bitoffset = 0;
764
783
  }
765
- export function rbyte(_this, unsigned) {
784
+ function rbyte(_this, unsigned) {
766
785
  check_size(_this, 1);
767
786
  var read = _this.data[_this.offset];
768
787
  _this.offset += 1;
@@ -774,7 +793,7 @@ export function rbyte(_this, unsigned) {
774
793
  return read > 127 ? read - 256 : read;
775
794
  }
776
795
  }
777
- export function wint16(_this, value, unsigned, endian) {
796
+ function wint16(_this, value, unsigned, endian) {
778
797
  check_size(_this, 2, 0);
779
798
  if (unsigned == true) {
780
799
  if (value < 0 || value > 65535) {
@@ -801,7 +820,7 @@ export function wint16(_this, value, unsigned, endian) {
801
820
  _this.offset += 2;
802
821
  _this.bitoffset = 0;
803
822
  }
804
- export function rint16(_this, unsigned, endian) {
823
+ function rint16(_this, unsigned, endian) {
805
824
  check_size(_this, 2);
806
825
  var read;
807
826
  if ((endian != undefined ? endian : _this.endian) == "little") {
@@ -819,7 +838,7 @@ export function rint16(_this, unsigned, endian) {
819
838
  return read & 0xFFFF;
820
839
  }
821
840
  }
822
- export function rhalffloat(_this, endian) {
841
+ function rhalffloat(_this, endian) {
823
842
  var uint16Value = _this.readInt16(true, (endian != undefined ? endian : _this.endian));
824
843
  const sign = (uint16Value & 0x8000) >> 15;
825
844
  const exponent = (uint16Value & 0x7C00) >> 10;
@@ -848,7 +867,7 @@ export function rhalffloat(_this, endian) {
848
867
  }
849
868
  return floatValue;
850
869
  }
851
- export function whalffloat(_this, value, endian) {
870
+ function whalffloat(_this, value, endian) {
852
871
  check_size(_this, 2, 0);
853
872
  const maxValue = 65504;
854
873
  const minValue = 5.96e-08;
@@ -891,7 +910,7 @@ export function whalffloat(_this, value, endian) {
891
910
  _this.offset += 2;
892
911
  _this.bitoffset = 0;
893
912
  }
894
- export function wint32(_this, value, unsigned, endian) {
913
+ function wint32(_this, value, unsigned, endian) {
895
914
  check_size(_this, 4, 0);
896
915
  if (unsigned == true) {
897
916
  if (value < 0 || value > 4294967295) {
@@ -922,7 +941,7 @@ export function wint32(_this, value, unsigned, endian) {
922
941
  _this.offset += 4;
923
942
  _this.bitoffset = 0;
924
943
  }
925
- export function rint32(_this, unsigned, endian) {
944
+ function rint32(_this, unsigned, endian) {
926
945
  check_size(_this, 4);
927
946
  var read;
928
947
  if ((endian != undefined ? endian : _this.endian) == "little") {
@@ -940,7 +959,7 @@ export function rint32(_this, unsigned, endian) {
940
959
  return read >>> 0;
941
960
  }
942
961
  }
943
- export function rfloat(_this, endian) {
962
+ function rfloat(_this, endian) {
944
963
  var uint32Value = _this.readInt32(true, (endian == undefined ? _this.endian : endian));
945
964
  // Check if the value is negative (i.e., the most significant bit is set)
946
965
  const isNegative = (uint32Value & 0x80000000) !== 0 ? 1 : 0;
@@ -963,7 +982,7 @@ export function rfloat(_this, endian) {
963
982
  }
964
983
  return floatValue;
965
984
  }
966
- export function wfloat(_this, value, endian) {
985
+ function wfloat(_this, value, endian) {
967
986
  check_size(_this, 4, 0);
968
987
  const maxValue = 3.402823466e+38;
969
988
  const minValue = 1.175494351e-38;
@@ -985,7 +1004,7 @@ export function wfloat(_this, value, endian) {
985
1004
  _this.offset += 4;
986
1005
  _this.bitoffset = 0;
987
1006
  }
988
- export function rint64(_this, unsigned, endian) {
1007
+ function rint64(_this, unsigned, endian) {
989
1008
  check_size(_this, 8);
990
1009
  // Convert the byte array to a BigInt
991
1010
  let value = BigInt(0);
@@ -1015,7 +1034,7 @@ export function rint64(_this, unsigned, endian) {
1015
1034
  _this.bitoffset = 0;
1016
1035
  return value;
1017
1036
  }
1018
- export function wint64(_this, value, unsigned, endian) {
1037
+ function wint64(_this, value, unsigned, endian) {
1019
1038
  check_size(_this, 8, 0);
1020
1039
  if (unsigned == true) {
1021
1040
  if (value < 0 || value > Math.pow(2, 64) - 1) {
@@ -1069,7 +1088,7 @@ export function wint64(_this, value, unsigned, endian) {
1069
1088
  _this.offset += 8;
1070
1089
  _this.bitoffset = 0;
1071
1090
  }
1072
- export function wdfloat(_this, value, endian) {
1091
+ function wdfloat(_this, value, endian) {
1073
1092
  check_size(_this, 8, 0);
1074
1093
  const maxValue = 1.7976931348623158e308;
1075
1094
  const minValue = 2.2250738585072014e-308;
@@ -1092,7 +1111,7 @@ export function wdfloat(_this, value, endian) {
1092
1111
  _this.offset += 8;
1093
1112
  _this.bitoffset = 0;
1094
1113
  }
1095
- export function rdfloat(_this, endian) {
1114
+ function rdfloat(_this, endian) {
1096
1115
  var uint64Value = _this.readInt64(true, (endian == undefined ? _this.endian : endian));
1097
1116
  const sign = (uint64Value & 0x8000000000000000n) >> 63n;
1098
1117
  const exponent = Number((uint64Value & 0x7ff0000000000000n) >> 52n) - 1023;
@@ -1121,7 +1140,7 @@ export function rdfloat(_this, endian) {
1121
1140
  }
1122
1141
  return floatValue;
1123
1142
  }
1124
- export function rstring(_this, options) {
1143
+ function rstring(_this, options) {
1125
1144
  var length = options && options.length;
1126
1145
  var stringType = options && options.stringType || 'utf-8';
1127
1146
  var terminateValue = options && options.terminateValue;
@@ -1243,7 +1262,7 @@ export function rstring(_this, options) {
1243
1262
  throw new Error('Unsupported string type: ' + stringType);
1244
1263
  }
1245
1264
  }
1246
- export function wstring(_this, string, options) {
1265
+ function wstring(_this, string, options) {
1247
1266
  var length = options && options.length;
1248
1267
  var stringType = options && options.stringType || 'utf-8';
1249
1268
  var terminateValue = options && options.terminateValue;
@@ -1496,6 +1515,26 @@ export class bireader {
1496
1515
  // move from current position
1497
1516
  //
1498
1517
  /**
1518
+ * Aligns current byte position
1519
+ *
1520
+ * Note: Will extend array if strict mode is off and outside of max size
1521
+ *
1522
+ * @param {number} number - Byte to align
1523
+ */
1524
+ align(number) {
1525
+ return align(this, number);
1526
+ }
1527
+ /**
1528
+ * Reverse aligns current byte position
1529
+ *
1530
+ * Note: Will extend array if strict mode is off and outside of max size
1531
+ *
1532
+ * @param {number} number - Byte to align
1533
+ */
1534
+ alignRev(number) {
1535
+ return alignRev(this, number);
1536
+ }
1537
+ /**
1499
1538
  * Offset current byte or bit position
1500
1539
  * Note: Will extend array if strict mode is off and outside of max size
1501
1540
  *
@@ -4445,6 +4484,14 @@ export class bireader {
4445
4484
  return wbyte(this, value, unsigned);
4446
4485
  }
4447
4486
  /**
4487
+ * Write unsigned byte
4488
+ *
4489
+ * @param {number} value - value as int
4490
+ */
4491
+ writeUByte(value) {
4492
+ return wbyte(this, value, true);
4493
+ }
4494
+ /**
4448
4495
  * Read byte
4449
4496
  *
4450
4497
  * @param {boolean} unsigned - if value is unsigned or not
@@ -4510,6 +4557,15 @@ export class bireader {
4510
4557
  return wint16(this, value, unsigned, endian);
4511
4558
  }
4512
4559
  /**
4560
+ * Write unsigned int16
4561
+ *
4562
+ * @param {number} value - value as int
4563
+ * @param {string} endian - ``big`` or ``little`
4564
+ */
4565
+ writeUInt16(value, endian) {
4566
+ return wint16(this, value, true, endian);
4567
+ }
4568
+ /**
4513
4569
  * Read short
4514
4570
  *
4515
4571
  * @param {boolean} unsigned - if value is unsigned or not
@@ -4818,6 +4874,15 @@ export class bireader {
4818
4874
  return wint32(this, value, unsigned, endian);
4819
4875
  }
4820
4876
  /**
4877
+ * Write unsigned int32
4878
+ *
4879
+ * @param {number} value - value as int
4880
+ * @param {string} endian - ``big`` or ``little``
4881
+ */
4882
+ writeUInt32(value, endian) {
4883
+ return wint32(this, value, true, endian);
4884
+ }
4885
+ /**
4821
4886
  * Read 32 bit integer
4822
4887
  *
4823
4888
  * @param {boolean} unsigned - if value is unsigned or not
@@ -5912,7 +5977,28 @@ export class biwriter {
5912
5977
  // move from current position
5913
5978
  //
5914
5979
  /**
5980
+ * Aligns current byte position
5981
+ *
5982
+ * Note: Will extend array if strict mode is off and outside of max size
5983
+ *
5984
+ * @param {number} number - Byte to align
5985
+ */
5986
+ align(number) {
5987
+ return align(this, number);
5988
+ }
5989
+ /**
5990
+ * Reverse aligns current byte position
5991
+ *
5992
+ * Note: Will extend array if strict mode is off and outside of max size
5993
+ *
5994
+ * @param {number} number - Byte to align
5995
+ */
5996
+ alignRev(number) {
5997
+ return alignRev(this, number);
5998
+ }
5999
+ /**
5915
6000
  * Offset current byte or bit position
6001
+ *
5916
6002
  * Note: Will extend array if strict mode is off and outside of max size
5917
6003
  *
5918
6004
  * @param {number} bytes - Bytes to skip
@@ -9126,6 +9212,14 @@ export class biwriter {
9126
9212
  return rbyte(this, unsigned);
9127
9213
  }
9128
9214
  /**
9215
+ * Read unsigned byte
9216
+ *
9217
+ * @returns number
9218
+ */
9219
+ readUByte() {
9220
+ return rbyte(this, true);
9221
+ }
9222
+ /**
9129
9223
  * Write byte
9130
9224
  *
9131
9225
  * @param {number} value - value as int
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bireader",
3
- "version": "1.0.36",
3
+ "version": "1.0.38",
4
4
  "description": "Read and write data in binary",
5
5
  "module": "lib/esm/index.mjs",
6
6
  "main": "lib/cjs/index.js",