use crate::checksum::Checksum;
use crate::error::{Error, Result};
use crate::objtype::ObjectType;
use crate::varint;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SizeEntry {
pub checksum: Checksum,
pub compressed: u64,
pub unpacked: u64,
pub objtype: Option<ObjectType>,
}
pub fn pack_entry(entry: &SizeEntry, out: &mut Vec<u8>) {
out.extend_from_slice(entry.checksum.as_bytes());
varint::encode(entry.compressed, out);
varint::encode(entry.unpacked, out);
if let Some(ty) = entry.objtype {
out.push(ty.as_u32() as u8);
}
}
pub fn unpack_entry(buf: &[u8]) -> Result<SizeEntry> {
if buf.len() < 32 {
return Err(Error::InvalidSizeEntry("entry is shorter than a checksum"));
}
let checksum = Checksum::from_ay(&buf[..32])?;
let mut pos = 32;
let (compressed, n) = varint::decode(&buf[pos..])?;
pos += n;
let (unpacked, m) = varint::decode(&buf[pos..])?;
pos += m;
let objtype = match buf.len() - pos {
0 => None,
1 => match ObjectType::from_u32(u32::from(buf[pos]))? {
ObjectType::ComposefsImage => {
return Err(Error::InvalidObjectType(u32::from(buf[pos])));
}
ty => Some(ty),
},
_ => return Err(Error::InvalidSizeEntry("entry has trailing bytes")),
};
Ok(SizeEntry {
checksum,
compressed,
unpacked,
objtype,
})
}
pub fn pack_sizes(mut entries: Vec<SizeEntry>) -> Vec<Vec<u8>> {
entries.sort_by(|a, b| a.checksum.cmp(&b.checksum));
entries
.iter()
.map(|e| {
let mut buf = Vec::with_capacity(56);
pack_entry(e, &mut buf);
buf
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn csum(byte: u8) -> Checksum {
Checksum::from_bytes([byte; 32])
}
#[test]
fn entry_round_trips_with_objtype() {
let entry = SizeEntry {
checksum: csum(0xab),
compressed: 300,
unpacked: 16384,
objtype: Some(ObjectType::File),
};
let mut buf = Vec::new();
pack_entry(&entry, &mut buf);
assert_eq!(buf.len(), 32 + 2 + 3 + 1);
assert_eq!(unpack_entry(&buf).unwrap(), entry);
}
#[test]
fn tolerates_absent_objtype_byte() {
let entry = SizeEntry {
checksum: csum(0x01),
compressed: 1,
unpacked: 2,
objtype: None,
};
let mut buf = Vec::new();
pack_entry(&entry, &mut buf);
assert_eq!(buf.len(), 32 + 1 + 1);
assert_eq!(unpack_entry(&buf).unwrap().objtype, None);
}
#[test]
fn rejects_trailing_garbage() {
let mut buf = Vec::new();
pack_entry(
&SizeEntry {
checksum: csum(0x02),
compressed: 5,
unpacked: 6,
objtype: Some(ObjectType::File),
},
&mut buf,
);
buf.push(0x00);
assert!(matches!(
unpack_entry(&buf),
Err(Error::InvalidSizeEntry("entry has trailing bytes"))
));
}
#[test]
fn rejects_the_composefs_image_type() {
let mut buf = Vec::new();
pack_entry(
&SizeEntry {
checksum: csum(0x03),
compressed: 5,
unpacked: 6,
objtype: None,
},
&mut buf,
);
buf.push(100);
assert!(matches!(
unpack_entry(&buf),
Err(Error::InvalidObjectType(100))
));
}
#[test]
fn pack_sizes_sorts_by_checksum() {
let entries = vec![
SizeEntry {
checksum: csum(0xff),
compressed: 1,
unpacked: 1,
objtype: Some(ObjectType::File),
},
SizeEntry {
checksum: csum(0x00),
compressed: 1,
unpacked: 1,
objtype: Some(ObjectType::File),
},
];
let packed = pack_sizes(entries);
assert_eq!(&packed[0][..32], &[0x00u8; 32]);
assert_eq!(&packed[1][..32], &[0xffu8; 32]);
}
}