UNPKG

kaitai-struct

Version:
509 lines (508 loc) 17 kB
interface IconvLite { decode(buffer: Buffer | Uint8Array, encoding: string): string; } interface Zlib { inflateSync(buf: ArrayBuffer | NodeJS.ArrayBufferView): Buffer; } interface Pako { inflate(data: Uint8Array | number[]): Uint8Array; } declare global { interface CallableFunction { apply<T, A, R>(this: (this: T, ...args: A[]) => R, thisArg: T, args: ArrayLike<A>): R; } } /** * KaitaiStream is an implementation of Kaitai Struct API for JavaScript. * Based on DataStream - https://github.com/kig/DataStream.js . */ declare class KaitaiStream { /** * @param arrayBuffer ArrayBuffer to read from. * @param byteOffset Offset from arrayBuffer beginning for the KaitaiStream. */ constructor(arrayBuffer: ArrayBuffer | DataView<ArrayBuffer> | number, byteOffset?: number); /** * 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. */ private _byteLength; private _byteOffset; private _buffer; private _dataView; pos: number; bits: number; bitsLeft: number; /** * 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. */ static depUrls: Record<string, string | undefined>; static iconvlite?: IconvLite; static zlib?: Pako | Zlib; /** * Gets the backing ArrayBuffer of the KaitaiStream object. * * @returns The backing ArrayBuffer. */ get buffer(): ArrayBuffer; /** * Sets the backing ArrayBuffer of the KaitaiStream object and updates the * DataView to point to the new buffer. */ set buffer(v: ArrayBuffer); /** * Gets the byteOffset of the KaitaiStream object. * * @returns The byteOffset. */ get byteOffset(): number; /** * Sets the byteOffset of the KaitaiStream object and updates the DataView to * point to the new byteOffset. */ set byteOffset(v: number); /** * Gets the backing DataView of the KaitaiStream object. * * @returns The backing DataView. */ get dataView(): DataView<ArrayBuffer>; /** * Sets the backing DataView of the KaitaiStream object and updates the buffer * and byteOffset to point to the DataView values. */ set dataView(v: DataView<ArrayBuffer>); /** * 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). */ private _trimAlloc; /** * Returns true if the KaitaiStream seek pointer is at the end of buffer and * there's no more data to read. * * @returns True if the seek pointer is at the end of the buffer. */ isEof(): boolean; /** * Sets the KaitaiStream read/write position to given position. * Clamps between 0 and KaitaiStream length. * * @param pos Position to seek to. */ seek(pos: number): void; /** * Returns the byte length of the KaitaiStream object. * * @returns The byte length. */ get size(): number; /** * Reads an 8-bit signed int from the stream. * * @returns The read number. */ readS1(): number; /** * Reads a 16-bit big-endian signed int from the stream. * * @returns The read number. */ readS2be(): number; /** * Reads a 32-bit big-endian signed int from the stream. * * @returns The read number. */ readS4be(): number; /** * 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. * * @returns The read number. */ readS8be(): number; /** * Reads a 16-bit little-endian signed int from the stream. * * @returns The read number. */ readS2le(): number; /** * Reads a 32-bit little-endian signed int from the stream. * * @returns The read number. */ readS4le(): number; /** * 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. * * @returns The read number. */ readS8le(): number; /** * Reads an 8-bit unsigned int from the stream. * * @returns The read number. */ readU1(): number; /** * Reads a 16-bit big-endian unsigned int from the stream. * * @returns The read number. */ readU2be(): number; /** * Reads a 32-bit big-endian unsigned int from the stream. * * @returns The read number. */ readU4be(): number; /** * 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. * * @returns The read number. */ readU8be(): number; /** * Reads a 16-bit little-endian unsigned int from the stream. * * @returns The read number. */ readU2le(): number; /** * Reads a 32-bit little-endian unsigned int from the stream. * * @returns The read number. */ readU4le(): number; /** * 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. * * @returns The read number. */ readU8le(): number; /** * Reads a 32-bit big-endian float from the stream. * * @returns The read number. */ readF4be(): number; /** * Reads a 64-bit big-endian float from the stream. * * @returns The read number. */ readF8be(): number; /** * Reads a 32-bit little-endian float from the stream. * * @returns The read number. */ readF4le(): number; /** * Reads a 64-bit little-endian float from the stream. * * @returns The read number. */ readF8le(): number; /** * Aligns the stream position to the next byte boundary. */ alignToByte(): void; /** * @param n The number of bits to read. * @returns The read bits. * @throws {RangeError} */ readBitsIntBe(n: number): number; /** * Unused since Kaitai Struct Compiler v0.9+ - compatibility with older versions. * * @deprecated Use {@link readBitsIntBe} instead. * @param n The number of bits to read. * @returns The read bits. */ readBitsInt(n: number): number; /** * @param n The number of bits to read. * @returns The read bits. * @throws {RangeError} */ readBitsIntLe(n: number): number; /** * Native endianness. Either KaitaiStream.BIG_ENDIAN or KaitaiStream.LITTLE_ENDIAN * depending on the platform endianness. */ static endianness: boolean; /** * @param len The number of bytes to read. * @returns The read bytes. */ readBytes(len: number): Uint8Array; /** * @returns The read bytes. */ readBytesFull(): Uint8Array; /** * Reads bytes until the terminator byte is found. * * @param terminator The terminator byte. * @param include True if the terminator should be included with the returned bytes. * @param consume True if the terminator should be consumed from the input stream. * @param eosError True to throw an error if the end of stream is reached. * @returns The read bytes. * @throws {string} */ readBytesTerm(terminator: number, include: boolean, consume: boolean, eosError: boolean): Uint8Array; /** * Reads bytes until the terminator byte sequence is found. * * @param terminator The terminator byte sequence. * @param include True if the terminator should be included with the returned bytes. * @param consume True if the terminator should be consumed from the input stream. * @param eosError True to throw an error if the end of stream is reached. * @returns The read bytes. * @throws {string} */ readBytesTermMulti(terminator: Uint8Array, include: boolean, consume: boolean, eosError: boolean): Uint8Array; /** * Unused since Kaitai Struct Compiler v0.9+ - compatibility with older versions. * * @param expected The expected bytes. * @returns The read bytes. * @throws {KaitaiStream.UnexpectedDataError} */ ensureFixedContents(expected: ArrayLike<number>): Uint8Array; /** * @param data The data. * @param padByte The byte to strip. * @returns The stripped data. */ static bytesStripRight(data: Uint8Array, padByte: number): Uint8Array; /** * @param data The data. * @param term The terminator. * @param include True if the returned bytes should include the terminator. * @returns The terminated bytes. */ static bytesTerminate(data: Uint8Array, term: number, include: boolean): Uint8Array; /** * @param data The data. * @param term The terminator. * @param include True if the returned bytes should include the terminator. * @returns The terminated bytes. */ static bytesTerminateMulti(data: Uint8Array, term: Uint8Array, include: boolean): Uint8Array; /** * @param arr The bytes. * @param encoding The character encoding. * @returns The decoded string. */ static bytesToStr(arr: Uint8Array, encoding: string): string; /** * @param data The input bytes. * @param key The key byte. * @returns The Xor'd bytes. */ static processXorOne(data: Uint8Array, key: number): Uint8Array; /** * @param data The input bytes. * @param key The key bytes. * @returns The Xor'd bytes. */ static processXorMany(data: Uint8Array, key: Uint8Array): Uint8Array; /** * @param data The input bytes. * @param amount The shift amount in bits. * @param groupSize The number of bytes in each group. * @returns The rotated bytes. * @throws {string} */ static processRotateLeft(data: Uint8Array, amount: number, groupSize: number): Uint8Array; /** * @param buf The input bytes. * @returns The uncompressed bytes. */ static processZlib(buf: Uint8Array): Uint8Array; /** * @param a The dividend. * @param b The divisor. * @returns The result of `a` mod `b`. * @throws {string} */ static mod(a: number, b: number): number; /** * Gets the smallest value in an array. * * @param arr The input array. * @returns The smallest value. */ static arrayMin(arr: ArrayLike<number>): number; /** * Gets the largest value in an array. * * @param arr The input array. * @returns The largest value. */ static arrayMax(arr: ArrayLike<number>): number; /** * Compares two arrays of bytes from left to right. * * @param a The first array. * @param b The second array. * @returns `0` if the arrays are the equal, a positive number if `a` is greater than `b`, or a negative number if `a` is less than `b`. */ static byteArrayCompare(a: Uint8Array, b: Uint8Array): number; static EOFError: { new (bytesReq: number, bytesAvail: number): { name: string; bytesReq: number; bytesAvail: number; message: string; stack?: string; }; captureStackTrace(targetObject: object, constructorOpt?: Function): void; prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined; stackTraceLimit: number; }; /** * Unused since Kaitai Struct Compiler v0.9+ - compatibility with older versions. */ static UnexpectedDataError: { new (expected: any, actual: any): { name: string; expected: any; actual: any; message: string; stack?: string; }; captureStackTrace(targetObject: object, constructorOpt?: Function): void; prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined; stackTraceLimit: number; }; static UndecidedEndiannessError: { new (): { name: string; message: string; stack?: string; }; captureStackTrace(targetObject: object, constructorOpt?: Function): void; prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined; stackTraceLimit: number; }; static ValidationNotEqualError: { new (expected: any, actual: any): { name: string; expected: any; actual: any; message: string; stack?: string; }; captureStackTrace(targetObject: object, constructorOpt?: Function): void; prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined; stackTraceLimit: number; }; static ValidationLessThanError: { new (min: any, actual: any): { name: string; min: any; actual: any; message: string; stack?: string; }; captureStackTrace(targetObject: object, constructorOpt?: Function): void; prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined; stackTraceLimit: number; }; static ValidationGreaterThanError: { new (max: any, actual: any): { name: string; max: any; actual: any; message: string; stack?: string; }; captureStackTrace(targetObject: object, constructorOpt?: Function): void; prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined; stackTraceLimit: number; }; static ValidationNotAnyOfError: { new (actual: any): { name: string; actual: any; message: string; stack?: string; }; captureStackTrace(targetObject: object, constructorOpt?: Function): void; prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined; stackTraceLimit: number; }; static ValidationNotInEnumError: { new (actual: any): { name: string; actual: any; message: string; stack?: string; }; captureStackTrace(targetObject: object, constructorOpt?: Function): void; prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined; stackTraceLimit: number; }; static ValidationExprError: { new (actual: any): { name: string; actual: any; message: string; stack?: string; }; captureStackTrace(targetObject: object, constructorOpt?: Function): void; prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined; stackTraceLimit: number; }; /** * Ensures that we have at least `length` bytes left in the stream. * If not, throws an EOFError. * * @param length Number of bytes to require. * @throws {KaitaiStream.EOFError} */ ensureBytesLeft(length: number): void; /** * Maps a Uint8Array into the KaitaiStream buffer. * Nice for quickly reading in data. * * @param length Number of elements to map. * @returns A Uint8Array to the KaitaiStream backing buffer. */ mapUint8Array(length: number): Uint8Array; /** * 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 Array of character codes. * @returns String created from the character codes. */ static createStringFromArray(array: Uint8Array): string; } export default KaitaiStream;