use std::collections::BTreeMap;
use miniz_oxide::inflate::decompress_to_vec_with_limit;
const BLOCK: usize = 512;
const PACKAGE: &str = "package/";
const FHCRC: u8 = 0b0010;
const FEXTRA: u8 = 0b0100;
const FNAME: u8 = 0b1000;
const FCOMMENT: u8 = 0b1_0000;
fn deflate_start(bytes: &[u8]) -> Option<usize> {
let [0x1f, 0x8b, 8, flags, ..] = *bytes else {
return None;
};
let mut at = 10;
if flags & FEXTRA != 0 {
let length = usize::from(u16::from_le_bytes([*bytes.get(at)?, *bytes.get(at + 1)?]));
at += 2 + length;
}
for flag in [FNAME, FCOMMENT] {
if flags & flag != 0 {
at += bytes.get(at..)?.iter().position(|&byte| byte == 0)? + 1;
}
}
if flags & FHCRC != 0 {
at += 2;
}
(at <= bytes.len()).then_some(at)
}
pub(crate) fn gunzip(bytes: &[u8], cap: usize) -> Result<Vec<u8>, String> {
let start = deflate_start(bytes).ok_or_else(|| "the tarball isn't gzip".to_owned())?;
decompress_to_vec_with_limit(&bytes[start..], cap).map_err(|error| match error.status {
miniz_oxide::inflate::TINFLStatus::HasMoreOutput => {
format!("it unzips to more than {cap} bytes")
}
_ => "the tarball isn't gzip".to_owned(),
})
}
fn field(header: &[u8], start: usize, length: usize) -> String {
let bytes = &header[start..start + length];
let end = bytes
.iter()
.position(|&byte| byte == 0)
.unwrap_or(bytes.len());
String::from_utf8_lossy(&bytes[..end]).into_owned()
}
fn size_of(header: &[u8]) -> Result<usize, String> {
let written = field(header, 124, 12);
let written = written.trim();
if !written.bytes().all(|byte| (b'0'..=b'7').contains(&byte)) {
return Err(format!(
"a tar entry's size isn't a whole number of bytes: `{written}`"
));
}
if written.is_empty() {
return Ok(0);
}
usize::from_str_radix(written, 8)
.map_err(|_| format!("a tar entry's size isn't a whole number of bytes: `{written}`"))
}
fn pax_path(data: &[u8]) -> Option<String> {
let text = String::from_utf8_lossy(data);
text.split('\n').find_map(|record| {
let (length, rest) = record.split_once(' ')?;
(length.bytes().all(|byte| byte.is_ascii_digit()) && !length.is_empty())
.then(|| rest.strip_prefix("path="))
.flatten()
.map(str::to_owned)
})
}
fn package_path(name: &str) -> Option<&str> {
let path = name.strip_prefix(PACKAGE)?;
let unsafe_path = path.is_empty()
|| path.contains('\\')
|| path
.split('/')
.any(|part| part == ".." || part == "." || part.is_empty());
(!unsafe_path).then_some(path)
}
pub(crate) fn untar(archive: &[u8], max_size: usize) -> Result<BTreeMap<String, Vec<u8>>, String> {
let mut files = BTreeMap::new();
let mut offset = 0;
let mut total = 0usize;
let mut long_name: Option<String> = None;
while offset + BLOCK <= archive.len() {
let header = &archive[offset..offset + BLOCK];
if header.iter().all(|&byte| byte == 0) {
break;
}
let size = size_of(header)?;
let kind = header[156];
let end = offset
.checked_add(BLOCK + size)
.filter(|end| *end <= archive.len())
.ok_or_else(|| "a tar entry runs past the tarball's end".to_owned())?;
let data = &archive[offset + BLOCK..end];
offset += BLOCK + size.div_ceil(BLOCK) * BLOCK;
let regular = kind == b'0' || kind == 0;
if regular {
total += size;
if total > max_size {
return Err(format!(
"its files take more than the {max_size} bytes its registry declares"
));
}
}
match kind {
b'x' => {
long_name = pax_path(data);
continue;
}
b'L' => {
long_name = Some(field(data, 0, data.len()));
continue;
}
_ => {}
}
let prefix = field(header, 345, 155);
let short = field(header, 0, 100);
let name = long_name.take().unwrap_or_else(|| {
if prefix.is_empty() {
short
} else {
format!("{prefix}/{short}")
}
});
if let (true, Some(path)) = (regular, package_path(&name)) {
files.insert(path.to_owned(), data.to_vec());
}
}
Ok(files)
}
pub(crate) fn split(
files: BTreeMap<String, Vec<u8>>,
) -> (BTreeMap<String, String>, BTreeMap<String, Vec<u8>>) {
let mut text = BTreeMap::new();
let mut binary = BTreeMap::new();
for (path, bytes) in files {
match String::from_utf8(bytes) {
Ok(content) => {
text.insert(path, content);
}
Err(error) => {
binary.insert(path, error.into_bytes());
}
}
}
(text, binary)
}
#[cfg(test)]
pub(crate) mod tests;