Coverage Report

Created: 2025-06-10 07:27

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