src/pdfmake_parser.c

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