Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Format:PDF

From ReverseEngineering.wiki
Format
Versions %PDF-1.0 to 1.7, ISO 32000-2 (PDF 2.0)
Magic 25 50 44 46 2D ('%PDF-') in the first 1024 bytes
Extensions .pdf
Endianness n/a (ASCII syntax; xref stream fields big endian)
On this wiki Facts · Code · Notes · Topics

Adobe/ISO 32000 Portable Document Format (.pdf): a text-and-binary file starting %PDF-1.x with numbered objects, a cross-reference table or cross-reference stream located by startxref, a trailer dictionary, optional object streams and incremental updates, and an optional linearization dictionary and /Encrypt dictionary.

Overview

A PDF is a body of numbered indirect objects (12 0 obj ... endobj: object number, generation) found through a cross-reference (xref) structure, with a trailer dictionary whose /Root points to the catalog. Reversers meet PDFs as documents inside programs and games, as fonts and images embedded in them (Format:OpenType font file data lives in /FontFile2 and /FontFile3 streams) and as malware carriers. The syntax below was read in qpdf's parser.

Locating the structure

  • qpdf looks for %PDF- anywhere in the first 1024 bytes of the file, not only at offset 0 (leading junk is tolerated). Without it, it warns and assumes version 1.2.
  • It searches backwards for startxref in the last 1054 bytes (1024 for %%EOF plus 30 for the keyword and number); the integer after startxref is the byte offset of the newest xref section.
  • If that fails, or the offset does not lead to an xref, qpdf reconstructs the table by scanning the whole file for N G obj headers. A damaged startxref therefore does not hide objects from tools, only from strict readers.

Two xref forms

  • Classic table: keyword xref, then subsections first count followed by count fixed 20-byte lines oooooooooo ggggg n or f (offset, generation, in-use or free) followed by trailer and the trailer dictionary.
  • Cross-reference stream (PDF 1.5+): the startxref offset points to an object N G obj << /Type /XRef /Size .. /W [a b c] /Index [..] /Root .. >> stream. Entries are fixed-width big-endian fields of widths /W; a zero width for field 1 means the default type 1; field 1 is the type (0 free, 1 uncompressed at byte offset, 2 compressed in an object stream), see Fact:PDF/behavior/Cross-reference stream entries; the classic entry is Fact:PDF/struct/Classic xref table entry.
  • Hybrid files carry both: the trailer of the classic table has /XRefStm pointing to a cross-reference stream for objects in object streams.

Incremental updates

An update appends new object versions, a new xref section and a new trailer whose /Prev holds the byte offset of the previous xref section; readers follow the chain and the first definition of an object found (newest) wins. A file with several %%EOF markers is therefore a history: earlier revisions (deleted text, old signatures) stay in the file.

Object streams

Objects of PDF 1.5 files may be packed in a stream with /Type /ObjStm, /N (count) and /First (offset of the first object in the decoded data); the decoded data starts with N pairs objectNumber offset and the objects follow. Unverified: streams (including other object streams and the xref stream) cannot be inside an object stream (a rule of the specification). qpdf warns "object stream claims to contain itself" (QPDF_objects.cc line 1797 at 4eba958998).

Linearization

A linearized ("fast web view") file has as its first object a dictionary with /Linearized 1, /L (the file length, which must equal the real length or qpdf treats the file as not linearized), /H (hint stream offset and length, plus two more values for an overflow stream), /O (first page object number), /E (end of the first page section), /N (page count), /T (offset of the main xref's first entry) and optionally /P. See Fact:PDF/behavior/Linearization parameter dictionary.

Encryption (identification only)

The trailer has /Encrypt pointing to an encryption dictionary; /Filter /Standard with /V (algorithm) and /R (revision). qpdf accepts V = 1, 2, 4, 5 and R = 2 to 6. Only recognize the dictionary; do not recover passwords. See Fact:PDF/enum/Standard security handler V and R accepted.

Code & repositories

(see the linked repository below)

  • https://github.com/qpdf/qpdf @ 4eba95899886e851cc41d76886483b347612f2a8 · license: Apache-2.0
    qpdf (Apache-2.0): PDF structure tool and C++ library; libqpdf/QPDF_objects.cc has header/startxref search, xref table and stream parsing, object streams and reconstruction; QPDF_linearization.cc the linearization dictionary; QPDF_encryption.cc the security handler checks.
    Tested on: source read, not run · checked 2026-10-18; last activity 2026-09-26; Note: the latest release ships binaries; the wiki links only the source, never the binaries

Open questions

  • Settled: in a cross-reference stream a type-2 entry stores the object stream number in field 2 and the index inside it in field 3: qpdf QPDF_objects.cc (4eba958998) passes f1 and f2 to QPDFXRefEntry(toI(f1), f2) for type 2 (line 1182-1183), and prints them as "stream = getObjStreamNumber(), index = getObjStreamIndex()" (lines 1213-1215); the dispatcher calls resolveObjectsInStream(entry.getObjStreamNumber()) (line 1693).
  • Unverified: R = 5 is Adobe's extension level for AES-256, R = 6 is ISO 32000-2.