import { Assert } from '@/util/assert.js' import { big_to_bytes } from './big.js' import { num_to_bytes } from './num.js' import { hex_to_bytes } from './hex.js' import type { Buffable, Endian } from '@/types.js' /** * Converts various data types to a Uint8Array. * Supported types: ArrayBuffer, Uint8Array, hex string, bigint, number. * @param bytes - Data to convert * @param size - Optional byte size for padding * @param endian - Byte order: 'be' (big-endian) or 'le' (little-endian) * @returns Uint8Array representation of the input * @throws {TypeError} If input type is not supported */ export function buffer ( bytes : Buffable | ArrayBuffer, size ?: number, endian ?: Endian ) : Uint8Array { if (bytes instanceof ArrayBuffer) { return new Uint8Array(bytes) } else if (bytes instanceof Uint8Array) { return create_bytes(bytes, size, endian) } else if (typeof bytes === 'string') { Assert.is_hex(bytes) return hex_to_bytes(bytes, size, endian) } else if (typeof bytes === 'bigint') { return big_to_bytes(bytes, size, endian) } else if (typeof bytes === 'number') { return num_to_bytes(bytes, size, endian) } throw new TypeError(`Input type not supported: ${typeof bytes}`) } /** * Creates a new Uint8Array of specified size with data positioned according to endianness. * @param data - Source data (number array or Uint8Array) * @param size - Target size (defaults to data length) * @param endian - Byte order: 'le' (little-endian, default) or 'be' (big-endian) * @returns Uint8Array with data positioned according to endianness * @throws {TypeError} If data is larger than specified size */ export function create_bytes ( data : number[] | Uint8Array, size ?: number, endian : Endian = 'le' ) : Uint8Array { if (size === undefined) size = data.length Assert.within_size(data, size) const buffer = new Uint8Array(size).fill(0) const offset = (endian === 'be') ? 0 : size - data.length buffer.set(data, offset) return buffer } /** * Joins multiple byte arrays into a single Uint8Array. * @param arr - Array of Uint8Arrays or number arrays to join * @returns Single Uint8Array containing all input data concatenated */ export function join_bytes ( arr : Array ) : Uint8Array { let i : number, offset = 0 const size = arr.reduce((len, arr) => len + arr.length, 0) const buff = new Uint8Array(size) for (i = 0; i < arr.length; i++) { const a = arr[i] buff.set(a, offset) offset += a.length } return buff } /** * Splits a byte array into equal-sized chunks. * @param data_blob - Uint8Array to split * @param chunk_size - Size of each chunk * @param total_size - Expected total size (must equal data_blob.length) * @returns Array of Uint8Array chunks * @throws {TypeError} If total_size is not divisible by chunk_size * @throws {TypeError} If data_blob.length doesn't equal total_size */ export function split_bytes ( data_blob : Uint8Array, chunk_size : number, total_size : number ) : Uint8Array[] { const len = data_blob.length, count = total_size / chunk_size if (total_size % chunk_size !== 0) { throw new TypeError(`Invalid parameters: ${total_size} % ${chunk_size} !== 0`) } if (len !== total_size) { throw new TypeError(`Invalid data stream: ${len} !== ${total_size}`) } if (len % chunk_size !== 0) { throw new TypeError(`Invalid data stream: ${len} % ${chunk_size} !== 0`) } const chunks = new Array(count) for (let i = 0; i < count; i++) { const idx = i * chunk_size chunks[i] = data_blob.subarray(idx, idx + chunk_size) } return chunks }