Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/pdf-canvas/benches/text_recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn content_stream(text: &[u8]) -> ContentStream {
let mut data = b"BT /F1 10 Tf (".to_vec();
data.extend_from_slice(text);
data.extend_from_slice(b") Tj ET");
let stream = StreamObject::new(1, 0, Dictionary::new(Default::default()), data);
let stream = StreamObject::new(1, 0, Dictionary::default(), data);
pdf_object_reader::ObjectReader::new(EmptySource)
.read::<ContentStream>(&ObjectVariant::Stream(stream))
.expect("benchmark stream should parse")
Expand Down
2 changes: 1 addition & 1 deletion crates/pdf-canvas/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub fn content_stream(object_number: usize, data: &[u8]) -> ContentStream {
let stream = StreamObject::new(
object_number,
0,
Dictionary::new(std::collections::BTreeMap::<
Dictionary::from_iter(std::collections::BTreeMap::<
Vec<u8>,
pdf_object_reader::object_variant::ObjectVariant,
>::new()),
Expand Down
4 changes: 2 additions & 2 deletions crates/pdf-canvas/tests/pdf_canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn form_resource(name: &str, stream: ContentStream) -> Resources {
}

fn image_dictionary() -> Dictionary {
Dictionary::new(std::collections::BTreeMap::from([
Dictionary::from_iter(std::collections::BTreeMap::from([
(Vec::from(b"BitsPerComponent"), ObjectVariant::Integer(1)),
(
Vec::from(b"ColorSpace"),
Expand All @@ -75,7 +75,7 @@ fn image_dictionary() -> Dictionary {

fn inline_image() -> PdfImage {
PdfImage::inline(
Dictionary::new(std::collections::BTreeMap::from([
Dictionary::from_iter(std::collections::BTreeMap::from([
(Vec::from(b"BPC"), ObjectVariant::Integer(1)),
(
Vec::from(b"CS"),
Expand Down
33 changes: 11 additions & 22 deletions crates/pdf-ccitt/src/ccitt_fax_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,10 @@ mod tests {
use pdf_object_reader::EmptySource;
use pdf_object_reader::object_variant::ObjectVariant;
use pdf_object_reader::{Dictionary, ObjectReader};
use std::collections::BTreeMap;

#[test]
fn params_from_empty_dict_uses_defaults() -> ReadResult<()> {
use std::collections::BTreeMap;
let dict = Dictionary::new(BTreeMap::<Vec<u8>, ObjectVariant>::new());
let dict = Dictionary::default();
let p = ObjectReader::new(EmptySource)
.read::<CCITTFaxParams>(&ObjectVariant::Dictionary(dict))?;
assert_eq!(p.k, 0);
Expand All @@ -102,25 +100,16 @@ mod tests {
fn params_from_dict_reads_all_keys() -> ReadResult<()> {
use pdf_object_reader::object_variant::ObjectVariant;

let mut dict = Dictionary::new(BTreeMap::<Vec<u8>, ObjectVariant>::new());
dict.dictionary
.insert(b"K".to_vec(), ObjectVariant::Integer(-1));
dict.dictionary
.insert(b"Columns".to_vec(), ObjectVariant::Integer(800));
dict.dictionary
.insert(b"Rows".to_vec(), ObjectVariant::Integer(600));
dict.dictionary
.insert(b"EndOfLine".to_vec(), ObjectVariant::Boolean(true));
dict.dictionary
.insert(b"EncodedByteAlign".to_vec(), ObjectVariant::Boolean(true));
dict.dictionary
.insert(b"EndOfBlock".to_vec(), ObjectVariant::Boolean(false));
dict.dictionary
.insert(b"BlackIs1".to_vec(), ObjectVariant::Boolean(true));
dict.dictionary.insert(
b"DamagedRowsBeforeError".to_vec(),
ObjectVariant::Integer(2),
);
let dict = Dictionary::from_entries([
(b"K", ObjectVariant::Integer(-1)),
(b"Columns", ObjectVariant::Integer(800)),
(b"Rows", ObjectVariant::Integer(600)),
(b"EndOfLine", ObjectVariant::Boolean(true)),
(b"EncodedByteAlign", ObjectVariant::Boolean(true)),
(b"EndOfBlock", ObjectVariant::Boolean(false)),
(b"BlackIs1", ObjectVariant::Boolean(true)),
(b"DamagedRowsBeforeError", ObjectVariant::Integer(2)),
]);

let p = ObjectReader::new(EmptySource)
.read::<CCITTFaxParams>(&ObjectVariant::Dictionary(dict))?;
Expand Down
5 changes: 2 additions & 3 deletions crates/pdf-color-space/src/device_n_color_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ mod tests {
ObjectVariant::Stream(StreamObject::new(
1,
0,
Dictionary::new(dict),
Dictionary::from_iter(dict),
code.as_bytes().to_vec(),
))
}
Expand Down Expand Up @@ -173,8 +173,7 @@ mod tests {

#[test]
fn parses_five_element_device_n_array_with_attributes() {
let attrs =
ObjectVariant::Dictionary(Dictionary::new(BTreeMap::<Vec<u8>, ObjectVariant>::new()));
let attrs = ObjectVariant::Dictionary(Dictionary::default());
let arr = device_n_array(vec![
ObjectVariant::Array(vec![name("Spot")].into()),
name("DeviceCMYK"),
Expand Down
8 changes: 1 addition & 7 deletions crates/pdf-color-space/src/indexed_color_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ impl IndexedColorSpace {
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use std::collections::BTreeMap;

use pdf_object_reader::{
EmptySource, ObjectReader, dictionary::Dictionary, object_variant::ObjectVariant,
Expand All @@ -169,12 +168,7 @@ mod tests {

#[test]
fn stream_lookup_shares_decoded_stream_bytes() {
let stream = StreamObject::new(
1,
0,
Dictionary::new(BTreeMap::<Vec<u8>, ObjectVariant>::new()),
vec![1, 2, 3, 4],
);
let stream = StreamObject::new(1, 0, Dictionary::default(), vec![1, 2, 3, 4]);
let stream_data = stream.shared_data();
let lookup = ObjectVariant::Stream(stream);

Expand Down
7 changes: 1 addition & 6 deletions crates/pdf-content-stream/benches/operator_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ fn repeated_stream(fragment: &[u8]) -> ObjectVariant {
data.extend_from_slice(fragment);
}

ObjectVariant::Stream(StreamObject::new(
1,
0,
Dictionary::new(Default::default()),
data,
))
ObjectVariant::Stream(StreamObject::new(1, 0, Dictionary::default(), data))
}

fn benchmark_stream(
Expand Down
11 changes: 3 additions & 8 deletions crates/pdf-content-stream/src/content_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,7 @@ mod tests {
}

fn stream_object(object_number: usize, data: &[u8]) -> StreamObject {
StreamObject::new(
object_number,
0,
Dictionary::new(BTreeMap::<Vec<u8>, ObjectVariant>::new()),
data.to_vec(),
)
StreamObject::new(object_number, 0, Dictionary::default(), data.to_vec())
}

struct MapResolver {
Expand Down Expand Up @@ -291,7 +286,7 @@ mod tests {

#[test]
fn optional_contents_preserves_allocator_for_missing_contents() {
let page = Dictionary::new(BTreeMap::<Vec<u8>, ObjectVariant>::new());
let page = Dictionary::default();
let reader = pdf_object_reader::ObjectReader::new(EmptySource);

let contents = reader
Expand All @@ -318,7 +313,7 @@ mod tests {
]
.into(),
);
let page = Dictionary::new(BTreeMap::from([(Vec::from(b"Contents"), contents)]));
let page = Dictionary::from_iter(BTreeMap::from([(Vec::from(b"Contents"), contents)]));
let resolver = MapResolver {
objects: BTreeMap::from([
(1, ObjectVariant::Stream(stream_object(1, b"q"))),
Expand Down
11 changes: 3 additions & 8 deletions crates/pdf-content-stream/tests/parser_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ impl pdf_object_reader::FromPdfObject for PageContents {
}

fn stream_object(object_number: usize, data: &[u8]) -> StreamObject {
StreamObject::new(
object_number,
0,
Dictionary::new(BTreeMap::<Vec<u8>, ObjectVariant>::new()),
data.to_vec(),
)
StreamObject::new(object_number, 0, Dictionary::default(), data.to_vec())
}

struct MapResolver {
Expand Down Expand Up @@ -151,7 +146,7 @@ fn content_stream_read_skips_unknown_operator_and_recovers() {

#[test]
fn optional_contents_preserves_allocator_for_missing_contents() {
let page = Dictionary::new(BTreeMap::<Vec<u8>, ObjectVariant>::new());
let page = Dictionary::default();
let reader = pdf_object_reader::ObjectReader::new(EmptySource);

let contents = reader
Expand All @@ -178,7 +173,7 @@ fn optional_contents_parses_stream_arrays_and_allocates_monotonically() {
]
.into(),
);
let page = Dictionary::new(BTreeMap::from([(Vec::from(b"Contents"), contents)]));
let page = Dictionary::from_iter(BTreeMap::from([(Vec::from(b"Contents"), contents)]));
let resolver = MapResolver {
objects: BTreeMap::from([
(1, ObjectVariant::Stream(stream_object(1, b"1 2"))),
Expand Down
34 changes: 13 additions & 21 deletions crates/pdf-document/src/decryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,27 +292,19 @@ impl DocumentDecryptor {
/// Decrypts dictionary values while preserving signature contents required by PDF rules.
fn decrypt_dictionary(
&self,
dictionary: Dictionary,
mut dictionary: Dictionary,
object_number: usize,
generation_number: usize,
) -> Result<Dictionary, DecryptionError> {
let is_signature = is_signature_dictionary(&dictionary);
let entries = dictionary
.dictionary
.into_iter()
.map(|(key, value)| {
if is_signature && key == b"Contents" {
Ok((key, value))
} else {
self.decrypt_object_value(value, object_number, generation_number)
.map(|value| (key, value))
}
})
.collect::<Result<_, _>>()?;
Ok(Dictionary {
dictionary: entries,
object_number: dictionary.object_number,
})
for (key, value) in dictionary.iter_mut() {
if is_signature && key == b"Contents" {
continue;
}
let encrypted = std::mem::replace(value, ObjectVariant::Null);
*value = self.decrypt_object_value(encrypted, object_number, generation_number)?;
}
Ok(dictionary)
}

/// Decrypts a stream's data and recursively transforms its dictionary values.
Expand Down Expand Up @@ -385,7 +377,7 @@ mod tests {
);
}

StreamObject::new(7, 0, Dictionary::new(entries), data)
StreamObject::new(7, 0, Dictionary::from_iter(entries), data)
}

fn encrypt_for_object(
Expand Down Expand Up @@ -473,7 +465,7 @@ mod tests {
number: object_number,
generation: generation_number,
};
let object = ObjectVariant::Dictionary(Dictionary::new(BTreeMap::from([
let object = ObjectVariant::Dictionary(Dictionary::from_iter(BTreeMap::from([
(
Vec::from(b"Contents"),
pdf_object_reader::pdf_string::PdfString::from(
Expand Down Expand Up @@ -556,7 +548,7 @@ mod tests {
let stream = StreamObject::new(
99,
1,
Dictionary::new(BTreeMap::from([(
Dictionary::from_iter(BTreeMap::from([(
Vec::from(b"Label"),
pdf_object_reader::pdf_string::PdfString::from(
encrypt_for_object(
Expand Down Expand Up @@ -602,7 +594,7 @@ mod tests {
number: 9,
generation: 0,
};
let object = ObjectVariant::Dictionary(Dictionary::new(BTreeMap::from([
let object = ObjectVariant::Dictionary(Dictionary::from_iter(BTreeMap::from([
(
Vec::from(b"Type"),
pdf_object_reader::pdf_string::PdfString::from(
Expand Down
2 changes: 1 addition & 1 deletion crates/pdf-document/src/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ mod tests {
for (key, value) in entries {
map.insert(Vec::from(key.as_bytes()), value);
}
Dictionary::new(map)
Dictionary::from_iter(map)
}

#[test]
Expand Down
8 changes: 4 additions & 4 deletions crates/pdf-document/tests/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ fn color_space(value: &str) -> ObjectVariant {
}

fn resources_dictionary(entries: &[(&str, &str)]) -> Dictionary {
let color_spaces = Dictionary::new(
let color_spaces = Dictionary::from_iter(
entries
.iter()
.map(|(name, value)| (Vec::from(name.as_bytes()), color_space(value)))
.collect::<BTreeMap<_, _>>(),
);

Dictionary::new(BTreeMap::from([(
Dictionary::from_iter(BTreeMap::from([(
Vec::from(b"ColorSpace"),
ObjectVariant::Dictionary(color_spaces),
)]))
Expand Down Expand Up @@ -92,7 +92,7 @@ fn page_dictionary(
);
}

let mut dictionary = Dictionary::new(entries);
let mut dictionary = Dictionary::from_iter(entries);
dictionary.object_number = Some(object_number);
dictionary
}
Expand Down Expand Up @@ -134,7 +134,7 @@ fn pages_dictionary(
);
}

let mut dictionary = Dictionary::new(entries);
let mut dictionary = Dictionary::from_iter(entries);
dictionary.object_number = Some(object_number);
dictionary
}
Expand Down
Loading
Loading