### Install @oslojs/binary Source: https://github.com/oslo-project/binary/blob/main/docs/pages/index.md This snippet shows how to install the @oslojs/binary package using npm, the Node Package Manager. ```bash npm i @oslojs/binary ``` -------------------------------- ### Install @oslojs/binary Source: https://github.com/oslo-project/binary/blob/main/README.md Installs the @oslojs/binary package using npm. This is the primary method for adding the library to your JavaScript project. ```bash npm i @oslojs/binary ``` -------------------------------- ### compareBytes() Usage Example Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/compareBytes.md Demonstrates how to import and use the `compareBytes` function from the 'oslo/binary' module to compare two Uint8Array instances. ```ts import { compareBytes } from "oslo/binary"; const a = new Uint8Array([0, 1, 2]); const b = new Uint8Array([0, 1, 2]); const equal = compareBytes(a, b); ``` -------------------------------- ### ByteOrder Methods Explained Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/ByteOrder.md Details the functionality of each method within the ByteOrder interface. This includes the expected input parameters, the operation performed (reading or writing bytes), the return type, and potential error conditions like TypeErrors for insufficient data or invalid values. ```APIDOC ByteOrder Methods: - uint8(data: Uint8Array, offset: number): number Converts the first 1 byte from the offset to an integer. Throws a `TypeError` if there isn't enough bytes. - uint16(data: Uint8Array, offset: number): number Converts the first 2 bytes from the offset to an integer. Throws a `TypeError` if there isn't enough bytes. - uint32(data: Uint8Array, offset: number): number Converts the first 4 bytes from the offset to an integer. Throws a `TypeError` if there isn't enough bytes. - uint64(data: Uint8Array, offset: number): bigint Converts the first 8 bytes from the offset to an integer. Throws a `TypeError` if there isn't enough bytes. - putUint8(target: Uint8Array, value: number, offset: number): void Puts the binary representation of the integer to the first 1 byte from the offset. Throws a `TypeError` on insufficient space in `target` and invalid `value`. - putUint16(target: Uint8Array, value: number, offset: number): void Puts the binary representation of the integer to the first 2 byte from the offset. Throws a `TypeError` on insufficient space in `target` and invalid `value`. - putUint32(target: Uint8Array, value: number, offset: number): void Puts the binary representation of the integer to the first 4 byte from the offset. Throws a `TypeError` on insufficient space in `target` and invalid `value`. - putUint64(target: Uint8Array, value: bigint, offset: number): void Puts the binary representation of the integer to the first 8 byte from the offset. Throws a `TypeError` on insufficient space in `target` and invalid `value`. ``` -------------------------------- ### DynamicBuffer Constructor Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/DynamicBuffer/index.md Initializes a new DynamicBuffer with a specified initial capacity. The buffer's capacity will automatically grow by a factor of 2 as needed. ```ts function constructor(capacity: number): this; ``` -------------------------------- ### ByteOrder Interface Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/index.md Defines the structure for byte order, typically 'BE' for big-endian and 'LE' for little-endian. ```APIDOC ByteOrder: Type: "BE" | "LE" Description: Specifies the byte order for data interpretation. ``` -------------------------------- ### Binary to Integer Conversion Source: https://github.com/oslo-project/binary/blob/main/docs/pages/examples/integers.md Converts a Uint8Array to uint32 and uint64 using BigEndian and LittleEndian respectively. Demonstrates reading unsigned integers from binary data. ```typescript import { BigEndian, LittleEndian } from "@oslojs/binary"; const toUint32: number = BigEndian.uint32(new Uint8Array([1, 2, 3, 4])); const toUint64: bigint = LittleEndian.uint64(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8])); ``` -------------------------------- ### Endianness Constants Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/index.md Provides constants for specifying byte order (endianness). ```javascript import { bigEndian, littleEndian } from "@oslojs/binary"; console.log(bigEndian); // Output: 'BE' console.log(littleEndian); // Output: 'LE' ``` -------------------------------- ### ByteOrder Interface Definition Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/ByteOrder.md Defines the structure of the ByteOrder interface, outlining methods for reading and writing unsigned integers of different byte lengths. This interface is crucial for handling binary data representations. ```ts interface ByteOrder { uint8(data: Uint8Array, offset: number): number; uint16(data: Uint8Array, offset: number): number; uint32(data: Uint8Array, offset: number): number; uint64(data: Uint8Array, offset: number): bigint; putUint8(target: Uint8Array, value: number, offset: number): void; putUint16(target: Uint8Array, value: number, offset: number): void; putUint32(target: Uint8Array, value: number, offset: number): void; putUint64(target: Uint8Array, value: bigint, offset: number): void; } ``` -------------------------------- ### Integer to Binary Conversion Source: https://github.com/oslo-project/binary/blob/main/docs/pages/examples/integers.md Converts integer values (uint32 and uint64) into Uint8Array representations using LittleEndian and BigEndian put methods. Shows writing unsigned integers to binary data. ```typescript import { BigEndian, LittleEndian } from "@oslojs/binary"; const fromUint32 = new Uint8Array(4); const fromUint64 = new Uint8Array(8); LittleEndian.putUint32(fromUint32, 135155375, 0); BigEndian.putUint64(fromUint64, 281478725684090n, 0); ``` -------------------------------- ### compareBytes() Function Definition Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/compareBytes.md Defines the `compareBytes` function which takes two Uint8Array arguments and returns a boolean indicating if they are identical. Note that this comparison is not constant time. ```ts function compareBytes(a: Uint8Array, b: Uint8Array): boolean; ``` -------------------------------- ### DynamicBuffer Properties Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/DynamicBuffer/index.md Defines the properties of a DynamicBuffer, including its current capacity and the number of bytes currently stored. ```ts interface Properties { capacity: number; length: number; } ``` -------------------------------- ### DynamicBuffer Class Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/index.md Represents a dynamic buffer for manipulating byte sequences. Provides methods for writing and reading various data types with specified endianness. ```javascript import { DynamicBuffer } from "@oslojs/binary"; // Example usage: const buffer = new DynamicBuffer(); buffer.writeUInt32BE(0x12345678); buffer.writeBuffer(Buffer.from([0x01, 0x02])); console.log(buffer.toBuffer()); // Example output: console.log(buffer.readUInt32BE()); // Output: 305419896 ``` -------------------------------- ### rotl64() Function Definition Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/rotl64.md Defines the rotl64() function which performs a 64-bit bitwise left rotation. It takes a bigint `x` and a number `n` as input, returning a number. ```ts function rotl64(x: bigint, n: number): number; ``` -------------------------------- ### 32-bit Integer Left Rotation Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/index.md Performs a 32-bit left rotation on an integer. ```javascript import { rotl32 } from "@oslojs/binary"; const value = 0x12345678; const shift = 4; const rotatedValue = rotl32(value, shift); console.log(rotatedValue.toString(16)); // Example output: '23456781' ``` -------------------------------- ### 64-bit Integer Left Rotation Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/index.md Performs a 64-bit left rotation on a BigInt value. ```javascript import { rotl64 } from "@oslojs/binary"; const value = 0x1122334455667788n; const shift = 8; const rotatedValue = rotl64(value, shift); console.log(rotatedValue.toString(16)); // Example output: '2233445566778811' ``` -------------------------------- ### 32-bit Integer Right Rotation Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/index.md Performs a 32-bit right rotation on an integer. ```javascript import { rotr32 } from "@oslojs/binary"; const value = 0x12345678; const shift = 4; const rotatedValue = rotr32(value, shift); console.log(rotatedValue.toString(16)); // Example output: '81234567' ``` -------------------------------- ### 64-bit Integer Right Rotation Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/index.md Performs a 64-bit right rotation on a BigInt value. ```javascript import { rotr64 } from "@oslojs/binary"; const value = 0x1122334455667788n; const shift = 8; const rotatedValue = rotr64(value, shift); console.log(rotatedValue.toString(16)); // Example output: '8811223344556677' ``` -------------------------------- ### BigInt to Bytes Conversion Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/index.md Converts a BigInt value to a byte array. Supports specifying endianness and padding. ```javascript import { bigIntBytes } from "@oslojs/binary"; const bigIntValue = 12345678901234567890n; const bytes = bigIntBytes(bigIntValue); console.log(bytes); // Example output: ``` -------------------------------- ### DynamicBuffer.readInto() Method Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/DynamicBuffer/readInto.md Copies the current value of the DynamicBuffer into a provided Uint8Array. This operation will throw an error if the target array does not have sufficient space to hold the buffer's content. ```typescript function readInto(target: Uint8Array): void; // Parameters: // target: The Uint8Array to copy the buffer's content into. ``` -------------------------------- ### rotr64() Function Definition Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/rotr64.md Defines the rotr64 function for performing a 64-bit right bitwise rotation. It takes a bigint `x` and a number `n` as input. ```ts function rotr64(x: bigint, n: number): number; ``` -------------------------------- ### rotl32() Function Definition Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/rotl32.md Defines the rotl32() function which takes a number `x` and a rotation count `n` as input, and returns the result of a 32-bit left bitwise rotation. This function is commonly used in cryptographic algorithms and low-level bit manipulation tasks. ```ts function rotl32(x: number, n: number): number; ``` -------------------------------- ### Byte Array Comparison Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/index.md Compares two byte arrays lexicographically. Returns -1 if a < b, 0 if a === b, and 1 if a > b. ```javascript import { compareBytes } from "@oslojs/binary"; const bytesA = Buffer.from([0x01, 0x02, 0x03]); const bytesB = Buffer.from([0x01, 0x02, 0x04]); console.log(compareBytes(bytesA, bytesB)); // Output: -1 ``` -------------------------------- ### concatenateBytes() Function Definition Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/concatenateBytes.md Defines the concatenateBytes function which takes two Uint8Array parameters and returns a Uint8Array representing their concatenation. This function is useful for combining binary data. ```ts function concatenateBytes(a: Uint8Array, b: Uint8Array): Uint8Array; ``` -------------------------------- ### Bytes to BigInt Conversion Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/index.md Converts a byte array to a BigInt value. Supports specifying endianness. ```javascript import { bigIntFromBytes } from "@oslojs/binary"; const byteBuffer = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]); // Example byte buffer const bigIntValue = bigIntFromBytes(byteBuffer); console.log(bigIntValue); // Example output: 726238597932243585n ``` -------------------------------- ### rotr32() Function Definition Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/rotr32.md Defines the rotr32 function for performing a 32-bit bitwise right rotation. It takes a number `x` and a rotation count `n` as input and returns the rotated number. ```ts function rotr32(x: number, n: number): number; ``` -------------------------------- ### Concatenate Byte Arrays Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/index.md Concatenates multiple byte arrays into a single byte array. ```javascript import { concatenateBytes } from "@oslojs/binary"; const buffer1 = Buffer.from([0x01, 0x02]); const buffer2 = Buffer.from([0x03, 0x04]); const combinedBuffer = concatenateBytes(buffer1, buffer2); console.log(combinedBuffer); // Example output: ``` -------------------------------- ### DynamicBuffer.bytes() Method Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/DynamicBuffer/bytes.md The `bytes()` method returns a copy of the current value stored within the DynamicBuffer. This is useful for retrieving the buffer's contents without modifying the original buffer. It returns a `Uint8Array`. ```ts function bytes(): Uint8Array; ``` -------------------------------- ### DynamicBuffer.clear() Function Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/DynamicBuffer/clear.md Resets the value of a DynamicBuffer to an empty array. This function has no parameters and returns void. ```ts function clear(): void; ``` -------------------------------- ### DynamicBuffer.write() Function Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/DynamicBuffer/write.md Appends a Uint8Array of bytes to the end of the DynamicBuffer. This function modifies the buffer in place and does not return any value. ```ts function write(bytes: Uint8Array): void; ``` -------------------------------- ### bigIntFromBytes() Function Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/bigIntFromBytes.md Converts a byte array to a big-endian unsigned integer. This function takes a Uint8Array as input and returns a bigint representing the converted integer. ```typescript function bigIntBytes(bytes: Uint8Array): bigint; ``` -------------------------------- ### DynamicBuffer.writeByte() Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/DynamicBuffer/writeByte.md Adds a byte to the end of the current value. This method is part of the DynamicBuffer class and is used for manipulating byte buffers. ```ts function writeByte(byte: number): void; Parameters: - byte: The byte to add to the buffer. ``` -------------------------------- ### bigIntBytes() Function Source: https://github.com/oslo-project/binary/blob/main/docs/pages/reference/main/bigIntBytes.md Converts a bigint value to its absolute value represented as a big-endian byte array. This function is useful for serializing large integer values into a byte format. ```typescript function bigIntBytes(value: bigint): Uint8Array; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.