bireader 2.0.0 → 2.0.1

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.
@@ -0,0 +1,3531 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReaderBase = exports.wstring = exports.rstring = exports.rdfloat = exports.wdfloat = exports.wint64 = exports.rint64 = exports.wfloat = exports.rfloat = exports.rint32 = exports.wint32 = exports.whalffloat = exports.rhalffloat = exports.rint16 = exports.wint16 = exports.rbyte = exports.wbyte = exports.rbit = exports.wbit = exports.fDoubleFloat = exports.fBigInt = exports.fFloat = exports.fHalfFloat = exports.fNumber = exports.fString = exports.ADD = exports.RSHIFT = exports.LSHIFT = exports.NOT = exports.XOR = exports.OR = exports.AND = exports.hexDump = exports.hexdump = exports.addData = exports.remove = exports.goto = exports.alignRev = exports.align = exports.skip = exports.checkSize = exports.extendarray = exports.arraybuffcheck = exports.buffcheck = exports.check_size = exports.isBuffer = void 0;
4
+ function isBuffer(obj) {
5
+ return buffcheck(obj);
6
+ }
7
+ exports.isBuffer = isBuffer;
8
+ function check_size(_this, write_bytes, write_bit, offset) {
9
+ return checkSize(_this, write_bytes || 0, write_bit || 0, offset || _this.offset);
10
+ }
11
+ exports.check_size = check_size;
12
+ function buffcheck(obj) {
13
+ return (typeof Buffer !== 'undefined' && obj instanceof Buffer);
14
+ }
15
+ exports.buffcheck = buffcheck;
16
+ function arraybuffcheck(_this, obj) {
17
+ return obj instanceof Uint8Array || isBuffer(obj);
18
+ }
19
+ exports.arraybuffcheck = arraybuffcheck;
20
+ function extendarray(_this, to_padd) {
21
+ if ((typeof Buffer !== 'undefined' && _this.data instanceof Buffer)) {
22
+ var paddbuffer = Buffer.alloc(to_padd);
23
+ _this.data = Buffer.concat([_this.data, paddbuffer]);
24
+ }
25
+ else {
26
+ const addArray = new Array(to_padd);
27
+ _this.data = new Uint8Array([..._this.data, ...addArray]);
28
+ }
29
+ _this.size = _this.data.length;
30
+ _this.sizeB = _this.data.length * 8;
31
+ }
32
+ exports.extendarray = extendarray;
33
+ function checkSize(_this, write_bytes, write_bit, offset) {
34
+ const bits = (write_bit || 0) + _this.bitoffset;
35
+ var new_off = (offset || _this.offset);
36
+ var writesize = write_bytes || 0;
37
+ if (bits != 0) {
38
+ //add bits
39
+ writesize += Math.ceil(bits / 8);
40
+ }
41
+ //if biger extend
42
+ const needed_size = new_off + writesize;
43
+ if (needed_size > _this.size) {
44
+ const dif = needed_size - _this.size;
45
+ if (_this.strict == false) {
46
+ _this.extendArray(dif);
47
+ }
48
+ else {
49
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m hexdump:\n" + _this.hexdump() : "";
50
+ throw new Error(`\x1b[33m[Strict mode]\x1b[0m: Reached end of data: writing to ` + needed_size + " at " + _this.offset + " of " + _this.size);
51
+ }
52
+ }
53
+ //start read location
54
+ return new_off;
55
+ }
56
+ exports.checkSize = checkSize;
57
+ function skip(_this, bytes, bits) {
58
+ var new_size = (((bytes || 0) + _this.offset) + Math.ceil((_this.bitoffset + (bits || 0)) / 8));
59
+ if (bits && bits < 0) {
60
+ new_size = Math.floor(((((bytes || 0) + _this.offset) * 8) + _this.bitoffset + (bits || 0)) / 8);
61
+ }
62
+ if (new_size > _this.size) {
63
+ if (_this.strict == false) {
64
+ _this.extendArray(new_size - _this.size);
65
+ }
66
+ else {
67
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m hexdump:\n" + _this.hexdump() : "";
68
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: Seek of range of data: seek " + new_size + " of " + _this.size);
69
+ }
70
+ }
71
+ // Adjust byte offset based on bit overflow
72
+ _this.offset += Math.floor((_this.bitoffset + (bits || 0)) / 8);
73
+ // Adjust bit offset
74
+ _this.bitoffset = (_this.bitoffset + (bits || 0) + 64) % 8;
75
+ // Adjust byte offset based on byte overflow
76
+ _this.offset += bytes;
77
+ // Ensure bit offset stays between 0-7
78
+ _this.bitoffset = Math.min(Math.max(_this.bitoffset, 0), 7);
79
+ // Ensure offset doesn't go negative
80
+ _this.offset = Math.max(_this.offset, 0);
81
+ }
82
+ exports.skip = skip;
83
+ function align(_this, n) {
84
+ var a = _this.offset % n;
85
+ if (a) {
86
+ _this.skip(n - a);
87
+ }
88
+ }
89
+ exports.align = align;
90
+ function alignRev(_this, n) {
91
+ var a = _this.offset % n;
92
+ if (a) {
93
+ _this.skip(a * -1);
94
+ }
95
+ }
96
+ exports.alignRev = alignRev;
97
+ function goto(_this, bytes, bits) {
98
+ var new_size = (((bytes || 0)) + Math.ceil(((bits || 0)) / 8));
99
+ if (bits && bits < 0) {
100
+ new_size = Math.floor(((((bytes || 0)) * 8) + (bits || 0)) / 8);
101
+ }
102
+ if (new_size > _this.size) {
103
+ if (_this.strict == false) {
104
+ _this.extendArray(new_size - _this.size);
105
+ }
106
+ else {
107
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m hexdump:\n" + _this.hexdump() : "";
108
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: Goto utside of range of data: goto " + new_size + " of " + _this.size);
109
+ }
110
+ }
111
+ _this.offset = bytes;
112
+ // Adjust byte offset based on bit overflow
113
+ _this.offset += Math.floor(((bits || 0)) / 8);
114
+ // Adjust bit offset
115
+ _this.bitoffset = ((bits || 0) + 64) % 8;
116
+ // Ensure bit offset stays between 0-7
117
+ _this.bitoffset = Math.min(Math.max(_this.bitoffset, 0), 7);
118
+ // Ensure offset doesn't go negative
119
+ _this.offset = Math.max(_this.offset, 0);
120
+ }
121
+ exports.goto = goto;
122
+ function remove(_this, startOffset, endOffset, consume, remove, fillValue) {
123
+ const new_start = Math.abs(startOffset || 0);
124
+ const new_offset = (endOffset || _this.offset);
125
+ if (new_offset > _this.size) {
126
+ if (_this.strict == false) {
127
+ _this.extendArray(new_offset - _this.size);
128
+ }
129
+ else {
130
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
131
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + endOffset + " of " + _this.size);
132
+ }
133
+ }
134
+ if (_this.strict == true && remove == true) {
135
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
136
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: Can not remove data in strict mode: endOffset" + endOffset + " of " + _this.size);
137
+ }
138
+ const data_removed = _this.data.slice(new_start, new_offset);
139
+ if (remove) {
140
+ const part1 = _this.data.subarray(0, new_start);
141
+ const part2 = _this.data.subarray(new_offset, _this.size);
142
+ if (isBuffer(_this.data)) {
143
+ _this.data = Buffer.concat([part1, part2]);
144
+ }
145
+ else {
146
+ _this.data = new Uint8Array([...part1, ...part2]);
147
+ }
148
+ _this.size = _this.data.length;
149
+ _this.sizeB = _this.data.length * 8;
150
+ }
151
+ if (fillValue != undefined && remove == false) {
152
+ const part1 = _this.data.subarray(0, new_start);
153
+ const part2 = _this.data.subarray(new_offset, _this.size);
154
+ const replacement = new Array(data_removed.length).fill(fillValue & 0xff);
155
+ if (isBuffer(_this.data)) {
156
+ const buff_placement = Buffer.from(replacement);
157
+ _this.data = Buffer.concat([part1, buff_placement, part2]);
158
+ }
159
+ else {
160
+ _this.data = new Uint8Array([...part1, ...replacement, ...part2]);
161
+ }
162
+ _this.size = _this.data.length;
163
+ _this.sizeB = _this.data.length * 8;
164
+ }
165
+ if (consume == true) {
166
+ if (remove != true) {
167
+ _this.offset = new_offset;
168
+ _this.bitoffset = 0;
169
+ }
170
+ else {
171
+ _this.offset = new_start;
172
+ _this.bitoffset = 0;
173
+ }
174
+ }
175
+ return data_removed;
176
+ }
177
+ exports.remove = remove;
178
+ function addData(_this, data, consume, offset, replace) {
179
+ if (_this.strict == true) {
180
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
181
+ throw new Error(`\x1b[33m[Strict mode]\x1b[0m: Can not insert data in strict mode. Use unrestrict() to enable.`);
182
+ }
183
+ if (typeof Buffer !== 'undefined' && data instanceof Buffer && !(_this.data instanceof Buffer)) {
184
+ throw new Error("Data insert must be a Buffer");
185
+ }
186
+ if (data instanceof Uint8Array && !(_this.data instanceof Uint8Array)) {
187
+ throw new Error("Data insert must be a Uint8Array");
188
+ }
189
+ var needed_size = offset || _this.offset;
190
+ if (replace) {
191
+ needed_size = (offset || _this.offset) + data.length;
192
+ }
193
+ if (replace) {
194
+ const part1 = _this.data.subarray(0, needed_size - data.length);
195
+ const part2 = _this.data.subarray(needed_size, _this.size);
196
+ if (isBuffer(_this.data)) {
197
+ _this.data = Buffer.concat([part1, data, part2]);
198
+ }
199
+ else {
200
+ _this.data = new Uint8Array([...part1, ...data, ...part2]);
201
+ }
202
+ _this.size = _this.data.length;
203
+ _this.sizeB = _this.data.length * 8;
204
+ }
205
+ else {
206
+ const part1 = _this.data.subarray(0, needed_size);
207
+ const part2 = _this.data.subarray(needed_size, _this.size);
208
+ if (isBuffer(_this.data)) {
209
+ _this.data = Buffer.concat([part1, data, part2]);
210
+ }
211
+ else {
212
+ _this.data = new Uint8Array([...part1, ...data, ...part2]);
213
+ }
214
+ _this.size = _this.data.length;
215
+ _this.sizeB = _this.data.length * 8;
216
+ }
217
+ if (consume) {
218
+ _this.offset = (offset || _this.offset) + data.length;
219
+ _this.bitoffset = 0;
220
+ }
221
+ }
222
+ exports.addData = addData;
223
+ /**
224
+ * Console logs provided data as hex dump.
225
+ *
226
+ * @param {Uint8Array|Buffer} src - Uint8Array or Buffer
227
+ * @param {object} options
228
+ * ```javascript
229
+ * {
230
+ * length: 192, // number of bytes to log, default 192 or end of data
231
+ * startByte: 0, // byte to start dump (default 0)
232
+ * supressUnicode: false // Supress unicode character preview for even columns
233
+ * }
234
+ * ```
235
+ */
236
+ function hexdump(src, options) {
237
+ if (!(src instanceof Uint8Array || isBuffer(src))) {
238
+ throw new Error("Write data must be Uint8Array or Buffer.");
239
+ }
240
+ const fake_reader = {
241
+ data: src,
242
+ size: src.length,
243
+ offset: options && options.startByte || 0,
244
+ errorDump: true,
245
+ extendArray: extendarray,
246
+ };
247
+ hexDump(fake_reader, options);
248
+ }
249
+ exports.hexdump = hexdump;
250
+ function hexDump(_this, options) {
251
+ var length = options && options.length;
252
+ var startByte = options && options.startByte;
253
+ var supressUnicode = options && options.supressUnicode || false;
254
+ if ((startByte || 0) > _this.size) {
255
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
256
+ throw new Error("Hexdump start is outside of data size: " + startByte + " of " + _this.size);
257
+ }
258
+ const start = startByte || _this.offset;
259
+ const end = Math.min(start + (length || 192), _this.size);
260
+ if (start + (length || 0) > _this.size) {
261
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
262
+ throw new Error("Hexdump amount is outside of data size: " + (start + (length || 0)) + " of " + end);
263
+ }
264
+ function hex_check(byte, bits) {
265
+ var value = 0;
266
+ for (var i = 0; i < bits;) {
267
+ var remaining = bits - i;
268
+ var bitOffset = 0;
269
+ var currentByte = byte;
270
+ var read = Math.min(remaining, 8 - bitOffset);
271
+ var mask, readBits;
272
+ mask = ~(0xFF << read);
273
+ readBits = (currentByte >> (8 - read - bitOffset)) & mask;
274
+ value <<= read;
275
+ value |= readBits;
276
+ i += read;
277
+ }
278
+ value = value >>> 0;
279
+ return value;
280
+ }
281
+ const rows = [];
282
+ var header = " 0 1 2 3 4 5 6 7 8 9 A B C D E F ";
283
+ var ending = "0123456789ABCDEF";
284
+ var addr = "";
285
+ for (let i = start; i < end; i += 16) {
286
+ addr = i.toString(16).padStart(5, '0');
287
+ var row = _this.data?.slice(i, i + 16) || [];
288
+ var hex = Array.from(row, (byte) => byte.toString(16).padStart(2, '0')).join(' ');
289
+ rows.push(`${addr} ${hex.padEnd(47)} `);
290
+ }
291
+ let result = '';
292
+ let make_wide = false;
293
+ let i = start;
294
+ while (i < end) {
295
+ const byte = _this.data[i];
296
+ if (byte < 32 || byte == 127) {
297
+ result += '.';
298
+ }
299
+ else if (byte < 127) {
300
+ // Valid UTF-8 start byte or single-byte character
301
+ // Convert the byte to a character and add it to the result
302
+ result += String.fromCharCode(byte);
303
+ }
304
+ else if (supressUnicode) {
305
+ result += '.';
306
+ }
307
+ else if (hex_check(byte, 1) == 0) {
308
+ //Byte 1
309
+ result += String.fromCharCode(byte);
310
+ }
311
+ else if (hex_check(byte, 3) == 6) {
312
+ //Byte 2
313
+ if (i + 1 <= end) {
314
+ //check second byte
315
+ const byte2 = _this.data[i + 1];
316
+ if (hex_check(byte2, 2) == 2) {
317
+ const charCode = ((byte & 0x1f) << 6) | (byte2 & 0x3f);
318
+ i++;
319
+ make_wide = true;
320
+ const read = " " + String.fromCharCode(charCode);
321
+ result += read;
322
+ }
323
+ else {
324
+ result += ".";
325
+ }
326
+ }
327
+ else {
328
+ result += ".";
329
+ }
330
+ }
331
+ else if (hex_check(byte, 4) == 14) {
332
+ //Byte 3
333
+ if (i + 1 <= end) {
334
+ //check second byte
335
+ const byte2 = _this.data[i + 1];
336
+ if (hex_check(byte2, 2) == 2) {
337
+ if (i + 2 <= end) {
338
+ //check third byte
339
+ const byte3 = _this.data[i + 2];
340
+ if (hex_check(byte3, 2) == 2) {
341
+ const charCode = ((byte & 0x0f) << 12) |
342
+ ((byte2 & 0x3f) << 6) |
343
+ (byte3 & 0x3f);
344
+ i += 2;
345
+ make_wide = true;
346
+ const read = " " + String.fromCharCode(charCode);
347
+ result += read;
348
+ }
349
+ else {
350
+ i++;
351
+ result += " .";
352
+ }
353
+ }
354
+ else {
355
+ i++;
356
+ result += " .";
357
+ }
358
+ }
359
+ else {
360
+ result += ".";
361
+ }
362
+ }
363
+ else {
364
+ result += ".";
365
+ }
366
+ }
367
+ else if (hex_check(byte, 5) == 28) {
368
+ //Byte 4
369
+ if (i + 1 <= end) {
370
+ //check second byte
371
+ const byte2 = _this.data[i + 1];
372
+ if (hex_check(byte2, 2) == 2) {
373
+ if (i + 2 <= end) {
374
+ //check third byte
375
+ const byte3 = _this.data[i + 2];
376
+ if (hex_check(byte3, 2) == 2) {
377
+ if (i + 3 <= end) {
378
+ //check fourth byte
379
+ const byte4 = _this.data[i + 2];
380
+ if (hex_check(byte4, 2) == 2) {
381
+ const charCode = (((byte4 & 0xFF) << 24) | ((byte3 & 0xFF) << 16) | ((byte2 & 0xFF) << 8) | (byte & 0xFF));
382
+ i += 3;
383
+ make_wide = true;
384
+ const read = " " + String.fromCharCode(charCode);
385
+ result += read;
386
+ }
387
+ else {
388
+ i += 2;
389
+ result += " .";
390
+ }
391
+ }
392
+ else {
393
+ i += 2;
394
+ result += " .";
395
+ }
396
+ }
397
+ else {
398
+ i++;
399
+ result += " .";
400
+ }
401
+ }
402
+ else {
403
+ i++;
404
+ result += " .";
405
+ }
406
+ }
407
+ else {
408
+ result += ".";
409
+ }
410
+ }
411
+ else {
412
+ result += ".";
413
+ }
414
+ }
415
+ else {
416
+ // Invalid UTF-8 byte, add a period to the result
417
+ result += '.';
418
+ }
419
+ i++;
420
+ }
421
+ const chunks = result.match(new RegExp(`.{1,${16}}`, 'g'));
422
+ chunks?.forEach((self, i) => {
423
+ rows[i] = rows[i] + (make_wide ? "|" + self + "|" : self);
424
+ });
425
+ header = "".padStart(addr.length) + header + (make_wide ? "" : ending);
426
+ rows.unshift(header);
427
+ if (make_wide) {
428
+ rows.push("*Removed character byte header on unicode detection");
429
+ }
430
+ console.log(rows.join("\n"));
431
+ }
432
+ exports.hexDump = hexDump;
433
+ function AND(_this, and_key, start, end, consume) {
434
+ const input = _this.data;
435
+ if ((end || 0) > _this.size) {
436
+ if (_this.strict == false) {
437
+ _this.extendArray((end || 0) - _this.size);
438
+ }
439
+ else {
440
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
441
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
442
+ }
443
+ }
444
+ if (typeof and_key == "number") {
445
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
446
+ input[i] = input[i] & (and_key & 0xff);
447
+ if (consume) {
448
+ _this.offset = i;
449
+ _this.bitoffset = 0;
450
+ }
451
+ }
452
+ }
453
+ else {
454
+ if (arraybuffcheck(_this, and_key)) {
455
+ let number = -1;
456
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
457
+ if (number != and_key.length - 1) {
458
+ number = number + 1;
459
+ }
460
+ else {
461
+ number = 0;
462
+ }
463
+ input[i] = input[i] & and_key[number];
464
+ if (consume) {
465
+ _this.offset = i;
466
+ _this.bitoffset = 0;
467
+ }
468
+ }
469
+ }
470
+ else {
471
+ throw new Error("AND key must be a byte value, string, Uint8Array or Buffer");
472
+ }
473
+ }
474
+ }
475
+ exports.AND = AND;
476
+ function OR(_this, or_key, start, end, consume) {
477
+ const input = _this.data;
478
+ if ((end || 0) > _this.size) {
479
+ if (_this.strict == false) {
480
+ _this.extendArray((end || 0) - _this.size);
481
+ }
482
+ else {
483
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
484
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
485
+ }
486
+ }
487
+ if (typeof or_key == "number") {
488
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
489
+ input[i] = input[i] | (or_key & 0xff);
490
+ if (consume) {
491
+ _this.offset = i;
492
+ _this.bitoffset = 0;
493
+ }
494
+ }
495
+ }
496
+ else {
497
+ if (arraybuffcheck(_this, or_key)) {
498
+ let number = -1;
499
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
500
+ if (number != or_key.length - 1) {
501
+ number = number + 1;
502
+ }
503
+ else {
504
+ number = 0;
505
+ }
506
+ input[i] = input[i] | or_key[number];
507
+ if (consume) {
508
+ _this.offset = i;
509
+ _this.bitoffset = 0;
510
+ }
511
+ }
512
+ }
513
+ else {
514
+ throw new Error("OR key must be a byte value, string, Uint8Array or Buffer");
515
+ }
516
+ }
517
+ }
518
+ exports.OR = OR;
519
+ function XOR(_this, xor_key, start, end, consume) {
520
+ const input = _this.data;
521
+ if ((end || 0) > _this.size) {
522
+ if (_this.strict == false) {
523
+ _this.extendArray((end || 0) - _this.size);
524
+ }
525
+ else {
526
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
527
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
528
+ }
529
+ }
530
+ if (typeof xor_key == "number") {
531
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
532
+ input[i] = input[i] ^ (xor_key & 0xff);
533
+ if (consume) {
534
+ _this.offset = i;
535
+ _this.bitoffset = 0;
536
+ }
537
+ }
538
+ }
539
+ else {
540
+ if (arraybuffcheck(_this, xor_key)) {
541
+ let number = -1;
542
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
543
+ if (number != xor_key.length - 1) {
544
+ number = number + 1;
545
+ }
546
+ else {
547
+ number = 0;
548
+ }
549
+ input[i] = input[i] ^ xor_key[number];
550
+ if (consume) {
551
+ _this.offset = i;
552
+ _this.bitoffset = 0;
553
+ }
554
+ }
555
+ }
556
+ else {
557
+ throw new Error("XOR key must be a byte value, string, Uint8Array or Buffer");
558
+ }
559
+ }
560
+ }
561
+ exports.XOR = XOR;
562
+ function NOT(_this, start, end, consume) {
563
+ if ((end || 0) > _this.size) {
564
+ if (_this.strict == false) {
565
+ _this.extendArray((end || 0) - _this.size);
566
+ }
567
+ else {
568
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
569
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
570
+ }
571
+ }
572
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
573
+ _this.data[i] = ~_this.data[i];
574
+ if (consume) {
575
+ _this.offset = i;
576
+ _this.bitoffset = 0;
577
+ }
578
+ }
579
+ }
580
+ exports.NOT = NOT;
581
+ function LSHIFT(_this, shift_key, start, end, consume) {
582
+ const input = _this.data;
583
+ if ((end || 0) > _this.size) {
584
+ if (_this.strict == false) {
585
+ _this.extendArray((end || 0) - _this.size);
586
+ }
587
+ else {
588
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
589
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
590
+ }
591
+ }
592
+ if (typeof shift_key == "number") {
593
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
594
+ input[i] = input[i] << shift_key;
595
+ if (consume) {
596
+ _this.offset = i;
597
+ _this.bitoffset = 0;
598
+ }
599
+ }
600
+ }
601
+ else {
602
+ if (arraybuffcheck(_this, shift_key)) {
603
+ let number = -1;
604
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
605
+ if (number != shift_key.length - 1) {
606
+ number = number + 1;
607
+ }
608
+ else {
609
+ number = 0;
610
+ }
611
+ input[i] = input[i] << shift_key[number];
612
+ if (consume) {
613
+ _this.offset = i;
614
+ _this.bitoffset = 0;
615
+ }
616
+ }
617
+ }
618
+ else {
619
+ throw new Error("XOR key must be a byte value, string, Uint8Array or Buffer");
620
+ }
621
+ }
622
+ }
623
+ exports.LSHIFT = LSHIFT;
624
+ function RSHIFT(_this, shift_key, start, end, consume) {
625
+ const input = _this.data;
626
+ if ((end || 0) > _this.size) {
627
+ if (_this.strict == false) {
628
+ _this.extendArray((end || 0) - _this.size);
629
+ }
630
+ else {
631
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
632
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
633
+ }
634
+ }
635
+ if (typeof shift_key == "number") {
636
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
637
+ input[i] = input[i] >> shift_key;
638
+ if (consume) {
639
+ _this.offset = i;
640
+ _this.bitoffset = 0;
641
+ }
642
+ }
643
+ }
644
+ else {
645
+ if (arraybuffcheck(_this, shift_key)) {
646
+ let number = -1;
647
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
648
+ if (number != shift_key.length - 1) {
649
+ number = number + 1;
650
+ }
651
+ else {
652
+ number = 0;
653
+ }
654
+ input[i] = input[i] >> shift_key[number];
655
+ if (consume) {
656
+ _this.offset = i;
657
+ _this.bitoffset = 0;
658
+ }
659
+ }
660
+ }
661
+ else {
662
+ throw new Error("XOR key must be a byte value, string, Uint8Array or Buffer");
663
+ }
664
+ }
665
+ }
666
+ exports.RSHIFT = RSHIFT;
667
+ function ADD(_this, add_key, start, end, consume) {
668
+ const input = _this.data;
669
+ if ((end || 0) > _this.size) {
670
+ if (_this.strict == false) {
671
+ _this.extendArray((end || 0) - _this.size);
672
+ }
673
+ else {
674
+ _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
675
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
676
+ }
677
+ }
678
+ if (typeof add_key == "number") {
679
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
680
+ input[i] = input[i] + add_key;
681
+ if (consume) {
682
+ _this.offset = i;
683
+ _this.bitoffset = 0;
684
+ }
685
+ }
686
+ }
687
+ else {
688
+ if (arraybuffcheck(_this, add_key)) {
689
+ let number = -1;
690
+ for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
691
+ if (number != add_key.length - 1) {
692
+ number = number + 1;
693
+ }
694
+ else {
695
+ number = 0;
696
+ }
697
+ input[i] = input[i] + add_key[number];
698
+ if (consume) {
699
+ _this.offset = i;
700
+ _this.bitoffset = 0;
701
+ }
702
+ }
703
+ }
704
+ else {
705
+ throw new Error("XOR key must be a byte value, string, Uint8Array or Buffer");
706
+ }
707
+ }
708
+ }
709
+ exports.ADD = ADD;
710
+ function fString(_this, searchString) {
711
+ // Convert the searchString to Uint8Array
712
+ const searchArray = new TextEncoder().encode(searchString);
713
+ for (let i = _this.offset; i <= _this.size - searchArray.length; i++) {
714
+ let match = true;
715
+ for (let j = 0; j < searchArray.length; j++) {
716
+ if (_this.data[i + j] !== searchArray[j]) {
717
+ match = false;
718
+ break;
719
+ }
720
+ }
721
+ if (match) {
722
+ return i; // Found the string, return the index
723
+ }
724
+ }
725
+ return -1; // String not found
726
+ }
727
+ exports.fString = fString;
728
+ function fNumber(_this, targetNumber, bits, unsigned, endian) {
729
+ check_size(_this, Math.floor(bits / 8), 0);
730
+ for (let z = _this.offset; z <= (_this.size - (bits / 8)); z++) {
731
+ var off_in_bits = 0;
732
+ var value = 0;
733
+ for (var i = 0; i < bits;) {
734
+ var remaining = bits - i;
735
+ var bitOffset = off_in_bits & 7;
736
+ var currentByte = _this.data[z + (off_in_bits >> 3)];
737
+ var read = Math.min(remaining, 8 - bitOffset);
738
+ var mask, readBits;
739
+ if ((endian != undefined ? endian : _this.endian) == "big") {
740
+ mask = ~(0xFF << read);
741
+ readBits = (currentByte >> (8 - read - bitOffset)) & mask;
742
+ value <<= read;
743
+ value |= readBits;
744
+ }
745
+ else {
746
+ mask = ~(0xFF << read);
747
+ readBits = (currentByte >> bitOffset) & mask;
748
+ value |= readBits << i;
749
+ }
750
+ off_in_bits += read;
751
+ i += read;
752
+ }
753
+ if (unsigned == true || bits <= 7) {
754
+ value = value >>> 0;
755
+ }
756
+ else {
757
+ if (bits !== 32 && value & (1 << (bits - 1))) {
758
+ value |= -1 ^ ((1 << bits) - 1);
759
+ }
760
+ }
761
+ if (value === targetNumber) {
762
+ return z - _this.offset; // Found the byte, return the index from current
763
+ }
764
+ }
765
+ return -1; // number not found
766
+ }
767
+ exports.fNumber = fNumber;
768
+ function fHalfFloat(_this, targetNumber, endian) {
769
+ check_size(_this, 2, 0);
770
+ for (let z = _this.offset; z <= (_this.size - 2); z++) {
771
+ var value = 0;
772
+ if ((endian != undefined ? endian : _this.endian) == "little") {
773
+ value = (_this.data[z + 1] << 8) | _this.data[z];
774
+ }
775
+ else {
776
+ value = (_this.data[z] << 8) | _this.data[z + 1];
777
+ }
778
+ const sign = (value & 0x8000) >> 15;
779
+ const exponent = (value & 0x7C00) >> 10;
780
+ const fraction = value & 0x03FF;
781
+ let floatValue;
782
+ if (exponent === 0) {
783
+ if (fraction === 0) {
784
+ floatValue = (sign === 0) ? 0 : -0; // +/-0
785
+ }
786
+ else {
787
+ // Denormalized number
788
+ floatValue = (sign === 0 ? 1 : -1) * Math.pow(2, -14) * (fraction / 0x0400);
789
+ }
790
+ }
791
+ else if (exponent === 0x1F) {
792
+ if (fraction === 0) {
793
+ floatValue = (sign === 0) ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
794
+ }
795
+ else {
796
+ floatValue = Number.NaN;
797
+ }
798
+ }
799
+ else {
800
+ // Normalized number
801
+ floatValue = (sign === 0 ? 1 : -1) * Math.pow(2, exponent - 15) * (1 + fraction / 0x0400);
802
+ }
803
+ if (floatValue === targetNumber) {
804
+ return z; // Found the number, return the index
805
+ }
806
+ }
807
+ return -1; // number not found
808
+ }
809
+ exports.fHalfFloat = fHalfFloat;
810
+ function fFloat(_this, targetNumber, endian) {
811
+ check_size(_this, 4, 0);
812
+ for (let z = _this.offset; z <= (_this.size - 4); z++) {
813
+ var value = 0;
814
+ if ((endian != undefined ? endian : _this.endian) == "little") {
815
+ value = ((_this.data[z + 3] << 24) | (_this.data[z + 2] << 16) | (_this.data[z + 1] << 8) | _this.data[z]);
816
+ }
817
+ else {
818
+ value = (_this.data[z] << 24) | (_this.data[z + 1] << 16) | (_this.data[z + 2] << 8) | _this.data[z + 3];
819
+ }
820
+ const isNegative = (value & 0x80000000) !== 0 ? 1 : 0;
821
+ // Extract the exponent and fraction parts
822
+ const exponent = (value >> 23) & 0xFF;
823
+ const fraction = value & 0x7FFFFF;
824
+ // Calculate the float value
825
+ let floatValue;
826
+ if (exponent === 0) {
827
+ // Denormalized number (exponent is 0)
828
+ floatValue = Math.pow(-1, isNegative) * Math.pow(2, -126) * (fraction / Math.pow(2, 23));
829
+ }
830
+ else if (exponent === 0xFF) {
831
+ // Infinity or NaN (exponent is 255)
832
+ floatValue = fraction === 0 ? (isNegative ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY) : Number.NaN;
833
+ }
834
+ else {
835
+ // Normalized number
836
+ floatValue = Math.pow(-1, isNegative) * Math.pow(2, exponent - 127) * (1 + fraction / Math.pow(2, 23));
837
+ }
838
+ if (floatValue === targetNumber) {
839
+ return z; // Found the number, return the index
840
+ }
841
+ }
842
+ return -1; // number not found
843
+ }
844
+ exports.fFloat = fFloat;
845
+ function fBigInt(_this, targetNumber, unsigned, endian) {
846
+ check_size(_this, 8, 0);
847
+ for (let z = _this.offset; z <= (_this.size - 8); z++) {
848
+ let value = BigInt(0);
849
+ if ((endian == undefined ? _this.endian : endian) == "little") {
850
+ for (let i = 0; i < 8; i++) {
851
+ value = value | BigInt(_this.data[z + i]) << BigInt(8 * i);
852
+ }
853
+ if (unsigned == undefined || unsigned == false) {
854
+ if (value & (BigInt(1) << BigInt(63))) {
855
+ value -= BigInt(1) << BigInt(64);
856
+ }
857
+ }
858
+ }
859
+ else {
860
+ for (let i = 0; i < 8; i++) {
861
+ value = (value << BigInt(8)) | BigInt(_this.data[z + i]);
862
+ }
863
+ if (unsigned == undefined || unsigned == false) {
864
+ if (value & (BigInt(1) << BigInt(63))) {
865
+ value -= BigInt(1) << BigInt(64);
866
+ }
867
+ }
868
+ }
869
+ if (value == BigInt(targetNumber)) {
870
+ return z;
871
+ }
872
+ }
873
+ return -1; // number not found
874
+ }
875
+ exports.fBigInt = fBigInt;
876
+ function fDoubleFloat(_this, targetNumber, endian) {
877
+ check_size(_this, 8, 0);
878
+ for (let z = _this.offset; z <= (_this.size - 8); z++) {
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[z + i]) << BigInt(8 * i);
883
+ }
884
+ }
885
+ else {
886
+ for (let i = 0; i < 8; i++) {
887
+ value = (value << BigInt(8)) | BigInt(_this.data[z + i]);
888
+ }
889
+ }
890
+ const sign = (value & 0x8000000000000000n) >> 63n;
891
+ const exponent = Number((value & 0x7ff0000000000000n) >> 52n) - 1023;
892
+ const fraction = Number(value & 0x000fffffffffffffn) / Math.pow(2, 52);
893
+ var floatValue;
894
+ if (exponent == -1023) {
895
+ if (fraction == 0) {
896
+ floatValue = (sign == 0n) ? 0 : -0; // +/-0
897
+ }
898
+ else {
899
+ // Denormalized number
900
+ floatValue = (sign == 0n ? 1 : -1) * Math.pow(2, -1022) * fraction;
901
+ }
902
+ }
903
+ else if (exponent == 1024) {
904
+ if (fraction == 0) {
905
+ floatValue = (sign == 0n) ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
906
+ }
907
+ else {
908
+ floatValue = Number.NaN;
909
+ }
910
+ }
911
+ else {
912
+ // Normalized number
913
+ floatValue = (sign == 0n ? 1 : -1) * Math.pow(2, exponent) * (1 + fraction);
914
+ }
915
+ if (floatValue == targetNumber) {
916
+ return z;
917
+ }
918
+ }
919
+ return -1; // number not found
920
+ }
921
+ exports.fDoubleFloat = fDoubleFloat;
922
+ function wbit(_this, value, bits, unsigned, endian) {
923
+ if (value == undefined) {
924
+ throw new Error('Must supply value.');
925
+ }
926
+ if (bits == undefined) {
927
+ throw new Error("Enter number of bits to write");
928
+ }
929
+ if (bits == 0) {
930
+ return;
931
+ }
932
+ if (bits <= 0 || bits > 32) {
933
+ throw new Error('Bit length must be between 1 and 32. Got ' + bits);
934
+ }
935
+ if (unsigned == true) {
936
+ if (value < 0 || value > Math.pow(2, bits)) {
937
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
938
+ throw new Error(`Value is out of range for the specified ${bits}bit length.` + " min: " + 0 + " max: " + Math.pow(2, bits) + " value: " + value);
939
+ }
940
+ }
941
+ else {
942
+ const maxValue = Math.pow(2, bits - 1) - 1;
943
+ const minValue = -maxValue - 1;
944
+ if (value < minValue || value > maxValue) {
945
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
946
+ throw new Error(`Value is out of range for the specified ${bits}bit length.` + " min: " + minValue + " max: " + maxValue + " value: " + value);
947
+ }
948
+ }
949
+ if (unsigned == true) {
950
+ const maxValue = Math.pow(2, bits) - 1;
951
+ value = value & maxValue;
952
+ }
953
+ const size_needed = ((((bits - 1) + _this.bitoffset) / 8) + _this.offset);
954
+ if (size_needed > _this.size) {
955
+ //add size
956
+ _this.extendArray(size_needed - _this.size);
957
+ }
958
+ var off_in_bits = (_this.offset * 8) + _this.bitoffset;
959
+ for (var i = 0; i < bits;) {
960
+ var remaining = bits - i;
961
+ var bitOffset = off_in_bits & 7;
962
+ var byteOffset = off_in_bits >> 3;
963
+ var written = Math.min(remaining, 8 - bitOffset);
964
+ var mask, writeBits, destMask;
965
+ if ((endian != undefined ? endian : _this.endian) == "big") {
966
+ mask = ~(~0 << written);
967
+ writeBits = (value >> (bits - i - written)) & mask;
968
+ var destShift = 8 - bitOffset - written;
969
+ destMask = ~(mask << destShift);
970
+ _this.data[byteOffset] = (_this.data[byteOffset] & destMask) | (writeBits << destShift);
971
+ }
972
+ else {
973
+ mask = ~(0xFF << written);
974
+ writeBits = value & mask;
975
+ value >>= written;
976
+ destMask = ~(mask << bitOffset);
977
+ _this.data[byteOffset] = (_this.data[byteOffset] & destMask) | (writeBits << bitOffset);
978
+ }
979
+ off_in_bits += written;
980
+ i += written;
981
+ }
982
+ _this.offset = _this.offset + Math.floor(((bits) + _this.bitoffset) / 8); //end byte
983
+ _this.bitoffset = ((bits) + _this.bitoffset) % 8;
984
+ }
985
+ exports.wbit = wbit;
986
+ function rbit(_this, bits, unsigned, endian) {
987
+ if (bits == undefined || typeof bits != "number") {
988
+ throw new Error("Enter number of bits to read");
989
+ }
990
+ if (bits == 0) {
991
+ return 0;
992
+ }
993
+ if (bits <= 0 || bits > 32) {
994
+ throw new Error('Bit length must be between 1 and 32. Got ' + bits);
995
+ }
996
+ const size_needed = ((((bits - 1) + _this.bitoffset) / 8) + _this.offset);
997
+ if (bits <= 0 || size_needed > _this.size) {
998
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
999
+ throw new Error("Invalid number of bits to read: " + size_needed + " of " + _this.size);
1000
+ }
1001
+ var off_in_bits = (_this.offset * 8) + _this.bitoffset;
1002
+ var value = 0;
1003
+ for (var i = 0; i < bits;) {
1004
+ var remaining = bits - i;
1005
+ var bitOffset = off_in_bits & 7;
1006
+ var currentByte = _this.data[off_in_bits >> 3];
1007
+ var read = Math.min(remaining, 8 - bitOffset);
1008
+ var mask, readBits;
1009
+ if ((endian != undefined ? endian : _this.endian) == "big") {
1010
+ mask = ~(0xFF << read);
1011
+ readBits = (currentByte >> (8 - read - bitOffset)) & mask;
1012
+ value <<= read;
1013
+ value |= readBits;
1014
+ }
1015
+ else {
1016
+ mask = ~(0xFF << read);
1017
+ readBits = (currentByte >> bitOffset) & mask;
1018
+ value |= readBits << i;
1019
+ }
1020
+ off_in_bits += read;
1021
+ i += read;
1022
+ }
1023
+ _this.offset = _this.offset + Math.floor(((bits) + _this.bitoffset) / 8); //end byte
1024
+ _this.bitoffset = ((bits) + _this.bitoffset) % 8;
1025
+ if (unsigned == true || bits <= 7) {
1026
+ return value >>> 0;
1027
+ }
1028
+ if (bits !== 32 && value & (1 << (bits - 1))) {
1029
+ value |= -1 ^ ((1 << bits) - 1);
1030
+ }
1031
+ return value;
1032
+ }
1033
+ exports.rbit = rbit;
1034
+ function wbyte(_this, value, unsigned) {
1035
+ check_size(_this, 1, 0);
1036
+ if (unsigned == true) {
1037
+ if (value < 0 || value > 255) {
1038
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1039
+ throw new Error('Value is out of range for the specified 8bit length.' + " min: " + 0 + " max: " + 255 + " value: " + value);
1040
+ }
1041
+ }
1042
+ else {
1043
+ const maxValue = Math.pow(2, 8 - 1) - 1;
1044
+ const minValue = -maxValue - 1;
1045
+ if (value < minValue || value > maxValue) {
1046
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1047
+ throw new Error('Value is out of range for the specified 8bit length.' + " min: " + minValue + " max: " + maxValue + " value: " + value);
1048
+ }
1049
+ }
1050
+ _this.data[_this.offset] = (unsigned == undefined || unsigned == false) ? value : value & 0xFF;
1051
+ _this.offset += 1;
1052
+ _this.bitoffset = 0;
1053
+ }
1054
+ exports.wbyte = wbyte;
1055
+ function rbyte(_this, unsigned) {
1056
+ check_size(_this, 1);
1057
+ var read = _this.data[_this.offset];
1058
+ _this.offset += 1;
1059
+ _this.bitoffset = 0;
1060
+ if (unsigned == true) {
1061
+ return read & 0xFF;
1062
+ }
1063
+ else {
1064
+ return read > 127 ? read - 256 : read;
1065
+ }
1066
+ }
1067
+ exports.rbyte = rbyte;
1068
+ function wint16(_this, value, unsigned, endian) {
1069
+ check_size(_this, 2, 0);
1070
+ if (unsigned == true) {
1071
+ if (value < 0 || value > 65535) {
1072
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1073
+ throw new Error('Value is out of range for the specified 16bit length.' + " min: " + 0 + " max: " + 65535 + " value: " + value);
1074
+ }
1075
+ }
1076
+ else {
1077
+ const maxValue = Math.pow(2, 16 - 1) - 1;
1078
+ const minValue = -maxValue - 1;
1079
+ if (value < minValue || value > maxValue) {
1080
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1081
+ throw new Error('Value is out of range for the specified 16bit length.' + " min: " + minValue + " max: " + maxValue + " value: " + value);
1082
+ }
1083
+ }
1084
+ if ((endian != undefined ? endian : _this.endian) == "little") {
1085
+ _this.data[_this.offset] = (unsigned == undefined || unsigned == false) ? value : value & 0xff;
1086
+ _this.data[_this.offset + 1] = (unsigned == undefined || unsigned == false) ? (value >> 8) : (value >> 8) & 0xff;
1087
+ }
1088
+ else {
1089
+ _this.data[_this.offset] = (unsigned == undefined || unsigned == false) ? (value >> 8) : (value >> 8) & 0xff;
1090
+ _this.data[_this.offset + 1] = (unsigned == undefined || unsigned == false) ? value : value & 0xff;
1091
+ }
1092
+ _this.offset += 2;
1093
+ _this.bitoffset = 0;
1094
+ }
1095
+ exports.wint16 = wint16;
1096
+ function rint16(_this, unsigned, endian) {
1097
+ check_size(_this, 2);
1098
+ var read;
1099
+ if ((endian != undefined ? endian : _this.endian) == "little") {
1100
+ read = (_this.data[_this.offset + 1] << 8) | _this.data[_this.offset];
1101
+ }
1102
+ else {
1103
+ read = (_this.data[_this.offset] << 8) | _this.data[_this.offset + 1];
1104
+ }
1105
+ _this.offset += 2;
1106
+ _this.bitoffset = 0;
1107
+ if (unsigned == undefined || unsigned == false) {
1108
+ return read & 0x8000 ? -(0x10000 - read) : read;
1109
+ }
1110
+ else {
1111
+ return read & 0xFFFF;
1112
+ }
1113
+ }
1114
+ exports.rint16 = rint16;
1115
+ function rhalffloat(_this, endian) {
1116
+ var uint16Value = _this.readInt16(true, (endian != undefined ? endian : _this.endian));
1117
+ const sign = (uint16Value & 0x8000) >> 15;
1118
+ const exponent = (uint16Value & 0x7C00) >> 10;
1119
+ const fraction = uint16Value & 0x03FF;
1120
+ let floatValue;
1121
+ if (exponent === 0) {
1122
+ if (fraction === 0) {
1123
+ floatValue = (sign === 0) ? 0 : -0; // +/-0
1124
+ }
1125
+ else {
1126
+ // Denormalized number
1127
+ floatValue = (sign === 0 ? 1 : -1) * Math.pow(2, -14) * (fraction / 0x0400);
1128
+ }
1129
+ }
1130
+ else if (exponent === 0x1F) {
1131
+ if (fraction === 0) {
1132
+ floatValue = (sign === 0) ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
1133
+ }
1134
+ else {
1135
+ floatValue = Number.NaN;
1136
+ }
1137
+ }
1138
+ else {
1139
+ // Normalized number
1140
+ floatValue = (sign === 0 ? 1 : -1) * Math.pow(2, exponent - 15) * (1 + fraction / 0x0400);
1141
+ }
1142
+ return floatValue;
1143
+ }
1144
+ exports.rhalffloat = rhalffloat;
1145
+ function whalffloat(_this, value, endian) {
1146
+ check_size(_this, 2, 0);
1147
+ const maxValue = 65504;
1148
+ const minValue = 5.96e-08;
1149
+ if (value < minValue || value > maxValue) {
1150
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1151
+ throw new Error('Value is out of range for the specified half float length.' + " min: " + minValue + " max: " + maxValue + " value: " + value);
1152
+ }
1153
+ const signMask = 0x8000;
1154
+ const exponentMask = 0x7C00;
1155
+ const fractionMask = 0x03FF;
1156
+ // Determine sign, exponent, and fraction bits
1157
+ let signBit = (value & signMask) >> 15;
1158
+ let exponentBits = (value & exponentMask) >> 10;
1159
+ let fractionBits = value & fractionMask;
1160
+ // Special cases for NaN and Infinity
1161
+ if (exponentBits === 0x1F) {
1162
+ // NaN or Infinity, copy exponent and fraction
1163
+ exponentBits = 0xFF;
1164
+ }
1165
+ else if (exponentBits === 0x00) {
1166
+ // Denormalized numbers, exponent is 0, adjust exponent bits
1167
+ exponentBits = 0x00;
1168
+ fractionBits = 0x00; // Clear fraction for denormals
1169
+ }
1170
+ else {
1171
+ // Normalized number, subtract exponent bias
1172
+ exponentBits -= 15;
1173
+ }
1174
+ // Combine sign, exponent, and fraction bits into half float format
1175
+ let halfFloatBits = (signBit << 15) | (exponentBits << 10) | fractionBits;
1176
+ // Write bytes based on endianness
1177
+ if ((endian == undefined ? _this.endian : endian) == "little") {
1178
+ _this.data[_this.offset] = halfFloatBits & 0xFF;
1179
+ _this.data[_this.offset + 1] = (halfFloatBits >> 8) & 0xFF;
1180
+ }
1181
+ else {
1182
+ _this.data[_this.offset] = (halfFloatBits >> 8) & 0xFF;
1183
+ _this.data[_this.offset + 1] = halfFloatBits & 0xFF;
1184
+ }
1185
+ _this.offset += 2;
1186
+ _this.bitoffset = 0;
1187
+ }
1188
+ exports.whalffloat = whalffloat;
1189
+ function wint32(_this, value, unsigned, endian) {
1190
+ check_size(_this, 4, 0);
1191
+ if (unsigned == true) {
1192
+ if (value < 0 || value > 4294967295) {
1193
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1194
+ throw new Error('Value is out of range for the specified 32bit length.' + " min: " + 0 + " max: " + 4294967295 + " value: " + value);
1195
+ }
1196
+ }
1197
+ else {
1198
+ const maxValue = Math.pow(2, 32 - 1) - 1;
1199
+ const minValue = -maxValue - 1;
1200
+ if (value < minValue || value > maxValue) {
1201
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1202
+ throw new Error('Value is out of range for the specified 32bit length.' + " min: " + minValue + " max: " + maxValue + " value: " + value);
1203
+ }
1204
+ }
1205
+ if ((endian == undefined ? _this.endian : endian) == "little") {
1206
+ _this.data[_this.offset] = (unsigned == undefined || unsigned == false) ? value : value & 0xFF;
1207
+ _this.data[_this.offset + 1] = (unsigned == undefined || unsigned == false) ? (value >> 8) : (value >> 8) & 0xFF;
1208
+ _this.data[_this.offset + 2] = (unsigned == undefined || unsigned == false) ? (value >> 16) : (value >> 16) & 0xFF;
1209
+ _this.data[_this.offset + 3] = (unsigned == undefined || unsigned == false) ? (value >> 24) : (value >> 24) & 0xFF;
1210
+ }
1211
+ else {
1212
+ _this.data[_this.offset] = (unsigned == undefined || unsigned == false) ? (value >> 24) : (value >> 24) & 0xFF;
1213
+ _this.data[_this.offset + 1] = (unsigned == undefined || unsigned == false) ? (value >> 16) : (value >> 16) & 0xFF;
1214
+ _this.data[_this.offset + 2] = (unsigned == undefined || unsigned == false) ? (value >> 8) : (value >> 8) & 0xFF;
1215
+ _this.data[_this.offset + 3] = (unsigned == undefined || unsigned == false) ? value : value & 0xFF;
1216
+ }
1217
+ _this.offset += 4;
1218
+ _this.bitoffset = 0;
1219
+ }
1220
+ exports.wint32 = wint32;
1221
+ function rint32(_this, unsigned, endian) {
1222
+ check_size(_this, 4);
1223
+ var read;
1224
+ if ((endian != undefined ? endian : _this.endian) == "little") {
1225
+ read = ((_this.data[_this.offset + 3] << 24) | (_this.data[_this.offset + 2] << 16) | (_this.data[_this.offset + 1] << 8) | _this.data[_this.offset]);
1226
+ }
1227
+ else {
1228
+ read = (_this.data[_this.offset] << 24) | (_this.data[_this.offset + 1] << 16) | (_this.data[_this.offset + 2] << 8) | _this.data[_this.offset + 3];
1229
+ }
1230
+ _this.offset += 4;
1231
+ _this.bitoffset = 0;
1232
+ if (unsigned == undefined || unsigned == false) {
1233
+ return read;
1234
+ }
1235
+ else {
1236
+ return read >>> 0;
1237
+ }
1238
+ }
1239
+ exports.rint32 = rint32;
1240
+ function rfloat(_this, endian) {
1241
+ var uint32Value = _this.readInt32(true, (endian == undefined ? _this.endian : endian));
1242
+ // Check if the value is negative (i.e., the most significant bit is set)
1243
+ const isNegative = (uint32Value & 0x80000000) !== 0 ? 1 : 0;
1244
+ // Extract the exponent and fraction parts
1245
+ const exponent = (uint32Value >> 23) & 0xFF;
1246
+ const fraction = uint32Value & 0x7FFFFF;
1247
+ // Calculate the float value
1248
+ let floatValue;
1249
+ if (exponent === 0) {
1250
+ // Denormalized number (exponent is 0)
1251
+ floatValue = Math.pow(-1, isNegative) * Math.pow(2, -126) * (fraction / Math.pow(2, 23));
1252
+ }
1253
+ else if (exponent === 0xFF) {
1254
+ // Infinity or NaN (exponent is 255)
1255
+ floatValue = fraction === 0 ? (isNegative ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY) : Number.NaN;
1256
+ }
1257
+ else {
1258
+ // Normalized number
1259
+ floatValue = Math.pow(-1, isNegative) * Math.pow(2, exponent - 127) * (1 + fraction / Math.pow(2, 23));
1260
+ }
1261
+ return floatValue;
1262
+ }
1263
+ exports.rfloat = rfloat;
1264
+ function wfloat(_this, value, endian) {
1265
+ check_size(_this, 4, 0);
1266
+ const MIN_POSITIVE_FLOAT32 = Number.MIN_VALUE;
1267
+ const MAX_POSITIVE_FLOAT32 = 3.4028235e+38;
1268
+ const MIN_NEGATIVE_FLOAT32 = -3.4028235e+38;
1269
+ const MAX_NEGATIVE_FLOAT32 = -Number.MIN_VALUE;
1270
+ if (!((value === 0) ||
1271
+ (value >= MIN_POSITIVE_FLOAT32 && value <= MAX_POSITIVE_FLOAT32) ||
1272
+ (value >= MIN_NEGATIVE_FLOAT32 && value <= MAX_NEGATIVE_FLOAT32))) {
1273
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1274
+ throw new Error('Value is out of range for the specified float length.' + " min: " + MIN_NEGATIVE_FLOAT32 + " max: " + MAX_POSITIVE_FLOAT32 + " value: " + value);
1275
+ }
1276
+ const dataView = new DataView(new Uint8Array(4).buffer);
1277
+ dataView.setFloat32(0, value, true);
1278
+ let intValue = dataView.getInt32(0, true);
1279
+ let shift = 0;
1280
+ for (let i = 0; i < 4; i++) {
1281
+ if ((endian == undefined ? _this.endian : endian) == "little") {
1282
+ _this.data[_this.offset + i] = (intValue >> shift) & 0xFF;
1283
+ }
1284
+ else {
1285
+ _this.data[_this.offset + (3 - i)] = (intValue >> shift) & 0xFF;
1286
+ }
1287
+ shift += 8;
1288
+ }
1289
+ _this.offset += 4;
1290
+ _this.bitoffset = 0;
1291
+ }
1292
+ exports.wfloat = wfloat;
1293
+ function rint64(_this, unsigned, endian) {
1294
+ check_size(_this, 8);
1295
+ // Convert the byte array to a BigInt
1296
+ let value = BigInt(0);
1297
+ if ((endian == undefined ? _this.endian : endian) == "little") {
1298
+ for (let i = 0; i < 8; i++) {
1299
+ value = value | BigInt(_this.data[_this.offset]) << BigInt(8 * i);
1300
+ _this.offset += 1;
1301
+ }
1302
+ if (unsigned == undefined || unsigned == false) {
1303
+ if (value & (BigInt(1) << BigInt(63))) {
1304
+ value -= BigInt(1) << BigInt(64);
1305
+ }
1306
+ }
1307
+ }
1308
+ else {
1309
+ for (let i = 0; i < 8; i++) {
1310
+ value = (value << BigInt(8)) | BigInt(_this.data[_this.offset]);
1311
+ _this.offset += 1;
1312
+ }
1313
+ if (unsigned == undefined || unsigned == false) {
1314
+ if (value & (BigInt(1) << BigInt(63))) {
1315
+ value -= BigInt(1) << BigInt(64);
1316
+ }
1317
+ }
1318
+ }
1319
+ _this.bitoffset = 0;
1320
+ return value;
1321
+ }
1322
+ exports.rint64 = rint64;
1323
+ function wint64(_this, value, unsigned, endian) {
1324
+ check_size(_this, 8, 0);
1325
+ if (unsigned == true) {
1326
+ if (value < 0 || value > Math.pow(2, 64) - 1) {
1327
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1328
+ throw new Error('Value is out of range for the specified 64bit length.' + " min: " + 0 + " max: " + (Math.pow(2, 64) - 1) + " value: " + value);
1329
+ }
1330
+ }
1331
+ else {
1332
+ const maxValue = Math.pow(2, 63) - 1;
1333
+ const minValue = -Math.pow(2, 63);
1334
+ if (value < minValue || value > maxValue) {
1335
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1336
+ throw new Error('Value is out of range for the specified 64bit length.' + " min: " + minValue + " max: " + maxValue + " value: " + value);
1337
+ }
1338
+ }
1339
+ // Convert the BigInt to a 64-bit signed integer
1340
+ const bigIntArray = new BigInt64Array(1);
1341
+ bigIntArray[0] = BigInt(value);
1342
+ // Use two 32-bit views to write the Int64
1343
+ const int32Array = new Int32Array(bigIntArray.buffer);
1344
+ for (let i = 0; i < 2; i++) {
1345
+ if ((endian == undefined ? _this.endian : endian) == "little") {
1346
+ if (unsigned == undefined || unsigned == false) {
1347
+ _this.data[_this.offset + i * 4 + 0] = int32Array[i];
1348
+ _this.data[_this.offset + i * 4 + 1] = (int32Array[i] >> 8);
1349
+ _this.data[_this.offset + i * 4 + 2] = (int32Array[i] >> 16);
1350
+ _this.data[_this.offset + i * 4 + 3] = (int32Array[i] >> 24);
1351
+ }
1352
+ else {
1353
+ _this.data[_this.offset + i * 4 + 0] = int32Array[i] & 0xFF;
1354
+ _this.data[_this.offset + i * 4 + 1] = (int32Array[i] >> 8) & 0xFF;
1355
+ _this.data[_this.offset + i * 4 + 2] = (int32Array[i] >> 16) & 0xFF;
1356
+ _this.data[_this.offset + i * 4 + 3] = (int32Array[i] >> 24) & 0xFF;
1357
+ }
1358
+ }
1359
+ else {
1360
+ if (unsigned == undefined || unsigned == false) {
1361
+ _this.data[_this.offset + (1 - i) * 4 + 0] = int32Array[i];
1362
+ _this.data[_this.offset + (1 - i) * 4 + 1] = (int32Array[i] >> 8);
1363
+ _this.data[_this.offset + (1 - i) * 4 + 2] = (int32Array[i] >> 16);
1364
+ _this.data[_this.offset + (1 - i) * 4 + 3] = (int32Array[i] >> 24);
1365
+ }
1366
+ else {
1367
+ _this.data[_this.offset + (1 - i) * 4 + 0] = int32Array[i] & 0xFF;
1368
+ _this.data[_this.offset + (1 - i) * 4 + 1] = (int32Array[i] >> 8) & 0xFF;
1369
+ _this.data[_this.offset + (1 - i) * 4 + 2] = (int32Array[i] >> 16) & 0xFF;
1370
+ _this.data[_this.offset + (1 - i) * 4 + 3] = (int32Array[i] >> 24) & 0xFF;
1371
+ }
1372
+ }
1373
+ }
1374
+ _this.offset += 8;
1375
+ _this.bitoffset = 0;
1376
+ }
1377
+ exports.wint64 = wint64;
1378
+ function wdfloat(_this, value, endian) {
1379
+ check_size(_this, 8, 0);
1380
+ const MIN_POSITIVE_FLOAT64 = 2.2250738585072014e-308;
1381
+ const MAX_POSITIVE_FLOAT64 = Number.MAX_VALUE;
1382
+ const MIN_NEGATIVE_FLOAT64 = -Number.MAX_VALUE;
1383
+ const MAX_NEGATIVE_FLOAT64 = -2.2250738585072014e-308;
1384
+ if (!((value === 0) ||
1385
+ (value >= MIN_POSITIVE_FLOAT64 && value <= MAX_POSITIVE_FLOAT64) ||
1386
+ (value >= MIN_NEGATIVE_FLOAT64 && value <= MAX_NEGATIVE_FLOAT64))) {
1387
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1388
+ throw new Error('Value is out of range for the specified 64bit length.' + " min: " + MIN_NEGATIVE_FLOAT64 + " max: " + MAX_POSITIVE_FLOAT64 + " value: " + value);
1389
+ }
1390
+ const intArray = new Int32Array(2);
1391
+ const floatArray = new Float64Array(intArray.buffer);
1392
+ floatArray[0] = value;
1393
+ const bytes = new Uint8Array(intArray.buffer);
1394
+ for (let i = 0; i < 8; i++) {
1395
+ if ((endian == undefined ? _this.endian : endian) == "little") {
1396
+ _this.data[_this.offset + i] = bytes[i];
1397
+ }
1398
+ else {
1399
+ _this.data[_this.offset + (7 - i)] = bytes[i];
1400
+ }
1401
+ }
1402
+ _this.offset += 8;
1403
+ _this.bitoffset = 0;
1404
+ }
1405
+ exports.wdfloat = wdfloat;
1406
+ function rdfloat(_this, endian) {
1407
+ var uint64Value = _this.readInt64(true, (endian == undefined ? _this.endian : endian));
1408
+ const sign = (uint64Value & 0x8000000000000000n) >> 63n;
1409
+ const exponent = Number((uint64Value & 0x7ff0000000000000n) >> 52n) - 1023;
1410
+ const fraction = Number(uint64Value & 0x000fffffffffffffn) / Math.pow(2, 52);
1411
+ var floatValue;
1412
+ if (exponent == -1023) {
1413
+ if (fraction == 0) {
1414
+ floatValue = (sign == 0n) ? 0 : -0; // +/-0
1415
+ }
1416
+ else {
1417
+ // Denormalized number
1418
+ floatValue = (sign == 0n ? 1 : -1) * Math.pow(2, -1022) * fraction;
1419
+ }
1420
+ }
1421
+ else if (exponent == 1024) {
1422
+ if (fraction == 0) {
1423
+ floatValue = (sign == 0n) ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
1424
+ }
1425
+ else {
1426
+ floatValue = Number.NaN;
1427
+ }
1428
+ }
1429
+ else {
1430
+ // Normalized number
1431
+ floatValue = (sign == 0n ? 1 : -1) * Math.pow(2, exponent) * (1 + fraction);
1432
+ }
1433
+ return floatValue;
1434
+ }
1435
+ exports.rdfloat = rdfloat;
1436
+ function rstring(_this, options) {
1437
+ var length = options && options.length;
1438
+ var stringType = options && options.stringType || 'utf-8';
1439
+ var terminateValue = options && options.terminateValue;
1440
+ var lengthReadSize = options && options.lengthReadSize || 1;
1441
+ var stripNull = options && options.stripNull || true;
1442
+ var encoding = options && options.encoding || 'utf-8';
1443
+ var endian = options && options.endian || _this.endian;
1444
+ var terminate = terminateValue;
1445
+ if (length != undefined) {
1446
+ check_size(_this, length);
1447
+ }
1448
+ if (typeof terminateValue == "number") {
1449
+ terminate = terminateValue & 0xFF;
1450
+ }
1451
+ else {
1452
+ if (terminateValue != undefined) {
1453
+ throw new Error("terminateValue must be a number");
1454
+ }
1455
+ }
1456
+ if (stringType == 'utf-8' || stringType == 'utf-16') {
1457
+ if (encoding == undefined) {
1458
+ if (stringType == 'utf-8') {
1459
+ encoding = 'utf-8';
1460
+ }
1461
+ if (stringType == 'utf-16') {
1462
+ encoding = 'utf-16';
1463
+ }
1464
+ }
1465
+ // Read the string as UTF-8 encoded untill 0 or terminateValue
1466
+ const encodedBytes = [];
1467
+ if (length == undefined && terminateValue == undefined) {
1468
+ terminate = 0;
1469
+ }
1470
+ var read_length = 0;
1471
+ if (length != undefined) {
1472
+ read_length = length;
1473
+ }
1474
+ else {
1475
+ read_length = _this.data.length - _this.offset;
1476
+ }
1477
+ for (let i = 0; i < read_length; i++) {
1478
+ if (stringType === 'utf-8') {
1479
+ var read = _this.readUByte();
1480
+ if (read == terminate) {
1481
+ break;
1482
+ }
1483
+ else {
1484
+ if (!(stripNull == true && read == 0)) {
1485
+ encodedBytes.push(read);
1486
+ }
1487
+ }
1488
+ }
1489
+ else {
1490
+ var read = _this.readInt16(true, endian);
1491
+ var read1 = read & 0xFF;
1492
+ var read2 = (read >> 8) & 0xFF;
1493
+ if (read == terminate) {
1494
+ break;
1495
+ }
1496
+ else {
1497
+ if (!(stripNull == true && read == 0)) {
1498
+ encodedBytes.push(read1);
1499
+ encodedBytes.push(read2);
1500
+ }
1501
+ }
1502
+ }
1503
+ }
1504
+ return new TextDecoder(encoding).decode(new Uint8Array(encodedBytes));
1505
+ }
1506
+ else if (stringType == 'pascal' || stringType == 'wide-pascal') {
1507
+ if (encoding == undefined) {
1508
+ if (stringType == 'pascal') {
1509
+ encoding = 'utf-8';
1510
+ }
1511
+ if (stringType == 'wide-pascal') {
1512
+ encoding = 'utf-16';
1513
+ }
1514
+ }
1515
+ var maxBytes;
1516
+ if (lengthReadSize == 1) {
1517
+ maxBytes = _this.readUByte();
1518
+ }
1519
+ else if (lengthReadSize == 2) {
1520
+ maxBytes = _this.readInt16(true, endian);
1521
+ }
1522
+ else if (lengthReadSize == 4) {
1523
+ maxBytes = _this.readInt32(true, endian);
1524
+ }
1525
+ else {
1526
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1527
+ throw new Error("Invalid length read size: " + lengthReadSize);
1528
+ }
1529
+ // Read the string as Pascal or Delphi encoded
1530
+ const encodedBytes = [];
1531
+ for (let i = 0; i < maxBytes; i++) {
1532
+ if (stringType == 'wide-pascal') {
1533
+ const read = _this.readInt16(true, endian);
1534
+ if (!(stripNull == true && read == 0)) {
1535
+ encodedBytes.push(read);
1536
+ }
1537
+ }
1538
+ else {
1539
+ const read = _this.readUByte();
1540
+ if (!(stripNull == true && read == 0)) {
1541
+ encodedBytes.push(read);
1542
+ }
1543
+ }
1544
+ }
1545
+ var str_return;
1546
+ if (stringType == 'wide-pascal') {
1547
+ str_return = new TextDecoder(encoding).decode(new Uint16Array(encodedBytes));
1548
+ }
1549
+ else {
1550
+ str_return = new TextDecoder(encoding).decode(new Uint8Array(encodedBytes));
1551
+ }
1552
+ return str_return;
1553
+ }
1554
+ else {
1555
+ throw new Error('Unsupported string type: ' + stringType);
1556
+ }
1557
+ }
1558
+ exports.rstring = rstring;
1559
+ function wstring(_this, string, options) {
1560
+ var length = options && options.length;
1561
+ var stringType = options && options.stringType || 'utf-8';
1562
+ var terminateValue = options && options.terminateValue;
1563
+ var lengthWriteSize = options && options.lengthWriteSize || 1;
1564
+ var encoding = options && options.encoding || 'utf-8';
1565
+ var endian = options && options.endian || _this.endian;
1566
+ if (stringType === 'utf-8' || stringType === 'utf-16') {
1567
+ // Encode the string in the specified encoding
1568
+ if (encoding == undefined) {
1569
+ if (stringType == 'utf-8') {
1570
+ encoding = 'utf-8';
1571
+ }
1572
+ if (stringType == 'utf-16') {
1573
+ encoding = 'utf-16';
1574
+ }
1575
+ }
1576
+ const encoder = new TextEncoder();
1577
+ const encodedString = encoder.encode(string);
1578
+ if (length == undefined && terminateValue == undefined) {
1579
+ terminateValue = 0;
1580
+ }
1581
+ var totalLength = (length || encodedString.length) + (terminateValue != undefined ? 1 : 0);
1582
+ if (stringType == 'utf-16') {
1583
+ totalLength = (length || (encodedString.length * 2)) + (terminateValue != undefined ? 2 : 0);
1584
+ }
1585
+ check_size(_this, totalLength, 0);
1586
+ // Write the string bytes to the Uint8Array
1587
+ for (let i = 0; i < encodedString.length; i++) {
1588
+ if (stringType === 'utf-16') {
1589
+ const charCode = encodedString[i];
1590
+ if (endian == "little") {
1591
+ _this.data[_this.offset + i * 2] = charCode & 0xFF;
1592
+ _this.data[_this.offset + i * 2 + 1] = (charCode >> 8) & 0xFF;
1593
+ }
1594
+ else {
1595
+ _this.data[_this.offset + i * 2 + 1] = charCode & 0xFF;
1596
+ _this.data[_this.offset + i * 2] = (charCode >> 8) & 0xFF;
1597
+ }
1598
+ }
1599
+ else {
1600
+ _this.data[_this.offset + i] = encodedString[i];
1601
+ }
1602
+ }
1603
+ if (terminateValue != undefined) {
1604
+ if (stringType === 'utf-16') {
1605
+ _this.data[_this.offset + totalLength - 1] = terminateValue & 0xFF;
1606
+ _this.data[_this.offset + totalLength] = (terminateValue >> 8) & 0xFF;
1607
+ }
1608
+ else {
1609
+ _this.data[_this.offset + totalLength] = terminateValue;
1610
+ }
1611
+ }
1612
+ _this.offset += totalLength;
1613
+ _this.bitoffset = 0;
1614
+ }
1615
+ else if (stringType == 'pascal' || stringType == 'wide-pascal') {
1616
+ if (encoding == undefined) {
1617
+ if (stringType == 'pascal') {
1618
+ encoding = 'utf-8';
1619
+ }
1620
+ if (stringType == 'wide-pascal') {
1621
+ encoding = 'utf-16';
1622
+ }
1623
+ }
1624
+ const encoder = new TextEncoder();
1625
+ // Calculate the length of the string based on the specified max length
1626
+ var maxLength;
1627
+ // Encode the string in the specified encoding
1628
+ if (lengthWriteSize == 1) {
1629
+ maxLength = 255;
1630
+ }
1631
+ else if (lengthWriteSize == 2) {
1632
+ maxLength = 65535;
1633
+ }
1634
+ else if (lengthWriteSize == 4) {
1635
+ maxLength = 4294967295;
1636
+ }
1637
+ else {
1638
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1639
+ throw new Error("Invalid length write size: " + lengthWriteSize);
1640
+ }
1641
+ if (string.length > maxLength || (length || 0) > maxLength) {
1642
+ _this.errorDump ? "[Error], hexdump:\n" + _this.hexdump() : "";
1643
+ throw new Error("String outsize of max write length: " + maxLength);
1644
+ }
1645
+ var maxBytes = Math.min(string.length, maxLength);
1646
+ const encodedString = encoder.encode(string.substring(0, maxBytes));
1647
+ var totalLength = (length || encodedString.length);
1648
+ if (stringType == 'wide-pascal') {
1649
+ totalLength = (length || (encodedString.length * 2));
1650
+ }
1651
+ if (lengthWriteSize == 1) {
1652
+ _this.writeUByte(maxBytes);
1653
+ }
1654
+ else if (lengthWriteSize == 2) {
1655
+ _this.writeUInt16(maxBytes, endian);
1656
+ }
1657
+ else if (lengthWriteSize == 4) {
1658
+ _this.writeUInt32(maxBytes, endian);
1659
+ }
1660
+ check_size(_this, totalLength, 0);
1661
+ // Write the string bytes to the Uint8Array
1662
+ for (let i = 0; i < encodedString.length; i++) {
1663
+ if (stringType == 'wide-pascal') {
1664
+ const charCode = encodedString[i];
1665
+ if (endian == "little") {
1666
+ _this.data[_this.offset + i * 2] = charCode & 0xFF;
1667
+ _this.data[_this.offset + i * 2 + 1] = (charCode >> 8) & 0xFF;
1668
+ }
1669
+ else {
1670
+ _this.data[_this.offset + i * 2 + 1] = charCode & 0xFF;
1671
+ _this.data[_this.offset + i * 2] = (charCode >> 8) & 0xFF;
1672
+ }
1673
+ }
1674
+ else {
1675
+ _this.data[_this.offset + i] = encodedString[i];
1676
+ }
1677
+ }
1678
+ _this.offset += totalLength;
1679
+ _this.bitoffset = 0;
1680
+ }
1681
+ else {
1682
+ throw new Error('Unsupported string type: ' + stringType);
1683
+ }
1684
+ }
1685
+ exports.wstring = wstring;
1686
+ class ReaderBase {
1687
+ constructor() {
1688
+ /**
1689
+ * Endianness of default read.
1690
+ * @type {'little'|'big'}
1691
+ */
1692
+ this.endian = "little";
1693
+ /**
1694
+ * Current read byte location.
1695
+ */
1696
+ this.offset = 0;
1697
+ /**
1698
+ * Current read byte's bit location.
1699
+ */
1700
+ this.bitoffset = 0;
1701
+ /**
1702
+ * Size in bytes of the current buffer.
1703
+ */
1704
+ this.size = 0;
1705
+ /**
1706
+ * Size in bits of the current buffer.
1707
+ */
1708
+ this.sizeB = 0;
1709
+ /**
1710
+ * Allows the buffer to extend reading or writing outside of current size
1711
+ */
1712
+ this.strict = false;
1713
+ /**
1714
+ * Console log a hexdump on error.
1715
+ */
1716
+ this.errorDump = true;
1717
+ /**
1718
+ * Current buffer data.
1719
+ * @type {Buffer|Uint8Array}
1720
+ */
1721
+ this.data = [];
1722
+ }
1723
+ isBufferOrUint8Array(obj) {
1724
+ return arraybuffcheck(this, obj);
1725
+ }
1726
+ extendArray(to_padd) {
1727
+ return extendarray(this, to_padd);
1728
+ }
1729
+ /**
1730
+ *
1731
+ * Change endian, defaults to little.
1732
+ *
1733
+ * Can be changed at any time, doesn't loose position.
1734
+ *
1735
+ * @param {string} endian - endianness ``big`` or ``little``
1736
+ */
1737
+ endianness(endian) {
1738
+ if (endian == undefined || typeof endian != "string") {
1739
+ throw new Error("Endian must be big or little");
1740
+ }
1741
+ if (endian != undefined && !(endian == "big" || endian == "little")) {
1742
+ throw new Error("Endian must be big or little");
1743
+ }
1744
+ this.endian = endian;
1745
+ }
1746
+ /**
1747
+ * Sets endian to big.
1748
+ */
1749
+ bigEndian() {
1750
+ this.endianness("big");
1751
+ }
1752
+ /**
1753
+ * Sets endian to big.
1754
+ */
1755
+ big() {
1756
+ this.endianness("big");
1757
+ }
1758
+ /**
1759
+ * Sets endian to big.
1760
+ */
1761
+ be() {
1762
+ this.endianness("big");
1763
+ }
1764
+ /**
1765
+ * Sets endian to little.
1766
+ */
1767
+ littleEndian() {
1768
+ this.endianness("little");
1769
+ }
1770
+ /**
1771
+ * Sets endian to little.
1772
+ */
1773
+ little() {
1774
+ this.endianness("little");
1775
+ }
1776
+ /**
1777
+ * Sets endian to little.
1778
+ */
1779
+ le() {
1780
+ this.endianness("little");
1781
+ }
1782
+ /**
1783
+ * Size in bytes of the current buffer.
1784
+ *
1785
+ * @returns {number} size
1786
+ */
1787
+ get length() {
1788
+ return this.size;
1789
+ }
1790
+ /**
1791
+ * Size in bytes of the current buffer.
1792
+ *
1793
+ * @returns {number} size
1794
+ */
1795
+ get len() {
1796
+ return this.size;
1797
+ }
1798
+ /**
1799
+ * Size in bytes of the current buffer.
1800
+ *
1801
+ * @returns {number} size
1802
+ */
1803
+ get FileSize() {
1804
+ return this.size;
1805
+ }
1806
+ /**
1807
+ * Size in bits of the current buffer.
1808
+ *
1809
+ * @returns {number} size
1810
+ */
1811
+ get lengthB() {
1812
+ return this.sizeB;
1813
+ }
1814
+ /**
1815
+ * Size in bits of the current buffer.
1816
+ *
1817
+ * @returns {number} size
1818
+ */
1819
+ get FileSizeB() {
1820
+ return this.sizeB;
1821
+ }
1822
+ /**
1823
+ * Size in bits of the current buffer.
1824
+ *
1825
+ * @returns {number} size
1826
+ */
1827
+ get lenb() {
1828
+ return this.sizeB;
1829
+ }
1830
+ //
1831
+ //get position
1832
+ //
1833
+ /**
1834
+ * Get the current byte position.
1835
+ *
1836
+ * @return {number} current byte position
1837
+ */
1838
+ get tell() {
1839
+ return this.offset;
1840
+ }
1841
+ /**
1842
+ * Get the current byte position.
1843
+ *
1844
+ * @return {number} current byte position
1845
+ */
1846
+ get FTell() {
1847
+ return this.offset;
1848
+ }
1849
+ /**
1850
+ * Get the current byte position.
1851
+ *
1852
+ * @return {number} current byte position
1853
+ */
1854
+ get getOffset() {
1855
+ return this.offset;
1856
+ }
1857
+ /**
1858
+ * Get the current byte position;
1859
+ *
1860
+ * @return {number} current byte position
1861
+ */
1862
+ get saveOffset() {
1863
+ return this.offset;
1864
+ }
1865
+ /**
1866
+ * Get the current byte position;
1867
+ *
1868
+ * @return {number} current byte position
1869
+ */
1870
+ get off() {
1871
+ return this.offset;
1872
+ }
1873
+ /**
1874
+ * Get the current bit position (0-7).
1875
+ *
1876
+ * @return {number} current bit position
1877
+ */
1878
+ get getOffsetBit() {
1879
+ return this.bitoffset;
1880
+ }
1881
+ /**
1882
+ * Get the current bit position (0-7).
1883
+ *
1884
+ * @return {number} current bit position
1885
+ */
1886
+ get tellB() {
1887
+ return this.bitoffset;
1888
+ }
1889
+ /**
1890
+ * Get the current bit position (0-7).
1891
+ *
1892
+ * @return {number} current bit position
1893
+ */
1894
+ get FTellB() {
1895
+ return this.bitoffset;
1896
+ }
1897
+ /**
1898
+ * Get the current bit position (0-7).
1899
+ *
1900
+ * @return {number} current bit position
1901
+ */
1902
+ get offb() {
1903
+ return this.bitoffset;
1904
+ }
1905
+ /**
1906
+ * Get the current absolute bit position (from start of data).
1907
+ *
1908
+ * @return {number} current absolute bit position
1909
+ */
1910
+ get getOffsetAbsBit() {
1911
+ return (this.offset * 8) + this.bitoffset;
1912
+ }
1913
+ /**
1914
+ * Get the current absolute bit position (from start of data).
1915
+ *
1916
+ * @return {number} current bit position
1917
+ */
1918
+ get saveOffsetAbsBit() {
1919
+ return (this.offset * 8) + this.bitoffset;
1920
+ }
1921
+ /**
1922
+ * Get the current absolute bit position (from start of data).
1923
+ *
1924
+ * @return {number} current absolute bit position
1925
+ */
1926
+ get tellAbsB() {
1927
+ return (this.offset * 8) + this.bitoffset;
1928
+ }
1929
+ /**
1930
+ * Get the current absolute bit position (from start of data).
1931
+ *
1932
+ * @return {number} current absolute bit position
1933
+ */
1934
+ get saveOffsetBit() {
1935
+ return (this.offset * 8) + this.bitoffset;
1936
+ }
1937
+ /**
1938
+ * Get the current absolute bit position (from start of data).
1939
+ *
1940
+ * @return {number} current absolute bit position
1941
+ */
1942
+ get offab() {
1943
+ return (this.offset * 8) + this.bitoffset;
1944
+ }
1945
+ /**
1946
+ * Size in bytes of current read position to the end
1947
+ *
1948
+ * @returns {number} size
1949
+ */
1950
+ get remain() {
1951
+ return this.size - this.offset;
1952
+ }
1953
+ /**
1954
+ * Size in bytes of current read position to the end
1955
+ *
1956
+ * @returns {number} size
1957
+ */
1958
+ get FEoF() {
1959
+ return this.size - this.offset;
1960
+ }
1961
+ /**
1962
+ * Size in bits of current read position to the end
1963
+ *
1964
+ * @returns {number} size
1965
+ */
1966
+ get remainB() {
1967
+ return (this.size * 8) - this.saveOffsetAbsBit;
1968
+ }
1969
+ /**
1970
+ * Size in bits of current read position to the end
1971
+ *
1972
+ * @returns {number} size
1973
+ */
1974
+ get FEoFB() {
1975
+ return (this.size * 8) - this.saveOffsetAbsBit;
1976
+ }
1977
+ /**
1978
+ * Row line of the file (16 bytes per row).
1979
+ *
1980
+ * @returns {number} size
1981
+ */
1982
+ get getLine() {
1983
+ return Math.abs(Math.floor((this.offset - 1) / 16));
1984
+ }
1985
+ /**
1986
+ * Row line of the file (16 bytes per row).
1987
+ *
1988
+ * @returns {number} size
1989
+ */
1990
+ get row() {
1991
+ return Math.abs(Math.floor((this.offset - 1) / 16));
1992
+ }
1993
+ //
1994
+ //finishing
1995
+ //
1996
+ /**
1997
+ * Returns current data.
1998
+ *
1999
+ * @returns {Buffer|Uint8Array} ``Buffer`` or ``Uint8Array``
2000
+ */
2001
+ get get() {
2002
+ return this.data;
2003
+ }
2004
+ /**
2005
+ * Returns current data.
2006
+ *
2007
+ * @returns {Buffer|Uint8Array} ``Buffer`` or ``Uint8Array``
2008
+ */
2009
+ get return() {
2010
+ return this.data;
2011
+ }
2012
+ /**
2013
+ * Console logs data as hex dump.
2014
+ *
2015
+ * @param {object} options
2016
+ * ```javascript
2017
+ * {
2018
+ * length: 192, // number of bytes to log, default 192 or end of data
2019
+ * startByte: 0, // byte to start dump (default current byte position)
2020
+ * supressUnicode: false // Supress unicode character preview for even columns
2021
+ * }
2022
+ * ```
2023
+ */
2024
+ hexdump(options) {
2025
+ return hexDump(this, options);
2026
+ }
2027
+ /**
2028
+ * Turn hexdump on error off (default on).
2029
+ */
2030
+ errorDumpOff() {
2031
+ this.errorDump = false;
2032
+ }
2033
+ /**
2034
+ * Turn hexdump on error on (default on).
2035
+ */
2036
+ errorDumpOn() {
2037
+ this.errorDump = true;
2038
+ }
2039
+ //
2040
+ //strict mode change
2041
+ //
2042
+ /**
2043
+ * Disallows extending data if position is outside of max size.
2044
+ */
2045
+ restrict() {
2046
+ this.strict = true;
2047
+ }
2048
+ /**
2049
+ * Allows extending data if position is outside of max size.
2050
+ */
2051
+ unrestrict() {
2052
+ this.strict = false;
2053
+ }
2054
+ /**
2055
+ * removes data.
2056
+ */
2057
+ end() {
2058
+ this.data = undefined;
2059
+ }
2060
+ /**
2061
+ * removes data.
2062
+ */
2063
+ close() {
2064
+ this.data = undefined;
2065
+ }
2066
+ /**
2067
+ * removes data.
2068
+ */
2069
+ done() {
2070
+ this.data = undefined;
2071
+ }
2072
+ /**
2073
+ * removes data.
2074
+ */
2075
+ finished() {
2076
+ this.data = undefined;
2077
+ }
2078
+ //
2079
+ //find
2080
+ //
2081
+ /**
2082
+ * Searches for byte position of string from current read position.
2083
+ *
2084
+ * Returns -1 if not found.
2085
+ *
2086
+ * Does not change current read position.
2087
+ *
2088
+ * @param {string} string - String to search for.
2089
+ */
2090
+ findString(string) {
2091
+ return fString(this, string);
2092
+ }
2093
+ /**
2094
+ * Searches for byte value (can be signed or unsigned) position from current read position.
2095
+ *
2096
+ * Returns -1 if not found.
2097
+ *
2098
+ * Does not change current read position.
2099
+ *
2100
+ * @param {number} value - Number to search for.
2101
+ * @param {boolean} unsigned - If the number is unsigned (default true)
2102
+ * @param {string} endian - endianness of value (default set endian).
2103
+ */
2104
+ findByte(value, unsigned, endian) {
2105
+ return fNumber(this, value, 8, unsigned == undefined ? true : unsigned, endian);
2106
+ }
2107
+ /**
2108
+ * Searches for short value (can be signed or unsigned) position from current read position.
2109
+ *
2110
+ * Returns -1 if not found.
2111
+ *
2112
+ * Does not change current read position.
2113
+ *
2114
+ * @param {number} value - Number to search for.
2115
+ * @param {boolean} unsigned - If the number is unsigned (default true)
2116
+ * @param {string} endian - endianness of value (default set endian).
2117
+ */
2118
+ findShort(value, unsigned, endian) {
2119
+ return fNumber(this, value, 16, unsigned == undefined ? true : unsigned, endian);
2120
+ }
2121
+ /**
2122
+ * Searches for integer value (can be signed or unsigned) position from current read position.
2123
+ *
2124
+ * Returns -1 if not found.
2125
+ *
2126
+ * Does not change current read position.
2127
+ *
2128
+ * @param {number} value - Number to search for.
2129
+ * @param {boolean} unsigned - If the number is unsigned (default true)
2130
+ * @param {string} endian - endianness of value (default set endian).
2131
+ */
2132
+ findInt(value, unsigned, endian) {
2133
+ return fNumber(this, value, 32, unsigned == undefined ? true : unsigned, endian);
2134
+ }
2135
+ /**
2136
+ * Searches for 64 bit value (can be signed or unsigned) position from current read position.
2137
+ *
2138
+ * Returns -1 if not found.
2139
+ *
2140
+ * Does not change current read position.
2141
+ *
2142
+ * @param {number} value - Number to search for.
2143
+ * @param {boolean} unsigned - If the number is unsigned (default true)
2144
+ * @param {string} endian - endianness of value (default set endian).
2145
+ */
2146
+ findInt64(value, unsigned, endian) {
2147
+ return fBigInt(this, value, unsigned == undefined ? true : unsigned, endian);
2148
+ }
2149
+ /**
2150
+ * Searches for half float value position from current read position.
2151
+ *
2152
+ * Returns -1 if not found.
2153
+ *
2154
+ * Does not change current read position.
2155
+ *
2156
+ * @param {number} value - Number to search for.
2157
+ * @param {string} endian - endianness of value (default set endian).
2158
+ */
2159
+ findHalfFloat(value, endian) {
2160
+ return fHalfFloat(this, value, endian);
2161
+ }
2162
+ /**
2163
+ * Searches for float value position from current read position.
2164
+ *
2165
+ * Returns -1 if not found.
2166
+ *
2167
+ * Does not change current read position.
2168
+ *
2169
+ * @param {number} value - Number to search for.
2170
+ * @param {string} endian - endianness of value (default set endian).
2171
+ */
2172
+ findFloat(value, endian) {
2173
+ return fFloat(this, value, endian);
2174
+ }
2175
+ /**
2176
+ * Searches for double float value position from current read position.
2177
+ *
2178
+ * Returns -1 if not found.
2179
+ *
2180
+ * Does not change current read position.
2181
+ *
2182
+ * @param {number} value - Number to search for.
2183
+ * @param {string} endian - endianness of value (default set endian).
2184
+ */
2185
+ findDoubleFloat(value, endian) {
2186
+ return fDoubleFloat(this, value, endian);
2187
+ }
2188
+ //
2189
+ // move from current position
2190
+ //
2191
+ /**
2192
+ * Aligns current byte position.
2193
+ *
2194
+ * Note: Will extend array if strict mode is off and outside of max size.
2195
+ *
2196
+ * @param {number} number - Byte to align
2197
+ */
2198
+ align(number) {
2199
+ return align(this, number);
2200
+ }
2201
+ /**
2202
+ * Reverse aligns current byte position.
2203
+ *
2204
+ * Note: Will extend array if strict mode is off and outside of max size.
2205
+ *
2206
+ * @param {number} number - Byte to align
2207
+ */
2208
+ alignRev(number) {
2209
+ return alignRev(this, number);
2210
+ }
2211
+ //
2212
+ // directly set current position
2213
+ //
2214
+ /**
2215
+ * Offset current byte or bit position.
2216
+ *
2217
+ * Note: Will extend array if strict mode is off and outside of max size.
2218
+ *
2219
+ * @param {number} bytes - Bytes to skip
2220
+ * @param {number} bits - Bits to skip
2221
+ */
2222
+ skip(bytes, bits) {
2223
+ return skip(this, bytes, bits);
2224
+ }
2225
+ /**
2226
+ * Offset current byte or bit position.
2227
+ *
2228
+ * Note: Will extend array if strict mode is off and outside of max size.
2229
+ *
2230
+ * @param {number} bytes - Bytes to skip
2231
+ * @param {number} bits - Bits to skip
2232
+ */
2233
+ jump(bytes, bits) {
2234
+ this.skip(bytes, bits);
2235
+ }
2236
+ /**
2237
+ * Change position directly to address.
2238
+ *
2239
+ * Note: Will extend array if strict mode is off and outside of max size.
2240
+ *
2241
+ * @param {number} byte - byte to set to
2242
+ * @param {number} bit - bit to set to
2243
+ */
2244
+ FSeek(byte, bit) {
2245
+ return goto(this, byte, bit);
2246
+ }
2247
+ /**
2248
+ * Offset current byte or bit position.
2249
+ *
2250
+ * Note: Will extend array if strict mode is off and outside of max size.
2251
+ *
2252
+ * @param {number} bytes - Bytes to skip
2253
+ * @param {number} bits - Bits to skip
2254
+ */
2255
+ seek(bytes, bits) {
2256
+ return this.skip(bytes, bits);
2257
+ }
2258
+ /**
2259
+ * Change position directly to address.
2260
+ *
2261
+ * Note: Will extend array if strict mode is off and outside of max size.
2262
+ *
2263
+ * @param {number} byte - byte to set to
2264
+ * @param {number} bit - bit to set to
2265
+ */
2266
+ goto(byte, bit) {
2267
+ return goto(this, byte, bit);
2268
+ }
2269
+ /**
2270
+ * Change position directly to address.
2271
+ *
2272
+ * Note: Will extend array if strict mode is off and outside of max size.
2273
+ *
2274
+ * @param {number} byte - byte to set to
2275
+ * @param {number} bit - bit to set to
2276
+ */
2277
+ pointer(byte, bit) {
2278
+ return this.goto(byte, bit);
2279
+ }
2280
+ /**
2281
+ * Change position directly to address.
2282
+ *
2283
+ * Note: Will extend array if strict mode is off and outside of max size.
2284
+ *
2285
+ * @param {number} byte - byte to set to
2286
+ * @param {number} bit - bit to set to
2287
+ */
2288
+ warp(byte, bit) {
2289
+ return this.goto(byte, bit);
2290
+ }
2291
+ //
2292
+ // go to start
2293
+ //
2294
+ /**
2295
+ * Set byte and bit position to start of data.
2296
+ */
2297
+ rewind() {
2298
+ this.offset = 0;
2299
+ this.bitoffset = 0;
2300
+ }
2301
+ /**
2302
+ * Set byte and bit position to start of data.
2303
+ */
2304
+ gotoStart() {
2305
+ return this.rewind();
2306
+ }
2307
+ /**
2308
+ * Set current byte and bit position to end of data.
2309
+ */
2310
+ last() {
2311
+ this.offset = this.size;
2312
+ this.bitoffset = 0;
2313
+ }
2314
+ /**
2315
+ * Set current byte and bit position to end of data.
2316
+ */
2317
+ gotoEnd() {
2318
+ this.offset = this.size;
2319
+ this.bitoffset = 0;
2320
+ }
2321
+ /**
2322
+ * Set byte and bit position to start of data.
2323
+ */
2324
+ EoF() {
2325
+ this.offset = this.size;
2326
+ this.bitoffset = 0;
2327
+ }
2328
+ //
2329
+ //remove part of data
2330
+ //
2331
+ /**
2332
+ * Deletes part of data from start to current byte position unless supplied, returns removed.
2333
+ *
2334
+ * Note: Errors in strict mode.
2335
+ *
2336
+ * @param {number} startOffset - Start location (default 0)
2337
+ * @param {number} endOffset - End location (default current position)
2338
+ * @param {boolean} consume - Move position to end of removed data (default false)
2339
+ * @returns {Buffer|Uint8Array} Removed data as ``Buffer`` or ``Uint8Array``
2340
+ */
2341
+ delete(startOffset, endOffset, consume) {
2342
+ return remove(this, startOffset || 0, endOffset || this.offset, consume || false, true);
2343
+ }
2344
+ /**
2345
+ * Deletes part of data from current byte position to end, returns removed.
2346
+ *
2347
+ * Note: Errors in strict mode.
2348
+ *
2349
+ * @returns {Buffer|Uint8Array} Removed data as ``Buffer`` or ``Uint8Array``
2350
+ */
2351
+ clip() {
2352
+ return remove(this, this.offset, this.size, false, true);
2353
+ }
2354
+ /**
2355
+ * Deletes part of data from current byte position to end, returns removed.
2356
+ *
2357
+ * Note: Errors in strict mode.
2358
+ *
2359
+ * @returns {Buffer|Uint8Array} Removed data as ``Buffer`` or ``Uint8Array``
2360
+ */
2361
+ trim() {
2362
+ return remove(this, this.offset, this.size, false, true);
2363
+ }
2364
+ /**
2365
+ * Deletes part of data from current byte position to supplied length, returns removed.
2366
+ *
2367
+ * Note: Errors in strict mode.
2368
+ *
2369
+ * @param {number} length - Length of data in bytes to remove
2370
+ * @param {boolean} consume - Move position to end of removed data (default false)
2371
+ * @returns {Buffer|Uint8Array} Removed data as ``Buffer`` or ``Uint8Array``
2372
+ */
2373
+ crop(length, consume) {
2374
+ return remove(this, this.offset, this.offset + (length || 0), consume || false, true);
2375
+ }
2376
+ /**
2377
+ * Deletes part of data from current position to supplied length, returns removed.
2378
+ *
2379
+ * Note: Only works in strict mode.
2380
+ *
2381
+ * @param {number} length - Length of data in bytes to remove
2382
+ * @param {boolean} consume - Move position to end of removed data (default false)
2383
+ * @returns {Buffer|Uint8Array} Removed data as ``Buffer`` or ``Uint8Array``
2384
+ */
2385
+ drop(length, consume) {
2386
+ return remove(this, this.offset, this.offset + (length || 0), consume || false, true);
2387
+ }
2388
+ /**
2389
+ * Replaces data in data.
2390
+ *
2391
+ * Note: Must be same data type as supplied data. Errors on strict mode.
2392
+ *
2393
+ * @param {Buffer|Uint8Array} data - ``Uint8Array`` or ``Buffer`` to replace in data
2394
+ * @param {boolean} consume - Move current byte position to end of data (default false)
2395
+ * @param {number} offset - Offset to add it at (defaults to current position)
2396
+ */
2397
+ replace(data, consume, offset) {
2398
+ return addData(this, data, consume || false, offset || this.offset, true);
2399
+ }
2400
+ /**
2401
+ * Replaces data in data.
2402
+ *
2403
+ * Note: Must be same data type as supplied data. Errors on strict mode.
2404
+ *
2405
+ * @param {Buffer|Uint8Array} data - ``Uint8Array`` or ``Buffer`` to replace in data
2406
+ * @param {boolean} consume - Move current byte position to end of data (default false)
2407
+ * @param {number} offset - Offset to add it at (defaults to current position)
2408
+ */
2409
+ overwrite(data, consume, offset) {
2410
+ return addData(this, data, consume || false, offset || this.offset, true);
2411
+ }
2412
+ //
2413
+ // copy out
2414
+ //
2415
+ /**
2416
+ * Returns part of data from current byte position to end of data unless supplied.
2417
+ *
2418
+ * @param {number} startOffset - Start location (default current position)
2419
+ * @param {number} endOffset - End location (default end of data)
2420
+ * @param {boolean} consume - Move position to end of lifted data (default false)
2421
+ * @param {number} fillValue - Byte value to to fill returned data (does NOT fill unless supplied)
2422
+ * @returns {Buffer|Uint8Array} Selected data as ``Uint8Array`` or ``Buffer``
2423
+ */
2424
+ lift(startOffset, endOffset, consume, fillValue) {
2425
+ return remove(this, startOffset || this.offset, endOffset || this.size, consume || false, false, fillValue);
2426
+ }
2427
+ /**
2428
+ * Returns part of data from current byte position to end of data unless supplied.
2429
+ *
2430
+ * @param {number} startOffset - Start location (default current position)
2431
+ * @param {number} endOffset - End location (default end of data)
2432
+ * @param {boolean} consume - Move position to end of lifted data (default false)
2433
+ * @param {number} fillValue - Byte value to to fill returned data (does NOT fill unless supplied)
2434
+ * @returns {Buffer|Uint8Array} Selected data as ``Uint8Array`` or ``Buffer``
2435
+ */
2436
+ fill(startOffset, endOffset, consume, fillValue) {
2437
+ return remove(this, startOffset || this.offset, endOffset || this.size, consume || false, false, fillValue);
2438
+ }
2439
+ /**
2440
+ * Extract data from current position to length supplied.
2441
+ *
2442
+ * Note: Does not affect supplied data.
2443
+ *
2444
+ * @param {number} length - Length of data in bytes to copy from current offset
2445
+ * @param {number} consume - Moves offset to end of length
2446
+ * @returns {Buffer|Uint8Array} Selected data as ``Uint8Array`` or ``Buffer``
2447
+ */
2448
+ extract(length, consume) {
2449
+ return remove(this, this.offset, this.offset + (length || 0), consume || false, false);
2450
+ }
2451
+ /**
2452
+ * Extract data from current position to length supplied.
2453
+ *
2454
+ * Note: Does not affect supplied data.
2455
+ *
2456
+ * @param {number} length - Length of data in bytes to copy from current offset
2457
+ * @param {number} consume - Moves offset to end of length
2458
+ * @returns {Buffer|Uint8Array} Selected data as ``Uint8Array`` or ``Buffer``
2459
+ */
2460
+ slice(length, consume) {
2461
+ return remove(this, this.offset, this.offset + (length || 0), consume || false, false);
2462
+ }
2463
+ /**
2464
+ * Extract data from current position to length supplied.
2465
+ *
2466
+ * Note: Does not affect supplied data.
2467
+ *
2468
+ * @param {number} length - Length of data in bytes to copy from current offset
2469
+ * @param {number} consume - Moves offset to end of length
2470
+ * @returns {Buffer|Uint8Array} Selected data as ``Uint8Array`` or ``Buffer``
2471
+ */
2472
+ wrap(length, consume) {
2473
+ return remove(this, this.offset, this.offset + (length || 0), consume || false, false);
2474
+ }
2475
+ //
2476
+ //insert
2477
+ //
2478
+ /**
2479
+ * Inserts data into data.
2480
+ *
2481
+ * Note: Must be same data type as supplied data. Errors on strict mode.
2482
+ *
2483
+ * @param {Buffer|Uint8Array} data - ``Uint8Array`` or ``Buffer`` to add to data
2484
+ * @param {boolean} consume - Move current byte position to end of data (default false)
2485
+ * @param {number} offset - Byte position to add at (defaults to current position)
2486
+ */
2487
+ insert(data, consume, offset) {
2488
+ return addData(this, data, consume || false, offset || this.offset, false);
2489
+ }
2490
+ /**
2491
+ * Inserts data into data.
2492
+ *
2493
+ * Note: Must be same data type as supplied data. Errors on strict mode.
2494
+ *
2495
+ * @param {Buffer|Uint8Array} data - ``Uint8Array`` or ``Buffer`` to add to data
2496
+ * @param {boolean} consume - Move current byte position to end of data (default false)
2497
+ * @param {number} offset - Byte position to add at (defaults to current position)
2498
+ */
2499
+ place(data, consume, offset) {
2500
+ return addData(this, data, consume || false, offset || this.offset, false);
2501
+ }
2502
+ /**
2503
+ * Adds data to start of supplied data.
2504
+ *
2505
+ * Note: Must be same data type as supplied data. Errors on strict mode.
2506
+ *
2507
+ * @param {Buffer|Uint8Array} data - ``Uint8Array`` or ``Buffer`` to add to data
2508
+ * @param {boolean} consume - Move current write position to end of data (default false)
2509
+ */
2510
+ unshift(data, consume) {
2511
+ return addData(this, data, consume || false, 0, false);
2512
+ }
2513
+ /**
2514
+ * Adds data to start of supplied data.
2515
+ *
2516
+ * Note: Must be same data type as supplied data. Errors on strict mode.
2517
+ *
2518
+ * @param {Buffer|Uint8Array} data - ``Uint8Array`` or ``Buffer`` to add to data
2519
+ * @param {boolean} consume - Move current write position to end of data (default false)
2520
+ */
2521
+ prepend(data, consume) {
2522
+ return addData(this, data, consume || false, 0, false);
2523
+ }
2524
+ /**
2525
+ * Adds data to end of supplied data.
2526
+ *
2527
+ * Note: Must be same data type as supplied data. Errors on strict mode.
2528
+ *
2529
+ * @param {Buffer|Uint8Array} data - ``Uint8Array`` or ``Buffer`` to add to data
2530
+ * @param {boolean} consume - Move current write position to end of data (default false)
2531
+ */
2532
+ push(data, consume) {
2533
+ return addData(this, data, consume || false, this.size, false);
2534
+ }
2535
+ /**
2536
+ * Adds data to end of supplied data.
2537
+ *
2538
+ * Note: Must be same data type as supplied data. Errors on strict mode.
2539
+ *
2540
+ * @param {Buffer|Uint8Array} data - ``Uint8Array`` or ``Buffer`` to add to data
2541
+ * @param {boolean} consume - Move current write position to end of data (default false)
2542
+ */
2543
+ append(data, consume) {
2544
+ return addData(this, data, consume || false, this.size, false);
2545
+ }
2546
+ //
2547
+ // math
2548
+ //
2549
+ /**
2550
+ * XOR data.
2551
+ *
2552
+ * @param {number|string|Uint8Array|Buffer} xorKey - Value, string or array to XOR
2553
+ * @param {number} startOffset - Start location (default current byte position)
2554
+ * @param {number} endOffset - End location (default end of data)
2555
+ * @param {boolean} consume - Move current position to end of data (default false)
2556
+ */
2557
+ xor(xorKey, startOffset, endOffset, consume) {
2558
+ var XORKey = xorKey;
2559
+ if (typeof xorKey == "number") {
2560
+ //pass
2561
+ }
2562
+ else if (typeof xorKey == "string") {
2563
+ xorKey = new TextEncoder().encode(xorKey);
2564
+ }
2565
+ else if (this.isBufferOrUint8Array(XORKey)) {
2566
+ //pass
2567
+ }
2568
+ else {
2569
+ throw new Error("XOR must be a number, string, Uint8Array or Buffer");
2570
+ }
2571
+ return XOR(this, xorKey, startOffset || this.offset, endOffset || this.size, consume || false);
2572
+ }
2573
+ /**
2574
+ * XOR data.
2575
+ *
2576
+ * @param {number|string|Uint8Array|Buffer} xorKey - Value, string or array to XOR
2577
+ * @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)
2578
+ * @param {boolean} consume - Move current position to end of data (default false)
2579
+ */
2580
+ xorThis(xorKey, length, consume) {
2581
+ var Length = length || 1;
2582
+ var XORKey = xorKey;
2583
+ if (typeof xorKey == "number") {
2584
+ Length = length || 1;
2585
+ }
2586
+ else if (typeof xorKey == "string") {
2587
+ const encoder = new TextEncoder().encode(xorKey);
2588
+ XORKey = encoder;
2589
+ Length = length || encoder.length;
2590
+ }
2591
+ else if (this.isBufferOrUint8Array(XORKey)) {
2592
+ Length = length || xorKey.length;
2593
+ }
2594
+ else {
2595
+ throw new Error("XOR must be a number, string, Uint8Array or Buffer");
2596
+ }
2597
+ return XOR(this, XORKey, this.offset, this.offset + Length, consume || false);
2598
+ }
2599
+ /**
2600
+ * OR data
2601
+ *
2602
+ * @param {number|string|Uint8Array|Buffer} orKey - Value, string or array to OR
2603
+ * @param {number} startOffset - Start location (default current byte position)
2604
+ * @param {number} endOffset - End location (default end of data)
2605
+ * @param {boolean} consume - Move current position to end of data (default false)
2606
+ */
2607
+ or(orKey, startOffset, endOffset, consume) {
2608
+ var ORKey = orKey;
2609
+ if (typeof orKey == "number") {
2610
+ //pass
2611
+ }
2612
+ else if (typeof orKey == "string") {
2613
+ orKey = new TextEncoder().encode(orKey);
2614
+ }
2615
+ else if (this.isBufferOrUint8Array(ORKey)) {
2616
+ //pass
2617
+ }
2618
+ else {
2619
+ throw new Error("OR must be a number, string, Uint8Array or Buffer");
2620
+ }
2621
+ return OR(this, orKey, startOffset || this.offset, endOffset || this.size, consume || false);
2622
+ }
2623
+ /**
2624
+ * OR data.
2625
+ *
2626
+ * @param {number|string|Uint8Array|Buffer} orKey - Value, string or array to OR
2627
+ * @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)
2628
+ * @param {boolean} consume - Move current position to end of data (default false)
2629
+ */
2630
+ orThis(orKey, length, consume) {
2631
+ var Length = length || 1;
2632
+ var ORKey = orKey;
2633
+ if (typeof orKey == "number") {
2634
+ Length = length || 1;
2635
+ }
2636
+ else if (typeof orKey == "string") {
2637
+ const encoder = new TextEncoder().encode(orKey);
2638
+ ORKey = encoder;
2639
+ Length = length || encoder.length;
2640
+ }
2641
+ else if (this.isBufferOrUint8Array(ORKey)) {
2642
+ Length = length || orKey.length;
2643
+ }
2644
+ else {
2645
+ throw new Error("OR must be a number, string, Uint8Array or Buffer");
2646
+ }
2647
+ return OR(this, ORKey, this.offset, this.offset + Length, consume || false);
2648
+ }
2649
+ /**
2650
+ * AND data.
2651
+ *
2652
+ * @param {number|string|Array<number>|Buffer} andKey - Value, string or array to AND
2653
+ * @param {number} startOffset - Start location (default current byte position)
2654
+ * @param {number} endOffset - End location (default end of data)
2655
+ * @param {boolean} consume - Move current position to end of data (default false)
2656
+ */
2657
+ and(andKey, startOffset, endOffset, consume) {
2658
+ var ANDKey = andKey;
2659
+ if (typeof ANDKey == "number") {
2660
+ //pass
2661
+ }
2662
+ else if (typeof ANDKey == "string") {
2663
+ ANDKey = new TextEncoder().encode(ANDKey);
2664
+ }
2665
+ else if (typeof ANDKey == "object") {
2666
+ //pass
2667
+ }
2668
+ else {
2669
+ throw new Error("AND must be a number, string, number array or Buffer");
2670
+ }
2671
+ return AND(this, andKey, startOffset || this.offset, endOffset || this.size, consume || false);
2672
+ }
2673
+ /**
2674
+ * AND data.
2675
+ *
2676
+ * @param {number|string|Array<number>|Buffer} andKey - Value, string or array to AND
2677
+ * @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)
2678
+ * @param {boolean} consume - Move current position to end of data (default false)
2679
+ */
2680
+ andThis(andKey, length, consume) {
2681
+ var Length = length || 1;
2682
+ var ANDKey = andKey;
2683
+ if (typeof andKey == "number") {
2684
+ Length = length || 1;
2685
+ }
2686
+ else if (typeof andKey == "string") {
2687
+ const encoder = new TextEncoder().encode(andKey);
2688
+ ANDKey = encoder;
2689
+ Length = length || encoder.length;
2690
+ }
2691
+ else if (typeof andKey == "object") {
2692
+ Length = length || andKey.length;
2693
+ }
2694
+ else {
2695
+ throw new Error("AND must be a number, string, number array or Buffer");
2696
+ }
2697
+ return AND(this, ANDKey, this.offset, this.offset + Length, consume || false);
2698
+ }
2699
+ /**
2700
+ * Add value to data.
2701
+ *
2702
+ * @param {number|string|Array<number>|Buffer} addKey - Value, string or array to add to data
2703
+ * @param {number} startOffset - Start location (default current byte position)
2704
+ * @param {number} endOffset - End location (default end of data)
2705
+ * @param {boolean} consume - Move current position to end of data (default false)
2706
+ */
2707
+ add(addKey, startOffset, endOffset, consume) {
2708
+ var addedKey = addKey;
2709
+ if (typeof addedKey == "number") {
2710
+ //pass
2711
+ }
2712
+ else if (typeof addedKey == "string") {
2713
+ addedKey = new TextEncoder().encode(addedKey);
2714
+ }
2715
+ else if (typeof addedKey == "object") {
2716
+ //pass
2717
+ }
2718
+ else {
2719
+ throw new Error("Add key must be a number, string, number array or Buffer");
2720
+ }
2721
+ return ADD(this, addedKey, startOffset || this.offset, endOffset || this.size, consume || false);
2722
+ }
2723
+ /**
2724
+ * Add value to data.
2725
+ *
2726
+ * @param {number|string|Array<number>|Buffer} addKey - Value, string or array to add to data
2727
+ * @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)
2728
+ * @param {boolean} consume - Move current position to end of data (default false)
2729
+ */
2730
+ addThis(addKey, length, consume) {
2731
+ var Length = length || 1;
2732
+ var AddedKey = addKey;
2733
+ if (typeof AddedKey == "number") {
2734
+ Length = length || 1;
2735
+ }
2736
+ else if (typeof AddedKey == "string") {
2737
+ const encoder = new TextEncoder().encode(AddedKey);
2738
+ AddedKey = encoder;
2739
+ Length = length || encoder.length;
2740
+ }
2741
+ else if (typeof AddedKey == "object") {
2742
+ Length = length || AddedKey.length;
2743
+ }
2744
+ else {
2745
+ throw new Error("Add key must be a number, string, number array or Buffer");
2746
+ }
2747
+ return ADD(this, AddedKey, this.offset, this.offset + Length, consume || false);
2748
+ }
2749
+ /**
2750
+ * Not data.
2751
+ *
2752
+ * @param {number} startOffset - Start location (default current byte position)
2753
+ * @param {number} endOffset - End location (default end of data)
2754
+ * @param {boolean} consume - Move current position to end of data (default false)
2755
+ */
2756
+ not(startOffset, endOffset, consume) {
2757
+ return NOT(this, startOffset || this.offset, endOffset || this.size, consume || false);
2758
+ }
2759
+ /**
2760
+ * Not data.
2761
+ *
2762
+ * @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)
2763
+ * @param {boolean} consume - Move current position to end of data (default false)
2764
+ */
2765
+ notThis(length, consume) {
2766
+ return NOT(this, this.offset, this.offset + (length || 1), consume || false);
2767
+ }
2768
+ /**
2769
+ * Left shift data.
2770
+ *
2771
+ * @param {number|string|Array<number>|Buffer} shiftKey - Value, string or array to left shift data
2772
+ * @param {number} startOffset - Start location (default current byte position)
2773
+ * @param {number} endOffset - End location (default end of data)
2774
+ * @param {boolean} consume - Move current position to end of data (default false)
2775
+ */
2776
+ lShift(shiftKey, startOffset, endOffset, consume) {
2777
+ var lShiftKey = shiftKey;
2778
+ if (typeof lShiftKey == "number") {
2779
+ //pass
2780
+ }
2781
+ else if (typeof lShiftKey == "string") {
2782
+ lShiftKey = new TextEncoder().encode(lShiftKey);
2783
+ }
2784
+ else if (typeof lShiftKey == "object") {
2785
+ //pass
2786
+ }
2787
+ else {
2788
+ throw new Error("Left shift must be a number, string, number array or Buffer");
2789
+ }
2790
+ return LSHIFT(this, lShiftKey, startOffset || this.offset, endOffset || this.size, consume || false);
2791
+ }
2792
+ /**
2793
+ * Left shift data.
2794
+ *
2795
+ * @param {number|string|Array<number>|Buffer} shiftKey - Value, string or array to left shift data
2796
+ * @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)
2797
+ * @param {boolean} consume - Move current position to end of data (default false)
2798
+ */
2799
+ lShiftThis(shiftKey, length, consume) {
2800
+ var Length = length || 1;
2801
+ var lShiftKey = shiftKey;
2802
+ if (typeof lShiftKey == "number") {
2803
+ Length = length || 1;
2804
+ }
2805
+ else if (typeof lShiftKey == "string") {
2806
+ const encoder = new TextEncoder().encode(lShiftKey);
2807
+ lShiftKey = encoder;
2808
+ Length = length || encoder.length;
2809
+ }
2810
+ else if (typeof lShiftKey == "object") {
2811
+ Length = length || lShiftKey.length;
2812
+ }
2813
+ else {
2814
+ throw new Error("Left shift must be a number, string, number array or Buffer");
2815
+ }
2816
+ return LSHIFT(this, shiftKey, this.offset, this.offset + Length, consume || false);
2817
+ }
2818
+ /**
2819
+ * Right shift data.
2820
+ *
2821
+ * @param {number|string|Array<number>|Buffer} shiftKey - Value, string or array to right shift data
2822
+ * @param {number} startOffset - Start location (default current byte position)
2823
+ * @param {number} endOffset - End location (default end of data)
2824
+ * @param {boolean} consume - Move current position to end of data (default false)
2825
+ */
2826
+ rShift(shiftKey, startOffset, endOffset, consume) {
2827
+ var rShiftKey = shiftKey;
2828
+ if (typeof rShiftKey == "number") {
2829
+ //pass
2830
+ }
2831
+ else if (typeof rShiftKey == "string") {
2832
+ rShiftKey = new TextEncoder().encode(rShiftKey);
2833
+ }
2834
+ else if (typeof rShiftKey == "object") {
2835
+ //pass
2836
+ }
2837
+ else {
2838
+ throw new Error("Right shift must be a number, string, number array or Buffer");
2839
+ }
2840
+ return RSHIFT(this, rShiftKey, startOffset || this.offset, endOffset || this.size, consume || false);
2841
+ }
2842
+ /**
2843
+ * Right shift data.
2844
+ *
2845
+ * @param {number|string|Array<number>|Buffer} shiftKey - Value, string or array to right shift data
2846
+ * @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)
2847
+ * @param {boolean} consume - Move current position to end of data (default false)
2848
+ */
2849
+ rShiftThis(shiftKey, length, consume) {
2850
+ var Length = length || 1;
2851
+ var lShiftKey = shiftKey;
2852
+ if (typeof lShiftKey == "number") {
2853
+ Length = length || 1;
2854
+ }
2855
+ else if (typeof lShiftKey == "string") {
2856
+ const encoder = new TextEncoder().encode(lShiftKey);
2857
+ lShiftKey = encoder;
2858
+ Length = length || encoder.length;
2859
+ }
2860
+ else if (typeof lShiftKey == "object") {
2861
+ Length = length || lShiftKey.length;
2862
+ }
2863
+ else {
2864
+ throw new Error("Right shift must be a number, string, number array or Buffer");
2865
+ }
2866
+ return RSHIFT(this, lShiftKey, this.offset, this.offset + Length, consume || false);
2867
+ }
2868
+ //
2869
+ //bit reader
2870
+ //
2871
+ /**
2872
+ *
2873
+ * Write bits, must have at least value and number of bits.
2874
+ *
2875
+ * ``Note``: When returning to a byte write, remaining bits are skipped.
2876
+ *
2877
+ * @param {number} value - value as int
2878
+ * @param {number} bits - number of bits to write
2879
+ * @param {boolean} unsigned - if value is unsigned
2880
+ * @param {string} endian - ``big`` or ``little``
2881
+ */
2882
+ writeBit(value, bits, unsigned, endian) {
2883
+ return wbit(this, value, bits, unsigned, endian);
2884
+ }
2885
+ /**
2886
+ * Bit field writer.
2887
+ *
2888
+ * Note: When returning to a byte write, remaining bits are dropped.
2889
+ *
2890
+ * @param {number} value - value as int
2891
+ * @param {number} bits - bits to write
2892
+ * @returns number
2893
+ */
2894
+ writeUBitBE(value, bits) {
2895
+ return wbit(this, value, bits, true, "big");
2896
+ }
2897
+ /**
2898
+ * Bit field writer.
2899
+ *
2900
+ * Note: When returning to a byte write, remaining bits are dropped.
2901
+ *
2902
+ * @param {number} value - value as int
2903
+ * @param {number} bits - bits to write
2904
+ * @param {boolean} unsigned - if the value is unsigned
2905
+ * @returns number
2906
+ */
2907
+ writeBitBE(value, bits, unsigned) {
2908
+ return wbit(this, value, bits, unsigned, "big");
2909
+ }
2910
+ /**
2911
+ * Bit field writer.
2912
+ *
2913
+ * Note: When returning to a byte write, remaining bits are dropped.
2914
+ *
2915
+ * @param {number} value - value as int
2916
+ * @param {number} bits - bits to write
2917
+ * @returns number
2918
+ */
2919
+ writeUBitLE(value, bits) {
2920
+ return wbit(this, value, bits, true, "little");
2921
+ }
2922
+ /**
2923
+ * Bit field writer.
2924
+ *
2925
+ * Note: When returning to a byte write, remaining bits are dropped.
2926
+ *
2927
+ * @param {number} value - value as int
2928
+ * @param {number} bits - bits to write
2929
+ * @param {boolean} unsigned - if the value is unsigned
2930
+ * @returns number
2931
+ */
2932
+ writeBitLE(value, bits, unsigned) {
2933
+ return wbit(this, value, bits, unsigned, "little");
2934
+ }
2935
+ /**
2936
+ * Bit field reader.
2937
+ *
2938
+ * Note: When returning to a byte read, remaining bits are dropped.
2939
+ *
2940
+ * @param {number} bits - bits to read
2941
+ * @param {boolean} unsigned - if the value is unsigned
2942
+ * @param {string} endian - ``big`` or ``little``
2943
+ * @returns {number}
2944
+ */
2945
+ readBit(bits, unsigned, endian) {
2946
+ return rbit(this, bits, unsigned, endian);
2947
+ }
2948
+ /**
2949
+ * Bit field reader.
2950
+ *
2951
+ * Note: When returning to a byte read, remaining bits are dropped.
2952
+ *
2953
+ * @param {number} bits - bits to read
2954
+ * @returns {number}
2955
+ */
2956
+ readUBitBE(bits) {
2957
+ return this.readBit(bits, true, "big");
2958
+ }
2959
+ /**
2960
+ * Bit field reader.
2961
+ *
2962
+ * Note: When returning to a byte read, remaining bits are dropped.
2963
+ *
2964
+ * @param {number} bits - bits to read
2965
+ * @param {boolean} unsigned - if the value is unsigned
2966
+ * @returns {number}
2967
+ */
2968
+ readBitBE(bits, unsigned) {
2969
+ return this.readBit(bits, unsigned, "big");
2970
+ }
2971
+ /**
2972
+ * Bit field reader.
2973
+ *
2974
+ * Note: When returning to a byte read, remaining bits are dropped.
2975
+ *
2976
+ * @param {number} bits - bits to read
2977
+ * @returns {number}
2978
+ */
2979
+ readUBitLE(bits) {
2980
+ return this.readBit(bits, true, "little");
2981
+ }
2982
+ /**
2983
+ * Bit field reader.
2984
+ *
2985
+ * Note: When returning to a byte read, remaining bits are dropped.
2986
+ *
2987
+ * @param {number} bits - bits to read
2988
+ * @param {boolean} unsigned - if the value is unsigned
2989
+ * @returns {number}
2990
+ */
2991
+ readBitLE(bits, unsigned) {
2992
+ return this.readBit(bits, unsigned, "little");
2993
+ }
2994
+ /**
2995
+ * Read byte.
2996
+ *
2997
+ * @param {boolean} unsigned - if value is unsigned or not
2998
+ * @returns {number}
2999
+ */
3000
+ readByte(unsigned) {
3001
+ return rbyte(this, unsigned);
3002
+ }
3003
+ /**
3004
+ * Read multiple bytes.
3005
+ *
3006
+ * @param {number} amount - amount of bytes to read
3007
+ * @param {boolean} unsigned - if value is unsigned or not
3008
+ * @returns {number[]}
3009
+ */
3010
+ readBytes(amount, unsigned) {
3011
+ return Array.from({ length: amount }, () => rbyte(this, unsigned));
3012
+ }
3013
+ /**
3014
+ * Write byte.
3015
+ *
3016
+ * @param {number} value - value as int
3017
+ * @param {boolean} unsigned - if the value is unsigned
3018
+ */
3019
+ writeByte(value, unsigned) {
3020
+ return wbyte(this, value, unsigned);
3021
+ }
3022
+ /**
3023
+ * Write multiple bytes.
3024
+ *
3025
+ * @param {number[]} values - array of values as int
3026
+ * @param {boolean} unsigned - if the value is unsigned
3027
+ */
3028
+ writeBytes(values, unsigned) {
3029
+ for (let i = 0; i < values.length; i++) {
3030
+ wbyte(this, values[i], unsigned);
3031
+ }
3032
+ }
3033
+ /**
3034
+ * Write unsigned byte.
3035
+ *
3036
+ * @param {number} value - value as int
3037
+ */
3038
+ writeUByte(value) {
3039
+ return wbyte(this, value, true);
3040
+ }
3041
+ /**
3042
+ * Read unsigned byte.
3043
+ *
3044
+ * @returns {number}
3045
+ */
3046
+ readUByte() {
3047
+ return this.readByte(true);
3048
+ }
3049
+ /**
3050
+ * Read short.
3051
+ *
3052
+ * @param {boolean} unsigned - if value is unsigned or not
3053
+ * @param {string} endian - ``big`` or ``little``
3054
+ * @returns {number}
3055
+ */
3056
+ readInt16(unsigned, endian) {
3057
+ return rint16(this, unsigned, endian);
3058
+ }
3059
+ /**
3060
+ * Write int16.
3061
+ *
3062
+ * @param {number} value - value as int
3063
+ * @param {boolean} unsigned - if the value is unsigned
3064
+ * @param {string} endian - ``big`` or ``little``
3065
+ */
3066
+ writeInt16(value, unsigned, endian) {
3067
+ return wint16(this, value, unsigned, endian);
3068
+ }
3069
+ /**
3070
+ * Write unsigned int16.
3071
+ *
3072
+ * @param {number} value - value as int
3073
+ * @param {string} endian - ``big`` or ``little``
3074
+ */
3075
+ writeUInt16(value, endian) {
3076
+ return wint16(this, value, true, endian);
3077
+ }
3078
+ /**
3079
+ * Write unsigned int16.
3080
+ *
3081
+ * @param {number} value - value as int
3082
+ */
3083
+ writeUInt16BE(value) {
3084
+ return this.writeInt16(value, true, "big");
3085
+ }
3086
+ /**
3087
+ * Write unsigned int16.
3088
+ *
3089
+ * @param {number} value - value as int
3090
+ */
3091
+ writeUInt16LE(value) {
3092
+ return this.writeInt16(value, true, "little");
3093
+ }
3094
+ /**
3095
+ * Write signed int16.
3096
+ *
3097
+ * @param {number} value - value as int
3098
+ */
3099
+ writeInt16LE(value) {
3100
+ return this.writeInt16(value, false, "little");
3101
+ }
3102
+ /**
3103
+ * Read unsigned short.
3104
+ *
3105
+ * @param {string} endian - ``big`` or ``little``
3106
+ *
3107
+ * @returns {number}
3108
+ */
3109
+ readUInt16(endian) {
3110
+ return this.readInt16(true, endian);
3111
+ }
3112
+ /**
3113
+ * Read unsigned short in little endian.
3114
+ *
3115
+ * @returns {number}
3116
+ */
3117
+ readUInt16LE() {
3118
+ return this.readInt16(true, "little");
3119
+ }
3120
+ /**
3121
+ * Read signed short in little endian.
3122
+ *
3123
+ * @returns {number}
3124
+ */
3125
+ readInt16LE() {
3126
+ return this.readInt16(false, "little");
3127
+ }
3128
+ /**
3129
+ * Read unsigned short in big endian.
3130
+ *
3131
+ * @returns {number}
3132
+ */
3133
+ readUInt16BE() {
3134
+ return this.readInt16(true, "big");
3135
+ }
3136
+ /**
3137
+ * Read signed short in big endian.
3138
+ *
3139
+ * @returns {number}
3140
+ */
3141
+ readInt16BE() {
3142
+ return this.readInt16(false, "big");
3143
+ }
3144
+ /**
3145
+ * Read half float.
3146
+ *
3147
+ * @param {string} endian - ``big`` or ``little``
3148
+ * @returns {number}
3149
+ */
3150
+ readHalfFloat(endian) {
3151
+ return rhalffloat(this, endian);
3152
+ }
3153
+ /**
3154
+ * Writes half float.
3155
+ *
3156
+ * @param {number} value - value as int
3157
+ * @param {string} endian - ``big`` or ``little``
3158
+ */
3159
+ writeHalfFloat(value, endian) {
3160
+ return whalffloat(this, value, endian);
3161
+ }
3162
+ /**
3163
+ * Writes half float.
3164
+ *
3165
+ * @param {number} value - value as int
3166
+ */
3167
+ writeHalfFloatBE(value) {
3168
+ return this.writeHalfFloat(value, "big");
3169
+ }
3170
+ /**
3171
+ * Writes half float.
3172
+ *
3173
+ * @param {number} value - value as int
3174
+ */
3175
+ writeHalfFloatLE(value) {
3176
+ return this.writeHalfFloat(value, "little");
3177
+ }
3178
+ /**
3179
+ * Read half float.
3180
+ *
3181
+ * @returns {number}
3182
+ */
3183
+ readHalfFloatBE() {
3184
+ return this.readHalfFloat("big");
3185
+ }
3186
+ /**
3187
+ * Read half float.
3188
+ *
3189
+ * @returns {number}
3190
+ */
3191
+ readHalfFloatLE() {
3192
+ return this.readHalfFloat("little");
3193
+ }
3194
+ /**
3195
+ * Read 32 bit integer.
3196
+ *
3197
+ * @param {boolean} unsigned - if value is unsigned or not
3198
+ * @param {string} endian - ``big`` or ``little``
3199
+ * @returns {number}
3200
+ */
3201
+ readInt32(unsigned, endian) {
3202
+ return rint32(this, unsigned, endian);
3203
+ }
3204
+ /**
3205
+ * Write int32.
3206
+ *
3207
+ * @param {number} value - value as int
3208
+ * @param {boolean} unsigned - if the value is unsigned
3209
+ * @param {string} endian - ``big`` or ``little``
3210
+ */
3211
+ writeInt32(value, unsigned, endian) {
3212
+ return wint32(this, value, unsigned, endian);
3213
+ }
3214
+ /**
3215
+ * Write unsigned int32.
3216
+ *
3217
+ * @param {number} value - value as int
3218
+ * @param {string} endian - ``big`` or ``little``
3219
+ */
3220
+ writeUInt32(value, endian) {
3221
+ return wint32(this, value, true, endian);
3222
+ }
3223
+ /**
3224
+ * Write signed int32.
3225
+ *
3226
+ * @param {number} value - value as int
3227
+ */
3228
+ writeInt32LE(value) {
3229
+ return this.writeInt32(value, false, "little");
3230
+ }
3231
+ /**
3232
+ * Write unsigned int32.
3233
+ *
3234
+ * @param {number} value - value as int
3235
+ */
3236
+ writeUInt32LE(value) {
3237
+ return this.writeInt32(value, true, "little");
3238
+ }
3239
+ /**
3240
+ * Write signed int32.
3241
+ *
3242
+ * @param {number} value - value as int
3243
+ */
3244
+ writeInt32BE(value) {
3245
+ return this.writeInt32(value, false, "big");
3246
+ }
3247
+ /**
3248
+ * Read signed 32 bit integer.
3249
+ *
3250
+ * @returns {number}
3251
+ */
3252
+ readInt32BE() {
3253
+ return this.readInt32(false, "big");
3254
+ }
3255
+ /**
3256
+ * Read unsigned 32 bit integer.
3257
+ *
3258
+ * @returns {number}
3259
+ */
3260
+ readUInt32BE() {
3261
+ return this.readInt32(true, "big");
3262
+ }
3263
+ /**
3264
+ * Read signed 32 bit integer.
3265
+ *
3266
+ * @returns {number}
3267
+ */
3268
+ readInt32LE() {
3269
+ return this.readInt32(false, "little");
3270
+ }
3271
+ /**
3272
+ * Read signed 32 bit integer.
3273
+ *
3274
+ * @returns {number}
3275
+ */
3276
+ readUInt32LE() {
3277
+ return this.readInt32(true, "little");
3278
+ }
3279
+ /**
3280
+ * Read unsigned 32 bit integer.
3281
+ *
3282
+ * @returns {number}
3283
+ */
3284
+ readUInt() {
3285
+ return this.readInt32(true);
3286
+ }
3287
+ /**
3288
+ * Read float.
3289
+ *
3290
+ * @param {string} endian - ``big`` or ``little``
3291
+ * @returns {number}
3292
+ */
3293
+ readFloat(endian) {
3294
+ return rfloat(this, endian);
3295
+ }
3296
+ /**
3297
+ * Write float.
3298
+ *
3299
+ * @param {number} value - value as int
3300
+ * @param {string} endian - ``big`` or ``little``
3301
+ */
3302
+ writeFloat(value, endian) {
3303
+ return wfloat(this, value, endian);
3304
+ }
3305
+ /**
3306
+ * Write float.
3307
+ *
3308
+ * @param {number} value - value as int
3309
+ */
3310
+ writeFloatLE(value) {
3311
+ return this.writeFloat(value, "little");
3312
+ }
3313
+ /**
3314
+ * Write float.
3315
+ *
3316
+ * @param {number} value - value as int
3317
+ */
3318
+ writeFloatBE(value) {
3319
+ return this.writeFloat(value, "big");
3320
+ }
3321
+ /**
3322
+ * Read float.
3323
+ *
3324
+ * @returns {number}
3325
+ */
3326
+ readFloatBE() {
3327
+ return this.readFloat("big");
3328
+ }
3329
+ /**
3330
+ * Read float.
3331
+ *
3332
+ * @returns {number}
3333
+ */
3334
+ readFloatLE() {
3335
+ return this.readFloat("little");
3336
+ }
3337
+ /**
3338
+ * Read signed 64 bit integer.
3339
+ *
3340
+ * @param {boolean} unsigned - if value is unsigned or not
3341
+ * @param {string} endian - ``big`` or ``little``
3342
+ * @returns {number}
3343
+ */
3344
+ readInt64(unsigned, endian) {
3345
+ return rint64(this, unsigned, endian);
3346
+ }
3347
+ /**
3348
+ * Write 64 bit integer.
3349
+ *
3350
+ * @param {number} value - value as int
3351
+ * @param {boolean} unsigned - if the value is unsigned
3352
+ * @param {string} endian - ``big`` or ``little``
3353
+ */
3354
+ writeInt64(value, unsigned, endian) {
3355
+ return wint64(this, value, unsigned, endian);
3356
+ }
3357
+ /**
3358
+ * Write unsigned 64 bit integer.
3359
+ *
3360
+ * @param {number} value - value as int
3361
+ * @param {string} endian - ``big`` or ``little``
3362
+ */
3363
+ writeUInt64(value, endian) {
3364
+ return this.writeInt64(value, true, endian);
3365
+ }
3366
+ /**
3367
+ * Write signed 64 bit integer.
3368
+ *
3369
+ * @param {number} value - value as int
3370
+ */
3371
+ writeInt64LE(value) {
3372
+ return this.writeInt64(value, false, "little");
3373
+ }
3374
+ /**
3375
+ * Write unsigned 64 bit integer.
3376
+ *
3377
+ * @param {number} value - value as int
3378
+ */
3379
+ writeUInt64LE(value) {
3380
+ return this.writeInt64(value, true, "little");
3381
+ }
3382
+ /**
3383
+ * Write signed 64 bit integer.
3384
+ *
3385
+ * @param {number} value - value as int
3386
+ */
3387
+ writeInt64BE(value) {
3388
+ return this.writeInt64(value, false, "big");
3389
+ }
3390
+ /**
3391
+ * Write unsigned 64 bit integer.
3392
+ *
3393
+ * @param {number} value - value as int
3394
+ */
3395
+ writeUInt64BE(value) {
3396
+ return this.writeInt64(value, true, "big");
3397
+ }
3398
+ /**
3399
+ * Read unsigned 64 bit integer.
3400
+ *
3401
+ * @returns {number}
3402
+ */
3403
+ readUInt64() {
3404
+ return this.readInt64(true);
3405
+ }
3406
+ /**
3407
+ * Read signed 64 bit integer.
3408
+ *
3409
+ * @returns {number}
3410
+ */
3411
+ readInt64BE() {
3412
+ return this.readInt64(false, "big");
3413
+ }
3414
+ /**
3415
+ * Read unsigned 64 bit integer.
3416
+ *
3417
+ * @returns {number}
3418
+ */
3419
+ readUInt64BE() {
3420
+ return this.readInt64(true, "big");
3421
+ }
3422
+ /**
3423
+ * Read signed 64 bit integer.
3424
+ *
3425
+ * @returns {number}
3426
+ */
3427
+ readInt64LE() {
3428
+ return this.readInt64(false, "little");
3429
+ }
3430
+ /**
3431
+ * Read unsigned 64 bit integer.
3432
+ *
3433
+ * @returns {number}
3434
+ */
3435
+ readUInt64LE() {
3436
+ return this.readInt64(true, "little");
3437
+ }
3438
+ /**
3439
+ * Read double float.
3440
+ *
3441
+ * @param {string} endian - ``big`` or ``little``
3442
+ * @returns {number}
3443
+ */
3444
+ readDoubleFloat(endian) {
3445
+ return rdfloat(this, endian);
3446
+ }
3447
+ /**
3448
+ * Writes double float.
3449
+ *
3450
+ * @param {number} value - value as int
3451
+ * @param {string} endian - ``big`` or ``little``
3452
+ */
3453
+ writeDoubleFloat(value, endian) {
3454
+ return wdfloat(this, value, endian);
3455
+ }
3456
+ /**
3457
+ * Writes double float.
3458
+ *
3459
+ * @param {number} value - value as int
3460
+ */
3461
+ writeDoubleFloatBE(value) {
3462
+ return this.writeDoubleFloat(value, "big");
3463
+ }
3464
+ /**
3465
+ * Writes double float.
3466
+ *
3467
+ * @param {number} value - value as int
3468
+ */
3469
+ writeDoubleFloatLE(value) {
3470
+ return this.writeDoubleFloat(value, "little");
3471
+ }
3472
+ /**
3473
+ * Read double float.
3474
+ *
3475
+ * @returns {number}
3476
+ */
3477
+ readDoubleFloatBE() {
3478
+ return this.readDoubleFloat("big");
3479
+ }
3480
+ /**
3481
+ * Read double float.
3482
+ *
3483
+ * @returns {number}
3484
+ */
3485
+ readDoubleFloatLE() {
3486
+ return this.readDoubleFloat("little");
3487
+ }
3488
+ /**
3489
+ * Reads string, use options object for different types.
3490
+ *
3491
+ * @param {object} options
3492
+ * ```javascript
3493
+ * {
3494
+ * length: number, //for fixed length, non-terminate value utf strings
3495
+ * stringType: "utf-8", //utf-8, utf-16, pascal or wide-pascal
3496
+ * terminateValue: 0x00, // only for non-fixed length utf strings
3497
+ * lengthReadSize: 1, //for pascal strings. 1, 2 or 4 byte length read size
3498
+ * stripNull: true, // removes 0x00 characters
3499
+ * encoding: "utf-8", //TextEncoder accepted types
3500
+ * endian: "little", //for wide-pascal and utf-16
3501
+ * }
3502
+ * ```
3503
+ * @return string
3504
+ */
3505
+ readString(options) {
3506
+ return rstring(this, options);
3507
+ }
3508
+ /**
3509
+ * Writes string, use options object for different types.
3510
+ *
3511
+ *
3512
+ * @param {string} string - text string
3513
+ * @param {object} options - options:
3514
+ * ```javascript
3515
+ * {
3516
+ * length: string.length, //for fixed length, non-terminate value utf strings
3517
+ * stringType: "utf-8", //utf-8, utf-16, pascal or wide-pascal
3518
+ * terminateValue: 0x00, // only with stringType: "utf"
3519
+ * lengthWriteSize: 1, //for pascal strings. 1, 2 or 4 byte length write size
3520
+ * encoding: "utf-8", //TextEncoder accepted types
3521
+ * endian: "little", //for wide-pascal and utf-16
3522
+ * }
3523
+ * ```
3524
+ */
3525
+ writeString(string, options) {
3526
+ return wstring(this, string, options);
3527
+ }
3528
+ }
3529
+ exports.ReaderBase = ReaderBase;
3530
+ ;
3531
+ //# sourceMappingURL=common.js.map