use crate::encoding::{Reader, Writer}; use crate::error::{Error, Result}; use crate::object::chunker::next_chunk; use crate::object::id::{Id, object_id}; use crate::object::{ INLINE_THRESHOLD, MAX_CHUNK_LEN, MAX_MANIFEST_ENTRIES, RawObject, TYPE_BLOB, TYPE_CHUNK, }; #[derive(Clone, Copy, PartialEq, Eq)] pub enum BlobForm { Inline, Leaf, Interior, } #[derive(Clone)] pub struct BlobEntry { pub id: Id, pub length: u64, } #[derive(Clone)] pub struct Blob { pub form: BlobForm, pub data: Vec, pub entries: Vec, } pub struct BlobBuild { pub root: Id, pub objects: Vec, } pub fn encode_inline(data: &[u8]) -> Vec { let mut writer = Writer::with_capacity(data.len() + 8); writer.u8(0); writer.bytes(data); writer.into_vec() } pub fn decode(body: &[u8]) -> Result { let mut reader = Reader::new(body); let form = match reader.u8()? { 0 => BlobForm::Inline, 1 => BlobForm::Leaf, 2 => BlobForm::Interior, _ => return Err(Error::new("invalid blob form")), }; if form == BlobForm::Inline { let data = reader.bytes(INLINE_THRESHOLD)?.to_vec(); reader.finish()?; return Ok(Blob { form, data, entries: Vec::new(), }); } let count = reader.varint()?; let count = usize::try_from(count).map_err(|_| Error::new("manifest too large"))?; if count == 0 || count > MAX_MANIFEST_ENTRIES { return Err(Error::new("invalid manifest entry count")); } let mut entries = Vec::with_capacity(count); let mut total = 0u64; for _ in 0..count { let id = reader.id()?; let length = reader.varint()?; if length == 0 { return Err(Error::new("invalid manifest entry length")); } if form == BlobForm::Leaf && length > MAX_CHUNK_LEN as u64 { return Err(Error::new("chunk exceeds maximum size")); } total = total .checked_add(length) .ok_or_else(|| Error::new("manifest length overflow"))?; entries.push(BlobEntry { id, length }); } reader.finish()?; Ok(Blob { form, data: Vec::new(), entries, }) } fn cut_after(id: Id, current_length: usize) -> bool { current_length >= MAX_MANIFEST_ENTRIES || (current_length >= 16 && id.0[0] == 0) } struct NodeRef { id: Id, length: u64, } pub fn build(data: &[u8]) -> BlobBuild { if data.len() <= INLINE_THRESHOLD { let body = encode_inline(data); let root = object_id(TYPE_BLOB, &body); return BlobBuild { root, objects: vec![RawObject::known(TYPE_BLOB, root, body)], }; } let mut objects = Vec::new(); let mut level = Vec::new(); let mut offset = 0; while offset < data.len() { let length = next_chunk(&data[offset..]); let chunk = &data[offset..offset + length]; let id = object_id(TYPE_CHUNK, chunk); objects.push(RawObject::known(TYPE_CHUNK, id, chunk.to_vec())); level.push(NodeRef { id, length: length as u64, }); offset += length; } let mut leaf_level = true; let root = loop { let mut groups = Vec::new(); let mut start = 0; for index in 0..level.len() { let current = index - start + 1; if cut_after(level[index].id, current) || index + 1 == level.len() { groups.push((start, current)); start = index + 1; } } let mut next = Vec::with_capacity(groups.len()); for (start, count) in groups { let mut writer = Writer::with_capacity(count * 40 + 8); writer.u8(if leaf_level { 1 } else { 2 }); writer.varint(count as u64); let mut total = 0u64; for index in 0..count { let node = &level[start + index]; writer.id(node.id); writer.varint(node.length); total += node.length; } let body = writer.into_vec(); let id = object_id(TYPE_BLOB, &body); objects.push(RawObject::known(TYPE_BLOB, id, body)); next.push(NodeRef { id, length: total }); } if next.len() == 1 { break next[0].id; } level = next; leaf_level = false; }; BlobBuild { root, objects } }