package core:encoding/varint

Overview

Implementation of the LEB128 variable integer encoding as used by DWARF encoding and DEX files, among others.

Author of this Odin package: Jeroen van Rijn

Example:
```odin
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")
}
```


package varint implements variable length integer encoding and decoding using the LEB128 format as used by DWARF debug info, Android .dex and other file formats.

Index

Types (1)
Constants (1)
Variables (0)

This section is empty.

Procedure Groups (2)

Types

Error ¶

Error :: enum int {
	None             = 0, 
	Buffer_Too_Small = 1, 
	Value_Too_Large  = 2, 
}

Constants

LEB128_MAX_BYTES ¶

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.

Variables

This section is empty.

Procedures

decode_ileb128_buffer ¶

decode_ileb128_buffer :: proc "odin" (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 ¶

decode_ileb128_byte :: proc "odin" (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 ¶

decode_uleb128_buffer :: proc "odin" (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 ¶

decode_uleb128_byte :: proc "odin" (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 ¶

encode_ileb128 :: proc "odin" (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 ¶

encode_uleb128 :: proc "odin" (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

Source Files

Generation Information

Generated with odin version dev-2022-12 (vendor "odin") Windows_amd64 @ 2022-12-06 21:08:55.091749400 +0000 UTC