Coverage Report

Created: 2025-06-10 07:19

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