Read gzip-compressed GDSII directly in the CLI

Detect gzip headers on every CLI input and decode all members before parsing.
Keep report offsets relative to the decompressed stream and report corrupt
archives as invalid input. Recognize OASIS headers and explain how to provide
GDSII data.

Cover mixed compressed and plain comparisons, corrupt trailers, and layer-name
output from compressed input.
This commit is contained in:
Jan Petykiewicz 2026-11-17 16:14:53 -08:00
commit 6e4b371046

View file

@ -43,6 +43,14 @@ fn layout(cells: &[(&str, Vec<Vec<u8>>)]) -> Vec<u8> {
bytes
}
fn gzip(data: &[u8]) -> Vec<u8> {
use std::io::Write;
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
encoder.write_all(data).unwrap();
encoder.finish().unwrap()
}
fn polygon(points: &[[i32; 2]]) -> Vec<u8> {
let mut bytes = Vec::new();
record(&mut bytes, RTAG_BOUNDARY, &[]);
@ -283,6 +291,21 @@ fn ten_fixture_pairs_have_explicit_expected_changes() {
}
}
#[test]
fn review_identifies_gzip_and_oasis_input() {
for (bytes, expected) in [
(&b"\x1f\x8b\x08\x00"[..], "gzip-compressed input"),
(&b"%SEMI-OASIS\r\n"[..], "OASIS input"),
] {
let error = Document::parse(bytes).unwrap_err();
assert!(
error.starts_with(&format!("at byte 0: {expected}")),
"{error}"
);
assert_eq!(pheon::review::dump_records(bytes).unwrap_err(), error);
}
}
#[test]
fn canonical_polygon_dump_is_readable_and_offsets_are_separate() {
let bytes = layout(&[("CHIP", vec![square()])]);
@ -691,6 +714,73 @@ fn cli_uses_documented_exit_codes_and_accepts_paths_with_spaces() {
);
}
#[test]
fn cli_decodes_gzip_by_header_and_checks_the_trailer() {
let directory = std::env::temp_dir().join(format!("pheon gzip {}", std::process::id()));
fs::create_dir_all(&directory).unwrap();
struct Cleanup(PathBuf);
impl Drop for Cleanup {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.0);
}
}
let _cleanup = Cleanup(directory.clone());
let plain = directory.join("plain.gds");
let compressed = directory.join("compressed.gds");
let data = shared(square());
// Splitting a record between gzip members must preserve the decompressed layout.
let mut encoded = [gzip(&data[..7]), gzip(&data[7..])].concat();
fs::write(&plain, &data).unwrap();
fs::write(&compressed, &encoded).unwrap();
let command = || Command::new(env!("CARGO_BIN_EXE_pheon"));
let dump = |path: &PathBuf| {
let output = command()
.arg("dump")
.arg(path)
.arg("--records")
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
output.stdout
};
assert_eq!(dump(&plain), dump(&compressed));
let compare = || {
let output = command()
.arg("diff")
.arg(&plain)
.arg(&compressed)
.args(["--format", "json"])
.output()
.unwrap();
let report: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
(output.status.code(), report)
};
let (code, report) = compare();
assert_eq!(code, Some(0));
assert_eq!(report["result"], "equal");
// Corrupt the last CRC after all the GDSII bytes have already been decoded.
let crc = encoded.len() - 8;
encoded[crc] ^= 0xff;
fs::write(&compressed, encoded).unwrap();
let (code, report) = compare();
assert_eq!(code, Some(2));
assert_eq!(report["status"], "failed");
let diagnostic = &report["diagnostics"][0];
assert_eq!(diagnostic["code"], "invalid_input");
assert_eq!(diagnostic["side"], "new");
assert!(
diagnostic["message"]
.as_str()
.unwrap()
.contains("gzip decompression failed")
);
}
#[test]
fn metadata_writer_preserves_unrelated_bytes_and_consolidates_updates() {
use pheon::review::names::write_layer_names;
@ -776,9 +866,9 @@ fn metadata_writer_rejects_ambiguous_carriers_versions_and_bad_names() {
fn metadata_cli_creates_a_separate_file_and_refuses_existing_output() {
let directory = std::env::temp_dir().join(format!("pheon-metadata-{}", std::process::id()));
fs::create_dir_all(&directory).unwrap();
let source = directory.join("source.gds");
let source = directory.join("source.gds.gz");
let output = directory.join("output.gds");
let bytes = shared(square());
let bytes = gzip(&shared(square()));
fs::write(&source, &bytes).unwrap();
let _ = fs::remove_file(&output);
let mut command = Command::new(env!("CARGO_BIN_EXE_pheon"));