use crate::packet::{self, HEADER_LEN};
#[derive(Debug, Clone)]
pub struct RawPacket {
pub recovery_set_id: [u8; 16],
pub packet_type: [u8; 16],
pub body: Vec<u8>,
}
pub fn read_packets(data: &[u8]) -> Vec<RawPacket> {
read_packets_with_offsets(data)
.into_iter()
.map(|(p, _)| p)
.collect()
}
pub fn read_packets_with_offsets(data: &[u8]) -> Vec<(RawPacket, usize)> {
let mut packets = Vec::new();
let mut pos = 0usize;
while pos + 8 <= data.len() {
if &data[pos..pos + 8] != packet::MAGIC.as_slice() {
pos += 1;
continue;
}
match try_parse_one(&data[pos..]) {
Some((raw, consumed)) => {
packets.push((raw, pos));
pos += consumed;
}
None => pos += 1,
}
}
packets
}
fn try_parse_one(buf: &[u8]) -> Option<(RawPacket, usize)> {
if buf.len() < HEADER_LEN {
return None;
}
let total_length = u64::from_le_bytes(buf[8..16].try_into().ok()?);
if total_length < HEADER_LEN as u64 || !total_length.is_multiple_of(4) {
return None;
}
let total_length = usize::try_from(total_length).ok()?;
if total_length > buf.len() {
return None;
}
let stored_hash = &buf[16..32];
let computed_hash = packet::md5(&buf[32..total_length]);
if stored_hash != computed_hash {
return None;
}
let mut recovery_set_id = [0u8; 16];
recovery_set_id.copy_from_slice(&buf[32..48]);
let mut packet_type = [0u8; 16];
packet_type.copy_from_slice(&buf[48..64]);
let body = buf[HEADER_LEN..total_length].to_vec();
Some((
RawPacket {
recovery_set_id,
packet_type,
body,
},
total_length,
))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trips_a_single_packet() {
let rsid = [0x11u8; 16];
let body = b"abcd1234".to_vec(); let bytes = packet::serialize_packet(&rsid, &packet::TYPE_CREATOR, &body);
let packets = read_packets(&bytes);
assert_eq!(packets.len(), 1);
assert_eq!(packets[0].recovery_set_id, rsid);
assert_eq!(packets[0].packet_type, packet::TYPE_CREATOR);
assert_eq!(packets[0].body, body);
}
#[test]
fn round_trips_multiple_concatenated_packets() {
let rsid = [0x22u8; 16];
let mut bytes = Vec::new();
bytes.extend(packet::serialize_packet(&rsid, &packet::TYPE_MAIN, b"aaaa"));
bytes.extend(packet::serialize_packet(
&rsid,
&packet::TYPE_CREATOR,
b"bbbbbbbb",
));
bytes.extend(packet::serialize_packet(&rsid, &packet::TYPE_IFSC, b"cccc"));
let packets = read_packets(&bytes);
assert_eq!(packets.len(), 3);
assert_eq!(packets[0].packet_type, packet::TYPE_MAIN);
assert_eq!(packets[1].packet_type, packet::TYPE_CREATOR);
assert_eq!(packets[2].packet_type, packet::TYPE_IFSC);
}
#[test]
fn rejects_a_packet_with_a_corrupted_hash_but_keeps_scanning() {
let rsid = [0x33u8; 16];
let mut bytes = packet::serialize_packet(&rsid, &packet::TYPE_CREATOR, b"aaaa");
bytes[20] ^= 0xFF;
bytes.extend(packet::serialize_packet(&rsid, &packet::TYPE_IFSC, b"bbbb"));
let packets = read_packets(&bytes);
assert_eq!(packets.len(), 1);
assert_eq!(packets[0].packet_type, packet::TYPE_IFSC);
}
#[test]
fn a_forged_huge_length_field_is_rejected_without_panicking_or_over_reading() {
let rsid = [0x44u8; 16];
let mut bytes = packet::serialize_packet(&rsid, &packet::TYPE_CREATOR, b"aaaa");
bytes[8..16].copy_from_slice(&(u64::MAX - 3).to_le_bytes());
let packets = read_packets(&bytes);
assert!(packets.is_empty());
}
#[test]
fn truncated_input_does_not_panic() {
let rsid = [0x55u8; 16];
let full = packet::serialize_packet(&rsid, &packet::TYPE_MAIN, b"aaaaaaaa");
for cut in 0..full.len() {
let _ = read_packets(&full[..cut]);
}
}
#[test]
fn empty_and_garbage_input_yield_no_packets() {
assert!(read_packets(&[]).is_empty());
assert!(read_packets(b"not a par2 file at all").is_empty());
}
mod props {
use super::*;
use proptest::prelude::*;
proptest! {
#![proptest_config(ProptestConfig::with_cases(1000))]
#[test]
fn arbitrary_bytes_never_panic(data in prop::collection::vec(any::<u8>(), 0..4096)) {
let _ = read_packets(&data);
}
#[test]
fn a_valid_packet_survives_surrounding_noise(
noise_before in prop::collection::vec(any::<u8>(), 0..96),
noise_after in prop::collection::vec(any::<u8>(), 0..96),
mut body in prop::collection::vec(any::<u8>(), 0..64),
) {
while !body.len().is_multiple_of(4) {
body.push(0);
}
let rsid = [0x77u8; 16];
let mut bytes = noise_before;
bytes.extend(packet::serialize_packet(&rsid, &packet::TYPE_CREATOR, &body));
bytes.extend(noise_after);
let packets = read_packets(&bytes);
prop_assert!(packets
.iter()
.any(|p| p.packet_type == packet::TYPE_CREATOR
&& p.recovery_set_id == rsid
&& p.body == body));
}
}
}
}