bireader 1.0.14 → 1.0.15

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.
@@ -1,44 +1,53 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.bireader = void 0;
4
+ const common_1 = require("./common");
4
5
  /**
6
+ * Binary reader, includes bitfields and strings
5
7
  *
6
- * byte reader, includes bitfields and strings
7
- *
8
- * @param {Buffer|Uint8Array} data - ```Buffer``` or ```Uint8Array```
9
- * @param {number} byteOffset - byte offset to start reader, default is 0
10
- * @param {number} bitOffset - bit offset to start reader, 0-7
11
- * @param {string} endianness - endianness ```big``` or ```little``` (default ```little```)
8
+ * @param {Buffer|Uint8Array} data - ```Buffer``` or ```Uint8Array```. Always found in ``biwriter.data``
9
+ * @param {number} byteOffset - Byte offset to start reader (default 0)
10
+ * @param {number} bitOffset - Bit offset 0-7 to start reader (default 0)
11
+ * @param {string} endianness - Endianness ```big``` or ```little``` (default ```little```)
12
+ * @param {boolean} strict - Strict mode: if true does not extend supplied array on outside write (default true)
12
13
  */
13
14
  class bireader {
14
- check_size(read_size, read_bits) {
15
- const new_off = this.offset + (read_size || 0) + Math.ceil((this.bitoffset + (read_bits || 0)) / 8);
16
- if (new_off > this.size) {
17
- this.errorDump ? "[Error], hexdump:\n" + this.hexdump() : "";
18
- throw new Error(`Reader reached end of data: reading to ` + new_off + " at " + this.offset + " of " + this.size);
19
- }
20
- }
21
15
  isBuffer(obj) {
22
16
  return (typeof Buffer !== 'undefined' && obj instanceof Buffer);
23
17
  }
24
18
  isBufferOrUint8Array(obj) {
25
19
  return obj instanceof Uint8Array || this.isBuffer(obj);
26
20
  }
21
+ extendArray(to_padd) {
22
+ if ((typeof Buffer !== 'undefined' && this.data instanceof Buffer)) {
23
+ var paddbuffer = Buffer.alloc(to_padd);
24
+ this.data = Buffer.concat([this.data, paddbuffer]);
25
+ }
26
+ else {
27
+ const addArray = new Array(to_padd);
28
+ this.data = new Uint8Array([...this.data, ...addArray]);
29
+ }
30
+ }
31
+ check_size(read_size, read_bits) {
32
+ return (0, common_1.checkSize)(this, read_size || 0, read_bits || 0, this.offset);
33
+ }
27
34
  /**
35
+ * Binary reader, includes bitfields and strings
28
36
  *
29
- * byte reader, includes bitfields and strings
30
- *
31
- * @param {Buffer|Uint8Array} data - ```Buffer``` or ```Uint8Array```
32
- * @param {number} byteOffset - byte offset to start reader, default is 0
33
- * @param {number} bitOffset - bit offset to start reader, 0-7
34
- * @param {string} endianness - endianness ```big``` or ```little``` (default ```little```)
37
+ * @param {Buffer|Uint8Array} data - ```Buffer``` or ```Uint8Array```. Always found in ``biwriter.data``
38
+ * @param {number} byteOffset - Byte offset to start reader (default 0)
39
+ * @param {number} bitOffset - Bit offset 0-7 to start reader (default 0)
40
+ * @param {string} endianness - Endianness ```big``` or ```little``` (default ```little```)
41
+ * @param {boolean} strict - Strict mode: if true does not extend supplied array on outside write (default true)
35
42
  */
36
- constructor(data, byteOffset, bitOffset, endianness) {
43
+ constructor(data, byteOffset, bitOffset, endianness, strict) {
37
44
  this.endian = "little";
38
45
  this.offset = 0;
39
46
  this.bitoffset = 0;
40
47
  this.size = 0;
48
+ this.strict = false;
41
49
  this.errorDump = true;
50
+ this.data = [];
42
51
  if (endianness != undefined && typeof endianness != "string") {
43
52
  throw new Error("Endian must be big or little");
44
53
  }
@@ -57,6 +66,14 @@ class bireader {
57
66
  if (bitOffset != undefined) {
58
67
  this.bitoffset = bitOffset % 8;
59
68
  }
69
+ if (typeof strict == "boolean") {
70
+ this.strict = strict;
71
+ }
72
+ else {
73
+ if (strict != undefined) {
74
+ throw new Error("Strict mode must be true of false");
75
+ }
76
+ }
60
77
  if (data == undefined) {
61
78
  throw new Error("Data required");
62
79
  }
@@ -121,126 +138,98 @@ class bireader {
121
138
  le() {
122
139
  this.endianness("little");
123
140
  }
141
+ //
142
+ // move from current position
143
+ //
124
144
  /**
125
- * Move current read byte or bit position
145
+ * Offset current byte or bit position
146
+ * Note: Will extend array if strict mode is off and outside of max size
126
147
  *
127
- * @param {number} bytes - bytes to skip
128
- * @param {number} bits - bits to skip
148
+ * @param {number} bytes - Bytes to skip
149
+ * @param {number} bits - Bits to skip (0-7)
129
150
  */
130
151
  skip(bytes, bits) {
131
- this.check_size(bytes || 0);
132
- if ((((bytes || 0) + this.offset) + Math.ceil((this.bitoffset + (bits || 0)) / 8)) > this.size) {
133
- this.errorDump ? "[Error], hexdump:\n" + this.hexdump() : "";
134
- throw new Error("Seek outside of size of data: " + this.size);
135
- }
136
- this.bitoffset += (bits || 0) % 8;
137
- this.offset += (bytes || 0);
152
+ return (0, common_1.skip)(this, bytes, bits);
138
153
  }
139
154
  /**
140
- * Move current read byte or bit position
155
+ * Offset current byte or bit position
156
+ * Note: Will extend array if strict mode is off and outside of max size
141
157
  *
142
- * @param {number} bytes - bytes to skip
143
- * @param {number} bits - bits to skip
158
+ * @param {number} bytes - Bytes to skip
159
+ * @param {number} bits - Bits to skip (0-7)
144
160
  */
145
- fskip(bytes, bits) {
161
+ jump(bytes, bits) {
146
162
  this.skip(bytes, bits);
147
163
  }
164
+ //
165
+ // directly set current position
166
+ //
148
167
  /**
149
- * Change current byte or bit read position
168
+ * Change position directly to address
169
+ * Note: Will extend array if strict mode is off and outside of max size
150
170
  *
151
- * @param {number} byte - byte to jump to
152
- * @param {number} bit - bit to jump to (0-7)
171
+ * @param {number} byte - byte to set to
172
+ * @param {number} bit - bit to set to (0-7)
153
173
  */
154
174
  goto(byte, bit) {
155
- const new_size = (byte + Math.ceil((bit || 0) / 8));
156
- if (new_size > this.size) {
157
- this.errorDump ? "[Error], hexdump:\n" + this.hexdump() : "";
158
- throw new Error("Goto outside of size of data: goto " + new_size + " of " + this.size);
159
- }
160
- this.offset = byte;
161
- this.bitoffset = (bit || 0) % 8;
175
+ return (0, common_1.goto)(this, byte, bit);
162
176
  }
163
177
  /**
164
- * Change current byte or bit read position
178
+ * Change position directly to address
179
+ * Note: Will extend array if strict mode is off and outside of max size
165
180
  *
166
- * @param {number} byte - byte to jump to
167
- * @param {number} bit - bit to jump to (0-7)
181
+ * @param {number} byte - byte to set to
182
+ * @param {number} bit - bit to set to (0-7)
168
183
  */
169
184
  seek(byte, bit) {
170
- this.goto(byte, bit);
171
- }
172
- /**
173
- * Change current byte or bit read position
174
- *
175
- * @param {number} byte - byte to jump to
176
- * @param {number} bit - bit to jump to (0-7)
177
- */
178
- fseek(byte, bit) {
179
- this.goto(byte, bit);
185
+ return this.goto(byte, bit);
180
186
  }
181
187
  /**
182
- * Change current byte or bit read position
188
+ * Change position directly to address
189
+ * Note: Will extend array if strict mode is off and outside of max size
183
190
  *
184
- * @param {number} byte - byte to jump to
185
- * @param {number} bit - bit to jump to (0-7)
186
- */
187
- jump(byte, bit) {
188
- this.goto(byte, bit);
189
- }
190
- /**
191
- * Change current byte or bit read position
192
- *
193
- * @param {number} byte - byte to jump to
194
- * @param {number} bit - bit to jump to (0-7)
191
+ * @param {number} byte - byte to set to
192
+ * @param {number} bit - bit to set to (0-7)
195
193
  */
196
194
  pointer(byte, bit) {
197
- this.goto(byte, bit);
195
+ return this.goto(byte, bit);
198
196
  }
199
197
  /**
200
- * Change current byte or bit read position
198
+ * Change position directly to address
199
+ * Note: Will extend array if strict mode is off and outside of max size
201
200
  *
202
- * @param {number} byte - byte to jump to
203
- * @param {number} bit - bit to jump to (0-7)
201
+ * @param {number} byte - byte to set to
202
+ * @param {number} bit - bit to set to (0-7)
204
203
  */
205
204
  warp(byte, bit) {
206
- this.goto(byte, bit);
207
- }
208
- /**
209
- * Change current byte or bit read position
210
- *
211
- * @param {number} byte - byte to jump to
212
- * @param {number} bit - bit to jump to (0-7)
213
- */
214
- fsetpos(byte, bit) {
215
- this.goto(byte, bit);
205
+ return this.goto(byte, bit);
216
206
  }
207
+ //
208
+ //go to start
209
+ //
217
210
  /**
218
- * Set offset to start of file
211
+ * Set byte and bit position to start of data
219
212
  */
220
213
  rewind() {
221
214
  this.offset = 0;
222
215
  this.bitoffset = 0;
223
216
  }
224
217
  /**
225
- * Set offset to start of file
218
+ * Set byte and bit position to start of data
226
219
  */
227
220
  gotostart() {
228
221
  this.offset = 0;
229
222
  this.bitoffset = 0;
230
223
  }
231
- /**
232
- * Set offset to start of file
233
- */
234
- tostart() {
235
- this.offset = 0;
236
- this.bitoffset = 0;
237
- }
224
+ //
225
+ //get position
226
+ //
238
227
  /**
239
228
  * Get the current byte position
240
229
  *
241
230
  * @return {number} current byte position
242
231
  */
243
- ftell() {
232
+ tell() {
244
233
  return this.offset;
245
234
  }
246
235
  /**
@@ -248,7 +237,7 @@ class bireader {
248
237
  *
249
238
  * @return {number} current byte position
250
239
  */
251
- tell() {
240
+ getOffset() {
252
241
  return this.offset;
253
242
  }
254
243
  /**
@@ -256,113 +245,204 @@ class bireader {
256
245
  *
257
246
  * @return {number} current byte position
258
247
  */
259
- fgetpos() {
248
+ saveOffset() {
260
249
  return this.offset;
261
250
  }
251
+ //
252
+ //strict mode change
253
+ //
262
254
  /**
263
- * Get the current byte position
255
+ * Disallows extending data if position is outside of max size
256
+ */
257
+ restrict() {
258
+ this.strict = true;
259
+ }
260
+ /**
261
+ * Allows extending data if position is outside of max size
262
+ */
263
+ unrestrict() {
264
+ this.strict = false;
265
+ }
266
+ //
267
+ //remove part of data
268
+ //
269
+ /**
270
+ * Deletes part of data from start to current byte position unless supplied, returns removed
271
+ * Note: Errors in strict mode
264
272
  *
265
- * @return {number} current byte position
273
+ * @param {number} startOffset - Start location (default 0)
274
+ * @param {number} endOffset - End location (default current position)
275
+ * @param {boolean} consume - Move position to end of removed data (default false)
276
+ * @returns {Buffer|Uint8Array} Removed data as ``Buffer`` or ``Uint8Array``
266
277
  */
267
- saveOffset() {
268
- return this.offset;
278
+ delete(startOffset, endOffset, consume) {
279
+ return (0, common_1.remove)(this, startOffset || 0, endOffset || this.offset, consume || false, true);
269
280
  }
270
281
  /**
271
- * Truncates array from start to current position unless supplied
272
- * Note: Does not affect supplied data
273
- * @param {number} startOffset - Start location, default 0
274
- * @param {number} endOffset - end location, default current write position
275
- * @returns {Buffer|Uint8Array} ``Buffer`` or ``Uint8Array``
282
+ * Deletes part of data from start to current byte position unless supplied, returns removed
283
+ * Note: Errors in strict mode
284
+ *
285
+ * @param {number} startOffset - Start location (default 0)
286
+ * @param {number} endOffset - End location (default current position)
287
+ * @param {boolean} consume - Move position to end of removed data (default false)
288
+ * @returns {Buffer|Uint8Array} Removed data as ``Buffer`` or ``Uint8Array``
276
289
  */
277
- clip(startOffset, endOffset) {
278
- if ((endOffset || this.offset) > this.size) {
279
- this.errorDump ? "[Error], hexdump:\n" + this.hexdump() : "";
280
- throw new Error("End offset outside of data: endOffset" + endOffset + " of " + this.size);
281
- }
282
- return this.data.slice(startOffset || 0, endOffset || this.offset);
290
+ clip(startOffset, endOffset, consume) {
291
+ return (0, common_1.remove)(this, startOffset || 0, endOffset || this.offset, consume || false, true);
283
292
  }
284
293
  /**
285
- * Truncates array from start to current position unless supplied
286
- * Note: Does not affect supplied data
287
- * @param {number} startOffset - Start location, default 0
288
- * @param {number} endOffset - end location, default current write position
289
- * @returns {Buffer|Uint8Array} ``Buffer`` or ``Uint8Array``
294
+ * Deletes part of data from current byte position to supplied length, returns removed
295
+ * Note: Errors in strict mode
296
+ *
297
+ * @param {number} length - Length of data in bytes to remove
298
+ * @param {boolean} consume - Move position to end of removed data (default false)
299
+ * @returns {Buffer|Uint8Array} Removed data as ``Buffer`` or ``Uint8Array``
290
300
  */
291
- crop(startOffset, endOffset) {
292
- if ((endOffset || this.offset) > this.size) {
293
- this.errorDump ? "[Error], hexdump:\n" + this.hexdump() : "";
294
- throw new Error("End offset outside of data: endOffset" + endOffset + " of " + this.size);
295
- }
296
- return this.data.slice(startOffset || 0, endOffset || this.offset);
301
+ crop(length, consume) {
302
+ return (0, common_1.remove)(this, this.offset, this.offset + (length || 0), consume || false, true);
297
303
  }
298
304
  /**
299
- * Truncates array from start to current position unless supplied
300
- * Note: Does not affect supplied data
301
- * @param {number} startOffset - Start location, default 0
302
- * @param {number} endOffset - end location, default current write position
303
- * @returns {Buffer|Uint8Array} ``Buffer`` or ``Uint8Array``
305
+ * Deletes part of data from current position to supplied length, returns removed
306
+ * Note: Only works in strict mode
307
+ *
308
+ * @param {number} length - Length of data in bytes to remove
309
+ * @param {boolean} consume - Move position to end of removed data (default false)
310
+ * @returns {Buffer|Uint8Array} Removed data as ``Buffer`` or ``Uint8Array``
304
311
  */
305
- truncate(startOffset, endOffset) {
306
- if ((endOffset || this.offset) > this.size) {
307
- this.errorDump ? "[Error], hexdump:\n" + this.hexdump() : "";
308
- throw new Error("End offset outside of data: endOffset" + endOffset + " of " + this.size);
309
- }
310
- return this.data.slice(startOffset || 0, endOffset || this.offset);
312
+ drop(length, consume) {
313
+ return (0, common_1.remove)(this, this.offset, this.offset + (length || 0), consume || false, true);
311
314
  }
315
+ //
316
+ //copy out
317
+ //
312
318
  /**
313
- * Truncates array from start to current position unless supplied
314
- * Note: Does not affect supplied data
315
- * @param {number} startOffset - Start location, default 0
316
- * @param {number} endOffset - end location, default current write position
317
- * @returns {Buffer|Uint8Array} ``Buffer`` or ``Uint8Array``
319
+ * Returns part of data from current byte position to end of data unless supplied
320
+ *
321
+ * @param {number} startOffset - Start location (default current position)
322
+ * @param {number} endOffset - End location (default end of data)
323
+ * @param {boolean} consume - Move position to end of lifted data (default false)
324
+ * @param {number} fillValue - Byte value to to fill returned data (does NOT fill unless supplied)
325
+ * @returns {Buffer|Uint8Array} Selected data as ```Uint8Array``` or ```Buffer```
318
326
  */
319
- slice(startOffset, endOffset) {
320
- if ((endOffset || this.offset) > this.size) {
321
- this.errorDump ? "[Error], hexdump:\n" + this.hexdump() : "";
322
- throw new Error("End offset outside of data: endOffset" + endOffset + " of " + this.size);
323
- }
324
- return this.data.slice(startOffset || 0, endOffset || this.offset);
327
+ lift(startOffset, endOffset, consume, fillValue) {
328
+ return (0, common_1.remove)(this, startOffset || this.offset, endOffset || this.size, consume || false, false, fillValue);
329
+ }
330
+ /**
331
+ * Returns part of data from current byte position to end of data unless supplied
332
+ *
333
+ * @param {number} startOffset - Start location (default current position)
334
+ * @param {number} endOffset - End location (default end of data)
335
+ * @param {boolean} consume - Move position to end of lifted data (default false)
336
+ * @param {number} fillValue - Byte value to to fill returned data (does NOT fill unless supplied)
337
+ * @returns {Buffer|Uint8Array} Selected data as ```Uint8Array``` or ```Buffer```
338
+ */
339
+ fill(startOffset, endOffset, consume, fillValue) {
340
+ return (0, common_1.remove)(this, startOffset || this.offset, endOffset || this.size, consume || false, false, fillValue);
325
341
  }
326
342
  /**
327
- * Extract array from current position to length supplied
343
+ * Extract data from current position to length supplied
328
344
  * Note: Does not affect supplied data
329
- * @param {number} length - length of data to copy from current offset
330
- * @param {number} consume - moves offset to end of length
331
- * @returns {Buffer|Uint8Array} ``Buffer`` or ``Uint8Array``
345
+ *
346
+ * @param {number} length - Length of data in bytes to copy from current offset
347
+ * @param {number} consume - Moves offset to end of length
348
+ * @returns {Buffer|Uint8Array} Selected data as ```Uint8Array``` or ```Buffer```
332
349
  */
333
350
  extract(length, consume) {
334
- if (this.offset + (length || 0) > this.size) {
335
- this.errorDump ? "[Error], hexdump:\n" + this.hexdump() : "";
336
- throw new Error("End offset outside of data: at " + this.offset + " reading " + length + " of" + this.size);
337
- }
338
- const extract = this.data.slice(this.offset, Number(this.offset + (length || 0)));
339
- if (consume) {
340
- this.offset += length;
341
- }
342
- return extract;
351
+ return (0, common_1.remove)(this, this.offset, length || 0, consume || false, false);
343
352
  }
344
353
  /**
345
- * Extract array from current position to length supplied
354
+ * Extract data from current position to length supplied
346
355
  * Note: Does not affect supplied data
347
- * @param {number} length - length of data to copy from current offset
348
- * @param {number} consume - moves offset to end of length
349
- * @returns {Buffer|Uint8Array} ``Buffer`` or ``Uint8Array``
356
+ *
357
+ * @param {number} length - Length of data in bytes to copy from current offset
358
+ * @param {number} consume - Moves offset to end of length
359
+ * @returns {Buffer|Uint8Array} Selected data as ```Uint8Array``` or ```Buffer```
350
360
  */
351
- wrap(length, consume) {
352
- return this.extract(length, consume);
361
+ slice(length, consume) {
362
+ return (0, common_1.remove)(this, this.offset, length || 0, consume || false, false);
353
363
  }
354
364
  /**
355
- * Extract array from current position to length supplied
365
+ * Extract data from current position to length supplied
356
366
  * Note: Does not affect supplied data
357
- * @param {number} length - length of data to copy from current offset
358
- * @param {number} consume - moves offset to end of length
359
- * @returns {Buffer|Uint8Array} ``Buffer`` or ``Uint8Array``
367
+ *
368
+ * @param {number} length - Length of data in bytes to copy from current offset
369
+ * @param {number} consume - Moves offset to end of length
370
+ * @returns {Buffer|Uint8Array} Selected data as ```Uint8Array``` or ```Buffer```
371
+ */
372
+ wrap(length, consume) {
373
+ return (0, common_1.remove)(this, this.offset, length || 0, consume || false, false);
374
+ }
375
+ //
376
+ //insert
377
+ //
378
+ /**
379
+ * Inserts data into data
380
+ * Note: Must be same data type as supplied data. Errors on strict mode.
381
+ *
382
+ * @param {Buffer|Uint8Array} data - ```Uint8Array``` or ```Buffer``` to add to data
383
+ * @param {boolean} consume - Move current write position to end of data (default false)
384
+ * @param {number} offset - Offset to add it at (defaults to current position)
385
+ */
386
+ insert(data, consume, offset) {
387
+ return (0, common_1.addData)(this, data, consume || false, offset || this.offset);
388
+ }
389
+ /**
390
+ * Inserts data into data
391
+ * Note: Must be same data type as supplied data. Errors on strict mode.
392
+ *
393
+ * @param {Buffer|Uint8Array} data - ```Uint8Array``` or ```Buffer``` to add to data
394
+ * @param {boolean} consume - Move current write position to end of data (default false)
395
+ * @param {number} offset - Offset to add it at (defaults to current position)
396
+ */
397
+ place(data, consume, offset) {
398
+ return (0, common_1.addData)(this, data, consume || false, offset || this.offset);
399
+ }
400
+ /**
401
+ * Adds data to start of supplied data
402
+ * Note: Must be same data type as supplied data. Errors on strict mode.
403
+ *
404
+ * @param {Buffer|Uint8Array} data - ```Uint8Array``` or ```Buffer``` to add to data
405
+ * @param {boolean} consume - Move current write position to end of data (default false)
406
+ */
407
+ unshift(data, consume) {
408
+ return (0, common_1.addData)(this, data, consume || false, 0);
409
+ }
410
+ /**
411
+ * Adds data to start of supplied data
412
+ * Note: Must be same data type as supplied data. Errors on strict mode.
413
+ *
414
+ * @param {Buffer|Uint8Array} data - ```Uint8Array``` or ```Buffer``` to add to data
415
+ * @param {boolean} consume - Move current write position to end of data (default false)
416
+ */
417
+ prepend(data, consume) {
418
+ return (0, common_1.addData)(this, data, consume || false, 0);
419
+ }
420
+ /**
421
+ * Adds data to end of supplied data
422
+ * Note: Must be same data type as supplied data. Errors on strict mode.
423
+ *
424
+ * @param {Buffer|Uint8Array} data - ```Uint8Array``` or ```Buffer``` to add to data
425
+ * @param {boolean} consume - Move current write position to end of data (default false)
360
426
  */
361
- lift(length, consume) {
362
- return this.extract(length, consume);
427
+ push(data, consume) {
428
+ return (0, common_1.addData)(this, data, consume || false, this.size);
363
429
  }
364
430
  /**
431
+ * Adds data to end of supplied data
432
+ * Note: Must be same data type as supplied data. Errors on strict mode.
433
+ *
434
+ * @param {Buffer|Uint8Array} data - ```Uint8Array``` or ```Buffer``` to add to data
435
+ * @param {boolean} consume - Move current write position to end of data (default false)
436
+ */
437
+ append(data, consume) {
438
+ return (0, common_1.addData)(this, data, consume || false, this.size);
439
+ }
440
+ //
441
+ //finishing
442
+ //
443
+ /**
365
444
  * Returns current data
445
+ *
366
446
  * @returns {Buffer|Uint8Array} ``Buffer`` or ``Uint8Array``
367
447
  */
368
448
  get() {
@@ -370,46 +450,35 @@ class bireader {
370
450
  }
371
451
  /**
372
452
  * Returns current data
453
+ *
373
454
  * @returns {Buffer|Uint8Array} ``Buffer`` or ``Uint8Array``
374
455
  */
375
456
  return() {
376
457
  return this.data;
377
458
  }
378
459
  /**
379
- * removes reading data
460
+ * removes data
380
461
  */
381
462
  end() {
382
- this.data = [];
463
+ this.data = undefined;
383
464
  }
384
465
  /**
385
- * removes reading data
466
+ * removes data
386
467
  */
387
468
  close() {
388
- this.data = [];
469
+ this.data = undefined;
389
470
  }
390
471
  /**
391
- * removes reading data
472
+ * removes data
392
473
  */
393
474
  done() {
394
- this.data = [];
475
+ this.data = undefined;
395
476
  }
396
477
  /**
397
- * removes reading data
478
+ * removes data
398
479
  */
399
480
  finished() {
400
- this.data = [];
401
- }
402
- /**
403
- * Turn hexdump on error off, default on
404
- */
405
- errorDumpOff() {
406
- this.errorDump = false;
407
- }
408
- /**
409
- * Turn hexdump on error on, default on
410
- */
411
- errorDumpOn() {
412
- this.errorDump = true;
481
+ this.data = undefined;
413
482
  }
414
483
  /**
415
484
  * Console logs data as hex dump
@@ -424,186 +493,19 @@ class bireader {
424
493
  * ```
425
494
  */
426
495
  hexdump(options) {
427
- var length = options && options.length;
428
- var startByte = options && options.startByte;
429
- var supressUnicode = options && options.supressUnicode || false;
430
- if ((startByte || 0) > this.size) {
431
- this.errorDump ? "[Error], hexdump:\n" + this.hexdump() : "";
432
- throw new Error("Hexdump start is outside of data size: " + startByte + " of " + this.size);
433
- }
434
- const start = startByte || this.offset;
435
- const end = Math.min(start + (length || 192), this.size);
436
- if (start + (length || 0) > this.size) {
437
- this.errorDump ? "[Error], hexdump:\n" + this.hexdump() : "";
438
- throw new Error("Hexdump amount is outside of data size: " + (start + (length || 0)) + " of " + end);
439
- }
440
- function hex_check(byte, bits) {
441
- var value = 0;
442
- for (var i = 0; i < bits;) {
443
- var remaining = bits - i;
444
- var bitOffset = 0;
445
- var currentByte = byte;
446
- var read = Math.min(remaining, 8 - bitOffset);
447
- var mask, readBits;
448
- mask = ~(0xFF << read);
449
- readBits = (currentByte >> (8 - read - bitOffset)) & mask;
450
- value <<= read;
451
- value |= readBits;
452
- i += read;
453
- }
454
- value = value >>> 0;
455
- return value;
456
- }
457
- const rows = [];
458
- var header = " 0 1 2 3 4 5 6 7 8 9 A B C D E F ";
459
- var ending = "0123456789ABCDEF";
460
- var addr = "";
461
- for (let i = start; i < end; i += 16) {
462
- addr = i.toString(16).padStart(5, '0');
463
- var row = this.data?.slice(i, i + 16) || [];
464
- var hex = Array.from(row, (byte) => byte.toString(16).padStart(2, '0')).join(' ');
465
- rows.push(`${addr} ${hex.padEnd(47)} `);
466
- }
467
- let result = '';
468
- let make_wide = false;
469
- let i = start;
470
- while (i < end) {
471
- const byte = this.data[i];
472
- if (byte < 32 || byte == 127) {
473
- result += '.';
474
- }
475
- else if (byte < 127) {
476
- // Valid UTF-8 start byte or single-byte character
477
- // Convert the byte to a character and add it to the result
478
- result += String.fromCharCode(byte);
479
- }
480
- else if (supressUnicode) {
481
- result += '.';
482
- }
483
- else if (hex_check(byte, 1) == 0) {
484
- //Byte 1
485
- result += String.fromCharCode(byte);
486
- }
487
- else if (hex_check(byte, 3) == 6) {
488
- //Byte 2
489
- if (i + 1 <= end) {
490
- //check second byte
491
- const byte2 = this.data[i + 1];
492
- if (hex_check(byte2, 2) == 2) {
493
- const charCode = ((byte & 0x1f) << 6) | (byte2 & 0x3f);
494
- i++;
495
- make_wide = true;
496
- const read = " " + String.fromCharCode(charCode);
497
- result += read;
498
- }
499
- else {
500
- result += ".";
501
- }
502
- }
503
- else {
504
- result += ".";
505
- }
506
- }
507
- else if (hex_check(byte, 4) == 14) {
508
- //Byte 3
509
- if (i + 1 <= end) {
510
- //check second byte
511
- const byte2 = this.data[i + 1];
512
- if (hex_check(byte2, 2) == 2) {
513
- if (i + 2 <= end) {
514
- //check third byte
515
- const byte3 = this.data[i + 2];
516
- if (hex_check(byte3, 2) == 2) {
517
- const charCode = ((byte & 0x0f) << 12) |
518
- ((byte2 & 0x3f) << 6) |
519
- (byte3 & 0x3f);
520
- i += 2;
521
- make_wide = true;
522
- const read = " " + String.fromCharCode(charCode);
523
- result += read;
524
- }
525
- else {
526
- i++;
527
- result += " .";
528
- }
529
- }
530
- else {
531
- i++;
532
- result += " .";
533
- }
534
- }
535
- else {
536
- result += ".";
537
- }
538
- }
539
- else {
540
- result += ".";
541
- }
542
- }
543
- else if (hex_check(byte, 5) == 28) {
544
- //Byte 4
545
- if (i + 1 <= end) {
546
- //check second byte
547
- const byte2 = this.data[i + 1];
548
- if (hex_check(byte2, 2) == 2) {
549
- if (i + 2 <= end) {
550
- //check third byte
551
- const byte3 = this.data[i + 2];
552
- if (hex_check(byte3, 2) == 2) {
553
- if (i + 3 <= end) {
554
- //check fourth byte
555
- const byte4 = this.data[i + 2];
556
- if (hex_check(byte4, 2) == 2) {
557
- const charCode = (((byte4 & 0xFF) << 24) | ((byte3 & 0xFF) << 16) | ((byte2 & 0xFF) << 8) | (byte & 0xFF));
558
- i += 3;
559
- make_wide = true;
560
- const read = " " + String.fromCharCode(charCode);
561
- result += read;
562
- }
563
- else {
564
- i += 2;
565
- result += " .";
566
- }
567
- }
568
- else {
569
- i += 2;
570
- result += " .";
571
- }
572
- }
573
- else {
574
- i++;
575
- result += " .";
576
- }
577
- }
578
- else {
579
- i++;
580
- result += " .";
581
- }
582
- }
583
- else {
584
- result += ".";
585
- }
586
- }
587
- else {
588
- result += ".";
589
- }
590
- }
591
- else {
592
- // Invalid UTF-8 byte, add a period to the result
593
- result += '.';
594
- }
595
- i++;
596
- }
597
- const chunks = result.match(new RegExp(`.{1,${16}}`, 'g'));
598
- chunks?.forEach((self, i) => {
599
- rows[i] = rows[i] + (make_wide ? "|" + self + "|" : self);
600
- });
601
- header = "".padStart(addr.length) + header + (make_wide ? "" : ending);
602
- rows.unshift(header);
603
- if (make_wide) {
604
- rows.push("*Removed character byte header on unicode detection");
605
- }
606
- console.log(rows.join("\n"));
496
+ return (0, common_1.hexDump)(this, options);
497
+ }
498
+ /**
499
+ * Turn hexdump on error off (default on)
500
+ */
501
+ errorDumpOff() {
502
+ this.errorDump = false;
503
+ }
504
+ /**
505
+ * Turn hexdump on error on (default on)
506
+ */
507
+ errorDumpOn() {
508
+ this.errorDump = true;
607
509
  }
608
510
  //
609
511
  //bit reader
@@ -3994,6 +3896,23 @@ class bireader {
3994
3896
  throw new Error('Unsupported string type: ' + stringType);
3995
3897
  }
3996
3898
  }
3899
+ /**
3900
+ * Reads string, use options object for different types
3901
+ *
3902
+ * @param {object} options
3903
+ * ```javascript
3904
+ * {
3905
+ * length: number, //for fixed length, non-terminate value utf strings
3906
+ * stringType: "utf-8", //utf-8, utf-16, pascal or wide-pascal
3907
+ * terminateValue: 0x00, // only for non-fixed length utf strings
3908
+ * lengthReadSize: 1, //for pascal strings. 1, 2 or 4 byte length read size
3909
+ * stripNull: true, // removes 0x00 characters
3910
+ * encoding: "utf-8", //TextEncoder accepted types
3911
+ * endian: "little", //for wide-pascal and utf-16
3912
+ * }
3913
+ * ```
3914
+ * @return string
3915
+ */
3997
3916
  string(options) {
3998
3917
  return this.readString(options);
3999
3918
  }