[−][src]Crate wasabi_leb128
Read and write the variable length LEB128 number format.
LEB128 ("Little Endian Base 128") is used, for example in DWARF debugging information (see Appenix 4 for C pseudo code) and in the WebAssembly binary format.
Example
use wasabi_leb128::{ReadLeb128, WriteLeb128}; // Vec<u8> as byte-oriented reader/writer. let mut buf = Vec::new(); // Encoding/writing a u16 as an LEB128 byte sequence. let original_value: u16 = 128; buf.write_leb128(original_value).unwrap(); assert_eq!(buf, [0x80, 0x01]); // Decoding/reading an LEB128 number back to a u16. let (value, bytes_read): (u16, usize) = buf.as_slice().read_leb128().unwrap(); assert_eq!(value, original_value);
See ReadLeb128
and WriteLeb128
traits for more information.
Related Work
Other open-source implementations of LEB128 numbers:
- LLVM: http://llvm.org/doxygen/LEB128_8h_source.html
- Note that
decodesSLEB128()
seems to have no overflow checking!?
- Note that
- V8: https://github.com/v8/v8/blob/4b9b23521e6fd42373ebbcb20ebe03bf445494f9/src/wasm/decoder.h#L329
- Note some clever engineering: template-based unrolling, handling of signed and unsigned and different sizes in a single template, proper overflow checks.
parity-wasm
crate: https://github.com/paritytech/parity-wasm/blob/556a02a6d2e816044d2e486bf78123a9bc0657f5/src/elements/primitives.rs#L35leb128
crate: https://github.com/gimli-rs/leb128
Differences between this crate and existing Rust implementations in parity-wasm
and leb128
:
- Available for all primitive integers (not just
u64
ori64
). - A single, combined implementation for signed/unsigned and all sizes (thanks to
num_traits
). - Proper overflow checking for all target types when parsing.
- (Hopefully:) easy to understand, comments and explanations inline.
Enums
ParseLeb128Error | Errors while parsing an LEB128 value from an |
Traits
ReadLeb128 | A trait that extends readers (implementers of the |
WriteLeb128 | A trait that extends writers (implementers of the |
Functions
max_bytes | Maximum number of bytes that the LEB128 encoding of values of type |