Coverage Report

Created: 2025-06-10 06:56

/src/ghostpdl/psi/iscan.c
Line
Count
Source (jump to first uncovered line)
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
1.94M
{
62
1.94M
    pda->is_dynamic = false;
63
1.94M
    pda->limit = pda->buf + sizeof(pda->buf);
64
1.94M
    pda->next = pda->base = pda->buf;
65
1.94M
    pda->memory = mem;
66
1.94M
}
67
68
/* Free a dynamic string. */
69
static void
70
dynamic_free(da_ptr pda)
71
15.1M
{
72
15.1M
    if (pda->is_dynamic)
73
77.2k
        gs_free_string(pda->memory, pda->base, da_size(pda), "scanner");
74
15.1M
}
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
2.03M
{
81
2.03M
    uint old_size = da_size(pda);
82
2.03M
    uint pos = pda->next - pda->base;
83
2.03M
    gs_memory_t *mem = pda->memory;
84
2.03M
    byte *base;
85
86
2.03M
    if (pda->is_dynamic) {
87
13.1k
        base = gs_resize_string(mem, pda->base, old_size,
88
13.1k
                                new_size, "scanner");
89
13.1k
        if (base == 0)
90
0
            return_error(gs_error_VMerror);
91
2.01M
    } else {                    /* switching from static to dynamic */
92
2.01M
        base = gs_alloc_string(mem, new_size, "scanner");
93
2.01M
        if (base == 0)
94
0
            return_error(gs_error_VMerror);
95
2.01M
        memcpy(base, pda->base, min(old_size, new_size));
96
2.01M
        pda->is_dynamic = true;
97
2.01M
    }
98
2.03M
    pda->base = base;
99
2.03M
    pda->next = base + pos;
100
2.03M
    pda->limit = base + new_size;
101
2.03M
    return 0;
102
2.03M
}
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
90.4k
{
111
90.4k
    uint old_size = da_size(pda);
112
90.4k
    uint new_size = (old_size < 10 ? 20 :
113
90.4k
                     old_size >= (max_size >> 1) ? max_size :
114
29.2k
                     old_size << 1);
115
90.4k
    int code;
116
117
90.4k
    pda->next = next;
118
90.4k
    if (old_size >= max_size)
119
2
        return_error(gs_error_limitcheck);
120
90.4k
    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
90.4k
    return code;
127
90.4k
}
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
9.00k
{
134
9.00k
    if (!pda->is_dynamic && pda->base != pda->buf) {
135
0
        int len = da_size(pda);
136
137
0
        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
0
        if (pda->base != NULL)
145
0
            memcpy(pda->buf, pda->base, len);
146
0
        pda->next = pda->buf + len;
147
0
        pda->base = pda->buf;
148
0
    }
149
9.00k
}
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
1.93M
{
155
1.93M
    uint size = (pda->next = next) - pda->base;
156
1.93M
    int code = dynamic_resize(pda, size);
157
158
1.93M
    if (code < 0)
159
0
        return code;
160
1.93M
    make_tasv_new(pref, t_string,
161
1.93M
                  a_all | imemory_space((gs_ref_memory_t *) pda->memory),
162
1.93M
                  size, bytes, pda->base);
163
1.93M
    return 0;
164
1.93M
}
165
166
/* ------ Main scanner ------ */
167
168
/* GC procedures */
169
static
170
CLEAR_MARKS_PROC(scanner_clear_marks)
171
1
{
172
1
    scanner_state *const ssptr = vptr;
173
174
1
    r_clear_attrs(&ssptr->s_file, l_mark);
175
1
    r_clear_attrs(&ssptr->s_ss.binary.bin_array, l_mark);
176
1
    r_clear_attrs(&ssptr->s_error.object, l_mark);
177
1
}
178
static
179
4
ENUM_PTRS_WITH(scanner_enum_ptrs, scanner_state *ssptr) return 0;
180
1
case 0:
181
1
    ENUM_RETURN_REF(&ssptr->s_file);
182
1
case 1:
183
1
    ENUM_RETURN_REF(&ssptr->s_error.object);
184
1
case 2:
185
1
    if (ssptr->s_scan_type == scanning_none ||
186
1
        !ssptr->s_da.is_dynamic
187
1
        )
188
1
        ENUM_RETURN(0);
189
0
    return ENUM_STRING2(ssptr->s_da.base, da_size(&ssptr->s_da));
190
1
case 3:
191
1
    if (ssptr->s_scan_type != scanning_binary)
192
1
        return 0;
193
4
    ENUM_RETURN_REF(&ssptr->s_ss.binary.bin_array);
194
4
ENUM_PTRS_END
195
1
static RELOC_PTRS_WITH(scanner_reloc_ptrs, scanner_state *ssptr)
196
1
{
197
1
    RELOC_REF_VAR(ssptr->s_file);
198
1
    r_clear_attrs(&ssptr->s_file, l_mark);
199
1
    if (ssptr->s_scan_type != scanning_none && ssptr->s_da.is_dynamic) {
200
0
        gs_string sda;
201
202
0
        sda.data = ssptr->s_da.base;
203
0
        sda.size = da_size(&ssptr->s_da);
204
0
        RELOC_STRING_VAR(sda);
205
0
        ssptr->s_da.limit = sda.data + sda.size;
206
0
        ssptr->s_da.next = sda.data + (ssptr->s_da.next - ssptr->s_da.base);
207
0
        ssptr->s_da.base = sda.data;
208
0
    }
209
1
    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
1
    RELOC_REF_VAR(ssptr->s_error.object);
214
1
    r_clear_attrs(&ssptr->s_error.object, l_mark);
215
1
}
216
1
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
67.8M
{
224
67.8M
    ref_assign(&sstate->s_file, fop);
225
67.8M
    sstate->s_scan_type = scanning_none;
226
67.8M
    sstate->s_pstack = 0;
227
67.8M
    sstate->s_options = options;
228
67.8M
    SCAN_INIT_ERROR(sstate);
229
67.8M
}
230
void gs_scanner_init_stream_options(scanner_state *sstate, stream *s,
231
                                 int options)
232
11.5k
{
233
    /*
234
     * The file 'object' will never be accessed, but it must be in correct
235
     * form for the GC.
236
     */
237
11.5k
    ref fobj;
238
239
11.5k
    make_file(&fobj, a_read, 0, s);
240
11.5k
    gs_scanner_init_options(sstate, &fobj, options);
241
11.5k
}
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
57
{
251
57
    if (!r_has_type(&pstate->s_error.object, t__invalid)) {
252
2
        ref_assign(pseo, &pstate->s_error.object);
253
2
        return 0;
254
2
    }
255
55
    if (pstate->s_error.string[0]) {
256
4
        int len = strlen(pstate->s_error.string);
257
258
4
        if (pstate->s_error.is_name) {
259
0
            int code = name_ref(imemory, (const byte *)pstate->s_error.string, len, pseo, 1);
260
261
0
            if (code < 0)
262
0
                return code;
263
0
            r_set_attrs(pseo, a_executable); /* Adobe compatibility */
264
0
            return 0;
265
4
        } else {
266
4
            byte *estr = ialloc_string(len, "gs_scanner_error_object");
267
268
4
            if (estr == 0)
269
0
                return -1;              /* VMerror */
270
4
            memcpy(estr, (const byte *)pstate->s_error.string, len);
271
4
            make_string(pseo, a_all | icurrent_space, len, estr);
272
4
            return 0;
273
4
        }
274
4
    }
275
51
    return -1;                  /* no error object */
276
55
}
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
26.1k
{
285
26.1k
    const ref *const fop = &sstate->s_file;
286
26.1k
    stream *s = fptr(fop);
287
26.1k
    uint avail = sbufavailable(s);
288
26.1k
    int status;
289
290
26.1k
    if (s->end_status == EOFC) {
291
        /* More data needed, but none available, so this is a syntax error. */
292
5
        return_error(gs_error_syntaxerror);
293
5
    }
294
26.1k
    status = s_process_read_buf(s);
295
26.1k
    if (sbufavailable(s) > avail)
296
6.48k
        return 0;
297
19.6k
    if (status == 0)
298
19.6k
        status = s->end_status;
299
19.6k
    switch (status) {
300
7
        case EOFC:
301
            /* We just discovered that we're at EOF. */
302
            /* Let the caller find this out. */
303
7
            return 0;
304
0
        case ERRC:
305
0
            return_error(gs_error_ioerror);
306
0
        case INTC:
307
19.6k
        case CALLC:
308
19.6k
            {
309
19.6k
                ref rstate[1];
310
19.6k
                scanner_state *pstate;
311
312
19.6k
                if (save) {
313
17.8k
                    pstate = (scanner_state *)
314
17.8k
                        ialloc_struct(scanner_state_dynamic, &st_scanner_state_dynamic,
315
17.8k
                                      "gs_scan_handle_refill");
316
17.8k
                    if (pstate == 0)
317
0
                        return_error(gs_error_VMerror);
318
17.8k
                    ((scanner_state_dynamic *)pstate)->mem = imemory;
319
17.8k
                    *pstate = *sstate;
320
17.8k
                } else
321
1.78k
                    pstate = sstate;
322
19.6k
                make_istruct(&rstate[0], 0, pstate);
323
19.6k
                return s_handle_read_exception(i_ctx_p, status, fop,
324
19.6k
                                               rstate, 1, cont);
325
19.6k
            }
326
19.6k
    }
327
    /* No more data available, but no exception. */
328
    /* A filter is consuming headers but returns nothing. */
329
0
    return 0;
330
19.6k
}
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
41.0k
{
340
41.0k
    uint len = (uint) (end - base);
341
41.0k
    int code;
342
#ifdef DEBUG
343
    const char *sstr = (saved ? ">" : "");
344
#endif
345
346
41.0k
    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
6.86k
        if (pstate->s_options & SCAN_PROCESS_DSC_COMMENTS) {
356
6.85k
            code = scan_DSC_Comment;
357
6.85k
            goto comment;
358
6.85k
        }
359
        /* Treat as an ordinary comment. */
360
6.86k
    }
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
34.1k
    if (pstate->s_options & SCAN_PROCESS_COMMENTS) {
371
0
        code = scan_Comment;
372
0
        goto comment;
373
0
    }
374
34.1k
    return 0;
375
6.85k
 comment:
376
6.85k
    {
377
6.85k
        byte *cstr = ialloc_string(len, "scan_comment");
378
379
6.85k
        if (cstr == 0)
380
0
            return_error(gs_error_VMerror);
381
6.85k
        memcpy(cstr, base, len);
382
6.85k
        make_string(pref, a_all | icurrent_space, len, cstr);
383
6.85k
    }
384
0
    return code;
385
6.85k
}
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
840
{
394
840
    stream st;
395
840
    stream *s = &st;
396
840
    scanner_state state;
397
840
    int code;
398
399
840
    if (!r_has_attr(pstr, a_read))
400
0
        return_error(gs_error_invalidaccess);
401
840
    s_init(s, NULL);
402
840
    sread_string(s, pstr->value.bytes, r_size(pstr));
403
840
    gs_scanner_init_stream_options(&state, s, options | SCAN_FROM_STRING);
404
840
    switch (code = gs_scan_token(i_ctx_p, pref, &state)) {
405
0
        default:                /* error or comment */
406
0
            if (code < 0)
407
0
                break;
408
            /* falls through */
409
840
        case 0:         /* read a token */
410
840
        case scan_BOS:
411
840
            {
412
840
                uint pos = stell(s);
413
414
840
                pstr->value.bytes += pos;
415
840
                r_dec_size(pstr, pos);
416
840
            }
417
840
            break;
418
0
        case scan_Refill:       /* error */
419
0
            code = gs_note_error(gs_error_syntaxerror);
420
0
        case scan_EOF:
421
0
            break;
422
840
    }
423
840
    if (code < 0)
424
0
        gs_scanner_error_object(i_ctx_p, &state, &i_ctx_p->error_object);
425
840
    return code;
426
840
}
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
67.8M
{
439
67.8M
    stream *const s = pstate->s_file.value.pfile;
440
67.8M
    ref *myref = pref;
441
67.8M
    int retcode = 0;
442
67.8M
    int c;
443
444
67.8M
    s_declare_inline(s, sptr, endptr);
445
67.8M
    const byte *newptr;
446
67.8M
    byte *daptr;
447
448
67.8M
#define sreturn(code)\
449
67.8M
  { retcode = gs_note_error(code); goto sret; }
450
67.8M
#define if_not_spush1()\
451
74.5M
  if ( osp < ostop ) osp++;\
452
74.5M
  else if ( (retcode = ref_stack_push(&o_stack, 1)) >= 0 )\
453
3.20k
    ;\
454
3.20k
  else
455
67.8M
#define spop1()\
456
67.8M
  if ( osp >= osbot ) osp--;\
457
2.35M
  else ref_stack_pop(&o_stack, 1)
458
67.8M
    int max_name_ctype =
459
67.8M
        ((ref_binary_object_format.value.intval != 0 && level2_enabled)? ctype_name : ctype_btoken);
460
461
67.8M
#define scan_sign(sign, ptr)\
462
67.8M
  switch ( *ptr ) {\
463
58.0k
    case '-': sign = -1; ptr++; break;\
464
281
    case '+': sign = 1; ptr++; break;\
465
19.9M
    default: sign = 0;\
466
19.9M
  }
467
67.8M
#define refill2_back(styp,nback)\
468
67.8M
  BEGIN sptr -= nback; sstate.s_scan_type = styp; goto pause; END
469
67.8M
#define ensure2_back(styp,nback)\
470
67.8M
  if ( sptr >= endptr ) refill2_back(styp,nback)
471
67.8M
#define ensure2(styp) ensure2_back(styp, 1)
472
67.8M
#define refill2(styp) refill2_back(styp, 1)
473
67.8M
    byte s1[2];
474
67.8M
    const byte *const decoder = scan_char_decoder;
475
67.8M
    int status;
476
67.8M
    int sign;
477
67.8M
    const bool check_only = (pstate->s_options & SCAN_CHECK_ONLY) != 0;
478
67.8M
    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
67.8M
    const int ctrld = (pstate->s_options & SCAN_FROM_STRING ||
485
67.8M
                      PDFScanRules ? 0x04 : 0xffff);
486
67.8M
    scanner_state sstate;
487
488
67.8M
    sptr = endptr = NULL; /* Quiet compiler */
489
67.8M
    if (pstate->s_pstack != 0) {
490
7.11k
        if_not_spush1()
491
0
            return retcode;
492
7.11k
        myref = osp;
493
7.11k
    }
494
    /* Check whether we are resuming after an interruption. */
495
67.8M
    if (pstate->s_scan_type != scanning_none) {
496
9.56k
        sstate = *pstate;
497
9.56k
        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
11
            uint next = sstate.s_da.next - sstate.s_da.base;
501
11
            uint limit = sstate.s_da.limit - sstate.s_da.base;
502
503
11
            sstate.s_da.base = sstate.s_da.buf;
504
11
            sstate.s_da.next = sstate.s_da.buf + next;
505
11
            sstate.s_da.limit = sstate.s_da.buf + limit;
506
11
        }
507
9.56k
        daptr = sstate.s_da.next;
508
9.56k
        switch (sstate.s_scan_type) {
509
559
            case scanning_binary:
510
559
                retcode = (*sstate.s_ss.binary.cont)
511
559
                    (i_ctx_p, myref, &sstate);
512
559
                s_begin_inline(s, sptr, endptr);
513
559
                if (retcode == scan_Refill)
514
550
                    goto pause;
515
9
                goto sret;
516
9
            case scanning_comment:
517
0
                s_begin_inline(s, sptr, endptr);
518
0
                goto cont_comment;
519
9.00k
            case scanning_name:
520
9.00k
                goto cont_name;
521
0
            case scanning_string:
522
0
                goto cont_string;
523
0
            default:
524
0
                return_error(gs_error_Fatal);
525
9.56k
        }
526
9.56k
    }
527
67.8M
    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
67.8M
        sstate.s_da.base = sstate.s_da.next = &(sstate.s_da.buf[0]);
534
67.8M
        sstate.s_da.limit = sstate.s_da.next;
535
67.8M
        sstate.s_da.is_dynamic = false;
536
67.8M
    }
537
    /* Fetch any state variables that are relevant even if */
538
    /* sstate.s_scan_type == scanning_none. */
539
67.8M
    sstate.s_pstack = pstate->s_pstack;
540
67.8M
    sstate.s_pdepth = pstate->s_pdepth;
541
67.8M
    ref_assign(&sstate.s_file, &pstate->s_file);
542
67.8M
    sstate.s_options = pstate->s_options;
543
67.8M
    SCAN_INIT_ERROR(&sstate);
544
67.8M
    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
140M
  top:c = sgetc_inline(s, sptr, endptr);
550
140M
    if_debug1m('S', imemory, (c >= 32 && c <= 126 ? "`%c'" : c >= 0 ? "`\\%03o'" : "`%d'"), c);
551
140M
    switch (c) {
552
559k
        case ' ':
553
565k
        case '\f':
554
574k
        case '\t':
555
575k
        case char_CR:
556
714k
        case char_EOL:
557
912k
        case char_NULL:
558
912k
            goto top;
559
20.2k
        case 0x04:              /* see ctrld above */
560
20.2k
            if (c == ctrld)     /* treat as ordinary name char */
561
0
                goto begin_name;
562
            /* fall through */
563
9.48M
        case '[':
564
17.3M
        case ']':
565
17.3M
            s1[0] = (byte) c;
566
17.3M
            retcode = name_ref(imemory, s1, 1, myref, 1);       /* can't fail */
567
17.3M
            r_set_attrs(myref, a_executable);
568
17.3M
            break;
569
141k
        case '<':
570
141k
            if (level2_enabled) {
571
128k
                ensure2(scanning_none);
572
128k
                c = sgetc_inline(s, sptr, endptr);
573
128k
                switch (c) {
574
84.0k
                    case '<':
575
84.0k
                        sputback_inline(s, sptr, endptr);
576
84.0k
                        sstate.s_ss.s_name.s_name_type = 0;
577
84.0k
                        sstate.s_ss.s_name.s_try_number = false;
578
84.0k
                        goto try_funny_name;
579
1
                    case '~':
580
1
                        s_A85D_init_inline(&sstate.s_ss.a85d);
581
1
                        sstate.s_ss.st.templat = &s_A85D_template;
582
1
                        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
1
                        sstate.s_ss.a85d.pdf_rules = false;
587
1
                        goto str;
588
128k
                }
589
44.6k
                sputback_inline(s, sptr, endptr);
590
44.6k
            }
591
57.0k
            (void)s_AXD_init_inline(&sstate.s_ss.axd);
592
57.0k
            sstate.s_ss.st.templat = &s_AXD_template;
593
1.93M
          str:s_end_inline(s, sptr, endptr);
594
1.93M
            dynamic_init(&sstate.s_da, imemory);
595
1.98M
          cont_string:for (;;) {
596
1.98M
                stream_cursor_write w;
597
598
1.98M
                w.ptr = sstate.s_da.next - 1;
599
1.98M
                w.limit = sstate.s_da.limit - 1;
600
1.98M
                status = (*sstate.s_ss.st.templat->process)
601
1.98M
                    (&sstate.s_ss.st, &s->cursor.r, &w,
602
1.98M
                     s->end_status == EOFC);
603
1.98M
                if (!check_only)
604
1.98M
                    sstate.s_da.next = w.ptr + 1;
605
1.98M
                switch (status) {
606
57.7k
                    case 0:
607
57.7k
                        status = s->end_status;
608
57.7k
                        if (status < 0) {
609
12
                            if (status == EOFC) {
610
12
                                if (check_only) {
611
0
                                    retcode = scan_Refill;
612
0
                                    sstate.s_scan_type = scanning_string;
613
0
                                    goto suspend;
614
0
                                } else
615
12
                                    sreturn(gs_error_syntaxerror);
616
0
                            }
617
0
                            break;
618
12
                        }
619
57.6k
                        s_process_read_buf(s);
620
57.6k
                        continue;
621
158
                    case 1:
622
158
                        if (!check_only) {
623
158
                            retcode = dynamic_grow(&sstate.s_da, sstate.s_da.next, max_string_size);
624
158
                            if (retcode == gs_error_VMerror) {
625
0
                                sstate.s_scan_type = scanning_string;
626
0
                                goto suspend;
627
158
                            } else if (retcode < 0)
628
158
                                sreturn(retcode);
629
158
                        }
630
158
                        continue;
631
1.98M
                }
632
1.93M
                break;
633
1.98M
            }
634
1.93M
            s_begin_inline(s, sptr, endptr);
635
1.93M
            switch (status) {
636
4
                default:
637
                    /*case ERRC: */
638
4
                    sreturn(gs_error_syntaxerror);
639
0
                case INTC:
640
0
                case CALLC:
641
0
                    sstate.s_scan_type = scanning_string;
642
0
                    goto pause;
643
1.93M
                case EOFC:
644
1.93M
                    ;
645
1.93M
            }
646
1.93M
            retcode = dynamic_make_string(i_ctx_p, myref, &sstate.s_da, sstate.s_da.next);
647
1.93M
            if (retcode < 0) {  /* VMerror */
648
0
                sputback(s);    /* rescan ) */
649
0
                sstate.s_scan_type = scanning_string;
650
0
                goto suspend;
651
0
            }
652
1.93M
            break;
653
1.93M
        case '(':
654
1.87M
            sstate.s_ss.pssd.from_string =
655
1.87M
                ((pstate->s_options & SCAN_FROM_STRING) != 0) &&
656
1.87M
                !level2_enabled;
657
1.87M
            s_PSSD_partially_init_inline(&sstate.s_ss.pssd);
658
1.87M
            sstate.s_ss.st.templat = &s_PSSD_template;
659
1.87M
            goto str;
660
8.65M
        case '{':
661
8.65M
            if (sstate.s_pstack == 0) {  /* outermost procedure */
662
2.35M
                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
2.35M
                sstate.s_pdepth = ref_stack_count_inline(&o_stack);
668
2.35M
            }
669
8.65M
            make_int(osp, sstate.s_pstack);
670
8.65M
            sstate.s_pstack = ref_stack_count_inline(&o_stack);
671
8.65M
            if_debug3m('S', imemory, "[S{]d=%d, s=%d->%d\n",
672
8.65M
                       sstate.s_pdepth, (int)osp->value.intval, sstate.s_pstack);
673
8.65M
            goto snext;
674
83.9k
        case '>':
675
83.9k
            if (level2_enabled) {
676
83.9k
                ensure2(scanning_none);
677
83.9k
                sstate.s_ss.s_name.s_name_type = 0;
678
83.9k
                sstate.s_ss.s_name.s_try_number = false;
679
83.9k
                goto try_funny_name;
680
83.9k
            }
681
            /* falls through */
682
1
        case ')':
683
1
            sreturn(gs_error_syntaxerror);
684
8.64M
        case '}':
685
8.64M
            if (sstate.s_pstack == 0)
686
8.64M
                sreturn(gs_error_syntaxerror);
687
8.64M
            osp--;
688
8.64M
            {
689
8.64M
                uint size = ref_stack_count_inline(&o_stack) - sstate.s_pstack;
690
8.64M
                ref arr;
691
692
8.64M
                if_debug4m('S', imemory, "[S}]d=%"PRIu32", s=%"PRIu32"->%"PRIpsint", c=%"PRIu32"\n",
693
8.64M
                           sstate.s_pdepth, sstate.s_pstack,
694
8.64M
                           (sstate.s_pstack == sstate.s_pdepth ? 0 :
695
8.64M
                           ref_stack_index(&o_stack, size)->value.intval),
696
8.64M
                           size + sstate.s_pstack);
697
8.64M
                if (size > max_array_size)
698
8.64M
                    sreturn(gs_error_limitcheck);
699
8.64M
                myref = (sstate.s_pstack == sstate.s_pdepth ? pref : &arr);
700
8.64M
                if (check_only) {
701
0
                    make_empty_array(myref, 0);
702
0
                    ref_stack_pop(&o_stack, size);
703
8.64M
                } else if (ref_array_packing.value.boolval) {
704
8.22M
                    retcode = make_packed_array(myref, &o_stack, size,
705
8.22M
                                                idmemory, "scanner(packed)");
706
8.22M
                    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
8.22M
                    r_set_attrs(myref, a_executable);
713
8.22M
                } else {
714
419k
                    retcode = ialloc_ref_array(myref,
715
419k
                                               a_executable + a_all, size,
716
419k
                                               "scanner(proc)");
717
419k
                    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
419k
                    retcode = ref_stack_store(&o_stack, myref, size, 0, 1,
724
419k
                                              false, idmemory, "scanner");
725
419k
                    if (retcode < 0) {
726
0
                        ifree_ref_array(myref, "scanner(proc)");
727
0
                        sreturn(retcode);
728
0
                    }
729
419k
                    ref_stack_pop(&o_stack, size);
730
419k
                }
731
8.64M
                if (sstate.s_pstack == sstate.s_pdepth) {         /* This was the top-level procedure. */
732
2.35M
                    spop1();
733
2.35M
                    sstate.s_pstack = 0;
734
6.29M
                } else {
735
6.29M
                    if (osp < osbot)
736
0
                        ref_stack_pop_block(&o_stack);
737
6.29M
                    sstate.s_pstack = osp->value.intval;
738
6.29M
                    *osp = arr;
739
6.29M
                    goto snext;
740
6.29M
                }
741
8.64M
            }
742
2.35M
            break;
743
32.8M
        case '/':
744
            /*
745
             * If the last thing in the input is a '/', don't try to read
746
             * any more data.
747
             */
748
32.8M
            if (sptr >= endptr && s->end_status != EOFC) {
749
5.47k
                refill2(scanning_none);
750
5.47k
            }
751
32.8M
            c = sgetc_inline(s, sptr, endptr);
752
32.8M
            if (!PDFScanRules && (c == '/')) {
753
3.06M
                sstate.s_ss.s_name.s_name_type = 2;
754
3.06M
                c = sgetc_inline(s, sptr, endptr);
755
3.06M
            } else
756
29.8M
                sstate.s_ss.s_name.s_name_type = 1;
757
32.8M
            sstate.s_ss.s_name.s_try_number = false;
758
32.8M
            switch (decoder[c]) {
759
3.55M
                case ctype_name:
760
32.8M
                default:
761
32.8M
                    goto do_name;
762
32.8M
                case ctype_btoken:
763
7
                    if (!(ref_binary_object_format.value.intval != 0 && level2_enabled))
764
0
                        goto do_name;
765
                    /* otherwise, an empty name */
766
8
                case ctype_exception:
767
1.80k
                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
44.5k
                case ctype_other:
775
44.5k
                    if (c == ctrld) /* see above */
776
0
                        goto do_name;
777
44.5k
                    sstate.s_da.base = sstate.s_da.limit = daptr = 0;
778
44.5k
                    sstate.s_da.is_dynamic = false;
779
44.5k
                    goto nx;
780
32.8M
            }
781
41.0k
        case '%':
782
41.0k
            {                   /* Scan as much as possible within the buffer. */
783
41.0k
                const byte *base = sptr;
784
41.0k
                const byte *end;
785
786
535k
                while (++sptr < endptr)         /* stop 1 char early */
787
535k
                    switch (*sptr) {
788
1
                        case char_CR:
789
1
                            end = sptr;
790
1
                            if (sptr[1] == char_EOL)
791
0
                                sptr++;
792
40.7k
                          cend: /* Check for externally processed comments. */
793
40.7k
                            retcode = scan_comment(i_ctx_p, myref, &sstate,
794
40.7k
                                                   base, end, false);
795
40.7k
                            if (retcode != 0)
796
6.76k
                                goto comment;
797
33.9k
                            goto top;
798
34.8k
                        case char_EOL:
799
40.7k
                        case '\f':
800
40.7k
                            end = sptr;
801
40.7k
                            goto cend;
802
535k
                    }
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
326
                --sptr;
810
326
                sstate.s_da.buf[1] = 0;
811
326
                {
812
                    /* Could be an externally processable comment. */
813
326
                    uint len = sptr + 1 - base;
814
326
                    if (len > sizeof(sstate.s_da.buf))
815
0
                        len = sizeof(sstate.s_da.buf);
816
817
326
                    memcpy(sstate.s_da.buf, base, len);
818
326
                    daptr = sstate.s_da.buf + len;
819
326
                }
820
326
                sstate.s_da.base = sstate.s_da.buf;
821
326
                sstate.s_da.is_dynamic = false;
822
326
            }
823
            /* Enter here to continue scanning a comment. */
824
            /* daptr must be set. */
825
309k
          cont_comment:for (;;) {
826
309k
                switch ((c = sgetc_inline(s, sptr, endptr))) {
827
309k
                    default:
828
309k
                        if (c < 0)
829
4
                            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
                                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
                                    goto end_comment;
842
0
                                default:
843
0
                                    sreturn(gs_error_syntaxerror);
844
4
                            }
845
309k
                        if (daptr < sstate.s_da.buf + max_comment_line)
846
6.83k
                            *daptr++ = c;
847
309k
                        continue;
848
2
                    case char_CR:
849
275
                    case char_EOL:
850
322
                    case '\f':
851
326
                      end_comment:
852
326
                        retcode = scan_comment(i_ctx_p, myref, &sstate,
853
326
                                               sstate.s_da.buf, daptr, true);
854
326
                        if (retcode != 0)
855
93
                            goto comment;
856
233
                        goto top;
857
309k
                }
858
309k
            }
859
            /*NOTREACHED */
860
17.8k
        case EOFC:
861
17.8k
            if (sstate.s_pstack != 0) {
862
31
                if (check_only)
863
0
                    goto pause;
864
31
                sreturn(gs_error_syntaxerror);
865
0
            }
866
17.7k
            retcode = scan_EOF;
867
17.7k
            break;
868
0
        case ERRC:
869
0
            sreturn(gs_error_ioerror);
870
871
            /* Check for a Level 2 funny name (<< or >>). */
872
            /* c is '<' or '>'.  We already did an ensure2. */
873
167k
          try_funny_name:
874
167k
            {
875
167k
                int c1 = sgetc_inline(s, sptr, endptr);
876
877
167k
                if (c1 == c) {
878
167k
                    s1[0] = s1[1] = c;
879
167k
                    name_ref(imemory, s1, 2, myref, 1); /* can't fail */
880
167k
                    goto have_name;
881
167k
                }
882
0
                sputback_inline(s, sptr, endptr);
883
0
            }
884
0
            sreturn(gs_error_syntaxerror);
885
886
            /* Handle separately the names that might be a number. */
887
1.71M
        case '0':
888
18.5M
        case '1':
889
20.2M
        case '2':
890
21.4M
        case '3':
891
22.1M
        case '4':
892
22.4M
        case '5':
893
22.6M
        case '6':
894
22.8M
        case '7':
895
22.9M
        case '8':
896
23.1M
        case '9':
897
27.9M
        case '.':
898
27.9M
            sign = 0;
899
28.5M
    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
28.5M
            retcode = scan_number(sptr + (sign & 1),
907
28.5M
                    endptr /*(*endptr == char_CR ? endptr : endptr + 1) */ ,
908
28.5M
                                  sign, myref, &newptr, i_ctx_p->scanner_options);
909
28.5M
            if (retcode == 1 && decoder[newptr[-1]] == ctype_space) {
910
8.59M
                sptr = newptr - 1;
911
8.59M
                if (*sptr == char_CR && sptr[1] == char_EOL)
912
0
                    sptr++;
913
8.59M
                retcode = 0;
914
8.59M
                ref_mark_new(myref);
915
8.59M
                break;
916
8.59M
            }
917
19.9M
            sstate.s_ss.s_name.s_name_type = 0;
918
19.9M
            sstate.s_ss.s_name.s_try_number = true;
919
19.9M
            goto do_name;
920
281
        case '+':
921
281
            sign = 1;
922
281
            goto nr;
923
587k
        case '-':
924
587k
            sign = -1;
925
587k
            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
587k
            goto nr;
951
952
            /* Check for a binary object */
953
587k
          case 128: case 129: case 130: case 131: case 132: case 133: case 134: case 135:
954
66.1k
          case 136: case 137: case 138: case 139: case 140: case 141: case 142: case 143:
955
67.6k
          case 144: case 145: case 146: case 147: case 148: case 149: case 150: case 151:
956
67.6k
          case 152: case 153: case 154: case 155: case 156: case 157: case 158: case 159:
957
67.6k
            if ((ref_binary_object_format.value.intval != 0 && level2_enabled)) {
958
67.6k
                s_end_inline(s, sptr, endptr);
959
67.6k
                retcode = scan_binary_token(i_ctx_p, myref, &sstate);
960
67.6k
                s_begin_inline(s, sptr, endptr);
961
67.6k
                if (retcode == scan_Refill)
962
392
                    goto pause;
963
67.2k
                break;
964
67.6k
            }
965
            /* Not a binary object, fall through. */
966
967
            /* The default is a name. */
968
70.7k
        default:
969
70.7k
            if (c < 0) {
970
10.7k
                dynamic_init(&sstate.s_da, name_memory(imemory));        /* sstate.s_da state must be clean */
971
10.7k
                sstate.s_scan_type = scanning_none;
972
10.7k
                goto pause;
973
10.7k
            }
974
            /* Populate the switch with enough cases to force */
975
            /* simple compilers to use a dispatch rather than tests. */
976
60.4k
        case '!':
977
126k
        case '"':
978
573k
        case '#':
979
759k
        case '$':
980
759k
        case '&':
981
823k
        case '\'':
982
826k
        case '*':
983
1.02M
        case ',':
984
1.48M
        case '=':
985
1.48M
        case ':':
986
1.83M
        case ';':
987
1.83M
        case '?':
988
1.83M
        case '@':
989
1.84M
        case 'A':
990
1.84M
        case 'B':
991
1.93M
        case 'C':
992
1.99M
        case 'D':
993
2.05M
        case 'E':
994
2.12M
        case 'F':
995
2.14M
        case 'G':
996
2.18M
        case 'H':
997
2.23M
        case 'I':
998
2.23M
        case 'J':
999
2.23M
        case 'K':
1000
2.24M
        case 'L':
1001
2.25M
        case 'M':
1002
2.28M
        case 'N':
1003
2.33M
        case 'O':
1004
2.43M
        case 'P':
1005
2.48M
        case 'Q':
1006
2.62M
        case 'R':
1007
2.73M
        case 'S':
1008
2.84M
        case 'T':
1009
2.86M
        case 'U':
1010
2.91M
        case 'V':
1011
2.94M
        case 'W':
1012
2.94M
        case 'X':
1013
2.94M
        case 'Y':
1014
2.94M
        case 'Z':
1015
2.94M
        case '\\':
1016
2.94M
        case '^':
1017
2.94M
        case '_':
1018
2.94M
        case '`':
1019
4.02M
        case 'a':
1020
4.62M
        case 'b':
1021
7.08M
        case 'c':
1022
13.0M
        case 'd':
1023
18.5M
        case 'e':
1024
19.7M
        case 'f':
1025
22.0M
        case 'g':
1026
22.1M
        case 'h':
1027
27.7M
        case 'i':
1028
27.7M
        case 'j':
1029
28.2M
        case 'k':
1030
29.3M
        case 'l':
1031
29.9M
        case 'm':
1032
31.0M
        case 'n':
1033
31.5M
        case 'o':
1034
36.4M
        case 'p':
1035
36.4M
        case 'q':
1036
38.1M
        case 'r':
1037
40.4M
        case 's':
1038
41.0M
        case 't':
1039
41.1M
        case 'u':
1040
41.2M
        case 'v':
1041
41.6M
        case 'w':
1042
41.6M
        case 'x':
1043
41.7M
        case 'y':
1044
41.7M
        case 'z':
1045
41.7M
        case '|':
1046
41.7M
        case '~':
1047
41.7M
          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
41.7M
            sstate.s_ss.s_name.s_name_type = 0;
1053
41.7M
            sstate.s_ss.s_name.s_try_number = false;
1054
94.5M
          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
94.5M
            sstate.s_da.base = (byte *) sptr;
1059
94.5M
            sstate.s_da.is_dynamic = false;
1060
94.5M
            {
1061
94.5M
                const byte *endp1 = endptr - 1;
1062
1063
704M
                do {
1064
704M
                    if (sptr >= endp1)  /* stop 1 early! */
1065
86.6k
                        goto dyn_name;
1066
704M
                }
1067
703M
                while (decoder[*++sptr] <= max_name_ctype || *sptr == ctrld);   /* digit or name */
1068
94.5M
            }
1069
            /* Name ended within the buffer. */
1070
94.4M
            daptr = (byte *) sptr;
1071
94.4M
            c = *sptr;
1072
94.4M
            goto nx;
1073
86.6k
          dyn_name:             /* Name extended past end of buffer. */
1074
86.6k
            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
86.6k
            sstate.s_da.limit = (byte *)++ sptr;
1079
86.6k
            sstate.s_da.memory = name_memory(imemory);
1080
86.6k
            retcode = dynamic_grow(&sstate.s_da, sstate.s_da.limit, name_max_string);
1081
86.6k
            if (retcode < 0) {
1082
0
                dynamic_save(&sstate.s_da);
1083
0
                if (retcode != gs_error_VMerror)
1084
0
                    sreturn(retcode);
1085
0
                sstate.s_scan_type = scanning_name;
1086
0
                goto pause_ret;
1087
0
            }
1088
86.6k
            daptr = sstate.s_da.next;
1089
            /* Enter here to continue scanning a name. */
1090
            /* daptr must be set. */
1091
95.6k
          cont_name:s_begin_inline(s, sptr, endptr);
1092
563k
            while (decoder[c = sgetc_inline(s, sptr, endptr)] <= max_name_ctype || c == ctrld) {
1093
467k
                if (daptr == sstate.s_da.limit) {
1094
3.66k
                    retcode = dynamic_grow(&sstate.s_da, daptr,
1095
3.66k
                                           name_max_string);
1096
3.66k
                    if (retcode < 0) {
1097
2
                        dynamic_save(&sstate.s_da);
1098
2
                        if (retcode != gs_error_VMerror)
1099
2
                            sreturn(retcode);
1100
0
                        sputback_inline(s, sptr, endptr);
1101
0
                        sstate.s_scan_type = scanning_name;
1102
0
                        goto pause_ret;
1103
2
                    }
1104
3.66k
                    daptr = sstate.s_da.next;
1105
3.66k
                }
1106
467k
                *daptr++ = c;
1107
467k
            }
1108
94.5M
          nx:switch (decoder[c]) {
1109
53.8M
                case ctype_other:
1110
53.8M
                    if (c == ctrld) /* see above */
1111
0
                        break;
1112
53.8M
                case ctype_btoken:
1113
53.8M
                    sputback_inline(s, sptr, endptr);
1114
53.8M
                    break;
1115
40.6M
                case ctype_space:
1116
                    /* Check for \r\n */
1117
40.6M
                    if (c == char_CR) {
1118
72.1k
                        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
84
                            if (s->end_status != EOFC) {
1122
83
                                sptr--;
1123
83
                                goto pause_name;
1124
83
                            }
1125
72.0k
                        } else if (sptr[1] == char_EOL)
1126
0
                            sptr++;
1127
72.1k
                    }
1128
40.6M
                    break;
1129
40.6M
                case ctype_exception:
1130
38.3k
                    switch (c) {
1131
0
                        case INTC:
1132
8.92k
                        case CALLC:
1133
8.92k
                            goto pause_name;
1134
0
                        case ERRC:
1135
0
                            sreturn(gs_error_ioerror);
1136
29.4k
                        case EOFC:
1137
29.4k
                            break;
1138
38.3k
                    }
1139
94.5M
            }
1140
            /* Check for a number */
1141
94.5M
            if (sstate.s_ss.s_name.s_try_number) {
1142
19.9M
                const byte *base = sstate.s_da.base;
1143
1144
19.9M
                scan_sign(sign, base);
1145
19.9M
                retcode = scan_number(base, daptr, sign, myref, &newptr, i_ctx_p->scanner_options);
1146
19.9M
                if (retcode == 1) {
1147
66.5k
                    ref_mark_new(myref);
1148
66.5k
                    retcode = 0;
1149
19.9M
                } else if (retcode != gs_error_syntaxerror) {
1150
15.0M
                    dynamic_free(&sstate.s_da);
1151
15.0M
                    if (sstate.s_ss.s_name.s_name_type == 2)
1152
15.0M
                        sreturn(gs_error_syntaxerror);
1153
15.0M
                    break;      /* might be gs_error_limitcheck */
1154
15.0M
                }
1155
19.9M
            }
1156
79.5M
            if (sstate.s_da.is_dynamic) {        /* We've already allocated the string on the heap. */
1157
77.5k
                uint size = daptr - sstate.s_da.base;
1158
1159
77.5k
                retcode = name_ref(imemory, sstate.s_da.base, size, myref, -1);
1160
77.5k
                if (retcode >= 0) {
1161
68.2k
                    dynamic_free(&sstate.s_da);
1162
68.2k
                } else {
1163
9.34k
                    retcode = dynamic_resize(&sstate.s_da, size);
1164
9.34k
                    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
9.34k
                    retcode = name_ref(imemory, sstate.s_da.base, size, myref, 2);
1171
9.34k
                }
1172
79.4M
            } else {
1173
79.4M
                retcode = name_ref(imemory, sstate.s_da.base, (uint) (daptr - sstate.s_da.base),
1174
79.4M
                                   myref, !s->foreign);
1175
79.4M
            }
1176
            /* Done scanning.  Check for preceding /'s. */
1177
79.5M
            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
79.6M
          have_name:switch (sstate.s_ss.s_name.s_name_type) {
1190
46.8M
                case 0: /* ordinary executable name */
1191
46.8M
                    if (r_has_type(myref, t_name))      /* i.e., not a number */
1192
46.8M
                        r_set_attrs(myref, a_executable);
1193
76.6M
                case 1: /* quoted name */
1194
76.6M
                    break;
1195
3.06M
                case 2: /* immediate lookup */
1196
3.06M
                    {
1197
3.06M
                        ref *pvalue;
1198
1199
3.06M
                        if (!r_has_type(myref, t_name) ||
1200
3.06M
                            (pvalue = dict_find_name(myref)) == 0) {
1201
2
                            ref_assign(&sstate.s_error.object, myref);
1202
2
                            r_set_attrs(&sstate.s_error.object,
1203
2
                                a_executable); /* Adobe compatibility */
1204
2
                            sreturn(gs_error_undefined);
1205
0
                        }
1206
3.06M
                        if (sstate.s_pstack != 0 &&
1207
3.06M
                            r_space(pvalue) > ialloc_space(idmemory)
1208
3.06M
                            )
1209
3.06M
                            sreturn(gs_error_invalidaccess);
1210
3.06M
                        ref_assign_new(myref, pvalue);
1211
3.06M
                    }
1212
79.6M
            }
1213
140M
    }
1214
125M
  sret:if (retcode < 0) {
1215
56
        s_end_inline(s, sptr, endptr);
1216
56
        pstate->s_error = sstate.s_error;
1217
56
        if (sstate.s_pstack != 0) {
1218
40
            if (retcode == gs_error_undefined)
1219
0
                *pref = *osp;   /* return undefined name as error token */
1220
40
            ref_stack_pop(&o_stack,
1221
40
                          ref_stack_count(&o_stack) - (sstate.s_pdepth - 1));
1222
40
        }
1223
56
        return retcode;
1224
56
    }
1225
    /* If we are at the top level, return the object, */
1226
    /* otherwise keep going. */
1227
125M
    if (sstate.s_pstack == 0) {
1228
67.8M
        s_end_inline(s, sptr, endptr);
1229
67.8M
        return retcode;
1230
67.8M
    }
1231
72.1M
  snext:if_not_spush1() {
1232
1
        s_end_inline(s, sptr, endptr);
1233
1
        sstate.s_scan_type = scanning_none;
1234
1
        goto save;
1235
1
    }
1236
72.1M
    myref = osp;
1237
72.1M
    goto top;
1238
1239
    /* Pause for an interrupt or callout. */
1240
9.00k
  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
9.00k
    sstate.s_da.next = daptr;
1244
9.00k
    dynamic_save(&sstate.s_da);
1245
9.00k
    sstate.s_scan_type = scanning_name;
1246
26.1k
  pause:
1247
26.1k
    retcode = scan_Refill;
1248
26.1k
  pause_ret:
1249
26.1k
    s_end_inline(s, sptr, endptr);
1250
26.1k
  suspend:
1251
26.1k
    if (sstate.s_pstack != 0)
1252
392
        osp--;                  /* myref */
1253
32.9k
  save:
1254
32.9k
    *pstate = sstate;
1255
32.9k
    return retcode;
1256
1257
    /* Handle a scanned comment. */
1258
6.85k
 comment:
1259
6.85k
    if (retcode < 0)
1260
0
        goto sret;
1261
6.85k
    s_end_inline(s, sptr, endptr);
1262
6.85k
    sstate.s_scan_type = scanning_none;
1263
6.85k
    goto save;
1264
6.85k
}