| 1 | # Leb128fmt |
| 2 | |
| 3 | Leb128fmt is a library to decode and encode [LEB128][leb128_wiki] formatted numbers. |
| 4 | LEB128 is a variable length integer compression format. |
| 5 | |
| 6 | * [Latest API Documentation][api_docs] |
| 7 | |
| 8 | The library does not allocate memory and can be used in `no_std` and |
| 9 | `no_std::no_alloc` environments. |
| 10 | |
| 11 | Various functions are provided which encode and decode signed and unsigned |
| 12 | integers with the number of bits in the function name. There are generic |
| 13 | functions provided to read and write slices of encoded values as well. |
| 14 | |
| 15 | There are encoding functions with the word `fixed` in the name which will |
| 16 | write out a value using the maximum number of bytes for a given bit size. |
| 17 | For instance, using `encode_fixed_u32` will always use 5 bytes to |
| 18 | write out the value. While always using the maximum number of bytes removes |
| 19 | the benefit of compression, in some scenarios, it is beneficial to have a |
| 20 | fixed encoding size. |
| 21 | |
| 22 | Finally, there are macros provided which you can use to build your own |
| 23 | encoding and decoding functions for unusual variants like signed 33 bit |
| 24 | values. |
| 25 | |
| 26 | ## Installation |
| 27 | |
| 28 | ```sh |
| 29 | cargo add leb128fmt |
| 30 | ``` |
| 31 | |
| 32 | ## Examples |
| 33 | |
| 34 | ### Functions using Arrays |
| 35 | |
| 36 | ```rust |
| 37 | // Encode an unsigned 32 bit number: |
| 38 | let (output, written_len) = leb128fmt::encode_u32(43110).unwrap(); |
| 39 | // The number of bytes written in the output array |
| 40 | assert_eq!(written_len, 3); |
| 41 | assert_eq!(&output[..written_len], &[0xE6, 0xD0, 0x02]); |
| 42 | // The entire output array. Note you should only use &output[..written_len] to copy |
| 43 | // into your output buffer |
| 44 | assert_eq!(output, [0xE6, 0xD0, 0x02, 0x00, 0x00]); |
| 45 | |
| 46 | // Decode an unsigned 32 bit number: |
| 47 | let input = [0xE6, 0xD0, 0x02, 0x00, 0x00]; |
| 48 | let (result, read_len) = leb128fmt::decode_u32(input).unwrap(); |
| 49 | assert_eq!(result, 43110); |
| 50 | assert_eq!(read_len, 3); |
| 51 | ``` |
| 52 | |
| 53 | #### Helper Functions |
| 54 | |
| 55 | If you are reading from an input buffer, you can use `is_last` and |
| 56 | `max_len` to determine the bytes to copy into the array. |
| 57 | |
| 58 | ```rust |
| 59 | let buffer = vec![0xFE, 0xFE, 0xE6, 0xD0, 0x02, 0xFE, 0xFE, 0xFE]; |
| 60 | let pos = 2; |
| 61 | let end = buffer.iter().skip(pos).copied().position(leb128fmt::is_last).map(|p| pos + p); |
| 62 | if let Some(end) = end { |
| 63 | if end <= pos + leb128fmt::max_len::<32>() { |
| 64 | let mut input = [0u8; leb128fmt::max_len::<32>()]; |
| 65 | input[..=end - pos].copy_from_slice(&buffer[pos..=end]); |
| 66 | let (result, read_len) = leb128fmt::decode_u32(input).unwrap(); |
| 67 | assert_eq!(result, 43110); |
| 68 | assert_eq!(read_len, 3); |
| 69 | } else { |
| 70 | // invalid LEB128 encoding |
| 71 | panic!(); |
| 72 | } |
| 73 | } else { |
| 74 | if buffer.len() - pos < leb128fmt::max_len::<32>() { |
| 75 | // Need more bytes in the buffer |
| 76 | panic!(); |
| 77 | } else { |
| 78 | // invalid LEB128 encoding |
| 79 | panic!(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | ``` |
| 84 | |
| 85 | ### Functions Using Slices |
| 86 | |
| 87 | ```rust |
| 88 | let mut buffer = vec![0xFE; 10]; |
| 89 | let mut pos = 1; |
| 90 | |
| 91 | // Encode an unsigned 64 bit number with a mutable slice: |
| 92 | let result = leb128fmt::encode_uint_slice::<u64, 64>(43110u64, &mut buffer, &mut pos); |
| 93 | // The number of bytes written in the output array |
| 94 | assert_eq!(result, Some(3)); |
| 95 | assert_eq!(pos, 4); |
| 96 | |
| 97 | assert_eq!(buffer, [0xFE, 0xE6, 0xD0, 0x02, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE]); |
| 98 | |
| 99 | // Decode an unsigned 64 bit number with a slice: |
| 100 | pos = 1; |
| 101 | let result = leb128fmt::decode_uint_slice::<u64, 64>(&buffer, &mut pos); |
| 102 | assert_eq!(result, Ok(43110)); |
| 103 | assert_eq!(pos, 4); |
| 104 | ``` |
| 105 | |
| 106 | ### Functions Using Fixed Sized Encoding |
| 107 | |
| 108 | There may be several different ways to encode a value. For instance, `0` can |
| 109 | be encoded as 32 bits unsigned: |
| 110 | |
| 111 | ```rust |
| 112 | let mut pos = 0; |
| 113 | assert_eq!(leb128fmt::decode_uint_slice::<u32, 32>(&[0x00], &mut pos), Ok(0)); |
| 114 | pos = 0; |
| 115 | assert_eq!(leb128fmt::decode_uint_slice::<u32, 32>(&[0x80, 0x00], &mut pos), Ok(0)); |
| 116 | pos = 0; |
| 117 | assert_eq!(leb128fmt::decode_uint_slice::<u32, 32>(&[0x80, 0x80, 0x00], &mut pos), Ok(0)); |
| 118 | pos = 0; |
| 119 | assert_eq!(leb128fmt::decode_uint_slice::<u32, 32>(&[0x80, 0x80, 0x80, 0x00], &mut pos), Ok(0)); |
| 120 | pos = 0; |
| 121 | assert_eq!(leb128fmt::decode_uint_slice::<u32, 32>(&[0x80, 0x80, 0x80, 0x80, 0x00], &mut pos), Ok(0)); |
| 122 | ``` |
| 123 | |
| 124 | There are functions provided to encode a value using the maximum number of |
| 125 | bytes possible for a given bit size. Using the maximum number of bytes |
| 126 | removes the benefit of compression, but it may be useful in a few scenarios. |
| 127 | |
| 128 | For instance, if a binary format needs to store the size or offset of some |
| 129 | data before the size of data is known, it can be beneficial to write a fixed |
| 130 | sized `0` placeholder value first. Then, once the real value is known, the |
| 131 | `0` placeholder can be overwritten without moving other bytes. The real |
| 132 | value is also written out using the fixed maximum number of bytes. |
| 133 | |
| 134 | ```rust |
| 135 | // Encode an unsigned 32 bit number with all 5 bytes: |
| 136 | let output = leb128fmt::encode_fixed_u32(43110).unwrap(); |
| 137 | assert_eq!(output, [0xE6, 0xD0, 0x82, 0x80, 0x00]); |
| 138 | |
| 139 | // Decode an unsigned 32 bit number: |
| 140 | let input = output; |
| 141 | let (result, read_len) = leb128fmt::decode_u32(input).unwrap(); |
| 142 | assert_eq!(result, 43110); |
| 143 | |
| 144 | // Note that all 5 bytes are read |
| 145 | assert_eq!(read_len, 5); |
| 146 | ``` |
| 147 | |
| 148 | ## License |
| 149 | |
| 150 | Licensed under either of [Apache License, Version 2.0][LICENSE_APACHE] or [MIT |
| 151 | License][LICENSE_MIT] at your option. |
| 152 | |
| 153 | ### Contributions |
| 154 | |
| 155 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 156 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 157 | dual licensed as above, without any additional terms or conditions. |
| 158 | |
| 159 | [LICENSE_APACHE]: LICENSE-APACHE |
| 160 | [LICENSE_MIT]: LICENSE-MIT |
| 161 | [leb128_wiki]: https://en.wikipedia.org/wiki/LEB128 |
| 162 | [api_docs]: https://docs.rs/leb128fmt/ |