Files
indd-utils/src/lib.rs
T
aiongg 5543af5867 feat: add indd audit and a corpus-wide audit ranking
indd audit converts a document while recording which objects, chunks and attribute IDs the converter reads, then lists what it did not read: classes, chunks, byte-stream objects, attribute IDs per kind of list, codes of known attributes without an IDML value, and strand run kinds, with counts and the conversion's warnings. --tsv gives rows for scripts. tools/audit_corpus.py audits every distinct corpus file and ranks the items by files affected. Without the audit, recording does nothing and conversion output is unchanged.
2026-10-07 08:48:34 +00:00

118 lines
3.8 KiB
Rust

//! Reader for INDD files, the native document format of Adobe® InDesign®.
//!
//! The format is undocumented. Everything this crate knows about it is
//! recorded in `docs/format/`; see `CLEANROOM.md` for how it was learned.
pub mod audit;
pub mod container;
pub mod database;
pub mod header;
pub mod idml;
pub mod model;
pub mod object;
pub use container::{Container, ContigObject, MasterPage};
pub use database::{Database, Entry};
pub use header::{ByteOrder, Header, Version};
pub use object::{Chunk, Cursor, Object};
#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
/// The file does not start with the INDD signature.
NotIndd,
/// The file is shorter than its header or its database pages say.
Truncated {
needed: usize,
got: usize,
},
UnknownByteOrder(u8),
/// A contiguous object's trailer does not match its header.
BadContigObject {
offset: usize,
},
/// The database structures are inconsistent.
Corrupt(String),
/// A valid file using a feature this crate does not read yet.
Unsupported(&'static str),
/// The file has the master pages but not the object database they
/// point to (a file stripped to its metadata).
NoDatabase {
db_pages: u32,
directory: u32,
},
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Io(e) => write!(f, "{e}"),
Error::NotIndd => write!(f, "not an INDD file"),
Error::Truncated { needed, got } => {
write!(f, "file truncated: need {needed} bytes, got {got}")
}
Error::UnknownByteOrder(b) => write!(f, "unknown byte-order flag {b:#04x}"),
Error::BadContigObject { offset } => {
write!(
f,
"contiguous object at {offset:#x} has no matching trailer"
)
}
Error::Corrupt(msg) => write!(f, "corrupt database: {msg}"),
Error::Unsupported(what) => write!(f, "not supported yet: {what}"),
Error::NoDatabase {
db_pages,
directory,
} => write!(
f,
"no object database: the page directory is page {directory}, \
but the file has only {db_pages} database pages"
),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
}
}
/// Read only the header of the file at `path`.
pub fn read_header(path: impl AsRef<std::path::Path>) -> Result<Header, Error> {
use std::io::Read;
let mut buf = [0u8; header::PROBE_LEN];
let mut f = std::fs::File::open(path)?;
let mut got = 0;
while got < buf.len() {
match f.read(&mut buf[got..])? {
0 => break,
n => got += n,
}
}
Header::parse(&buf[..got])
}
/// Convert INDD bytes to an IDML package written to `out`. `name` is the
/// document name recorded in the package (normally the INDD file name).
/// Returns warnings about content that could not be converted.
pub fn convert(indd: &[u8], name: &str, out: impl std::io::Write) -> Result<Vec<String>, Error> {
let container = Container::parse(indd)?;
let _order = object::use_byte_order(container.header.byte_order);
let _tag = object::use_string_tag(object::string_tag_for(container.header.version));
let db = container.database()?;
let doc = model::Reader::new(&db).document(container.header.version)?;
let mut warnings = doc.warnings.clone();
warnings.extend(idml::write(&doc, name, out)?);
Ok(warnings)
}