Ksc.Stream (Ksc v0.1.0)

Copy Markdown View Source

Lightweight runtime for Kaitai Struct generated parsers.

Summary

Functions

Align bit state back to byte boundary, returning binary.

Decode a binary from the given encoding to a UTF-8 string.

Floor division (Python-style: result rounds towards negative infinity).

Floor modulo (Python-style: result has same sign as divisor).

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.

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.

Strip trailing pad bytes from binary.

Terminate at terminator, then strip pad bytes only when terminator was NOT found.

Terminate binary at first occurrence of byte.

Convert value to integer (like Ruby's .to_i).

Convert value to integer with enum reverse lookup.

Functions

align_to_byte(data)

Align bit state back to byte boundary, returning binary.

decode_string(data, encoding)

Decode a binary from the given encoding to a UTF-8 string.

floor_div(a, b)

Floor division (Python-style: result rounds towards negative infinity).

floor_mod(a, b)

Floor modulo (Python-style: result has same sign as divisor).

kaitai_add(a, b)

KSY add operator - string concat or arithmetic add.

kaitai_at(bin, idx)

Access element at index (supports both lists and binaries).

kaitai_first(bin)

Get first element of a list or first byte of a binary.

kaitai_io_size(arg1)

Get the IO stream size for a parsed object. Uses stored _io_size metadata.

kaitai_last(bin)

Get last element of a list or last byte of a binary.

kaitai_length(bin)

Length of a string (character count) or size of a list/binary.

kaitai_max(list)

Get maximum value from a list or binary.

kaitai_min(list)

Get minimum value from a list or binary (treating bytes as values).

kaitai_size(bin)

Get size of a list or byte_size of a binary.

kaitai_sizeof(arg1)

Get the sizeof a parsed type or field. Uses stored _sizeof metadata.

process_rotate_left(data, amount)

Rotate each byte left by amount bits.

process_xor(data, key)

XOR each byte in data with a single-byte key.

process_zlib(data)

Zlib decompress.

read_bits(data, num_bits)

Read N bits from a binary, big-endian bit order. Returns {value, rest_binary}.

read_bits_be(data, num_bits)

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_bits_le(data, num_bits)

Read N bits in little-endian bit order.

read_strz(data, encoding \\ "UTF-8")

Read a null-terminated string from binary, returning {string, rest}.

read_strz_consume(data, consume)

Read a null-terminated string with consume control.

read_strz_enc(data, encoding, consume \\ true, include \\ false)

Read a null-terminated string from binary with encoding support.

read_terminated(data, term_byte, consume \\ true, include \\ false)

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

repeat_eos(data, parse_fn)

Parse items repeatedly until binary is exhausted.

repeat_eos_bits(data, num_bits, bit_fn)

Parse bit items repeatedly until binary is exhausted.

repeat_eos_idx(data, parse_fn)

Parse items repeatedly until binary is exhausted, with index tracking.

repeat_until(data, parse_fn, until_fn)

Parse items repeatedly until condition is met.

repeat_until_check(data, parse_fn)

Parse items until parse fn signals done (returns {item, rest, true}).

repeat_until_check_idx(data, parse_fn)

Parse items until parse fn signals done, with index tracking.

repeat_until_idx(data, parse_fn, until_fn)

Parse items repeatedly until condition is met, with index tracking.

strip_pad_right(data, pad_byte)

Strip trailing pad bytes from binary.

terminate_and_pad(data, term_byte, include, pad_byte)

Terminate at terminator, then strip pad bytes only when terminator was NOT found.

terminate_at(data, term_byte, include \\ false)

Terminate binary at first occurrence of byte.

to_i(x)

Convert value to integer (like Ruby's .to_i).

to_i(x, reverse_map)

Convert value to integer with enum reverse lookup.