bireader 1.0.43 → 1.0.44

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.');
@@ -1447,16 +1653,16 @@ class bireader {
1447
1653
  throw new Error("Byte order must be big or little");
1448
1654
  }
1449
1655
  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;
1656
+ if (byteOffset != undefined || bitOffset != undefined) {
1657
+ this.offset = ((Math.abs(byteOffset || 0)) + Math.ceil((Math.abs(bitOffset || 0)) / 8));
1658
+ // Adjust byte offset based on bit overflow
1659
+ this.offset += Math.floor((Math.abs(bitOffset || 0)) / 8);
1660
+ // Adjust bit offset
1661
+ this.bitoffset = (Math.abs(bitOffset || 0) + 64) % 8;
1662
+ // Ensure bit offset stays between 0-7
1663
+ this.bitoffset = Math.min(Math.max(this.bitoffset, 0), 7);
1664
+ // Ensure offset doesn't go negative
1665
+ this.offset = Math.max(this.offset, 0);
1460
1666
  }
1461
1667
  if (typeof strict == "boolean") {
1462
1668
  this.strict = strict;
@@ -2317,6 +2523,116 @@ class bireader {
2317
2523
  this.errorDump = true;
2318
2524
  }
2319
2525
  //
2526
+ //find
2527
+ //
2528
+ /**
2529
+ * Searches for byte position of string from current read position.
2530
+ *
2531
+ * Returns -1 if not found.
2532
+ *
2533
+ * Does not change current read position.
2534
+ *
2535
+ * @param {string} string - String to search for.
2536
+ */
2537
+ findString(string) {
2538
+ return fString(this, string);
2539
+ }
2540
+ /**
2541
+ * Searches for byte value (can be signed or unsigned) position from current read position.
2542
+ *
2543
+ * Returns -1 if not found.
2544
+ *
2545
+ * Does not change current read position.
2546
+ *
2547
+ * @param {number} value - Number to search for.
2548
+ * @param {boolean} unsigned - If the number is unsigned (default true)
2549
+ * @param {string} endian - endianness of value (default set endian).
2550
+ */
2551
+ findByte(value, unsigned, endian) {
2552
+ return fNumber(this, value, 8, unsigned == undefined ? true : unsigned, endian);
2553
+ }
2554
+ /**
2555
+ * Searches for short value (can be signed or unsigned) position from current read position.
2556
+ *
2557
+ * Returns -1 if not found.
2558
+ *
2559
+ * Does not change current read position.
2560
+ *
2561
+ * @param {number} value - Number to search for.
2562
+ * @param {boolean} unsigned - If the number is unsigned (default true)
2563
+ * @param {string} endian - endianness of value (default set endian).
2564
+ */
2565
+ findShort(value, unsigned, endian) {
2566
+ return fNumber(this, value, 16, unsigned == undefined ? true : unsigned, endian);
2567
+ }
2568
+ /**
2569
+ * Searches for integer value (can be signed or unsigned) position from current read position.
2570
+ *
2571
+ * Returns -1 if not found.
2572
+ *
2573
+ * Does not change current read position.
2574
+ *
2575
+ * @param {number} value - Number to search for.
2576
+ * @param {boolean} unsigned - If the number is unsigned (default true)
2577
+ * @param {string} endian - endianness of value (default set endian).
2578
+ */
2579
+ findInt(value, unsigned, endian) {
2580
+ return fNumber(this, value, 32, unsigned == undefined ? true : unsigned, endian);
2581
+ }
2582
+ /**
2583
+ * Searches for half float value position from current read position.
2584
+ *
2585
+ * Returns -1 if not found.
2586
+ *
2587
+ * Does not change current read position.
2588
+ *
2589
+ * @param {number} value - Number to search for.
2590
+ * @param {string} endian - endianness of value (default set endian).
2591
+ */
2592
+ findHalfFloat(value, endian) {
2593
+ return fHalfFloat(this, value, endian);
2594
+ }
2595
+ /**
2596
+ * Searches for float value position from current read position.
2597
+ *
2598
+ * Returns -1 if not found.
2599
+ *
2600
+ * Does not change current read position.
2601
+ *
2602
+ * @param {number} value - Number to search for.
2603
+ * @param {string} endian - endianness of value (default set endian).
2604
+ */
2605
+ findFloat(value, endian) {
2606
+ return fFloat(this, value, endian);
2607
+ }
2608
+ /**
2609
+ * Searches for 64 bit value (can be signed or unsigned) position from current read position.
2610
+ *
2611
+ * Returns -1 if not found.
2612
+ *
2613
+ * Does not change current read position.
2614
+ *
2615
+ * @param {number} value - Number to search for.
2616
+ * @param {boolean} unsigned - If the number is unsigned (default true)
2617
+ * @param {string} endian - endianness of value (default set endian).
2618
+ */
2619
+ findInt64(value, unsigned, endian) {
2620
+ return fBigInt(this, value, unsigned == undefined ? true : unsigned, endian);
2621
+ }
2622
+ /**
2623
+ * Searches for double float value position from current read position.
2624
+ *
2625
+ * Returns -1 if not found.
2626
+ *
2627
+ * Does not change current read position.
2628
+ *
2629
+ * @param {number} value - Number to search for.
2630
+ * @param {string} endian - endianness of value (default set endian).
2631
+ */
2632
+ findDoubleFloat(value, endian) {
2633
+ return fDoubleFloat(this, value, endian);
2634
+ }
2635
+ //
2320
2636
  //bit reader
2321
2637
  //
2322
2638
  /**
@@ -5904,16 +6220,16 @@ class biwriter {
5904
6220
  throw new Error("Endianness must be big or little");
5905
6221
  }
5906
6222
  this.endian = endianness || "little";
5907
- if (byteOffset != undefined) {
5908
- if (typeof byteOffset == "number") {
5909
- this.offset = Math.round(byteOffset) || 0;
5910
- }
5911
- else {
5912
- throw new Error("Byte offset must be number");
5913
- }
5914
- }
5915
- if (bitOffset != undefined) {
5916
- this.bitoffset = (bitOffset % 8);
6223
+ if (byteOffset != undefined || bitOffset != undefined) {
6224
+ this.offset = ((Math.abs(byteOffset || 0)) + Math.ceil((Math.abs(bitOffset || 0)) / 8));
6225
+ // Adjust byte offset based on bit overflow
6226
+ this.offset += Math.floor((Math.abs(bitOffset || 0)) / 8);
6227
+ // Adjust bit offset
6228
+ this.bitoffset = (Math.abs(bitOffset || 0) + 64) % 8;
6229
+ // Ensure bit offset stays between 0-7
6230
+ this.bitoffset = Math.min(Math.max(this.bitoffset, 0), 7);
6231
+ // Ensure offset doesn't go negative
6232
+ this.offset = Math.max(this.offset, 0);
5917
6233
  }
5918
6234
  if (typeof strict == "boolean") {
5919
6235
  this.strict = strict;
@@ -5924,7 +6240,12 @@ class biwriter {
5924
6240
  }
5925
6241
  }
5926
6242
  if (data == undefined) {
5927
- throw new Error("Data required");
6243
+ if (typeof Buffer !== 'undefined') {
6244
+ this.data = Buffer.alloc(this.offset || 1 + (this.bitoffset != 0 ? 1 : 0));
6245
+ }
6246
+ else {
6247
+ this.data = new Uint8Array(this.offset || 1 + (this.bitoffset != 0 ? 1 : 0));
6248
+ }
5928
6249
  }
5929
6250
  else {
5930
6251
  if (!this.isBufferOrUint8Array(data)) {
@@ -6780,6 +7101,116 @@ class biwriter {
6780
7101
  this.errorDump = true;
6781
7102
  }
6782
7103
  //
7104
+ //find
7105
+ //
7106
+ /**
7107
+ * Searches for byte position of string from current read position.
7108
+ *
7109
+ * Returns -1 if not found.
7110
+ *
7111
+ * Does not change current read position.
7112
+ *
7113
+ * @param {string} string - String to search for.
7114
+ */
7115
+ findString(string) {
7116
+ return fString(this, string);
7117
+ }
7118
+ /**
7119
+ * Searches for byte value (can be signed or unsigned) position from current read position.
7120
+ *
7121
+ * Returns -1 if not found.
7122
+ *
7123
+ * Does not change current read position.
7124
+ *
7125
+ * @param {number} value - Number to search for.
7126
+ * @param {boolean} unsigned - If the number is unsigned (default true)
7127
+ * @param {string} endian - endianness of value (default set endian).
7128
+ */
7129
+ findByte(value, unsigned, endian) {
7130
+ return fNumber(this, value, 8, unsigned == undefined ? true : unsigned, endian);
7131
+ }
7132
+ /**
7133
+ * Searches for short value (can be signed or unsigned) position from current read position.
7134
+ *
7135
+ * Returns -1 if not found.
7136
+ *
7137
+ * Does not change current read position.
7138
+ *
7139
+ * @param {number} value - Number to search for.
7140
+ * @param {boolean} unsigned - If the number is unsigned (default true)
7141
+ * @param {string} endian - endianness of value (default set endian).
7142
+ */
7143
+ findShort(value, unsigned, endian) {
7144
+ return fNumber(this, value, 16, unsigned == undefined ? true : unsigned, endian);
7145
+ }
7146
+ /**
7147
+ * Searches for integer value (can be signed or unsigned) position from current read position.
7148
+ *
7149
+ * Returns -1 if not found.
7150
+ *
7151
+ * Does not change current read position.
7152
+ *
7153
+ * @param {number} value - Number to search for.
7154
+ * @param {boolean} unsigned - If the number is unsigned (default true)
7155
+ * @param {string} endian - endianness of value (default set endian).
7156
+ */
7157
+ findInt(value, unsigned, endian) {
7158
+ return fNumber(this, value, 32, unsigned == undefined ? true : unsigned, endian);
7159
+ }
7160
+ /**
7161
+ * Searches for half float value position from current read position.
7162
+ *
7163
+ * Returns -1 if not found.
7164
+ *
7165
+ * Does not change current read position.
7166
+ *
7167
+ * @param {number} value - Number to search for.
7168
+ * @param {string} endian - endianness of value (default set endian).
7169
+ */
7170
+ findHalfFloat(value, endian) {
7171
+ return fHalfFloat(this, value, endian);
7172
+ }
7173
+ /**
7174
+ * Searches for float value position from current read position.
7175
+ *
7176
+ * Returns -1 if not found.
7177
+ *
7178
+ * Does not change current read position.
7179
+ *
7180
+ * @param {number} value - Number to search for.
7181
+ * @param {string} endian - endianness of value (default set endian).
7182
+ */
7183
+ findFloat(value, endian) {
7184
+ return fFloat(this, value, endian);
7185
+ }
7186
+ /**
7187
+ * Searches for 64 bit value (can be signed or unsigned) position from current read position.
7188
+ *
7189
+ * Returns -1 if not found.
7190
+ *
7191
+ * Does not change current read position.
7192
+ *
7193
+ * @param {number} value - Number to search for.
7194
+ * @param {boolean} unsigned - If the number is unsigned (default true)
7195
+ * @param {string} endian - endianness of value (default set endian).
7196
+ */
7197
+ findInt64(value, unsigned, endian) {
7198
+ return fBigInt(this, value, unsigned == undefined ? true : unsigned, endian);
7199
+ }
7200
+ /**
7201
+ * Searches for double float value position from current read position.
7202
+ *
7203
+ * Returns -1 if not found.
7204
+ *
7205
+ * Does not change current read position.
7206
+ *
7207
+ * @param {number} value - Number to search for.
7208
+ * @param {string} endian - endianness of value (default set endian).
7209
+ */
7210
+ findDoubleFloat(value, endian) {
7211
+ return fDoubleFloat(this, value, endian);
7212
+ }
7213
+ //
6783
7214
  //bit writer
6784
7215
  //
6785
7216
  /**