use crate::errors::ProtocolError;
use crate::proto::types;
pub(in crate::proto) fn run<'a, O>(
input: &'a [u8],
decode: impl FnOnce(&mut Cursor<'a>) -> Result<O, ProtocolError>,
) -> Result<O, ProtocolError> {
let mut cur = Cursor::new(input);
let value = decode(&mut cur)?;
cur.finish()?;
Ok(value)
}
pub(in crate::proto) struct Cursor<'a> {
input: &'a [u8],
pos: usize,
}
impl<'a> Cursor<'a> {
pub(in crate::proto) fn new(input: &'a [u8]) -> Self {
Self { input, pos: 0 }
}
pub(in crate::proto) fn position(&self) -> usize {
self.pos
}
fn remaining(&self) -> usize {
self.input.len() - self.pos
}
fn end_of_input(&self, needed: usize) -> ProtocolError {
ProtocolError::InvalidValue {
position: self.input.len(),
message: format!(
"unexpected end of input: need {needed} bytes, have {remaining}",
remaining = self.remaining(),
),
}
}
fn read_bytes(&mut self, len: usize) -> Result<&'a [u8], ProtocolError> {
if len > self.remaining() {
return Err(self.end_of_input(len));
}
let start = self.pos;
self.pos += len;
Ok(&self.input[start..self.pos])
}
pub(in crate::proto) fn read_u8(&mut self) -> Result<u8, ProtocolError> {
let bytes = self.read_bytes(1)?;
Ok(bytes[0])
}
pub(in crate::proto) fn read_i16(&mut self) -> Result<i16, ProtocolError> {
let bytes = self.read_bytes(2)?;
Ok(i16::from_be_bytes([bytes[0], bytes[1]]))
}
pub(in crate::proto) fn read_i32(&mut self) -> Result<i32, ProtocolError> {
let bytes = self.read_bytes(4)?;
Ok(i32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
}
pub(in crate::proto) fn read_cstring(&mut self) -> Result<String, ProtocolError> {
let start = self.pos;
let Some(len) = self.input[self.pos..].iter().position(|&b| b == 0) else {
return Err(ProtocolError::InvalidValue {
position: self.input.len(),
message: "unterminated C string".to_owned(),
});
};
let value = std::str::from_utf8(&self.input[start..start + len]).map_err(|e| {
ProtocolError::InvalidValue {
position: start,
message: format!("invalid UTF-8: {e}"),
}
})?;
self.pos = start + len + 1;
Ok(value.to_owned())
}
pub(in crate::proto) fn read_nullable_bytes(
&mut self,
) -> Result<Option<Vec<u8>>, ProtocolError> {
let start = self.pos;
let len = self.read_i32()?;
if len < 0 {
return Ok(None);
}
let len = len as usize;
let remaining = self.remaining();
if len > remaining {
return Err(ProtocolError::InvalidValue {
position: start,
message: format!(
"declared length {len} exceeds remaining input ({remaining} bytes)"
),
});
}
Ok(Some(self.read_bytes(len)?.to_vec()))
}
pub(in crate::proto) fn read_count_prefixed<O>(
&mut self,
mut item: impl FnMut(&mut Self) -> Result<O, ProtocolError>,
) -> Result<Vec<O>, ProtocolError> {
let start = self.pos;
let raw = self.read_i16()?;
if raw < 0 {
return Err(ProtocolError::InvalidValue {
position: start,
message: format!("negative item count: {raw}"),
});
}
let count = raw as usize;
let remaining = self.remaining();
if count > remaining {
return Err(ProtocolError::InvalidValue {
position: start,
message: format!(
"declared count {count} exceeds remaining input ({remaining} bytes)"
),
});
}
let mut items = Vec::with_capacity(count);
for _ in 0..count {
items.push(item(self)?);
}
Ok(items)
}
pub(in crate::proto) fn read_format_code(
&mut self,
) -> Result<types::FormatCode, ProtocolError> {
let start = self.pos;
let raw = self.read_i16()?;
match raw {
0 => Ok(types::FormatCode::Text),
1 => Ok(types::FormatCode::Binary),
_ => Err(ProtocolError::InvalidValue {
position: start,
message: format!("invalid format code: {raw}"),
}),
}
}
pub(in crate::proto) fn take_rest(&mut self) -> &'a [u8] {
let rest = &self.input[self.pos..];
self.pos = self.input.len();
rest
}
fn finish(&self) -> Result<(), ProtocolError> {
let remaining = self.remaining();
if remaining > 0 {
return Err(ProtocolError::InvalidValue {
position: self.pos,
message: format!("expected end of input ({remaining} trailing bytes)"),
});
}
Ok(())
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
#[test]
fn read_u8_single_byte() {
assert_eq!(run(&[7], Cursor::read_u8).unwrap(), 7);
}
#[test]
fn read_u8_empty_input() {
let err = run(&[], Cursor::read_u8).unwrap_err();
assert!(matches!(err, ProtocolError::InvalidValue { .. }));
}
#[test]
fn read_i16_big_endian() {
assert_eq!(run(&7i16.to_be_bytes(), Cursor::read_i16).unwrap(), 7);
assert_eq!(run(&(-2i16).to_be_bytes(), Cursor::read_i16).unwrap(), -2);
}
#[test]
fn read_i16_truncated() {
let err = run(&[0], Cursor::read_i16).unwrap_err();
assert!(matches!(err, ProtocolError::InvalidValue { .. }));
}
#[test]
fn read_i32_big_endian() {
assert_eq!(run(&42i32.to_be_bytes(), Cursor::read_i32).unwrap(), 42);
assert_eq!(run(&(-1i32).to_be_bytes(), Cursor::read_i32).unwrap(), -1);
}
#[test]
fn read_i32_truncated() {
let err = run(&[0, 0], Cursor::read_i32).unwrap_err();
assert!(matches!(err, ProtocolError::InvalidValue { .. }));
}
#[test]
fn read_cstring_sequence() {
let result = run(b"hello\0world\0", |cur| {
Ok((cur.read_cstring()?, cur.read_cstring()?))
})
.unwrap();
assert_eq!(result, ("hello".to_owned(), "world".to_owned()));
}
#[test]
fn read_cstring_empty() {
assert_eq!(run(b"\0", Cursor::read_cstring).unwrap(), String::new());
}
#[test]
fn read_cstring_unterminated() {
let err = run(b"hello", Cursor::read_cstring).unwrap_err();
assert!(matches!(err, ProtocolError::InvalidValue { .. }));
}
#[test]
fn read_cstring_invalid_utf8() {
let err = run(&[0xFF, 0xFE, 0x00], Cursor::read_cstring).unwrap_err();
assert!(
matches!(err, ProtocolError::InvalidValue { .. })
&& err.to_string().contains("invalid UTF-8"),
"expected an invalid-UTF-8 error, got: {err}"
);
}
#[test]
fn read_count_prefixed_items() {
let mut buf = Vec::new();
buf.extend_from_slice(&3i16.to_be_bytes());
buf.extend_from_slice(&10i32.to_be_bytes());
buf.extend_from_slice(&20i32.to_be_bytes());
buf.extend_from_slice(&30i32.to_be_bytes());
let items = run(&buf, |cur| cur.read_count_prefixed(Cursor::read_i32)).unwrap();
assert_eq!(items, vec![10, 20, 30]);
}
#[test]
fn read_count_prefixed_negative_count() {
let err = run(&[0xFF, 0xFF], |cur| {
cur.read_count_prefixed(Cursor::read_i32)
})
.unwrap_err();
assert!(
matches!(err, ProtocolError::InvalidValue { .. })
&& err.to_string().contains("negative item count"),
"expected a negative-count guard error, got: {err}"
);
}
#[test]
fn read_count_prefixed_count_exceeds_remaining() {
let buf = i16::MAX.to_be_bytes();
let err = run(&buf, |cur| cur.read_count_prefixed(Cursor::read_i32)).unwrap_err();
assert!(
matches!(err, ProtocolError::InvalidValue { .. })
&& err.to_string().contains("exceeds"),
"expected a declared-count guard error, got: {err}"
);
}
#[test]
fn read_count_prefixed_guard_error_position_is_count_start() {
let err = run(&[0x00, 0xFF, 0xFF], |cur| {
cur.read_u8()?;
cur.read_count_prefixed(Cursor::read_i32)
})
.unwrap_err();
assert!(
matches!(err, ProtocolError::InvalidValue { position: 1, .. }),
"expected the error at byte 1, got: {err}"
);
}
#[test]
fn read_nullable_bytes_present() {
let mut buf = Vec::new();
buf.extend_from_slice(&5i32.to_be_bytes());
buf.extend_from_slice(b"hello");
let value = run(&buf, Cursor::read_nullable_bytes).unwrap();
assert_eq!(value, Some(b"hello".to_vec()));
}
#[test]
fn read_nullable_bytes_null() {
let value = run(&(-1i32).to_be_bytes(), Cursor::read_nullable_bytes).unwrap();
assert_eq!(value, None);
}
#[test]
fn read_nullable_bytes_any_negative_is_null() {
let value = run(&(-5i32).to_be_bytes(), Cursor::read_nullable_bytes).unwrap();
assert_eq!(value, None);
}
#[test]
fn read_nullable_bytes_length_exceeds_remaining() {
let mut buf = Vec::new();
buf.extend_from_slice(&i32::MAX.to_be_bytes());
buf.extend_from_slice(b"hi");
let err = run(&buf, Cursor::read_nullable_bytes).unwrap_err();
assert!(
matches!(err, ProtocolError::InvalidValue { .. })
&& err.to_string().contains("exceeds"),
"expected a declared-length guard error, got: {err}"
);
}
#[test]
fn read_nullable_bytes_truncated_value() {
let mut buf = Vec::new();
buf.extend_from_slice(&5i32.to_be_bytes());
buf.extend_from_slice(b"abc");
let err = run(&buf, Cursor::read_nullable_bytes).unwrap_err();
assert!(
matches!(err, ProtocolError::InvalidValue { .. })
&& err.to_string().contains("exceeds"),
"expected a declared-length guard error, got: {err}"
);
}
#[test]
fn read_format_code_valid() {
assert_eq!(
run(&0i16.to_be_bytes(), Cursor::read_format_code).unwrap(),
types::FormatCode::Text
);
assert_eq!(
run(&1i16.to_be_bytes(), Cursor::read_format_code).unwrap(),
types::FormatCode::Binary
);
}
#[test]
fn read_format_code_invalid() {
let err = run(&99i16.to_be_bytes(), Cursor::read_format_code).unwrap_err();
assert!(
matches!(err, ProtocolError::InvalidValue { .. })
&& err.to_string().contains("invalid format code"),
"expected a format-code error, got: {err}"
);
}
#[test]
fn take_rest_returns_remaining() {
let rest = run(&[0, 1, b'x'], |cur| {
cur.read_i16()?;
Ok(cur.take_rest().to_vec())
})
.unwrap();
assert_eq!(rest, b"x".to_vec());
}
#[test]
fn take_rest_empty() {
let rest = run(b"", |cur| Ok(cur.take_rest().to_vec())).unwrap();
assert_eq!(rest, Vec::<u8>::new());
}
#[test]
fn trailing_bytes_rejected() {
let err = run(&[0, 0, 0, 1, 0xFF], Cursor::read_i32).unwrap_err();
assert!(matches!(err, ProtocolError::InvalidValue { .. }));
}
#[test]
fn exact_consumption_succeeds() {
assert_eq!(run(&1i32.to_be_bytes(), Cursor::read_i32).unwrap(), 1);
}
}