Coverage Report

Created: 2026-09-14 07:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/psi/iscan.c
Line
Count
Source
1
/* Copyright (C) 2001-2023 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
17
/* Token scanner for Ghostscript interpreter */
18
#include "ghost.h"
19
#include "memory_.h"
20
#include "string_.h"
21
#include "stream.h"
22
#include "ierrors.h"
23
#include "btoken.h"             /* for ref_binary_object_format */
24
#include "files.h"              /* for fptr */
25
#include "ialloc.h"
26
#include "idict.h"              /* for //name lookup */
27
#include "dstack.h"             /* ditto */
28
#include "ilevel.h"
29
#include "iname.h"
30
#include "ipacked.h"
31
#include "iparray.h"
32
#include "strimpl.h"            /* for string decoding */
33
#include "sa85d.h"              /* ditto */
34
#include "sfilter.h"            /* ditto */
35
#include "ostack.h"             /* for accumulating proc bodies; */
36
                                        /* must precede iscan.h */
37
#include "iscan.h"              /* defines interface */
38
#include "iscanbin.h"
39
#include "iscannum.h"
40
#include "istream.h"
41
#include "istruct.h"            /* for RELOC_REF_VAR */
42
#include "iutil.h"
43
#include "ivmspace.h"
44
#include "store.h"
45
#include "scanchar.h"
46
47
/*
48
 * Level 2 includes some changes in the scanner:
49
 *      - \ is always recognized in strings, regardless of the data source;
50
 *      - << and >> are legal tokens;
51
 *      - <~ introduces an ASCII85 encoded string (terminated by ~>);
52
 *      - Character codes above 127 introduce binary objects.
53
 * We explicitly enable or disable these changes based on level2_enabled.
54
 */
55
56
/* ------ Dynamic strings ------ */
57
58
/* Begin collecting a dynamically allocated string. */
59
static inline void
60
dynamic_init(da_ptr pda, gs_memory_t *mem)
61
158M
{
62
158M
    pda->is_dynamic = false;
63
158M
    pda->limit = pda->buf + sizeof(pda->buf);
64
158M
    pda->next = pda->base = pda->buf;
65
158M
    pda->memory = mem;
66
158M
}
67
68
/* Free a dynamic string. */
69
static void
70
dynamic_free(da_ptr pda)
71
1.24G
{
72
1.24G
    if (pda->is_dynamic)
73
8.29M
        gs_free_string(pda->memory, pda->base, da_size(pda), "scanner");
74
1.24G
}
75
76
/* Resize a dynamic string. */
77
/* If the allocation fails, return gs_error_VMerror; otherwise, return 0. */
78
static int
79
dynamic_resize(da_ptr pda, uint new_size)
80
167M
{
81
167M
    uint old_size = da_size(pda);
82
167M
    uint pos = pda->next - pda->base;
83
167M
    gs_memory_t *mem = pda->memory;
84
167M
    byte *base;
85
86
167M
    if (pda->is_dynamic) {
87
1.01M
        base = gs_resize_string(mem, pda->base, old_size,
88
1.01M
                                new_size, "scanner");
89
1.01M
        if (base == 0)
90
0
            return_error(gs_error_VMerror);
91
166M
    } else {                    /* switching from static to dynamic */
92
166M
        base = gs_alloc_string(mem, new_size, "scanner");
93
166M
        if (base == 0)
94
0
            return_error(gs_error_VMerror);
95
166M
        memcpy(base, pda->base, min(old_size, new_size));
96
166M
        pda->is_dynamic = true;
97
166M
    }
98
167M
    pda->base = base;
99
167M
    pda->next = base + pos;
100
167M
    pda->limit = base + new_size;
101
167M
    return 0;
102
167M
}
103
104
/* Grow a dynamic string. */
105
/* Return 0 if the allocation failed, the new 'next' ptr if OK. */
106
/* Return 0 or an error code, updating pda->next to point to the first */
107
/* available byte after growing. */
108
static int
109
dynamic_grow(da_ptr pda, byte * next, uint max_size)
110
9.31M
{
111
9.31M
    uint old_size = da_size(pda);
112
9.31M
    uint new_size = (old_size < 10 ? 20 :
113
9.31M
                     old_size >= (max_size >> 1) ? max_size :
114
2.44M
                     old_size << 1);
115
9.31M
    int code;
116
117
9.31M
    pda->next = next;
118
9.31M
    if (old_size >= max_size)
119
67
        return_error(gs_error_limitcheck);
120
9.31M
    while ((code = dynamic_resize(pda, new_size)) < 0) {
121
        /* Try trimming down the requested new size. */
122
0
        new_size -= (new_size - old_size + 1) >> 1;
123
0
        if (new_size <= old_size)
124
0
                break;
125
0
    }
126
9.31M
    return code;
127
9.31M
}
128
129
/* Ensure that a dynamic string is either on the heap or in the */
130
/* private buffer. */
131
static void
132
dynamic_save(da_ptr pda)
133
728k
{
134
728k
    if (!pda->is_dynamic && pda->base != pda->buf) {
135
24
        int len = da_size(pda);
136
137
24
        if (len > sizeof(pda->buf))
138
0
            len = sizeof(pda->buf);
139
        /* This can happen if we get a /<CR> at the end of a buffer, and the file is
140
         * not at EOF. In this case 'len' will be zero so we don't actually copy any
141
         * bytes. So this is safe on current C run-time libraries, but it's probably
142
         * best to avoid it. Coverity ID C382008
143
         */
144
24
        if (pda->base != NULL)
145
20
            memcpy(pda->buf, pda->base, len);
146
24
        pda->next = pda->buf + len;
147
24
        pda->base = pda->buf;
148
24
    }
149
728k
}
150
151
/* Finish collecting a dynamic string. */
152
static int
153
dynamic_make_string(i_ctx_t *i_ctx_p, ref * pref, da_ptr pda, byte * next)
154
157M
{
155
157M
    uint size = (pda->next = next) - pda->base;
156
157M
    int code = dynamic_resize(pda, size);
157
158
157M
    if (code < 0)
159
0
        return code;
160
157M
    make_tasv_new(pref, t_string,
161
157M
                  a_all | imemory_space((gs_ref_memory_t *) pda->memory),
162
157M
                  size, bytes, pda->base);
163
157M
    return 0;
164
157M
}
165
166
/* ------ Main scanner ------ */
167
168
/* GC procedures */
169
static
170
CLEAR_MARKS_PROC(scanner_clear_marks)
171
284
{
172
284
    scanner_state *const ssptr = vptr;
173
174
284
    r_clear_attrs(&ssptr->s_file, l_mark);
175
284
    r_clear_attrs(&ssptr->s_ss.binary.bin_array, l_mark);
176
284
    r_clear_attrs(&ssptr->s_error.object, l_mark);
177
284
}
178
static
179
892
ENUM_PTRS_WITH(scanner_enum_ptrs, scanner_state *ssptr) return 0;
180
223
case 0:
181
223
    ENUM_RETURN_REF(&ssptr->s_file);
182
223
case 1:
183
223
    ENUM_RETURN_REF(&ssptr->s_error.object);
184
223
case 2:
185
223
    if (ssptr->s_scan_type == scanning_none ||
186
14
        !ssptr->s_da.is_dynamic
187
223
        )
188
209
        ENUM_RETURN(0);
189
14
    return ENUM_STRING2(ssptr->s_da.base, da_size(&ssptr->s_da));
190
223
case 3:
191
223
    if (ssptr->s_scan_type != scanning_binary)
192
223
        return 0;
193
892
    ENUM_RETURN_REF(&ssptr->s_ss.binary.bin_array);
194
892
ENUM_PTRS_END
195
223
static RELOC_PTRS_WITH(scanner_reloc_ptrs, scanner_state *ssptr)
196
223
{
197
223
    RELOC_REF_VAR(ssptr->s_file);
198
223
    r_clear_attrs(&ssptr->s_file, l_mark);
199
223
    if (ssptr->s_scan_type != scanning_none && ssptr->s_da.is_dynamic) {
200
14
        gs_string sda;
201
202
14
        sda.data = ssptr->s_da.base;
203
14
        sda.size = da_size(&ssptr->s_da);
204
14
        RELOC_STRING_VAR(sda);
205
14
        ssptr->s_da.limit = sda.data + sda.size;
206
14
        ssptr->s_da.next = sda.data + (ssptr->s_da.next - ssptr->s_da.base);
207
14
        ssptr->s_da.base = sda.data;
208
14
    }
209
223
    if (ssptr->s_scan_type == scanning_binary) {
210
0
        RELOC_REF_VAR(ssptr->s_ss.binary.bin_array);
211
0
        r_clear_attrs(&ssptr->s_ss.binary.bin_array, l_mark);
212
0
    }
213
223
    RELOC_REF_VAR(ssptr->s_error.object);
214
223
    r_clear_attrs(&ssptr->s_error.object, l_mark);
215
223
}
216
223
RELOC_PTRS_END
217
/* Structure type */
218
public_st_scanner_state_dynamic();
219
220
/* Initialize a scanner. */
221
void
222
gs_scanner_init_options(scanner_state *sstate, const ref *fop, int options)
223
5.46G
{
224
5.46G
    ref_assign(&sstate->s_file, fop);
225
5.46G
    sstate->s_scan_type = scanning_none;
226
5.46G
    sstate->s_pstack = 0;
227
5.46G
    sstate->s_options = options;
228
5.46G
    SCAN_INIT_ERROR(sstate);
229
5.46G
}
230
void gs_scanner_init_stream_options(scanner_state *sstate, stream *s,
231
                                 int options)
232
2.49M
{
233
    /*
234
     * The file 'object' will never be accessed, but it must be in correct
235
     * form for the GC.
236
     */
237
2.49M
    ref fobj;
238
239
2.49M
    make_file(&fobj, a_read, 0, s);
240
2.49M
    gs_scanner_init_options(sstate, &fobj, options);
241
2.49M
}
242
243
/*
244
 * Return the "error object" to be stored in $error.command instead of
245
 * --token--, if any, or <0 if no special error object is available.
246
 */
247
int
248
gs_scanner_error_object(i_ctx_t *i_ctx_p, const scanner_state *pstate,
249
                     ref *pseo)
250
16.5k
{
251
16.5k
    if (!r_has_type(&pstate->s_error.object, t__invalid)) {
252
71
        ref_assign(pseo, &pstate->s_error.object);
253
71
        return 0;
254
71
    }
255
16.4k
    if (pstate->s_error.string[0]) {
256
5.37k
        int len = strlen(pstate->s_error.string);
257
258
5.37k
        if (pstate->s_error.is_name) {
259
135
            int code = name_ref(imemory, (const byte *)pstate->s_error.string, len, pseo, 1);
260
261
135
            if (code < 0)
262
0
                return code;
263
135
            r_set_attrs(pseo, a_executable); /* Adobe compatibility */
264
135
            return 0;
265
5.23k
        } else {
266
5.23k
            byte *estr = ialloc_string(len, "gs_scanner_error_object");
267
268
5.23k
            if (estr == 0)
269
0
                return -1;              /* VMerror */
270
5.23k
            memcpy(estr, (const byte *)pstate->s_error.string, len);
271
5.23k
            make_string(pseo, a_all | icurrent_space, len, estr);
272
5.23k
            return 0;
273
5.23k
        }
274
5.37k
    }
275
11.1k
    return -1;                  /* no error object */
276
16.4k
}
277
278
/* Handle a scan_Refill return from gs_scan_token. */
279
/* This may return o_push_estack, 0 (meaning just call gs_scan_token */
280
/* again), or an error code. */
281
int
282
gs_scan_handle_refill(i_ctx_t *i_ctx_p, scanner_state * sstate,
283
                   bool save, op_proc_t cont)
284
2.01M
{
285
2.01M
    const ref *const fop = &sstate->s_file;
286
2.01M
    stream *s = fptr(fop);
287
2.01M
    uint avail = sbufavailable(s);
288
2.01M
    int status;
289
290
2.01M
    if (s->end_status == EOFC) {
291
        /* More data needed, but none available, so this is a syntax error. */
292
769
        return_error(gs_error_syntaxerror);
293
769
    }
294
2.01M
    status = s_process_read_buf(s);
295
2.01M
    if (sbufavailable(s) > avail)
296
405k
        return 0;
297
1.60M
    if (status == 0)
298
1.60M
        status = s->end_status;
299
1.60M
    switch (status) {
300
874
        case EOFC:
301
            /* We just discovered that we're at EOF. */
302
            /* Let the caller find this out. */
303
874
            return 0;
304
32
        case ERRC:
305
32
            return_error(gs_error_ioerror);
306
0
        case INTC:
307
1.60M
        case CALLC:
308
1.60M
            {
309
1.60M
                ref rstate[1];
310
1.60M
                scanner_state *pstate;
311
312
1.60M
                if (save) {
313
1.46M
                    pstate = (scanner_state *)
314
1.46M
                        ialloc_struct(scanner_state_dynamic, &st_scanner_state_dynamic,
315
1.46M
                                      "gs_scan_handle_refill");
316
1.46M
                    if (pstate == 0)
317
0
                        return_error(gs_error_VMerror);
318
1.46M
                    ((scanner_state_dynamic *)pstate)->mem = imemory;
319
1.46M
                    *pstate = *sstate;
320
1.46M
                } else
321
136k
                    pstate = sstate;
322
1.60M
                make_istruct(&rstate[0], 0, pstate);
323
1.60M
                return s_handle_read_exception(i_ctx_p, status, fop,
324
1.60M
                                               rstate, 1, cont);
325
1.60M
            }
326
1.60M
    }
327
    /* No more data available, but no exception. */
328
    /* A filter is consuming headers but returns nothing. */
329
1.82k
    return 0;
330
1.60M
}
331
332
/*
333
 * Handle a comment.  The 'saved' argument is needed only for
334
 * tracing printout.
335
 */
336
static int
337
scan_comment(i_ctx_t *i_ctx_p, ref *pref, scanner_state *pstate,
338
             const byte * base, const byte * end, bool saved)
339
3.18M
{
340
3.18M
    uint len = (uint) (end - base);
341
3.18M
    int code;
342
#ifdef DEBUG
343
    const char *sstr = (saved ? ">" : "");
344
#endif
345
346
3.18M
    if (len > 1 && (base[1] == '%' || base[1] == '!')) {
347
        /* Process as a DSC comment if requested. */
348
#ifdef DEBUG
349
        if (gs_debug_c('%')) {
350
            dmlprintf2(imemory, "[%%%%%s%c]", sstr, (len >= 3 ? '+' : '-'));
351
            debug_print_string(imemory, base, len);
352
            dmputs(imemory, "\n");
353
        }
354
#endif
355
1.16M
        if (pstate->s_options & SCAN_PROCESS_DSC_COMMENTS) {
356
1.14M
            code = scan_DSC_Comment;
357
1.14M
            goto comment;
358
1.14M
        }
359
        /* Treat as an ordinary comment. */
360
1.16M
    }
361
#ifdef DEBUG
362
    else {
363
        if (gs_debug_c('%')) {
364
            dmlprintf2(imemory, "[%% %s%c]", sstr, (len >= 2 ? '+' : '-'));
365
            debug_print_string(imemory, base, len);
366
            dmputs(imemory, "\n");
367
        }
368
    }
369
#endif
370
2.03M
    if (pstate->s_options & SCAN_PROCESS_COMMENTS) {
371
0
        code = scan_Comment;
372
0
        goto comment;
373
0
    }
374
2.03M
    return 0;
375
1.14M
 comment:
376
1.14M
    {
377
1.14M
        byte *cstr = ialloc_string(len, "scan_comment");
378
379
1.14M
        if (cstr == 0)
380
0
            return_error(gs_error_VMerror);
381
1.14M
        memcpy(cstr, base, len);
382
1.14M
        make_string(pref, a_all | icurrent_space, len, cstr);
383
1.14M
    }
384
0
    return code;
385
1.14M
}
386
387
/* Read a token from a string. */
388
/* Update the string if succesful. */
389
/* Store the error object in i_ctx_p->error_object if not. */
390
int
391
gs_scan_string_token_options(i_ctx_t *i_ctx_p, ref * pstr, ref * pref,
392
                             int options)
393
1.61M
{
394
1.61M
    stream st;
395
1.61M
    stream *s = &st;
396
1.61M
    scanner_state state;
397
1.61M
    int code;
398
399
1.61M
    if (!r_has_attr(pstr, a_read))
400
0
        return_error(gs_error_invalidaccess);
401
1.61M
    s_init(s, NULL);
402
1.61M
    sread_string(s, pstr->value.bytes, r_size(pstr));
403
1.61M
    gs_scanner_init_stream_options(&state, s, options | SCAN_FROM_STRING);
404
1.61M
    switch (code = gs_scan_token(i_ctx_p, pref, &state)) {
405
11
        default:                /* error or comment */
406
11
            if (code < 0)
407
11
                break;
408
            /* falls through */
409
1.61M
        case 0:         /* read a token */
410
1.61M
        case scan_BOS:
411
1.61M
            {
412
1.61M
                uint pos = stell(s);
413
414
1.61M
                pstr->value.bytes += pos;
415
1.61M
                r_dec_size(pstr, pos);
416
1.61M
            }
417
1.61M
            break;
418
0
        case scan_Refill:       /* error */
419
0
            code = gs_note_error(gs_error_syntaxerror);
420
111
        case scan_EOF:
421
111
            break;
422
1.61M
    }
423
1.61M
    if (code < 0)
424
11
        gs_scanner_error_object(i_ctx_p, &state, &i_ctx_p->error_object);
425
1.61M
    return code;
426
1.61M
}
427
428
/*
429
 * Read a token from a stream.  Return 0 if an ordinary token was read,
430
 * >0 for special situations (see iscan.h).
431
 * If the token required a terminating character (i.e., was a name or
432
 * number) and the next character was whitespace, read and discard
433
 * that character.  Note that the state is relevant for gs_error_VMerror
434
 * as well as for scan_Refill.
435
 */
436
int
437
gs_scan_token(i_ctx_t *i_ctx_p, ref * pref, scanner_state * pstate) /* lgtm [cpp/use-of-goto] */
438
5.46G
{
439
5.46G
    stream *const s = pstate->s_file.value.pfile;
440
5.46G
    ref *myref = pref;
441
5.46G
    int retcode = 0;
442
5.46G
    int c;
443
444
5.46G
    s_declare_inline(s, sptr, endptr);
445
5.46G
    const byte *newptr;
446
5.46G
    byte *daptr;
447
448
5.46G
#define sreturn(code)\
449
5.46G
  { retcode = gs_note_error(code); goto sret; }
450
5.46G
#define if_not_spush1()\
451
5.77G
  if ( osp < ostop ) osp++;\
452
5.77G
  else if ( (retcode = ref_stack_push(&o_stack, 1)) >= 0 )\
453
41.9k
    ;\
454
41.9k
  else
455
5.46G
#define spop1()\
456
5.46G
  if ( osp >= osbot ) osp--;\
457
191M
  else ref_stack_pop(&o_stack, 1)
458
5.46G
    int max_name_ctype =
459
5.46G
        ((ref_binary_object_format.value.intval != 0 && level2_enabled)? ctype_name : ctype_btoken);
460
461
5.46G
#define scan_sign(sign, ptr)\
462
5.46G
  switch ( *ptr ) {\
463
3.41M
    case '-': sign = -1; ptr++; break;\
464
4.95k
    case '+': sign = 1; ptr++; break;\
465
1.61G
    default: sign = 0;\
466
1.61G
  }
467
5.46G
#define refill2_back(styp,nback)\
468
5.46G
  BEGIN sptr -= nback; sstate.s_scan_type = styp; goto pause; END
469
5.46G
#define ensure2_back(styp,nback)\
470
5.46G
  if ( sptr >= endptr ) refill2_back(styp,nback)
471
5.46G
#define ensure2(styp) ensure2_back(styp, 1)
472
5.46G
#define refill2(styp) refill2_back(styp, 1)
473
5.46G
    byte s1[2];
474
5.46G
    const byte *const decoder = scan_char_decoder;
475
5.46G
    int status;
476
5.46G
    int sign;
477
5.46G
    const bool check_only = (pstate->s_options & SCAN_CHECK_ONLY) != 0;
478
5.46G
    const bool PDFScanRules = (i_ctx_p->scanner_options & SCAN_PDF_RULES) != 0;
479
    /*
480
     * The following is a hack so that ^D will be self-delimiting in PS files
481
     * (to compensate for bugs in some PostScript-generating applications)
482
     * but not in strings (to match CPSI on the CET) or PDF.
483
     */
484
5.46G
    const int ctrld = (pstate->s_options & SCAN_FROM_STRING ||
485
5.46G
                      PDFScanRules ? 0x04 : 0xffff);
486
5.46G
    scanner_state sstate;
487
488
5.46G
    sptr = endptr = NULL; /* Quiet compiler */
489
5.46G
    if (pstate->s_pstack != 0) {
490
306k
        if_not_spush1()
491
0
            return retcode;
492
306k
        myref = osp;
493
306k
    }
494
    /* Check whether we are resuming after an interruption. */
495
5.46G
    if (pstate->s_scan_type != scanning_none) {
496
752k
        sstate = *pstate;
497
752k
        if (!sstate.s_da.is_dynamic && sstate.s_da.base != sstate.s_da.buf) {
498
            /* The sstate.s_da contains some self-referencing pointers. */
499
            /* Fix them up now. */
500
238
            uint next = sstate.s_da.next - sstate.s_da.base;
501
238
            uint limit = sstate.s_da.limit - sstate.s_da.base;
502
503
238
            sstate.s_da.base = sstate.s_da.buf;
504
238
            sstate.s_da.next = sstate.s_da.buf + next;
505
238
            sstate.s_da.limit = sstate.s_da.buf + limit;
506
238
        }
507
752k
        daptr = sstate.s_da.next;
508
752k
        switch (sstate.s_scan_type) {
509
24.0k
            case scanning_binary:
510
24.0k
                retcode = (*sstate.s_ss.binary.cont)
511
24.0k
                    (i_ctx_p, myref, &sstate);
512
24.0k
                s_begin_inline(s, sptr, endptr);
513
24.0k
                if (retcode == scan_Refill)
514
23.8k
                    goto pause;
515
220
                goto sret;
516
220
            case scanning_comment:
517
0
                s_begin_inline(s, sptr, endptr);
518
0
                goto cont_comment;
519
728k
            case scanning_name:
520
728k
                goto cont_name;
521
4
            case scanning_string:
522
4
                goto cont_string;
523
0
            default:
524
0
                return_error(gs_error_Fatal);
525
752k
        }
526
752k
    }
527
5.46G
    else {
528
        /* We *may* use these in the event of returning to this function after
529
         * a interruption, but not every code path below sets them. Set them
530
         * to sane values here for safety. We can write the contents of sstate
531
         * (back) to pstate before returning.
532
         */
533
5.46G
        sstate.s_da.base = sstate.s_da.next = &(sstate.s_da.buf[0]);
534
5.46G
        sstate.s_da.limit = sstate.s_da.next;
535
5.46G
        sstate.s_da.is_dynamic = false;
536
5.46G
    }
537
    /* Fetch any state variables that are relevant even if */
538
    /* sstate.s_scan_type == scanning_none. */
539
5.46G
    sstate.s_pstack = pstate->s_pstack;
540
5.46G
    sstate.s_pdepth = pstate->s_pdepth;
541
5.46G
    ref_assign(&sstate.s_file, &pstate->s_file);
542
5.46G
    sstate.s_options = pstate->s_options;
543
5.46G
    SCAN_INIT_ERROR(&sstate);
544
5.46G
    s_begin_inline(s, sptr, endptr);
545
    /*
546
     * Loop invariants:
547
     *      If sstate.s_pstack != 0, myref = osp, and *osp is a valid slot.
548
     */
549
11.0G
  top:c = sgetc_inline(s, sptr, endptr);
550
11.0G
    if_debug1m('S', imemory, (c >= 32 && c <= 126 ? "`%c'" : c >= 0 ? "`\\%03o'" : "`%d'"), c);
551
11.0G
    switch (c) {
552
35.0M
        case ' ':
553
35.2M
        case '\f':
554
35.6M
        case '\t':
555
35.7M
        case char_CR:
556
38.3M
        case char_EOL:
557
44.3M
        case char_NULL:
558
44.3M
            goto top;
559
734k
        case 0x04:              /* see ctrld above */
560
734k
            if (c == ctrld)     /* treat as ordinary name char */
561
3
                goto begin_name;
562
            /* fall through */
563
610M
        case '[':
564
1.20G
        case ']':
565
1.20G
            s1[0] = (byte) c;
566
1.20G
            retcode = name_ref(imemory, s1, 1, myref, 1);       /* can't fail */
567
1.20G
            r_set_attrs(myref, a_executable);
568
1.20G
            break;
569
11.6M
        case '<':
570
11.6M
            if (level2_enabled) {
571
10.6M
                ensure2(scanning_none);
572
10.6M
                c = sgetc_inline(s, sptr, endptr);
573
10.6M
                switch (c) {
574
6.87M
                    case '<':
575
6.87M
                        sputback_inline(s, sptr, endptr);
576
6.87M
                        sstate.s_ss.s_name.s_name_type = 0;
577
6.87M
                        sstate.s_ss.s_name.s_try_number = false;
578
6.87M
                        goto try_funny_name;
579
695
                    case '~':
580
695
                        s_A85D_init_inline(&sstate.s_ss.a85d);
581
695
                        sstate.s_ss.st.templat = &s_A85D_template;
582
695
                        sstate.s_ss.a85d.require_eod = true;
583
                        /* If this is an inline ASCII string, interpret it normally, throw an error
584
                         * if it fails rather than ignoring it as PDF (Acrobat) does.
585
                         */
586
695
                        sstate.s_ss.a85d.pdf_rules = false;
587
695
                        goto str;
588
10.6M
                }
589
3.76M
                sputback_inline(s, sptr, endptr);
590
3.76M
            }
591
4.79M
            (void)s_AXD_init_inline(&sstate.s_ss.axd);
592
4.79M
            sstate.s_ss.st.templat = &s_AXD_template;
593
157M
          str:s_end_inline(s, sptr, endptr);
594
157M
            dynamic_init(&sstate.s_da, imemory);
595
157M
          cont_string:for (;;) {
596
157M
                stream_cursor_write w;
597
598
157M
                w.ptr = sstate.s_da.next - 1;
599
157M
                w.limit = sstate.s_da.limit - 1;
600
157M
                status = (*sstate.s_ss.st.templat->process)
601
157M
                    (&sstate.s_ss.st, &s->cursor.r, &w,
602
157M
                     s->end_status == EOFC);
603
157M
                if (!check_only)
604
157M
                    sstate.s_da.next = w.ptr + 1;
605
157M
                switch (status) {
606
613k
                    case 0:
607
613k
                        status = s->end_status;
608
613k
                        if (status < 0) {
609
2.75k
                            if (status == EOFC) {
610
2.75k
                                if (check_only) {
611
0
                                    retcode = scan_Refill;
612
0
                                    sstate.s_scan_type = scanning_string;
613
0
                                    goto suspend;
614
0
                                } else
615
2.75k
                                    sreturn(gs_error_syntaxerror);
616
0
                            }
617
4
                            break;
618
2.75k
                        }
619
610k
                        s_process_read_buf(s);
620
610k
                        continue;
621
8.49k
                    case 1:
622
8.49k
                        if (!check_only) {
623
8.49k
                            retcode = dynamic_grow(&sstate.s_da, sstate.s_da.next, max_string_size);
624
8.49k
                            if (retcode == gs_error_VMerror) {
625
0
                                sstate.s_scan_type = scanning_string;
626
0
                                goto suspend;
627
8.49k
                            } else if (retcode < 0)
628
8.49k
                                sreturn(retcode);
629
8.49k
                        }
630
8.49k
                        continue;
631
157M
                }
632
157M
                break;
633
157M
            }
634
157M
            s_begin_inline(s, sptr, endptr);
635
157M
            switch (status) {
636
1.57k
                default:
637
                    /*case ERRC: */
638
1.57k
                    sreturn(gs_error_syntaxerror);
639
0
                case INTC:
640
4
                case CALLC:
641
4
                    sstate.s_scan_type = scanning_string;
642
4
                    goto pause;
643
157M
                case EOFC:
644
157M
                    ;
645
157M
            }
646
157M
            retcode = dynamic_make_string(i_ctx_p, myref, &sstate.s_da, sstate.s_da.next);
647
157M
            if (retcode < 0) {  /* VMerror */
648
0
                sputback(s);    /* rescan ) */
649
0
                sstate.s_scan_type = scanning_string;
650
0
                goto suspend;
651
0
            }
652
157M
            break;
653
157M
        case '(':
654
152M
            sstate.s_ss.pssd.from_string =
655
152M
                ((pstate->s_options & SCAN_FROM_STRING) != 0) &&
656
3
                !level2_enabled;
657
152M
            s_PSSD_partially_init_inline(&sstate.s_ss.pssd);
658
152M
            sstate.s_ss.st.templat = &s_PSSD_template;
659
152M
            goto str;
660
682M
        case '{':
661
682M
            if (sstate.s_pstack == 0) {  /* outermost procedure */
662
191M
                if_not_spush1() {
663
0
                    sputback_inline(s, sptr, endptr);
664
0
                    sstate.s_scan_type = scanning_none;
665
0
                    goto pause_ret;
666
0
                }
667
191M
                sstate.s_pdepth = ref_stack_count_inline(&o_stack);
668
191M
            }
669
682M
            make_int(osp, sstate.s_pstack);
670
682M
            sstate.s_pstack = ref_stack_count_inline(&o_stack);
671
682M
            if_debug3m('S', imemory, "[S{]d=%d, s=%d->%d\n",
672
682M
                       sstate.s_pdepth, (int)osp->value.intval, sstate.s_pstack);
673
682M
            goto snext;
674
6.78M
        case '>':
675
6.78M
            if (level2_enabled) {
676
6.78M
                ensure2(scanning_none);
677
6.78M
                sstate.s_ss.s_name.s_name_type = 0;
678
6.78M
                sstate.s_ss.s_name.s_try_number = false;
679
6.78M
                goto try_funny_name;
680
6.78M
            }
681
            /* falls through */
682
166
        case ')':
683
166
            sreturn(gs_error_syntaxerror);
684
678M
        case '}':
685
678M
            if (sstate.s_pstack == 0)
686
678M
                sreturn(gs_error_syntaxerror);
687
678M
            osp--;
688
678M
            {
689
678M
                uint size = ref_stack_count_inline(&o_stack) - sstate.s_pstack;
690
678M
                ref arr;
691
692
678M
                if_debug4m('S', imemory, "[S}]d=%"PRIu32", s=%"PRIu32"->%"PRIpsint", c=%"PRIu32"\n",
693
678M
                           sstate.s_pdepth, sstate.s_pstack,
694
678M
                           (sstate.s_pstack == sstate.s_pdepth ? 0 :
695
678M
                           ref_stack_index(&o_stack, size)->value.intval),
696
678M
                           size + sstate.s_pstack);
697
678M
                if (size > max_array_size)
698
678M
                    sreturn(gs_error_limitcheck);
699
678M
                myref = (sstate.s_pstack == sstate.s_pdepth ? pref : &arr);
700
678M
                if (check_only) {
701
0
                    make_empty_array(myref, 0);
702
0
                    ref_stack_pop(&o_stack, size);
703
678M
                } else if (ref_array_packing.value.boolval) {
704
660M
                    retcode = make_packed_array(myref, &o_stack, size,
705
660M
                                                idmemory, "scanner(packed)");
706
660M
                    if (retcode < 0) {  /* must be VMerror */
707
0
                        osp++;
708
0
                        sputback_inline(s, sptr, endptr);
709
0
                        sstate.s_scan_type = scanning_none;
710
0
                        goto pause_ret;
711
0
                    }
712
660M
                    r_set_attrs(myref, a_executable);
713
660M
                } else {
714
18.4M
                    retcode = ialloc_ref_array(myref,
715
18.4M
                                               a_executable + a_all, size,
716
18.4M
                                               "scanner(proc)");
717
18.4M
                    if (retcode < 0) {  /* must be VMerror */
718
0
                        osp++;
719
0
                        sputback_inline(s, sptr, endptr);
720
0
                        sstate.s_scan_type = scanning_none;
721
0
                        goto pause_ret;
722
0
                    }
723
18.4M
                    retcode = ref_stack_store(&o_stack, myref, size, 0, 1,
724
18.4M
                                              false, idmemory, "scanner");
725
18.4M
                    if (retcode < 0) {
726
0
                        ifree_ref_array(myref, "scanner(proc)");
727
0
                        sreturn(retcode);
728
0
                    }
729
18.4M
                    ref_stack_pop(&o_stack, size);
730
18.4M
                }
731
678M
                if (sstate.s_pstack == sstate.s_pdepth) {         /* This was the top-level procedure. */
732
191M
                    spop1();
733
191M
                    sstate.s_pstack = 0;
734
487M
                } else {
735
487M
                    if (osp < osbot)
736
0
                        ref_stack_pop_block(&o_stack);
737
487M
                    sstate.s_pstack = osp->value.intval;
738
487M
                    *osp = arr;
739
487M
                    goto snext;
740
487M
                }
741
678M
            }
742
191M
            break;
743
2.63G
        case '/':
744
            /*
745
             * If the last thing in the input is a '/', don't try to read
746
             * any more data.
747
             */
748
2.63G
            if (sptr >= endptr && s->end_status != EOFC) {
749
376k
                refill2(scanning_none);
750
376k
            }
751
2.63G
            c = sgetc_inline(s, sptr, endptr);
752
2.63G
            if (!PDFScanRules && (c == '/')) {
753
247M
                sstate.s_ss.s_name.s_name_type = 2;
754
247M
                c = sgetc_inline(s, sptr, endptr);
755
247M
            } else
756
2.38G
                sstate.s_ss.s_name.s_name_type = 1;
757
2.63G
            sstate.s_ss.s_name.s_try_number = false;
758
2.63G
            switch (decoder[c]) {
759
288M
                case ctype_name:
760
2.62G
                default:
761
2.62G
                    goto do_name;
762
2.62G
                case ctype_btoken:
763
1.97k
                    if (!(ref_binary_object_format.value.intval != 0 && level2_enabled))
764
0
                        goto do_name;
765
                    /* otherwise, an empty name */
766
2.27k
                case ctype_exception:
767
163k
                case ctype_space:
768
                    /*
769
                     * Amazingly enough, the Adobe implementations don't accept
770
                     * / or // followed by [, ], <<, or >>, so we do the same.
771
                     * (Older versions of our code had a ctype_other case here
772
                     * that handled these specially.)
773
                     */
774
910k
                case ctype_other:
775
910k
                    if (c == ctrld) /* see above */
776
0
                        goto do_name;
777
910k
                    sstate.s_da.base = sstate.s_da.limit = daptr = 0;
778
910k
                    sstate.s_da.is_dynamic = false;
779
910k
                    goto nx;
780
2.63G
            }
781
3.18M
        case '%':
782
3.18M
            {                   /* Scan as much as possible within the buffer. */
783
3.18M
                const byte *base = sptr;
784
3.18M
                const byte *end;
785
786
42.3M
                while (++sptr < endptr)         /* stop 1 char early */
787
42.3M
                    switch (*sptr) {
788
89.5k
                        case char_CR:
789
89.5k
                            end = sptr;
790
89.5k
                            if (sptr[1] == char_EOL)
791
17.5k
                                sptr++;
792
3.15M
                          cend: /* Check for externally processed comments. */
793
3.15M
                            retcode = scan_comment(i_ctx_p, myref, &sstate,
794
3.15M
                                                   base, end, false);
795
3.15M
                            if (retcode != 0)
796
1.13M
                                goto comment;
797
2.02M
                            goto top;
798
3.00M
                        case char_EOL:
799
3.06M
                        case '\f':
800
3.06M
                            end = sptr;
801
3.06M
                            goto cend;
802
42.3M
                    }
803
                /*
804
                 * We got to the end of the buffer while inside a comment.
805
                 * If there is a possibility that we must pass the comment
806
                 * to an external procedure, move what we have collected
807
                 * so far into a private buffer now.
808
                 */
809
24.5k
                --sptr;
810
24.5k
                sstate.s_da.buf[1] = 0;
811
24.5k
                {
812
                    /* Could be an externally processable comment. */
813
24.5k
                    uint len = sptr + 1 - base;
814
24.5k
                    if (len > sizeof(sstate.s_da.buf))
815
8
                        len = sizeof(sstate.s_da.buf);
816
817
24.5k
                    memcpy(sstate.s_da.buf, base, len);
818
24.5k
                    daptr = sstate.s_da.buf + len;
819
24.5k
                }
820
24.5k
                sstate.s_da.base = sstate.s_da.buf;
821
24.5k
                sstate.s_da.is_dynamic = false;
822
24.5k
            }
823
            /* Enter here to continue scanning a comment. */
824
            /* daptr must be set. */
825
4.41M
          cont_comment:for (;;) {
826
4.41M
                switch ((c = sgetc_inline(s, sptr, endptr))) {
827
4.39M
                    default:
828
4.39M
                        if (c < 0)
829
4.47k
                            switch (c) {
830
0
                                case INTC:
831
0
                                case CALLC:
832
0
                                    sstate.s_da.next = daptr;
833
0
                                    sstate.s_scan_type = scanning_comment;
834
0
                                    goto pause;
835
4.47k
                                case EOFC:
836
                                    /*
837
                                     * One would think that an EOF in a comment
838
                                     * should be a syntax error, but there are
839
                                     * quite a number of files that end that way.
840
                                     */
841
4.47k
                                    goto end_comment;
842
0
                                default:
843
0
                                    sreturn(gs_error_syntaxerror);
844
4.47k
                            }
845
4.39M
                        if (daptr < sstate.s_da.buf + max_comment_line)
846
453k
                            *daptr++ = c;
847
4.39M
                        continue;
848
1.77k
                    case char_CR:
849
18.2k
                    case char_EOL:
850
20.1k
                    case '\f':
851
24.5k
                      end_comment:
852
24.5k
                        retcode = scan_comment(i_ctx_p, myref, &sstate,
853
24.5k
                                               sstate.s_da.buf, daptr, true);
854
24.5k
                        if (retcode != 0)
855
13.3k
                            goto comment;
856
11.2k
                        goto top;
857
4.41M
                }
858
4.41M
            }
859
            /*NOTREACHED */
860
1.50M
        case EOFC:
861
1.50M
            if (sstate.s_pstack != 0) {
862
6.05k
                if (check_only)
863
0
                    goto pause;
864
6.05k
                sreturn(gs_error_syntaxerror);
865
0
            }
866
1.50M
            retcode = scan_EOF;
867
1.50M
            break;
868
4
        case ERRC:
869
4
            sreturn(gs_error_ioerror);
870
871
            /* Check for a Level 2 funny name (<< or >>). */
872
            /* c is '<' or '>'.  We already did an ensure2. */
873
13.6M
          try_funny_name:
874
13.6M
            {
875
13.6M
                int c1 = sgetc_inline(s, sptr, endptr);
876
877
13.6M
                if (c1 == c) {
878
13.6M
                    s1[0] = s1[1] = c;
879
13.6M
                    name_ref(imemory, s1, 2, myref, 1); /* can't fail */
880
13.6M
                    goto have_name;
881
13.6M
                }
882
52
                sputback_inline(s, sptr, endptr);
883
52
            }
884
52
            sreturn(gs_error_syntaxerror);
885
886
            /* Handle separately the names that might be a number. */
887
126M
        case '0':
888
1.54G
        case '1':
889
1.68G
        case '2':
890
1.78G
        case '3':
891
1.84G
        case '4':
892
1.86G
        case '5':
893
1.88G
        case '6':
894
1.89G
        case '7':
895
1.90G
        case '8':
896
1.91G
        case '9':
897
2.29G
        case '.':
898
2.29G
            sign = 0;
899
2.33G
    nr:     /*
900
             * Skip a leading sign, if any, by conditionally passing
901
             * sptr + 1 rather than sptr.  Also, if the last character
902
             * in the buffer is a CR, we must stop the scan 1 character
903
             * early, to be sure that we can test for CR+LF within the
904
             * buffer, by passing endptr rather than endptr + 1.
905
             */
906
2.33G
            retcode = scan_number(sptr + (sign & 1),
907
2.33G
                    endptr /*(*endptr == char_CR ? endptr : endptr + 1) */ ,
908
2.33G
                                  sign, myref, &newptr, i_ctx_p->scanner_options);
909
2.33G
            if (retcode == 1 && decoder[newptr[-1]] == ctype_space) {
910
720M
                sptr = newptr - 1;
911
720M
                if (*sptr == char_CR && sptr[1] == char_EOL)
912
1.32k
                    sptr++;
913
720M
                retcode = 0;
914
720M
                ref_mark_new(myref);
915
720M
                break;
916
720M
            }
917
1.61G
            sstate.s_ss.s_name.s_name_type = 0;
918
1.61G
            sstate.s_ss.s_name.s_try_number = true;
919
1.61G
            goto do_name;
920
5.05k
        case '+':
921
5.05k
            sign = 1;
922
5.05k
            goto nr;
923
46.7M
        case '-':
924
46.7M
            sign = -1;
925
46.7M
            if(i_ctx_p->scanner_options & SCAN_PDF_INV_NUM) {
926
0
                const byte *osptr = sptr;
927
0
                do {
928
                    /* This is slightly unpleasant: we have to bounds check the buffer,
929
                       rather than just incrementing the point until we find a non '-' character.
930
                       But we cannot differentiate between multiple '-' characters that
931
                       straddle a buffer boundary, or a token that is only one or more '-' characters.
932
                       Handling this relies on the fact that the Postscript-based PDF interpreter
933
                       always uses the "token" operator to tokenize a stream, thus we can assume
934
                       here that the current buffer contains the entire token. So if we reach
935
                       the end of the buffer without hitting a character taht is not a '-', we'll reset
936
                       the buffer pointer, and retry, treating it as a name object.
937
                     */
938
0
                    if (sptr + 1 > endptr) {
939
0
                        sptr = osptr;
940
0
                        sstate.s_ss.s_name.s_name_type = 0;
941
0
                        sstate.s_ss.s_name.s_try_number = true;
942
0
                        goto do_name;
943
0
                    }
944
0
                    if (*(sptr + 1) == '-') {
945
0
                        sptr++;
946
0
                    } else
947
0
                        break;
948
0
                } while (1);
949
0
            }
950
46.7M
            goto nr;
951
952
            /* Check for a binary object */
953
46.7M
          case 128: case 129: case 130: case 131: case 132: case 133: case 134: case 135:
954
843k
          case 136: case 137: case 138: case 139: case 140: case 141: case 142: case 143:
955
1.48M
          case 144: case 145: case 146: case 147: case 148: case 149: case 150: case 151:
956
1.48M
          case 152: case 153: case 154: case 155: case 156: case 157: case 158: case 159:
957
1.48M
            if ((ref_binary_object_format.value.intval != 0 && level2_enabled)) {
958
1.48M
                s_end_inline(s, sptr, endptr);
959
1.48M
                retcode = scan_binary_token(i_ctx_p, myref, &sstate);
960
1.48M
                s_begin_inline(s, sptr, endptr);
961
1.48M
                if (retcode == scan_Refill)
962
5.92k
                    goto pause;
963
1.47M
                break;
964
1.48M
            }
965
            /* Not a binary object, fall through. */
966
967
            /* The default is a name. */
968
2.39M
        default:
969
2.39M
            if (c < 0) {
970
876k
                dynamic_init(&sstate.s_da, name_memory(imemory));        /* sstate.s_da state must be clean */
971
876k
                sstate.s_scan_type = scanning_none;
972
876k
                goto pause;
973
876k
            }
974
            /* Populate the switch with enough cases to force */
975
            /* simple compilers to use a dispatch rather than tests. */
976
1.53M
        case '!':
977
2.21M
        case '"':
978
5.33M
        case '#':
979
20.6M
        case '$':
980
20.9M
        case '&':
981
22.3M
        case '\'':
982
22.6M
        case '*':
983
24.1M
        case ',':
984
62.1M
        case '=':
985
62.1M
        case ':':
986
91.3M
        case ';':
987
91.4M
        case '?':
988
91.4M
        case '@':
989
92.5M
        case 'A':
990
93.0M
        case 'B':
991
100M
        case 'C':
992
105M
        case 'D':
993
111M
        case 'E':
994
117M
        case 'F':
995
118M
        case 'G':
996
121M
        case 'H':
997
125M
        case 'I':
998
125M
        case 'J':
999
126M
        case 'K':
1000
129M
        case 'L':
1001
129M
        case 'M':
1002
141M
        case 'N':
1003
143M
        case 'O':
1004
152M
        case 'P':
1005
156M
        case 'Q':
1006
172M
        case 'R':
1007
183M
        case 'S':
1008
192M
        case 'T':
1009
194M
        case 'U':
1010
198M
        case 'V':
1011
199M
        case 'W':
1012
200M
        case 'X':
1013
200M
        case 'Y':
1014
200M
        case 'Z':
1015
200M
        case '\\':
1016
200M
        case '^':
1017
200M
        case '_':
1018
200M
        case '`':
1019
295M
        case 'a':
1020
343M
        case 'b':
1021
539M
        case 'c':
1022
1.01G
        case 'd':
1023
1.44G
        case 'e':
1024
1.54G
        case 'f':
1025
1.73G
        case 'g':
1026
1.74G
        case 'h':
1027
2.19G
        case 'i':
1028
2.19G
        case 'j':
1029
2.23G
        case 'k':
1030
2.32G
        case 'l':
1031
2.37G
        case 'm':
1032
2.46G
        case 'n':
1033
2.50G
        case 'o':
1034
2.90G
        case 'p':
1035
2.90G
        case 'q':
1036
3.04G
        case 'r':
1037
3.23G
        case 's':
1038
3.28G
        case 't':
1039
3.29G
        case 'u':
1040
3.30G
        case 'v':
1041
3.33G
        case 'w':
1042
3.34G
        case 'x':
1043
3.34G
        case 'y':
1044
3.34G
        case 'z':
1045
3.34G
        case '|':
1046
3.34G
        case '~':
1047
3.34G
          begin_name:
1048
            /* Common code for scanning a name. */
1049
            /* sstate.s_ss.s_name.s_try_number and sstate.s_ss.s_name.s_name_type are already set. */
1050
            /* We know c has ctype_name (or maybe ctype_btoken, */
1051
            /* or is ^D) or is a digit. */
1052
3.34G
            sstate.s_ss.s_name.s_name_type = 0;
1053
3.34G
            sstate.s_ss.s_name.s_try_number = false;
1054
7.59G
          do_name:
1055
            /* Try to scan entirely within the stream buffer. */
1056
            /* We stop 1 character early, so we don't switch buffers */
1057
            /* looking ahead if the name is terminated by \r\n. */
1058
7.59G
            sstate.s_da.base = (byte *) sptr;
1059
7.59G
            sstate.s_da.is_dynamic = false;
1060
7.59G
            {
1061
7.59G
                const byte *endp1 = endptr - 1;
1062
1063
57.1G
                do {
1064
57.1G
                    if (sptr >= endp1)  /* stop 1 early! */
1065
9.14M
                        goto dyn_name;
1066
57.1G
                }
1067
57.1G
                while (decoder[*++sptr] <= max_name_ctype || *sptr == ctrld);   /* digit or name */
1068
7.59G
            }
1069
            /* Name ended within the buffer. */
1070
7.58G
            daptr = (byte *) sptr;
1071
7.58G
            c = *sptr;
1072
7.58G
            goto nx;
1073
9.14M
          dyn_name:             /* Name extended past end of buffer. */
1074
9.14M
            s_end_inline(s, sptr, endptr);
1075
            /* Initialize the dynamic area. */
1076
            /* We have to do this before the next */
1077
            /* sgetc, which will overwrite the buffer. */
1078
9.14M
            sstate.s_da.limit = (byte *)++ sptr;
1079
9.14M
            sstate.s_da.memory = name_memory(imemory);
1080
9.14M
            retcode = dynamic_grow(&sstate.s_da, sstate.s_da.limit, name_max_string);
1081
9.14M
            if (retcode < 0) {
1082
20
                dynamic_save(&sstate.s_da);
1083
20
                if (retcode != gs_error_VMerror)
1084
20
                    sreturn(retcode);
1085
0
                sstate.s_scan_type = scanning_name;
1086
0
                goto pause_ret;
1087
20
            }
1088
9.14M
            daptr = sstate.s_da.next;
1089
            /* Enter here to continue scanning a name. */
1090
            /* daptr must be set. */
1091
9.87M
          cont_name:s_begin_inline(s, sptr, endptr);
1092
39.0M
            while (decoder[c = sgetc_inline(s, sptr, endptr)] <= max_name_ctype || c == ctrld) {
1093
29.1M
                if (daptr == sstate.s_da.limit) {
1094
164k
                    retcode = dynamic_grow(&sstate.s_da, daptr,
1095
164k
                                           name_max_string);
1096
164k
                    if (retcode < 0) {
1097
47
                        dynamic_save(&sstate.s_da);
1098
47
                        if (retcode != gs_error_VMerror)
1099
47
                            sreturn(retcode);
1100
0
                        sputback_inline(s, sptr, endptr);
1101
0
                        sstate.s_scan_type = scanning_name;
1102
0
                        goto pause_ret;
1103
47
                    }
1104
164k
                    daptr = sstate.s_da.next;
1105
164k
                }
1106
29.1M
                *daptr++ = c;
1107
29.1M
            }
1108
7.59G
          nx:switch (decoder[c]) {
1109
4.29G
                case ctype_other:
1110
4.29G
                    if (c == ctrld) /* see above */
1111
0
                        break;
1112
4.29G
                case ctype_btoken:
1113
4.29G
                    sputback_inline(s, sptr, endptr);
1114
4.29G
                    break;
1115
3.28G
                case ctype_space:
1116
                    /* Check for \r\n */
1117
3.28G
                    if (c == char_CR) {
1118
2.00M
                        if (sptr >= endptr) {   /* ensure2 *//* We have to check specially for */
1119
                            /* the case where the very last */
1120
                            /* character of a file is a CR. */
1121
2.93k
                            if (s->end_status != EOFC) {
1122
2.75k
                                sptr--;
1123
2.75k
                                goto pause_name;
1124
2.75k
                            }
1125
1.99M
                        } else if (sptr[1] == char_EOL)
1126
17.2k
                            sptr++;
1127
2.00M
                    }
1128
3.28G
                    break;
1129
3.28G
                case ctype_exception:
1130
4.70M
                    switch (c) {
1131
0
                        case INTC:
1132
725k
                        case CALLC:
1133
725k
                            goto pause_name;
1134
4
                        case ERRC:
1135
4
                            sreturn(gs_error_ioerror);
1136
3.98M
                        case EOFC:
1137
3.98M
                            break;
1138
4.70M
                    }
1139
7.59G
            }
1140
            /* Check for a number */
1141
7.59G
            if (sstate.s_ss.s_name.s_try_number) {
1142
1.61G
                const byte *base = sstate.s_da.base;
1143
1144
1.61G
                scan_sign(sign, base);
1145
1.61G
                retcode = scan_number(base, daptr, sign, myref, &newptr, i_ctx_p->scanner_options);
1146
1.61G
                if (retcode == 1) {
1147
2.43M
                    ref_mark_new(myref);
1148
2.43M
                    retcode = 0;
1149
1.61G
                } else if (retcode != gs_error_syntaxerror) {
1150
1.24G
                    dynamic_free(&sstate.s_da);
1151
1.24G
                    if (sstate.s_ss.s_name.s_name_type == 2)
1152
1.24G
                        sreturn(gs_error_syntaxerror);
1153
1.24G
                    break;      /* might be gs_error_limitcheck */
1154
1.24G
                }
1155
1.61G
            }
1156
6.34G
            if (sstate.s_da.is_dynamic) {        /* We've already allocated the string on the heap. */
1157
6.84M
                uint size = daptr - sstate.s_da.base;
1158
1159
6.84M
                retcode = name_ref(imemory, sstate.s_da.base, size, myref, -1);
1160
6.84M
                if (retcode >= 0) {
1161
5.99M
                    dynamic_free(&sstate.s_da);
1162
5.99M
                } else {
1163
845k
                    retcode = dynamic_resize(&sstate.s_da, size);
1164
845k
                    if (retcode < 0) {  /* VMerror */
1165
0
                        if (c != EOFC)
1166
0
                            sputback_inline(s, sptr, endptr);
1167
0
                        sstate.s_scan_type = scanning_name;
1168
0
                        goto pause_ret;
1169
0
                    }
1170
845k
                    retcode = name_ref(imemory, sstate.s_da.base, size, myref, 2);
1171
845k
                }
1172
6.34G
            } else {
1173
6.34G
                retcode = name_ref(imemory, sstate.s_da.base, (uint) (daptr - sstate.s_da.base),
1174
6.34G
                                   myref, !s->foreign);
1175
6.34G
            }
1176
            /* Done scanning.  Check for preceding /'s. */
1177
6.34G
            if (retcode < 0) {
1178
0
                if (retcode != gs_error_VMerror)
1179
0
                    sreturn(retcode);
1180
0
                if (!sstate.s_da.is_dynamic) {
1181
0
                    sstate.s_da.next = daptr;
1182
0
                    dynamic_save(&sstate.s_da);
1183
0
                }
1184
0
                if (c != EOFC)
1185
0
                    sputback_inline(s, sptr, endptr);
1186
0
                sstate.s_scan_type = scanning_name;
1187
0
                goto pause_ret;
1188
0
            }
1189
6.36G
          have_name:switch (sstate.s_ss.s_name.s_name_type) {
1190
3.73G
                case 0: /* ordinary executable name */
1191
3.73G
                    if (r_has_type(myref, t_name))      /* i.e., not a number */
1192
3.73G
                        r_set_attrs(myref, a_executable);
1193
6.11G
                case 1: /* quoted name */
1194
6.11G
                    break;
1195
247M
                case 2: /* immediate lookup */
1196
247M
                    {
1197
247M
                        ref *pvalue;
1198
1199
247M
                        if (!r_has_type(myref, t_name) ||
1200
247M
                            (pvalue = dict_find_name(myref)) == 0) {
1201
71
                            ref_assign(&sstate.s_error.object, myref);
1202
71
                            r_set_attrs(&sstate.s_error.object,
1203
71
                                a_executable); /* Adobe compatibility */
1204
71
                            sreturn(gs_error_undefined);
1205
0
                        }
1206
247M
                        if (sstate.s_pstack != 0 &&
1207
215M
                            r_space(pvalue) > ialloc_space(idmemory)
1208
247M
                            )
1209
247M
                            sreturn(gs_error_invalidaccess);
1210
247M
                        ref_assign_new(myref, pvalue);
1211
247M
                    }
1212
6.36G
            }
1213
11.0G
    }
1214
9.88G
  sret:if (retcode < 0) {
1215
16.5k
        s_end_inline(s, sptr, endptr);
1216
16.5k
        pstate->s_error = sstate.s_error;
1217
16.5k
        if (sstate.s_pstack != 0) {
1218
7.28k
            if (retcode == gs_error_undefined)
1219
46
                *pref = *osp;   /* return undefined name as error token */
1220
7.28k
            ref_stack_pop(&o_stack,
1221
7.28k
                          ref_stack_count(&o_stack) - (sstate.s_pdepth - 1));
1222
7.28k
        }
1223
16.5k
        return retcode;
1224
16.5k
    }
1225
    /* If we are at the top level, return the object, */
1226
    /* otherwise keep going. */
1227
9.88G
    if (sstate.s_pstack == 0) {
1228
5.46G
        s_end_inline(s, sptr, endptr);
1229
5.46G
        return retcode;
1230
5.46G
    }
1231
5.58G
  snext:if_not_spush1() {
1232
11
        s_end_inline(s, sptr, endptr);
1233
11
        sstate.s_scan_type = scanning_none;
1234
11
        goto save;
1235
11
    }
1236
5.58G
    myref = osp;
1237
5.58G
    goto top;
1238
1239
    /* Pause for an interrupt or callout. */
1240
728k
  pause_name:
1241
    /* If we're still scanning within the stream buffer, */
1242
    /* move the characters to the private buffer (sstate.s_da.buf) now. */
1243
728k
    sstate.s_da.next = daptr;
1244
728k
    dynamic_save(&sstate.s_da);
1245
728k
    sstate.s_scan_type = scanning_name;
1246
2.01M
  pause:
1247
2.01M
    retcode = scan_Refill;
1248
2.01M
  pause_ret:
1249
2.01M
    s_end_inline(s, sptr, endptr);
1250
2.01M
  suspend:
1251
2.01M
    if (sstate.s_pstack != 0)
1252
158k
        osp--;                  /* myref */
1253
3.15M
  save:
1254
3.15M
    *pstate = sstate;
1255
3.15M
    return retcode;
1256
1257
    /* Handle a scanned comment. */
1258
1.14M
 comment:
1259
1.14M
    if (retcode < 0)
1260
0
        goto sret;
1261
1.14M
    s_end_inline(s, sptr, endptr);
1262
1.14M
    sstate.s_scan_type = scanning_none;
1263
1.14M
    goto save;
1264
1.14M
}