From 348675c5387a11a8992a99428b7609bd78047c58 Mon Sep 17 00:00:00 2001 From: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:52:19 +0200 Subject: [PATCH 1/2] Packs: open streamed archives the way Java's ZipFile does A resource pack written while it streams (Nexo's server behind resourcepack.gommehd.io, gommehd.net's required pack) leaves every local header a stub, every uncompressed size at 0xFFFFFFFF and the END record's entry counts and disk number at 0 and 0xFFFF. Java tolerates all three; the zip crate's ZipArchive refuses the END record as multi-disk and empty, so a required server pack failed its reload and the join never got through (each retry also rebuilt the renderer, once a second). Count the real central-directory entries and normalize the END record's disk and count fields when they disagree with it, and bound the inflate output when an entry's size is the 0xFFFFFFFF sentinel. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- crates/horizon-client/src/zip.rs | 108 ++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/zip.rs b/crates/horizon-client/src/zip.rs index 22cab132d..17442858e 100644 --- a/crates/horizon-client/src/zip.rs +++ b/crates/horizon-client/src/zip.rs @@ -23,7 +23,8 @@ impl Zip { pub fn open(path: &Path) -> io::Result { Self::from_bytes(fs::read(path)?) } - pub fn from_bytes(data: Vec) -> io::Result { + pub fn from_bytes(mut data: Vec) -> io::Result { + normalize_end(&mut data); let archive = ZipArchive::new(Cursor::new(Arc::from(data))).map_err(invalid)?; let entries = (0..archive.len()) .filter_map(|i| Some((archive.name_for_index(i)?.replace('\\', "/"), i))) @@ -51,7 +52,14 @@ impl Zip { match method { CompressionMethod::Stored => Ok(raw), CompressionMethod::Deflated => { - miniz_oxide::inflate::decompress_to_vec_with_limit(&raw, size.max(1) * 2 + 1024) + // Streamed archives write 0xFFFFFFFF for the uncompressed + // size; bound the output instead of trusting it. + let limit = if size == u32::MAX as usize { + 1 << 30 + } else { + size.max(1) * 2 + 1024 + }; + miniz_oxide::inflate::decompress_to_vec_with_limit(&raw, limit) .map_err(|_| invalid("corrupt deflate stream")) } _ => Err(invalid("unsupported zip compression")), @@ -59,6 +67,59 @@ impl Zip { } } +/// The 22 fixed bytes of the end-of-central-directory record. +const END_HEADER: usize = 22; + +/// Java's `ZipFile` ignores the END record's disk numbers and file counts, +/// reading the central directory from `cenpos` to `cenpos + cenlen` whatever +/// they say; the `zip` crate insists the record agrees with the directory. +/// Packs written while streaming (Nexo serves them for gommehd.net, for one) +/// leave every file's local header a stub, the uncompressed sizes at +/// 0xFFFFFFFF and the END counts and disk number at 0 and 0xFFFF — archives +/// Java opens but the crate refuses. Count the real directory entries and +/// normalize the record so it does not look multi-disk or empty. +fn normalize_end(data: &mut [u8]) { + if data.len() < END_HEADER { + return; + } + let u16_at = |data: &[u8], at: usize| u16::from_le_bytes(data[at..at + 2].try_into().unwrap()); + let u32_at = |data: &[u8], at: usize| u32::from_le_bytes(data[at..at + 4].try_into().unwrap()); + // The END record's comment can hide the signature: scan back through the + // last 64 KiB, like every zip reader. + let start = data.len().saturating_sub(END_HEADER + u16::MAX as usize); + let mut end = None; + for pos in (start..=data.len().saturating_sub(END_HEADER)).rev() { + if data[pos..pos + 4] == [0x50, 0x4b, 0x05, 0x06] + && pos + END_HEADER + u16_at(data, pos + 20) as usize <= data.len() + { + end = Some(pos); + break; + } + } + let Some(end) = end else { return }; + // The directory's real position and length, as Java derives them; the + // recorded offset is relative to the archive's start. + let cenlen = u32_at(data, end + 12) as usize; + let cenpos = end.saturating_sub(cenlen); + let mut count = 0u16; + let mut p = cenpos; + while p + 46 <= end && data[p..p + 4] == [0x50, 0x4b, 0x01, 0x02] { + count = count.saturating_add(1); + p += 46 + + u16_at(data, p + 28) as usize + + u16_at(data, p + 30) as usize + + u16_at(data, p + 32) as usize; + } + let disk = u16_at(data, end + 4); + let with_directory = u16_at(data, end + 6); + if disk == with_directory && u16_at(data, end + 8) == count && u16_at(data, end + 10) == count { + return; + } + data[end + 4..end + 10].fill(0); + data[end + 8..end + 10].copy_from_slice(&count.to_le_bytes()); + data[end + 10..end + 12].copy_from_slice(&count.to_le_bytes()); +} + #[cfg(test)] mod tests { use super::*; @@ -207,4 +268,47 @@ mod tests { cut.truncate(cut.len() / 2); assert!(Zip::from_bytes(cut).is_err()); } + + /// A pack as a streaming writer serves it (Nexo/gommehd.net): every + /// local header is a stub, the uncompressed sizes are 0xFFFFFFFF and the + /// END record's counts and disk numbers are wrong. Java's ZipFile opens + /// it anyway; so do we. + #[test] + fn reads_a_streamed_archive() { + let files: [(&str, &[u8], bool); 2] = [ + ("pack.mcmeta", b"{\"pack\":{}}", true), + ("a/b.txt", b"streamed entry", true), + ]; + let mut out = Vec::new(); + let mut central = Vec::new(); + for (name, body, _) in files { + let data = miniz_oxide::deflate::compress_to_vec(body, 6); + let offset = out.len() as u32; + out.extend(0x0403_4b50u32.to_le_bytes()); + out.extend([0; 26]); + out.extend(&data); + central.extend(0x0201_4b50u32.to_le_bytes()); + central.extend([20, 0, 20, 0, 0, 0]); + central.extend(8u16.to_le_bytes()); + central.extend([0; 8]); + central.extend((data.len() as u32).to_le_bytes()); + central.extend(u32::MAX.to_le_bytes()); + central.extend((name.len() as u16).to_le_bytes()); + central.extend([0; 12]); + central.extend(offset.to_le_bytes()); + central.extend(name.as_bytes()); + } + let start = out.len() as u32; + out.extend(¢ral); + out.extend(0x0605_4b50u32.to_le_bytes()); + out.extend(u16::MAX.to_le_bytes()); + out.extend([0; 6]); + out.extend((central.len() as u32).to_le_bytes()); + out.extend(start.to_le_bytes()); + out.extend([0; 2]); + let zip = Zip::from_bytes(out).unwrap(); + assert_eq!(zip.names().collect::>(), ["a/b.txt", "pack.mcmeta"]); + assert_eq!(zip.read("pack.mcmeta").unwrap(), b"{\"pack\":{}}"); + assert_eq!(zip.read("a/b.txt").unwrap(), b"streamed entry"); + } } From e7f85e5bfd55783f91348a7f9c6701d201e1cda3 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 10:44:31 +0100 Subject: [PATCH 2/2] Zip: test the streamed shapes Java 25's ZipFile opens ZipFile refuses a 0xFFFFFFFF size without its Zip64 extra field ("invalid zip64 extra len size"), so the test builds both shapes it accepts: real sizes, and the sentinel with the extra (the zip crate reads the real size from it). With every accepted shape carrying a real size, the inflate limit needs no special case. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/zip.rs | 105 ++++++++++++++++--------------- 1 file changed, 56 insertions(+), 49 deletions(-) diff --git a/crates/horizon-client/src/zip.rs b/crates/horizon-client/src/zip.rs index 17442858e..7f8c2a614 100644 --- a/crates/horizon-client/src/zip.rs +++ b/crates/horizon-client/src/zip.rs @@ -52,14 +52,7 @@ impl Zip { match method { CompressionMethod::Stored => Ok(raw), CompressionMethod::Deflated => { - // Streamed archives write 0xFFFFFFFF for the uncompressed - // size; bound the output instead of trusting it. - let limit = if size == u32::MAX as usize { - 1 << 30 - } else { - size.max(1) * 2 + 1024 - }; - miniz_oxide::inflate::decompress_to_vec_with_limit(&raw, limit) + miniz_oxide::inflate::decompress_to_vec_with_limit(&raw, size.max(1) * 2 + 1024) .map_err(|_| invalid("corrupt deflate stream")) } _ => Err(invalid("unsupported zip compression")), @@ -74,10 +67,10 @@ const END_HEADER: usize = 22; /// reading the central directory from `cenpos` to `cenpos + cenlen` whatever /// they say; the `zip` crate insists the record agrees with the directory. /// Packs written while streaming (Nexo serves them for gommehd.net, for one) -/// leave every file's local header a stub, the uncompressed sizes at -/// 0xFFFFFFFF and the END counts and disk number at 0 and 0xFFFF — archives -/// Java opens but the crate refuses. Count the real directory entries and -/// normalize the record so it does not look multi-disk or empty. +/// leave every file's local header a stub and the END counts and disk number +/// at 0 and 0xFFFF: archives Java opens but the crate refuses. Count the real +/// directory entries and normalize the record so it does not look multi-disk +/// or empty. fn normalize_end(data: &mut [u8]) { if data.len() < END_HEADER { return; @@ -270,45 +263,59 @@ mod tests { } /// A pack as a streaming writer serves it (Nexo/gommehd.net): every - /// local header is a stub, the uncompressed sizes are 0xFFFFFFFF and the - /// END record's counts and disk numbers are wrong. Java's ZipFile opens - /// it anyway; so do we. + /// local header is a 30-byte stub and the END record's disk number and + /// counts are wrong. Java 25's `ZipFile` opens it, with each size either + /// in the directory or as 0xFFFFFFFF plus its Zip64 extra field (without + /// that field it refuses the archive: "invalid zip64 extra len size"). #[test] fn reads_a_streamed_archive() { - let files: [(&str, &[u8], bool); 2] = [ - ("pack.mcmeta", b"{\"pack\":{}}", true), - ("a/b.txt", b"streamed entry", true), - ]; - let mut out = Vec::new(); - let mut central = Vec::new(); - for (name, body, _) in files { - let data = miniz_oxide::deflate::compress_to_vec(body, 6); - let offset = out.len() as u32; - out.extend(0x0403_4b50u32.to_le_bytes()); - out.extend([0; 26]); - out.extend(&data); - central.extend(0x0201_4b50u32.to_le_bytes()); - central.extend([20, 0, 20, 0, 0, 0]); - central.extend(8u16.to_le_bytes()); - central.extend([0; 8]); - central.extend((data.len() as u32).to_le_bytes()); - central.extend(u32::MAX.to_le_bytes()); - central.extend((name.len() as u16).to_le_bytes()); - central.extend([0; 12]); - central.extend(offset.to_le_bytes()); - central.extend(name.as_bytes()); + for zip64 in [false, true] { + let files: [(&str, &[u8]); 2] = [ + ("pack.mcmeta", b"{\"pack\":{}}"), + ("a/b.txt", b"streamed entry"), + ]; + let mut out = Vec::new(); + let mut central = Vec::new(); + for (name, body) in files { + let data = miniz_oxide::deflate::compress_to_vec(body, 6); + let offset = out.len() as u32; + out.extend(0x0403_4b50u32.to_le_bytes()); + out.extend([0; 26]); + out.extend(&data); + let extra: Vec = if zip64 { + [1u16, 8] + .iter() + .flat_map(|v| v.to_le_bytes()) + .chain((body.len() as u64).to_le_bytes()) + .collect() + } else { + Vec::new() + }; + central.extend(0x0201_4b50u32.to_le_bytes()); + central.extend([20, 0, 20, 0, 0, 0]); + central.extend(8u16.to_le_bytes()); + central.extend([0; 8]); + central.extend((data.len() as u32).to_le_bytes()); + central.extend(if zip64 { u32::MAX } else { body.len() as u32 }.to_le_bytes()); + central.extend((name.len() as u16).to_le_bytes()); + central.extend((extra.len() as u16).to_le_bytes()); + central.extend([0; 10]); + central.extend(offset.to_le_bytes()); + central.extend(name.as_bytes()); + central.extend(extra); + } + let start = out.len() as u32; + out.extend(¢ral); + out.extend(0x0605_4b50u32.to_le_bytes()); + out.extend(u16::MAX.to_le_bytes()); + out.extend([0; 6]); + out.extend((central.len() as u32).to_le_bytes()); + out.extend(start.to_le_bytes()); + out.extend([0; 2]); + let zip = Zip::from_bytes(out).unwrap(); + assert_eq!(zip.names().collect::>(), ["a/b.txt", "pack.mcmeta"]); + assert_eq!(zip.read("pack.mcmeta").unwrap(), b"{\"pack\":{}}"); + assert_eq!(zip.read("a/b.txt").unwrap(), b"streamed entry"); } - let start = out.len() as u32; - out.extend(¢ral); - out.extend(0x0605_4b50u32.to_le_bytes()); - out.extend(u16::MAX.to_le_bytes()); - out.extend([0; 6]); - out.extend((central.len() as u32).to_le_bytes()); - out.extend(start.to_le_bytes()); - out.extend([0; 2]); - let zip = Zip::from_bytes(out).unwrap(); - assert_eq!(zip.names().collect::>(), ["a/b.txt", "pack.mcmeta"]); - assert_eq!(zip.read("pack.mcmeta").unwrap(), b"{\"pack\":{}}"); - assert_eq!(zip.read("a/b.txt").unwrap(), b"streamed entry"); } }