syntax

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package syntax reads PDF objects from a byte slice.

Nothing here does I/O or follows references: the package turns bytes into values, and internal/file decides which bytes. Values point into the input wherever they can, so the input must outlive them and must not be modified.

Index

Constants

View Source
const DefaultMaxDepth = 100

DefaultMaxDepth is the nesting limit when a Parser is given none. Real files stay below ten.

Variables

View Source
var (
	// ErrSyntax: the bytes at the position do not start an object.
	ErrSyntax = errors.New("pdf syntax error")
	// ErrTooDeep: arrays and dictionaries are nested beyond the limit.
	ErrTooDeep = errors.New("pdf objects nested too deeply")
)

Errors returned by the parser. io.EOF means there was no object left.

Functions

func HasPrefixAt

func HasPrefixAt(data []byte, pos int, s string) bool

HasPrefixAt reports whether data has the token s at pos, followed by a delimiter, white space or the end of the data.

func IsDelim

func IsDelim(c byte) bool

IsDelim reports whether c is a PDF delimiter character.

func IsRegular

func IsRegular(c byte) bool

IsRegular reports whether c is neither white space nor a delimiter.

func IsSpace

func IsSpace(c byte) bool

IsSpace reports whether c is a PDF white-space character.

func SkipSpace

func SkipSpace(data []byte, pos int) int

SkipSpace returns the position of the first byte at or after pos that is not white space and not inside a comment.

func StreamData

func StreamData(data []byte, pos int) (offset int, ok bool)

StreamData reports whether the keyword stream follows at pos (after white space), and if so the offset of the first byte of stream data. The keyword is followed by a line feed, or a carriage return and a line feed; a lone carriage return is tolerated.

func UnescapeString

func UnescapeString(dst, raw []byte) []byte

UnescapeString appends to dst the bytes a literal string body stands for: backslash escapes are resolved and an end of line in any form becomes a line feed.

func Unhex

func Unhex(dst, raw []byte) []byte

Unhex appends to dst the bytes that hexadecimal digits stand for. Anything that is not a digit is ignored, and an odd final digit is taken as the high half of a byte.

Types

type Kind

type Kind uint8

Kind is the type of a Value.

const (
	// Null is the null object, and the zero Value.
	Null Kind = iota
	Bool
	Int
	Real
	// Name holds the name without its leading slash, with #xx decoded.
	Name
	// String holds the decoded bytes of a literal or hexadecimal string.
	String
	// Array holds its elements in List.
	Array
	// Dict holds its entries in List as alternating key and value.
	Dict
	// Ref is an indirect reference: object number in N, generation in Gen.
	Ref
	// Stream is a dictionary followed by stream data. List holds the
	// dictionary entries; N is the offset of the first byte of data in the
	// input; Obj and Gen identify the object, which decryption needs.
	Stream
	// Keyword is any other token of regular characters: obj, endobj,
	// stream, xref, and the operators of a content stream.
	Keyword
)

func (Kind) String

func (k Kind) String() string

type Parser

type Parser struct {

	// Skipped counts the bytes that were skipped as unreadable.
	Skipped int
	// contains filtered or unexported fields
}

Parser reads objects from a byte slice, starting at a position.

It is tolerant inside containers: a stray byte in an array or a dictionary is skipped and counted in Skipped, so that one bad byte does not cost the object.

func NewParser

func NewParser(data []byte, pos, maxDepth int) Parser

NewParser returns a parser positioned at pos. maxDepth bounds the nesting of arrays and dictionaries; 0 means DefaultMaxDepth.

func (*Parser) Object

func (p *Parser) Object() (Value, error)

Object reads the next object. Two integers followed by R are returned as one Ref. At the end of the data it returns io.EOF; at a byte that cannot start an object it returns ErrSyntax and stays at that byte.

func (*Parser) Pos

func (p *Parser) Pos() int

Pos returns the position after the last object read.

func (*Parser) SetPos

func (p *Parser) SetPos(pos int)

SetPos moves the parser.

type Value

type Value struct {
	Kind Kind
	// Hex reports that a String was written in hexadecimal form.
	Hex bool
	Gen uint16
	// Obj is the object number of a Stream.
	Obj uint32
	// N is the value of an Int or Bool (0 or 1), the bits of the float64
	// of a Real (Float and Number read it), the object number of a Ref,
	// or the data offset of a Stream.
	N int64
	// Data is the bytes of a Name, String or Keyword.
	Data []byte
	// List is the elements of an Array or the entries of a Dict or Stream.
	List []Value
}

Value is one PDF object. It is a plain struct, not an interface, so that numbers and names need no allocation of their own.

func ParseNumber

func ParseNumber(data []byte, pos int) (Value, int)

ParseNumber reads the number that starts at pos and returns it with the position after it. A sign or a point with no digits reads as 0.

func RealValue

func RealValue(f float64) Value

RealValue returns a Real of value f.

func (Value) AsDict

func (v Value) AsDict() Value

AsDict returns the dictionary of a Stream as a Dict value, and any other value unchanged.

func (Value) Elems

func (v Value) Elems() []Value

Elems returns the elements of an Array, or nil.

func (Value) Entries

func (v Value) Entries() int

Entries returns the number of entries of a Dict or Stream.

func (Value) Entry

func (v Value) Entry(i int) (key []byte, val Value)

Entry returns the i-th key and value of a Dict or Stream.

func (Value) Equal

func (v Value) Equal(s string) bool

Equal reports whether two keyword or name values hold the same bytes.

func (Value) Float

func (v Value) Float() (float64, bool)

Float returns the value of a number.

func (Value) Get

func (v Value) Get(key string) Value

Get returns the value of key (given without the slash) in a Dict or Stream, or the null Value. When a key occurs twice the first wins.

func (Value) Has

func (v Value) Has(key string) bool

Has reports whether a Dict or Stream has the key.

func (Value) Integer

func (v Value) Integer() (int64, bool)

Integer returns the value of an Int, or of a Real with no fraction that fits, which is how some producers write counts and lengths.

func (Value) IsDict

func (v Value) IsDict() bool

IsDict reports whether v has dictionary entries: a Dict or a Stream.

func (Value) IsName

func (v Value) IsName(s string) bool

IsName reports whether v is the name s (given without the slash).

func (Value) IsNull

func (v Value) IsNull() bool

IsNull reports whether v is the null object.

func (Value) NameString

func (v Value) NameString() string

NameString returns the name without its slash, or "" if v is not a name.

func (Value) Number

func (v Value) Number() float64

Number returns the value of a number, or 0 for anything else.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL