1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 pub fn write_to_vec(vec: &mut Vec<u8>, byte: u8) {
16 #[cfg(target_pointer_width = "32")]
17 const USIZE_LEB128_SIZE: usize = 5;
18 #[cfg(target_pointer_width = "64")]
19 const USIZE_LEB128_SIZE: usize = 10;
21 macro_rules! leb128_size {
26 (usize) => (USIZE_LEB128_SIZE);
29 macro_rules! impl_write_unsigned_leb128 {
30 ($fn_name:ident, $int_ty:ident) => (
32 pub fn $fn_name(out: &mut Vec<u8>, mut value: $int_ty) {
33 for _ in 0 .. leb128_size!($int_ty) {
34 let mut byte = (value & 0x7F) as u8;
40 write_to_vec(out, byte);
50 impl_write_unsigned_leb128!(write_u16_leb128, u16);
51 impl_write_unsigned_leb128!(write_u32_leb128, u32);
52 impl_write_unsigned_leb128!(write_u64_leb128, u64);
53 impl_write_unsigned_leb128!(write_u128_leb128, u128);
54 impl_write_unsigned_leb128!(write_usize_leb128, usize);
57 macro_rules! impl_read_unsigned_leb128 {
58 ($fn_name:ident, $int_ty:ident) => (
60 pub fn $fn_name(slice: &[u8]) -> ($int_ty, usize) {
61 let mut result: $int_ty = 0;
65 for _ in 0 .. leb128_size!($int_ty) {
67 *slice.get_unchecked(position)
70 result |= ((byte & 0x7F) as $int_ty) << shift;
71 if (byte & 0x80) == 0 {
77 // Do a single bounds check at the end instead of for every byte.
78 assert!(position <= slice.len());
85 impl_read_unsigned_leb128!(read_u16_leb128, u16);
86 impl_read_unsigned_leb128!(read_u32_leb128, u32);
87 impl_read_unsigned_leb128!(read_u64_leb128, u64);
88 impl_read_unsigned_leb128!(read_u128_leb128, u128);
89 impl_read_unsigned_leb128!(read_usize_leb128, usize);
94 /// encodes an integer using signed leb128 encoding and stores
95 /// the result using a callback function.
97 /// The callback `write` is called once for each position
98 /// that is to be written to with the byte to be encoded
100 pub fn write_signed_leb128_to<W>(mut value: i128, mut write: W)
104 let mut byte = (value as u8) & 0x7f;
106 let more = !((((value == 0) && ((byte & 0x40) == 0)) ||
107 ((value == -1) && ((byte & 0x40) != 0))));
110 byte |= 0x80; // Mark this byte to show that more bytes will follow.
121 pub fn write_signed_leb128(out: &mut Vec<u8>, value: i128) {
122 write_signed_leb128_to(value, |v| write_to_vec(out, v))
126 pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i128, usize) {
129 let mut position = start_position;
133 byte = data[position];
135 result |= ((byte & 0x7F) as i128) << shift;
138 if (byte & 0x80) == 0 {
143 if (shift < 64) && ((byte & 0x40) != 0) {
145 result |= -(1 << shift);
148 (result, position - start_position)
151 macro_rules! impl_test_unsigned_leb128 {
152 ($test_name:ident, $write_fn_name:ident, $read_fn_name:ident, $int_ty:ident) => (
155 let mut stream = Vec::new();
158 $write_fn_name(&mut stream, (3u64 << x) as $int_ty);
161 let mut position = 0;
163 let expected = (3u64 << x) as $int_ty;
164 let (actual, bytes_read) = $read_fn_name(&stream[position ..]);
165 assert_eq!(expected, actual);
166 position += bytes_read;
168 assert_eq!(stream.len(), position);
173 impl_test_unsigned_leb128!(test_u16_leb128, write_u16_leb128, read_u16_leb128, u16);
174 impl_test_unsigned_leb128!(test_u32_leb128, write_u32_leb128, read_u32_leb128, u32);
175 impl_test_unsigned_leb128!(test_u64_leb128, write_u64_leb128, read_u64_leb128, u64);
176 impl_test_unsigned_leb128!(test_u128_leb128, write_u128_leb128, read_u128_leb128, u128);
177 impl_test_unsigned_leb128!(test_usize_leb128, write_usize_leb128, read_usize_leb128, usize);
180 fn test_signed_leb128() {
181 let values: Vec<_> = (-500..500).map(|i| i * 0x12345789ABCDEF).collect();
182 let mut stream = Vec::new();
184 write_signed_leb128(&mut stream, x);
188 let (value, bytes_read) = read_signed_leb128(&mut stream, pos);
190 assert_eq!(x, value);
192 assert_eq!(pos, stream.len());