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:
parent
58adf0cc99
commit
6e4b371046
2 changed files with 185 additions and 9 deletions
|
|
@ -1,3 +1,4 @@
|
|||
use flate2::read::MultiGzDecoder;
|
||||
use pheon::review::report::{ReportInputs, ReportOptions, failure_report};
|
||||
use pheon::review::{
|
||||
Comparison, Diagnostic, Document, Outcome, Side, dump_records,
|
||||
|
|
@ -8,7 +9,7 @@ use std::{
|
|||
collections::BTreeMap,
|
||||
ffi::{OsStr, OsString},
|
||||
fs,
|
||||
io::{self, Write},
|
||||
io::{self, Read, Write},
|
||||
path::PathBuf,
|
||||
process::ExitCode,
|
||||
};
|
||||
|
|
@ -21,9 +22,36 @@ const HELP: &str = "Usage:
|
|||
|
||||
Structural comparison: ignore timestamps and element order, normalize polygon start/winding,
|
||||
preserve duplicates, compare all retained fields exactly. No geometric XOR or tolerance.
|
||||
Gzip-compressed GDSII input is recognized by its header.
|
||||
Exit status: 0 equal/success, 1 different, 2 invalid/unsupported/incomplete.
|
||||
";
|
||||
|
||||
fn read_input(path: &OsStr) -> Result<Vec<u8>, Diagnostic> {
|
||||
let io_error =
|
||||
|error: io::Error| Diagnostic::new("io_error", format!("{path:?}: {error}"), None);
|
||||
let mut file = fs::File::open(path).map_err(io_error)?;
|
||||
let mut bytes = Vec::new();
|
||||
(&mut file)
|
||||
.take(2)
|
||||
.read_to_end(&mut bytes)
|
||||
.map_err(io_error)?;
|
||||
if bytes == b"\x1f\x8b" {
|
||||
let mut decoded = Vec::new();
|
||||
MultiGzDecoder::new(bytes.as_slice().chain(file))
|
||||
.read_to_end(&mut decoded)
|
||||
.map_err(|error| {
|
||||
Diagnostic::new(
|
||||
"invalid_input",
|
||||
format!("{path:?}: gzip decompression failed: {error}"),
|
||||
None,
|
||||
)
|
||||
})?;
|
||||
return Ok(decoded);
|
||||
}
|
||||
file.read_to_end(&mut bytes).map_err(io_error)?;
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
fn value(args: &[OsString], index: &mut usize) -> Result<String, String> {
|
||||
*index += 1;
|
||||
args.get(*index)
|
||||
|
|
@ -61,7 +89,7 @@ fn name_layers(args: &[OsString]) -> Result<u8, String> {
|
|||
if names.is_empty() {
|
||||
return Err("provide at least one --name".to_string());
|
||||
}
|
||||
let input = fs::read(&args[0]).map_err(|e| format!("{:?}: {e}", args[0]))?;
|
||||
let input = read_input(&args[0]).map_err(|error| error.message)?;
|
||||
let output = write_layer_names(
|
||||
&input,
|
||||
&cell.ok_or("provide --cell")?,
|
||||
|
|
@ -101,8 +129,9 @@ fn prepare_comparison(
|
|||
) -> Result<Comparison, Diagnostic> {
|
||||
let read = |path: Option<&OsStr>, side| {
|
||||
path.map(|path| {
|
||||
fs::read(path).map_err(|error| {
|
||||
Diagnostic::new("io_error", format!("{path:?}: {error}"), Some(side))
|
||||
read_input(path).map_err(|mut diagnostic| {
|
||||
diagnostic.side = Some(side);
|
||||
diagnostic
|
||||
})
|
||||
})
|
||||
.transpose()
|
||||
|
|
@ -267,7 +296,7 @@ fn run(args: &[OsString]) -> Result<u8, String> {
|
|||
{
|
||||
return Err("format must be ascii or mermaid (diff also accepts none)".to_string());
|
||||
}
|
||||
let read = |path: &OsString| fs::read(path).map_err(|e| format!("{path:?}: {e}"));
|
||||
let read = |path: &OsString| read_input(path).map_err(|error| error.message);
|
||||
if raw {
|
||||
if scope != Scope::default() {
|
||||
return Err("record dumps do not accept object scope selectors".to_string());
|
||||
|
|
|
|||
|
|
@ -500,6 +500,16 @@ fn object<'a>(records: &[Record<'a>], issues: &mut Vec<Diagnostic>) -> Result<Ob
|
|||
}
|
||||
|
||||
fn read_records(input: &[u8]) -> Result<Vec<Record<'_>>, String> {
|
||||
if input.starts_with(b"\x1f\x8b") {
|
||||
return Err(error(
|
||||
0,
|
||||
"gzip-compressed input. Decompress it before reading GDSII",
|
||||
));
|
||||
}
|
||||
if input.starts_with(b"%SEMI-OASIS\r\n") {
|
||||
return Err(error(0, "OASIS input. Export the layout as GDSII"));
|
||||
}
|
||||
|
||||
let mut records = Vec::new();
|
||||
let mut offset = 0;
|
||||
while offset < input.len() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue