• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![doc(hidden)]
2 
3 //! `CodedInputStream` and `CodedOutputStream` implementations
4 
5 use std::io;
6 use std::io::BufRead;
7 use std::io::Read;
8 use std::mem;
9 use std::slice;
10 
11 #[cfg(feature = "bytes")]
12 use crate::chars::Chars;
13 #[cfg(feature = "bytes")]
14 use bytes::Bytes;
15 
16 use crate::buf_read_iter::BufReadIter;
17 use crate::enums::ProtobufEnum;
18 use crate::error::ProtobufError;
19 use crate::error::ProtobufResult;
20 use crate::error::WireError;
21 use crate::message::Message;
22 use crate::unknown::UnknownValue;
23 use crate::wire_format;
24 use crate::zigzag::decode_zig_zag_32;
25 use crate::zigzag::decode_zig_zag_64;
26 
27 /// Default recursion level limit. 100 is the default value of C++'s implementation.
28 const DEFAULT_RECURSION_LIMIT: u32 = 100;
29 
30 /// Max allocated vec when reading length-delimited from unknown input stream
31 pub(crate) const READ_RAW_BYTES_MAX_ALLOC: usize = 10_000_000;
32 
33 /// Buffered read with handy utilities.
34 pub struct CodedInputStream<'a> {
35     source: BufReadIter<'a>,
36     recursion_level: u32,
37     recursion_limit: u32,
38 }
39 
40 impl<'a> CodedInputStream<'a> {
41     /// Wrap a `Read`.
42     ///
43     /// Note resulting `CodedInputStream` is buffered even if `Read` is not.
new(read: &'a mut dyn Read) -> CodedInputStream<'a>44     pub fn new(read: &'a mut dyn Read) -> CodedInputStream<'a> {
45         CodedInputStream::from_buf_read_iter(BufReadIter::from_read(read))
46     }
47 
48     /// Create from `BufRead`.
49     ///
50     /// `CodedInputStream` will utilize `BufRead` buffer.
from_buffered_reader(buf_read: &'a mut dyn BufRead) -> CodedInputStream<'a>51     pub fn from_buffered_reader(buf_read: &'a mut dyn BufRead) -> CodedInputStream<'a> {
52         CodedInputStream::from_buf_read_iter(BufReadIter::from_buf_read(buf_read))
53     }
54 
55     /// Read from byte slice
from_bytes(bytes: &'a [u8]) -> CodedInputStream<'a>56     pub fn from_bytes(bytes: &'a [u8]) -> CodedInputStream<'a> {
57         CodedInputStream::from_buf_read_iter(BufReadIter::from_byte_slice(bytes))
58     }
59 
60     /// Read from `Bytes`.
61     ///
62     /// `CodedInputStream` operations like
63     /// [`read_carllerche_bytes`](crate::CodedInputStream::read_carllerche_bytes)
64     /// will return a shared copy of this bytes object.
65     #[cfg(feature = "bytes")]
from_carllerche_bytes(bytes: &'a Bytes) -> CodedInputStream<'a>66     pub fn from_carllerche_bytes(bytes: &'a Bytes) -> CodedInputStream<'a> {
67         CodedInputStream::from_buf_read_iter(BufReadIter::from_bytes(bytes))
68     }
69 
from_buf_read_iter(source: BufReadIter<'a>) -> CodedInputStream<'a>70     fn from_buf_read_iter(source: BufReadIter<'a>) -> CodedInputStream<'a> {
71         CodedInputStream {
72             source: source,
73             recursion_level: 0,
74             recursion_limit: DEFAULT_RECURSION_LIMIT,
75         }
76     }
77 
78     /// Set the recursion limit.
set_recursion_limit(&mut self, limit: u32)79     pub fn set_recursion_limit(&mut self, limit: u32) {
80         self.recursion_limit = limit;
81     }
82 
83     #[inline]
incr_recursion(&mut self) -> ProtobufResult<()>84     pub(crate) fn incr_recursion(&mut self) -> ProtobufResult<()> {
85         if self.recursion_level >= self.recursion_limit {
86             return Err(ProtobufError::WireError(WireError::OverRecursionLimit));
87         }
88         self.recursion_level += 1;
89         Ok(())
90     }
91 
92     #[inline]
decr_recursion(&mut self)93     pub(crate) fn decr_recursion(&mut self) {
94         self.recursion_level -= 1;
95     }
96 
97     /// How many bytes processed
pos(&self) -> u6498     pub fn pos(&self) -> u64 {
99         self.source.pos()
100     }
101 
102     /// How many bytes until current limit
bytes_until_limit(&self) -> u64103     pub fn bytes_until_limit(&self) -> u64 {
104         self.source.bytes_until_limit()
105     }
106 
107     /// Read bytes into given `buf`.
108     ///
109     /// Return `0` on EOF.
110     // TODO: overload with `Read::read`
read(&mut self, buf: &mut [u8]) -> ProtobufResult<()>111     pub fn read(&mut self, buf: &mut [u8]) -> ProtobufResult<()> {
112         self.source.read_exact(buf)?;
113         Ok(())
114     }
115 
116     /// Read exact number of bytes as `Bytes` object.
117     ///
118     /// This operation returns a shared view if `CodedInputStream` is
119     /// constructed with `Bytes` parameter.
120     #[cfg(feature = "bytes")]
read_raw_callerche_bytes(&mut self, count: usize) -> ProtobufResult<Bytes>121     fn read_raw_callerche_bytes(&mut self, count: usize) -> ProtobufResult<Bytes> {
122         self.source.read_exact_bytes(count)
123     }
124 
125     /// Read one byte
126     #[inline(always)]
read_raw_byte(&mut self) -> ProtobufResult<u8>127     pub fn read_raw_byte(&mut self) -> ProtobufResult<u8> {
128         self.source.read_byte()
129     }
130 
131     /// Push new limit, return previous limit.
push_limit(&mut self, limit: u64) -> ProtobufResult<u64>132     pub fn push_limit(&mut self, limit: u64) -> ProtobufResult<u64> {
133         self.source.push_limit(limit)
134     }
135 
136     /// Restore previous limit.
pop_limit(&mut self, old_limit: u64)137     pub fn pop_limit(&mut self, old_limit: u64) {
138         self.source.pop_limit(old_limit);
139     }
140 
141     /// Are we at EOF?
142     #[inline(always)]
eof(&mut self) -> ProtobufResult<bool>143     pub fn eof(&mut self) -> ProtobufResult<bool> {
144         self.source.eof()
145     }
146 
147     /// Check we are at EOF.
148     ///
149     /// Return error if we are not at EOF.
check_eof(&mut self) -> ProtobufResult<()>150     pub fn check_eof(&mut self) -> ProtobufResult<()> {
151         let eof = self.eof()?;
152         if !eof {
153             return Err(ProtobufError::WireError(WireError::UnexpectedEof));
154         }
155         Ok(())
156     }
157 
read_raw_varint64_slow(&mut self) -> ProtobufResult<u64>158     fn read_raw_varint64_slow(&mut self) -> ProtobufResult<u64> {
159         let mut r: u64 = 0;
160         let mut i = 0;
161         loop {
162             if i == 10 {
163                 return Err(ProtobufError::WireError(WireError::IncorrectVarint));
164             }
165             let b = self.read_raw_byte()?;
166             // TODO: may overflow if i == 9
167             r = r | (((b & 0x7f) as u64) << (i * 7));
168             i += 1;
169             if b < 0x80 {
170                 return Ok(r);
171             }
172         }
173     }
174 
175     /// Read varint
176     #[inline(always)]
read_raw_varint64(&mut self) -> ProtobufResult<u64>177     pub fn read_raw_varint64(&mut self) -> ProtobufResult<u64> {
178         'slow: loop {
179             let ret;
180             let consume;
181 
182             loop {
183                 let rem = self.source.remaining_in_buf();
184 
185                 if rem.len() >= 1 {
186                     // most varints are in practice fit in 1 byte
187                     if rem[0] < 0x80 {
188                         ret = rem[0] as u64;
189                         consume = 1;
190                     } else {
191                         // handle case of two bytes too
192                         if rem.len() >= 2 && rem[1] < 0x80 {
193                             ret = (rem[0] & 0x7f) as u64 | (rem[1] as u64) << 7;
194                             consume = 2;
195                         } else if rem.len() >= 10 {
196                             // Read from array when buf at at least 10 bytes,
197                             // max len for varint.
198                             let mut r: u64 = 0;
199                             let mut i: usize = 0;
200                             {
201                                 let rem = rem;
202                                 loop {
203                                     if i == 10 {
204                                         return Err(ProtobufError::WireError(
205                                             WireError::IncorrectVarint,
206                                         ));
207                                     }
208 
209                                     let b = if true {
210                                         // skip range check
211                                         unsafe { *rem.get_unchecked(i) }
212                                     } else {
213                                         rem[i]
214                                     };
215 
216                                     // TODO: may overflow if i == 9
217                                     r = r | (((b & 0x7f) as u64) << (i * 7));
218                                     i += 1;
219                                     if b < 0x80 {
220                                         break;
221                                     }
222                                 }
223                             }
224                             consume = i;
225                             ret = r;
226                         } else {
227                             break 'slow;
228                         }
229                     }
230                 } else {
231                     break 'slow;
232                 }
233                 break;
234             }
235 
236             self.source.consume(consume);
237             return Ok(ret);
238         }
239 
240         self.read_raw_varint64_slow()
241     }
242 
243     /// Read varint
244     #[inline(always)]
read_raw_varint32(&mut self) -> ProtobufResult<u32>245     pub fn read_raw_varint32(&mut self) -> ProtobufResult<u32> {
246         self.read_raw_varint64().map(|v| v as u32)
247     }
248 
249     /// Read little-endian 32-bit integer
read_raw_little_endian32(&mut self) -> ProtobufResult<u32>250     pub fn read_raw_little_endian32(&mut self) -> ProtobufResult<u32> {
251         let mut r = 0u32;
252         let bytes: &mut [u8] = unsafe {
253             let p: *mut u8 = mem::transmute(&mut r);
254             slice::from_raw_parts_mut(p, mem::size_of::<u32>())
255         };
256         self.read(bytes)?;
257         Ok(r.to_le())
258     }
259 
260     /// Read little-endian 64-bit integer
read_raw_little_endian64(&mut self) -> ProtobufResult<u64>261     pub fn read_raw_little_endian64(&mut self) -> ProtobufResult<u64> {
262         let mut r = 0u64;
263         let bytes: &mut [u8] = unsafe {
264             let p: *mut u8 = mem::transmute(&mut r);
265             slice::from_raw_parts_mut(p, mem::size_of::<u64>())
266         };
267         self.read(bytes)?;
268         Ok(r.to_le())
269     }
270 
271     /// Read tag
272     #[inline]
read_tag(&mut self) -> ProtobufResult<wire_format::Tag>273     pub fn read_tag(&mut self) -> ProtobufResult<wire_format::Tag> {
274         let v = self.read_raw_varint32()?;
275         match wire_format::Tag::new(v) {
276             Some(tag) => Ok(tag),
277             None => Err(ProtobufError::WireError(WireError::IncorrectTag(v))),
278         }
279     }
280 
281     /// Read tag, return it is pair (field number, wire type)
282     #[inline]
read_tag_unpack(&mut self) -> ProtobufResult<(u32, wire_format::WireType)>283     pub fn read_tag_unpack(&mut self) -> ProtobufResult<(u32, wire_format::WireType)> {
284         self.read_tag().map(|t| t.unpack())
285     }
286 
287     /// Read `double`
read_double(&mut self) -> ProtobufResult<f64>288     pub fn read_double(&mut self) -> ProtobufResult<f64> {
289         let bits = self.read_raw_little_endian64()?;
290         unsafe { Ok(mem::transmute::<u64, f64>(bits)) }
291     }
292 
293     /// Read `float`
read_float(&mut self) -> ProtobufResult<f32>294     pub fn read_float(&mut self) -> ProtobufResult<f32> {
295         let bits = self.read_raw_little_endian32()?;
296         unsafe { Ok(mem::transmute::<u32, f32>(bits)) }
297     }
298 
299     /// Read `int64`
read_int64(&mut self) -> ProtobufResult<i64>300     pub fn read_int64(&mut self) -> ProtobufResult<i64> {
301         self.read_raw_varint64().map(|v| v as i64)
302     }
303 
304     /// Read `int32`
read_int32(&mut self) -> ProtobufResult<i32>305     pub fn read_int32(&mut self) -> ProtobufResult<i32> {
306         self.read_raw_varint32().map(|v| v as i32)
307     }
308 
309     /// Read `uint64`
read_uint64(&mut self) -> ProtobufResult<u64>310     pub fn read_uint64(&mut self) -> ProtobufResult<u64> {
311         self.read_raw_varint64()
312     }
313 
314     /// Read `uint32`
read_uint32(&mut self) -> ProtobufResult<u32>315     pub fn read_uint32(&mut self) -> ProtobufResult<u32> {
316         self.read_raw_varint32()
317     }
318 
319     /// Read `sint64`
read_sint64(&mut self) -> ProtobufResult<i64>320     pub fn read_sint64(&mut self) -> ProtobufResult<i64> {
321         self.read_uint64().map(decode_zig_zag_64)
322     }
323 
324     /// Read `sint32`
read_sint32(&mut self) -> ProtobufResult<i32>325     pub fn read_sint32(&mut self) -> ProtobufResult<i32> {
326         self.read_uint32().map(decode_zig_zag_32)
327     }
328 
329     /// Read `fixed64`
read_fixed64(&mut self) -> ProtobufResult<u64>330     pub fn read_fixed64(&mut self) -> ProtobufResult<u64> {
331         self.read_raw_little_endian64()
332     }
333 
334     /// Read `fixed32`
read_fixed32(&mut self) -> ProtobufResult<u32>335     pub fn read_fixed32(&mut self) -> ProtobufResult<u32> {
336         self.read_raw_little_endian32()
337     }
338 
339     /// Read `sfixed64`
read_sfixed64(&mut self) -> ProtobufResult<i64>340     pub fn read_sfixed64(&mut self) -> ProtobufResult<i64> {
341         self.read_raw_little_endian64().map(|v| v as i64)
342     }
343 
344     /// Read `sfixed32`
read_sfixed32(&mut self) -> ProtobufResult<i32>345     pub fn read_sfixed32(&mut self) -> ProtobufResult<i32> {
346         self.read_raw_little_endian32().map(|v| v as i32)
347     }
348 
349     /// Read `bool`
read_bool(&mut self) -> ProtobufResult<bool>350     pub fn read_bool(&mut self) -> ProtobufResult<bool> {
351         self.read_raw_varint32().map(|v| v != 0)
352     }
353 
354     /// Read `enum` as `ProtobufEnum`
read_enum<E: ProtobufEnum>(&mut self) -> ProtobufResult<E>355     pub fn read_enum<E: ProtobufEnum>(&mut self) -> ProtobufResult<E> {
356         let i = self.read_int32()?;
357         match ProtobufEnum::from_i32(i) {
358             Some(e) => Ok(e),
359             None => Err(ProtobufError::WireError(WireError::InvalidEnumValue(i))),
360         }
361     }
362 
363     /// Read `repeated` packed `double`
read_repeated_packed_double_into( &mut self, target: &mut Vec<f64>, ) -> ProtobufResult<()>364     pub fn read_repeated_packed_double_into(
365         &mut self,
366         target: &mut Vec<f64>,
367     ) -> ProtobufResult<()> {
368         let len = self.read_raw_varint64()?;
369 
370         target.reserve((len / 4) as usize);
371 
372         let old_limit = self.push_limit(len)?;
373         while !self.eof()? {
374             target.push(self.read_double()?);
375         }
376         self.pop_limit(old_limit);
377         Ok(())
378     }
379 
380     /// Read `repeated` packed `float`
read_repeated_packed_float_into(&mut self, target: &mut Vec<f32>) -> ProtobufResult<()>381     pub fn read_repeated_packed_float_into(&mut self, target: &mut Vec<f32>) -> ProtobufResult<()> {
382         let len = self.read_raw_varint64()?;
383 
384         target.reserve((len / 4) as usize);
385 
386         let old_limit = self.push_limit(len)?;
387         while !self.eof()? {
388             target.push(self.read_float()?);
389         }
390         self.pop_limit(old_limit);
391         Ok(())
392     }
393 
394     /// Read `repeated` packed `int64`
read_repeated_packed_int64_into(&mut self, target: &mut Vec<i64>) -> ProtobufResult<()>395     pub fn read_repeated_packed_int64_into(&mut self, target: &mut Vec<i64>) -> ProtobufResult<()> {
396         let len = self.read_raw_varint64()?;
397         let old_limit = self.push_limit(len as u64)?;
398         while !self.eof()? {
399             target.push(self.read_int64()?);
400         }
401         self.pop_limit(old_limit);
402         Ok(())
403     }
404 
405     /// Read repeated packed `int32`
read_repeated_packed_int32_into(&mut self, target: &mut Vec<i32>) -> ProtobufResult<()>406     pub fn read_repeated_packed_int32_into(&mut self, target: &mut Vec<i32>) -> ProtobufResult<()> {
407         let len = self.read_raw_varint64()?;
408         let old_limit = self.push_limit(len)?;
409         while !self.eof()? {
410             target.push(self.read_int32()?);
411         }
412         self.pop_limit(old_limit);
413         Ok(())
414     }
415 
416     /// Read repeated packed `uint64`
read_repeated_packed_uint64_into( &mut self, target: &mut Vec<u64>, ) -> ProtobufResult<()>417     pub fn read_repeated_packed_uint64_into(
418         &mut self,
419         target: &mut Vec<u64>,
420     ) -> ProtobufResult<()> {
421         let len = self.read_raw_varint64()?;
422         let old_limit = self.push_limit(len)?;
423         while !self.eof()? {
424             target.push(self.read_uint64()?);
425         }
426         self.pop_limit(old_limit);
427         Ok(())
428     }
429 
430     /// Read repeated packed `uint32`
read_repeated_packed_uint32_into( &mut self, target: &mut Vec<u32>, ) -> ProtobufResult<()>431     pub fn read_repeated_packed_uint32_into(
432         &mut self,
433         target: &mut Vec<u32>,
434     ) -> ProtobufResult<()> {
435         let len = self.read_raw_varint64()?;
436         let old_limit = self.push_limit(len)?;
437         while !self.eof()? {
438             target.push(self.read_uint32()?);
439         }
440         self.pop_limit(old_limit);
441         Ok(())
442     }
443 
444     /// Read repeated packed `sint64`
read_repeated_packed_sint64_into( &mut self, target: &mut Vec<i64>, ) -> ProtobufResult<()>445     pub fn read_repeated_packed_sint64_into(
446         &mut self,
447         target: &mut Vec<i64>,
448     ) -> ProtobufResult<()> {
449         let len = self.read_raw_varint64()?;
450         let old_limit = self.push_limit(len)?;
451         while !self.eof()? {
452             target.push(self.read_sint64()?);
453         }
454         self.pop_limit(old_limit);
455         Ok(())
456     }
457 
458     /// Read repeated packed `sint32`
read_repeated_packed_sint32_into( &mut self, target: &mut Vec<i32>, ) -> ProtobufResult<()>459     pub fn read_repeated_packed_sint32_into(
460         &mut self,
461         target: &mut Vec<i32>,
462     ) -> ProtobufResult<()> {
463         let len = self.read_raw_varint64()?;
464         let old_limit = self.push_limit(len)?;
465         while !self.eof()? {
466             target.push(self.read_sint32()?);
467         }
468         self.pop_limit(old_limit);
469         Ok(())
470     }
471 
472     /// Read repeated packed `fixed64`
read_repeated_packed_fixed64_into( &mut self, target: &mut Vec<u64>, ) -> ProtobufResult<()>473     pub fn read_repeated_packed_fixed64_into(
474         &mut self,
475         target: &mut Vec<u64>,
476     ) -> ProtobufResult<()> {
477         let len = self.read_raw_varint64()?;
478 
479         target.reserve((len / 8) as usize);
480 
481         let old_limit = self.push_limit(len)?;
482         while !self.eof()? {
483             target.push(self.read_fixed64()?);
484         }
485         self.pop_limit(old_limit);
486         Ok(())
487     }
488 
489     /// Read repeated packed `fixed32`
read_repeated_packed_fixed32_into( &mut self, target: &mut Vec<u32>, ) -> ProtobufResult<()>490     pub fn read_repeated_packed_fixed32_into(
491         &mut self,
492         target: &mut Vec<u32>,
493     ) -> ProtobufResult<()> {
494         let len = self.read_raw_varint64()?;
495 
496         target.reserve((len / 4) as usize);
497 
498         let old_limit = self.push_limit(len)?;
499         while !self.eof()? {
500             target.push(self.read_fixed32()?);
501         }
502         self.pop_limit(old_limit);
503         Ok(())
504     }
505 
506     /// Read repeated packed `sfixed64`
read_repeated_packed_sfixed64_into( &mut self, target: &mut Vec<i64>, ) -> ProtobufResult<()>507     pub fn read_repeated_packed_sfixed64_into(
508         &mut self,
509         target: &mut Vec<i64>,
510     ) -> ProtobufResult<()> {
511         let len = self.read_raw_varint64()?;
512 
513         target.reserve((len / 8) as usize);
514 
515         let old_limit = self.push_limit(len)?;
516         while !self.eof()? {
517             target.push(self.read_sfixed64()?);
518         }
519         self.pop_limit(old_limit);
520         Ok(())
521     }
522 
523     /// Read repeated packed `sfixed32`
read_repeated_packed_sfixed32_into( &mut self, target: &mut Vec<i32>, ) -> ProtobufResult<()>524     pub fn read_repeated_packed_sfixed32_into(
525         &mut self,
526         target: &mut Vec<i32>,
527     ) -> ProtobufResult<()> {
528         let len = self.read_raw_varint64()?;
529 
530         target.reserve((len / 4) as usize);
531 
532         let old_limit = self.push_limit(len)?;
533         while !self.eof()? {
534             target.push(self.read_sfixed32()?);
535         }
536         self.pop_limit(old_limit);
537         Ok(())
538     }
539 
540     /// Read repeated packed `bool`
read_repeated_packed_bool_into(&mut self, target: &mut Vec<bool>) -> ProtobufResult<()>541     pub fn read_repeated_packed_bool_into(&mut self, target: &mut Vec<bool>) -> ProtobufResult<()> {
542         let len = self.read_raw_varint64()?;
543 
544         // regular bool value is 1-byte size
545         target.reserve(len as usize);
546 
547         let old_limit = self.push_limit(len)?;
548         while !self.eof()? {
549             target.push(self.read_bool()?);
550         }
551         self.pop_limit(old_limit);
552         Ok(())
553     }
554 
555     /// Read repeated packed `enum` into `ProtobufEnum`
read_repeated_packed_enum_into<E: ProtobufEnum>( &mut self, target: &mut Vec<E>, ) -> ProtobufResult<()>556     pub fn read_repeated_packed_enum_into<E: ProtobufEnum>(
557         &mut self,
558         target: &mut Vec<E>,
559     ) -> ProtobufResult<()> {
560         let len = self.read_raw_varint64()?;
561         let old_limit = self.push_limit(len)?;
562         while !self.eof()? {
563             target.push(self.read_enum()?);
564         }
565         self.pop_limit(old_limit);
566         Ok(())
567     }
568 
569     /// Read `UnknownValue`
read_unknown( &mut self, wire_type: wire_format::WireType, ) -> ProtobufResult<UnknownValue>570     pub fn read_unknown(
571         &mut self,
572         wire_type: wire_format::WireType,
573     ) -> ProtobufResult<UnknownValue> {
574         match wire_type {
575             wire_format::WireTypeVarint => {
576                 self.read_raw_varint64().map(|v| UnknownValue::Varint(v))
577             }
578             wire_format::WireTypeFixed64 => self.read_fixed64().map(|v| UnknownValue::Fixed64(v)),
579             wire_format::WireTypeFixed32 => self.read_fixed32().map(|v| UnknownValue::Fixed32(v)),
580             wire_format::WireTypeLengthDelimited => {
581                 let len = self.read_raw_varint32()?;
582                 self.read_raw_bytes(len)
583                     .map(|v| UnknownValue::LengthDelimited(v))
584             }
585             _ => Err(ProtobufError::WireError(WireError::UnexpectedWireType(
586                 wire_type,
587             ))),
588         }
589     }
590 
591     /// Skip field
skip_field(&mut self, wire_type: wire_format::WireType) -> ProtobufResult<()>592     pub fn skip_field(&mut self, wire_type: wire_format::WireType) -> ProtobufResult<()> {
593         self.read_unknown(wire_type).map(|_| ())
594     }
595 
596     /// Read raw bytes into the supplied vector.  The vector will be resized as needed and
597     /// overwritten.
read_raw_bytes_into(&mut self, count: u32, target: &mut Vec<u8>) -> ProtobufResult<()>598     pub fn read_raw_bytes_into(&mut self, count: u32, target: &mut Vec<u8>) -> ProtobufResult<()> {
599         if false {
600             // Master uses this version, but keep existing version for a while
601             // to avoid possible breakages.
602             return self.source.read_exact_to_vec(count as usize, target);
603         }
604 
605         let count = count as usize;
606 
607         // TODO: also do some limits when reading from unlimited source
608         if count as u64 > self.source.bytes_until_limit() {
609             return Err(ProtobufError::WireError(WireError::TruncatedMessage));
610         }
611 
612         unsafe {
613             target.set_len(0);
614         }
615 
616         if count >= READ_RAW_BYTES_MAX_ALLOC {
617             // avoid calling `reserve` on buf with very large buffer: could be a malformed message
618 
619             let mut take = self.by_ref().take(count as u64);
620             take.read_to_end(target)?;
621 
622             if target.len() != count {
623                 return Err(ProtobufError::WireError(WireError::TruncatedMessage));
624             }
625         } else {
626             target.reserve(count);
627             unsafe {
628                 target.set_len(count);
629             }
630 
631             self.source.read_exact(target)?;
632         }
633         Ok(())
634     }
635 
636     /// Read exact number of bytes
read_raw_bytes(&mut self, count: u32) -> ProtobufResult<Vec<u8>>637     pub fn read_raw_bytes(&mut self, count: u32) -> ProtobufResult<Vec<u8>> {
638         let mut r = Vec::new();
639         self.read_raw_bytes_into(count, &mut r)?;
640         Ok(r)
641     }
642 
643     /// Skip exact number of bytes
skip_raw_bytes(&mut self, count: u32) -> ProtobufResult<()>644     pub fn skip_raw_bytes(&mut self, count: u32) -> ProtobufResult<()> {
645         // TODO: make it more efficient
646         self.read_raw_bytes(count).map(|_| ())
647     }
648 
649     /// Read `bytes` field, length delimited
read_bytes(&mut self) -> ProtobufResult<Vec<u8>>650     pub fn read_bytes(&mut self) -> ProtobufResult<Vec<u8>> {
651         let mut r = Vec::new();
652         self.read_bytes_into(&mut r)?;
653         Ok(r)
654     }
655 
656     /// Read `bytes` field, length delimited
657     #[cfg(feature = "bytes")]
read_carllerche_bytes(&mut self) -> ProtobufResult<Bytes>658     pub fn read_carllerche_bytes(&mut self) -> ProtobufResult<Bytes> {
659         let len = self.read_raw_varint32()?;
660         self.read_raw_callerche_bytes(len as usize)
661     }
662 
663     /// Read `string` field, length delimited
664     #[cfg(feature = "bytes")]
read_carllerche_chars(&mut self) -> ProtobufResult<Chars>665     pub fn read_carllerche_chars(&mut self) -> ProtobufResult<Chars> {
666         let bytes = self.read_carllerche_bytes()?;
667         Ok(Chars::from_bytes(bytes)?)
668     }
669 
670     /// Read `bytes` field, length delimited
read_bytes_into(&mut self, target: &mut Vec<u8>) -> ProtobufResult<()>671     pub fn read_bytes_into(&mut self, target: &mut Vec<u8>) -> ProtobufResult<()> {
672         let len = self.read_raw_varint32()?;
673         self.read_raw_bytes_into(len, target)?;
674         Ok(())
675     }
676 
677     /// Read `string` field, length delimited
read_string(&mut self) -> ProtobufResult<String>678     pub fn read_string(&mut self) -> ProtobufResult<String> {
679         let mut r = String::new();
680         self.read_string_into(&mut r)?;
681         Ok(r)
682     }
683 
684     /// Read `string` field, length delimited
read_string_into(&mut self, target: &mut String) -> ProtobufResult<()>685     pub fn read_string_into(&mut self, target: &mut String) -> ProtobufResult<()> {
686         target.clear();
687         // take target's buffer
688         let mut vec = mem::replace(target, String::new()).into_bytes();
689         self.read_bytes_into(&mut vec)?;
690 
691         let s = match String::from_utf8(vec) {
692             Ok(t) => t,
693             Err(_) => return Err(ProtobufError::WireError(WireError::Utf8Error)),
694         };
695         *target = s;
696         Ok(())
697     }
698 
699     /// Read message, do not check if message is initialized
merge_message<M: Message>(&mut self, message: &mut M) -> ProtobufResult<()>700     pub fn merge_message<M: Message>(&mut self, message: &mut M) -> ProtobufResult<()> {
701         let len = self.read_raw_varint64()?;
702         let old_limit = self.push_limit(len)?;
703         message.merge_from(self)?;
704         self.pop_limit(old_limit);
705         Ok(())
706     }
707 
708     /// Read message
read_message<M: Message>(&mut self) -> ProtobufResult<M>709     pub fn read_message<M: Message>(&mut self) -> ProtobufResult<M> {
710         let mut r: M = Message::new();
711         self.merge_message(&mut r)?;
712         r.check_initialized()?;
713         Ok(r)
714     }
715 }
716 
717 impl<'a> Read for CodedInputStream<'a> {
read(&mut self, buf: &mut [u8]) -> io::Result<usize>718     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
719         self.source.read(buf).map_err(Into::into)
720     }
721 }
722 
723 impl<'a> BufRead for CodedInputStream<'a> {
fill_buf(&mut self) -> io::Result<&[u8]>724     fn fill_buf(&mut self) -> io::Result<&[u8]> {
725         self.source.fill_buf().map_err(Into::into)
726     }
727 
consume(&mut self, amt: usize)728     fn consume(&mut self, amt: usize) {
729         self.source.consume(amt)
730     }
731 }
732 
733 /// Helper internal utility, should not be used directly
734 #[doc(hidden)]
735 pub trait WithCodedInputStream {
with_coded_input_stream<T, F>(self, cb: F) -> ProtobufResult<T> where F: FnOnce(&mut CodedInputStream) -> ProtobufResult<T>736     fn with_coded_input_stream<T, F>(self, cb: F) -> ProtobufResult<T>
737     where
738         F: FnOnce(&mut CodedInputStream) -> ProtobufResult<T>;
739 }
740 
741 impl<'a> WithCodedInputStream for &'a mut (dyn Read + 'a) {
with_coded_input_stream<T, F>(self, cb: F) -> ProtobufResult<T> where F: FnOnce(&mut CodedInputStream) -> ProtobufResult<T>,742     fn with_coded_input_stream<T, F>(self, cb: F) -> ProtobufResult<T>
743     where
744         F: FnOnce(&mut CodedInputStream) -> ProtobufResult<T>,
745     {
746         let mut is = CodedInputStream::new(self);
747         let r = cb(&mut is)?;
748         is.check_eof()?;
749         Ok(r)
750     }
751 }
752 
753 impl<'a> WithCodedInputStream for &'a mut (dyn BufRead + 'a) {
with_coded_input_stream<T, F>(self, cb: F) -> ProtobufResult<T> where F: FnOnce(&mut CodedInputStream) -> ProtobufResult<T>,754     fn with_coded_input_stream<T, F>(self, cb: F) -> ProtobufResult<T>
755     where
756         F: FnOnce(&mut CodedInputStream) -> ProtobufResult<T>,
757     {
758         let mut is = CodedInputStream::from_buffered_reader(self);
759         let r = cb(&mut is)?;
760         is.check_eof()?;
761         Ok(r)
762     }
763 }
764 
765 impl<'a> WithCodedInputStream for &'a [u8] {
with_coded_input_stream<T, F>(self, cb: F) -> ProtobufResult<T> where F: FnOnce(&mut CodedInputStream) -> ProtobufResult<T>,766     fn with_coded_input_stream<T, F>(self, cb: F) -> ProtobufResult<T>
767     where
768         F: FnOnce(&mut CodedInputStream) -> ProtobufResult<T>,
769     {
770         let mut is = CodedInputStream::from_bytes(self);
771         let r = cb(&mut is)?;
772         is.check_eof()?;
773         Ok(r)
774     }
775 }
776 
777 #[cfg(feature = "bytes")]
778 impl<'a> WithCodedInputStream for &'a Bytes {
with_coded_input_stream<T, F>(self, cb: F) -> ProtobufResult<T> where F: FnOnce(&mut CodedInputStream) -> ProtobufResult<T>,779     fn with_coded_input_stream<T, F>(self, cb: F) -> ProtobufResult<T>
780     where
781         F: FnOnce(&mut CodedInputStream) -> ProtobufResult<T>,
782     {
783         let mut is = CodedInputStream::from_carllerche_bytes(self);
784         let r = cb(&mut is)?;
785         is.check_eof()?;
786         Ok(r)
787     }
788 }
789 
790 #[cfg(test)]
791 mod test {
792 
793     use std::fmt::Debug;
794     use std::io;
795     use std::io::BufRead;
796     use std::io::Read;
797 
798     use crate::error::ProtobufError;
799     use crate::error::ProtobufResult;
800     use crate::hex::decode_hex;
801 
802     use super::CodedInputStream;
803     use super::READ_RAW_BYTES_MAX_ALLOC;
804 
test_read_partial<F>(hex: &str, mut callback: F) where F: FnMut(&mut CodedInputStream),805     fn test_read_partial<F>(hex: &str, mut callback: F)
806     where
807         F: FnMut(&mut CodedInputStream),
808     {
809         let d = decode_hex(hex);
810         let mut reader = io::Cursor::new(d);
811         let mut is = CodedInputStream::from_buffered_reader(&mut reader as &mut dyn BufRead);
812         assert_eq!(0, is.pos());
813         callback(&mut is);
814     }
815 
test_read<F>(hex: &str, mut callback: F) where F: FnMut(&mut CodedInputStream),816     fn test_read<F>(hex: &str, mut callback: F)
817     where
818         F: FnMut(&mut CodedInputStream),
819     {
820         let len = decode_hex(hex).len();
821         test_read_partial(hex, |reader| {
822             callback(reader);
823             assert!(reader.eof().expect("eof"));
824             assert_eq!(len as u64, reader.pos());
825         });
826     }
827 
test_read_v<F, V>(hex: &str, v: V, mut callback: F) where F: FnMut(&mut CodedInputStream) -> ProtobufResult<V>, V: PartialEq + Debug,828     fn test_read_v<F, V>(hex: &str, v: V, mut callback: F)
829     where
830         F: FnMut(&mut CodedInputStream) -> ProtobufResult<V>,
831         V: PartialEq + Debug,
832     {
833         test_read(hex, |reader| {
834             assert_eq!(v, callback(reader).unwrap());
835         });
836     }
837 
838     #[test]
test_input_stream_read_raw_byte()839     fn test_input_stream_read_raw_byte() {
840         test_read("17", |is| {
841             assert_eq!(23, is.read_raw_byte().unwrap());
842         });
843     }
844 
845     #[test]
test_input_stream_read_raw_varint()846     fn test_input_stream_read_raw_varint() {
847         test_read_v("07", 7, |reader| reader.read_raw_varint32());
848         test_read_v("07", 7, |reader| reader.read_raw_varint64());
849 
850         test_read_v("96 01", 150, |reader| reader.read_raw_varint32());
851         test_read_v("96 01", 150, |reader| reader.read_raw_varint64());
852 
853         test_read_v(
854             "ff ff ff ff ff ff ff ff ff 01",
855             0xffffffffffffffff,
856             |reader| reader.read_raw_varint64(),
857         );
858 
859         test_read_v("ff ff ff ff 0f", 0xffffffff, |reader| {
860             reader.read_raw_varint32()
861         });
862         test_read_v("ff ff ff ff 0f", 0xffffffff, |reader| {
863             reader.read_raw_varint64()
864         });
865     }
866 
867     #[test]
test_input_stream_read_raw_vaint_malformed()868     fn test_input_stream_read_raw_vaint_malformed() {
869         // varint cannot have length > 10
870         test_read_partial("ff ff ff ff ff ff ff ff ff ff 01", |reader| {
871             let result = reader.read_raw_varint64();
872             match result {
873                 // TODO: make an enum variant
874                 Err(ProtobufError::WireError(..)) => (),
875                 _ => panic!(),
876             }
877         });
878         test_read_partial("ff ff ff ff ff ff ff ff ff ff 01", |reader| {
879             let result = reader.read_raw_varint32();
880             match result {
881                 // TODO: make an enum variant
882                 Err(ProtobufError::WireError(..)) => (),
883                 _ => panic!(),
884             }
885         });
886     }
887 
888     #[test]
test_input_stream_read_raw_varint_unexpected_eof()889     fn test_input_stream_read_raw_varint_unexpected_eof() {
890         test_read_partial("96 97", |reader| {
891             let result = reader.read_raw_varint32();
892             match result {
893                 Err(ProtobufError::WireError(..)) => (),
894                 _ => panic!(),
895             }
896         });
897     }
898 
899     #[test]
test_input_stream_read_raw_varint_pos()900     fn test_input_stream_read_raw_varint_pos() {
901         test_read_partial("95 01 98", |reader| {
902             assert_eq!(149, reader.read_raw_varint32().unwrap());
903             assert_eq!(2, reader.pos());
904         });
905     }
906 
907     #[test]
test_input_stream_read_int32()908     fn test_input_stream_read_int32() {
909         test_read_v("02", 2, |reader| reader.read_int32());
910     }
911 
912     #[test]
test_input_stream_read_float()913     fn test_input_stream_read_float() {
914         test_read_v("95 73 13 61", 17e19, |is| is.read_float());
915     }
916 
917     #[test]
test_input_stream_read_double()918     fn test_input_stream_read_double() {
919         test_read_v("40 d5 ab 68 b3 07 3d 46", 23e29, |is| is.read_double());
920     }
921 
922     #[test]
test_input_stream_skip_raw_bytes()923     fn test_input_stream_skip_raw_bytes() {
924         test_read("", |reader| {
925             reader.skip_raw_bytes(0).unwrap();
926         });
927         test_read("aa bb", |reader| {
928             reader.skip_raw_bytes(2).unwrap();
929         });
930         test_read("aa bb cc dd ee ff", |reader| {
931             reader.skip_raw_bytes(6).unwrap();
932         });
933     }
934 
935     #[test]
test_input_stream_read_raw_bytes()936     fn test_input_stream_read_raw_bytes() {
937         test_read("", |reader| {
938             assert_eq!(
939                 Vec::from(&b""[..]),
940                 reader.read_raw_bytes(0).expect("read_raw_bytes")
941             );
942         })
943     }
944 
945     #[test]
test_input_stream_limits()946     fn test_input_stream_limits() {
947         test_read("aa bb cc", |is| {
948             let old_limit = is.push_limit(1).unwrap();
949             assert_eq!(1, is.bytes_until_limit());
950             let r1 = is.read_raw_bytes(1).unwrap();
951             assert_eq!(&[0xaa as u8], &r1[..]);
952             is.pop_limit(old_limit);
953             let r2 = is.read_raw_bytes(2).unwrap();
954             assert_eq!(&[0xbb as u8, 0xcc], &r2[..]);
955         });
956     }
957 
958     #[test]
test_input_stream_io_read()959     fn test_input_stream_io_read() {
960         test_read("aa bb cc", |is| {
961             let mut buf = [0; 3];
962             assert_eq!(Read::read(is, &mut buf).expect("io::Read"), 3);
963             assert_eq!(buf, [0xaa, 0xbb, 0xcc]);
964         });
965     }
966 
967     #[test]
test_input_stream_io_bufread()968     fn test_input_stream_io_bufread() {
969         test_read("aa bb cc", |is| {
970             assert_eq!(
971                 BufRead::fill_buf(is).expect("io::BufRead::fill_buf"),
972                 &[0xaa, 0xbb, 0xcc]
973             );
974             BufRead::consume(is, 3);
975         });
976     }
977 
978     #[test]
test_input_stream_read_raw_bytes_into_huge()979     fn test_input_stream_read_raw_bytes_into_huge() {
980         let mut v = Vec::new();
981         for i in 0..READ_RAW_BYTES_MAX_ALLOC + 1000 {
982             v.push((i % 10) as u8);
983         }
984 
985         let mut slice: &[u8] = v.as_slice();
986 
987         let mut is = CodedInputStream::new(&mut slice);
988 
989         let mut buf = Vec::new();
990 
991         is.read_raw_bytes_into(READ_RAW_BYTES_MAX_ALLOC as u32 + 10, &mut buf)
992             .expect("read");
993 
994         assert_eq!(READ_RAW_BYTES_MAX_ALLOC + 10, buf.len());
995 
996         buf.clear();
997 
998         is.read_raw_bytes_into(1000 - 10, &mut buf).expect("read");
999 
1000         assert_eq!(1000 - 10, buf.len());
1001 
1002         assert!(is.eof().expect("eof"));
1003     }
1004 }
1005