bireader 1.0.14 → 1.0.17

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