src/pdfmake_parser.c

filter: Statementstmt 63.8% 518 / 811 Branchbran 48.3% 264 / 546 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 57.6% 782 / 1357 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/pdfmake_watermark.c ↑ Summary src/pdfmake_ocg.c »
1 /*
2 * pdfmake_parser.c — PDF object parser + cross-reference reader
3 *
4 * Implementation of recursive-descent parser for PDF objects and
5 * xref table/stream parsing.
6 */
7  
8 #include "pdfmake_parser.h"
9 #include "pdfmake_filter.h"
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <ctype.h>
14  
15 /*============================================================================
16 * Internal helpers
17 *==========================================================================*/
18  
19 /* Set error state */
20 60 static void set_error(pdfmake_parser_t *p, pdfmake_err_t err,
21 size_t offset, const char *msg) {
22 60 p->last_err = err;
23 60 p->err_offset = offset;
24 60 ▶ if (msg) {
Branch: 1 branch
BranchTrueFalseTotal
if (msg) { 60 0 60
25 60 strncpy(p->err_msg, msg, sizeof(p->err_msg) - 1);
26 60 p->err_msg[sizeof(p->err_msg) - 1] = '\0';
27 } else {
28 0 p->err_msg[0] = '\0';
29 }
30 60 }
31  
32 /* Ensure xref table has capacity for object num */
33 2008 static int ensure_xref_cap(pdfmake_parser_t *p, uint32_t num) {
34 size_t new_cap;
35 pdfmake_xref_entry_t *new_xref;
36  
37 2008 ▶ if (num < p->xref_cap) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (num < p->xref_cap) return 1; 1844 164 2008
38
39 164 ▶ new_cap = p->xref_cap ? p->xref_cap * 2 : 64;
Branch: 1 branch
BranchTrueFalseTotal
new_cap = p->xref_cap ? p->xref_cap * 2 : 64; 0 164 164
40 164 ▶ while (new_cap <= num) new_cap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_cap <= num) new_cap *= 2; 0 164 164
41
42 164 new_xref = realloc(p->xref, new_cap * sizeof(pdfmake_xref_entry_t));
43 164 ▶ if (!new_xref) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!new_xref) return 0; 0 164 164
44
45 /* Zero new entries */
46 164 memset(new_xref + p->xref_cap, 0, (new_cap - p->xref_cap) * sizeof(pdfmake_xref_entry_t));
47 164 p->xref = new_xref;
48 164 p->xref_cap = new_cap;
49 164 return 1;
50 }
51  
52 /* Check if offset was already visited in /Prev chain (cycle detection) */
53 48 static int prev_visited(pdfmake_parser_t *p, uint64_t offset) {
54 size_t i;
55 96 ▶ for (i = 0; i < p->prev_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < p->prev_count; i++) { 80 16 96
56 80 ▶ if (p->prev_offsets[i] == offset) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (p->prev_offsets[i] == offset) return 1; 32 48 80
57 }
58 16 return 0;
59 }
60  
61 /* Add offset to /Prev visited list */
62 322 static int prev_add(pdfmake_parser_t *p, uint64_t offset) {
63 322 ▶ if (p->prev_count >= p->prev_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->prev_count >= p->prev_cap) { 163 159 322
64 163 ▶ size_t new_cap = p->prev_cap ? p->prev_cap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = p->prev_cap ? p->prev_cap * 2 : 8; 0 163 163
65 163 uint64_t *new_arr = realloc(p->prev_offsets, new_cap * sizeof(uint64_t));
66 163 ▶ if (!new_arr) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!new_arr) return 0; 0 163 163
67 163 p->prev_offsets = new_arr;
68 163 p->prev_cap = new_cap;
69 }
70 322 p->prev_offsets[p->prev_count++] = offset;
71 322 return 1;
72 }
73  
74 /* Get interned name ID from arena */
75 2691 static uint32_t intern_name(pdfmake_parser_t *p, const char *bytes, size_t len) {
76 2691 return pdfmake_arena_intern_name(p->doc->arena, bytes, len);
77 }
78  
79 /* Decode hex digit */
80 20599 static int hex_digit(int c) {
81 20599 ▶ if (c >= '0' && c <= '9') return c - '0';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') return c - '0'; 20599 0 20599
if (c >= '0' && c <= '9') return c - '0'; 13501 7098 20599
82 7098 ▶ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 1922 5176 7098
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 1922 0 1922
83 5176 ▶ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 5176 0 5176
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 5176 0 5176
84 0 return -1;
85 }
86  
87 /*============================================================================
88 * Parser lifecycle
89 *==========================================================================*/
90  
91 169 pdfmake_parser_t *pdfmake_parser_new(const uint8_t *buf, size_t len) {
92 169 pdfmake_parser_t *p = calloc(1, sizeof(pdfmake_parser_t));
93 169 ▶ if (!p) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return NULL; 0 169 169
94
95 169 p->buf = buf;
96 169 p->buf_len = len;
97 169 pdfmake_tokenizer_init(&p->tok, buf, len);
98
99 169 p->doc = pdfmake_doc_new();
100 169 ▶ if (!p->doc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p->doc) { 0 169 169
101 0 free(p);
102 0 return NULL;
103 }
104
105 169 return p;
106 }
107  
108 169 void pdfmake_parser_free(pdfmake_parser_t *parser) {
109 169 ▶ if (!parser) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!parser) return; 0 169 169
110 169 free(parser->xref);
111 169 free(parser->prev_offsets);
112 /* Note: does NOT free doc - caller owns it */
113 169 free(parser);
114 }
115  
116 145 void pdfmake_parser_set_repair(pdfmake_parser_t *parser, int enable) {
117 145 ▶ if (parser) parser->repair = enable ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
if (parser) parser->repair = enable ? 1 : 0; 145 0 145
118 145 }
119  
120 0 const char *pdfmake_parser_errmsg(pdfmake_parser_t *parser) {
121 0 ▶ return parser ? parser->err_msg : "NULL parser";
Branch: 1 branch
BranchTrueFalseTotal
return parser ? parser->err_msg : "NULL parser"; 0 0 0
122 }
123  
124 0 size_t pdfmake_parser_erroffset(pdfmake_parser_t *parser) {
125 0 ▶ return parser ? parser->err_offset : 0;
Branch: 1 branch
BranchTrueFalseTotal
return parser ? parser->err_offset : 0; 0 0 0
126 }
127  
128 /*============================================================================
129 * PDF header validation
130 *==========================================================================*/
131  
132 312 pdfmake_err_t pdfmake_check_header(pdfmake_parser_t *p, int *major, int *minor) {
133 /* Look for %PDF-N.M in first 1024 bytes */
134 312 const char *needle = "%PDF-";
135 312 size_t search_len = p->buf_len < 1024 ? p->buf_len : 1024;
136 312 const uint8_t *found = NULL;
137 312 int maj = 0, min = 0;
138 const uint8_t *ptr;
139 size_t i;
140
141 312 ▶ for (i = 0; i + 7 < search_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 7 < search_len; i++) { 312 0 312
142 312 ▶ if (memcmp(p->buf + i, needle, 5) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(p->buf + i, needle, 5) == 0) { 312 0 312
143 312 found = p->buf + i;
144 312 break;
145 }
146 }
147
148 312 ▶ if (!found) {
Branch: 1 branch
BranchTrueFalseTotal
if (!found) { 0 312 312
149 0 set_error(p, PDFMAKE_EHEADER, 0, "PDF header not found");
150 0 return PDFMAKE_EHEADER;
151 }
152
153 /* Parse version. Bounded by the buffer: a file whose digits run to EOF
154 * would otherwise walk off the end of it. */
155 312 ptr = found + 5;
156 {
157 312 const uint8_t *bufend = p->buf + p->buf_len;
158 624 ▶ while (ptr < bufend && isdigit(*ptr)) maj = maj * 10 + (*ptr++ - '0');
Branch: 2 branches
BranchTrueFalseTotal
while (ptr < bufend && isdigit(*ptr)) maj = maj * 10 + (*ptr++ - '0'); 624 0 624
while (ptr < bufend && isdigit(*ptr)) maj = maj * 10 + (*ptr++ - '0'); 312 312 624
159 312 ▶ if (ptr < bufend && *ptr == '.') ptr++;
Branch: 2 branches
BranchTrueFalseTotal
if (ptr < bufend && *ptr == '.') ptr++; 312 0 312
if (ptr < bufend && *ptr == '.') ptr++; 312 0 312
160 624 ▶ while (ptr < bufend && isdigit(*ptr)) min = min * 10 + (*ptr++ - '0');
Branch: 2 branches
BranchTrueFalseTotal
while (ptr < bufend && isdigit(*ptr)) min = min * 10 + (*ptr++ - '0'); 624 0 624
while (ptr < bufend && isdigit(*ptr)) min = min * 10 + (*ptr++ - '0'); 312 312 624
161 }
162
163 312 ▶ if (major) *major = maj;
Branch: 1 branch
BranchTrueFalseTotal
if (major) *major = maj; 312 0 312
164 312 ▶ if (minor) *minor = min;
Branch: 1 branch
BranchTrueFalseTotal
if (minor) *minor = min; 312 0 312
165
166 312 return PDFMAKE_OK;
167 }
168  
169 /*============================================================================
170 * Object parsing — recursive descent
171 *==========================================================================*/
172  
173 /* Forward declaration for mutual recursion */
174 static pdfmake_obj_t parse_object_internal(pdfmake_parser_t *p);
175  
176 /* Parse name token - decode #XX escapes */
177 8332 static pdfmake_obj_t parse_name(pdfmake_parser_t *p, pdfmake_tok_t *tok) {
178 /* Skip leading / - use tok.buf since it may be repositioned */
179 8332 const uint8_t *src = p->tok.buf + tok->offset + 1;
180 8332 size_t src_len = tok->length - 1;
181 8332 int has_escape = 0;
182 uint8_t *decoded;
183 size_t out_len;
184 size_t i;
185
186 /* Check if we need to decode #XX */
187 57944 ▶ for (i = 0; i < src_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < src_len; i++) { 49613 8331 57944
188 49613 ▶ if (src[i] == '#') { has_escape = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (src[i] == '#') { has_escape = 1; break; } 1 49612 49613
189 }
190
191 8332 ▶ if (!has_escape) {
Branch: 1 branch
BranchTrueFalseTotal
if (!has_escape) { 8331 1 8332
192 8331 return pdfmake_name(p->doc->arena, (const char *)src, src_len);
193 }
194
195 /* Decode escapes */
196 1 decoded = pdfmake_arena_alloc(p->doc->arena, src_len);
197 1 ▶ if (!decoded) return pdfmake_null();
Branch: 1 branch
BranchTrueFalseTotal
if (!decoded) return pdfmake_null(); 0 1 1
198
199 1 out_len = 0;
200 6 ▶ for (i = 0; i < src_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < src_len; i++) { 5 1 6
201 5 ▶ if (src[i] == '#' && i + 2 < src_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (src[i] == '#' && i + 2 < src_len) { 1 4 5
if (src[i] == '#' && i + 2 < src_len) { 1 0 1
202 1 int hi = hex_digit(src[i + 1]);
203 1 int lo = hex_digit(src[i + 2]);
204 1 ▶ if (hi >= 0 && lo >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (hi >= 0 && lo >= 0) { 1 0 1
if (hi >= 0 && lo >= 0) { 1 0 1
205 1 decoded[out_len++] = (uint8_t)((hi << 4) | lo);
206 1 i += 2;
207 1 continue;
208 }
209 }
210 4 decoded[out_len++] = src[i];
211 }
212
213 1 return pdfmake_name(p->doc->arena, (const char *)decoded, out_len);
214 }
215  
216 /* Parse literal string - decode escape sequences */
217 47 static pdfmake_obj_t parse_literal_string(pdfmake_parser_t *p, pdfmake_tok_t *tok) {
218 /* Content is between ( and ) - skip outer parens - use tok.buf */
219 47 const uint8_t *src = p->tok.buf + tok->offset + 1;
220 47 size_t src_len = tok->length - 2;
221 uint8_t *decoded;
222 size_t out_len;
223 int paren_depth;
224 size_t i;
225
226 /* Decode string escapes */
227 47 decoded = pdfmake_arena_alloc(p->doc->arena, src_len + 1);
228 47 ▶ if (!decoded) return pdfmake_null();
Branch: 1 branch
BranchTrueFalseTotal
if (!decoded) return pdfmake_null(); 0 47 47
229
230 47 out_len = 0;
231 47 paren_depth = 0;
232
233 1244 ▶ for (i = 0; i < src_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < src_len; i++) { 1197 47 1244
234 1197 uint8_t c = src[i];
235
236 1197 ▶ if (c == '\\' && i + 1 < src_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '\\' && i + 1 < src_len) { 3 1194 1197
if (c == '\\' && i + 1 < src_len) { 3 0 3
237 3 uint8_t next = src[i + 1];
238 3 switch (next) {
239 1 case 'n': decoded[out_len++] = '\n'; i++; continue;
240 0 case 'r': decoded[out_len++] = '\r'; i++; continue;
241 0 case 't': decoded[out_len++] = '\t'; i++; continue;
242 0 case 'b': decoded[out_len++] = '\b'; i++; continue;
243 0 case 'f': decoded[out_len++] = '\f'; i++; continue;
244 1 case '(': decoded[out_len++] = '('; i++; continue;
245 1 case ')': decoded[out_len++] = ')'; i++; continue;
246 0 case '\\': decoded[out_len++] = '\\'; i++; continue;
247 0 case '\n': i++; continue; /* Line continuation LF */
248 0 case '\r': /* Line continuation CR or CRLF */
249 0 i++;
250 0 ▶ if (i + 1 < src_len && src[i + 1] == '\n') i++;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 1 < src_len && src[i + 1] == '\n') i++; 0 0 0
if (i + 1 < src_len && src[i + 1] == '\n') i++; 0 0 0
251 0 continue;
252 0 default:
253 /* Octal escape? */
254 0 ▶ if (next >= '0' && next <= '7') {
Branch: 2 branches
BranchTrueFalseTotal
if (next >= '0' && next <= '7') { 0 0 0
if (next >= '0' && next <= '7') { 0 0 0
255 0 int val = next - '0';
256 0 i++;
257 0 ▶ if (i + 1 < src_len && src[i + 1] >= '0' && src[i + 1] <= '7') {
Branch: 3 branches
BranchTrueFalseTotal
if (i + 1 < src_len && src[i + 1] >= '0' && src[i + 1] <= '7') { 0 0 0
if (i + 1 < src_len && src[i + 1] >= '0' && src[i + 1] <= '7') { 0 0 0
if (i + 1 < src_len && src[i + 1] >= '0' && src[i + 1] <= '7') { 0 0 0
258 0 val = val * 8 + (src[++i] - '0');
259 0 ▶ if (i + 1 < src_len && src[i + 1] >= '0' && src[i + 1] <= '7') {
Branch: 3 branches
BranchTrueFalseTotal
if (i + 1 < src_len && src[i + 1] >= '0' && src[i + 1] <= '7') { 0 0 0
if (i + 1 < src_len && src[i + 1] >= '0' && src[i + 1] <= '7') { 0 0 0
if (i + 1 < src_len && src[i + 1] >= '0' && src[i + 1] <= '7') { 0 0 0
260 0 val = val * 8 + (src[++i] - '0');
261 }
262 }
263 0 decoded[out_len++] = (uint8_t)(val & 0xFF);
264 0 continue;
265 }
266 /* Unknown escape - just keep the backslash per PDF spec */
267 0 decoded[out_len++] = c;
268 0 continue;
269 }
270 }
271
272 /* Track balanced parens (tokenizer already validated) */
273 1194 ▶ if (c == '(') paren_depth++;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '(') paren_depth++; 0 1194 1194
274 1194 ▶ else if (c == ')') paren_depth--;
Branch: 1 branch
BranchTrueFalseTotal
else if (c == ')') paren_depth--; 0 1194 1194
275
276 1194 decoded[out_len++] = c;
277 }
278
279 (void)paren_depth; /* Validated by tokenizer */
280 47 return pdfmake_str(p->doc->arena, (const char *)decoded, out_len);
281 }
282  
283 /* Parse hex string - decode hex pairs */
284 608 static pdfmake_obj_t parse_hex_string(pdfmake_parser_t *p, pdfmake_tok_t *tok) {
285 /* Content is between < and > - use tok.buf */
286 608 const uint8_t *src = p->tok.buf + tok->offset + 1;
287 608 size_t src_len = tok->length - 2;
288 uint8_t *decoded;
289 size_t out_len;
290 int high;
291 size_t i;
292
293 /* Decode hex pairs (skip whitespace) */
294 608 decoded = pdfmake_arena_alloc(p->doc->arena, (src_len + 1) / 2);
295 608 ▶ if (!decoded) return pdfmake_null();
Branch: 1 branch
BranchTrueFalseTotal
if (!decoded) return pdfmake_null(); 0 608 608
296
297 608 out_len = 0;
298 608 high = -1;
299
300 21205 ▶ for (i = 0; i < src_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < src_len; i++) { 20597 608 21205
301 20597 uint8_t c = src[i];
302 int digit;
303 20597 ▶ if (pdfmake_is_whitespace(c)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_is_whitespace(c)) continue; 0 20597 20597
304
305 20597 digit = hex_digit(c);
306 20597 ▶ if (digit < 0) continue; /* Invalid hex digit - skip */
Branch: 1 branch
BranchTrueFalseTotal
if (digit < 0) continue; /* Invalid hex digit - skip */ 0 20597 20597
307
308 20597 ▶ if (high < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (high < 0) { 10299 10298 20597
309 10299 high = digit;
310 } else {
311 10298 decoded[out_len++] = (uint8_t)((high << 4) | digit);
312 10298 high = -1;
313 }
314 }
315
316 /* Odd number of digits - pad with 0 */
317 608 ▶ if (high >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (high >= 0) { 1 607 608
318 1 decoded[out_len++] = (uint8_t)(high << 4);
319 }
320
321 608 return pdfmake_hexstr(p->doc->arena, decoded, out_len);
322 }
323  
324 /* Parse array */
325 897 static pdfmake_obj_t parse_array(pdfmake_parser_t *p) {
326 897 pdfmake_obj_t arr = pdfmake_array_new(p->doc->arena);
327 pdfmake_tok_t tok;
328 pdfmake_obj_t item;
329
330 897 ▶ if (arr.kind != PDFMAKE_ARRAY) return pdfmake_null();
Branch: 1 branch
BranchTrueFalseTotal
if (arr.kind != PDFMAKE_ARRAY) return pdfmake_null(); 897 0 897
331
332 while (1) {
333 7167 pdfmake_tok_peek_significant(&p->tok, &tok);
334
335 7167 ▶ if (tok.kind == PDFMAKE_TOK_ARR_CLOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.kind == PDFMAKE_TOK_ARR_CLOSE) { 897 6270 7167
336 897 pdfmake_tok_next_significant(&p->tok); /* Consume ] */
337 897 break;
338 }
339 6270 ▶ if (tok.kind == PDFMAKE_TOK_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.kind == PDFMAKE_TOK_EOF) { 0 6270 6270
340 0 set_error(p, PDFMAKE_EPARSE, tok.offset, "Unterminated array");
341 0 return pdfmake_null();
342 }
343
344 6270 item = parse_object_internal(p);
345 6270 ▶ if (p->last_err != PDFMAKE_OK) return pdfmake_null();
Branch: 1 branch
BranchTrueFalseTotal
if (p->last_err != PDFMAKE_OK) return pdfmake_null(); 0 6270 6270
346
347 6270 ▶ if (!pdfmake_array_push(p->doc->arena, &arr, item)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_array_push(p->doc->arena, &arr, item)) { 0 6270 6270
348 0 set_error(p, PDFMAKE_ENOMEM, tok.offset, "Array push failed");
349 0 return pdfmake_null();
350 }
351 }
352
353 897 return arr;
354 }
355  
356 /* Parse dictionary (and possibly stream) */
357 1921 static pdfmake_obj_t parse_dict(pdfmake_parser_t *p) {
358 1921 pdfmake_obj_t dict = pdfmake_dict_new(p->doc->arena);
359 pdfmake_tok_t tok;
360 pdfmake_obj_t key_obj;
361 pdfmake_obj_t value;
362 pdfmake_tok_t next;
363 pdfmake_obj_t stream;
364 pdfmake_obj_t stream_dict_wrapper;
365 pdfmake_dict_iter_t iter;
366 const uint8_t *body_data;
367 size_t body_len;
368 pdfmake_err_t err;
369 size_t endstream_pos;
370 const char *endstream_kw;
371
372 1921 ▶ if (dict.kind != PDFMAKE_DICT) return pdfmake_null();
Branch: 1 branch
BranchTrueFalseTotal
if (dict.kind != PDFMAKE_DICT) return pdfmake_null(); 0 1921 1921
373
374 while (1) {
375 8429 pdfmake_tok_peek_significant(&p->tok, &tok);
376
377 8429 ▶ if (tok.kind == PDFMAKE_TOK_DICT_CLOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.kind == PDFMAKE_TOK_DICT_CLOSE) { 1921 6508 8429
378 1921 pdfmake_tok_next_significant(&p->tok); /* Consume >> */
379 1921 break;
380 }
381 6508 ▶ if (tok.kind == PDFMAKE_TOK_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.kind == PDFMAKE_TOK_EOF) { 0 6508 6508
382 0 set_error(p, PDFMAKE_EPARSE, tok.offset, "Unterminated dictionary");
383 0 return pdfmake_null();
384 }
385
386 /* Key must be a name */
387 6508 ▶ if (tok.kind != PDFMAKE_TOK_NAME) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.kind != PDFMAKE_TOK_NAME) { 0 6508 6508
388 0 set_error(p, PDFMAKE_EPARSE, tok.offset, "Dictionary key must be a name");
389 0 return pdfmake_null();
390 }
391
392 6508 pdfmake_tok_next_significant(&p->tok); /* Consume key */
393 6508 key_obj = parse_name(p, &tok);
394 6508 ▶ if (key_obj.kind != PDFMAKE_NAME) return pdfmake_null();
Branch: 1 branch
BranchTrueFalseTotal
if (key_obj.kind != PDFMAKE_NAME) return pdfmake_null(); 0 6508 6508
395
396 /* Value */
397 6508 value = parse_object_internal(p);
398 6508 ▶ if (p->last_err != PDFMAKE_OK) return pdfmake_null();
Branch: 1 branch
BranchTrueFalseTotal
if (p->last_err != PDFMAKE_OK) return pdfmake_null(); 0 6508 6508
399
400 6508 ▶ if (!pdfmake_dict_set(p->doc->arena, &dict, key_obj.as.name.id, value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_dict_set(p->doc->arena, &dict, key_obj.as.name.id, value)) { 0 6508 6508
401 0 set_error(p, PDFMAKE_ENOMEM, tok.offset, "Dict set failed");
402 0 return pdfmake_null();
403 }
404 }
405
406 /* Check if followed by stream keyword */
407 1921 pdfmake_tok_peek_significant(&p->tok, &next);
408
409 1921 ▶ if (next.kind == PDFMAKE_TOK_KW_STREAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (next.kind == PDFMAKE_TOK_KW_STREAM) { 245 1676 1921
410 245 pdfmake_tok_next_significant(&p->tok); /* Consume stream */
411
412 /* Create stream object */
413 245 stream = pdfmake_stream_new(p->doc->arena);
414 245 ▶ if (stream.kind != PDFMAKE_STREAM) return pdfmake_null();
Branch: 1 branch
BranchTrueFalseTotal
if (stream.kind != PDFMAKE_STREAM) return pdfmake_null(); 0 245 245
415
416 /* Copy dict entries to stream dict - need a wrapper pdfmake_obj_t */
417 245 stream_dict_wrapper.kind = PDFMAKE_DICT;
418 245 stream_dict_wrapper.as.dict = stream.as.stream->dict;
419
420 245 pdfmake_dict_iter_init(&iter, &dict);
421 870 ▶ while (pdfmake_dict_iter_next(&iter)) {
Branch: 1 branch
BranchTrueFalseTotal
while (pdfmake_dict_iter_next(&iter)) { 625 245 870
422 625 pdfmake_dict_set(p->doc->arena, &stream_dict_wrapper,
423 625 iter.current_key, *iter.current_value);
424 }
425
426 /* Extract stream body */
427 245 err = pdfmake_extract_stream_body(p, &dict, next.payload.body_offset,
428 &body_data, &body_len);
429 245 ▶ if (err != PDFMAKE_OK) return pdfmake_null();
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return pdfmake_null(); 0 245 245
430
431 /* Copy stream data to arena */
432 245 pdfmake_stream_set_data(p->doc->arena, &stream, body_data, body_len);
433
434 /* Skip tokenizer past stream body to find endstream.
435 * The tokenizer can't parse arbitrary binary stream content,
436 * so we manually position it after the stream body. */
437 245 endstream_pos = next.payload.body_offset + body_len;
438
439 /* Skip optional EOL before endstream */
440 487 ▶ while (endstream_pos < p->buf_len &&
Branch: 1 branch
BranchTrueFalseTotal
while (endstream_pos < p->buf_len && 487 0 487
441 487 ▶ (p->buf[endstream_pos] == '\r' || p->buf[endstream_pos] == '\n')) {
Branch: 2 branches
BranchTrueFalseTotal
(p->buf[endstream_pos] == '\r' || p->buf[endstream_pos] == '\n')) { 125 362 487
(p->buf[endstream_pos] == '\r' || p->buf[endstream_pos] == '\n')) { 117 245 362
442 242 endstream_pos++;
443 }
444
445 /* Verify endstream keyword */
446 245 endstream_kw = "endstream";
447 245 ▶ if (endstream_pos + 9 > p->buf_len ||
Branch: 1 branch
BranchTrueFalseTotal
if (endstream_pos + 9 > p->buf_len || 245 0 245
448 245 ▶ memcmp(p->buf + endstream_pos, endstream_kw, 9) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
memcmp(p->buf + endstream_pos, endstream_kw, 9) != 0) { 0 245 245
449 0 set_error(p, PDFMAKE_ESTREAM, endstream_pos, "Expected endstream");
450 0 return pdfmake_null();
451 }
452
453 /* Position tokenizer after endstream */
454 245 p->tok.pos = endstream_pos + 9;
455 245 p->tok.has_peeked = 0;
456
457 245 return stream;
458 }
459
460 1676 return dict;
461 }
462  
463 /* Parse any object at current position */
464 14230 static pdfmake_obj_t parse_object_internal(pdfmake_parser_t *p) {
465 14230 pdfmake_tok_t tok = pdfmake_tok_next_significant(&p->tok);
466
467 14230 switch (tok.kind) {
468 2 case PDFMAKE_TOK_KW_NULL:
469 2 return pdfmake_null();
470
471 3 case PDFMAKE_TOK_KW_TRUE:
472 3 return pdfmake_bool(1);
473
474 7 case PDFMAKE_TOK_KW_FALSE:
475 7 return pdfmake_bool(0);
476
477 8912 case PDFMAKE_TOK_INT: {
478 /* Check for indirect reference: INT INT R */
479 pdfmake_tok_t peek1, peek2;
480 8912 pdfmake_tok_peek_significant(&p->tok, &peek1);
481
482 8912 ▶ if (peek1.kind == PDFMAKE_TOK_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (peek1.kind == PDFMAKE_TOK_INT) { 7147 1765 8912
483 /* Save tokenizer state */
484 7147 size_t saved_pos = p->tok.pos;
485 7147 int saved_peeked = p->tok.has_peeked;
486 7147 pdfmake_tok_t saved_peek = p->tok.peeked;
487
488 7147 pdfmake_tok_next_significant(&p->tok); /* Consume second int */
489 7147 pdfmake_tok_peek_significant(&p->tok, &peek2);
490
491 7147 ▶ if (peek2.kind == PDFMAKE_TOK_KW_R) {
Branch: 1 branch
BranchTrueFalseTotal
if (peek2.kind == PDFMAKE_TOK_KW_R) { 2332 4815 7147
492 2332 pdfmake_tok_next_significant(&p->tok); /* Consume R */
493 2332 return pdfmake_ref((uint32_t)tok.payload.int_val,
494 2332 (uint16_t)peek1.payload.int_val);
495 }
496
497 /* Not a reference - restore state */
498 4815 p->tok.pos = saved_pos;
499 4815 p->tok.has_peeked = saved_peeked;
500 4815 p->tok.peeked = saved_peek;
501 }
502 6580 return pdfmake_int(tok.payload.int_val);
503 }
504
505 9 case PDFMAKE_TOK_REAL:
506 9 return pdfmake_real(tok.payload.real_val);
507
508 1824 case PDFMAKE_TOK_NAME:
509 1824 return parse_name(p, &tok);
510
511 47 case PDFMAKE_TOK_LSTR:
512 47 return parse_literal_string(p, &tok);
513
514 608 case PDFMAKE_TOK_HSTR:
515 608 return parse_hex_string(p, &tok);
516
517 897 case PDFMAKE_TOK_ARR_OPEN:
518 897 return parse_array(p);
519
520 1921 case PDFMAKE_TOK_DICT_OPEN:
521 1921 return parse_dict(p);
522
523 0 case PDFMAKE_TOK_EOF:
524 0 set_error(p, PDFMAKE_EPARSE, tok.offset, "Unexpected end of input");
525 0 return pdfmake_null();
526
527 0 default:
528 0 set_error(p, PDFMAKE_EPARSE, tok.offset, "Unexpected token");
529 0 return pdfmake_null();
530 }
531 }
532  
533 0 pdfmake_obj_t pdfmake_parse_object(pdfmake_parser_t *parser) {
534 0 parser->last_err = PDFMAKE_OK;
535 0 return parse_object_internal(parser);
536 }
537  
538 /*============================================================================
539 * Stream body extraction
540 *==========================================================================*/
541  
542 245 pdfmake_err_t pdfmake_extract_stream_body(pdfmake_parser_t *p,
543 pdfmake_obj_t *stream_dict,
544 size_t body_offset,
545 const uint8_t **out_data,
546 size_t *out_len) {
547 /* Get /Length from dict */
548 245 uint32_t length_id = intern_name(p, "Length", 6);
549 245 pdfmake_obj_t *length_obj = pdfmake_dict_get(stream_dict, length_id);
550 245 size_t declared_len = 0;
551 245 int have_length = 0;
552 const char *endstream_marker;
553 size_t endstream_len;
554 size_t scan_start;
555 size_t scan_end;
556 size_t i;
557
558 245 ▶ if (length_obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (length_obj) { 245 0 245
559 245 ▶ if (length_obj->kind == PDFMAKE_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (length_obj->kind == PDFMAKE_INT) { 245 0 245
560 245 declared_len = (size_t)length_obj->as.i;
561 245 have_length = 1;
562 0 ▶ } else if (length_obj->kind == PDFMAKE_REF) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (length_obj->kind == PDFMAKE_REF) { 0 0 0
563 /* /Length is indirect - need to resolve */
564 0 pdfmake_obj_t *resolved = pdfmake_parser_resolve(p, length_obj->as.ref);
565 0 ▶ if (resolved && resolved->kind == PDFMAKE_INT) {
Branch: 2 branches
BranchTrueFalseTotal
if (resolved && resolved->kind == PDFMAKE_INT) { 0 0 0
if (resolved && resolved->kind == PDFMAKE_INT) { 0 0 0
566 0 declared_len = (size_t)resolved->as.i;
567 0 have_length = 1;
568 }
569 }
570 }
571
572 /* Find endstream */
573 245 endstream_marker = "endstream";
574 245 endstream_len = 9;
575 245 scan_start = body_offset;
576 245 scan_end = p->buf_len;
577
578 /* If we have declared length, try it first */
579 245 ▶ if (have_length && body_offset + declared_len + endstream_len <= p->buf_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (have_length && body_offset + declared_len + endstream_len <= p->buf_len) { 245 0 245
if (have_length && body_offset + declared_len + endstream_len <= p->buf_len) { 245 0 245
580 245 size_t expected_end = body_offset + declared_len;
581
582 /* Skip optional EOL before endstream */
583 485 ▶ while (expected_end < p->buf_len &&
Branch: 1 branch
BranchTrueFalseTotal
while (expected_end < p->buf_len && 485 0 485
584 485 ▶ (p->buf[expected_end] == '\r' || p->buf[expected_end] == '\n')) {
Branch: 2 branches
BranchTrueFalseTotal
(p->buf[expected_end] == '\r' || p->buf[expected_end] == '\n')) { 125 360 485
(p->buf[expected_end] == '\r' || p->buf[expected_end] == '\n')) { 115 245 360
585 240 expected_end++;
586 }
587
588 245 ▶ if (expected_end + endstream_len <= p->buf_len &&
Branch: 1 branch
BranchTrueFalseTotal
if (expected_end + endstream_len <= p->buf_len && 245 0 245
589 245 ▶ memcmp(p->buf + expected_end, endstream_marker, endstream_len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
memcmp(p->buf + expected_end, endstream_marker, endstream_len) == 0) { 243 2 245
590 243 *out_data = p->buf + body_offset;
591 243 *out_len = declared_len;
592 243 return PDFMAKE_OK;
593 }
594 }
595
596 /* Fallback: scan for endstream */
597 76 ▶ for (i = scan_start; i + endstream_len <= scan_end; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = scan_start; i + endstream_len <= scan_end; i++) { 76 0 76
598 76 ▶ if (memcmp(p->buf + i, endstream_marker, endstream_len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(p->buf + i, endstream_marker, endstream_len) == 0) { 2 74 76
599 /* Check preceding bytes for proper delimiter */
600 2 size_t body_end = i;
601
602 /* Back up over optional EOL */
603 2 ▶ if (body_end > body_offset && p->buf[body_end - 1] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (body_end > body_offset && p->buf[body_end - 1] == '\n') { 2 0 2
if (body_end > body_offset && p->buf[body_end - 1] == '\n') { 2 0 2
604 2 body_end--;
605 }
606 2 ▶ if (body_end > body_offset && p->buf[body_end - 1] == '\r') {
Branch: 2 branches
BranchTrueFalseTotal
if (body_end > body_offset && p->buf[body_end - 1] == '\r') { 2 0 2
if (body_end > body_offset && p->buf[body_end - 1] == '\r') { 0 2 2
607 0 body_end--;
608 }
609
610 2 *out_data = p->buf + body_offset;
611 2 *out_len = body_end - body_offset;
612 2 return PDFMAKE_OK;
613 }
614 }
615
616 0 set_error(p, PDFMAKE_ESTREAM, body_offset, "endstream not found");
617 0 return PDFMAKE_ESTREAM;
618 }
619  
620 /*============================================================================
621 * Indirect object parsing
622 *==========================================================================*/
623  
624 1114 pdfmake_err_t pdfmake_parse_indirect_object(pdfmake_parser_t *p) {
625 pdfmake_tok_t num_tok;
626 pdfmake_tok_t gen_tok;
627 pdfmake_tok_t obj_tok;
628 uint32_t num;
629 uint16_t gen;
630 pdfmake_obj_t obj;
631 pdfmake_tok_t endobj_tok;
632 size_t idx;
633
634 /* Clear any residual error from previous operations */
635 1114 p->last_err = PDFMAKE_OK;
636
637 1114 num_tok = pdfmake_tok_next_significant(&p->tok);
638 1114 ▶ if (num_tok.kind != PDFMAKE_TOK_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_tok.kind != PDFMAKE_TOK_INT) { 0 1114 1114
639 0 set_error(p, PDFMAKE_EPARSE, num_tok.offset, "Expected object number");
640 0 return PDFMAKE_EPARSE;
641 }
642
643 1114 gen_tok = pdfmake_tok_next_significant(&p->tok);
644 1114 ▶ if (gen_tok.kind != PDFMAKE_TOK_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (gen_tok.kind != PDFMAKE_TOK_INT) { 0 1114 1114
645 0 set_error(p, PDFMAKE_EPARSE, gen_tok.offset, "Expected generation number");
646 0 return PDFMAKE_EPARSE;
647 }
648
649 1114 obj_tok = pdfmake_tok_next_significant(&p->tok);
650 1114 ▶ if (obj_tok.kind != PDFMAKE_TOK_KW_OBJ) {
Branch: 1 branch
BranchTrueFalseTotal
if (obj_tok.kind != PDFMAKE_TOK_KW_OBJ) { 0 1114 1114
651 0 set_error(p, PDFMAKE_EPARSE, obj_tok.offset, "Expected 'obj' keyword");
652 0 return PDFMAKE_EPARSE;
653 }
654
655 1114 num = (uint32_t)num_tok.payload.int_val;
656 1114 gen = (uint16_t)gen_tok.payload.int_val;
657
658 /* Parse the object value */
659 1114 obj = parse_object_internal(p);
660 1114 ▶ if (p->last_err != PDFMAKE_OK) return p->last_err;
Branch: 1 branch
BranchTrueFalseTotal
if (p->last_err != PDFMAKE_OK) return p->last_err; 0 1114 1114
661
662 /* Expect endobj */
663 1114 endobj_tok = pdfmake_tok_next_significant(&p->tok);
664 1114 ▶ if (endobj_tok.kind != PDFMAKE_TOK_KW_ENDOBJ) {
Branch: 1 branch
BranchTrueFalseTotal
if (endobj_tok.kind != PDFMAKE_TOK_KW_ENDOBJ) { 0 1114 1114
665 /* Some PDFs omit endobj before stream - tolerate */
666 0 ▶ if (endobj_tok.kind != PDFMAKE_TOK_KW_STREAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (endobj_tok.kind != PDFMAKE_TOK_KW_STREAM) { 0 0 0
667 0 set_error(p, PDFMAKE_EPARSE, endobj_tok.offset, "Expected 'endobj' keyword");
668 0 return PDFMAKE_EPARSE;
669 }
670 }
671
672 /* Store in document */
673 /* Grow objects array if needed - use num directly as we need capacity for num items */
674 /* pdfmake_doc uses 1-based numbering with object N at index N-1 */
675 1114 ▶ while (num > p->doc->obj_cap) {
Branch: 1 branch
BranchTrueFalseTotal
while (num > p->doc->obj_cap) { 0 1114 1114
676 0 ▶ size_t new_cap = p->doc->obj_cap ? p->doc->obj_cap * 2 : PDFMAKE_DOC_INIT_CAP;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = p->doc->obj_cap ? p->doc->obj_cap * 2 : PDFMAKE_DOC_INIT_CAP; 0 0 0
677 pdfmake_indirect_t *new_objs;
678 0 ▶ while (new_cap < num) new_cap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_cap < num) new_cap *= 2; 0 0 0
679 0 new_objs = realloc(p->doc->objects,
680 new_cap * sizeof(pdfmake_indirect_t));
681 0 ▶ if (!new_objs) {
Branch: 1 branch
BranchTrueFalseTotal
if (!new_objs) { 0 0 0
682 0 set_error(p, PDFMAKE_ENOMEM, 0, "Failed to grow object table");
683 0 return PDFMAKE_ENOMEM;
684 }
685 0 memset(new_objs + p->doc->obj_cap, 0,
686 0 (new_cap - p->doc->obj_cap) * sizeof(pdfmake_indirect_t));
687 0 p->doc->objects = new_objs;
688 0 p->doc->obj_cap = new_cap;
689 }
690
691 /* Store at index num-1 (1-based numbering) */
692 1114 idx = num - 1;
693 1114 p->doc->objects[idx].num = num;
694 1114 p->doc->objects[idx].gen = gen;
695 1114 p->doc->objects[idx].obj = obj;
696 1114 p->doc->objects[idx].in_use = 1;
697 1114 p->doc->objects[idx].byte_offset = num_tok.offset;
698
699 1114 ▶ if (num > p->doc->obj_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (num > p->doc->obj_count) { 291 823 1114
700 291 p->doc->obj_count = num;
701 }
702
703 /* Update xref if tracking */
704 1114 ▶ if (ensure_xref_cap(p, num)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ensure_xref_cap(p, num)) { 1114 0 1114
705 1114 p->xref[num].num = num;
706 1114 p->xref[num].gen = gen;
707 1114 p->xref[num].type = PDFMAKE_XREF_UNCOMPRESSED;
708 1114 p->xref[num].loc.offset = num_tok.offset;
709 1114 p->xref[num].loaded = 1;
710 1114 ▶ if (num >= p->xref_size) p->xref_size = num + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (num >= p->xref_size) p->xref_size = num + 1; 0 1114 1114
711 }
712
713 1114 return PDFMAKE_OK;
714 }
715  
716 /*============================================================================
717 * startxref locator
718 *==========================================================================*/
719  
720 312 pdfmake_err_t pdfmake_locate_startxref(pdfmake_parser_t *p, uint64_t *offset) {
721 /* Scan backward from EOF - check last 1024 bytes per spec */
722 312 size_t search_start = p->buf_len > 1024 ? p->buf_len - 1024 : 0;
723 312 const char *keyword = "startxref";
724 312 size_t keyword_len = 9;
725 312 const uint8_t *found = NULL;
726 const uint8_t *ptr;
727 const uint8_t *end;
728 uint64_t off;
729 size_t i;
730
731 312 ▶ if (p->buf_len < keyword_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->buf_len < keyword_len) { 0 312 312
732 0 set_error(p, PDFMAKE_EXREF, 0, "startxref not found");
733 0 return PDFMAKE_EXREF;
734 }
735  
736 /* Find last occurrence of startxref. The scan starts at the last
737 * position the whole keyword can occupy: comparing from buf_len - 1
738 * reads eight bytes past the buffer, which is allocator slack on most
739 * platforms and a guard page on OpenBSD. The low end is search_start
740 * itself - a keyword there is inside the last 1024 bytes. */
741 3888 ▶ for (i = p->buf_len - keyword_len + 1; i > search_start; ) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = p->buf_len - keyword_len + 1; i > search_start; ) { 3882 6 3888
742 3882 i--;
743 3882 ▶ if (memcmp(p->buf + i, keyword, keyword_len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(p->buf + i, keyword, keyword_len) == 0) { 306 3576 3882
744 306 found = p->buf + i;
745 306 break;
746 }
747 }
748
749 312 ▶ if (!found) {
Branch: 1 branch
BranchTrueFalseTotal
if (!found) { 6 306 312
750 6 set_error(p, PDFMAKE_EXREF, 0, "startxref not found");
751 6 return PDFMAKE_EXREF;
752 }
753
754 /* Parse offset after startxref */
755 306 ptr = found + keyword_len;
756 306 end = p->buf + p->buf_len;
757
758 /* Skip whitespace */
759 612 ▶ while (ptr < end && (*ptr == ' ' || *ptr == '\t' || *ptr == '\r' || *ptr == '\n')) {
Branch: 5 branches
BranchTrueFalseTotal
while (ptr < end && (*ptr == ' ' || *ptr == '\t' || *ptr == '\r' || *ptr == '\n')) { 612 0 612
while (ptr < end && (*ptr == ' ' || *ptr == '\t' || *ptr == '\r' || *ptr == '\n')) { 0 612 612
while (ptr < end && (*ptr == ' ' || *ptr == '\t' || *ptr == '\r' || *ptr == '\n')) { 0 612 612
while (ptr < end && (*ptr == ' ' || *ptr == '\t' || *ptr == '\r' || *ptr == '\n')) { 32 580 612
while (ptr < end && (*ptr == ' ' || *ptr == '\t' || *ptr == '\r' || *ptr == '\n')) { 274 306 580
760 306 ptr++;
761 }
762
763 /* Parse number */
764 306 off = 0;
765 1308 ▶ while (ptr < end && isdigit(*ptr)) {
Branch: 2 branches
BranchTrueFalseTotal
while (ptr < end && isdigit(*ptr)) { 1308 0 1308
while (ptr < end && isdigit(*ptr)) { 1002 306 1308
766 1002 off = off * 10 + (*ptr - '0');
767 1002 ptr++;
768 }
769
770 306 *offset = off;
771 306 return PDFMAKE_OK;
772 }
773  
774 /*============================================================================
775 * Classic xref table parser (§7.5.4)
776 *==========================================================================*/
777  
778 /* Consume a "first_num count" pair that introduces a classic-xref
779 * subsection. Caller must have already confirmed via peek that the next
780 * token is not KW_TRAILER. */
781 static pdfmake_err_t
782 300 parse_xref_subsection_header(pdfmake_parser_t *p, uint64_t table_offset,
783 uint32_t *out_first, uint32_t *out_count)
784 {
785 pdfmake_tok_t first_tok;
786 pdfmake_tok_t count_tok;
787
788 300 first_tok = pdfmake_tok_next_significant(&p->tok);
789 300 ▶ if (first_tok.kind != PDFMAKE_TOK_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (first_tok.kind != PDFMAKE_TOK_INT) { 0 300 300
790 0 set_error(p, PDFMAKE_EXREF, table_offset + first_tok.offset,
791 "Expected subsection start object number");
792 0 return PDFMAKE_EXREF;
793 }
794  
795 300 count_tok = pdfmake_tok_next_significant(&p->tok);
796 300 ▶ if (count_tok.kind != PDFMAKE_TOK_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (count_tok.kind != PDFMAKE_TOK_INT) { 0 300 300
797 0 set_error(p, PDFMAKE_EXREF, table_offset + count_tok.offset,
798 "Expected subsection entry count");
799 0 return PDFMAKE_EXREF;
800 }
801  
802 300 *out_first = (uint32_t)first_tok.payload.int_val;
803 300 *out_count = (uint32_t)count_tok.payload.int_val;
804 300 return PDFMAKE_OK;
805 }
806  
807 /* Parse one classic-xref entry (offset, gen, n|f) from the token stream
808 * and store it into p->xref[obj_num] unless the slot is already loaded
809 * from a later xref section. The type byte is a single-character ERROR
810 * token rather than a keyword — the tokenizer does not recognise 'n'/'f'
811 * specially. */
812 static pdfmake_err_t
813 3772 parse_xref_entry(pdfmake_parser_t *p, uint64_t table_offset, uint32_t obj_num)
814 {
815 pdfmake_tok_t off_tok;
816 pdfmake_tok_t gen_tok;
817 pdfmake_tok_t type_tok;
818 char type_char;
819
820 3772 off_tok = pdfmake_tok_next_significant(&p->tok);
821 3772 ▶ if (off_tok.kind != PDFMAKE_TOK_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (off_tok.kind != PDFMAKE_TOK_INT) { 0 3772 3772
822 0 set_error(p, PDFMAKE_EXREF, table_offset + off_tok.offset,
823 "Expected xref entry offset");
824 0 return PDFMAKE_EXREF;
825 }
826  
827 3772 gen_tok = pdfmake_tok_next_significant(&p->tok);
828 3772 ▶ if (gen_tok.kind != PDFMAKE_TOK_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (gen_tok.kind != PDFMAKE_TOK_INT) { 0 3772 3772
829 0 set_error(p, PDFMAKE_EXREF, table_offset + gen_tok.offset,
830 "Expected xref entry generation");
831 0 return PDFMAKE_EXREF;
832 }
833  
834 3772 type_tok = pdfmake_tok_next(&p->tok);
835 7544 ▶ while (type_tok.kind == PDFMAKE_TOK_WS || type_tok.kind == PDFMAKE_TOK_COMMENT) {
Branch: 2 branches
BranchTrueFalseTotal
while (type_tok.kind == PDFMAKE_TOK_WS || type_tok.kind == PDFMAKE_TOK_COMMENT) { 3772 3772 7544
while (type_tok.kind == PDFMAKE_TOK_WS || type_tok.kind == PDFMAKE_TOK_COMMENT) { 0 3772 3772
836 3772 type_tok = pdfmake_tok_next(&p->tok);
837 }
838  
839 3772 type_char = 'n';
840 3772 ▶ if (type_tok.kind == PDFMAKE_TOK_ERROR && type_tok.length == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (type_tok.kind == PDFMAKE_TOK_ERROR && type_tok.length == 1) { 3772 0 3772
if (type_tok.kind == PDFMAKE_TOK_ERROR && type_tok.length == 1) { 3772 0 3772
841 3772 type_char = (char)p->buf[table_offset + type_tok.offset];
842 }
843  
844 3772 ▶ if (!p->xref[obj_num].loaded) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p->xref[obj_num].loaded) { 3772 0 3772
845 3772 p->xref[obj_num].num = obj_num;
846 3772 p->xref[obj_num].gen = (uint16_t)gen_tok.payload.int_val;
847  
848 3772 ▶ if (type_char == 'n') {
Branch: 1 branch
BranchTrueFalseTotal
if (type_char == 'n') { 3500 272 3772
849 3500 p->xref[obj_num].type = PDFMAKE_XREF_UNCOMPRESSED;
850 3500 p->xref[obj_num].loc.offset = (uint64_t)off_tok.payload.int_val;
851 } else {
852 272 p->xref[obj_num].type = PDFMAKE_XREF_FREE;
853 272 p->xref[obj_num].loc.next_free = (uint32_t)off_tok.payload.int_val;
854 }
855 }
856  
857 3772 ▶ if (obj_num >= p->xref_size) p->xref_size = obj_num + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (obj_num >= p->xref_size) p->xref_size = obj_num + 1; 1696 2076 3772
858 3772 return PDFMAKE_OK;
859 }
860  
861 300 pdfmake_err_t pdfmake_parse_xref_table(pdfmake_parser_t *p, uint64_t offset) {
862 pdfmake_tok_t tok;
863 pdfmake_tok_t next;
864 uint32_t first_num, count;
865 pdfmake_err_t err;
866 uint32_t i;
867
868 300 ▶ if (offset >= p->buf_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset >= p->buf_len) { 0 300 300
869 0 set_error(p, PDFMAKE_EXREF, (size_t)offset, "Xref offset beyond EOF");
870 0 return PDFMAKE_EXREF;
871 }
872  
873 300 pdfmake_tokenizer_init(&p->tok, p->buf + offset, p->buf_len - offset);
874  
875 300 tok = pdfmake_tok_next_significant(&p->tok);
876 300 ▶ if (tok.kind != PDFMAKE_TOK_KW_XREF) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.kind != PDFMAKE_TOK_KW_XREF) { 300 0 300
877 0 set_error(p, PDFMAKE_EXREF, offset + tok.offset, "Expected 'xref' keyword");
878 0 return PDFMAKE_EXREF;
879 }
880  
881 for (;;) {
882 600 pdfmake_tok_peek_significant(&p->tok, &next);
883 600 ▶ if (next.kind == PDFMAKE_TOK_KW_TRAILER) break; /* trailer follows */
Branch: 1 branch
BranchTrueFalseTotal
if (next.kind == PDFMAKE_TOK_KW_TRAILER) break; /* trailer follows */ 300 300 600
884  
885 300 err = parse_xref_subsection_header(p, offset, &first_num, &count);
886 300 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 300 300
887  
888 300 ▶ if (!ensure_xref_cap(p, first_num + count)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ensure_xref_cap(p, first_num + count)) { 0 300 300
889 0 set_error(p, PDFMAKE_ENOMEM, 0, "Failed to allocate xref table");
890 0 return PDFMAKE_ENOMEM;
891 }
892  
893 4072 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 3772 300 4072
894 3772 err = parse_xref_entry(p, offset, first_num + i);
895 3772 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 3772 3772
896 }
897 }
898  
899 300 return PDFMAKE_OK;
900 }
901  
902 /*============================================================================
903 * Trailer parsing
904 *==========================================================================*/
905  
906 338 pdfmake_err_t pdfmake_parse_trailer(pdfmake_parser_t *p, pdfmake_obj_t *trailer) {
907 pdfmake_tok_t tok;
908 uint32_t root_id, info_id, size_id, prev_id, encrypt_id;
909 pdfmake_obj_t *root;
910 pdfmake_obj_t *info;
911 pdfmake_obj_t *encrypt;
912 uint32_t id_id;
913 pdfmake_obj_t *id_arr;
914 pdfmake_obj_t *size;
915 pdfmake_obj_t *prev;
916
917 338 tok = pdfmake_tok_next_significant(&p->tok);
918 338 ▶ if (tok.kind != PDFMAKE_TOK_KW_TRAILER) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.kind != PDFMAKE_TOK_KW_TRAILER) { 0 338 338
919 0 set_error(p, PDFMAKE_ETRAILER, tok.offset, "Expected 'trailer' keyword");
920 0 return PDFMAKE_ETRAILER;
921 }
922
923 338 *trailer = parse_object_internal(p);
924 338 ▶ if (p->last_err != PDFMAKE_OK) return p->last_err;
Branch: 1 branch
BranchTrueFalseTotal
if (p->last_err != PDFMAKE_OK) return p->last_err; 0 338 338
925
926 338 ▶ if (trailer->kind != PDFMAKE_DICT) {
Branch: 1 branch
BranchTrueFalseTotal
if (trailer->kind != PDFMAKE_DICT) { 0 338 338
927 0 set_error(p, PDFMAKE_ETRAILER, tok.offset, "Trailer must be a dictionary");
928 0 return PDFMAKE_ETRAILER;
929 }
930
931 /* Extract trailer values */
932 338 root_id = intern_name(p, "Root", 4);
933 338 info_id = intern_name(p, "Info", 4);
934 338 size_id = intern_name(p, "Size", 4);
935 338 prev_id = intern_name(p, "Prev", 4);
936 338 encrypt_id = intern_name(p, "Encrypt", 7);
937
938 338 root = pdfmake_dict_get(trailer, root_id);
939 338 ▶ if (root && root->kind == PDFMAKE_REF) {
Branch: 2 branches
BranchTrueFalseTotal
if (root && root->kind == PDFMAKE_REF) { 322 16 338
if (root && root->kind == PDFMAKE_REF) { 322 0 322
940 322 p->root_num = root->as.ref.num;
941 322 p->root_gen = root->as.ref.gen;
942 }
943
944 338 info = pdfmake_dict_get(trailer, info_id);
945 338 ▶ if (info && info->kind == PDFMAKE_REF) {
Branch: 2 branches
BranchTrueFalseTotal
if (info && info->kind == PDFMAKE_REF) { 266 72 338
if (info && info->kind == PDFMAKE_REF) { 266 0 266
946 266 p->info_num = info->as.ref.num;
947 266 p->info_gen = info->as.ref.gen;
948 }
949
950 338 encrypt = pdfmake_dict_get(trailer, encrypt_id);
951 338 ▶ if (encrypt && encrypt->kind == PDFMAKE_REF) {
Branch: 2 branches
BranchTrueFalseTotal
if (encrypt && encrypt->kind == PDFMAKE_REF) { 66 272 338
if (encrypt && encrypt->kind == PDFMAKE_REF) { 66 0 66
952 66 p->encrypt_num = encrypt->as.ref.num;
953 66 p->encrypt_gen = encrypt->as.ref.gen;
954 }
955  
956 /* Extract /ID array (needed for encryption key derivation) */
957 338 id_id = intern_name(p, "ID", 2);
958 338 id_arr = pdfmake_dict_get(trailer, id_id);
959 338 ▶ if (id_arr && id_arr->kind == PDFMAKE_ARRAY && pdfmake_array_len(id_arr) >= 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (id_arr && id_arr->kind == PDFMAKE_ARRAY && pdfmake_array_len(id_arr) >= 1) { 286 52 338
if (id_arr && id_arr->kind == PDFMAKE_ARRAY && pdfmake_array_len(id_arr) >= 1) { 286 0 286
if (id_arr && id_arr->kind == PDFMAKE_ARRAY && pdfmake_array_len(id_arr) >= 1) { 286 0 286
960 286 pdfmake_obj_t *id0 = pdfmake_array_get(id_arr, 0);
961 286 ▶ if (id0 && id0->kind == PDFMAKE_STR && p->doc_id_len == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (id0 && id0->kind == PDFMAKE_STR && p->doc_id_len == 0) { 286 0 286
if (id0 && id0->kind == PDFMAKE_STR && p->doc_id_len == 0) { 286 0 286
if (id0 && id0->kind == PDFMAKE_STR && p->doc_id_len == 0) { 129 157 286
962 129 size_t len = id0->as.str.len;
963 129 ▶ if (len > sizeof(p->doc_id)) len = sizeof(p->doc_id);
Branch: 1 branch
BranchTrueFalseTotal
if (len > sizeof(p->doc_id)) len = sizeof(p->doc_id); 0 129 129
964 129 memcpy(p->doc_id, id0->as.str.bytes, len);
965 129 p->doc_id_len = len;
966 }
967 }
968
969 338 size = pdfmake_dict_get(trailer, size_id);
970 338 ▶ if (size && size->kind == PDFMAKE_INT) {
Branch: 2 branches
BranchTrueFalseTotal
if (size && size->kind == PDFMAKE_INT) { 338 0 338
if (size && size->kind == PDFMAKE_INT) { 338 0 338
971 338 ▶ if (!ensure_xref_cap(p, (uint32_t)size->as.i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ensure_xref_cap(p, (uint32_t)size->as.i)) { 0 338 338
972 0 return PDFMAKE_ENOMEM;
973 }
974 338 ▶ if ((size_t)size->as.i > p->xref_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)size->as.i > p->xref_size) { 0 338 338
975 0 p->xref_size = (size_t)size->as.i;
976 }
977 }
978
979 /* Check for /Prev - incremental update chain */
980 338 prev = pdfmake_dict_get(trailer, prev_id);
981 338 ▶ if (prev && prev->kind == PDFMAKE_INT) {
Branch: 2 branches
BranchTrueFalseTotal
if (prev && prev->kind == PDFMAKE_INT) { 48 290 338
if (prev && prev->kind == PDFMAKE_INT) { 48 0 48
982 48 uint64_t prev_offset = (uint64_t)prev->as.i;
983 pdfmake_err_t err;
984 pdfmake_obj_t prev_trailer;
985
986 /* Cycle detection */
987 48 ▶ if (prev_visited(p, prev_offset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (prev_visited(p, prev_offset)) { 32 16 48
988 32 set_error(p, PDFMAKE_ECYCLE, 0, "Cycle detected in /Prev chain");
989 32 return PDFMAKE_ECYCLE;
990 }
991
992 16 ▶ if (!prev_add(p, prev_offset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!prev_add(p, prev_offset)) { 0 16 16
993 0 return PDFMAKE_ENOMEM;
994 }
995
996 /* Parse previous xref */
997 16 err = pdfmake_parse_xref_table(p, prev_offset);
998 16 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 16 16
999
1000 /* Parse previous trailer */
1001 16 err = pdfmake_parse_trailer(p, &prev_trailer);
1002 16 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 16 16
1003 }
1004
1005 306 return PDFMAKE_OK;
1006 }
1007  
1008 /*============================================================================
1009 * Xref stream parser (§7.5.8)
1010 *==========================================================================*/
1011  
1012 /* Parsed shape of the xref-stream dictionary parameters. */
1013 typedef struct {
1014 int w1, w2, w3; /* /W field widths */
1015 int entry_size; /* sum of w1+w2+w3 */
1016 pdfmake_obj_t *index; /* /Index array (or NULL → default range) */
1017 int64_t size; /* /Size value (total object count) */
1018 } xref_stream_params_t;
1019  
1020 /* Extract /W, /Index and /Size from the xref-stream dict. Returns
1021 * PDFMAKE_EXREF if /W is missing or malformed; /Index and /Size are
1022 * optional (callers fall back to a default range). */
1023 static pdfmake_err_t
1024 0 xref_stream_extract_params(pdfmake_parser_t *p, uint64_t offset,
1025 pdfmake_obj_t *dict,
1026 xref_stream_params_t *out)
1027 {
1028 0 uint32_t w_id = intern_name(p, "W", 1);
1029 0 pdfmake_obj_t *w_arr = pdfmake_dict_get(dict, w_id);
1030 uint32_t index_id;
1031 uint32_t size_id;
1032 pdfmake_obj_t *size_obj;
1033
1034 0 ▶ if (!w_arr || w_arr->kind != PDFMAKE_ARRAY || pdfmake_array_len(w_arr) < 3) {
Branch: 3 branches
BranchTrueFalseTotal
if (!w_arr || w_arr->kind != PDFMAKE_ARRAY || pdfmake_array_len(w_arr) < 3) { 0 0 0
if (!w_arr || w_arr->kind != PDFMAKE_ARRAY || pdfmake_array_len(w_arr) < 3) { 0 0 0
if (!w_arr || w_arr->kind != PDFMAKE_ARRAY || pdfmake_array_len(w_arr) < 3) { 0 0 0
1035 0 set_error(p, PDFMAKE_EXREF, offset, "Invalid /W array in xref stream");
1036 0 return PDFMAKE_EXREF;
1037 }
1038  
1039 0 out->w1 = (int)pdfmake_get_int(pdfmake_array_get(w_arr, 0));
1040 0 out->w2 = (int)pdfmake_get_int(pdfmake_array_get(w_arr, 1));
1041 0 out->w3 = (int)pdfmake_get_int(pdfmake_array_get(w_arr, 2));
1042 0 out->entry_size = out->w1 + out->w2 + out->w3;
1043  
1044 0 index_id = intern_name(p, "Index", 5);
1045 0 size_id = intern_name(p, "Size", 4);
1046 0 out->index = pdfmake_dict_get(dict, index_id);
1047  
1048 0 size_obj = pdfmake_dict_get(dict, size_id);
1049 0 ▶ out->size = (size_obj && size_obj->kind == PDFMAKE_INT) ? size_obj->as.i : 0;
Branch: 2 branches
BranchTrueFalseTotal
out->size = (size_obj && size_obj->kind == PDFMAKE_INT) ? size_obj->as.i : 0; 0 0 0
out->size = (size_obj && size_obj->kind == PDFMAKE_INT) ? size_obj->as.i : 0; 0 0 0
1050  
1051 0 return PDFMAKE_OK;
1052 }
1053  
1054 /* Propagate /Root, /Info and /Encrypt references from the xref-stream dict
1055 * into the parser state. Mirrors the trailer dict handling used by classic
1056 * xref tables. The /Prev chain is handled by the orchestrator because it
1057 * recurses. */
1058 static void
1059 0 xref_stream_apply_trailer(pdfmake_parser_t *p, pdfmake_obj_t *dict)
1060 {
1061 0 uint32_t root_id = intern_name(p, "Root", 4);
1062 0 uint32_t info_id = intern_name(p, "Info", 4);
1063 0 uint32_t encrypt_id = intern_name(p, "Encrypt", 7);
1064 pdfmake_obj_t *root;
1065 pdfmake_obj_t *info;
1066 pdfmake_obj_t *encrypt;
1067  
1068 0 root = pdfmake_dict_get(dict, root_id);
1069 0 ▶ if (root && root->kind == PDFMAKE_REF) {
Branch: 2 branches
BranchTrueFalseTotal
if (root && root->kind == PDFMAKE_REF) { 0 0 0
if (root && root->kind == PDFMAKE_REF) { 0 0 0
1070 0 p->root_num = root->as.ref.num;
1071 0 p->root_gen = root->as.ref.gen;
1072 }
1073  
1074 0 info = pdfmake_dict_get(dict, info_id);
1075 0 ▶ if (info && info->kind == PDFMAKE_REF) {
Branch: 2 branches
BranchTrueFalseTotal
if (info && info->kind == PDFMAKE_REF) { 0 0 0
if (info && info->kind == PDFMAKE_REF) { 0 0 0
1076 0 p->info_num = info->as.ref.num;
1077 0 p->info_gen = info->as.ref.gen;
1078 }
1079  
1080 0 encrypt = pdfmake_dict_get(dict, encrypt_id);
1081 0 ▶ if (encrypt && encrypt->kind == PDFMAKE_REF) {
Branch: 2 branches
BranchTrueFalseTotal
if (encrypt && encrypt->kind == PDFMAKE_REF) { 0 0 0
if (encrypt && encrypt->kind == PDFMAKE_REF) { 0 0 0
1082 0 p->encrypt_num = encrypt->as.ref.num;
1083 0 p->encrypt_gen = encrypt->as.ref.gen;
1084 }
1085 0 }
1086  
1087 /* Read one variable-width xref-stream entry at *pp (advancing it) and store
1088 * it into p->xref[obj_num] unless that slot is already loaded from a later
1089 * xref section. Called from both the /Index subsection loop and the
1090 * default-range loop, which are otherwise identical. */
1091 static PDFMAKE_INLINE void
1092 0 xref_stream_read_entry(pdfmake_parser_t *p,
1093 const uint8_t **pp,
1094 int w1, int w2, int w3,
1095 uint32_t obj_num)
1096 {
1097 0 const uint8_t *ptr = *pp;
1098 0 uint64_t field1 = 0, field2 = 0, field3 = 0;
1099 int j;
1100 int type;
1101  
1102 0 ▶ for (j = 0; j < w1; j++) field1 = (field1 << 8) | *ptr++;
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < w1; j++) field1 = (field1 << 8) | *ptr++; 0 0 0
1103 0 ▶ for (j = 0; j < w2; j++) field2 = (field2 << 8) | *ptr++;
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < w2; j++) field2 = (field2 << 8) | *ptr++; 0 0 0
1104 0 ▶ for (j = 0; j < w3; j++) field3 = (field3 << 8) | *ptr++;
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < w3; j++) field3 = (field3 << 8) | *ptr++; 0 0 0
1105 0 *pp = ptr;
1106  
1107 /* Default type is 1 when /W specifies a zero-width type field. */
1108 0 ▶ type = (w1 == 0) ? 1 : (int)field1;
Branch: 1 branch
BranchTrueFalseTotal
type = (w1 == 0) ? 1 : (int)field1; 0 0 0
1109  
1110 0 ▶ if (!p->xref[obj_num].loaded) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p->xref[obj_num].loaded) { 0 0 0
1111 0 p->xref[obj_num].num = obj_num;
1112 0 switch (type) {
1113 0 case 0: /* Free object */
1114 0 p->xref[obj_num].type = PDFMAKE_XREF_FREE;
1115 0 p->xref[obj_num].loc.next_free = (uint32_t)field2;
1116 0 p->xref[obj_num].gen = (uint16_t)field3;
1117 0 break;
1118 0 case 1: /* Uncompressed object */
1119 0 p->xref[obj_num].type = PDFMAKE_XREF_UNCOMPRESSED;
1120 0 p->xref[obj_num].loc.offset = field2;
1121 0 p->xref[obj_num].gen = (uint16_t)field3;
1122 0 break;
1123 0 case 2: /* Compressed object */
1124 0 p->xref[obj_num].type = PDFMAKE_XREF_COMPRESSED;
1125 0 p->xref[obj_num].loc.compressed.obj_stm_num = (uint32_t)field2;
1126 0 p->xref[obj_num].loc.compressed.index = (uint32_t)field3;
1127 0 p->xref[obj_num].gen = 0;
1128 0 break;
1129 }
1130 }
1131  
1132 0 ▶ if (obj_num >= p->xref_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (obj_num >= p->xref_size) { 0 0 0
1133 0 p->xref_size = obj_num + 1;
1134 }
1135 0 }
1136  
1137 20 pdfmake_err_t pdfmake_parse_xref_stream(pdfmake_parser_t *p, uint64_t offset) {
1138 pdfmake_tok_t num_tok;
1139 pdfmake_tok_t gen_tok;
1140 pdfmake_tok_t obj_tok;
1141 pdfmake_obj_t stream;
1142 pdfmake_obj_t stream_dict_obj;
1143 xref_stream_params_t params;
1144 pdfmake_err_t err;
1145 uint8_t *decoded_data;
1146 size_t decoded_len;
1147 const uint8_t *ptr;
1148 const uint8_t *end;
1149 uint32_t prev_id;
1150 pdfmake_obj_t *prev;
1151
1152 20 ▶ if (offset >= p->buf_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset >= p->buf_len) { 0 20 20
1153 0 set_error(p, PDFMAKE_EXREF, (size_t)offset, "Xref stream offset beyond EOF");
1154 0 return PDFMAKE_EXREF;
1155 }
1156
1157 /* Position tokenizer at xref stream object */
1158 20 pdfmake_tokenizer_init(&p->tok, p->buf + offset, p->buf_len - offset);
1159
1160 /* Parse as indirect object */
1161 20 num_tok = pdfmake_tok_next_significant(&p->tok);
1162 20 ▶ if (num_tok.kind != PDFMAKE_TOK_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_tok.kind != PDFMAKE_TOK_INT) { 0 20 20
1163 0 set_error(p, PDFMAKE_EXREF, offset, "Expected xref stream object number");
1164 0 return PDFMAKE_EXREF;
1165 }
1166
1167 20 gen_tok = pdfmake_tok_next_significant(&p->tok);
1168 20 ▶ if (gen_tok.kind != PDFMAKE_TOK_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (gen_tok.kind != PDFMAKE_TOK_INT) { 0 20 20
1169 0 set_error(p, PDFMAKE_EXREF, offset, "Expected xref stream generation");
1170 0 return PDFMAKE_EXREF;
1171 }
1172
1173 20 obj_tok = pdfmake_tok_next_significant(&p->tok);
1174 20 ▶ if (obj_tok.kind != PDFMAKE_TOK_KW_OBJ) {
Branch: 1 branch
BranchTrueFalseTotal
if (obj_tok.kind != PDFMAKE_TOK_KW_OBJ) { 20 0 20
1175 20 set_error(p, PDFMAKE_EXREF, offset, "Expected 'obj' keyword");
1176 20 return PDFMAKE_EXREF;
1177 }
1178
1179 /* Parse stream object */
1180 0 stream = parse_object_internal(p);
1181 0 ▶ if (p->last_err != PDFMAKE_OK) return p->last_err;
Branch: 1 branch
BranchTrueFalseTotal
if (p->last_err != PDFMAKE_OK) return p->last_err; 0 0 0
1182
1183 0 ▶ if (stream.kind != PDFMAKE_STREAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (stream.kind != PDFMAKE_STREAM) { 0 0 0
1184 0 set_error(p, PDFMAKE_EXREF, offset, "Xref stream is not a stream");
1185 0 return PDFMAKE_EXREF;
1186 }
1187
1188 /* Wrap the stream dict so we can call pdfmake_dict_get on it. */
1189 0 stream_dict_obj.kind = PDFMAKE_DICT;
1190 0 stream_dict_obj.as.dict = stream.as.stream->dict;
1191  
1192 0 err = xref_stream_extract_params(p, offset, &stream_dict_obj, &params);
1193 0 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 0 0
1194  
1195 /* Decode stream data */
1196 0 err = pdfmake_decode_stream(p, stream.as.stream, &decoded_data, &decoded_len);
1197 0 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 0 0
1198  
1199 /* Parse entries */
1200 0 ptr = decoded_data;
1201 0 end = decoded_data + decoded_len;
1202  
1203 0 ▶ if (params.index && params.index->kind == PDFMAKE_ARRAY) {
Branch: 2 branches
BranchTrueFalseTotal
if (params.index && params.index->kind == PDFMAKE_ARRAY) { 0 0 0
if (params.index && params.index->kind == PDFMAKE_ARRAY) { 0 0 0
1204 /* Multiple subsections */
1205 0 size_t num_pairs = pdfmake_array_len(params.index) / 2;
1206 size_t pair;
1207 0 ▶ for (pair = 0; pair < num_pairs; pair++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pair = 0; pair < num_pairs; pair++) { 0 0 0
1208 0 pdfmake_obj_t *first = pdfmake_array_get(params.index, pair * 2);
1209 0 pdfmake_obj_t *count = pdfmake_array_get(params.index, pair * 2 + 1);
1210 uint32_t first_num;
1211 uint32_t entry_count;
1212 uint32_t i;
1213 0 ▶ if (!first || !count) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!first || !count) continue; 0 0 0
if (!first || !count) continue; 0 0 0
1214  
1215 0 first_num = (uint32_t)pdfmake_get_int(first);
1216 0 entry_count = (uint32_t)pdfmake_get_int(count);
1217  
1218 0 ▶ if (!ensure_xref_cap(p, first_num + entry_count)) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!ensure_xref_cap(p, first_num + entry_count)) return PDFMAKE_ENOMEM; 0 0 0
1219  
1220 0 ▶ for (i = 0; i < entry_count && ptr + params.entry_size <= end; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < entry_count && ptr + params.entry_size <= end; i++) { 0 0 0
for (i = 0; i < entry_count && ptr + params.entry_size <= end; i++) { 0 0 0
1221 0 xref_stream_read_entry(p, &ptr, params.w1, params.w2, params.w3,
1222 first_num + i);
1223 }
1224 }
1225 } else {
1226 /* Default: single subsection from 0 to Size-1 */
1227 0 uint32_t entry_count = (uint32_t)params.size;
1228 uint32_t obj_num;
1229 0 ▶ if (!ensure_xref_cap(p, entry_count)) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!ensure_xref_cap(p, entry_count)) return PDFMAKE_ENOMEM; 0 0 0
1230  
1231 0 for (obj_num = 0;
1232 0 ▶ obj_num < entry_count && ptr + params.entry_size <= end;
Branch: 2 branches
BranchTrueFalseTotal
obj_num < entry_count && ptr + params.entry_size <= end; 0 0 0
obj_num < entry_count && ptr + params.entry_size <= end; 0 0 0
1233 0 obj_num++) {
1234 0 xref_stream_read_entry(p, &ptr, params.w1, params.w2, params.w3, obj_num);
1235 }
1236 }
1237  
1238 /* Propagate trailer-equivalent refs from the stream dict. */
1239 0 xref_stream_apply_trailer(p, &stream_dict_obj);
1240  
1241 /* Check for /Prev - incremental update chain */
1242 0 prev_id = intern_name(p, "Prev", 4);
1243 0 prev = pdfmake_dict_get(&stream_dict_obj, prev_id);
1244 0 ▶ if (prev && prev->kind == PDFMAKE_INT) {
Branch: 2 branches
BranchTrueFalseTotal
if (prev && prev->kind == PDFMAKE_INT) { 0 0 0
if (prev && prev->kind == PDFMAKE_INT) { 0 0 0
1245 0 uint64_t prev_offset = (uint64_t)prev->as.i;
1246 pdfmake_tokenizer_t save_tok;
1247 pdfmake_err_t prev_err;
1248
1249 0 ▶ if (prev_visited(p, prev_offset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (prev_visited(p, prev_offset)) { 0 0 0
1250 0 set_error(p, PDFMAKE_ECYCLE, 0, "Cycle detected in /Prev chain");
1251 0 return PDFMAKE_ECYCLE;
1252 }
1253
1254 0 ▶ if (!prev_add(p, prev_offset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!prev_add(p, prev_offset)) { 0 0 0
1255 0 return PDFMAKE_ENOMEM;
1256 }
1257
1258 /* Could be classic xref or xref stream */
1259 /* Try xref stream first, fall back to classic */
1260 0 save_tok = p->tok;
1261 0 prev_err = pdfmake_parse_xref_stream(p, prev_offset);
1262 0 ▶ if (prev_err != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (prev_err != PDFMAKE_OK) { 0 0 0
1263 0 p->tok = save_tok;
1264 0 prev_err = pdfmake_parse_xref_table(p, prev_offset);
1265 0 ▶ if (prev_err == PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (prev_err == PDFMAKE_OK) { 0 0 0
1266 pdfmake_obj_t prev_trailer;
1267 0 prev_err = pdfmake_parse_trailer(p, &prev_trailer);
1268 }
1269 }
1270 0 ▶ if (prev_err != PDFMAKE_OK) return prev_err;
Branch: 1 branch
BranchTrueFalseTotal
if (prev_err != PDFMAKE_OK) return prev_err; 0 0 0
1271 }
1272
1273 0 return PDFMAKE_OK;
1274 }
1275  
1276 /*============================================================================
1277 * Stream decoding helper
1278 *==========================================================================*/
1279  
1280 209 pdfmake_err_t pdfmake_decode_stream(pdfmake_parser_t *p,
1281 pdfmake_stream_t *stream,
1282 uint8_t **out_data,
1283 size_t *out_len) {
1284 pdfmake_obj_t dict_obj;
1285 uint32_t filter_id;
1286 uint32_t parms_id;
1287 pdfmake_obj_t *filter;
1288 pdfmake_obj_t *parms;
1289 pdfmake_buf_t out;
1290 pdfmake_err_t err;
1291 size_t len;
1292 uint8_t *data;
1293
1294 209 ▶ if (stream->filtered) {
Branch: 1 branch
BranchTrueFalseTotal
if (stream->filtered) { 0 209 209
1295 /* Already decoded - just return raw data */
1296 0 *out_data = (uint8_t *)stream->raw;
1297 0 *out_len = stream->raw_len;
1298 0 return PDFMAKE_OK;
1299 }
1300
1301 /* Get /Filter and /DecodeParms from stream dict */
1302 209 dict_obj.kind = PDFMAKE_DICT;
1303 209 dict_obj.as.dict = stream->dict;
1304
1305 209 filter_id = intern_name(p, "Filter", 6);
1306 209 parms_id = intern_name(p, "DecodeParms", 11);
1307
1308 209 filter = pdfmake_dict_get(&dict_obj, filter_id);
1309 209 parms = pdfmake_dict_get(&dict_obj, parms_id);
1310
1311 209 ▶ if (!filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (!filter) { 115 94 209
1312 /* No filter - raw data */
1313 115 *out_data = (uint8_t *)stream->raw;
1314 115 *out_len = stream->raw_len;
1315 115 return PDFMAKE_OK;
1316 }
1317
1318 /* Decode through filter chain */
1319 94 pdfmake_buf_init(&out);
1320
1321 94 err = pdfmake_filter_chain_decode(p->doc->arena, filter, parms,
1322 94 stream->raw, stream->raw_len, &out);
1323 94 ▶ if (err != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) { 0 94 94
1324 0 pdfmake_buf_free(&out);
1325 0 set_error(p, err, 0, "Stream decode failed");
1326 0 return err;
1327 }
1328
1329 /* Copy to arena */
1330 94 len = pdfmake_buf_len(&out);
1331 94 data = pdfmake_arena_alloc(p->doc->arena, len);
1332 94 ▶ if (!data) {
Branch: 1 branch
BranchTrueFalseTotal
if (!data) { 0 94 94
1333 0 pdfmake_buf_free(&out);
1334 0 return PDFMAKE_ENOMEM;
1335 }
1336 94 memcpy(data, pdfmake_buf_data(&out), len);
1337 94 pdfmake_buf_free(&out);
1338
1339 94 *out_data = data;
1340 94 *out_len = len;
1341 94 return PDFMAKE_OK;
1342 }
1343  
1344 /*============================================================================
1345 * Repair mode (§7.5 fallback)
1346 *==========================================================================*/
1347  
1348 44 pdfmake_err_t pdfmake_repair_xref(pdfmake_parser_t *p) {
1349 44 size_t trailer_offset = 0;
1350
1351 /* Scan entire file for "N G obj" patterns */
1352 44 pdfmake_tokenizer_init(&p->tok, p->buf, p->buf_len);
1353
1354 2912 while (1) {
1355 2956 pdfmake_tok_t tok = pdfmake_tok_next_significant(&p->tok);
1356 2956 ▶ if (tok.kind == PDFMAKE_TOK_EOF) break;
Branch: 1 branch
BranchTrueFalseTotal
if (tok.kind == PDFMAKE_TOK_EOF) break; 44 2912 2956
1357
1358 2912 ▶ if (tok.kind == PDFMAKE_TOK_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.kind == PDFMAKE_TOK_INT) { 1820 1092 2912
1359 1820 size_t save_pos = p->tok.pos;
1360 pdfmake_tok_t gen_tok;
1361
1362 1820 gen_tok = pdfmake_tok_next_significant(&p->tok);
1363 1820 ▶ if (gen_tok.kind == PDFMAKE_TOK_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (gen_tok.kind == PDFMAKE_TOK_INT) { 1046 774 1820
1364 1046 pdfmake_tok_t obj_tok = pdfmake_tok_next_significant(&p->tok);
1365 1046 ▶ if (obj_tok.kind == PDFMAKE_TOK_KW_OBJ) {
Branch: 1 branch
BranchTrueFalseTotal
if (obj_tok.kind == PDFMAKE_TOK_KW_OBJ) { 256 790 1046
1366 /* Found object definition */
1367 256 uint32_t num = (uint32_t)tok.payload.int_val;
1368 256 uint16_t gen = (uint16_t)gen_tok.payload.int_val;
1369
1370 256 ▶ if (ensure_xref_cap(p, num + 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ensure_xref_cap(p, num + 1)) { 256 0 256
1371 256 p->xref[num].num = num;
1372 256 p->xref[num].gen = gen;
1373 256 p->xref[num].type = PDFMAKE_XREF_UNCOMPRESSED;
1374 256 p->xref[num].loc.offset = tok.offset;
1375 256 ▶ if (num >= p->xref_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (num >= p->xref_size) { 52 204 256
1376 52 p->xref_size = num + 1;
1377 }
1378 }
1379
1380 /* Skip to endobj */
1381 5801 while (1) {
1382 6057 pdfmake_tok_t skip = pdfmake_tok_next_significant(&p->tok);
1383 6057 ▶ if (skip.kind == PDFMAKE_TOK_KW_ENDOBJ ||
Branch: 1 branch
BranchTrueFalseTotal
if (skip.kind == PDFMAKE_TOK_KW_ENDOBJ || 5817 240 6057
1384 5817 ▶ skip.kind == PDFMAKE_TOK_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
skip.kind == PDFMAKE_TOK_EOF) { 5801 16 5817
1385 break;
1386 }
1387 }
1388 256 continue;
1389 }
1390 }
1391
1392 /* Not an object - restore position */
1393 1564 p->tok.pos = save_pos;
1394 1564 p->tok.has_peeked = 0;
1395 }
1396 }
1397
1398 /* Look for trailer dict */
1399 44 pdfmake_tokenizer_init(&p->tok, p->buf, p->buf_len);
1400
1401 /* Find last trailer keyword */
1402 17848 while (1) {
1403 17892 pdfmake_tok_t tok = pdfmake_tok_next(&p->tok);
1404 17892 ▶ if (tok.kind == PDFMAKE_TOK_EOF) break;
Branch: 1 branch
BranchTrueFalseTotal
if (tok.kind == PDFMAKE_TOK_EOF) break; 44 17848 17892
1405 17848 ▶ if (tok.kind == PDFMAKE_TOK_KW_TRAILER) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.kind == PDFMAKE_TOK_KW_TRAILER) { 38 17810 17848
1406 38 trailer_offset = tok.offset;
1407 }
1408 }
1409
1410 44 ▶ if (trailer_offset > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (trailer_offset > 0) { 38 6 44
1411 pdfmake_obj_t trailer;
1412 pdfmake_err_t trailer_err;
1413 38 pdfmake_tokenizer_init(&p->tok, p->buf + trailer_offset, p->buf_len - trailer_offset);
1414 38 p->last_err = PDFMAKE_OK; /* Clear any residual error from xref scan */
1415 38 trailer_err = pdfmake_parse_trailer(p, &trailer);
1416 (void)trailer_err; /* Ignore errors - best effort */
1417 }
1418
1419 44 return PDFMAKE_OK;
1420 }
1421  
1422 /*============================================================================
1423 * Object resolution
1424 *==========================================================================*/
1425  
1426 11803 pdfmake_obj_t *pdfmake_parser_resolve(pdfmake_parser_t *p, pdfmake_ref_t ref) {
1427 pdfmake_xref_entry_t *entry;
1428
1429 11803 ▶ if (ref.num >= p->xref_size) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ref.num >= p->xref_size) return NULL; 1 11802 11803
1430
1431 11802 entry = &p->xref[ref.num];
1432 11802 ▶ if (entry->type == PDFMAKE_XREF_FREE) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (entry->type == PDFMAKE_XREF_FREE) return NULL; 0 11802 11802
1433
1434 /* Check generation */
1435 11802 ▶ if (entry->gen != ref.gen) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (entry->gen != ref.gen) return NULL; 0 11802 11802
1436
1437 /* Already loaded? */
1438 11802 ▶ if (entry->loaded && ref.num < p->doc->obj_count) {
Branch: 2 branches
BranchTrueFalseTotal
if (entry->loaded && ref.num < p->doc->obj_count) { 10711 1091 11802
if (entry->loaded && ref.num < p->doc->obj_count) { 10688 23 10711
1439 10688 pdfmake_obj_t *obj = pdfmake_doc_get(p->doc, ref.num);
1440 10688 ▶ if (obj) return obj;
Branch: 1 branch
BranchTrueFalseTotal
if (obj) return obj; 10688 0 10688
1441 }
1442
1443 /* Load object */
1444 1114 ▶ if (entry->type == PDFMAKE_XREF_UNCOMPRESSED) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->type == PDFMAKE_XREF_UNCOMPRESSED) { 1114 0 1114
1445 /* Save tokenizer state */
1446 1114 pdfmake_tokenizer_t save_tok = p->tok;
1447 pdfmake_err_t err;
1448
1449 /* Position at object - use full buffer but set position */
1450 1114 pdfmake_tokenizer_init(&p->tok, p->buf, p->buf_len);
1451 1114 p->tok.pos = entry->loc.offset;
1452
1453 /* Parse indirect object */
1454 1114 err = pdfmake_parse_indirect_object(p);
1455
1456 /* Restore tokenizer */
1457 1114 p->tok = save_tok;
1458
1459 1114 ▶ if (err == PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == PDFMAKE_OK) { 1114 0 1114
1460 1114 entry->loaded = 1;
1461 1114 return pdfmake_doc_get(p->doc, ref.num);
1462 }
1463 }
1464 0 ▶ else if (entry->type == PDFMAKE_XREF_COMPRESSED) {
Branch: 1 branch
BranchTrueFalseTotal
else if (entry->type == PDFMAKE_XREF_COMPRESSED) { 0 0 0
1465 /* Object is in an object stream - need to decompress and parse */
1466 /* First resolve the object stream */
1467 pdfmake_ref_t stm_ref;
1468 pdfmake_obj_t *stm_obj;
1469 uint8_t *decoded;
1470 size_t decoded_len;
1471 pdfmake_obj_t stm_dict_obj;
1472 uint32_t n_id;
1473 uint32_t first_id;
1474 pdfmake_obj_t *n_obj;
1475 pdfmake_obj_t *first_obj;
1476 int64_t n, first;
1477 pdfmake_tokenizer_t stm_tok;
1478 uint32_t target_index;
1479 int64_t target_offset;
1480 int64_t i;
1481 pdfmake_tokenizer_t save_tok;
1482 pdfmake_obj_t obj;
1483 size_t idx;
1484
1485 0 stm_ref.num = entry->loc.compressed.obj_stm_num;
1486 0 stm_ref.gen = 0;
1487 0 stm_obj = pdfmake_parser_resolve(p, stm_ref);
1488
1489 0 ▶ if (!stm_obj || stm_obj->kind != PDFMAKE_STREAM) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!stm_obj || stm_obj->kind != PDFMAKE_STREAM) return NULL; 0 0 0
if (!stm_obj || stm_obj->kind != PDFMAKE_STREAM) return NULL; 0 0 0
1490
1491 /* Decode stream */
1492 0 ▶ if (pdfmake_decode_stream(p, stm_obj->as.stream, &decoded, &decoded_len) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_decode_stream(p, stm_obj->as.stream, &decoded, &decoded_len) != PDFMAKE_OK) { 0 0 0
1493 0 return NULL;
1494 }
1495
1496 /* Parse object stream header */
1497 /* Format: N1 offset1 N2 offset2 ... followed by objects */
1498 0 stm_dict_obj.kind = PDFMAKE_DICT;
1499 0 stm_dict_obj.as.dict = stm_obj->as.stream->dict;
1500
1501 0 n_id = intern_name(p, "N", 1);
1502 0 first_id = intern_name(p, "First", 5);
1503
1504 0 n_obj = pdfmake_dict_get(&stm_dict_obj, n_id);
1505 0 first_obj = pdfmake_dict_get(&stm_dict_obj, first_id);
1506
1507 0 ▶ if (!n_obj || !first_obj) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!n_obj || !first_obj) return NULL; 0 0 0
if (!n_obj || !first_obj) return NULL; 0 0 0
1508
1509 0 n = pdfmake_get_int(n_obj);
1510 0 first = pdfmake_get_int(first_obj);
1511
1512 /* Parse header (pairs of obj_num offset) */
1513 0 pdfmake_tokenizer_init(&stm_tok, decoded, decoded_len);
1514
1515 0 target_index = entry->loc.compressed.index;
1516 0 target_offset = -1;
1517
1518 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
1519 0 pdfmake_tok_t num_tok = pdfmake_tok_next_significant(&stm_tok);
1520 0 pdfmake_tok_t off_tok = pdfmake_tok_next_significant(&stm_tok);
1521
1522 0 ▶ if (num_tok.kind != PDFMAKE_TOK_INT || off_tok.kind != PDFMAKE_TOK_INT) {
Branch: 2 branches
BranchTrueFalseTotal
if (num_tok.kind != PDFMAKE_TOK_INT || off_tok.kind != PDFMAKE_TOK_INT) { 0 0 0
if (num_tok.kind != PDFMAKE_TOK_INT || off_tok.kind != PDFMAKE_TOK_INT) { 0 0 0
1523 break;
1524 }
1525
1526 0 ▶ if ((uint32_t)i == target_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ((uint32_t)i == target_index) { 0 0 0
1527 0 target_offset = first + off_tok.payload.int_val;
1528 0 break;
1529 }
1530 }
1531
1532 0 ▶ if (target_offset < 0 || (size_t)target_offset >= decoded_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (target_offset < 0 || (size_t)target_offset >= decoded_len) { 0 0 0
if (target_offset < 0 || (size_t)target_offset >= decoded_len) { 0 0 0
1533 0 return NULL;
1534 }
1535
1536 /* Parse object at offset */
1537 0 save_tok = p->tok;
1538 0 pdfmake_tokenizer_init(&p->tok, decoded + target_offset, decoded_len - target_offset);
1539
1540 0 obj = parse_object_internal(p);
1541 0 p->tok = save_tok;
1542
1543 0 ▶ if (p->last_err != PDFMAKE_OK) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (p->last_err != PDFMAKE_OK) return NULL; 0 0 0
1544
1545 /* Store in document (1-based numbering: object N at index N-1) */
1546 0 ▶ while (ref.num > p->doc->obj_cap) {
Branch: 1 branch
BranchTrueFalseTotal
while (ref.num > p->doc->obj_cap) { 0 0 0
1547 0 ▶ size_t new_cap = p->doc->obj_cap ? p->doc->obj_cap * 2 : PDFMAKE_DOC_INIT_CAP;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = p->doc->obj_cap ? p->doc->obj_cap * 2 : PDFMAKE_DOC_INIT_CAP; 0 0 0
1548 pdfmake_indirect_t *new_objs;
1549 0 ▶ while (new_cap < ref.num) new_cap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_cap < ref.num) new_cap *= 2; 0 0 0
1550 0 new_objs = realloc(p->doc->objects,
1551 new_cap * sizeof(pdfmake_indirect_t));
1552 0 ▶ if (!new_objs) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!new_objs) return NULL; 0 0 0
1553 0 memset(new_objs + p->doc->obj_cap, 0,
1554 0 (new_cap - p->doc->obj_cap) * sizeof(pdfmake_indirect_t));
1555 0 p->doc->objects = new_objs;
1556 0 p->doc->obj_cap = new_cap;
1557 }
1558
1559 0 idx = ref.num - 1;
1560 0 p->doc->objects[idx].num = ref.num;
1561 0 p->doc->objects[idx].gen = 0;
1562 0 p->doc->objects[idx].obj = obj;
1563 0 p->doc->objects[idx].in_use = 1;
1564
1565 0 ▶ if (ref.num > p->doc->obj_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref.num > p->doc->obj_count) { 0 0 0
1566 0 p->doc->obj_count = ref.num;
1567 }
1568
1569 0 entry->loaded = 1;
1570 0 return &p->doc->objects[idx].obj;
1571 }
1572
1573 0 return NULL;
1574 }
1575  
1576 0 pdfmake_obj_t *pdfmake_doc_resolve(pdfmake_doc_t *doc, pdfmake_parser_t *parser,
1577 uint32_t num, uint16_t gen) {
1578 pdfmake_ref_t ref;
1579 (void)doc; /* doc info is in parser->doc, used for consistency with other APIs */
1580 0 ref.num = num;
1581 0 ref.gen = gen;
1582 0 return pdfmake_parser_resolve(parser, ref);
1583 }
1584  
1585 /*============================================================================
1586 * Main parser entry point
1587 *==========================================================================*/
1588  
1589 312 pdfmake_err_t pdfmake_parser_run(pdfmake_parser_t *p, pdfmake_doc_t **out_doc) {
1590 pdfmake_err_t err;
1591 int major, minor;
1592 uint64_t xref_offset;
1593 pdfmake_tok_t first;
1594 pdfmake_obj_t trailer;
1595
1596 /* Check header */
1597 312 err = pdfmake_check_header(p, &major, &minor);
1598 312 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 312 312
1599
1600 /* Locate startxref */
1601 312 err = pdfmake_locate_startxref(p, &xref_offset);
1602 312 ▶ if (err != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) { 6 306 312
1603 6 ▶ if (p->repair) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->repair) { 6 0 6
1604 6 err = pdfmake_repair_xref(p);
1605 6 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 6 6
1606 } else {
1607 0 return err;
1608 }
1609 } else {
1610 /* Add to visited list for cycle detection */
1611 306 ▶ if (!prev_add(p, xref_offset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!prev_add(p, xref_offset)) { 0 306 306
1612 0 return PDFMAKE_ENOMEM;
1613 }
1614
1615 /* Try xref stream first (common in modern PDFs) */
1616 /* Check what's at the offset */
1617 306 ▶ if (xref_offset < p->buf_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (xref_offset < p->buf_len) { 306 0 306
1618 306 pdfmake_tokenizer_init(&p->tok, p->buf + xref_offset, p->buf_len - xref_offset);
1619 306 pdfmake_tok_peek_significant(&p->tok, &first);
1620
1621 306 ▶ if (first.kind == PDFMAKE_TOK_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (first.kind == PDFMAKE_TOK_INT) { 20 286 306
1622 /* Xref stream */
1623 20 err = pdfmake_parse_xref_stream(p, xref_offset);
1624 286 ▶ } else if (first.kind == PDFMAKE_TOK_KW_XREF) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (first.kind == PDFMAKE_TOK_KW_XREF) { 284 2 286
1625 /* Classic xref */
1626 284 err = pdfmake_parse_xref_table(p, xref_offset);
1627 284 ▶ if (err == PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (err == PDFMAKE_OK) { 284 0 284
1628 284 err = pdfmake_parse_trailer(p, &trailer);
1629 }
1630 } else {
1631 2 err = PDFMAKE_EXREF;
1632 2 set_error(p, err, (size_t)xref_offset, "Invalid xref location");
1633 }
1634
1635 306 ▶ if (err != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) { 38 268 306
1636 38 ▶ if (p->repair) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->repair) { 38 0 38
1637 38 err = pdfmake_repair_xref(p);
1638 38 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 38 38
1639 } else {
1640 0 return err;
1641 }
1642 }
1643 } else {
1644 /* Offset beyond file - try repair if enabled */
1645 0 ▶ if (p->repair) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->repair) { 0 0 0
1646 0 err = pdfmake_repair_xref(p);
1647 0 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 0 0
1648 } else {
1649 0 set_error(p, PDFMAKE_EXREF, (size_t)xref_offset, "Xref offset beyond EOF");
1650 0 return PDFMAKE_EXREF;
1651 }
1652 }
1653 }
1654
1655 /* Set document trailer refs */
1656 312 ▶ if (p->root_num > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->root_num > 0) { 306 6 312
1657 306 pdfmake_doc_set_root(p->doc, p->root_num, p->root_gen);
1658 }
1659 312 ▶ if (p->info_num > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->info_num > 0) { 250 62 312
1660 250 pdfmake_doc_set_info(p->doc, p->info_num, p->info_gen);
1661 }
1662
1663 312 *out_doc = p->doc;
1664 312 return PDFMAKE_OK;
1665 }
« src/pdfmake_watermark.c ↑ Summary src/pdfmake_ocg.c »