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