Coverage Report

Created: 2025-11-16 07:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/pdf/pdf_int.c
Line
Count
Source
1
/* Copyright (C) 2018-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
135M
#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
8.03G
{
57
8.03G
    if (c == 0x00 || c == 0x09 || c == 0x0a || c == 0x0c || c == 0x0d || c == 0x20)
58
1.33G
        return true;
59
6.70G
    else
60
6.70G
        return false;
61
8.03G
}
62
63
static bool isdelimiter(char c)
64
4.44G
{
65
4.44G
    if (c == '/' || c == '(' || c == ')' || c == '[' || c == ']' || c == '<' || c == '>' || c == '{' || c == '}' || c == '%')
66
104M
        return true;
67
4.34G
    else
68
4.34G
        return false;
69
4.44G
}
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.81G
{
78
1.81G
    int c;
79
80
2.27G
    do {
81
2.27G
        c = pdfi_read_byte(ctx, s);
82
2.27G
        if (c < 0)
83
516k
            return 0;
84
2.27G
    } while (iswhite(c));
85
86
1.81G
    pdfi_unread_byte(ctx, s, (byte)c);
87
1.81G
    return 0;
88
1.81G
}
89
90
int pdfi_skip_eol(pdf_context *ctx, pdf_c_stream *s)
91
4.04M
{
92
4.04M
    int c;
93
94
4.26M
    do {
95
4.26M
        c = pdfi_read_byte(ctx, s);
96
4.26M
        if (c < 0 || c == 0x0a)
97
1.99M
            return 0;
98
4.26M
    } while (c != 0x0d);
99
2.05M
    c = pdfi_read_byte(ctx, s);
100
2.05M
    if (c == 0x0a)
101
2.04M
        return 0;
102
9.61k
    if (c >= 0)
103
9.51k
        pdfi_unread_byte(ctx, s, (byte)c);
104
9.61k
    pdfi_set_warning(ctx, 0, NULL, W_PDF_STREAM_BAD_KEYWORD, "pdfi_skip_eol", NULL);
105
9.61k
    return 0;
106
2.05M
}
107
108
/* Fast(ish) but inaccurate strtof, with Adobe overflow handling,
109
 * lifted from MuPDF. */
110
static float acrobat_compatible_atof(char *s)
111
205M
{
112
205M
    int neg = 0;
113
205M
    int i = 0;
114
115
223M
    while (*s == '-') {
116
17.9M
        neg = 1;
117
17.9M
        ++s;
118
17.9M
    }
119
205M
    while (*s == '+') {
120
9
        ++s;
121
9
    }
122
123
705M
    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
499M
        i = i * 10 + (*s - '0');
129
499M
        ++s;
130
499M
    }
131
132
205M
    if (*s == '.') {
133
205M
        float MAX = (MAX_FLOAT-9)/10;
134
205M
        float v = (float)i;
135
205M
        float n = 0;
136
205M
        float d = 1;
137
205M
        ++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
877M
        while (*s >= '0' && *s <= '9' && n <= MAX) {
141
672M
            n = 10 * n + (*s - '0');
142
672M
            d = 10 * d;
143
672M
            ++s;
144
672M
        }
145
205M
        v += n / d;
146
205M
        return neg ? -v : v;
147
205M
    } else {
148
244k
        return (float)(neg ? -i : i);
149
244k
    }
150
205M
}
151
152
int pdfi_read_bare_int(pdf_context *ctx, pdf_c_stream *s, int *parsed_int)
153
153M
{
154
153M
    int index = 0;
155
153M
    int int_val = 0;
156
153M
    int negative = 0;
157
158
153M
restart:
159
153M
    pdfi_skip_white(ctx, s);
160
161
656M
    do {
162
656M
        int c = pdfi_read_byte(ctx, s);
163
656M
        if (c == EOFC)
164
3.87k
            break;
165
166
656M
        if (c < 0)
167
38.6k
            return_error(gs_error_ioerror);
168
169
656M
        if (iswhite(c)) {
170
153M
            break;
171
502M
        } else if (c == '%' && index == 0) {
172
61.8k
            pdfi_skip_comment(ctx, s);
173
61.8k
            goto restart;
174
502M
        } else if (isdelimiter(c)) {
175
38.5k
            pdfi_unread_byte(ctx, s, (byte)c);
176
38.5k
            break;
177
38.5k
        }
178
179
502M
        if (c >= '0' && c <= '9') {
180
502M
            int_val = int_val*10 + c - '0';
181
502M
        } else if (c == '.') {
182
3.49k
            goto error;
183
335k
        } else if (c == 'e' || c == 'E') {
184
5.27k
            pdfi_log_info(ctx, "pdfi_read_bare_int", (char *)"Invalid number format: scientific notation\n");
185
5.27k
            goto error;
186
330k
        } else if (c == '-') {
187
            /* Any - sign not at the start of the string indicates a malformed number. */
188
1.45k
            if (index != 0 || negative) {
189
203
                pdfi_log_info(ctx, "pdfi_read_bare_int", (char *)"Invalid number format: sign not at the start\n");
190
203
                goto error;
191
203
            }
192
1.24k
            negative = 1;
193
329k
        } else if (c == '+') {
194
203k
            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
203k
                continue;
198
203k
            } else {
199
165
                pdfi_log_info(ctx, "pdfi_read_bare_int", (char *)"Invalid number format: sign not at the start\n");
200
165
                goto error;
201
165
            }
202
203k
        } else {
203
125k
            if (index > 0) {
204
15.2k
                pdfi_log_info(ctx, "pdfi_read_bare_int", (char *)"Invalid number format: Ignoring missing white space while parsing number\n");
205
15.2k
                goto error;
206
15.2k
            }
207
109k
            pdfi_unread_byte(ctx, s, (byte)c);
208
109k
            goto error;
209
125k
        }
210
502M
        if (++index > 255)
211
191
            goto error;
212
502M
    } while(1);
213
214
153M
    *parsed_int = negative ? -int_val : int_val;
215
153M
    if (ctx->args.pdfdebug)
216
0
        outprintf(ctx->memory, " %d", *parsed_int);
217
153M
    return (index > 0);
218
219
134k
error:
220
134k
    *parsed_int = 0;
221
134k
    return_error(gs_error_syntaxerror);
222
153M
}
223
224
static int pdfi_read_num(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen)
225
493M
{
226
493M
    byte Buffer[256];
227
493M
    unsigned short index = 0;
228
493M
    bool real = false;
229
493M
    bool has_decimal_point = false;
230
493M
    bool has_exponent = false;
231
493M
    unsigned short exponent_index = 0;
232
493M
    pdf_num *num;
233
493M
    int code = 0, malformed = false, doubleneg = false, recovered = false, negative = false, overflowed = false;
234
493M
    unsigned int int_val = 0;
235
493M
    int tenth_max_int = max_int / 10, tenth_max_uint = max_uint / 10;
236
237
493M
    pdfi_skip_white(ctx, s);
238
239
2.69G
    do {
240
2.69G
        int c = pdfi_read_byte(ctx, s);
241
2.69G
        if (c == EOFC) {
242
13.3k
            Buffer[index] = 0x00;
243
13.3k
            break;
244
13.3k
        }
245
246
2.69G
        if (c < 0)
247
5.42k
            return_error(gs_error_ioerror);
248
249
2.69G
        if (iswhite(c)) {
250
437M
            Buffer[index] = 0x00;
251
437M
            break;
252
2.26G
        } else if (isdelimiter(c)) {
253
47.2M
            pdfi_unread_byte(ctx, s, (byte)c);
254
47.2M
            Buffer[index] = 0x00;
255
47.2M
            break;
256
47.2M
        }
257
2.21G
        Buffer[index] = (byte)c;
258
259
2.21G
        if (c >= '0' && c <= '9') {
260
1.95G
            if  (!(malformed && recovered) && !overflowed && !real) {
261
1.21G
                if ((negative && int_val <= tenth_max_int) || (!negative && int_val <= tenth_max_uint))
262
1.21G
                    int_val = int_val*10 + c - '0';
263
1.98M
                else {
264
1.98M
                    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.98M
                    overflowed = true;
268
1.98M
                }
269
1.21G
            }
270
1.95G
        } else if (c == '.') {
271
213M
            if (has_decimal_point == true) {
272
5.23M
                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
5.23M
                malformed = true;
276
208M
            } else {
277
208M
                has_decimal_point = true;
278
208M
                real = true;
279
208M
            }
280
213M
        } 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
579k
            if (has_exponent == true) {
285
66.6k
                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
66.6k
                malformed = true;
289
512k
            } else {
290
512k
                pdfi_set_warning(ctx, 0, NULL, W_PDF_NUM_EXPONENT, "pdfi_read_num", NULL);
291
512k
                has_exponent = true;
292
512k
                exponent_index = index;
293
512k
                real = true;
294
512k
            }
295
46.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
36.8M
            if (!(index == 0 || (has_exponent && index == exponent_index+1))) {
299
5.62M
                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
5.62M
                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
1.23M
                    malformed = true;
306
1.23M
                    Buffer[index] = 0;
307
1.23M
                    recovered = true;
308
1.23M
                }
309
5.62M
            }
310
36.8M
            if (!has_exponent && !(malformed && recovered)) {
311
35.5M
                doubleneg = negative;
312
35.5M
                negative = 1;
313
35.5M
            }
314
36.8M
        } else if (c == '+') {
315
181k
            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
168k
                index--;
319
168k
            } else {
320
12.5k
                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
12.5k
                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
11.9k
                    malformed = true;
327
11.9k
                    Buffer[index] = 0;
328
11.9k
                    recovered = true;
329
11.9k
                }
330
12.5k
            }
331
9.08M
        } else {
332
9.08M
            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
9.08M
            pdfi_unread_byte(ctx, s, (byte)c);
336
9.08M
            Buffer[index] = 0x00;
337
9.08M
            break;
338
9.08M
        }
339
2.20G
        if (++index > 255)
340
70.6k
            return_error(gs_error_syntaxerror);
341
2.20G
    } while(1);
342
343
493M
    if (real && (!malformed || (malformed && recovered)))
344
206M
        code = pdfi_object_alloc(ctx, PDF_REAL, 0, (pdf_obj **)&num);
345
287M
    else
346
287M
        code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)&num);
347
493M
    if (code < 0)
348
0
        return code;
349
350
493M
    if ((malformed && !recovered) || (!real && doubleneg)) {
351
2.82M
        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
2.82M
        num->value.i = 0;
355
490M
    } else if (has_exponent) {
356
472k
        float f, exp;
357
472k
        char *p = strstr((const char *)Buffer, "e");
358
359
472k
        if (p == NULL)
360
57.0k
            p = strstr((const char *)Buffer, "E");
361
362
472k
        if (p == NULL) {
363
5.79k
            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
5.79k
            num->value.d = 0;
367
467k
        } else {
368
467k
            p++;
369
370
467k
            if (sscanf((char *)p, "%g", &exp) != 1 || exp > 38) {
371
424k
                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
424k
                num->value.d = 0;
375
424k
            } else {
376
42.7k
                if (sscanf((char *)Buffer, "%g", &f) == 1) {
377
42.0k
                    num->value.d = f;
378
42.0k
                } else {
379
761
                    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
761
                    num->value.d = 0;
383
761
                }
384
42.7k
            }
385
467k
        }
386
490M
    } else if (real) {
387
205M
        num->value.d = acrobat_compatible_atof((char *)Buffer);
388
284M
    } else {
389
        /* The doubleneg case is taken care of above. */
390
284M
        num->value.i = negative ? (int64_t)int_val * -1 : (int64_t)int_val;
391
284M
    }
392
493M
    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
493M
    num->indirect_num = indirect_num;
399
493M
    num->indirect_gen = indirect_gen;
400
401
493M
    code = pdfi_push(ctx, (pdf_obj *)num);
402
403
493M
exit:
404
493M
    if (code < 0)
405
947
        pdfi_free_object((pdf_obj *)num);
406
407
493M
    return code;
408
493M
}
409
410
static int pdfi_read_name(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen)
411
136M
{
412
136M
    char *Buffer, *NewBuf = NULL;
413
136M
    unsigned short index = 0;
414
136M
    short bytes = 0;
415
136M
    uint32_t size = 256;
416
136M
    pdf_name *name = NULL;
417
136M
    int code;
418
419
136M
    Buffer = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_read_name");
420
136M
    if (Buffer == NULL)
421
0
        return_error(gs_error_VMerror);
422
423
985M
    do {
424
985M
        int c = pdfi_read_byte(ctx, s);
425
985M
        if (c < 0)
426
12.2k
            break;
427
428
985M
        if (iswhite((char)c)) {
429
94.8M
            Buffer[index] = 0x00;
430
94.8M
            break;
431
890M
        } else if (isdelimiter((char)c)) {
432
41.5M
            pdfi_unread_byte(ctx, s, (char)c);
433
41.5M
            Buffer[index] = 0x00;
434
41.5M
            break;
435
41.5M
        }
436
848M
        Buffer[index] = (char)c;
437
438
        /* Check for and convert escaped name characters */
439
848M
        if (c == '#') {
440
409k
            byte NumBuf[2];
441
442
409k
            bytes = pdfi_read_bytes(ctx, (byte *)&NumBuf, 1, 2, s);
443
409k
            if (bytes < 2 || (!ishex(NumBuf[0]) || !ishex(NumBuf[1]))) {
444
143k
                pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_NAME_ESCAPE, "pdfi_read_name", NULL);
445
143k
                pdfi_unread(ctx, s, (byte *)NumBuf, bytes);
446
                /* This leaves the name buffer with a # in it, rather than anything sane! */
447
143k
            }
448
265k
            else
449
265k
                Buffer[index] = (fromhex(NumBuf[0]) << 4) + fromhex(NumBuf[1]);
450
409k
        }
451
452
        /* If we ran out of memory, increase the buffer size */
453
848M
        if (index++ >= size - 1) {
454
96.4k
            NewBuf = (char *)gs_alloc_bytes(ctx->memory, (size_t)size + 256, "pdfi_read_name");
455
96.4k
            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
96.4k
            memcpy(NewBuf, Buffer, size);
460
96.4k
            gs_free_object(ctx->memory, Buffer, "pdfi_read_name");
461
96.4k
            Buffer = NewBuf;
462
96.4k
            size += 256;
463
96.4k
        }
464
848M
    } while(1);
465
466
136M
    code = pdfi_object_alloc(ctx, PDF_NAME, index, (pdf_obj **)&name);
467
136M
    if (code < 0) {
468
0
        gs_free_object(ctx->memory, Buffer, "pdfi_read_name error");
469
0
        return code;
470
0
    }
471
136M
    memcpy(name->data, Buffer, index);
472
136M
    name->indirect_num = indirect_num;
473
136M
    name->indirect_gen = indirect_gen;
474
475
136M
    if (ctx->args.pdfdebug)
476
0
        outprintf(ctx->memory, " /%s", Buffer);
477
478
136M
    gs_free_object(ctx->memory, Buffer, "pdfi_read_name");
479
480
136M
    code = pdfi_push(ctx, (pdf_obj *)name);
481
482
136M
    if (code < 0)
483
0
        pdfi_free_object((pdf_obj *)name);
484
485
136M
    return code;
486
136M
}
487
488
static int pdfi_read_hexstring(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen)
489
5.97M
{
490
5.97M
    char *Buffer, *NewBuf = NULL;
491
5.97M
    unsigned short index = 0;
492
5.97M
    uint32_t size = 256;
493
5.97M
    pdf_string *string = NULL;
494
5.97M
    int code, hex0, hex1;
495
496
5.97M
    Buffer = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_read_hexstring");
497
5.97M
    if (Buffer == NULL)
498
0
        return_error(gs_error_VMerror);
499
500
5.97M
    if (ctx->args.pdfdebug)
501
0
        outprintf(ctx->memory, " <");
502
503
310M
    do {
504
310M
        do {
505
310M
            hex0 = pdfi_read_byte(ctx, s);
506
310M
            if (hex0 < 0)
507
956
                break;
508
310M
        } while(iswhite(hex0));
509
310M
        if (hex0 < 0)
510
956
            break;
511
512
310M
        if (hex0 == '>')
513
5.54M
            break;
514
515
304M
        if (ctx->args.pdfdebug)
516
0
            outprintf(ctx->memory, "%c", (char)hex0);
517
518
305M
        do {
519
305M
            hex1 = pdfi_read_byte(ctx, s);
520
305M
            if (hex1 < 0)
521
998
                break;
522
305M
        } while(iswhite(hex1));
523
304M
        if (hex1 < 0)
524
998
            break;
525
526
304M
        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
54.0k
            hex1 = 0x30;
532
54.0k
            if (!ishex(hex0) || !ishex(hex1)) {
533
2.81k
                code = gs_note_error(gs_error_syntaxerror);
534
2.81k
                goto exit;
535
2.81k
            }
536
51.2k
            Buffer[index] = (fromhex(hex0) << 4) + fromhex(hex1);
537
51.2k
            if (ctx->args.pdfdebug)
538
0
                outprintf(ctx->memory, "%c", hex1);
539
51.2k
            break;
540
54.0k
        }
541
542
304M
        if (!ishex(hex0) || !ishex(hex1)) {
543
371k
            code = gs_note_error(gs_error_syntaxerror);
544
371k
            goto exit;
545
371k
        }
546
547
304M
        if (ctx->args.pdfdebug)
548
0
            outprintf(ctx->memory, "%c", (char)hex1);
549
550
304M
        Buffer[index] = (fromhex(hex0) << 4) + fromhex(hex1);
551
552
304M
        if (index++ >= size - 1) {
553
1.07M
            NewBuf = (char *)gs_alloc_bytes(ctx->memory, (size_t)size + 256, "pdfi_read_hexstring");
554
1.07M
            if (NewBuf == NULL) {
555
0
                code = gs_note_error(gs_error_VMerror);
556
0
                goto exit;
557
0
            }
558
1.07M
            memcpy(NewBuf, Buffer, size);
559
1.07M
            gs_free_object(ctx->memory, Buffer, "pdfi_read_hexstring");
560
1.07M
            Buffer = NewBuf;
561
1.07M
            size += 256;
562
1.07M
        }
563
304M
    } while(1);
564
565
5.60M
    if (ctx->args.pdfdebug)
566
0
        outprintf(ctx->memory, ">");
567
568
5.60M
    code = pdfi_object_alloc(ctx, PDF_STRING, index, (pdf_obj **)&string);
569
5.60M
    if (code < 0)
570
0
        goto exit;
571
5.60M
    memcpy(string->data, Buffer, index);
572
5.60M
    string->indirect_num = indirect_num;
573
5.60M
    string->indirect_gen = indirect_gen;
574
575
5.60M
    if (ctx->encryption.is_encrypted && ctx->encryption.decrypt_strings) {
576
2.86k
        code = pdfi_decrypt_string(ctx, string);
577
2.86k
        if (code < 0)
578
0
            return code;
579
2.86k
    }
580
581
5.60M
    code = pdfi_push(ctx, (pdf_obj *)string);
582
5.60M
    if (code < 0)
583
0
        pdfi_free_object((pdf_obj *)string);
584
585
5.97M
 exit:
586
5.97M
    gs_free_object(ctx->memory, Buffer, "pdfi_read_hexstring");
587
5.97M
    return code;
588
5.60M
}
589
590
static int pdfi_read_string(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen)
591
29.6M
{
592
29.6M
    char *Buffer, *NewBuf = NULL;
593
29.6M
    unsigned short index = 0;
594
29.6M
    uint32_t size = 256;
595
29.6M
    pdf_string *string = NULL;
596
29.6M
    int c, code, nesting = 0;
597
29.6M
    bool escape = false, skip_eol = false, exit_loop = false;
598
599
29.6M
    Buffer = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_read_string");
600
29.6M
    if (Buffer == NULL)
601
0
        return_error(gs_error_VMerror);
602
603
1.16G
    do {
604
1.16G
        if (index >= size - 1) {
605
2.48M
            NewBuf = (char *)gs_alloc_bytes(ctx->memory, (size_t)size + 256, "pdfi_read_string");
606
2.48M
            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
2.48M
            memcpy(NewBuf, Buffer, size);
611
2.48M
            gs_free_object(ctx->memory, Buffer, "pdfi_read_string");
612
2.48M
            Buffer = NewBuf;
613
2.48M
            size += 256;
614
2.48M
        }
615
616
1.16G
        c = pdfi_read_byte(ctx, s);
617
618
1.16G
        if (c < 0) {
619
30.6k
            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
30.6k
            Buffer[index] = 0x00;
624
30.6k
            break;
625
30.6k
        }
626
627
1.16G
        if (skip_eol) {
628
19.3M
            if (c == 0x0a || c == 0x0d)
629
2.70M
                continue;
630
16.6M
            skip_eol = false;
631
16.6M
        }
632
1.15G
        Buffer[index] = (char)c;
633
634
1.15G
        if (escape) {
635
5.03M
            escape = false;
636
5.03M
            switch (Buffer[index]) {
637
14.1k
                case 0x0a:
638
93.5k
                case 0x0d:
639
93.5k
                    skip_eol = true;
640
93.5k
                    continue;
641
124k
                case 'n':
642
124k
                    Buffer[index] = 0x0a;
643
124k
                    break;
644
120k
                case 'r':
645
120k
                    Buffer[index] = 0x0d;
646
120k
                    break;
647
28.4k
                case 't':
648
28.4k
                    Buffer[index] = 0x09;
649
28.4k
                    break;
650
29.3k
                case 'b':
651
29.3k
                    Buffer[index] = 0x08;
652
29.3k
                    break;
653
32.5k
                case 'f':
654
32.5k
                    Buffer[index] = 0x0c;
655
32.5k
                    break;
656
335k
                case '(':
657
675k
                case ')':
658
769k
                case '\\':
659
769k
                    break;
660
1.20M
                case '0':
661
1.23M
                case '1':
662
1.67M
                case '2':
663
1.98M
                case '3':
664
2.00M
                case '4':
665
2.01M
                case '5':
666
2.02M
                case '6':
667
2.04M
                case '7':
668
2.04M
                {
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
2.04M
                    int c1 = pdfi_read_byte(ctx, s);
673
2.04M
                    c -= '0';
674
2.04M
                    if (c1 < 0) {
675
                        /* Nothing to do, or unread */
676
2.04M
                    } else if (c1 < '0' || c1 > '7') {
677
111k
                        pdfi_unread_byte(ctx, s, (char)c1);
678
1.92M
                    } else {
679
1.92M
                        c = c*8 + c1 - '0';
680
1.92M
                        c1 = pdfi_read_byte(ctx, s);
681
1.92M
                        if (c1 < 0) {
682
                            /* Nothing to do, or unread */
683
1.92M
                        } else if (c1 < '0' || c1 > '7') {
684
48.0k
                            pdfi_unread_byte(ctx, s, (char)c1);
685
48.0k
                        } else
686
1.88M
                            c = c*8 + c1 - '0';
687
1.92M
                    }
688
2.04M
                    Buffer[index] = c;
689
2.04M
                    break;
690
2.02M
                }
691
1.79M
                default:
692
                    /* PDF Reference, literal strings, if the character following a
693
                     * escape \ character is not recognised, then it is ignored.
694
                     */
695
1.79M
                    escape = false;
696
1.79M
                    index++;
697
1.79M
                    continue;
698
5.03M
            }
699
1.15G
        } else {
700
1.15G
            switch(Buffer[index]) {
701
4.36M
                case 0x0d:
702
4.36M
                    Buffer[index] = 0x0a;
703
                    /*fallthrough*/
704
16.5M
                case 0x0a:
705
16.5M
                    skip_eol = true;
706
16.5M
                    break;
707
33.5M
                case ')':
708
33.5M
                    if (nesting == 0) {
709
29.5M
                        Buffer[index] = 0x00;
710
29.5M
                        exit_loop = true;
711
29.5M
                    } else
712
3.98M
                        nesting--;
713
33.5M
                    break;
714
5.03M
                case '\\':
715
5.03M
                    escape = true;
716
5.03M
                    continue;
717
4.78M
                case '(':
718
4.78M
                    nesting++;
719
4.78M
                    break;
720
1.09G
                default:
721
1.09G
                    break;
722
1.15G
            }
723
1.15G
        }
724
725
1.15G
        if (exit_loop)
726
29.5M
            break;
727
728
1.12G
        index++;
729
1.13G
    } while(1);
730
731
29.6M
    code = pdfi_object_alloc(ctx, PDF_STRING, index, (pdf_obj **)&string);
732
29.6M
    if (code < 0) {
733
0
        gs_free_object(ctx->memory, Buffer, "pdfi_read_name error");
734
0
        return code;
735
0
    }
736
29.6M
    memcpy(string->data, Buffer, index);
737
29.6M
    string->indirect_num = indirect_num;
738
29.6M
    string->indirect_gen = indirect_gen;
739
740
29.6M
    gs_free_object(ctx->memory, Buffer, "pdfi_read_string");
741
742
29.6M
    if (ctx->encryption.is_encrypted && ctx->encryption.decrypt_strings) {
743
20.1k
        code = pdfi_decrypt_string(ctx, string);
744
20.1k
        if (code < 0)
745
0
            return code;
746
20.1k
    }
747
748
29.6M
    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
29.6M
    code = pdfi_push(ctx, (pdf_obj *)string);
757
29.6M
    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
29.6M
    return code;
763
29.6M
}
764
765
int pdfi_read_dict(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen)
766
12.6k
{
767
12.6k
    int code, depth;
768
769
12.6k
    code = pdfi_read_token(ctx, s, indirect_num, indirect_gen);
770
12.6k
    if (code < 0)
771
12
        return code;
772
12.6k
    if (code == 0)
773
0
        return_error(gs_error_syntaxerror);
774
775
12.6k
    if (pdfi_type_of(ctx->stack_top[-1]) != PDF_DICT_MARK)
776
25
        return_error(gs_error_typecheck);
777
12.5k
    depth = pdfi_count_stack(ctx);
778
779
197k
    do {
780
197k
        code = pdfi_read_token(ctx, s, indirect_num, indirect_gen);
781
197k
        if (code < 0)
782
180
            return code;
783
196k
        if (code == 0)
784
17
            return_error(gs_error_syntaxerror);
785
196k
    } while(pdfi_count_stack(ctx) > depth);
786
12.3k
    return 0;
787
12.5k
}
788
789
int pdfi_skip_comment(pdf_context *ctx, pdf_c_stream *s)
790
2.63M
{
791
2.63M
    int c;
792
793
2.63M
    if (ctx->args.pdfdebug)
794
0
        outprintf (ctx->memory, " %%");
795
796
56.2M
    do {
797
56.2M
        c = pdfi_read_byte(ctx, s);
798
56.2M
        if (c < 0)
799
12.0k
            break;
800
801
56.2M
        if (ctx->args.pdfdebug)
802
0
            outprintf (ctx->memory, "%c", (char)c);
803
804
56.2M
    } while (c != 0x0a && c != 0x0d);
805
806
2.63M
    return 0;
807
2.63M
}
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
210M
#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
6.07M
{
821
6.07M
    byte Buffer[256];
822
6.07M
    int code, index = 0;
823
6.07M
    int c;
824
6.07M
    void *t;
825
826
6.07M
    pdfi_skip_white(ctx, s);
827
828
37.6M
    do {
829
37.6M
        c = pdfi_read_byte(ctx, s);
830
37.6M
        if (c < 0)
831
30.7k
            break;
832
833
37.5M
        if (iswhite(c) || isdelimiter(c)) {
834
6.04M
            pdfi_unread_byte(ctx, s, (byte)c);
835
6.04M
            break;
836
6.04M
        }
837
31.5M
        Buffer[index] = (byte)c;
838
31.5M
        index++;
839
31.5M
    } while (index < 255);
840
841
6.07M
    if (index >= 255 || index == 0) {
842
45.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
45.6k
        return TOKEN_INVALID_KEY;
846
45.6k
    }
847
848
6.03M
    Buffer[index] = 0x00;
849
6.03M
    t = bsearch((const void *)Buffer,
850
6.03M
                (const void *)pdf_token_strings[TOKEN_INVALID_KEY+1],
851
6.03M
                nelems(pdf_token_strings)-(TOKEN_INVALID_KEY+1),
852
6.03M
                sizeof(pdf_token_strings[0]),
853
6.03M
                (bsearch_comparator)&strcmp);
854
6.03M
    if (t == NULL)
855
108k
        return TOKEN_INVALID_KEY;
856
857
5.92M
    if (ctx->args.pdfdebug)
858
0
        outprintf(ctx->memory, " %s\n", Buffer);
859
860
5.92M
    return (((const char *)t) - pdf_token_strings[0]) / sizeof(pdf_token_strings[0]);
861
6.03M
}
862
863
static pdf_key lookup_keyword(const byte *Buffer)
864
204M
{
865
204M
    void *t = bsearch((const void *)Buffer,
866
204M
                      (const void *)pdf_token_strings[TOKEN_INVALID_KEY+1],
867
204M
                      nelems(pdf_token_strings)-(TOKEN_INVALID_KEY+1),
868
204M
                      sizeof(pdf_token_strings[0]),
869
204M
                      (bsearch_comparator)&strcmp);
870
204M
    if (t == NULL)
871
20.8M
        return TOKEN_NOT_A_KEYWORD;
872
873
183M
    return (pdf_key)((((const char *)t) - pdf_token_strings[0]) /
874
183M
                     sizeof(pdf_token_strings[0]));
875
204M
}
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
201M
{
886
201M
    byte Buffer[256];
887
201M
    unsigned short index = 0;
888
201M
    int c, code;
889
201M
    pdf_keyword *keyword;
890
201M
    pdf_key key;
891
892
201M
    pdfi_skip_white(ctx, s);
893
894
745M
    do {
895
745M
        c = pdfi_read_byte(ctx, s);
896
745M
        if (c < 0)
897
346k
            break;
898
899
745M
        if (iswhite(c) || isdelimiter(c)) {
900
200M
            pdfi_unread_byte(ctx, s, (byte)c);
901
200M
            break;
902
200M
        }
903
544M
        Buffer[index] = (byte)c;
904
544M
        index++;
905
544M
    } while (index < 255);
906
907
201M
    if (index >= 255 || index == 0) {
908
291k
        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
291k
        key = (index >= 255 ? TOKEN_TOO_LONG : TOKEN_INVALID_KEY);
912
291k
        index = 0;
913
291k
        Buffer[0] = 0;
914
201M
    } else {
915
201M
        Buffer[index] = 0x00;
916
201M
        key = lookup_keyword(Buffer);
917
918
201M
        if (ctx->args.pdfdebug)
919
0
            outprintf(ctx->memory, " %s\n", Buffer);
920
921
201M
        switch (key) {
922
25.1M
            case TOKEN_R:
923
25.1M
            {
924
25.1M
                pdf_indirect_ref *o;
925
25.1M
                uint64_t obj_num;
926
25.1M
                uint32_t gen_num;
927
928
25.1M
                if(pdfi_count_stack(ctx) < 2) {
929
48.7k
                    pdfi_clearstack(ctx);
930
48.7k
                    return_error(gs_error_stackunderflow);
931
48.7k
                }
932
933
25.0M
                if(pdfi_type_of(ctx->stack_top[-1]) != PDF_INT || pdfi_type_of(ctx->stack_top[-2]) != PDF_INT) {
934
70.1k
                    pdfi_clearstack(ctx);
935
70.1k
                    return_error(gs_error_typecheck);
936
70.1k
                }
937
938
25.0M
                gen_num = ((pdf_num *)ctx->stack_top[-1])->value.i;
939
25.0M
                pdfi_pop(ctx, 1);
940
25.0M
                obj_num = ((pdf_num *)ctx->stack_top[-1])->value.i;
941
25.0M
                pdfi_pop(ctx, 1);
942
943
25.0M
                code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&o);
944
25.0M
                if (code < 0)
945
0
                    return code;
946
947
25.0M
                o->ref_generation_num = gen_num;
948
25.0M
                o->ref_object_num = obj_num;
949
25.0M
                o->indirect_num = indirect_num;
950
25.0M
                o->indirect_gen = indirect_gen;
951
952
25.0M
                code = pdfi_push(ctx, (pdf_obj *)o);
953
25.0M
                if (code < 0)
954
0
                    pdfi_free_object((pdf_obj *)o);
955
956
25.0M
                return code;
957
25.0M
            }
958
20.8M
            case TOKEN_NOT_A_KEYWORD:
959
                 /* Unexpected keyword found. We'll allocate an object for the buffer below. */
960
20.8M
                 break;
961
4.04M
            case TOKEN_STREAM:
962
4.04M
                code = pdfi_skip_eol(ctx, s);
963
4.04M
                if (code < 0)
964
0
                    return code;
965
                /* fallthrough */
966
5.44M
            case TOKEN_PDF_TRUE:
967
6.10M
            case TOKEN_PDF_FALSE:
968
6.19M
            case TOKEN_null:
969
155M
            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
155M
                return pdfi_push(ctx, (pdf_obj *)(intptr_t)key);
974
201M
        }
975
201M
    }
976
977
    /* Unexpected keyword. We can't handle this with the fast no-allocation case. */
978
21.1M
    code = pdfi_object_alloc(ctx, PDF_KEYWORD, index, (pdf_obj **)&keyword);
979
21.1M
    if (code < 0)
980
0
        return code;
981
982
21.1M
    if (index)
983
20.8M
        memcpy(keyword->data, Buffer, index);
984
985
    /* keyword->length set as part of allocation. */
986
21.1M
    keyword->indirect_num = indirect_num;
987
21.1M
    keyword->indirect_gen = indirect_gen;
988
989
21.1M
    code = pdfi_push(ctx, (pdf_obj *)keyword);
990
21.1M
    if (code < 0)
991
30
        pdfi_free_object((pdf_obj *)keyword);
992
993
21.1M
    return code;
994
21.1M
}
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
947M
{
1001
947M
    int c, code;
1002
1003
953M
rescan:
1004
953M
    pdfi_skip_white(ctx, s);
1005
1006
953M
    c = pdfi_read_byte(ctx, s);
1007
953M
    if (c == EOFC)
1008
443k
        return 0;
1009
952M
    if (c < 0)
1010
17.8k
        return_error(gs_error_ioerror);
1011
1012
952M
    switch(c) {
1013
130M
        case 0x30:
1014
205M
        case 0x31:
1015
257M
        case 0x32:
1016
314M
        case 0x33:
1017
360M
        case 0x34:
1018
392M
        case 0x35:
1019
418M
        case 0x36:
1020
438M
        case 0x37:
1021
451M
        case 0x38:
1022
459M
        case 0x39:
1023
459M
        case '+':
1024
490M
        case '-':
1025
493M
        case '.':
1026
493M
            pdfi_unread_byte(ctx, s, (byte)c);
1027
493M
            code = pdfi_read_num(ctx, s, indirect_num, indirect_gen);
1028
493M
            if (code < 0)
1029
77.0k
                return code;
1030
493M
            break;
1031
493M
        case '/':
1032
136M
            code = pdfi_read_name(ctx, s, indirect_num, indirect_gen);
1033
136M
            if (code < 0)
1034
0
                return code;
1035
136M
            return 1;
1036
0
            break;
1037
24.8M
        case '<':
1038
24.8M
            c = pdfi_read_byte(ctx, s);
1039
24.8M
            if (c < 0)
1040
420
                return (gs_error_ioerror);
1041
24.8M
            if (iswhite(c)) {
1042
62.9k
                code = pdfi_skip_white(ctx, s);
1043
62.9k
                if (code < 0)
1044
0
                    return code;
1045
62.9k
                c = pdfi_read_byte(ctx, s);
1046
62.9k
            }
1047
24.8M
            if (c == '<') {
1048
18.0M
                if (ctx->args.pdfdebug)
1049
0
                    outprintf (ctx->memory, " <<\n");
1050
18.0M
                if (ctx->object_nesting < MAX_NESTING_DEPTH) {
1051
17.3M
                    ctx->object_nesting++;
1052
17.3M
                    code = pdfi_mark_stack(ctx, PDF_DICT_MARK);
1053
17.3M
                    if (code < 0)
1054
1
                        return code;
1055
17.3M
                }
1056
704k
                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
18.0M
                return 1;
1060
18.0M
            } else if (c == '>') {
1061
40.5k
                pdfi_unread_byte(ctx, s, (byte)c);
1062
40.5k
                code = pdfi_read_hexstring(ctx, s, indirect_num, indirect_gen);
1063
40.5k
                if (code < 0)
1064
0
                    return code;
1065
40.5k
                return 1;
1066
6.72M
            } else if (ishex(c)) {
1067
5.93M
                pdfi_unread_byte(ctx, s, (byte)c);
1068
5.93M
                code = pdfi_read_hexstring(ctx, s, indirect_num, indirect_gen);
1069
5.93M
                if (code < 0)
1070
373k
                    return code;
1071
5.93M
            }
1072
787k
            else
1073
787k
                return_error(gs_error_syntaxerror);
1074
5.56M
            break;
1075
19.0M
        case '>':
1076
19.0M
            c = pdfi_read_byte(ctx, s);
1077
19.0M
            if (c < 0)
1078
836
                return (gs_error_ioerror);
1079
19.0M
            if (c == '>') {
1080
17.9M
                if (ctx->object_nesting > 0) {
1081
16.7M
                    ctx->object_nesting--;
1082
16.7M
                    code = pdfi_dict_from_stack(ctx, indirect_num, indirect_gen, false);
1083
16.7M
                    if (code < 0)
1084
480k
                        return code;
1085
16.7M
                } else {
1086
1.14M
                    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
1.14M
                    goto rescan;
1090
1.14M
                }
1091
16.3M
                return 1;
1092
17.9M
            } else {
1093
1.13M
                pdfi_unread_byte(ctx, s, (byte)c);
1094
1.13M
                return_error(gs_error_syntaxerror);
1095
1.13M
            }
1096
0
            break;
1097
29.6M
        case '(':
1098
29.6M
            code = pdfi_read_string(ctx, s, indirect_num, indirect_gen);
1099
29.6M
            if (code < 0)
1100
0
                return code;
1101
29.6M
            return 1;
1102
0
            break;
1103
22.0M
        case '[':
1104
22.0M
            if (ctx->args.pdfdebug)
1105
0
                outprintf (ctx->memory, "[");
1106
22.0M
            if (ctx->object_nesting < MAX_NESTING_DEPTH) {
1107
20.6M
                ctx->object_nesting++;
1108
20.6M
                code = pdfi_mark_stack(ctx, PDF_ARRAY_MARK);
1109
20.6M
                if (code < 0)
1110
0
                    return code;
1111
20.6M
            } else
1112
1.41M
                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
22.0M
            return 1;
1115
0
            break;
1116
20.9M
        case ']':
1117
20.9M
            if (ctx->object_nesting > 0) {
1118
20.4M
                ctx->object_nesting--;
1119
20.4M
                code = pdfi_array_from_stack(ctx, indirect_num, indirect_gen);
1120
20.4M
                if (code < 0)
1121
279k
                    return code;
1122
20.4M
            } else {
1123
459k
                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
459k
                goto rescan;
1127
459k
            }
1128
20.2M
            break;
1129
20.2M
        case '{':
1130
730k
            if (ctx->args.pdfdebug)
1131
0
                outprintf (ctx->memory, "{");
1132
730k
            code = pdfi_mark_stack(ctx, PDF_PROC_MARK);
1133
730k
            if (code < 0)
1134
0
                return code;
1135
730k
            return 1;
1136
0
            break;
1137
406k
        case '}':
1138
406k
            pdfi_clear_to_mark(ctx);
1139
406k
            goto rescan;
1140
0
            break;
1141
2.46M
        case '%':
1142
2.46M
            pdfi_skip_comment(ctx, s);
1143
2.46M
            goto rescan;
1144
0
            break;
1145
202M
        default:
1146
202M
            if (isdelimiter(c)) {
1147
1.09M
                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
1.09M
                goto rescan;
1151
1.09M
            }
1152
201M
            pdfi_unread_byte(ctx, s, (byte)c);
1153
201M
            code = pdfi_read_keyword(ctx, s, indirect_num, indirect_gen);
1154
201M
            if (code < 0)
1155
119k
                return code;
1156
201M
            return 1;
1157
0
            break;
1158
952M
    }
1159
519M
    return 1;
1160
952M
}
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
243M
{
1169
243M
    int code;
1170
243M
    *o = NULL;
1171
1172
243M
    code = pdfi_object_alloc(ctx, PDF_NAME, size, o);
1173
243M
    if (code < 0)
1174
0
        return code;
1175
1176
243M
    memcpy(((pdf_name *)*o)->data, n, size);
1177
1178
243M
    return 0;
1179
243M
}
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
3.54M
{
1202
3.54M
    byte Buffer[256];
1203
3.54M
    pdf_key key;
1204
3.54M
    int code;
1205
1206
3.54M
    if (length > 255)
1207
0
        return_error(gs_error_rangecheck);
1208
1209
3.54M
    memcpy(Buffer, data, length);
1210
3.54M
    Buffer[length] = 0;
1211
3.54M
    key = lookup_keyword(Buffer);
1212
3.54M
    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
3.54M
        *pkey = (pdf_keyword *)PDF_TOKEN_AS_OBJ(key);
1216
3.54M
        return 1;
1217
3.54M
    }
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
3.42M
{
1232
3.42M
    int i;
1233
1234
20.5M
    for (i = 0; i < 5; i++) {
1235
17.1M
        if (memcmp(str, op_table_3[i], 3) == 0)
1236
12.7k
            return make_keyword_obj(ctx, str, 3, key);
1237
17.1M
    }
1238
3.41M
    return 0;
1239
3.42M
}
1240
1241
static int search_table_2(pdf_context *ctx, unsigned char *str, pdf_keyword **key)
1242
6.76M
{
1243
6.76M
    int i;
1244
1245
260M
    for (i = 0; i < 39; i++) {
1246
254M
        if (memcmp(str, op_table_2[i], 2) == 0)
1247
471k
            return make_keyword_obj(ctx, str, 2, key);
1248
254M
    }
1249
6.29M
    return 0;
1250
6.76M
}
1251
1252
static int search_table_1(pdf_context *ctx, unsigned char *str, pdf_keyword **key)
1253
6.17M
{
1254
6.17M
    int i;
1255
1256
129M
    for (i = 0; i < 27; i++) {
1257
126M
        if (memcmp(str, op_table_1[i], 1) == 0)
1258
3.06M
            return make_keyword_obj(ctx, str, 1, key);
1259
126M
    }
1260
3.11M
    return 0;
1261
6.17M
}
1262
1263
static int split_bogus_operator(pdf_context *ctx, pdf_c_stream *source, pdf_dict *stream_dict, pdf_dict *page_dict)
1264
12.1M
{
1265
12.1M
    int code = 0;
1266
12.1M
    pdf_keyword *keyword = (pdf_keyword *)ctx->stack_top[-1], *key1 = NULL, *key2 = NULL;
1267
12.1M
    int length = keyword->length - 6;
1268
1269
12.1M
    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
4.27M
        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
27
            code = make_keyword_obj(ctx, keyword->data, length, &key1);
1277
27
            if (code < 0)
1278
0
                goto error_exit;
1279
27
            pdfi_pop(ctx, 1);
1280
27
            pdfi_push(ctx, (pdf_obj *)key1);
1281
27
            pdfi_countdown(key1); /* Drop the reference returned by make_keyword_obj. */
1282
27
            code = pdfi_interpret_stream_operator(ctx, source, stream_dict, page_dict);
1283
27
            if (code < 0)
1284
14
                goto error_exit;
1285
13
            pdfi_push(ctx, PDF_TOKEN_AS_OBJ(TOKEN_ENDOBJ));
1286
13
            return 0;
1287
4.27M
        } else {
1288
4.27M
            length = keyword->length - 9;
1289
4.27M
            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
26
                code = make_keyword_obj(ctx, keyword->data, length, &key1);
1293
26
                if (code < 0)
1294
0
                    goto error_exit;
1295
26
                pdfi_pop(ctx, 1);
1296
26
                pdfi_push(ctx, (pdf_obj *)key1);
1297
26
                pdfi_countdown(key1); /* Drop the reference returned by make_keyword_obj. */
1298
26
                code = pdfi_interpret_stream_operator(ctx, source, stream_dict, page_dict);
1299
26
                if (code < 0)
1300
24
                    goto error_exit;
1301
2
                pdfi_push(ctx, PDF_TOKEN_AS_OBJ(TOKEN_ENDSTREAM));
1302
2
                return 0;
1303
4.27M
            } else {
1304
4.27M
                pdfi_clearstack(ctx);
1305
4.27M
                return 0;
1306
4.27M
            }
1307
4.27M
        }
1308
4.27M
    }
1309
1310
7.92M
    if (keyword->length > 3) {
1311
2.69M
        code = search_table_3(ctx, keyword->data, &key1);
1312
2.69M
        if (code < 0)
1313
0
            goto error_exit;
1314
1315
2.69M
        if (code > 0) {
1316
11.1k
            switch (keyword->length - 3) {
1317
8.84k
                case 1:
1318
8.84k
                    code = search_table_1(ctx, &keyword->data[3], &key2);
1319
8.84k
                    break;
1320
1.25k
                case 2:
1321
1.25k
                    code = search_table_2(ctx, &keyword->data[3], &key2);
1322
1.25k
                    break;
1323
1.00k
                case 3:
1324
1.00k
                    code = search_table_3(ctx, &keyword->data[3], &key2);
1325
1.00k
                    break;
1326
0
                default:
1327
0
                    goto error_exit;
1328
11.1k
            }
1329
11.1k
        }
1330
2.69M
        if (code < 0)
1331
0
            goto error_exit;
1332
2.69M
        if (code > 0)
1333
2.23k
            goto match;
1334
2.69M
    }
1335
7.92M
    pdfi_countdown(key1);
1336
7.92M
    pdfi_countdown(key2);
1337
7.92M
    key1 = NULL;
1338
7.92M
    key2 = NULL;
1339
1340
7.92M
    if (keyword->length > 5 || keyword->length < 2)
1341
2.08M
        goto error_exit;
1342
1343
5.83M
    code = search_table_2(ctx, keyword->data, &key1);
1344
5.83M
    if (code < 0)
1345
0
        goto error_exit;
1346
1347
5.83M
    if (code > 0) {
1348
407k
        switch(keyword->length - 2) {
1349
161k
            case 1:
1350
161k
                code = search_table_1(ctx, &keyword->data[2], &key2);
1351
161k
                break;
1352
146k
            case 2:
1353
146k
                code = search_table_2(ctx, &keyword->data[2], &key2);
1354
146k
                break;
1355
99.5k
            case 3:
1356
99.5k
                code = search_table_3(ctx, &keyword->data[2], &key2);
1357
99.5k
                break;
1358
0
            default:
1359
0
                goto error_exit;
1360
407k
        }
1361
407k
        if (code < 0)
1362
0
            goto error_exit;
1363
407k
        if (code > 0)
1364
61.1k
            goto match;
1365
407k
    }
1366
5.77M
    pdfi_countdown(key1);
1367
5.77M
    pdfi_countdown(key2);
1368
5.77M
    key1 = NULL;
1369
5.77M
    key2 = NULL;
1370
1371
5.77M
    if (keyword->length > 4)
1372
960k
        goto error_exit;
1373
1374
4.81M
    code = search_table_1(ctx, keyword->data, &key1);
1375
4.81M
    if (code <= 0)
1376
2.20M
        goto error_exit;
1377
1378
2.60M
    switch(keyword->length - 1) {
1379
1.19M
        case 1:
1380
1.19M
            code = search_table_1(ctx, &keyword->data[1], &key2);
1381
1.19M
            break;
1382
783k
        case 2:
1383
783k
            code = search_table_2(ctx, &keyword->data[1], &key2);
1384
783k
            break;
1385
634k
        case 3:
1386
634k
            code = search_table_3(ctx, &keyword->data[1], &key2);
1387
634k
            break;
1388
0
        default:
1389
0
            goto error_exit;
1390
2.60M
    }
1391
2.60M
    if (code <= 0)
1392
2.15M
        goto error_exit;
1393
1394
519k
match:
1395
519k
    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
519k
    pdfi_push(ctx, (pdf_obj *)key1);
1400
519k
    code = pdfi_interpret_stream_operator(ctx, source, stream_dict, page_dict);
1401
519k
    if (code < 0)
1402
386k
        goto error_exit;
1403
1404
132k
    pdfi_push(ctx, (pdf_obj *)key2);
1405
132k
    code = pdfi_interpret_stream_operator(ctx, source, stream_dict, page_dict);
1406
1407
132k
    pdfi_countdown(key1);
1408
132k
    pdfi_countdown(key2);
1409
132k
    pdfi_clearstack(ctx);
1410
132k
    return code;
1411
1412
7.79M
error_exit:
1413
7.79M
    pdfi_set_error(ctx, code, NULL, E_PDF_TOKENERROR, "split_bogus_operator", NULL);
1414
7.79M
    pdfi_countdown(key1);
1415
7.79M
    pdfi_countdown(key2);
1416
7.79M
    pdfi_clearstack(ctx);
1417
7.79M
    return code;
1418
519k
}
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
136M
{
1423
136M
    pdf_obj *keyword = ctx->stack_top[-1];
1424
136M
    int code = 0;
1425
1426
136M
    if (keyword < PDF_TOKEN_AS_OBJ(TOKEN__LAST_KEY))
1427
124M
    {
1428
124M
        switch((uintptr_t)keyword) {
1429
19.8k
            case TOKEN_b:           /* closepath, fill, stroke */
1430
19.8k
                pdfi_pop(ctx, 1);
1431
19.8k
                code = pdfi_b(ctx);
1432
19.8k
                break;
1433
56.8k
            case TOKEN_B:           /* fill, stroke */
1434
56.8k
                pdfi_pop(ctx, 1);
1435
56.8k
                code = pdfi_B(ctx);
1436
56.8k
                break;
1437
1.34k
            case TOKEN_bstar:       /* closepath, eofill, stroke */
1438
1.34k
                pdfi_pop(ctx, 1);
1439
1.34k
                code = pdfi_b_star(ctx);
1440
1.34k
                break;
1441
2.54k
            case TOKEN_Bstar:       /* eofill, stroke */
1442
2.54k
                pdfi_pop(ctx, 1);
1443
2.54k
                code = pdfi_B_star(ctx);
1444
2.54k
                break;
1445
395k
            case TOKEN_BI:       /* begin inline image */
1446
395k
                pdfi_pop(ctx, 1);
1447
395k
                code = pdfi_BI(ctx);
1448
395k
                break;
1449
673k
            case TOKEN_BDC:   /* begin marked content sequence with property list */
1450
673k
                pdfi_pop(ctx, 1);
1451
673k
                code = pdfi_op_BDC(ctx, stream_dict, page_dict);
1452
673k
                break;
1453
30.1k
            case TOKEN_BMC:   /* begin marked content sequence */
1454
30.1k
                pdfi_pop(ctx, 1);
1455
30.1k
                code = pdfi_op_BMC(ctx);
1456
30.1k
                break;
1457
3.69M
            case TOKEN_BT:       /* begin text */
1458
3.69M
                pdfi_pop(ctx, 1);
1459
3.69M
                code = pdfi_BT(ctx);
1460
3.69M
                break;
1461
142k
            case TOKEN_BX:       /* begin compatibility section */
1462
142k
                pdfi_pop(ctx, 1);
1463
142k
                break;
1464
14.3M
            case TOKEN_c:           /* curveto */
1465
14.3M
                pdfi_pop(ctx, 1);
1466
14.3M
                code = pdfi_curveto(ctx);
1467
14.3M
                break;
1468
4.88M
            case TOKEN_cm:       /* concat */
1469
4.88M
                pdfi_pop(ctx, 1);
1470
4.88M
                code = pdfi_concat(ctx);
1471
4.88M
                break;
1472
82.4k
            case TOKEN_CS:       /* set stroke colour space */
1473
82.4k
                pdfi_pop(ctx, 1);
1474
82.4k
                code = pdfi_setstrokecolor_space(ctx, stream_dict, page_dict);
1475
82.4k
                break;
1476
436k
            case TOKEN_cs:       /* set non-stroke colour space */
1477
436k
                pdfi_pop(ctx, 1);
1478
436k
                code = pdfi_setfillcolor_space(ctx, stream_dict, page_dict);
1479
436k
                break;
1480
1.27M
            case TOKEN_d:           /* set dash params */
1481
1.27M
                pdfi_pop(ctx, 1);
1482
1.27M
                code = pdfi_setdash(ctx);
1483
1.27M
                break;
1484
4.35k
            case TOKEN_d0:       /* set type 3 font glyph width */
1485
4.35k
                pdfi_pop(ctx, 1);
1486
4.35k
                code = pdfi_d0(ctx);
1487
4.35k
                break;
1488
49.2k
            case TOKEN_d1:       /* set type 3 font glyph width and bounding box */
1489
49.2k
                pdfi_pop(ctx, 1);
1490
49.2k
                code = pdfi_d1(ctx);
1491
49.2k
                break;
1492
443k
            case TOKEN_Do:       /* invoke named XObject */
1493
443k
                pdfi_pop(ctx, 1);
1494
443k
                code = pdfi_Do(ctx, stream_dict, page_dict);
1495
443k
                break;
1496
918
            case TOKEN_DP:       /* define marked content point with property list */
1497
918
                pdfi_pop(ctx, 1);
1498
918
                code = pdfi_op_DP(ctx, stream_dict, page_dict);
1499
918
                break;
1500
386k
            case TOKEN_EI:       /* end inline image */
1501
386k
                pdfi_pop(ctx, 1);
1502
386k
                code = pdfi_EI(ctx);
1503
386k
                break;
1504
3.62M
            case TOKEN_ET:       /* end text */
1505
3.62M
                pdfi_pop(ctx, 1);
1506
3.62M
                code = pdfi_ET(ctx);
1507
3.62M
                break;
1508
691k
            case TOKEN_EMC:   /* end marked content sequence */
1509
691k
                pdfi_pop(ctx, 1);
1510
691k
                code = pdfi_op_EMC(ctx);
1511
691k
                break;
1512
140k
            case TOKEN_EX:       /* end compatibility section */
1513
140k
                pdfi_pop(ctx, 1);
1514
140k
                break;
1515
3.28M
            case TOKEN_f:           /* fill */
1516
3.28M
                pdfi_pop(ctx, 1);
1517
3.28M
                code = pdfi_fill(ctx);
1518
3.28M
                break;
1519
19.4k
            case TOKEN_F:           /* fill (obselete operator) */
1520
19.4k
                pdfi_pop(ctx, 1);
1521
19.4k
                code = pdfi_fill(ctx);
1522
19.4k
                break;
1523
132k
            case TOKEN_fstar:       /* eofill */
1524
132k
                pdfi_pop(ctx, 1);
1525
132k
                code = pdfi_eofill(ctx);
1526
132k
                break;
1527
843k
            case TOKEN_G:           /* setgray for stroke */
1528
843k
                pdfi_pop(ctx, 1);
1529
843k
                code = pdfi_setgraystroke(ctx);
1530
843k
                break;
1531
1.17M
            case TOKEN_g:           /* setgray for non-stroke */
1532
1.17M
                pdfi_pop(ctx, 1);
1533
1.17M
                code = pdfi_setgrayfill(ctx);
1534
1.17M
                break;
1535
759k
            case TOKEN_gs:       /* set graphics state from dictionary */
1536
759k
                pdfi_pop(ctx, 1);
1537
759k
                code = pdfi_setgstate(ctx, stream_dict, page_dict);
1538
759k
                break;
1539
1.03M
            case TOKEN_h:           /* closepath */
1540
1.03M
                pdfi_pop(ctx, 1);
1541
1.03M
                code = pdfi_closepath(ctx);
1542
1.03M
                break;
1543
436k
            case TOKEN_i:           /* setflat */
1544
436k
                pdfi_pop(ctx, 1);
1545
436k
                code = pdfi_setflat(ctx);
1546
436k
                break;
1547
395k
            case TOKEN_ID:       /* begin inline image data */
1548
395k
                pdfi_pop(ctx, 1);
1549
395k
                code = pdfi_ID(ctx, stream_dict, page_dict, source);
1550
395k
                break;
1551
1.34M
            case TOKEN_j:           /* setlinejoin */
1552
1.34M
                pdfi_pop(ctx, 1);
1553
1.34M
                code = pdfi_setlinejoin(ctx);
1554
1.34M
                break;
1555
1.56M
            case TOKEN_J:           /* setlinecap */
1556
1.56M
                pdfi_pop(ctx, 1);
1557
1.56M
                code = pdfi_setlinecap(ctx);
1558
1.56M
                break;
1559
67.7k
            case TOKEN_K:           /* setcmyk for non-stroke */
1560
67.7k
                pdfi_pop(ctx, 1);
1561
67.7k
                code = pdfi_setcmykstroke(ctx);
1562
67.7k
                break;
1563
241k
            case TOKEN_k:           /* setcmyk for non-stroke */
1564
241k
                pdfi_pop(ctx, 1);
1565
241k
                code = pdfi_setcmykfill(ctx);
1566
241k
                break;
1567
18.7M
            case TOKEN_l:           /* lineto */
1568
18.7M
                pdfi_pop(ctx, 1);
1569
18.7M
                code = pdfi_lineto(ctx);
1570
18.7M
                break;
1571
9.95M
            case TOKEN_m:           /* moveto */
1572
9.95M
                pdfi_pop(ctx, 1);
1573
9.95M
                code = pdfi_moveto(ctx);
1574
9.95M
                break;
1575
63.2k
            case TOKEN_M:           /* setmiterlimit */
1576
63.2k
                pdfi_pop(ctx, 1);
1577
63.2k
                code = pdfi_setmiterlimit(ctx);
1578
63.2k
                break;
1579
3.24k
            case TOKEN_MP:       /* define marked content point */
1580
3.24k
                pdfi_pop(ctx, 1);
1581
3.24k
                code = pdfi_op_MP(ctx);
1582
3.24k
                break;
1583
2.04M
            case TOKEN_n:           /* newpath */
1584
2.04M
                pdfi_pop(ctx, 1);
1585
2.04M
                code = pdfi_newpath(ctx);
1586
2.04M
                break;
1587
5.99M
            case TOKEN_q:           /* gsave */
1588
5.99M
                pdfi_pop(ctx, 1);
1589
5.99M
                code = pdfi_op_q(ctx);
1590
5.99M
                break;
1591
6.05M
            case TOKEN_Q:           /* grestore */
1592
6.05M
                pdfi_pop(ctx, 1);
1593
6.05M
                code = pdfi_op_Q(ctx);
1594
6.05M
                break;
1595
73.2k
            case TOKEN_r:       /* non-standard set rgb colour for non-stroke */
1596
73.2k
                pdfi_pop(ctx, 1);
1597
73.2k
                code = pdfi_setrgbfill_array(ctx);
1598
73.2k
                break;
1599
4.88M
            case TOKEN_re:       /* append rectangle */
1600
4.88M
                pdfi_pop(ctx, 1);
1601
4.88M
                code = pdfi_rectpath(ctx);
1602
4.88M
                break;
1603
1.33M
            case TOKEN_RG:       /* set rgb colour for stroke */
1604
1.33M
                pdfi_pop(ctx, 1);
1605
1.33M
                code = pdfi_setrgbstroke(ctx);
1606
1.33M
                break;
1607
1.84M
            case TOKEN_rg:       /* set rgb colour for non-stroke */
1608
1.84M
                pdfi_pop(ctx, 1);
1609
1.84M
                code = pdfi_setrgbfill(ctx);
1610
1.84M
                break;
1611
207k
            case TOKEN_ri:       /* set rendering intent */
1612
207k
                pdfi_pop(ctx, 1);
1613
207k
                code = pdfi_ri(ctx);
1614
207k
                break;
1615
121k
            case TOKEN_s:           /* closepath, stroke */
1616
121k
                pdfi_pop(ctx, 1);
1617
121k
                code = pdfi_closepath_stroke(ctx);
1618
121k
                break;
1619
5.30M
            case TOKEN_S:           /* stroke */
1620
5.30M
                pdfi_pop(ctx, 1);
1621
5.30M
                code = pdfi_stroke(ctx);
1622
5.30M
                break;
1623
96.7k
            case TOKEN_SC:       /* set colour for stroke */
1624
96.7k
                pdfi_pop(ctx, 1);
1625
96.7k
                code = pdfi_setstrokecolor(ctx);
1626
96.7k
                break;
1627
309k
            case TOKEN_sc:       /* set colour for non-stroke */
1628
309k
                pdfi_pop(ctx, 1);
1629
309k
                code = pdfi_setfillcolor(ctx);
1630
309k
                break;
1631
26.6k
            case TOKEN_SCN:   /* set special colour for stroke */
1632
26.6k
                pdfi_pop(ctx, 1);
1633
26.6k
                code = pdfi_setcolorN(ctx, stream_dict, page_dict, false);
1634
26.6k
                break;
1635
193k
            case TOKEN_scn:   /* set special colour for non-stroke */
1636
193k
                pdfi_pop(ctx, 1);
1637
193k
                code = pdfi_setcolorN(ctx, stream_dict, page_dict, true);
1638
193k
                break;
1639
383k
            case TOKEN_sh:       /* fill with sahding pattern */
1640
383k
                pdfi_pop(ctx, 1);
1641
383k
                code = pdfi_shading(ctx, stream_dict, page_dict);
1642
383k
                break;
1643
192k
            case TOKEN_Tstar:       /* Move to start of next text line */
1644
192k
                pdfi_pop(ctx, 1);
1645
192k
                code = pdfi_T_star(ctx);
1646
192k
                break;
1647
1.12M
            case TOKEN_Tc:       /* set character spacing */
1648
1.12M
                pdfi_pop(ctx, 1);
1649
1.12M
                code = pdfi_Tc(ctx);
1650
1.12M
                break;
1651
2.89M
            case TOKEN_Td:       /* move text position */
1652
2.89M
                pdfi_pop(ctx, 1);
1653
2.89M
                code = pdfi_Td(ctx);
1654
2.89M
                break;
1655
549k
            case TOKEN_TD:       /* Move text position, set leading */
1656
549k
                pdfi_pop(ctx, 1);
1657
549k
                code = pdfi_TD(ctx);
1658
549k
                break;
1659
2.56M
            case TOKEN_Tf:       /* set font and size */
1660
2.56M
                pdfi_pop(ctx, 1);
1661
2.56M
                code = pdfi_Tf(ctx, stream_dict, page_dict);
1662
2.56M
                break;
1663
3.17M
            case TOKEN_Tj:       /* show text */
1664
3.17M
                pdfi_pop(ctx, 1);
1665
3.17M
                code = pdfi_Tj(ctx);
1666
3.17M
                break;
1667
3.50M
            case TOKEN_TJ:       /* show text with individual glyph positioning */
1668
3.50M
                pdfi_pop(ctx, 1);
1669
3.50M
                code = pdfi_TJ(ctx);
1670
3.50M
                break;
1671
30.8k
            case TOKEN_TL:       /* set text leading */
1672
30.8k
                pdfi_pop(ctx, 1);
1673
30.8k
                code = pdfi_TL(ctx);
1674
30.8k
                break;
1675
3.08M
            case TOKEN_Tm:       /* set text matrix */
1676
3.08M
                pdfi_pop(ctx, 1);
1677
3.08M
                code = pdfi_Tm(ctx);
1678
3.08M
                break;
1679
992k
            case TOKEN_Tr:       /* set text rendering mode */
1680
992k
                pdfi_pop(ctx, 1);
1681
992k
                code = pdfi_Tr(ctx);
1682
992k
                break;
1683
6.49k
            case TOKEN_Ts:       /* set text rise */
1684
6.49k
                pdfi_pop(ctx, 1);
1685
6.49k
                code = pdfi_Ts(ctx);
1686
6.49k
                break;
1687
221k
            case TOKEN_Tw:       /* set word spacing */
1688
221k
                pdfi_pop(ctx, 1);
1689
221k
                code = pdfi_Tw(ctx);
1690
221k
                break;
1691
295k
            case TOKEN_Tz:       /* set text matrix */
1692
295k
                pdfi_pop(ctx, 1);
1693
295k
                code = pdfi_Tz(ctx);
1694
295k
                break;
1695
188k
            case TOKEN_v:           /* append curve (initial point replicated) */
1696
188k
                pdfi_pop(ctx, 1);
1697
188k
                code = pdfi_v_curveto(ctx);
1698
188k
                break;
1699
3.70M
            case TOKEN_w:           /* setlinewidth */
1700
3.70M
                pdfi_pop(ctx, 1);
1701
3.70M
                code = pdfi_setlinewidth(ctx);
1702
3.70M
                break;
1703
1.20M
            case TOKEN_W:           /* clip */
1704
1.20M
                pdfi_pop(ctx, 1);
1705
1.20M
                ctx->clip_active = true;
1706
1.20M
                ctx->do_eoclip = false;
1707
1.20M
                break;
1708
76.8k
            case TOKEN_Wstar:       /* eoclip */
1709
76.8k
                pdfi_pop(ctx, 1);
1710
76.8k
                ctx->clip_active = true;
1711
76.8k
                ctx->do_eoclip = true;
1712
76.8k
                break;
1713
219k
            case TOKEN_y:           /* append curve (final point replicated) */
1714
219k
                pdfi_pop(ctx, 1);
1715
219k
                code = pdfi_y_curveto(ctx);
1716
219k
                break;
1717
23.1k
            case TOKEN_APOSTROPHE:          /* move to next line and show text */
1718
23.1k
                pdfi_pop(ctx, 1);
1719
23.1k
                code = pdfi_singlequote(ctx);
1720
23.1k
                break;
1721
9.07k
            case TOKEN_QUOTE:           /* set word and character spacing, move to next line, show text */
1722
9.07k
                pdfi_pop(ctx, 1);
1723
9.07k
                code = pdfi_doublequote(ctx);
1724
9.07k
                break;
1725
4.86k
            default:
1726
                /* Shouldn't we return an error here? Original code didn't seem to. */
1727
4.86k
                break;
1728
124M
        }
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
124M
        if (code > 0)
1734
0
            code = 0;
1735
124M
        return code;
1736
124M
    } 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
12.1M
        code = split_bogus_operator(ctx, source, stream_dict, page_dict);
1743
12.1M
        if (code < 0)
1744
475k
            return code;
1745
11.7M
        if (pdfi_count_stack(ctx) > 0) {
1746
15
            keyword = ctx->stack_top[-1];
1747
15
            if (keyword != PDF_TOKEN_AS_OBJ(TOKEN_NOT_A_KEYWORD))
1748
15
                return REPAIRED_KEYWORD;
1749
15
        }
1750
11.7M
    }
1751
11.7M
    return 0;
1752
136M
}
1753
1754
void local_save_stream_state(pdf_context *ctx, stream_save *local_save)
1755
941k
{
1756
    /* copy the 'save_stream' data from the context to a local structure */
1757
941k
    local_save->stream_offset = ctx->current_stream_save.stream_offset;
1758
941k
    local_save->gsave_level = ctx->current_stream_save.gsave_level;
1759
941k
    local_save->stack_count = ctx->current_stream_save.stack_count;
1760
941k
    local_save->group_depth = ctx->current_stream_save.group_depth;
1761
941k
}
1762
1763
void cleanup_context_interpretation(pdf_context *ctx, stream_save *local_save)
1764
941k
{
1765
941k
    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
941k
    if (ctx->current_stream_save.group_depth != local_save->group_depth) {
1770
9
        pdfi_set_warning(ctx, 0, NULL, W_PDF_GROUPERROR, "pdfi_cleanup_context_interpretation", NULL);
1771
18
        while (ctx->current_stream_save.group_depth > local_save->group_depth)
1772
9
            pdfi_trans_end_group(ctx);
1773
9
    }
1774
941k
    if (ctx->pgs->level > ctx->current_stream_save.gsave_level)
1775
29.1k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_TOOMANYq, "pdfi_cleanup_context_interpretation", NULL);
1776
941k
    if (pdfi_count_stack(ctx) > ctx->current_stream_save.stack_count)
1777
11.5k
        pdfi_set_warning(ctx, 0, NULL, W_PDF_STACKGARBAGE, "pdfi_cleanup_context_interpretation", NULL);
1778
1.29M
    while (ctx->pgs->level > ctx->current_stream_save.gsave_level)
1779
357k
        pdfi_grestore(ctx);
1780
941k
    pdfi_clearstack(ctx);
1781
941k
}
1782
1783
void local_restore_stream_state(pdf_context *ctx, stream_save *local_save)
1784
941k
{
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
941k
    ctx->current_stream_save.stream_offset = local_save->stream_offset;
1790
941k
    ctx->current_stream_save.gsave_level = local_save->gsave_level;
1791
941k
    ctx->current_stream_save.stack_count = local_save->stack_count;
1792
941k
    ctx->current_stream_save.group_depth = local_save->group_depth;
1793
941k
}
1794
1795
void initialise_stream_save(pdf_context *ctx)
1796
941k
{
1797
    /* Set up the values in the context to the current values */
1798
941k
    ctx->current_stream_save.stream_offset = pdfi_tell(ctx->main_stream);
1799
941k
    ctx->current_stream_save.gsave_level = ctx->pgs->level;
1800
941k
    ctx->current_stream_save.stack_count = pdfi_count_total_stack(ctx);
1801
941k
}
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
446k
{
1807
446k
    int code = 0, code1 = 0;
1808
446k
    gs_gstate *DefaultQState = NULL;
1809
    /* Save any existing Default* colour spaces */
1810
446k
    gs_color_space *PageDefaultGray = ctx->page.DefaultGray_cs;
1811
446k
    gs_color_space *PageDefaultRGB = ctx->page.DefaultRGB_cs;
1812
446k
    gs_color_space *PageDefaultCMYK = ctx->page.DefaultCMYK_cs;
1813
1814
446k
    ctx->page.DefaultGray_cs = NULL;
1815
446k
    ctx->page.DefaultRGB_cs = NULL;
1816
446k
    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
446k
    code = pdfi_setup_DefaultSpaces(ctx, stream_obj->stream_dict);
1825
446k
    if (code < 0)
1826
0
        goto exit;
1827
1828
    /* If no Default* space found, try using the Page level ones (if any) */
1829
446k
    if (ctx->page.DefaultGray_cs == NULL) {
1830
446k
        ctx->page.DefaultGray_cs = PageDefaultGray;
1831
446k
        rc_increment(PageDefaultGray);
1832
446k
    }
1833
446k
    if (ctx->page.DefaultRGB_cs == NULL) {
1834
445k
        ctx->page.DefaultRGB_cs = PageDefaultRGB;
1835
445k
        rc_increment(PageDefaultRGB);
1836
445k
    }
1837
446k
    if (ctx->page.DefaultCMYK_cs == NULL) {
1838
446k
        ctx->page.DefaultCMYK_cs = PageDefaultCMYK;
1839
446k
        rc_increment(PageDefaultCMYK);
1840
446k
    }
1841
1842
446k
    code = pdfi_copy_DefaultQState(ctx, &DefaultQState);
1843
446k
    if (code < 0)
1844
0
        goto exit;
1845
1846
446k
    code = pdfi_set_DefaultQState(ctx, ctx->pgs);
1847
446k
    if (code < 0)
1848
0
        goto exit;
1849
1850
446k
    code = pdfi_interpret_inner_content_stream(ctx, stream_obj, page_dict, stoponerror, desc);
1851
1852
446k
    code1 = pdfi_restore_DefaultQState(ctx, &DefaultQState);
1853
446k
    if (code >= 0)
1854
446k
        code = code1;
1855
1856
446k
exit:
1857
446k
    if (DefaultQState != NULL) {
1858
0
        gs_gstate_free(DefaultQState);
1859
0
        DefaultQState = NULL;
1860
0
    }
1861
1862
    /* Count down any Default* colour spaces */
1863
446k
    rc_decrement(ctx->page.DefaultGray_cs, "pdfi_run_context");
1864
446k
    rc_decrement(ctx->page.DefaultRGB_cs, "pdfi_run_context");
1865
446k
    rc_decrement(ctx->page.DefaultCMYK_cs, "pdfi_run_context");
1866
1867
    /* And restore the page level ones (if any) */
1868
446k
    ctx->page.DefaultGray_cs = PageDefaultGray;
1869
446k
    ctx->page.DefaultRGB_cs = PageDefaultRGB;
1870
446k
    ctx->page.DefaultCMYK_cs = PageDefaultCMYK;
1871
1872
#if DEBUG_CONTEXT
1873
    dbgmprintf(ctx->memory, "pdfi_run_context END\n");
1874
#endif
1875
446k
    return code;
1876
446k
}
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
531k
{
1887
531k
    int code = 0;
1888
531k
    bool saved_stoponerror = ctx->args.pdfstoponerror;
1889
531k
    stream_save local_entry_save;
1890
1891
531k
    local_save_stream_state(ctx, &local_entry_save);
1892
531k
    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
531k
    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
531k
    if (code < 0)
1920
531k
        dbgmprintf1(ctx->memory, "ERROR: inner_stream: code %d when rendering stream\n", code);
1921
1922
531k
    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
531k
    cleanup_context_interpretation(ctx, &local_entry_save);
1932
531k
    local_restore_stream_state(ctx, &local_entry_save);
1933
531k
    if (code < 0)
1934
10.4k
        code = pdfi_set_error_stop(ctx, code, NULL, 0, "pdfi_interpret_inner_content", NULL);
1935
531k
    return code;
1936
531k
}
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
85.2k
{
1946
85.2k
    int code = 0;
1947
85.2k
    pdf_c_stream *stream = NULL;
1948
85.2k
    pdf_stream *stream_obj = NULL;
1949
1950
85.2k
    if (content_length == 0)
1951
0
        return 0;
1952
1953
85.2k
    code = pdfi_open_memory_stream_from_memory(ctx, content_length,
1954
85.2k
                                               content_data, &stream, true);
1955
85.2k
    if (code < 0)
1956
0
        goto exit;
1957
1958
85.2k
    code = pdfi_obj_dict_to_stream(ctx, stream_dict, &stream_obj, false);
1959
85.2k
    if (code < 0)
1960
0
        return code;
1961
1962
    /* NOTE: stream gets closed in here */
1963
85.2k
    code = pdfi_interpret_inner_content(ctx, stream, stream_obj, page_dict, stoponerror, desc);
1964
85.2k
    pdfi_countdown(stream_obj);
1965
85.2k
 exit:
1966
85.2k
    return code;
1967
85.2k
}
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
26.3k
{
1976
26.3k
    uint32_t length = (uint32_t)strlen(content_string);
1977
26.3k
    bool decrypt_strings;
1978
26.3k
    int code;
1979
1980
26.3k
    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
26.3k
    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
26.3k
    decrypt_strings = ctx->encryption.decrypt_strings;
1993
26.3k
    ctx->encryption.decrypt_strings = false;
1994
26.3k
    code = pdfi_interpret_inner_content_buffer(ctx, (byte *)content_string, length,
1995
26.3k
                                               stream_dict, page_dict, stoponerror, desc);
1996
26.3k
    ctx->encryption.decrypt_strings = decrypt_strings;
1997
1998
26.3k
    return code;
1999
26.3k
}
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
58.9k
{
2008
58.9k
    return pdfi_interpret_inner_content_buffer(ctx, content_string->data, content_string->length,
2009
58.9k
                                               stream_dict, page_dict, stoponerror, desc);
2010
58.9k
}
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
446k
{
2018
446k
    return pdfi_interpret_inner_content(ctx, NULL, stream_obj, page_dict, stoponerror, desc);
2019
446k
}
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
745k
{
2030
745k
    int code;
2031
745k
    pdf_c_stream *stream = NULL, *SubFile_stream = NULL;
2032
745k
    pdf_keyword *keyword;
2033
745k
    pdf_stream *s = ctx->current_stream;
2034
745k
    pdf_obj_type type;
2035
745k
    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
1.04M
    while (s != NULL && pdfi_type_of(s) == PDF_STREAM) {
2045
299k
        if (s->object_num > 0) {
2046
299k
            if (s->object_num == stream_obj->object_num) {
2047
1.11k
                pdf_dict *d = NULL;
2048
1.11k
                bool known = false;
2049
2050
1.11k
                code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream_obj, &d);
2051
1.11k
                if (code >= 0) {
2052
1.11k
                    code = pdfi_dict_known(ctx, d, "Parent", &known);
2053
1.11k
                    if (code >= 0 && known)
2054
179
                        (void)pdfi_dict_delete(ctx, d, "Parent");
2055
1.11k
                }
2056
1.11k
                pdfi_set_error(ctx, 0, NULL, E_PDF_CIRCULARREF, "pdfi_interpret_content_stream", "Aborting stream");
2057
1.11k
                return_error(gs_error_circular_reference);
2058
1.11k
            }
2059
299k
        }
2060
298k
        s = (pdf_stream *)s->parent_obj;
2061
298k
    }
2062
2063
743k
    if (content_stream != NULL) {
2064
142k
        stream = content_stream;
2065
601k
    } else {
2066
601k
        code = pdfi_seek(ctx, ctx->main_stream, pdfi_stream_offset(ctx, stream_obj), SEEK_SET);
2067
601k
        if (code < 0)
2068
0
            return code;
2069
2070
601k
        if (stream_obj->length_valid) {
2071
592k
            if (stream_obj->Length == 0)
2072
3.14k
                return 0;
2073
589k
            code = pdfi_apply_SubFileDecode_filter(ctx, stream_obj->Length, NULL, ctx->main_stream, &SubFile_stream, false);
2074
589k
        }
2075
9.32k
        else
2076
9.32k
            code = pdfi_apply_SubFileDecode_filter(ctx, 0, EODString, ctx->main_stream, &SubFile_stream, false);
2077
598k
        if (code < 0)
2078
0
            return code;
2079
2080
598k
        code = pdfi_filter(ctx, stream_obj, SubFile_stream, &stream, false);
2081
598k
        if (code < 0) {
2082
674
            pdfi_close_file(ctx, SubFile_stream);
2083
674
            return code;
2084
674
        }
2085
598k
    }
2086
2087
740k
    pdfi_set_stream_parent(ctx, stream_obj, ctx->current_stream);
2088
740k
    ctx->current_stream = stream_obj;
2089
2090
484M
    do {
2091
484M
        code = pdfi_read_token(ctx, stream, stream_obj->object_num, stream_obj->generation_num);
2092
484M
        if (code < 0) {
2093
945k
            if (code == gs_error_ioerror || code == gs_error_VMerror || ctx->args.pdfstoponerror) {
2094
20.8k
                if (code == gs_error_ioerror) {
2095
20.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
20.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
20.8k
                goto exit;
2100
20.8k
            }
2101
925k
            continue;
2102
945k
        }
2103
2104
483M
        if (pdfi_count_stack(ctx) <= 0) {
2105
380k
            if(stream->eof == true)
2106
352k
                break;
2107
380k
        }
2108
2109
483M
repaired_keyword:
2110
483M
        type = pdfi_type_of(ctx->stack_top[-1]);
2111
483M
        if (type == PDF_FAST_KEYWORD) {
2112
123M
            keyword = (pdf_keyword *)ctx->stack_top[-1];
2113
2114
123M
            switch((uintptr_t)keyword) {
2115
57.0k
                case TOKEN_ENDSTREAM:
2116
57.0k
                    pdfi_pop(ctx,1);
2117
57.0k
                    goto exit;
2118
0
                    break;
2119
211
                case TOKEN_ENDOBJ:
2120
211
                    pdfi_clearstack(ctx);
2121
211
                    code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MISSINGENDSTREAM, "pdfi_interpret_content_stream", NULL);
2122
211
                    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
123M
                default:
2135
123M
                    goto execute;
2136
123M
            }
2137
123M
        }
2138
359M
        else if (type == PDF_KEYWORD)
2139
12.1M
        {
2140
135M
execute:
2141
135M
            {
2142
135M
                pdf_dict *stream_dict = NULL;
2143
2144
135M
                code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream_obj, &stream_dict);
2145
135M
                if (code < 0)
2146
0
                    goto exit;
2147
2148
135M
                code = pdfi_interpret_stream_operator(ctx, stream, stream_dict, page_dict);
2149
135M
                if (code == REPAIRED_KEYWORD)
2150
15
                    goto repaired_keyword;
2151
2152
135M
                if (code < 0) {
2153
6.44M
                    if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_TOKENERROR, "pdf_interpret_content_stream", NULL)) < 0) {
2154
1.25k
                        pdfi_clearstack(ctx);
2155
1.25k
                        goto exit;
2156
1.25k
                    }
2157
6.44M
                }
2158
135M
            }
2159
135M
        }
2160
482M
        if(stream->eof == true)
2161
308k
            break;
2162
483M
    }while(1);
2163
2164
740k
exit:
2165
740k
    ctx->current_stream = pdfi_stream_parent(ctx, stream_obj);
2166
740k
    pdfi_clear_stream_parent(ctx, stream_obj);
2167
740k
    pdfi_close_file(ctx, stream);
2168
740k
    if (SubFile_stream != NULL)
2169
597k
        pdfi_close_file(ctx, SubFile_stream);
2170
740k
    return code;
2171
740k
}