use crate::error::Error;
use crate::payload_util::require_at_least;
use alloc::vec::Vec;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileTransfer {
pub file_type: u8,
pub total_size: u32,
pub offset: u32,
pub fragment: Vec<u8>,
}
impl FileTransfer {
pub fn encode(&self) -> Result<Vec<u8>, Error> {
if self.fragment.len() > u16::MAX as usize {
return Err(Error::MalformedPayload {
code: 0x7C,
reason: "FILETRANSFER fragment > 65535 bytes",
});
}
let mut out = Vec::with_capacity(11 + self.fragment.len());
out.push(self.file_type);
out.extend_from_slice(&self.total_size.to_le_bytes());
out.extend_from_slice(&self.offset.to_le_bytes());
out.extend_from_slice(&(self.fragment.len() as u16).to_le_bytes());
out.extend_from_slice(&self.fragment);
Ok(out)
}
pub fn decode(data: &[u8]) -> Result<Self, Error> {
require_at_least(data, 11, 0x7C)?;
let total_size = u32::from_le_bytes([data[1], data[2], data[3], data[4]]);
let offset = u32::from_le_bytes([data[5], data[6], data[7], data[8]]);
let frag_len = u16::from_le_bytes([data[9], data[10]]) as usize;
if data.len() != 11 + frag_len {
return Err(Error::MalformedPayload {
code: 0x7C,
reason: "FILETRANSFER fragment length disagrees with payload",
});
}
Ok(Self {
file_type: data[0],
total_size,
offset,
fragment: data[11..11 + frag_len].to_vec(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn roundtrip() {
let body = FileTransfer {
file_type: 0x07,
total_size: 0x0000_0100,
offset: 0x0000_0040,
fragment: alloc::vec![0xAA, 0xBB, 0xCC],
};
let bytes = body.encode().unwrap();
assert_eq!(
bytes,
[
0x07, 0x00, 0x01, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x03, 0x00, 0xAA, 0xBB, 0xCC
]
);
assert_eq!(FileTransfer::decode(&bytes).unwrap(), body);
}
#[test]
fn decode_rejects_short_header() {
assert!(matches!(
FileTransfer::decode(&[0; 10]),
Err(Error::PayloadTooShort { code: 0x7C, .. })
));
}
#[test]
fn decode_rejects_length_mismatch() {
let bad = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0xAA, 0xBB,
];
assert!(matches!(
FileTransfer::decode(&bad),
Err(Error::MalformedPayload { code: 0x7C, .. })
));
}
}