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 fn write_to_vec(vec: &mut Vec<u8>, position: usize, byte: u8) {
13 if position == vec.len() {
21 /// encodes an integer using unsigned leb128 encoding and stores
22 /// the result using a callback function.
24 /// The callback `write` is called once for each position
25 /// that is to be written to with the byte to be encoded
27 pub fn write_unsigned_leb128_to<W>(mut value: u128, mut write: W) -> usize
28 where W: FnMut(usize, u8)
32 let mut byte = (value & 0x7F) as u8;
38 write(position, byte);
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))
54 pub fn read_unsigned_leb128(data: &[u8], start_position: usize) -> (u128, usize) {
57 let mut position = start_position;
59 let byte = data[position];
61 result |= ((byte & 0x7F) as u128) << shift;
62 if (byte & 0x80) == 0 {
68 (result, position - start_position)
72 /// encodes an integer using signed leb128 encoding and stores
73 /// the result using a callback function.
75 /// The callback `write` is called once for each position
76 /// that is to be written to with the byte to be encoded
78 pub fn write_signed_leb128_to<W>(mut value: i128, mut write: W) -> usize
79 where W: FnMut(usize, u8)
84 let mut byte = (value as u8) & 0x7f;
86 let more = !((((value == 0) && ((byte & 0x40) == 0)) ||
87 ((value == -1) && ((byte & 0x40) != 0))));
90 byte |= 0x80; // Mark this byte to show that more bytes will follow.
93 write(position, byte);
103 pub fn write_signed_leb128(out: &mut Vec<u8>, start_position: usize, value: i128) -> usize {
104 write_signed_leb128_to(value, |i, v| write_to_vec(out, start_position+i, v))
108 pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i128, usize) {
111 let mut position = start_position;
115 byte = data[position];
117 result |= ((byte & 0x7F) as i128) << shift;
120 if (byte & 0x80) == 0 {
125 if (shift < 64) && ((byte & 0x40) != 0) {
127 result |= -(1 << shift);
130 (result, position - start_position)
134 fn test_unsigned_leb128() {
135 let mut stream = Vec::with_capacity(10000);
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);
143 let mut position = 0;
145 let expected = 3 << x;
146 let (actual, bytes_read) = read_unsigned_leb128(&stream, position);
147 assert_eq!(expected, actual);
148 position += bytes_read;
150 assert_eq!(stream.len(), position);
154 fn test_signed_leb128() {
155 let values: Vec<_> = (-500..500).map(|i| i * 0x12345789ABCDEF).collect();
156 let mut stream = Vec::new();
158 let pos = stream.len();
159 let bytes_written = write_signed_leb128(&mut stream, pos, x);
160 assert_eq!(stream.len(), pos + bytes_written);
164 let (value, bytes_read) = read_signed_leb128(&mut stream, pos);
166 assert_eq!(x, value);
168 assert_eq!(pos, stream.len());