Coverage Report

Created: 2026-04-09 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/pdf/pdf_int.c
Line
Count
Source
1
/* Copyright (C) 2018-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
/* The PDF interpreter written in C */
17
18
#include "pdf_int.h"
19
#include "pdf_file.h"
20
#include "strmio.h"
21
#include "stream.h"
22
#include "pdf_misc.h"
23
#include "pdf_path.h"
24
#include "pdf_colour.h"
25
#include "pdf_image.h"
26
#include "pdf_shading.h"
27
#include "pdf_font.h"
28
#include "pdf_font_types.h"
29
#include "pdf_cmap.h"
30
#include "pdf_text.h"
31
#include "pdf_gstate.h"
32
#include "pdf_stack.h"
33
#include "pdf_xref.h"
34
#include "pdf_dict.h"
35
#include "pdf_array.h"
36
#include "pdf_trans.h"
37
#include "pdf_optcontent.h"
38
#include "pdf_sec.h"
39
#include <stdlib.h>
40
41
#include "gsstate.h"    /* for gs_gstate_free */
42
43
/* we use -ve returns for error, 0 for success and +ve for 'take an action' */
44
/* Defining tis return so we do not need to define a new error */
45
79.1M
#define REPAIRED_KEYWORD 1
46
47
/***********************************************************************************/
48
/* 'token' reading functions. Tokens in this sense are PDF logical objects and the */
49
/* related keywords. So that's numbers, booleans, names, strings, dictionaries,    */
50
/* arrays, the  null object and indirect references. The keywords are obj/endobj   */
51
/* stream/endstream, xref, startxref and trailer.                                  */
52
53
/***********************************************************************************/
54
/* Some simple functions to find white space, delimiters and hex bytes             */
55
static bool iswhite(char c)
56
4.90G
{
57
4.90G
    if (c == 0x00 || c == 0x09 || c == 0x0a || c == 0x0c || c == 0x0d || c == 0x20)
58
772M
        return true;
59
4.13G
    else
60
4.13G
        return false;
61
4.90G
}
62
63
static bool isdelimiter(char c)
64
2.85G
{
65
2.85G
    if (c == '/' || c == '(' || c == ')' || c == '[' || c == ']' || c == '<' || c == '>' || c == '{' || c == '}' || c == '%')
66
64.3M
        return true;
67
2.79G
    else
68
2.79G
        return false;
69
2.85G
}
70
71
/* The 'read' functions all return the newly created object on the context's stack
72
 * which means these objects are created with a reference count of 0, and only when
73
 * pushed onto the stack does the reference count become 1, indicating the stack is
74
 * the only reference.
75
 */
76
int pdfi_skip_white(pdf_context *ctx, pdf_c_stream *s)
77
1.11G
{
78
1.11G
    int c;
79
80
1.33G
    do {
81
1.33G
        c = pdfi_read_byte(ctx, s);
82
1.33G
        if (c < 0)
83
343k
            return 0;
84
1.33G
    } while (iswhite(c));
85
86
1.11G
    pdfi_unread_byte(ctx, s, (byte)c);
87
1.11G
    return 0;
88
1.11G
}
89
90
int pdfi_skip_eol(pdf_context *ctx, pdf_c_stream *s)
91
2.65M
{
92
2.65M
    int c;
93
94
2.84M
    do {
95
2.84M
        c = pdfi_read_byte(ctx, s);
96
2.84M
        if (c < 0 || c == 0x0a)
97
1.13M
            return 0;
98
2.84M
    } while (c != 0x0d);
99
1.51M
    c = pdfi_read_byte(ctx, s);
100
1.51M
    if (c == 0x0a)
101
1.50M
        return 0;
102
7.96k
    if (c >= 0)
103
7.88k
        pdfi_unread_byte(ctx, s, (byte)c);
104
7.96k
    pdfi_set_warning(ctx, 0, NULL, W_PDF_STREAM_BAD_KEYWORD, "pdfi_skip_eol", NULL);
105
7.96k
    return 0;
106
1.51M
}
107
108
/* Fast(ish) but inaccurate strtof, with Adobe overflow handling,
109
 * lifted from MuPDF. */
110
static float acrobat_compatible_atof(char *s)
111
138M
{
112
138M
    int neg = 0;
113
138M
    int i = 0;
114
115
146M
    while (*s == '-') {
116
8.39M
        neg = 1;
117
8.39M
        ++s;
118
8.39M
    }
119
138M
    while (*s == '+') {
120
6
        ++s;
121
6
    }
122
123
488M
    while (*s >= '0' && *s <= '9') {
124
        /* We deliberately ignore overflow here.
125
         * Tests show that Acrobat handles * overflows in exactly the same way we do:
126
         * 123450000000000000000678 is read as 678.
127
         */
128
349M
        i = i * 10 + (*s - '0');
129
349M
        ++s;
130
349M
    }
131
132
138M
    if (*s == '.') {
133
138M
        float MAX = (MAX_FLOAT-9)/10;
134
138M
        float v = (float)i;
135
138M
        float n = 0;
136
138M
        float d = 1;
137
138M
        ++s;
138
        /* Bug 705211: Ensure that we don't overflow n here - just ignore any
139
         * trailing digits after this. This will be plenty accurate enough. */
140
588M
        while (*s >= '0' && *s <= '9' && n <= MAX) {
141
449M
            n = 10 * n + (*s - '0');
142
449M
            d = 10 * d;
143
449M
            ++s;
144
449M
        }
145
138M
        v += n / d;
146
138M
        return neg ? -v : v;
147
138M
    } else {
148
89.7k
        return (float)(neg ? -i : i);
149
89.7k
    }
150
138M
}
151
152
int pdfi_read_bare_int(pdf_context *ctx, pdf_c_stream *s, int *parsed_int)
153
111M
{
154
111M
    int index = 0;
155
111M
    int int_val = 0;
156
111M
    int negative = 0;
157
111M
    int tenth_max_int = max_int / 10, tenth_max_uint = max_uint / 10;
158
111M
    bool overflowed = false;
159
111M
    int code = 0;
160
161
111M
restart:
162
111M
    pdfi_skip_white(ctx, s);
163
164
480M
    do {
165
480M
        int c = pdfi_read_byte(ctx, s);
166
480M
        if (c == EOFC)
167
3.45k
            break;
168
169
480M
        if (c < 0)
170
7.51k
            return_error(gs_error_ioerror);
171
172
480M
        if (iswhite(c)) {
173
111M
            break;
174
368M
        } else if (c == '%' && index == 0) {
175
45.6k
            pdfi_skip_comment(ctx, s);
176
45.6k
            goto restart;
177
368M
        } else if (isdelimiter(c)) {
178
16.2k
            pdfi_unread_byte(ctx, s, (byte)c);
179
16.2k
            break;
180
16.2k
        }
181
182
368M
        if (c >= '0' && c <= '9') {
183
368M
            if (!overflowed) {
184
368M
                if ((negative && int_val <= tenth_max_int) || (!negative && int_val <= tenth_max_uint))
185
368M
                    int_val = int_val*10 + c - '0';
186
1.82k
                else {
187
1.82k
                    if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_NUMBEROVERFLOW, "pdfi_read_num", NULL)) < 0) {
188
0
                        return code;
189
0
                    }
190
1.82k
                    overflowed = true;
191
1.82k
                }
192
368M
            }
193
368M
        } else if (c == '.') {
194
2.48k
            goto error;
195
197k
        } else if (c == 'e' || c == 'E') {
196
2.71k
            pdfi_log_info(ctx, "pdfi_read_bare_int", (char *)"Invalid number format: scientific notation\n");
197
2.71k
            goto error;
198
194k
        } else if (c == '-') {
199
            /* Any - sign not at the start of the string indicates a malformed number. */
200
704
            if (index != 0 || negative) {
201
132
                pdfi_log_info(ctx, "pdfi_read_bare_int", (char *)"Invalid number format: sign not at the start\n");
202
132
                goto error;
203
132
            }
204
572
            negative = 1;
205
194k
        } else if (c == '+') {
206
124k
            if (index == 0) {
207
                /* Just drop the + it's pointless, and it'll get in the way
208
                 * of our negation handling for floats. */
209
124k
                continue;
210
124k
            } else {
211
148
                pdfi_log_info(ctx, "pdfi_read_bare_int", (char *)"Invalid number format: sign not at the start\n");
212
148
                goto error;
213
148
            }
214
124k
        } else {
215
69.4k
            if (index > 0) {
216
5.89k
                pdfi_log_info(ctx, "pdfi_read_bare_int", (char *)"Invalid number format: Ignoring missing white space while parsing number\n");
217
5.89k
                goto error;
218
5.89k
            }
219
63.5k
            pdfi_unread_byte(ctx, s, (byte)c);
220
63.5k
            goto error;
221
69.4k
        }
222
368M
        if (++index > 255)
223
184
            goto error;
224
368M
    } while(1);
225
226
111M
    *parsed_int = negative ? -int_val : int_val;
227
111M
    if (ctx->args.pdfdebug)
228
0
        outprintf(ctx->memory, " %d", *parsed_int);
229
111M
    return (index > 0);
230
231
75.1k
error:
232
75.1k
    *parsed_int = 0;
233
75.1k
    return_error(gs_error_syntaxerror);
234
111M
}
235
236
static int pdfi_read_num(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen)
237
303M
{
238
303M
    byte Buffer[256];
239
303M
    unsigned short index = 0;
240
303M
    bool real = false;
241
303M
    bool has_decimal_point = false;
242
303M
    bool has_exponent = false;
243
303M
    unsigned short exponent_index = 0;
244
303M
    pdf_num *num;
245
303M
    int code = 0, malformed = false, doubleneg = false, recovered = false, negative = false, overflowed = false;
246
303M
    unsigned int int_val = 0;
247
303M
    int tenth_max_int = max_int / 10, tenth_max_uint = max_uint / 10;
248
249
303M
    pdfi_skip_white(ctx, s);
250
251
1.71G
    do {
252
1.71G
        int c = pdfi_read_byte(ctx, s);
253
1.71G
        if (c == EOFC) {
254
10.9k
            Buffer[index] = 0x00;
255
10.9k
            break;
256
10.9k
        }
257
258
1.71G
        if (c < 0)
259
3.84k
            return_error(gs_error_ioerror);
260
261
1.71G
        if (iswhite(c)) {
262
272M
            Buffer[index] = 0x00;
263
272M
            break;
264
1.44G
        } else if (isdelimiter(c)) {
265
25.8M
            pdfi_unread_byte(ctx, s, (byte)c);
266
25.8M
            Buffer[index] = 0x00;
267
25.8M
            break;
268
25.8M
        }
269
1.41G
        Buffer[index] = (byte)c;
270
271
1.41G
        if (c >= '0' && c <= '9') {
272
1.25G
            if  (!(malformed && recovered) && !overflowed && !real) {
273
759M
                if ((negative && int_val <= tenth_max_int) || (!negative && int_val <= tenth_max_uint))
274
758M
                    int_val = int_val*10 + c - '0';
275
1.26M
                else {
276
1.26M
                    if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_NUMBEROVERFLOW, "pdfi_read_num", NULL)) < 0) {
277
0
                        return code;
278
0
                    }
279
1.26M
                    overflowed = true;
280
1.26M
                }
281
759M
            }
282
1.25G
        } else if (c == '.') {
283
142M
            if (has_decimal_point == true) {
284
2.91M
                if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", NULL)) < 0) {
285
0
                    return code;
286
0
                }
287
2.91M
                malformed = true;
288
140M
            } else {
289
140M
                has_decimal_point = true;
290
140M
                real = true;
291
140M
            }
292
142M
        } else if (c == 'e' || c == 'E') {
293
            /* TODO: technically scientific notation isn't in PDF spec,
294
             * but gs seems to accept it, so we should also?
295
             */
296
462k
            if (has_exponent == true) {
297
43.6k
                if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", NULL)) < 0) {
298
0
                    return code;
299
0
                }
300
43.6k
                malformed = true;
301
418k
            } else {
302
418k
                pdfi_set_warning(ctx, 0, NULL, W_PDF_NUM_EXPONENT, "pdfi_read_num", NULL);
303
418k
                has_exponent = true;
304
418k
                exponent_index = index;
305
418k
                real = true;
306
418k
            }
307
22.8M
        } else if (c == '-') {
308
            /* Any - sign not at the start of the string, or just after an exponent
309
             * indicates a malformed number. */
310
17.0M
            if (!(index == 0 || (has_exponent && index == exponent_index+1))) {
311
4.08M
                if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", NULL)) < 0) {
312
0
                    return code;
313
0
                }
314
4.08M
                if (Buffer[index - 1] != '-') {
315
                    /* We are parsing a number line 123-56. We should continue parsing, but
316
                     * ignore anything from the second -. */
317
594k
                    malformed = true;
318
594k
                    Buffer[index] = 0;
319
594k
                    recovered = true;
320
594k
                }
321
4.08M
            }
322
17.0M
            if (!has_exponent && !(malformed && recovered)) {
323
16.4M
                doubleneg = negative;
324
16.4M
                negative = 1;
325
16.4M
            }
326
17.0M
        } else if (c == '+') {
327
169k
            if (index == 0 || (has_exponent && index == exponent_index+1)) {
328
                /* Just drop the + it's pointless, and it'll get in the way
329
                 * of our negation handling for floats. */
330
161k
                index--;
331
161k
            } else {
332
8.50k
                if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", NULL)) < 0) {
333
0
                    return code;
334
0
                }
335
8.50k
                if (Buffer[index - 1] != '-') {
336
                    /* We are parsing a number line 123-56. We should continue parsing, but
337
                     * ignore anything from the second -. */
338
8.13k
                    malformed = true;
339
8.13k
                    Buffer[index] = 0;
340
8.13k
                    recovered = true;
341
8.13k
                }
342
8.50k
            }
343
5.55M
        } else {
344
5.55M
            if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MISSINGWHITESPACE, "pdfi_read_num", NULL)) < 0) {
345
0
                return code;
346
0
            }
347
5.55M
            pdfi_unread_byte(ctx, s, (byte)c);
348
5.55M
            Buffer[index] = 0x00;
349
5.55M
            break;
350
5.55M
        }
351
1.41G
        if (++index > 255)
352
46.8k
            return_error(gs_error_syntaxerror);
353
1.41G
    } while(1);
354
355
303M
    if (real && (!malformed || (malformed && recovered)))
356
138M
        code = pdfi_object_alloc(ctx, PDF_REAL, 0, (pdf_obj **)&num);
357
164M
    else
358
164M
        code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)&num);
359
303M
    if (code < 0)
360
0
        return code;
361
362
303M
    if ((malformed && !recovered) || (!real && doubleneg)) {
363
1.54M
        if ((code = pdfi_set_warning_var(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", "Treating malformed number %s as 0", Buffer)) < 0) {
364
0
            goto exit;
365
0
        }
366
1.54M
        num->value.i = 0;
367
302M
    } else if (has_exponent) {
368
389k
        float f, exp;
369
389k
        char *p = strstr((const char *)Buffer, "e");
370
371
389k
        if (p == NULL)
372
38.4k
            p = strstr((const char *)Buffer, "E");
373
374
389k
        if (p == NULL) {
375
3.78k
            if ((code = pdfi_set_warning_var(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", "Treating malformed float %s as 0", Buffer)) < 0) {
376
0
                goto exit;
377
0
            }
378
3.78k
            num->value.d = 0;
379
386k
        } else {
380
386k
            p++;
381
382
386k
            if (sscanf((char *)p, "%g", &exp) != 1 || exp > 38) {
383
355k
                if ((code = pdfi_set_warning_var(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", "Treating malformed float %s as 0", Buffer)) < 0) {
384
0
                    goto exit;
385
0
                }
386
355k
                num->value.d = 0;
387
355k
            } else {
388
30.3k
                if (sscanf((char *)Buffer, "%g", &f) == 1) {
389
29.7k
                    num->value.d = f;
390
29.7k
                } else {
391
663
                    if ((code = pdfi_set_warning_var(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", "Treating malformed float %s as 0", Buffer)) < 0) {
392
0
                        goto exit;
393
0
                    }
394
663
                    num->value.d = 0;
395
663
                }
396
30.3k
            }
397
386k
        }
398
301M
    } else if (real) {
399
138M
        num->value.d = acrobat_compatible_atof((char *)Buffer);
400
163M
    } else {
401
        /* The doubleneg case is taken care of above. */
402
163M
        num->value.i = negative ? (int64_t)int_val * -1 : (int64_t)int_val;
403
163M
    }
404
303M
    if (ctx->args.pdfdebug) {
405
0
        if (real)
406
0
            outprintf(ctx->memory, " %f", num->value.d);
407
0
        else
408
0
            outprintf(ctx->memory, " %"PRIi64, num->value.i);
409
0
    }
410
303M
    num->indirect_num = indirect_num;
411
303M
    num->indirect_gen = indirect_gen;
412
413
303M
    code = pdfi_push(ctx, (pdf_obj *)num);
414
415
303M
exit:
416
303M
    if (code < 0)
417
947
        pdfi_free_object((pdf_obj *)num);
418
419
303M
    return code;
420
303M
}
421
422
static int pdfi_read_name(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen)
423
84.2M
{
424
84.2M
    char *Buffer, *NewBuf = NULL;
425
84.2M
    unsigned short index = 0;
426
84.2M
    short bytes = 0;
427
84.2M
    uint32_t size = 256;
428
84.2M
    pdf_name *name = NULL;
429
84.2M
    int code;
430
431
84.2M
    Buffer = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_read_name");
432
84.2M
    if (Buffer == NULL)
433
0
        return_error(gs_error_VMerror);
434
435
607M
    do {
436
607M
        int c = pdfi_read_byte(ctx, s);
437
607M
        if (c < 0)
438
9.60k
            break;
439
440
607M
        if (iswhite((char)c)) {
441
55.1M
            Buffer[index] = 0x00;
442
55.1M
            break;
443
552M
        } else if (isdelimiter((char)c)) {
444
29.0M
            pdfi_unread_byte(ctx, s, (char)c);
445
29.0M
            Buffer[index] = 0x00;
446
29.0M
            break;
447
29.0M
        }
448
523M
        Buffer[index] = (char)c;
449
450
        /* Check for and convert escaped name characters */
451
523M
        if (c == '#') {
452
227k
            byte NumBuf[2];
453
454
227k
            bytes = pdfi_read_bytes(ctx, (byte *)&NumBuf, 1, 2, s);
455
227k
            if (bytes < 2 || (!ishex(NumBuf[0]) || !ishex(NumBuf[1]))) {
456
97.3k
                pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_NAME_ESCAPE, "pdfi_read_name", NULL);
457
97.3k
                pdfi_unread(ctx, s, (byte *)NumBuf, bytes);
458
                /* This leaves the name buffer with a # in it, rather than anything sane! */
459
97.3k
            }
460
130k
            else
461
130k
                Buffer[index] = (fromhex(NumBuf[0]) << 4) + fromhex(NumBuf[1]);
462
227k
        }
463
464
        /* If we ran out of memory, increase the buffer size */
465
523M
        if (index++ >= size - 1) {
466
45.2k
            NewBuf = (char *)gs_alloc_bytes(ctx->memory, (size_t)size + 256, "pdfi_read_name");
467
45.2k
            if (NewBuf == NULL) {
468
0
                gs_free_object(ctx->memory, Buffer, "pdfi_read_name error");
469
0
                return_error(gs_error_VMerror);
470
0
            }
471
45.2k
            memcpy(NewBuf, Buffer, size);
472
45.2k
            gs_free_object(ctx->memory, Buffer, "pdfi_read_name");
473
45.2k
            Buffer = NewBuf;
474
45.2k
            size += 256;
475
45.2k
        }
476
523M
    } while(1);
477
478
84.2M
    code = pdfi_object_alloc(ctx, PDF_NAME, index, (pdf_obj **)&name);
479
84.2M
    if (code < 0) {
480
0
        gs_free_object(ctx->memory, Buffer, "pdfi_read_name error");
481
0
        return code;
482
0
    }
483
84.2M
    memcpy(name->data, Buffer, index);
484
84.2M
    name->indirect_num = indirect_num;
485
84.2M
    name->indirect_gen = indirect_gen;
486
487
84.2M
    if (ctx->args.pdfdebug)
488
0
        outprintf(ctx->memory, " /%s", Buffer);
489
490
84.2M
    gs_free_object(ctx->memory, Buffer, "pdfi_read_name");
491
492
84.2M
    code = pdfi_push(ctx, (pdf_obj *)name);
493
494
84.2M
    if (code < 0)
495
0
        pdfi_free_object((pdf_obj *)name);
496
497
84.2M
    return code;
498
84.2M
}
499
500
static int pdfi_read_hexstring(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen)
501
2.73M
{
502
2.73M
    char *Buffer, *NewBuf = NULL;
503
2.73M
    unsigned short index = 0;
504
2.73M
    uint32_t size = 256;
505
2.73M
    pdf_string *string = NULL;
506
2.73M
    int code, hex0, hex1;
507
508
2.73M
    Buffer = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_read_hexstring");
509
2.73M
    if (Buffer == NULL)
510
0
        return_error(gs_error_VMerror);
511
512
2.73M
    if (ctx->args.pdfdebug)
513
0
        outprintf(ctx->memory, " <");
514
515
134M
    do {
516
134M
        do {
517
134M
            hex0 = pdfi_read_byte(ctx, s);
518
134M
            if (hex0 < 0)
519
745
                break;
520
134M
        } while(iswhite(hex0));
521
134M
        if (hex0 < 0)
522
745
            break;
523
524
134M
        if (hex0 == '>')
525
2.40M
            break;
526
527
131M
        if (ctx->args.pdfdebug)
528
0
            outprintf(ctx->memory, "%c", (char)hex0);
529
530
132M
        do {
531
132M
            hex1 = pdfi_read_byte(ctx, s);
532
132M
            if (hex1 < 0)
533
709
                break;
534
132M
        } while(iswhite(hex1));
535
131M
        if (hex1 < 0)
536
709
            break;
537
538
131M
        if (hex1 == '>') {
539
            /* PDF Reference 1.7 page 56:
540
             * "If the final digit of a hexadecimal string is missing that is,
541
             * if there is an odd number of digits the final digit is assumed to be 0."
542
             */
543
40.7k
            hex1 = 0x30;
544
40.7k
            if (!ishex(hex0) || !ishex(hex1)) {
545
2.04k
                code = gs_note_error(gs_error_syntaxerror);
546
2.04k
                goto exit;
547
2.04k
            }
548
38.6k
            Buffer[index] = (fromhex(hex0) << 4) + fromhex(hex1);
549
38.6k
            if (ctx->args.pdfdebug)
550
0
                outprintf(ctx->memory, "%c", hex1);
551
38.6k
            break;
552
40.7k
        }
553
554
131M
        if (!ishex(hex0) || !ishex(hex1)) {
555
287k
            code = gs_note_error(gs_error_syntaxerror);
556
287k
            goto exit;
557
287k
        }
558
559
131M
        if (ctx->args.pdfdebug)
560
0
            outprintf(ctx->memory, "%c", (char)hex1);
561
562
131M
        Buffer[index] = (fromhex(hex0) << 4) + fromhex(hex1);
563
564
131M
        if (index++ >= size - 1) {
565
450k
            NewBuf = (char *)gs_alloc_bytes(ctx->memory, (size_t)size + 256, "pdfi_read_hexstring");
566
450k
            if (NewBuf == NULL) {
567
0
                code = gs_note_error(gs_error_VMerror);
568
0
                goto exit;
569
0
            }
570
450k
            memcpy(NewBuf, Buffer, size);
571
450k
            gs_free_object(ctx->memory, Buffer, "pdfi_read_hexstring");
572
450k
            Buffer = NewBuf;
573
450k
            size += 256;
574
450k
        }
575
131M
    } while(1);
576
577
2.44M
    if (ctx->args.pdfdebug)
578
0
        outprintf(ctx->memory, ">");
579
580
2.44M
    code = pdfi_object_alloc(ctx, PDF_STRING, index, (pdf_obj **)&string);
581
2.44M
    if (code < 0)
582
0
        goto exit;
583
2.44M
    memcpy(string->data, Buffer, index);
584
2.44M
    string->indirect_num = indirect_num;
585
2.44M
    string->indirect_gen = indirect_gen;
586
587
2.44M
    if (ctx->encryption.is_encrypted && ctx->encryption.decrypt_strings) {
588
2.17k
        code = pdfi_decrypt_string(ctx, string);
589
2.17k
        if (code < 0)
590
0
            return code;
591
2.17k
    }
592
593
2.44M
    code = pdfi_push(ctx, (pdf_obj *)string);
594
2.44M
    if (code < 0)
595
0
        pdfi_free_object((pdf_obj *)string);
596
597
2.73M
 exit:
598
2.73M
    gs_free_object(ctx->memory, Buffer, "pdfi_read_hexstring");
599
2.73M
    return code;
600
2.44M
}
601
602
static int pdfi_read_string(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen)
603
14.2M
{
604
14.2M
    char *Buffer, *NewBuf = NULL;
605
14.2M
    unsigned short index = 0;
606
14.2M
    uint32_t size = 256;
607
14.2M
    pdf_string *string = NULL;
608
14.2M
    int c, code, nesting = 0;
609
14.2M
    bool escape = false, skip_eol = false, exit_loop = false;
610
611
14.2M
    Buffer = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_read_string");
612
14.2M
    if (Buffer == NULL)
613
0
        return_error(gs_error_VMerror);
614
615
697M
    do {
616
697M
        if (index >= size - 1) {
617
1.55M
            NewBuf = (char *)gs_alloc_bytes(ctx->memory, (size_t)size + 256, "pdfi_read_string");
618
1.55M
            if (NewBuf == NULL) {
619
0
                gs_free_object(ctx->memory, Buffer, "pdfi_read_string error");
620
0
                return_error(gs_error_VMerror);
621
0
            }
622
1.55M
            memcpy(NewBuf, Buffer, size);
623
1.55M
            gs_free_object(ctx->memory, Buffer, "pdfi_read_string");
624
1.55M
            Buffer = NewBuf;
625
1.55M
            size += 256;
626
1.55M
        }
627
628
697M
        c = pdfi_read_byte(ctx, s);
629
630
697M
        if (c < 0) {
631
20.8k
            if (nesting > 0 && (code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_UNESCAPEDSTRING, "pdfi_read_string", NULL)) < 0) {
632
0
                gs_free_object(ctx->memory, Buffer, "pdfi_read_string error");
633
0
                return code;
634
0
            }
635
20.8k
            Buffer[index] = 0x00;
636
20.8k
            break;
637
20.8k
        }
638
639
697M
        if (skip_eol) {
640
11.3M
            if (c == 0x0a || c == 0x0d)
641
1.37M
                continue;
642
10.0M
            skip_eol = false;
643
10.0M
        }
644
696M
        Buffer[index] = (char)c;
645
646
696M
        if (escape) {
647
2.92M
            escape = false;
648
2.92M
            switch (Buffer[index]) {
649
10.8k
                case 0x0a:
650
66.7k
                case 0x0d:
651
66.7k
                    skip_eol = true;
652
66.7k
                    continue;
653
63.0k
                case 'n':
654
63.0k
                    Buffer[index] = 0x0a;
655
63.0k
                    break;
656
65.3k
                case 'r':
657
65.3k
                    Buffer[index] = 0x0d;
658
65.3k
                    break;
659
16.5k
                case 't':
660
16.5k
                    Buffer[index] = 0x09;
661
16.5k
                    break;
662
20.3k
                case 'b':
663
20.3k
                    Buffer[index] = 0x08;
664
20.3k
                    break;
665
21.5k
                case 'f':
666
21.5k
                    Buffer[index] = 0x0c;
667
21.5k
                    break;
668
179k
                case '(':
669
366k
                case ')':
670
454k
                case '\\':
671
454k
                    break;
672
519k
                case '0':
673
535k
                case '1':
674
817k
                case '2':
675
1.02M
                case '3':
676
1.03M
                case '4':
677
1.04M
                case '5':
678
1.05M
                case '6':
679
1.06M
                case '7':
680
1.06M
                {
681
                    /* Octal chars can be 1, 2 or 3 chars in length, terminated either
682
                     * by being 3 chars long, EOFC, or a non-octal char. We do not allow
683
                     * line breaks in the middle of octal chars. */
684
1.06M
                    int c1 = pdfi_read_byte(ctx, s);
685
1.06M
                    c -= '0';
686
1.06M
                    if (c1 < 0) {
687
                        /* Nothing to do, or unread */
688
1.06M
                    } else if (c1 < '0' || c1 > '7') {
689
73.6k
                        pdfi_unread_byte(ctx, s, (char)c1);
690
992k
                    } else {
691
992k
                        c = c*8 + c1 - '0';
692
992k
                        c1 = pdfi_read_byte(ctx, s);
693
992k
                        if (c1 < 0) {
694
                            /* Nothing to do, or unread */
695
992k
                        } else if (c1 < '0' || c1 > '7') {
696
31.8k
                            pdfi_unread_byte(ctx, s, (char)c1);
697
31.8k
                        } else
698
960k
                            c = c*8 + c1 - '0';
699
992k
                    }
700
1.06M
                    Buffer[index] = c;
701
1.06M
                    break;
702
1.05M
                }
703
1.15M
                default:
704
                    /* PDF Reference, literal strings, if the character following a
705
                     * escape \ character is not recognised, then it is ignored.
706
                     */
707
1.15M
                    escape = false;
708
1.15M
                    index++;
709
1.15M
                    continue;
710
2.92M
            }
711
693M
        } else {
712
693M
            switch(Buffer[index]) {
713
2.78M
                case 0x0d:
714
2.78M
                    Buffer[index] = 0x0a;
715
                    /*fallthrough*/
716
9.94M
                case 0x0a:
717
9.94M
                    skip_eol = true;
718
9.94M
                    break;
719
16.6M
                case ')':
720
16.6M
                    if (nesting == 0) {
721
14.2M
                        Buffer[index] = 0x00;
722
14.2M
                        exit_loop = true;
723
14.2M
                    } else
724
2.41M
                        nesting--;
725
16.6M
                    break;
726
2.92M
                case '\\':
727
2.92M
                    escape = true;
728
2.92M
                    continue;
729
2.91M
                case '(':
730
2.91M
                    nesting++;
731
2.91M
                    break;
732
660M
                default:
733
660M
                    break;
734
693M
            }
735
693M
        }
736
737
691M
        if (exit_loop)
738
14.2M
            break;
739
740
677M
        index++;
741
683M
    } while(1);
742
743
14.2M
    code = pdfi_object_alloc(ctx, PDF_STRING, index, (pdf_obj **)&string);
744
14.2M
    if (code < 0) {
745
0
        gs_free_object(ctx->memory, Buffer, "pdfi_read_name error");
746
0
        return code;
747
0
    }
748
14.2M
    memcpy(string->data, Buffer, index);
749
14.2M
    string->indirect_num = indirect_num;
750
14.2M
    string->indirect_gen = indirect_gen;
751
752
14.2M
    gs_free_object(ctx->memory, Buffer, "pdfi_read_string");
753
754
14.2M
    if (ctx->encryption.is_encrypted && ctx->encryption.decrypt_strings) {
755
12.1k
        code = pdfi_decrypt_string(ctx, string);
756
12.1k
        if (code < 0)
757
0
            return code;
758
12.1k
    }
759
760
14.2M
    if (ctx->args.pdfdebug) {
761
0
        int i;
762
0
        outprintf(ctx->memory, " (");
763
0
        for (i=0;i<string->length;i++)
764
0
            outprintf(ctx->memory, "%c", string->data[i]);
765
0
        outprintf(ctx->memory, ")");
766
0
    }
767
768
14.2M
    code = pdfi_push(ctx, (pdf_obj *)string);
769
14.2M
    if (code < 0) {
770
0
        pdfi_free_object((pdf_obj *)string);
771
0
        pdfi_set_error(ctx, code, NULL, 0, "pdfi_read_string", NULL);
772
0
    }
773
774
14.2M
    return code;
775
14.2M
}
776
777
int pdfi_read_dict(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen)
778
8.39k
{
779
8.39k
    int code, depth;
780
781
8.39k
    code = pdfi_read_token(ctx, s, indirect_num, indirect_gen);
782
8.39k
    if (code < 0)
783
11
        return code;
784
8.38k
    if (code == 0)
785
0
        return_error(gs_error_syntaxerror);
786
787
8.38k
    if (pdfi_type_of(ctx->stack_top[-1]) != PDF_DICT_MARK)
788
21
        return_error(gs_error_typecheck);
789
8.35k
    depth = pdfi_count_stack(ctx);
790
791
137k
    do {
792
137k
        code = pdfi_read_token(ctx, s, indirect_num, indirect_gen);
793
137k
        if (code < 0)
794
117
            return code;
795
137k
        if (code == 0)
796
15
            return_error(gs_error_syntaxerror);
797
137k
    } while(pdfi_count_stack(ctx) > depth);
798
8.22k
    return 0;
799
8.35k
}
800
801
int pdfi_skip_comment(pdf_context *ctx, pdf_c_stream *s)
802
1.77M
{
803
1.77M
    int c;
804
805
1.77M
    if (ctx->args.pdfdebug)
806
0
        outprintf (ctx->memory, " %%");
807
808
41.6M
    do {
809
41.6M
        c = pdfi_read_byte(ctx, s);
810
41.6M
        if (c < 0)
811
9.14k
            break;
812
813
41.6M
        if (ctx->args.pdfdebug)
814
0
            outprintf (ctx->memory, "%c", (char)c);
815
816
41.6M
    } while (c != 0x0a && c != 0x0d);
817
818
1.77M
    return 0;
819
1.77M
}
820
821
#define PARAM1(A) # A,
822
#define PARAM2(A,B) A,
823
static const char pdf_token_strings[][10] = {
824
#include "pdf_tokens.h"
825
};
826
827
124M
#define nelems(A) (sizeof(A)/sizeof(A[0]))
828
829
typedef int (*bsearch_comparator)(const void *, const void *);
830
831
int pdfi_read_bare_keyword(pdf_context *ctx, pdf_c_stream *s)
832
3.83M
{
833
3.83M
    byte Buffer[256];
834
3.83M
    int code, index = 0;
835
3.83M
    int c;
836
3.83M
    void *t;
837
838
3.83M
    pdfi_skip_white(ctx, s);
839
840
23.8M
    do {
841
23.8M
        c = pdfi_read_byte(ctx, s);
842
23.8M
        if (c < 0)
843
24.2k
            break;
844
845
23.8M
        if (iswhite(c) || isdelimiter(c)) {
846
3.81M
            pdfi_unread_byte(ctx, s, (byte)c);
847
3.81M
            break;
848
3.81M
        }
849
20.0M
        Buffer[index] = (byte)c;
850
20.0M
        index++;
851
20.0M
    } while (index < 255);
852
853
3.83M
    if (index >= 255 || index == 0) {
854
33.2k
        if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_NOERROR, "pdfi_read_bare_keyword", "")) < 0) {
855
0
            return code;
856
0
        }
857
33.2k
        return TOKEN_INVALID_KEY;
858
33.2k
    }
859
860
3.80M
    Buffer[index] = 0x00;
861
3.80M
    t = bsearch((const void *)Buffer,
862
3.80M
                (const void *)pdf_token_strings[TOKEN_INVALID_KEY+1],
863
3.80M
                nelems(pdf_token_strings)-(TOKEN_INVALID_KEY+1),
864
3.80M
                sizeof(pdf_token_strings[0]),
865
3.80M
                (bsearch_comparator)&strcmp);
866
3.80M
    if (t == NULL)
867
74.5k
        return TOKEN_INVALID_KEY;
868
869
3.72M
    if (ctx->args.pdfdebug)
870
0
        outprintf(ctx->memory, " %s\n", Buffer);
871
872
3.72M
    return (((const char *)t) - pdf_token_strings[0]) / sizeof(pdf_token_strings[0]);
873
3.80M
}
874
875
static pdf_key lookup_keyword(const byte *Buffer)
876
120M
{
877
120M
    void *t = bsearch((const void *)Buffer,
878
120M
                      (const void *)pdf_token_strings[TOKEN_INVALID_KEY+1],
879
120M
                      nelems(pdf_token_strings)-(TOKEN_INVALID_KEY+1),
880
120M
                      sizeof(pdf_token_strings[0]),
881
120M
                      (bsearch_comparator)&strcmp);
882
120M
    if (t == NULL)
883
14.5M
        return TOKEN_NOT_A_KEYWORD;
884
885
105M
    return (pdf_key)((((const char *)t) - pdf_token_strings[0]) /
886
105M
                     sizeof(pdf_token_strings[0]));
887
120M
}
888
889
/* This function is slightly misnamed. We read 'keywords' from
890
 * the stream (including null, true, false and R), and will usually
891
 * return them directly as TOKENs cast to be pointers. In the event
892
 * that we can't match what we parse to a known keyword, we'll
893
 * instead return a PDF_KEYWORD object. In the even that we parse
894
 * an 'R', we will return a PDF_INDIRECT object.
895
 */
896
static int pdfi_read_keyword(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen)
897
118M
{
898
118M
    byte Buffer[256];
899
118M
    unsigned short index = 0;
900
118M
    int c, code;
901
118M
    pdf_keyword *keyword;
902
118M
    pdf_key key;
903
904
118M
    pdfi_skip_white(ctx, s);
905
906
461M
    do {
907
461M
        c = pdfi_read_byte(ctx, s);
908
461M
        if (c < 0)
909
232k
            break;
910
911
461M
        if (iswhite(c) || isdelimiter(c)) {
912
117M
            pdfi_unread_byte(ctx, s, (byte)c);
913
117M
            break;
914
117M
        }
915
343M
        Buffer[index] = (byte)c;
916
343M
        index++;
917
343M
    } while (index < 255);
918
919
118M
    if (index >= 255 || index == 0) {
920
234k
        if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, 0, "pdfi_read_keyword", NULL)) < 0) {
921
0
            return code;
922
0
        }
923
234k
        key = (index >= 255 ? TOKEN_TOO_LONG : TOKEN_INVALID_KEY);
924
234k
        index = 0;
925
234k
        Buffer[0] = 0;
926
118M
    } else {
927
118M
        Buffer[index] = 0x00;
928
118M
        key = lookup_keyword(Buffer);
929
930
118M
        if (ctx->args.pdfdebug)
931
0
            outprintf(ctx->memory, " %s\n", Buffer);
932
933
118M
        switch (key) {
934
14.5M
            case TOKEN_R:
935
14.5M
            {
936
14.5M
                pdf_indirect_ref *o;
937
14.5M
                uint64_t obj_num;
938
14.5M
                uint32_t gen_num;
939
940
14.5M
                if(pdfi_count_stack(ctx) < 2) {
941
35.6k
                    pdfi_clearstack(ctx);
942
35.6k
                    return_error(gs_error_stackunderflow);
943
35.6k
                }
944
945
14.4M
                if(pdfi_type_of(ctx->stack_top[-1]) != PDF_INT || pdfi_type_of(ctx->stack_top[-2]) != PDF_INT) {
946
46.7k
                    pdfi_clearstack(ctx);
947
46.7k
                    return_error(gs_error_typecheck);
948
46.7k
                }
949
950
14.4M
                gen_num = ((pdf_num *)ctx->stack_top[-1])->value.i;
951
14.4M
                pdfi_pop(ctx, 1);
952
14.4M
                obj_num = ((pdf_num *)ctx->stack_top[-1])->value.i;
953
14.4M
                pdfi_pop(ctx, 1);
954
955
14.4M
                code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&o);
956
14.4M
                if (code < 0)
957
0
                    return code;
958
959
14.4M
                o->ref_generation_num = gen_num;
960
14.4M
                o->ref_object_num = obj_num;
961
14.4M
                o->indirect_num = indirect_num;
962
14.4M
                o->indirect_gen = indirect_gen;
963
964
14.4M
                code = pdfi_push(ctx, (pdf_obj *)o);
965
14.4M
                if (code < 0)
966
0
                    pdfi_free_object((pdf_obj *)o);
967
968
14.4M
                return code;
969
14.4M
            }
970
14.5M
            case TOKEN_NOT_A_KEYWORD:
971
                 /* Unexpected keyword found. We'll allocate an object for the buffer below. */
972
14.5M
                 break;
973
2.65M
            case TOKEN_STREAM:
974
2.65M
                code = pdfi_skip_eol(ctx, s);
975
2.65M
                if (code < 0)
976
0
                    return code;
977
                /* fallthrough */
978
3.33M
            case TOKEN_PDF_TRUE:
979
3.76M
            case TOKEN_PDF_FALSE:
980
3.82M
            case TOKEN_null:
981
88.9M
            default:
982
                /* This is the fast, common exit case. We just push the key
983
                 * onto the stack. No allocation required. No deallocation
984
                 * in the case of error. */
985
88.9M
                return pdfi_push(ctx, (pdf_obj *)(intptr_t)key);
986
118M
        }
987
118M
    }
988
989
    /* Unexpected keyword. We can't handle this with the fast no-allocation case. */
990
14.8M
    code = pdfi_object_alloc(ctx, PDF_KEYWORD, index, (pdf_obj **)&keyword);
991
14.8M
    if (code < 0)
992
0
        return code;
993
994
14.8M
    if (index)
995
14.5M
        memcpy(keyword->data, Buffer, index);
996
997
    /* keyword->length set as part of allocation. */
998
14.8M
    keyword->indirect_num = indirect_num;
999
14.8M
    keyword->indirect_gen = indirect_gen;
1000
1001
14.8M
    code = pdfi_push(ctx, (pdf_obj *)keyword);
1002
14.8M
    if (code < 0)
1003
30
        pdfi_free_object((pdf_obj *)keyword);
1004
1005
14.8M
    return code;
1006
14.8M
}
1007
1008
/* This function reads from the given stream, at the current offset in the stream,
1009
 * a single PDF 'token' and returns it on the stack.
1010
 */
1011
int pdfi_read_token(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen)
1012
573M
{
1013
573M
    int c, code;
1014
1015
577M
rescan:
1016
577M
    pdfi_skip_white(ctx, s);
1017
1018
577M
    c = pdfi_read_byte(ctx, s);
1019
577M
    if (c == EOFC)
1020
296k
        return 0;
1021
576M
    if (c < 0)
1022
12.8k
        return_error(gs_error_ioerror);
1023
1024
576M
    switch(c) {
1025
73.3M
        case 0x30:
1026
117M
        case 0x31:
1027
153M
        case 0x32:
1028
194M
        case 0x33:
1029
228M
        case 0x34:
1030
247M
        case 0x35:
1031
266M
        case 0x36:
1032
277M
        case 0x37:
1033
284M
        case 0x38:
1034
289M
        case 0x39:
1035
289M
        case '+':
1036
302M
        case '-':
1037
303M
        case '.':
1038
303M
            pdfi_unread_byte(ctx, s, (byte)c);
1039
303M
            code = pdfi_read_num(ctx, s, indirect_num, indirect_gen);
1040
303M
            if (code < 0)
1041
51.6k
                return code;
1042
303M
            break;
1043
303M
        case '/':
1044
84.2M
            code = pdfi_read_name(ctx, s, indirect_num, indirect_gen);
1045
84.2M
            if (code < 0)
1046
0
                return code;
1047
84.2M
            return 1;
1048
0
            break;
1049
14.6M
        case '<':
1050
14.6M
            c = pdfi_read_byte(ctx, s);
1051
14.6M
            if (c < 0)
1052
325
                return (gs_error_ioerror);
1053
14.6M
            if (iswhite(c)) {
1054
49.5k
                code = pdfi_skip_white(ctx, s);
1055
49.5k
                if (code < 0)
1056
0
                    return code;
1057
49.5k
                c = pdfi_read_byte(ctx, s);
1058
49.5k
            }
1059
14.6M
            if (c == '<') {
1060
11.3M
                if (ctx->args.pdfdebug)
1061
0
                    outprintf (ctx->memory, " <<\n");
1062
11.3M
                if (ctx->object_nesting < MAX_NESTING_DEPTH) {
1063
10.8M
                    ctx->object_nesting++;
1064
10.8M
                    code = pdfi_mark_stack(ctx, PDF_DICT_MARK);
1065
10.8M
                    if (code < 0)
1066
1
                        return code;
1067
10.8M
                }
1068
502k
                else if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_limitcheck), NULL, E_PDF_NESTEDTOODEEP, "pdfi_read_token", NULL)) < 0) {
1069
0
                        return code;
1070
0
                }
1071
11.3M
                return 1;
1072
11.3M
            } else if (c == '>') {
1073
30.7k
                pdfi_unread_byte(ctx, s, (byte)c);
1074
30.7k
                code = pdfi_read_hexstring(ctx, s, indirect_num, indirect_gen);
1075
30.7k
                if (code < 0)
1076
0
                    return code;
1077
30.7k
                return 1;
1078
3.24M
            } else if (ishex(c)) {
1079
2.70M
                pdfi_unread_byte(ctx, s, (byte)c);
1080
2.70M
                code = pdfi_read_hexstring(ctx, s, indirect_num, indirect_gen);
1081
2.70M
                if (code < 0)
1082
289k
                    return code;
1083
2.70M
            }
1084
545k
            else
1085
545k
                return_error(gs_error_syntaxerror);
1086
2.41M
            break;
1087
11.9M
        case '>':
1088
11.9M
            c = pdfi_read_byte(ctx, s);
1089
11.9M
            if (c < 0)
1090
584
                return (gs_error_ioerror);
1091
11.9M
            if (c == '>') {
1092
11.1M
                if (ctx->object_nesting > 0) {
1093
10.4M
                    ctx->object_nesting--;
1094
10.4M
                    code = pdfi_dict_from_stack(ctx, indirect_num, indirect_gen, false);
1095
10.4M
                    if (code < 0)
1096
336k
                        return code;
1097
10.4M
                } else {
1098
688k
                    if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_unmatchedmark), NULL, E_PDF_UNMATCHEDMARK, "pdfi_read_token", NULL)) < 0) {
1099
0
                        return code;
1100
0
                    }
1101
688k
                    goto rescan;
1102
688k
                }
1103
10.1M
                return 1;
1104
11.1M
            } else {
1105
788k
                pdfi_unread_byte(ctx, s, (byte)c);
1106
788k
                return_error(gs_error_syntaxerror);
1107
788k
            }
1108
0
            break;
1109
14.2M
        case '(':
1110
14.2M
            code = pdfi_read_string(ctx, s, indirect_num, indirect_gen);
1111
14.2M
            if (code < 0)
1112
0
                return code;
1113
14.2M
            return 1;
1114
0
            break;
1115
13.9M
        case '[':
1116
13.9M
            if (ctx->args.pdfdebug)
1117
0
                outprintf (ctx->memory, "[");
1118
13.9M
            if (ctx->object_nesting < MAX_NESTING_DEPTH) {
1119
12.6M
                ctx->object_nesting++;
1120
12.6M
                code = pdfi_mark_stack(ctx, PDF_ARRAY_MARK);
1121
12.6M
                if (code < 0)
1122
0
                    return code;
1123
12.6M
            } else
1124
1.31M
                if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_NESTEDTOODEEP, "pdfi_read_token", NULL)) < 0)
1125
0
                    return code;
1126
13.9M
            return 1;
1127
0
            break;
1128
12.8M
        case ']':
1129
12.8M
            if (ctx->object_nesting > 0) {
1130
12.5M
                ctx->object_nesting--;
1131
12.5M
                code = pdfi_array_from_stack(ctx, indirect_num, indirect_gen);
1132
12.5M
                if (code < 0)
1133
208k
                    return code;
1134
12.5M
            } else {
1135
306k
                if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_unmatchedmark), NULL, E_PDF_UNMATCHEDMARK, "pdfi_read_token", NULL)) < 0) {
1136
0
                    return code;
1137
0
                }
1138
306k
                goto rescan;
1139
306k
            }
1140
12.3M
            break;
1141
12.3M
        case '{':
1142
317k
            if (ctx->args.pdfdebug)
1143
0
                outprintf (ctx->memory, "{");
1144
317k
            code = pdfi_mark_stack(ctx, PDF_PROC_MARK);
1145
317k
            if (code < 0)
1146
0
                return code;
1147
317k
            return 1;
1148
0
            break;
1149
282k
        case '}':
1150
282k
            pdfi_clear_to_mark(ctx);
1151
282k
            goto rescan;
1152
0
            break;
1153
1.65M
        case '%':
1154
1.65M
            pdfi_skip_comment(ctx, s);
1155
1.65M
            goto rescan;
1156
0
            break;
1157
119M
        default:
1158
119M
            if (isdelimiter(c)) {
1159
691k
                if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, 0, "pdfi_read_token", NULL)) < 0) {
1160
0
                    return code;
1161
0
                }
1162
691k
                goto rescan;
1163
691k
            }
1164
118M
            pdfi_unread_byte(ctx, s, (byte)c);
1165
118M
            code = pdfi_read_keyword(ctx, s, indirect_num, indirect_gen);
1166
118M
            if (code < 0)
1167
82.5k
                return code;
1168
118M
            return 1;
1169
0
            break;
1170
576M
    }
1171
318M
    return 1;
1172
576M
}
1173
1174
/* In contrast to the 'read' functions, the 'make' functions create an object with a
1175
 * reference count of 1. This indicates that the caller holds the reference. Thus the
1176
 * caller need not increment the reference count to the object, but must decrement
1177
 * it (pdf_countdown) before exiting.
1178
 */
1179
int pdfi_name_alloc(pdf_context *ctx, byte *n, uint32_t size, pdf_obj **o)
1180
143M
{
1181
143M
    int code;
1182
143M
    *o = NULL;
1183
1184
143M
    code = pdfi_object_alloc(ctx, PDF_NAME, size, o);
1185
143M
    if (code < 0)
1186
0
        return code;
1187
1188
143M
    memcpy(((pdf_name *)*o)->data, n, size);
1189
1190
143M
    return 0;
1191
143M
}
1192
1193
static char op_table_3[5][3] = {
1194
    "BDC", "BMC", "EMC", "SCN", "scn"
1195
};
1196
1197
static char op_table_2[39][2] = {
1198
    "b*", "BI", "BT", "BX", "cm", "CS", "cs", "EI", "d0", "d1", "Do", "DP", "ET", "EX", "f*", "gs", "ID", "MP", "re", "RG",
1199
    "rg", "ri", "SC", "sc", "sh", "T*", "Tc", "Td", "TD", "Tf", "Tj", "TJ", "TL", "Tm", "Tr", "Ts", "Tw", "Tz", "W*",
1200
};
1201
1202
static char op_table_1[27][1] = {
1203
    "b", "B", "c", "d", "f", "F", "G", "g", "h", "i", "j", "J", "K", "k", "l", "m", "n", "q", "Q", "s", "S", "v", "w", "W",
1204
    "y", "'", "\""
1205
};
1206
1207
/* forward definition for the 'split_bogus_operator' function to use */
1208
static int pdfi_interpret_stream_operator(pdf_context *ctx, pdf_c_stream *source,
1209
                                          pdf_dict *stream_dict, pdf_dict *page_dict);
1210
1211
static int
1212
make_keyword_obj(pdf_context *ctx, const byte *data, int length, pdf_keyword **pkey)
1213
2.14M
{
1214
2.14M
    byte Buffer[256];
1215
2.14M
    pdf_key key;
1216
2.14M
    int code;
1217
1218
2.14M
    if (length > 255)
1219
0
        return_error(gs_error_rangecheck);
1220
1221
2.14M
    memcpy(Buffer, data, length);
1222
2.14M
    Buffer[length] = 0;
1223
2.14M
    key = lookup_keyword(Buffer);
1224
2.14M
    if (key != TOKEN_INVALID_KEY) {
1225
        /* The common case. We've found a real key, just cast the token to
1226
         * a pointer, and return that. */
1227
2.14M
        *pkey = (pdf_keyword *)PDF_TOKEN_AS_OBJ(key);
1228
2.14M
        return 1;
1229
2.14M
    }
1230
    /* We still haven't found a real keyword. Allocate a new object and
1231
     * return it. */
1232
0
    code = pdfi_object_alloc(ctx, PDF_KEYWORD, length, (pdf_obj **)pkey);
1233
0
    if (code < 0)
1234
0
        return code;
1235
0
    if (length)
1236
0
        memcpy((*pkey)->data, Buffer, length);
1237
0
    pdfi_countup(*pkey);
1238
1239
0
    return 1;
1240
0
}
1241
1242
static int search_table_3(pdf_context *ctx, unsigned char *str, pdf_keyword **key)
1243
2.02M
{
1244
2.02M
    int i;
1245
1246
12.1M
    for (i = 0; i < 5; i++) {
1247
10.1M
        if (memcmp(str, op_table_3[i], 3) == 0)
1248
7.75k
            return make_keyword_obj(ctx, str, 3, key);
1249
10.1M
    }
1250
2.01M
    return 0;
1251
2.02M
}
1252
1253
static int search_table_2(pdf_context *ctx, unsigned char *str, pdf_keyword **key)
1254
4.71M
{
1255
4.71M
    int i;
1256
1257
183M
    for (i = 0; i < 39; i++) {
1258
178M
        if (memcmp(str, op_table_2[i], 2) == 0)
1259
271k
            return make_keyword_obj(ctx, str, 2, key);
1260
178M
    }
1261
4.44M
    return 0;
1262
4.71M
}
1263
1264
static int search_table_1(pdf_context *ctx, unsigned char *str, pdf_keyword **key)
1265
4.40M
{
1266
4.40M
    int i;
1267
1268
97.3M
    for (i = 0; i < 27; i++) {
1269
94.7M
        if (memcmp(str, op_table_1[i], 1) == 0)
1270
1.86M
            return make_keyword_obj(ctx, str, 1, key);
1271
94.7M
    }
1272
2.53M
    return 0;
1273
4.40M
}
1274
1275
static int split_bogus_operator(pdf_context *ctx, pdf_c_stream *source, pdf_dict *stream_dict, pdf_dict *page_dict)
1276
8.31M
{
1277
8.31M
    int code = 0;
1278
8.31M
    pdf_keyword *keyword = (pdf_keyword *)ctx->stack_top[-1], *key1 = NULL, *key2 = NULL;
1279
8.31M
    int length = keyword->length - 6;
1280
1281
8.31M
    if (length > 0) {
1282
        /* Longer than 2 3-character operators, we only allow for up to two
1283
         * operators. Check to see if it includes an endstream or endobj.
1284
         */
1285
2.68M
        if (memcmp(&keyword->data[length], "endobj", 6) == 0) {
1286
            /* Keyword is "<something>endobj". So make a keyword just from
1287
             * <something>, push that, execute it, then push endobj. */
1288
25
            code = make_keyword_obj(ctx, keyword->data, length, &key1);
1289
25
            if (code < 0)
1290
0
                goto error_exit;
1291
25
            pdfi_pop(ctx, 1);
1292
25
            pdfi_push(ctx, (pdf_obj *)key1);
1293
25
            pdfi_countdown(key1); /* Drop the reference returned by make_keyword_obj. */
1294
25
            code = pdfi_interpret_stream_operator(ctx, source, stream_dict, page_dict);
1295
25
            if (code < 0)
1296
14
                goto error_exit;
1297
11
            pdfi_push(ctx, PDF_TOKEN_AS_OBJ(TOKEN_ENDOBJ));
1298
11
            return 0;
1299
2.68M
        } else {
1300
2.68M
            length = keyword->length - 9;
1301
2.68M
            if (length > 0 && memcmp(&keyword->data[length], "endstream", 9) == 0) {
1302
                /* Keyword is "<something>endstream". So make a keyword just from
1303
                 * <something>, push that, execute it, then push endstream. */
1304
4
                code = make_keyword_obj(ctx, keyword->data, length, &key1);
1305
4
                if (code < 0)
1306
0
                    goto error_exit;
1307
4
                pdfi_pop(ctx, 1);
1308
4
                pdfi_push(ctx, (pdf_obj *)key1);
1309
4
                pdfi_countdown(key1); /* Drop the reference returned by make_keyword_obj. */
1310
4
                code = pdfi_interpret_stream_operator(ctx, source, stream_dict, page_dict);
1311
4
                if (code < 0)
1312
0
                    goto error_exit;
1313
4
                pdfi_push(ctx, PDF_TOKEN_AS_OBJ(TOKEN_ENDSTREAM));
1314
4
                return 0;
1315
2.68M
            } else {
1316
2.68M
                pdfi_clearstack(ctx);
1317
2.68M
                return 0;
1318
2.68M
            }
1319
2.68M
        }
1320
2.68M
    }
1321
1322
5.63M
    if (keyword->length > 3) {
1323
1.60M
        code = search_table_3(ctx, keyword->data, &key1);
1324
1.60M
        if (code < 0)
1325
0
            goto error_exit;
1326
1327
1.60M
        if (code > 0) {
1328
6.56k
            switch (keyword->length - 3) {
1329
5.16k
                case 1:
1330
5.16k
                    code = search_table_1(ctx, &keyword->data[3], &key2);
1331
5.16k
                    break;
1332
797
                case 2:
1333
797
                    code = search_table_2(ctx, &keyword->data[3], &key2);
1334
797
                    break;
1335
600
                case 3:
1336
600
                    code = search_table_3(ctx, &keyword->data[3], &key2);
1337
600
                    break;
1338
0
                default:
1339
0
                    goto error_exit;
1340
6.56k
            }
1341
6.56k
        }
1342
1.60M
        if (code < 0)
1343
0
            goto error_exit;
1344
1.60M
        if (code > 0)
1345
1.29k
            goto match;
1346
1.60M
    }
1347
5.63M
    pdfi_countdown(key1);
1348
5.63M
    pdfi_countdown(key2);
1349
5.63M
    key1 = NULL;
1350
5.63M
    key2 = NULL;
1351
1352
5.63M
    if (keyword->length > 5 || keyword->length < 2)
1353
1.45M
        goto error_exit;
1354
1355
4.17M
    code = search_table_2(ctx, keyword->data, &key1);
1356
4.17M
    if (code < 0)
1357
0
        goto error_exit;
1358
1359
4.17M
    if (code > 0) {
1360
231k
        switch(keyword->length - 2) {
1361
99.1k
            case 1:
1362
99.1k
                code = search_table_1(ctx, &keyword->data[2], &key2);
1363
99.1k
                break;
1364
85.9k
            case 2:
1365
85.9k
                code = search_table_2(ctx, &keyword->data[2], &key2);
1366
85.9k
                break;
1367
46.7k
            case 3:
1368
46.7k
                code = search_table_3(ctx, &keyword->data[2], &key2);
1369
46.7k
                break;
1370
0
            default:
1371
0
                goto error_exit;
1372
231k
        }
1373
231k
        if (code < 0)
1374
0
            goto error_exit;
1375
231k
        if (code > 0)
1376
47.4k
            goto match;
1377
231k
    }
1378
4.12M
    pdfi_countdown(key1);
1379
4.12M
    pdfi_countdown(key2);
1380
4.12M
    key1 = NULL;
1381
4.12M
    key2 = NULL;
1382
1383
4.12M
    if (keyword->length > 4)
1384
586k
        goto error_exit;
1385
1386
3.54M
    code = search_table_1(ctx, keyword->data, &key1);
1387
3.54M
    if (code <= 0)
1388
1.95M
        goto error_exit;
1389
1390
1.58M
    switch(keyword->length - 1) {
1391
755k
        case 1:
1392
755k
            code = search_table_1(ctx, &keyword->data[1], &key2);
1393
755k
            break;
1394
456k
        case 2:
1395
456k
            code = search_table_2(ctx, &keyword->data[1], &key2);
1396
456k
            break;
1397
373k
        case 3:
1398
373k
            code = search_table_3(ctx, &keyword->data[1], &key2);
1399
373k
            break;
1400
0
        default:
1401
0
            goto error_exit;
1402
1.58M
    }
1403
1.58M
    if (code <= 0)
1404
1.31M
        goto error_exit;
1405
1406
317k
match:
1407
317k
    pdfi_set_warning(ctx, 0, NULL, W_PDF_MISSING_WHITE_OPS, "split_bogus_operator", NULL);
1408
    /* If we get here, we have two PDF_KEYWORD objects. We push them on the stack
1409
     * one at a time, and execute them.
1410
     */
1411
317k
    pdfi_push(ctx, (pdf_obj *)key1);
1412
317k
    code = pdfi_interpret_stream_operator(ctx, source, stream_dict, page_dict);
1413
317k
    if (code < 0)
1414
248k
        goto error_exit;
1415
1416
69.3k
    pdfi_push(ctx, (pdf_obj *)key2);
1417
69.3k
    code = pdfi_interpret_stream_operator(ctx, source, stream_dict, page_dict);
1418
1419
69.3k
    pdfi_countdown(key1);
1420
69.3k
    pdfi_countdown(key2);
1421
69.3k
    pdfi_clearstack(ctx);
1422
69.3k
    return code;
1423
1424
5.56M
error_exit:
1425
5.56M
    pdfi_set_error(ctx, code, NULL, E_PDF_TOKENERROR, "split_bogus_operator", NULL);
1426
5.56M
    pdfi_countdown(key1);
1427
5.56M
    pdfi_countdown(key2);
1428
5.56M
    pdfi_clearstack(ctx);
1429
5.56M
    return code;
1430
317k
}
1431
1432
static int pdfi_interpret_stream_operator(pdf_context *ctx, pdf_c_stream *source,
1433
                                          pdf_dict *stream_dict, pdf_dict *page_dict)
1434
79.5M
{
1435
79.5M
    pdf_obj *keyword = ctx->stack_top[-1];
1436
79.5M
    int code = 0;
1437
1438
79.5M
    if (keyword < PDF_TOKEN_AS_OBJ(TOKEN__LAST_KEY))
1439
71.2M
    {
1440
71.2M
        switch((uintptr_t)keyword) {
1441
16.9k
            case TOKEN_b:           /* closepath, fill, stroke */
1442
16.9k
                pdfi_pop(ctx, 1);
1443
16.9k
                code = pdfi_b(ctx);
1444
16.9k
                break;
1445
43.9k
            case TOKEN_B:           /* fill, stroke */
1446
43.9k
                pdfi_pop(ctx, 1);
1447
43.9k
                code = pdfi_B(ctx);
1448
43.9k
                break;
1449
433
            case TOKEN_bstar:       /* closepath, eofill, stroke */
1450
433
                pdfi_pop(ctx, 1);
1451
433
                code = pdfi_b_star(ctx);
1452
433
                break;
1453
1.75k
            case TOKEN_Bstar:       /* eofill, stroke */
1454
1.75k
                pdfi_pop(ctx, 1);
1455
1.75k
                code = pdfi_B_star(ctx);
1456
1.75k
                break;
1457
245k
            case TOKEN_BI:       /* begin inline image */
1458
245k
                pdfi_pop(ctx, 1);
1459
245k
                code = pdfi_BI(ctx);
1460
245k
                break;
1461
324k
            case TOKEN_BDC:   /* begin marked content sequence with property list */
1462
324k
                pdfi_pop(ctx, 1);
1463
324k
                code = pdfi_op_BDC(ctx, stream_dict, page_dict);
1464
324k
                break;
1465
16.8k
            case TOKEN_BMC:   /* begin marked content sequence */
1466
16.8k
                pdfi_pop(ctx, 1);
1467
16.8k
                code = pdfi_op_BMC(ctx);
1468
16.8k
                break;
1469
1.86M
            case TOKEN_BT:       /* begin text */
1470
1.86M
                pdfi_pop(ctx, 1);
1471
1.86M
                code = pdfi_BT(ctx);
1472
1.86M
                break;
1473
55.4k
            case TOKEN_BX:       /* begin compatibility section */
1474
55.4k
                pdfi_pop(ctx, 1);
1475
55.4k
                break;
1476
11.0M
            case TOKEN_c:           /* curveto */
1477
11.0M
                pdfi_pop(ctx, 1);
1478
11.0M
                code = pdfi_curveto(ctx);
1479
11.0M
                break;
1480
2.32M
            case TOKEN_cm:       /* concat */
1481
2.32M
                pdfi_pop(ctx, 1);
1482
2.32M
                code = pdfi_concat(ctx);
1483
2.32M
                break;
1484
25.9k
            case TOKEN_CS:       /* set stroke colour space */
1485
25.9k
                pdfi_pop(ctx, 1);
1486
25.9k
                code = pdfi_setstrokecolor_space(ctx, stream_dict, page_dict);
1487
25.9k
                break;
1488
221k
            case TOKEN_cs:       /* set non-stroke colour space */
1489
221k
                pdfi_pop(ctx, 1);
1490
221k
                code = pdfi_setfillcolor_space(ctx, stream_dict, page_dict);
1491
221k
                break;
1492
797k
            case TOKEN_d:           /* set dash params */
1493
797k
                pdfi_pop(ctx, 1);
1494
797k
                code = pdfi_setdash(ctx);
1495
797k
                break;
1496
3.35k
            case TOKEN_d0:       /* set type 3 font glyph width */
1497
3.35k
                pdfi_pop(ctx, 1);
1498
3.35k
                code = pdfi_d0(ctx);
1499
3.35k
                break;
1500
37.8k
            case TOKEN_d1:       /* set type 3 font glyph width and bounding box */
1501
37.8k
                pdfi_pop(ctx, 1);
1502
37.8k
                code = pdfi_d1(ctx);
1503
37.8k
                break;
1504
263k
            case TOKEN_Do:       /* invoke named XObject */
1505
263k
                pdfi_pop(ctx, 1);
1506
263k
                code = pdfi_Do(ctx, stream_dict, page_dict);
1507
263k
                break;
1508
838
            case TOKEN_DP:       /* define marked content point with property list */
1509
838
                pdfi_pop(ctx, 1);
1510
838
                code = pdfi_op_DP(ctx, stream_dict, page_dict);
1511
838
                break;
1512
241k
            case TOKEN_EI:       /* end inline image */
1513
241k
                pdfi_pop(ctx, 1);
1514
241k
                code = pdfi_EI(ctx);
1515
241k
                break;
1516
1.78M
            case TOKEN_ET:       /* end text */
1517
1.78M
                pdfi_pop(ctx, 1);
1518
1.78M
                code = pdfi_ET(ctx);
1519
1.78M
                break;
1520
334k
            case TOKEN_EMC:   /* end marked content sequence */
1521
334k
                pdfi_pop(ctx, 1);
1522
334k
                code = pdfi_op_EMC(ctx);
1523
334k
                break;
1524
52.6k
            case TOKEN_EX:       /* end compatibility section */
1525
52.6k
                pdfi_pop(ctx, 1);
1526
52.6k
                break;
1527
2.29M
            case TOKEN_f:           /* fill */
1528
2.29M
                pdfi_pop(ctx, 1);
1529
2.29M
                code = pdfi_fill(ctx);
1530
2.29M
                break;
1531
15.4k
            case TOKEN_F:           /* fill (obselete operator) */
1532
15.4k
                pdfi_pop(ctx, 1);
1533
15.4k
                code = pdfi_fill(ctx);
1534
15.4k
                break;
1535
47.9k
            case TOKEN_fstar:       /* eofill */
1536
47.9k
                pdfi_pop(ctx, 1);
1537
47.9k
                code = pdfi_eofill(ctx);
1538
47.9k
                break;
1539
535k
            case TOKEN_G:           /* setgray for stroke */
1540
535k
                pdfi_pop(ctx, 1);
1541
535k
                code = pdfi_setgraystroke(ctx);
1542
535k
                break;
1543
691k
            case TOKEN_g:           /* setgray for non-stroke */
1544
691k
                pdfi_pop(ctx, 1);
1545
691k
                code = pdfi_setgrayfill(ctx);
1546
691k
                break;
1547
374k
            case TOKEN_gs:       /* set graphics state from dictionary */
1548
374k
                pdfi_pop(ctx, 1);
1549
374k
                code = pdfi_setgstate(ctx, stream_dict, page_dict);
1550
374k
                break;
1551
471k
            case TOKEN_h:           /* closepath */
1552
471k
                pdfi_pop(ctx, 1);
1553
471k
                code = pdfi_closepath(ctx);
1554
471k
                break;
1555
159k
            case TOKEN_i:           /* setflat */
1556
159k
                pdfi_pop(ctx, 1);
1557
159k
                code = pdfi_setflat(ctx);
1558
159k
                break;
1559
245k
            case TOKEN_ID:       /* begin inline image data */
1560
245k
                pdfi_pop(ctx, 1);
1561
245k
                code = pdfi_ID(ctx, stream_dict, page_dict, source);
1562
245k
                break;
1563
774k
            case TOKEN_j:           /* setlinejoin */
1564
774k
                pdfi_pop(ctx, 1);
1565
774k
                code = pdfi_setlinejoin(ctx);
1566
774k
                break;
1567
903k
            case TOKEN_J:           /* setlinecap */
1568
903k
                pdfi_pop(ctx, 1);
1569
903k
                code = pdfi_setlinecap(ctx);
1570
903k
                break;
1571
47.0k
            case TOKEN_K:           /* setcmyk for non-stroke */
1572
47.0k
                pdfi_pop(ctx, 1);
1573
47.0k
                code = pdfi_setcmykstroke(ctx);
1574
47.0k
                break;
1575
168k
            case TOKEN_k:           /* setcmyk for non-stroke */
1576
168k
                pdfi_pop(ctx, 1);
1577
168k
                code = pdfi_setcmykfill(ctx);
1578
168k
                break;
1579
11.5M
            case TOKEN_l:           /* lineto */
1580
11.5M
                pdfi_pop(ctx, 1);
1581
11.5M
                code = pdfi_lineto(ctx);
1582
11.5M
                break;
1583
5.53M
            case TOKEN_m:           /* moveto */
1584
5.53M
                pdfi_pop(ctx, 1);
1585
5.53M
                code = pdfi_moveto(ctx);
1586
5.53M
                break;
1587
42.0k
            case TOKEN_M:           /* setmiterlimit */
1588
42.0k
                pdfi_pop(ctx, 1);
1589
42.0k
                code = pdfi_setmiterlimit(ctx);
1590
42.0k
                break;
1591
2.35k
            case TOKEN_MP:       /* define marked content point */
1592
2.35k
                pdfi_pop(ctx, 1);
1593
2.35k
                code = pdfi_op_MP(ctx);
1594
2.35k
                break;
1595
1.13M
            case TOKEN_n:           /* newpath */
1596
1.13M
                pdfi_pop(ctx, 1);
1597
1.13M
                code = pdfi_newpath(ctx);
1598
1.13M
                break;
1599
2.97M
            case TOKEN_q:           /* gsave */
1600
2.97M
                pdfi_pop(ctx, 1);
1601
2.97M
                code = pdfi_op_q(ctx);
1602
2.97M
                break;
1603
2.88M
            case TOKEN_Q:           /* grestore */
1604
2.88M
                pdfi_pop(ctx, 1);
1605
2.88M
                code = pdfi_op_Q(ctx);
1606
2.88M
                break;
1607
55.9k
            case TOKEN_r:       /* non-standard set rgb colour for non-stroke */
1608
55.9k
                pdfi_pop(ctx, 1);
1609
55.9k
                code = pdfi_setrgbfill_array(ctx);
1610
55.9k
                break;
1611
2.86M
            case TOKEN_re:       /* append rectangle */
1612
2.86M
                pdfi_pop(ctx, 1);
1613
2.86M
                code = pdfi_rectpath(ctx);
1614
2.86M
                break;
1615
864k
            case TOKEN_RG:       /* set rgb colour for stroke */
1616
864k
                pdfi_pop(ctx, 1);
1617
864k
                code = pdfi_setrgbstroke(ctx);
1618
864k
                break;
1619
994k
            case TOKEN_rg:       /* set rgb colour for non-stroke */
1620
994k
                pdfi_pop(ctx, 1);
1621
994k
                code = pdfi_setrgbfill(ctx);
1622
994k
                break;
1623
80.6k
            case TOKEN_ri:       /* set rendering intent */
1624
80.6k
                pdfi_pop(ctx, 1);
1625
80.6k
                code = pdfi_ri(ctx);
1626
80.6k
                break;
1627
81.7k
            case TOKEN_s:           /* closepath, stroke */
1628
81.7k
                pdfi_pop(ctx, 1);
1629
81.7k
                code = pdfi_closepath_stroke(ctx);
1630
81.7k
                break;
1631
2.65M
            case TOKEN_S:           /* stroke */
1632
2.65M
                pdfi_pop(ctx, 1);
1633
2.65M
                code = pdfi_stroke(ctx);
1634
2.65M
                break;
1635
31.8k
            case TOKEN_SC:       /* set colour for stroke */
1636
31.8k
                pdfi_pop(ctx, 1);
1637
31.8k
                code = pdfi_setstrokecolor(ctx);
1638
31.8k
                break;
1639
143k
            case TOKEN_sc:       /* set colour for non-stroke */
1640
143k
                pdfi_pop(ctx, 1);
1641
143k
                code = pdfi_setfillcolor(ctx);
1642
143k
                break;
1643
12.1k
            case TOKEN_SCN:   /* set special colour for stroke */
1644
12.1k
                pdfi_pop(ctx, 1);
1645
12.1k
                code = pdfi_setcolorN(ctx, stream_dict, page_dict, false);
1646
12.1k
                break;
1647
119k
            case TOKEN_scn:   /* set special colour for non-stroke */
1648
119k
                pdfi_pop(ctx, 1);
1649
119k
                code = pdfi_setcolorN(ctx, stream_dict, page_dict, true);
1650
119k
                break;
1651
72.5k
            case TOKEN_sh:       /* fill with sahding pattern */
1652
72.5k
                pdfi_pop(ctx, 1);
1653
72.5k
                code = pdfi_shading(ctx, stream_dict, page_dict);
1654
72.5k
                break;
1655
137k
            case TOKEN_Tstar:       /* Move to start of next text line */
1656
137k
                pdfi_pop(ctx, 1);
1657
137k
                code = pdfi_T_star(ctx);
1658
137k
                break;
1659
712k
            case TOKEN_Tc:       /* set character spacing */
1660
712k
                pdfi_pop(ctx, 1);
1661
712k
                code = pdfi_Tc(ctx);
1662
712k
                break;
1663
1.17M
            case TOKEN_Td:       /* move text position */
1664
1.17M
                pdfi_pop(ctx, 1);
1665
1.17M
                code = pdfi_Td(ctx);
1666
1.17M
                break;
1667
390k
            case TOKEN_TD:       /* Move text position, set leading */
1668
390k
                pdfi_pop(ctx, 1);
1669
390k
                code = pdfi_TD(ctx);
1670
390k
                break;
1671
1.12M
            case TOKEN_Tf:       /* set font and size */
1672
1.12M
                pdfi_pop(ctx, 1);
1673
1.12M
                code = pdfi_Tf(ctx, stream_dict, page_dict);
1674
1.12M
                break;
1675
1.67M
            case TOKEN_Tj:       /* show text */
1676
1.67M
                pdfi_pop(ctx, 1);
1677
1.67M
                code = pdfi_Tj(ctx);
1678
1.67M
                break;
1679
1.68M
            case TOKEN_TJ:       /* show text with individual glyph positioning */
1680
1.68M
                pdfi_pop(ctx, 1);
1681
1.68M
                code = pdfi_TJ(ctx);
1682
1.68M
                break;
1683
14.0k
            case TOKEN_TL:       /* set text leading */
1684
14.0k
                pdfi_pop(ctx, 1);
1685
14.0k
                code = pdfi_TL(ctx);
1686
14.0k
                break;
1687
1.68M
            case TOKEN_Tm:       /* set text matrix */
1688
1.68M
                pdfi_pop(ctx, 1);
1689
1.68M
                code = pdfi_Tm(ctx);
1690
1.68M
                break;
1691
665k
            case TOKEN_Tr:       /* set text rendering mode */
1692
665k
                pdfi_pop(ctx, 1);
1693
665k
                code = pdfi_Tr(ctx);
1694
665k
                break;
1695
3.76k
            case TOKEN_Ts:       /* set text rise */
1696
3.76k
                pdfi_pop(ctx, 1);
1697
3.76k
                code = pdfi_Ts(ctx);
1698
3.76k
                break;
1699
155k
            case TOKEN_Tw:       /* set word spacing */
1700
155k
                pdfi_pop(ctx, 1);
1701
155k
                code = pdfi_Tw(ctx);
1702
155k
                break;
1703
125k
            case TOKEN_Tz:       /* set text matrix */
1704
125k
                pdfi_pop(ctx, 1);
1705
125k
                code = pdfi_Tz(ctx);
1706
125k
                break;
1707
167k
            case TOKEN_v:           /* append curve (initial point replicated) */
1708
167k
                pdfi_pop(ctx, 1);
1709
167k
                code = pdfi_v_curveto(ctx);
1710
167k
                break;
1711
2.05M
            case TOKEN_w:           /* setlinewidth */
1712
2.05M
                pdfi_pop(ctx, 1);
1713
2.05M
                code = pdfi_setlinewidth(ctx);
1714
2.05M
                break;
1715
614k
            case TOKEN_W:           /* clip */
1716
614k
                pdfi_pop(ctx, 1);
1717
614k
                ctx->clip_active = true;
1718
614k
                ctx->do_eoclip = false;
1719
614k
                break;
1720
54.2k
            case TOKEN_Wstar:       /* eoclip */
1721
54.2k
                pdfi_pop(ctx, 1);
1722
54.2k
                ctx->clip_active = true;
1723
54.2k
                ctx->do_eoclip = true;
1724
54.2k
                break;
1725
188k
            case TOKEN_y:           /* append curve (final point replicated) */
1726
188k
                pdfi_pop(ctx, 1);
1727
188k
                code = pdfi_y_curveto(ctx);
1728
188k
                break;
1729
18.4k
            case TOKEN_APOSTROPHE:          /* move to next line and show text */
1730
18.4k
                pdfi_pop(ctx, 1);
1731
18.4k
                code = pdfi_singlequote(ctx);
1732
18.4k
                break;
1733
7.75k
            case TOKEN_QUOTE:           /* set word and character spacing, move to next line, show text */
1734
7.75k
                pdfi_pop(ctx, 1);
1735
7.75k
                code = pdfi_doublequote(ctx);
1736
7.75k
                break;
1737
3.89k
            default:
1738
                /* Shouldn't we return an error here? Original code didn't seem to. */
1739
3.89k
                break;
1740
71.2M
        }
1741
        /* We use a return value of 1 to indicate a repaired keyword (a pair of operators
1742
         * was concatenated, and we split them up). We must not return a value > 0 from here
1743
         * to avoid tripping that test.
1744
         */
1745
71.2M
        if (code > 0)
1746
0
            code = 0;
1747
71.2M
        return code;
1748
71.2M
    } else {
1749
        /* This means we either have a corrupted or illegal operator. The most
1750
         * usual corruption is two concatented operators (eg QBT instead of Q BT)
1751
         * I plan to tackle this by trying to see if I can make two or more operators
1752
         * out of the mangled one.
1753
         */
1754
8.31M
        code = split_bogus_operator(ctx, source, stream_dict, page_dict);
1755
8.31M
        if (code < 0)
1756
287k
            return code;
1757
8.02M
        if (pdfi_count_stack(ctx) > 0) {
1758
15
            keyword = ctx->stack_top[-1];
1759
15
            if (keyword != PDF_TOKEN_AS_OBJ(TOKEN_NOT_A_KEYWORD))
1760
15
                return REPAIRED_KEYWORD;
1761
15
        }
1762
8.02M
    }
1763
8.02M
    return 0;
1764
79.5M
}
1765
1766
void local_save_stream_state(pdf_context *ctx, stream_save *local_save)
1767
635k
{
1768
    /* copy the 'save_stream' data from the context to a local structure */
1769
635k
    local_save->stream_offset = ctx->current_stream_save.stream_offset;
1770
635k
    local_save->gsave_level = ctx->current_stream_save.gsave_level;
1771
635k
    local_save->stack_count = ctx->current_stream_save.stack_count;
1772
635k
    local_save->group_depth = ctx->current_stream_save.group_depth;
1773
635k
}
1774
1775
void cleanup_context_interpretation(pdf_context *ctx, stream_save *local_save)
1776
635k
{
1777
635k
    pdfi_seek(ctx, ctx->main_stream, ctx->current_stream_save.stream_offset, SEEK_SET);
1778
    /* The transparency group implenetation does a gsave, so the end group does a
1779
     * grestore. Therefore we need to do this before we check the saved gstate depth
1780
     */
1781
635k
    if (ctx->current_stream_save.group_depth != local_save->group_depth) {
1782
9
        pdfi_set_warning(ctx, 0, NULL, W_PDF_GROUPERROR, "pdfi_cleanup_context_interpretation", NULL);
1783
18
        while (ctx->current_stream_save.group_depth > local_save->group_depth)
1784
9
            pdfi_trans_end_group(ctx);
1785
9
    }
1786
635k
    if (ctx->pgs->level > ctx->current_stream_save.gsave_level)
1787
20.7k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TOOMANYq, "pdfi_cleanup_context_interpretation", NULL);
1788
635k
    if (pdfi_count_stack(ctx) > ctx->current_stream_save.stack_count)
1789
7.45k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_STACKGARBAGE, "pdfi_cleanup_context_interpretation", NULL);
1790
851k
    while (ctx->pgs->level > ctx->current_stream_save.gsave_level)
1791
216k
        pdfi_grestore(ctx);
1792
635k
    pdfi_clearstack(ctx);
1793
635k
}
1794
1795
void local_restore_stream_state(pdf_context *ctx, stream_save *local_save)
1796
635k
{
1797
    /* Put the entries stored in the context back to what they were on entry
1798
     * We shouldn't really need to do this, the cleanup above should mean all the
1799
     * entries are properly reset.
1800
     */
1801
635k
    ctx->current_stream_save.stream_offset = local_save->stream_offset;
1802
635k
    ctx->current_stream_save.gsave_level = local_save->gsave_level;
1803
635k
    ctx->current_stream_save.stack_count = local_save->stack_count;
1804
635k
    ctx->current_stream_save.group_depth = local_save->group_depth;
1805
635k
}
1806
1807
void initialise_stream_save(pdf_context *ctx)
1808
635k
{
1809
    /* Set up the values in the context to the current values */
1810
635k
    ctx->current_stream_save.stream_offset = pdfi_tell(ctx->main_stream);
1811
635k
    ctx->current_stream_save.gsave_level = ctx->pgs->level;
1812
635k
    ctx->current_stream_save.stack_count = pdfi_count_total_stack(ctx);
1813
635k
}
1814
1815
/* Run a stream in a sub-context (saves/restores DefaultQState) */
1816
int pdfi_run_context(pdf_context *ctx, pdf_stream *stream_obj,
1817
                     pdf_dict *page_dict, bool stoponerror, const char *desc)
1818
313k
{
1819
313k
    int code = 0, code1 = 0;
1820
313k
    gs_gstate *DefaultQState = NULL;
1821
    /* Save any existing Default* colour spaces */
1822
313k
    gs_color_space *PageDefaultGray = ctx->page.DefaultGray_cs;
1823
313k
    gs_color_space *PageDefaultRGB = ctx->page.DefaultRGB_cs;
1824
313k
    gs_color_space *PageDefaultCMYK = ctx->page.DefaultCMYK_cs;
1825
1826
313k
    ctx->page.DefaultGray_cs = NULL;
1827
313k
    ctx->page.DefaultRGB_cs = NULL;
1828
313k
    ctx->page.DefaultCMYK_cs = NULL;
1829
1830
#if DEBUG_CONTEXT
1831
    dbgmprintf(ctx->memory, "pdfi_run_context BEGIN\n");
1832
#endif
1833
    /* If the stream has any Default* colour spaces, replace the page level ones.
1834
     * This will derement the reference counts to the current spaces if they are replaced.
1835
     */
1836
313k
    code = pdfi_setup_DefaultSpaces(ctx, stream_obj->stream_dict);
1837
313k
    if (code < 0)
1838
0
        goto exit;
1839
1840
    /* If no Default* space found, try using the Page level ones (if any) */
1841
313k
    if (ctx->page.DefaultGray_cs == NULL) {
1842
313k
        ctx->page.DefaultGray_cs = PageDefaultGray;
1843
313k
        rc_increment(PageDefaultGray);
1844
313k
    }
1845
313k
    if (ctx->page.DefaultRGB_cs == NULL) {
1846
313k
        ctx->page.DefaultRGB_cs = PageDefaultRGB;
1847
313k
        rc_increment(PageDefaultRGB);
1848
313k
    }
1849
313k
    if (ctx->page.DefaultCMYK_cs == NULL) {
1850
313k
        ctx->page.DefaultCMYK_cs = PageDefaultCMYK;
1851
313k
        rc_increment(PageDefaultCMYK);
1852
313k
    }
1853
1854
313k
    code = pdfi_copy_DefaultQState(ctx, &DefaultQState);
1855
313k
    if (code < 0)
1856
0
        goto exit;
1857
1858
313k
    code = pdfi_set_DefaultQState(ctx, ctx->pgs);
1859
313k
    if (code < 0)
1860
0
        goto exit;
1861
1862
313k
    code = pdfi_interpret_inner_content_stream(ctx, stream_obj, page_dict, stoponerror, desc);
1863
1864
313k
    code1 = pdfi_restore_DefaultQState(ctx, &DefaultQState);
1865
313k
    if (code >= 0)
1866
313k
        code = code1;
1867
1868
313k
exit:
1869
313k
    if (DefaultQState != NULL) {
1870
0
        gs_gstate_free(DefaultQState);
1871
0
        DefaultQState = NULL;
1872
0
    }
1873
1874
    /* Count down any Default* colour spaces */
1875
313k
    rc_decrement(ctx->page.DefaultGray_cs, "pdfi_run_context");
1876
313k
    rc_decrement(ctx->page.DefaultRGB_cs, "pdfi_run_context");
1877
313k
    rc_decrement(ctx->page.DefaultCMYK_cs, "pdfi_run_context");
1878
1879
    /* And restore the page level ones (if any) */
1880
313k
    ctx->page.DefaultGray_cs = PageDefaultGray;
1881
313k
    ctx->page.DefaultRGB_cs = PageDefaultRGB;
1882
313k
    ctx->page.DefaultCMYK_cs = PageDefaultCMYK;
1883
1884
#if DEBUG_CONTEXT
1885
    dbgmprintf(ctx->memory, "pdfi_run_context END\n");
1886
#endif
1887
313k
    return code;
1888
313k
}
1889
1890
1891
/* Interpret a sub-content stream, with some handling of error recovery, clearing stack, etc.
1892
 * This temporarily turns on pdfstoponerror if requested.
1893
 * It will make sure the stack is cleared and the gstate is matched.
1894
 */
1895
static int
1896
pdfi_interpret_inner_content(pdf_context *ctx, pdf_c_stream *content_stream, pdf_stream *stream_obj,
1897
                             pdf_dict *page_dict, bool stoponerror, const char *desc)
1898
350k
{
1899
350k
    int code = 0;
1900
350k
    bool saved_stoponerror = ctx->args.pdfstoponerror;
1901
350k
    stream_save local_entry_save;
1902
1903
350k
    local_save_stream_state(ctx, &local_entry_save);
1904
350k
    initialise_stream_save(ctx);
1905
1906
    /* This causes several files to render 'incorrectly', even though they are in some sense
1907
     * invalid. It doesn't seem to provide any benefits so I have, for now, removed it. If
1908
     * there is a good reason for it we can put it back again.
1909
     * FIXME - either restore or remove these lines
1910
     * /tests_private/pdf/PDF_2.0_FTS/fts_23_2310.pdf
1911
     * /tests_private/pdf/PDF_2.0_FTS/fts_23_2311.pdf
1912
     * /tests_private/pdf/PDF_2.0_FTS/fts_23_2312.pdf
1913
     * /tests_private/pdf/sumatra/recursive_colorspace.pdf
1914
     * /tests_private/pdf/uploads/Bug696410.pdf
1915
     * /tests_private/pdf/sumatra/1900_-_cairo_transparency_inefficiency.pdf (with pdfwrite)
1916
     */
1917
#if 0
1918
    /* Stop on error in substream, and also be prepared to clean up the stack */
1919
    if (stoponerror)
1920
        ctx->args.pdfstoponerror = true;
1921
#endif
1922
1923
#if DEBUG_CONTEXT
1924
    dbgmprintf1(ctx->memory, "BEGIN %s stream\n", desc);
1925
#endif
1926
350k
    code = pdfi_interpret_content_stream(ctx, content_stream, stream_obj, page_dict);
1927
#if DEBUG_CONTEXT
1928
    dbgmprintf1(ctx->memory, "END %s stream\n", desc);
1929
#endif
1930
1931
350k
    if (code < 0)
1932
350k
        dbgmprintf1(ctx->memory, "ERROR: inner_stream: code %d when rendering stream\n", code);
1933
1934
350k
    ctx->args.pdfstoponerror = saved_stoponerror;
1935
1936
    /* Put our state back the way it was on entry */
1937
#if PROBE_STREAMS
1938
    if (ctx->pgs->level > ctx->current_stream_save.gsave_level ||
1939
        pdfi_count_stack(ctx) > ctx->current_stream_save.stack_count)
1940
        code = ((pdf_context *)0)->first_page;
1941
#endif
1942
1943
350k
    cleanup_context_interpretation(ctx, &local_entry_save);
1944
350k
    local_restore_stream_state(ctx, &local_entry_save);
1945
350k
    if (code < 0)
1946
7.11k
        code = pdfi_set_error_stop(ctx, code, NULL, 0, "pdfi_interpret_inner_content", NULL);
1947
350k
    return code;
1948
350k
}
1949
1950
/* Interpret inner content from a buffer
1951
 */
1952
int
1953
pdfi_interpret_inner_content_buffer(pdf_context *ctx, byte *content_data,
1954
                                      uint32_t content_length,
1955
                                      pdf_dict *stream_dict, pdf_dict *page_dict,
1956
                                      bool stoponerror, const char *desc)
1957
37.1k
{
1958
37.1k
    int code = 0;
1959
37.1k
    pdf_c_stream *stream = NULL;
1960
37.1k
    pdf_stream *stream_obj = NULL;
1961
1962
37.1k
    if (content_length == 0)
1963
0
        return 0;
1964
1965
37.1k
    code = pdfi_open_memory_stream_from_memory(ctx, content_length,
1966
37.1k
                                               content_data, &stream, true);
1967
37.1k
    if (code < 0)
1968
0
        goto exit;
1969
1970
37.1k
    code = pdfi_obj_dict_to_stream(ctx, stream_dict, &stream_obj, false);
1971
37.1k
    if (code < 0)
1972
0
        return code;
1973
1974
    /* NOTE: stream gets closed in here */
1975
37.1k
    code = pdfi_interpret_inner_content(ctx, stream, stream_obj, page_dict, stoponerror, desc);
1976
37.1k
    pdfi_countdown(stream_obj);
1977
37.1k
 exit:
1978
37.1k
    return code;
1979
37.1k
}
1980
1981
/* Interpret inner content from a C string
1982
 */
1983
int
1984
pdfi_interpret_inner_content_c_string(pdf_context *ctx, char *content_string,
1985
                                      pdf_dict *stream_dict, pdf_dict *page_dict,
1986
                                      bool stoponerror, const char *desc)
1987
22.3k
{
1988
22.3k
    uint32_t length = (uint32_t)strlen(content_string);
1989
22.3k
    bool decrypt_strings;
1990
22.3k
    int code;
1991
1992
22.3k
    if (length == 0)
1993
0
        return 0;
1994
1995
    /* Underlying buffer limit is uint32, so handle the extremely unlikely case that
1996
     * our string is too big.
1997
     */
1998
22.3k
    if (length != strlen(content_string))
1999
0
        return_error(gs_error_limitcheck);
2000
2001
    /* Since this is a constructed string content, not part of the file, it can never
2002
     * be encrypted. So disable decryption during this call.
2003
     */
2004
22.3k
    decrypt_strings = ctx->encryption.decrypt_strings;
2005
22.3k
    ctx->encryption.decrypt_strings = false;
2006
22.3k
    code = pdfi_interpret_inner_content_buffer(ctx, (byte *)content_string, length,
2007
22.3k
                                               stream_dict, page_dict, stoponerror, desc);
2008
22.3k
    ctx->encryption.decrypt_strings = decrypt_strings;
2009
2010
22.3k
    return code;
2011
22.3k
}
2012
2013
/* Interpret inner content from a string
2014
 */
2015
int
2016
pdfi_interpret_inner_content_string(pdf_context *ctx, pdf_string *content_string,
2017
                                    pdf_dict *stream_dict, pdf_dict *page_dict,
2018
                                    bool stoponerror, const char *desc)
2019
14.7k
{
2020
14.7k
    return pdfi_interpret_inner_content_buffer(ctx, content_string->data, content_string->length,
2021
14.7k
                                               stream_dict, page_dict, stoponerror, desc);
2022
14.7k
}
2023
2024
/* Interpret inner content from a stream_dict
2025
 */
2026
int
2027
pdfi_interpret_inner_content_stream(pdf_context *ctx, pdf_stream *stream_obj,
2028
                                    pdf_dict *page_dict, bool stoponerror, const char *desc)
2029
313k
{
2030
313k
    return pdfi_interpret_inner_content(ctx, NULL, stream_obj, page_dict, stoponerror, desc);
2031
313k
}
2032
2033
/*
2034
 * Interpret a content stream.
2035
 * content_stream -- content to parse.  If NULL, get it from the stream_dict
2036
 * stream_dict -- dict containing the stream
2037
 */
2038
int
2039
pdfi_interpret_content_stream(pdf_context *ctx, pdf_c_stream *content_stream,
2040
                              pdf_stream *stream_obj, pdf_dict *page_dict)
2041
494k
{
2042
494k
    int code;
2043
494k
    pdf_c_stream *stream = NULL, *SubFile_stream = NULL;
2044
494k
    pdf_keyword *keyword;
2045
494k
    pdf_stream *s = ctx->current_stream;
2046
494k
    pdf_obj_type type;
2047
494k
    char EODString[] = "endstream";
2048
2049
    /* Check this stream, and all the streams currently being executed, to see
2050
     * if the stream we've been given is already in train. If it is, then we
2051
     * have encountered recursion. This can happen if a non-page stream such
2052
     * as a Form or Pattern uses a Resource, but does not declare it in it's
2053
     * Resources, and instead inherits it from the parent. We cannot detect that
2054
     * before the Resource is used, so all we can do is check here.
2055
     */
2056
684k
    while (s != NULL && pdfi_type_of(s) == PDF_STREAM) {
2057
189k
        if (s->object_num > 0) {
2058
188k
            if (s->object_num == stream_obj->object_num) {
2059
237
                pdf_dict *d = NULL;
2060
237
                bool known = false;
2061
2062
237
                code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream_obj, &d);
2063
237
                if (code >= 0) {
2064
237
                    code = pdfi_dict_known(ctx, d, "Parent", &known);
2065
237
                    if (code >= 0 && known)
2066
48
                        (void)pdfi_dict_delete(ctx, d, "Parent");
2067
237
                }
2068
237
                pdfi_set_error(ctx, 0, NULL, E_PDF_CIRCULARREF, "pdfi_interpret_content_stream", "Aborting stream");
2069
237
                return_error(gs_error_circular_reference);
2070
237
            }
2071
188k
        }
2072
189k
        s = (pdf_stream *)s->parent_obj;
2073
189k
    }
2074
2075
494k
    if (content_stream != NULL) {
2076
78.3k
        stream = content_stream;
2077
416k
    } else {
2078
416k
        code = pdfi_seek(ctx, ctx->main_stream, pdfi_stream_offset(ctx, stream_obj), SEEK_SET);
2079
416k
        if (code < 0)
2080
0
            return code;
2081
2082
416k
        if (stream_obj->length_valid) {
2083
409k
            if (stream_obj->Length == 0)
2084
2.65k
                return 0;
2085
407k
            code = pdfi_apply_SubFileDecode_filter(ctx, stream_obj->Length, NULL, ctx->main_stream, &SubFile_stream, false);
2086
407k
        }
2087
6.50k
        else
2088
6.50k
            code = pdfi_apply_SubFileDecode_filter(ctx, 0, EODString, ctx->main_stream, &SubFile_stream, false);
2089
413k
        if (code < 0)
2090
0
            return code;
2091
2092
413k
        code = pdfi_filter(ctx, stream_obj, SubFile_stream, &stream, false);
2093
413k
        if (code < 0) {
2094
552
            pdfi_close_file(ctx, SubFile_stream);
2095
552
            return code;
2096
552
        }
2097
413k
    }
2098
2099
491k
    pdfi_set_stream_parent(ctx, stream_obj, ctx->current_stream);
2100
491k
    ctx->current_stream = stream_obj;
2101
2102
285M
    do {
2103
285M
        code = pdfi_read_token(ctx, stream, stream_obj->object_num, stream_obj->generation_num);
2104
285M
        if (code < 0) {
2105
727k
            if (code == gs_error_ioerror || code == gs_error_VMerror || ctx->args.pdfstoponerror) {
2106
14.5k
                if (code == gs_error_ioerror) {
2107
14.5k
                    pdfi_set_error(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_interpret_content_stream", (char *)"**** Error reading a content stream.  The page may be incomplete");
2108
14.5k
                } else if (code == gs_error_VMerror) {
2109
0
                    pdfi_set_error(ctx, code, NULL, E_PDF_OUTOFMEMORY, "pdfi_interpret_content_stream", (char *)"**** Error ran out of memory reading a content stream.  The page may be incomplete");
2110
0
                }
2111
14.5k
                goto exit;
2112
14.5k
            }
2113
713k
            continue;
2114
727k
        }
2115
2116
285M
        if (pdfi_count_stack(ctx) <= 0) {
2117
255k
            if(stream->eof == true)
2118
232k
                break;
2119
255k
        }
2120
2121
285M
repaired_keyword:
2122
285M
        type = pdfi_type_of(ctx->stack_top[-1]);
2123
285M
        if (type == PDF_FAST_KEYWORD) {
2124
70.8M
            keyword = (pdf_keyword *)ctx->stack_top[-1];
2125
2126
70.8M
            switch((uintptr_t)keyword) {
2127
41.2k
                case TOKEN_ENDSTREAM:
2128
41.2k
                    pdfi_pop(ctx,1);
2129
41.2k
                    goto exit;
2130
0
                    break;
2131
167
                case TOKEN_ENDOBJ:
2132
167
                    pdfi_clearstack(ctx);
2133
167
                    code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MISSINGENDSTREAM, "pdfi_interpret_content_stream", NULL);
2134
167
                    goto exit;
2135
0
                    break;
2136
0
                case TOKEN_INVALID_KEY:
2137
0
                    pdfi_clearstack(ctx);
2138
0
                    if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_limitcheck), NULL, E_PDF_KEYWORDTOOLONG, "pdfi_interpret_content_stream", NULL)) < 0)
2139
0
                        goto exit;
2140
0
                    break;
2141
0
                case TOKEN_TOO_LONG:
2142
0
                    pdfi_clearstack(ctx);
2143
0
                    if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MISSINGENDSTREAM, "pdfi_interpret_content_stream", NULL)) < 0)
2144
0
                        goto exit;
2145
0
                    break;
2146
70.8M
                default:
2147
70.8M
                    goto execute;
2148
70.8M
            }
2149
70.8M
        }
2150
214M
        else if (type == PDF_KEYWORD)
2151
8.31M
        {
2152
79.1M
execute:
2153
79.1M
            {
2154
79.1M
                pdf_dict *stream_dict = NULL;
2155
2156
79.1M
                code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream_obj, &stream_dict);
2157
79.1M
                if (code < 0)
2158
0
                    goto exit;
2159
2160
79.1M
                code = pdfi_interpret_stream_operator(ctx, stream, stream_dict, page_dict);
2161
79.1M
                if (code == REPAIRED_KEYWORD)
2162
15
                    goto repaired_keyword;
2163
2164
79.1M
                if (code < 0) {
2165
3.93M
                    if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_TOKENERROR, "pdf_interpret_content_stream", NULL)) < 0) {
2166
805
                        pdfi_clearstack(ctx);
2167
805
                        goto exit;
2168
805
                    }
2169
3.93M
                }
2170
79.1M
            }
2171
79.1M
        }
2172
284M
        if(stream->eof == true)
2173
202k
            break;
2174
285M
    }while(1);
2175
2176
491k
exit:
2177
491k
    ctx->current_stream = pdfi_stream_parent(ctx, stream_obj);
2178
491k
    pdfi_clear_stream_parent(ctx, stream_obj);
2179
491k
    pdfi_close_file(ctx, stream);
2180
491k
    if (SubFile_stream != NULL)
2181
413k
        pdfi_close_file(ctx, SubFile_stream);
2182
491k
    return code;
2183
491k
}