use crate::encoding::Reader; use crate::error::{Error, Result}; use crate::object::id::{Id, hash_bytes}; #[derive(Clone)] pub struct IndexEntry { pub id: Id, pub offset: u64, } #[derive(Clone)] pub struct Index { pub entries: Vec, pub pack_trailer: [u8; 32], } impl Index { pub fn find(&self, id: Id) -> Option { self.entries .binary_search_by(|entry| entry.id.0.cmp(&id.0)) .ok() } } pub fn decode(data: &[u8], expected_pack_trailer: Option<[u8; 32]>) -> Result { const BASE: usize = 1032 + 64; if data.len() < BASE { return Err(Error::new("index is truncated")); } if &data[..4] != b"VIX1" { return Err(Error::new("invalid index magic")); } let mut reader = Reader::new(data); reader.raw(4)?; let version = reader.u32le()?; if version != 1 { return Err(Error::new("unsupported index version")); } let mut fanout = [0u32; 256]; let mut previous = 0u32; for slot in &mut fanout { let value = reader.u32le()?; if value < previous { return Err(Error::new("index fanout is not sorted")); } previous = value; *slot = value; } let body = data.len() - BASE; if !body.is_multiple_of(40) { return Err(Error::new("index body has an invalid length")); } let count = body / 40; if count != fanout[255] as usize { return Err(Error::new("index fanout does not match entry count")); } let ids_start = 1032; let offsets_start = ids_start + count * 32; let trailer_offset = data.len() - 32; let mut entries: Vec = Vec::with_capacity(count); let mut counts = [0u32; 256]; let mut previous: Option<[u8; 32]> = None; for (index, chunk) in data[ids_start..offsets_start].chunks_exact(32).enumerate() { let mut id = [0u8; 32]; id.copy_from_slice(chunk); if previous.is_some_and(|value| value >= id) { return Err(Error::new("index ids are not sorted")); } previous = Some(id); counts[id[0] as usize] += 1; let mut offset_bytes = [0u8; 8]; let start = offsets_start + index * 8; offset_bytes.copy_from_slice(&data[start..start + 8]); entries.push(IndexEntry { id: Id(id), offset: u64::from_le_bytes(offset_bytes), }); } let mut pack_trailer = [0u8; 32]; pack_trailer.copy_from_slice(&data[trailer_offset - 32..trailer_offset]); if hash_bytes(&data[..trailer_offset])[..] != data[trailer_offset..] { return Err(Error::new("index checksum mismatch")); } if expected_pack_trailer.is_some_and(|value| value != pack_trailer) { return Err(Error::new("index pack trailer mismatch")); } let mut running = 0u32; for index in 0..256 { running += counts[index]; if running != fanout[index] { return Err(Error::new("index fanout is inconsistent")); } } Ok(Index { entries, pack_trailer, }) } pub fn encode(entries: &[IndexEntry], pack_trailer: [u8; 32]) -> Vec { use crate::encoding::Writer; let mut counts = [0u32; 256]; for entry in entries { counts[entry.id.0[0] as usize] += 1; } let mut writer = Writer::with_capacity(entries.len() * 40 + 128); writer.raw(b"VIX1"); writer.u32le(1); let mut running = 0u32; for count in counts { running += count; writer.u32le(running); } for entry in entries { writer.id(entry.id); } for entry in entries { writer.u64le(entry.offset); } writer.raw(&pack_trailer); let mut data = writer.into_vec(); let digest = hash_bytes(&data); data.extend_from_slice(&digest); data }