2015-12-25 13:59:02 -05:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
#[inline]
|
2018-01-09 16:53:35 +01:00
|
|
|
pub fn write_to_vec(vec: &mut Vec<u8>, position: usize, byte: u8) {
|
2016-12-14 01:45:03 +02:00
|
|
|
if position == vec.len() {
|
2015-12-25 13:59:02 -05:00
|
|
|
vec.push(byte);
|
|
|
|
} else {
|
2016-12-14 01:45:03 +02:00
|
|
|
vec[position] = byte;
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
const USIZE_LEB128_SIZE: usize = 5;
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
|
|
|
const USIZE_LEB128_SIZE: usize = 10;
|
|
|
|
|
|
|
|
macro_rules! leb128_size {
|
|
|
|
(u16) => (3);
|
|
|
|
(u32) => (5);
|
|
|
|
(u64) => (10);
|
|
|
|
(u128) => (19);
|
|
|
|
(usize) => (USIZE_LEB128_SIZE);
|
|
|
|
}
|
2015-12-25 13:59:02 -05:00
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
macro_rules! impl_write_unsigned_leb128 {
|
|
|
|
($fn_name:ident, $int_ty:ident) => (
|
|
|
|
#[inline]
|
|
|
|
pub fn $fn_name(out: &mut Vec<u8>, start_position: usize, mut value: $int_ty) -> usize {
|
|
|
|
let mut position = start_position;
|
|
|
|
for _ in 0 .. leb128_size!($int_ty) {
|
|
|
|
let mut byte = (value & 0x7F) as u8;
|
|
|
|
value >>= 7;
|
|
|
|
if value != 0 {
|
|
|
|
byte |= 0x80;
|
|
|
|
}
|
|
|
|
|
|
|
|
write_to_vec(out, position, byte);
|
|
|
|
position += 1;
|
|
|
|
|
|
|
|
if value == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
position - start_position
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
2018-01-09 16:53:35 +01:00
|
|
|
)
|
2016-12-14 01:45:03 +02:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
impl_write_unsigned_leb128!(write_u16_leb128, u16);
|
|
|
|
impl_write_unsigned_leb128!(write_u32_leb128, u32);
|
|
|
|
impl_write_unsigned_leb128!(write_u64_leb128, u64);
|
|
|
|
impl_write_unsigned_leb128!(write_u128_leb128, u128);
|
|
|
|
impl_write_unsigned_leb128!(write_usize_leb128, usize);
|
|
|
|
|
|
|
|
|
|
|
|
macro_rules! impl_read_unsigned_leb128 {
|
|
|
|
($fn_name:ident, $int_ty:ident) => (
|
|
|
|
#[inline]
|
|
|
|
pub fn $fn_name(slice: &[u8]) -> ($int_ty, usize) {
|
|
|
|
let mut result: $int_ty = 0;
|
|
|
|
let mut shift = 0;
|
|
|
|
let mut position = 0;
|
|
|
|
|
|
|
|
for _ in 0 .. leb128_size!($int_ty) {
|
|
|
|
let byte = unsafe {
|
|
|
|
*slice.get_unchecked(position)
|
|
|
|
};
|
|
|
|
position += 1;
|
|
|
|
result |= ((byte & 0x7F) as $int_ty) << shift;
|
|
|
|
if (byte & 0x80) == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
shift += 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a single bounds check at the end instead of for every byte.
|
|
|
|
assert!(position <= slice.len());
|
|
|
|
|
|
|
|
(result, position)
|
|
|
|
}
|
|
|
|
)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
impl_read_unsigned_leb128!(read_u16_leb128, u16);
|
|
|
|
impl_read_unsigned_leb128!(read_u32_leb128, u32);
|
|
|
|
impl_read_unsigned_leb128!(read_u64_leb128, u64);
|
|
|
|
impl_read_unsigned_leb128!(read_u128_leb128, u128);
|
|
|
|
impl_read_unsigned_leb128!(read_usize_leb128, usize);
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
|
|
|
|
|
2016-12-14 01:45:03 +02:00
|
|
|
#[inline]
|
|
|
|
/// encodes an integer using signed leb128 encoding and stores
|
|
|
|
/// the result using a callback function.
|
|
|
|
///
|
|
|
|
/// The callback `write` is called once for each position
|
|
|
|
/// that is to be written to with the byte to be encoded
|
|
|
|
/// at that position.
|
2016-08-23 03:56:52 +03:00
|
|
|
pub fn write_signed_leb128_to<W>(mut value: i128, mut write: W) -> usize
|
2016-12-14 01:45:03 +02:00
|
|
|
where W: FnMut(usize, u8)
|
|
|
|
{
|
|
|
|
let mut position = 0;
|
2015-12-25 13:59:02 -05:00
|
|
|
|
|
|
|
loop {
|
|
|
|
let mut byte = (value as u8) & 0x7f;
|
|
|
|
value >>= 7;
|
2016-01-19 14:30:48 +13:00
|
|
|
let more = !((((value == 0) && ((byte & 0x40) == 0)) ||
|
2015-12-25 13:59:02 -05:00
|
|
|
((value == -1) && ((byte & 0x40) != 0))));
|
2016-08-24 15:58:40 +03:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
if more {
|
|
|
|
byte |= 0x80; // Mark this byte to show that more bytes will follow.
|
|
|
|
}
|
|
|
|
|
2016-12-14 01:45:03 +02:00
|
|
|
write(position, byte);
|
|
|
|
position += 1;
|
2016-08-24 15:58:40 +03:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
if !more {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-12-14 01:45:03 +02:00
|
|
|
position
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:56:52 +03:00
|
|
|
pub fn write_signed_leb128(out: &mut Vec<u8>, start_position: usize, value: i128) -> usize {
|
2016-12-14 01:45:03 +02:00
|
|
|
write_signed_leb128_to(value, |i, v| write_to_vec(out, start_position+i, v))
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2016-08-23 03:56:52 +03:00
|
|
|
pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i128, usize) {
|
2016-08-24 15:58:40 +03:00
|
|
|
let mut result = 0;
|
|
|
|
let mut shift = 0;
|
|
|
|
let mut position = start_position;
|
|
|
|
let mut byte;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
byte = data[position];
|
|
|
|
position += 1;
|
|
|
|
result |= ((byte & 0x7F) as i128) << shift;
|
|
|
|
shift += 7;
|
|
|
|
|
|
|
|
if (byte & 0x80) == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shift < 64) && ((byte & 0x40) != 0) {
|
|
|
|
// sign extend
|
|
|
|
result |= -(1 << shift);
|
|
|
|
}
|
|
|
|
|
|
|
|
(result, position - start_position)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
macro_rules! impl_test_unsigned_leb128 {
|
|
|
|
($test_name:ident, $write_fn_name:ident, $read_fn_name:ident, $int_ty:ident) => (
|
|
|
|
#[test]
|
|
|
|
fn $test_name() {
|
|
|
|
let mut stream = Vec::new();
|
|
|
|
|
|
|
|
for x in 0..62 {
|
|
|
|
let pos = stream.len();
|
|
|
|
let bytes_written = $write_fn_name(&mut stream, pos, (3u64 << x) as $int_ty);
|
|
|
|
assert_eq!(stream.len(), pos + bytes_written);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut position = 0;
|
|
|
|
for x in 0..62 {
|
|
|
|
let expected = (3u64 << x) as $int_ty;
|
|
|
|
let (actual, bytes_read) = $read_fn_name(&stream[position ..]);
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
position += bytes_read;
|
|
|
|
}
|
|
|
|
assert_eq!(stream.len(), position);
|
|
|
|
}
|
|
|
|
)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
impl_test_unsigned_leb128!(test_u16_leb128, write_u16_leb128, read_u16_leb128, u16);
|
|
|
|
impl_test_unsigned_leb128!(test_u32_leb128, write_u32_leb128, read_u32_leb128, u32);
|
|
|
|
impl_test_unsigned_leb128!(test_u64_leb128, write_u64_leb128, read_u64_leb128, u64);
|
|
|
|
impl_test_unsigned_leb128!(test_u128_leb128, write_u128_leb128, read_u128_leb128, u128);
|
|
|
|
impl_test_unsigned_leb128!(test_usize_leb128, write_usize_leb128, read_usize_leb128, usize);
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
#[test]
|
|
|
|
fn test_signed_leb128() {
|
2016-08-25 02:16:21 +03:00
|
|
|
let values: Vec<_> = (-500..500).map(|i| i * 0x12345789ABCDEF).collect();
|
2015-12-25 13:59:02 -05:00
|
|
|
let mut stream = Vec::new();
|
|
|
|
for &x in &values {
|
|
|
|
let pos = stream.len();
|
|
|
|
let bytes_written = write_signed_leb128(&mut stream, pos, x);
|
|
|
|
assert_eq!(stream.len(), pos + bytes_written);
|
|
|
|
}
|
|
|
|
let mut pos = 0;
|
|
|
|
for &x in &values {
|
|
|
|
let (value, bytes_read) = read_signed_leb128(&mut stream, pos);
|
|
|
|
pos += bytes_read;
|
|
|
|
assert_eq!(x, value);
|
|
|
|
}
|
|
|
|
assert_eq!(pos, stream.len());
|
|
|
|
}
|