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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use std::fmt;
use std::error::Error;
use std::result;
#[cfg(test)]
mod tests;
pub struct BitReader<'a> {
bytes: &'a [u8],
position: u64,
relative_offset: u64,
}
impl<'a> BitReader<'a> {
pub fn new(bytes: &'a [u8]) -> BitReader<'a> {
BitReader {
bytes: bytes,
position: 0,
relative_offset: 0,
}
}
pub fn relative_reader(&self) -> BitReader<'a> {
BitReader {
bytes: self.bytes,
position: self.position,
relative_offset: self.position,
}
}
pub fn read_u8(&mut self, bit_count: u8) -> Result<u8> {
let value = try!(self.read_value(bit_count, 8));
Ok((value & 0xff) as u8)
}
pub fn read_u16(&mut self, bit_count: u8) -> Result<u16> {
let value = try!(self.read_value(bit_count, 16));
Ok((value & 0xffff) as u16)
}
pub fn read_u32(&mut self, bit_count: u8) -> Result<u32> {
let value = try!(self.read_value(bit_count, 32));
Ok((value & 0xffffffff) as u32)
}
pub fn read_u64(&mut self, bit_count: u8) -> Result<u64> {
let value = try!(self.read_value(bit_count, 64));
Ok(value)
}
pub fn read_i8(&mut self, bit_count: u8) -> Result<i8> {
let value = try!(self.read_signed_value(bit_count, 8));
Ok((value & 0xff) as i8)
}
pub fn read_i16(&mut self, bit_count: u8) -> Result<i16> {
let value = try!(self.read_signed_value(bit_count, 16));
Ok((value & 0xffff) as i16)
}
pub fn read_i32(&mut self, bit_count: u8) -> Result<i32> {
let value = try!(self.read_signed_value(bit_count, 32));
Ok((value & 0xffffffff) as i32)
}
pub fn read_i64(&mut self, bit_count: u8) -> Result<i64> {
let value = try!(self.read_signed_value(bit_count, 64));
Ok(value)
}
pub fn read_bool(&mut self) -> Result<bool> {
match try!(self.read_value(1, 1)) {
0 => Ok(false),
_ => Ok(true),
}
}
pub fn skip(&mut self, bit_count: u64) -> Result<()> {
let end_position = self.position + bit_count;
if end_position > self.bytes.len() as u64 * 8 {
return Err(BitReaderError::NotEnoughData {
position: self.position,
length: (self.bytes.len() * 8) as u64,
requested: bit_count,
});
}
self.position = end_position;
Ok(())
}
pub fn position(&self) -> u64 {
self.position - self.relative_offset
}
pub fn is_aligned(&self, alignment_bytes: u32) -> bool {
self.position % (alignment_bytes as u64 * 8) == 0
}
fn read_signed_value(&mut self, bit_count: u8, maximum_count: u8) -> Result<i64> {
let unsigned = try!(self.read_value(bit_count, maximum_count));
let sign_bit = unsigned >> (bit_count - 1) & 1;
let high_bits = if sign_bit == 1 { -1 } else { 0 };
Ok(high_bits << bit_count | unsigned as i64)
}
fn read_value(&mut self, bit_count: u8, maximum_count: u8) -> Result<u64> {
if bit_count == 0 {
return Ok(0);
}
if bit_count > maximum_count {
return Err(BitReaderError::TooManyBitsForType {
position: self.position,
requested: bit_count,
allowed: maximum_count,
});
}
let start_position = self.position;
let end_position = self.position + bit_count as u64;
if end_position > self.bytes.len() as u64 * 8 {
return Err(BitReaderError::NotEnoughData {
position: self.position,
length: (self.bytes.len() * 8) as u64,
requested: bit_count as u64,
});
}
let mut value: u64 = 0;
for i in start_position..end_position {
let byte_index = (i / 8) as usize;
let byte = self.bytes[byte_index];
let shift = 7 - (i % 8);
let bit = (byte >> shift) as u64 & 1;
value = (value << 1) | bit;
}
self.position = end_position;
Ok(value)
}
}
pub type Result<T> = result::Result<T, BitReaderError>;
#[derive(Debug,PartialEq,Copy,Clone)]
pub enum BitReaderError {
NotEnoughData {
position: u64,
length: u64,
requested: u64,
},
TooManyBitsForType {
position: u64,
requested: u8,
allowed: u8,
}
}
impl Error for BitReaderError {
fn description(&self) -> &str {
match *self {
BitReaderError::NotEnoughData {..} => "Requested more bits than the byte slice has left",
BitReaderError::TooManyBitsForType {..} => "Requested more bits than the requested integer type can hold",
}
}
}
impl fmt::Display for BitReaderError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
BitReaderError::NotEnoughData { position, length, requested } => write!(fmt, "BitReader: Requested {} bits with only {}/{} bits left (position {})", requested, length - position, length, position),
BitReaderError::TooManyBitsForType { position, requested, allowed } => write!(fmt, "BitReader: Requested {} bits while the type can only hold {} (position {})", requested, allowed, position),
}
}
}
pub trait ReadInto
where Self: Sized
{
fn read(reader: &mut BitReader, bits: u8) -> Result<Self>;
}
macro_rules! impl_read_into {
($T:ty, $method:ident) => (
impl ReadInto for $T {
fn read(reader: &mut BitReader, bits: u8) -> Result<Self> {
reader.$method(bits)
}
}
)
}
impl_read_into!(u8, read_u8);
impl_read_into!(u16, read_u16);
impl_read_into!(u32, read_u32);
impl_read_into!(u64, read_u64);
impl_read_into!(i8, read_i8);
impl_read_into!(i16, read_i16);
impl_read_into!(i32, read_i32);
impl_read_into!(i64, read_i64);
impl ReadInto for bool {
fn read(reader: &mut BitReader, bits: u8) -> Result<Self> {
match try!(reader.read_u8(bits)) {
0 => Ok(false),
_ => Ok(true),
}
}
}