bireader 1.0.15 → 1.0.17

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.
@@ -13,23 +13,16 @@ const common_1 = require("./common");
13
13
  */
14
14
  class bireader {
15
15
  isBuffer(obj) {
16
- return (typeof Buffer !== 'undefined' && obj instanceof Buffer);
16
+ return (0, common_1.buffcheck)(obj);
17
17
  }
18
18
  isBufferOrUint8Array(obj) {
19
- return obj instanceof Uint8Array || this.isBuffer(obj);
19
+ return (0, common_1.arraybuffcheck)(this, obj);
20
20
  }
21
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
- }
22
+ return (0, common_1.extendarray)(this, to_padd);
30
23
  }
31
- check_size(read_size, read_bits) {
32
- return (0, common_1.checkSize)(this, read_size || 0, read_bits || 0, this.offset);
24
+ check_size(write_bytes, write_bit, offset) {
25
+ return (0, common_1.checkSize)(this, write_bytes || 0, write_bit || 0, offset || this.offset);
33
26
  }
34
27
  /**
35
28
  * Binary reader, includes bitfields and strings
@@ -248,6 +241,54 @@ class bireader {
248
241
  saveOffset() {
249
242
  return this.offset;
250
243
  }
244
+ /**
245
+ * Get the current bit position (0-7)
246
+ *
247
+ * @return {number} current bit position
248
+ */
249
+ tellB() {
250
+ return this.bitoffset;
251
+ }
252
+ /**
253
+ * Get the current bit position (0-7)
254
+ *
255
+ * @return {number} current bit position
256
+ */
257
+ getOffsetBit() {
258
+ return this.bitoffset;
259
+ }
260
+ /**
261
+ * Get the current bit position (0-7)
262
+ *
263
+ * @return {number} current bit position
264
+ */
265
+ saveOffsetAbsBit() {
266
+ return (this.offset * 8) + this.bitoffset;
267
+ }
268
+ /**
269
+ * Get the current absolute bit position (from start of data)
270
+ *
271
+ * @return {number} current absolute bit position
272
+ */
273
+ tellAbsB() {
274
+ return (this.offset * 8) + this.bitoffset;
275
+ }
276
+ /**
277
+ * Get the current absolute bit position (from start of data)
278
+ *
279
+ * @return {number} current absolute bit position
280
+ */
281
+ getOffsetAbsBit() {
282
+ return (this.offset * 8) + this.bitoffset;
283
+ }
284
+ /**
285
+ * Get the current absolute bit position (from start of data)
286
+ *
287
+ * @return {number} current absolute bit position
288
+ */
289
+ saveOffsetBit() {
290
+ return (this.offset * 8) + this.bitoffset;
291
+ }
251
292
  //
252
293
  //strict mode change
253
294
  //
@@ -264,6 +305,262 @@ class bireader {
264
305
  this.strict = false;
265
306
  }
266
307
  //
308
+ //math
309
+ //
310
+ /**
311
+ * XOR data
312
+ *
313
+ * @param {number|string|Uint8Array|Buffer} xorKey - Value, string or array to XOR
314
+ * @param {number} startOffset - Start location (default current byte position)
315
+ * @param {number} endOffset - End location (default end of data)
316
+ * @param {boolean} consume - Move current position to end of data (default false)
317
+ */
318
+ xor(xorKey, startOffset, endOffset, consume) {
319
+ var XORKey = xorKey;
320
+ if (typeof xorKey == "number") {
321
+ //pass
322
+ }
323
+ else if (typeof xorKey == "string") {
324
+ //pass
325
+ }
326
+ else if (this.isBufferOrUint8Array(XORKey)) {
327
+ //pass
328
+ }
329
+ else {
330
+ throw new Error("XOR must be a number, string, Uint8Array or Buffer");
331
+ }
332
+ return (0, common_1.XOR)(this, xorKey, startOffset || this.offset, endOffset || this.size, consume || false);
333
+ }
334
+ /**
335
+ * XOR data
336
+ *
337
+ * @param {number|string|Uint8Array|Buffer} xorKey - Value, string or array to XOR
338
+ * @param {number} length - Length in bytes to XOR from curent position (default 1 byte for value, length of string or array for Uint8Array or Buffer)
339
+ * @param {boolean} consume - Move current position to end of data (default false)
340
+ */
341
+ xorThis(xorKey, length, consume) {
342
+ var Length = length || 1;
343
+ var XORKey = xorKey;
344
+ if (typeof xorKey == "number") {
345
+ Length = length || 1;
346
+ }
347
+ else if (typeof xorKey == "string") {
348
+ const encoder = new TextEncoder().encode(xorKey);
349
+ XORKey = encoder;
350
+ Length = length || encoder.length;
351
+ }
352
+ else if (this.isBufferOrUint8Array(XORKey)) {
353
+ Length = length || xorKey.length;
354
+ }
355
+ else {
356
+ throw new Error("XOR must be a number, string, Uint8Array or Buffer");
357
+ }
358
+ return (0, common_1.XOR)(this, XORKey, this.offset, this.offset + Length, consume || false);
359
+ }
360
+ /**
361
+ * OR data
362
+ *
363
+ * @param {number|string|Uint8Array|Buffer} orKey - Value, string or array to OR
364
+ * @param {number} startOffset - Start location (default current byte position)
365
+ * @param {number} endOffset - End location (default end of data)
366
+ * @param {boolean} consume - Move current position to end of data (default false)
367
+ */
368
+ or(orKey, startOffset, endOffset, consume) {
369
+ var ORKey = orKey;
370
+ if (typeof orKey == "number") {
371
+ //pass
372
+ }
373
+ else if (typeof orKey == "string") {
374
+ //pass
375
+ }
376
+ else if (this.isBufferOrUint8Array(ORKey)) {
377
+ //pass
378
+ }
379
+ else {
380
+ throw new Error("OR must be a number, string, Uint8Array or Buffer");
381
+ }
382
+ return (0, common_1.OR)(this, orKey, startOffset || this.offset, endOffset || this.size, consume || false);
383
+ }
384
+ /**
385
+ * OR data
386
+ *
387
+ * @param {number|string|Uint8Array|Buffer} orKey - Value, string or array to OR
388
+ * @param {number} length - Length in bytes to OR from curent position (default 1 byte for value, length of string or array for Uint8Array or Buffer)
389
+ * @param {boolean} consume - Move current position to end of data (default false)
390
+ */
391
+ orThis(orKey, length, consume) {
392
+ var Length = length || 1;
393
+ var ORKey = orKey;
394
+ if (typeof orKey == "number") {
395
+ Length = length || 1;
396
+ }
397
+ else if (typeof orKey == "string") {
398
+ const encoder = new TextEncoder().encode(orKey);
399
+ ORKey = encoder;
400
+ Length = length || encoder.length;
401
+ }
402
+ else if (this.isBufferOrUint8Array(ORKey)) {
403
+ Length = length || orKey.length;
404
+ }
405
+ else {
406
+ throw new Error("OR must be a number, string, Uint8Array or Buffer");
407
+ }
408
+ return (0, common_1.OR)(this, ORKey, this.offset, this.offset + Length, consume || false);
409
+ }
410
+ /**
411
+ * AND data
412
+ *
413
+ * @param {number|string|Uint8Array|Buffer} andKey - Value, string or array to AND
414
+ * @param {number} startOffset - Start location (default current byte position)
415
+ * @param {number} endOffset - End location (default end of data)
416
+ * @param {boolean} consume - Move current position to end of data (default false)
417
+ */
418
+ and(andKey, startOffset, endOffset, consume) {
419
+ var ANDKey = andKey;
420
+ if (typeof andKey == "number") {
421
+ //pass
422
+ }
423
+ else if (typeof andKey == "string") {
424
+ //pass
425
+ }
426
+ else if (this.isBufferOrUint8Array(ANDKey)) {
427
+ //pass
428
+ }
429
+ else {
430
+ throw new Error("AND must be a number, string, Uint8Array or Buffer");
431
+ }
432
+ return (0, common_1.AND)(this, andKey, startOffset || this.offset, endOffset || this.size, consume || false);
433
+ }
434
+ /**
435
+ * AND data
436
+ *
437
+ * @param {number|string|Uint8Array|Buffer} andKey - Value, string or array to AND
438
+ * @param {number} length - Length in bytes to AND from curent position (default 1 byte for value, length of string or array for Uint8Array or Buffer)
439
+ * @param {boolean} consume - Move current position to end of data (default false)
440
+ */
441
+ andThis(andKey, length, consume) {
442
+ var Length = length || 1;
443
+ var ANDKey = andKey;
444
+ if (typeof andKey == "number") {
445
+ Length = length || 1;
446
+ }
447
+ else if (typeof andKey == "string") {
448
+ const encoder = new TextEncoder().encode(andKey);
449
+ ANDKey = encoder;
450
+ Length = length || encoder.length;
451
+ }
452
+ else if (this.isBufferOrUint8Array(ANDKey)) {
453
+ Length = length || andKey.length;
454
+ }
455
+ else {
456
+ throw new Error("XOR must be a number, string, Uint8Array or Buffer");
457
+ }
458
+ return (0, common_1.AND)(this, ANDKey, this.offset, this.offset + Length, consume || false);
459
+ }
460
+ /**
461
+ * Not data
462
+ *
463
+ * @param {number} startOffset - Start location (default current byte position)
464
+ * @param {number} endOffset - End location (default end of data)
465
+ * @param {boolean} consume - Move current position to end of data (default false)
466
+ */
467
+ not(startOffset, endOffset, consume) {
468
+ return (0, common_1.NOT)(this, startOffset || this.offset, endOffset || this.size, consume || false);
469
+ }
470
+ /**
471
+ * Not data
472
+ *
473
+ * @param {number} length - Length in bytes to NOT from curent position (default 1 byte for value, length of string or array for Uint8Array or Buffer)
474
+ * @param {boolean} consume - Move current position to end of data (default false)
475
+ */
476
+ notThis(length, consume) {
477
+ return (0, common_1.NOT)(this, this.offset, this.offset + (length || 1), consume || false);
478
+ }
479
+ /**
480
+ * Left shift data
481
+ *
482
+ * @param {number} shiftValue - Value to left shift
483
+ * @param {number} startOffset - Start location (default current byte position)
484
+ * @param {number} endOffset - End location (default end of data)
485
+ * @param {boolean} consume - Move current position to end of data (default false)
486
+ */
487
+ lShift(shiftValue, startOffset, endOffset, consume) {
488
+ if (typeof shiftValue != "number") {
489
+ throw new Error("Shift value must be a number");
490
+ }
491
+ return (0, common_1.LSHIFT)(this, shiftValue, startOffset || this.offset, endOffset || this.size, consume || false);
492
+ }
493
+ /**
494
+ * Left shift data
495
+ *
496
+ * @param {number} shiftValue - Value to left shift
497
+ * @param {number} length - Length in bytes to left shift from curent position (default 1 byte for value, length of string or array for Uint8Array or Buffer)
498
+ * @param {boolean} consume - Move current position to end of data (default false)
499
+ */
500
+ lShiftThis(shiftValue, length, consume) {
501
+ var Length = length || 1;
502
+ if (typeof shiftValue != "number") {
503
+ throw new Error("Shift value must be a number");
504
+ }
505
+ return (0, common_1.LSHIFT)(this, shiftValue, this.offset, this.offset + Length, consume || false);
506
+ }
507
+ /**
508
+ * Right shift data
509
+ *
510
+ * @param {number} shiftValue - Value to right shift
511
+ * @param {number} startOffset - Start location (default current byte position)
512
+ * @param {number} endOffset - End location (default end of data)
513
+ * @param {boolean} consume - Move current position to end of data (default false)
514
+ */
515
+ rShift(shiftValue, startOffset, endOffset, consume) {
516
+ if (typeof shiftValue != "number") {
517
+ throw new Error("Shift value must be a number");
518
+ }
519
+ return (0, common_1.RSHIFT)(this, shiftValue, startOffset || this.offset, endOffset || this.size, consume || false);
520
+ }
521
+ /**
522
+ * Right shift data
523
+ *
524
+ * @param {number} shiftValue - Value to right shift
525
+ * @param {number} length - Length in bytes to right shift from curent position (default 1 byte for value, length of string or array for Uint8Array or Buffer)
526
+ * @param {boolean} consume - Move current position to end of data (default false)
527
+ */
528
+ rShiftThis(shiftValue, length, consume) {
529
+ var Length = length || 1;
530
+ if (typeof shiftValue != "number") {
531
+ throw new Error("Shift value must be a number");
532
+ }
533
+ return (0, common_1.RSHIFT)(this, shiftValue, this.offset, this.offset + Length, consume || false);
534
+ }
535
+ /**
536
+ * Add value to data
537
+ *
538
+ * @param {number} addValue - Value to add
539
+ * @param {number} startOffset - Start location (default current byte position)
540
+ * @param {number} endOffset - End location (default end of data)
541
+ * @param {boolean} consume - Move current position to end of data (default false)
542
+ */
543
+ add(addValue, startOffset, endOffset, consume) {
544
+ if (typeof addValue != "number") {
545
+ throw new Error("Add value must be a number");
546
+ }
547
+ return (0, common_1.ADD)(this, addValue, startOffset || this.offset, endOffset || this.size, consume || false);
548
+ }
549
+ /**
550
+ * Add value to data
551
+ *
552
+ * @param {number} addValue - Value to add
553
+ * @param {number} length - Length in bytes to add from curent position (default 1 byte for value, length of string or array for Uint8Array or Buffer)
554
+ * @param {boolean} consume - Move current position to end of data (default false)
555
+ */
556
+ addThis(addValue, length, consume) {
557
+ var Length = length || 1;
558
+ if (typeof addValue != "number") {
559
+ throw new Error("Add value must be a number");
560
+ }
561
+ return (0, common_1.ADD)(this, addValue, this.offset, this.offset + Length, consume || false);
562
+ }
563
+ //
267
564
  //remove part of data
268
565
  //
269
566
  /**
@@ -380,8 +677,8 @@ class bireader {
380
677
  * Note: Must be same data type as supplied data. Errors on strict mode.
381
678
  *
382
679
  * @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)
680
+ * @param {boolean} consume - Move current byte position to end of data (default false)
681
+ * @param {number} offset - Byte position to add at (defaults to current position)
385
682
  */
386
683
  insert(data, consume, offset) {
387
684
  return (0, common_1.addData)(this, data, consume || false, offset || this.offset);
@@ -391,13 +688,35 @@ class bireader {
391
688
  * Note: Must be same data type as supplied data. Errors on strict mode.
392
689
  *
393
690
  * @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)
691
+ * @param {boolean} consume - Move current byte position to end of data (default false)
692
+ * @param {number} offset - Byte position to add at (defaults to current position)
396
693
  */
397
694
  place(data, consume, offset) {
398
695
  return (0, common_1.addData)(this, data, consume || false, offset || this.offset);
399
696
  }
400
697
  /**
698
+ * Replaces data in data
699
+ * Note: Must be same data type as supplied data. Errors on strict mode.
700
+ *
701
+ * @param {Buffer|Uint8Array} data - ```Uint8Array``` or ```Buffer``` to replace in data
702
+ * @param {boolean} consume - Move current byte position to end of data (default false)
703
+ * @param {number} offset - Offset to add it at (defaults to current position)
704
+ */
705
+ replace(data, consume, offset) {
706
+ return (0, common_1.addData)(this, data, consume || false, offset || this.offset, true);
707
+ }
708
+ /**
709
+ * Replaces data in data
710
+ * Note: Must be same data type as supplied data. Errors on strict mode.
711
+ *
712
+ * @param {Buffer|Uint8Array} data - ```Uint8Array``` or ```Buffer``` to replace in data
713
+ * @param {boolean} consume - Move current byte position to end of data (default false)
714
+ * @param {number} offset - Offset to add it at (defaults to current position)
715
+ */
716
+ overwrite(data, consume, offset) {
717
+ return (0, common_1.addData)(this, data, consume || false, offset || this.offset, true);
718
+ }
719
+ /**
401
720
  * Adds data to start of supplied data
402
721
  * Note: Must be same data type as supplied data. Errors on strict mode.
403
722
  *
@@ -483,12 +802,12 @@ class bireader {
483
802
  /**
484
803
  * Console logs data as hex dump
485
804
  *
486
- * @param {object} options - options object
805
+ * @param {object} options
487
806
  * ```javascript
488
807
  * {
489
808
  * length: 192, // number of bytes to log, default 192 or end of data
490
- * startByte: 0, // byte to start dump, default current position
491
- * supressUnicode: false // Supress unicode character preview for cleaner columns
809
+ * startByte: 0, // byte to start dump (default current byte position)
810
+ * supressUnicode: false // Supress unicode character preview for even columns
492
811
  * }
493
812
  * ```
494
813
  */
@@ -511,6 +830,20 @@ class bireader {
511
830
  //bit reader
512
831
  //
513
832
  /**
833
+ *
834
+ * Write bits, must have at least value and number of bits
835
+ *
836
+ * ``Note``: When returning to a byte write, remaining bits are skipped
837
+ *
838
+ * @param {number} value - value as int
839
+ * @param {number} bits - number of bits to write
840
+ * @param {boolean} unsigned - if value is unsigned
841
+ * @param {string} endian - ``big`` or ``little``
842
+ */
843
+ writeBit(value, bits, unsigned, endian) {
844
+ return (0, common_1.wbit)(this, value, bits, unsigned, endian);
845
+ }
846
+ /**
514
847
  * Bit field reader
515
848
  *
516
849
  * Note: When returning to a byte read, remaining bits are dropped
@@ -521,48 +854,7 @@ class bireader {
521
854
  * @returns number
522
855
  */
523
856
  readBit(bits, unsigned, endian) {
524
- if (bits == undefined || typeof bits != "number") {
525
- throw new Error("Enter number of bits to read");
526
- }
527
- if (bits <= 0 || bits > 32) {
528
- throw new Error('Bit length must be between 1 and 32.');
529
- }
530
- const size_needed = ((((bits - 1) + this.bitoffset) / 8) + this.offset);
531
- if (bits <= 0 || size_needed > this.size) {
532
- this.errorDump ? "[Error], hexdump:\n" + this.hexdump() : "";
533
- throw new Error("Invalid number of bits to read: " + size_needed + " of " + this.size);
534
- }
535
- var off_in_bits = (this.offset * 8) + this.bitoffset;
536
- var value = 0;
537
- for (var i = 0; i < bits;) {
538
- var remaining = bits - i;
539
- var bitOffset = off_in_bits & 7;
540
- var currentByte = this.data[off_in_bits >> 3];
541
- var read = Math.min(remaining, 8 - bitOffset);
542
- var mask, readBits;
543
- if ((endian != undefined ? endian : this.endian) == "big") {
544
- mask = ~(0xFF << read);
545
- readBits = (currentByte >> (8 - read - bitOffset)) & mask;
546
- value <<= read;
547
- value |= readBits;
548
- }
549
- else {
550
- mask = ~(0xFF << read);
551
- readBits = (currentByte >> bitOffset) & mask;
552
- value |= readBits << i;
553
- }
554
- off_in_bits += read;
555
- i += read;
556
- }
557
- this.offset = this.offset + Math.floor(((bits) + this.bitoffset) / 8); //end byte
558
- this.bitoffset = ((bits) + this.bitoffset) % 8;
559
- if (unsigned == true || bits <= 7) {
560
- return value >>> 0;
561
- }
562
- if (bits !== 32 && value & (1 << (bits - 1))) {
563
- value |= -1 ^ ((1 << bits) - 1);
564
- }
565
- return value;
857
+ return (0, common_1.rbit)(this, bits, unsigned, endian);
566
858
  }
567
859
  /**
568
860
  * Bit field reader
@@ -575,7 +867,7 @@ class bireader {
575
867
  * @returns number
576
868
  */
577
869
  bit(bits, unsigned, endian) {
578
- return this.readBit(bits, unsigned, endian);
870
+ return this.bit(bits, unsigned, endian);
579
871
  }
580
872
  /**
581
873
  * Bit field reader
@@ -583,10 +875,11 @@ class bireader {
583
875
  * Note: When returning to a byte read, remaining bits are dropped
584
876
  *
585
877
  * @param {boolean} unsigned - if the value is unsigned
878
+ * @param {string} endian - ``big`` or ``little`
586
879
  * @returns number
587
880
  */
588
- bit1(unsigned) {
589
- return this.bit(1, unsigned);
881
+ bit1(unsigned, endian) {
882
+ return this.bit(1, unsigned, endian);
590
883
  }
591
884
  /**
592
885
  * Bit field reader
@@ -2602,7 +2895,7 @@ class bireader {
2602
2895
  * @returns number
2603
2896
  */
2604
2897
  readUBitBE(bits) {
2605
- return this.readBit(bits, true, "big");
2898
+ return this.bit(bits, true, "big");
2606
2899
  }
2607
2900
  /**
2608
2901
  * Bit field reader
@@ -2613,7 +2906,7 @@ class bireader {
2613
2906
  * @returns number
2614
2907
  */
2615
2908
  ubitbe(bits) {
2616
- return this.readBit(bits, true, "big");
2909
+ return this.bit(bits, true, "big");
2617
2910
  }
2618
2911
  /**
2619
2912
  * Bit field reader
@@ -2625,7 +2918,7 @@ class bireader {
2625
2918
  * @returns number
2626
2919
  */
2627
2920
  readBitBE(bits, unsigned) {
2628
- return this.readBit(bits, unsigned, "big");
2921
+ return this.bit(bits, unsigned, "big");
2629
2922
  }
2630
2923
  /**
2631
2924
  * Bit field reader
@@ -2637,7 +2930,7 @@ class bireader {
2637
2930
  * @returns number
2638
2931
  */
2639
2932
  bitbe(bits, unsigned) {
2640
- return this.readBit(bits, unsigned, "big");
2933
+ return this.bit(bits, unsigned, "big");
2641
2934
  }
2642
2935
  /**
2643
2936
  * Bit field reader
@@ -2648,7 +2941,7 @@ class bireader {
2648
2941
  * @returns number
2649
2942
  */
2650
2943
  readUBitLE(bits) {
2651
- return this.readBit(bits, true, "little");
2944
+ return this.bit(bits, true, "little");
2652
2945
  }
2653
2946
  /**
2654
2947
  * Bit field reader
@@ -2659,7 +2952,7 @@ class bireader {
2659
2952
  * @returns number
2660
2953
  */
2661
2954
  ubitle(bits) {
2662
- return this.readBit(bits, true, "little");
2955
+ return this.bit(bits, true, "little");
2663
2956
  }
2664
2957
  /**
2665
2958
  * Bit field reader
@@ -2671,7 +2964,7 @@ class bireader {
2671
2964
  * @returns number
2672
2965
  */
2673
2966
  readBitLE(bits, unsigned) {
2674
- return this.readBit(bits, unsigned, "little");
2967
+ return this.bit(bits, unsigned, "little");
2675
2968
  }
2676
2969
  /**
2677
2970
  * Bit field reader
@@ -2683,7 +2976,7 @@ class bireader {
2683
2976
  * @returns number
2684
2977
  */
2685
2978
  bitle(bits, unsigned) {
2686
- return this.readBit(bits, unsigned, "little");
2979
+ return this.bit(bits, unsigned, "little");
2687
2980
  }
2688
2981
  //
2689
2982
  //byte read
@@ -2695,15 +2988,16 @@ class bireader {
2695
2988
  * @returns number
2696
2989
  */
2697
2990
  readByte(unsigned) {
2698
- this.check_size(1);
2699
- var read = this.data[this.offset];
2700
- this.offset += 1;
2701
- if (unsigned == true) {
2702
- return read & 0xFF;
2703
- }
2704
- else {
2705
- return read > 127 ? read - 256 : read;
2706
- }
2991
+ return (0, common_1.rbyte)(this, unsigned);
2992
+ }
2993
+ /**
2994
+ * Write byte
2995
+ *
2996
+ * @param {number} value - value as int
2997
+ * @param {boolean} unsigned - if the value is unsigned
2998
+ */
2999
+ writeByte(value, unsigned) {
3000
+ return (0, common_1.wbyte)(this, value, unsigned);
2707
3001
  }
2708
3002
  /**
2709
3003
  * Read byte
@@ -2758,21 +3052,17 @@ class bireader {
2758
3052
  * @returns number
2759
3053
  */
2760
3054
  readInt16(unsigned, endian) {
2761
- this.check_size(2);
2762
- var read;
2763
- if ((endian != undefined ? endian : this.endian) == "little") {
2764
- read = (this.data[this.offset + 1] << 8) | this.data[this.offset];
2765
- }
2766
- else {
2767
- read = (this.data[this.offset] << 8) | this.data[this.offset + 1];
2768
- }
2769
- this.offset += 2;
2770
- if (unsigned == undefined || unsigned == false) {
2771
- return read & 0x8000 ? -(0x10000 - read) : read;
2772
- }
2773
- else {
2774
- return read & 0xFFFF;
2775
- }
3055
+ return (0, common_1.rint16)(this, unsigned, endian);
3056
+ }
3057
+ /**
3058
+ * Write int16
3059
+ *
3060
+ * @param {number} value - value as int
3061
+ * @param {boolean} unsigned - if the value is unsigned
3062
+ * @param {string} endian - ``big`` or ``little`
3063
+ */
3064
+ writeInt16(value, unsigned, endian) {
3065
+ return (0, common_1.wint16)(this, value, unsigned, endian);
2776
3066
  }
2777
3067
  /**
2778
3068
  * Read short
@@ -2982,34 +3272,16 @@ class bireader {
2982
3272
  * @returns number
2983
3273
  */
2984
3274
  readHalfFloat(endian) {
2985
- this.check_size(2);
2986
- var uint16Value = this.readInt16(true, (endian != undefined ? endian : this.endian));
2987
- const sign = (uint16Value & 0x8000) >> 15;
2988
- const exponent = (uint16Value & 0x7C00) >> 10;
2989
- const fraction = uint16Value & 0x03FF;
2990
- let floatValue;
2991
- if (exponent === 0) {
2992
- if (fraction === 0) {
2993
- floatValue = (sign === 0) ? 0 : -0; // +/-0
2994
- }
2995
- else {
2996
- // Denormalized number
2997
- floatValue = (sign === 0 ? 1 : -1) * Math.pow(2, -14) * (fraction / 0x0400);
2998
- }
2999
- }
3000
- else if (exponent === 0x1F) {
3001
- if (fraction === 0) {
3002
- floatValue = (sign === 0) ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
3003
- }
3004
- else {
3005
- floatValue = Number.NaN;
3006
- }
3007
- }
3008
- else {
3009
- // Normalized number
3010
- floatValue = (sign === 0 ? 1 : -1) * Math.pow(2, exponent - 15) * (1 + fraction / 0x0400);
3011
- }
3012
- return floatValue;
3275
+ return (0, common_1.rhalffloat)(this, endian);
3276
+ }
3277
+ /**
3278
+ * Writes half float
3279
+ *
3280
+ * @param {number} value - value as int
3281
+ * @param {string} endian - ``big`` or ``little`
3282
+ */
3283
+ writeHalfFloat(value, endian) {
3284
+ return (0, common_1.whalffloat)(this, value, endian);
3013
3285
  }
3014
3286
  /**
3015
3287
  * Read half float
@@ -3088,21 +3360,17 @@ class bireader {
3088
3360
  * @returns number
3089
3361
  */
3090
3362
  readInt32(unsigned, endian) {
3091
- this.check_size(4);
3092
- var read;
3093
- if ((endian != undefined ? endian : this.endian) == "little") {
3094
- read = ((this.data[this.offset + 3] << 24) | (this.data[this.offset + 2] << 16) | (this.data[this.offset + 1] << 8) | this.data[this.offset]);
3095
- }
3096
- else {
3097
- read = (this.data[this.offset] << 24) | (this.data[this.offset + 1] << 16) | (this.data[this.offset + 2] << 8) | this.data[this.offset + 3];
3098
- }
3099
- this.offset += 4;
3100
- if (unsigned == undefined || unsigned == false) {
3101
- return read;
3102
- }
3103
- else {
3104
- return read >>> 0;
3105
- }
3363
+ return (0, common_1.rint32)(this, unsigned, endian);
3364
+ }
3365
+ /**
3366
+ * Write int32
3367
+ *
3368
+ * @param {number} value - value as int
3369
+ * @param {boolean} unsigned - if the value is unsigned
3370
+ * @param {string} endian - ``big`` or ``little`
3371
+ */
3372
+ writeInt32(value, unsigned, endian) {
3373
+ return (0, common_1.wint32)(this, value, unsigned, endian);
3106
3374
  }
3107
3375
  /**
3108
3376
  * Read 32 bit integer
@@ -3354,28 +3622,16 @@ class bireader {
3354
3622
  * @returns number
3355
3623
  */
3356
3624
  readFloat(endian) {
3357
- this.check_size(4);
3358
- var uint32Value = this.readInt32(true, (endian == undefined ? this.endian : endian));
3359
- // Check if the value is negative (i.e., the most significant bit is set)
3360
- const isNegative = (uint32Value & 0x80000000) !== 0 ? 1 : 0;
3361
- // Extract the exponent and fraction parts
3362
- const exponent = (uint32Value >> 23) & 0xFF;
3363
- const fraction = uint32Value & 0x7FFFFF;
3364
- // Calculate the float value
3365
- let floatValue;
3366
- if (exponent === 0) {
3367
- // Denormalized number (exponent is 0)
3368
- floatValue = Math.pow(-1, isNegative) * Math.pow(2, -126) * (fraction / Math.pow(2, 23));
3369
- }
3370
- else if (exponent === 0xFF) {
3371
- // Infinity or NaN (exponent is 255)
3372
- floatValue = fraction === 0 ? (isNegative ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY) : Number.NaN;
3373
- }
3374
- else {
3375
- // Normalized number
3376
- floatValue = Math.pow(-1, isNegative) * Math.pow(2, exponent - 127) * (1 + fraction / Math.pow(2, 23));
3377
- }
3378
- return floatValue;
3625
+ return (0, common_1.rfloat)(this, endian);
3626
+ }
3627
+ /**
3628
+ * Write float
3629
+ *
3630
+ * @param {number} value - value as int
3631
+ * @param {string} endian - ``big`` or ``little`
3632
+ */
3633
+ writeFloat(value, endian) {
3634
+ return (0, common_1.wfloat)(this, value, endian);
3379
3635
  }
3380
3636
  /**
3381
3637
  * Read float
@@ -3428,39 +3684,17 @@ class bireader {
3428
3684
  * @returns number
3429
3685
  */
3430
3686
  readInt64(unsigned, endian) {
3431
- this.check_size(8);
3432
- // Convert the byte array to a BigInt
3433
- let value = BigInt(0);
3434
- if ((endian == undefined ? this.endian : endian) == "little") {
3435
- for (let i = 0; i < 8; i++) {
3436
- value = value | BigInt(this.data[this.offset]) << BigInt(8 * i);
3437
- this.offset += 1;
3438
- }
3439
- if (unsigned == undefined || unsigned == false) {
3440
- if (value & (BigInt(1) << BigInt(63))) {
3441
- value -= BigInt(1) << BigInt(64);
3442
- }
3443
- return value;
3444
- }
3445
- else {
3446
- return value;
3447
- }
3448
- }
3449
- else {
3450
- for (let i = 0; i < 8; i++) {
3451
- value = (value << BigInt(8)) | BigInt(this.data[this.offset]);
3452
- this.offset += 1;
3453
- }
3454
- if (unsigned == undefined || unsigned == false) {
3455
- if (value & (BigInt(1) << BigInt(63))) {
3456
- value -= BigInt(1) << BigInt(64);
3457
- }
3458
- return value;
3459
- }
3460
- else {
3461
- return value;
3462
- }
3463
- }
3687
+ return (0, common_1.rint64)(this, unsigned, endian);
3688
+ }
3689
+ /**
3690
+ * Write 64 bit integer
3691
+ *
3692
+ * @param {number} value - value as int
3693
+ * @param {boolean} unsigned - if the value is unsigned
3694
+ * @param {string} endian - ``big`` or ``little`
3695
+ */
3696
+ writeInt64(value, unsigned, endian) {
3697
+ return (0, common_1.wint64)(this, value, unsigned, endian);
3464
3698
  }
3465
3699
  /**
3466
3700
  * Read signed 64 bit integer
@@ -3659,34 +3893,17 @@ class bireader {
3659
3893
  * @returns number
3660
3894
  */
3661
3895
  readDoubleFloat(endian) {
3662
- this.check_size(8);
3663
- var uint64Value = this.readInt64(true, (endian == undefined ? this.endian : endian));
3664
- const sign = (uint64Value & 0x8000000000000000n) >> 63n;
3665
- const exponent = Number((uint64Value & 0x7ff0000000000000n) >> 52n) - 1023;
3666
- const fraction = Number(uint64Value & 0x000fffffffffffffn) / Math.pow(2, 52);
3667
- var floatValue;
3668
- if (exponent == -1023) {
3669
- if (fraction == 0) {
3670
- floatValue = (sign == 0n) ? 0 : -0; // +/-0
3671
- }
3672
- else {
3673
- // Denormalized number
3674
- floatValue = (sign == 0n ? 1 : -1) * Math.pow(2, -1022) * fraction;
3675
- }
3676
- }
3677
- else if (exponent == 1024) {
3678
- if (fraction == 0) {
3679
- floatValue = (sign == 0n) ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
3680
- }
3681
- else {
3682
- floatValue = Number.NaN;
3683
- }
3684
- }
3685
- else {
3686
- // Normalized number
3687
- floatValue = (sign == 0n ? 1 : -1) * Math.pow(2, exponent) * (1 + fraction);
3688
- }
3689
- return floatValue;
3896
+ return (0, common_1.rdfloat)(this, endian);
3897
+ }
3898
+ /**
3899
+ * Writes double float
3900
+ *
3901
+ * @param {number} value - value as int
3902
+ * @param {number} offset - byte offset (default last write position)
3903
+ * @param {string} endian - ``big`` or ``little`
3904
+ */
3905
+ writeDoubleFloat(value, endian) {
3906
+ return (0, common_1.wdfloat)(this, value, endian);
3690
3907
  }
3691
3908
  /**
3692
3909
  * Read double float
@@ -3775,126 +3992,27 @@ class bireader {
3775
3992
  * @return string
3776
3993
  */
3777
3994
  readString(options) {
3778
- var length = options && options.length;
3779
- var stringType = options && options.stringType || 'utf-8';
3780
- var terminateValue = options && options.terminateValue;
3781
- var lengthReadSize = options && options.lengthReadSize || 1;
3782
- var stripNull = options && options.stripNull || true;
3783
- var encoding = options && options.encoding || 'utf-8';
3784
- var endian = options && options.endian || this.endian;
3785
- var terminate = terminateValue;
3786
- if (length != undefined) {
3787
- this.check_size(length);
3788
- }
3789
- if (typeof terminateValue == "number") {
3790
- terminate = terminateValue & 0xFF;
3791
- }
3792
- else {
3793
- if (terminateValue != undefined) {
3794
- throw new Error("terminateValue must be a number");
3795
- }
3796
- }
3797
- if (stringType == 'utf-8' || stringType == 'utf-16') {
3798
- if (encoding == undefined) {
3799
- if (stringType == 'utf-8') {
3800
- encoding = 'utf-8';
3801
- }
3802
- if (stringType == 'utf-16') {
3803
- encoding = 'utf-16';
3804
- }
3805
- }
3806
- // Read the string as UTF-8 encoded untill 0 or terminateValue
3807
- const encodedBytes = [];
3808
- if (length == undefined && terminateValue == undefined) {
3809
- terminate = 0;
3810
- }
3811
- var read_length = 0;
3812
- if (length != undefined) {
3813
- read_length = length;
3814
- }
3815
- else {
3816
- read_length = this.data.length - this.offset;
3817
- }
3818
- for (let i = 0; i < read_length; i++) {
3819
- if (stringType === 'utf-8') {
3820
- var read = this.readUByte();
3821
- if (read == terminate) {
3822
- break;
3823
- }
3824
- else {
3825
- if (!(stripNull == true && read == 0)) {
3826
- encodedBytes.push(read);
3827
- }
3828
- }
3829
- }
3830
- else {
3831
- var read = this.readInt16(true, endian);
3832
- var read1 = read & 0xFF;
3833
- var read2 = (read >> 8) & 0xFF;
3834
- if (read == terminate) {
3835
- break;
3836
- }
3837
- else {
3838
- if (!(stripNull == true && read == 0)) {
3839
- encodedBytes.push(read1);
3840
- encodedBytes.push(read2);
3841
- }
3842
- }
3843
- }
3844
- }
3845
- return new TextDecoder(encoding).decode(new Uint8Array(encodedBytes));
3846
- }
3847
- else if (stringType == 'pascal' || stringType == 'wide-pascal') {
3848
- if (encoding == undefined) {
3849
- if (stringType == 'pascal') {
3850
- encoding = 'utf-8';
3851
- }
3852
- if (stringType == 'wide-pascal') {
3853
- encoding = 'utf-16';
3854
- }
3855
- }
3856
- var maxBytes;
3857
- if (lengthReadSize == 1) {
3858
- maxBytes = this.readUByte();
3859
- }
3860
- else if (lengthReadSize == 2) {
3861
- maxBytes = this.readInt16(true, endian);
3862
- }
3863
- else if (lengthReadSize == 4) {
3864
- maxBytes = this.readInt32(true, endian);
3865
- }
3866
- else {
3867
- this.errorDump ? "[Error], hexdump:\n" + this.hexdump() : "";
3868
- throw new Error("Invalid length read size: " + lengthReadSize);
3869
- }
3870
- // Read the string as Pascal or Delphi encoded
3871
- const encodedBytes = [];
3872
- for (let i = 0; i < maxBytes; i++) {
3873
- if (stringType == 'wide-pascal') {
3874
- const read = this.readInt16(true, endian);
3875
- if (!(stripNull == true && read == 0)) {
3876
- encodedBytes.push(read);
3877
- }
3878
- }
3879
- else {
3880
- const read = this.readUByte();
3881
- if (!(stripNull == true && read == 0)) {
3882
- encodedBytes.push(read);
3883
- }
3884
- }
3885
- }
3886
- var str_return;
3887
- if (stringType == 'wide-pascal') {
3888
- str_return = new TextDecoder(encoding).decode(new Uint16Array(encodedBytes));
3889
- }
3890
- else {
3891
- str_return = new TextDecoder(encoding).decode(new Uint8Array(encodedBytes));
3892
- }
3893
- return str_return;
3894
- }
3895
- else {
3896
- throw new Error('Unsupported string type: ' + stringType);
3897
- }
3995
+ return (0, common_1.rstring)(this, options);
3996
+ }
3997
+ /**
3998
+ * Writes string, use options object for different types
3999
+ *
4000
+ *
4001
+ * @param {string} string - text string
4002
+ * @param {object} options - options:
4003
+ * ```javascript
4004
+ * {
4005
+ * length: string.length, //for fixed length, non-terminate value utf strings
4006
+ * stringType: "utf-8", //utf-8, utf-16, pascal or wide-pascal
4007
+ * terminateValue: 0x00, // only with stringType: "utf"
4008
+ * lengthWriteSize: 1, //for pascal strings. 1, 2 or 4 byte length write size
4009
+ * encoding: "utf-8", //TextEncoder accepted types
4010
+ * endian: "little", //for wide-pascal and utf-16
4011
+ * }
4012
+ * ```
4013
+ */
4014
+ writeString(string, options) {
4015
+ return (0, common_1.wstring)(this, string, options);
3898
4016
  }
3899
4017
  /**
3900
4018
  * Reads string, use options object for different types