quick_protobuf/reader.rs
1//! A module to manage protobuf deserialization
2//!
3//! There are actually two main *readers*
4//! - a `BytesReader` which parses data from a `&[u8]`
5//! - a `Reader` which is a wrapper on `BytesReader` which has its own buffer. It provides
6//! convenient functions to the user suche as `from_file`
7//!
8//! It is advised, for convenience to directly work with a `Reader`.
9
10#[cfg(feature = "std")]
11use std::fs::File;
12#[cfg(feature = "std")]
13use std::io::Read;
14#[cfg(feature = "std")]
15use std::path::Path;
16
17use core::convert::TryFrom;
18
19#[cfg(not(feature = "std"))]
20extern crate alloc;
21#[cfg(not(feature = "std"))]
22use alloc::vec::Vec;
23
24use crate::errors::{Error, Result};
25use crate::message::MessageRead;
26
27use byteorder::ByteOrder;
28use byteorder::LittleEndian as LE;
29
30const WIRE_TYPE_VARINT: u8 = 0;
31const WIRE_TYPE_FIXED64: u8 = 1;
32const WIRE_TYPE_LENGTH_DELIMITED: u8 = 2;
33const WIRE_TYPE_START_GROUP: u8 = 3;
34const WIRE_TYPE_END_GROUP: u8 = 4;
35const WIRE_TYPE_FIXED32: u8 = 5;
36
37/// A struct to read protocol binary files
38///
39/// # Examples
40///
41/// ```rust
42/// # mod foo_bar {
43/// # use quick_protobuf::{MessageRead, BytesReader, Result};
44/// # pub struct Foo {}
45/// # pub struct Bar {}
46/// # pub struct FooBar { pub foos: Vec<Foo>, pub bars: Vec<Bar>, }
47/// # impl<'a> MessageRead<'a> for FooBar {
48/// # fn from_reader(_: &mut BytesReader, _: &[u8]) -> Result<Self> {
49/// # Ok(FooBar { foos: vec![], bars: vec![] })
50/// # }
51/// # }
52/// # }
53///
54/// // FooBar is a message generated from a proto file
55/// // in parcicular it contains a `from_reader` function
56/// use foo_bar::FooBar;
57/// use quick_protobuf::{MessageRead, BytesReader};
58///
59/// fn main() {
60/// // bytes is a buffer on the data we want to deserialize
61/// // typically bytes is read from a `Read`:
62/// // r.read_to_end(&mut bytes).expect("cannot read bytes");
63/// let mut bytes: Vec<u8>;
64/// # bytes = vec![];
65///
66/// // we can build a bytes reader directly out of the bytes
67/// let mut reader = BytesReader::from_bytes(&bytes);
68///
69/// // now using the generated module decoding is as easy as:
70/// let foobar = FooBar::from_reader(&mut reader, &bytes).expect("Cannot read FooBar");
71///
72/// // if instead the buffer contains a length delimited stream of message we could use:
73/// // while !r.is_eof() {
74/// // let foobar: FooBar = r.read_message(&bytes).expect(...);
75/// // ...
76/// // }
77/// println!("Found {} foos and {} bars", foobar.foos.len(), foobar.bars.len());
78/// }
79/// ```
80#[derive(Debug, Clone, PartialEq, Eq)]
81pub struct BytesReader {
82 start: usize,
83 end: usize,
84}
85
86impl BytesReader {
87 /// Creates a new reader from chunks of data
88 pub fn from_bytes(bytes: &[u8]) -> BytesReader {
89 BytesReader {
90 start: 0,
91 end: bytes.len(),
92 }
93 }
94
95 /// Reads next tag, `None` if all bytes have been read
96 #[cfg_attr(std, inline(always))]
97 pub fn next_tag(&mut self, bytes: &[u8]) -> Result<u32> {
98 self.read_varint32(bytes)
99 }
100
101 /// Reads the next byte
102 #[cfg_attr(std, inline(always))]
103 pub fn read_u8(&mut self, bytes: &[u8]) -> Result<u8> {
104 let b = bytes.get(self.start).ok_or(Error::UnexpectedEndOfBuffer)?;
105 self.start += 1;
106 Ok(*b)
107 }
108
109 /// Reads the next varint encoded u64
110 #[cfg_attr(std, inline(always))]
111 pub fn read_varint32(&mut self, bytes: &[u8]) -> Result<u32> {
112 let mut b = self.read_u8(bytes)?; // byte0
113 if b & 0x80 == 0 {
114 return Ok(b as u32);
115 }
116 let mut r = (b & 0x7f) as u32;
117
118 b = self.read_u8(bytes)?; // byte1
119 r |= ((b & 0x7f) as u32) << 7;
120 if b & 0x80 == 0 {
121 return Ok(r);
122 }
123
124 b = self.read_u8(bytes)?; // byte2
125 r |= ((b & 0x7f) as u32) << 14;
126 if b & 0x80 == 0 {
127 return Ok(r);
128 }
129
130 b = self.read_u8(bytes)?; // byte3
131 r |= ((b & 0x7f) as u32) << 21;
132 if b & 0x80 == 0 {
133 return Ok(r);
134 }
135
136 b = self.read_u8(bytes)?; // byte4
137 r |= ((b & 0xf) as u32) << 28; // silently prevent overflow; only mask 0xF
138 if b & 0x80 == 0 {
139 // WARNING ABOUT TRUNCATION
140 //
141 // In this case, byte4 takes the form 0ZZZ_YYYY where:
142 // Y: part of the resulting 32-bit number
143 // Z: beyond 32 bits (excess bits,not used)
144 //
145 // If the Z bits were set, it might indicate that the number being
146 // decoded was intended to be bigger than 32 bits, suggesting an
147 // error somewhere else.
148 //
149 // However, for the sake of consistency with Google's own protobuf
150 // implementation, and also to allow for any efficient use of those
151 // extra bits by users if they wish (this crate is meant for speed
152 // optimization anyway) we shall not check for this here.
153 //
154 // Therefore, THIS FUNCTION SIMPLY IGNORES THE EXTRA BITS, WHICH IS
155 // ESSENTIALLY A SILENT TRUNCATION!
156 return Ok(r);
157 }
158
159 // ANOTHER WARNING ABOUT TRUNCATION
160 //
161 // Again, we do not check whether the byte representation fits within 32
162 // bits, and simply ignore extra bytes, CONSTITUTING A SILENT
163 // TRUNCATION!
164 //
165 // Therefore, if the user wants this function to avoid ignoring any
166 // bits/bytes, they need to ensure that the input is a varint
167 // representing a value within EITHER u32 OR i32 range. Since at this
168 // point we are beyond 5 bits, the only possible case is a negative i32
169 // (since negative numbers are always 10 bytes in protobuf). We must
170 // have exactly 5 bytes more to go.
171 //
172 // Since we know it must be a negative number, and this function is
173 // meant to read 32-bit ints (there is a different function for reading
174 // 64-bit ints), the user might want to take care to ensure that this
175 // negative number is within valid i32 range, i.e. at least
176 // -2,147,483,648. Otherwise, this function simply ignores the extra
177 // bits, essentially constituting a silent truncation!
178 //
179 // What this means in the end is that the user should ensure that the
180 // resulting number, once decoded from the varint format, takes such a
181 // form:
182 //
183 // 11111111_11111111_11111111_11111111_1XXXXXXX_XXXXXXXX_XXXXXXXX_XXXXXXXX
184 // ^(MSB bit 63) ^(bit 31 is set) ^(LSB bit 0)
185
186 // discards extra bytes
187 for _ in 0..5 {
188 if self.read_u8(bytes)? & 0x80 == 0 {
189 return Ok(r);
190 }
191 }
192
193 // cannot read more than 10 bytes
194 Err(Error::Varint)
195 }
196
197 /// Reads the next varint encoded u64
198 #[cfg_attr(std, inline(always))]
199 pub fn read_varint64(&mut self, bytes: &[u8]) -> Result<u64> {
200 // part0
201 let mut b = self.read_u8(bytes)?;
202 if b & 0x80 == 0 {
203 return Ok(b as u64);
204 }
205 let mut r0 = (b & 0x7f) as u32;
206
207 b = self.read_u8(bytes)?;
208 r0 |= ((b & 0x7f) as u32) << 7;
209 if b & 0x80 == 0 {
210 return Ok(r0 as u64);
211 }
212
213 b = self.read_u8(bytes)?;
214 r0 |= ((b & 0x7f) as u32) << 14;
215 if b & 0x80 == 0 {
216 return Ok(r0 as u64);
217 }
218
219 b = self.read_u8(bytes)?;
220 r0 |= ((b & 0x7f) as u32) << 21;
221 if b & 0x80 == 0 {
222 return Ok(r0 as u64);
223 }
224
225 // part1
226 b = self.read_u8(bytes)?;
227 let mut r1 = (b & 0x7f) as u32;
228 if b & 0x80 == 0 {
229 return Ok(r0 as u64 | (r1 as u64) << 28);
230 }
231
232 b = self.read_u8(bytes)?;
233 r1 |= ((b & 0x7f) as u32) << 7;
234 if b & 0x80 == 0 {
235 return Ok(r0 as u64 | (r1 as u64) << 28);
236 }
237
238 b = self.read_u8(bytes)?;
239 r1 |= ((b & 0x7f) as u32) << 14;
240 if b & 0x80 == 0 {
241 return Ok(r0 as u64 | (r1 as u64) << 28);
242 }
243
244 b = self.read_u8(bytes)?;
245 r1 |= ((b & 0x7f) as u32) << 21;
246 if b & 0x80 == 0 {
247 return Ok(r0 as u64 | (r1 as u64) << 28);
248 }
249
250 // part2
251 b = self.read_u8(bytes)?;
252 let mut r2 = (b & 0x7f) as u32;
253 if b & 0x80 == 0 {
254 return Ok((r0 as u64 | (r1 as u64) << 28) | (r2 as u64) << 56);
255 }
256
257 // WARNING ABOUT TRUNCATION:
258 //
259 // For the number to be within valid 64 bit range, some conditions about
260 // this last byte must be met:
261 // 1. This must be the last byte (MSB not set)
262 // 2. No 64-bit overflow (middle 6 bits are beyond 64 bits for the
263 // entire varint, so they cannot be set either)
264 //
265 // However, for the sake of consistency with Google's own protobuf
266 // implementation, and also to allow for any efficient use of those
267 // extra bits by users if they wish (this crate is meant for speed
268 // optimization anyway) we shall not check for this here.
269 //
270 // Therefore, THIS FUNCTION SIMPLY IGNORES THE EXTRA BITS, WHICH IS
271 // ESSENTIALLY A SILENT TRUNCATION!
272 b = self.read_u8(bytes)?;
273 r2 |= (b as u32) << 7;
274 if b & 0x80 == 0 {
275 return Ok((r0 as u64 | (r1 as u64) << 28) | (r2 as u64) << 56);
276 }
277
278 // cannot read more than 10 bytes
279 Err(Error::Varint)
280 }
281
282 /// Reads int32 (varint)
283 #[cfg_attr(std, inline)]
284 pub fn read_int32(&mut self, bytes: &[u8]) -> Result<i32> {
285 self.read_varint32(bytes).map(|i| i as i32)
286 }
287
288 /// Reads int64 (varint)
289 #[cfg_attr(std, inline)]
290 pub fn read_int64(&mut self, bytes: &[u8]) -> Result<i64> {
291 self.read_varint64(bytes).map(|i| i as i64)
292 }
293
294 /// Reads uint32 (varint)
295 #[cfg_attr(std, inline)]
296 pub fn read_uint32(&mut self, bytes: &[u8]) -> Result<u32> {
297 self.read_varint32(bytes)
298 }
299
300 /// Reads uint64 (varint)
301 #[cfg_attr(std, inline)]
302 pub fn read_uint64(&mut self, bytes: &[u8]) -> Result<u64> {
303 self.read_varint64(bytes)
304 }
305
306 /// Reads sint32 (varint)
307 #[cfg_attr(std, inline)]
308 pub fn read_sint32(&mut self, bytes: &[u8]) -> Result<i32> {
309 // zigzag
310 let n = self.read_varint32(bytes)?;
311 Ok(((n >> 1) as i32) ^ (-((n & 1) as i32)))
312 }
313
314 /// Reads sint64 (varint)
315 #[cfg_attr(std, inline)]
316 pub fn read_sint64(&mut self, bytes: &[u8]) -> Result<i64> {
317 // zigzag
318 let n = self.read_varint64(bytes)?;
319 Ok(((n >> 1) as i64) ^ (-((n & 1) as i64)))
320 }
321
322 /// Reads fixed64 (little endian u64)
323 #[cfg_attr(std, inline)]
324 fn read_fixed<M, F: Fn(&[u8]) -> M>(&mut self, bytes: &[u8], len: usize, read: F) -> Result<M> {
325 let v = read(
326 &bytes
327 .get(self.start..self.start + len)
328 .ok_or_else(|| Error::UnexpectedEndOfBuffer)?,
329 );
330 self.start += len;
331 Ok(v)
332 }
333
334 /// Reads fixed64 (little endian u64)
335 #[cfg_attr(std, inline)]
336 pub fn read_fixed64(&mut self, bytes: &[u8]) -> Result<u64> {
337 self.read_fixed(bytes, 8, LE::read_u64)
338 }
339
340 /// Reads fixed32 (little endian u32)
341 #[cfg_attr(std, inline)]
342 pub fn read_fixed32(&mut self, bytes: &[u8]) -> Result<u32> {
343 self.read_fixed(bytes, 4, LE::read_u32)
344 }
345
346 /// Reads sfixed64 (little endian i64)
347 #[cfg_attr(std, inline)]
348 pub fn read_sfixed64(&mut self, bytes: &[u8]) -> Result<i64> {
349 self.read_fixed(bytes, 8, LE::read_i64)
350 }
351
352 /// Reads sfixed32 (little endian i32)
353 #[cfg_attr(std, inline)]
354 pub fn read_sfixed32(&mut self, bytes: &[u8]) -> Result<i32> {
355 self.read_fixed(bytes, 4, LE::read_i32)
356 }
357
358 /// Reads float (little endian f32)
359 #[cfg_attr(std, inline)]
360 pub fn read_float(&mut self, bytes: &[u8]) -> Result<f32> {
361 self.read_fixed(bytes, 4, LE::read_f32)
362 }
363
364 /// Reads double (little endian f64)
365 #[cfg_attr(std, inline)]
366 pub fn read_double(&mut self, bytes: &[u8]) -> Result<f64> {
367 self.read_fixed(bytes, 8, LE::read_f64)
368 }
369
370 /// Reads bool (varint, check if == 0)
371 #[cfg_attr(std, inline)]
372 pub fn read_bool(&mut self, bytes: &[u8]) -> Result<bool> {
373 self.read_varint32(bytes).map(|i| i != 0)
374 }
375
376 /// Reads enum, encoded as i32
377 #[cfg_attr(std, inline)]
378 pub fn read_enum<E: From<i32>>(&mut self, bytes: &[u8]) -> Result<E> {
379 self.read_int32(bytes).map(|e| e.into())
380 }
381
382 /// First reads a varint and use it as size to read a generic object
383 #[cfg_attr(std, inline(always))]
384 fn read_len_varint<'a, M, F>(&mut self, bytes: &'a [u8], read: F) -> Result<M>
385 where
386 F: FnMut(&mut BytesReader, &'a [u8]) -> Result<M>,
387 {
388 let len = self.read_varint32(bytes)? as usize;
389 self.read_len(bytes, read, len)
390 }
391
392 /// Reads a certain number of bytes specified by len
393 #[cfg_attr(std, inline(always))]
394 fn read_len<'a, M, F>(&mut self, bytes: &'a [u8], mut read: F, len: usize) -> Result<M>
395 where
396 F: FnMut(&mut BytesReader, &'a [u8]) -> Result<M>,
397 {
398 let cur_end = self.end;
399 self.end = self.start + len;
400 let v = read(self, bytes)?;
401 self.start = self.end;
402 self.end = cur_end;
403 Ok(v)
404 }
405
406 /// Reads bytes (Vec<u8>)
407 #[cfg_attr(std, inline)]
408 pub fn read_bytes<'a>(&mut self, bytes: &'a [u8]) -> Result<&'a [u8]> {
409 self.read_len_varint(bytes, |r, b| {
410 b.get(r.start..r.end)
411 .ok_or_else(|| Error::UnexpectedEndOfBuffer)
412 })
413 }
414
415 /// Reads string (String)
416 #[cfg_attr(std, inline)]
417 pub fn read_string<'a>(&mut self, bytes: &'a [u8]) -> Result<&'a str> {
418 self.read_len_varint(bytes, |r, b| {
419 b.get(r.start..r.end)
420 .ok_or_else(|| Error::UnexpectedEndOfBuffer)
421 .and_then(|x| ::core::str::from_utf8(x).map_err(|e| e.into()))
422 })
423 }
424
425 /// Reads packed repeated field (Vec<M>)
426 ///
427 /// Note: packed field are stored as a variable length chunk of data, while regular repeated
428 /// fields behaves like an iterator, yielding their tag everytime
429 #[cfg_attr(std, inline)]
430 pub fn read_packed<'a, M, F>(&mut self, bytes: &'a [u8], mut read: F) -> Result<Vec<M>>
431 where
432 F: FnMut(&mut BytesReader, &'a [u8]) -> Result<M>,
433 {
434 self.read_len_varint(bytes, |r, b| {
435 let mut v = Vec::new();
436 while !r.is_eof() {
437 v.push(read(r, b)?);
438 }
439 Ok(v)
440 })
441 }
442
443 /// Reads packed repeated field where M can directly be transmutted from raw bytes
444 ///
445 /// Note: packed field are stored as a variable length chunk of data, while regular repeated
446 /// fields behaves like an iterator, yielding their tag everytime
447 #[cfg_attr(std, inline)]
448 pub fn read_packed_fixed<'a, M>(&mut self, bytes: &'a [u8]) -> Result<&'a [M]> {
449 let len = self.read_varint32(bytes)? as usize;
450 if self.len() < len {
451 return Err(Error::UnexpectedEndOfBuffer);
452 }
453 let n = len / ::core::mem::size_of::<M>();
454 let slice = unsafe {
455 ::core::slice::from_raw_parts(
456 bytes.get_unchecked(self.start) as *const u8 as *const M,
457 n,
458 )
459 };
460 self.start += len;
461 Ok(slice)
462 }
463
464 /// Reads a nested message
465 ///
466 /// First reads a varint and interprets it as the length of the message
467 #[cfg_attr(std, inline)]
468 pub fn read_message<'a, M>(&mut self, bytes: &'a [u8]) -> Result<M>
469 where
470 M: MessageRead<'a>,
471 {
472 self.read_len_varint(bytes, M::from_reader)
473 }
474
475 /// Reads a nested message
476 ///
477 /// Reads just the message and does not try to read it's size first.
478 /// * 'len' - The length of the message to be read.
479 #[cfg_attr(std, inline)]
480 pub fn read_message_by_len<'a, M>(&mut self, bytes: &'a [u8], len: usize) -> Result<M>
481 where
482 M: MessageRead<'a>,
483 {
484 self.read_len(bytes, M::from_reader, len)
485 }
486
487 /// Reads a map item: (key, value)
488 #[cfg_attr(std, inline)]
489 pub fn read_map<'a, K, V, F, G>(
490 &mut self,
491 bytes: &'a [u8],
492 mut read_key: F,
493 mut read_val: G,
494 ) -> Result<(K, V)>
495 where
496 F: FnMut(&mut BytesReader, &'a [u8]) -> Result<K>,
497 G: FnMut(&mut BytesReader, &'a [u8]) -> Result<V>,
498 K: ::core::fmt::Debug + Default,
499 V: ::core::fmt::Debug + Default,
500 {
501 self.read_len_varint(bytes, |r, bytes| {
502 let mut k = K::default();
503 let mut v = V::default();
504 while !r.is_eof() {
505 let t = r.read_u8(bytes)?;
506 match t >> 3 {
507 1 => k = read_key(r, bytes)?,
508 2 => v = read_val(r, bytes)?,
509 t => return Err(Error::Map(t)),
510 }
511 }
512 Ok((k, v))
513 })
514 }
515
516 /// Reads unknown data, based on its tag value (which itself gives us the wire_type value)
517 #[cfg_attr(std, inline)]
518 pub fn read_unknown(&mut self, bytes: &[u8], tag_value: u32) -> Result<()> {
519 // Since `read.varint64()` calls `read_u8()`, which increments
520 // `self.start`, we don't need to manually increment `self.start` in
521 // control flows that either call `read_varint64()` or error out.
522 let offset = match (tag_value & 0x7) as u8 {
523 WIRE_TYPE_VARINT => {
524 self.read_varint64(bytes)?;
525 return Ok(());
526 }
527 WIRE_TYPE_FIXED64 => 8,
528 WIRE_TYPE_FIXED32 => 4,
529 WIRE_TYPE_LENGTH_DELIMITED => {
530 usize::try_from(self.read_varint64(bytes)?).map_err(|_| Error::Varint)?
531 }
532 WIRE_TYPE_START_GROUP | WIRE_TYPE_END_GROUP => {
533 return Err(Error::Deprecated("group"));
534 }
535 t => {
536 return Err(Error::UnknownWireType(t));
537 }
538 };
539
540 // Meant to prevent overflowing. Comparison used is *strictly* lesser
541 // since `self.end` is given by `len()`; i.e. `self.end` is 1 more than
542 // highest index
543 if self.end - self.start < offset {
544 Err(Error::Varint)
545 } else {
546 self.start += offset;
547 Ok(())
548 }
549 }
550
551 /// Gets the remaining length of bytes not read yet
552 #[cfg_attr(std, inline(always))]
553 pub fn len(&self) -> usize {
554 self.end - self.start
555 }
556
557 /// Checks if `self.len == 0`
558 #[cfg_attr(std, inline(always))]
559 pub fn is_eof(&self) -> bool {
560 self.start == self.end
561 }
562
563 /// Advance inner cursor to the end
564 pub fn read_to_end(&mut self) {
565 self.start = self.end;
566 }
567}
568
569/// A struct to read protobuf data
570///
571/// Contrary to `BytesReader`, this struct will own a buffer
572///
573/// # Examples
574///
575/// ```rust,should_panic
576/// # mod foo_bar {
577/// # use quick_protobuf::{MessageRead, BytesReader, Result};
578/// # pub struct Foo {}
579/// # pub struct Bar {}
580/// # pub struct FooBar { pub foos: Vec<Foo>, pub bars: Vec<Bar>, }
581/// # impl<'a> MessageRead<'a> for FooBar {
582/// # fn from_reader(_: &mut BytesReader, _: &[u8]) -> Result<Self> {
583/// # Ok(FooBar { foos: vec![], bars: vec![] })
584/// # }
585/// # }
586/// # }
587///
588/// // FooBar is a message generated from a proto file
589/// // In particular it implements the `MessageRead` trait, containing a `from_reader` function.
590/// use foo_bar::FooBar;
591/// use quick_protobuf::Reader;
592///
593/// fn main() {
594/// // create a reader, which will parse the protobuf binary file and pop events
595/// // this reader will read the entire file into an internal buffer
596/// let mut reader = Reader::from_file("/path/to/binary/protobuf.bin")
597/// .expect("Cannot read input file");
598///
599/// // Use the generated module fns with the reader to convert your data into rust structs.
600/// //
601/// // Depending on your input file, the message can or not be prefixed with the encoded length
602/// // for instance, a *stream* which contains several messages generally split them using this
603/// // technique (see https://developers.google.com/protocol-buffers/docs/techniques#streaming)
604/// //
605/// // To read a message without a length prefix you can directly call `FooBar::from_reader`:
606/// // let foobar = reader.read(FooBar::from_reader).expect("Cannot read FooBar message");
607/// //
608/// // Else to read a length then a message, you can use:
609/// let foobar: FooBar = reader.read(|r, b| r.read_message(b))
610/// .expect("Cannot read FooBar message");
611/// // Reader::read_message uses `FooBar::from_reader` internally through the `MessageRead`
612/// // trait.
613///
614/// println!("Found {} foos and {} bars!", foobar.foos.len(), foobar.bars.len());
615/// }
616/// ```
617pub struct Reader {
618 buffer: Vec<u8>,
619 inner: BytesReader,
620}
621
622impl Reader {
623 /// Creates a new `Reader`
624 #[cfg(feature = "std")]
625 pub fn from_reader<R: Read>(mut r: R, capacity: usize) -> Result<Reader> {
626 let mut buf = Vec::with_capacity(capacity);
627 unsafe {
628 buf.set_len(capacity);
629 }
630 buf.shrink_to_fit();
631 r.read_exact(&mut buf)?;
632 Ok(Reader::from_bytes(buf))
633 }
634
635 /// Creates a new `Reader` out of a file path
636 #[cfg(feature = "std")]
637 pub fn from_file<P: AsRef<Path>>(src: P) -> Result<Reader> {
638 let len = src.as_ref().metadata().unwrap().len() as usize;
639 let f = File::open(src)?;
640 Reader::from_reader(f, len)
641 }
642
643 /// Creates a new reader consuming the bytes
644 pub fn from_bytes(bytes: Vec<u8>) -> Reader {
645 let reader = BytesReader {
646 start: 0,
647 end: bytes.len(),
648 };
649 Reader {
650 buffer: bytes,
651 inner: reader,
652 }
653 }
654
655 /// Run a `BytesReader` dependent function
656 #[cfg_attr(std, inline)]
657 pub fn read<'a, M, F>(&'a mut self, mut read: F) -> Result<M>
658 where
659 F: FnMut(&mut BytesReader, &'a [u8]) -> Result<M>,
660 {
661 read(&mut self.inner, &self.buffer)
662 }
663
664 /// Gets the inner `BytesReader`
665 pub fn inner(&mut self) -> &mut BytesReader {
666 &mut self.inner
667 }
668
669 /// Gets the buffer used internally
670 pub fn buffer(&self) -> &[u8] {
671 &self.buffer
672 }
673}
674
675/// Deserialize a `MessageRead from a `&[u8]`
676pub fn deserialize_from_slice<'a, M: MessageRead<'a>>(bytes: &'a [u8]) -> Result<M> {
677 let mut reader = BytesReader::from_bytes(&bytes);
678 reader.read_message::<M>(&bytes)
679}
680
681#[test]
682fn test_varint() {
683 let data = [0x96, 0x01];
684 let mut r = BytesReader::from_bytes(&data[..]);
685 assert_eq!(150, r.read_varint32(&data[..]).unwrap());
686 assert!(r.is_eof());
687}
688
689#[test]
690fn read_size_overflowing_unknown() {
691 let bytes = &[
692 200, 250, 35, // varint tag with WIRE_TYPE_VARINT -- 589128
693 //
694 //
695 47, // varint itself
696 //
697 //
698 250, 36, // varint tag with WIRE_TYPE_LENGTH_DELIMITED -- 4730
699 //
700 //
701 255, 255, 255, 255, 255, 255, 255, 255, 255, 3, // huge 10-byte length
702 //
703 //
704 255, 255, 227, // unused extra bytes
705 ];
706
707 let mut r = BytesReader::from_bytes(bytes);
708
709 assert!(!r.is_eof());
710 assert_eq!(r.next_tag(bytes).unwrap(), 589128);
711 r.read_unknown(bytes, 589128).unwrap();
712
713 assert!(!r.is_eof());
714 assert_eq!(r.next_tag(bytes).unwrap(), 4730);
715 let e = r.read_unknown(bytes, 4730).unwrap_err();
716
717 assert!(matches!(e, Error::Varint), "{:?}", e);
718}