/// import { IFileAbstraction } from "./fileAbstraction"; import { IStream } from "./stream"; /** * @summary Specifies the text encoding used when converting betweenInclusive a string and a * {@link ByteVector}. * @remarks * This enumeration is used by {@link ByteVector.fromString} and * {@link ByteVector.toString} */ export declare enum StringType { /** * @summary The string is to be Latin-1 encoded. */ Latin1 = 0, /** * @summary The string is to be UTF-16 encoded. */ UTF16 = 1, /** * @summary The string is to be UTF-16BE encoded. */ UTF16BE = 2, /** * @summary The string is to be UTF-8 encoded. */ UTF8 = 3, /** * @summary The string is to be UTF-16LE encoded. */ UTF16LE = 4, /** * @summary The string is to be encoded as a hex string for each byte (eg, 0x00, 0x12, 0xAF). * Intended to be used for debugging purposes, only. */ Hex = 5 } /** * Wrapper around the `iconv-lite` library to provide string encoding and decoding functionality. */ export declare class Encoding { private static readonly HEX_ENCODING_KEY; private static readonly ENCODINGS; /** * Contains the last generic UTF16 encoding read. Defaults to UTF16-LE */ private static _lastUtf16Encoding; private readonly _encoding; private constructor(); /** * Gets the appropriate encoding instance for encoding and decoding strings, based on the * provided `type`. * @param type Type of string to get an {@link Encoding} class instance for * @param bom Optional, the byte order marker for the string. Used to determine UTF16 endianess */ static getEncoding(type: StringType, bom?: ByteVector): Encoding; decode(data: Uint8Array): string; encode(text: string): Uint8Array; } /** * Wrapper around a `Uint8Array` that provides functionality for reading and writing byte arrays. * @remarks * Implementation of this class uses a single `Uint8Array` to store bytes. Due to * `Uint8Array`s being fixed length, any operation that inserts or removes values into the * instance will result in a copy of the internal array being made. If multiple additions will * be made, rather than using multiple inserts/adds, the {@link ByteVector.concatenate} method * is provided to group additions/inserts and therefore improve performance. * * The original .NET implementation had a ubiquitous `mid` method that would return a subset * of the bytes in the current instance. In versions <5 of the node port, `mid` would make a * copy of the subset of the bytes. Since this was frequently done right before reading a * number or string, this copy was extremely wasteful. In version 5, the `mid` method was * replaced with `subarray` which behaves identically to `Uint8Array.subarray` and returns * an instance that is a 'view' of an existing instance - no copying involved. However, all * write operations make copies, instances that are backed by 'views' may waste memory by * referencing a `Uint8Array` that is much larger than the view. * * With this in mind, best practices for using `ByteVectors`: * * Calling {@link ByteVector.subarray} is cheap, use it when possible * * If storing a subset of a `ByteVector`, store a copy with {@link ByteVector.toByteVector} * * If building a `ByteVector`, use {@link ByteVector.concatenate} when possible * * If the instance should be immutable, use {@link ByteVector.makeReadOnly} */ export declare class ByteVector { private static readonly CRC_TABLE; /** * Contains a one byte text delimiter */ private static readonly TD1; /** * Contains a two byte text delimiter */ private static readonly TD2; private _bytes; private _isReadOnly; private constructor(); /** * Creates a {@link ByteVector} from a collection of bytes, byte arrays, and byte vectors. This * method is better to use when a known quantity of byte vectors will be concatenated together, * since doing multiple calls to {@link ByteVector.addByteVector} results in the entire byte * vector being copied for each call. * @param vectors ByteVectors, byte arrays, or straight bytes to concatenate together into a * new {@link ByteVector} * @returns * Single byte vector with the contents of the byte vectors in `vectors` concatenated * together */ static concatenate(...vectors: Array): ByteVector; /** * Creates an empty {@link ByteVector} */ static empty(): ByteVector; /** * Creates a {@link ByteVector} from a base64 string. * @param str Base64 string to convert into a byte vector */ static fromBase64String(str: string): ByteVector; /** * Creates a {@link ByteVector} from a `Uint8Array` or `Buffer` * @param bytes Uint8Array of the bytes to put in the ByteVector * @param length Optionally, number of bytes to read. If this is not provided, it will default * to the full length of `bytes`. If it is less than the length of `bytes`, `bytes` will be * copied into the {@link ByteVector}. */ static fromByteArray(bytes: Uint8Array | Buffer | number[], length?: number): ByteVector; /** * Creates a new instance by reading in the contents of a specified file abstraction. * @param abstraction File abstraction to read */ static fromFileAbstraction(abstraction: IFileAbstraction): ByteVector; /** * Creates a 4 byte {@link ByteVector} with a signed 32-bit integer as the data * @param value Signed 32-bit integer to use as the data. * @param isBigEndian If `true`, `value` will be stored in big endian format. If `false`, * `value` will be stored in little endian format */ static fromInt(value: number, isBigEndian?: boolean): ByteVector; /** * Creates a ByteVector using the contents of an TagLibSharp-node stream as the contents. This * method reads from the current offset of the stream, not the beginning of the stream * @param stream TagLibSharp-node internal stream object */ static fromInternalStream(stream: IStream): ByteVector; /** * Creates an 8 byte {@link ByteVector} with a signed 64-bit integer as the data * @param value Signed 64-bit integer to use as the data. If using a `bigint`, it must fit * within 8 bytes. If using a `number`, it must be a safe integer. * @param isBigEndian If `true`, `value` will be stored in big endian format. If `false`, * `value` will be stored in little endian format */ static fromLong(value: bigint | number, isBigEndian?: boolean): ByteVector; /** * Creates a 1 byte {@link ByteVector} with an unsigned 8-bit integer as the data * @param value Unsigned 8-bit integer to use as the data. */ static fromByte(value: number): ByteVector; /** * Creates a {@link ByteVector} using the contents of a file as the data * @param path Path to the file to store in the ByteVector */ static fromPath(path: string): ByteVector; /** * Creates a 2 byte {@link ByteVector} with a signed 16-bit integer as the data * @param value Signed 16-bit integer to use as the data. * @param isBigEndian If `true`, `value` will be stored in big endian format. If `false`, * `value` will be stored in little endian format */ static fromShort(value: number, isBigEndian?: boolean): ByteVector; /** * Creates a {@link ByteVector} of a given length with a given value for all the elements * @param size Length of the ByteVector. Must be a positive safe integer * @param fill Byte value to initialize all elements to. Must be a positive 8-bit integer */ static fromSize(size: number, fill?: number): ByteVector; /** * Creates {@link ByteVector} with the contents of a stream as the data. The stream will be read * to the end before the ByteVector is returned. * @param readStream Readable stream that will be read in entirety. */ static fromStream(readStream: NodeJS.ReadableStream): Promise; /** * Creates {@link ByteVector} with the byte representation of a string as the data. * @param text String to store in the ByteVector * @param type StringType to use to encode the string. If {@link StringType.UTF16} is used, the * string will be encoded as UTF16-LE. * @param length Number of characters from the string to store in the ByteVector. Must be a * positive 32-bit integer. */ static fromString(text: string, type: StringType, length?: number): ByteVector; /** * Creates a 4 byte {@link ByteVector} with a positive 32-bit integer as the data * @param value Positive 32-bit integer to use as the data * @param isBigEndian If `true`, `value` will be stored in big endian format. If `false`, * `value` will be stored in little endian format */ static fromUint(value: number, isBigEndian?: boolean): ByteVector; /** * Creates an 8 byte {@link ByteVector} with a positive 64-bit integer as the data * @param value Positive 64-bit integer to use as the data. If using a `bigint` it must fit * within 8 bytes. * @param isBigEndian If `true`, `value` will be stored in big endian format. If `false`, * `value` will be stored in little endian format */ static fromUlong(value: bigint | number, isBigEndian?: boolean): ByteVector; /** * Creates a 2 byte {@link ByteVector} with a positive 16-bit integer as the data * @param value Positive 16-bit integer to use as the data. * @param isBigEndian If `true`, `value` will be stored in big endian format. If `false`, * `value` will be stored in little endian format */ static fromUshort(value: number, isBigEndian?: boolean): ByteVector; /** * Calculates the CRC32 of the current instance. */ get checksum(): number; /** * Whether the current instance has 0 bytes stored. */ get isEmpty(): boolean; /** * Whether the current instance is read-only. If `true`, any call that will modify the instance * will throw. */ get isReadOnly(): boolean; /** * Whether the current instance is a 'view' of another byte vector. */ get isView(): boolean; /** * Number of bytes currently in this instance. */ get length(): number; /** * Gets the appropriate length null-byte text delimiter for the specified `type`. * @param type String type to get delimiter for */ static getTextDelimiter(type: StringType): ByteVector; /** * Compares two byte vectors. Returns a numeric value * @param a Byte vector to compare against `b` * @param b Byte vector to compare against `a` * @returns * `0` if the two vectors are the same. Any other value indicates the two are * different. If the two vectors differ by length, this will be the length of `a` minus the * length of `b`. If the lengths are the same it will be the difference between the first * element that differs. */ static compare(a: ByteVector, b: ByteVector): number; /** * Returns `true` if the contents of the two {@link ByteVector}s are identical, returns `false` * otherwise * @param first ByteVector to compare with `second` * @param second ByteVector to compare with `first` */ static equals(first: ByteVector, second: ByteVector): boolean; /** * Gets iterator for iterating over bytes in the current instance. */ [Symbol.iterator](): Iterator; /** * Adds a single byte to the end of the current instance * @param byte Value to add to the end of the ByteVector. Must be positive 8-bit integer. */ addByte(byte: number): void; /** * Adds an array of bytes to the end of the current instance * @param data Array of bytes to add to the end of the ByteVector * @param length Number of elements from `data` to copy into the current instance */ addByteArray(data: Uint8Array, length?: number): void; /** * Adds a {@link ByteVector} to the end of this ByteVector * @param data ByteVector to add to the end of this ByteVector */ addByteVector(data: ByteVector): void; /** * Removes all elements from this {@link ByteVector} * @remarks This method replaces the internal byte array with a new one. */ clear(): void; /** * Determines if `pattern` exists at a certain `offset` in this byte vector. * @param pattern ByteVector to search for at in this byte vector * @param offset Position in this byte vector to search for the pattern. If omitted, defaults * to `0` */ containsAt(pattern: ByteVector, offset?: number): boolean; /** * Compares the current instance to another byte vector. Returns a numeric result. * @param other Other byte vector to compare against the current instance. */ compareTo(other: ByteVector): number; /** * Determines whether this byte vector ends with the provided `pattern`. * @param pattern ByteVector to look for at the end of this byte vector */ endsWith(pattern: ByteVector): boolean; /** * Determines whether this byte vector ends with a part of the `pattern`. * NOTE: if this instance ends with `pattern` perfectly, it must end with n-1 or * fewer bytes. * @param pattern ByteVector to look for at the end of this byte vector */ endsWithPartialMatch(pattern: ByteVector): number; /** * Determines if this instance has identical contents to the `other` instance. * @param other Other instance to compare against the current instance. */ equals(other: ByteVector): boolean; /** * Searches this instance for the `pattern`. Returns the index of the first instance * of the pattern, or `-1` if it was not found. Providing a `byteAlign` requires the * pattern to appear at an index that is a multiple of the byteAlign parameter. * Example: searching "abcd" for "ab" with byteAlign 1 will return 0. Searching "abcd" for * "ab" with byteAlign 2 will return 1. Searching "00ab" for "ab" with byteAlign 2 will return * 2. Searching "0abc" with byteAlign 2 will return -1. * @param pattern Pattern of bytes to search this instance for * @param byteAlign Optional, byte alignment the pattern much align to */ find(pattern: ByteVector, byteAlign?: number): number; /** * Gets the byte at the given `index`. * @param index Element index to return */ get(index: number): number; /** * Gets the index of the first occurrence of the specified value. * @param item A byte to find within the current instance. * @returns * An integer containing the first index at which the value was found, or -1 if it was not * found */ indexOf(item: number): number; /** * Makes the current instance read-only, causing any call that would modify it or allow it to * be modified to throw. */ makeReadOnly(): this; /** * Searches this instance for the `pattern` occurring after a given offset. Returns the index * of the first instance of the pattern, relative to the start of the array, or `-1` if it was * not found. Providing a `byteAlign` requires the pattern to appear at an index that is a * multiple of the byteAlign parameter. Example: searching "abcd" for "ab" with byteAlign 1 * will return 0. Searching "abcd" for "ab" with byteAlign 2 will return 1. Searching "00ab" * for "ab" with byteAlign 2 will return 2. Searching "0abc" with byteAlign 2 will return -1. * @param pattern Pattern of bytes to search this instance for * @param offset Index into the instance to begin searching for `pattern` * @param byteAlign Optional, byte alignment the pattern much align to */ offsetFind(pattern: ByteVector, offset: number, byteAlign?: number): number; /** * Resizes this instance to the length specified in `size`. If the desired size is * longer than the current length, it will be filled with the byte value in * `padding`. If the desired size is shorter than the current length, bytes will be * removed. * @param size Length of the byte vector after resizing. Must be unsigned 32-bit integer * @param padding Byte to fill any excess space created after resizing */ resize(size: number, padding?: number): void; /** * Finds a byte vector by searching from the end of this instance and working towards the * beginning of this instance. Returns the index of the first instance of the pattern, or `-1` * if it was not found. Providing a `byteAlign` requires the pattern to appear at an * index that is a multiple of the byteAlign parameter. * Example: searching "abcd" for "ab" with byteAlign 1 will return 0. Searching "abcd" for * "ab" with byteAlign 2 will return 1. Searching "00ab" for "ab" with byteAlign 2 will return * 2. Searching "0abc" with byteAlign 2 will return -1. * @param pattern Pattern of bytes to search this instance for * @param byteAlign Optional, byte alignment the pattern must align to */ rFind(pattern: ByteVector, byteAlign?: number): number; /** * Sets the value at a specified index * @param index Index to set the value of * @param value Value to set at the index. Must be a valid integer betweenInclusive 0x0 and 0xff */ set(index: number, value: number): void; /** * Changes the contents of the current instance by removing or replacing existing elements * and/or adding new elements. * @param start Index at which to start changing the array. Must be less than the length of * the instance * @param deleteCount Number of elements in the array to remove from start. If greater than * the remaining length of the element, it will be capped at the remaining length * @param items Elements to add to the array beginning from start. If omitted, the method will * only remove elements from the current instance. */ splice(start: number, deleteCount: number, items?: ByteVector | Uint8Array | number[]): void; /** * Splits this byte vector into a list of byte vectors using a separator * @param separator Object to use to split this byte vector * @param byteAlign Byte align to use when splitting. in order to split when a pattern is * encountered, the index at which it is found must be divisible by this value. * @param max Maximum number of objects to return or 0 to not limit the number. If that number * is reached, the last value will contain the remainder of the file even if it contains * more instances of `separator`. * @returns ByteVector[] The split contents of the current instance */ split(separator: ByteVector, byteAlign?: number, max?: number): ByteVector[]; /** * Returns a window over the current instance. * @param startIndex Offset into this instance where the comprehension begins * @param length Number of elements from the instance to include. If omitted, defaults to the * remainder of the instance */ subarray(startIndex: number, length?: number): ByteVector; /** * Checks whether a pattern appears at the beginning of the current instance. * @param pattern ByteVector containing the pattern to check for in the current instance. * @returns * `true` if the pattern was found at the beginning of the current instance, `false` * otherwise. */ startsWith(pattern: ByteVector): boolean; /** * Returns the current instance as a base64 encoded string. */ toBase64String(): string; /** * Returns the bytes for the instance. Don't use it unless you need to. * @internal * @deprecated DON'T USE IT UNLESS YOU HAVE NO CHOICE. */ toByteArray(): Uint8Array; /** * Returns a writable copy of the bytes represented by this instance. * @remarks This is a **copy** of the data. Use sparingly. */ toByteVector(): ByteVector; /** * Converts the first eight bytes of the current instance to a double-precision floating-point * value. * @param mostSignificantByteFirst If `true` the most significant byte appears first (big * endian format). * @throws Error If there are less than eight bytes in the current instance. * @returns A double value containing the value read from the current instance. */ toDouble(mostSignificantByteFirst?: boolean): number; /** * Converts the first four bytes of the current instance to a single-precision floating-point * value. * @param mostSignificantByteFirst If `true` the most significant byte appears first (big * endian format). * @throws Error If there are less than four bytes in the current instance * @returns A float value containing the value read from the current instance. */ toFloat(mostSignificantByteFirst?: boolean): number; /** * Converts the first four bytes of the current instance to a signed integer. If the current * instance is less than four bytes, the most significant bytes will be filled with 0x00. * @param mostSignificantByteFirst If `true` the most significant byte appears first (big * endian format) * @returns A signed integer value containing the value read from the current instance */ toInt(mostSignificantByteFirst?: boolean): number; /** * Converts the first eight bytes of the current instance to a signed long. If the current * instance is less than eight bytes, the most significant bytes will be filled with 0x00. * @param mostSignificantByteFirst If `true` the most significant byte appears first (big * endian format) * @returns * A signed long value containing the value read from the current instance, * represented as a BigInt due to JavaScript's 52-bit integer limitation. */ toLong(mostSignificantByteFirst?: boolean): bigint; /** * Converts the first two bytes of the current instance to a signed short. If the current * instance is less than two bytes, the most significant bytes will be filled with 0x00. * @param mostSignificantByteFirst If `true` the most significant byte appears first (big * endian format) * @returns A signed short value containing the value read from the current instance */ toShort(mostSignificantByteFirst?: boolean): number; /** * Converts a portion of the current instance to a string using a specified encoding * @param type Value indicating the encoding to use when converting to a string. * @returns String containing the converted bytes */ toString(type: StringType): string; /** * Converts the current instance into an array of strings starting at the specified offset and * using the specified encoding, assuming the values are `null` separated and limiting it to a * specified number of items. * @param type A {@link StringType} value indicating the encoding to use when converting * @param count Value specifying a limit to the number of strings to create. Once the limit has * been reached, the last string will be filled by the remainder of the data * @returns Array of strings containing the converted text. */ toStrings(type: StringType, count?: number): string[]; /** * Converts the first four bytes of the current instance to an unsigned integer. If the current * instance is less than four bytes, the most significant bytes will be filled with 0x00. * @param mostSignificantByteFirst If `true` the most significant byte appears first (big * endian format) * @returns An unsigned integer value containing the value read from the current instance */ toUint(mostSignificantByteFirst?: boolean): number; /** * Converts the first eight bytes of the current instance to an unsigned long. If the current * instance is less than eight bytes, the most significant bytes will be filled with 0x00. * @param mostSignificantByteFirst If `true` the most significant byte appears first (big * endian format) * @returns * An unsigned short value containing the value read from the current instance, * represented as a BigInt due to JavaScript's 32-bit integer limitation */ toUlong(mostSignificantByteFirst?: boolean): bigint; /** * Converts the first two bytes of the current instance to an unsigned short. If the current * instance is less than two bytes, the most significant bytes will be filled with 0x00. * @param mostSignificantByteFirst If `true` the most significant byte appears first (big * endian format) * @returns An unsigned short value containing the value read from the current instance */ toUshort(mostSignificantByteFirst?: boolean): number; private getSizedDataView; private throwIfReadOnly; }