pub(crate) const HEADER: usize = 16;
pub(crate) const MAX_MESSAGE: usize = 1024 * 1024;
const NONE: u32 = 1;
const BOOL: u32 = 2;
const ID: u32 = 3;
const INT: u32 = 4;
const LONG: u32 = 5;
const STRING: u32 = 8;
const STRUCT: u32 = 14;
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Bad(pub(crate) &'static str);
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Pod<'a> {
None,
Bool(bool),
Id(u32),
Int(i32),
Long(i64),
String(&'a [u8]),
Struct(Fields<'a>),
Other(u32),
}
pub(crate) type Entry<'a> = (&'a [u8], &'a [u8]);
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Fields<'a>(&'a [u8]);
pub(crate) fn parse(bytes: &[u8]) -> Result<(Pod<'_>, usize), Bad> {
let (Some(size), Some(kind)) = (word(bytes, 0), word(bytes, 4)) else {
return Err(Bad("a POD cut short in its header"));
};
let size = size as usize;
let Some(body) = bytes.get(8..).and_then(|rest| rest.get(..size)) else {
return Err(Bad("a POD longer than its message"));
};
let fixed = |len| body.get(..len).ok_or(Bad("a POD too short for its type"));
let pod = match kind {
NONE => Pod::None,
BOOL => Pod::Bool(u32::from_ne_bytes(array(fixed(4)?)) != 0),
ID => Pod::Id(u32::from_ne_bytes(array(fixed(4)?))),
INT => Pod::Int(i32::from_ne_bytes(array(fixed(4)?))),
LONG => Pod::Long(i64::from_ne_bytes(array(fixed(8)?))),
STRING => match body.split_last() {
Some((0, text)) => Pod::String(text),
_ => return Err(Bad("a string with no closing NUL")),
},
STRUCT => Pod::Struct(Fields(body)),
other => Pod::Other(other),
};
let taken = (8 + size).next_multiple_of(8).min(bytes.len());
Ok((pod, taken))
}
impl<'a> Fields<'a> {
pub(crate) fn of(bytes: &'a [u8]) -> Result<Fields<'a>, Bad> {
match parse(bytes)?.0 {
Pod::Struct(fields) => Ok(fields),
_ => Err(Bad("a message that isn't a struct")),
}
}
pub(crate) fn next(&mut self) -> Result<Pod<'a>, Bad> {
if self.0.is_empty() {
return Err(Bad("a struct with a field missing"));
}
let (pod, taken) = parse(self.0)?;
self.0 = &self.0[taken..];
Ok(pod)
}
pub(crate) fn uint(&mut self) -> Result<u32, Bad> {
match self.next()? {
Pod::Int(value) => Ok(value.cast_unsigned()),
_ => Err(Bad("a field that isn't an Int")),
}
}
pub(crate) fn int(&mut self) -> Result<i32, Bad> {
match self.next()? {
Pod::Int(value) => Ok(value),
_ => Err(Bad("a field that isn't an Int")),
}
}
pub(crate) fn long(&mut self) -> Result<i64, Bad> {
match self.next()? {
Pod::Long(value) => Ok(value),
_ => Err(Bad("a field that isn't a Long")),
}
}
pub(crate) fn text(&mut self) -> Result<Option<&'a [u8]>, Bad> {
match self.next()? {
Pod::String(text) => Ok(Some(text)),
Pod::None => Ok(None),
_ => Err(Bad("a field that isn't a String")),
}
}
pub(crate) fn fields(&mut self) -> Result<Fields<'a>, Bad> {
match self.next()? {
Pod::Struct(fields) => Ok(fields),
_ => Err(Bad("a field that isn't a Struct")),
}
}
pub(crate) fn dict(&mut self) -> Result<Vec<Entry<'a>>, Bad> {
let mut fields = self.fields()?;
let count = fields.uint()?;
let mut entries = Vec::new();
for _ in 0..count {
let key = fields.text()?.ok_or(Bad("a dictionary key that is None"))?;
if let Some(value) = fields.text()? {
entries.push((key, value));
}
}
Ok(entries)
}
}
fn word(bytes: &[u8], at: usize) -> Option<u32> {
let bytes = bytes.get(at..at + 4)?;
Some(u32::from_ne_bytes(array(bytes)))
}
fn array<const N: usize>(bytes: &[u8]) -> [u8; N] {
bytes.try_into().expect("invariant: sliced to N bytes")
}
#[derive(Default)]
pub(crate) struct Builder(Vec<u8>);
impl Builder {
pub(crate) fn int(mut self, value: i32) -> Builder {
push(&mut self.0, INT, &value.to_ne_bytes());
self
}
pub(crate) fn uint(self, value: u32) -> Builder {
self.int(value.cast_signed())
}
pub(crate) fn string(mut self, text: &str) -> Builder {
let mut body = text.as_bytes().to_vec();
body.push(0);
push(&mut self.0, STRING, &body);
self
}
pub(crate) fn structure(mut self, fields: Builder) -> Builder {
push(&mut self.0, STRUCT, &fields.0);
self
}
pub(crate) fn finish(self) -> Vec<u8> {
let mut pod = Vec::new();
push(&mut pod, STRUCT, &self.0);
pod
}
}
fn push(out: &mut Vec<u8>, kind: u32, body: &[u8]) {
let size = u32::try_from(body.len()).expect("invariant: PODs sent are small");
out.extend_from_slice(&size.to_ne_bytes());
out.extend_from_slice(&kind.to_ne_bytes());
out.extend_from_slice(body);
out.resize(out.len().next_multiple_of(8), 0);
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Header {
pub(crate) id: u32,
pub(crate) opcode: u8,
pub(crate) size: usize,
}
pub(crate) fn header(bytes: &[u8]) -> Option<Header> {
let (id, packed) = (word(bytes, 0)?, word(bytes, 4)?);
word(bytes, 12)?;
Some(Header {
id,
opcode: (packed >> 24) as u8,
size: (packed & 0x00ff_ffff) as usize,
})
}
pub(crate) fn message(out: &mut Vec<u8>, id: u32, opcode: u8, seq: u32, body: &[u8]) {
let size = u32::try_from(body.len()).expect("invariant: messages sent are small");
for word in [id, u32::from(opcode) << 24 | size, seq, 0] {
out.extend_from_slice(&word.to_ne_bytes());
}
out.extend_from_slice(body);
}