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