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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use std::io::Read;
use std::path::Path;
use std::fs::File;
use errors::{Result, ErrorKind};
use byteorder::LittleEndian as LE;
use byteorder::ByteOrder;
const WIRE_TYPE_VARINT: u8 = 0;
const WIRE_TYPE_FIXED64: u8 = 1;
const WIRE_TYPE_LENGTH_DELIMITED: u8 = 2;
const WIRE_TYPE_START_GROUP: u8 = 3;
const WIRE_TYPE_END_GROUP: u8 = 4;
const WIRE_TYPE_FIXED32: u8 = 5;
#[derive(Debug, Clone, PartialEq)]
pub struct BytesReader {
start: usize,
end: usize,
}
impl BytesReader {
pub fn from_bytes(bytes: &[u8]) -> BytesReader {
BytesReader {
start: 0,
end: bytes.len()
}
}
#[inline(always)]
pub fn next_tag(&mut self, bytes: &[u8]) -> Result<u32> {
self.read_varint32(bytes)
}
#[inline(always)]
fn read_u8(&mut self, bytes: &[u8]) -> u8 {
let b = bytes[self.start];
self.start += 1;
b
}
#[inline(always)]
pub fn read_varint32(&mut self, bytes: &[u8]) -> Result<u32> {
let mut b = self.read_u8(bytes);
if b & 0x80 == 0 { return Ok(b as u32); }
let mut r = (b & 0x7f) as u32;
b = self.read_u8(bytes);
r |= ((b & 0x7f) as u32) << 7;
if b & 0x80 == 0 { return Ok(r); }
b = self.read_u8(bytes);
r |= ((b & 0x7f) as u32) << 14;
if b & 0x80 == 0 { return Ok(r); }
b = self.read_u8(bytes);
r |= ((b & 0x7f) as u32) << 21;
if b & 0x80 == 0 { return Ok(r); }
b = self.read_u8(bytes);
r |= ((b & 0xf) as u32) << 28;
if b & 0x80 == 0 { return Ok(r); }
for _ in 0..5 {
if self.read_u8(bytes) & 0x80 == 0 { return Ok(r); }
}
Err(ErrorKind::Varint.into())
}
#[inline(always)]
pub fn read_varint64(&mut self, bytes: &[u8]) -> Result<u64> {
let mut b = self.read_u8(bytes);
if b & 0x80 == 0 { return Ok(b as u64); }
let mut r0 = (b & 0x7f) as u32;
b = self.read_u8(bytes);
r0 |= ((b & 0x7f) as u32) << 7;
if b & 0x80 == 0 { return Ok(r0 as u64); }
b = self.read_u8(bytes);
r0 |= ((b & 0x7f) as u32) << 14;
if b & 0x80 == 0 { return Ok(r0 as u64); }
b = self.read_u8(bytes);
r0 |= ((b & 0x7f) as u32) << 21;
if b & 0x80 == 0 { return Ok(r0 as u64); }
b = self.read_u8(bytes);
let mut r1 = (b & 0x7f) as u32;
if b & 0x80 == 0 { return Ok((r0 as u64 | (r1 as u64) << 28)); }
b = self.read_u8(bytes);
r1 |= ((b & 0x7f) as u32) << 7;
if b & 0x80 == 0 { return Ok((r0 as u64 | (r1 as u64) << 28)); }
b = self.read_u8(bytes);
r1 |= ((b & 0x7f) as u32) << 14;
if b & 0x80 == 0 { return Ok((r0 as u64 | (r1 as u64) << 28)); }
b = self.read_u8(bytes);
r1 |= ((b & 0x7f) as u32) << 21;
if b & 0x80 == 0 { return Ok((r0 as u64 | (r1 as u64) << 28)); }
b = self.read_u8(bytes);
let mut r2 = (b & 0x7f) as u32;
if b & 0x80 == 0 { return Ok(((r0 as u64 | (r1 as u64) << 28) | (r2 as u64) << 56)); }
b = self.read_u8(bytes);
r2 |= (b as u32) << 7;
if b & 0x80 == 0 { return Ok(((r0 as u64 | (r1 as u64) << 28) | (r2 as u64) << 56)); }
Err(ErrorKind::Varint.into())
}
#[inline]
pub fn read_int32(&mut self, bytes: &[u8]) -> Result<i32> {
self.read_varint32(bytes).map(|i| i as i32)
}
#[inline]
pub fn read_int64(&mut self, bytes: &[u8]) -> Result<i64> {
self.read_varint64(bytes).map(|i| i as i64)
}
#[inline]
pub fn read_uint32(&mut self, bytes: &[u8]) -> Result<u32> {
self.read_varint32(bytes)
}
#[inline]
pub fn read_uint64(&mut self, bytes: &[u8]) -> Result<u64> {
self.read_varint64(bytes)
}
#[inline]
pub fn read_sint32(&mut self, bytes: &[u8]) -> Result<i32> {
let n = self.read_varint32(bytes)?;
Ok(((n >> 1) as i32) ^ (-((n & 1) as i32)))
}
#[inline]
pub fn read_sint64(&mut self, bytes: &[u8]) -> Result<i64> {
let n = self.read_varint64(bytes)?;
Ok(((n >> 1) as i64) ^ (-((n & 1) as i64)))
}
#[inline]
fn read_fixed<M, F: Fn(&[u8]) -> M>(&mut self, bytes: &[u8], len: usize, read: F) -> Result<M> {
let v = read(&bytes[self.start .. self.start + len]);
self.start += len;
Ok(v)
}
#[inline]
pub fn read_fixed64(&mut self, bytes: &[u8]) -> Result<u64> {
self.read_fixed(bytes, 8, LE::read_u64)
}
#[inline]
pub fn read_fixed32(&mut self, bytes: &[u8]) -> Result<u32> {
self.read_fixed(bytes, 4, LE::read_u32)
}
#[inline]
pub fn read_sfixed64(&mut self, bytes: &[u8]) -> Result<i64> {
self.read_fixed(bytes, 8, LE::read_i64)
}
#[inline]
pub fn read_sfixed32(&mut self, bytes: &[u8]) -> Result<i32> {
self.read_fixed(bytes, 4, LE::read_i32)
}
#[inline]
pub fn read_float(&mut self, bytes: &[u8]) -> Result<f32> {
self.read_fixed(bytes, 4, LE::read_f32)
}
#[inline]
pub fn read_double(&mut self, bytes: &[u8]) -> Result<f64> {
self.read_fixed(bytes, 8, LE::read_f64)
}
#[inline]
pub fn read_bool(&mut self, bytes: &[u8]) -> Result<bool> {
self.read_varint32(bytes).map(|i| i != 0)
}
#[inline]
pub fn read_enum<E: From<i32>>(&mut self, bytes: &[u8]) -> Result<E> {
self.read_int32(bytes).map(|e| e.into())
}
#[inline(always)]
fn read_len<'a, M, F>(&mut self, bytes: &'a [u8], mut read: F) -> Result<M>
where F: FnMut(&mut BytesReader, &'a[u8]) -> Result<M>,
{
let len = self.read_varint32(bytes)? as usize;
let cur_end = self.end;
self.end = self.start + len;
let v = read(self, bytes)?;
self.start = self.end;
self.end = cur_end;
Ok(v)
}
#[inline]
pub fn read_bytes<'a>(&mut self, bytes: &'a[u8]) -> Result<&'a[u8]> {
self.read_len(bytes, |r, b| Ok(&b[r.start..r.end]))
}
#[inline]
pub fn read_string<'a>(&mut self, bytes: &'a[u8]) -> Result<&'a str> {
self.read_len(bytes, |r, b| ::std::str::from_utf8(&b[r.start..r.end]).map_err(|e| e.into()))
}
#[inline]
pub fn read_packed<'a, M, F>(&mut self, bytes: &'a[u8], mut read: F) -> Result<Vec<M>>
where F: FnMut(&mut BytesReader, &'a[u8]) -> Result<M>,
{
self.read_len(bytes, |r, b| {
let mut v = Vec::new();
while !r.is_eof() {
v.push(read(r, b)?);
}
Ok(v)
})
}
#[inline]
pub fn read_message<'a, M, F>(&mut self, bytes: &'a[u8], read: F) -> Result<M>
where F: FnMut(&mut BytesReader, &'a[u8]) -> Result<M>
{
self.read_len(bytes, read)
}
#[inline]
pub fn read_unknown(&mut self, bytes: &[u8], tag_value: u32) -> Result<()> {
match (tag_value & 0x7) as u8 {
WIRE_TYPE_VARINT => { self.read_varint64(bytes)?; },
WIRE_TYPE_FIXED64 => self.start += 8,
WIRE_TYPE_FIXED32 => self.start += 4,
WIRE_TYPE_LENGTH_DELIMITED => {
let len = self.read_varint64(bytes)? as usize;
self.start += len;
},
WIRE_TYPE_START_GROUP |
WIRE_TYPE_END_GROUP => { return Err(ErrorKind::Deprecated("group").into()); },
t => { return Err(ErrorKind::UnknownWireType(t).into()); },
}
Ok(())
}
#[inline]
pub fn len(&self) -> usize {
self.end - self.start
}
#[inline]
pub fn is_eof(&self) -> bool {
self.start == self.end
}
}
pub struct Reader {
buf: Vec<u8>,
reader: BytesReader,
}
impl Reader {
pub fn from_reader<R: Read>(mut r: R, capacity: usize) -> Result<Reader> {
let mut buf = Vec::with_capacity(capacity);
unsafe { buf.set_len(capacity); }
buf.shrink_to_fit();
r.read_exact(&mut buf)?;
let reader = BytesReader {
start: 0,
end: capacity,
};
Ok(Reader {
buf: buf,
reader: reader,
})
}
pub fn from_file<P: AsRef<Path>>(src: P) -> Result<Reader> {
let len = src.as_ref().metadata().unwrap().len() as usize;
let f = File::open(src)?;
Reader::from_reader(f, len)
}
#[inline]
pub fn read<'a, M, F>(&'a mut self, mut read: F) -> Result<M>
where F: FnMut(&mut BytesReader, &'a[u8]) -> Result<M>
{
read(&mut self.reader, &self.buf)
}
}
#[test]
fn test_varint() {
let data = [0x96, 0x01];
let mut r = BytesReader::from_bytes(&data[..]);
assert_eq!(150, r.read_varint32(&data[..]).unwrap());
assert!(r.is_eof());
}