Debian Codegraph @

pdf/pdf_xref.c

raw
1 /* Copyright (C) 2018-2025 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
13 CA 94129, USA, for further information.
14 */
15
16 /* xref parsing */
17
18 #include "pdf_int.h"
19 #include "pdf_stack.h"
20 #include "pdf_xref.h"
21 #include "pdf_file.h"
22 #include "pdf_loop_detect.h"
23 #include "pdf_dict.h"
24 #include "pdf_array.h"
25 #include "pdf_repair.h"
26
27 static int resize_xref(pdf_context *ctx, uint64_t new_size)
28 {
29 xref_entry *new_xrefs;
30
31 /* Although we can technically handle object numbers larger than this, on some systems (32-bit Windows)
32 * memset is limited to a (signed!) integer for the size of memory to clear. We could deal
33 * with this by clearing the memory in blocks, but really, this is almost certainly a
34 * corrupted file or something.
35 */
36 if (new_size >= (0x7ffffff / sizeof(xref_entry)))
37 return_error(gs_error_rangecheck);
38
39 new_xrefs = (xref_entry *)gs_alloc_bytes(ctx->memory, (size_t)(new_size) * sizeof(xref_entry), "read_xref_stream allocate xref table entries");
40 if (new_xrefs == NULL){
41 pdfi_countdown(ctx->xref_table);
42 ctx->xref_table = NULL;
43 return_error(gs_error_VMerror);
44 }
45 memset(new_xrefs, 0x00, (new_size) * sizeof(xref_entry));
46 memcpy(new_xrefs, ctx->xref_table->xref, ctx->xref_table->xref_size * sizeof(xref_entry));
47 gs_free_object(ctx->memory, ctx->xref_table->xref, "reallocated xref entries");
48 ctx->xref_table->xref = new_xrefs;
49 ctx->xref_table->xref_size = new_size;
50 return 0;
51 }
52
53 static int read_xref_stream_entries(pdf_context *ctx, pdf_c_stream *s, int64_t first, int64_t last, int64_t *W)
54 {
55 uint i, j;
56 uint64_t field_width = 0;
57 uint32_t type = 0;
58 uint64_t objnum = 0, gen = 0;
59 byte *Buffer;
60 int64_t bytes = 0;
61 xref_entry *entry;
62
63 /* Find max number of bytes to be read */
64 field_width = W[0];
65 if (W[1] > field_width)
66 field_width = W[1];
67 if (W[2] > field_width)
68 field_width = W[2];
69
70 Buffer = gs_alloc_bytes(ctx->memory, field_width, "read_xref_stream_entry working buffer");
71 if (Buffer == NULL)
72 return_error(gs_error_VMerror);
73
74 for (i=first;i<=last; i++){
75 /* Defaults if W[n] = 0 */
76 type = 1;
77 objnum = gen = 0;
78
79 if (W[0] != 0) {
80 type = 0;
81 bytes = pdfi_read_bytes(ctx, Buffer, 1, W[0], s);
82 if (bytes < W[0]){
83 gs_free_object(ctx->memory, Buffer, "read_xref_stream_entry, free working buffer (error)");
84 return_error(gs_error_ioerror);
85 }
86 for (j=0;j<W[0];j++)
87 type = (type << 8) + Buffer[j];
88 }
89
90 if (W[1] != 0) {
91 bytes = pdfi_read_bytes(ctx, Buffer, 1, W[1], s);
92 if (bytes < W[1]){
93 gs_free_object(ctx->memory, Buffer, "read_xref_stream_entry free working buffer (error)");
94 return_error(gs_error_ioerror);
95 }
96 for (j=0;j<W[1];j++)
97 objnum = (objnum << 8) + Buffer[j];
98 }
99
100 if (W[2] != 0) {
101 bytes = pdfi_read_bytes(ctx, Buffer, 1, W[2], s);
102 if (bytes < W[2]){
103 gs_free_object(ctx->memory, Buffer, "read_xref_stream_entry, free working buffer (error)");
104 return_error(gs_error_ioerror);
105 }
106 for (j=0;j<W[2];j++)
107 gen = (gen << 8) + Buffer[j];
108 }
109
110 entry = &ctx->xref_table->xref[i];
111 if (entry->object_num != 0 && !entry->free)
112 continue;
113
114 entry->compressed = false;
115 entry->free = false;
116 entry->object_num = i;
117 entry->cache = NULL;
118
119 switch(type) {
120 case 0:
121 entry->free = true;
122 entry->u.uncompressed.offset = objnum; /* For free objects we use the offset to store the object number of the next free object */
123 entry->u.uncompressed.generation_num = gen; /* And the generation number is the numebr to use if this object is used again */
124 break;
125 case 1:
126 entry->u.uncompressed.offset = objnum;
127 entry->u.uncompressed.generation_num = gen;
128 break;
129 case 2:
130 entry->compressed = true;
131 entry->u.compressed.compressed_stream_num = objnum; /* The object number of the compressed stream */
132 entry->u.compressed.object_index = gen; /* And the index of the object within the stream */
133 break;
134 default:
135 gs_free_object(ctx->memory, Buffer, "read_xref_stream_entry, free working buffer");
136 return_error(gs_error_rangecheck);
137 break;
138 }
139 }
140 gs_free_object(ctx->memory, Buffer, "read_xref_stream_entry, free working buffer");
141 return 0;
142 }
143
144 /* Forward definition */
145 static int read_xref(pdf_context *ctx, pdf_c_stream *s);
146 static int pdfi_check_xref_stream(pdf_context *ctx);
147 /* These two routines are recursive.... */
148 static int pdfi_read_xref_stream_dict(pdf_context *ctx, pdf_c_stream *s, int obj_num);
149
150 static int pdfi_process_xref_stream(pdf_context *ctx, pdf_stream *stream_obj, pdf_c_stream *s)
151 {
152 pdf_c_stream *XRefStrm;
153 int code, i;
154 pdf_dict *sdict = NULL;
155 pdf_name *n;
156 pdf_array *a;
157 int64_t size;
158 int64_t num;
159 int64_t W[3] = {0, 0, 0};
160 int objnum;
161 bool known = false;
162
163 if (pdfi_type_of(stream_obj) != PDF_STREAM)
164 return_error(gs_error_typecheck);
165
166 code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream_obj, &sdict);
167 if (code < 0)
168 return code;
169
170 code = pdfi_dict_get_type(ctx, sdict, "Type", PDF_NAME, (pdf_obj **)&n);
171 if (code < 0)
172 return code;
173
174 if (n->length != 4 || memcmp(n->data, "XRef", 4) != 0) {
175 pdfi_countdown(n);
176 return_error(gs_error_syntaxerror);
177 }
178 pdfi_countdown(n);
179
180 code = pdfi_dict_get_int(ctx, sdict, "Size", &size);
181 if (code < 0)
182 return code;
183 if (size < 1)
184 return 0;
185
186 if (size < 0 || size > floor((double)ARCH_MAX_SIZE_T / (double)sizeof(xref_entry)))
187 return_error(gs_error_rangecheck);
188
189 /* If this is the first xref stream then allocate the xref table and store the trailer */
190 if (ctx->xref_table == NULL) {
191 ctx->xref_table = (xref_table_t *)gs_alloc_bytes(ctx->memory, sizeof(xref_table_t), "read_xref_stream allocate xref table");
192 if (ctx->xref_table == NULL) {
193 return_error(gs_error_VMerror);
194 }
195 memset(ctx->xref_table, 0x00, sizeof(xref_table_t));
196 ctx->xref_table->xref = (xref_entry *)gs_alloc_bytes(ctx->memory, (size_t)size * sizeof(xref_entry), "read_xref_stream allocate xref table entries");
197 if (ctx->xref_table->xref == NULL){
198 gs_free_object(ctx->memory, ctx->xref_table, "failed to allocate xref table entries");
199 ctx->xref_table = NULL;
200 return_error(gs_error_VMerror);
201 }
202 memset(ctx->xref_table->xref, 0x00, size * sizeof(xref_entry));
203 ctx->xref_table->ctx = ctx;
204 ctx->xref_table->type = PDF_XREF_TABLE;
205 ctx->xref_table->xref_size = size;
206 #if REFCNT_DEBUG
207 ctx->xref_table->UID = ctx->ref_UID++;
208 outprintf(ctx->memory, "Allocated xref table with UID %"PRIi64"\n", ctx->xref_table->UID);
209 #endif
210 pdfi_countup(ctx->xref_table);
211
212 pdfi_countdown(ctx->Trailer);
213
214 ctx->Trailer = sdict;
215 pdfi_countup(sdict);
216 } else {
217 if (size > ctx->xref_table->xref_size)
218 return_error(gs_error_rangecheck);
219
220 code = pdfi_merge_dicts(ctx, ctx->Trailer, sdict);
221 if (code < 0 && (code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BADXREF, "pdfi_process_xref_stream", NULL)) < 0) {
222 goto exit;
223 }
224 }
225
226 pdfi_seek(ctx, ctx->main_stream, pdfi_stream_offset(ctx, stream_obj), SEEK_SET);
227
228 /* Bug #691220 has a PDF file with a compressed XRef, the stream dictionary has
229 * a /DecodeParms entry for the stream, which has a /Colors value of 5, which makes
230 * *no* sense whatever. If we try to apply a Predictor then we end up in a loop trying
231 * to read 5 colour samples. Rather than meddles with more parameters to the filter
232 * code, we'll just remove the Colors entry from the DecodeParms dictionary,
233 * because it is nonsense. This means we'll get the (sensible) default value of 1.
234 */
235 code = pdfi_dict_known(ctx, sdict, "DecodeParms", &known);
236 if (code < 0)
237 return code;
238
239 if (known) {
240 pdf_dict *DP;
241 double f;
242 pdf_obj *name;
243
244 code = pdfi_dict_get_type(ctx, sdict, "DecodeParms", PDF_DICT, (pdf_obj **)&DP);
245 if (code < 0)
246 return code;
247
248 code = pdfi_dict_knownget_number(ctx, DP, "Colors", &f);
249 if (code < 0) {
250 pdfi_countdown(DP);
251 return code;
252 }
253 if (code > 0 && f != (double)1)
254 {
255 code = pdfi_name_alloc(ctx, (byte *)"Colors", 6, &name);
256 if (code < 0) {
257 pdfi_countdown(DP);
258 return code;
259 }
260 pdfi_countup(name);
261
262 code = pdfi_dict_delete_pair(ctx, DP, (pdf_name *)name);
263 pdfi_countdown(name);
264 if (code < 0) {
265 pdfi_countdown(DP);
266 return code;
267 }
268 }
269 pdfi_countdown(DP);
270 }
271
272 code = pdfi_filter_no_decryption(ctx, stream_obj, s, &XRefStrm, false);
273 if (code < 0) {
274 pdfi_countdown(ctx->xref_table);
275 ctx->xref_table = NULL;
276 return code;
277 }
278
279 code = pdfi_dict_get_type(ctx, sdict, "W", PDF_ARRAY, (pdf_obj **)&a);
280 if (code < 0) {
281 pdfi_close_file(ctx, XRefStrm);
282 pdfi_countdown(ctx->xref_table);
283 ctx->xref_table = NULL;
284 return code;
285 }
286
287 if (pdfi_array_size(a) != 3) {
288 pdfi_countdown(a);
289 pdfi_close_file(ctx, XRefStrm);
290 pdfi_countdown(ctx->xref_table);
291 ctx->xref_table = NULL;
292 return_error(gs_error_rangecheck);
293 }
294 for (i=0;i<3;i++) {
295 code = pdfi_array_get_int(ctx, a, (uint64_t)i, (int64_t *)&W[i]);
296 if (code < 0 || W[i] < 0) {
297 pdfi_countdown(a);
298 pdfi_close_file(ctx, XRefStrm);
299 pdfi_countdown(ctx->xref_table);
300 ctx->xref_table = NULL;
301 if (W[i] < 0)
302 code = gs_note_error(gs_error_rangecheck);
303 return code;
304 }
305 }
306 pdfi_countdown(a);
307
308 /* W[0] is either:
309 * 0 (no type field) or a single byte with the type.
310 * W[1] is either:
311 * The object number of the next free object, the byte offset of this object in the file or the object5 number of the object stream where this object is stored.
312 * W[2] is either:
313 * The generation number to use if this object is used again, the generation number of the object or the index of this object within the object stream.
314 *
315 * Object and generation numbers are limited to unsigned 64-bit values, as are bytes offsets in the file, indexes of objects within the stream likewise (actually
316 * most of these are generally 32-bit max). So we can limit the field widths to 8 bytes, enough to hold a 64-bit number.
317 * Even if a later version of the spec makes these larger (which seems unlikely!) we still cna't cope with integers > 64-bits.
318 */
319 if (W[0] > 1 || W[1] > 8 || W[2] > 8) {
320 pdfi_close_file(ctx, XRefStrm);
321 pdfi_countdown(ctx->xref_table);
322 ctx->xref_table = NULL;
323 return code;
324 }
325
326 code = pdfi_dict_get_type(ctx, sdict, "Index", PDF_ARRAY, (pdf_obj **)&a);
327 if (code == gs_error_undefined) {
328 code = read_xref_stream_entries(ctx, XRefStrm, 0, size - 1, W);
329 if (code < 0) {
330 pdfi_close_file(ctx, XRefStrm);
331 pdfi_countdown(ctx->xref_table);
332 ctx->xref_table = NULL;
333 return code;
334 }
335 } else {
336 int64_t start, size;
337
338 if (code < 0) {
339 pdfi_close_file(ctx, XRefStrm);
340 pdfi_countdown(ctx->xref_table);
341 ctx->xref_table = NULL;
342 return code;
343 }
344
345 if (pdfi_array_size(a) & 1) {
346 pdfi_countdown(a);
347 pdfi_close_file(ctx, XRefStrm);
348 pdfi_countdown(ctx->xref_table);
349 ctx->xref_table = NULL;
350 return_error(gs_error_rangecheck);
351 }
352
353 for (i=0;i < pdfi_array_size(a);i+=2){
354 code = pdfi_array_get_int(ctx, a, (uint64_t)i, &start);
355 if (code < 0 || start < 0) {
356 pdfi_countdown(a);
357 pdfi_close_file(ctx, XRefStrm);
358 pdfi_countdown(ctx->xref_table);
359 ctx->xref_table = NULL;
360 return code;
361 }
362
363 code = pdfi_array_get_int(ctx, a, (uint64_t)i+1, &size);
364 if (code < 0) {
365 pdfi_countdown(a);
366 pdfi_close_file(ctx, XRefStrm);
367 pdfi_countdown(ctx->xref_table);
368 ctx->xref_table = NULL;
369 return code;
370 }
371
372 if (size < 1)
373 continue;
374
375 if (start + size >= ctx->xref_table->xref_size) {
376 code = resize_xref(ctx, start + size);
377 if (code < 0) {
378 pdfi_countdown(a);
379 pdfi_close_file(ctx, XRefStrm);
380 pdfi_countdown(ctx->xref_table);
381 ctx->xref_table = NULL;
382 return code;
383 }
384 }
385
386 code = read_xref_stream_entries(ctx, XRefStrm, start, start + size - 1, W);
387 if (code < 0) {
388 pdfi_countdown(a);
389 pdfi_close_file(ctx, XRefStrm);
390 pdfi_countdown(ctx->xref_table);
391 ctx->xref_table = NULL;
392 return code;
393 }
394 }
395 }
396 pdfi_countdown(a);
397
398 pdfi_close_file(ctx, XRefStrm);
399
400 code = pdfi_dict_get_int(ctx, sdict, "Prev", &num);
401 if (code == gs_error_undefined)
402 return 0;
403
404 if (code < 0)
405 return code;
406
407 if (num < 0 || num > ctx->main_stream_length)
408 return_error(gs_error_rangecheck);
409
410 if (pdfi_loop_detector_check_object(ctx, num) == true)
411 return_error(gs_error_circular_reference);
412 else {
413 code = pdfi_loop_detector_add_object(ctx, num);
414 if (code < 0)
415 return code;
416 }
417
418 if(ctx->args.pdfdebug)
419 outprintf(ctx->memory, "%% Reading /Prev xref\n");
420
421 pdfi_seek(ctx, s, num, SEEK_SET);
422
423 code = pdfi_read_bare_int(ctx, ctx->main_stream, &objnum);
424 if (code == 1) {
425 if (pdfi_check_xref_stream(ctx))
426 return pdfi_read_xref_stream_dict(ctx, s, objnum);
427 }
428
429 code = pdfi_read_bare_keyword(ctx, ctx->main_stream);
430 if (code < 0)
431 return code;
432 if (code == TOKEN_XREF) {
433 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_PREV_NOT_XREF_STREAM, "pdfi_process_xref_stream", NULL)) < 0) {
434 goto exit;
435 }
436 /* Read old-style xref table */
437 return(read_xref(ctx, ctx->main_stream));
438 }
439 exit:
440 return_error(gs_error_syntaxerror);
441 }
442
443 static int pdfi_check_xref_stream(pdf_context *ctx)
444 {
445 gs_offset_t offset;
446 int gen_num, code = 0;
447
448 offset = pdfi_unread_tell(ctx);
449
450 code = pdfi_read_bare_int(ctx, ctx->main_stream, &gen_num);
451 if (code <= 0) {
452 code = 0;
453 goto exit;
454 }
455
456 /* Try to read 'obj' */
457 code = pdfi_read_bare_keyword(ctx, ctx->main_stream);
458 if (code <= 0) {
459 code = 0;
460 goto exit;
461 }
462
463 /* Third element must be obj, or it's not a valid xref */
464 if (code != TOKEN_OBJ)
465 code = 0;
466 else
467 code = 1;
468
469 exit:
470 pdfi_seek(ctx, ctx->main_stream, offset, SEEK_SET);
471 return code;
472 }
473
474 static int pdfi_read_xref_stream_dict(pdf_context *ctx, pdf_c_stream *s, int obj_num)
475 {
476 int code;
477 int gen_num;
478
479 if (ctx->args.pdfdebug)
480 outprintf(ctx->memory, "\n%% Reading PDF 1.5+ xref stream\n");
481
482 /* We have the obj_num. Lets try for obj_num gen obj as a XRef stream */
483 code = pdfi_read_bare_int(ctx, ctx->main_stream, &gen_num);
484 if (code <= 0) {
485 if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BADXREFSTREAM, "pdfi_read_xref_stream_dict", "")) < 0) {
486 return code;
487 }
488 return(pdfi_repair_file(ctx));
489 }
490
491 /* Try to read 'obj' */
492 code = pdfi_read_bare_keyword(ctx, ctx->main_stream);
493 if (code < 0)
494 return code;
495 if (code == 0)
496 return_error(gs_error_syntaxerror);
497
498 /* Third element must be obj, or it's not a valid xref */
499 if (code != TOKEN_OBJ) {
500 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, E_PDF_BAD_XREFSTMOFFSET, "pdfi_read_xref_stream_dict", "")) < 0) {
501 return code;
502 }
503 return(pdfi_repair_file(ctx));
504 }
505
506 do {
507 code = pdfi_read_token(ctx, ctx->main_stream, obj_num, gen_num);
508 if (code <= 0) {
509 if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BADXREFSTREAM, "pdfi_read_xref_stream_dict", NULL)) < 0) {
510 return code;
511 }
512 return pdfi_repair_file(ctx);
513 }
514
515 if (pdfi_count_stack(ctx) >= 2 && pdfi_type_of(ctx->stack_top[-1]) == PDF_FAST_KEYWORD) {
516 uintptr_t keyword = (uintptr_t)ctx->stack_top[-1];
517 if (keyword == TOKEN_STREAM) {
518 pdf_dict *dict;
519 pdf_stream *sdict = NULL;
520 int64_t Length;
521
522 /* Remove the 'stream' token from the stack, should leave a dictionary object on the stack */
523 pdfi_pop(ctx, 1);
524 if (pdfi_type_of(ctx->stack_top[-1]) != PDF_DICT) {
525 if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BADXREFSTREAM, "pdfi_read_xref_stream_dict", NULL)) < 0) {
526 return code;
527 }
528 return pdfi_repair_file(ctx);
529 }
530 dict = (pdf_dict *)ctx->stack_top[-1];
531
532 /* Convert the dict into a stream (sdict comes back with at least one ref) */
533 code = pdfi_obj_dict_to_stream(ctx, dict, &sdict, true);
534 /* Pop off the dict */
535 pdfi_pop(ctx, 1);
536 if (code < 0) {
537 if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BADXREFSTREAM, "pdfi_read_xref_stream_dict", NULL)) < 0) {
538 return code;
539 }
540 /* TODO: should I return code instead of trying to repair?
541 * Normally the above routine should not fail so something is
542 * probably seriously fubar.
543 */
544 return pdfi_repair_file(ctx);
545 }
546 dict = NULL;
547
548 /* Init the stuff for the stream */
549 sdict->stream_offset = pdfi_unread_tell(ctx);
550 sdict->object_num = obj_num;
551 sdict->generation_num = gen_num;
552
553 code = pdfi_dict_get_int(ctx, sdict->stream_dict, "Length", &Length);
554 if (code < 0) {
555 /* TODO: Not positive this will actually have a length -- just use 0 */
556 (void)pdfi_set_error_var(ctx, 0, NULL, E_PDF_BADSTREAM, "pdfi_read_xref_stream_dict", "Xref Stream object %u missing mandatory keyword /Length\n", obj_num);
557 code = 0;
558 Length = 0;
559 }
560 sdict->Length = Length;
561 sdict->length_valid = true;
562
563 code = pdfi_process_xref_stream(ctx, sdict, ctx->main_stream);
564 pdfi_countdown(sdict);
565 if (code < 0) {
566 pdfi_set_error(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_PREV_NOT_XREF_STREAM, "pdfi_read_xref_stream_dict", NULL);
567 return code;
568 }
569 break;
570 } else if (keyword == TOKEN_ENDOBJ) {
571 /* Something went wrong, this is not a stream dictionary */
572 if ((code = pdfi_set_error_var(ctx, 0, NULL, E_PDF_BADSTREAM, "pdfi_read_xref_stream_dict", "Xref Stream object %u missing mandatory keyword /Length\n", obj_num)) < 0) {
573 return code;
574 }
575 return(pdfi_repair_file(ctx));
576 }
577 }
578 } while(1);
579 return 0;
580 }
581
582 static int skip_to_digit(pdf_context *ctx, pdf_c_stream *s, unsigned int limit)
583 {
584 int c, read = 0;
585
586 do {
587 c = pdfi_read_byte(ctx, s);
588 if (c < 0)
589 return_error(gs_error_ioerror);
590 if (c >= '0' && c <= '9') {
591 pdfi_unread_byte(ctx, s, (byte)c);
592 return read;
593 }
594 read++;
595 } while (read < limit);
596
597 return read;
598 }
599
600 static int read_digits(pdf_context *ctx, pdf_c_stream *s, byte *Buffer, int limit)
601 {
602 int c, read = 0;
603
604 /* Since the "limit" is a value calculated by the caller,
605 it's easier to check it in one place (here) than before
606 every call.
607 */
608 if (limit <= 0)
609 return_error(gs_error_syntaxerror);
610
611 /* We assume that Buffer always has limit+1 bytes available, so we can
612 * safely terminate it. */
613
614 do {
615 c = pdfi_read_byte(ctx, s);
616 if (c < 0)
617 return_error(gs_error_ioerror);
618 if (c < '0' || c > '9') {
619 pdfi_unread_byte(ctx, s, c);
620 break;
621 }
622 *Buffer++ = (byte)c;
623 read++;
624 } while (read < limit);
625 *Buffer = 0;
626
627 return read;
628 }
629
630
631 static int read_xref_entry_slow(pdf_context *ctx, pdf_c_stream *s, gs_offset_t *offset, uint32_t *generation_num, unsigned char *free)
632 {
633 byte Buffer[20];
634 int c, code, read = 0;
635
636 /* First off, find a number. If we don't find one, and read 20 bytes, throw an error */
637 code = skip_to_digit(ctx, s, 20);
638 if (code < 0)
639 return code;
640 read += code;
641
642 /* Now read a number */
643 code = read_digits(ctx, s, (byte *)&Buffer, (read > 10 ? 20 - read : 10));
644 if (code < 0)
645 return code;
646 read += code;
647
648 *offset = atol((const char *)Buffer);
649
650 /* find next number */
651 code = skip_to_digit(ctx, s, 20 - read);
652 if (code < 0)
653 return code;
654 read += code;
655
656 /* and read it */
657 code = read_digits(ctx, s, (byte *)&Buffer, (read > 15 ? 20 - read : 5));
658 if (code < 0)
659 return code;
660 read += code;
661
662 *generation_num = atol((const char *)Buffer);
663
664 do {
665 c = pdfi_read_byte(ctx, s);
666 if (c < 0)
667 return_error(gs_error_ioerror);
668 read ++;
669 if (c == 0x09 || c == 0x20)
670 continue;
671 if (c == 'n' || c == 'f') {
672 *free = (unsigned char)c;
673 break;
674 } else {
675 return_error(gs_error_syntaxerror);
676 }
677 } while (read < 20);
678 if (read >= 20)
679 return_error(gs_error_syntaxerror);
680
681 do {
682 c = pdfi_read_byte(ctx, s);
683 if (c < 0)
684 return_error(gs_error_syntaxerror);
685 read++;
686 if (c == 0x20 || c == 0x09 || c == 0x0d || c == 0x0a)
687 continue;
688 } while (read < 20);
689 return 0;
690 }
691
692 static int write_offset(byte *B, gs_offset_t o, unsigned int g, unsigned char free)
693 {
694 byte b[20], *ptr = B;
695 int index = 0;
696
697 gs_snprintf((char *)b, sizeof(b), "%"PRIdOFFSET"", o);
698 if (strlen((const char *)b) > 10)
699 return_error(gs_error_rangecheck);
700 for(index=0;index < 10 - strlen((const char *)b); index++) {
701 *ptr++ = 0x30;
702 }
703 memcpy(ptr, b, strlen((const char *)b));
704 ptr += strlen((const char *)b);
705 *ptr++ = 0x20;
706
707 gs_snprintf((char *)b, sizeof(b), "%d", g);
708 if (strlen((const char *)b) > 5)
709 return_error(gs_error_rangecheck);
710 for(index=0;index < 5 - strlen((const char *)b);index++) {
711 *ptr++ = 0x30;
712 }
713 memcpy(ptr, b, strlen((const char *)b));
714 ptr += strlen((const char *)b);
715 *ptr++ = 0x20;
716 *ptr++ = free;
717 *ptr++ = 0x20;
718 *ptr++ = 0x0d;
719 return 0;
720 }
721
722 static int read_xref_section(pdf_context *ctx, pdf_c_stream *s, uint64_t *section_start, uint64_t *section_size)
723 {
724 int code = 0, i, j;
725 int start = 0;
726 int size = 0;
727 int64_t bytes = 0;
728 char Buffer[21];
729
730 *section_start = *section_size = 0;
731
732 if (ctx->args.pdfdebug)
733 outprintf(ctx->memory, "\n%% Reading xref section\n");
734
735 code = pdfi_read_bare_int(ctx, ctx->main_stream, &start);
736 if (code < 0) {
737 /* Not an int, might be a keyword */
738 code = pdfi_read_bare_keyword(ctx, ctx->main_stream);
739 if (code < 0)
740 return code;
741
742 if (code != TOKEN_TRAILER) {
743 /* element is not an integer, and not a keyword - not a valid xref */
744 return_error(gs_error_typecheck);
745 }
746 return 1;
747 }
748
749 if (start < 0)
750 return_error(gs_error_rangecheck);
751
752 *section_start = start;
753
754 code = pdfi_read_bare_int(ctx, ctx->main_stream, &size);
755 if (code < 0)
756 return code;
757 if (code == 0)
758 return_error(gs_error_syntaxerror);
759
760 /* Zero sized xref sections are valid; see the file attached to
761 * bug 704947 for an example. */
762 if (size < 0)
763 return_error(gs_error_rangecheck);
764
765 *section_size = size;
766
767 if (ctx->args.pdfdebug)
768 outprintf(ctx->memory, "\n%% Section starts at %d and has %d entries\n", (unsigned int) start, (unsigned int)size);
769
770 if (size > 0) {
771 if (ctx->xref_table == NULL) {
772 ctx->xref_table = (xref_table_t *)gs_alloc_bytes(ctx->memory, sizeof(xref_table_t), "read_xref_stream allocate xref table");
773 if (ctx->xref_table == NULL)
774 return_error(gs_error_VMerror);
775 memset(ctx->xref_table, 0x00, sizeof(xref_table_t));
776
777 ctx->xref_table->xref = (xref_entry *)gs_alloc_bytes(ctx->memory, ((size_t)start + (size_t)size) * (size_t)sizeof(xref_entry), "read_xref_stream allocate xref table entries");
778 if (ctx->xref_table->xref == NULL){
779 gs_free_object(ctx->memory, ctx->xref_table, "free xref table on error allocating entries");
780 ctx->xref_table = NULL;
781 return_error(gs_error_VMerror);
782 }
783 #if REFCNT_DEBUG
784 ctx->xref_table->UID = ctx->ref_UID++;
785 outprintf(ctx->memory, "Allocated xref table with UID %"PRIi64"\n", ctx->xref_table->UID);
786 #endif
787
788 memset(ctx->xref_table->xref, 0x00, (start + size) * sizeof(xref_entry));
789 ctx->xref_table->ctx = ctx;
790 ctx->xref_table->type = PDF_XREF_TABLE;
791 ctx->xref_table->xref_size = start + size;
792 pdfi_countup(ctx->xref_table);
793 } else {
794 if (start + size > ctx->xref_table->xref_size) {
795 code = resize_xref(ctx, start + size);
796 if (code < 0)
797 return code;
798 }
799 }
800 }
801
802 pdfi_skip_white(ctx, s);
803 for (i=0;i< size;i++){
804 xref_entry *entry = &ctx->xref_table->xref[i + start];
805 unsigned char free;
806 gs_offset_t off;
807 unsigned int gen;
808
809 bytes = pdfi_read_bytes(ctx, (byte *)Buffer, 1, 20, s);
810 if (bytes < 20)
811 return_error(gs_error_ioerror);
812 j = 19;
813 if ((Buffer[19] != 0x0a && Buffer[19] != 0x0d) || (Buffer[18] != 0x0d && Buffer[18] != 0x0a && Buffer[18] != 0x20))
814 pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_XREF_ENTRY_SIZE, "read_xref_section", NULL);
815 while (Buffer[j] != 0x0D && Buffer[j] != 0x0A) {
816 pdfi_unread_byte(ctx, s, (byte)Buffer[j]);
817 if (--j < 0) {
818 pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_XREF_ENTRY_NO_EOL, "read_xref_section", NULL);
819 outprintf(ctx->memory, "Invalid xref entry, line terminator missing.\n");
820 code = read_xref_entry_slow(ctx, s, &off, &gen, &free);
821 if (code < 0)
822 return code;
823 code = write_offset((byte *)Buffer, off, gen, free);
824 if (code < 0)
825 return code;
826 j = 19;
827 break;
828 }
829 }
830 Buffer[j] = 0x00;
831 if (entry->object_num != 0)
832 continue;
833
834 if (sscanf(Buffer, "%"PRIdOFFSET" %d %c", &entry->u.uncompressed.offset, &entry->u.uncompressed.generation_num, &free) != 3) {
835 pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_XREF_ENTRY_FORMAT, "read_xref_section", NULL);
836 outprintf(ctx->memory, "Invalid xref entry, incorrect format.\n");
837 pdfi_unread(ctx, s, (byte *)Buffer, 20);
838 code = read_xref_entry_slow(ctx, s, &off, &gen, &free);
839 if (code < 0)
840 return code;
841 code = write_offset((byte *)Buffer, off, gen, free);
842 if (code < 0)
843 return code;
844 }
845
846 entry->compressed = false;
847 entry->object_num = i + start;
848 if (free == 'f')
849 entry->free = true;
850 if(free == 'n')
851 entry->free = false;
852 if (entry->object_num == 0) {
853 if (!entry->free) {
854 pdfi_set_warning(ctx, 0, NULL, W_PDF_XREF_OBJECT0_NOT_FREE, "read_xref_section", NULL);
855 }
856 }
857 }
858
859 return 0;
860 }
861
862 static int read_xref(pdf_context *ctx, pdf_c_stream *s)
863 {
864 int code = 0;
865 pdf_dict *d = NULL;
866 uint64_t max_obj = 0;
867 int64_t num, XRefStm = 0;
868 int obj_num;
869 bool known = false;
870
871 if (ctx->repaired)
872 return 0;
873
874 do {
875 uint64_t section_start, section_size;
876
877 code = read_xref_section(ctx, s, &section_start, &section_size);
878 if (code < 0)
879 return code;
880
881 if (section_size > 0 && section_start + section_size - 1 > max_obj)
882 max_obj = section_start + section_size - 1;
883
884 /* code == 1 => read_xref_section ended with a trailer. */
885 } while (code != 1);
886
887 code = pdfi_read_dict(ctx, ctx->main_stream, 0, 0);
888 if (code < 0)
889 return code;
890
891 d = (pdf_dict *)ctx->stack_top[-1];
892 if (pdfi_type_of(d) != PDF_DICT) {
893 pdfi_pop(ctx, 1);
894 return_error(gs_error_typecheck);
895 }
896 pdfi_countup(d);
897 pdfi_pop(ctx, 1);
898
899 /* We don't want to pollute the Trailer dictionary with any XRefStm key/value pairs
900 * which will happen when we do pdfi_merge_dicts(). So we get any XRefStm here and
901 * if there was one, remove it from the dictionary before we merge with the
902 * primary trailer.
903 */
904 code = pdfi_dict_get_int(ctx, d, "XRefStm", &XRefStm);
905 if (code < 0 && code != gs_error_undefined)
906 goto error;
907
908 if (code == 0) {
909 code = pdfi_dict_delete(ctx, d, "XRefStm");
910 if (code < 0)
911 goto error;
912 }
913
914 if (ctx->Trailer == NULL) {
915 ctx->Trailer = d;
916 pdfi_countup(d);
917 } else {
918 code = pdfi_merge_dicts(ctx, ctx->Trailer, d);
919 if (code < 0) {
920 if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BADXREF, "read_xref", "")) < 0) {
921 return code;
922 }
923 }
924 }
925
926 /* Check if the highest subsection + size exceeds the /Size in the
927 * trailer dictionary and set a warning flag if it does
928 */
929 code = pdfi_dict_get_int(ctx, d, "Size", &num);
930 if (code < 0)
931 goto error;
932
933 if (max_obj >= num)
934 pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_XREF_SIZE, "read_xref", NULL);
935
936 /* Check if this is a modified file and has any
937 * previous xref entries.
938 */
939 code = pdfi_dict_known(ctx, d, "Prev", &known);
940 if (known) {
941 code = pdfi_dict_get_int(ctx, d, "Prev", &num);
942 if (code < 0)
943 goto error;
944
945 if (num < 0 || num > ctx->main_stream_length) {
946 code = gs_note_error(gs_error_rangecheck);
947 goto error;
948 }
949
950 if (pdfi_loop_detector_check_object(ctx, num) == true) {
951 code = gs_note_error(gs_error_circular_reference);
952 goto error;
953 }
954 else {
955 code = pdfi_loop_detector_add_object(ctx, num);
956 if (code < 0)
957 goto error;
958 }
959
960 code = pdfi_seek(ctx, s, num, SEEK_SET);
961 if (code < 0)
962 goto error;
963
964 if (!ctx->repaired) {
965 code = pdfi_read_token(ctx, ctx->main_stream, 0, 0);
966 if (code < 0)
967 goto error;
968
969 if (code == 0) {
970 code = gs_note_error(gs_error_syntaxerror);
971 goto error;
972 }
973 } else {
974 code = 0;
975 goto error;
976 }
977
978 if ((intptr_t)(ctx->stack_top[-1]) == (intptr_t)TOKEN_XREF) {
979 /* Read old-style xref table */
980 pdfi_pop(ctx, 1);
981 code = read_xref(ctx, ctx->main_stream);
982 if (code < 0)
983 goto error;
984 } else {
985 pdfi_pop(ctx, 1);
986 code = gs_note_error(gs_error_typecheck);
987 goto error;
988 }
989 }
990
991 /* Now check if this is a hybrid file. */
992 if (XRefStm != 0) {
993 ctx->is_hybrid = true;
994
995 if (ctx->args.pdfdebug)
996 outprintf(ctx->memory, "%% File is a hybrid, containing xref table and xref stream. Reading the stream.\n");
997
998
999 if (pdfi_loop_detector_check_object(ctx, XRefStm) == true) {
1000 code = gs_note_error(gs_error_circular_reference);
1001 goto error;
1002 }
1003 else {
1004 code = pdfi_loop_detector_add_object(ctx, XRefStm);
1005 if (code < 0)
1006 goto error;
1007 }
1008
1009 code = pdfi_loop_detector_mark(ctx);
1010 if (code < 0)
1011 goto error;
1012
1013 /* Because of the way the code works when we read a file which is a pure
1014 * xref stream file, we need to read the first integer of 'x y obj'
1015 * because the xref stream decoding code expects that to be on the stack.
1016 */
1017 pdfi_seek(ctx, s, XRefStm, SEEK_SET);
1018
1019 code = pdfi_read_bare_int(ctx, ctx->main_stream, &obj_num);
1020 if (code < 0) {
1021 pdfi_set_error(ctx, 0, NULL, E_PDF_BADXREFSTREAM, "read_xref", "");
1022 pdfi_loop_detector_cleartomark(ctx);
1023 goto error;
1024 }
1025
1026 code = pdfi_read_xref_stream_dict(ctx, ctx->main_stream, obj_num);
1027 /* We could just fall through to the exit here, but choose not to in order to avoid possible mistakes in future */
1028 if (code < 0) {
1029 pdfi_loop_detector_cleartomark(ctx);
1030 goto error;
1031 }
1032
1033 pdfi_loop_detector_cleartomark(ctx);
1034 } else
1035 code = 0;
1036
1037 error:
1038 pdfi_countdown(d);
1039 return code;
1040 }
1041
1042 int pdfi_read_xref(pdf_context *ctx)
1043 {
1044 int code = 0;
1045 int obj_num;
1046
1047 code = pdfi_loop_detector_mark(ctx);
1048 if (code < 0)
1049 return code;
1050
1051 if (ctx->startxref == 0)
1052 goto repair;
1053
1054 code = pdfi_loop_detector_add_object(ctx, ctx->startxref);
1055 if (code < 0)
1056 goto exit;
1057
1058 if (ctx->args.pdfdebug)
1059 outprintf(ctx->memory, "%% Trying to read 'xref' token for xref table, or 'int int obj' for an xref stream\n");
1060
1061 if (ctx->startxref > ctx->main_stream_length - 5) {
1062 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, E_PDF_BADSTARTXREF, "pdfi_read_xref", (char *)"startxref offset is beyond end of file")) < 0)
1063 goto exit;
1064
1065 goto repair;
1066 }
1067 if (ctx->startxref < 0) {
1068 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, E_PDF_BADSTARTXREF, "pdfi_read_xref", (char *)"startxref offset is before start of file")) < 0)
1069 goto exit;
1070
1071 goto repair;
1072 }
1073
1074 /* Read the xref(s) */
1075 pdfi_seek(ctx, ctx->main_stream, ctx->startxref, SEEK_SET);
1076
1077 /* If it starts with an int, it's an xref stream dict */
1078 code = pdfi_read_bare_int(ctx, ctx->main_stream, &obj_num);
1079 if (code == 1) {
1080 if (pdfi_check_xref_stream(ctx)) {
1081 code = pdfi_read_xref_stream_dict(ctx, ctx->main_stream, obj_num);
1082 if (code < 0)
1083 goto repair;
1084 } else
1085 goto repair;
1086 } else {
1087 /* If not, it had better start 'xref', and be an old-style xref table */
1088 code = pdfi_read_bare_keyword(ctx, ctx->main_stream);
1089 if (code != TOKEN_XREF) {
1090 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_BADSTARTXREF, "pdfi_read_xref", (char *)"Failed to read any token at the startxref location")) < 0)
1091 goto exit;
1092
1093 goto repair;
1094 }
1095
1096 code = read_xref(ctx, ctx->main_stream);
1097 if (code < 0)
1098 goto repair;
1099 }
1100
1101 if(ctx->args.pdfdebug && ctx->xref_table) {
1102 int i, j;
1103 xref_entry *entry;
1104 char Buffer[32];
1105
1106 outprintf(ctx->memory, "\n%% Dumping xref table\n");
1107 for (i=0;i < ctx->xref_table->xref_size;i++) {
1108 entry = &ctx->xref_table->xref[i];
1109 if(entry->compressed) {
1110 outprintf(ctx->memory, "*");
1111 gs_snprintf(Buffer, sizeof(Buffer), "%"PRId64"", entry->object_num);
1112 j = 10 - strlen(Buffer);
1113 while(j--) {
1114 outprintf(ctx->memory, " ");
1115 }
1116 outprintf(ctx->memory, "%s ", Buffer);
1117
1118 gs_snprintf(Buffer, sizeof(Buffer), "%ld", entry->u.compressed.compressed_stream_num);
1119 j = 10 - strlen(Buffer);
1120 while(j--) {
1121 outprintf(ctx->memory, " ");
1122 }
1123 outprintf(ctx->memory, "%s ", Buffer);
1124
1125 gs_snprintf(Buffer, sizeof(Buffer), "%ld", entry->u.compressed.object_index);
1126 j = 10 - strlen(Buffer);
1127 while(j--) {
1128 outprintf(ctx->memory, " ");
1129 }
1130 outprintf(ctx->memory, "%s ", Buffer);
1131 }
1132 else {
1133 outprintf(ctx->memory, " ");
1134
1135 gs_snprintf(Buffer, sizeof(Buffer), "%ld", entry->object_num);
1136 j = 10 - strlen(Buffer);
1137 while(j--) {
1138 outprintf(ctx->memory, " ");
1139 }
1140 outprintf(ctx->memory, "%s ", Buffer);
1141
1142 gs_snprintf(Buffer, sizeof(Buffer), "%"PRIdOFFSET"", entry->u.uncompressed.offset);
1143 j = 10 - strlen(Buffer);
1144 while(j--) {
1145 outprintf(ctx->memory, " ");
1146 }
1147 outprintf(ctx->memory, "%s ", Buffer);
1148
1149 gs_snprintf(Buffer, sizeof(Buffer), "%ld", entry->u.uncompressed.generation_num);
1150 j = 10 - strlen(Buffer);
1151 while(j--) {
1152 outprintf(ctx->memory, " ");
1153 }
1154 outprintf(ctx->memory, "%s ", Buffer);
1155 }
1156 if (entry->free)
1157 outprintf(ctx->memory, "f\n");
1158 else
1159 outprintf(ctx->memory, "n\n");
1160 }
1161 }
1162 if (ctx->args.pdfdebug)
1163 outprintf(ctx->memory, "\n");
1164
1165 exit:
1166 (void)pdfi_loop_detector_cleartomark(ctx);
1167
1168 if (code < 0)
1169 return code;
1170
1171 return 0;
1172
1173 repair:
1174 (void)pdfi_loop_detector_cleartomark(ctx);
1175 if (!ctx->repaired && !ctx->args.pdfstoponerror)
1176 return(pdfi_repair_file(ctx));
1177 return 0;
1178 }