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