use std::{
future::Future,
io,
num::NonZeroU64,
ops::RangeBounds,
pin::Pin,
task::{self, ready, Poll},
};
use tokio::io::{AsyncBufRead, AsyncRead, AsyncReadExt, ReadBuf};
use trailer::{read_trailer, ReadTrailer, Trailer};
#[doc(hidden)]
pub use self::trailer::Pad;
pub(crate) use self::trailer::Tag;
mod trailer;
#[derive(Debug)]
#[allow(private_bounds)]
pub struct BytesReader<R, T: Tag = Pad> {
state: State<R, T>,
}
#[inline(always)]
fn split_user_len(user_len: NonZeroU64) -> (u64, u8) {
let n = user_len.get() - 1;
let body_len = n & !7;
let tail_len = (n & 7) as u8 + 1;
(body_len, tail_len)
}
#[derive(Debug)]
enum State<R, T: Tag> {
Body {
reader: Option<R>,
consumed: u64,
user_len: NonZeroU64,
},
ReadTrailer(ReadTrailer<R, T>),
ReleaseTrailer { consumed: u8, data: Trailer },
}
impl<R> BytesReader<R>
where
R: AsyncRead + Unpin,
{
pub async fn new<S: RangeBounds<u64>>(reader: R, allowed_size: S) -> io::Result<Self> {
BytesReader::new_internal(reader, allowed_size).await
}
}
#[allow(private_bounds)]
impl<R, T: Tag> BytesReader<R, T>
where
R: AsyncRead + Unpin,
{
pub(crate) async fn new_internal<S: RangeBounds<u64>>(
mut reader: R,
allowed_size: S,
) -> io::Result<Self> {
let size = reader.read_u64_le().await?;
if !allowed_size.contains(&size) {
return Err(io::Error::new(io::ErrorKind::InvalidData, "invalid size"));
}
Ok(Self {
state: match NonZeroU64::new(size) {
Some(size) => State::Body {
reader: Some(reader),
consumed: 0,
user_len: size,
},
None => State::ReleaseTrailer {
consumed: 0,
data: read_trailer::<R, T>(reader, 0).await?,
},
},
})
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn len(&self) -> u64 {
match self.state {
State::Body {
consumed, user_len, ..
} => user_len.get() - consumed,
State::ReadTrailer(ref fut) => fut.len() as u64,
State::ReleaseTrailer { consumed, ref data } => data.len() as u64 - consumed as u64,
}
}
}
#[allow(private_bounds)]
impl<R: AsyncRead + Unpin, T: Tag> AsyncRead for BytesReader<R, T> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut task::Context,
buf: &mut ReadBuf,
) -> Poll<io::Result<()>> {
let this = &mut self.state;
loop {
match this {
State::Body {
reader,
consumed,
user_len,
} => {
let (body_len, tail_len) = split_user_len(*user_len);
let remaining = body_len - *consumed;
let reader = if remaining == 0 {
let reader = reader.take().unwrap();
*this = State::ReadTrailer(read_trailer(reader, tail_len));
continue;
} else {
Pin::new(reader.as_mut().unwrap())
};
let mut bytes_read = 0;
ready!(with_limited(buf, remaining, |buf| {
let ret = reader.poll_read(cx, buf);
bytes_read = buf.filled().len();
ret
}))?;
*consumed += bytes_read as u64;
return if bytes_read != 0 {
Ok(())
} else {
Err(io::ErrorKind::UnexpectedEof.into())
}
.into();
}
State::ReadTrailer(fut) => {
*this = State::ReleaseTrailer {
consumed: 0,
data: ready!(Pin::new(fut).poll(cx))?,
};
}
State::ReleaseTrailer { consumed, data } => {
let data = &data[*consumed as usize..];
let data = &data[..usize::min(data.len(), buf.remaining())];
buf.put_slice(data);
*consumed += data.len() as u8;
return Ok(()).into();
}
}
}
}
}
#[allow(private_bounds)]
impl<R: AsyncBufRead + Unpin, T: Tag> AsyncBufRead for BytesReader<R, T> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut task::Context) -> Poll<io::Result<&[u8]>> {
let this = &mut self.get_mut().state;
loop {
match this {
State::Body {
reader,
consumed,
user_len,
} if {
let (body_len, _) = split_user_len(*user_len);
let remaining = body_len - *consumed;
remaining == 0
} =>
{
let reader = reader.take().unwrap();
let (_, tail_len) = split_user_len(*user_len);
*this = State::ReadTrailer(read_trailer(reader, tail_len));
}
State::Body {
reader,
consumed,
user_len,
} => {
let (body_len, _) = split_user_len(*user_len);
let remaining = body_len - *consumed;
let reader = Pin::new(reader.as_mut().unwrap());
match ready!(reader.poll_fill_buf(cx))? {
&[] => {
return Err(io::ErrorKind::UnexpectedEof.into()).into();
}
mut buf => {
if buf.len() as u64 > remaining {
buf = &buf[..remaining as usize];
}
return Ok(buf).into();
}
}
}
State::ReadTrailer(fut) => {
*this = State::ReleaseTrailer {
consumed: 0,
data: ready!(Pin::new(fut).poll(cx))?,
};
}
State::ReleaseTrailer { consumed, data } => {
return Ok(&data[*consumed as usize..]).into();
}
}
}
}
fn consume(mut self: Pin<&mut Self>, amt: usize) {
match &mut self.state {
State::Body {
reader,
consumed,
user_len,
} => {
let reader = Pin::new(reader.as_mut().unwrap());
let (body_len, _) = split_user_len(*user_len);
*consumed = consumed
.checked_add(amt as u64)
.filter(|&consumed| consumed <= body_len)
.expect("consumed out of bounds");
reader.consume(amt);
}
State::ReadTrailer(_) => unreachable!(),
State::ReleaseTrailer { consumed, data } => {
*consumed = amt
.checked_add(*consumed as usize)
.filter(|&consumed| consumed <= data.len())
.expect("consumed out of bounds") as u8;
}
}
}
}
fn with_limited<R>(buf: &mut ReadBuf, n: u64, f: impl FnOnce(&mut ReadBuf) -> R) -> R {
let mut nbuf = buf.take(n.try_into().unwrap_or(usize::MAX));
let ptr = nbuf.initialized().as_ptr();
let ret = f(&mut nbuf);
unsafe {
assert_eq!(nbuf.initialized().as_ptr(), ptr);
let n = nbuf.filled().len();
buf.assume_init(n);
buf.advance(n);
}
ret
}
#[cfg(test)]
mod tests {
use std::sync::LazyLock;
use std::time::Duration;
use crate::wire::bytes::{padding_len, write_bytes};
use hex_literal::hex;
use rstest::rstest;
use tokio::io::{AsyncReadExt, BufReader};
use tokio_test::io::Builder;
use super::*;
const MAX_LEN: u64 = 1024;
pub static LARGE_PAYLOAD: LazyLock<Vec<u8>> =
LazyLock::new(|| (0..255).collect::<Vec<u8>>().repeat(4 * 1024));
async fn produce_packet_bytes(payload: &[u8]) -> Vec<u8> {
let mut exp = vec![];
write_bytes(&mut exp, payload).await.unwrap();
exp
}
#[rstest]
#[case::empty(&[])] #[case::size_1b(&[0xff])] #[case::size_8b(&hex!("0001020304050607"))] #[case::size_9b(&hex!("000102030405060708"))] #[case::size_1m(LARGE_PAYLOAD.as_slice())] #[tokio::test]
async fn read_payload_correct(#[case] payload: &[u8]) {
let mut mock = Builder::new()
.read(&produce_packet_bytes(payload).await)
.build();
let mut r = BytesReader::new(&mut mock, ..=LARGE_PAYLOAD.len() as u64)
.await
.unwrap();
let mut buf = Vec::new();
r.read_to_end(&mut buf).await.expect("must succeed");
assert_eq!(payload, &buf[..]);
}
#[rstest]
#[case::empty(&[])] #[case::size_1b(&[0xff])] #[case::size_8b(&hex!("0001020304050607"))] #[case::size_9b(&hex!("000102030405060708"))] #[case::size_1m(LARGE_PAYLOAD.as_slice())] #[tokio::test]
async fn read_payload_correct_readbuf(#[case] payload: &[u8]) {
let mut mock = BufReader::new(
Builder::new()
.read(&produce_packet_bytes(payload).await)
.build(),
);
let mut r = BytesReader::new(&mut mock, ..=LARGE_PAYLOAD.len() as u64)
.await
.unwrap();
let mut buf = Vec::new();
tokio::io::copy_buf(&mut r, &mut buf)
.await
.expect("copy_buf must succeed");
assert_eq!(payload, &buf[..]);
}
#[tokio::test]
async fn read_bigger_than_allowed_fail() {
let payload = LARGE_PAYLOAD.as_slice();
let mut mock = Builder::new()
.read(&produce_packet_bytes(payload).await[0..8]) .build();
assert_eq!(
BytesReader::new(&mut mock, ..2048)
.await
.unwrap_err()
.kind(),
io::ErrorKind::InvalidData
);
}
#[tokio::test]
async fn read_smaller_than_allowed_fail() {
let payload = &[0x00, 0x01, 0x02];
let mut mock = Builder::new()
.read(&produce_packet_bytes(payload).await[0..8]) .build();
assert_eq!(
BytesReader::new(&mut mock, 1024..2048)
.await
.unwrap_err()
.kind(),
io::ErrorKind::InvalidData
);
}
#[cfg(feature = "async")]
#[tokio::test]
async fn read_trailer_immediately() {
use crate::nar::wire::PadPar;
let mut mock = Builder::new()
.read(&[0; 8])
.read(&PadPar::PATTERN[8..])
.build();
BytesReader::<_, PadPar>::new_internal(&mut mock, ..)
.await
.unwrap();
}
#[cfg(feature = "async")]
#[tokio::test]
async fn read_exact_trailer() {
use crate::nar::wire::PadPar;
let mut mock = Builder::new()
.read(&16u64.to_le_bytes())
.read(&[0x55; 16])
.read(&PadPar::PATTERN[8..])
.build();
let mut reader = BytesReader::<_, PadPar>::new_internal(&mut mock, ..)
.await
.unwrap();
let mut buf = [0; 16];
reader.read_exact(&mut buf).await.unwrap();
assert_eq!(buf, [0x55; 16]);
}
#[tokio::test]
async fn read_fail_if_nonzero_padding() {
let payload = &[0x00, 0x01, 0x02];
let mut packet_bytes = produce_packet_bytes(payload).await;
packet_bytes[12] = 0xff;
let mut mock = Builder::new().read(&packet_bytes).build(); let mut r = BytesReader::new(&mut mock, ..MAX_LEN).await.unwrap();
let mut buf = Vec::new();
r.read_to_end(&mut buf).await.expect_err("must fail");
}
#[tokio::test]
async fn read_9b_eof_during_size() {
let payload = &hex!("FF0102030405060708");
let mut mock = Builder::new()
.read(&produce_packet_bytes(payload).await[..4])
.build();
assert_eq!(
BytesReader::new(&mut mock, ..MAX_LEN)
.await
.expect_err("must fail")
.kind(),
io::ErrorKind::UnexpectedEof
);
}
#[tokio::test]
async fn read_9b_eof_during_payload() {
let payload = &hex!("FF0102030405060708");
let mut mock = Builder::new()
.read(&produce_packet_bytes(payload).await[..8 + 4])
.build();
let mut r = BytesReader::new(&mut mock, ..MAX_LEN).await.unwrap();
let mut buf = [0; 9];
r.read_exact(&mut buf[..4]).await.expect("must succeed");
assert_eq!(
r.read_exact(&mut buf[4..=4])
.await
.expect_err("must fail")
.kind(),
std::io::ErrorKind::UnexpectedEof
);
}
#[rstest]
#[case::before_padding(8 + 9)]
#[case::during_padding(8 + 9 + 2)]
#[case::after_padding(8 + 9 + padding_len(9) as usize - 1)]
#[tokio::test]
async fn read_9b_eof_after_payload(#[case] offset: usize) {
let payload = &hex!("FF0102030405060708");
let mut mock = Builder::new()
.read(&produce_packet_bytes(payload).await[..offset])
.build();
let mut r = BytesReader::new(&mut mock, ..MAX_LEN).await.unwrap();
assert_eq!(r.read_exact(&mut [0; 8]).await.unwrap(), 8);
assert_eq!(
r.read_exact(&mut [0]).await.unwrap_err().kind(),
std::io::ErrorKind::UnexpectedEof
);
}
#[rstest]
#[case::during_size(4)]
#[case::before_payload(8)]
#[case::during_payload(8 + 4)]
#[case::before_padding(8 + 4)]
#[case::during_padding(8 + 9 + 2)]
#[tokio::test]
async fn propagate_error_from_reader(#[case] offset: usize) {
let payload = &hex!("FF0102030405060708");
let mut mock = Builder::new()
.read(&produce_packet_bytes(payload).await[..offset])
.read_error(std::io::Error::new(std::io::ErrorKind::Other, "foo"))
.build();
let err: io::Error = async {
let mut r = BytesReader::new(&mut mock, ..MAX_LEN).await?;
let mut buf = Vec::new();
r.read_to_end(&mut buf).await?;
Ok(())
}
.await
.expect_err("must fail");
assert_eq!(
err.kind(),
std::io::ErrorKind::Other,
"error kind must match"
);
assert_eq!(
err.into_inner().unwrap().to_string(),
"foo",
"error payload must contain foo"
);
}
#[rstest]
#[case::during_size(4)]
#[case::before_payload(8)]
#[case::during_payload(8 + 4)]
#[case::before_padding(8 + 4)]
#[case::during_padding(8 + 9 + 2)]
#[tokio::test]
async fn propagate_error_from_reader_buffered(#[case] offset: usize) {
let payload = &hex!("FF0102030405060708");
let mock = Builder::new()
.read(&produce_packet_bytes(payload).await[..offset])
.read_error(std::io::Error::new(std::io::ErrorKind::Other, "foo"))
.build();
let mut mock = BufReader::new(mock);
let err: io::Error = async {
let mut r = BytesReader::new(&mut mock, ..MAX_LEN).await?;
let mut buf = Vec::new();
tokio::io::copy_buf(&mut r, &mut buf).await?;
Ok(())
}
.await
.expect_err("must fail");
assert_eq!(
err.kind(),
std::io::ErrorKind::Other,
"error kind must match"
);
assert_eq!(
err.into_inner().unwrap().to_string(),
"foo",
"error payload must contain foo"
);
}
#[tokio::test]
async fn no_error_after_eof() {
let payload = &hex!("FF0102030405060708");
let mut mock = Builder::new()
.read(&produce_packet_bytes(payload).await)
.read_error(std::io::Error::new(std::io::ErrorKind::Other, "foo"))
.build();
let mut r = BytesReader::new(&mut mock, ..MAX_LEN).await.unwrap();
let mut buf = Vec::new();
r.read_to_end(&mut buf).await.expect("must succeed");
assert_eq!(buf.as_slice(), payload);
}
#[tokio::test]
async fn no_error_after_eof_buffered() {
let payload = &hex!("FF0102030405060708");
let mock = Builder::new()
.read(&produce_packet_bytes(payload).await)
.read_error(std::io::Error::new(std::io::ErrorKind::Other, "foo"))
.build();
let mut mock = BufReader::new(mock);
let mut r = BytesReader::new(&mut mock, ..MAX_LEN).await.unwrap();
let mut buf = Vec::new();
tokio::io::copy_buf(&mut r, &mut buf)
.await
.expect("must succeed");
assert_eq!(buf.as_slice(), payload);
}
#[rstest]
#[case::beginning(0)]
#[case::before_payload(8)]
#[case::during_payload(8 + 4)]
#[case::before_padding(8 + 4)]
#[case::during_padding(8 + 9 + 2)]
#[tokio::test]
async fn read_payload_correct_pending(#[case] offset: usize) {
let payload = &hex!("FF0102030405060708");
let mut mock = Builder::new()
.read(&produce_packet_bytes(payload).await[..offset])
.wait(Duration::from_nanos(0))
.read(&produce_packet_bytes(payload).await[offset..])
.build();
let mut r = BytesReader::new(&mut mock, ..=LARGE_PAYLOAD.len() as u64)
.await
.unwrap();
let mut buf = Vec::new();
r.read_to_end(&mut buf).await.expect("must succeed");
assert_eq!(payload, &buf[..]);
}
}