vera:binary

Binary encoding/decoding for multi-byte integers.

import "vera:binary"

Overview

Provides big-endian (network byte order) and little-endian read/write helpers for i16, i32, and i64 values. All write functions return the new offset (offset + bytes written) for easy chaining, allowing sequential writes without manual offset arithmetic.

Dependencies: None (pure VERA)

Functions

Big-Endian (Network Byte Order)


write_i16_be

function write_i16_be(buf: ptr, offset: i64, value: i32) -> i64

Write a 16-bit integer in big-endian byte order at buf + offset.

Parameters:

  • buf (ptr) — destination buffer
  • offset (i64) — byte offset within the buffer
  • value (i32) — the 16-bit value to write (low 16 bits used)

Returns: offset + 2 (the new offset for chaining)


read_i16_be

function read_i16_be(buf: ptr, offset: i64) -> i32

Read a 16-bit big-endian integer from buf + offset.

Parameters:

  • buf (ptr) — source buffer
  • offset (i64) — byte offset within the buffer

Returns: the 16-bit value as an i32


write_i32_be

function write_i32_be(buf: ptr, offset: i64, value: i32) -> i64

Write a 32-bit integer in big-endian byte order at buf + offset.

Parameters:

  • buf (ptr) — destination buffer
  • offset (i64) — byte offset within the buffer
  • value (i32) — the 32-bit value to write

Returns: offset + 4 (the new offset for chaining)


read_i32_be

function read_i32_be(buf: ptr, offset: i64) -> i32

Read a 32-bit big-endian integer from buf + offset.

Parameters:

  • buf (ptr) — source buffer
  • offset (i64) — byte offset within the buffer

Returns: the 32-bit value


write_i64_be

function write_i64_be(buf: ptr, offset: i64, value: i64) -> i64

Write a 64-bit integer in big-endian byte order at buf + offset.

Parameters:

  • buf (ptr) — destination buffer
  • offset (i64) — byte offset within the buffer
  • value (i64) — the 64-bit value to write

Returns: offset + 8 (the new offset for chaining)


read_i64_be

function read_i64_be(buf: ptr, offset: i64) -> i64

Read a 64-bit big-endian integer from buf + offset.

Parameters:

  • buf (ptr) — source buffer
  • offset (i64) — byte offset within the buffer

Returns: the 64-bit value

Little-Endian


write_i16_le

function write_i16_le(buf: ptr, offset: i64, value: i32) -> i64

Write a 16-bit integer in little-endian byte order at buf + offset.

Parameters:

  • buf (ptr) — destination buffer
  • offset (i64) — byte offset within the buffer
  • value (i32) — the 16-bit value to write (low 16 bits used)

Returns: offset + 2 (the new offset for chaining)


read_i16_le

function read_i16_le(buf: ptr, offset: i64) -> i32

Read a 16-bit little-endian integer from buf + offset.

Parameters:

  • buf (ptr) — source buffer
  • offset (i64) — byte offset within the buffer

Returns: the 16-bit value as an i32


write_i32_le

function write_i32_le(buf: ptr, offset: i64, value: i32) -> i64

Write a 32-bit integer in little-endian byte order at buf + offset.

Parameters:

  • buf (ptr) — destination buffer
  • offset (i64) — byte offset within the buffer
  • value (i32) — the 32-bit value to write

Returns: offset + 4 (the new offset for chaining)


read_i32_le

function read_i32_le(buf: ptr, offset: i64) -> i32

Read a 32-bit little-endian integer from buf + offset.

Parameters:

  • buf (ptr) — source buffer
  • offset (i64) — byte offset within the buffer

Returns: the 32-bit value


write_i64_le

function write_i64_le(buf: ptr, offset: i64, value: i64) -> i64

Write a 64-bit integer in little-endian byte order at buf + offset.

Parameters:

  • buf (ptr) — destination buffer
  • offset (i64) — byte offset within the buffer
  • value (i64) — the 64-bit value to write

Returns: offset + 8 (the new offset for chaining)


read_i64_le

function read_i64_le(buf: ptr, offset: i64) -> i64

Read a 64-bit little-endian integer from buf + offset.

Parameters:

  • buf (ptr) — source buffer
  • offset (i64) — byte offset within the buffer

Returns: the 64-bit value