Skip to main content

Parser

Struct Parser 

Source
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>

Source

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.

Source

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.

Source

pub const fn pos(&self) -> usize

The current byte offset.

Added in 0.2.0.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<'a> Clone for Parser<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Parser<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Forget for Parser<'a>

§

impl<'a> Freeze for Parser<'a>

§

impl<'a> Move for Parser<'a>

§

impl<'a> RefUnwindSafe for Parser<'a>

§

impl<'a> Send for Parser<'a>

§

impl<'a> Sync for Parser<'a>

§

impl<'a> Unpin for Parser<'a>

§

impl<'a> UnsafeUnpin for Parser<'a>

§

impl<'a> UnwindSafe for Parser<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.