UNPKG

@hulle107/libslm-binary

Version:

Low-level binary data manipulation library with typed primitives and bitwise operations in TypeScript.

184 lines (183 loc) 8.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.word = exports.byte = exports.nibble = exports.bit = void 0; //################################################################################################################## //# //# Bit //# //################################################################################################################## /** * Represents a 1-bit binary value. */ const bit = (value) => (value & exports.bit.mask); exports.bit = bit; /** Number of bits. */ exports.bit.size = 1; /** Bitmask for extraction. */ exports.bit.mask = 0x1; /** Mask for unsigned representation. */ exports.bit.decimalMask = 0x0; /** Sign mask for determining negativity in signed values. */ exports.bit.signMask = 0x1; /** Converts a `bit` to a 1-bit array. */ exports.bit.toArray = (binary) => { return toArray(binary, exports.bit.size); }; /** Converts to a binary string. */ exports.bit.toBinary = (binary) => { return padding(binary.toString(2), 1).toLocaleUpperCase(); }; /** Converts to hexadecimal string. */ exports.bit.toHexadecimal = (binary) => { return padding(binary.toString(16), 1).toLocaleUpperCase(); }; /** Converts to string. */ exports.bit.toString = (binary) => { return binary.toString(2).toLocaleUpperCase(); }; /** Converts to signed number. */ exports.bit.toSigned = (binary) => { return toSigned(binary, exports.bit.signMask, exports.bit.decimalMask); }; /** Returns numeric value. */ exports.bit.valueOf = (binary) => { return binary.valueOf(); }; /** Gets the bit value at index. */ exports.bit.get = (binary, at) => { return get(binary, at); }; /** Sets the bit at index with a `bitLike` value. */ exports.bit.set = (binary, at, value) => { return (0, exports.bit)(set(binary, at, value)); }; /** Creates a `bit` from a `bitLike` array. */ exports.bit.fromArray = (array) => { return (0, exports.bit)(fromArray(array)); }; //################################################################################################################## //# //# Nibble //# //################################################################################################################## /** * Represents a 4-bit binary value. */ const nibble = (value) => (value & exports.nibble.mask); exports.nibble = nibble; /** Number of bits. */ exports.nibble.size = 4; /** Bitmask for extraction. */ exports.nibble.mask = 0xF; /** Mask for unsigned representation. */ exports.nibble.decimalMask = 0x7; /** Sign mask for determining negativity in signed values. */ exports.nibble.signMask = 0x8; /** Converts a `nibble` to a 4-bit array. */ exports.nibble.toArray = (binary) => { return toArray(binary, exports.nibble.size); }; /** Converts to a binary string. */ exports.nibble.toBinary = (binary) => { return padding(binary.toString(2), 4).toLocaleUpperCase(); }; /** Converts to hexadecimal string. */ exports.nibble.toHexadecimal = (binary) => { return padding(binary.toString(16), 1).toLocaleUpperCase(); }; /** Converts to string. */ exports.nibble.toString = (binary) => { return binary.toString(2).toLocaleUpperCase(); }; /** Converts to signed number. */ exports.nibble.toSigned = (binary) => { return toSigned(binary, exports.nibble.signMask, exports.nibble.decimalMask); }; /** Returns numeric value. */ exports.nibble.valueOf = (binary) => { return binary.valueOf(); }; /** Gets the bit value at index. */ exports.nibble.get = (binary, at) => { return get(binary, at); }; /** Sets the bit at index with a `bitLike` value. */ exports.nibble.set = (binary, at, value) => { return (0, exports.nibble)(set(binary, at, value)); }; /** Creates a `nibble` from a `bitLike` array. */ exports.nibble.fromArray = (array) => { return (0, exports.nibble)(fromArray(array)); }; //################################################################################################################## //# //# Byte //# //################################################################################################################## /** * Represents an 8-bit binary value. */ const byte = (value) => (value & exports.byte.mask); exports.byte = byte; /** Number of bits. */ exports.byte.size = 8; /** Bitmask for extraction. */ exports.byte.mask = 0xFF; /** Mask for unsigned representation. */ exports.byte.decimalMask = 0x7F; /** Sign mask for determining negativity in signed values. */ exports.byte.signMask = 0x80; /** Converts a `byte` to an 8-bit array. */ exports.byte.toArray = (binary) => { return toArray(binary, exports.byte.size); }; /** Converts to a binary string. */ exports.byte.toBinary = (binary) => { return padding(binary.toString(2), 8).toLocaleUpperCase(); }; /** Converts to hexadecimal string. */ exports.byte.toHexadecimal = (binary) => { return padding(binary.toString(16), 2).toLocaleUpperCase(); }; /** Converts to string. */ exports.byte.toString = (binary) => { return binary.toString(2).toLocaleUpperCase(); }; /** Converts to signed number. */ exports.byte.toSigned = (binary) => { return toSigned(binary, exports.byte.signMask, exports.byte.decimalMask); }; /** Returns numeric value. */ exports.byte.valueOf = (binary) => { return binary.valueOf(); }; /** Gets the bit value at index. */ exports.byte.get = (binary, at) => { return get(binary, at); }; /** Sets the bit at index with a `bitLike` value. */ exports.byte.set = (binary, at, value) => { return (0, exports.byte)(set(binary, at, value)); }; /** Creates a `byte` from a `bitLike` array. */ exports.byte.fromArray = (array) => { return (0, exports.byte)(fromArray(array)); }; //################################################################################################################## //# //# Word //# //################################################################################################################## /** * Represents a 16-bit binary value. */ const word = (value) => (value & exports.word.mask); exports.word = word; /** Number of bits. */ exports.word.size = 16; /** Bitmask for extraction. */ exports.word.mask = 0xFFFF; /** Mask for unsigned representation. */ exports.word.decimalMask = 0x7FFF; /** Sign mask for determining negativity in signed values. */ exports.word.signMask = 0x8000; /** Converts a `word` to an 8-bit array. */ exports.word.toArray = (binary) => { return toArray(binary, exports.word.size); }; /** Converts to a binary string. */ exports.word.toBinary = (binary) => { return padding(binary.toString(2), 16).toLocaleUpperCase(); }; /** Converts to hexadecimal string. */ exports.word.toHexadecimal = (binary) => { return padding(binary.toString(16), 4).toLocaleUpperCase(); }; /** Converts to string. */ exports.word.toString = (binary) => { return binary.toString(2).toLocaleUpperCase(); }; /** Converts to signed number. */ exports.word.toSigned = (binary) => { return toSigned(binary, exports.word.signMask, exports.word.decimalMask); }; /** Returns numeric value. */ exports.word.valueOf = (binary) => { return binary.valueOf(); }; /** Gets the bit value at index. */ exports.word.get = (binary, at) => { return get(binary, at); }; /** Sets the bit at index with a `bitLike` value. */ exports.word.set = (binary, at, value) => { return (0, exports.word)(set(binary, at, value)); }; /** Creates a `word` from a `bitLike` array. */ exports.word.fromArray = (array) => { return (0, exports.word)(fromArray(array)); }; //################################################################################################################## //# //# Utilities //# //################################################################################################################## function get(binary, at) { return (binary >> at) & 0x1 ? true : false; } function set(binary, at, value) { let current = get(binary, at); let adder = value ? 1 << at : -1 << at; if (current !== (value ? true : false)) return binary + adder; return binary; } function toSigned(binary, signMask, decimalMask) { return binary & signMask ? -(binary & decimalMask) : binary; } function toArray(binary, length) { let result = []; for (let index = length - 1, position = 0; index >= 0; result[index] = binary >> position ? 1 : 0, index--, position++) ; return result; } function fromArray(array) { let result = 0; for (let index = array.length - 1, position = 0; index >= 0; result += (array[index] ? 1 : 0) << position, index--, position++) ; return result; } function padding(value, length) { let result = ''; for (let index = 0, stringIndex = value.length - length; index < length; result += stringIndex >= 0 ? value[stringIndex] : '0', index++, stringIndex++) ; return result; }