Buffer

The buffer library provides safe binary serialization and deserialization with bounds checking and clean error handling.

class st.buf.Buf
init(buffer)

Initialize a buffer instance.

Parameters

buffer (str) – initial buffer contents

size()

Return the size of the internal buffer.

Returns

size in bytes

Return type

number

tell()

Return the current 0-based byte position of the buffer.

Returns

0-based position in bytes

Return type

number

bit_tell()

Return the current 0-based bit position of the buffer, modulus 8.

Returns

0-based bit position in bits, modulus 8.

Return type

number

remain()

Return the number of bytes remaining in the buffer.

Returns

buffer’s remaining bytes

Return type

number

bits_remain()

Return the number of bits remaining in the buffer.

Returns

buffer’s remaining bytes

Return type

number

seek(n)

Seek buffer by n bytes.

Parameters

n (number) – number bytes to seek

bit_seek(n)

Seek buffer by n bits.

Parameters

n (number) – number bytes to seek

class st.buf.Writer: st.buf.Buf
init()

Initialize buf writer instance.

pretty_print()

Get a string representation of the Writer

Returns

a string representation of the Writer

Return type

str

write_int(value, width, signed, little_endain)

Serialize and write an integer value to the internal buffer.

Parameters
  • value (number) – value to write

  • width (number) – integer width in bytes

  • signed (boolean) – true if signed, false if unsigned

  • little_endain (boolean) – true if little endian, false if big endian

write_u8(value)

Write a Uint8 to the internal buffer.

Parameters

value (number) – value to write

write_le_u16(value)

Write a little-endian Uint16 to the internal buffer.

Parameters

value (number) – value to write

write_be_u16(value)

Write a big-endian Uint16 to the internal buffer.

Parameters

value (number) – value to write

write_le_u24(value)

Write a little-endian Uint24 to the internal buffer.

Parameters

value (number) – value to write

write_be_u24(value)

Write a big-endian Uint24 to the internal buffer.

Parameters

value (number) – value to write

write_le_u32(value)

Write a little-endian Uint32 to the internal buffer.

Parameters

value (number) – value to write

write_be_u32(value)

Write a big-endian Uint32 to the internal buffer.

Parameters

value (number) – value to write

write_le_u40(value)

Write a little-endian Uint40 to the internal buffer.

Parameters

value (number) – value to write

write_be_u40(value)

Write a big-endian Uint40 to the internal buffer.

Parameters

value (number) – value to write

write_le_u48(value)

Write a little-endian Uint48 to the internal buffer.

Parameters

value (number) – value to write

write_be_u48(value)

Write a big-endian Uint48 to the internal buffer.

Parameters

value (number) – value to write

write_le_u56(value)

Write a little-endian Uint56 to the internal buffer.

Parameters

value (number) – value to write

write_be_u56(value)

Write a big-endian Uint56 to the internal buffer.

Parameters

value (number) – value to write

write_i8(value)

Write an Int8 to the internal buffer.

Parameters

value (number) – value to write

write_le_i16(value)

Write a little-endian Int16 to the internal buffer.

Parameters

value (number) – value to write

write_be_i16(value)

Write a big-endian Int16 to the internal buffer.

Parameters

value (number) – value to write

write_le_i24(value)

Write a little-endian Int24 to the internal buffer.

Parameters

value (number) – value to write

write_be_i24(value)

Write a big-endian Int24 to the internal buffer.

Parameters

value (number) – value to write

write_le_i32(value)

Write a little-endian Int32 to the internal buffer.

Parameters

value (number) – value to write

write_be_i32(value)

Write a big-endian Int32 to the internal buffer.

Parameters

value (number) – value to write

write_le_i40(value)

Write a little-endian Int40 to the internal buffer.

Parameters

value (number) – value to write

write_be_i40(value)

Write a big-endian Int40 to the internal buffer.

Parameters

value (number) – value to write

write_le_i48(value)

Write a little-endian Int48 to the internal buffer.

Parameters

value (number) – value to write

write_be_i48(value)

Write a big-endian Int48 to the internal buffer.

Parameters

value (number) – value to write

write_le_i56(value)

Write a little-endian Int56 to the internal buffer.

Parameters

value (number) – value to write

write_be_i56(value)

Write a big-endian Int56 to the internal buffer.

Parameters

value (number) – value to write

write_le_i64(value)

Write a little-endian Int64 to the internal buffer.

Parameters

value (number) – value to write

write_be_i64(value)

Write a big-endian Int64 to the internal buffer.

Parameters

value (number) – value to write

write_bytes(data)

Write an arbitrary number of bytes to the internal buffer.

Parameters

data (str) – data to write

write_bool(value)

Write a boolean bit to the internal buffer.

Parameters

value (boolean) – value to write

write_bits(width, data)

Write a bit field to the internal buffer.

Bits are extracted from the passed value in little-endian order.

Parameters
  • width (number) – bit field width

  • data (number) – bit field

class st.buf.Reader: st.buf.Buf
init(buffer)

Initialize buf reader instance.

Parameters

buffer (str) – buffer contents

pretty_print()

Get a string representation of the Reader

Returns

a string representation of the Reader

Return type

str

store(value, key, out)

Store a value at the specified key either in the internal parsed table or,

if passed, the specified ‘out’ table.

Parameters
  • value (any) – value to store

  • key (any) – key at which to store value, or nil if storage is not desired

  • out (table) – table in which to store value, or nil if storage in the internal ‘parsed’ table is desired.

read_int(width, signed, little_endian)

Read and deserialize an integer value from the internal buffer.

Parameters
  • width (number) – integer width in bytes

  • signed (boolean) – true if signed, false if unsigned

  • little_endian (boolean) – true if little endian, false if big endian

Returns

integer parsed from the internal buffer

Return type

number

read_u8()

Read a Uint8 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_le_u16()

Read a little-endian Uint16 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_be_u16()

Read a big-endian Uint16 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_le_u24()

Read a little-endian Uint24 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_be_u24()

Read a big-endian Uint24 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_le_u32()

Read a little-endian Uint32 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_be_u32()

Read a big-endian Uint32 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_le_u40()

Read a little-endian Uint40 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_be_u40()

Read a big-endian Uint40 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_le_u48()

Read a little-endian Uint48 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_be_u48()

Read a big-endian Uint48 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_le_u56()

Read a little-endian Uint56 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_be_u56()

Read a big-endian Uint56 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_i8()

Read an Int8 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_le_i16()

Read a little-endian Int16 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_be_i16()

Read a big-endian Int16 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_le_i24()

Read a little-endian Int24 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_be_i24()

Read a big-endian Int24 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_le_i32()

Read a little-endian Int32 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_be_i32()

Read a big-endian Int32 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_le_i40()

Read a little-endian Int40 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_be_i40()

Read a big-endian Int40 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_le_i48()

Read a little-endian Int48 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_be_i48()

Read a big-endian Int40 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_le_i56()

Read a little-endian Int56 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_be_i56()

Read a big-endian Int56 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_le_i64()

Read a little-endian Int64 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_be_i64()

Read a big-endian Int64 from the internal buffer.

Returns

value read from the buffer

Return type

number

read_bytes(len)

Read an arbitrary number of bytes from the internal buffer.

Parameters

len (number) – of bytes to read

Returns

data read from the buffer

Return type

str

read_bool()

Read a boolean bit from the internal buffer.

Returns

true if the bit read is set, false if not

Return type

boolean

read_bits(len)

Read a bit field from the internal buffer.

Bit extractions are returned in little-endian order.

Parameters

len (number) – of bits to read

Returns

numerical representation of the bit field.

Return type

number

peek_u8()

Peek a Uint8 from the internal buffer, but do not advance internal pointer.

Returns

value read from the buffer

Return type

number