Debian Codegraph @

pdf/pdf_repair.c

raw
1 /* Copyright (C) 2020-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 /* Routines to attempt repair of PDF files in the event of an error */
17
18 #include "pdf_int.h"
19 #include "pdf_stack.h"
20 #include "strmio.h"
21 #include "stream.h"
22 #include "pdf_deref.h"
23 #include "pdf_dict.h"
24 #include "pdf_file.h"
25 #include "pdf_misc.h"
26 #include "pdf_repair.h"
27
28 static int pdfi_repair_add_object(pdf_context *ctx, int64_t obj, int64_t gen, gs_offset_t offset)
29 {
30 /* Although we can handle object numbers larger than this, on some systems (32-bit Windows)
31 * memset is limited to a (signed!) integer for the size of memory to clear. We could deal
32 * with this by clearing the memory in blocks, but really, this is almost certainly a
33 * corrupted file or something.
34 */
35 if (obj >= 0x7ffffff / sizeof(xref_entry) || obj < 1 || gen < 0 || offset < 0)
36 return_error(gs_error_rangecheck);
37
38 if (ctx->xref_table == NULL) {
39 ctx->xref_table = (xref_table_t *)gs_alloc_bytes(ctx->memory, sizeof(xref_table_t), "repair xref table");
40 if (ctx->xref_table == NULL) {
41 return_error(gs_error_VMerror);
42 }
43 memset(ctx->xref_table, 0x00, sizeof(xref_table_t));
44 ctx->xref_table->xref = (xref_entry *)gs_alloc_bytes(ctx->memory, (size_t)(obj + 1) * sizeof(xref_entry), "repair xref table");
45 if (ctx->xref_table->xref == NULL){
46 gs_free_object(ctx->memory, ctx->xref_table, "failed to allocate xref table entries for repair");
47 ctx->xref_table = NULL;
48 return_error(gs_error_VMerror);
49 }
50 memset(ctx->xref_table->xref, 0x00, (obj + 1) * sizeof(xref_entry));
51 ctx->xref_table->ctx = ctx;
52 ctx->xref_table->type = PDF_XREF_TABLE;
53 ctx->xref_table->xref_size = obj + 1;
54 #if REFCNT_DEBUG
55 ctx->xref_table->UID = ctx->ref_UID++;
56 outprintf(ctx->memory, "Allocated xref table with UID %"PRIi64"\n", ctx->xref_table->UID);
57 #endif
58 pdfi_countup(ctx->xref_table);
59 } else {
60 if (ctx->xref_table->xref_size < (obj + 1)) {
61 xref_entry *new_xrefs;
62
63 new_xrefs = (xref_entry *)gs_alloc_bytes(ctx->memory, (size_t)(obj + 1) * sizeof(xref_entry), "read_xref_stream allocate xref table entries");
64 if (new_xrefs == NULL){
65 pdfi_countdown(ctx->xref_table);
66 ctx->xref_table = NULL;
67 return_error(gs_error_VMerror);
68 }
69 memset(new_xrefs, 0x00, (obj + 1) * sizeof(xref_entry));
70 memcpy(new_xrefs, ctx->xref_table->xref, ctx->xref_table->xref_size * sizeof(xref_entry));
71 gs_free_object(ctx->memory, ctx->xref_table->xref, "reallocated xref entries");
72 ctx->xref_table->xref = new_xrefs;
73 ctx->xref_table->xref_size = obj + 1;
74 }
75 }
76 ctx->xref_table->xref[obj].compressed = false;
77 ctx->xref_table->xref[obj].free = false;
78 ctx->xref_table->xref[obj].object_num = obj;
79 ctx->xref_table->xref[obj].u.uncompressed.generation_num = gen;
80 ctx->xref_table->xref[obj].u.uncompressed.offset = offset;
81 return 0;
82 }
83
84 int pdfi_repair_file(pdf_context *ctx)
85 {
86 int code = 0;
87 gs_offset_t offset, saved_offset;
88 int64_t object_num = 0, generation_num = 0;
89 int i;
90 gs_offset_t outer_saved_offset[3];
91
92 if (ctx->repaired) {
93 pdfi_set_error(ctx, 0, NULL, E_PDF_UNREPAIRABLE, "pdfi_repair_file", (char *)"%% Trying to repair file for second time -- unrepairable");
94 return_error(gs_error_undefined);
95 }
96
97 saved_offset = pdfi_unread_tell(ctx);
98
99 ctx->repaired = true;
100 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_ioerror), NULL, E_PDF_REPAIRED, "pdfi_repair_file", NULL)) < 0)
101 return code;
102
103 ctx->repairing = true;
104
105 pdfi_clearstack(ctx);
106
107 if(ctx->args.pdfdebug)
108 outprintf(ctx->memory, "%% Error encountered in opening PDF file, attempting repair\n");
109
110 /* First try to locate a %PDF header. If we can't find one, abort this, the file is too broken
111 * and may not even be a PDF file.
112 */
113 pdfi_seek(ctx, ctx->main_stream, 0, SEEK_SET);
114 {
115 static const char test[] = "%PDF";
116 int index = 0;
117
118 do {
119 int c = pdfi_read_byte(ctx, ctx->main_stream);
120 if (c < 0)
121 goto exit;
122
123 if (c == test[index])
124 index++;
125 else
126 index = 0;
127 } while (index < 4);
128 if (index != 4) {
129 code = gs_note_error(gs_error_undefined);
130 goto exit;
131 }
132 pdfi_unread(ctx, ctx->main_stream, (byte *)test, 4);
133 pdfi_skip_comment(ctx, ctx->main_stream);
134 }
135 if (ctx->main_stream->eof == true) {
136 code = gs_note_error(gs_error_ioerror);
137 goto exit;
138 }
139
140 /* First pass, identify all the objects of the form x y obj */
141
142 do {
143 code = pdfi_skip_white(ctx, ctx->main_stream);
144 if (code < 0) {
145 if (code != gs_error_VMerror && code != gs_error_ioerror) {
146 pdfi_clearstack(ctx);
147 continue;
148 } else
149 goto exit;
150 }
151 offset = pdfi_unread_tell(ctx);
152 outer_saved_offset[0] = outer_saved_offset[1] = outer_saved_offset[2] = 0;
153 do {
154 outer_saved_offset[0] = outer_saved_offset[1];
155 outer_saved_offset[1] = outer_saved_offset[2];
156 outer_saved_offset[2] = pdfi_unread_tell(ctx);
157
158 object_num = 0;
159
160 code = pdfi_read_token(ctx, ctx->main_stream, 0, 0);
161 if (code < 0) {
162 if (code != gs_error_VMerror && code != gs_error_ioerror) {
163 pdfi_clearstack(ctx);
164 continue;
165 } else
166 goto exit;
167 }
168 if (pdfi_count_stack(ctx) > 0) {
169 if (pdfi_type_of(ctx->stack_top[-1]) == PDF_FAST_KEYWORD) {
170 pdf_obj *k = ctx->stack_top[-1];
171 pdf_num *n;
172
173 if (k == PDF_TOKEN_AS_OBJ(TOKEN_OBJ)) {
174 gs_offset_t saved_offset[3];
175
176 offset = outer_saved_offset[0];
177
178 saved_offset[0] = saved_offset[1] = saved_offset[2] = 0;
179
180 if (pdfi_count_stack(ctx) < 3 || pdfi_type_of(ctx->stack_top[-3]) != PDF_INT || pdfi_type_of(ctx->stack_top[-2]) != PDF_INT) {
181 pdfi_clearstack(ctx);
182 continue;
183 }
184 n = (pdf_num *)ctx->stack_top[-3];
185 object_num = n->value.i;
186 n = (pdf_num *)ctx->stack_top[-2];
187 generation_num = n->value.i;
188 pdfi_clearstack(ctx);
189
190 do {
191 /* move all the saved offsets up by one */
192 saved_offset[0] = saved_offset[1];
193 saved_offset[1] = saved_offset[2];
194 saved_offset[2] = pdfi_unread_tell(ctx);
195
196 code = pdfi_read_token(ctx, ctx->main_stream, 0, 0);
197 if (code < 0) {
198 if (code != gs_error_VMerror && code != gs_error_ioerror)
199 continue;
200 goto exit;
201 }
202 if (code == 0 && ctx->main_stream->eof)
203 break;
204
205 if (pdfi_type_of(ctx->stack_top[-1]) == PDF_FAST_KEYWORD) {
206 pdf_obj *k = ctx->stack_top[-1];
207
208 if (k == PDF_TOKEN_AS_OBJ(TOKEN_OBJ)) {
209 /* Found obj while looking for endobj, store the existing 'obj'
210 * and start afresh.
211 */
212 code = pdfi_repair_add_object(ctx, object_num, generation_num, offset);
213 if (pdfi_count_stack(ctx) < 3 || pdfi_type_of(ctx->stack_top[-3]) != PDF_INT || pdfi_type_of(ctx->stack_top[-2]) != PDF_INT) {
214 pdfi_clearstack(ctx);
215 break;
216 }
217 n = (pdf_num *)ctx->stack_top[-3];
218 object_num = n->value.i;
219 n = (pdf_num *)ctx->stack_top[-2];
220 generation_num = n->value.i;
221 pdfi_clearstack(ctx);
222 offset = saved_offset[0];
223 continue;
224 }
225
226 if (k == PDF_TOKEN_AS_OBJ(TOKEN_ENDOBJ)) {
227 code = pdfi_repair_add_object(ctx, object_num, generation_num, offset);
228 if (code < 0)
229 goto exit;
230 pdfi_clearstack(ctx);
231 break;
232 } else {
233 if (k == PDF_TOKEN_AS_OBJ(TOKEN_STREAM)) {
234 static const char test[] = "endstream";
235 int index = 0;
236
237 do {
238 int c = pdfi_read_byte(ctx, ctx->main_stream);
239 if (c == EOFC)
240 break;
241 if (c < 0)
242 goto exit;
243 if (c == test[index])
244 index++;
245 else if (c == test[0]) /* Pesky 'e' appears twice */
246 index = 1;
247 else
248 index = 0;
249 } while (index < 9);
250 do {
251 code = pdfi_read_bare_keyword(ctx, ctx->main_stream);
252 if (code == gs_error_VMerror || code == gs_error_ioerror)
253 goto exit;
254 if (code < 0) {
255 /* Something went wrong and we couldn't read a token, consume one byte and retry */
256 (void)pdfi_read_byte(ctx, ctx->main_stream);
257 } else {
258 if (code == TOKEN_ENDOBJ || code == TOKEN_INVALID_KEY) {
259 code = pdfi_repair_add_object(ctx, object_num, generation_num, offset);
260 if (code == gs_error_VMerror || code == gs_error_ioerror)
261 goto exit;
262 break;
263 }
264 }
265 } while(ctx->main_stream->eof == false);
266 pdfi_clearstack(ctx);
267 break;
268 } else {
269 pdfi_clearstack(ctx);
270 }
271 }
272 }
273 } while(1);
274 break;
275 } else {
276 if (k == PDF_TOKEN_AS_OBJ(TOKEN_ENDOBJ)) {
277 pdfi_clearstack(ctx);
278 } else
279 if (k == PDF_TOKEN_AS_OBJ(TOKEN_STARTXREF)) {
280 code = pdfi_read_token(ctx, ctx->main_stream, 0, 0);
281 if (code < 0 && code != gs_error_VMerror && code != gs_error_ioerror)
282 continue;
283 if (code < 0)
284 goto exit;
285 pdfi_clearstack(ctx);
286 } else {
287 if (k == PDF_TOKEN_AS_OBJ(TOKEN_TRAILER)) {
288 code = pdfi_read_bare_object(ctx, ctx->main_stream, 0, 0, 0);
289 if (code == 0 && pdfi_count_stack(ctx) > 0 && pdfi_type_of(ctx->stack_top[-1]) == PDF_DICT) {
290 if (ctx->Trailer) {
291 pdf_dict *d = (pdf_dict *)ctx->stack_top[-1];
292 bool known = false;
293
294 code = pdfi_dict_known(ctx, d, "Root", &known);
295 if (code == 0 && known) {
296 pdfi_countdown(ctx->Trailer);
297 ctx->Trailer = (pdf_dict *)ctx->stack_top[-1];
298 pdfi_countup(ctx->Trailer);
299 }
300 } else {
301 ctx->Trailer = (pdf_dict *)ctx->stack_top[-1];
302 pdfi_countup(ctx->Trailer);
303 }
304 }
305 }
306 pdfi_clearstack(ctx);
307 }
308 }
309 code = pdfi_skip_white(ctx, ctx->main_stream);
310 if (code < 0) {
311 if (code != gs_error_VMerror && code != gs_error_ioerror) {
312 pdfi_clearstack(ctx);
313 continue;
314 } else
315 goto exit;
316 }
317 }
318 if (pdfi_count_stack(ctx) > 0 && pdfi_type_of(ctx->stack_top[-1]) != PDF_INT)
319 pdfi_clearstack(ctx);
320 }
321 } while (ctx->main_stream->eof == false);
322 } while(ctx->main_stream->eof == false);
323
324 pdfi_seek(ctx, ctx->main_stream, 0, SEEK_SET);
325 ctx->main_stream->eof = false;
326
327 /* Second pass, examine every object we have located to see if its an ObjStm */
328 if (ctx->xref_table == NULL || ctx->xref_table->xref_size < 1) {
329 code = gs_note_error(gs_error_syntaxerror);
330 goto exit;
331 }
332
333 for (i=1;i < ctx->xref_table->xref_size;i++) {
334 if (ctx->xref_table->xref[i].object_num != 0) {
335 /* At this stage, all the objects we've found must be uncompressed */
336 if (ctx->xref_table->xref[i].u.uncompressed.offset > ctx->main_stream_length) {
337 /* This can only happen if we had read an xref table before we tried to repair
338 * the file, and the table has entries we didn't find in the file. So
339 * mark the entry as free, and offset of 0, and just carry on.
340 */
341 ctx->xref_table->xref[i].free = 1;
342 ctx->xref_table->xref[i].u.uncompressed.offset = 0;
343 continue;
344 }
345
346 pdfi_seek(ctx, ctx->main_stream, ctx->xref_table->xref[i].u.uncompressed.offset, SEEK_SET);
347 do {
348 code = pdfi_read_token(ctx, ctx->main_stream, 0, 0);
349 if (ctx->main_stream->eof == true || (code < 0 && code != gs_error_ioerror && code != gs_error_VMerror)) {
350 /* object offset is beyond EOF or object is broken (possibly due to multiple xref
351 * errors) ignore the error and carry on, if the object gets used then we will
352 * error out at that point.
353 */
354 code = 0;
355 break;
356 }
357 if (code < 0)
358 goto exit;
359 if (pdfi_type_of(ctx->stack_top[-1]) == PDF_FAST_KEYWORD) {
360 pdf_obj *k = ctx->stack_top[-1];
361
362 if (k == PDF_TOKEN_AS_OBJ(TOKEN_OBJ)) {
363 continue;
364 }
365 if (k == PDF_TOKEN_AS_OBJ(TOKEN_ENDOBJ)) {
366 if (pdfi_count_stack(ctx) > 1) {
367 if (pdfi_type_of(ctx->stack_top[-2]) == PDF_DICT) {
368 pdf_dict *d = (pdf_dict *)ctx->stack_top[-2];
369 pdf_obj *o = NULL;
370
371 code = pdfi_dict_knownget_type(ctx, d, "Type", PDF_NAME, &o);
372 if (code < 0) {
373 pdfi_clearstack(ctx);
374 continue;
375 }
376 if (code > 0) {
377 pdf_name *n = (pdf_name *)o;
378
379 if (pdfi_name_is(n, "Catalog")) {
380 pdfi_countdown(ctx->Root); /* In case it was already set */
381 ctx->Root = (pdf_dict *)ctx->stack_top[-2];
382 pdfi_countup(ctx->Root);
383 }
384 }
385 pdfi_countdown(o);
386 }
387 }
388 pdfi_clearstack(ctx);
389 break;
390 }
391 if (k == PDF_TOKEN_AS_OBJ(TOKEN_STREAM)) {
392 pdf_dict *d;
393 pdf_name *n = NULL;
394
395 if (pdfi_count_stack(ctx) <= 1) {
396 pdfi_clearstack(ctx);
397 break;;
398 }
399 d = (pdf_dict *)ctx->stack_top[-2];
400 if (pdfi_type_of(d) != PDF_DICT) {
401 pdfi_clearstack(ctx);
402 break;;
403 }
404 code = pdfi_dict_knownget_type(ctx, d, "Type", PDF_NAME, (pdf_obj **)&n);
405 if (code < 0) {
406 if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_UNREPAIRABLE, "pdfi_repair_file", NULL)) < 0) {
407 pdfi_clearstack(ctx);
408 goto exit;
409 }
410 }
411 if (code > 0) {
412 if (pdfi_name_is(n, "ObjStm")) {
413 int64_t N;
414 int obj_num, offset;
415 int j;
416 pdf_c_stream *compressed_stream;
417 pdf_stream *stream;
418
419 offset = pdfi_unread_tell(ctx);
420 pdfi_seek(ctx, ctx->main_stream, offset, SEEK_SET);
421
422 code = pdfi_obj_dict_to_stream(ctx, d, &stream, true);
423 if (code == 0)
424 code = pdfi_filter(ctx, stream, ctx->main_stream, &compressed_stream, false);
425
426 pdfi_countdown(stream);
427
428 if (code == 0) {
429 code = pdfi_dict_get_int(ctx, d, "N", &N);
430 if (code == 0) {
431 for (j=0;j < N; j++) {
432 code = pdfi_read_bare_int(ctx, compressed_stream, &obj_num);
433 if (code <= 0)
434 break;
435 else {
436 code = pdfi_read_bare_int(ctx, compressed_stream, &offset);
437 if (code > 0) {
438 if (obj_num < 1) {
439 pdfi_close_file(ctx, compressed_stream);
440 pdfi_countdown(n);
441 pdfi_clearstack(ctx);
442 code = gs_note_error(gs_error_rangecheck);
443 goto exit;
444 }
445 if (obj_num >= ctx->xref_table->xref_size)
446 code = pdfi_repair_add_object(ctx, obj_num, 0, 0);
447
448 if (code >= 0) {
449 ctx->xref_table->xref[obj_num].compressed = true;
450 ctx->xref_table->xref[obj_num].free = false;
451 ctx->xref_table->xref[obj_num].object_num = obj_num;
452 ctx->xref_table->xref[obj_num].u.compressed.compressed_stream_num = i;
453 ctx->xref_table->xref[obj_num].u.compressed.object_index = j;
454 }
455 }
456 }
457 }
458 }
459 pdfi_close_file(ctx, compressed_stream);
460 }
461 if (code < 0) {
462 if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_UNREPAIRABLE, "pdfi_repair_file", NULL)) < 0) {
463 pdfi_countdown(n);
464 pdfi_clearstack(ctx);
465 goto exit;
466 }
467 }
468 }
469 }
470 pdfi_countdown(n);
471 pdfi_clearstack(ctx);
472 break;
473 }
474 }
475 } while (1);
476 }
477 }
478
479 exit:
480 if (code > 0)
481 code = 0;
482 pdfi_seek(ctx, ctx->main_stream, saved_offset, SEEK_SET);
483 ctx->main_stream->eof = false;
484 ctx->repairing = false;
485 return code;
486 }