(bytes: &mut R)
| 21 | /// The length and number of bytes read are returned. |
| 22 | #[inline] |
| 23 | fn read_variable_length<R: std::io::Read>(bytes: &mut R) -> Result<(usize, usize), Error> { |
| 24 | // The length is encoded in the first two bits of the first byte. |
| 25 | let mut len_len_byte = [0u8; 1]; |
| 26 | if bytes.read(&mut len_len_byte)? == 0 { |
| 27 | // Return in case there's nothing to read and this is just an |
| 28 | // empty vector. |
| 29 | return Ok((0, 0)); |
| 30 | } |
| 31 | |
| 32 | let mut length: usize = (len_len_byte[0] & 0x3F).into(); |
| 33 | let len_len = (len_len_byte[0] >> 6).into(); |
| 34 | debug_assert!(len_len <= 3); |
| 35 | if len_len > 3 { |
| 36 | return Err(Error::InvalidVectorLength); |
| 37 | } |
| 38 | for _ in 0..len_len { |
| 39 | let mut next = [0u8; 1]; |
| 40 | bytes.read_exact(&mut next)?; |
| 41 | length = (length << 8) + usize::from(next[0]); |
| 42 | } |
| 43 | |
| 44 | Ok((length, len_len)) |
| 45 | } |
| 46 | |
| 47 | #[inline] |
| 48 | fn length_encoding_bytes(length: u64) -> usize { |
no test coverage detected