#leb128 #dwarf #web-assembly #wasm

wasabi_leb128

Read and write the variable length LEB128 number format

2 unstable releases

0.4.0 Mar 6, 2020
0.3.0 Oct 2, 2019

#832 in WebAssembly

Download history 234/week @ 2022-12-01 110/week @ 2022-12-08 145/week @ 2022-12-15 103/week @ 2022-12-22 246/week @ 2022-12-29 245/week @ 2023-01-05 226/week @ 2023-01-12 189/week @ 2023-01-19 221/week @ 2023-01-26 240/week @ 2023-02-02 345/week @ 2023-02-09 348/week @ 2023-02-16 484/week @ 2023-02-23 239/week @ 2023-03-02 262/week @ 2023-03-09 291/week @ 2023-03-16

1,310 downloads per month
Used in shards

MIT license

23KB
296 lines

wasabi_leb128 build status

Read and write the variable length LEB128 number format. For 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: u16 = buf.as_slice().read_leb128().unwrap();
assert_eq!(value, original_value);

For more info, see:


lib.rs:

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:

Differences between this crate and existing Rust implementations in parity-wasm and leb128:

  • Available for all primitive integers (not just u64 or i64).
  • 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.

Dependencies

~150KB