mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-14 22:42:46 +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.
99 lines
3.1 KiB
Rust
99 lines
3.1 KiB
Rust
//! Stream data that can share a larger buffer instead of copying out of it.
|
|
|
|
use std::ops::{Deref, Range};
|
|
use std::sync::Arc;
|
|
|
|
/// A stream's bytes: a buffer of their own, or a range of a larger shared buffer. Streams read
|
|
/// from a file point into the file's bytes, which the document keeps anyway, so caching a
|
|
/// parsed stream costs its dictionary and not a second copy of its data.
|
|
#[derive(Clone, Default)]
|
|
pub struct Bytes {
|
|
buf: Arc<Vec<u8>>,
|
|
range: Range<usize>,
|
|
}
|
|
|
|
impl Bytes {
|
|
/// `range` of `buf`, without copying; `None` when the range is not within `buf`.
|
|
pub(crate) fn view(buf: &Arc<Vec<u8>>, range: Range<usize>) -> Option<Self> {
|
|
(range.start <= range.end && range.end <= buf.len()).then(|| Self { buf: buf.clone(), range })
|
|
}
|
|
|
|
/// Whether these bytes keep alive a larger buffer than themselves that isn't `owner`.
|
|
pub(crate) fn borrows_other_than(&self, owner: &Arc<Vec<u8>>) -> bool {
|
|
self.range.len() < self.buf.len() && !Arc::ptr_eq(&self.buf, owner)
|
|
}
|
|
|
|
/// Whether these bytes are (a view into) `buf`, holding no memory of their own.
|
|
pub(crate) fn shares(&self, buf: &Arc<Vec<u8>>) -> bool {
|
|
Arc::ptr_eq(&self.buf, buf)
|
|
}
|
|
|
|
/// The same bytes, in a buffer of their own.
|
|
pub(crate) fn detached(&self) -> Self {
|
|
Self::from(self.to_vec())
|
|
}
|
|
}
|
|
|
|
impl From<Vec<u8>> for Bytes {
|
|
fn from(v: Vec<u8>) -> Self {
|
|
let range = 0..v.len();
|
|
Self { buf: Arc::new(v), range }
|
|
}
|
|
}
|
|
|
|
impl From<Arc<Vec<u8>>> for Bytes {
|
|
fn from(buf: Arc<Vec<u8>>) -> Self {
|
|
let range = 0..buf.len();
|
|
Self { buf, range }
|
|
}
|
|
}
|
|
|
|
impl Deref for Bytes {
|
|
type Target = [u8];
|
|
|
|
fn deref(&self) -> &[u8] {
|
|
// `view` and `From` only build in-bounds ranges; an empty slice keeps this panic-free.
|
|
self.buf.get(self.range.clone()).unwrap_or_default()
|
|
}
|
|
}
|
|
|
|
impl AsRef<[u8]> for Bytes {
|
|
fn as_ref(&self) -> &[u8] {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl PartialEq for Bytes {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
(Arc::ptr_eq(&self.buf, &other.buf) && self.range == other.range) || **self == **other
|
|
}
|
|
}
|
|
|
|
impl Eq for Bytes {}
|
|
|
|
impl std::fmt::Debug for Bytes {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
(**self).fmt(f)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn views_share_their_buffer_and_compare_by_content() {
|
|
let file = Arc::new(b"0123456789".to_vec());
|
|
let view = Bytes::view(&file, 2..5).expect("in range");
|
|
assert_eq!(&*view, b"234");
|
|
assert_eq!(view.as_ptr(), file[2..].as_ptr(), "no copy");
|
|
assert_eq!(view, Bytes::from(b"234".to_vec()));
|
|
assert!(Bytes::view(&file, 5..11).is_none() && Bytes::view(&file, Range { start: 6, end: 5 }).is_none());
|
|
let other = Arc::new(Vec::new());
|
|
assert!(view.borrows_other_than(&other) && !view.borrows_other_than(&file));
|
|
let own = view.detached();
|
|
assert!(!own.borrows_other_than(&other) && own == view && own.as_ptr() != view.as_ptr());
|
|
assert!(!Bytes::from(file.clone()).borrows_other_than(&other), "a whole buffer is its own");
|
|
}
|
|
}
|