KaitaiStruct.Stream (kaitai_struct v0.1.0)

Interface for streaming data of known size

Implements the functions mentioned in the Kaitai Runtime Specification

Summary

Functions

Re-align to bytes after moving to an non-standard bit alignment

Returns a specification to start this module under a supervisor.

Takes a list of bytes and ensures that the next bytes in the stream are an exact match

true if the stream has emitted its final byte, false otherwise

Current position (bytes read) within the stream

Read an array of n bits from stream.

Read an integer (composed of N bits) from stream.

An array of n bytes from stream.

An array of all remaining bytes from stream.

Read bytes until the character (in the given encoding) is reached. Accepted encodings at KaitaiStruct.encodings/0

Read float (4 bytes, big-endian) from stream.

Read float (4 bytes, little-endian) from stream.

Read float (8 bytes, big-endian) from stream.

Read float (8 bytes, little-endian) from stream.

Read signed integer (1 byte) from stream.

Read signed integer (2 bytes, big-endian) from stream.

Read signed integer (2 bytes, little-endian) from stream.

Read signed integer (4 bytes, big-endian) from stream.

Read signed integer (4 bytes, little-endian) from stream.

Read signed integer (8 bytes, big-endian) from stream.

Read unsigned integer (1 byte) from stream.

Read unsigned integer (2 bytes, big-endian) from stream.

Read unsigned integer (2 bytes, little-endian) from stream.

Read unsigned integer (4 bytes, big-endian) from stream.

Read unsigned integer (4 bytes, little-endian) from stream.

Read unsigned integer (8 bytes, big-endian) from stream.

Read unsigned integer (8 bytes, little-endian) from stream.

Moves the stream position forward by n bytes, or to the end of the stream. Whichever is smaller.

Total size of the stream, in bytes

Types

read_error()

@type read_error() :: :reached_eof

stream_state()

@type stream_state() ::
  {:stream_state, stream :: Enumerable.t(), size_bits :: non_neg_integer(),
   pos_bits :: non_neg_integer(), buffer :: binary()}

Functions

align_to_byte(pid)

@spec align_to_byte(stream :: pid()) :: :ok

Re-align to bytes after moving to an non-standard bit alignment

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

ensure_fixed_contents(pid, contents)

@spec ensure_fixed_contents(stream :: pid(), contents :: binary()) ::
  :ok | {:error, read_error()} | {:error, :fixed_contents_mismatch}

Takes a list of bytes and ensures that the next bytes in the stream are an exact match

eof?(pid)

@spec eof?(stream :: pid()) :: boolean()

true if the stream has emitted its final byte, false otherwise

pos(pid)

@spec pos(stream :: pid()) :: non_neg_integer()

Current position (bytes read) within the stream

read_bits_array(pid, n)

@spec read_bits_array(stream :: pid(), n :: non_neg_integer()) ::
  {:ok, [boolean()]} | {:error, read_error()}

Read an array of n bits from stream.

read_bits_int(pid, n)

@spec read_bits_int(stream :: pid(), n :: non_neg_integer()) ::
  {:ok, integer()} | {:error, read_error()}

Read an integer (composed of N bits) from stream.

read_bytes_array(pid, n)

@spec read_bytes_array(stream :: pid(), n :: non_neg_integer()) ::
  {:ok, [integer()]} | {:error, read_error()}

An array of n bytes from stream.

read_bytes_full(pid)

@spec read_bytes_full(stream :: pid()) :: {:ok, [integer()]} | {:error, read_error()}

An array of all remaining bytes from stream.

read_bytes_term(pid, encoding, term, include_term, consume_term, eos_error)

@spec read_bytes_term(
  stream :: pid(),
  encoding :: String.t(),
  term :: integer(),
  include_term :: boolean(),
  consume_term :: boolean(),
  eos_error :: boolean()
) ::
  {:ok, [integer()]}
  | {:error, read_error()}
  | {:error, :unsupported_encoding}
  | {:error, {:term_encoding_error, term()}}

Read bytes until the character (in the given encoding) is reached. Accepted encodings at KaitaiStruct.encodings/0

read_f4be(pid)

@spec read_f4be(stream :: pid()) :: {:ok, float()} | {:error, read_error()}

Read float (4 bytes, big-endian) from stream.

read_f4le(pid)

@spec read_f4le(stream :: pid()) :: {:ok, float()} | {:error, read_error()}

Read float (4 bytes, little-endian) from stream.

read_f8be(pid)

@spec read_f8be(stream :: pid()) :: {:ok, float()} | {:error, read_error()}

Read float (8 bytes, big-endian) from stream.

read_f8le(pid)

@spec read_f8le(stream :: pid()) :: {:ok, float()} | {:error, read_error()}

Read float (8 bytes, little-endian) from stream.

read_s1(pid)

@spec read_s1(stream :: pid()) :: {:ok, integer()} | {:error, read_error()}

Read signed integer (1 byte) from stream.

read_s2be(pid)

@spec read_s2be(stream :: pid()) :: {:ok, integer()} | {:error, read_error()}

Read signed integer (2 bytes, big-endian) from stream.

read_s2le(pid)

@spec read_s2le(stream :: pid()) :: {:ok, integer()} | {:error, read_error()}

Read signed integer (2 bytes, little-endian) from stream.

read_s4be(pid)

@spec read_s4be(stream :: pid()) :: {:ok, integer()} | {:error, read_error()}

Read signed integer (4 bytes, big-endian) from stream.

read_s4le(pid)

@spec read_s4le(stream :: pid()) :: {:ok, integer()} | {:error, read_error()}

Read signed integer (4 bytes, little-endian) from stream.

read_s8be(pid)

@spec read_s8be(stream :: pid()) :: {:ok, integer()} | {:error, read_error()}

Read signed integer (8 bytes, big-endian) from stream.

read_u1(pid)

@spec read_u1(stream :: pid()) :: {:ok, non_neg_integer()} | {:error, read_error()}

Read unsigned integer (1 byte) from stream.

read_u2be(pid)

@spec read_u2be(stream :: pid()) :: {:ok, non_neg_integer()} | {:error, read_error()}

Read unsigned integer (2 bytes, big-endian) from stream.

read_u2le(pid)

@spec read_u2le(stream :: pid()) :: {:ok, non_neg_integer()} | {:error, read_error()}

Read unsigned integer (2 bytes, little-endian) from stream.

read_u4be(pid)

@spec read_u4be(stream :: pid()) :: {:ok, non_neg_integer()} | {:error, read_error()}

Read unsigned integer (4 bytes, big-endian) from stream.

read_u4le(pid)

@spec read_u4le(stream :: pid()) :: {:ok, non_neg_integer()} | {:error, read_error()}

Read unsigned integer (4 bytes, little-endian) from stream.

read_u8be(pid)

@spec read_u8be(stream :: pid()) :: {:ok, non_neg_integer()} | {:error, read_error()}

Read unsigned integer (8 bytes, big-endian) from stream.

read_u8le(pid)

@spec read_u8le(stream :: pid()) :: {:ok, non_neg_integer()} | {:error, read_error()}

Read unsigned integer (8 bytes, little-endian) from stream.

seek(pid, n)

@spec seek(stream :: pid(), n :: non_neg_integer()) :: :ok

Moves the stream position forward by n bytes, or to the end of the stream. Whichever is smaller.

size(pid)

@spec size(stream :: pid()) :: non_neg_integer()

Total size of the stream, in bytes