craft-zip: the lenient reader as craft_zip::lenient instead of a second ZIP crate

craft-unzip's raw-bytes reader moves unchanged into craft-zip as the
`lenient` module, behind the `lenient` feature (DEFLATE) and
`lenient-bzip2`, `lenient-lzma` and `lenient-zstd` for the other
methods, with `serde` for the listing types. craft-zip's default build
is unchanged; the module doesn't use the `zip` crate. Its unit and
integration tests move with it and run in craft-zip's test build.
This commit is contained in:
FredApps
2026-11-16 12:25:00 +01:00
parent ecc89eccff
commit 712feab586
21 changed files with 128 additions and 121 deletions
Generated
+6 -13
View File
@@ -2898,19 +2898,6 @@ dependencies = [
"web-sys",
]
[[package]]
name = "craft-unzip"
version = "0.1.0"
dependencies = [
"bzip2-rs",
"lzma-rs",
"miniz_oxide 0.8.9",
"ruzstd",
"serde",
"serde_json",
"thiserror 2.0.21",
]
[[package]]
name = "craft-update"
version = "0.1.0"
@@ -3113,8 +3100,14 @@ dependencies = [
name = "craft-zip"
version = "0.1.0"
dependencies = [
"bzip2-rs",
"craft-zip",
"flate2",
"lzma-rs",
"miniz_oxide 0.8.9",
"ruzstd",
"serde",
"serde_json",
"thiserror 2.0.21",
"zip",
]
-1
View File
@@ -174,7 +174,6 @@ craft-xcf = { path = "crates/xcf" }
craft-xml = { path = "crates/xml" }
craft-zip = { path = "crates/zip" }
craft-y4m = { path = "crates/y4m" }
craft-unzip = { path = "crates/unzip" }
craft-epub = { path = "crates/epub" }
craft-ews = { path = "crates/ews" }
craft-video = { path = "crates/video" }
+1 -2
View File
@@ -392,8 +392,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
- [`craft-kra`](crates/kra/README.md): Krita documents (`.kra`, `.krz`), read: the ZIP archive, the `maindoc.xml` node tree as written, and the tiled pixel files with their LZF compression, a tile at a time. From PhotoCraft.
- [`craft-xcf`](crates/xcf/README.md): GIMP XCF images (`.xcf`, `.xcf.gz`), read: header, properties, parasites, layers with their item paths, channels, and tiled pixels (uncompressed, RLE, zlib) at every precision, under a caller's byte budget. From PhotoCraft.
- [`craft-xml`](crates/xml/README.md): bounded XML for hostile input: an owned element tree with limits on size, depth, nodes and attributes, streaming of repeated elements, namespace handling (as written, by local name, or canonical prefixes by URI), escaping and a writer. No DTDs and no entity expansion.
- [`craft-zip`](crates/zip/README.md): ZIP archives from untrusted files: an entry-count check before the archive is indexed (ZIP64 included), entry reads within per-entry and total budgets that count real bytes, duplicate-name detection, a strict structure preflight, and a deterministic stored/deflate writer. The shared ZIP layer for the package formats.
- [`craft-unzip`](crates/unzip/src/lib.rs): ZIP reading over bytes already in memory (or one named entry from a seekable file), from Craft Anything: the central-directory listing with ZIP64, prepended-stub and multi-volume checks, CP437 and Info-ZIP Unicode Path names, and entry reads (stored, DEFLATE, and bzip2, LZMA, XZ and Zstandard behind default features) checked against the declared size and CRC-32. Pure Rust with no `zip` crate underneath; for inspectors that must describe damaged archives rather than refuse them.
- [`craft-zip`](crates/zip/README.md): ZIP archives from untrusted files: an entry-count check before the archive is indexed (ZIP64 included), entry reads within per-entry and total budgets that count real bytes, duplicate-name detection, a strict structure preflight, and a deterministic stored/deflate writer. The shared ZIP layer for the package formats. With the `lenient` feature, also a reader of the raw bytes without the `zip` crate, from Craft Anything, for inspectors that must describe damaged archives rather than refuse them: the central-directory listing with ZIP64, prepended-stub and multi-volume checks, CP437 and Info-ZIP Unicode Path names, and entry reads (stored, DEFLATE, and bzip2, LZMA, XZ and Zstandard behind `lenient-*` features) checked against the declared size and CRC-32.
- [Shared UI component audit](docs/ui-components-audit.md): source inventory, extraction priorities
and compatibility boundaries across the Craft suite.
- [Theme infrastructure](docs/theme-infrastructure.md): eight-app comparison and the small shared
-34
View File
@@ -1,34 +0,0 @@
[package]
name = "craft-unzip"
description = "ZIP archives (APK, JAR, IPA, MSIX…) from hostile bytes, from Craft Anything: a central-directory listing with ZIP64, prepended-stub and multi-volume checks, and bounded entry reads checked against the declared size and CRC-32. Pure Rust, no I/O beyond an optional seekable reader."
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
repository.workspace = true
[lints]
workspace = true
[dependencies]
thiserror = { workspace = true }
miniz_oxide = { workspace = true }
# Entry methods beyond stored and DEFLATE. All pure Rust and wasm32-safe.
bzip2-rs = { version = "0.1", optional = true } # MIT OR Apache-2.0
lzma-rs = { version = "0.3", optional = true } # MIT
ruzstd = { version = "0.9.1", default-features = false, features = ["std"], optional = true } # MIT
# `serde`: `Serialize` on the listing types, for apps that report listings as JSON.
serde = { workspace = true, optional = true }
[features]
default = ["bzip2", "lzma", "zstd"]
# Method 12.
bzip2 = ["dep:bzip2-rs"]
# Methods 14 (ZIP LZMA) and 95 (XZ).
lzma = ["dep:lzma-rs"]
# Method 93.
zstd = ["dep:ruzstd"]
serde = ["dep:serde"]
[dev-dependencies]
serde_json = { workspace = true }
+19 -1
View File
@@ -17,10 +17,28 @@ thiserror = { workspace = true }
zip = { version = "9", default-features = false, features = ["deflate-flate2"] }
# The backend of zip's deflate: `deflate-flate2` leaves the choice to the build.
flate2 = { version = "1", default-features = false, features = ["rust_backend"] }
# `lenient`: DEFLATE for the raw-bytes reader.
miniz_oxide = { workspace = true, optional = true }
# `lenient` entry methods beyond stored and DEFLATE. All pure Rust and wasm32-safe.
bzip2-rs = { version = "0.1", optional = true } # MIT OR Apache-2.0
lzma-rs = { version = "0.3", optional = true } # MIT
ruzstd = { version = "0.9.1", default-features = false, features = ["std"], optional = true } # MIT
# `serde`: `Serialize` on the `lenient` listing types, for apps that report listings as JSON.
serde = { workspace = true, optional = true }
[features]
# `zip64_entry_count_sample`, the regression input for a reader's own entry-count test.
test-support = []
# `craft_zip::lenient`: the raw-bytes reader for inspectors (from Craft Anything), without `zip`.
lenient = ["dep:miniz_oxide"]
# Method 12.
lenient-bzip2 = ["lenient", "dep:bzip2-rs"]
# Methods 14 (ZIP LZMA) and 95 (XZ).
lenient-lzma = ["lenient", "dep:lzma-rs"]
# Method 93.
lenient-zstd = ["lenient", "dep:ruzstd"]
serde = ["dep:serde"]
[dev-dependencies]
craft-zip = { path = ".", features = ["test-support"] }
craft-zip = { path = ".", features = ["test-support", "lenient-bzip2", "lenient-lzma", "lenient-zstd", "serde"] }
serde_json = { workspace = true }
+10
View File
@@ -79,3 +79,13 @@ bytes and charges repeated reads against twice that number. Call `Indexing::fini
returned handle after the ZIP index has been parsed to permit normal streaming payload reads.
The caller must still enforce entry-count, declared-size and overlap limits: the wrapper only
bounds metadata reads and does not validate the archive.
## Lenient reading (feature `lenient`)
`craft_zip::lenient` reads an archive from its raw bytes without the `zip` crate, for inspectors
(Craft Anything) that must list and describe damaged, prepended, split or oddly encoded archives
instead of refusing them. Only the central directory is trusted for the listing; entry reads are
bounded, checked against the declared size and CRC-32, and decode stored and DEFLATE entries, plus
bzip2 (`lenient-bzip2`), ZIP LZMA and XZ (`lenient-lzma`) and Zstandard (`lenient-zstd`).
`lenient::file::read_named_entry` reads one entry from a seekable file without loading the
archive. With `serde`, the listing types are `Serialize`.
@@ -1,14 +1,17 @@
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
#![forbid(unsafe_code)]
//! ZIP archives (also APK, JAR, IPA, MSIX, HAP…): central directory listing and bounded entry reads.
//! ZIP archives (also APK, JAR, IPA, MSIX, HAP…) read leniently from the raw bytes, for
//! inspectors that must list and describe damaged, prepended, split or oddly encoded archives
//! rather than refuse them (feature `lenient`). The rest of this crate is the strict side: it
//! refuses what fails [`crate::preflight`] and reads through the `zip` crate; nothing here uses
//! `zip`.
//!
//! Only the central directory is trusted for the listing. Entry data is located through the local
//! header, decompressed with a hard output cap, and checked against the recorded CRC-32. Stored,
//! DEFLATE, bzip2, ZIP LZMA, XZ and Zstandard entries are supported; other methods and encrypted entries are listed.
//!
//! The bzip2, LZMA/XZ and Zstandard methods sit behind the default `bzip2`, `lzma` and `zstd`
//! features; without one, entries using it are listed and reading them is
//! The bzip2, LZMA/XZ and Zstandard methods sit behind the `lenient-bzip2`, `lenient-lzma` and
//! `lenient-zstd` features; without one, entries using it are listed and reading them is
//! [`ZipError::Unsupported`]. [`file::read_named_entry`] reads one entry from a seekable file
//! without loading the archive. With the `serde` feature the listing types are `Serialize`.
//!
@@ -18,11 +21,11 @@
//! against the directory, and CRC-32 verification.
mod bytes;
#[cfg(any(feature = "bzip2", feature = "lzma", feature = "zstd"))]
#[cfg(any(feature = "lenient-bzip2", feature = "lenient-lzma", feature = "lenient-zstd"))]
mod decode;
pub mod file;
use crate::bytes::Reader;
use crate::lenient::bytes::Reader;
/// Biggest tail we search for the end-of-central-directory record (64 KiB comment + record).
pub const EOCD_SEARCH: usize = 22 + 0xffff;
@@ -474,12 +477,12 @@ pub fn read_entry_unverified(data: &[u8], e: &ZipEntry, max_bytes: u64) -> Resul
Ok(out)
}
#[cfg(any(feature = "bzip2", feature = "lzma"))]
#[cfg(any(feature = "lenient-bzip2", feature = "lenient-lzma"))]
fn decode_bzip2_or_xz(raw: &[u8], e: &ZipEntry, max_bytes: u64) -> Result<Vec<u8>, ZipError> {
let decoded = match e.method {
#[cfg(feature = "bzip2")]
#[cfg(feature = "lenient-bzip2")]
12 => decode::bzip2(raw, max_bytes),
#[cfg(feature = "lzma")]
#[cfg(feature = "lenient-lzma")]
95 => decode::xz(raw, max_bytes),
_ => return Err(not_built(e)),
};
@@ -489,12 +492,12 @@ fn decode_bzip2_or_xz(raw: &[u8], e: &ZipEntry, max_bytes: u64) -> Result<Vec<u8
})
}
#[cfg(not(any(feature = "bzip2", feature = "lzma")))]
#[cfg(not(any(feature = "lenient-bzip2", feature = "lenient-lzma")))]
fn decode_bzip2_or_xz(_: &[u8], e: &ZipEntry, _: u64) -> Result<Vec<u8>, ZipError> {
Err(not_built(e))
}
#[cfg(feature = "lzma")]
#[cfg(feature = "lenient-lzma")]
fn decode_lzma(raw: &[u8], e: &ZipEntry, max_bytes: u64) -> Result<Vec<u8>, ZipError> {
let header = Reader::new(raw);
let properties_size = header.u16_le(2).ok_or(ZipError::Truncated("LZMA properties header"))?;
@@ -508,23 +511,23 @@ fn decode_lzma(raw: &[u8], e: &ZipEntry, max_bytes: u64) -> Result<Vec<u8>, ZipE
})
}
#[cfg(not(feature = "lzma"))]
#[cfg(not(feature = "lenient-lzma"))]
fn decode_lzma(_: &[u8], e: &ZipEntry, _: u64) -> Result<Vec<u8>, ZipError> {
Err(not_built(e))
}
#[cfg(feature = "zstd")]
#[cfg(feature = "lenient-zstd")]
fn decode_zstd(raw: &[u8], e: &ZipEntry, max_bytes: u64) -> Result<Vec<u8>, ZipError> {
decode::zstd(raw, max_bytes).map_err(|err| ZipError::Corrupt(format!("zstd stream for {} is damaged: {err}", e.name)))
}
#[cfg(not(feature = "zstd"))]
#[cfg(not(feature = "lenient-zstd"))]
fn decode_zstd(_: &[u8], e: &ZipEntry, _: u64) -> Result<Vec<u8>, ZipError> {
Err(not_built(e))
}
/// An entry whose method this build leaves out (see the crate features).
#[cfg_attr(all(feature = "bzip2", feature = "lzma", feature = "zstd"), allow(dead_code))]
#[cfg_attr(all(feature = "lenient-bzip2", feature = "lenient-lzma", feature = "lenient-zstd"), allow(dead_code))]
fn not_built(e: &ZipEntry) -> ZipError {
ZipError::Unsupported(format!("compression method {} ({}) for {} is not built in", e.method, e.method_name(), e.name))
}
@@ -3,9 +3,9 @@
//! allocating without limit) and never panics on a corrupt or truncated stream.
use std::io;
#[cfg(any(feature = "bzip2", feature = "lzma"))]
#[cfg(any(feature = "lenient-bzip2", feature = "lenient-lzma"))]
use std::io::Read;
#[cfg(any(feature = "lzma", feature = "zstd"))]
#[cfg(any(feature = "lenient-lzma", feature = "lenient-zstd"))]
use std::io::Write;
/// Why a decode stopped.
@@ -14,7 +14,7 @@ pub(crate) enum DecompressError {
#[error("the compressed stream is damaged")]
Corrupt,
#[error("the compressed stream ends early")]
#[cfg(any(feature = "lzma", feature = "zstd"))]
#[cfg(any(feature = "lenient-lzma", feature = "lenient-zstd"))]
Truncated,
#[error("the output would exceed the size limit")]
TooLarge,
@@ -22,21 +22,21 @@ pub(crate) enum DecompressError {
/// A `Write` sink that collects at most `cap` bytes, then refuses the rest. The `exceeded` flag lets
/// the caller tell a size-limit stop from a genuine decode error.
#[cfg(any(feature = "lzma", feature = "zstd"))]
#[cfg(any(feature = "lenient-lzma", feature = "lenient-zstd"))]
struct CapWrite {
buf: Vec<u8>,
cap: usize,
exceeded: bool,
}
#[cfg(any(feature = "lzma", feature = "zstd"))]
#[cfg(any(feature = "lenient-lzma", feature = "lenient-zstd"))]
impl CapWrite {
fn new(cap: usize) -> Self {
CapWrite { buf: Vec::new(), cap, exceeded: false }
}
}
#[cfg(any(feature = "lzma", feature = "zstd"))]
#[cfg(any(feature = "lenient-lzma", feature = "lenient-zstd"))]
impl Write for CapWrite {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
if self.buf.len().saturating_add(data.len()) > self.cap {
@@ -57,7 +57,7 @@ fn cap_of(max_out: u64) -> usize {
}
/// Read a `Read` source into a `Vec`, capped at `cap` bytes.
#[cfg(feature = "bzip2")]
#[cfg(feature = "lenient-bzip2")]
fn read_capped(mut src: impl Read, cap: usize) -> Result<Vec<u8>, DecompressError> {
let mut out = Vec::new();
let mut chunk = [0u8; 64 * 1024];
@@ -77,7 +77,7 @@ fn read_capped(mut src: impl Read, cap: usize) -> Result<Vec<u8>, DecompressErro
}
}
#[cfg(feature = "lzma")]
#[cfg(feature = "lenient-lzma")]
/// Decompress every xz stream, including standard stream padding, within one output budget.
pub(crate) fn xz(input: &[u8], max_out: u64) -> Result<Vec<u8>, DecompressError> {
let ranges = xz_stream_ranges(input, max_out)?;
@@ -95,7 +95,7 @@ pub(crate) fn xz(input: &[u8], max_out: u64) -> Result<Vec<u8>, DecompressError>
Ok(out.buf)
}
#[cfg(feature = "lzma")]
#[cfg(feature = "lenient-lzma")]
/// Find stream boundaries backwards through their checked footer/index sizes, without
/// repeatedly trying the decoder at candidate magic bytes inside compressed data.
fn xz_stream_ranges(input: &[u8], max_out: u64) -> Result<Vec<std::ops::Range<usize>>, DecompressError> {
@@ -112,9 +112,9 @@ fn xz_stream_ranges(input: &[u8], max_out: u64) -> Result<Vec<std::ops::Range<us
}
let footer_at = end.checked_sub(12).ok_or(DecompressError::Truncated)?;
let footer = input.get(footer_at..end).ok_or(DecompressError::Truncated)?;
let read = crate::bytes::Reader::new(footer);
let read = crate::lenient::bytes::Reader::new(footer);
let footer_fields = footer.get(4..10).ok_or(DecompressError::Truncated)?;
if footer.get(10..) != Some(b"YZ") || read.u32_le(0) != Some(crate::crc32(footer_fields)) {
if footer.get(10..) != Some(b"YZ") || read.u32_le(0) != Some(crate::lenient::crc32(footer_fields)) {
return Err(DecompressError::Corrupt);
}
let backward = u64::from(read.u32_le(4).ok_or(DecompressError::Truncated)?);
@@ -122,9 +122,9 @@ fn xz_stream_ranges(input: &[u8], max_out: u64) -> Result<Vec<std::ops::Range<us
let index_at = footer_at.checked_sub(index_len).ok_or(DecompressError::Truncated)?;
let index = input.get(index_at..footer_at).ok_or(DecompressError::Truncated)?;
let crc_at = index.len().checked_sub(4).ok_or(DecompressError::Truncated)?;
let index_read = crate::bytes::Reader::new(index);
let index_read = crate::lenient::bytes::Reader::new(index);
let index_fields = index.get(..crc_at).ok_or(DecompressError::Truncated)?;
if index.first() != Some(&0) || index_read.u32_le(crc_at) != Some(crate::crc32(index_fields)) {
if index.first() != Some(&0) || index_read.u32_le(crc_at) != Some(crate::lenient::crc32(index_fields)) {
return Err(DecompressError::Corrupt);
}
let mut records = index.get(1..crc_at).ok_or(DecompressError::Corrupt)?;
@@ -159,7 +159,7 @@ fn xz_stream_ranges(input: &[u8], max_out: u64) -> Result<Vec<std::ops::Range<us
Ok(ranges)
}
#[cfg(feature = "lzma")]
#[cfg(feature = "lenient-lzma")]
/// XZ variable-length integers have at most nine bytes and no redundant final zero.
fn xz_vli(input: &mut &[u8]) -> Result<u64, DecompressError> {
let mut value = 0u64;
@@ -174,7 +174,7 @@ fn xz_vli(input: &mut &[u8]) -> Result<u64, DecompressError> {
Err(DecompressError::Corrupt)
}
#[cfg(feature = "lzma")]
#[cfg(feature = "lenient-lzma")]
/// Decode the property bytes and raw stream of a ZIP method-14 member.
pub(crate) fn lzma_zip(input: &[u8], expected_size: u64, has_end_marker: bool, max_out: u64) -> Result<Vec<u8>, DecompressError> {
let cap = cap_of(max_out).min(isize::MAX as usize);
@@ -204,13 +204,13 @@ pub(crate) fn lzma_zip(input: &[u8], expected_size: u64, has_end_marker: bool, m
}
}
#[cfg(feature = "bzip2")]
#[cfg(feature = "lenient-bzip2")]
/// Decompress a bzip2 stream, producing at most `max_out` bytes.
pub(crate) fn bzip2(input: &[u8], max_out: u64) -> Result<Vec<u8>, DecompressError> {
read_capped(bzip2_rs::DecoderReader::new(input), cap_of(max_out))
}
#[cfg(feature = "zstd")]
#[cfg(feature = "lenient-zstd")]
/// Decompress a Zstandard stream, producing at most `max_out` bytes.
pub(crate) fn zstd(mut input: &[u8], max_out: u64) -> Result<Vec<u8>, DecompressError> {
if input.is_empty() {
@@ -235,9 +235,9 @@ pub(crate) fn zstd(mut input: &[u8], max_out: u64) -> Result<Vec<u8>, Decompress
#[cfg(test)]
mod tests {
use super::*;
use crate::testkit as fx;
use crate::lenient::testkit as fx;
#[cfg(feature = "lzma")]
#[cfg(feature = "lenient-lzma")]
#[test]
fn xz_round_trip() {
let c = fx::XZ_FIXTURE;
@@ -250,7 +250,7 @@ mod tests {
}
}
#[cfg(feature = "lzma")]
#[cfg(feature = "lenient-lzma")]
#[test]
fn xz_concatenated_streams_and_padding_share_one_budget() {
let input = [fx::XZ_FIXTURE, &[0; 4], fx::XZ_FIXTURE, &[0; 8]].concat();
@@ -264,7 +264,7 @@ mod tests {
assert!(xz(&empty, 0).unwrap().is_empty());
}
#[cfg(feature = "lzma")]
#[cfg(feature = "lenient-lzma")]
#[test]
fn xz_rejects_incomplete_members_and_nonstandard_padding() {
for padding in [1, 2, 3, 5] {
@@ -284,7 +284,7 @@ mod tests {
}
}
#[cfg(feature = "lzma")]
#[cfg(feature = "lenient-lzma")]
#[test]
fn xz_index_sizes_and_integer_encodings_are_checked() {
assert_eq!(xz_vli(&mut &[0x80, 0][..]), Err(DecompressError::Corrupt));
@@ -295,12 +295,12 @@ mod tests {
let backward = u32::from_le_bytes(damaged[footer + 4..footer + 8].try_into().unwrap());
let index = footer - (backward as usize + 1) * 4;
damaged[index + 2] = 0;
let checksum = crate::crc32(&damaged[index..footer - 4]);
let checksum = crate::lenient::crc32(&damaged[index..footer - 4]);
damaged[footer - 4..footer].copy_from_slice(&checksum.to_le_bytes());
assert!(xz(&damaged, 1000).is_err());
}
#[cfg(feature = "bzip2")]
#[cfg(feature = "lenient-bzip2")]
#[test]
fn bzip2_round_trip() {
let c = fx::BZIP2_FIXTURE;
@@ -311,7 +311,7 @@ mod tests {
}
}
#[cfg(feature = "zstd")]
#[cfg(feature = "lenient-zstd")]
#[test]
fn zstd_round_trip() {
let c = fx::ZSTD_FIXTURE;
@@ -322,7 +322,7 @@ mod tests {
}
}
#[cfg(feature = "zstd")]
#[cfg(feature = "lenient-zstd")]
#[test]
fn zstandard_members_can_contain_multiple_frames() {
let frame = fx::ZSTD_FIXTURE;
@@ -340,11 +340,11 @@ mod tests {
fn garbage_never_panics() {
for seed in 0..64u64 {
let n = fx::noise(seed, 400);
#[cfg(feature = "lzma")]
#[cfg(feature = "lenient-lzma")]
let _ = xz(&n, 1 << 20);
#[cfg(feature = "bzip2")]
#[cfg(feature = "lenient-bzip2")]
let _ = bzip2(&n, 1 << 20);
#[cfg(feature = "zstd")]
#[cfg(feature = "lenient-zstd")]
let _ = zstd(&n, 1 << 20);
let _ = &n;
}
@@ -2,8 +2,8 @@
//! central directory and that entry's bytes are read. For archives too big to hold in memory just
//! to look at one small entry (a JAR's manifest).
use crate::bytes::Reader;
use crate::{self as zip, ZipError};
use crate::lenient::bytes::Reader;
use crate::lenient::{self as zip, ZipError};
use std::io::{Read, Seek, SeekFrom};
/// Most central directory read (some hundreds of thousands of entries); a bigger one is refused.
@@ -159,7 +159,7 @@ mod tests {
#[test]
fn an_entry_is_read_from_the_directory_without_the_whole_file() {
let filler = vec![7u8; 200_000];
let archive = crate::testkit::zip_stored(&[("app/Big.class", &filler), ("META-INF/MANIFEST.MF", MANIFEST), ("META-INF/", b"")]);
let archive = crate::lenient::testkit::zip_stored(&[("app/Big.class", &filler), ("META-INF/MANIFEST.MF", MANIFEST), ("META-INF/", b"")]);
assert_eq!(read(&archive, "META-INF/MANIFEST.MF").unwrap().as_deref(), Some(MANIFEST));
assert_eq!(read(&archive, "META-INF/MISSING").unwrap(), None);
assert_eq!(read(&archive, "META-INF/").unwrap(), None, "a folder isn't an entry to read");
@@ -178,7 +178,7 @@ mod tests {
let names: Vec<String> = (0..3000).map(|i| format!("com/example/generated/Class{i:05}.class")).collect();
let mut files: Vec<(&str, &[u8])> = names.iter().map(|n| (n.as_str(), &b"x"[..])).collect();
files.push(("META-INF/MANIFEST.MF", MANIFEST));
let archive = crate::testkit::zip_stored(&files);
let archive = crate::lenient::testkit::zip_stored(&files);
assert_eq!(read(&archive, "META-INF/MANIFEST.MF").unwrap().as_deref(), Some(MANIFEST));
}
@@ -215,7 +215,7 @@ mod tests {
let deflated = zip_deflated("META-INF/MANIFEST.MF", MANIFEST);
assert_eq!(read(&deflated, "META-INF/MANIFEST.MF").unwrap().as_deref(), Some(MANIFEST));
let mut commented = crate::testkit::zip_stored(&[("META-INF/MANIFEST.MF", MANIFEST)]);
let mut commented = crate::lenient::testkit::zip_stored(&[("META-INF/MANIFEST.MF", MANIFEST)]);
let comment = b"built by PK\x05\x06 tools";
let at = commented.len() - 2;
commented[at..].copy_from_slice(&(comment.len() as u16).to_le_bytes());
@@ -227,7 +227,7 @@ mod tests {
/// the classic fields disagree with it and win, as they do for `zip::parse`.
#[test]
fn a_comment_shaped_like_zip64_records_is_not_read_as_them() {
let mut archive = crate::testkit::zip_stored(&[("META-INF/MANIFEST.MF", MANIFEST)]);
let mut archive = crate::lenient::testkit::zip_stored(&[("META-INF/MANIFEST.MF", MANIFEST)]);
let end = archive.len() - 22;
let last = archive.windows(4).rposition(|w| w == b"PK\x01\x02").unwrap();
let mut fake = b"PK\x06\x06".to_vec();
@@ -250,7 +250,7 @@ mod tests {
/// only the offset), after a stub too.
#[test]
fn zip64_archives_are_read_in_parts() {
let archive = crate::testkit::zip_stored(&[("app/Main.class", b"x"), ("META-INF/MANIFEST.MF", MANIFEST)]);
let archive = crate::lenient::testkit::zip_stored(&[("app/Main.class", b"x"), ("META-INF/MANIFEST.MF", MANIFEST)]);
for saturate in [&[][..], &[8, 10, 12, 16], &[16], &[12], &[10], &[4]] {
let zip64 = as_zip64(&archive, saturate);
assert_eq!(read(&zip64, "META-INF/MANIFEST.MF").unwrap().as_deref(), Some(MANIFEST), "saturated {saturate:?}");
@@ -3,7 +3,7 @@
#![allow(clippy::cast_possible_truncation, dead_code)]
use crate::crc32;
use crate::lenient::crc32;
/// Plaintext of [`XZ_FIXTURE`] and [`LZMA_FIXTURE`].
pub const XZ_PLAIN: &[u8] = b"Hello, xz compression world!\n";
@@ -39,7 +39,7 @@ pub(crate) fn build(files: &[(&str, &[u8])]) -> Vec<u8> {
out
}
#[cfg(all(feature = "bzip2", feature = "lzma"))]
#[cfg(all(feature = "lenient-bzip2", feature = "lenient-lzma"))]
fn build_codec_entry(method: u16, compressed: &[u8], plain: &[u8]) -> Vec<u8> {
let mut file = build(&[("entry", compressed)]);
let central = 30 + "entry".len() + compressed.len();
@@ -55,10 +55,10 @@ fn build_codec_entry(method: u16, compressed: &[u8], plain: &[u8]) -> Vec<u8> {
file
}
#[cfg(all(feature = "bzip2", feature = "lzma"))]
#[cfg(all(feature = "lenient-bzip2", feature = "lenient-lzma"))]
#[test]
fn bzip2_and_xz_zip_entries_keep_size_crc_and_output_checks() {
use crate::testkit::{BZIP2_FIXTURE, BZIP2_PLAIN, XZ_FIXTURE, XZ_PLAIN};
use crate::lenient::testkit::{BZIP2_FIXTURE, BZIP2_PLAIN, XZ_FIXTURE, XZ_PLAIN};
for (method, compressed, plain) in [(12, BZIP2_FIXTURE, BZIP2_PLAIN), (95, XZ_FIXTURE, XZ_PLAIN)] {
let file = build_codec_entry(method, compressed, plain);
let original = parse(&file, 1).unwrap().entries.remove(0);
@@ -80,7 +80,7 @@ fn bzip2_and_xz_zip_entries_keep_size_crc_and_output_checks() {
}
}
#[cfg(feature = "lzma")]
#[cfg(feature = "lenient-lzma")]
fn zip_lzma_member(plain: &[u8], end_marker: bool) -> Vec<u8> {
let mut encoded = Vec::new();
let size = if end_marker { None } else { Some(plain.len() as u64) };
@@ -104,7 +104,7 @@ fn zip_lzma_member(plain: &[u8], end_marker: bool) -> Vec<u8> {
file
}
#[cfg(feature = "lzma")]
#[cfg(feature = "lenient-lzma")]
#[test]
fn zip_lzma_reads_size_delimited_and_end_marked_streams() {
for end_marker in [false, true] {
@@ -129,7 +129,7 @@ fn zip_lzma_reads_size_delimited_and_end_marked_streams() {
}
}
#[cfg(feature = "lzma")]
#[cfg(feature = "lenient-lzma")]
#[test]
fn zip_lzma_rejects_bad_properties_without_unbounded_dictionaries() {
let mut file = zip_lzma_member(b"small", true);
@@ -286,7 +286,7 @@ fn lists_and_reads_stored_entries() {
assert!(matches!(read_entry(&z, &a.entries[1], 3), Err(ZipError::TooLarge(3))));
}
#[cfg(feature = "zstd")]
#[cfg(feature = "lenient-zstd")]
#[test]
fn zstandard_members_are_read_with_their_limits_and_checks() {
// A raw Zstandard frame holding "hello zip" (single segment, one raw block).
@@ -373,7 +373,7 @@ fn crc_known_value() {
#[cfg(feature = "serde")]
#[test]
fn listings_serialize_without_empty_warnings_or_unix_mode() {
let data = crate::testkit::zip_stored(&[("a.txt", b"hi")]);
let data = crate::lenient::testkit::zip_stored(&[("a.txt", b"hi")]);
let archive = parse(&data, 10).unwrap();
let value = serde_json::to_value(&archive).unwrap();
assert!(value.get("warnings").is_none());
@@ -381,10 +381,10 @@ fn listings_serialize_without_empty_warnings_or_unix_mode() {
assert!(value["entries"][0].get("unix_mode").is_none());
}
#[cfg(not(feature = "zstd"))]
#[cfg(not(feature = "lenient-zstd"))]
#[test]
fn a_method_left_out_of_the_build_is_unsupported() {
let mut data = crate::testkit::zip_stored(&[("a", b"abc")]);
let mut data = crate::lenient::testkit::zip_stored(&[("a", b"abc")]);
let archive = parse(&data, 10).unwrap();
let mut entry = archive.entries[0].clone();
entry.method = 93;
+5
View File
@@ -13,6 +13,9 @@
//! timestamp on every entry so equal input gives equal bytes.
//! - [`preflight`] is a stricter check of the whole end-record and central-directory structure
//! on the raw bytes, for a caller that wants nothing ambiguous to reach a ZIP reader at all.
//! - `lenient` (feature `lenient`) is the other way round: a reader of the raw bytes without
//! `zip`, for inspectors that list and describe damaged or unusual archives instead of refusing
//! them, with bounded, CRC-checked entry reads.
//!
//! Every limit is the caller's number. Entry-name rules (which names are safe, which spellings
//! are one part) stay with the format that knows them.
@@ -23,6 +26,8 @@ struct ReadmeExamples;
mod archive;
mod guard;
#[cfg(feature = "lenient")]
pub mod lenient;
mod metadata_reader;
pub mod preflight;
mod seekable;
@@ -2,7 +2,7 @@
#![allow(dead_code, clippy::cast_possible_truncation)]
use craft_unzip::crc32;
use craft_zip::lenient::crc32;
/// A stored-only ZIP of `files`.
pub fn zip_stored(files: &[(&str, &[u8])]) -> Vec<u8> {
@@ -1,5 +1,7 @@
#![cfg(feature = "lenient")]
// From Craft Anything crates/formats/tests/zip64_end_record_compatibility.rs.
use craft_unzip::{self as zip, ZipArchive, ZipError};
use craft_zip::lenient::{self as zip, ZipArchive, ZipError};
#[path = "lenient_common/mod.rs"]
mod common;
use common::zip_stored;
@@ -1,7 +1,9 @@
#![cfg(feature = "lenient")]
// From Craft Anything crates/formats/tests/zip64_extra_boundaries.rs.
#[path = "lenient_common/mod.rs"]
mod common;
use craft_unzip as zip;
use craft_zip::lenient as zip;
fn archive(extra: &[u8], sentinel: usize) -> Vec<u8> {
let mut data = common::zip_stored(&[("a", b"x")]);
@@ -1,7 +1,9 @@
#![cfg(feature = "lenient")]
// From Craft Anything crates/formats/tests/zip_decoded_size.rs.
#[path = "lenient_common/mod.rs"]
mod common;
use craft_unzip::{self as zip, ZipEntry, ZipError};
use craft_zip::lenient::{self as zip, ZipEntry, ZipError};
fn deflated(body: &[u8]) -> (Vec<u8>, ZipEntry) {
let compressed = miniz_oxide::deflate::compress_to_vec(body, 6);
@@ -1,5 +1,7 @@
#![cfg(feature = "lenient")]
// From Craft Anything crates/formats/tests/zip_directory_bounds.rs.
use craft_unzip::{self as zip, ZipError};
use craft_zip::lenient::{self as zip, ZipError};
#[path = "lenient_common/mod.rs"]
mod common;
use common::zip_stored;
@@ -1,5 +1,7 @@
#![cfg(feature = "lenient")]
// From Craft Anything crates/formats/tests/zip_entry_detection.rs.
use craft_unzip::has_entry as zip_has_entry;
use craft_zip::lenient::has_entry as zip_has_entry;
#[path = "lenient_common/mod.rs"]
mod common;
use common::zip_stored;
@@ -1,5 +1,7 @@
#![cfg(feature = "lenient")]
// From Craft Anything crates/formats/tests/zip_split_volumes.rs.
use craft_unzip::{self as zip, ZipError};
use craft_zip::lenient::{self as zip, ZipError};
#[path = "lenient_common/mod.rs"]
mod common;
use common::zip_stored;
@@ -1,7 +1,9 @@
#![cfg(feature = "lenient")]
// From Craft Anything crates/formats/tests/zip_stored_cap.rs.
#[path = "lenient_common/mod.rs"]
mod common;
use craft_unzip::{self as zip, ZipError};
use craft_zip::lenient::{self as zip, ZipError};
#[test]
fn a_stored_entry_cannot_understate_its_size_to_bypass_the_output_limit() {