filter

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: MPL-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package filter implements the non-image PDF stream filters (FlateDecode, LZWDecode, ASCIIHexDecode, ASCII85Decode, and RunLengthDecode), the PNG and TIFF predictor transforms, and bounded chain application. The image-only filters (DCTDecode, CCITTFaxDecode, JBIG2Decode, JPXDecode) belong to internal/imaging and are rejected here, as is the Crypt filter (internal/cos drops Identity crypt filters before building a chain and rejects named ones; document-level encryption is undone at parse time by internal/crypt).

Two caps keep hostile input from forcing unbounded work: a chain may apply at most MaxChainLength filters, and each stage's output may not exceed MaxDecodedSize(len(input)) bytes. These caps guarantee termination; there are no timeouts.

Decoding is otherwise fault-tolerant, matching the warn-and-continue behavior of widely deployed readers: corrupt input that still yields some output returns that partial output without an error. Resource-limit violations are always hard errors.

Index

Constants

View Source
const MaxChainLength = 8

MaxChainLength is the maximum number of filters DecodeChain applies. The spec places no limit on chain length, but no legitimate producer chains more than two or three filters; the cap stops hostile input from forcing unbounded decompression rounds.

Variables

View Source
var (
	ErrChainTooLong      = errors.New("filter chain is too long")
	ErrTooLarge          = errors.New("decoded stream exceeds the size limit")
	ErrUnsupportedFilter = errors.New("unsupported filter")
)

Errors returned by this package.

Functions

func Decode

func Decode(spec Spec, data []byte, maxSize int) ([]byte, error)

Decode applies a single filter to data, capping the output at maxSize bytes. The returned slice never aliases data. Decode owns and may modify its result buffers, but never data itself.

func DecodeChain

func DecodeChain(specs []Spec, data []byte) (out []byte, decoded int, err error)

DecodeChain applies each filter in specs to data in order, enforcing MaxChainLength and capping every stage's output at MaxDecodedSize(len(data)) bytes. It returns the decoded bytes and the number of bytes the chain PRODUCED, summed over its stages, which a caller charging a work budget cannot infer from the result: a failed chain returns no bytes, yet a stage that reports ErrTooLarge inflated the entire MaxDecodedSize allowance first, so a 64 KB zip bomb reports ~64 MB of work alongside its error. data is never modified; the result may alias it only when specs is empty.

func MaxDecodedSize

func MaxDecodedSize(inputLen int) int

MaxDecodedSize returns the largest output each decoding stage may produce for an original input of inputLen bytes: max(64 MB, 256 × inputLen). The generous fixed floor accommodates small streams that legitimately expand enormously (such as xref streams and bitmap data), while the multiplier scales the allowance for large inputs.

Types

type Params

type Params struct {
	// Predictor selects the predictor transform applied after Flate or LZW decoding: 1 = none, 2 = TIFF horizontal
	// differencing, 10-15 = the PNG filters (the specific value is irrelevant on decode; each row carries its own PNG
	// filter type byte).
	Predictor int
	// Colors is the number of interleaved color components per sample (predictor transforms only).
	Colors int
	// BitsPerComponent is the number of bits per color component (predictor transforms only).
	BitsPerComponent int
	// Columns is the number of samples per row (predictor transforms only).
	Columns int
	// EarlyChange selects the LZW code-width change convention: 1 (the default) increases the code width one code
	// early, 0 increases it at the standard point.
	EarlyChange int
}

Params holds the decode parameters (from a stream's /DecodeParms dictionary) that the filters in this package consume. The zero value is not meaningful; start from DefaultParams.

func DefaultParams

func DefaultParams() Params

DefaultParams returns Params with the defaults ISO 32000-2 assigns when /DecodeParms omits a key.

type Spec

type Spec struct {
	// Name is the PDF filter name, e.g. "FlateDecode". Abbreviated inline-image names are accepted too.
	Name string
	// Params holds the decode parameters for this filter.
	Params Params
}

Spec names one filter application within a chain.

Jump to

Keyboard shortcuts

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