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