use crate::data;
use gix_error::{Result, bail};
pub(crate) const N32_SIZE: usize = std::mem::size_of::<u32>();
pub const SIZE: usize = b"PACK".len() + N32_SIZE * 2;
pub fn decode(data: &[u8; SIZE]) -> Result<(data::Version, u32)> {
let mut ofs = 0;
if &data[ofs..ofs + b"PACK".len()] != b"PACK" {
bail!(gix_error::corruption("Pack data type not recognized"));
}
ofs += N32_SIZE;
let kind = match crate::read_u32(&data[ofs..ofs + N32_SIZE]) {
2 => data::Version::V2,
3 => data::Version::V3,
v => {
bail!("Unsupported pack version: {v}".unsupported());
}
};
ofs += N32_SIZE;
let num_objects = crate::read_u32(&data[ofs..ofs + N32_SIZE]);
Ok((kind, num_objects))
}
pub fn encode(version: data::Version, num_objects: u32) -> [u8; SIZE] {
use crate::data::Version::*;
let mut buf = [0u8; SIZE];
buf[..4].copy_from_slice(b"PACK");
buf[4..8].copy_from_slice(
&match version {
V2 => 2u32,
V3 => 3,
}
.to_be_bytes()[..],
);
buf[8..].copy_from_slice(&num_objects.to_be_bytes()[..]);
buf
}