MCPcopy Create free account
hub / github.com/LibPDF-js/core / findStartXRef

Method findStartXRef

src/parser/xref-parser.ts:46–87  ·  view source on GitHub ↗

* Find the startxref offset by scanning backwards from end of file. * Returns the byte offset where xref starts.

()

Source from the content-addressed store, hash-verified

44 * Returns the byte offset where xref starts.
45 */
46 findStartXRef(): number {
47 const bytes = this.scanner.bytes;
48 const len = bytes.length;
49
50 // Skip trailing whitespace/null bytes to find the effective end of file.
51 // Some systems pad PDFs with null bytes to block boundaries, which can
52 // push the actual %%EOF / startxref beyond the normal 1024-byte window.
53 let effectiveEnd = len;
54
55 while (effectiveEnd > 0 && isWhitespace(bytes[effectiveEnd - 1])) {
56 effectiveEnd--;
57 }
58
59 // Search backwards from effective end, looking for "startxref"
60 const searchStart = Math.max(0, effectiveEnd - 1024);
61
62 let startxrefPos = -1;
63
64 for (let i = effectiveEnd - 9; i >= searchStart; i--) {
65 if (this.matchesAt(i, "startxref")) {
66 startxrefPos = i;
67 break;
68 }
69 }
70
71 if (startxrefPos === -1) {
72 throw new XRefParseError("Could not find startxref marker");
73 }
74
75 // Skip "startxref" and whitespace to get the offset number
76 let pos = startxrefPos + 9;
77 pos = this.skipWhitespace(pos);
78
79 // Read the offset number
80 const offset = this.readIntegerAt(pos);
81
82 if (offset === null) {
83 throw new XRefParseError("Invalid startxref offset");
84 }
85
86 return offset;
87 }
88
89 /**
90 * Parse xref at given byte offset.

Callers 4

parseNormalMethod · 0.95
loadMethod · 0.95
reloadMethod · 0.95

Calls 4

matchesAtMethod · 0.95
skipWhitespaceMethod · 0.95
readIntegerAtMethod · 0.95
isWhitespaceFunction · 0.90

Tested by

no test coverage detected