bireader 1.0.4 → 1.0.6

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/README.md CHANGED
@@ -173,6 +173,16 @@ Common functions for setup and movement shared by both (unless indicated).
173
173
  <tr>
174
174
  <td>Aliases</td>
175
175
  <td>clip(startOffset, endOffset)<br>truncate(startOffset, endOffset)<br>slice(startOffset, endOffset)</td>
176
+ </tr>
177
+ <tr>
178
+ <td>Name</td>
179
+ <td>extract(<b>length</b>)</td>
180
+ <td align="center" rowspan="2">length of data from current position</td>
181
+ <td rowspan="2">Returns data from current read position to supplied length. <br><b>Note:</b> Does not affect supplied data or current read position.</td>
182
+ </tr>
183
+ <tr>
184
+ <td>Aliases</td>
185
+ <td>wrap(<b>length</b>)<br>lift(<b>length</b>)</td>
176
186
  </tr>
177
187
  <tr>
178
188
  <td>Name</td>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bireader",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Read and write data in binary",
5
5
  "main": "index.js",
6
6
  "directories": {
package/src/reader.js CHANGED
@@ -21,10 +21,6 @@ class bireader {
21
21
  }
22
22
  }
23
23
 
24
- #flip = function(unsignedValue){
25
- return unsignedValue > 127 ? unsignedValue - 256 : unsignedValue;
26
- }
27
-
28
24
  /**
29
25
  *
30
26
  * byte reader, includes bitfields and strings
@@ -148,6 +144,18 @@ class bireader {
148
144
  clip = this.crop = this.truncate = this.slice = function(startOffset, endOffset){
149
145
  return this.data.slice(startOffset || 0, endOffset || this.offset)
150
146
  }
147
+
148
+ /**
149
+ * Extract array from current position to length supplied
150
+ * Note: Does not affect supplied data
151
+ * @param {number} length - length of data to copy from current offset
152
+ */
153
+ extract = this.wrap = this.lift = function(length){
154
+ if(this.offset + (length ||0) > this.size){
155
+ throw new Error("End offset outside of data: " + this.size)
156
+ }
157
+ return this.data.slice(this.offset, this.offset + (length ||0))
158
+ }
151
159
 
152
160
  /**
153
161
  * Returns current data
@@ -2477,12 +2485,12 @@ class bireader {
2477
2485
  */
2478
2486
  readByte = this.byte = this.int8 = function(unsigned){
2479
2487
  this.#check_size(1)
2480
- const read = this.#flip(this.data[this.offset])
2488
+ var read = this.data[this.offset]
2481
2489
  this.offset += 1
2482
2490
  if(unsigned == true){
2483
2491
  return read & 0xFF
2484
2492
  } else {
2485
- return read
2493
+ return read > 127 ? read - 256 : read;
2486
2494
  }
2487
2495
  }
2488
2496
 
@@ -2510,13 +2518,13 @@ class bireader {
2510
2518
  this.#check_size(2)
2511
2519
  var read
2512
2520
  if((endian != undefined ? endian : this.endian) == "little"){
2513
- read = (this.#flip(this.data[this.offset + 1]) << 8) | this.#flip(this.data[this.offset]);
2521
+ read = ((this.data[this.offset + 1] & 0xFFFF) << 8) | (this.data[this.offset]& 0xFFFF);
2514
2522
  } else {
2515
- read = (this.#flip(this.data[this.offset]) << 8) | this.#flip(this.data[this.offset + 1]);
2523
+ read = ((this.data[this.offset]& 0xFFFF) << 8) | (this.data[this.offset + 1] & 0xFFFF);
2516
2524
  }
2517
2525
  this.offset += 2
2518
2526
  if(unsigned == undefined || unsigned == false){
2519
- return read
2527
+ return read & 0x8000 ? -(0x10000 - read) : read
2520
2528
  } else {
2521
2529
  return read & 0xFFFF
2522
2530
  }
@@ -2781,7 +2789,7 @@ class bireader {
2781
2789
  let value = BigInt(0);
2782
2790
  if((endian == undefined ? this.endian : endian) == "little"){
2783
2791
  for (let i = 0; i < 8; i++) {
2784
- value |= BigInt(this.#flip(this.data[this.offset])) << BigInt(8 * i);
2792
+ value |= BigInt((this.data[this.offset] & 0xFF)) << BigInt(8 * i);
2785
2793
  this.offset += 1
2786
2794
  }
2787
2795
  if(unsigned == undefined || unsigned == false){
@@ -2794,7 +2802,7 @@ class bireader {
2794
2802
  }
2795
2803
  } else {
2796
2804
  for (let i = 0; i < 8; i++) {
2797
- value = (value << BigInt(8)) | BigInt(this.#flip(this.data[this.offset]));
2805
+ value = (value << BigInt(8)) | BigInt((this.data[this.offset] & 0xFF));
2798
2806
  this.offset += 1
2799
2807
  }
2800
2808
  if(unsigned == undefined || unsigned == false){
package/src/writer.js CHANGED
@@ -213,6 +213,23 @@ class biwriter {
213
213
  }
214
214
  return this.data.slice(Math.abs(startOffset || 0), endOffset || this.offset)
215
215
  }
216
+
217
+ /**
218
+ * Extract array from current position to length supplied
219
+ * Note: Does not affect supplied data
220
+ * Note: Will extend array if strict mode is off
221
+ * @param {number} length - length of data to copy from current offset
222
+ */
223
+ extract = this.wrap = this.lift = function(length){
224
+ if(this.offset + (length ||0) > this.size){
225
+ if(this.strict == false){
226
+ this.extendArray(this.offset + (length ||0) - this.size)
227
+ } else {
228
+ throw new Error("End offset outside of data: " + this.size)
229
+ }
230
+ }
231
+ return this.data.slice(this.offset, this.offset + (length ||0))
232
+ }
216
233
 
217
234
  /**
218
235
  * Returns current data