| 1 | # `leb128` |
| 2 | |
| 3 | [ ](https://crates.io/crates/leb128) [](https://travis-ci.org/gimli-rs/leb128) [](https://coveralls.io/github/gimli-rs/leb128?branch=master) |
| 4 | |
| 5 | Read and write DWARF's "Little Endian Base 128" (LEB128) variable length integer |
| 6 | encoding. |
| 7 | |
| 8 | The implementation is a direct translation of the pseudocode in the DWARF 4 |
| 9 | standard's appendix C. |
| 10 | |
| 11 | ## Install |
| 12 | |
| 13 | Either |
| 14 | |
| 15 | $ cargo add leb128 |
| 16 | |
| 17 | or add this to your `Cargo.toml`: |
| 18 | |
| 19 | [dependencies] |
| 20 | leb128 = "0.2.1" |
| 21 | |
| 22 | ## Example |
| 23 | |
| 24 | ```rust |
| 25 | use leb128; |
| 26 | |
| 27 | let mut buf = [0; 1024]; |
| 28 | |
| 29 | // Write to anything that implements `std::io::Write`. |
| 30 | { |
| 31 | let mut writable = &mut buf[..]; |
| 32 | leb128::write::signed(&mut writable, -12345).expect("Should write number"); |
| 33 | } |
| 34 | |
| 35 | // Read from anything that implements `std::io::Read`. |
| 36 | let mut readable = &buf[..]; |
| 37 | let val = leb128::read::signed(&mut readable).expect("Should read number"); |
| 38 | assert_eq!(val, -12345); |
| 39 | ``` |
| 40 | |
| 41 | ## Documentation |
| 42 | |
| 43 | [Documentation](https://gimli-rs.github.io/leb128/leb128/index.html) |
| 44 | |
| 45 | ## Read-Eval-Print-Loop for LEB128 |
| 46 | |
| 47 | This crate comes with a `leb128-repl` program that you can use after `cargo |
| 48 | install leb128` or by running `cargo run` in clone of this repository. |
| 49 | |
| 50 | ``` |
| 51 | $ leb128-repl |
| 52 | LEB128 Read-Eval-Print-Loop! |
| 53 | |
| 54 | Converts numbers to signed and unsigned LEB128 and displays the results in |
| 55 | base-10, hex, and binary. |
| 56 | |
| 57 | > 42 |
| 58 | # unsigned LEB128 |
| 59 | [42] |
| 60 | [2a] |
| 61 | [00101010] |
| 62 | |
| 63 | # signed LEB128 |
| 64 | [42] |
| 65 | [2a] |
| 66 | [00101010] |
| 67 | |
| 68 | > -42 |
| 69 | # unsigned LEB128 |
| 70 | error |
| 71 | |
| 72 | # signed LEB128 |
| 73 | [86] |
| 74 | [56] |
| 75 | [01010110] |
| 76 | |
| 77 | > 9001 |
| 78 | # unsigned LEB128 |
| 79 | [169, 70] |
| 80 | [a9, 46] |
| 81 | [10101001, 01000110] |
| 82 | |
| 83 | # signed LEB128 |
| 84 | [169, 198, 0] |
| 85 | [a9, c6, 0] |
| 86 | [10101001, 11000110, 00000000] |
| 87 | |
| 88 | > -9001 |
| 89 | # unsigned LEB128 |
| 90 | error |
| 91 | |
| 92 | # signed LEB128 |
| 93 | [215, 185, 127] |
| 94 | [d7, b9, 7f] |
| 95 | [11010111, 10111001, 01111111] |
| 96 | |
| 97 | > |
| 98 | ``` |
| 99 | |
| 100 | ## License |
| 101 | |
| 102 | Licensed under either of |
| 103 | |
| 104 | * Apache License, Version 2.0 ([`LICENSE-APACHE`](./LICENSE-APACHE) or https://www.apache.org/licenses/LICENSE-2.0) |
| 105 | * MIT license ([`LICENSE-MIT`](./LICENSE-MIT) or https://opensource.org/licenses/MIT) |
| 106 | |
| 107 | at your option. |
| 108 | |
| 109 | ## Contribution |
| 110 | |
| 111 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 112 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 113 | dual licensed as above, without any additional terms or conditions. |