use crate::json_inspect::decode_pdf_text_string;
use crate::{Dictionary, Error, Object, ObjectRef, Pdf, Result, WriteOptions};
use std::collections::BTreeSet;
use std::io::{Read, Seek};
pub const DEFAULT_MAX_SIGNATURE_FIELD_DEPTH: usize = crate::pages::DEFAULT_MAX_PAGE_TREE_DEPTH;
pub const SIG_FLAGS_SIGNATURES_EXIST: u32 = 1;
pub const SIG_FLAGS_APPEND_ONLY: u32 = 2;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SignatureInfo {
pub field_ref: ObjectRef,
pub signature_ref: Option<ObjectRef>,
pub field_name: String,
pub byte_range: [u64; 4],
pub sub_filter: Option<String>,
pub signer_name: Option<String>,
pub signing_time: Option<String>,
pub reason: Option<String>,
pub location: Option<String>,
pub contact_info: Option<String>,
pub certificate: Option<Vec<u8>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SignatureWriteMode {
Incremental,
FullRewrite,
}
impl SignatureWriteMode {
fn from_options(options: &WriteOptions) -> Self {
if options.full_rewrite {
Self::FullRewrite
} else {
Self::Incremental
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SignatureRewriteReason {
NoSignatures,
FullRewrite,
IncrementalTouchesAcroForm,
IncrementalTouchesSignedObject,
IncrementalPreservesSignedByteRanges,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SignatureRewriteImpact {
pub has_signatures: bool,
pub invalidates_signatures: bool,
pub reason: SignatureRewriteReason,
pub first_invalidating_ref: Option<ObjectRef>,
pub signed_object_refs: Vec<ObjectRef>,
pub touched_object_refs: Vec<ObjectRef>,
pub acroform_ref: Option<ObjectRef>,
pub sig_flags: Option<u32>,
}
impl SignatureRewriteImpact {
pub fn signatures_exist(&self) -> bool {
self.sig_flags
.is_some_and(|flags| flags & SIG_FLAGS_SIGNATURES_EXIST != 0)
}
pub fn append_only(&self) -> bool {
self.sig_flags
.is_some_and(|flags| flags & SIG_FLAGS_APPEND_ONLY != 0)
}
}
pub fn signatures<R: Read + Seek>(pdf: &mut Pdf<R>) -> Result<Vec<SignatureInfo>> {
signatures_with_max_depth(pdf, DEFAULT_MAX_SIGNATURE_FIELD_DEPTH)
}
pub fn signatures_with_max_depth<R: Read + Seek>(
pdf: &mut Pdf<R>,
max_depth: usize,
) -> Result<Vec<SignatureInfo>> {
let Some(catalog_ref) = pdf.root_ref() else {
return Ok(Vec::new());
};
let catalog_obj = pdf.resolve_borrowed(catalog_ref)?;
let Object::Dictionary(catalog_dict) = catalog_obj else {
return Ok(Vec::new());
};
let Some(acroform_val) = catalog_dict.get("AcroForm").cloned() else {
return Ok(Vec::new());
};
let Some(acroform_dict) = resolve_dictionary(pdf, acroform_val)? else {
return Ok(Vec::new());
};
let Some(fields_obj) = acroform_dict.get("Fields").cloned() else {
return Ok(Vec::new());
};
let fields = resolve_array(pdf, fields_obj)?;
let mut output = Vec::new();
let mut seen = BTreeSet::new();
for field in fields {
if let Object::Reference(field_ref) = field {
walk_signature_field(pdf, field_ref, "", &mut output, &mut seen, 0, max_depth)?;
}
}
Ok(output)
}
pub fn would_rewrite_invalidate_signatures<R: Read + Seek>(
pdf: &mut Pdf<R>,
options: &WriteOptions,
) -> Result<bool> {
Ok(
signature_rewrite_impact(pdf, SignatureWriteMode::from_options(options))?
.invalidates_signatures,
)
}
pub fn acroform_sig_flags<R: Read + Seek>(pdf: &mut Pdf<R>) -> Result<Option<u32>> {
Ok(resolve_catalog_acroform(pdf)?
.and_then(|(_, acroform)| sig_flags_from_acroform_dict(&acroform)))
}
pub fn clear_sig_flags<R: Read + Seek>(pdf: &mut Pdf<R>) -> Result<bool> {
let Some((home, mut acroform)) = resolve_catalog_acroform(pdf)? else {
return Ok(false);
};
if !clear_sig_flags_in_dict(&mut acroform) {
return Ok(false);
}
match home {
AcroformHome::Object(acroform_ref) => {
pdf.set_object(acroform_ref, Object::Dictionary(acroform));
}
AcroformHome::Inline {
root_ref,
mut catalog,
} => {
catalog.insert("AcroForm", Object::Dictionary(acroform));
pdf.set_object(root_ref, Object::Dictionary(catalog));
}
}
Ok(true)
}
pub fn strip_signature_values<R: Read + Seek>(pdf: &mut Pdf<R>) -> Result<bool> {
let Some((_, mut acroform)) = resolve_catalog_acroform(pdf)? else {
return Ok(false);
};
let Some(fields_obj) = acroform.remove("Fields") else {
return Ok(false);
};
let mut changed = false;
let mut seen = BTreeSet::new();
for field in resolve_array(pdf, fields_obj)? {
let Object::Reference(field_ref) = field else {
continue;
};
strip_signature_values_from_field(pdf, field_ref, None, 0, &mut seen, &mut changed)?;
}
Ok(changed)
}
enum AcroformHome {
Object(ObjectRef),
Inline {
root_ref: ObjectRef,
catalog: Dictionary,
},
}
fn resolve_catalog_acroform<R: Read + Seek>(
pdf: &mut Pdf<R>,
) -> Result<Option<(AcroformHome, Dictionary)>> {
let Some(root_ref) = pdf.root_ref() else {
return Ok(None);
};
let Object::Dictionary(catalog) = pdf.resolve(root_ref)? else {
return Ok(None);
};
let Some(acroform) = catalog.get("AcroForm").cloned() else {
return Ok(None);
};
match acroform {
Object::Reference(acroform_ref) => match pdf.resolve(acroform_ref)? {
Object::Dictionary(dict) => Ok(Some((AcroformHome::Object(acroform_ref), dict))),
_ => Ok(None),
},
Object::Dictionary(dict) => Ok(Some((AcroformHome::Inline { root_ref, catalog }, dict))),
_ => Ok(None),
}
}
fn sig_flags_from_acroform_dict(acroform: &Dictionary) -> Option<u32> {
acroform
.get("SigFlags")
.and_then(Object::as_integer)
.and_then(|n| u32::try_from(n).ok())
}
fn clear_sig_flags_in_dict(acroform: &mut Dictionary) -> bool {
let Some(flags) = sig_flags_from_acroform_dict(acroform) else {
return false;
};
let cleared = flags & !(SIG_FLAGS_SIGNATURES_EXIST | SIG_FLAGS_APPEND_ONLY);
if cleared == flags {
return false;
}
acroform.insert("SigFlags", Object::Integer(i64::from(cleared)));
true
}
pub fn signature_rewrite_impact<R: Read + Seek>(
pdf: &mut Pdf<R>,
mode: SignatureWriteMode,
) -> Result<SignatureRewriteImpact> {
let touched_object_refs = pdf.dirty_object_refs();
let touched: BTreeSet<ObjectRef> = touched_object_refs.iter().copied().collect();
let rewrite_info = collect_signature_rewrite_info(pdf)?;
let has_signatures = !rewrite_info.signed_object_refs.is_empty();
let mut signed_object_refs: Vec<ObjectRef> =
rewrite_info.signed_object_refs.iter().copied().collect();
signed_object_refs.sort();
if !has_signatures {
return Ok(SignatureRewriteImpact {
has_signatures: false,
invalidates_signatures: false,
reason: SignatureRewriteReason::NoSignatures,
first_invalidating_ref: None,
signed_object_refs,
touched_object_refs,
acroform_ref: rewrite_info.acroform_ref,
sig_flags: rewrite_info.sig_flags,
});
}
if mode == SignatureWriteMode::FullRewrite {
return Ok(SignatureRewriteImpact {
has_signatures: true,
invalidates_signatures: true,
reason: SignatureRewriteReason::FullRewrite,
first_invalidating_ref: None,
signed_object_refs,
touched_object_refs,
acroform_ref: rewrite_info.acroform_ref,
sig_flags: rewrite_info.sig_flags,
});
}
if let Some(acroform_ref) = rewrite_info.acroform_ref {
if touched.contains(&acroform_ref) {
return Ok(SignatureRewriteImpact {
has_signatures: true,
invalidates_signatures: true,
reason: SignatureRewriteReason::IncrementalTouchesAcroForm,
first_invalidating_ref: Some(acroform_ref),
signed_object_refs,
touched_object_refs,
acroform_ref: Some(acroform_ref),
sig_flags: rewrite_info.sig_flags,
});
}
}
if let Some(object_ref) = signed_object_refs
.iter()
.copied()
.find(|object_ref| touched.contains(object_ref))
{
return Ok(SignatureRewriteImpact {
has_signatures: true,
invalidates_signatures: true,
reason: SignatureRewriteReason::IncrementalTouchesSignedObject,
first_invalidating_ref: Some(object_ref),
signed_object_refs,
touched_object_refs,
acroform_ref: rewrite_info.acroform_ref,
sig_flags: rewrite_info.sig_flags,
});
}
Ok(SignatureRewriteImpact {
has_signatures: true,
invalidates_signatures: false,
reason: SignatureRewriteReason::IncrementalPreservesSignedByteRanges,
first_invalidating_ref: None,
signed_object_refs,
touched_object_refs,
acroform_ref: rewrite_info.acroform_ref,
sig_flags: rewrite_info.sig_flags,
})
}
#[derive(Debug, Default)]
struct SignatureRewriteInfo {
acroform_ref: Option<ObjectRef>,
signed_object_refs: BTreeSet<ObjectRef>,
sig_flags: Option<u32>,
}
fn collect_signature_rewrite_info<R: Read + Seek>(
pdf: &mut Pdf<R>,
) -> Result<SignatureRewriteInfo> {
let mut info = SignatureRewriteInfo::default();
if let Some(root_ref) = pdf.root_ref() {
let root = pdf.resolve(root_ref)?;
if let Object::Dictionary(catalog) = root {
if let Some(acroform) = catalog.get("AcroForm").cloned() {
let (acroform_ref, acroform_dict) = resolve_acroform(pdf, root_ref, acroform)?;
info.acroform_ref = Some(acroform_ref);
info.sig_flags = sig_flags_from_acroform_dict(&acroform_dict);
collect_acroform_signatures(pdf, &acroform_dict, &mut info)?;
}
for key in ["Perms", "DSS"] {
if let Some(value) = catalog.get(key).cloned() {
collect_known_signature_value(pdf, value, &mut info, 0)?;
}
}
}
}
Ok(info)
}
fn resolve_acroform<R: Read + Seek>(
pdf: &mut Pdf<R>,
root_ref: ObjectRef,
acroform: Object,
) -> Result<(ObjectRef, Dictionary)> {
match acroform {
Object::Reference(acroform_ref) => {
let object = pdf.resolve(acroform_ref)?;
match object {
Object::Dictionary(dict) => Ok((acroform_ref, dict)),
_ => Ok((acroform_ref, Dictionary::new())),
}
}
Object::Dictionary(dict) => Ok((root_ref, dict)),
_ => Ok((root_ref, Dictionary::new())),
}
}
fn collect_acroform_signatures<R: Read + Seek>(
pdf: &mut Pdf<R>,
acroform: &Dictionary,
info: &mut SignatureRewriteInfo,
) -> Result<()> {
let Some(fields) = acroform.get("Fields").cloned() else {
return Ok(());
};
for field in resolve_array(pdf, fields)? {
if let Object::Reference(field_ref) = field {
walk_signature_rewrite_field(pdf, field_ref, None, 0, info)?;
}
}
Ok(())
}
fn strip_signature_values_from_field<R: Read + Seek>(
pdf: &mut Pdf<R>,
field_ref: ObjectRef,
inherited_type: Option<Vec<u8>>,
depth: usize,
seen: &mut BTreeSet<ObjectRef>,
changed: &mut bool,
) -> Result<()> {
if depth > DEFAULT_MAX_SIGNATURE_FIELD_DEPTH || !seen.insert(field_ref) {
return Ok(());
}
let Object::Dictionary(mut dict) = pdf.resolve(field_ref)? else {
return Ok(());
};
let field_type = inherited_name(pdf, &dict, "FT")?.or(inherited_type);
let kids_obj = dict.get("Kids").cloned();
let signature_value_ref = dict.get("V").and_then(Object::as_ref_id);
if field_type.as_deref() == Some(b"Sig") && dict.remove("V").is_some() {
pdf.set_object(field_ref, Object::Dictionary(dict));
if let Some(signature_ref) = signature_value_ref {
pdf.delete_object(signature_ref);
}
*changed = true;
if depth == DEFAULT_MAX_SIGNATURE_FIELD_DEPTH {
return Ok(());
}
let Some(kids_obj) = kids_obj else {
return Ok(());
};
return strip_signature_values_from_kids(pdf, kids_obj, field_type, depth, seen, changed);
}
if depth == DEFAULT_MAX_SIGNATURE_FIELD_DEPTH {
return Ok(());
}
let Some(kids_obj) = kids_obj else {
return Ok(());
};
strip_signature_values_from_kids(pdf, kids_obj, field_type, depth, seen, changed)
}
fn strip_signature_values_from_kids<R: Read + Seek>(
pdf: &mut Pdf<R>,
kids_obj: Object,
field_type: Option<Vec<u8>>,
depth: usize,
seen: &mut BTreeSet<ObjectRef>,
changed: &mut bool,
) -> Result<()> {
for kid in resolve_array(pdf, kids_obj)? {
let Object::Reference(kid_ref) = kid else {
continue;
};
let kid_obj = pdf.resolve_borrowed(kid_ref)?;
let Object::Dictionary(kid_dict) = kid_obj else {
continue;
};
if is_pure_widget(kid_dict) {
continue;
}
strip_signature_values_from_field(
pdf,
kid_ref,
field_type.clone(),
depth + 1,
seen,
changed,
)?;
}
Ok(())
}
fn walk_signature_rewrite_field<R: Read + Seek>(
pdf: &mut Pdf<R>,
field_ref: ObjectRef,
inherited_ft: Option<Vec<u8>>,
depth: usize,
info: &mut SignatureRewriteInfo,
) -> Result<()> {
if depth > DEFAULT_MAX_SIGNATURE_FIELD_DEPTH {
return Err(crate::Error::Unsupported(format!(
"signature field-tree depth limit {DEFAULT_MAX_SIGNATURE_FIELD_DEPTH} exceeded at {field_ref}"
)));
}
let Object::Dictionary(dict) = pdf.resolve(field_ref)? else {
return Ok(());
};
let field_type = inherited_name(pdf, &dict, "FT")?.or(inherited_ft);
if field_type.as_deref() == Some(b"Sig") {
info.signed_object_refs.insert(field_ref);
if let Some(value) = dict.get("V").cloned() {
collect_signature_value(pdf, value, info)?;
}
}
if dict.get("ByteRange").is_some() {
info.signed_object_refs.insert(field_ref);
}
if let Some(kids) = dict.get("Kids").cloned() {
for kid in resolve_array(pdf, kids)? {
if let Object::Reference(kid_ref) = kid {
walk_signature_rewrite_field(pdf, kid_ref, field_type.clone(), depth + 1, info)?;
}
}
}
Ok(())
}
fn collect_known_signature_value<R: Read + Seek>(
pdf: &mut Pdf<R>,
value: Object,
info: &mut SignatureRewriteInfo,
depth: usize,
) -> Result<()> {
if depth > DEFAULT_MAX_SIGNATURE_FIELD_DEPTH {
return Err(crate::Error::Unsupported(format!(
"signature known-container depth limit {DEFAULT_MAX_SIGNATURE_FIELD_DEPTH} exceeded"
)));
}
match value {
Object::Reference(object_ref) => {
let object = pdf.resolve(object_ref)?;
if object_has_byte_range(&object) {
info.signed_object_refs.insert(object_ref);
}
collect_known_signature_value(pdf, object, info, depth + 1)?;
}
Object::Dictionary(dict) => {
for (_, value) in dict.iter() {
collect_known_signature_value(pdf, value.clone(), info, depth + 1)?;
}
}
Object::Array(items) => {
for item in items {
collect_known_signature_value(pdf, item, info, depth + 1)?;
}
}
Object::Stream(stream) if stream.dict.get("ByteRange").is_some() => {
for (_, value) in stream.dict.iter() {
collect_known_signature_value(pdf, value.clone(), info, depth + 1)?;
}
}
_ => {}
}
Ok(())
}
fn collect_signature_value<R: Read + Seek>(
pdf: &mut Pdf<R>,
value: Object,
info: &mut SignatureRewriteInfo,
) -> Result<()> {
match value {
Object::Reference(sig_ref) => {
let object = pdf.resolve(sig_ref)?;
if object_has_byte_range(&object) {
info.signed_object_refs.insert(sig_ref);
}
}
object if object_has_byte_range(&object) => {}
_ => {}
}
Ok(())
}
fn walk_signature_field<R: Read + Seek>(
pdf: &mut Pdf<R>,
field_ref: ObjectRef,
parent_name: &str,
output: &mut Vec<SignatureInfo>,
seen: &mut BTreeSet<ObjectRef>,
depth: usize,
max_depth: usize,
) -> Result<()> {
if depth > max_depth || !seen.insert(field_ref) {
return Ok(());
}
let field_obj = pdf.resolve_borrowed(field_ref)?;
let Object::Dictionary(field_dict) = field_obj else {
return Ok(());
};
let field_dict = field_dict.clone();
let field_name = join_field_name(parent_name, text_entry(pdf, &field_dict, "T")?);
if inherited_name(pdf, &field_dict, "FT")?.as_deref() == Some(b"Sig") {
if let Some(info) = signature_info_for_field(pdf, field_ref, &field_name, &field_dict)? {
output.push(info);
}
}
if depth == max_depth {
return Ok(());
}
let Some(kids_obj) = field_dict.get("Kids").cloned() else {
return Ok(());
};
for kid in resolve_array(pdf, kids_obj)? {
let Object::Reference(kid_ref) = kid else {
continue;
};
let kid_obj = pdf.resolve_borrowed(kid_ref)?;
let Object::Dictionary(kid_dict) = kid_obj else {
continue;
};
if is_pure_widget(kid_dict) {
continue;
}
walk_signature_field(
pdf,
kid_ref,
&field_name,
output,
seen,
depth + 1,
max_depth,
)?;
}
Ok(())
}
fn signature_info_for_field<R: Read + Seek>(
pdf: &mut Pdf<R>,
field_ref: ObjectRef,
field_name: &str,
field_dict: &Dictionary,
) -> Result<Option<SignatureInfo>> {
let Some(value) = inherited_field_value(pdf, field_dict, "V")? else {
return Ok(None);
};
let signature_ref = value.as_ref_id();
let Some(signature_dict) = resolve_dictionary(pdf, value)? else {
return Ok(None);
};
let Some(byte_range_obj) = signature_dict.get("ByteRange").cloned() else {
return Ok(None);
};
let byte_range = parse_byte_range(pdf, byte_range_obj)?;
Ok(Some(SignatureInfo {
field_ref,
signature_ref,
field_name: field_name.to_string(),
byte_range,
sub_filter: name_entry(pdf, &signature_dict, "SubFilter")?,
signer_name: text_entry(pdf, &signature_dict, "Name")?,
signing_time: text_entry(pdf, &signature_dict, "M")?,
reason: text_entry(pdf, &signature_dict, "Reason")?,
location: text_entry(pdf, &signature_dict, "Location")?,
contact_info: text_entry(pdf, &signature_dict, "ContactInfo")?,
certificate: certificate_entry(pdf, &signature_dict)?,
}))
}
fn resolve_dictionary<R: Read + Seek>(
pdf: &mut Pdf<R>,
value: Object,
) -> Result<Option<Dictionary>> {
match value {
Object::Dictionary(dict) => Ok(Some(dict)),
Object::Reference(object_ref) => match pdf.resolve_borrowed(object_ref)? {
Object::Dictionary(dict) => Ok(Some(dict.clone())),
_ => Ok(None),
},
_ => Ok(None),
}
}
fn resolve_array<R: Read + Seek>(pdf: &mut Pdf<R>, value: Object) -> Result<Vec<Object>> {
match value {
Object::Array(values) => Ok(values),
Object::Reference(object_ref) => match pdf.resolve(object_ref)? {
Object::Array(values) => Ok(values),
_ => Ok(Vec::new()),
},
_ => Ok(Vec::new()),
}
}
fn parse_byte_range<R: Read + Seek>(pdf: &mut Pdf<R>, value: Object) -> Result<[u64; 4]> {
let values = match value {
Object::Array(values) => values,
Object::Reference(object_ref) => match pdf.resolve_borrowed(object_ref)? {
Object::Array(values) => values.clone(),
_ => return Err(invalid_byte_range("must be an array")),
},
_ => return Err(invalid_byte_range("must be an array")),
};
if values.len() != 4 {
return Err(invalid_byte_range("must contain exactly four integers"));
}
let mut out = [0; 4];
for (idx, value) in values.iter().enumerate() {
let Some(n) = value.as_integer() else {
return Err(invalid_byte_range("must contain only integers"));
};
out[idx] = u64::try_from(n)
.map_err(|_| invalid_byte_range("must contain non-negative integers"))?;
}
Ok(out)
}
fn invalid_byte_range(message: &'static str) -> Error {
Error::parse(0, format!("invalid signature /ByteRange: {message}"))
}
fn inherited_name<R: Read + Seek>(
pdf: &mut Pdf<R>,
field_dict: &Dictionary,
key: &str,
) -> Result<Option<Vec<u8>>> {
match inherited_field_value(pdf, field_dict, key)? {
Some(Object::Name(name)) => Ok(Some(name)),
Some(Object::Reference(object_ref)) => match pdf.resolve_borrowed(object_ref)? {
Object::Name(name) => Ok(Some(name.clone())),
_ => Ok(None),
},
_ => Ok(None),
}
}
fn inherited_field_value<R: Read + Seek>(
pdf: &mut Pdf<R>,
field_dict: &Dictionary,
key: &str,
) -> Result<Option<Object>> {
if let Some(local) = field_dict.get(key).cloned() {
return Ok(Some(local));
}
let mut parent = field_dict.get("Parent").cloned();
let mut seen = BTreeSet::new();
while let Some(Object::Reference(parent_ref)) = parent {
if !seen.insert(parent_ref) {
break;
}
match pdf.resolve_borrowed(parent_ref)? {
Object::Dictionary(parent_dict) => {
if let Some(value) = parent_dict.get(key).cloned() {
return Ok(Some(value));
}
parent = parent_dict.get("Parent").cloned();
}
_ => break,
}
}
Ok(None)
}
fn join_field_name(parent_name: &str, local_name: Option<String>) -> String {
let local_name = local_name.unwrap_or_default();
if parent_name.is_empty() {
local_name
} else if local_name.is_empty() {
parent_name.to_string()
} else {
format!("{parent_name}.{local_name}")
}
}
fn is_pure_widget(dict: &Dictionary) -> bool {
let is_widget = matches!(
dict.get("Subtype"),
Some(Object::Name(name)) if name.as_slice() == b"Widget"
);
let has_field_entries = dict.get("T").is_some()
|| dict.get("FT").is_some()
|| dict.get("Kids").is_some()
|| dict.get("V").is_some()
|| dict.get("DV").is_some()
|| dict.get("Ff").is_some()
|| dict.get("TU").is_some()
|| dict.get("TM").is_some();
is_widget && !has_field_entries
}
fn resolve_entry<R: Read + Seek>(
pdf: &mut Pdf<R>,
dict: &Dictionary,
key: &str,
) -> Result<Option<Object>> {
match dict.get(key) {
Some(Object::Reference(object_ref)) => Ok(Some(pdf.resolve(*object_ref)?)),
Some(object) => Ok(Some(object.clone())),
None => Ok(None),
}
}
fn name_entry<R: Read + Seek>(
pdf: &mut Pdf<R>,
dict: &Dictionary,
key: &str,
) -> Result<Option<String>> {
match resolve_entry(pdf, dict, key)? {
Some(Object::Name(name)) => Ok(Some(String::from_utf8_lossy(&name).into_owned())),
_ => Ok(None),
}
}
fn text_entry<R: Read + Seek>(
pdf: &mut Pdf<R>,
dict: &Dictionary,
key: &str,
) -> Result<Option<String>> {
match resolve_entry(pdf, dict, key)? {
Some(Object::String(bytes)) => Ok(Some(
decode_pdf_text_string(&bytes)
.unwrap_or_else(|| String::from_utf8_lossy(&bytes).into()),
)),
_ => Ok(None),
}
}
fn certificate_entry<R: Read + Seek>(
pdf: &mut Pdf<R>,
dict: &Dictionary,
) -> Result<Option<Vec<u8>>> {
match resolve_entry(pdf, dict, "Cert")? {
Some(Object::String(bytes)) => Ok(Some(bytes)),
Some(Object::Array(values)) => {
for value in values {
match value {
Object::String(bytes) => return Ok(Some(bytes)),
Object::Reference(object_ref) => {
if let Object::String(bytes) = pdf.resolve(object_ref)? {
return Ok(Some(bytes));
}
}
_ => {}
}
}
Ok(None)
}
_ => Ok(None),
}
}
fn object_has_byte_range(object: &Object) -> bool {
match object {
Object::Dictionary(dict) => dict.get("ByteRange").is_some(),
Object::Stream(stream) => stream.dict.get("ByteRange").is_some(),
_ => false,
}
}