1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! qpdf correspondence: QPDFParser.cc content callbacks.
//! Content-stream object callbacks (ISO 32000-1 §7.8.2).
//!
//! A PDF content stream is a sequence of operands followed by an operator,
//! interleaved with inline images and comments. This module routes the shared
//! tokenizer and [`crate::parser`] through qpdf-shaped
//! [`ObjectHandleParserCallbacks`]. It contains orchestration and event accumulation only;
//! lexical boundaries remain owned by the tokenizer.
//!
//! [`parse_content_operations`] provides the common operand/operator adapter
//! for consumers that do not need inline-image payload events.
use crate::parser::ContentHandleParser;
use crate::tokenizer::{TokenType, Tokenizer, TokenizerStateError};
use crate::{
object_handle::{DocumentResolver, ObjectHandle},
Error, Result,
};
use std::rc::Rc;
/// Whether content-stream parsing should continue after an object callback.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParseControl {
/// Continue parsing the content stream.
Continue,
/// Stop immediately without calling [`ObjectHandleParserCallbacks::handle_eof`].
Stop,
}
/// qpdf's `QPDFObjectHandle::ParserCallbacks` boundary.
///
/// Parsed values are canonical [`ObjectHandle`]s, so callback code
/// can inspect identity and parsed offsets without introducing an
/// ObjectHandle-to-Object consumer bridge.
pub trait ObjectHandleParserCallbacks {
/// Receive the full decoded content size before the first object.
fn content_size(&mut self, _size: usize) -> Result<()> {
Ok(())
}
/// Receive one parsed ObjectHandle and its qpdf content span.
fn handle_object(
&mut self,
object: ObjectHandle,
offset: usize,
length: usize,
) -> Result<ParseControl>;
/// Receive a non-fatal parser recovery diagnostic.
fn handle_diagnostic(&mut self, _offset: usize, _message: &str) -> Result<()> {
Ok(())
}
/// Receive normal content EOF. A [`ParseControl::Stop`] return from
/// `handle_object` skips this callback, matching qpdf's
/// `terminateParsing` path.
fn handle_eof(&mut self) -> Result<()>;
}
/// Parse decoded content bytes into ObjectHandle callbacks.
pub(crate) fn parse_content_stream_handles<C: ObjectHandleParserCallbacks>(
input: &[u8],
context: Option<Rc<dyn DocumentResolver>>,
callbacks: &mut C,
) -> Result<()> {
callbacks.content_size(input.len())?;
let mut tokenizer = Tokenizer::new(input);
tokenizer.allow_eof();
while tokenizer.position() < input.len() {
let probe = tokenizer.read_token(true, 0)?;
let offset = probe.start;
tokenizer.set_position(offset)?;
let (object, length, diagnostics) = {
let mut parser = ContentHandleParser::with_tokenizer(&mut tokenizer, context.clone());
let object = match parser.parse_content_object()? {
Some(object) => object,
None => break,
};
let length = parser.position() - offset;
let diagnostics = parser.take_diagnostics();
(object, length, diagnostics)
};
for diagnostic in diagnostics {
callbacks.handle_diagnostic(diagnostic.relative_offset, &diagnostic.message)?;
}
let is_id = object.as_operator().as_deref() == Some(b"ID");
if callbacks.handle_object(object, offset, length)? == ParseControl::Stop {
return Ok(());
}
if is_id {
// qpdf discards the byte after ID without making a short read an
// exception; the subsequent inline-image token read reports the
// warning-only EOF case (QPDFObjectHandle.cc:1820-1848).
if tokenizer.consume_one_byte().is_err() {
callbacks.handle_diagnostic(input.len(), "EOF found while reading inline image")?;
break;
}
let inline_offset = tokenizer.position();
// The shared tokenizer is reset by consume_one_byte, so this
// state failure is unreachable through this pull route. Keep the
// defensive mapping documented for callers that change tokenizer
// state handling.
// cov:ignore-start: consume_one_byte resets the shared tokenizer; qpdf state errors are unreachable here
tokenizer.expect_inline_image().map_err(|error| {
let message = match error {
TokenizerStateError::TokenWaiting => "tokenizer already has a token waiting",
TokenizerStateError::ImproperInlineImageState => {
"tokenizer is in an improper inline image state"
}
};
Error::parse(inline_offset, message)
})?;
// cov:ignore-end
let image = tokenizer.read_token(true, 0)?;
if image.token_type == TokenType::Bad {
// QPDFObjectHandle::parseContentStream_data warns and lets the
// surrounding parseContentStream_internal deliver handleEOF;
// an incomplete inline image is not a parser exception on this
// owning ObjectHandle route (QPDFObjectHandle.cc:1826-1848).
let diagnostic = "EOF found while reading inline image";
callbacks.handle_diagnostic(image.error_offset, diagnostic)?;
break;
}
let image_offset = image.start;
let image_length = image.end - image.start;
if callbacks.handle_object(
ObjectHandle::inline_image(image.value),
image_offset,
image_length,
)? == ParseControl::Stop
{
return Ok(());
}
}
}
callbacks.handle_eof()
}
/// Accumulates content objects until an operator event is received.
///
/// This adapter deliberately sees only parser events. Lexical boundaries and
/// inline-image discovery remain owned by [`parse_content_stream_handles`].
pub(crate) struct OperationCallbacks<F> {
operands: Vec<ObjectHandle>,
on_operation: F,
}
impl<F> ObjectHandleParserCallbacks for OperationCallbacks<F>
where
F: FnMut(&[ObjectHandle], &[u8]) -> Result<ParseControl>,
{
fn handle_object(
&mut self,
object: ObjectHandle,
_offset: usize,
_length: usize,
) -> Result<ParseControl> {
if let Some(operator) = object.as_operator() {
let control = (self.on_operation)(&self.operands, &operator)?;
self.operands.clear();
Ok(control)
} else if object.as_inline_image().is_some() {
Ok(ParseControl::Continue)
} else {
self.operands.push(object);
Ok(ParseControl::Continue)
}
}
fn handle_eof(&mut self) -> Result<()> {
Ok(())
}
}
/// Parse content and invoke `on_operation` with each operator's accumulated
/// operands.
///
/// Inline-image payload events are ignored by this convenience adapter.
/// Consumers that need inline-image headers or payloads should implement
/// [`ObjectHandleParserCallbacks`] directly.
///
/// # Errors
///
/// Recoverable object-token errors are skipped at parser-owned boundaries.
/// Inline-image/tokenizer state errors and callback errors are propagated.
pub fn parse_content_operations<F>(input: &[u8], on_operation: F) -> Result<()>
where
F: FnMut(&[ObjectHandle], &[u8]) -> Result<ParseControl>,
{
let mut callbacks = OperationCallbacks {
operands: Vec::new(),
on_operation,
};
parse_content_stream_handles(input, None, &mut callbacks)
}