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:
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")
}
Copyright 2022 Jeroen van Rijn <nom@duclavier.com>.
Made available under Odin's BSD-3 license.
List of contributors:
Jeroen van Rijn: Initial implementation.
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, }
Related Procedures With Returns
- decode_ileb128_buffer
- decode_ileb128_byte
- decode_uleb128_buffer
- decode_uleb128_byte
- encode_ileb128
- encode_uleb128
- decode_ileb128 (procedure groups)
- decode_uleb128 (procedure groups)
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 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(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 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(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 val
into buf
as a signed LEB128 encoded series of bytes.
buf
must be appropriately sized.
encode_uleb128 ¶
Encode val
into buf
as an unsigned LEB128 encoded series of bytes.
buf
must be appropriately sized.
Procedure Groups
decode_ileb128 ¶
decode_ileb128 :: proc{ decode_ileb128_buffer, decode_ileb128_byte, }
decode_uleb128 ¶
decode_uleb128 :: proc{ decode_uleb128_buffer, decode_uleb128_byte, }
Source Files
Generation Information
Generated with odin version dev-2024-10 (vendor "odin") Windows_amd64 @ 2024-10-05 21:10:06.113585000 +0000 UTC