1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
extern crate codicon;
extern crate signrel;
extern crate uabs;
#[cfg(test)]
extern crate leb128;
#[cfg(test)]
mod tests;
use uabs::UnsignedAbs;
use signrel::SignRel;
use std::cmp::min;
use std::slice;
use std::error;
use std::fmt;
use std::mem;
use std::io;
pub struct Leb128;
#[derive(Debug)]
pub enum Error {
IoError(io::Error),
Overflow
}
impl error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::IoError(ref e) => e.fmt(f),
Error::Overflow => write!(f, "LEB128 integer overflow"),
}
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::IoError(e)
}
}
trait WriteByte: io::Write {
fn write_byte(&mut self, byte: u8) -> io::Result<()> {
self.write_all(slice::from_ref(&byte))?;
Ok(())
}
}
trait ReadByte: io::Read {
fn read_byte(&mut self) -> io::Result<u8> {
let mut byte = 0u8;
self.read_exact(slice::from_mut(&mut byte))?;
Ok(byte)
}
}
trait ByteMax: SignRel {
const MAX: Self::Unsigned;
}
const CONT: u8 = 0b10000000;
impl<T: io::Write> WriteByte for T {}
impl<T: io::Read> ReadByte for T {}
macro_rules! leb_impl {
($($s:ident:$u:ident)*) => (
$(
impl ByteMax for $s {
const MAX: Self::Unsigned = 0b00111111;
}
impl ByteMax for $u {
const MAX: Self::Unsigned = 0b01111111;
}
leb_impl! { $s }
leb_impl! { $u }
)*
);
($t:ident) => (
impl codicon::Decoder<Leb128, Error> for $t {
fn decode<R: io::Read>(reader: &mut R, _: Leb128) -> Result<Self, Error> {
const BITS: u32 = mem::size_of::<$t>() as u32 * 8;
let mut value = <Self as SignRel>::Unsigned::from(0u8);
let mut shift = 0u32;
let mut byte = CONT;
while byte & CONT == CONT {
if shift > BITS {
return Err(Error::Overflow);
}
byte = reader.read_byte()?;
let low = <Self as SignRel>::Unsigned::from(byte & !CONT);
value |= low << shift;
shift += 7;
}
if shift > BITS {
let offs = 1 - (Self::MAX >> 6);
let mask = 0b11111111 << 7 - (shift - BITS) >> offs;
if byte & mask != mask && byte & mask != 0 {
return Err(Error::Overflow);
}
}
let off = BITS - min(shift, BITS);
value <<= off;
Ok(value as Self >> off)
}
}
impl codicon::Encoder<Leb128, io::Error> for $t {
fn encode<W: io::Write>(&self, writer: &mut W, _: Leb128) -> Result<(), io::Error> {
let mut value = *self;
while value.uabs() > Self::MAX {
writer.write_byte(value as u8 | CONT)?;
value >>= 7;
}
Ok(writer.write_byte(value as u8 & !CONT)?)
}
}
);
}
leb_impl! { isize:usize i128:u128 i64:u64 i32:u32 i16:u16 i8:u8 }