pub struct Parser<'a> { /* private fields */ }Expand description
A parser over PDF bytes, positioned at a byte offset.
Used for single objects (Parser::object) and for the file-structure layer
(Parser::object_header, Parser::indirect_object).
Added in 0.2.0.
Implementations§
Source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcepub const fn new(data: &'a [u8], pos: usize) -> Self
pub const fn new(data: &'a [u8], pos: usize) -> Self
A parser over data, starting at byte offset pos, with the default Limits.
Added in 0.2.0.
Sourcepub const fn with_limits(self, limits: &Limits) -> Self
pub const fn with_limits(self, limits: &Limits) -> Self
Applies limits (Limits::max_nesting_depth and Limits::max_string_length).
Added in 0.2.0.
Sourcepub const fn reached_end(&self) -> bool
pub const fn reached_end(&self) -> bool
Whether parsing so far has looked for a byte past the end of the input: to finish a
token (Lexer::reached_end), or to find a stream’s endstream.
When it has not, the result (value or error) depends only on bytes before the end, so
parsing a window of a larger input gave the same result as parsing the whole input.
laminapdf-cos relies on this to read sources without an in-memory slice in windows.
Added in 0.2.0.
Sourcepub const fn skipped_keys(&self) -> bool
pub const fn skipped_keys(&self) -> bool
Whether a dictionary parsed so far by this parser, in any object, had something other
than a name where a key belongs; it is never reset. Such a token is skipped, as pdf.js
and PDFium do, and parsing goes on with the next one: a number, string or other keyword,
a whole array or dictionary (with a reference n g R as one), a stray ], or an
unbalanced ) or >. An object keyword (obj, endobj, stream, endstream) still
ends the parse with an error: the dictionary was never closed. Callers report it
(ADR 0003 §1).
Added in 0.4.0.
Sourcepub fn object(&mut self) -> Result<Object>
pub fn object(&mut self) -> Result<Object>
Parses one direct object (references resolved lexically as n g R).
§Errors
Malformed syntax, truncation, or a hard limit exceeded (Limits::max_nesting_depth,
Limits::max_string_length).
Added in 0.2.0.
Sourcepub fn object_header(&mut self) -> Result<ObjRef>
pub fn object_header(&mut self) -> Result<ObjRef>
Reads num gen obj at the current position.
§Errors
Error::Syntax when the next tokens are not an object header.
Added in 0.2.0.
Sourcepub fn indirect_object(
&mut self,
resolve_length: &mut dyn FnMut(ObjRef) -> Option<i64>,
) -> Result<IndirectObject>
pub fn indirect_object( &mut self, resolve_length: &mut dyn FnMut(ObjRef) -> Option<i64>, ) -> Result<IndirectObject>
Parses num gen obj <object> [stream…endstream] endobj at the current position.
resolve_length resolves an indirect /Length; when it fails or the length is
wrong, the stream extent is recovered by scanning for endstream.
§Errors
A malformed header or object, or a stream without any endstream.
Added in 0.2.0.
Sourcepub fn indirect_head(
&mut self,
resolve_length: &mut dyn FnMut(ObjRef) -> Option<i64>,
) -> Result<IndirectHead>
pub fn indirect_head( &mut self, resolve_length: &mut dyn FnMut(ObjRef) -> Option<i64>, ) -> Result<IndirectHead>
The first part of Parser::indirect_object: the header and the value, and for a
stream, its dictionary, where its data starts and its declared /Length (resolved
with resolve_length when indirect), without looking for its end.
For callers that find a stream’s extent themselves (laminapdf-cos reading a source in
windows checks a /Length with a small read at the declared end instead of reading
up to it). endstream_at and stream_data_end are the checks
Parser::indirect_object makes, so both give the same result.
§Errors
A malformed header or object.
Added in 0.2.0.
Sourcepub const fn lexer(&mut self) -> &mut Lexer<'a>
pub const fn lexer(&mut self) -> &mut Lexer<'a>
The lexer this parser reads tokens from, for callers that mix tokens and objects (an
xref table followed by its trailer). Reads through it count for
Parser::reached_end.
Added in 0.2.0.