| 1 |
use crate::err::Res; |
| 2 |
#[cfg(feature = "read-bhttp")] |
| 3 |
use crate::{err::Error, ReadSeek}; |
| 4 |
#[cfg(feature = "read-bhttp")] |
| 5 |
use std::borrow::BorrowMut; |
| 6 |
use std::{convert::TryFrom, io}; |
| 7 |
|
| 8 |
#[cfg(feature = "write-bhttp")] |
| 9 |
#[allow(clippy::cast_possible_truncation)] |
| 10 |
fn write_uint(n: u8, v: impl Into<u64>, w: &mut impl io::Write) -> Res<()> { |
| 11 |
let v = v.into(); |
| 12 |
assert!(n > 0 && usize::from(n) < std::mem::size_of::<u64>()); |
| 13 |
for i in 0..n { |
| 14 |
w.write_all(&[((v >> (8 * (n - i - 1))) & 0xff) as u8])?; |
| 15 |
} |
| 16 |
Ok(()) |
| 17 |
} |
| 18 |
|
| 19 |
#[cfg(feature = "write-bhttp")] |
| 20 |
pub fn write_varint(v: impl Into<u64>, w: &mut impl io::Write) -> Res<()> { |
| 21 |
let v = v.into(); |
| 22 |
match () { |
| 23 |
_ if v < (1 << 6) => write_uint(1, v, w), |
| 24 |
_ if v < (1 << 14) => write_uint(2, v | (1 << 14), w), |
| 25 |
_ if v < (1 << 30) => write_uint(4, v | (2 << 30), w), |
| 26 |
_ if v < (1 << 62) => write_uint(8, v | (3 << 62), w), |
| 27 |
_ => panic!("Varint value too large"), |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
#[cfg(feature = "write-bhttp")] |
| 32 |
pub fn write_len(len: usize, w: &mut impl io::Write) -> Res<()> { |
| 33 |
write_varint(u64::try_from(len).unwrap(), w) |
| 34 |
} |
| 35 |
|
| 36 |
#[cfg(feature = "write-bhttp")] |
| 37 |
pub fn write_vec(v: &[u8], w: &mut impl io::Write) -> Res<()> { |
| 38 |
write_len(v.len(), w)?; |
| 39 |
w.write_all(v)?; |
| 40 |
Ok(()) |
| 41 |
} |
| 42 |
|
| 43 |
#[cfg(feature = "read-bhttp")] |
| 44 |
fn read_uint<T, R>(n: usize, r: &mut T) -> Res<Option<u64>> |
| 45 |
where |
| 46 |
T: BorrowMut<R> + ?Sized, |
| 47 |
R: ReadSeek + ?Sized, |
| 48 |
{ |
| 49 |
let mut buf = [0; 7]; |
| 50 |
let count = r.borrow_mut().read(&mut buf[..n])?; |
| 51 |
if count == 0 { |
| 52 |
return Ok(None); |
| 53 |
} else if count < n { |
| 54 |
return Err(Error::Truncated); |
| 55 |
} |
| 56 |
let mut v = 0; |
| 57 |
for i in &buf[..n] { |
| 58 |
v = (v << 8) | u64::from(*i); |
| 59 |
} |
| 60 |
Ok(Some(v)) |
| 61 |
} |
| 62 |
|
| 63 |
#[cfg(feature = "read-bhttp")] |
| 64 |
pub fn read_varint<T, R>(r: &mut T) -> Res<Option<u64>> |
| 65 |
where |
| 66 |
T: BorrowMut<R> + ?Sized, |
| 67 |
R: ReadSeek + ?Sized, |
| 68 |
{ |
| 69 |
if let Some(b1) = read_uint(1, r)? { |
| 70 |
Ok(Some(match b1 >> 6 { |
| 71 |
0 => b1 & 0x3f, |
| 72 |
1 => ((b1 & 0x3f) << 8) | read_uint(1, r)?.ok_or(Error::Truncated)?, |
| 73 |
2 => ((b1 & 0x3f) << 24) | read_uint(3, r)?.ok_or(Error::Truncated)?, |
| 74 |
3 => ((b1 & 0x3f) << 56) | read_uint(7, r)?.ok_or(Error::Truncated)?, |
| 75 |
_ => unreachable!(), |
| 76 |
})) |
| 77 |
} else { |
| 78 |
Ok(None) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
#[cfg(feature = "read-bhttp")] |
| 83 |
pub fn read_vec<T, R>(r: &mut T) -> Res<Option<Vec<u8>>> |
| 84 |
where |
| 85 |
T: BorrowMut<R> + ?Sized, |
| 86 |
R: ReadSeek + ?Sized, |
| 87 |
{ |
| 88 |
use std::io::SeekFrom; |
| 89 |
|
| 90 |
if let Some(len) = read_varint(r)? { |
| 91 |
// Check that the input contains enough data. Before allocating. |
| 92 |
let r = r.borrow_mut(); |
| 93 |
let pos = r.stream_position()?; |
| 94 |
let end = r.seek(SeekFrom::End(0))?; |
| 95 |
if end - pos < len { |
| 96 |
return Err(Error::Truncated); |
| 97 |
} |
| 98 |
let _ = r.seek(SeekFrom::Start(pos))?; |
| 99 |
|
| 100 |
let mut v = vec![0; usize::try_from(len)?]; |
| 101 |
r.read_exact(&mut v)?; |
| 102 |
Ok(Some(v)) |
| 103 |
} else { |
| 104 |
Ok(None) |
| 105 |
} |
| 106 |
} |