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/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,16 +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
- if (bits && bits < 0) {
61
- _this.bitoffset = ((_this.bitoffset + (bits || 0) % 8) + 8) % 8;
62
- _this.offset = new_size;
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);
70
+ }
71
+ function align(_this, n) {
72
+ var a = _this.offset % n;
73
+ if (a) {
74
+ _this.skip(n - a);
63
75
  }
64
- else {
65
- _this.bitoffset += (bits || 0) % 8;
66
- _this.offset += (bytes || 0);
76
+ }
77
+ function alignRev(_this, n) {
78
+ var a = _this.offset % n;
79
+ if (a) {
80
+ _this.skip(a * -1);
67
81
  }
68
82
  }
69
- export function goto(_this, byte, bit) {
83
+ function goto(_this, byte, bit) {
70
84
  const new_size = (byte + Math.ceil((bit || 0) / 8));
71
85
  if (new_size > _this.size) {
72
86
  if (_this.strict == false) {
@@ -80,7 +94,7 @@ export function goto(_this, byte, bit) {
80
94
  _this.offset = byte;
81
95
  _this.bitoffset = (bit || 0) % 8;
82
96
  }
83
- export function remove(_this, startOffset, endOffset, consume, remove, fillValue) {
97
+ function remove(_this, startOffset, endOffset, consume, remove, fillValue) {
84
98
  const new_start = Math.abs(startOffset || 0);
85
99
  const new_offset = (endOffset || _this.offset);
86
100
  if (new_offset > _this.size) {
@@ -133,7 +147,7 @@ export function remove(_this, startOffset, endOffset, consume, remove, fillValue
133
147
  }
134
148
  return data_removed;
135
149
  }
136
- export function addData(_this, data, consume, offset, repalce) {
150
+ function addData(_this, data, consume, offset, repalce) {
137
151
  if (_this.strict == true) {
138
152
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
139
153
  throw new Error(`\x1b[33m[Strict mode]\x1b[0m: Can not insert data in strict mode. Use unrestrict() to enable.`);
@@ -191,7 +205,7 @@ export function addData(_this, data, consume, offset, repalce) {
191
205
  _this.bitoffset = 0;
192
206
  }
193
207
  }
194
- export function hexDump(_this, options) {
208
+ function hexDump(_this, options) {
195
209
  var length = options && options.length;
196
210
  var startByte = options && options.startByte;
197
211
  var supressUnicode = options && options.supressUnicode || false;
@@ -373,7 +387,7 @@ export function hexDump(_this, options) {
373
387
  }
374
388
  console.log(rows.join("\n"));
375
389
  }
376
- export function AND(_this, and_key, start, end, consume) {
390
+ function AND(_this, and_key, start, end, consume) {
377
391
  const input = _this.data;
378
392
  if ((end || 0) > _this.size) {
379
393
  if (_this.strict == false) {
@@ -394,7 +408,7 @@ export function AND(_this, and_key, start, end, consume) {
394
408
  }
395
409
  }
396
410
  else {
397
- if (_this.isBufferOrUint8Array(and_key)) {
411
+ if (arraybuffcheck(_this, and_key)) {
398
412
  let number = -1;
399
413
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
400
414
  if (number != and_key.length - 1) {
@@ -415,7 +429,7 @@ export function AND(_this, and_key, start, end, consume) {
415
429
  }
416
430
  }
417
431
  }
418
- export function OR(_this, or_key, start, end, consume) {
432
+ function OR(_this, or_key, start, end, consume) {
419
433
  const input = _this.data;
420
434
  if ((end || 0) > _this.size) {
421
435
  if (_this.strict == false) {
@@ -436,7 +450,7 @@ export function OR(_this, or_key, start, end, consume) {
436
450
  }
437
451
  }
438
452
  else {
439
- if (_this.isBufferOrUint8Array(or_key)) {
453
+ if (arraybuffcheck(_this, or_key)) {
440
454
  let number = -1;
441
455
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
442
456
  if (number != or_key.length - 1) {
@@ -457,7 +471,7 @@ export function OR(_this, or_key, start, end, consume) {
457
471
  }
458
472
  }
459
473
  }
460
- export function XOR(_this, xor_key, start, end, consume) {
474
+ function XOR(_this, xor_key, start, end, consume) {
461
475
  const input = _this.data;
462
476
  if ((end || 0) > _this.size) {
463
477
  if (_this.strict == false) {
@@ -478,7 +492,7 @@ export function XOR(_this, xor_key, start, end, consume) {
478
492
  }
479
493
  }
480
494
  else {
481
- if (_this.isBufferOrUint8Array(xor_key)) {
495
+ if (arraybuffcheck(_this, xor_key)) {
482
496
  let number = -1;
483
497
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
484
498
  if (number != xor_key.length - 1) {
@@ -499,7 +513,7 @@ export function XOR(_this, xor_key, start, end, consume) {
499
513
  }
500
514
  }
501
515
  }
502
- export function NOT(_this, start, end, consume) {
516
+ function NOT(_this, start, end, consume) {
503
517
  if ((end || 0) > _this.size) {
504
518
  if (_this.strict == false) {
505
519
  _this.extendArray((end || 0) - _this.size);
@@ -517,7 +531,7 @@ export function NOT(_this, start, end, consume) {
517
531
  }
518
532
  }
519
533
  }
520
- export function LSHIFT(_this, shift_key, start, end, consume) {
534
+ function LSHIFT(_this, shift_key, start, end, consume) {
521
535
  const input = _this.data;
522
536
  if ((end || 0) > _this.size) {
523
537
  if (_this.strict == false) {
@@ -538,7 +552,7 @@ export function LSHIFT(_this, shift_key, start, end, consume) {
538
552
  }
539
553
  }
540
554
  else {
541
- if (_this.isBufferOrUint8Array(shift_key)) {
555
+ if (arraybuffcheck(_this, shift_key)) {
542
556
  let number = -1;
543
557
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
544
558
  if (number != shift_key.length - 1) {
@@ -559,7 +573,7 @@ export function LSHIFT(_this, shift_key, start, end, consume) {
559
573
  }
560
574
  }
561
575
  }
562
- export function RSHIFT(_this, shift_key, start, end, consume) {
576
+ function RSHIFT(_this, shift_key, start, end, consume) {
563
577
  const input = _this.data;
564
578
  if ((end || 0) > _this.size) {
565
579
  if (_this.strict == false) {
@@ -580,7 +594,7 @@ export function RSHIFT(_this, shift_key, start, end, consume) {
580
594
  }
581
595
  }
582
596
  else {
583
- if (_this.isBufferOrUint8Array(shift_key)) {
597
+ if (arraybuffcheck(_this, shift_key)) {
584
598
  let number = -1;
585
599
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
586
600
  if (number != shift_key.length - 1) {
@@ -601,7 +615,7 @@ export function RSHIFT(_this, shift_key, start, end, consume) {
601
615
  }
602
616
  }
603
617
  }
604
- export function ADD(_this, add_key, start, end, consume) {
618
+ function ADD(_this, add_key, start, end, consume) {
605
619
  const input = _this.data;
606
620
  if ((end || 0) > _this.size) {
607
621
  if (_this.strict == false) {
@@ -622,7 +636,7 @@ export function ADD(_this, add_key, start, end, consume) {
622
636
  }
623
637
  }
624
638
  else {
625
- if (_this.isBufferOrUint8Array(add_key)) {
639
+ if (arraybuffcheck(_this, add_key)) {
626
640
  let number = -1;
627
641
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
628
642
  if (number != add_key.length - 1) {
@@ -643,7 +657,7 @@ export function ADD(_this, add_key, start, end, consume) {
643
657
  }
644
658
  }
645
659
  }
646
- export function wbit(_this, value, bits, unsigned, endian) {
660
+ function wbit(_this, value, bits, unsigned, endian) {
647
661
  if (value == undefined) {
648
662
  throw new Error('Must supply value.');
649
663
  }
@@ -703,7 +717,7 @@ export function wbit(_this, value, bits, unsigned, endian) {
703
717
  _this.offset = _this.offset + Math.floor(((bits) + _this.bitoffset) / 8); //end byte
704
718
  _this.bitoffset = ((bits) + _this.bitoffset) % 8;
705
719
  }
706
- export function rbit(_this, bits, unsigned, endian) {
720
+ function rbit(_this, bits, unsigned, endian) {
707
721
  if (bits == undefined || typeof bits != "number") {
708
722
  throw new Error("Enter number of bits to read");
709
723
  }
@@ -747,7 +761,7 @@ export function rbit(_this, bits, unsigned, endian) {
747
761
  }
748
762
  return value;
749
763
  }
750
- export function wbyte(_this, value, unsigned) {
764
+ function wbyte(_this, value, unsigned) {
751
765
  check_size(_this, 1, 0);
752
766
  if (unsigned == true) {
753
767
  if (value < 0 || value > 255) {
@@ -767,7 +781,7 @@ export function wbyte(_this, value, unsigned) {
767
781
  _this.offset += 1;
768
782
  _this.bitoffset = 0;
769
783
  }
770
- export function rbyte(_this, unsigned) {
784
+ function rbyte(_this, unsigned) {
771
785
  check_size(_this, 1);
772
786
  var read = _this.data[_this.offset];
773
787
  _this.offset += 1;
@@ -779,7 +793,7 @@ export function rbyte(_this, unsigned) {
779
793
  return read > 127 ? read - 256 : read;
780
794
  }
781
795
  }
782
- export function wint16(_this, value, unsigned, endian) {
796
+ function wint16(_this, value, unsigned, endian) {
783
797
  check_size(_this, 2, 0);
784
798
  if (unsigned == true) {
785
799
  if (value < 0 || value > 65535) {
@@ -806,7 +820,7 @@ export function wint16(_this, value, unsigned, endian) {
806
820
  _this.offset += 2;
807
821
  _this.bitoffset = 0;
808
822
  }
809
- export function rint16(_this, unsigned, endian) {
823
+ function rint16(_this, unsigned, endian) {
810
824
  check_size(_this, 2);
811
825
  var read;
812
826
  if ((endian != undefined ? endian : _this.endian) == "little") {
@@ -824,7 +838,7 @@ export function rint16(_this, unsigned, endian) {
824
838
  return read & 0xFFFF;
825
839
  }
826
840
  }
827
- export function rhalffloat(_this, endian) {
841
+ function rhalffloat(_this, endian) {
828
842
  var uint16Value = _this.readInt16(true, (endian != undefined ? endian : _this.endian));
829
843
  const sign = (uint16Value & 0x8000) >> 15;
830
844
  const exponent = (uint16Value & 0x7C00) >> 10;
@@ -853,7 +867,7 @@ export function rhalffloat(_this, endian) {
853
867
  }
854
868
  return floatValue;
855
869
  }
856
- export function whalffloat(_this, value, endian) {
870
+ function whalffloat(_this, value, endian) {
857
871
  check_size(_this, 2, 0);
858
872
  const maxValue = 65504;
859
873
  const minValue = 5.96e-08;
@@ -896,7 +910,7 @@ export function whalffloat(_this, value, endian) {
896
910
  _this.offset += 2;
897
911
  _this.bitoffset = 0;
898
912
  }
899
- export function wint32(_this, value, unsigned, endian) {
913
+ function wint32(_this, value, unsigned, endian) {
900
914
  check_size(_this, 4, 0);
901
915
  if (unsigned == true) {
902
916
  if (value < 0 || value > 4294967295) {
@@ -927,7 +941,7 @@ export function wint32(_this, value, unsigned, endian) {
927
941
  _this.offset += 4;
928
942
  _this.bitoffset = 0;
929
943
  }
930
- export function rint32(_this, unsigned, endian) {
944
+ function rint32(_this, unsigned, endian) {
931
945
  check_size(_this, 4);
932
946
  var read;
933
947
  if ((endian != undefined ? endian : _this.endian) == "little") {
@@ -945,7 +959,7 @@ export function rint32(_this, unsigned, endian) {
945
959
  return read >>> 0;
946
960
  }
947
961
  }
948
- export function rfloat(_this, endian) {
962
+ function rfloat(_this, endian) {
949
963
  var uint32Value = _this.readInt32(true, (endian == undefined ? _this.endian : endian));
950
964
  // Check if the value is negative (i.e., the most significant bit is set)
951
965
  const isNegative = (uint32Value & 0x80000000) !== 0 ? 1 : 0;
@@ -968,7 +982,7 @@ export function rfloat(_this, endian) {
968
982
  }
969
983
  return floatValue;
970
984
  }
971
- export function wfloat(_this, value, endian) {
985
+ function wfloat(_this, value, endian) {
972
986
  check_size(_this, 4, 0);
973
987
  const maxValue = 3.402823466e+38;
974
988
  const minValue = 1.175494351e-38;
@@ -990,7 +1004,7 @@ export function wfloat(_this, value, endian) {
990
1004
  _this.offset += 4;
991
1005
  _this.bitoffset = 0;
992
1006
  }
993
- export function rint64(_this, unsigned, endian) {
1007
+ function rint64(_this, unsigned, endian) {
994
1008
  check_size(_this, 8);
995
1009
  // Convert the byte array to a BigInt
996
1010
  let value = BigInt(0);
@@ -1020,7 +1034,7 @@ export function rint64(_this, unsigned, endian) {
1020
1034
  _this.bitoffset = 0;
1021
1035
  return value;
1022
1036
  }
1023
- export function wint64(_this, value, unsigned, endian) {
1037
+ function wint64(_this, value, unsigned, endian) {
1024
1038
  check_size(_this, 8, 0);
1025
1039
  if (unsigned == true) {
1026
1040
  if (value < 0 || value > Math.pow(2, 64) - 1) {
@@ -1074,7 +1088,7 @@ export function wint64(_this, value, unsigned, endian) {
1074
1088
  _this.offset += 8;
1075
1089
  _this.bitoffset = 0;
1076
1090
  }
1077
- export function wdfloat(_this, value, endian) {
1091
+ function wdfloat(_this, value, endian) {
1078
1092
  check_size(_this, 8, 0);
1079
1093
  const maxValue = 1.7976931348623158e308;
1080
1094
  const minValue = 2.2250738585072014e-308;
@@ -1097,7 +1111,7 @@ export function wdfloat(_this, value, endian) {
1097
1111
  _this.offset += 8;
1098
1112
  _this.bitoffset = 0;
1099
1113
  }
1100
- export function rdfloat(_this, endian) {
1114
+ function rdfloat(_this, endian) {
1101
1115
  var uint64Value = _this.readInt64(true, (endian == undefined ? _this.endian : endian));
1102
1116
  const sign = (uint64Value & 0x8000000000000000n) >> 63n;
1103
1117
  const exponent = Number((uint64Value & 0x7ff0000000000000n) >> 52n) - 1023;
@@ -1126,7 +1140,7 @@ export function rdfloat(_this, endian) {
1126
1140
  }
1127
1141
  return floatValue;
1128
1142
  }
1129
- export function rstring(_this, options) {
1143
+ function rstring(_this, options) {
1130
1144
  var length = options && options.length;
1131
1145
  var stringType = options && options.stringType || 'utf-8';
1132
1146
  var terminateValue = options && options.terminateValue;
@@ -1248,7 +1262,7 @@ export function rstring(_this, options) {
1248
1262
  throw new Error('Unsupported string type: ' + stringType);
1249
1263
  }
1250
1264
  }
1251
- export function wstring(_this, string, options) {
1265
+ function wstring(_this, string, options) {
1252
1266
  var length = options && options.length;
1253
1267
  var stringType = options && options.stringType || 'utf-8';
1254
1268
  var terminateValue = options && options.terminateValue;
@@ -1501,6 +1515,26 @@ export class bireader {
1501
1515
  // move from current position
1502
1516
  //
1503
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
+ /**
1504
1538
  * Offset current byte or bit position
1505
1539
  * Note: Will extend array if strict mode is off and outside of max size
1506
1540
  *
@@ -4450,6 +4484,14 @@ export class bireader {
4450
4484
  return wbyte(this, value, unsigned);
4451
4485
  }
4452
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
+ /**
4453
4495
  * Read byte
4454
4496
  *
4455
4497
  * @param {boolean} unsigned - if value is unsigned or not
@@ -4515,6 +4557,15 @@ export class bireader {
4515
4557
  return wint16(this, value, unsigned, endian);
4516
4558
  }
4517
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
+ /**
4518
4569
  * Read short
4519
4570
  *
4520
4571
  * @param {boolean} unsigned - if value is unsigned or not
@@ -4823,6 +4874,15 @@ export class bireader {
4823
4874
  return wint32(this, value, unsigned, endian);
4824
4875
  }
4825
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
+ /**
4826
4886
  * Read 32 bit integer
4827
4887
  *
4828
4888
  * @param {boolean} unsigned - if value is unsigned or not
@@ -5917,7 +5977,28 @@ export class biwriter {
5917
5977
  // move from current position
5918
5978
  //
5919
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
+ /**
5920
6000
  * Offset current byte or bit position
6001
+ *
5921
6002
  * Note: Will extend array if strict mode is off and outside of max size
5922
6003
  *
5923
6004
  * @param {number} bytes - Bytes to skip
@@ -9131,6 +9212,14 @@ export class biwriter {
9131
9212
  return rbyte(this, unsigned);
9132
9213
  }
9133
9214
  /**
9215
+ * Read unsigned byte
9216
+ *
9217
+ * @returns number
9218
+ */
9219
+ readUByte() {
9220
+ return rbyte(this, true);
9221
+ }
9222
+ /**
9134
9223
  * Write byte
9135
9224
  *
9136
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.35",
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",