use crate::exceptions::{Error, Result};
const EOCD_SIGNATURE: &[u8; 4] = b"PK\x05\x06";
const EOCD64_LOCATOR_SIGNATURE: &[u8; 4] = b"PK\x06\x07";
const CENTRAL_SIGNATURE: &[u8; 4] = b"PK\x01\x02";
const EOCD_FIXED_LEN: usize = 22;
const CENTRAL_FIXED_LEN: usize = 46;
const MAX_ENTRIES: usize = u16::MAX as usize;
const MAX_CENTRAL_SIZE: u32 = u32::MAX;
struct CentralDirectory {
entries: u16,
size: u32,
offset: u32,
}
pub fn repair(bytes: Vec<u8>) -> Result<Option<Vec<u8>>> {
if zip::ZipArchive::new(std::io::Cursor::new(&bytes[..])).is_ok() {
return Ok(None);
}
if let Some(repaired) = truncate_after_eocd(&bytes) {
if zip::ZipArchive::new(std::io::Cursor::new(&repaired[..])).is_ok() {
return Ok(Some(repaired));
}
}
let (central, directory_end) = walk_central_directory(&bytes).ok_or_else(|| {
Error::InvalidFile(
"the zip's end-of-central-directory record is missing and its central directory \
could not be read, so the file is truncated beyond recovery"
.to_string(),
)
})?;
let mut repaired = bytes;
repaired.truncate(directory_end);
if let Some(position) = find_last(&repaired, EOCD64_LOCATOR_SIGNATURE) {
repaired.truncate(position);
}
repaired.extend_from_slice(&build_eocd(¢ral));
Ok(Some(repaired))
}
fn truncate_after_eocd(bytes: &[u8]) -> Option<Vec<u8>> {
let position = find_last(bytes, EOCD_SIGNATURE)?;
if position + EOCD_FIXED_LEN > bytes.len() {
return None;
}
let comment_len = u16::from_le_bytes([bytes[position + 20], bytes[position + 21]]) as usize;
let end = position + EOCD_FIXED_LEN + comment_len;
if end > bytes.len() {
return None;
}
if end == bytes.len() {
return None;
}
let mut repaired = bytes[..end].to_vec();
repaired.shrink_to_fit();
Some(repaired)
}
fn walk_central_directory(bytes: &[u8]) -> Option<(CentralDirectory, usize)> {
let mut search_from = 0usize;
while let Some(offset) = find_from(bytes, CENTRAL_SIGNATURE, search_from) {
if let Some(found) = walk_from(bytes, offset) {
return Some(found);
}
search_from = offset + 1;
}
None
}
fn is_partial_eocd(tail: &[u8]) -> bool {
tail.len() < EOCD_FIXED_LEN
&& (EOCD_SIGNATURE.starts_with(tail) || tail.starts_with(EOCD_SIGNATURE))
}
fn walk_from(bytes: &[u8], offset: usize) -> Option<(CentralDirectory, usize)> {
let mut cursor = offset;
let mut entries = 0usize;
while cursor < bytes.len() {
if is_partial_eocd(&bytes[cursor..]) {
break;
}
let entry = bytes.get(cursor..)?;
if entry.len() < CENTRAL_FIXED_LEN || &entry[..4] != CENTRAL_SIGNATURE {
return None;
}
let read_u16 = |at: usize| u16::from_le_bytes([entry[at], entry[at + 1]]);
let read_u32 = |at: usize| {
u32::from_le_bytes([entry[at], entry[at + 1], entry[at + 2], entry[at + 3]])
};
if read_u32(20) == u32::MAX || read_u32(24) == u32::MAX || read_u32(42) == u32::MAX {
return None;
}
let name_len = read_u16(28) as usize;
let extra_len = read_u16(30) as usize;
let comment_len = read_u16(32) as usize;
let next = cursor + CENTRAL_FIXED_LEN + name_len + extra_len + comment_len;
if next > bytes.len() {
return None;
}
cursor = next;
entries += 1;
if entries > MAX_ENTRIES {
return None;
}
}
if entries == 0 {
return None;
}
let tail = &bytes[cursor..];
if !tail.is_empty() && !is_partial_eocd(tail) {
return None;
}
let size = cursor - offset;
if size > MAX_CENTRAL_SIZE as usize || offset > MAX_CENTRAL_SIZE as usize {
return None;
}
Some((
CentralDirectory {
entries: entries as u16,
size: size as u32,
offset: offset as u32,
},
cursor,
))
}
fn build_eocd(central: &CentralDirectory) -> [u8; EOCD_FIXED_LEN] {
let mut record = [0u8; EOCD_FIXED_LEN];
record[..4].copy_from_slice(EOCD_SIGNATURE);
let put_u16 = |record: &mut [u8; EOCD_FIXED_LEN], at: usize, value: u16| {
record[at..at + 2].copy_from_slice(&value.to_le_bytes());
};
let put_u32 = |record: &mut [u8; EOCD_FIXED_LEN], at: usize, value: u32| {
record[at..at + 4].copy_from_slice(&value.to_le_bytes());
};
put_u16(&mut record, 4, 0);
put_u16(&mut record, 6, 0);
put_u16(&mut record, 8, central.entries);
put_u16(&mut record, 10, central.entries);
put_u32(&mut record, 12, central.size);
put_u32(&mut record, 16, central.offset);
put_u16(&mut record, 20, 0);
record
}
fn find_last(haystack: &[u8], needle: &[u8; 4]) -> Option<usize> {
if haystack.len() < needle.len() {
return None;
}
(0..=haystack.len() - needle.len())
.rev()
.find(|&at| &haystack[at..at + needle.len()] == needle)
}
fn find_from(haystack: &[u8], needle: &[u8; 4], from: usize) -> Option<usize> {
if haystack.len() < needle.len() || from > haystack.len() - needle.len() {
return None;
}
(from..=haystack.len() - needle.len()).find(|&at| &haystack[at..at + needle.len()] == needle)
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::{Cursor, Write};
fn sample_archive() -> Vec<u8> {
let mut buffer = Cursor::new(Vec::new());
{
let mut writer = zip::ZipWriter::new(&mut buffer);
let options: zip::write::FileOptions<'_, ()> = zip::write::FileOptions::default()
.compression_method(zip::CompressionMethod::Stored);
for name in [
"[Content_Types].xml",
"xl/workbook.xml",
"xl/worksheets/sheet1.xml",
] {
writer.start_file(name, options).expect("writable");
writer.write_all(b"<x/>").expect("writable");
}
writer.finish().expect("finished");
}
buffer.into_inner()
}
fn opens(bytes: &[u8]) -> bool {
zip::ZipArchive::new(Cursor::new(bytes.to_vec())).is_ok()
}
fn entry_count(bytes: &[u8]) -> usize {
zip::ZipArchive::new(Cursor::new(bytes.to_vec()))
.map(|archive| archive.len())
.unwrap_or(0)
}
#[test]
fn an_intact_archive_is_left_alone() {
let archive = sample_archive();
assert!(repair(archive.clone()).expect("no error").is_none());
}
#[test]
fn a_truncated_end_of_central_directory_is_rebuilt() {
let archive = sample_archive();
let truncated = &archive[..archive.len() - EOCD_FIXED_LEN];
assert!(!opens(truncated), "the truncation really did break it");
let repaired = repair(truncated.to_vec())
.expect("repairable")
.expect("something to repair");
assert!(opens(&repaired));
assert_eq!(entry_count(&repaired), 3, "every entry survived");
}
#[test]
fn a_partly_truncated_end_of_central_directory_is_rebuilt() {
let archive = sample_archive();
for cut in [1usize, 2, 3, 4, 5, 8, 12, 16, 19, 20, 21] {
let truncated = &archive[..archive.len() - cut];
assert!(!opens(truncated), "{cut} bytes off should break it");
let repaired = repair(truncated.to_vec())
.unwrap_or_else(|error| panic!("{cut} bytes off: {error}"))
.unwrap_or_else(|| panic!("{cut} bytes off: nothing repaired"));
assert_eq!(entry_count(&repaired), 3, "{cut} bytes off");
}
}
#[test]
fn junk_after_the_record_is_tolerated_without_our_help() {
let mut archive = sample_archive();
assert!(opens(&archive));
archive.extend_from_slice(&[0u8; 4096]);
assert!(opens(&archive), "trailing junk already tolerated");
assert!(repair(archive).expect("no error").is_none());
}
#[test]
fn a_truncation_into_the_central_directory_is_refused() {
let archive = sample_archive();
let truncated = &archive[..archive.len() - EOCD_FIXED_LEN - 20];
let error = repair(truncated.to_vec()).expect_err("unrecoverable");
assert!(
error.to_string().contains("truncated beyond recovery"),
"unexpected error: {error}"
);
}
#[test]
fn something_that_is_not_a_zip_at_all_is_refused() {
let error = repair(b"this is not a zip file".to_vec()).expect_err("unrecoverable");
assert!(error.to_string().contains("truncated beyond recovery"));
}
#[test]
fn a_rebuilt_record_reports_the_true_directory() {
let archive = sample_archive();
let original = find_last(&archive, EOCD_SIGNATURE).expect("a record");
let truncated = &archive[..original];
assert!(!opens(truncated), "the truncation really did break it");
let repaired = repair(truncated.to_vec())
.expect("repairable")
.expect("something to repair");
let rebuilt = find_last(&repaired, EOCD_SIGNATURE).expect("a rebuilt record");
assert_eq!(
&repaired[rebuilt + 4..rebuilt + EOCD_FIXED_LEN],
&archive[original + 4..original + EOCD_FIXED_LEN],
"every field of the rebuilt record matches the original"
);
}
#[test]
fn a_central_directory_signature_inside_data_does_not_fool_the_walk() {
let archive = sample_archive();
let decoy = b"PK\x01\x02 decoy bytes that are not an entry header at all";
let mut spliced = archive[..archive.len() - EOCD_FIXED_LEN].to_vec();
spliced.extend_from_slice(decoy);
let error = repair(spliced).expect_err("a decoy is not a directory");
assert!(error.to_string().contains("truncated beyond recovery"));
}
}