|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 | 11 | #[inline]
|
12 |
| -fn write_to_vec(vec: &mut Vec<u8>, position: usize, byte: u8) { |
| 12 | +pub fn write_to_vec(vec: &mut Vec<u8>, position: usize, byte: u8) { |
13 | 13 | if position == vec.len() {
|
14 | 14 | vec.push(byte);
|
15 | 15 | } else {
|
16 | 16 | vec[position] = byte;
|
17 | 17 | }
|
18 | 18 | }
|
19 | 19 |
|
20 |
| -#[inline] |
21 |
| -/// encodes an integer using unsigned leb128 encoding and stores |
22 |
| -/// the result using a callback function. |
23 |
| -/// |
24 |
| -/// The callback `write` is called once for each position |
25 |
| -/// that is to be written to with the byte to be encoded |
26 |
| -/// at that position. |
27 |
| -pub fn write_unsigned_leb128_to<W>(mut value: u128, mut write: W) -> usize |
28 |
| - where W: FnMut(usize, u8) |
29 |
| -{ |
30 |
| - let mut position = 0; |
31 |
| - loop { |
32 |
| - let mut byte = (value & 0x7F) as u8; |
33 |
| - value >>= 7; |
34 |
| - if value != 0 { |
35 |
| - byte |= 0x80; |
36 |
| - } |
37 |
| - |
38 |
| - write(position, byte); |
39 |
| - position += 1; |
| 20 | +#[cfg(target_pointer_width = "32")] |
| 21 | +const USIZE_LEB128_SIZE: usize = 5; |
| 22 | +#[cfg(target_pointer_width = "64")] |
| 23 | +const USIZE_LEB128_SIZE: usize = 10; |
| 24 | + |
| 25 | +macro_rules! leb128_size { |
| 26 | + (u16) => (3); |
| 27 | + (u32) => (5); |
| 28 | + (u64) => (10); |
| 29 | + (u128) => (19); |
| 30 | + (usize) => (USIZE_LEB128_SIZE); |
| 31 | +} |
40 | 32 |
|
41 |
| - if value == 0 { |
42 |
| - break; |
| 33 | +macro_rules! impl_write_unsigned_leb128 { |
| 34 | + ($fn_name:ident, $int_ty:ident) => ( |
| 35 | + #[inline] |
| 36 | + pub fn $fn_name(out: &mut Vec<u8>, start_position: usize, mut value: $int_ty) -> usize { |
| 37 | + let mut position = start_position; |
| 38 | + for _ in 0 .. leb128_size!($int_ty) { |
| 39 | + let mut byte = (value & 0x7F) as u8; |
| 40 | + value >>= 7; |
| 41 | + if value != 0 { |
| 42 | + byte |= 0x80; |
| 43 | + } |
| 44 | + |
| 45 | + write_to_vec(out, position, byte); |
| 46 | + position += 1; |
| 47 | + |
| 48 | + if value == 0 { |
| 49 | + break; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + position - start_position |
43 | 54 | }
|
44 |
| - } |
45 |
| - |
46 |
| - position |
| 55 | + ) |
47 | 56 | }
|
48 | 57 |
|
49 |
| -pub fn write_unsigned_leb128(out: &mut Vec<u8>, start_position: usize, value: u128) -> usize { |
50 |
| - write_unsigned_leb128_to(value, |i, v| write_to_vec(out, start_position+i, v)) |
| 58 | +impl_write_unsigned_leb128!(write_u16_leb128, u16); |
| 59 | +impl_write_unsigned_leb128!(write_u32_leb128, u32); |
| 60 | +impl_write_unsigned_leb128!(write_u64_leb128, u64); |
| 61 | +impl_write_unsigned_leb128!(write_u128_leb128, u128); |
| 62 | +impl_write_unsigned_leb128!(write_usize_leb128, usize); |
| 63 | + |
| 64 | + |
| 65 | +macro_rules! impl_read_unsigned_leb128 { |
| 66 | + ($fn_name:ident, $int_ty:ident) => ( |
| 67 | + #[inline] |
| 68 | + pub fn $fn_name(slice: &[u8]) -> ($int_ty, usize) { |
| 69 | + let mut result: $int_ty = 0; |
| 70 | + let mut shift = 0; |
| 71 | + let mut position = 0; |
| 72 | + |
| 73 | + for _ in 0 .. leb128_size!($int_ty) { |
| 74 | + let byte = unsafe { |
| 75 | + *slice.get_unchecked(position) |
| 76 | + }; |
| 77 | + position += 1; |
| 78 | + result |= ((byte & 0x7F) as $int_ty) << shift; |
| 79 | + if (byte & 0x80) == 0 { |
| 80 | + break; |
| 81 | + } |
| 82 | + shift += 7; |
| 83 | + } |
| 84 | + |
| 85 | + // Do a single bounds check at the end instead of for every byte. |
| 86 | + assert!(position <= slice.len()); |
| 87 | + |
| 88 | + (result, position) |
| 89 | + } |
| 90 | + ) |
51 | 91 | }
|
52 | 92 |
|
53 |
| -#[inline] |
54 |
| -pub fn read_unsigned_leb128(data: &[u8], start_position: usize) -> (u128, usize) { |
55 |
| - let mut result = 0; |
56 |
| - let mut shift = 0; |
57 |
| - let mut position = start_position; |
58 |
| - loop { |
59 |
| - let byte = data[position]; |
60 |
| - position += 1; |
61 |
| - result |= ((byte & 0x7F) as u128) << shift; |
62 |
| - if (byte & 0x80) == 0 { |
63 |
| - break; |
64 |
| - } |
65 |
| - shift += 7; |
66 |
| - } |
| 93 | +impl_read_unsigned_leb128!(read_u16_leb128, u16); |
| 94 | +impl_read_unsigned_leb128!(read_u32_leb128, u32); |
| 95 | +impl_read_unsigned_leb128!(read_u64_leb128, u64); |
| 96 | +impl_read_unsigned_leb128!(read_u128_leb128, u128); |
| 97 | +impl_read_unsigned_leb128!(read_usize_leb128, usize); |
| 98 | + |
67 | 99 |
|
68 |
| - (result, position - start_position) |
69 |
| -} |
70 | 100 |
|
71 | 101 | #[inline]
|
72 | 102 | /// encodes an integer using signed leb128 encoding and stores
|
@@ -130,26 +160,36 @@ pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i128, usize) {
|
130 | 160 | (result, position - start_position)
|
131 | 161 | }
|
132 | 162 |
|
133 |
| -#[test] |
134 |
| -fn test_unsigned_leb128() { |
135 |
| - let mut stream = Vec::with_capacity(10000); |
136 |
| - |
137 |
| - for x in 0..62 { |
138 |
| - let pos = stream.len(); |
139 |
| - let bytes_written = write_unsigned_leb128(&mut stream, pos, 3 << x); |
140 |
| - assert_eq!(stream.len(), pos + bytes_written); |
141 |
| - } |
142 |
| - |
143 |
| - let mut position = 0; |
144 |
| - for x in 0..62 { |
145 |
| - let expected = 3 << x; |
146 |
| - let (actual, bytes_read) = read_unsigned_leb128(&stream, position); |
147 |
| - assert_eq!(expected, actual); |
148 |
| - position += bytes_read; |
149 |
| - } |
150 |
| - assert_eq!(stream.len(), position); |
| 163 | +macro_rules! impl_test_unsigned_leb128 { |
| 164 | + ($test_name:ident, $write_fn_name:ident, $read_fn_name:ident, $int_ty:ident) => ( |
| 165 | + #[test] |
| 166 | + fn $test_name() { |
| 167 | + let mut stream = Vec::new(); |
| 168 | + |
| 169 | + for x in 0..62 { |
| 170 | + let pos = stream.len(); |
| 171 | + let bytes_written = $write_fn_name(&mut stream, pos, (3u64 << x) as $int_ty); |
| 172 | + assert_eq!(stream.len(), pos + bytes_written); |
| 173 | + } |
| 174 | + |
| 175 | + let mut position = 0; |
| 176 | + for x in 0..62 { |
| 177 | + let expected = (3u64 << x) as $int_ty; |
| 178 | + let (actual, bytes_read) = $read_fn_name(&stream[position ..]); |
| 179 | + assert_eq!(expected, actual); |
| 180 | + position += bytes_read; |
| 181 | + } |
| 182 | + assert_eq!(stream.len(), position); |
| 183 | + } |
| 184 | + ) |
151 | 185 | }
|
152 | 186 |
|
| 187 | +impl_test_unsigned_leb128!(test_u16_leb128, write_u16_leb128, read_u16_leb128, u16); |
| 188 | +impl_test_unsigned_leb128!(test_u32_leb128, write_u32_leb128, read_u32_leb128, u32); |
| 189 | +impl_test_unsigned_leb128!(test_u64_leb128, write_u64_leb128, read_u64_leb128, u64); |
| 190 | +impl_test_unsigned_leb128!(test_u128_leb128, write_u128_leb128, read_u128_leb128, u128); |
| 191 | +impl_test_unsigned_leb128!(test_usize_leb128, write_usize_leb128, read_usize_leb128, usize); |
| 192 | + |
153 | 193 | #[test]
|
154 | 194 | fn test_signed_leb128() {
|
155 | 195 | let values: Vec<_> = (-500..500).map(|i| i * 0x12345789ABCDEF).collect();
|
|
0 commit comments