use crate::repo_read_layout::ObjectFormat;
use miniz_oxide::inflate::TINFLStatus;
use miniz_oxide::inflate::core::{DecompressorOxide, decompress, inflate_flags};
pub const OBJECT_SIZE_LIMIT: usize = 256 << 20;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ObjectKind {
Commit,
Tree,
Blob,
Tag,
}
impl ObjectKind {
pub fn word(self) -> &'static str {
return match self {
ObjectKind::Commit => "commit",
ObjectKind::Tree => "tree",
ObjectKind::Blob => "blob",
ObjectKind::Tag => "tag",
};
}
pub fn from_word(word: &[u8]) -> Option<ObjectKind> {
return match word {
b"commit" => Some(ObjectKind::Commit),
b"tree" => Some(ObjectKind::Tree),
b"blob" => Some(ObjectKind::Blob),
b"tag" => Some(ObjectKind::Tag),
_ => None,
};
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RawObject {
pub kind: ObjectKind,
pub bytes: Vec<u8>,
}
pub fn name_length(format: ObjectFormat) -> usize {
return format.hex_length() / 2;
}
pub fn to_hex(name: &[u8]) -> String {
let mut text: String = String::with_capacity(name.len() * 2);
for byte in name {
text.push(char::from(b"0123456789abcdef"[usize::from(byte >> 4)]));
text.push(char::from(b"0123456789abcdef"[usize::from(byte & 15)]));
}
return text;
}
pub fn from_hex(text: &[u8], format: ObjectFormat) -> Option<Vec<u8>> {
if text.len() != format.hex_length() {
return None;
}
let digit = |byte: u8| -> Option<u8> {
return match byte {
b'0'..=b'9' => Some(byte - b'0'),
b'a'..=b'f' => Some(byte - b'a' + 10),
_ => None,
};
};
let mut name: Vec<u8> = Vec::with_capacity(text.len() / 2);
for pair in text.chunks(2) {
name.push(digit(pair[0])? << 4 | digit(pair[1])?);
}
return Some(name);
}
pub const INFLATE_INPUT_LIMIT: usize = 512 << 10;
const FIRST_OUTPUT_BYTES: usize = 64;
pub fn inflate_zlib(whole_input: &[u8], limit: usize) -> Option<(Vec<u8>, usize)> {
let input: &[u8] = &whole_input[..whole_input.len().min(INFLATE_INPUT_LIMIT)];
let flags: u32 = inflate_flags::TINFL_FLAG_PARSE_ZLIB_HEADER | inflate_flags::TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
let mut output: Vec<u8> = vec![0; input.len().saturating_mul(2).min(limit)];
let mut state: Box<DecompressorOxide> = Box::<DecompressorOxide>::default();
let mut produced: usize = 0;
let mut used: usize = 0;
loop {
let (status, consumed, written) = decompress(&mut state, input.get(used..)?, &mut output, produced, flags);
used = used.checked_add(consumed)?;
produced = produced.checked_add(written)?;
match status {
TINFLStatus::Done => {
output.truncate(produced);
return Some((output, used));
}
TINFLStatus::HasMoreOutput => {
if output.len() >= limit {
return None;
}
let grown: usize = output.len().saturating_mul(2).max(FIRST_OUTPUT_BYTES).min(limit);
output.resize(grown, 0);
}
_ => return None,
}
}
}
pub fn parse_loose(file_bytes: &[u8]) -> Option<RawObject> {
let (whole, consumed): (Vec<u8>, usize) = inflate_zlib(file_bytes, OBJECT_SIZE_LIMIT + 32)?;
if consumed != file_bytes.len() {
return None;
}
let nul: usize = whole.iter().position(|byte| { return *byte == 0; })?;
let header: &[u8] = &whole[..nul];
let space: usize = header.iter().position(|byte| { return *byte == b' '; })?;
let kind: ObjectKind = ObjectKind::from_word(&header[..space])?;
let size_text: &str = std::str::from_utf8(&header[space + 1..]).ok()?;
if size_text.is_empty() || (size_text.len() > 1 && size_text.starts_with('0')) || !size_text.bytes().all(|byte| { return byte.is_ascii_digit(); }) {
return None;
}
let size: usize = size_text.parse::<usize>().ok()?;
let content: &[u8] = &whole[nul + 1..];
if content.len() != size {
return None;
}
return Some(RawObject { kind, bytes: content.to_vec() });
}
pub fn inflate_exact(window: &[u8], expected: usize) -> Option<Vec<u8>> {
if expected > OBJECT_SIZE_LIMIT {
return None;
}
let (inflated, _): (Vec<u8>, usize) = inflate_zlib(window, expected)?;
if inflated.len() != expected {
return None;
}
return Some(inflated);
}
#[cfg(test)]
#[path = "repo_read_object_tests.rs"]
mod tests;