mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-14 06:22:24 +00:00
craft-pdf: the PDF object layer from PdfCraft (`cos`), on filters and crypt * hayro-jbig2: bound a stream's total decode work and what a page retains; no i32 overflow Port of pdfcraft 85c10051 (patches 5-9), 82439525 (patch 10) and 10107d94 (patch 12). * hayro-jbig2: PdfCraft's JBIG2 follow-ups (refinement fixes, file header, work accounting, dictionary pixel cap) Ports pdfcraft 72e763ad, b7ec3669, e3bc4e85, 01d3479f, b9e068aa, 858ce960, 07f44038, e38b9d18, 8717a085, 58c30af3 and the hayro-jbig2 part of e2464e14 (with the hayro-jbig2 part of 0ed33990 they build on). * Add craft-stream-codecs: the codec modules of PdfCraft's stream filters LZW, ASCII85, ASCIIHex, RunLength, the TIFF and PNG predictors, bounded CCITT fax decoding (through hayro-ccitt) with Group 4 encoding (through fax), and the lossless JBIG2 encoder with its MQ coder, from pdfcraft-filters at PdfCraft revision 80e26e674d1aa33078e83c8139875047f3289eb6. The crate sits below craft-pdf and craft-tiff: it knows no container. The PDF filter chain, Flate and the bounded JBIG2 decoding entry are not part of it. * craft-pdf: the PDF filter chain from PdfCraft (`filters`) Adds the `filters` feature: the /Filter chain of pdfcraft-filters (PdfCraft revision 80e26e674d1aa33078e83c8139875047f3289eb6) as craft_pdf::filters, over the codecs in craft-stream-codecs. It adds Flate, strict and tolerant chain decoding, byte accounting and a shared work allowance, and encoding, with the chain's unit tests, its property tests and its fuzz target. The code is unchanged apart from the imports of the moved codecs; FilterError is craft-stream-codecs' CodecError under its old name. * craft-stream-codecs: bounded JBIG2 decoding (decode_jbig2_within) The JBIG2 decoding entry of pdfcraft-filters (PdfCraft revision 80e26e674d1aa33078e83c8139875047f3289eb6, src/jbig2/decode.rs) with its tests. It spends from the caller's work allowance through hayro-jbig2's Image::decode_within and charge_reading, which come with fix/sec-hayro-jbig2-followups (#886). * vendor/README.craft-reader.md: one #831 paragraph, the follow-ups after it * craft-pdf: filters re-exports decode_jbig2_within (and lib.rs module order) * craft-pdf: the object layer from PdfCraft (`cos`) Adds the `cos` feature: PdfCraft's pdfcraft-cos crate (revision 1d2af9907ba772b368f6b54811359805fa5428e9) as craft_pdf::cos, on crypt and filters, with its unit tests, integration tests and examples. Its test helpers come with it: pdfcraft-testkit as src/cos/testkit.rs (cfg(test)) and the qpdf runner of pdfcraft-oracle as tests/cos/oracle.rs. The code is unchanged apart from module paths and doc comments. The child test of file_identifier is named by its module path, since the integration tests are one binary. * craft-pdf: cos unit tests build for wasm The property tests in src/cos/writer.rs need proptest, a dev-dependency of the non-wasm targets only, and one test in src/cos/document/tests.rs times a load on a thread, which wasm32-unknown-unknown does not have. Both are compiled for non-wasm targets only; the other unit tests still build for wasm.
36 lines
1.5 KiB
Rust
36 lines
1.5 KiB
Rust
//! Encrypt a PDF: `cargo run -p craft-pdf --features cos --example cos_protect in.pdf out.pdf rc4-40|rc4-128|aes-128|aes-256 USER OWNER`
|
|
/// 32 fresh bytes for the file key. `RandomState` is seeded by the operating system (as the
|
|
/// engine's `entropy` uses it), so every run encrypts with a different key.
|
|
fn seed() -> [u8; 32] {
|
|
use std::hash::{BuildHasher, Hasher};
|
|
let mut out = [0u8; 32];
|
|
for (i, chunk) in out.chunks_mut(8).enumerate() {
|
|
let mut h = std::collections::hash_map::RandomState::new().build_hasher();
|
|
h.write_usize(i);
|
|
chunk.copy_from_slice(&h.finish().to_le_bytes());
|
|
}
|
|
out
|
|
}
|
|
|
|
fn main() {
|
|
let a: Vec<String> = std::env::args().skip(1).collect();
|
|
let [input, output, alg, user, owner] = &a[..] else { panic!("usage: protect in out alg user owner") };
|
|
let algorithm = match alg.as_str() {
|
|
"rc4-40" => craft_pdf::cos::Algorithm::Rc4_40,
|
|
"rc4-128" => craft_pdf::cos::Algorithm::Rc4_128,
|
|
"aes-128" => craft_pdf::cos::Algorithm::Aes128,
|
|
_ => craft_pdf::cos::Algorithm::Aes256,
|
|
};
|
|
let mut doc = craft_pdf::cos::Document::open(std::sync::Arc::new(std::fs::read(input).unwrap())).unwrap();
|
|
doc.set_encryption(&craft_pdf::cos::NewEncryption {
|
|
algorithm,
|
|
user_password: user,
|
|
owner_password: owner,
|
|
permissions: -1,
|
|
encrypt_metadata: true,
|
|
seed: seed(),
|
|
})
|
|
.unwrap();
|
|
std::fs::write(output, craft_pdf::cos::write_full(&doc, &Default::default()).unwrap()).unwrap();
|
|
}
|