bireader 1.0.43 → 1.0.45

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
@@ -670,6 +670,212 @@ function ADD(_this, add_key, start, end, consume) {
670
670
  }
671
671
  }
672
672
  }
673
+ function fString(_this, searchString) {
674
+ // Convert the searchString to Uint8Array
675
+ const searchArray = new TextEncoder().encode(searchString);
676
+ for (let i = _this.offset; i <= _this.size - searchArray.length; i++) {
677
+ let match = true;
678
+ for (let j = 0; j < searchArray.length; j++) {
679
+ if (_this.data[i + j] !== searchArray[j]) {
680
+ match = false;
681
+ break;
682
+ }
683
+ }
684
+ if (match) {
685
+ return i; // Found the string, return the index
686
+ }
687
+ }
688
+ return -1; // String not found
689
+ }
690
+ function fNumber(_this, targetNumber, bits, unsigned, endian) {
691
+ check_size(_this, Math.floor(bits / 8), 0);
692
+ for (let z = _this.offset; z <= (_this.size - (bits / 8)); z++) {
693
+ var off_in_bits = 0;
694
+ var value = 0;
695
+ for (var i = 0; i < bits;) {
696
+ var remaining = bits - i;
697
+ var bitOffset = off_in_bits & 7;
698
+ var currentByte = _this.data[z + (off_in_bits >> 3)];
699
+ var read = Math.min(remaining, 8 - bitOffset);
700
+ var mask, readBits;
701
+ if ((endian != undefined ? endian : _this.endian) == "big") {
702
+ mask = ~(0xFF << read);
703
+ readBits = (currentByte >> (8 - read - bitOffset)) & mask;
704
+ value <<= read;
705
+ value |= readBits;
706
+ }
707
+ else {
708
+ mask = ~(0xFF << read);
709
+ readBits = (currentByte >> bitOffset) & mask;
710
+ value |= readBits << i;
711
+ }
712
+ off_in_bits += read;
713
+ i += read;
714
+ }
715
+ if (unsigned == true || bits <= 7) {
716
+ value = value >>> 0;
717
+ }
718
+ else {
719
+ if (bits !== 32 && value & (1 << (bits - 1))) {
720
+ value |= -1 ^ ((1 << bits) - 1);
721
+ }
722
+ }
723
+ if (value === targetNumber) {
724
+ return z; // Found the byte, return the index
725
+ }
726
+ }
727
+ return -1; // number not found
728
+ }
729
+ function fHalfFloat(_this, targetNumber, endian) {
730
+ check_size(_this, 2, 0);
731
+ for (let z = _this.offset; z <= (_this.size - 2); z++) {
732
+ var value = 0;
733
+ if ((endian != undefined ? endian : _this.endian) == "little") {
734
+ value = (_this.data[z + 1] << 8) | _this.data[z];
735
+ }
736
+ else {
737
+ value = (_this.data[z] << 8) | _this.data[z + 1];
738
+ }
739
+ const sign = (value & 0x8000) >> 15;
740
+ const exponent = (value & 0x7C00) >> 10;
741
+ const fraction = value & 0x03FF;
742
+ let floatValue;
743
+ if (exponent === 0) {
744
+ if (fraction === 0) {
745
+ floatValue = (sign === 0) ? 0 : -0; // +/-0
746
+ }
747
+ else {
748
+ // Denormalized number
749
+ floatValue = (sign === 0 ? 1 : -1) * Math.pow(2, -14) * (fraction / 0x0400);
750
+ }
751
+ }
752
+ else if (exponent === 0x1F) {
753
+ if (fraction === 0) {
754
+ floatValue = (sign === 0) ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
755
+ }
756
+ else {
757
+ floatValue = Number.NaN;
758
+ }
759
+ }
760
+ else {
761
+ // Normalized number
762
+ floatValue = (sign === 0 ? 1 : -1) * Math.pow(2, exponent - 15) * (1 + fraction / 0x0400);
763
+ }
764
+ if (floatValue === targetNumber) {
765
+ return z; // Found the number, return the index
766
+ }
767
+ }
768
+ return -1; // number not found
769
+ }
770
+ function fFloat(_this, targetNumber, endian) {
771
+ check_size(_this, 4, 0);
772
+ for (let z = _this.offset; z <= (_this.size - 4); z++) {
773
+ var value = 0;
774
+ if ((endian != undefined ? endian : _this.endian) == "little") {
775
+ value = ((_this.data[z + 3] << 24) | (_this.data[z + 2] << 16) | (_this.data[z + 1] << 8) | _this.data[z]);
776
+ }
777
+ else {
778
+ value = (_this.data[z] << 24) | (_this.data[z + 1] << 16) | (_this.data[z + 2] << 8) | _this.data[z + 3];
779
+ }
780
+ const isNegative = (value & 0x80000000) !== 0 ? 1 : 0;
781
+ // Extract the exponent and fraction parts
782
+ const exponent = (value >> 23) & 0xFF;
783
+ const fraction = value & 0x7FFFFF;
784
+ // Calculate the float value
785
+ let floatValue;
786
+ if (exponent === 0) {
787
+ // Denormalized number (exponent is 0)
788
+ floatValue = Math.pow(-1, isNegative) * Math.pow(2, -126) * (fraction / Math.pow(2, 23));
789
+ }
790
+ else if (exponent === 0xFF) {
791
+ // Infinity or NaN (exponent is 255)
792
+ floatValue = fraction === 0 ? (isNegative ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY) : Number.NaN;
793
+ }
794
+ else {
795
+ // Normalized number
796
+ floatValue = Math.pow(-1, isNegative) * Math.pow(2, exponent - 127) * (1 + fraction / Math.pow(2, 23));
797
+ }
798
+ if (floatValue === targetNumber) {
799
+ return z; // Found the number, return the index
800
+ }
801
+ }
802
+ return -1; // number not found
803
+ }
804
+ function fBigInt(_this, targetNumber, unsigned, endian) {
805
+ check_size(_this, 8, 0);
806
+ for (let z = _this.offset; z <= (_this.size - 8); z++) {
807
+ let value = BigInt(0);
808
+ if ((endian == undefined ? _this.endian : endian) == "little") {
809
+ for (let i = 0; i < 8; i++) {
810
+ value = value | BigInt(_this.data[z + i]) << BigInt(8 * i);
811
+ }
812
+ if (unsigned == undefined || unsigned == false) {
813
+ if (value & (BigInt(1) << BigInt(63))) {
814
+ value -= BigInt(1) << BigInt(64);
815
+ }
816
+ }
817
+ }
818
+ else {
819
+ for (let i = 0; i < 8; i++) {
820
+ value = (value << BigInt(8)) | BigInt(_this.data[z + i]);
821
+ }
822
+ if (unsigned == undefined || unsigned == false) {
823
+ if (value & (BigInt(1) << BigInt(63))) {
824
+ value -= BigInt(1) << BigInt(64);
825
+ }
826
+ }
827
+ }
828
+ if (value == BigInt(targetNumber)) {
829
+ return z;
830
+ }
831
+ }
832
+ return -1; // number not found
833
+ }
834
+ function fDoubleFloat(_this, targetNumber, endian) {
835
+ check_size(_this, 8, 0);
836
+ for (let z = _this.offset; z <= (_this.size - 8); z++) {
837
+ let value = BigInt(0);
838
+ if ((endian == undefined ? _this.endian : endian) == "little") {
839
+ for (let i = 0; i < 8; i++) {
840
+ value = value | BigInt(_this.data[z + i]) << BigInt(8 * i);
841
+ }
842
+ }
843
+ else {
844
+ for (let i = 0; i < 8; i++) {
845
+ value = (value << BigInt(8)) | BigInt(_this.data[z + i]);
846
+ }
847
+ }
848
+ const sign = (value & 0x8000000000000000n) >> 63n;
849
+ const exponent = Number((value & 0x7ff0000000000000n) >> 52n) - 1023;
850
+ const fraction = Number(value & 0x000fffffffffffffn) / Math.pow(2, 52);
851
+ var floatValue;
852
+ if (exponent == -1023) {
853
+ if (fraction == 0) {
854
+ floatValue = (sign == 0n) ? 0 : -0; // +/-0
855
+ }
856
+ else {
857
+ // Denormalized number
858
+ floatValue = (sign == 0n ? 1 : -1) * Math.pow(2, -1022) * fraction;
859
+ }
860
+ }
861
+ else if (exponent == 1024) {
862
+ if (fraction == 0) {
863
+ floatValue = (sign == 0n) ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
864
+ }
865
+ else {
866
+ floatValue = Number.NaN;
867
+ }
868
+ }
869
+ else {
870
+ // Normalized number
871
+ floatValue = (sign == 0n ? 1 : -1) * Math.pow(2, exponent) * (1 + fraction);
872
+ }
873
+ if (floatValue == targetNumber) {
874
+ return z;
875
+ }
876
+ }
877
+ return -1; // number not found
878
+ }
673
879
  function wbit(_this, value, bits, unsigned, endian) {
674
880
  if (value == undefined) {
675
881
  throw new Error('Must supply value.');
@@ -1440,6 +1646,16 @@ class bireader {
1440
1646
  this.strict = false;
1441
1647
  this.errorDump = true;
1442
1648
  this.data = [];
1649
+ if (data == undefined) {
1650
+ throw new Error("Data required");
1651
+ }
1652
+ else {
1653
+ if (!this.isBufferOrUint8Array(data)) {
1654
+ throw new Error("Write data must be Uint8Array or Buffer");
1655
+ }
1656
+ }
1657
+ this.size = data.length;
1658
+ this.data = data;
1443
1659
  if (endianness != undefined && typeof endianness != "string") {
1444
1660
  throw new Error("Endian must be big or little");
1445
1661
  }
@@ -1447,17 +1663,6 @@ class bireader {
1447
1663
  throw new Error("Byte order must be big or little");
1448
1664
  }
1449
1665
  this.endian = endianness || "little";
1450
- if (byteOffset != undefined) {
1451
- if (typeof byteOffset == "number") {
1452
- this.offset = Math.round(byteOffset) || 0;
1453
- }
1454
- else {
1455
- throw new Error("Byte offset must be number");
1456
- }
1457
- }
1458
- if (bitOffset != undefined) {
1459
- this.bitoffset = bitOffset % 8;
1460
- }
1461
1666
  if (typeof strict == "boolean") {
1462
1667
  this.strict = strict;
1463
1668
  }
@@ -1466,16 +1671,25 @@ class bireader {
1466
1671
  throw new Error("Strict mode must be true of false");
1467
1672
  }
1468
1673
  }
1469
- if (data == undefined) {
1470
- throw new Error("Data required");
1471
- }
1472
- else {
1473
- if (!this.isBufferOrUint8Array(data)) {
1474
- throw new Error("Write data must be Uint8Array or Buffer");
1674
+ if (byteOffset != undefined || bitOffset != undefined) {
1675
+ this.offset = ((Math.abs(byteOffset || 0)) + Math.ceil((Math.abs(bitOffset || 0)) / 8));
1676
+ // Adjust byte offset based on bit overflow
1677
+ this.offset += Math.floor((Math.abs(bitOffset || 0)) / 8);
1678
+ // Adjust bit offset
1679
+ this.bitoffset = (Math.abs(bitOffset || 0) + 64) % 8;
1680
+ // Ensure bit offset stays between 0-7
1681
+ this.bitoffset = Math.min(Math.max(this.bitoffset, 0), 7);
1682
+ // Ensure offset doesn't go negative
1683
+ this.offset = Math.max(this.offset, 0);
1684
+ if (this.offset > this.size) {
1685
+ if (this.strict == false) {
1686
+ this.extendArray(this.offset - this.size);
1687
+ }
1688
+ else {
1689
+ throw new Error(`Starting offset outside of size: ${this.offset} of ${this.size}`);
1690
+ }
1475
1691
  }
1476
1692
  }
1477
- this.size = data.length;
1478
- this.data = data;
1479
1693
  }
1480
1694
  /**
1481
1695
  *
@@ -2317,6 +2531,116 @@ class bireader {
2317
2531
  this.errorDump = true;
2318
2532
  }
2319
2533
  //
2534
+ //find
2535
+ //
2536
+ /**
2537
+ * Searches for byte position of string from current read position.
2538
+ *
2539
+ * Returns -1 if not found.
2540
+ *
2541
+ * Does not change current read position.
2542
+ *
2543
+ * @param {string} string - String to search for.
2544
+ */
2545
+ findString(string) {
2546
+ return fString(this, string);
2547
+ }
2548
+ /**
2549
+ * Searches for byte value (can be signed or unsigned) position from current read position.
2550
+ *
2551
+ * Returns -1 if not found.
2552
+ *
2553
+ * Does not change current read position.
2554
+ *
2555
+ * @param {number} value - Number to search for.
2556
+ * @param {boolean} unsigned - If the number is unsigned (default true)
2557
+ * @param {string} endian - endianness of value (default set endian).
2558
+ */
2559
+ findByte(value, unsigned, endian) {
2560
+ return fNumber(this, value, 8, unsigned == undefined ? true : unsigned, endian);
2561
+ }
2562
+ /**
2563
+ * Searches for short value (can be signed or unsigned) position from current read position.
2564
+ *
2565
+ * Returns -1 if not found.
2566
+ *
2567
+ * Does not change current read position.
2568
+ *
2569
+ * @param {number} value - Number to search for.
2570
+ * @param {boolean} unsigned - If the number is unsigned (default true)
2571
+ * @param {string} endian - endianness of value (default set endian).
2572
+ */
2573
+ findShort(value, unsigned, endian) {
2574
+ return fNumber(this, value, 16, unsigned == undefined ? true : unsigned, endian);
2575
+ }
2576
+ /**
2577
+ * Searches for integer value (can be signed or unsigned) position from current read position.
2578
+ *
2579
+ * Returns -1 if not found.
2580
+ *
2581
+ * Does not change current read position.
2582
+ *
2583
+ * @param {number} value - Number to search for.
2584
+ * @param {boolean} unsigned - If the number is unsigned (default true)
2585
+ * @param {string} endian - endianness of value (default set endian).
2586
+ */
2587
+ findInt(value, unsigned, endian) {
2588
+ return fNumber(this, value, 32, unsigned == undefined ? true : unsigned, endian);
2589
+ }
2590
+ /**
2591
+ * Searches for half float value position from current read position.
2592
+ *
2593
+ * Returns -1 if not found.
2594
+ *
2595
+ * Does not change current read position.
2596
+ *
2597
+ * @param {number} value - Number to search for.
2598
+ * @param {string} endian - endianness of value (default set endian).
2599
+ */
2600
+ findHalfFloat(value, endian) {
2601
+ return fHalfFloat(this, value, endian);
2602
+ }
2603
+ /**
2604
+ * Searches for float value position from current read position.
2605
+ *
2606
+ * Returns -1 if not found.
2607
+ *
2608
+ * Does not change current read position.
2609
+ *
2610
+ * @param {number} value - Number to search for.
2611
+ * @param {string} endian - endianness of value (default set endian).
2612
+ */
2613
+ findFloat(value, endian) {
2614
+ return fFloat(this, value, endian);
2615
+ }
2616
+ /**
2617
+ * Searches for 64 bit value (can be signed or unsigned) position from current read position.
2618
+ *
2619
+ * Returns -1 if not found.
2620
+ *
2621
+ * Does not change current read position.
2622
+ *
2623
+ * @param {number} value - Number to search for.
2624
+ * @param {boolean} unsigned - If the number is unsigned (default true)
2625
+ * @param {string} endian - endianness of value (default set endian).
2626
+ */
2627
+ findInt64(value, unsigned, endian) {
2628
+ return fBigInt(this, value, unsigned == undefined ? true : unsigned, endian);
2629
+ }
2630
+ /**
2631
+ * Searches for double float value position from current read position.
2632
+ *
2633
+ * Returns -1 if not found.
2634
+ *
2635
+ * Does not change current read position.
2636
+ *
2637
+ * @param {number} value - Number to search for.
2638
+ * @param {string} endian - endianness of value (default set endian).
2639
+ */
2640
+ findDoubleFloat(value, endian) {
2641
+ return fDoubleFloat(this, value, endian);
2642
+ }
2643
+ //
2320
2644
  //bit reader
2321
2645
  //
2322
2646
  /**
@@ -5897,42 +6221,55 @@ class biwriter {
5897
6221
  this.strict = false;
5898
6222
  this.errorDump = true;
5899
6223
  this.data = [];
5900
- if (endianness != undefined && typeof endianness != "string") {
5901
- throw new Error("endianness must be big or little");
5902
- }
5903
- if (endianness != undefined && !(endianness == "big" || endianness == "little")) {
5904
- throw new Error("Endianness must be big or little");
5905
- }
5906
- this.endian = endianness || "little";
5907
- if (byteOffset != undefined) {
5908
- if (typeof byteOffset == "number") {
5909
- this.offset = Math.round(byteOffset) || 0;
6224
+ if (data == undefined) {
6225
+ if (typeof Buffer !== 'undefined') {
6226
+ this.data = Buffer.alloc(this.offset || 1 + (this.bitoffset != 0 ? 1 : 0));
5910
6227
  }
5911
6228
  else {
5912
- throw new Error("Byte offset must be number");
6229
+ this.data = new Uint8Array(this.offset || 1 + (this.bitoffset != 0 ? 1 : 0));
5913
6230
  }
5914
6231
  }
5915
- if (bitOffset != undefined) {
5916
- this.bitoffset = (bitOffset % 8);
6232
+ else {
6233
+ if (!this.isBufferOrUint8Array(data)) {
6234
+ throw new Error("Write data must be Uint8Array or Buffer.");
6235
+ }
6236
+ this.data = data;
5917
6237
  }
6238
+ this.size = this.data.length;
5918
6239
  if (typeof strict == "boolean") {
5919
6240
  this.strict = strict;
5920
6241
  }
5921
6242
  else {
5922
6243
  if (strict != undefined) {
5923
- throw new Error("Strict mode must be true of false");
6244
+ throw new Error("Strict mode must be true of false.");
5924
6245
  }
5925
6246
  }
5926
- if (data == undefined) {
5927
- throw new Error("Data required");
6247
+ if (endianness != undefined && typeof endianness != "string") {
6248
+ throw new Error("endianness must be big or little.");
5928
6249
  }
5929
- else {
5930
- if (!this.isBufferOrUint8Array(data)) {
5931
- throw new Error("Write data must be Uint8Array or Buffer");
6250
+ if (endianness != undefined && !(endianness == "big" || endianness == "little")) {
6251
+ throw new Error("Endianness must be big or little.");
6252
+ }
6253
+ this.endian = endianness || "little";
6254
+ if (byteOffset != undefined || bitOffset != undefined) {
6255
+ this.offset = ((Math.abs(byteOffset || 0)) + Math.ceil((Math.abs(bitOffset || 0)) / 8));
6256
+ // Adjust byte offset based on bit overflow
6257
+ this.offset += Math.floor((Math.abs(bitOffset || 0)) / 8);
6258
+ // Adjust bit offset
6259
+ this.bitoffset = (Math.abs(bitOffset || 0) + 64) % 8;
6260
+ // Ensure bit offset stays between 0-7
6261
+ this.bitoffset = Math.min(Math.max(this.bitoffset, 0), 7);
6262
+ // Ensure offset doesn't go negative
6263
+ this.offset = Math.max(this.offset, 0);
6264
+ if (this.offset > this.size) {
6265
+ if (this.strict == false) {
6266
+ this.extendArray(this.offset - this.size);
6267
+ }
6268
+ else {
6269
+ throw new Error(`Starting offset outside of size: ${this.offset} of ${this.size}`);
6270
+ }
5932
6271
  }
5933
6272
  }
5934
- this.data = data;
5935
- this.size = this.data.length;
5936
6273
  }
5937
6274
  /**
5938
6275
  * Change Endian (default little)
@@ -6780,6 +7117,116 @@ class biwriter {
6780
7117
  this.errorDump = true;
6781
7118
  }
6782
7119
  //
7120
+ //find
7121
+ //
7122
+ /**
7123
+ * Searches for byte position of string from current read position.
7124
+ *
7125
+ * Returns -1 if not found.
7126
+ *
7127
+ * Does not change current read position.
7128
+ *
7129
+ * @param {string} string - String to search for.
7130
+ */
7131
+ findString(string) {
7132
+ return fString(this, string);
7133
+ }
7134
+ /**
7135
+ * Searches for byte value (can be signed or unsigned) position from current read position.
7136
+ *
7137
+ * Returns -1 if not found.
7138
+ *
7139
+ * Does not change current read position.
7140
+ *
7141
+ * @param {number} value - Number to search for.
7142
+ * @param {boolean} unsigned - If the number is unsigned (default true)
7143
+ * @param {string} endian - endianness of value (default set endian).
7144
+ */
7145
+ findByte(value, unsigned, endian) {
7146
+ return fNumber(this, value, 8, unsigned == undefined ? true : unsigned, endian);
7147
+ }
7148
+ /**
7149
+ * Searches for short value (can be signed or unsigned) position from current read position.
7150
+ *
7151
+ * Returns -1 if not found.
7152
+ *
7153
+ * Does not change current read position.
7154
+ *
7155
+ * @param {number} value - Number to search for.
7156
+ * @param {boolean} unsigned - If the number is unsigned (default true)
7157
+ * @param {string} endian - endianness of value (default set endian).
7158
+ */
7159
+ findShort(value, unsigned, endian) {
7160
+ return fNumber(this, value, 16, unsigned == undefined ? true : unsigned, endian);
7161
+ }
7162
+ /**
7163
+ * Searches for integer value (can be signed or unsigned) position from current read position.
7164
+ *
7165
+ * Returns -1 if not found.
7166
+ *
7167
+ * Does not change current read position.
7168
+ *
7169
+ * @param {number} value - Number to search for.
7170
+ * @param {boolean} unsigned - If the number is unsigned (default true)
7171
+ * @param {string} endian - endianness of value (default set endian).
7172
+ */
7173
+ findInt(value, unsigned, endian) {
7174
+ return fNumber(this, value, 32, unsigned == undefined ? true : unsigned, endian);
7175
+ }
7176
+ /**
7177
+ * Searches for half float value position from current read position.
7178
+ *
7179
+ * Returns -1 if not found.
7180
+ *
7181
+ * Does not change current read position.
7182
+ *
7183
+ * @param {number} value - Number to search for.
7184
+ * @param {string} endian - endianness of value (default set endian).
7185
+ */
7186
+ findHalfFloat(value, endian) {
7187
+ return fHalfFloat(this, value, endian);
7188
+ }
7189
+ /**
7190
+ * Searches for float value position from current read position.
7191
+ *
7192
+ * Returns -1 if not found.
7193
+ *
7194
+ * Does not change current read position.
7195
+ *
7196
+ * @param {number} value - Number to search for.
7197
+ * @param {string} endian - endianness of value (default set endian).
7198
+ */
7199
+ findFloat(value, endian) {
7200
+ return fFloat(this, value, endian);
7201
+ }
7202
+ /**
7203
+ * Searches for 64 bit value (can be signed or unsigned) position from current read position.
7204
+ *
7205
+ * Returns -1 if not found.
7206
+ *
7207
+ * Does not change current read position.
7208
+ *
7209
+ * @param {number} value - Number to search for.
7210
+ * @param {boolean} unsigned - If the number is unsigned (default true)
7211
+ * @param {string} endian - endianness of value (default set endian).
7212
+ */
7213
+ findInt64(value, unsigned, endian) {
7214
+ return fBigInt(this, value, unsigned == undefined ? true : unsigned, endian);
7215
+ }
7216
+ /**
7217
+ * Searches for double float value position from current read position.
7218
+ *
7219
+ * Returns -1 if not found.
7220
+ *
7221
+ * Does not change current read position.
7222
+ *
7223
+ * @param {number} value - Number to search for.
7224
+ * @param {string} endian - endianness of value (default set endian).
7225
+ */
7226
+ findDoubleFloat(value, endian) {
7227
+ return fDoubleFloat(this, value, endian);
7228
+ }
7229
+ //
6783
7230
  //bit writer
6784
7231
  //
6785
7232
  /**