Debian Codegraph @

pdf/pdf_deref.c

raw
1 /* Copyright (C) 2020-2026 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 /* Functions to deal with dereferencing indirect objects
17 * for the PDF interpreter. In here we also keep the code
18 * for dealing with the object cache, because the dereferencing
19 * functions are currently the only place that deals with it.
20 */
21
22 #include "pdf_int.h"
23 #include "pdf_stack.h"
24 #include "pdf_loop_detect.h"
25 #include "strmio.h"
26 #include "stream.h"
27 #include "pdf_file.h"
28 #include "pdf_misc.h"
29 #include "pdf_dict.h"
30 #include "pdf_array.h"
31 #include "pdf_deref.h"
32 #include "pdf_repair.h"
33
34 /* Start with the object caching functions */
35 /* Disable object caching (for easier debugging with reference counting)
36 * by uncommenting the following line
37 */
38 /*#define DISABLE CACHE*/
39
40 /* given an object, create a cache entry for it. If we have too many entries
41 * then delete the leat-recently-used cache entry. Make the new entry be the
42 * most-recently-used entry. The actual entries are attached to the xref table
43 * (as well as being a double-linked list), because we detect an existing
44 * cache entry by seeing that the xref table for the object number has a non-NULL
45 * 'cache' member.
46 * So we need to update the xref as well if we add or delete cache entries.
47 */
48 static int pdfi_add_to_cache(pdf_context *ctx, pdf_obj *o)
49 {
50 #ifndef DISABLE_CACHE
51 pdf_obj_cache_entry *entry;
52
53 if (o < PDF_TOKEN_AS_OBJ(TOKEN__LAST_KEY))
54 return 0;
55
56 if (o->object_num >= ctx->xref_table->xref_size)
57 return_error(gs_error_rangecheck);
58
59 if (ctx->xref_table->xref[o->object_num].cache != NULL) {
60 #if DEBUG_CACHE
61 outprintf(ctx->memory, "Attempting to add object %d to cache when the object is already cached!\n", o->object_num);
62 #endif
63 return_error(gs_error_unknownerror);
64 }
65
66 #if DEBUG_CACHE
67 dbgmprintf1(ctx->memory, "Adding object %d\n", o->object_num);
68 #endif
69 if (ctx->cache_entries == ctx->args.PDFCacheSize)
70 {
71 #if DEBUG_CACHE
72 dbgmprintf(ctx->memory, "Cache full, evicting LRU\n");
73 #endif
74 if (ctx->cache_LRU) {
75 entry = ctx->cache_LRU;
76 #if DEBUG_CACHE
77 dbgmprintf1(ctx->memory, "Evicting %d\n", entry->o->object_num);
78 #endif
79 ctx->cache_LRU = entry->next;
80 if (entry->next)
81 ((pdf_obj_cache_entry *)entry->next)->previous = NULL;
82 ctx->xref_table->xref[entry->o->object_num].cache = NULL;
83 pdfi_countdown(entry->o);
84 ctx->cache_entries--;
85 gs_free_object(ctx->memory, entry, "pdfi_add_to_cache, free LRU");
86 } else
87 return_error(gs_error_unknownerror);
88 }
89 entry = (pdf_obj_cache_entry *)gs_alloc_bytes(ctx->memory, sizeof(pdf_obj_cache_entry), "pdfi_add_to_cache");
90 if (entry == NULL)
91 return_error(gs_error_VMerror);
92
93 memset(entry, 0x00, sizeof(pdf_obj_cache_entry));
94
95 entry->o = o;
96 pdfi_countup(o);
97 if (ctx->cache_MRU) {
98 entry->previous = ctx->cache_MRU;
99 ctx->cache_MRU->next = entry;
100 }
101 ctx->cache_MRU = entry;
102 if (ctx->cache_LRU == NULL)
103 ctx->cache_LRU = entry;
104
105 ctx->cache_entries++;
106 ctx->xref_table->xref[o->object_num].cache = entry;
107 #endif
108 return 0;
109 }
110
111 /* Given an existing cache entry, promote it to be the most-recently-used
112 * cache entry.
113 */
114 static void pdfi_promote_cache_entry(pdf_context *ctx, pdf_obj_cache_entry *cache_entry)
115 {
116 #ifndef DISABLE_CACHE
117 if (ctx->cache_MRU && cache_entry != ctx->cache_MRU) {
118 if ((pdf_obj_cache_entry *)cache_entry->next != NULL)
119 ((pdf_obj_cache_entry *)cache_entry->next)->previous = cache_entry->previous;
120 if ((pdf_obj_cache_entry *)cache_entry->previous != NULL)
121 ((pdf_obj_cache_entry *)cache_entry->previous)->next = cache_entry->next;
122 else {
123 /* the existing entry is the current least recently used, we need to make the 'next'
124 * cache entry into the LRU.
125 */
126 ctx->cache_LRU = cache_entry->next;
127 }
128 cache_entry->next = NULL;
129 cache_entry->previous = ctx->cache_MRU;
130 ctx->cache_MRU->next = cache_entry;
131 ctx->cache_MRU = cache_entry;
132 }
133 #endif
134 return;
135 }
136
137 int pdfi_cache_object(pdf_context *ctx, pdf_obj *o)
138 {
139 if (o->object_num == 0)
140 return 0;
141 if (ctx->xref_table->xref[o->object_num].cache == NULL)
142 return pdfi_add_to_cache(ctx, o);
143 else
144 pdfi_promote_cache_entry(ctx, ctx->xref_table->xref[o->object_num].cache);
145 return 0;
146 }
147
148 /* This one's a bit of an oddity, its used for fonts. When we build a PDF font object
149 * we want the object cache to reference *that* object, not the dictionary which was
150 * read out of the PDF file, so this allows us to replace the font dictionary in the
151 * cache with the actual font object, so that later dereferences will get this font
152 * object.
153 */
154 int replace_cache_entry(pdf_context *ctx, pdf_obj *o)
155 {
156 #ifndef DISABLE_CACHE
157 xref_entry *entry;
158 pdf_obj_cache_entry *cache_entry;
159 pdf_obj *old_cached_obj = NULL;
160
161 /* Limited error checking here, we assume that things like the
162 * validity of the object (eg not a free oobject) have already been handled.
163 */
164
165 entry = &ctx->xref_table->xref[o->object_num];
166 cache_entry = entry->cache;
167
168 if (cache_entry == NULL) {
169 return(pdfi_add_to_cache(ctx, o));
170 } else {
171 /* NOTE: We grab the object without decrementing, to avoid triggering
172 * a warning message for freeing an object that's in the cache
173 */
174 if (cache_entry->o != NULL)
175 old_cached_obj = cache_entry->o;
176
177 /* Put new entry in the cache */
178 cache_entry->o = o;
179 pdfi_countup(o);
180 pdfi_promote_cache_entry(ctx, cache_entry);
181
182 /* Now decrement the old cache entry, if any */
183 pdfi_countdown(old_cached_obj);
184 }
185 #endif
186 return 0;
187 }
188
189 /* Now the dereferencing functions */
190
191 /*
192 * Technically we can accept a stream other than the main PDF file stream here. This is
193 * really for the case of compressed objects where we read tokens from the compressed
194 * stream, but it also (with some judicious tinkering) allows us to layer a SubFileDecode
195 * on top of the main file stream, which may be useful. Note that this cannot work with
196 * objects in compressed object streams! They should always pass a value of 0 for the stream_offset.
197 * The stream_offset is the offset from the start of the underlying uncompressed PDF file of
198 * the stream we are using. See the comments below when keyword is PDF_STREAM.
199 */
200
201 /* Determine if a PDF object is in a compressed ObjStm. Returns < 0
202 * for an error, 0 if it is not in a compressed ObjStm and 1 if it is.
203 * Currently errors are inmpossible. This is only used by the decryption code
204 * to determine if a string is in a compressed object stream, if it is then
205 * it can't be used for decryption.
206 */
207 int is_compressed_object(pdf_context *ctx, uint32_t obj, uint32_t gen)
208 {
209 xref_entry *entry;
210
211 /* Can't possibly be a compressed object before we have finished reading
212 * the xref.
213 */
214 if (ctx->xref_table == NULL)
215 return 0;
216
217 entry = &ctx->xref_table->xref[obj];
218
219 if (entry->compressed)
220 return 1;
221
222 return 0;
223 }
224
225 /* We should never read a 'stream' keyword from a compressed object stream
226 * so this case should never end up here.
227 */
228 static int pdfi_read_stream_object(pdf_context *ctx, pdf_c_stream *s, gs_offset_t stream_offset,
229 uint32_t objnum, uint32_t gen)
230 {
231 int code = 0;
232 int64_t i;
233 pdf_dict *dict = NULL;
234 gs_offset_t offset;
235 pdf_stream *stream_obj = NULL;
236
237 /* Strange code time....
238 * If we are using a stream which is *not* the PDF uncompressed main file stream
239 * then doing stell on it will only tell us how many bytes have been read from
240 * that stream, it won't tell us the underlying file position. So we add on the
241 * 'unread' bytes, *and* we add on the position of the start of the stream in
242 * the actual main file. This is all done so that we can check the /Length
243 * of the object. Note that this will *only* work for regular objects it can
244 * not be used for compressed object streams, but those don't need checking anyway
245 * they have a different mechanism altogether and should never get here.
246 */
247 if (s != ctx->main_stream) {
248 offset = stell(s->s) - s->unread_size + stream_offset;
249 code = pdfi_seek(ctx, ctx->main_stream, offset, SEEK_SET);
250 if (code < 0)
251 return_error(gs_error_ioerror);
252 } else {
253 offset = stell(s->s) - s->unread_size;
254 }
255
256 if (pdfi_count_stack(ctx) < 1)
257 return_error(gs_error_stackunderflow);
258
259 dict = (pdf_dict *)ctx->stack_top[-1];
260
261 if (pdfi_type_of(dict) != PDF_DICT) {
262 pdfi_pop(ctx, 1);
263 return_error(gs_error_syntaxerror);
264 }
265
266 dict->indirect_num = dict->object_num = objnum;
267 dict->indirect_gen = dict->generation_num = gen;
268
269 /* Convert the dict into a stream */
270 code = pdfi_obj_dict_to_stream(ctx, dict, &stream_obj, true);
271 if (code < 0) {
272 pdfi_pop(ctx, 1);
273 return code;
274 }
275 /* Pop off the dict and push the stream */
276 pdfi_pop(ctx, 1);
277 dict = NULL;
278 pdfi_push(ctx, (pdf_obj *)stream_obj);
279
280 stream_obj->stream_dict->indirect_num = stream_obj->stream_dict->object_num = objnum;
281 stream_obj->stream_dict->indirect_gen = stream_obj->stream_dict->generation_num = gen;
282 stream_obj->stream_offset = offset;
283
284 /* Exceptional code. Normally we do not need to worry about detecting circular references
285 * when reading objects, because we do not dereference any indirect objects. However streams
286 * are a slight exception in that we do get the Length from the stream dictionay and if that
287 * is an indirect reference, then we dereference it.
288 * OSS-fuzz bug 43247 has a stream where the value associated iwht the /Length is an indirect
289 * reference to the same stream object, and leads to infinite recursion. So deal with that
290 * possibility here.
291 */
292 code = pdfi_loop_detector_mark(ctx);
293 if (code < 0) {
294 pdfi_countdown(stream_obj); /* get rid of extra ref */
295 return code;
296 }
297 if (pdfi_loop_detector_check_object(ctx, stream_obj->object_num)) {
298 pdfi_countdown(stream_obj); /* get rid of extra ref */
299 pdfi_loop_detector_cleartomark(ctx);
300 return_error(gs_error_circular_reference);
301 }
302
303 code = pdfi_loop_detector_add_object(ctx, stream_obj->object_num);
304 if (code < 0) {
305 pdfi_countdown(stream_obj); /* get rid of extra ref */
306 pdfi_loop_detector_cleartomark(ctx);
307 return code;
308 }
309
310 /* This code may be a performance overhead, it simply skips over the stream contents
311 * and checks that the stream ends with a 'endstream endobj' pair. We could add a
312 * 'go faster' flag for users who are certain their PDF files are well-formed. This
313 * could also allow us to skip all kinds of other checking.....
314 */
315
316 code = pdfi_dict_get_int(ctx, (pdf_dict *)stream_obj->stream_dict, "Length", &i);
317 if (code < 0) {
318 char extra_info[gp_file_name_sizeof];
319
320 (void)pdfi_loop_detector_cleartomark(ctx);
321 gs_snprintf(extra_info, sizeof(extra_info), "Stream object %u missing mandatory keyword /Length, unable to verify the stream length.\n", objnum);
322 code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_ioerror), NULL, E_PDF_BADSTREAM, "pdfi_read_stream_object", extra_info);
323 pdfi_countdown(stream_obj); /* get rid of extra ref */
324 return code;
325 }
326 code = pdfi_loop_detector_cleartomark(ctx);
327 if (code < 0) {
328 pdfi_countdown(stream_obj); /* get rid of extra ref */
329 return code;
330 }
331
332 if (i < 0 || (i + offset)> ctx->main_stream_length) {
333 char extra_info[gp_file_name_sizeof];
334
335 gs_snprintf(extra_info, sizeof(extra_info), "Stream object %u has /Length which, when added to offset of object, exceeds file size.\n", objnum);
336 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_ioerror), NULL, E_PDF_BADSTREAM, "pdfi_read_stream_object", extra_info))< 0) {
337 pdfi_pop(ctx, 1);
338 pdfi_countdown(stream_obj); /* get rid of extra ref */
339 return code;
340 }
341 } else {
342 code = pdfi_seek(ctx, ctx->main_stream, i, SEEK_CUR);
343 if (code < 0) {
344 pdfi_pop(ctx, 1);
345 pdfi_countdown(stream_obj); /* get rid of extra ref */
346 return code;
347 }
348
349 stream_obj->Length = 0;
350 stream_obj->length_valid = false;
351
352 code = pdfi_read_bare_keyword(ctx, ctx->main_stream);
353 if (code == 0) {
354 char extra_info[gp_file_name_sizeof];
355
356 gs_snprintf(extra_info, sizeof(extra_info), "Failed to find a valid object at end of stream object %u.\n", objnum);
357 pdfi_log_info(ctx, "pdfi_read_stream_object", extra_info);
358 /* It is possible for pdfi_read_token to clear the stack, losing the stream object. If that
359 * happens give up.
360 */
361 if (pdfi_count_stack(ctx) == 0) {
362 pdfi_countdown(stream_obj); /* get rid of extra ref */
363 return code;
364 }
365 } else if (code < 0) {
366 char extra_info[gp_file_name_sizeof];
367
368 gs_snprintf(extra_info, sizeof(extra_info), "Failed to find 'endstream' keyword at end of stream object %u.\n", objnum);
369 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MISSINGENDOBJ, "pdfi_read_stream_object", extra_info)) < 0) {
370 pdfi_countdown(stream_obj); /* get rid of extra ref */
371 return code;
372 }
373 } else if (code != TOKEN_ENDSTREAM) {
374 char extra_info[gp_file_name_sizeof];
375
376 gs_snprintf(extra_info, sizeof(extra_info), "Stream object %u has an incorrect /Length of %"PRIu64"\n", objnum, i);
377 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_BAD_LENGTH, "pdfi_read_stream_object", extra_info)) < 0) {
378 pdfi_countdown(stream_obj); /* get rid of extra ref */
379 return code;
380 }
381 } else {
382 /* Cache the Length in the stream object and mark it valid */
383 stream_obj->Length = i;
384 stream_obj->length_valid = true;
385 }
386 }
387
388 /* If we failed to find a valid object, or the object wasn't a keyword, or the
389 * keywrod wasn't 'endstream' then the Length is wrong. We need to have the correct
390 * Length for streams if we have encrypted files, because we must install a
391 * SubFileDecode filter with a Length (EODString is incompatible with AES encryption)
392 * Rather than mess about checking for encryption, we'll choose to just correctly
393 * calculate the Length of all streams. Although this takes time, it will only
394 * happen for files which are invalid.
395 */
396 if (stream_obj->length_valid != true) {
397 char Buffer[10];
398 unsigned int bytes, total = 0;
399 int c = 0;
400
401 code = pdfi_seek(ctx, ctx->main_stream, stream_obj->stream_offset, SEEK_SET);
402 if (code < 0) {
403 pdfi_countdown(stream_obj); /* get rid of extra ref */
404 pdfi_pop(ctx, 1);
405 return code;
406 }
407 memset(Buffer, 0x00, 10);
408 bytes = pdfi_read_bytes(ctx, (byte *)Buffer, 1, 9, ctx->main_stream);
409 if (bytes < 9) {
410 pdfi_countdown(stream_obj); /* get rid of extra ref */
411 return_error(gs_error_ioerror);
412 }
413
414 total = bytes;
415 do {
416 if (memcmp(Buffer, "endstream", 9) == 0) {
417 if (Buffer[9] != 0x00)
418 total--;
419 stream_obj->Length = total - 9;
420 stream_obj->length_valid = true;
421 break;
422 }
423 if (memcmp(Buffer, "endobj", 6) == 0) {
424 if (Buffer[9] != 0x00)
425 total--;
426 stream_obj->Length = total - 6;
427 stream_obj->length_valid = true;
428 break;
429 }
430 memmove(Buffer, Buffer+1, 9);
431 c = pdfi_read_byte(ctx, ctx->main_stream);
432 if (c < 0)
433 break;
434 Buffer[9] = (byte)c;
435 total++;
436 } while(1);
437 pdfi_countdown(stream_obj); /* get rid of extra ref */
438 if (c < 0)
439 return_error(gs_error_ioerror);
440 return 0;
441 }
442
443 code = pdfi_read_bare_keyword(ctx, ctx->main_stream);
444 if (code < 0) {
445 pdfi_countdown(stream_obj); /* get rid of extra ref */
446 if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_MISSINGENDOBJ, "pdfi_read_stream_object", "")) < 0) {
447 return code;
448 }
449 /* Something went wrong looking for endobj, but we found endstream, so assume
450 * for now that will suffice.
451 */
452 return 0;
453 }
454
455 if (code == 0) {
456 pdfi_countdown(stream_obj); /* get rid of extra ref */
457 return_error(gs_error_stackunderflow);
458 }
459
460 if (code != TOKEN_ENDOBJ) {
461 pdfi_countdown(stream_obj); /* get rid of extra ref */
462 code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_typecheck), NULL, E_PDF_MISSINGENDOBJ, "pdfi_read_stream_object", NULL);
463 /* Didn't find an endobj, but we have an endstream, so assume
464 * for now that will suffice
465 */
466 return code;
467 }
468 pdfi_countdown(stream_obj); /* get rid of extra ref */
469
470 return 0;
471 }
472
473 /* This reads an object *after* the x y obj keyword has been found. Its broken out
474 * separately for the benefit of the repair code when reading the dictionary following
475 * the 'trailer' keyword, which does not have a 'obj' keyword. Note that it also does
476 * not have an 'endobj', we rely on the error handling to take care of that for us.
477 */
478 int pdfi_read_bare_object(pdf_context *ctx, pdf_c_stream *s, gs_offset_t stream_offset, uint32_t objnum, uint32_t gen)
479 {
480 int code = 0, initial_depth = 0;
481 pdf_key keyword;
482 gs_offset_t saved_offset[3];
483 pdf_obj_type type;
484
485 initial_depth = pdfi_count_stack(ctx);
486 saved_offset[0] = saved_offset[1] = saved_offset[2] = 0;
487
488 code = pdfi_read_token(ctx, s, objnum, gen);
489 if (code < 0)
490 return code;
491
492 if (code == 0)
493 /* failed to read a token */
494 return_error(gs_error_syntaxerror);
495
496 if (pdfi_type_of(ctx->stack_top[-1]) == PDF_FAST_KEYWORD) {
497 keyword = (pdf_key)(uintptr_t)(ctx->stack_top[-1]);
498 if (keyword == TOKEN_ENDOBJ) {
499 ctx->stack_top[-1] = PDF_NULL_OBJ;
500 return 0;
501 }
502 }
503
504 do {
505 /* move all the saved offsets up by one */
506 saved_offset[0] = saved_offset[1];
507 saved_offset[1] = saved_offset[2];
508 saved_offset[2] = pdfi_unread_tell(ctx);
509
510 code = pdfi_read_token(ctx, s, objnum, gen);
511 if (code < 0) {
512 pdfi_clearstack(ctx);
513 return code;
514 }
515 if (s->eof)
516 return_error(gs_error_syntaxerror);
517 code = 0;
518 type = pdfi_type_of(ctx->stack_top[-1]);
519 if (type == PDF_KEYWORD)
520 goto missing_endobj;
521 } while (type != PDF_FAST_KEYWORD);
522
523 keyword = (pdf_key)(uintptr_t)(ctx->stack_top[-1]);
524 if (keyword == TOKEN_ENDOBJ) {
525 pdf_obj *o;
526
527 if (pdfi_count_stack(ctx) - initial_depth < 2) {
528 pdfi_clearstack(ctx);
529 return_error(gs_error_stackunderflow);
530 }
531
532 o = ctx->stack_top[-2];
533
534 pdfi_pop(ctx, 1);
535
536 if (o >= PDF_TOKEN_AS_OBJ(TOKEN__LAST_KEY)) {
537 o->indirect_num = o->object_num = objnum;
538 o->indirect_gen = o->generation_num = gen;
539 }
540 return code;
541 }
542 if (keyword == TOKEN_STREAM) {
543 pdfi_pop(ctx, 1);
544 return pdfi_read_stream_object(ctx, s, stream_offset, objnum, gen);
545 }
546 if (keyword == TOKEN_OBJ) {
547 pdf_obj *o;
548
549 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MISSINGENDOBJ, "pdfi_read_bare_object", NULL)) < 0) {
550 return code;
551 }
552
553 /* 4 for; the object we want, the object number, generation number and 'obj' keyword */
554 if (pdfi_count_stack(ctx) - initial_depth < 4)
555 return_error(gs_error_stackunderflow);
556
557 /* If we have that many objects, assume that we can throw away the x y obj and just use the remaining object */
558 o = ctx->stack_top[-4];
559
560 pdfi_pop(ctx, 3);
561
562 if (pdfi_type_of(o) != PDF_BOOL && pdfi_type_of(o) != PDF_NULL && pdfi_type_of(o) != PDF_FAST_KEYWORD) {
563 o->indirect_num = o->object_num = objnum;
564 o->indirect_gen = o->generation_num = gen;
565 }
566 if (saved_offset[0] > 0)
567 (void)pdfi_seek(ctx, s, saved_offset[0], SEEK_SET);
568 return 0;
569 }
570
571 missing_endobj:
572 /* Assume that any other keyword means a missing 'endobj' */
573 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MISSINGENDOBJ, "pdfi_read_xref_stream_dict", "")) == 0) {
574 pdf_obj *o;
575
576 pdfi_set_error(ctx, 0, NULL, E_PDF_MISSINGENDOBJ, "pdfi_read_bare_object", NULL);
577
578 if (pdfi_count_stack(ctx) - initial_depth < 2)
579 return_error(gs_error_stackunderflow);
580
581 o = ctx->stack_top[-2];
582
583 pdfi_pop(ctx, 1);
584
585 if (pdfi_type_of(o) != PDF_BOOL && pdfi_type_of(o) != PDF_NULL && pdfi_type_of(o) != PDF_FAST_KEYWORD) {
586 o->indirect_num = o->object_num = objnum;
587 o->indirect_gen = o->generation_num = gen;
588 }
589 return code;
590 }
591 pdfi_pop(ctx, 2);
592 return_error(gs_error_syntaxerror);
593 }
594
595 static int pdfi_read_object(pdf_context *ctx, pdf_c_stream *s, gs_offset_t stream_offset)
596 {
597 int code = 0;
598 int objnum = 0, gen = 0;
599
600 /* An object consists of 'num gen obj' followed by a token, follwed by an endobj
601 * A stream dictionary might have a 'stream' instead of an 'endobj', in which case we
602 * want to deal with it specially by getting the Length, jumping to the end and checking
603 * for an endobj. Or not, possibly, because it would be slow.
604 */
605 code = pdfi_read_bare_int(ctx, s, &objnum);
606 if (code < 0)
607 return code;
608 if (code == 0)
609 return_error(gs_error_syntaxerror);
610
611 code = pdfi_read_bare_int(ctx, s, &gen);
612 if (code < 0)
613 return code;
614 if (code == 0)
615 return_error(gs_error_syntaxerror);
616
617 code = pdfi_read_bare_keyword(ctx, s);
618 if (code < 0)
619 return code;
620 if (code == 0)
621 return gs_note_error(gs_error_ioerror);
622 if (code != TOKEN_OBJ) {
623 return_error(gs_error_syntaxerror);
624 }
625
626 return pdfi_read_bare_object(ctx, s, stream_offset, objnum, gen);
627 }
628
629 static int pdfi_deref_compressed(pdf_context *ctx, uint64_t obj, uint64_t gen, pdf_obj **object,
630 const xref_entry *entry, bool cache)
631 {
632 int code = 0;
633 xref_entry *compressed_entry;
634 pdf_c_stream *compressed_stream = NULL;
635 pdf_c_stream *SubFile_stream = NULL;
636 pdf_c_stream *Object_stream = NULL;
637 int i = 0, object_length = 0;
638 int64_t num_entries;
639 int found_object;
640 int64_t Length, First;
641 gs_offset_t offset = 0;
642 pdf_stream *compressed_object = NULL;
643 pdf_dict *compressed_sdict = NULL; /* alias */
644 pdf_name *Type = NULL;
645
646 if (entry->u.compressed.compressed_stream_num > ctx->xref_table->xref_size - 1)
647 return_error(gs_error_undefined);
648
649 compressed_entry = &ctx->xref_table->xref[entry->u.compressed.compressed_stream_num];
650
651 if (ctx->args.pdfdebug) {
652 outprintf(ctx->memory, "%% Reading compressed object (%"PRIi64" 0 obj)", obj);
653 outprintf(ctx->memory, " from ObjStm with object number %"PRIi64"\n", compressed_entry->object_num);
654 }
655
656 if (compressed_entry->cache == NULL) {
657 #if CACHE_STATISTICS
658 ctx->compressed_misses++;
659 #endif
660 code = pdfi_seek(ctx, ctx->main_stream, compressed_entry->u.uncompressed.offset, SEEK_SET);
661 if (code < 0)
662 goto exit;
663
664 code = pdfi_read_object(ctx, ctx->main_stream, 0);
665 if (code < 0)
666 goto exit;
667
668 if (pdfi_count_stack(ctx) < 1) {
669 code = gs_note_error(gs_error_stackunderflow);
670 goto exit;
671 }
672
673 if (pdfi_type_of(ctx->stack_top[-1]) != PDF_STREAM) {
674 pdfi_pop(ctx, 1);
675 code = gs_note_error(gs_error_typecheck);
676 goto exit;
677 }
678 if (ctx->stack_top[-1]->object_num != compressed_entry->object_num) {
679 pdfi_pop(ctx, 1);
680 /* Same error (undefined) as when we read an uncompressed object with the wrong number */
681 code = gs_note_error(gs_error_undefined);
682 goto exit;
683 }
684 compressed_object = (pdf_stream *)ctx->stack_top[-1];
685 pdfi_countup(compressed_object);
686 pdfi_pop(ctx, 1);
687 code = pdfi_add_to_cache(ctx, (pdf_obj *)compressed_object);
688 if (code < 0)
689 goto exit;
690 } else {
691 #if CACHE_STATISTICS
692 ctx->compressed_hits++;
693 #endif
694 compressed_object = (pdf_stream *)compressed_entry->cache->o;
695 pdfi_countup(compressed_object);
696 pdfi_promote_cache_entry(ctx, compressed_entry->cache);
697 }
698 code = pdfi_dict_from_obj(ctx, (pdf_obj *)compressed_object, &compressed_sdict);
699 if (code < 0)
700 return code;
701
702 if (ctx->loop_detection != NULL) {
703 code = pdfi_loop_detector_mark(ctx);
704 if (code < 0)
705 goto exit;
706 if (compressed_sdict->object_num != 0) {
707 if (pdfi_loop_detector_check_object(ctx, compressed_sdict->object_num)) {
708 code = gs_note_error(gs_error_circular_reference);
709 } else {
710 code = pdfi_loop_detector_add_object(ctx, compressed_sdict->object_num);
711 }
712 if (code < 0) {
713 (void)pdfi_loop_detector_cleartomark(ctx);
714 goto exit;
715 }
716 }
717 }
718 /* Check its an ObjStm ! */
719 code = pdfi_dict_get_type(ctx, compressed_sdict, "Type", PDF_NAME, (pdf_obj **)&Type);
720 if (code < 0) {
721 if (ctx->loop_detection != NULL)
722 (void)pdfi_loop_detector_cleartomark(ctx);
723 goto exit;
724 }
725
726 if (!pdfi_name_is(Type, "ObjStm")){
727 if (ctx->loop_detection != NULL)
728 (void)pdfi_loop_detector_cleartomark(ctx);
729 code = gs_note_error(gs_error_syntaxerror);
730 goto exit;
731 }
732
733 /* Need to check the /N entry to see if the object is actually in this stream! */
734 code = pdfi_dict_get_int(ctx, compressed_sdict, "N", &num_entries);
735 if (code < 0) {
736 if (ctx->loop_detection != NULL)
737 (void)pdfi_loop_detector_cleartomark(ctx);
738 goto exit;
739 }
740
741 if (num_entries < 0 || num_entries > ctx->xref_table->xref_size) {
742 if (ctx->loop_detection != NULL)
743 (void)pdfi_loop_detector_cleartomark(ctx);
744 code = gs_note_error(gs_error_rangecheck);
745 goto exit;
746 }
747
748 code = pdfi_dict_get_int(ctx, compressed_sdict, "Length", &Length);
749 if (code < 0) {
750 if (ctx->loop_detection != NULL)
751 (void)pdfi_loop_detector_cleartomark(ctx);
752 goto exit;
753 }
754
755 code = pdfi_dict_get_int(ctx, compressed_sdict, "First", &First);
756 if (code < 0) {
757 if (ctx->loop_detection != NULL)
758 (void)pdfi_loop_detector_cleartomark(ctx);
759 goto exit;
760 }
761
762 if (ctx->loop_detection != NULL)
763 (void)pdfi_loop_detector_cleartomark(ctx);
764
765 code = pdfi_seek(ctx, ctx->main_stream, pdfi_stream_offset(ctx, compressed_object), SEEK_SET);
766 if (code < 0)
767 goto exit;
768
769 code = pdfi_apply_SubFileDecode_filter(ctx, Length, NULL, ctx->main_stream, &SubFile_stream, false);
770 if (code < 0)
771 goto exit;
772
773 code = pdfi_filter(ctx, compressed_object, SubFile_stream, &compressed_stream, false);
774 if (code < 0)
775 goto exit;
776
777 for (i=0;i < num_entries;i++)
778 {
779 int new_offset;
780 code = pdfi_read_bare_int(ctx, compressed_stream, &found_object);
781 if (code < 0)
782 goto exit;
783 if (code == 0) {
784 code = gs_note_error(gs_error_syntaxerror);
785 goto exit;
786 }
787 code = pdfi_read_bare_int(ctx, compressed_stream, &new_offset);
788 if (code < 0)
789 goto exit;
790 if (code == 0) {
791 code = gs_note_error(gs_error_syntaxerror);
792 goto exit;
793 }
794 if (i == entry->u.compressed.object_index) {
795 if (found_object != obj) {
796 code = gs_note_error(gs_error_undefined);
797 goto exit;
798 }
799 offset = new_offset;
800 }
801 if (i == entry->u.compressed.object_index + 1)
802 object_length = new_offset - offset;
803 }
804
805 /* Bug #705259 - The first object need not lie immediately after the initial
806 * table of object numbers and offsets. The start of the first object is given
807 * by the value of First. We don't know how many bytes we consumed getting to
808 * the end of the table, unfortunately, so we close the stream, rewind the main
809 * stream back to the beginning of the ObjStm, and then read and discard 'First'
810 * bytes in order to get to the start of the first object. Then we read the
811 * number of bytes required to get from there to the start of the object we
812 * actually want.
813 * If this ever looks like it's causing performance problems we could read the
814 * initial table above manually instead of using the existing code, and track
815 * how many bytes we'd read, which would avoid us having to tear down and
816 * rebuild the stream.
817 */
818 if (compressed_stream) {
819 pdfi_close_file(ctx, compressed_stream);
820 compressed_stream = NULL;
821 }
822 if (SubFile_stream) {
823 pdfi_close_file(ctx, SubFile_stream);
824 SubFile_stream = NULL;
825 }
826
827 code = pdfi_seek(ctx, ctx->main_stream, pdfi_stream_offset(ctx, compressed_object), SEEK_SET);
828 if (code < 0)
829 goto exit;
830
831 /* We already dereferenced this above, so we don't need the loop detection checking here */
832 code = pdfi_dict_get_int(ctx, compressed_sdict, "Length", &Length);
833 if (code < 0)
834 goto exit;
835
836 code = pdfi_apply_SubFileDecode_filter(ctx, Length, NULL, ctx->main_stream, &SubFile_stream, false);
837 if (code < 0)
838 goto exit;
839
840 code = pdfi_filter(ctx, compressed_object, SubFile_stream, &compressed_stream, false);
841 if (code < 0)
842 goto exit;
843
844 for (i=0;i < First;i++)
845 {
846 int c = pdfi_read_byte(ctx, compressed_stream);
847 if (c < 0) {
848 code = gs_note_error(gs_error_ioerror);
849 goto exit;
850 }
851 }
852
853 /* Skip to the offset of the object we want to read */
854 for (i=0;i < offset;i++)
855 {
856 int c = pdfi_read_byte(ctx, compressed_stream);
857 if (c < 0) {
858 code = gs_note_error(gs_error_ioerror);
859 goto exit;
860 }
861 }
862
863 /* If object_length is not 0, then we want to apply a SubFileDecode filter to limit
864 * the number of bytes we read to the declared size of the object (difference between
865 * the offsets of the object we want to read, and the next object). If it is 0 then
866 * we're reading the last object in the stream, so we just rely on the SubFileDecode
867 * we set up when we created compressed_stream to limit the bytes to the length of
868 * that stream.
869 */
870 if (object_length > 0) {
871 code = pdfi_apply_SubFileDecode_filter(ctx, object_length, NULL, compressed_stream, &Object_stream, false);
872 if (code < 0)
873 goto exit;
874 } else {
875 Object_stream = compressed_stream;
876 }
877
878 code = pdfi_read_token(ctx, Object_stream, obj, gen);
879 if (code < 0)
880 goto exit;
881 if (code == 0) {
882 code = gs_note_error(gs_error_syntaxerror);
883 goto exit;
884 }
885 if (pdfi_type_of(ctx->stack_top[-1]) == PDF_ARRAY_MARK || pdfi_type_of(ctx->stack_top[-1]) == PDF_DICT_MARK) {
886 int start_depth = pdfi_count_stack(ctx);
887
888 /* Need to read all the elements from COS objects */
889 do {
890 code = pdfi_read_token(ctx, Object_stream, obj, gen);
891 if (code < 0)
892 goto exit;
893 if (code == 0) {
894 code = gs_note_error(gs_error_syntaxerror);
895 goto exit;
896 }
897 if (compressed_stream->eof == true) {
898 code = gs_note_error(gs_error_ioerror);
899 goto exit;
900 }
901 } while ((pdfi_type_of(ctx->stack_top[-1]) != PDF_ARRAY && pdfi_type_of(ctx->stack_top[-1]) != PDF_DICT) || pdfi_count_stack(ctx) > start_depth);
902 }
903
904 *object = ctx->stack_top[-1];
905 /* For compressed objects we don't get a 'obj gen obj' sequence which is what sets
906 * the object number for uncompressed objects. So we need to do that here.
907 */
908 if (*object >= PDF_TOKEN_AS_OBJ(TOKEN__LAST_KEY)) {
909 (*object)->indirect_num = (*object)->object_num = obj;
910 (*object)->indirect_gen = (*object)->generation_num = gen;
911 pdfi_countup(*object);
912 }
913 pdfi_pop(ctx, 1);
914
915 if (cache) {
916 code = pdfi_add_to_cache(ctx, *object);
917 if (code < 0) {
918 pdfi_countdown(*object);
919 goto exit;
920 }
921 }
922
923 exit:
924 if (Object_stream)
925 pdfi_close_file(ctx, Object_stream);
926 if (Object_stream != compressed_stream)
927 if (compressed_stream)
928 pdfi_close_file(ctx, compressed_stream);
929 if (SubFile_stream)
930 pdfi_close_file(ctx, SubFile_stream);
931 pdfi_countdown(compressed_object);
932 pdfi_countdown(Type);
933 return code;
934 }
935
936 /* pdf_dereference returns an object with a reference count of at least 1, this represents the
937 * reference being held by the caller (in **object) when we return from this function.
938 */
939 static int pdfi_dereference_main(pdf_context *ctx, uint64_t obj, uint64_t gen, pdf_obj **object, bool cache)
940 {
941 xref_entry *entry;
942 int code, stack_depth = pdfi_count_stack(ctx);
943 gs_offset_t saved_stream_offset;
944 bool saved_decrypt_strings = ctx->encryption.decrypt_strings;
945
946 *object = NULL;
947
948 if (ctx->xref_table == NULL)
949 return_error(gs_error_typecheck);
950
951 if (ctx->main_stream == NULL || ctx->main_stream->s == NULL)
952 return_error(gs_error_ioerror);
953
954 if (obj >= ctx->xref_table->xref_size) {
955 char extra_info[gp_file_name_sizeof];
956
957 gs_snprintf(extra_info, sizeof(extra_info), "Error, attempted to dereference object %"PRIu64", which is not present in the xref table\n", obj);
958 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, E_PDF_BADOBJNUMBER, "pdfi_dereference", extra_info)) < 0) {
959 return code;
960 }
961
962 code = pdfi_repair_file(ctx);
963 if (code < 0) {
964 *object = NULL;
965 return code;
966 }
967 if (obj >= ctx->xref_table->xref_size) {
968 *object = NULL;
969 return_error(gs_error_rangecheck);
970 }
971 }
972
973 entry = &ctx->xref_table->xref[obj];
974
975 if(entry->object_num == 0) {
976 pdfi_set_error(ctx, 0, NULL, E_PDF_BADOBJNUMBER, "pdfi_dereference_main", "Attempt to dereference object 0");
977 return_error(gs_error_undefined);
978 }
979
980 if (entry->free) {
981 char extra_info[gp_file_name_sizeof];
982
983 gs_snprintf(extra_info, sizeof(extra_info), "Attempt to dereference free object %"PRIu64", treating as NULL object.\n", entry->object_num);
984 code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_undefined), NULL, E_PDF_DEREF_FREE_OBJ, "pdfi_dereference", extra_info);
985 *object = PDF_NULL_OBJ;
986 return code;
987 }else {
988 if (!entry->compressed) {
989 if(entry->u.uncompressed.generation_num != gen)
990 pdfi_set_warning(ctx, 0, NULL, W_PDF_MISMATCH_GENERATION, "pdfi_dereference_main", "");
991 }
992 }
993
994 if (ctx->loop_detection) {
995 if (pdfi_loop_detector_check_object(ctx, obj) == true)
996 return_error(gs_error_circular_reference);
997 if (entry->free) {
998 code = pdfi_loop_detector_add_object(ctx, obj);
999 if (code < 0)
1000 return code;
1001 }
1002 }
1003 if (entry->cache != NULL){
1004 pdf_obj_cache_entry *cache_entry = entry->cache;
1005
1006 #if CACHE_STATISTICS
1007 ctx->hits++;
1008 #endif
1009 *object = cache_entry->o;
1010 pdfi_countup(*object);
1011
1012 pdfi_promote_cache_entry(ctx, cache_entry);
1013 } else {
1014 saved_stream_offset = pdfi_unread_tell(ctx);
1015
1016 if (entry->compressed) {
1017 /* This is an object in a compressed object stream */
1018 ctx->encryption.decrypt_strings = false;
1019
1020 code = pdfi_deref_compressed(ctx, obj, gen, object, entry, cache);
1021 if (code < 0 || *object == NULL)
1022 goto error;
1023 } else {
1024 #if CACHE_STATISTICS
1025 ctx->misses++;
1026 #endif
1027 ctx->encryption.decrypt_strings = true;
1028
1029 code = pdfi_seek(ctx, ctx->main_stream, entry->u.uncompressed.offset, SEEK_SET);
1030 if (code < 0)
1031 goto error;
1032
1033 code = pdfi_read_object(ctx, ctx->main_stream, entry->u.uncompressed.offset);
1034
1035 /* pdfi_read_object() could do a repair, which would invalidate the xref and rebuild it.
1036 * reload the xref entry to be certain it is valid.
1037 */
1038 entry = &ctx->xref_table->xref[obj];
1039 if (code < 0) {
1040 int code1 = 0;
1041 if (entry->free) {
1042 char extra_info[gp_file_name_sizeof];
1043
1044 gs_snprintf(extra_info, sizeof(extra_info), "Attempt to dereference free object %"PRIu64", treating as NULL object.\n", entry->object_num);
1045 code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_undefined), NULL, E_PDF_DEREF_FREE_OBJ, "pdfi_dereference", extra_info);
1046 *object = PDF_NULL_OBJ;
1047 if (code < 0)
1048 goto error;
1049 goto free_obj;
1050 }
1051 ctx->encryption.decrypt_strings = saved_decrypt_strings;
1052 (void)pdfi_seek(ctx, ctx->main_stream, saved_stream_offset, SEEK_SET);
1053 pdfi_pop(ctx, pdfi_count_stack(ctx) - stack_depth);
1054
1055 code1 = pdfi_repair_file(ctx);
1056 if (code1 == 0)
1057 return pdfi_dereference_main(ctx, obj, gen, object, cache);
1058 /* Repair failed, just give up and return an error */
1059 goto error;
1060 }
1061
1062 /* We only expect a single object back when dereferencing an indirect reference
1063 * The only way (I think) we can end up with more than one is if the object initially
1064 * appears to be a dictionary or array, but the object terminates (with endobj or
1065 * simply reaching EOF) without terminating the array or dictionary. That's clearly
1066 * an error. We might, as a future 'improvement' choose to walk back through
1067 * the stack looking for unterminated dictionary or array markers, and closing them
1068 * so that (hopefully!) we end up with a single 'repaired' object on the stack.
1069 * But for now I'm simply going to treat these as errors. We will try a repair on the
1070 * file to see if we end up using a different (hopefully intact) object from the file.
1071 */
1072 if (pdfi_count_stack(ctx) - stack_depth > 1) {
1073 int code1 = 0;
1074
1075 code1 = pdfi_repair_file(ctx);
1076 if (code1 == 0)
1077 return pdfi_dereference_main(ctx, obj, gen, object, cache);
1078 /* Repair failed, just give up and return an error */
1079 code = gs_note_error(gs_error_syntaxerror);
1080 goto error;
1081 }
1082
1083 if (pdfi_count_stack(ctx) > 0 &&
1084 ((ctx->stack_top[-1] > PDF_TOKEN_AS_OBJ(TOKEN__LAST_KEY) &&
1085 (ctx->stack_top[-1])->object_num == obj)
1086 || ctx->stack_top[-1] == PDF_NULL_OBJ)) {
1087 *object = ctx->stack_top[-1];
1088 pdfi_countup(*object);
1089 pdfi_pop(ctx, 1);
1090 if (pdfi_type_of(*object) == PDF_INDIRECT) {
1091 pdf_indirect_ref *iref = (pdf_indirect_ref *)*object;
1092
1093 if (iref->ref_object_num == obj) {
1094 code = gs_note_error(gs_error_circular_reference);
1095 pdfi_countdown(*object);
1096 *object = NULL;
1097 goto error;
1098 }
1099 }
1100 /* There's really no point in caching an indirect reference and
1101 * I think it could be potentially confusing to later calls.
1102 */
1103 if (cache && pdfi_type_of(*object) != PDF_INDIRECT) {
1104 code = pdfi_add_to_cache(ctx, *object);
1105 if (code < 0) {
1106 pdfi_countdown(*object);
1107 goto error;
1108 }
1109 }
1110 } else {
1111 int code1 = 0;
1112
1113 if (pdfi_count_stack(ctx) > 0)
1114 pdfi_pop(ctx, 1);
1115
1116 if (entry->free) {
1117 char extra_info[gp_file_name_sizeof];
1118
1119 gs_snprintf(extra_info, sizeof(extra_info), "Attempt to dereference free object %"PRIu64", treating as NULL object.\n", entry->object_num);
1120 code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_undefined), NULL, E_PDF_DEREF_FREE_OBJ, "pdfi_dereference", extra_info);
1121 *object = PDF_NULL_OBJ;
1122 if (code < 0)
1123 goto error;
1124 return code;
1125 }
1126 code1 = pdfi_repair_file(ctx);
1127 if (code1 == 0)
1128 return pdfi_dereference_main(ctx, obj, gen, object, cache);
1129 /* Repair failed, just give up and return an error */
1130 code = gs_note_error(gs_error_undefined);
1131 goto error;
1132 }
1133 }
1134 free_obj:
1135 (void)pdfi_seek(ctx, ctx->main_stream, saved_stream_offset, SEEK_SET);
1136 }
1137
1138 if (ctx->loop_detection && pdf_object_num(*object) != 0) {
1139 code = pdfi_loop_detector_add_object(ctx, (*object)->object_num);
1140 if (code < 0) {
1141 ctx->encryption.decrypt_strings = saved_decrypt_strings;
1142 return code;
1143 }
1144 }
1145 ctx->encryption.decrypt_strings = saved_decrypt_strings;
1146 return 0;
1147
1148 error:
1149 ctx->encryption.decrypt_strings = saved_decrypt_strings;
1150 (void)pdfi_seek(ctx, ctx->main_stream, saved_stream_offset, SEEK_SET);
1151 /* Return the stack to the state at entry */
1152 pdfi_pop(ctx, pdfi_count_stack(ctx) - stack_depth);
1153 return code;
1154 }
1155
1156 int pdfi_dereference(pdf_context *ctx, uint64_t obj, uint64_t gen, pdf_obj **object)
1157 {
1158 return pdfi_dereference_main(ctx, obj, gen, object, true);
1159 }
1160
1161 int pdfi_dereference_nocache(pdf_context *ctx, uint64_t obj, uint64_t gen, pdf_obj **object)
1162 {
1163 return pdfi_dereference_main(ctx, obj, gen, object, false);
1164 }
1165
1166 /* do a derefence with loop detection */
1167 int pdfi_deref_loop_detect(pdf_context *ctx, uint64_t obj, uint64_t gen, pdf_obj **object)
1168 {
1169 int code;
1170
1171 code = pdfi_loop_detector_mark(ctx);
1172 if (code < 0)
1173 return code;
1174
1175 code = pdfi_dereference(ctx, obj, gen, object);
1176 (void)pdfi_loop_detector_cleartomark(ctx);
1177 return code;
1178 }
1179
1180 int pdfi_deref_loop_detect_nocache(pdf_context *ctx, uint64_t obj, uint64_t gen, pdf_obj **object)
1181 {
1182 int code;
1183
1184 code = pdfi_loop_detector_mark(ctx);
1185 if (code < 0)
1186 return code;
1187
1188 code = pdfi_dereference_nocache(ctx, obj, gen, object);
1189 (void)pdfi_loop_detector_cleartomark(ctx);
1190 return code;
1191 }
1192
1193 static int pdfi_resolve_indirect_array(pdf_context *ctx, pdf_obj *obj, bool recurse)
1194 {
1195 int code = 0;
1196 uint64_t index, arraysize;
1197 pdf_obj *object = NULL;
1198 pdf_array *array = (pdf_array *)obj;
1199
1200 arraysize = pdfi_array_size(array);
1201 for (index = 0; index < arraysize; index++) {
1202 if (ctx->loop_detection != NULL) {
1203 code = pdfi_loop_detector_mark(ctx);
1204 if (code < 0)
1205 return code;
1206 }
1207
1208 code = pdfi_array_get_no_store_R(ctx, array, index, &object);
1209
1210 if (ctx->loop_detection != NULL) {
1211 int code1 = pdfi_loop_detector_cleartomark(ctx);
1212 if (code1 < 0)
1213 return code1;
1214 }
1215
1216 if (code == gs_error_circular_reference) {
1217 /* Previously we just left as an indirect reference, but now we want
1218 * to return the error so we don't end up replacing indirect references
1219 * to objects with circular references.
1220 */
1221 } else {
1222 if (code < 0) goto exit;
1223 if (recurse) {
1224 code = pdfi_resolve_indirect_loop_detect(ctx, NULL, object, recurse);
1225 if (code < 0) goto exit;
1226 }
1227 /* don't store the object if it's a stream (leave as a ref) */
1228 if (pdfi_type_of(object) != PDF_STREAM)
1229 code = pdfi_array_put(ctx, array, index, object);
1230 }
1231 if (code < 0) goto exit;
1232
1233 pdfi_countdown(object);
1234 object = NULL;
1235 }
1236
1237 exit:
1238 pdfi_countdown(object);
1239 return code;
1240 }
1241
1242 static int pdfi_resolve_indirect_dict(pdf_context *ctx, pdf_obj *obj, bool recurse)
1243 {
1244 int code = 0;
1245 pdf_dict *dict = (pdf_dict *)obj;
1246 pdf_name *Key = NULL;
1247 pdf_obj *Value = NULL;
1248 uint64_t index, dictsize;
1249
1250 dictsize = pdfi_dict_entries(dict);
1251
1252 /* Note: I am not using pdfi_dict_first/next because of needing to handle
1253 * circular references.
1254 */
1255 for (index=0; index<dictsize; index ++) {
1256 Key = (pdf_name *)dict->list[index].key;
1257 if (pdfi_name_is(Key, "Parent"))
1258 continue;
1259
1260 if (ctx->loop_detection != NULL) {
1261 code = pdfi_loop_detector_mark(ctx);
1262 if (code < 0)
1263 return code;
1264 }
1265
1266 code = pdfi_dict_get_no_store_R_key(ctx, dict, Key, &Value);
1267
1268 if (ctx->loop_detection != NULL) {
1269 int code1 = pdfi_loop_detector_cleartomark(ctx);
1270 if (code1 < 0)
1271 return code1;
1272 }
1273
1274 if (code == gs_error_circular_reference) {
1275 /* Just leave as an indirect ref */
1276 code = 0;
1277 } else {
1278 if (code < 0) goto exit;
1279 if (recurse) {
1280 code = pdfi_resolve_indirect_loop_detect(ctx, NULL, Value, recurse);
1281 if (code < 0)
1282 goto exit;
1283 }
1284 /* don't store the object if it's a stream (leave as a ref) */
1285 if (pdfi_type_of(Value) != PDF_STREAM)
1286 code = pdfi_dict_put_obj(ctx, dict, (pdf_obj *)Key, Value, true);
1287 }
1288 if (code < 0) goto exit;
1289
1290 pdfi_countdown(Value);
1291 Value = NULL;
1292 }
1293
1294 exit:
1295 pdfi_countdown(Value);
1296 return code;
1297 }
1298
1299 /* Resolve all the indirect references for an object
1300 * Note: This can be recursive
1301 */
1302 int pdfi_resolve_indirect(pdf_context *ctx, pdf_obj *value, bool recurse)
1303 {
1304 int code = 0;
1305
1306 switch(pdfi_type_of(value)) {
1307 case PDF_ARRAY:
1308 code = pdfi_resolve_indirect_array(ctx, value, recurse);
1309 break;
1310 case PDF_DICT:
1311 code = pdfi_resolve_indirect_dict(ctx, value, recurse);
1312 break;
1313 default:
1314 break;
1315 }
1316 return code;
1317 }
1318
1319 /* Resolve all the indirect references for an object
1320 * Resolve indirect references, either one level or recursively, with loop detect on
1321 * the parent (can by NULL) and the value.
1322 */
1323 int pdfi_resolve_indirect_loop_detect(pdf_context *ctx, pdf_obj *parent, pdf_obj *value, bool recurse)
1324 {
1325 int code = 0;
1326
1327 code = pdfi_loop_detector_mark(ctx);
1328 if (code < 0) goto exit;
1329 if (parent && parent->object_num != 0) {
1330 code = pdfi_loop_detector_add_object(ctx, parent->object_num);
1331 if (code < 0) goto exit;
1332 }
1333
1334 if (pdf_object_num(value) != 0) {
1335 if (pdfi_loop_detector_check_object(ctx, value->object_num)) {
1336 code = gs_note_error(gs_error_circular_reference);
1337 goto exit;
1338 }
1339 code = pdfi_loop_detector_add_object(ctx, value->object_num);
1340 if (code < 0) goto exit;
1341 }
1342 code = pdfi_resolve_indirect(ctx, value, recurse);
1343
1344 exit:
1345 (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current loop */
1346 return code;
1347 }