bireader 2.0.0 → 3.0.0

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