-
Notifications
You must be signed in to change notification settings - Fork 1
/
binary-reader.ts
617 lines (520 loc) · 21.8 KB
/
binary-reader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
import { readByte } from './utils/file';
import { SubArray } from './utils/array';
import { raise } from './utils/error';
import { CSCode } from './constants/error';
import { IEncoding, Encoding, IDecoder } from './encoding';
import { SeekOrigin } from './constants/mode';
import { BIG_0, BIG_7Fh, LONG_MAX, LONG_WRAP } from './constants/number';
import { IFile } from './addon/file';
type char = string;
/**@internal */
const MaxCharBytesSize = 128;
/**@internal */
const BufferSize = 16;
/**
* Reads primitive data types as binary values in a specific encoding.
*/
export class BinaryReader {
private readonly _file: IFile;
private readonly _decoder: IDecoder;
// Performance optimization for Read() w/ Unicode. Speeds us up by ~40%
private readonly _2BytesPerChar: boolean = false;
private readonly _leaveOpen: boolean = false;
private _disposed = false;
// use for peekChar
private _nReadBytes = 0;
/**
* Initializes a new instance of the BinaryReader class based on the specified IFile instance and character encoding, and optionally leaves the file open.
* @param input The input IFile instance.
* @param encoding The character encoding to use, or an object implementing the IEncoding interface. Default to `'utf8'`
* @param leaveOpen `true` to leave the file open after the BinaryReader object is disposed; otherwise, `false`. Default to `false`.
*/
constructor(input: IFile, encoding: BufferEncoding | string | IEncoding = 'utf8', leaveOpen = false) {
if (input == null || typeof input != 'object')
throw TypeError('"input" must be an object that implement the IFile interface.');
if (typeof leaveOpen != 'boolean') throw TypeError('"leaveOpen" must be a boolean.');
if (!input.canRead)
raise(ReferenceError('Input file is not readable.'), CSCode.FileNotReadable);
this._file = input;
if (typeof encoding == 'string')
this._decoder = new Encoding(encoding).getDecoder();
else if (encoding != null && typeof encoding == 'object')
this._decoder = (encoding as IEncoding).getDecoder();
else
throw TypeError('"encoding" must be a string or an instance that implements IEncoding.');
// For Encodings that always use 2 bytes per char (or more),
// special case them here to make Read() & Peek() faster.
this._2BytesPerChar = encoding == 'utf16le' || encoding == 'ucs2' || encoding == 'ucs-2';
this._leaveOpen = leaveOpen;
}
/**
* Get the underlying file instance of the BinaryReader.
*/
get file(): IFile {
return this._file;
}
/**
* Closes the current reader and the underlying file.
*/
close(): void {
if (!this._disposed) {
if (!this._leaveOpen) {
this._file.close();
}
this._disposed = true;
}
}
private throwIfDisposed(): void {
if (this._disposed) {
raise(ReferenceError('This BinaryReader instance is closed.'), CSCode.FileIsClosed);
}
}
/**
* Returns the next available character and does not advance the byte or character position.
* @returns The next available character, or -1 if no more characters are available or the file does not support seeking.
*/
peekChar(): number {
this.throwIfDisposed();
if (!this._file.canSeek) {
return -1;
}
const ch = this.readCharCode();
this._file.seek(-this._nReadBytes, SeekOrigin.Current);
return ch;
}
/**
* Reads characters from the underlying file and advances the current position of the file in accordance with the `Encoding` used and the specific character being read from the file.
* @returns The next character from the input file, or -1 if no characters are currently available.
*/
readCharCode(): number {
this.throwIfDisposed();
this._nReadBytes = 0;
let numBytes: number;
const _charBytes = Buffer.allocUnsafe(2);
// there isn't any decoder in JS world that can reuse output buffer
let singleChar = '';
while (singleChar.length == 0) {
// We really want to know what the minimum number of bytes per char
// is for our encoding. Otherwise for UnicodeEncoding we'd have to
// do ~1+log(n) reads to read n characters.
// Assume 1 byte can be 1 char unless _2BytesPerChar is true.
numBytes = this._2BytesPerChar ? 2 : 1;
let r = readByte(this._file);
_charBytes.writeUInt8(r < 0 ? r + 256 : r);
if (r == -1) {
numBytes = 0;
} else {
this._nReadBytes++;
}
if (numBytes == 2) {
r = readByte(this._file);
_charBytes.writeUInt8(r < 0 ? r + 256 : r, 1);
if (r == -1) {
numBytes = 1;
} else {
this._nReadBytes++;
}
}
if (numBytes == 0) {
return -1;
}
try {
singleChar = this._decoder.write(_charBytes.subarray(0, numBytes));
if (singleChar.length > 1)
raise(Error('BinaryReader hit a surrogate char in the read method.'), CSCode.SurrogateCharHit);
}
catch (err) {
// Handle surrogate char
if (err.code == CSCode.SurrogateCharHit && this._file.canSeek) {
this._file.seek(-this._nReadBytes, SeekOrigin.Current);
}
// else - we can't do much here
throw err;
}
}
return singleChar.charCodeAt(0);
}
/**
* Reads the next byte from the current file and advances the current position of the file by one byte.
* @returns The next byte read from the current file.
*/
readByte(): number {
return this.internalReadByte();
}
private internalReadByte(): number {
this.throwIfDisposed();
const b = readByte(this._file);
if (b == -1) {
raise(RangeError('Read beyond end-of-file.'), CSCode.ReadBeyondEndOfFile);
}
return b;
}
/**
* Reads a signed byte from this file and advances the current position of the file by one byte.
* @returns A signed byte read from the current file.
*/
readSByte(): number {
const rs = this.internalReadByte();
return rs > 127 ? rs - 256 : rs;
}
/**
* Reads a `boolean` value from the current file and advances the current position of the file by one byte.
* @returns `true` if the byte is nonzero; otherwise, `false`.
*/
readBoolean(): boolean {
return this.internalReadByte() != 0;
}
/**
* Reads the next character from the current file and advances the current position of the file in accordance with the `Encoding` used and the specific character being read from the file.
* @returns A character read from the current file.
*/
readChar(): char {
const value = this.readCharCode();
if (value == -1) {
raise(RangeError('Read beyond end-of-file.'), CSCode.ReadBeyondEndOfFile);
}
return String.fromCharCode(value);
}
/**
* Reads a 2-byte signed integer from the current file and advances the current position of the file by two bytes.
* @returns A 2-byte signed integer read from the current file.
*/
readInt16(): number {
return this.internalRead(2).readInt16LE();
}
/**
* Reads a 2-byte unsigned integer from the current file using little-endian encoding and advances the position of the file by two bytes.
* @returns A 2-byte unsigned integer read from this file.
*/
readUInt16(): number {
return this.internalRead(2).readUInt16LE();
}
/**
* Reads a 4-byte signed integer from the current file and advances the current position of the file by four bytes.
* @returns A 4-byte signed integer read from the current file.
*/
readInt32(): number {
return this.internalRead(4).readInt32LE();
}
/**
* Reads a 4-byte unsigned integer from the current file and advances the position of the file by four bytes.
* @returns A 4-byte unsigned integer read from this file.
*/
readUInt32(): number {
return this.internalRead(4).readUInt32LE();
}
/**
* Reads an 8-byte signed integer from the current file and advances the current position of the file by eight bytes.
* @returns An 8-byte signed integer read from the current file.
*/
readInt64(): bigint {
return this.internalRead(8).readBigInt64LE();
}
/**
* Reads an 8-byte unsigned integer from the current file and advances the position of the file by eight bytes.
* @returns An 8-byte unsigned integer read from this file.
*/
readUInt64(): bigint {
return this.internalRead(8).readBigUInt64LE();
}
/**
* Reads a 4-byte floating point value from the current file and advances the current position of the file by four bytes.
* @returns A 4-byte floating point value read from the current file.
*/
readSingle(): number {
return this.internalRead(4).readFloatLE();
}
/**
* Reads an 8-byte floating point value from the current file and advances the current position of the file by eight bytes.
* @returns An 8-byte floating point value read from the current file.
*/
readDouble(): number {
return this.internalRead(8).readDoubleLE();
}
/**
* Reads a string from the current file. The string is prefixed with the length, encoded as an integer seven bits at a time.
* @returns The string being read.
*/
readString(): string {
this.throwIfDisposed();
// Length of the string in bytes, not chars
const stringLength = this.read7BitEncodedInt();
return this.internalReadString(stringLength);
}
/**
* Reads a string from the current file. You have to provide the length of it.
* @param length The number of bytes to read.
* @returns The string being read.
*/
readRawString(length: number): string {
this.throwIfDisposed();
return this.internalReadString(length);
}
private internalReadString(stringLength: number): string {
if (stringLength < 0) {
raise(RangeError(`Invalid string's length: ${stringLength}.`), CSCode.InvalidEncodedStringLength);
}
if (stringLength == 0) {
return '';
}
let currPos = 0;
let n: number;
let readLength: number;
const _charBytes = Buffer.allocUnsafe(MaxCharBytesSize);
let sb = '';
do {
readLength = ((stringLength - currPos) > MaxCharBytesSize) ? MaxCharBytesSize : (stringLength - currPos);
n = this._file.read(_charBytes, 0, readLength);
if (n == 0) {
raise(RangeError('Read beyond end-of-file.'), CSCode.ReadBeyondEndOfFile);
}
const strRead = this._decoder.write(_charBytes.subarray(0, n));
if (currPos == 0 && n == stringLength) {
return strRead;
}
sb += strRead;
currPos += n;
} while (currPos < stringLength);
return sb;
}
/**
* Reads the specified number of characters from the file, starting from a specified point in the character array.
* @param buffer The buffer to read data into.
* @param index The starting point in the buffer at which to begin reading into the buffer.
* @param count The number of characters to read.
* @returns The total number of characters read into the buffer. This might be less than the number of characters requested if that many characters are not currently available, or it might be zero if the end of the file is reached.
*/
readIntoCharsEx(buffer: char[], index: number, count: number): number {
if (!Array.isArray(buffer)) throw TypeError('"buffer" must be a character array.');
if (!Number.isSafeInteger(index)) throw TypeError('"index" must be a safe integer.');
if (!Number.isSafeInteger(count)) throw TypeError('"count" must be a safe integer.');
if (index < 0)
throw RangeError('"index" need to be a non-negative number.');
if (count < 0)
throw RangeError('"count" need to be a non-negative number.');
if (buffer.length - index < count)
throw RangeError('This will read into out of buffer.');
this.throwIfDisposed();
return this.internalReadChars(SubArray.from(buffer, index, index + count));
}
/**
* Reads, from the current file, the same number of characters as the length of the provided buffer, writes them in the provided buffer, and advances the current position in accordance with the `Encoding` used and the specific character being read from the file.
* @param buffer A view of characters. When this method returns, the contents of this region are replaced by the characters read from the current source.
* @returns The total number of characters read into the buffer. This might be less than the number of characters requested if that many characters are not currently available, or it might be zero if the end of the file is reached.
*/
readIntoChars(buffer: char[]): number {
if (!Array.isArray(buffer)) throw TypeError('"buffer" must be a character array.');
this.throwIfDisposed();
return this.internalReadChars(SubArray.from(buffer));
}
private internalReadChars(buffer: SubArray<char>): number {
let totalCharsRead = 0;
while (!buffer.isEmpty) {
let numBytes = buffer.length;
// We really want to know what the minimum number of bytes per char
// is for our encoding. Otherwise for UnicodeEncoding we'd have to
// do ~1+log(n) reads to read n characters.
if (this._2BytesPerChar) {
numBytes = numBytes << 1 >>> 0;
}
// We do not want to read even a single byte more than necessary.
//
// Subtract pending bytes that the decoder may be holding onto. This assumes that each
// decoded char corresponds to one or more bytes. Note that custom encodings or encodings with
// a custom replacement sequence may violate this assumption.
if (numBytes > 1) {
// Check whether the decoder has any pending state.
if (this._decoder.hasState) {
numBytes--;
// The worst case is charsRemaining = 2 and UTF32Decoder holding onto 3 pending bytes. We need to read just
// one byte in this case.
if (this._2BytesPerChar && numBytes > 2)
numBytes -= 2;
}
}
const _charBytes = Buffer.allocUnsafe(MaxCharBytesSize);
if (numBytes > MaxCharBytesSize) {
numBytes = MaxCharBytesSize;
}
numBytes = this._file.read(_charBytes, 0, numBytes);
const byteBuffer = _charBytes.subarray(0, numBytes);
if (byteBuffer.length == 0) {
break;
}
const strRead = this._decoder.write(byteBuffer);
for (let i = 0; i < strRead.length; i++)
buffer.set(i, strRead[i]);
buffer = buffer.sub(strRead.length);
totalCharsRead += strRead.length;
}
// we may have read fewer than the number of characters requested if end of file reached
// or if the encoding makes the char count too big for the buffer (e.g. fallback sequence)
return totalCharsRead;
}
/**
* Reads the specified number of characters from the current file, returns the data in a character array, and advances the current position in accordance with the `Encoding` used and the specific character being read from the file.
* @param count The number of characters to read.
* @returns A character array containing data read from the underlying file. This might be less than the number of characters requested if the end of the file is reached.
*/
readChars(count: number): char[] {
if (!Number.isSafeInteger(count)) throw TypeError('"count" must be a safe integer.');
if (count < 0)
throw RangeError('"count" must be a non-negative value.');
this.throwIfDisposed();
if (count == 0) {
return [];
}
let chars = new Array<char>(count);
const n = this.internalReadChars(SubArray.from(chars));
if (n != count) {
chars = chars.slice(0, n);
}
return chars;
}
/**
* Reads the specified number of bytes from the file, starting from a specified point in the buffer.
* @param buffer The buffer to read data into.
* @param index The starting point in the buffer at which to begin reading into the buffer.
* @param count The number of bytes to read.
* @returns The number of bytes read into buffer. This might be less than the number of bytes requested if that many bytes are not available, or it might be zero if the end of the file is reached.
*/
readIntoBufferEx(buffer: Buffer, index: number, count: number): number {
if (!Buffer.isBuffer(buffer)) throw TypeError('"buffer" must be a Buffer.');
if (!Number.isSafeInteger(index)) throw TypeError('"index" must be a safe integer.');
if (!Number.isSafeInteger(count)) throw TypeError('"count" must be a safe integer.');
if (index < 0) {
throw RangeError('"index" must be a non-negative number.');
}
if (count < 0) {
throw RangeError('"count" must be a non-negative number.');
}
if (buffer.length - index < count) {
throw RangeError('This will read into out of buffer.');
}
this.throwIfDisposed();
return this._file.read(buffer, index, count);
}
/**
* Reads a sequence of bytes from the current file and advances the position within the file by the number of bytes read.
* @param buffer A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current source.
* @returns The total number of bytes read into the buffer. This can be less than the number of bytes allocated in the buffer if that many bytes are not currently available, or zero (0) if the end of the file has been reached.
*/
readIntoBuffer(buffer: Buffer): number {
if (!Buffer.isBuffer(buffer)) throw TypeError('"buffer" must be a Buffer.');
this.throwIfDisposed();
return this._file.read(buffer);
}
/**
* Reads the specified number of bytes from the current file into a buffer and advances the current position by that number of bytes.
* @param count The number of bytes to read. This value must be 0 or a non-negative number or an exception will occur.
* @returns A buffer containing data read from the underlying file. This might be less than the number of bytes requested if the end of the file is reached.
*/
readBytes(count: number): Buffer {
if (!Number.isSafeInteger(count)) throw TypeError('"count" must be a safe integer.');
if (count < 0) {
throw RangeError('"count" must be a non-negative number.');
}
this.throwIfDisposed();
if (count == 0) {
return Buffer.allocUnsafe(0);
}
let result = Buffer.allocUnsafe(count);
let numRead = 0;
do {
const n = this._file.read(result, numRead, count);
if (n == 0) {
break;
}
numRead += n;
count -= n;
} while (count > 0);
if (numRead != result.length) {
// Trim array. This should happen on EOF & possibly net streams.
result = result.subarray(0, numRead);
}
return result;
}
private internalRead(numBytes: number): Buffer {
this.throwIfDisposed();
const buffer = Buffer.allocUnsafe(BufferSize);
let bytesRead = 0;
do {
const n = this._file.read(buffer, bytesRead, numBytes - bytesRead);
if (n == 0) {
raise(RangeError('Read beyond end-of-file.'), CSCode.ReadBeyondEndOfFile);
}
bytesRead += n;
} while (bytesRead < numBytes);
return buffer;
}
/**
* Reads in a 32-bit integer in compressed format.
* @returns A 32-bit integer in compressed format.
*/
read7BitEncodedInt(): number {
// Unlike writing, we can't delegate to the 64-bit read on
// 64-bit platforms. The reason for this is that we want to
// stop consuming bytes if we encounter an integer overflow.
let result = 0;
let byteReadJustNow: number;
// Read the integer 7 bits at a time. The high bit
// of the byte when on means to continue reading more bytes.
//
// There are two failure cases: we've read more than 5 bytes,
// or the fifth byte is about to cause integer overflow.
// This means that we can read the first 4 bytes without
// worrying about integer overflow.
// bitwise OR operation's result is always signed, so there is no need to manually cast to signed number
const MaxBytesWithoutOverflow = 4;
for (let shift = 0; shift < MaxBytesWithoutOverflow * 7; shift += 7) { // TODO: check
// ReadByte handles end of file cases for us.
byteReadJustNow = this.readByte();
result |= (byteReadJustNow & 0x7F) << shift >>> 0;
if (byteReadJustNow <= 0x7F) {
return result; // early exit
}
}
// Read the 5th byte. Since we already read 28 bits,
// the value of this byte must fit within 4 bits (32 - 28),
// and it must not have the high bit set.
byteReadJustNow = this.readByte();
if (byteReadJustNow > 0b1111) {
raise(TypeError('Bad 7 bit encoded number in file.'), CSCode.BadEncodedIntFormat);
}
result |= byteReadJustNow << (MaxBytesWithoutOverflow * 7) >>> 0;
return result;
}
/**
* Reads in a 64-bit integer in compressed format.
* @returns A 64-bit integer in compressed format.
*/
read7BitEncodedInt64(): bigint {
let result = BIG_0;
let byteReadJustNow: number;
// Read the integer 7 bits at a time. The high bit
// of the byte when on means to continue reading more bytes.
//
// There are two failure cases: we've read more than 10 bytes,
// or the tenth byte is about to cause integer overflow.
// This means that we can read the first 9 bytes without
// worrying about integer overflow.
const MaxBytesWithoutOverflow = 9;
for (let shift = 0; shift < MaxBytesWithoutOverflow * 7; shift += 7) {
// ReadByte handles end of file cases for us.
byteReadJustNow = this.readByte();
result |= (BigInt(byteReadJustNow) & BIG_7Fh) << BigInt(shift);
if (byteReadJustNow <= 0x7F) {
return result; // early exit
}
}
// Read the 10th byte. Since we already read 63 bits,
// the value of this byte must fit within 1 bit (64 - 63),
// and it must not have the high bit set.
byteReadJustNow = this.readByte();
if (byteReadJustNow > 0b1) {
raise(TypeError('Bad 7 bit encoded number in file.'), CSCode.BadEncodedIntFormat);
}
result |= BigInt(byteReadJustNow) << BigInt(MaxBytesWithoutOverflow * 7);
return result > LONG_MAX ? result - LONG_WRAP : result;
}
}