-
Notifications
You must be signed in to change notification settings - Fork 13
/
KaitaiStream.js
842 lines (733 loc) · 24.6 KB
/
KaitaiStream.js
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
// -*- mode: js; js-indent-level: 2; -*-
'use strict';
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.KaitaiStream = factory();
}
}(this, function () {
/**
KaitaiStream is an implementation of Kaitai Struct API for JavaScript.
Based on DataStream - https://github.com/kig/DataStream.js
@param {ArrayBuffer} arrayBuffer ArrayBuffer to read from.
@param {?Number} byteOffset Offset from arrayBuffer beginning for the KaitaiStream.
*/
var KaitaiStream = function(arrayBuffer, byteOffset) {
this._byteOffset = byteOffset || 0;
if (arrayBuffer instanceof ArrayBuffer) {
this.buffer = arrayBuffer;
} else if (typeof arrayBuffer == "object") {
this.dataView = arrayBuffer;
if (byteOffset) {
this._byteOffset += byteOffset;
}
} else {
this.buffer = new ArrayBuffer(arrayBuffer || 1);
}
this.pos = 0;
this.alignToByte();
};
KaitaiStream.prototype = {};
/**
Dependency configuration data. Holds urls for (optional) dynamic loading
of code dependencies from a remote server. For use by (static) processing functions.
Caller should the supported keys to the asset urls as needed.
NOTE: `depUrls` is a static property of KaitaiStream (the factory),like the various
processing functions. It is NOT part of the prototype of instances.
@type {Object}
*/
KaitaiStream.depUrls = {
// processZlib uses this and expected a link to a copy of pako.
// specifically the pako_inflate.min.js script at:
// https://raw.githubusercontent.com/nodeca/pako/master/dist/pako_inflate.min.js
zlib: "//pako_inflate.js"
};
/**
Virtual byte length of the KaitaiStream backing buffer.
Updated to be max of original buffer size and last written size.
If dynamicSize is false is set to buffer size.
@type {number}
*/
KaitaiStream.prototype._byteLength = 0;
/**
Set/get the backing ArrayBuffer of the KaitaiStream object.
The setter updates the DataView to point to the new buffer.
@type {Object}
*/
Object.defineProperty(KaitaiStream.prototype, 'buffer',
{ get: function() {
this._trimAlloc();
return this._buffer;
},
set: function(v) {
this._buffer = v;
this._dataView = new DataView(this._buffer, this._byteOffset);
this._byteLength = this._buffer.byteLength;
} });
/**
Set/get the byteOffset of the KaitaiStream object.
The setter updates the DataView to point to the new byteOffset.
@type {number}
*/
Object.defineProperty(KaitaiStream.prototype, 'byteOffset',
{ get: function() {
return this._byteOffset;
},
set: function(v) {
this._byteOffset = v;
this._dataView = new DataView(this._buffer, this._byteOffset);
this._byteLength = this._buffer.byteLength;
} });
/**
Set/get the backing DataView of the KaitaiStream object.
The setter updates the buffer and byteOffset to point to the DataView values.
@type {Object}
*/
Object.defineProperty(KaitaiStream.prototype, 'dataView',
{ get: function() {
return this._dataView;
},
set: function(v) {
this._byteOffset = v.byteOffset;
this._buffer = v.buffer;
this._dataView = new DataView(this._buffer, this._byteOffset);
this._byteLength = this._byteOffset + v.byteLength;
} });
/**
Internal function to trim the KaitaiStream buffer when required.
Used for stripping out the extra bytes from the backing buffer when
the virtual byteLength is smaller than the buffer byteLength (happens after
growing the buffer with writes and not filling the extra space completely).
@return {null}
*/
KaitaiStream.prototype._trimAlloc = function() {
if (this._byteLength === this._buffer.byteLength) {
return;
}
var buf = new ArrayBuffer(this._byteLength);
var dst = new Uint8Array(buf);
var src = new Uint8Array(this._buffer, 0, dst.length);
dst.set(src);
this.buffer = buf;
};
// ========================================================================
// Stream positioning
// ========================================================================
/**
Returns true if the KaitaiStream seek pointer is at the end of buffer and
there's no more data to read.
@return {boolean} True if the seek pointer is at the end of the buffer.
*/
KaitaiStream.prototype.isEof = function() {
return this.pos >= this.size && this.bitsLeft === 0;
};
/**
Sets the KaitaiStream read/write position to given position.
Clamps between 0 and KaitaiStream length.
@param {number} pos Position to seek to.
@return {null}
*/
KaitaiStream.prototype.seek = function(pos) {
var npos = Math.max(0, Math.min(this.size, pos));
this.pos = (isNaN(npos) || !isFinite(npos)) ? 0 : npos;
};
/**
Returns the byte length of the KaitaiStream object.
@type {number}
*/
Object.defineProperty(KaitaiStream.prototype, 'size',
{ get: function() {
return this._byteLength - this._byteOffset;
}});
// ========================================================================
// Integer numbers
// ========================================================================
// ------------------------------------------------------------------------
// Signed
// ------------------------------------------------------------------------
/**
Reads an 8-bit signed int from the stream.
@return {number} The read number.
*/
KaitaiStream.prototype.readS1 = function() {
this.ensureBytesLeft(1);
var v = this._dataView.getInt8(this.pos);
this.pos += 1;
return v;
};
// ........................................................................
// Big-endian
// ........................................................................
/**
Reads a 16-bit big-endian signed int from the stream.
@return {number} The read number.
*/
KaitaiStream.prototype.readS2be = function() {
this.ensureBytesLeft(2);
var v = this._dataView.getInt16(this.pos);
this.pos += 2;
return v;
};
/**
Reads a 32-bit big-endian signed int from the stream.
@return {number} The read number.
*/
KaitaiStream.prototype.readS4be = function() {
this.ensureBytesLeft(4);
var v = this._dataView.getInt32(this.pos);
this.pos += 4;
return v;
};
/**
Reads a 64-bit big-endian unsigned int from the stream. Note that
JavaScript does not support 64-bit integers natively, so it will
automatically upgrade internal representation to use IEEE 754
double precision float.
@return {number} The read number.
*/
KaitaiStream.prototype.readS8be = function() {
this.ensureBytesLeft(8);
var v1 = this.readU4be();
var v2 = this.readU4be();
if ((v1 & 0x80000000) !== 0) {
// negative number
return -(0x100000000 * (v1 ^ 0xffffffff) + (v2 ^ 0xffffffff)) - 1;
} else {
return 0x100000000 * v1 + v2;
}
};
// ........................................................................
// Little-endian
// ........................................................................
/**
Reads a 16-bit little-endian signed int from the stream.
@return {number} The read number.
*/
KaitaiStream.prototype.readS2le = function() {
this.ensureBytesLeft(2);
var v = this._dataView.getInt16(this.pos, true);
this.pos += 2;
return v;
};
/**
Reads a 32-bit little-endian signed int from the stream.
@return {number} The read number.
*/
KaitaiStream.prototype.readS4le = function() {
this.ensureBytesLeft(4);
var v = this._dataView.getInt32(this.pos, true);
this.pos += 4;
return v;
};
/**
Reads a 64-bit little-endian unsigned int from the stream. Note that
JavaScript does not support 64-bit integers natively, so it will
automatically upgrade internal representation to use IEEE 754
double precision float.
@return {number} The read number.
*/
KaitaiStream.prototype.readS8le = function() {
this.ensureBytesLeft(8);
var v1 = this.readU4le();
var v2 = this.readU4le();
if ((v2 & 0x80000000) !== 0) {
// negative number
return -(0x100000000 * (v2 ^ 0xffffffff) + (v1 ^ 0xffffffff)) - 1;
} else {
return 0x100000000 * v2 + v1;
}
};
// ------------------------------------------------------------------------
// Unsigned
// ------------------------------------------------------------------------
/**
Reads an 8-bit unsigned int from the stream.
@return {number} The read number.
*/
KaitaiStream.prototype.readU1 = function() {
this.ensureBytesLeft(1);
var v = this._dataView.getUint8(this.pos);
this.pos += 1;
return v;
};
// ........................................................................
// Big-endian
// ........................................................................
/**
Reads a 16-bit big-endian unsigned int from the stream.
@return {number} The read number.
*/
KaitaiStream.prototype.readU2be = function() {
this.ensureBytesLeft(2);
var v = this._dataView.getUint16(this.pos);
this.pos += 2;
return v;
};
/**
Reads a 32-bit big-endian unsigned int from the stream.
@return {number} The read number.
*/
KaitaiStream.prototype.readU4be = function() {
this.ensureBytesLeft(4);
var v = this._dataView.getUint32(this.pos);
this.pos += 4;
return v;
};
/**
Reads a 64-bit big-endian unsigned int from the stream. Note that
JavaScript does not support 64-bit integers natively, so it will
automatically upgrade internal representation to use IEEE 754
double precision float.
@return {number} The read number.
*/
KaitaiStream.prototype.readU8be = function() {
this.ensureBytesLeft(8);
var v1 = this.readU4be();
var v2 = this.readU4be();
return 0x100000000 * v1 + v2;
};
// ........................................................................
// Little-endian
// ........................................................................
/**
Reads a 16-bit little-endian unsigned int from the stream.
@return {number} The read number.
*/
KaitaiStream.prototype.readU2le = function() {
this.ensureBytesLeft(2);
var v = this._dataView.getUint16(this.pos, true);
this.pos += 2;
return v;
};
/**
Reads a 32-bit little-endian unsigned int from the stream.
@return {number} The read number.
*/
KaitaiStream.prototype.readU4le = function() {
this.ensureBytesLeft(4);
var v = this._dataView.getUint32(this.pos, true);
this.pos += 4;
return v;
};
/**
Reads a 64-bit little-endian unsigned int from the stream. Note that
JavaScript does not support 64-bit integers natively, so it will
automatically upgrade internal representation to use IEEE 754
double precision float.
@return {number} The read number.
*/
KaitaiStream.prototype.readU8le = function() {
this.ensureBytesLeft(8);
var v1 = this.readU4le();
var v2 = this.readU4le();
return 0x100000000 * v2 + v1;
};
// ========================================================================
// Floating point numbers
// ========================================================================
// ------------------------------------------------------------------------
// Big endian
// ------------------------------------------------------------------------
KaitaiStream.prototype.readF4be = function() {
this.ensureBytesLeft(4);
var v = this._dataView.getFloat32(this.pos);
this.pos += 4;
return v;
};
KaitaiStream.prototype.readF8be = function() {
this.ensureBytesLeft(8);
var v = this._dataView.getFloat64(this.pos);
this.pos += 8;
return v;
};
// ------------------------------------------------------------------------
// Little endian
// ------------------------------------------------------------------------
KaitaiStream.prototype.readF4le = function() {
this.ensureBytesLeft(4);
var v = this._dataView.getFloat32(this.pos, true);
this.pos += 4;
return v;
};
KaitaiStream.prototype.readF8le = function() {
this.ensureBytesLeft(8);
var v = this._dataView.getFloat64(this.pos, true);
this.pos += 8;
return v;
};
// ------------------------------------------------------------------------
// Unaligned bit values
// ------------------------------------------------------------------------
KaitaiStream.prototype.alignToByte = function() {
this.bits = 0;
this.bitsLeft = 0;
};
KaitaiStream.prototype.readBitsIntBe = function(n) {
// JS only supports bit operations on 32 bits
if (n > 32) {
throw new Error("readBitsIntBe: the maximum supported bit length is 32");
}
var bitsNeeded = n - this.bitsLeft;
if (bitsNeeded > 0) {
// 1 bit => 1 byte
// 8 bits => 1 byte
// 9 bits => 2 bytes
var bytesNeeded = Math.ceil(bitsNeeded / 8);
var buf = this.readBytes(bytesNeeded);
for (var i = 0; i < bytesNeeded; i++) {
this.bits <<= 8;
this.bits |= buf[i];
this.bitsLeft += 8;
}
}
// raw mask with required number of 1s, starting from lowest bit
var mask = n === 32 ? 0xffffffff : (1 << n) - 1;
// shift this.bits to align the highest bits with the mask & derive reading result
var shiftBits = this.bitsLeft - n;
var res = (this.bits >>> shiftBits) & mask;
// clear top bits that we've just read => AND with 1s
this.bitsLeft -= n;
mask = (1 << this.bitsLeft) - 1;
this.bits &= mask;
return res;
};
/**
* Unused since Kaitai Struct Compiler v0.9+ - compatibility with older versions
*
* @deprecated use {@link readBitsIntBe} instead
*/
KaitaiStream.prototype.readBitsInt = KaitaiStream.prototype.readBitsIntBe;
KaitaiStream.prototype.readBitsIntLe = function(n) {
// JS only supports bit operations on 32 bits
if (n > 32) {
throw new Error("readBitsIntLe: the maximum supported bit length is 32");
}
var bitsNeeded = n - this.bitsLeft;
if (bitsNeeded > 0) {
// 1 bit => 1 byte
// 8 bits => 1 byte
// 9 bits => 2 bytes
var bytesNeeded = Math.ceil(bitsNeeded / 8);
var buf = this.readBytes(bytesNeeded);
for (var i = 0; i < bytesNeeded; i++) {
this.bits |= (buf[i] << this.bitsLeft);
this.bitsLeft += 8;
}
}
// raw mask with required number of 1s, starting from lowest bit
var mask = n === 32 ? 0xffffffff : (1 << n) - 1;
// derive reading result
var res = this.bits & mask;
// remove bottom bits that we've just read by shifting
this.bits >>= n;
this.bitsLeft -= n;
return res;
};
/**
Native endianness. Either KaitaiStream.BIG_ENDIAN or KaitaiStream.LITTLE_ENDIAN
depending on the platform endianness.
@type {boolean}
*/
KaitaiStream.endianness = new Int8Array(new Int16Array([1]).buffer)[0] > 0;
// ========================================================================
// Byte arrays
// ========================================================================
KaitaiStream.prototype.readBytes = function(len) {
return this.mapUint8Array(len);
};
KaitaiStream.prototype.readBytesFull = function() {
return this.mapUint8Array(this.size - this.pos);
};
KaitaiStream.prototype.readBytesTerm = function(terminator, include, consume, eosError) {
var blen = this.size - this.pos;
var u8 = new Uint8Array(this._buffer, this._byteOffset + this.pos);
for (var i = 0; i < blen && u8[i] !== terminator; i++); // find first zero byte
if (i === blen) {
// we've read all the buffer and haven't found the terminator
if (eosError) {
throw "End of stream reached, but no terminator " + terminator + " found";
} else {
return this.mapUint8Array(i);
}
} else {
var arr;
if (include) {
arr = this.mapUint8Array(i + 1);
} else {
arr = this.mapUint8Array(i);
}
if (consume) {
this.pos += 1;
}
return arr;
}
};
// Unused since Kaitai Struct Compiler v0.9+ - compatibility with older versions
KaitaiStream.prototype.ensureFixedContents = function(expected) {
var actual = this.readBytes(expected.length);
if (actual.length !== expected.length) {
throw new UnexpectedDataError(expected, actual);
}
var actLen = actual.length;
for (var i = 0; i < actLen; i++) {
if (actual[i] !== expected[i]) {
throw new UnexpectedDataError(expected, actual);
}
}
return actual;
};
KaitaiStream.bytesStripRight = function(data, padByte) {
var newLen = data.length;
while (data[newLen - 1] === padByte)
newLen--;
return data.slice(0, newLen);
};
KaitaiStream.bytesTerminate = function(data, term, include) {
var newLen = 0;
var maxLen = data.length;
while (newLen < maxLen && data[newLen] !== term)
newLen++;
if (include && newLen < maxLen)
newLen++;
return data.slice(0, newLen);
};
KaitaiStream.bytesToStr = function(arr, encoding) {
if (encoding == null || encoding.toLowerCase() === "ascii") {
return KaitaiStream.createStringFromArray(arr);
} else {
if (typeof TextDecoder === 'function') {
// we're in the browser that supports TextDecoder
return (new TextDecoder(encoding)).decode(arr);
} else {
// probably we're in node.js
// check if it's supported natively by node.js Buffer
// see https://github.com/nodejs/node/blob/master/lib/buffer.js#L187 for details
switch (encoding.toLowerCase()) {
case 'utf8':
case 'utf-8':
case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
return new Buffer(arr).toString(encoding);
break;
default:
// unsupported encoding, we'll have to resort to iconv-lite
if (typeof KaitaiStream.iconvlite === 'undefined')
KaitaiStream.iconvlite = require('iconv-lite');
return KaitaiStream.iconvlite.decode(arr, encoding);
}
}
}
};
// ========================================================================
// Byte array processing
// ========================================================================
KaitaiStream.processXorOne = function(data, key) {
var r = new Uint8Array(data.length);
var dl = data.length;
for (var i = 0; i < dl; i++)
r[i] = data[i] ^ key;
return r;
};
KaitaiStream.processXorMany = function(data, key) {
var dl = data.length;
var r = new Uint8Array(dl);
var kl = key.length;
var ki = 0;
for (var i = 0; i < dl; i++) {
r[i] = data[i] ^ key[ki];
ki++;
if (ki >= kl)
ki = 0;
}
return r;
};
KaitaiStream.processRotateLeft = function(data, amount, groupSize) {
if (groupSize !== 1)
throw("unable to rotate group of " + groupSize + " bytes yet");
var mask = groupSize * 8 - 1;
var antiAmount = -amount & mask;
var r = new Uint8Array(data.length);
for (var i = 0; i < data.length; i++)
r[i] = (data[i] << amount) & 0xff | (data[i] >> antiAmount);
return r;
};
KaitaiStream.processZlib = function(buf) {
if (typeof require !== 'undefined') {
// require is available - we're running under node
if (typeof KaitaiStream.zlib === 'undefined')
KaitaiStream.zlib = require('zlib');
// use node's zlib module API
var r = KaitaiStream.zlib.inflateSync(
Buffer.from(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength))
);
return r;
} else {
// no require() - assume we're running as a web worker in browser.
// user should have configured KaitaiStream.depUrls.zlib, if not
// we'll throw.
if (typeof KaitaiStream.zlib === 'undefined'
&& typeof KaitaiStream.depUrls.zlib !== 'undefined') {
importScripts(KaitaiStream.depUrls.zlib);
KaitaiStream.zlib = pako;
}
// use pako API
r = KaitaiStream.zlib.inflate(buf);
return r;
}
};
// ========================================================================
// Misc runtime operations
// ========================================================================
KaitaiStream.mod = function(a, b) {
if (b <= 0)
throw "mod divisor <= 0";
var r = a % b;
if (r < 0)
r += b;
return r;
};
KaitaiStream.arrayMin = function(arr) {
var min = arr[0];
var x;
for (var i = 1, n = arr.length; i < n; ++i) {
x = arr[i];
if (x < min) min = x;
}
return min;
};
KaitaiStream.arrayMax = function(arr) {
var max = arr[0];
var x;
for (var i = 1, n = arr.length; i < n; ++i) {
x = arr[i];
if (x > max) max = x;
}
return max;
};
KaitaiStream.byteArrayCompare = function(a, b) {
if (a === b)
return 0;
var al = a.length;
var bl = b.length;
var minLen = al < bl ? al : bl;
for (var i = 0; i < minLen; i++) {
var cmp = a[i] - b[i];
if (cmp !== 0)
return cmp;
}
// Reached the end of at least one of the arrays
if (al === bl) {
return 0;
} else {
return al - bl;
}
};
// ========================================================================
// Internal implementation details
// ========================================================================
var EOFError = KaitaiStream.EOFError = function(bytesReq, bytesAvail) {
this.name = "EOFError";
this.message = "requested " + bytesReq + " bytes, but only " + bytesAvail + " bytes available";
this.bytesReq = bytesReq;
this.bytesAvail = bytesAvail;
this.stack = (new Error()).stack;
};
EOFError.prototype = Object.create(Error.prototype);
EOFError.prototype.constructor = EOFError;
// Unused since Kaitai Struct Compiler v0.9+ - compatibility with older versions
var UnexpectedDataError = KaitaiStream.UnexpectedDataError = function(expected, actual) {
this.name = "UnexpectedDataError";
this.message = "expected [" + expected + "], but got [" + actual + "]";
this.expected = expected;
this.actual = actual;
this.stack = (new Error()).stack;
};
UnexpectedDataError.prototype = Object.create(Error.prototype);
UnexpectedDataError.prototype.constructor = UnexpectedDataError;
var UndecidedEndiannessError = KaitaiStream.UndecidedEndiannessError = function() {
this.name = "UndecidedEndiannessError";
this.stack = (new Error()).stack;
};
UndecidedEndiannessError.prototype = Object.create(Error.prototype);
UndecidedEndiannessError.prototype.constructor = UndecidedEndiannessError;
var ValidationNotEqualError = KaitaiStream.ValidationNotEqualError = function(expected, actual) {
this.name = "ValidationNotEqualError";
this.message = "not equal, expected [" + expected + "], but got [" + actual + "]";
this.expected = expected;
this.actual = actual;
this.stack = (new Error()).stack;
};
ValidationNotEqualError.prototype = Object.create(Error.prototype);
ValidationNotEqualError.prototype.constructor = ValidationNotEqualError;
var ValidationLessThanError = KaitaiStream.ValidationLessThanError = function(min, actual) {
this.name = "ValidationLessThanError";
this.message = "not in range, min [" + min + "], but got [" + actual + "]";
this.min = min;
this.actual = actual;
this.stack = (new Error()).stack;
};
ValidationLessThanError.prototype = Object.create(Error.prototype);
ValidationLessThanError.prototype.constructor = ValidationLessThanError;
var ValidationGreaterThanError = KaitaiStream.ValidationGreaterThanError = function(max, actual) {
this.name = "ValidationGreaterThanError";
this.message = "not in range, max [" + max + "], but got [" + actual + "]";
this.max = max;
this.actual = actual;
this.stack = (new Error()).stack;
};
ValidationGreaterThanError.prototype = Object.create(Error.prototype);
ValidationGreaterThanError.prototype.constructor = ValidationGreaterThanError;
var ValidationNotAnyOfError = KaitaiStream.ValidationNotAnyOfError = function(actual, io, srcPath) {
this.name = "ValidationNotAnyOfError";
this.message = "not any of the list, got [" + actual + "]";
this.actual = actual;
this.stack = (new Error()).stack;
};
ValidationNotAnyOfError.prototype = Object.create(Error.prototype);
ValidationNotAnyOfError.prototype.constructor = ValidationNotAnyOfError;
/**
Ensures that we have an least `length` bytes left in the stream.
If that's not true, throws an EOFError.
@param {number} length Number of bytes to require
*/
KaitaiStream.prototype.ensureBytesLeft = function(length) {
if (this.pos + length > this.size) {
throw new EOFError(length, this.size - this.pos);
}
};
/**
Maps a Uint8Array into the KaitaiStream buffer.
Nice for quickly reading in data.
@param {number} length Number of elements to map.
@return {Object} Uint8Array to the KaitaiStream backing buffer.
*/
KaitaiStream.prototype.mapUint8Array = function(length) {
length |= 0;
this.ensureBytesLeft(length);
var arr = new Uint8Array(this._buffer, this.byteOffset + this.pos, length);
this.pos += length;
return arr;
};
/**
Creates an array from an array of character codes.
Uses String.fromCharCode in chunks for memory efficiency and then concatenates
the resulting string chunks.
@param {array|Uint8Array} array Array of character codes.
@return {string} String created from the character codes.
**/
KaitaiStream.createStringFromArray = function(array) {
var chunk_size = 0x8000;
var chunks = [];
var useSubarray = typeof array.subarray === 'function';
for (var i=0; i < array.length; i += chunk_size) {
chunks.push(String.fromCharCode.apply(null, useSubarray ? array.subarray(i, i + chunk_size) : array.slice(i, i + chunk_size)));
}
return chunks.join("");
};
return KaitaiStream;
}));