Lightweight runtime for Kaitai Struct generated parsers.
Summary
Functions
Align bit state back to byte boundary, returning binary.
Append a single terminator byte to bytes.
Decode a binary from the given encoding to a UTF-8 string.
Encode a UTF-8 string back to the target encoding. Inverse of decode_string/2.
Raises {:unsupported_write_encoding, enc} for encodings not yet supported on write.
Floor division (Python-style: result rounds towards negative infinity).
Floor modulo (Python-style: result has same sign as divisor).
Flush a BE bit accumulator: zero-pad any partial byte at the high-end of the byte. Returns iodata.
Flush a LE bit accumulator: emit the partial byte (low bits already in place). Returns iodata.
KSY add operator - string concat or arithmetic add.
Access element at index (supports both lists and binaries).
Get first element of a list or first byte of a binary.
Get the IO stream size for a parsed object. Uses stored _io_size metadata.
Get last element of a list or last byte of a binary.
Length of a string (character count) or size of a list/binary.
Get maximum value from a list or binary.
Get minimum value from a list or binary (treating bytes as values).
Get size of a list or byte_size of a binary.
Get the sizeof a parsed type or field. Uses stored _sizeof metadata.
Pad bytes on the right with pad_byte until length equals size.
Raises {:size_overflow, actual, expected} if byte_size(bytes) > size.
Rotate each byte left by amount bits.
XOR each byte in data with a single-byte key.
Zlib decompress.
Read N bits from a binary, big-endian bit order. Returns {value, rest_binary}.
Read N bits in big-endian bit order from a {bits_remaining, bit_count, binary} tuple or binary. Returns {value, {bits_remaining, bit_count, rest_binary}}. This supports consecutive bit reads without byte-realignment.
Read N bits in little-endian bit order.
Read a null-terminated string from binary, returning {string, rest}.
Read a null-terminated string with consume control.
Read a null-terminated string from binary with encoding support.
Read bytes from binary until a terminator byte is found. Returns {bytes_read, rest_of_binary}.
Parse items repeatedly until binary is exhausted.
Parse bit items repeatedly until binary is exhausted.
Parse items repeatedly until binary is exhausted, with index tracking.
Parse items repeatedly until condition is met.
Parse items until parse fn signals done (returns {item, rest, true}).
Parse items until parse fn signals done, with index tracking.
Parse items repeatedly until condition is met, with index tracking.
Write a list of bit-typed values in big-endian bit order. Returns a binary with any trailing partial byte zero-padded.
Little-endian variant of repeat_write_bits_be/2.
Strip trailing pad bytes from binary.
Terminate at terminator, then strip pad bytes only when terminator was NOT found.
Append a terminator byte then pad to size (write-side analogue of terminate_and_pad/4).
The terminator counts toward the size.
Terminate binary at first occurrence of byte.
Reverse an iodata list and flatten to a binary. Used by generated to_binary/1.
Convert value to integer (like Ruby's .to_i).
Convert value to integer with enum reverse lookup.
Inverse of process_rotate_left/2: rotate bytes right by amount bits.
Equivalent to rotating left by 8 - amount.
Rotate bytes right (inverse direction for the ror(n) process).
Inverse of process_xor — XOR is self-inverse.
Zlib re-compress. Semantically correct round-trip but not byte-identical to the original compressed blob (compression level / dictionary may differ).
Write N bits in big-endian bit order into the accumulator state.
State is {bits_acc, bits_left, iodata}. Returns a new state.
Write N bits in little-endian bit order. Bits are accumulated low-end-first.
Append the appropriate null terminator for the encoding to already-encoded bytes. Two NUL bytes for UTF-16; one for everything else.
Functions
Align bit state back to byte boundary, returning binary.
Append a single terminator byte to bytes.
Decode a binary from the given encoding to a UTF-8 string.
Encode a UTF-8 string back to the target encoding. Inverse of decode_string/2.
Raises {:unsupported_write_encoding, enc} for encodings not yet supported on write.
Floor division (Python-style: result rounds towards negative infinity).
Floor modulo (Python-style: result has same sign as divisor).
Flush a BE bit accumulator: zero-pad any partial byte at the high-end of the byte. Returns iodata.
Flush a LE bit accumulator: emit the partial byte (low bits already in place). Returns iodata.
KSY add operator - string concat or arithmetic add.
Access element at index (supports both lists and binaries).
Get first element of a list or first byte of a binary.
Get the IO stream size for a parsed object. Uses stored _io_size metadata.
Get last element of a list or last byte of a binary.
Length of a string (character count) or size of a list/binary.
Get maximum value from a list or binary.
Get minimum value from a list or binary (treating bytes as values).
Get size of a list or byte_size of a binary.
Get the sizeof a parsed type or field. Uses stored _sizeof metadata.
Pad bytes on the right with pad_byte until length equals size.
Raises {:size_overflow, actual, expected} if byte_size(bytes) > size.
Rotate each byte left by amount bits.
XOR each byte in data with a single-byte key.
Zlib decompress.
Read N bits from a binary, big-endian bit order. Returns {value, rest_binary}.
Read N bits in big-endian bit order from a {bits_remaining, bit_count, binary} tuple or binary. Returns {value, {bits_remaining, bit_count, rest_binary}}. This supports consecutive bit reads without byte-realignment.
Read N bits in little-endian bit order.
Read a null-terminated string from binary, returning {string, rest}.
Read a null-terminated string with consume control.
Read a null-terminated string from binary with encoding support.
Read bytes from binary until a terminator byte is found. Returns {bytes_read, rest_of_binary}.
- consume: if true (default), the terminator byte is consumed from the stream
- include: if true, the terminator byte is included in the returned value
Parse items repeatedly until binary is exhausted.
Parse bit items repeatedly until binary is exhausted.
Parse items repeatedly until binary is exhausted, with index tracking.
Parse items repeatedly until condition is met.
Parse items until parse fn signals done (returns {item, rest, true}).
Parse items until parse fn signals done, with index tracking.
Parse items repeatedly until condition is met, with index tracking.
Write a list of bit-typed values in big-endian bit order. Returns a binary with any trailing partial byte zero-padded.
Little-endian variant of repeat_write_bits_be/2.
Strip trailing pad bytes from binary.
Terminate at terminator, then strip pad bytes only when terminator was NOT found.
Append a terminator byte then pad to size (write-side analogue of terminate_and_pad/4).
The terminator counts toward the size.
Terminate binary at first occurrence of byte.
Reverse an iodata list and flatten to a binary. Used by generated to_binary/1.
Convert value to integer (like Ruby's .to_i).
Convert value to integer with enum reverse lookup.
Inverse of process_rotate_left/2: rotate bytes right by amount bits.
Equivalent to rotating left by 8 - amount.
Rotate bytes right (inverse direction for the ror(n) process).
Inverse of process_xor — XOR is self-inverse.
Zlib re-compress. Semantically correct round-trip but not byte-identical to the original compressed blob (compression level / dictionary may differ).
Write N bits in big-endian bit order into the accumulator state.
State is {bits_acc, bits_left, iodata}. Returns a new state.
Write N bits in little-endian bit order. Bits are accumulated low-end-first.
Append the appropriate null terminator for the encoding to already-encoded bytes. Two NUL bytes for UTF-16; one for everything else.