use alloc::borrow::ToOwned;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ObjectId {
pub number: u32,
pub generation: u16,
}
impl ObjectId {
pub const fn new(number: u32, generation: u16) -> Self {
Self { number, generation }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StringFormat {
Literal,
Hex,
}
#[derive(Debug, Clone)]
pub struct PdfString {
format: StringFormat,
bytes: Vec<u8>,
}
impl PdfString {
pub fn literal(bytes: impl Into<Vec<u8>>) -> Self {
Self {
format: StringFormat::Literal,
bytes: bytes.into(),
}
}
pub fn hex(bytes: impl Into<Vec<u8>>) -> Self {
Self {
format: StringFormat::Hex,
bytes: bytes.into(),
}
}
pub fn format(&self) -> StringFormat {
self.format
}
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
pub fn into_bytes(self) -> Vec<u8> {
self.bytes
}
pub fn set_bytes(&mut self, bytes: impl Into<Vec<u8>>) {
self.bytes = bytes.into();
}
}
impl PartialEq for PdfString {
fn eq(&self, other: &Self) -> bool {
self.bytes == other.bytes
}
}
impl Eq for PdfString {}
impl From<&str> for PdfString {
fn from(value: &str) -> Self {
Self::literal(value)
}
}
impl From<Vec<u8>> for PdfString {
fn from(value: Vec<u8>) -> Self {
Self::literal(value)
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Dictionary {
entries: Vec<(Vec<u8>, Object)>,
}
impl Dictionary {
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, name: impl Into<Vec<u8>>, value: Object) -> Option<Object> {
let name = name.into();
if let Some((_, slot)) = self.entries.iter_mut().find(|(key, _)| *key == name) {
return Some(core::mem::replace(slot, value));
}
self.entries.push((name, value));
None
}
pub fn get(&self, name: &[u8]) -> Option<&Object> {
self.entries
.iter()
.find(|(key, _)| key == name)
.map(|(_, value)| value)
}
pub fn get_mut(&mut self, name: &[u8]) -> Option<&mut Object> {
self.entries
.iter_mut()
.find(|(key, _)| key == name)
.map(|(_, value)| value)
}
pub fn remove(&mut self, name: &[u8]) -> Option<Object> {
let position = self.entries.iter().position(|(key, _)| key == name)?;
Some(self.entries.remove(position).1)
}
pub fn contains_key(&self, name: &[u8]) -> bool {
self.get(name).is_some()
}
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn entries(&self) -> &[(Vec<u8>, Object)] {
&self.entries
}
pub fn iter(&self) -> impl Iterator<Item = (&[u8], &Object)> + '_ {
self.entries
.iter()
.map(|(name, value)| (name.as_slice(), value))
}
fn write_to(&self, out: &mut Vec<u8>) {
out.extend_from_slice(b"<<");
for (name, value) in &self.entries {
out.push(b' ');
write_name(name, out);
out.push(b' ');
value.write_to(out);
}
if !self.entries.is_empty() {
out.push(b' ');
}
out.extend_from_slice(b">>");
}
}
impl From<Vec<(Vec<u8>, Object)>> for Dictionary {
fn from(entries: Vec<(Vec<u8>, Object)>) -> Self {
Self { entries }
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Stream {
dict: Dictionary,
data: Vec<u8>,
}
impl Stream {
pub fn new(dict: Dictionary, data: impl Into<Vec<u8>>) -> Self {
Self {
dict,
data: data.into(),
}
}
pub fn dict(&self) -> &Dictionary {
&self.dict
}
pub fn dict_mut(&mut self) -> &mut Dictionary {
&mut self.dict
}
pub fn data(&self) -> &[u8] {
&self.data
}
pub fn into_data(self) -> Vec<u8> {
self.data
}
pub fn set_data(&mut self, data: impl Into<Vec<u8>>) {
let data = data.into();
self.dict
.insert(b"Length", Object::Integer(data.len() as i64));
self.data = data;
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Object {
Null,
Boolean(bool),
Integer(i64),
Real(f64),
String(PdfString),
Name(Vec<u8>),
Array(Vec<Object>),
Dictionary(Dictionary),
Stream(Stream),
Reference(ObjectId),
}
impl Object {
pub fn name(name: impl Into<Vec<u8>>) -> Self {
Self::Name(name.into())
}
pub fn string(bytes: impl Into<Vec<u8>>) -> Self {
Self::String(PdfString::literal(bytes))
}
pub fn hex_string(bytes: impl Into<Vec<u8>>) -> Self {
Self::String(PdfString::hex(bytes))
}
pub fn array(items: impl IntoIterator<Item = Object>) -> Self {
Self::Array(items.into_iter().collect())
}
pub const fn reference(id: ObjectId) -> Self {
Self::Reference(id)
}
pub fn is_null(&self) -> bool {
matches!(self, Self::Null)
}
pub fn as_integer(&self) -> Option<i64> {
match self {
Self::Integer(value) => Some(*value),
_ => None,
}
}
pub fn as_real(&self) -> Option<f64> {
match self {
Self::Integer(value) => Some(*value as f64),
Self::Real(value) => Some(*value),
_ => None,
}
}
pub fn as_name(&self) -> Option<&[u8]> {
match self {
Self::Name(name) => Some(name),
_ => None,
}
}
pub fn as_array(&self) -> Option<&[Object]> {
match self {
Self::Array(items) => Some(items),
_ => None,
}
}
pub fn as_dict(&self) -> Option<&Dictionary> {
match self {
Self::Dictionary(dict) => Some(dict),
_ => None,
}
}
pub fn as_stream(&self) -> Option<&Stream> {
match self {
Self::Stream(stream) => Some(stream),
_ => None,
}
}
pub fn as_string(&self) -> Option<&PdfString> {
match self {
Self::String(string) => Some(string),
_ => None,
}
}
pub fn as_reference(&self) -> Option<ObjectId> {
match self {
Self::Reference(id) => Some(*id),
_ => None,
}
}
pub fn write_to(&self, out: &mut Vec<u8>) {
match self {
Self::Null => out.extend_from_slice(b"null"),
Self::Boolean(value) => out.extend_from_slice(if *value { b"true" } else { b"false" }),
Self::Integer(value) => out.extend_from_slice(value.to_string().as_bytes()),
Self::Real(value) => out.extend_from_slice(real_to_string(*value).as_bytes()),
Self::String(string) => match string.format() {
StringFormat::Literal => write_literal(string.as_bytes(), out),
StringFormat::Hex => write_hex(string.as_bytes(), out),
},
Self::Name(name) => write_name(name, out),
Self::Array(items) => {
out.push(b'[');
for (index, item) in items.iter().enumerate() {
if index > 0 {
out.push(b' ');
}
item.write_to(out);
}
out.push(b']');
}
Self::Dictionary(dict) => dict.write_to(out),
Self::Stream(stream) => {
stream.dict.write_to(out);
out.extend_from_slice(b"\nstream\n");
out.extend_from_slice(stream.data());
out.extend_from_slice(b"\nendstream");
}
Self::Reference(id) => {
out.extend_from_slice(id.number.to_string().as_bytes());
out.push(b' ');
out.extend_from_slice(id.generation.to_string().as_bytes());
out.extend_from_slice(b" R");
}
}
}
pub fn to_bytes(&self) -> Vec<u8> {
let mut out = Vec::new();
self.write_to(&mut out);
out
}
}
fn is_regular(byte: u8) -> bool {
matches!(byte, 0x21..=0x7e)
&& !matches!(
byte,
b'(' | b')' | b'<' | b'>' | b'[' | b']' | b'{' | b'}' | b'/' | b'%' | b'#'
)
}
fn write_name(name: &[u8], out: &mut Vec<u8>) {
out.push(b'/');
for &byte in name {
if is_regular(byte) {
out.push(byte);
} else {
out.push(b'#');
out.extend_from_slice(format!("{byte:02X}").as_bytes());
}
}
}
fn write_literal(bytes: &[u8], out: &mut Vec<u8>) {
out.push(b'(');
for &byte in bytes {
match byte {
b'(' | b')' | b'\\' => {
out.push(b'\\');
out.push(byte);
}
b'\n' => out.extend_from_slice(b"\\n"),
b'\r' => out.extend_from_slice(b"\\r"),
b'\t' => out.extend_from_slice(b"\\t"),
0x08 => out.extend_from_slice(b"\\b"),
0x0c => out.extend_from_slice(b"\\f"),
0x20..=0x7e => out.push(byte),
_ => {
out.push(b'\\');
out.extend_from_slice(format!("{byte:03o}").as_bytes());
}
}
}
out.push(b')');
}
fn write_hex(bytes: &[u8], out: &mut Vec<u8>) {
out.push(b'<');
for &byte in bytes {
out.extend_from_slice(format!("{byte:02X}").as_bytes());
}
out.push(b'>');
}
fn real_to_string(value: f64) -> String {
if !value.is_finite() {
return "0".to_owned();
}
let mut text = format!("{value}");
if !text.contains('.') {
text.push_str(".0");
}
text
}
impl fmt::Display for Object {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&String::from_utf8_lossy(&self.to_bytes()))
}
}
#[cfg(test)]
mod tests {
use super::{Dictionary, Object, ObjectId, PdfString, Stream};
fn dict(entries: &[(&str, Object)]) -> Dictionary {
let mut dict = Dictionary::new();
for (name, value) in entries {
dict.insert(name.as_bytes(), value.clone());
}
dict
}
#[test]
fn scalars_serialize_canonically() {
assert_eq!(Object::Null.to_bytes(), b"null");
assert_eq!(Object::Boolean(true).to_bytes(), b"true");
assert_eq!(Object::Boolean(false).to_bytes(), b"false");
assert_eq!(Object::Integer(-17).to_bytes(), b"-17");
assert_eq!(Object::Real(2.375).to_bytes(), b"2.375");
assert_eq!(Object::Real(-0.5).to_bytes(), b"-0.5");
}
#[test]
fn reals_always_keep_a_decimal_point() {
assert_eq!(Object::Real(1.0).to_bytes(), b"1.0");
assert_eq!(Object::Real(123.0).to_bytes(), b"123.0");
assert_eq!(Object::Real(-2.0).to_bytes(), b"-2.0");
assert_eq!(Object::Real(1e-7).to_bytes(), b"0.0000001");
assert_eq!(Object::Real(f64::NAN).to_bytes(), b"0");
}
#[test]
fn names_escape_irregular_bytes() {
assert_eq!(Object::name("Type").to_bytes(), b"/Type");
assert_eq!(Object::name("A Name").to_bytes(), b"/A#20Name");
assert_eq!(Object::name("#hash").to_bytes(), b"/#23hash");
assert_eq!(
Object::name("slash/percent%").to_bytes(),
b"/slash#2Fpercent#25"
);
assert_eq!(Object::name("Name\u{e9}").to_bytes(), b"/Name#C3#A9");
assert_eq!(Object::name("").to_bytes(), b"/");
}
#[test]
fn literal_strings_escape_delimiters_and_binary() {
assert_eq!(Object::string("Hello").to_bytes(), b"(Hello)");
assert_eq!(Object::string("").to_bytes(), b"()");
assert_eq!(Object::string("a(b)c").to_bytes(), b"(a\\(b\\)c)");
assert_eq!(Object::string("back\\slash").to_bytes(), b"(back\\\\slash)");
assert_eq!(Object::string("line\nbreak").to_bytes(), b"(line\\nbreak)");
assert_eq!(Object::string("tab\there").to_bytes(), b"(tab\\there)");
assert_eq!(
Object::string(vec![0x00, 0x07, 0xff]).to_bytes(),
b"(\\000\\007\\377)"
);
}
#[test]
fn hex_strings_are_uppercase() {
assert_eq!(Object::hex_string("Hello").to_bytes(), b"<48656C6C6F>");
assert_eq!(Object::hex_string(Vec::new()).to_bytes(), b"<>");
assert_eq!(Object::hex_string(vec![0xde, 0xad]).to_bytes(), b"<DEAD>");
}
#[test]
fn string_equality_ignores_the_serialization_format() {
assert_eq!(PdfString::literal("Hi"), PdfString::hex("Hi"));
assert_ne!(PdfString::literal("Hi"), PdfString::literal("ho"));
}
#[test]
fn containers_separate_elements_with_one_space() {
assert_eq!(Object::array(Vec::new()).to_bytes(), b"[]");
assert_eq!(
Object::array([Object::Integer(1), Object::Integer(2), Object::Integer(3)]).to_bytes(),
b"[1 2 3]"
);
assert_eq!(
Object::array([
Object::array([Object::Integer(1)]),
Object::array([Object::Integer(2), Object::Integer(3)])
])
.to_bytes(),
b"[[1] [2 3]]"
);
assert_eq!(
Object::Dictionary(dict(&[("Type", Object::name("Page"))])).to_bytes(),
b"<< /Type /Page >>"
);
assert_eq!(Object::Dictionary(Dictionary::new()).to_bytes(), b"<<>>");
assert_eq!(
Object::Dictionary(dict(&[
("Type", Object::name("Page")),
("Count", Object::Integer(3)),
(
"Kids",
Object::array([Object::reference(ObjectId::new(1, 0))])
),
]))
.to_bytes(),
b"<< /Type /Page /Count 3 /Kids [1 0 R] >>"
);
}
#[test]
fn nested_dictionaries_serialize_in_insertion_order() {
let inner = dict(&[("Inner", Object::Null)]);
let outer = dict(&[
("Zed", Object::Integer(1)),
("Outer", Object::Dictionary(inner)),
]);
assert_eq!(
Object::Dictionary(outer).to_bytes(),
b"<< /Zed 1 /Outer << /Inner null >> >>"
);
}
#[test]
fn dictionary_insert_replaces_in_place() {
let mut dict = dict(&[("A", Object::Integer(1)), ("B", Object::Integer(2))]);
assert_eq!(
dict.insert("A", Object::Integer(9)),
Some(Object::Integer(1))
);
assert_eq!(dict.len(), 2);
assert_eq!(Object::Dictionary(dict).to_bytes(), b"<< /A 9 /B 2 >>");
}
#[test]
fn streams_serialize_the_dictionary_then_the_raw_data() {
let mut dict = Dictionary::new();
dict.insert("Length", Object::Integer(5));
dict.insert("Filter", Object::name("FlateDecode"));
let stream = Stream::new(dict, b"hello");
assert_eq!(
Object::Stream(stream).to_bytes(),
b"<< /Length 5 /Filter /FlateDecode >>\nstream\nhello\nendstream"
);
}
#[test]
fn set_data_keeps_length_in_step() {
let mut dict = Dictionary::new();
dict.insert("Length", Object::Integer(2));
let mut stream = Stream::new(dict, b"ab");
stream.set_data(b"abcdef");
assert_eq!(stream.dict().get(b"Length"), Some(&Object::Integer(6)));
assert_eq!(stream.data(), b"abcdef");
assert_eq!(
Object::Stream(stream).to_bytes(),
b"<< /Length 6 >>\nstream\nabcdef\nendstream"
);
}
#[test]
fn references_serialize_as_three_tokens() {
assert_eq!(
Object::Reference(ObjectId::new(12, 0)).to_bytes(),
b"12 0 R"
);
assert_eq!(ObjectId::new(7, 65535).generation, 65535);
}
}