bireader 1.0.39 → 1.0.41

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
@@ -80,8 +80,11 @@ function alignRev(_this, n) {
80
80
  _this.skip(a * -1);
81
81
  }
82
82
  }
83
- function goto(_this, byte, bit) {
84
- const new_size = (byte + Math.ceil((bit || 0) / 8));
83
+ function goto(_this, bytes, bits) {
84
+ var new_size = (((bytes || 0)) + Math.ceil(((bits || 0)) / 8));
85
+ if (bits && bits < 0) {
86
+ new_size = Math.floor(((((bytes || 0)) * 8) + (bits || 0)) / 8);
87
+ }
85
88
  if (new_size > _this.size) {
86
89
  if (_this.strict == false) {
87
90
  _this.extendArray(new_size - _this.size);
@@ -91,8 +94,15 @@ function goto(_this, byte, bit) {
91
94
  throw new Error("\x1b[33m[Strict mode]\x1b[0m: Goto utside of range of data: goto " + new_size + " of " + _this.size);
92
95
  }
93
96
  }
94
- _this.offset = byte;
95
- _this.bitoffset = (bit || 0) % 8;
97
+ _this.offset = bytes;
98
+ // Adjust byte offset based on bit overflow
99
+ _this.offset += Math.floor(((bits || 0)) / 8);
100
+ // Adjust bit offset
101
+ _this.bitoffset = ((bits || 0) + 64) % 8;
102
+ // Ensure bit offset stays between 0-7
103
+ _this.bitoffset = Math.min(Math.max(_this.bitoffset, 0), 7);
104
+ // Ensure offset doesn't go negative
105
+ _this.offset = Math.max(_this.offset, 0);
96
106
  }
97
107
  function remove(_this, startOffset, endOffset, consume, remove, fillValue) {
98
108
  const new_start = Math.abs(startOffset || 0);
@@ -147,7 +157,7 @@ function remove(_this, startOffset, endOffset, consume, remove, fillValue) {
147
157
  }
148
158
  return data_removed;
149
159
  }
150
- function addData(_this, data, consume, offset, repalce) {
160
+ function addData(_this, data, consume, offset, replace) {
151
161
  if (_this.strict == true) {
152
162
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
153
163
  throw new Error(`\x1b[33m[Strict mode]\x1b[0m: Can not insert data in strict mode. Use unrestrict() to enable.`);
@@ -159,7 +169,7 @@ function addData(_this, data, consume, offset, repalce) {
159
169
  throw new Error("Data insert must be a Uint8Array");
160
170
  }
161
171
  var needed_size = offset || _this.offset;
162
- if (repalce) {
172
+ if (replace) {
163
173
  needed_size = offset || _this.offset + data.length;
164
174
  }
165
175
  if (needed_size > _this.size) {
@@ -172,7 +182,7 @@ function addData(_this, data, consume, offset, repalce) {
172
182
  }
173
183
  _this.size = _this.data.length;
174
184
  }
175
- if (repalce) {
185
+ if (replace) {
176
186
  if (isBuffer(_this.data)) {
177
187
  const part1 = _this.data.subarray(0, needed_size - data.length);
178
188
  const part2 = _this.data.subarray(needed_size, _this.size);
@@ -664,6 +674,9 @@ function wbit(_this, value, bits, unsigned, endian) {
664
674
  if (bits == undefined) {
665
675
  throw new Error("Enter number of bits to write");
666
676
  }
677
+ if (bits == 0) {
678
+ return;
679
+ }
667
680
  if (bits <= 0 || bits > 32) {
668
681
  throw new Error('Bit length must be between 1 and 32.');
669
682
  }
@@ -721,6 +734,9 @@ function rbit(_this, bits, unsigned, endian) {
721
734
  if (bits == undefined || typeof bits != "number") {
722
735
  throw new Error("Enter number of bits to read");
723
736
  }
737
+ if (bits == 0) {
738
+ return 0;
739
+ }
724
740
  if (bits <= 0 || bits > 32) {
725
741
  throw new Error('Bit length must be between 1 and 32.');
726
742
  }
@@ -1455,7 +1471,7 @@ export class bireader {
1455
1471
  throw new Error("Write data must be Uint8Array or Buffer");
1456
1472
  }
1457
1473
  }
1458
- this.size = data.length + ((bitOffset || 0) % 8);
1474
+ this.size = data.length;
1459
1475
  this.data = data;
1460
1476
  }
1461
1477
  /**
@@ -1539,7 +1555,7 @@ export class bireader {
1539
1555
  * Note: Will extend array if strict mode is off and outside of max size
1540
1556
  *
1541
1557
  * @param {number} bytes - Bytes to skip
1542
- * @param {number} bits - Bits to skip (0-7)
1558
+ * @param {number} bits - Bits to skip
1543
1559
  */
1544
1560
  skip(bytes, bits) {
1545
1561
  return skip(this, bytes, bits);
@@ -1549,7 +1565,7 @@ export class bireader {
1549
1565
  * Note: Will extend array if strict mode is off and outside of max size
1550
1566
  *
1551
1567
  * @param {number} bytes - Bytes to skip
1552
- * @param {number} bits - Bits to skip (0-7)
1568
+ * @param {number} bits - Bits to skip
1553
1569
  */
1554
1570
  jump(bytes, bits) {
1555
1571
  this.skip(bytes, bits);
@@ -1562,7 +1578,7 @@ export class bireader {
1562
1578
  * Note: Will extend array if strict mode is off and outside of max size
1563
1579
  *
1564
1580
  * @param {number} byte - byte to set to
1565
- * @param {number} bit - bit to set to (0-7)
1581
+ * @param {number} bit - bit to set to
1566
1582
  */
1567
1583
  goto(byte, bit) {
1568
1584
  return goto(this, byte, bit);
@@ -1572,7 +1588,7 @@ export class bireader {
1572
1588
  * Note: Will extend array if strict mode is off and outside of max size
1573
1589
  *
1574
1590
  * @param {number} bytes - Bytes to skip
1575
- * @param {number} bits - Bits to skip (0-7)
1591
+ * @param {number} bits - Bits to skip
1576
1592
  */
1577
1593
  seek(bytes, bits) {
1578
1594
  return this.skip(bytes, bits);
@@ -1582,7 +1598,7 @@ export class bireader {
1582
1598
  * Note: Will extend array if strict mode is off and outside of max size
1583
1599
  *
1584
1600
  * @param {number} byte - byte to set to
1585
- * @param {number} bit - bit to set to (0-7)
1601
+ * @param {number} bit - bit to set to
1586
1602
  */
1587
1603
  pointer(byte, bit) {
1588
1604
  return this.goto(byte, bit);
@@ -1592,7 +1608,7 @@ export class bireader {
1592
1608
  * Note: Will extend array if strict mode is off and outside of max size
1593
1609
  *
1594
1610
  * @param {number} byte - byte to set to
1595
- * @param {number} bit - bit to set to (0-7)
1611
+ * @param {number} bit - bit to set to
1596
1612
  */
1597
1613
  warp(byte, bit) {
1598
1614
  return this.goto(byte, bit);
@@ -1611,8 +1627,7 @@ export class bireader {
1611
1627
  * Set byte and bit position to start of data
1612
1628
  */
1613
1629
  gotostart() {
1614
- this.offset = 0;
1615
- this.bitoffset = 0;
1630
+ return this.rewind();
1616
1631
  }
1617
1632
  //
1618
1633
  //get position
@@ -1631,7 +1646,7 @@ export class bireader {
1631
1646
  * @return {number} current byte position
1632
1647
  */
1633
1648
  getOffset() {
1634
- return this.offset;
1649
+ return this.tell();
1635
1650
  }
1636
1651
  /**
1637
1652
  * Get the current byte position
@@ -1639,7 +1654,7 @@ export class bireader {
1639
1654
  * @return {number} current byte position
1640
1655
  */
1641
1656
  saveOffset() {
1642
- return this.offset;
1657
+ return this.tell();
1643
1658
  }
1644
1659
  /**
1645
1660
  * Get the current bit position (0-7)
@@ -1655,7 +1670,7 @@ export class bireader {
1655
1670
  * @return {number} current bit position
1656
1671
  */
1657
1672
  getOffsetBit() {
1658
- return this.bitoffset;
1673
+ return this.tellB();
1659
1674
  }
1660
1675
  /**
1661
1676
  * Get the current bit position (0-7)
@@ -1671,7 +1686,7 @@ export class bireader {
1671
1686
  * @return {number} current absolute bit position
1672
1687
  */
1673
1688
  tellAbsB() {
1674
- return (this.offset * 8) + this.bitoffset;
1689
+ return this.saveOffsetAbsBit();
1675
1690
  }
1676
1691
  /**
1677
1692
  * Get the current absolute bit position (from start of data)
@@ -1679,7 +1694,7 @@ export class bireader {
1679
1694
  * @return {number} current absolute bit position
1680
1695
  */
1681
1696
  getOffsetAbsBit() {
1682
- return (this.offset * 8) + this.bitoffset;
1697
+ return this.saveOffsetAbsBit();
1683
1698
  }
1684
1699
  /**
1685
1700
  * Get the current absolute bit position (from start of data)
@@ -1687,7 +1702,7 @@ export class bireader {
1687
1702
  * @return {number} current absolute bit position
1688
1703
  */
1689
1704
  saveOffsetBit() {
1690
- return (this.offset * 8) + this.bitoffset;
1705
+ return this.saveOffsetAbsBit();
1691
1706
  }
1692
1707
  //
1693
1708
  //strict mode change
@@ -5913,7 +5928,7 @@ export class biwriter {
5913
5928
  }
5914
5929
  }
5915
5930
  this.data = data;
5916
- this.size = this.data.length + ((bitOffset || 0) % 8);
5931
+ this.size = this.data.length;
5917
5932
  }
5918
5933
  /**
5919
5934
  * Change Endian (default little)
@@ -6074,8 +6089,7 @@ export class biwriter {
6074
6089
  * Set byte and bit position to start of data
6075
6090
  */
6076
6091
  gotostart() {
6077
- this.offset = 0;
6078
- this.bitoffset = 0;
6092
+ return this.rewind();
6079
6093
  }
6080
6094
  //
6081
6095
  //get position
@@ -6094,7 +6108,7 @@ export class biwriter {
6094
6108
  * @return {number} current byte position
6095
6109
  */
6096
6110
  getOffset() {
6097
- return this.offset;
6111
+ return this.tell();
6098
6112
  }
6099
6113
  /**
6100
6114
  * Get the current byte position
@@ -6102,7 +6116,7 @@ export class biwriter {
6102
6116
  * @return {number} current byte position
6103
6117
  */
6104
6118
  saveOffset() {
6105
- return this.offset;
6119
+ return this.tell();
6106
6120
  }
6107
6121
  /**
6108
6122
  * Get the current bit position (0-7)
@@ -6118,7 +6132,7 @@ export class biwriter {
6118
6132
  * @return {number} current bit position
6119
6133
  */
6120
6134
  getOffsetBit() {
6121
- return this.bitoffset;
6135
+ return this.tellB();
6122
6136
  }
6123
6137
  /**
6124
6138
  * Get the current bit position (0-7)
@@ -6134,7 +6148,7 @@ export class biwriter {
6134
6148
  * @return {number} current absolute bit position
6135
6149
  */
6136
6150
  tellAbsB() {
6137
- return (this.offset * 8) + this.bitoffset;
6151
+ return this.saveOffsetAbsBit();
6138
6152
  }
6139
6153
  /**
6140
6154
  * Get the current absolute bit position (from start of data)
@@ -6142,7 +6156,7 @@ export class biwriter {
6142
6156
  * @return {number} current absolute bit position
6143
6157
  */
6144
6158
  getOffsetAbsBit() {
6145
- return (this.offset * 8) + this.bitoffset;
6159
+ return this.saveOffsetAbsBit();
6146
6160
  }
6147
6161
  /**
6148
6162
  * Get the current absolute bit position (from start of data)
@@ -6150,7 +6164,7 @@ export class biwriter {
6150
6164
  * @return {number} current absolute bit position
6151
6165
  */
6152
6166
  saveOffsetBit() {
6153
- return (this.offset * 8) + this.bitoffset;
6167
+ return this.saveOffsetAbsBit();
6154
6168
  }
6155
6169
  //
6156
6170
  //strict mode change
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bireader",
3
- "version": "1.0.39",
3
+ "version": "1.0.41",
4
4
  "description": "Read and write data in binary",
5
5
  "module": "lib/esm/index.mjs",
6
6
  "main": "lib/cjs/index.js",
@@ -19,7 +19,7 @@
19
19
  "./*": "./*"
20
20
  },
21
21
  "scripts": {
22
- "clean": "rmdir /s lib",
22
+ "clean": "rmdir /S /Q lib",
23
23
  "build": "npm run clean && npm run build:esm && npm run move && npm run build:cjs",
24
24
  "build:esm": "tsc --module esnext --outDir lib/esm",
25
25
  "move": "cd ./lib/esm/ && ren index.js index.mjs && cd ../../",