Module: encoding.msgpack

Source: ./encoding/msgpack.reef


Overview

encoding/msgpack - MessagePack binary serialization

MessagePack is an efficient binary serialization format that is more compact than JSON. This implementation supports the core MessagePack types.

Format overview:

  • Positive fixint: 0x00-0x7F (0-127)
  • Fixmap: 0x80-0x8F (map with 0-15 elements)
  • Fixarray: 0x90-0x9F (array with 0-15 elements)
  • Fixstr: 0xA0-0xBF (string with 0-31 bytes)
  • nil: 0xC0
  • false: 0xC2
  • true: 0xC3
  • uint8/16/32: 0xCC/0xCD/0xCE
  • int8/16/32: 0xD0/0xD1/0xD2
  • str8/16/32: 0xD9/0xDA/0xDB
  • array16/32: 0xDC/0xDD
  • map16/32: 0xDE/0xDF
  • Negative fixint: 0xE0-0xFF (-32 to -1)

Usage: import encoding.msgpack import core.result as result let buf: [int] = msgpack_alloc_buffer(256) let pos: int = msgpack_pack_int(42, buf, 0) let val: int = result.unwrap_ok(msgpack_unpack_int(buf, 0))

The unpack_* functions return core.result.Result, not a bare value: Ok(decoded value) when the tag byte at offset matches the expected MessagePack type, Err(core.error.Error) when it doesn't (a wrong-tag read, e.g. calling unpack_int on a string-tagged value). Each unpack_* checks msgpack_type(buf, offset) against the type it decodes before touching the payload bytes — this closes a sentinel-collision bug where a wrong-tag read used to silently return a legit-looking default (0 / false / "") instead of signaling failure.


Functions

fn MSGPACK_NIL(): int

fn MSGPACK_BOOL(): int

fn MSGPACK_INT(): int

fn MSGPACK_FLOAT(): int

fn MSGPACK_STRING(): int

fn MSGPACK_BINARY(): int

fn MSGPACK_ARRAY(): int

fn MSGPACK_MAP(): int

fn MSGPACK_UNKNOWN(): int

fn msgpack_alloc_buffer(size: int): [int]

fn msgpack_type(buf: [int], offset: int): int

fn msgpack_is_nil(buf: [int], offset: int): bool

fn msgpack_is_bool(buf: [int], offset: int): bool

fn msgpack_is_int(buf: [int], offset: int): bool

fn msgpack_is_float(buf: [int], offset: int): bool

fn msgpack_is_string(buf: [int], offset: int): bool

fn msgpack_is_binary(buf: [int], offset: int): bool

fn msgpack_is_array(buf: [int], offset: int): bool

fn msgpack_is_map(buf: [int], offset: int): bool

fn msgpack_pack_nil(buf: [int], offset: int): int

fn msgpack_pack_bool(value: bool, buf: [int], offset: int): int

fn msgpack_pack_int(value: int, buf: [int], offset: int): int

fn msgpack_pack_int64(value: int64, buf: [int], offset: int): int

int64-typed pack. Smart-picks the smallest type code that fits, falling through to msgpack_pack_int for values in int32 range and emitting 0xCF (uint64) or 0xD3 (int64) for wider values.

fn msgpack_pack_float(value: float, buf: [int], offset: int): int

Packs a float as MessagePack float64 (type code 0xCB, 8 bytes IEEE-754 BE). Reef's float is 64-bit, so float32 is never the smallest fit by default.

fn msgpack_pack_string(s: string, buf: [int], offset: int): int

fn msgpack_pack_binary(data: [int], len: int, buf: [int], offset: int): int

Packs a byte array as MessagePack binary, picking the smallest header: bin8 (0xC4) for len <= 255, bin16 (0xC5) for len <= 65535, else bin32 (0xC6).

fn msgpack_pack_array_header(count: int, buf: [int], offset: int): int

fn msgpack_pack_map_header(count: int, buf: [int], offset: int): int

fn msgpack_unpack_bool(buf: [int], offset: int): result.Result[bool, error.Error]

Ok(bool) when the tag at offset is 0xC2 (false) or 0xC3 (true). Err(InvalidInput) on any other tag — previously a non-bool tag silently decoded to false, colliding with a legit packed false.

fn msgpack_unpack_int(buf: [int], offset: int): result.Result[int, error.Error]

Ok(int) for fixint/uint8-32/int8-32 tags at offset. Err(InvalidInput) on any other tag — previously a non-int tag (e.g. a packed string) silently decoded to 0, indistinguishable from a legit packed 0 (the highest-value sentinel collision fixed here).

fn msgpack_unpack_int64(buf: [int], offset: int): result.Result[int64, error.Error]

int64-typed unpack covering all int type codes including 0xCF (uint64) and 0xD3 (int64) which msgpack_unpack_int can't return. Also widens the narrower types to int64 for uniform consumption.

Ok(int64) for any tag msgpack_type classifies as MSGPACK_INT(). Err(InvalidInput) on any other tag.

fn msgpack_unpack_float(buf: [int], offset: int): result.Result[float, error.Error]

Reads a MessagePack float (0xCA float32 or 0xCB float64), returning a 64-bit float.

Ok(float) for a float32/float64 tag. Err(InvalidInput) on any other tag.

fn msgpack_unpack_string(buf: [int], offset: int): result.Result[string, error.Error]

Ok(string) for a fixstr/str8/str16/str32 tag. Err(InvalidInput) on any other tag — previously a non-string tag silently decoded to "".

fn msgpack_unpack_binary_len(buf: [int], offset: int): result.Result[int, error.Error]

Returns the payload length of a bin8/bin16/bin32 value. Ok(len) on a binary tag. Err(InvalidInput) on any other tag — previously a non-binary tag silently decoded to 0.

fn msgpack_unpack_binary(buf: [int], offset: int): result.Result[[int], error.Error]

Allocates and returns a fresh [int] holding the decoded binary payload. Ok(bytes) on a binary tag. Err(InvalidInput) on any other tag — previously a non-binary tag silently decoded to an empty array.

fn msgpack_unpack_array_len(buf: [int], offset: int): result.Result[int, error.Error]

Ok(count) for a fixarray/array16/array32 tag. Err(InvalidInput) on any other tag — previously a non-array tag silently decoded to 0.

fn msgpack_unpack_map_len(buf: [int], offset: int): result.Result[int, error.Error]

Ok(count) for a fixmap/map16/map32 tag. Err(InvalidInput) on any other tag — previously a non-map tag silently decoded to 0.

fn msgpack_value_size(buf: [int], offset: int): int

Calculate bytes consumed by value at offset


Generated by reefc doc