bireader 1.0.15 → 1.0.18

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,3 +1,19 @@
1
+ export function buffcheck(obj) {
2
+ return (typeof Buffer !== 'undefined' && obj instanceof Buffer);
3
+ }
4
+ export function arraybuffcheck(_this, obj) {
5
+ return obj instanceof Uint8Array || _this.isBuffer(obj);
6
+ }
7
+ export function extendarray(_this, to_padd) {
8
+ if ((typeof Buffer !== 'undefined' && _this.data instanceof Buffer)) {
9
+ var paddbuffer = Buffer.alloc(to_padd);
10
+ _this.data = Buffer.concat([_this.data, paddbuffer]);
11
+ }
12
+ else {
13
+ const addArray = new Array(to_padd);
14
+ _this.data = new Uint8Array([..._this.data, ...addArray]);
15
+ }
16
+ }
1
17
  export function checkSize(_this, write_bytes, write_bit, offset) {
2
18
  const bits = (write_bit || 0) + _this.bitoffset;
3
19
  var new_off = (offset || _this.offset);
@@ -94,39 +110,56 @@ export function remove(_this, startOffset, endOffset, consume, remove, fillValue
94
110
  }
95
111
  return data_removed;
96
112
  }
97
- export function addData(_this, data, consume, offset) {
113
+ export function addData(_this, data, consume, offset, repalce) {
114
+ if (_this.strict == true) {
115
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
116
+ throw new Error(`\x1b[33m[Strict mode]\x1b[0m: Can not insert data in strict mode. Use unrestrict() to enable.`);
117
+ }
98
118
  if (typeof Buffer !== 'undefined' && data instanceof Buffer && !(_this.data instanceof Buffer)) {
99
119
  throw new Error("Data insert must be a Buffer");
100
120
  }
101
121
  if (data instanceof Uint8Array && !(_this.data instanceof Uint8Array)) {
102
122
  throw new Error("Data insert must be a Uint8Array");
103
123
  }
104
- const needed_size = offset || _this.offset;
124
+ var needed_size = offset || _this.offset;
125
+ if (repalce) {
126
+ needed_size = offset || _this.offset + data.length;
127
+ }
105
128
  if (needed_size > _this.size) {
106
129
  const dif = needed_size - _this.size;
107
- if (_this.strict == false) {
108
- _this.extendArray(dif);
130
+ _this.extendArray(dif);
131
+ _this.size = _this.data.length;
132
+ }
133
+ if (repalce) {
134
+ if (_this.isBuffer(_this.data)) {
135
+ const part1 = _this.data.subarray(0, needed_size - data.length);
136
+ const part2 = _this.data.subarray(needed_size, _this.size);
137
+ _this.data = Buffer.concat([part1, data, part2]);
138
+ _this.size = _this.data.length;
109
139
  }
110
140
  else {
111
- _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
112
- throw new Error(`\x1b[33m[Strict mode]\x1b[0m: Insert outside of current data: writing to ` + needed_size + " at " + _this.offset + " of " + _this.size);
141
+ const part1 = _this.data.subarray(0, needed_size - data.length);
142
+ const part2 = _this.data.subarray(needed_size, _this.size);
143
+ _this.data = new Uint8Array([...part1, ...data, ...part2]);
144
+ _this.size = _this.data.length;
113
145
  }
114
- _this.size = _this.data.length;
115
- }
116
- if (_this.isBuffer(_this.data)) {
117
- const part1 = _this.data.subarray(0, needed_size);
118
- const part2 = _this.data.subarray(needed_size, _this.size);
119
- _this.data = Buffer.concat([part1, data, part2]);
120
- _this.size = _this.data.length;
121
146
  }
122
147
  else {
123
- const part1 = _this.data.subarray(0, needed_size);
124
- const part2 = _this.data.subarray(needed_size, _this.size);
125
- _this.data = new Uint8Array([...part1, ...data, ...part2]);
126
- _this.size = _this.data.length;
148
+ if (_this.isBuffer(_this.data)) {
149
+ const part1 = _this.data.subarray(0, needed_size);
150
+ const part2 = _this.data.subarray(needed_size, _this.size);
151
+ _this.data = Buffer.concat([part1, data, part2]);
152
+ _this.size = _this.data.length;
153
+ }
154
+ else {
155
+ const part1 = _this.data.subarray(0, needed_size);
156
+ const part2 = _this.data.subarray(needed_size, _this.size);
157
+ _this.data = new Uint8Array([...part1, ...data, ...part2]);
158
+ _this.size = _this.data.length;
159
+ }
127
160
  }
128
161
  if (consume) {
129
- _this.offset = _this.offset + data.length;
162
+ _this.offset = needed_size;
130
163
  }
131
164
  }
132
165
  export function hexDump(_this, options) {
@@ -311,25 +344,23 @@ export function hexDump(_this, options) {
311
344
  }
312
345
  console.log(rows.join("\n"));
313
346
  }
314
- export function AND(_this, xor_key, start, end) {
347
+ export function AND(_this, xor_key, start, end, consume) {
315
348
  const input = _this.data;
316
- if (typeof xor_key == "number") {
317
- for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
318
- input[i] = input[i] & (xor_key && 0xff);
349
+ if ((end || 0) > _this.size) {
350
+ if (_this.strict == false) {
351
+ _this.extendArray((end || 0) - _this.size);
352
+ }
353
+ else {
354
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
355
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
319
356
  }
320
357
  }
321
- else if (typeof xor_key == "string") {
322
- const encoder = new TextEncoder();
323
- const xor_array = encoder.encode(xor_key);
324
- let number = -1;
358
+ if (typeof xor_key == "number") {
325
359
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
326
- if (number != xor_array.length - 1) {
327
- number = number + 1;
360
+ input[i] = input[i] & (xor_key & 0xff);
361
+ if (consume) {
362
+ _this.offset = i;
328
363
  }
329
- else {
330
- number = 0;
331
- }
332
- input[i] = input[i] & xor_array[number];
333
364
  }
334
365
  }
335
366
  else {
@@ -343,6 +374,9 @@ export function AND(_this, xor_key, start, end) {
343
374
  number = 0;
344
375
  }
345
376
  input[i] = input[i] & xor_key[number];
377
+ if (consume) {
378
+ _this.offset = i;
379
+ }
346
380
  }
347
381
  }
348
382
  else {
@@ -350,25 +384,23 @@ export function AND(_this, xor_key, start, end) {
350
384
  }
351
385
  }
352
386
  }
353
- export function OR(_this, xor_key, start, end) {
387
+ export function OR(_this, xor_key, start, end, consume) {
354
388
  const input = _this.data;
355
- if (typeof xor_key == "number") {
356
- for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
357
- input[i] = input[i] | (xor_key && 0xff);
389
+ if ((end || 0) > _this.size) {
390
+ if (_this.strict == false) {
391
+ _this.extendArray((end || 0) - _this.size);
392
+ }
393
+ else {
394
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
395
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
358
396
  }
359
397
  }
360
- else if (typeof xor_key == "string") {
361
- const encoder = new TextEncoder();
362
- const xor_array = encoder.encode(xor_key);
363
- let number = -1;
398
+ if (typeof xor_key == "number") {
364
399
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
365
- if (number != xor_array.length - 1) {
366
- number = number + 1;
400
+ input[i] = input[i] | (xor_key & 0xff);
401
+ if (consume) {
402
+ _this.offset = i;
367
403
  }
368
- else {
369
- number = 0;
370
- }
371
- input[i] = input[i] | xor_array[number];
372
404
  }
373
405
  }
374
406
  else {
@@ -382,6 +414,9 @@ export function OR(_this, xor_key, start, end) {
382
414
  number = 0;
383
415
  }
384
416
  input[i] = input[i] | xor_key[number];
417
+ if (consume) {
418
+ _this.offset = i;
419
+ }
385
420
  }
386
421
  }
387
422
  else {
@@ -389,25 +424,23 @@ export function OR(_this, xor_key, start, end) {
389
424
  }
390
425
  }
391
426
  }
392
- export function XOR(_this, xor_key, start, end) {
427
+ export function XOR(_this, xor_key, start, end, consume) {
393
428
  const input = _this.data;
394
- if (typeof xor_key == "number") {
395
- for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
396
- input[i] = input[i] ^ (xor_key && 0xff);
429
+ if ((end || 0) > _this.size) {
430
+ if (_this.strict == false) {
431
+ _this.extendArray((end || 0) - _this.size);
432
+ }
433
+ else {
434
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
435
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
397
436
  }
398
437
  }
399
- else if (typeof xor_key == "string") {
400
- const encoder = new TextEncoder();
401
- const xor_array = encoder.encode(xor_key);
402
- let number = -1;
438
+ if (typeof xor_key == "number") {
403
439
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
404
- if (number != xor_array.length - 1) {
405
- number = number + 1;
440
+ input[i] = input[i] ^ (xor_key & 0xff);
441
+ if (consume) {
442
+ _this.offset = i;
406
443
  }
407
- else {
408
- number = 0;
409
- }
410
- input[i] = input[i] ^ xor_array[number];
411
444
  }
412
445
  }
413
446
  else {
@@ -421,6 +454,9 @@ export function XOR(_this, xor_key, start, end) {
421
454
  number = 0;
422
455
  }
423
456
  input[i] = input[i] ^ xor_key[number];
457
+ if (consume) {
458
+ _this.offset = i;
459
+ }
424
460
  }
425
461
  }
426
462
  else {
@@ -428,19 +464,799 @@ export function XOR(_this, xor_key, start, end) {
428
464
  }
429
465
  }
430
466
  }
431
- export function NOT(_this, start, end) {
467
+ export function NOT(_this, start, end, consume) {
468
+ if ((end || 0) > _this.size) {
469
+ if (_this.strict == false) {
470
+ _this.extendArray((end || 0) - _this.size);
471
+ }
472
+ else {
473
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
474
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
475
+ }
476
+ }
432
477
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
433
478
  _this.data[i] = ~_this.data[i];
479
+ if (consume) {
480
+ _this.offset = i;
481
+ }
434
482
  }
435
483
  }
436
- export function LSHIFT(_this, value, start, end) {
484
+ export function LSHIFT(_this, value, start, end, consume) {
485
+ if ((end || 0) > _this.size) {
486
+ if (_this.strict == false) {
487
+ _this.extendArray((end || 0) - _this.size);
488
+ }
489
+ else {
490
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
491
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
492
+ }
493
+ }
437
494
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
438
495
  _this.data[i] = _this.data[i] << value;
496
+ if (consume) {
497
+ _this.offset = i;
498
+ }
439
499
  }
440
500
  }
441
- export function RSHIFT(_this, value, start, end) {
501
+ export function RSHIFT(_this, value, start, end, consume) {
502
+ if ((end || 0) > _this.size) {
503
+ if (_this.strict == false) {
504
+ _this.extendArray((end || 0) - _this.size);
505
+ }
506
+ else {
507
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
508
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
509
+ }
510
+ }
442
511
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
443
512
  _this.data[i] = _this.data[i] >> value;
513
+ if (consume) {
514
+ _this.offset = i;
515
+ }
516
+ }
517
+ }
518
+ export function ADD(_this, value, start, end, consume) {
519
+ if ((end || 0) > _this.size) {
520
+ if (_this.strict == false) {
521
+ _this.extendArray((end || 0) - _this.size);
522
+ }
523
+ else {
524
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
525
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
526
+ }
527
+ }
528
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
529
+ _this.data[i] += value;
530
+ if (consume) {
531
+ _this.offset = i;
532
+ }
533
+ }
534
+ }
535
+ export function wbit(_this, value, bits, unsigned, endian) {
536
+ if (value == undefined) {
537
+ throw new Error('Must supply value.');
538
+ }
539
+ if (bits == undefined) {
540
+ throw new Error("Enter number of bits to write");
541
+ }
542
+ if (bits <= 0 || bits > 32) {
543
+ throw new Error('Bit length must be between 1 and 32.');
544
+ }
545
+ if (unsigned == true) {
546
+ if (value < 0 || value > Math.pow(2, bits)) {
547
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
548
+ throw new Error(`Value is out of range for the specified ${bits}bit length.` + " min: " + 0 + " max: " + Math.pow(2, bits) + " value: " + value);
549
+ }
550
+ }
551
+ else {
552
+ const maxValue = Math.pow(2, bits - 1) - 1;
553
+ const minValue = -maxValue - 1;
554
+ if (value < minValue || value > maxValue) {
555
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
556
+ throw new Error(`Value is out of range for the specified ${bits}bit length.` + " min: " + minValue + " max: " + maxValue + " value: " + value);
557
+ }
558
+ }
559
+ if (unsigned == true) {
560
+ const maxValue = Math.pow(2, bits) - 1;
561
+ value = value & maxValue;
562
+ }
563
+ const size_needed = ((((bits - 1) + _this.bitoffset) / 8) + _this.offset);
564
+ if (size_needed > _this.size) {
565
+ //add size
566
+ _this.extendArray(size_needed - _this.size);
567
+ }
568
+ var off_in_bits = (_this.offset * 8) + _this.bitoffset;
569
+ for (var i = 0; i < bits;) {
570
+ var remaining = bits - i;
571
+ var bitOffset = off_in_bits & 7;
572
+ var byteOffset = off_in_bits >> 3;
573
+ var written = Math.min(remaining, 8 - bitOffset);
574
+ var mask, writeBits, destMask;
575
+ if ((endian != undefined ? endian : _this.endian) == "big") {
576
+ mask = ~(~0 << written);
577
+ writeBits = (value >> (bits - i - written)) & mask;
578
+ var destShift = 8 - bitOffset - written;
579
+ destMask = ~(mask << destShift);
580
+ _this.data[byteOffset] = (_this.data[byteOffset] & destMask) | (writeBits << destShift);
581
+ }
582
+ else {
583
+ mask = ~(0xFF << written);
584
+ writeBits = value & mask;
585
+ value >>= written;
586
+ destMask = ~(mask << bitOffset);
587
+ _this.data[byteOffset] = (_this.data[byteOffset] & destMask) | (writeBits << bitOffset);
588
+ }
589
+ off_in_bits += written;
590
+ i += written;
591
+ }
592
+ _this.offset = _this.offset + Math.floor(((bits) + _this.bitoffset) / 8); //end byte
593
+ _this.bitoffset = ((bits) + _this.bitoffset) % 8;
594
+ }
595
+ export function rbit(_this, bits, unsigned, endian) {
596
+ if (bits == undefined || typeof bits != "number") {
597
+ throw new Error("Enter number of bits to read");
598
+ }
599
+ if (bits <= 0 || bits > 32) {
600
+ throw new Error('Bit length must be between 1 and 32.');
601
+ }
602
+ const size_needed = ((((bits - 1) + _this.bitoffset) / 8) + _this.offset);
603
+ if (bits <= 0 || size_needed > _this.size) {
604
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
605
+ throw new Error("Invalid number of bits to read: " + size_needed + " of " + _this.size);
606
+ }
607
+ var off_in_bits = (_this.offset * 8) + _this.bitoffset;
608
+ var value = 0;
609
+ for (var i = 0; i < bits;) {
610
+ var remaining = bits - i;
611
+ var bitOffset = off_in_bits & 7;
612
+ var currentByte = _this.data[off_in_bits >> 3];
613
+ var read = Math.min(remaining, 8 - bitOffset);
614
+ var mask, readBits;
615
+ if ((endian != undefined ? endian : _this.endian) == "big") {
616
+ mask = ~(0xFF << read);
617
+ readBits = (currentByte >> (8 - read - bitOffset)) & mask;
618
+ value <<= read;
619
+ value |= readBits;
620
+ }
621
+ else {
622
+ mask = ~(0xFF << read);
623
+ readBits = (currentByte >> bitOffset) & mask;
624
+ value |= readBits << i;
625
+ }
626
+ off_in_bits += read;
627
+ i += read;
628
+ }
629
+ _this.offset = _this.offset + Math.floor(((bits) + _this.bitoffset) / 8); //end byte
630
+ _this.bitoffset = ((bits) + _this.bitoffset) % 8;
631
+ if (unsigned == true || bits <= 7) {
632
+ return value >>> 0;
633
+ }
634
+ if (bits !== 32 && value & (1 << (bits - 1))) {
635
+ value |= -1 ^ ((1 << bits) - 1);
636
+ }
637
+ return value;
638
+ }
639
+ export function wbyte(_this, value, unsigned) {
640
+ _this.check_size(1, 0);
641
+ if (unsigned == true) {
642
+ if (value < 0 || value > 255) {
643
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
644
+ throw new Error('Value is out of range for the specified 8bit length.' + " min: " + 0 + " max: " + 255 + " value: " + value);
645
+ }
646
+ }
647
+ else {
648
+ const maxValue = Math.pow(2, 8 - 1) - 1;
649
+ const minValue = -maxValue - 1;
650
+ if (value < minValue || value > maxValue) {
651
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
652
+ throw new Error('Value is out of range for the specified 8bit length.' + " min: " + minValue + " max: " + maxValue + " value: " + value);
653
+ }
654
+ }
655
+ _this.data[_this.offset] = (unsigned == undefined || unsigned == false) ? value : value & 0xFF;
656
+ _this.offset += 1;
657
+ }
658
+ export function rbyte(_this, unsigned) {
659
+ _this.check_size(1);
660
+ var read = _this.data[_this.offset];
661
+ _this.offset += 1;
662
+ if (unsigned == true) {
663
+ return read & 0xFF;
664
+ }
665
+ else {
666
+ return read > 127 ? read - 256 : read;
667
+ }
668
+ }
669
+ export function wint16(_this, value, unsigned, endian) {
670
+ _this.check_size(2, 0);
671
+ if (unsigned == true) {
672
+ if (value < 0 || value > 65535) {
673
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
674
+ throw new Error('Value is out of range for the specified 16bit length.' + " min: " + 0 + " max: " + 65535 + " value: " + value);
675
+ }
676
+ }
677
+ else {
678
+ const maxValue = Math.pow(2, 16 - 1) - 1;
679
+ const minValue = -maxValue - 1;
680
+ if (value < minValue || value > maxValue) {
681
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
682
+ throw new Error('Value is out of range for the specified 16bit length.' + " min: " + minValue + " max: " + maxValue + " value: " + value);
683
+ }
684
+ }
685
+ if ((endian != undefined ? endian : _this.endian) == "little") {
686
+ _this.data[_this.offset] = (unsigned == undefined || unsigned == false) ? value : value & 0xff;
687
+ _this.data[_this.offset + 1] = (unsigned == undefined || unsigned == false) ? (value >> 8) : (value >> 8) & 0xff;
688
+ }
689
+ else {
690
+ _this.data[_this.offset] = (unsigned == undefined || unsigned == false) ? (value >> 8) : (value >> 8) & 0xff;
691
+ _this.data[_this.offset + 1] = (unsigned == undefined || unsigned == false) ? value : value & 0xff;
692
+ }
693
+ _this.offset += 2;
694
+ }
695
+ export function rint16(_this, unsigned, endian) {
696
+ _this.check_size(2);
697
+ var read;
698
+ if ((endian != undefined ? endian : _this.endian) == "little") {
699
+ read = (_this.data[_this.offset + 1] << 8) | _this.data[_this.offset];
700
+ }
701
+ else {
702
+ read = (_this.data[_this.offset] << 8) | _this.data[_this.offset + 1];
703
+ }
704
+ _this.offset += 2;
705
+ if (unsigned == undefined || unsigned == false) {
706
+ return read & 0x8000 ? -(0x10000 - read) : read;
707
+ }
708
+ else {
709
+ return read & 0xFFFF;
710
+ }
711
+ }
712
+ export function rhalffloat(_this, endian) {
713
+ _this.check_size(2);
714
+ var uint16Value = _this.readInt16(true, (endian != undefined ? endian : _this.endian));
715
+ const sign = (uint16Value & 0x8000) >> 15;
716
+ const exponent = (uint16Value & 0x7C00) >> 10;
717
+ const fraction = uint16Value & 0x03FF;
718
+ let floatValue;
719
+ if (exponent === 0) {
720
+ if (fraction === 0) {
721
+ floatValue = (sign === 0) ? 0 : -0; // +/-0
722
+ }
723
+ else {
724
+ // Denormalized number
725
+ floatValue = (sign === 0 ? 1 : -1) * Math.pow(2, -14) * (fraction / 0x0400);
726
+ }
727
+ }
728
+ else if (exponent === 0x1F) {
729
+ if (fraction === 0) {
730
+ floatValue = (sign === 0) ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
731
+ }
732
+ else {
733
+ floatValue = Number.NaN;
734
+ }
735
+ }
736
+ else {
737
+ // Normalized number
738
+ floatValue = (sign === 0 ? 1 : -1) * Math.pow(2, exponent - 15) * (1 + fraction / 0x0400);
739
+ }
740
+ return floatValue;
741
+ }
742
+ export function whalffloat(_this, value, endian) {
743
+ _this.check_size(2, 0);
744
+ const maxValue = 65504;
745
+ const minValue = 5.96e-08;
746
+ if (value < minValue || value > maxValue) {
747
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
748
+ throw new Error('Value is out of range for the specified half float length.' + " min: " + minValue + " max: " + maxValue + " value: " + value);
749
+ }
750
+ const signMask = 0x8000;
751
+ const exponentMask = 0x7C00;
752
+ const fractionMask = 0x03FF;
753
+ // Determine sign, exponent, and fraction bits
754
+ let signBit = (value & signMask) >> 15;
755
+ let exponentBits = (value & exponentMask) >> 10;
756
+ let fractionBits = value & fractionMask;
757
+ // Special cases for NaN and Infinity
758
+ if (exponentBits === 0x1F) {
759
+ // NaN or Infinity, copy exponent and fraction
760
+ exponentBits = 0xFF;
761
+ }
762
+ else if (exponentBits === 0x00) {
763
+ // Denormalized numbers, exponent is 0, adjust exponent bits
764
+ exponentBits = 0x00;
765
+ fractionBits = 0x00; // Clear fraction for denormals
766
+ }
767
+ else {
768
+ // Normalized number, subtract exponent bias
769
+ exponentBits -= 15;
770
+ }
771
+ // Combine sign, exponent, and fraction bits into half float format
772
+ let halfFloatBits = (signBit << 15) | (exponentBits << 10) | fractionBits;
773
+ // Write bytes based on endianness
774
+ if ((endian = undefined ? endian : _this.endian) == "little") {
775
+ _this.data[_this.offset] = halfFloatBits & 0xFF;
776
+ _this.data[_this.offset + 1] = (halfFloatBits >> 8) & 0xFF;
777
+ }
778
+ else {
779
+ _this.data[_this.offset] = (halfFloatBits >> 8) & 0xFF;
780
+ _this.data[_this.offset + 1] = halfFloatBits & 0xFF;
781
+ }
782
+ _this.offset += 2;
783
+ }
784
+ export function wint32(_this, value, unsigned, endian) {
785
+ _this.check_size(4, 0);
786
+ if (unsigned == true) {
787
+ if (value < 0 || value > 4294967295) {
788
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
789
+ throw new Error('Value is out of range for the specified 32bit length.' + " min: " + 0 + " max: " + 4294967295 + " value: " + value);
790
+ }
791
+ }
792
+ else {
793
+ const maxValue = Math.pow(2, 32 - 1) - 1;
794
+ const minValue = -maxValue - 1;
795
+ if (value < minValue || value > maxValue) {
796
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
797
+ throw new Error('Value is out of range for the specified 32bit length.' + " min: " + minValue + " max: " + maxValue + " value: " + value);
798
+ }
799
+ }
800
+ if ((endian = undefined ? endian : _this.endian) == "little") {
801
+ _this.data[_this.offset] = (unsigned == undefined || unsigned == false) ? value : value & 0xFF;
802
+ _this.data[_this.offset + 1] = (unsigned == undefined || unsigned == false) ? (value >> 8) : (value >> 8) & 0xFF;
803
+ _this.data[_this.offset + 2] = (unsigned == undefined || unsigned == false) ? (value >> 16) : (value >> 16) & 0xFF;
804
+ _this.data[_this.offset + 3] = (unsigned == undefined || unsigned == false) ? (value >> 24) : (value >> 24) & 0xFF;
805
+ }
806
+ else {
807
+ _this.data[_this.offset] = (unsigned == undefined || unsigned == false) ? (value >> 24) : (value >> 24) & 0xFF;
808
+ _this.data[_this.offset + 1] = (unsigned == undefined || unsigned == false) ? (value >> 16) : (value >> 16) & 0xFF;
809
+ _this.data[_this.offset + 2] = (unsigned == undefined || unsigned == false) ? (value >> 8) : (value >> 8) & 0xFF;
810
+ _this.data[_this.offset + 3] = (unsigned == undefined || unsigned == false) ? value : value & 0xFF;
811
+ }
812
+ _this.offset += 4;
813
+ }
814
+ export function rint32(_this, unsigned, endian) {
815
+ _this.check_size(4);
816
+ var read;
817
+ if ((endian != undefined ? endian : _this.endian) == "little") {
818
+ read = ((_this.data[_this.offset + 3] << 24) | (_this.data[_this.offset + 2] << 16) | (_this.data[_this.offset + 1] << 8) | _this.data[_this.offset]);
819
+ }
820
+ else {
821
+ read = (_this.data[_this.offset] << 24) | (_this.data[_this.offset + 1] << 16) | (_this.data[_this.offset + 2] << 8) | _this.data[_this.offset + 3];
822
+ }
823
+ _this.offset += 4;
824
+ if (unsigned == undefined || unsigned == false) {
825
+ return read;
826
+ }
827
+ else {
828
+ return read >>> 0;
829
+ }
830
+ }
831
+ export function rfloat(_this, endian) {
832
+ _this.check_size(4);
833
+ var uint32Value = _this.readInt32(true, (endian == undefined ? _this.endian : endian));
834
+ // Check if the value is negative (i.e., the most significant bit is set)
835
+ const isNegative = (uint32Value & 0x80000000) !== 0 ? 1 : 0;
836
+ // Extract the exponent and fraction parts
837
+ const exponent = (uint32Value >> 23) & 0xFF;
838
+ const fraction = uint32Value & 0x7FFFFF;
839
+ // Calculate the float value
840
+ let floatValue;
841
+ if (exponent === 0) {
842
+ // Denormalized number (exponent is 0)
843
+ floatValue = Math.pow(-1, isNegative) * Math.pow(2, -126) * (fraction / Math.pow(2, 23));
844
+ }
845
+ else if (exponent === 0xFF) {
846
+ // Infinity or NaN (exponent is 255)
847
+ floatValue = fraction === 0 ? (isNegative ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY) : Number.NaN;
848
+ }
849
+ else {
850
+ // Normalized number
851
+ floatValue = Math.pow(-1, isNegative) * Math.pow(2, exponent - 127) * (1 + fraction / Math.pow(2, 23));
852
+ }
853
+ return floatValue;
854
+ }
855
+ export function wfloat(_this, value, endian) {
856
+ _this.check_size(4, 0);
857
+ const maxValue = 3.402823466e+38;
858
+ const minValue = 1.175494351e-38;
859
+ if (value < minValue || value > maxValue) {
860
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
861
+ throw new Error('Value is out of range for the specified float length.' + " min: " + minValue + " max: " + maxValue + " value: " + value);
862
+ }
863
+ let intValue = Float32Array.from([value])[0]; // Convert float to 32-bit integer representation
864
+ let shift = 0;
865
+ for (let i = 0; i < 4; i++) {
866
+ if ((endian = undefined ? endian : _this.endian) == "little") {
867
+ _this.data[_this.offset + i] = (intValue >> shift) & 0xFF;
868
+ }
869
+ else {
870
+ _this.data[_this.offset + (3 - i)] = (intValue >> shift) & 0xFF;
871
+ }
872
+ shift += 8;
873
+ }
874
+ _this.offset += 4;
875
+ }
876
+ export function rint64(_this, unsigned, endian) {
877
+ _this.check_size(8);
878
+ // Convert the byte array to a BigInt
879
+ let value = BigInt(0);
880
+ if ((endian == undefined ? _this.endian : endian) == "little") {
881
+ for (let i = 0; i < 8; i++) {
882
+ value = value | BigInt(_this.data[_this.offset]) << BigInt(8 * i);
883
+ _this.offset += 1;
884
+ }
885
+ if (unsigned == undefined || unsigned == false) {
886
+ if (value & (BigInt(1) << BigInt(63))) {
887
+ value -= BigInt(1) << BigInt(64);
888
+ }
889
+ return value;
890
+ }
891
+ else {
892
+ return value;
893
+ }
894
+ }
895
+ else {
896
+ for (let i = 0; i < 8; i++) {
897
+ value = (value << BigInt(8)) | BigInt(_this.data[_this.offset]);
898
+ _this.offset += 1;
899
+ }
900
+ if (unsigned == undefined || unsigned == false) {
901
+ if (value & (BigInt(1) << BigInt(63))) {
902
+ value -= BigInt(1) << BigInt(64);
903
+ }
904
+ return value;
905
+ }
906
+ else {
907
+ return value;
908
+ }
909
+ }
910
+ }
911
+ export function wint64(_this, value, unsigned, endian) {
912
+ _this.check_size(8, 0);
913
+ if (unsigned == true) {
914
+ if (value < 0 || value > Math.pow(2, 64) - 1) {
915
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
916
+ throw new Error('Value is out of range for the specified 64bit length.' + " min: " + 0 + " max: " + (Math.pow(2, 64) - 1) + " value: " + value);
917
+ }
918
+ }
919
+ else {
920
+ const maxValue = Math.pow(2, 63) - 1;
921
+ const minValue = -Math.pow(2, 63);
922
+ if (value < minValue || value > maxValue) {
923
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
924
+ throw new Error('Value is out of range for the specified 64bit length.' + " min: " + minValue + " max: " + maxValue + " value: " + value);
925
+ }
926
+ }
927
+ // Convert the BigInt to a 64-bit signed integer
928
+ const bigIntArray = new BigInt64Array(1);
929
+ bigIntArray[0] = BigInt(value);
930
+ // Use two 32-bit views to write the Int64
931
+ const int32Array = new Int32Array(bigIntArray.buffer);
932
+ for (let i = 0; i < 2; i++) {
933
+ if ((endian = undefined ? endian : _this.endian) == "little") {
934
+ if (unsigned == undefined || unsigned == false) {
935
+ _this.data[_this.offset + i * 4 + 0] = int32Array[i];
936
+ _this.data[_this.offset + i * 4 + 1] = (int32Array[i] >> 8);
937
+ _this.data[_this.offset + i * 4 + 2] = (int32Array[i] >> 16);
938
+ _this.data[_this.offset + i * 4 + 3] = (int32Array[i] >> 24);
939
+ }
940
+ else {
941
+ _this.data[_this.offset + i * 4 + 0] = int32Array[i] & 0xFF;
942
+ _this.data[_this.offset + i * 4 + 1] = (int32Array[i] >> 8) & 0xFF;
943
+ _this.data[_this.offset + i * 4 + 2] = (int32Array[i] >> 16) & 0xFF;
944
+ _this.data[_this.offset + i * 4 + 3] = (int32Array[i] >> 24) & 0xFF;
945
+ }
946
+ }
947
+ else {
948
+ if (unsigned == undefined || unsigned == false) {
949
+ _this.data[_this.offset + (1 - i) * 4 + 0] = int32Array[i];
950
+ _this.data[_this.offset + (1 - i) * 4 + 1] = (int32Array[i] >> 8);
951
+ _this.data[_this.offset + (1 - i) * 4 + 2] = (int32Array[i] >> 16);
952
+ _this.data[_this.offset + (1 - i) * 4 + 3] = (int32Array[i] >> 24);
953
+ }
954
+ else {
955
+ _this.data[_this.offset + (1 - i) * 4 + 0] = int32Array[i] & 0xFF;
956
+ _this.data[_this.offset + (1 - i) * 4 + 1] = (int32Array[i] >> 8) & 0xFF;
957
+ _this.data[_this.offset + (1 - i) * 4 + 2] = (int32Array[i] >> 16) & 0xFF;
958
+ _this.data[_this.offset + (1 - i) * 4 + 3] = (int32Array[i] >> 24) & 0xFF;
959
+ }
960
+ }
961
+ }
962
+ _this.offset += 8;
963
+ }
964
+ export function wdfloat(_this, value, endian) {
965
+ _this.check_size(8, 0);
966
+ const maxValue = 1.7976931348623158e308;
967
+ const minValue = 2.2250738585072014e-308;
968
+ if (value < minValue || value > maxValue) {
969
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
970
+ throw new Error('Value is out of range for the specified 64bit length.' + " min: " + minValue + " max: " + maxValue + " value: " + value);
971
+ }
972
+ const intArray = new Int32Array(2);
973
+ const floatArray = new Float64Array(intArray.buffer);
974
+ floatArray[0] = value;
975
+ const bytes = new Uint8Array(intArray.buffer);
976
+ for (let i = 0; i < 8; i++) {
977
+ if ((endian = undefined ? endian : _this.endian) == "little") {
978
+ _this.data[_this.offset + i] = bytes[i];
979
+ }
980
+ else {
981
+ _this.data[_this.offset + (7 - i)] = bytes[i];
982
+ }
983
+ }
984
+ _this.offset += 8;
985
+ }
986
+ export function rdfloat(_this, endian) {
987
+ _this.check_size(8);
988
+ var uint64Value = _this.readInt64(true, (endian == undefined ? _this.endian : endian));
989
+ const sign = (uint64Value & 0x8000000000000000n) >> 63n;
990
+ const exponent = Number((uint64Value & 0x7ff0000000000000n) >> 52n) - 1023;
991
+ const fraction = Number(uint64Value & 0x000fffffffffffffn) / Math.pow(2, 52);
992
+ var floatValue;
993
+ if (exponent == -1023) {
994
+ if (fraction == 0) {
995
+ floatValue = (sign == 0n) ? 0 : -0; // +/-0
996
+ }
997
+ else {
998
+ // Denormalized number
999
+ floatValue = (sign == 0n ? 1 : -1) * Math.pow(2, -1022) * fraction;
1000
+ }
1001
+ }
1002
+ else if (exponent == 1024) {
1003
+ if (fraction == 0) {
1004
+ floatValue = (sign == 0n) ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
1005
+ }
1006
+ else {
1007
+ floatValue = Number.NaN;
1008
+ }
1009
+ }
1010
+ else {
1011
+ // Normalized number
1012
+ floatValue = (sign == 0n ? 1 : -1) * Math.pow(2, exponent) * (1 + fraction);
1013
+ }
1014
+ return floatValue;
1015
+ }
1016
+ export function rstring(_this, options) {
1017
+ var length = options && options.length;
1018
+ var stringType = options && options.stringType || 'utf-8';
1019
+ var terminateValue = options && options.terminateValue;
1020
+ var lengthReadSize = options && options.lengthReadSize || 1;
1021
+ var stripNull = options && options.stripNull || true;
1022
+ var encoding = options && options.encoding || 'utf-8';
1023
+ var endian = options && options.endian || _this.endian;
1024
+ var terminate = terminateValue;
1025
+ if (length != undefined) {
1026
+ _this.check_size(length);
1027
+ }
1028
+ if (typeof terminateValue == "number") {
1029
+ terminate = terminateValue & 0xFF;
1030
+ }
1031
+ else {
1032
+ if (terminateValue != undefined) {
1033
+ throw new Error("terminateValue must be a number");
1034
+ }
1035
+ }
1036
+ if (stringType == 'utf-8' || stringType == 'utf-16') {
1037
+ if (encoding == undefined) {
1038
+ if (stringType == 'utf-8') {
1039
+ encoding = 'utf-8';
1040
+ }
1041
+ if (stringType == 'utf-16') {
1042
+ encoding = 'utf-16';
1043
+ }
1044
+ }
1045
+ // Read the string as UTF-8 encoded untill 0 or terminateValue
1046
+ const encodedBytes = [];
1047
+ if (length == undefined && terminateValue == undefined) {
1048
+ terminate = 0;
1049
+ }
1050
+ var read_length = 0;
1051
+ if (length != undefined) {
1052
+ read_length = length;
1053
+ }
1054
+ else {
1055
+ read_length = _this.data.length - _this.offset;
1056
+ }
1057
+ for (let i = 0; i < read_length; i++) {
1058
+ if (stringType === 'utf-8') {
1059
+ var read = _this.readUByte();
1060
+ if (read == terminate) {
1061
+ break;
1062
+ }
1063
+ else {
1064
+ if (!(stripNull == true && read == 0)) {
1065
+ encodedBytes.push(read);
1066
+ }
1067
+ }
1068
+ }
1069
+ else {
1070
+ var read = _this.readInt16(true, endian);
1071
+ var read1 = read & 0xFF;
1072
+ var read2 = (read >> 8) & 0xFF;
1073
+ if (read == terminate) {
1074
+ break;
1075
+ }
1076
+ else {
1077
+ if (!(stripNull == true && read == 0)) {
1078
+ encodedBytes.push(read1);
1079
+ encodedBytes.push(read2);
1080
+ }
1081
+ }
1082
+ }
1083
+ }
1084
+ return new TextDecoder(encoding).decode(new Uint8Array(encodedBytes));
1085
+ }
1086
+ else if (stringType == 'pascal' || stringType == 'wide-pascal') {
1087
+ if (encoding == undefined) {
1088
+ if (stringType == 'pascal') {
1089
+ encoding = 'utf-8';
1090
+ }
1091
+ if (stringType == 'wide-pascal') {
1092
+ encoding = 'utf-16';
1093
+ }
1094
+ }
1095
+ var maxBytes;
1096
+ if (lengthReadSize == 1) {
1097
+ maxBytes = _this.readUByte();
1098
+ }
1099
+ else if (lengthReadSize == 2) {
1100
+ maxBytes = _this.readInt16(true, endian);
1101
+ }
1102
+ else if (lengthReadSize == 4) {
1103
+ maxBytes = _this.readInt32(true, endian);
1104
+ }
1105
+ else {
1106
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1107
+ throw new Error("Invalid length read size: " + lengthReadSize);
1108
+ }
1109
+ // Read the string as Pascal or Delphi encoded
1110
+ const encodedBytes = [];
1111
+ for (let i = 0; i < maxBytes; i++) {
1112
+ if (stringType == 'wide-pascal') {
1113
+ const read = _this.readInt16(true, endian);
1114
+ if (!(stripNull == true && read == 0)) {
1115
+ encodedBytes.push(read);
1116
+ }
1117
+ }
1118
+ else {
1119
+ const read = _this.readUByte();
1120
+ if (!(stripNull == true && read == 0)) {
1121
+ encodedBytes.push(read);
1122
+ }
1123
+ }
1124
+ }
1125
+ var str_return;
1126
+ if (stringType == 'wide-pascal') {
1127
+ str_return = new TextDecoder(encoding).decode(new Uint16Array(encodedBytes));
1128
+ }
1129
+ else {
1130
+ str_return = new TextDecoder(encoding).decode(new Uint8Array(encodedBytes));
1131
+ }
1132
+ return str_return;
1133
+ }
1134
+ else {
1135
+ throw new Error('Unsupported string type: ' + stringType);
1136
+ }
1137
+ }
1138
+ export function wstring(_this, string, options) {
1139
+ var length = options && options.length;
1140
+ var stringType = options && options.stringType || 'utf-8';
1141
+ var terminateValue = options && options.terminateValue;
1142
+ var lengthWriteSize = options && options.lengthWriteSize || 1;
1143
+ var encoding = options && options.encoding || 'utf-8';
1144
+ var endian = options && options.endian || _this.endian;
1145
+ if (stringType === 'utf-8' || stringType === 'utf-16') {
1146
+ // Encode the string in the specified encoding
1147
+ if (encoding == undefined) {
1148
+ if (stringType == 'utf-8') {
1149
+ encoding = 'utf-8';
1150
+ }
1151
+ if (stringType == 'utf-16') {
1152
+ encoding = 'utf-16';
1153
+ }
1154
+ }
1155
+ const encoder = new TextEncoder();
1156
+ const encodedString = encoder.encode(string);
1157
+ if (length == undefined && terminateValue == undefined) {
1158
+ terminateValue = 0;
1159
+ }
1160
+ var totalLength = (length || encodedString.length) + (terminateValue != undefined ? 1 : 0);
1161
+ if (stringType == 'utf-16') {
1162
+ totalLength = (length || (encodedString.length * 2)) + (terminateValue != undefined ? 2 : 0);
1163
+ }
1164
+ _this.check_size(totalLength, 0);
1165
+ // Write the string bytes to the Uint8Array
1166
+ for (let i = 0; i < encodedString.length; i++) {
1167
+ if (stringType === 'utf-16') {
1168
+ const charCode = encodedString[i];
1169
+ if (endian == "little") {
1170
+ _this.data[_this.offset + i * 2] = charCode & 0xFF;
1171
+ _this.data[_this.offset + i * 2 + 1] = (charCode >> 8) & 0xFF;
1172
+ }
1173
+ else {
1174
+ _this.data[_this.offset + i * 2 + 1] = charCode & 0xFF;
1175
+ _this.data[_this.offset + i * 2] = (charCode >> 8) & 0xFF;
1176
+ }
1177
+ }
1178
+ else {
1179
+ _this.data[_this.offset + i] = encodedString[i];
1180
+ }
1181
+ }
1182
+ if (terminateValue != undefined) {
1183
+ if (stringType === 'utf-16') {
1184
+ _this.data[_this.offset + totalLength - 1] = terminateValue & 0xFF;
1185
+ _this.data[_this.offset + totalLength] = (terminateValue >> 8) & 0xFF;
1186
+ }
1187
+ else {
1188
+ _this.data[_this.offset + totalLength] = terminateValue;
1189
+ }
1190
+ }
1191
+ _this.offset += totalLength;
1192
+ }
1193
+ else if (stringType == 'pascal' || stringType == 'wide-pascal') {
1194
+ if (encoding == undefined) {
1195
+ if (stringType == 'pascal') {
1196
+ encoding = 'utf-8';
1197
+ }
1198
+ if (stringType == 'wide-pascal') {
1199
+ encoding = 'utf-16';
1200
+ }
1201
+ }
1202
+ const encoder = new TextEncoder();
1203
+ // Calculate the length of the string based on the specified max length
1204
+ var maxLength;
1205
+ // Encode the string in the specified encoding
1206
+ if (lengthWriteSize == 1) {
1207
+ maxLength = 255;
1208
+ }
1209
+ else if (lengthWriteSize == 2) {
1210
+ maxLength = 65535;
1211
+ }
1212
+ else if (lengthWriteSize == 4) {
1213
+ maxLength = 4294967295;
1214
+ }
1215
+ else {
1216
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1217
+ throw new Error("Invalid length write size: " + lengthWriteSize);
1218
+ }
1219
+ if (string.length > maxLength || (length || 0) > maxLength) {
1220
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1221
+ throw new Error("String outsize of max write length: " + maxLength);
1222
+ }
1223
+ var maxBytes = Math.min(string.length, maxLength);
1224
+ const encodedString = encoder.encode(string.substring(0, maxBytes));
1225
+ var totalLength = (length || encodedString.length) + lengthWriteSize;
1226
+ if (stringType == 'wide-pascal') {
1227
+ totalLength = (length || (encodedString.length * 2)) + lengthWriteSize;
1228
+ }
1229
+ _this.check_size(totalLength, 0);
1230
+ if (lengthWriteSize == 1) {
1231
+ _this.writeUByte(maxBytes);
1232
+ }
1233
+ else if (lengthWriteSize == 2) {
1234
+ _this.writeUInt16(maxBytes, endian);
1235
+ }
1236
+ else if (lengthWriteSize == 4) {
1237
+ _this.writeUInt32(maxBytes, endian);
1238
+ }
1239
+ // Write the string bytes to the Uint8Array
1240
+ for (let i = 0; i < encodedString.length; i++) {
1241
+ if (stringType == 'wide-pascal') {
1242
+ const charCode = encodedString[i];
1243
+ if (endian == "little") {
1244
+ _this.data[_this.offset + i * 2] = charCode & 0xFF;
1245
+ _this.data[_this.offset + i * 2 + 1] = (charCode >> 8) & 0xFF;
1246
+ }
1247
+ else {
1248
+ _this.data[_this.offset + i * 2 + 1] = charCode & 0xFF;
1249
+ _this.data[_this.offset + i * 2] = (charCode >> 8) & 0xFF;
1250
+ }
1251
+ }
1252
+ else {
1253
+ _this.data[_this.offset + i] = encodedString[i];
1254
+ }
1255
+ }
1256
+ _this.offset += totalLength;
1257
+ }
1258
+ else {
1259
+ throw new Error('Unsupported string type: ' + stringType);
444
1260
  }
445
1261
  }
446
1262
  //# sourceMappingURL=common.js.map