#dwarf #read-write #variables #length

bin+lib leb128

Read and write DWARF's "Little Endian Base 128" (LEB128) variable length integer encoding

7 releases

0.2.5 Oct 4, 2021
0.2.4 May 20, 2019
0.2.3 Apr 19, 2018
0.2.2 Oct 28, 2017
0.1.0 Jun 13, 2016

#66 in Encoding

Download history 242219/week @ 2024-07-07 230284/week @ 2024-07-14 238790/week @ 2024-07-21 233662/week @ 2024-07-28 229984/week @ 2024-08-04 229002/week @ 2024-08-11 248693/week @ 2024-08-18 226906/week @ 2024-08-25 235685/week @ 2024-09-01 230263/week @ 2024-09-08 214442/week @ 2024-09-15 223418/week @ 2024-09-22 232364/week @ 2024-09-29 258213/week @ 2024-10-06 237855/week @ 2024-10-13 259555/week @ 2024-10-20

1,004,359 downloads per month
Used in 1,437 crates (103 directly)

Apache-2.0/MIT

26KB
586 lines

leb128

Build Status Coverage Status

Read and write DWARF's "Little Endian Base 128" (LEB128) variable length integer encoding.

The implementation is a direct translation of the pseudocode in the DWARF 4 standard's appendix C.

Install

Either

$ cargo add leb128

or add this to your Cargo.toml:

[dependencies]
leb128 = "0.2.1"

Example

use leb128;

let mut buf = [0; 1024];

// Write to anything that implements `std::io::Write`.
{
    let mut writable = &mut buf[..];
    leb128::write::signed(&mut writable, -12345).expect("Should write number");
}

// Read from anything that implements `std::io::Read`.
let mut readable = &buf[..];
let val = leb128::read::signed(&mut readable).expect("Should read number");
assert_eq!(val, -12345);

Documentation

Documentation

Read-Eval-Print-Loop for LEB128

This crate comes with a leb128-repl program that you can use after cargo install leb128 or by running cargo run in clone of this repository.

$ leb128-repl
LEB128 Read-Eval-Print-Loop!

Converts numbers to signed and unsigned LEB128 and displays the results in
base-10, hex, and binary.

> 42
# unsigned LEB128
[42]
[2a]
[00101010]

# signed LEB128
[42]
[2a]
[00101010]

> -42
# unsigned LEB128
error

# signed LEB128
[86]
[56]
[01010110]

> 9001
# unsigned LEB128
[169, 70]
[a9, 46]
[10101001, 01000110]

# signed LEB128
[169, 198, 0]
[a9, c6, 0]
[10101001, 11000110, 00000000]

> -9001
# unsigned LEB128
error

# signed LEB128
[215, 185, 127]
[d7, b9, 7f]
[11010111, 10111001, 01111111]

>

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps