Coverage Report

Created: 2026-04-01 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/pdf/pdf_xref.c
Line
Count
Source
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
20.4k
{
29
20.4k
    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
20.4k
    if (new_size >= (0x7ffffff / sizeof(xref_entry)))
37
22
        return_error(gs_error_rangecheck);
38
39
20.4k
    new_xrefs = (xref_entry *)gs_alloc_bytes(ctx->memory, (size_t)(new_size) * sizeof(xref_entry), "read_xref_stream allocate xref table entries");
40
20.4k
    if (new_xrefs == NULL){
41
0
        pdfi_countdown(ctx->xref_table);
42
0
        ctx->xref_table = NULL;
43
0
        return_error(gs_error_VMerror);
44
0
    }
45
20.4k
    memset(new_xrefs, 0x00, (new_size) * sizeof(xref_entry));
46
20.4k
    memcpy(new_xrefs, ctx->xref_table->xref, ctx->xref_table->xref_size * sizeof(xref_entry));
47
20.4k
    gs_free_object(ctx->memory, ctx->xref_table->xref, "reallocated xref entries");
48
20.4k
    ctx->xref_table->xref = new_xrefs;
49
20.4k
    ctx->xref_table->xref_size = new_size;
50
20.4k
    return 0;
51
20.4k
}
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
15.3k
{
55
15.3k
    uint i, j;
56
15.3k
    uint64_t field_width = 0;
57
15.3k
    uint32_t type = 0;
58
15.3k
    uint64_t objnum = 0, gen = 0;
59
15.3k
    byte *Buffer;
60
15.3k
    int64_t bytes = 0;
61
15.3k
    xref_entry *entry;
62
63
    /* Find max number of bytes to be read */
64
15.3k
    field_width = W[0];
65
15.3k
    if (W[1] > field_width)
66
15.3k
        field_width = W[1];
67
15.3k
    if (W[2] > field_width)
68
19
        field_width = W[2];
69
70
15.3k
    Buffer = gs_alloc_bytes(ctx->memory, field_width, "read_xref_stream_entry working buffer");
71
15.3k
    if (Buffer == NULL)
72
0
        return_error(gs_error_VMerror);
73
74
626k
    for (i=first;i<=last; i++){
75
        /* Defaults if W[n] = 0 */
76
611k
        type = 1;
77
611k
        objnum = gen = 0;
78
79
611k
        if (W[0] != 0) {
80
607k
            type = 0;
81
607k
            bytes = pdfi_read_bytes(ctx, Buffer, 1, W[0], s);
82
607k
            if (bytes < W[0]){
83
133
                gs_free_object(ctx->memory, Buffer, "read_xref_stream_entry, free working buffer (error)");
84
133
                return_error(gs_error_ioerror);
85
133
            }
86
1.21M
            for (j=0;j<W[0];j++)
87
606k
                type = (type << 8) + Buffer[j];
88
606k
        }
89
90
611k
        if (W[1] != 0) {
91
611k
            bytes = pdfi_read_bytes(ctx, Buffer, 1, W[1], s);
92
611k
            if (bytes < W[1]){
93
36
                gs_free_object(ctx->memory, Buffer, "read_xref_stream_entry free working buffer (error)");
94
36
                return_error(gs_error_ioerror);
95
36
            }
96
2.09M
            for (j=0;j<W[1];j++)
97
1.48M
                objnum = (objnum << 8) + Buffer[j];
98
611k
        }
99
100
611k
        if (W[2] != 0) {
101
596k
            bytes = pdfi_read_bytes(ctx, Buffer, 1, W[2], s);
102
596k
            if (bytes < W[2]){
103
38
                gs_free_object(ctx->memory, Buffer, "read_xref_stream_entry, free working buffer (error)");
104
38
                return_error(gs_error_ioerror);
105
38
            }
106
1.20M
            for (j=0;j<W[2];j++)
107
610k
                gen = (gen << 8) + Buffer[j];
108
596k
        }
109
110
611k
        entry = &ctx->xref_table->xref[i];
111
611k
        if (entry->object_num != 0 && !entry->free)
112
3.95k
            continue;
113
114
607k
        entry->compressed = false;
115
607k
        entry->free = false;
116
607k
        entry->object_num = i;
117
607k
        entry->cache = NULL;
118
119
607k
        switch(type) {
120
15.6k
            case 0:
121
15.6k
                entry->free = true;
122
15.6k
                entry->u.uncompressed.offset = objnum;         /* For free objects we use the offset to store the object number of the next free object */
123
15.6k
                entry->u.uncompressed.generation_num = gen;    /* And the generation number is the numebr to use if this object is used again */
124
15.6k
                break;
125
206k
            case 1:
126
206k
                entry->u.uncompressed.offset = objnum;
127
206k
                entry->u.uncompressed.generation_num = gen;
128
206k
                break;
129
384k
            case 2:
130
384k
                entry->compressed = true;
131
384k
                entry->u.compressed.compressed_stream_num = objnum;   /* The object number of the compressed stream */
132
384k
                entry->u.compressed.object_index = gen;               /* And the index of the object within the stream */
133
384k
                break;
134
156
            default:
135
156
                gs_free_object(ctx->memory, Buffer, "read_xref_stream_entry, free working buffer");
136
156
                return_error(gs_error_rangecheck);
137
0
                break;
138
607k
        }
139
607k
    }
140
15.0k
    gs_free_object(ctx->memory, Buffer, "read_xref_stream_entry, free working buffer");
141
15.0k
    return 0;
142
15.3k
}
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
12.3k
{
152
12.3k
    pdf_c_stream *XRefStrm;
153
12.3k
    int code, i;
154
12.3k
    pdf_dict *sdict = NULL;
155
12.3k
    pdf_name *n;
156
12.3k
    pdf_array *a;
157
12.3k
    int64_t size;
158
12.3k
    int64_t num;
159
12.3k
    int64_t W[3] = {0, 0, 0};
160
12.3k
    int objnum;
161
12.3k
    bool known = false;
162
163
12.3k
    if (pdfi_type_of(stream_obj) != PDF_STREAM)
164
0
        return_error(gs_error_typecheck);
165
166
12.3k
    code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream_obj, &sdict);
167
12.3k
    if (code < 0)
168
0
        return code;
169
170
12.3k
    code = pdfi_dict_get_type(ctx, sdict, "Type", PDF_NAME, (pdf_obj **)&n);
171
12.3k
    if (code < 0)
172
59
        return code;
173
174
12.3k
    if (n->length != 4 || memcmp(n->data, "XRef", 4) != 0) {
175
26
        pdfi_countdown(n);
176
26
        return_error(gs_error_syntaxerror);
177
26
    }
178
12.2k
    pdfi_countdown(n);
179
180
12.2k
    code = pdfi_dict_get_int(ctx, sdict, "Size", &size);
181
12.2k
    if (code < 0)
182
12
        return code;
183
12.2k
    if (size < 1)
184
12
        return 0;
185
186
12.2k
    if (size < 0 || size > floor((double)ARCH_MAX_SIZE_T / (double)sizeof(xref_entry)))
187
0
        return_error(gs_error_rangecheck);
188
189
    /* If this is the first xref stream then allocate the xref table and store the trailer */
190
12.2k
    if (ctx->xref_table == NULL) {
191
7.92k
        ctx->xref_table = (xref_table_t *)gs_alloc_bytes(ctx->memory, sizeof(xref_table_t), "read_xref_stream allocate xref table");
192
7.92k
        if (ctx->xref_table == NULL) {
193
0
            return_error(gs_error_VMerror);
194
0
        }
195
7.92k
        memset(ctx->xref_table, 0x00, sizeof(xref_table_t));
196
7.92k
        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
7.92k
        if (ctx->xref_table->xref == NULL){
198
5
            gs_free_object(ctx->memory, ctx->xref_table, "failed to allocate xref table entries");
199
5
            ctx->xref_table = NULL;
200
5
            return_error(gs_error_VMerror);
201
5
        }
202
7.91k
        memset(ctx->xref_table->xref, 0x00, size * sizeof(xref_entry));
203
7.91k
        ctx->xref_table->ctx = ctx;
204
7.91k
        ctx->xref_table->type = PDF_XREF_TABLE;
205
7.91k
        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
7.91k
        pdfi_countup(ctx->xref_table);
211
212
7.91k
        pdfi_countdown(ctx->Trailer);
213
214
7.91k
        ctx->Trailer = sdict;
215
7.91k
        pdfi_countup(sdict);
216
7.91k
    } else {
217
4.34k
        if (size > ctx->xref_table->xref_size)
218
3
            return_error(gs_error_rangecheck);
219
220
4.34k
        code = pdfi_merge_dicts(ctx, ctx->Trailer, sdict);
221
4.34k
        if (code < 0 && (code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BADXREF, "pdfi_process_xref_stream", NULL)) < 0) {
222
0
            goto exit;
223
0
        }
224
4.34k
    }
225
226
12.2k
    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
12.2k
    code = pdfi_dict_known(ctx, sdict, "DecodeParms", &known);
236
12.2k
    if (code < 0)
237
0
        return code;
238
239
12.2k
    if (known) {
240
11.2k
        pdf_dict *DP;
241
11.2k
        double f;
242
11.2k
        pdf_obj *name;
243
244
11.2k
        code = pdfi_dict_get_type(ctx, sdict, "DecodeParms", PDF_DICT, (pdf_obj **)&DP);
245
11.2k
        if (code < 0)
246
2
            return code;
247
248
11.2k
        code = pdfi_dict_knownget_number(ctx, DP, "Colors", &f);
249
11.2k
        if (code < 0) {
250
0
            pdfi_countdown(DP);
251
0
            return code;
252
0
        }
253
11.2k
        if (code > 0 && f != (double)1)
254
0
        {
255
0
            code = pdfi_name_alloc(ctx, (byte *)"Colors", 6, &name);
256
0
            if (code < 0) {
257
0
                pdfi_countdown(DP);
258
0
                return code;
259
0
            }
260
0
            pdfi_countup(name);
261
262
0
            code = pdfi_dict_delete_pair(ctx, DP, (pdf_name *)name);
263
0
            pdfi_countdown(name);
264
0
            if (code < 0) {
265
0
                pdfi_countdown(DP);
266
0
                return code;
267
0
            }
268
0
        }
269
11.2k
        pdfi_countdown(DP);
270
11.2k
    }
271
272
12.2k
    code = pdfi_filter_no_decryption(ctx, stream_obj, s, &XRefStrm, false);
273
12.2k
    if (code < 0) {
274
52
        pdfi_countdown(ctx->xref_table);
275
52
        ctx->xref_table = NULL;
276
52
        return code;
277
52
    }
278
279
12.2k
    code = pdfi_dict_get_type(ctx, sdict, "W", PDF_ARRAY, (pdf_obj **)&a);
280
12.2k
    if (code < 0) {
281
11
        pdfi_close_file(ctx, XRefStrm);
282
11
        pdfi_countdown(ctx->xref_table);
283
11
        ctx->xref_table = NULL;
284
11
        return code;
285
11
    }
286
287
12.1k
    if (pdfi_array_size(a) != 3) {
288
11
        pdfi_countdown(a);
289
11
        pdfi_close_file(ctx, XRefStrm);
290
11
        pdfi_countdown(ctx->xref_table);
291
11
        ctx->xref_table = NULL;
292
11
        return_error(gs_error_rangecheck);
293
11
    }
294
48.6k
    for (i=0;i<3;i++) {
295
36.4k
        code = pdfi_array_get_int(ctx, a, (uint64_t)i, (int64_t *)&W[i]);
296
36.4k
        if (code < 0 || W[i] < 0) {
297
44
            pdfi_countdown(a);
298
44
            pdfi_close_file(ctx, XRefStrm);
299
44
            pdfi_countdown(ctx->xref_table);
300
44
            ctx->xref_table = NULL;
301
44
            if (W[i] < 0)
302
14
                code = gs_note_error(gs_error_rangecheck);
303
44
            return code;
304
44
        }
305
36.4k
    }
306
12.1k
    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
12.1k
    if (W[0] > 1 || W[1] > 8 || W[2] > 8) {
320
35
        pdfi_close_file(ctx, XRefStrm);
321
35
        pdfi_countdown(ctx->xref_table);
322
35
        ctx->xref_table = NULL;
323
35
        return code;
324
35
    }
325
326
12.1k
    code = pdfi_dict_get_type(ctx, sdict, "Index", PDF_ARRAY, (pdf_obj **)&a);
327
12.1k
    if (code == gs_error_undefined) {
328
4.21k
        code = read_xref_stream_entries(ctx, XRefStrm, 0, size - 1, W);
329
4.21k
        if (code < 0) {
330
119
            pdfi_close_file(ctx, XRefStrm);
331
119
            pdfi_countdown(ctx->xref_table);
332
119
            ctx->xref_table = NULL;
333
119
            return code;
334
119
        }
335
7.88k
    } else {
336
7.88k
        int64_t start, size;
337
338
7.88k
        if (code < 0) {
339
3
            pdfi_close_file(ctx, XRefStrm);
340
3
            pdfi_countdown(ctx->xref_table);
341
3
            ctx->xref_table = NULL;
342
3
            return code;
343
3
        }
344
345
7.88k
        if (pdfi_array_size(a) & 1) {
346
12
            pdfi_countdown(a);
347
12
            pdfi_close_file(ctx, XRefStrm);
348
12
            pdfi_countdown(ctx->xref_table);
349
12
            ctx->xref_table = NULL;
350
12
            return_error(gs_error_rangecheck);
351
12
        }
352
353
18.8k
        for (i=0;i < pdfi_array_size(a);i+=2){
354
11.2k
            code = pdfi_array_get_int(ctx, a, (uint64_t)i, &start);
355
11.2k
            if (code < 0 || start < 0) {
356
19
                pdfi_countdown(a);
357
19
                pdfi_close_file(ctx, XRefStrm);
358
19
                pdfi_countdown(ctx->xref_table);
359
19
                ctx->xref_table = NULL;
360
19
                return code;
361
19
            }
362
363
11.1k
            code = pdfi_array_get_int(ctx, a, (uint64_t)i+1, &size);
364
11.1k
            if (code < 0) {
365
14
                pdfi_countdown(a);
366
14
                pdfi_close_file(ctx, XRefStrm);
367
14
                pdfi_countdown(ctx->xref_table);
368
14
                ctx->xref_table = NULL;
369
14
                return code;
370
14
            }
371
372
11.1k
            if (size < 1)
373
12
                continue;
374
375
11.1k
            if (start + size >= ctx->xref_table->xref_size) {
376
7.19k
                code = resize_xref(ctx, start + size);
377
7.19k
                if (code < 0) {
378
6
                    pdfi_countdown(a);
379
6
                    pdfi_close_file(ctx, XRefStrm);
380
6
                    pdfi_countdown(ctx->xref_table);
381
6
                    ctx->xref_table = NULL;
382
6
                    return code;
383
6
                }
384
7.19k
            }
385
386
11.1k
            code = read_xref_stream_entries(ctx, XRefStrm, start, start + size - 1, W);
387
11.1k
            if (code < 0) {
388
244
                pdfi_countdown(a);
389
244
                pdfi_close_file(ctx, XRefStrm);
390
244
                pdfi_countdown(ctx->xref_table);
391
244
                ctx->xref_table = NULL;
392
244
                return code;
393
244
            }
394
11.1k
        }
395
7.87k
    }
396
11.6k
    pdfi_countdown(a);
397
398
11.6k
    pdfi_close_file(ctx, XRefStrm);
399
400
11.6k
    code = pdfi_dict_get_int(ctx, sdict, "Prev", &num);
401
11.6k
    if (code == gs_error_undefined)
402
4.66k
        return 0;
403
404
7.02k
    if (code < 0)
405
14
        return code;
406
407
7.00k
    if (num < 0 || num > ctx->main_stream_length)
408
2.24k
        return_error(gs_error_rangecheck);
409
410
4.76k
    if (pdfi_loop_detector_check_object(ctx, num) == true)
411
6
        return_error(gs_error_circular_reference);
412
4.76k
    else {
413
4.76k
        code = pdfi_loop_detector_add_object(ctx, num);
414
4.76k
        if (code < 0)
415
0
            return code;
416
4.76k
    }
417
418
4.76k
    if(ctx->args.pdfdebug)
419
0
        outprintf(ctx->memory, "%% Reading /Prev xref\n");
420
421
4.76k
    pdfi_seek(ctx, s, num, SEEK_SET);
422
423
4.76k
    code = pdfi_read_bare_int(ctx, ctx->main_stream, &objnum);
424
4.76k
    if (code == 1) {
425
4.41k
        if (pdfi_check_xref_stream(ctx))
426
4.37k
            return pdfi_read_xref_stream_dict(ctx, s, objnum);
427
4.41k
    }
428
429
388
    code = pdfi_read_bare_keyword(ctx, ctx->main_stream);
430
388
    if (code < 0)
431
0
        return code;
432
388
    if (code == TOKEN_XREF) {
433
25
        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
0
            goto exit;
435
0
        }
436
        /* Read old-style xref table */
437
25
        return(read_xref(ctx, ctx->main_stream));
438
25
    }
439
363
exit:
440
363
    return_error(gs_error_syntaxerror);
441
388
}
442
443
static int pdfi_check_xref_stream(pdf_context *ctx)
444
15.9k
{
445
15.9k
    gs_offset_t offset;
446
15.9k
    int gen_num, code = 0;
447
448
15.9k
    offset = pdfi_unread_tell(ctx);
449
450
15.9k
    code = pdfi_read_bare_int(ctx, ctx->main_stream, &gen_num);
451
15.9k
    if (code <= 0) {
452
1.19k
        code = 0;
453
1.19k
        goto exit;
454
1.19k
    }
455
456
    /* Try to read 'obj' */
457
14.7k
    code = pdfi_read_bare_keyword(ctx, ctx->main_stream);
458
14.7k
    if (code <= 0) {
459
0
        code = 0;
460
0
        goto exit;
461
0
    }
462
463
    /* Third element must be obj, or it's not a valid xref */
464
14.7k
    if (code != TOKEN_OBJ)
465
1.80k
        code = 0;
466
12.9k
    else
467
12.9k
        code = 1;
468
469
15.9k
exit:
470
15.9k
    pdfi_seek(ctx, ctx->main_stream, offset, SEEK_SET);
471
15.9k
    return code;
472
14.7k
}
473
474
static int pdfi_read_xref_stream_dict(pdf_context *ctx, pdf_c_stream *s, int obj_num)
475
13.0k
{
476
13.0k
    int code;
477
13.0k
    int gen_num;
478
479
13.0k
    if (ctx->args.pdfdebug)
480
0
        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
13.0k
    code = pdfi_read_bare_int(ctx, ctx->main_stream, &gen_num);
484
13.0k
    if (code <= 0) {
485
1
        if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BADXREFSTREAM, "pdfi_read_xref_stream_dict", "")) < 0) {
486
0
            return code;
487
0
        }
488
1
        return(pdfi_repair_file(ctx));
489
1
    }
490
491
    /* Try to read 'obj' */
492
13.0k
    code = pdfi_read_bare_keyword(ctx, ctx->main_stream);
493
13.0k
    if (code < 0)
494
0
        return code;
495
13.0k
    if (code == 0)
496
0
        return_error(gs_error_syntaxerror);
497
498
    /* Third element must be obj, or it's not a valid xref */
499
13.0k
    if (code != TOKEN_OBJ) {
500
0
        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
0
            return code;
502
0
        }
503
0
        return(pdfi_repair_file(ctx));
504
0
    }
505
506
537k
    do {
507
537k
        code = pdfi_read_token(ctx, ctx->main_stream, obj_num, gen_num);
508
537k
        if (code <= 0) {
509
506
            if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BADXREFSTREAM, "pdfi_read_xref_stream_dict", NULL)) < 0) {
510
0
                return code;
511
0
            }
512
506
            return pdfi_repair_file(ctx);
513
506
        }
514
515
536k
        if (pdfi_count_stack(ctx) >= 2 && pdfi_type_of(ctx->stack_top[-1]) == PDF_FAST_KEYWORD) {
516
14.8k
            uintptr_t keyword = (uintptr_t)ctx->stack_top[-1];
517
14.8k
            if (keyword == TOKEN_STREAM) {
518
12.3k
                pdf_dict *dict;
519
12.3k
                pdf_stream *sdict = NULL;
520
12.3k
                int64_t Length;
521
522
                /* Remove the 'stream' token from the stack, should leave a dictionary object on the stack */
523
12.3k
                pdfi_pop(ctx, 1);
524
12.3k
                if (pdfi_type_of(ctx->stack_top[-1]) != PDF_DICT) {
525
24
                    if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BADXREFSTREAM, "pdfi_read_xref_stream_dict", NULL)) < 0) {
526
0
                        return code;
527
0
                    }
528
24
                    return pdfi_repair_file(ctx);
529
24
                }
530
12.3k
                dict = (pdf_dict *)ctx->stack_top[-1];
531
532
                /* Convert the dict into a stream (sdict comes back with at least one ref) */
533
12.3k
                code = pdfi_obj_dict_to_stream(ctx, dict, &sdict, true);
534
                /* Pop off the dict */
535
12.3k
                pdfi_pop(ctx, 1);
536
12.3k
                if (code < 0) {
537
0
                    if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BADXREFSTREAM, "pdfi_read_xref_stream_dict", NULL)) < 0) {
538
0
                        return code;
539
0
                    }
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
0
                    return pdfi_repair_file(ctx);
545
0
                }
546
12.3k
                dict = NULL;
547
548
                /* Init the stuff for the stream */
549
12.3k
                sdict->stream_offset = pdfi_unread_tell(ctx);
550
12.3k
                sdict->object_num = obj_num;
551
12.3k
                sdict->generation_num = gen_num;
552
553
12.3k
                code = pdfi_dict_get_int(ctx, sdict->stream_dict, "Length", &Length);
554
12.3k
                if (code < 0) {
555
                    /* TODO: Not positive this will actually have a length -- just use 0 */
556
56
                    (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
56
                    code = 0;
558
56
                    Length = 0;
559
56
                }
560
12.3k
                sdict->Length = Length;
561
12.3k
                sdict->length_valid = true;
562
563
12.3k
                code = pdfi_process_xref_stream(ctx, sdict, ctx->main_stream);
564
12.3k
                pdfi_countdown(sdict);
565
12.3k
                if (code < 0) {
566
3.45k
                    pdfi_set_error(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_PREV_NOT_XREF_STREAM, "pdfi_read_xref_stream_dict", NULL);
567
3.45k
                    return code;
568
3.45k
                }
569
8.91k
                break;
570
12.3k
            } else if (keyword == TOKEN_ENDOBJ) {
571
                /* Something went wrong, this is not a stream dictionary */
572
164
                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
0
                    return code;
574
0
                }
575
164
                return(pdfi_repair_file(ctx));
576
164
            }
577
14.8k
        }
578
536k
    } while(1);
579
8.91k
    return 0;
580
13.0k
}
581
582
static int skip_to_digit(pdf_context *ctx, pdf_c_stream *s, unsigned int limit)
583
3.29k
{
584
3.29k
    int c, read = 0;
585
586
12.2k
    do {
587
12.2k
        c = pdfi_read_byte(ctx, s);
588
12.2k
        if (c < 0)
589
0
            return_error(gs_error_ioerror);
590
12.2k
        if (c >= '0' && c <= '9') {
591
2.97k
            pdfi_unread_byte(ctx, s, (byte)c);
592
2.97k
            return read;
593
2.97k
        }
594
9.31k
        read++;
595
9.31k
    } while (read < limit);
596
597
320
    return read;
598
3.29k
}
599
600
static int read_digits(pdf_context *ctx, pdf_c_stream *s, byte *Buffer, int limit)
601
3.29k
{
602
3.29k
    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
3.29k
    if (limit <= 0)
609
327
        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
17.7k
    do {
615
17.7k
        c = pdfi_read_byte(ctx, s);
616
17.7k
        if (c < 0)
617
0
            return_error(gs_error_ioerror);
618
17.7k
        if (c < '0' || c > '9') {
619
1.30k
            pdfi_unread_byte(ctx, s, c);
620
1.30k
            break;
621
1.30k
        }
622
16.4k
        *Buffer++ = (byte)c;
623
16.4k
        read++;
624
16.4k
    } while (read < limit);
625
2.96k
    *Buffer = 0;
626
627
2.96k
    return read;
628
2.96k
}
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
1.67k
{
633
1.67k
    byte Buffer[20];
634
1.67k
    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
1.67k
    code = skip_to_digit(ctx, s, 20);
638
1.67k
    if (code < 0)
639
0
        return code;
640
1.67k
    read += code;
641
642
    /* Now read a number */
643
1.67k
    code = read_digits(ctx, s, (byte *)&Buffer,  (read > 10 ? 20 - read : 10));
644
1.67k
    if (code < 0)
645
55
        return code;
646
1.61k
    read += code;
647
648
1.61k
    *offset = atol((const char *)Buffer);
649
650
    /* find next number */
651
1.61k
    code = skip_to_digit(ctx, s, 20 - read);
652
1.61k
    if (code < 0)
653
0
        return code;
654
1.61k
    read += code;
655
656
    /* and read it */
657
1.61k
    code = read_digits(ctx, s, (byte *)&Buffer, (read > 15 ? 20 - read : 5));
658
1.61k
    if (code < 0)
659
272
        return code;
660
1.34k
    read += code;
661
662
1.34k
    *generation_num = atol((const char *)Buffer);
663
664
2.32k
    do {
665
2.32k
        c = pdfi_read_byte(ctx, s);
666
2.32k
        if (c < 0)
667
0
            return_error(gs_error_ioerror);
668
2.32k
        read ++;
669
2.32k
        if (c == 0x09 || c == 0x20)
670
1.00k
            continue;
671
1.31k
        if (c == 'n' || c == 'f') {
672
769
            *free = (unsigned char)c;
673
769
            break;
674
769
        } else {
675
549
            return_error(gs_error_syntaxerror);
676
549
        }
677
1.31k
    } while (read < 20);
678
797
    if (read >= 20)
679
37
        return_error(gs_error_syntaxerror);
680
681
1.97k
    do {
682
1.97k
        c = pdfi_read_byte(ctx, s);
683
1.97k
        if (c < 0)
684
0
            return_error(gs_error_syntaxerror);
685
1.97k
        read++;
686
1.97k
        if (c == 0x20 || c == 0x09 || c == 0x0d || c == 0x0a)
687
955
            continue;
688
1.97k
    } while (read < 20);
689
760
    return 0;
690
760
}
691
692
static int write_offset(byte *B, gs_offset_t o, unsigned int g, unsigned char free)
693
760
{
694
760
    byte b[20], *ptr = B;
695
760
    int index = 0;
696
697
760
    gs_snprintf((char *)b, sizeof(b), "%"PRIdOFFSET"", o);
698
760
    if (strlen((const char *)b) > 10)
699
0
        return_error(gs_error_rangecheck);
700
6.25k
    for(index=0;index < 10 - strlen((const char *)b); index++) {
701
5.49k
        *ptr++ = 0x30;
702
5.49k
    }
703
760
    memcpy(ptr, b, strlen((const char *)b));
704
760
    ptr += strlen((const char *)b);
705
760
    *ptr++ = 0x20;
706
707
760
    gs_snprintf((char *)b, sizeof(b), "%d", g);
708
760
    if (strlen((const char *)b) > 5)
709
0
        return_error(gs_error_rangecheck);
710
3.45k
    for(index=0;index < 5 - strlen((const char *)b);index++) {
711
2.69k
        *ptr++ = 0x30;
712
2.69k
    }
713
760
    memcpy(ptr, b, strlen((const char *)b));
714
760
    ptr += strlen((const char *)b);
715
760
    *ptr++ = 0x20;
716
760
    *ptr++ = free;
717
760
    *ptr++ = 0x20;
718
760
    *ptr++ = 0x0d;
719
760
    return 0;
720
760
}
721
722
static int read_xref_section(pdf_context *ctx, pdf_c_stream *s, uint64_t *section_start, uint64_t *section_size)
723
37.0k
{
724
37.0k
    int code = 0, i, j;
725
37.0k
    int start = 0;
726
37.0k
    int size = 0;
727
37.0k
    int64_t bytes = 0;
728
37.0k
    char Buffer[21];
729
730
37.0k
    *section_start = *section_size = 0;
731
732
37.0k
    if (ctx->args.pdfdebug)
733
0
        outprintf(ctx->memory, "\n%% Reading xref section\n");
734
735
37.0k
    code = pdfi_read_bare_int(ctx, ctx->main_stream, &start);
736
37.0k
    if (code < 0) {
737
        /* Not an int, might be a keyword */
738
9.85k
        code = pdfi_read_bare_keyword(ctx, ctx->main_stream);
739
9.85k
        if (code < 0)
740
0
            return code;
741
742
9.85k
        if (code != TOKEN_TRAILER) {
743
            /* element is not an integer, and not a keyword - not a valid xref */
744
149
            return_error(gs_error_typecheck);
745
149
        }
746
9.70k
        return 1;
747
9.85k
    }
748
749
27.2k
    if (start < 0)
750
20
        return_error(gs_error_rangecheck);
751
752
27.1k
    *section_start = start;
753
754
27.1k
    code = pdfi_read_bare_int(ctx, ctx->main_stream, &size);
755
27.1k
    if (code < 0)
756
23
        return code;
757
27.1k
    if (code == 0)
758
54
        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
27.1k
    if (size < 0)
763
11
        return_error(gs_error_rangecheck);
764
765
27.0k
    *section_size = size;
766
767
27.0k
    if (ctx->args.pdfdebug)
768
0
        outprintf(ctx->memory, "\n%% Section starts at %d and has %d entries\n", (unsigned int) start, (unsigned int)size);
769
770
27.0k
    if (size > 0) {
771
26.6k
        if (ctx->xref_table == NULL) {
772
9.68k
            ctx->xref_table = (xref_table_t *)gs_alloc_bytes(ctx->memory, sizeof(xref_table_t), "read_xref_stream allocate xref table");
773
9.68k
            if (ctx->xref_table == NULL)
774
0
                return_error(gs_error_VMerror);
775
9.68k
            memset(ctx->xref_table, 0x00, sizeof(xref_table_t));
776
777
9.68k
            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
9.68k
            if (ctx->xref_table->xref == NULL){
779
32
                gs_free_object(ctx->memory, ctx->xref_table, "free xref table on error allocating entries");
780
32
                ctx->xref_table = NULL;
781
32
                return_error(gs_error_VMerror);
782
32
            }
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
9.65k
            memset(ctx->xref_table->xref, 0x00, (start + size) * sizeof(xref_entry));
789
9.65k
            ctx->xref_table->ctx = ctx;
790
9.65k
            ctx->xref_table->type = PDF_XREF_TABLE;
791
9.65k
            ctx->xref_table->xref_size = start + size;
792
9.65k
            pdfi_countup(ctx->xref_table);
793
16.9k
        } else {
794
16.9k
            if (start + size > ctx->xref_table->xref_size) {
795
13.2k
                code = resize_xref(ctx, start + size);
796
13.2k
                if (code < 0)
797
16
                    return code;
798
13.2k
            }
799
16.9k
        }
800
26.6k
    }
801
802
27.0k
    pdfi_skip_white(ctx, s);
803
335k
    for (i=0;i< size;i++){
804
309k
        xref_entry *entry = &ctx->xref_table->xref[i + start];
805
309k
        unsigned char free;
806
309k
        gs_offset_t off;
807
309k
        unsigned int gen;
808
809
309k
        bytes = pdfi_read_bytes(ctx, (byte *)Buffer, 1, 20, s);
810
309k
        if (bytes < 20)
811
6
            return_error(gs_error_ioerror);
812
309k
        j = 19;
813
309k
        if ((Buffer[19] != 0x0a && Buffer[19] != 0x0d) || (Buffer[18] != 0x0d && Buffer[18] != 0x0a && Buffer[18] != 0x20))
814
15.2k
            pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_XREF_ENTRY_SIZE, "read_xref_section", NULL);
815
332k
        while (Buffer[j] != 0x0D && Buffer[j] != 0x0A) {
816
24.1k
            pdfi_unread_byte(ctx, s, (byte)Buffer[j]);
817
24.1k
            if (--j < 0) {
818
783
                pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_XREF_ENTRY_NO_EOL, "read_xref_section", NULL);
819
783
                outprintf(ctx->memory, "Invalid xref entry, line terminator missing.\n");
820
783
                code = read_xref_entry_slow(ctx, s, &off, &gen, &free);
821
783
                if (code < 0)
822
395
                    return code;
823
388
                code = write_offset((byte *)Buffer, off, gen, free);
824
388
                if (code < 0)
825
0
                    return code;
826
388
                j = 19;
827
388
                break;
828
388
            }
829
24.1k
        }
830
308k
        Buffer[j] = 0x00;
831
308k
        if (entry->object_num != 0)
832
3.82k
            continue;
833
834
304k
        if (sscanf(Buffer, "%"PRIdOFFSET" %d %c", &entry->u.uncompressed.offset, &entry->u.uncompressed.generation_num, &free) != 3) {
835
890
            pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_XREF_ENTRY_FORMAT, "read_xref_section", NULL);
836
890
            outprintf(ctx->memory, "Invalid xref entry, incorrect format.\n");
837
890
            pdfi_unread(ctx, s, (byte *)Buffer, 20);
838
890
            code = read_xref_entry_slow(ctx, s, &off, &gen, &free);
839
890
            if (code < 0)
840
518
                return code;
841
372
            code = write_offset((byte *)Buffer, off, gen, free);
842
372
            if (code < 0)
843
0
                return code;
844
372
        }
845
846
304k
        entry->compressed = false;
847
304k
        entry->object_num = i + start;
848
304k
        if (free == 'f')
849
72.1k
            entry->free = true;
850
304k
        if(free == 'n')
851
231k
            entry->free = false;
852
304k
        if (entry->object_num == 0) {
853
6.45k
            if (!entry->free) {
854
77
                pdfi_set_warning(ctx, 0, NULL, W_PDF_XREF_OBJECT0_NOT_FREE, "read_xref_section", NULL);
855
77
            }
856
6.45k
        }
857
304k
    }
858
859
26.1k
    return 0;
860
27.0k
}
861
862
static int read_xref(pdf_context *ctx, pdf_c_stream *s)
863
10.9k
{
864
10.9k
    int code = 0;
865
10.9k
    pdf_dict *d = NULL;
866
10.9k
    uint64_t max_obj = 0;
867
10.9k
    int64_t num, XRefStm = 0;
868
10.9k
    int obj_num;
869
10.9k
    bool known = false;
870
871
10.9k
    if (ctx->repaired)
872
2
        return 0;
873
874
37.0k
    do {
875
37.0k
        uint64_t section_start, section_size;
876
877
37.0k
        code = read_xref_section(ctx, s, &section_start, &section_size);
878
37.0k
        if (code < 0)
879
1.22k
            return code;
880
881
35.8k
        if (section_size > 0 && section_start + section_size - 1 > max_obj)
882
23.0k
            max_obj = section_start + section_size - 1;
883
884
        /* code == 1 => read_xref_section ended with a trailer. */
885
35.8k
    } while (code != 1);
886
887
9.70k
    code = pdfi_read_dict(ctx, ctx->main_stream, 0, 0);
888
9.70k
    if (code < 0)
889
193
        return code;
890
891
9.51k
    d = (pdf_dict *)ctx->stack_top[-1];
892
9.51k
    if (pdfi_type_of(d) != PDF_DICT) {
893
16
        pdfi_pop(ctx, 1);
894
16
        return_error(gs_error_typecheck);
895
16
    }
896
9.49k
    pdfi_countup(d);
897
9.49k
    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
9.49k
    code = pdfi_dict_get_int(ctx, d, "XRefStm", &XRefStm);
905
9.49k
    if (code < 0 && code != gs_error_undefined)
906
2
        goto error;
907
908
9.49k
    if (code == 0) {
909
257
        code = pdfi_dict_delete(ctx, d, "XRefStm");
910
257
        if (code < 0)
911
0
            goto error;
912
257
    }
913
914
9.49k
    if (ctx->Trailer == NULL) {
915
8.45k
        ctx->Trailer = d;
916
8.45k
        pdfi_countup(d);
917
8.45k
    } else {
918
1.04k
        code = pdfi_merge_dicts(ctx, ctx->Trailer, d);
919
1.04k
        if (code < 0) {
920
0
            if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BADXREF, "read_xref", "")) < 0) {
921
0
                return code;
922
0
            }
923
0
        }
924
1.04k
    }
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
9.49k
    code = pdfi_dict_get_int(ctx, d, "Size", &num);
930
9.49k
    if (code < 0)
931
17
        goto error;
932
933
9.47k
    if (max_obj >= num)
934
583
        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
9.47k
    code = pdfi_dict_known(ctx, d, "Prev", &known);
940
9.47k
    if (known) {
941
4.17k
        code = pdfi_dict_get_int(ctx, d, "Prev", &num);
942
4.17k
        if (code < 0)
943
16
            goto error;
944
945
4.16k
        if (num < 0 || num > ctx->main_stream_length) {
946
1.50k
            code = gs_note_error(gs_error_rangecheck);
947
1.50k
            goto error;
948
1.50k
        }
949
950
2.66k
        if (pdfi_loop_detector_check_object(ctx, num) == true) {
951
5
            code = gs_note_error(gs_error_circular_reference);
952
5
            goto error;
953
5
        }
954
2.65k
        else {
955
2.65k
            code = pdfi_loop_detector_add_object(ctx, num);
956
2.65k
            if (code < 0)
957
0
                goto error;
958
2.65k
        }
959
960
2.65k
        code = pdfi_seek(ctx, s, num, SEEK_SET);
961
2.65k
        if (code < 0)
962
0
            goto error;
963
964
2.65k
        if (!ctx->repaired) {
965
2.65k
            code = pdfi_read_token(ctx, ctx->main_stream, 0, 0);
966
2.65k
            if (code < 0)
967
116
                goto error;
968
969
2.54k
            if (code == 0) {
970
3
                code = gs_note_error(gs_error_syntaxerror);
971
3
                goto error;
972
3
            }
973
2.54k
        } else {
974
0
            code = 0;
975
0
            goto error;
976
0
        }
977
978
2.53k
        if ((intptr_t)(ctx->stack_top[-1]) == (intptr_t)TOKEN_XREF) {
979
            /* Read old-style xref table */
980
1.12k
            pdfi_pop(ctx, 1);
981
1.12k
            code = read_xref(ctx, ctx->main_stream);
982
1.12k
            if (code < 0)
983
184
                goto error;
984
1.41k
        } else {
985
1.41k
            pdfi_pop(ctx, 1);
986
1.41k
            code = gs_note_error(gs_error_typecheck);
987
1.41k
            goto error;
988
1.41k
        }
989
2.53k
    }
990
991
    /* Now check if this is a hybrid file. */
992
6.23k
    if (XRefStm != 0) {
993
149
        ctx->is_hybrid = true;
994
995
149
        if (ctx->args.pdfdebug)
996
0
            outprintf(ctx->memory, "%% File is a hybrid, containing xref table and xref stream. Reading the stream.\n");
997
998
999
149
        if (pdfi_loop_detector_check_object(ctx, XRefStm) == true) {
1000
0
            code = gs_note_error(gs_error_circular_reference);
1001
0
            goto error;
1002
0
        }
1003
149
        else {
1004
149
            code = pdfi_loop_detector_add_object(ctx, XRefStm);
1005
149
            if (code < 0)
1006
0
                goto error;
1007
149
        }
1008
1009
149
        code = pdfi_loop_detector_mark(ctx);
1010
149
        if (code < 0)
1011
0
            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
149
        pdfi_seek(ctx, s, XRefStm, SEEK_SET);
1018
1019
149
        code = pdfi_read_bare_int(ctx, ctx->main_stream, &obj_num);
1020
149
        if (code < 0) {
1021
0
            pdfi_set_error(ctx, 0, NULL, E_PDF_BADXREFSTREAM, "read_xref", "");
1022
0
            pdfi_loop_detector_cleartomark(ctx);
1023
0
            goto error;
1024
0
        }
1025
1026
149
        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
149
        if (code < 0) {
1029
8
            pdfi_loop_detector_cleartomark(ctx);
1030
8
            goto error;
1031
8
        }
1032
1033
141
        pdfi_loop_detector_cleartomark(ctx);
1034
141
    } else
1035
6.08k
        code = 0;
1036
1037
9.49k
error:
1038
9.49k
    pdfi_countdown(d);
1039
9.49k
    return code;
1040
6.23k
}
1041
1042
int pdfi_read_xref(pdf_context *ctx)
1043
98.9k
{
1044
98.9k
    int code = 0;
1045
98.9k
    int obj_num;
1046
1047
98.9k
    code = pdfi_loop_detector_mark(ctx);
1048
98.9k
    if (code < 0)
1049
0
        return code;
1050
1051
98.9k
    if (ctx->startxref == 0)
1052
57.4k
        goto repair;
1053
1054
41.5k
    code = pdfi_loop_detector_add_object(ctx, ctx->startxref);
1055
41.5k
    if (code < 0)
1056
0
        goto exit;
1057
1058
41.5k
    if (ctx->args.pdfdebug)
1059
0
        outprintf(ctx->memory, "%% Trying to read 'xref' token for xref table, or 'int int obj' for an xref stream\n");
1060
1061
41.5k
    if (ctx->startxref > ctx->main_stream_length - 5) {
1062
10.5k
        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
0
            goto exit;
1064
1065
10.5k
        goto repair;
1066
10.5k
    }
1067
30.9k
    if (ctx->startxref < 0) {
1068
474
        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
0
            goto exit;
1070
1071
474
        goto repair;
1072
474
    }
1073
1074
    /* Read the xref(s) */
1075
30.4k
    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
30.4k
    code = pdfi_read_bare_int(ctx, ctx->main_stream, &obj_num);
1079
30.4k
    if (code == 1) {
1080
11.5k
        if (pdfi_check_xref_stream(ctx)) {
1081
8.54k
            code = pdfi_read_xref_stream_dict(ctx, ctx->main_stream, obj_num);
1082
8.54k
            if (code < 0)
1083
3.31k
                goto repair;
1084
8.54k
        } else
1085
2.96k
            goto repair;
1086
18.9k
    } else {
1087
        /* If not, it had better start 'xref', and be an old-style xref table */
1088
18.9k
        code = pdfi_read_bare_keyword(ctx, ctx->main_stream);
1089
18.9k
        if (code != TOKEN_XREF) {
1090
9.16k
            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
0
                goto exit;
1092
1093
9.16k
            goto repair;
1094
9.16k
        }
1095
1096
9.78k
        code = read_xref(ctx, ctx->main_stream);
1097
9.78k
        if (code < 0)
1098
4.50k
            goto repair;
1099
9.78k
    }
1100
1101
10.5k
    if(ctx->args.pdfdebug && ctx->xref_table) {
1102
0
        int i, j;
1103
0
        xref_entry *entry;
1104
0
        char Buffer[32];
1105
1106
0
        outprintf(ctx->memory, "\n%% Dumping xref table\n");
1107
0
        for (i=0;i < ctx->xref_table->xref_size;i++) {
1108
0
            entry = &ctx->xref_table->xref[i];
1109
0
            if(entry->compressed) {
1110
0
                outprintf(ctx->memory, "*");
1111
0
                gs_snprintf(Buffer, sizeof(Buffer), "%"PRId64"", entry->object_num);
1112
0
                j = 10 - strlen(Buffer);
1113
0
                while(j--) {
1114
0
                    outprintf(ctx->memory, " ");
1115
0
                }
1116
0
                outprintf(ctx->memory, "%s ", Buffer);
1117
1118
0
                gs_snprintf(Buffer, sizeof(Buffer), "%ld", entry->u.compressed.compressed_stream_num);
1119
0
                j = 10 - strlen(Buffer);
1120
0
                while(j--) {
1121
0
                    outprintf(ctx->memory, " ");
1122
0
                }
1123
0
                outprintf(ctx->memory, "%s ", Buffer);
1124
1125
0
                gs_snprintf(Buffer, sizeof(Buffer), "%ld", entry->u.compressed.object_index);
1126
0
                j = 10 - strlen(Buffer);
1127
0
                while(j--) {
1128
0
                    outprintf(ctx->memory, " ");
1129
0
                }
1130
0
                outprintf(ctx->memory, "%s ", Buffer);
1131
0
            }
1132
0
            else {
1133
0
                outprintf(ctx->memory, " ");
1134
1135
0
                gs_snprintf(Buffer, sizeof(Buffer), "%ld", entry->object_num);
1136
0
                j = 10 - strlen(Buffer);
1137
0
                while(j--) {
1138
0
                    outprintf(ctx->memory, " ");
1139
0
                }
1140
0
                outprintf(ctx->memory, "%s ", Buffer);
1141
1142
0
                gs_snprintf(Buffer, sizeof(Buffer), "%"PRIdOFFSET"", entry->u.uncompressed.offset);
1143
0
                j = 10 - strlen(Buffer);
1144
0
                while(j--) {
1145
0
                    outprintf(ctx->memory, " ");
1146
0
                }
1147
0
                outprintf(ctx->memory, "%s ", Buffer);
1148
1149
0
                gs_snprintf(Buffer, sizeof(Buffer), "%ld", entry->u.uncompressed.generation_num);
1150
0
                j = 10 - strlen(Buffer);
1151
0
                while(j--) {
1152
0
                    outprintf(ctx->memory, " ");
1153
0
                }
1154
0
                outprintf(ctx->memory, "%s ", Buffer);
1155
0
            }
1156
0
            if (entry->free)
1157
0
                outprintf(ctx->memory, "f\n");
1158
0
            else
1159
0
                outprintf(ctx->memory, "n\n");
1160
0
        }
1161
0
    }
1162
10.5k
    if (ctx->args.pdfdebug)
1163
0
        outprintf(ctx->memory, "\n");
1164
1165
10.5k
 exit:
1166
10.5k
    (void)pdfi_loop_detector_cleartomark(ctx);
1167
1168
10.5k
    if (code < 0)
1169
0
        return code;
1170
1171
10.5k
    return 0;
1172
1173
88.3k
repair:
1174
88.3k
    (void)pdfi_loop_detector_cleartomark(ctx);
1175
88.3k
    if (!ctx->repaired && !ctx->args.pdfstoponerror)
1176
88.3k
        return(pdfi_repair_file(ctx));
1177
65
    return 0;
1178
88.3k
}