`LEB128` variable integer encoding and decoding, as used by `DWARF` & `DEX` files. Example: package main import "core:encoding/varint" import "core:fmt" main :: proc() { buf: [varint.LEB128_MAX_BYTES]u8 value := u128(42) encode_size, encode_err := varint.encode_uleb128(buf[:], value) assert(encode_size == 1 && encode_err == .None) fmt.printf("Encoded as %v\n", buf[:encode_size]) decoded_val, decode_size, decode_err := varint.decode_uleb128(buf[:]) assert(decoded_val == value && decode_size == encode_size && decode_err == .None) fmt.printf("Decoded as %v, using %v byte%v\n", decoded_val, decode_size, "" if decode_size == 1 else "s") }

Collection Info

View Source
Collection
core
Path
encoding/varint
Entries
10

Source Files

Constants

1

LEB128_MAX_BYTES #

Source
LEB128_MAX_BYTES :: 19

In theory we should use the bigint package. In practice, varints bigger than this indicate a corrupted file. Instead we'll set limits on the values we'll encode/decode 18 * 7 bits = 126, which means that a possible 19th byte may at most be `0b0000_0011`.

Types

1

Procedures

6

decode_ileb128_buffer #

Source
decode_ileb128_buffer :: proc(buf: []u8) -> (val: i128, size: int, err: Error) {…}

Decode a slice of bytes encoding a signed LEB128 integer into value and number of bytes used. Returns `size` == 0 for an invalid value, empty slice, or a varint > 18 bytes.

decode_ileb128_byte #

Source
decode_ileb128_byte :: proc(input: u8, offset: int, accumulator: i128) -> (val: i128, size: int, err: Error) {…}

Decode a a signed LEB128 integer into value and number of bytes used, one byte at a time. Returns `size` == 0 for an invalid value, empty slice, or a varint > 18 bytes.

decode_uleb128_buffer #

Source
decode_uleb128_buffer :: proc(buf: []u8) -> (val: u128, size: int, err: Error) {…}

Decode a slice of bytes encoding an unsigned LEB128 integer into value and number of bytes used. Returns `size` == 0 for an invalid value, empty slice, or a varint > 18 bytes.

decode_uleb128_byte #

Source
decode_uleb128_byte :: proc(input: u8, offset: int, accumulator: u128) -> (val: u128, size: int, err: Error) {…}

Decodes an unsigned LEB128 integer into value a byte at a time. Returns `.None` when decoded properly, `.Value_Too_Large` when they value exceeds the limits of a u128, and `.Buffer_Too_Small` when it's not yet fully decoded.

encode_ileb128 #

Source
encode_ileb128 :: proc(buf: []u8, val: i128) -> (size: int, err: Error) {…}

Encode `val` into `buf` as a signed LEB128 encoded series of bytes. `buf` must be appropriately sized.

encode_uleb128 #

Source
encode_uleb128 :: proc(buf: []u8, val: u128) -> (size: int, err: Error) {…}

Encode `val` into `buf` as an unsigned LEB128 encoded series of bytes. `buf` must be appropriately sized.

Procedure Groups

2