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