diff --git a/Cargo.lock b/Cargo.lock index e5abbca..adde828 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -776,6 +776,12 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084" +[[package]] +name = "md5" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae960838283323069879657ca3de837e9f7bbb4c7bf6ea7f1b290d5e9476d2e0" + [[package]] name = "memchr" version = "2.8.3" @@ -1433,6 +1439,7 @@ dependencies = [ "hyphenate", "libc", "lopdf", + "md5", "png 0.18.1", "rusttype", "serde", diff --git a/crates/texrra-tex-core/Cargo.toml b/crates/texrra-tex-core/Cargo.toml index e6c53f4..1a1f2a3 100644 --- a/crates/texrra-tex-core/Cargo.toml +++ b/crates/texrra-tex-core/Cargo.toml @@ -8,6 +8,7 @@ description = "Experimental incremental TeX-compatible typesetting core (route D [dependencies] flate2 = "1.1" lopdf = "0.26" +md5 = "0.8" png = "0.18.1" rusttype = "0.8.3" serde = { version = "1.0", features = ["derive", "rc"] } diff --git a/crates/texrra-tex-core/src/execution/engine.rs b/crates/texrra-tex-core/src/execution/engine.rs index 4a6e7d9..ed8a6a2 100644 --- a/crates/texrra-tex-core/src/execution/engine.rs +++ b/crates/texrra-tex-core/src/execution/engine.rs @@ -977,6 +977,28 @@ impl Executor { self.print_tokens(&value, token)?; } } + Pdfmdfivesum => { + let file = self.keyword("file", files)?; + let body = self.scan_pdf_tokens(files)?; + let digest = if file { + let name = self.token_list_text(&body); + let name = self.decode_filename(name)?.replace('"', ""); + let from = self.source_name(token.span); + self.reads.insert(Dependency::FileLookup { + name: name.clone(), + from: from.clone(), + }); + files + .optional_tex_input(&name, &from) + .map_err(|message| self.error(message, token.span))? + .map(|resource| md5::compute(&resource.data)) + } else { + Some(md5::compute(self.pdf_token_bytes(&body)?)) + }; + if let Some(digest) = digest { + self.print_tokens(&format!("{digest:X}"), token)?; + } + } Pdftexrevision => self.print_tokens(Self::PDFTEX_REVISION, token)?, Etexrevision => self.print_tokens(".6", token)?, Csname => { diff --git a/crates/texrra-tex-core/src/execution/format_image.rs b/crates/texrra-tex-core/src/execution/format_image.rs index 2a3cab5..9b3e835 100644 --- a/crates/texrra-tex-core/src/execution/format_image.rs +++ b/crates/texrra-tex-core/src/execution/format_image.rs @@ -8,8 +8,8 @@ use std::{ sync::{Arc, Mutex, OnceLock, Weak}, }; -const SCHEMA: u32 = 44; -const COMPATIBLE_SCHEMAS: [u32; 13] = [27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, SCHEMA]; +const SCHEMA: u32 = 45; +const COMPATIBLE_SCHEMAS: [u32; 14] = [27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, SCHEMA]; const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION")); #[derive(serde::Serialize, serde::Deserialize)] diff --git a/crates/texrra-tex-core/src/execution/mod.rs b/crates/texrra-tex-core/src/execution/mod.rs index 0cf7477..f117469 100644 --- a/crates/texrra-tex-core/src/execution/mod.rs +++ b/crates/texrra-tex-core/src/execution/mod.rs @@ -250,6 +250,15 @@ pub trait SourceProvider { ) -> Result, String> { Ok(None) } + /// Read exact bytes through the normal TeX input policy. Missing inputs + /// return None. Denied or unavailable host operations remain errors. + fn optional_tex_input( + &mut self, + _name: &str, + _from: &str, + ) -> Result, String> { + Err("host optional TeX binary input unavailable".into()) + } /// Look up the byte length of a real TeX input resource. fn file_size(&mut self, _name: &str, _from: &str) -> Result, String> { Err("host file size lookup unavailable".into()) diff --git a/crates/texrra-tex-core/src/execution/state.rs b/crates/texrra-tex-core/src/execution/state.rs index 7337f51..e5a7c43 100644 --- a/crates/texrra-tex-core/src/execution/state.rs +++ b/crates/texrra-tex-core/src/execution/state.rs @@ -89,6 +89,7 @@ pub enum Primitive { Etexrevision, Pdffilesize, Pdffilemoddate, + Pdfmdfivesum, Pdfximage, Pdfrefximage, Pdflastximage, @@ -389,6 +390,7 @@ impl Primitive { | Etexrevision | Pdffilesize | Pdffilemoddate + | Pdfmdfivesum | Jobname | Pdfcreationdate | Pdftexrevision @@ -970,6 +972,7 @@ impl Default for State { ("end", End), ("pdffilesize", Pdffilesize), ("pdffilemoddate", Pdffilemoddate), + ("pdfmdfivesum", Pdfmdfivesum), ("pdfximage", Pdfximage), ("pdfrefximage", Pdfrefximage), ("pdflastximage", Pdflastximage), diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index 8f86974..e2e7dc0 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -1354,6 +1354,28 @@ DVI 的坐标文件在 PDF 驱动报告 DVI 导出限制前完成,原生 DVI - `latex-properties/receipt.json` 为 `be9892b107d9927b2fbd93afb574da32e9b23a2300c1424ea61926a05e44bcf5`。 - `acm-acmtog/receipt.json` 为 `5c7188f5f9134894f5b3e863eab89b4c5cadf3f013cac032020e3a0678d87715`。 +### PDF 文本与文件摘要 + +`\pdfmdfivesum` 按 PDF 扩展规则扫描并展开实参,按当前输入 profile 取得 +原始字节。`file` 分支沿用真实 TeX 文件查找和输入策略,对读取的实际二进制 +内容求 MD5。缺失文件产生空展开。宿主错误沿既有错误路径传回。可恢复检查点 +记录查找和读取版本,重放时复核内容身份。旧 schema44 可以加载,原样格式 +需要 schema45 转储才能得到新的原语及 expl3 绑定。 +[pdfTeX 的文件和字节摘要](https://github.com/TeX-Live/texlive-source/blob/trunk/texk/web2c/lib/texmfmp.c#L3769-L3830) + +与 pdfTeX 的6个定向结果一致,包括展开文本、空文本、原始8位字符、 +受保护的控制序列、含空格路径的二进制文件及缺失文件。摘要、文件大小和 +文件修改时间的3项相关测试通过。等长文件替换使续编从该内容读取前恢复, +复用553步,只重发第二页。PDF、SVG、归一化来源映射和实际摘要文件均与 +独立零复用一致。首次检查点样例的换行断言错误留档。原样 LaTeX 和 ACM +进展见出版模板矩阵。 + +回执在 `target/p3-md5-native`。 + +- `receipt.json` 为 `2d57bcf51ad2bb16873384959e2ae62342abcbadbf8d4427a4f6f7999e777c63`。 +- `checkpoint/receipt.json` 为 `d634bfc7484d960752e1cd41f47fdfb56508c208112bff7d75e820b859e46e7f`。 +- `file-tests.log` 为 `581ea9153bed96dda20f06219d6bd59c9e1eb904d5b1a8f1272cecaa1531d4af`。 + ### PDF 字面中的矩阵与来源映射 来源映射与 PDF 写入共用设备原点的取整。实际字面中的 `q`、`Q` 和 diff --git a/src/native_checkpoint_probe.rs b/src/native_checkpoint_probe.rs index e73d351..7b62e3d 100644 --- a/src/native_checkpoint_probe.rs +++ b/src/native_checkpoint_probe.rs @@ -131,6 +131,17 @@ impl SourceProvider for RecordingHost<'_> { .map_err(Clone::clone) }) } + fn optional_tex_input( + &mut self, + name: &str, + from: &str, + ) -> Result, String> { + let (name, from) = (name.to_owned(), from.to_owned()); + call!(self, optional_tex_input(&name, &from); |result| { + result.as_ref().map(|resource| resource.as_ref().map(|item| item.version.clone())) + .map_err(Clone::clone) + }) + } fn file_size(&mut self, name: &str, from: &str) -> Result, String> { let (name, from) = (name.to_owned(), from.to_owned()); call!(self, file_size(&name, &from)) diff --git a/src/native_job.rs b/src/native_job.rs index 336f61a..34c5895 100644 --- a/src/native_job.rs +++ b/src/native_job.rs @@ -1553,12 +1553,21 @@ impl SourceProvider for Files { } Ok(resource) } + fn optional_tex_input( + &mut self, + name: &str, + from: &str, + ) -> Result, String> { + let resource = self.world.read_tex_input_optional(name, Path::new(from))?; + if let Some(resource) = &resource { + self.observe_resource(ResourceKind::Tex, resource); + } + Ok(resource) + } fn file_size(&mut self, name: &str, from: &str) -> Result, String> { - let Some(resource) = self.world.read_tex_input_optional(name, Path::new(from))? else { - return Ok(None); - }; - self.observe_resource(ResourceKind::Tex, &resource); - Ok(Some(resource.version.bytes)) + Ok(self + .optional_tex_input(name, from)? + .map(|resource| resource.version.bytes)) } fn file_modification_date(&mut self, name: &str, from: &str) -> Result, String> { file_modification_date(&mut self.world, name, from) diff --git a/tests/native_file_modification.rs b/tests/native_file_modification.rs index 82e286d..4d1f46a 100644 --- a/tests/native_file_modification.rs +++ b/tests/native_file_modification.rs @@ -189,3 +189,53 @@ fn format_load_checks_positive_and_missing_file_date_dependencies() { ); fs::remove_dir_all(work).unwrap(); } + +#[test] +fn pdf_file_digest_uses_binary_input_and_reports_a_missing_file_as_empty() { + let root = scratch("digest"); + let asset = root.join("asset with spaces.dat"); + fs::write(&asset, [0, 255, 17]).unwrap(); + fs::write( + root.join("probe.tex"), + r#"\catcode123=1 \catcode125=2 +\def\name{"asset with spaces.dat"} +\def\text{abc} +\immediate\openout5=digest.dat +\immediate\write5{[\pdfmdfivesum{\text}][\pdfmdfivesum file{\name}][\pdfmdfivesum file{absent.dat}]} +\immediate\write5{[\pdffilesize{asset with spaces.dat}]} +\immediate\closeout5 +\end"#, + ) + .unwrap(); + let output = Command::new(env!("CARGO_BIN_EXE_texrra")) + .args([ + "native", + "--initex", + "--etex", + "--resource-manifest", + "resources.json", + "probe.tex", + "probe.pdf", + ]) + .current_dir(&root) + .output() + .unwrap(); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + assert_eq!( + fs::read(root.join("digest.dat")).unwrap(), + b"[900150983CD24FB0D6963F7D28E17F72][5D3EE27BCBE27B1ED2D5191FA8CFE156][]\n[3]\n" + ); + let trace: ResourceTrace = + serde_json::from_slice(&fs::read(root.join("resources.json")).unwrap()).unwrap(); + assert!( + trace + .reads + .iter() + .any(|item| item.path == asset && item.bytes == 3) + ); + fs::remove_dir_all(root).unwrap(); +}