bireader 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,7 +8,7 @@ Supported data types are:
8
8
  - [Bytes](#byte) ([u]int8, byte) 8 bit signed or unsigned value
9
9
  - [Shorts](#short) ([u]int16, word, short{le|be}) 16 bit signed or unsigned value in big or little endian order
10
10
  - [Half Floats](#half-float) (halffloat, half{le|be}) 16 bit decimal value in big or little endian order
11
- - [Integers](#integer) ([u]int32, int, double{le|be}) 32 bit signed or unsigned value in big or little endian order
11
+ - [Integers](#integer) ([u]int32, long, int, double{le|be}) 32 bit signed or unsigned value in big or little endian order
12
12
  - [Floats](#float) (float{le|be}) 32 bit decimal value in big or little endian
13
13
  - [Quadwords](#quadword) ([u]int64, quad, bigint{le|be}) 64 bit signed or unsigned in big or little endian
14
14
  - [Double Floats](#double-float) (doublefloat, dfloat{le|be}) 64 bit decimal value in big or little endian
@@ -341,7 +341,7 @@ Parse value as a half float (aka half). Can be in little or big endian order (``
341
341
 
342
342
  ## Integer
343
343
 
344
- Parse value as a int32 (aka int or double). Can be signed or unsigned (with a ``u`` at the start) and in little or big endian order (``be`` or ``le`` at the end).
344
+ Parse value as a int32 (aka int, long or double). Can be signed or unsigned (with a ``u`` at the start) and in little or big endian order (``be`` or ``le`` at the end).
345
345
 
346
346
  <table>
347
347
  <thead>
@@ -363,12 +363,12 @@ Parse value as a int32 (aka int or double). Can be signed or unsigned (with a ``
363
363
  </tr>
364
364
  <tr>
365
365
  <td align="center"><b>Presets (reader)</b></td>
366
- <td>[u]{int32|int|double}{be|le}(*unsigned, *endian)</td>
366
+ <td>[u]{int32|long|int|double}{be|le}(*unsigned, *endian)</td>
367
367
  <td>If value is unsigned, little or big endian.<br>*Note: functions without the starting letter <u>u</u> can still be called unsigned when <b>true</b> is the <i>first</i> augment, and functions without ending letters <u>be</u> or <u>le</u> can still be called the endian in the <i>second</i> augment (does not overwite set endian).</td>
368
368
  </tr>
369
369
  <tr>
370
370
  <td align="center"><b>Presets (writer)</b></td>
371
- <td>[u]{int32|int|double}{be|le}(<b>value</b>, offsetBytes, *unsigned, *endian)</td>
371
+ <td>[u]{int32|long|int|double}{be|le}(<b>value</b>, offsetBytes, *unsigned, *endian)</td>
372
372
  <td><b>value to write</b>, byte offset from current position, if value is unsigned, little or big endian<br>*Note: functions without the starting letter <u>u</u> can still be called unsigned when <b>true</b> is the <i>third</i> augment, and functions without ending letters <u>be</u> or <u>le</u> can still be called the endian in the <i>fourth</i> augment (does not overwite set endian).</td>
373
373
  </tr>
374
374
  </tbody>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bireader",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Read and write data in binary",
5
5
  "main": "index.js",
6
6
  "directories": {
package/src/reader.js CHANGED
@@ -21,6 +21,10 @@ class bireader {
21
21
  }
22
22
  }
23
23
 
24
+ #flip = function(unsignedValue){
25
+ return unsignedValue > 127 ? unsignedValue - 256 : unsignedValue;
26
+ }
27
+
24
28
  /**
25
29
  *
26
30
  * byte reader, includes bitfields and strings
@@ -2473,7 +2477,7 @@ class bireader {
2473
2477
  */
2474
2478
  readByte = this.byte = this.int8 = function(unsigned){
2475
2479
  this.#check_size(1)
2476
- const read = (this.data[this.offset])
2480
+ const read = this.#flip(this.data[this.offset])
2477
2481
  this.offset += 1
2478
2482
  if(unsigned == true){
2479
2483
  return read & 0xFF
@@ -2506,9 +2510,9 @@ class bireader {
2506
2510
  this.#check_size(2)
2507
2511
  var read
2508
2512
  if((endian != undefined ? endian : this.endian) == "little"){
2509
- read = (this.data[this.offset + 1] << 8) | this.data[this.offset];
2513
+ read = (this.#flip(this.data[this.offset + 1]) << 8) | this.#flip(this.data[this.offset]);
2510
2514
  } else {
2511
- read = (this.data[this.offset] << 8) | this.data[this.offset + 1];
2515
+ read = (this.#flip(this.data[this.offset]) << 8) | this.#flip(this.data[this.offset + 1]);
2512
2516
  }
2513
2517
  this.offset += 2
2514
2518
  if(unsigned == undefined || unsigned == false){
@@ -2521,6 +2525,17 @@ class bireader {
2521
2525
  /**
2522
2526
  * Read unsigned short
2523
2527
  *
2528
+ * @param {string} endian - ```big``` or ```little```
2529
+ *
2530
+ * @returns number
2531
+ */
2532
+ readUInt16 = this.uint16 = this.ushort = this.uword = function(endian){
2533
+ return this.readInt16(true, endian)
2534
+ }
2535
+
2536
+ /**
2537
+ * Read unsigned short in little endian
2538
+ *
2524
2539
  * @returns number
2525
2540
  */
2526
2541
  readUInt16LE = this.uint16le = this.ushortle = this.uwordle = function(){
@@ -2528,7 +2543,7 @@ class bireader {
2528
2543
  }
2529
2544
 
2530
2545
  /**
2531
- * Read signed short
2546
+ * Read signed short in little endian
2532
2547
  *
2533
2548
  * @returns number
2534
2549
  */
@@ -2537,7 +2552,7 @@ class bireader {
2537
2552
  }
2538
2553
 
2539
2554
  /**
2540
- * Read unsigned short
2555
+ * Read unsigned short in big endian
2541
2556
  *
2542
2557
  * @returns number
2543
2558
  */
@@ -2546,7 +2561,7 @@ class bireader {
2546
2561
  }
2547
2562
 
2548
2563
  /**
2549
- * Read signed short
2564
+ * Read signed short in big endian
2550
2565
  *
2551
2566
  * @returns number
2552
2567
  */
@@ -2628,13 +2643,13 @@ class bireader {
2628
2643
  * @param {string} endian - ```big``` or ```little```
2629
2644
  * @returns number
2630
2645
  */
2631
- readInt32 = this.int = this.double = this.int32 = function(unsigned, endian){
2646
+ readInt32 = this.int = this.double = this.int32 = this.long = function(unsigned, endian){
2632
2647
  this.#check_size(4)
2633
2648
  var read;
2634
2649
  if((endian != undefined ? endian : this.endian) == "little"){
2635
- read = ((this.data[this.offset + 3] << 24) | (this.data[this.offset + 2] << 16) | (this.data[this.offset + 1] << 8) | this.data[this.offset])
2650
+ read = ((this.#flip(this.data[this.offset + 3]) << 24) | (this.#flip(this.data[this.offset + 2]) << 16) | (this.#flip(this.data[this.offset + 1]) << 8) | this.#flip(this.data[this.offset]))
2636
2651
  } else {
2637
- read = (this.data[this.offset] << 24) | (this.data[this.offset + 1] << 16) | (this.data[this.offset + 2] << 8) | this.data[this.offset + 3]
2652
+ read = (this.#flip(this.data[this.offset]) << 24) | (this.#flip(this.data[this.offset + 1]) << 16) | (this.#flip(this.data[this.offset + 2]) << 8) | this.#flip(this.data[this.offset + 3])
2638
2653
  }
2639
2654
  this.offset += 4
2640
2655
  if(unsigned == undefined || unsigned == false){
@@ -2649,7 +2664,7 @@ class bireader {
2649
2664
  *
2650
2665
  * @returns number
2651
2666
  */
2652
- readUInt = this.uint = this.udouble = this.uint32 = function(){
2667
+ readUInt = this.uint = this.udouble = this.uint32 = this.ulong = function(){
2653
2668
  return this.readInt32(true)
2654
2669
  }
2655
2670
 
@@ -2658,7 +2673,7 @@ class bireader {
2658
2673
  *
2659
2674
  * @returns number
2660
2675
  */
2661
- readInt32BE = this.intbe = this.doublebe = this.int32be = function(){
2676
+ readInt32BE = this.intbe = this.doublebe = this.int32be = this.longbe = function(){
2662
2677
  return this.readInt32(false, "big")
2663
2678
  }
2664
2679
 
@@ -2667,7 +2682,7 @@ class bireader {
2667
2682
  *
2668
2683
  * @returns number
2669
2684
  */
2670
- readUInt32BE = this.uintbe = this.udoublebe = this.uint32be = function(){
2685
+ readUInt32BE = this.uintbe = this.udoublebe = this.uint32be = this.ulongbe = function(){
2671
2686
  return this.readInt32(true, "big")
2672
2687
  }
2673
2688
 
@@ -2676,7 +2691,7 @@ class bireader {
2676
2691
  *
2677
2692
  * @returns number
2678
2693
  */
2679
- readInt32LE = this.intle = this.doublele = this.int32le = function(){
2694
+ readInt32LE = this.intle = this.doublele = this.int32le = this.longle = function(){
2680
2695
  return this.readInt32(false, "little")
2681
2696
  }
2682
2697
 
@@ -2685,7 +2700,7 @@ class bireader {
2685
2700
  *
2686
2701
  * @returns number
2687
2702
  */
2688
- readUInt32LE = this.uintle = this.udoublele = this.uint32le = function(){
2703
+ readUInt32LE = this.uintle = this.udoublele = this.uint32le = this.ulongle = function(){
2689
2704
  return this.readInt32(true, "little")
2690
2705
  }
2691
2706
 
@@ -2766,7 +2781,7 @@ class bireader {
2766
2781
  let value = BigInt(0);
2767
2782
  if((endian == undefined ? this.endian : endian) == "little"){
2768
2783
  for (let i = 0; i < 8; i++) {
2769
- value |= BigInt(this.data[this.offset]) << BigInt(8 * i);
2784
+ value |= BigInt(this.#flip(this.data[this.offset])) << BigInt(8 * i);
2770
2785
  this.offset += 1
2771
2786
  }
2772
2787
  if(unsigned == undefined || unsigned == false){
@@ -2779,7 +2794,7 @@ class bireader {
2779
2794
  }
2780
2795
  } else {
2781
2796
  for (let i = 0; i < 8; i++) {
2782
- value = (value << BigInt(8)) | BigInt(this.data[this.offset]);
2797
+ value = (value << BigInt(8)) | BigInt(this.#flip(this.data[this.offset]));
2783
2798
  this.offset += 1
2784
2799
  }
2785
2800
  if(unsigned == undefined || unsigned == false){
@@ -2966,7 +2981,15 @@ class bireader {
2966
2981
  terminate = 0
2967
2982
  }
2968
2983
 
2969
- while (this.offset < this.data.length) {
2984
+ var read_length = 0;
2985
+
2986
+ if(length != undefined){
2987
+ read_length = length
2988
+ } else {
2989
+ read_length = this.data.length - this.offset
2990
+ }
2991
+
2992
+ for (let i = 0; i < read_length; i++) {
2970
2993
  if (stringType === 'utf-8') {
2971
2994
  var read = this.readUByte();
2972
2995
  if(read == terminate){
package/src/writer.js CHANGED
@@ -7,7 +7,7 @@
7
7
  * @param {string} endianness - endianness ``big`` or ``little`` (default little)
8
8
  * @param {boolean} strict - strict mode: if true does not extend supplied array on outside write (default false)
9
9
  */
10
- class biwriter {
10
+ class biwriter {
11
11
  endian = "little";
12
12
  offset = 0;
13
13
  bitoffset = 0;
@@ -3305,7 +3305,7 @@
3305
3305
  * @param {boolean} unsigned - if the value is unsigned
3306
3306
  * @param {string} endian - ``big`` or ``little`
3307
3307
  */
3308
- writeInt32 = this.int = this.int32 = this.double = function(value, offset, unsigned, endian) {
3308
+ writeInt32 = this.int = this.int32 = this.double = this.long = function(value, offset, unsigned, endian) {
3309
3309
  this.#check_size(4,0,offset)
3310
3310
  if (unsigned == true) {
3311
3311
  if (value < 0 || value > 4294967295) {
@@ -3339,7 +3339,7 @@
3339
3339
  * @param {number} offset - byte offset (default last write position)
3340
3340
  * @param {string} endian - ``big`` or ``little`
3341
3341
  */
3342
- writeUInt32 = this.uint32 = this.uint = this.udouble= function(value, offset, endian) {
3342
+ writeUInt32 = this.uint32 = this.uint = this.udouble = this.ulong = function(value, offset, endian) {
3343
3343
  return this.writeInt32(value, offset, true, endian)
3344
3344
  }
3345
3345
 
@@ -3349,7 +3349,7 @@
3349
3349
  * @param {number} value - value as int
3350
3350
  * @param {number} offset - byte offset (default last write position)
3351
3351
  */
3352
- writeInt32LE = this.int32le = this.intle = this.doublele = function(value, offset) {
3352
+ writeInt32LE = this.int32le = this.intle = this.doublele = this.longle = function(value, offset) {
3353
3353
  return this.writeInt32(value, offset, false, "little")
3354
3354
  }
3355
3355
 
@@ -3359,7 +3359,7 @@
3359
3359
  * @param {number} value - value as int
3360
3360
  * @param {number} offset - byte offset (default last write position)
3361
3361
  */
3362
- writeUInt32LE = this.uint32le = this.uintle = this.udoublele = function(value, offset) {
3362
+ writeUInt32LE = this.uint32le = this.uintle = this.udoublele = this.ulongle = function(value, offset) {
3363
3363
  return this.writeInt32(value, offset, true, "little")
3364
3364
  }
3365
3365
 
@@ -3369,7 +3369,7 @@
3369
3369
  * @param {number} value - value as int
3370
3370
  * @param {number} offset - byte offset (default last write position)
3371
3371
  */
3372
- writeInt32BE = this.int32be = this.int32be = this.doublebe = function(value, offset) {
3372
+ writeInt32BE = this.int32be = this.int32be = this.doublebe = this.longbe = function(value, offset) {
3373
3373
  return this.writeInt32(value, offset, false, "big")
3374
3374
  }
3375
3375
 
@@ -3379,7 +3379,7 @@
3379
3379
  * @param {number} value - value as int
3380
3380
  * @param {number} offset - byte offset (default last write position)
3381
3381
  */
3382
- writeUInt32BE = this.uint32be = this.uint32be = this.udoublebe = function(value, offset) {
3382
+ writeUInt32BE = this.uint32be = this.uint32be = this.udoublebe = this.ulongbe = function(value, offset) {
3383
3383
  return this.writeInt32(value, offset, true, "big")
3384
3384
  }
3385
3385