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
- Variables
- func HasPrefixAt(data []byte, pos int, s string) bool
- func IsDelim(c byte) bool
- func IsRegular(c byte) bool
- func IsSpace(c byte) bool
- func SkipSpace(data []byte, pos int) int
- func StreamData(data []byte, pos int) (offset int, ok bool)
- func UnescapeString(dst, raw []byte) []byte
- func Unhex(dst, raw []byte) []byte
- type Kind
- type Parser
- type Value
- func (v Value) AsDict() Value
- func (v Value) Elems() []Value
- func (v Value) Entries() int
- func (v Value) Entry(i int) (key []byte, val Value)
- func (v Value) Equal(s string) bool
- func (v Value) Float() (float64, bool)
- func (v Value) Get(key string) Value
- func (v Value) Has(key string) bool
- func (v Value) Integer() (int64, bool)
- func (v Value) IsDict() bool
- func (v Value) IsName(s string) bool
- func (v Value) IsNull() bool
- func (v Value) NameString() string
- func (v Value) Number() float64
Constants ¶
const DefaultMaxDepth = 100
DefaultMaxDepth is the nesting limit when a Parser is given none. Real files stay below ten.
Variables ¶
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 ¶
HasPrefixAt reports whether data has the token s at pos, followed by a delimiter, white space or the end of the data.
func SkipSpace ¶
SkipSpace returns the position of the first byte at or after pos that is not white space and not inside a comment.
func StreamData ¶
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 ¶
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.
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 )
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 ¶
NewParser returns a parser positioned at pos. maxDepth bounds the nesting of arrays and dictionaries; 0 means DefaultMaxDepth.
func (*Parser) Object ¶
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.
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 ¶
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 (Value) AsDict ¶
AsDict returns the dictionary of a Stream as a Dict value, and any other value unchanged.
func (Value) Get ¶
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) Integer ¶
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) NameString ¶
NameString returns the name without its slash, or "" if v is not a name.