Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/gpdl/txttop.c
Line
Count
Source
1
/* Copyright (C) 2026 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
/* Top-level API implementation for text file handling */
17
18
/* Language wrapper implementation (see pltop.h) */
19
20
21
/* Enable the following for a dump of the codepoints to stdout. */
22
/* #define DEBUG_CODEPOINTS */
23
24
/* Enable the following for a hacky dump of the output PCL to file. */
25
/* #define DEBUG_DUMP_PCL */
26
27
#ifdef DEBUG_DUMP_PCL
28
#include <stdio.h>
29
static FILE *debug_pcl_out = NULL;
30
static void wipe(void)
31
{
32
        fclose(debug_pcl_out);
33
        debug_pcl_out = NULL;
34
}
35
static void
36
debug_as_pcl(const char *p, int n)
37
{
38
        if (debug_pcl_out == NULL)
39
        {
40
            debug_pcl_out = fopen("debug_pcl_out", "wb");
41
            atexit(wipe);
42
        }
43
        fwrite(p, n, 1, debug_pcl_out);
44
}
45
#endif
46
47
#include "pltop.h"
48
#include "plmain.h"
49
50
#include "plparse.h" /* for e_ExitLanguage */
51
#include "plmain.h"
52
#include "gxdevice.h" /* so we can include gxht.h below */
53
#include "gserrors.h"
54
#include "gp.h"
55
#include "assert_.h"
56
57
/*
58
 * The TXT interpeter is identical to pl_interp_t.
59
 * The TXT interpreter instance is derived from pl_interp_implementation_t.
60
 */
61
62
typedef enum
63
{
64
    TXT_STATE_INIT = 0,
65
    TXT_STATE_UTF8,
66
    TXT_STATE_UTF8_MAYBE,
67
    TXT_STATE_UTF16_LE,
68
    TXT_STATE_UTF16_BE,
69
    TXT_STATE_ASCII
70
} txt_state_t;
71
72
typedef struct txt_interp_instance_s txt_interp_instance_t;
73
74
struct txt_interp_instance_s
75
{
76
    gs_memory_t *memory;                /* memory allocator to use */
77
78
    pl_interp_implementation_t *sub;
79
    gx_device *device;
80
81
    int buffered;
82
    byte buffer[4];
83
84
    int state;
85
    int detected;
86
    int just_had_lf;
87
    int just_had_cr;
88
    int col;
89
    int sent;
90
};
91
92
enum
93
{
94
    TXT_UNDETECTED = -1,
95
    TXT_UNKNOWN,
96
    TXT_UTF8,
97
    TXT_UTF8_MAYBE,
98
    TXT_UTF16_LE,
99
    TXT_UTF16_BE,
100
    TXT_ASCII,
101
};
102
103
static int
104
identify_from_buffer(const unsigned char *s, int len)
105
35.0k
{
106
35.0k
    int count_controls = 0;
107
35.0k
    int count_hi = 0;
108
35.0k
    int count_tabs = 0;
109
35.0k
    int plausibly_utf8 = 1;
110
35.0k
    int i;
111
112
    /* UTF-8 with a BOM */
113
35.0k
    if (len >= 3 && s[0] == 0xef && s[1] == 0xbb && s[2] == 0xbf)
114
25
        return TXT_UTF8;
115
    /* UTF-16 (little endian) */
116
35.0k
    if (len >= 2 && s[0] == 0xff && s[1] == 0xfe)
117
170
        return TXT_UTF16_LE;
118
    /* UTF-16 (big endian) */
119
34.8k
    if (len >= 2 && s[0] == 0xfe && s[1] == 0xff)
120
193
        return TXT_UTF16_BE;
121
122
    /* Gather some stats. */
123
38.8M
    for (i = 0; i < len; i++)
124
38.8M
    {
125
38.8M
        if (s[i] == 9)
126
127k
        {
127
127k
            count_tabs++;
128
127k
        }
129
38.7M
        else if (s[i] == 12)
130
174k
        {
131
            /* Form feed. We'll let that slide. */
132
174k
        }
133
38.5M
        else if (s[i] == 10)
134
349k
        {
135
349k
           if (i+1 < len && s[i+1] == 13)
136
33.1k
                i++;
137
349k
        }
138
38.1M
        else if (s[i] == 13)
139
780k
        {
140
780k
           if (i+1 < len && s[i+1] == 10)
141
155k
                i++;
142
780k
        }
143
37.4M
        else if (s[i] < 32 || s[i] == 0x7f)
144
8.65M
        {
145
8.65M
            count_controls++;
146
8.65M
        }
147
28.7M
        else if (s[i] < 0x7f)
148
21.4M
        {
149
            /* Seems like a reasonable ASCII value. */
150
21.4M
        }
151
7.31M
        else
152
7.31M
        {
153
7.31M
            count_hi++;
154
7.31M
            if ((s[i] & 0xF8) == 0xF0)
155
363k
            {
156
                /* 3 following bytes */
157
363k
                if (i+1 < len && (s[i+1] & 0xC0) != 0x80)
158
344k
                    plausibly_utf8 = 0;
159
18.3k
                else if (i+2 < len && (s[i+2] & 0xC0) != 0x80)
160
11.2k
                    plausibly_utf8 = 0;
161
7.14k
                else if (i+3 < len && (s[i+3] & 0xC0) != 0x80)
162
3.29k
                    plausibly_utf8 = 0;
163
3.84k
                else
164
3.84k
                    i+=3;
165
363k
            }
166
6.95M
            else if ((s[i] & 0xF0) == 0xE0)
167
489k
            {
168
                /* 2 following bytes */
169
489k
                if (i+1 < len && (s[i+1] & 0xC0) != 0x80)
170
444k
                    plausibly_utf8 = 0;
171
44.8k
                else if (i+2 < len && (s[i+2] & 0xC0) != 0x80)
172
29.1k
                    plausibly_utf8 = 0;
173
15.7k
                else
174
15.7k
                    i+=2;
175
489k
            }
176
6.46M
            else if ((s[i] & 0xE0) == 0xC0)
177
1.86M
            {
178
                /* 1 following bytes */
179
1.86M
                if (i+1 < len && (s[i+1] & 0xC0) != 0x80)
180
1.69M
                    plausibly_utf8 = 0;
181
169k
                else
182
169k
                    i++;
183
1.86M
            }
184
4.60M
            else
185
4.60M
                plausibly_utf8 = 0;
186
7.31M
        }
187
38.8M
    }
188
189
    /* Any (non tab/cr/lf/ff) control characters probably means this isn't text. */
190
34.7k
    if (count_controls > 0)
191
32.4k
        return TXT_UNKNOWN;
192
    /* If we've managed to decode all that as utf8 without problem, it's probably text. */
193
2.21k
    if (plausibly_utf8)
194
1.53k
        return TXT_UTF8_MAYBE;
195
    /* If we're hitting too many top bit set chars, give up. */
196
676
    if (count_hi > len/10)
197
228
        return TXT_UNKNOWN;
198
199
448
    return TXT_ASCII;
200
676
}
201
202
static int
203
txt_detect_language(const char *t, int len)
204
34.2k
{
205
34.2k
    const unsigned char *s = (const unsigned char *)t;
206
207
34.2k
    switch (identify_from_buffer(s, len))
208
34.2k
    {
209
13
    case TXT_UTF8:
210
102
    case TXT_UTF16_LE:
211
208
    case TXT_UTF16_BE:
212
        /* PCL spots files with lots of ESCs in them at confidence
213
         * level 80. We'll use 70, cos we don't want to override that. */
214
208
        return 70;
215
1.03k
    case TXT_UTF8_MAYBE:
216
1.33k
    case TXT_ASCII:
217
1.33k
        return 60;
218
0
    default:
219
32.7k
    case TXT_UNKNOWN:
220
32.7k
        break;
221
34.2k
    }
222
223
32.7k
    return 0;
224
34.2k
}
225
226
static const pl_interp_characteristics_t *
227
txt_impl_characteristics(const pl_interp_implementation_t *pimpl)
228
72.2k
{
229
72.2k
    static pl_interp_characteristics_t txt_characteristics =
230
72.2k
    {
231
72.2k
        "TXT",
232
72.2k
        txt_detect_language,
233
72.2k
    };
234
72.2k
    return &txt_characteristics;
235
72.2k
}
236
237
/* Do per-instance interpreter allocation/init. No device is set yet */
238
static int
239
txt_impl_allocate_interp_instance(pl_interp_implementation_t *impl,
240
                                  gs_memory_t *pmem)
241
18.4k
{
242
18.4k
    txt_interp_instance_t *instance;
243
244
18.4k
    instance = (txt_interp_instance_t *) gs_alloc_bytes(pmem,
245
18.4k
            sizeof(txt_interp_instance_t), "txt_impl_allocate_interp_instance");
246
247
18.4k
    if (!instance)
248
0
        return_error(gs_error_VMerror);
249
250
18.4k
    instance->memory = pmem;
251
18.4k
    instance->sub = NULL;
252
253
18.4k
    impl->interp_client_data = instance;
254
255
18.4k
    return 0;
256
18.4k
}
257
258
/* Prepare interp instance for the next "job" */
259
static int
260
txt_impl_init_job(pl_interp_implementation_t *impl,
261
                  gx_device                  *pdevice)
262
830
{
263
830
    txt_interp_instance_t *instance = impl->interp_client_data;
264
265
830
    instance->device = pdevice;
266
830
    instance->state = TXT_STATE_INIT;
267
830
    instance->buffered = 0;
268
830
    instance->detected = TXT_UNDETECTED;
269
830
    instance->just_had_lf = 0;
270
830
    instance->just_had_cr = 0;
271
830
    instance->col = 0;
272
273
830
    instance->sub = pl_main_get_pcl_instance(instance->memory);
274
275
830
    return pl_init_job(instance->sub, instance->device);
276
830
}
277
278
7.35k
#define ESC 27
279
280
static int
281
send_bytes(txt_interp_instance_t *instance, const byte *p, int n)
282
2.20M
{
283
2.20M
    stream_cursor_read cursor;
284
285
#ifdef DEBUG_DUMP_PCL
286
    debug_as_pcl(p, n);
287
#endif
288
289
2.20M
    stream_cursor_read_init(&cursor, p, n);
290
291
2.20M
    return instance->sub->proc_process(instance->sub, &cursor);
292
2.20M
}
293
294
static void
295
drop_buffered(txt_interp_instance_t *instance, int n)
296
1.51M
{
297
1.51M
    assert(instance->buffered >= n);
298
1.51M
    instance->buffered -= n;
299
1.51M
    if (instance->buffered > 0)
300
1.92k
        memmove(instance->buffer, &instance->buffer[n], instance->buffered);
301
1.51M
}
302
303
static int
304
send_pcl_init(txt_interp_instance_t *instance)
305
669
{
306
669
    static byte init[] = {
307
669
            ESC, 'E',                     /* Reset */
308
669
            ESC, '&', 'l', '0', 'O',      /* Orientation */
309
669
            ESC, '&', 'k', '1', '0', 'H', /* Horizontal spacing 10/120 of an inch. */
310
669
            ESC, '&', 'l', '8', 'C',      /* Vertical line spacing 8/48 of an inch. */
311
669
            ESC, '&', 't', '8', '3', 'P', /* &t = double byte parsing, 83 = utf-8, P = ? */
312
669
            ESC, '(', '1', '8', 'N',      /* Primary symbol set = 18N = Unicode */
313
669
            ESC, '(', 's', '0', 'P',      /* Fixed pitch */
314
669
            ESC, '(', 's', '1', '2', 'H', /* Secondary fixed pitch 12cpi */
315
669
            ESC, '(', 's', '8', 'V',      /* Point size 8 */
316
669
            ESC, '(', 's', '3', 'T',      /* Typeface number 3 */
317
669
            ESC, '&', 's', '0', 'C'       /* Wrappity wrap wrap */
318
669
    };
319
320
669
    return send_bytes(instance, init, sizeof(init));
321
669
}
322
323
static int
324
send_urc(txt_interp_instance_t *instance, int n)
325
508
{
326
508
    static byte unicode_replacement_char_as_utf8[] = { 0xe3, 0xbf, 0xbd };
327
328
508
    if (instance->state == TXT_STATE_UTF8_MAYBE)
329
61
    {
330
        /* We were guessing that this was UTF8. Now we know it's not. Drop back to ascii. */
331
61
        instance->state = TXT_STATE_ASCII;
332
61
        return 0;
333
61
    }
334
335
447
    drop_buffered(instance, n);
336
337
447
    instance->sent = 1;
338
447
    return send_bytes(instance, unicode_replacement_char_as_utf8, sizeof(unicode_replacement_char_as_utf8));
339
508
}
340
341
static int
342
send_utf8(txt_interp_instance_t *instance, int val)
343
2.20M
{
344
2.20M
    byte buf[4];
345
2.20M
    int n;
346
347
    /* Finally, send the val! */
348
2.20M
    if (val < 0x80)
349
1.76M
    {
350
1.76M
        buf[0] = val;
351
1.76M
        n = 1;
352
1.76M
    }
353
435k
    else if (val < 0x800)
354
111k
    {
355
111k
        buf[0] = 0xC0 + (val>>6);
356
111k
        buf[1] = 0x80 + (val & 0x3F);
357
111k
        n = 2;
358
111k
    }
359
323k
    else if (val < 0x10000)
360
323k
    {
361
323k
        buf[0] = 0xE0 + (val>>12);
362
323k
        buf[1] = 0x80 + ((val>>6) & 0x3F);
363
323k
        buf[2] = 0x80 + (val & 0x3F);
364
323k
        n = 3;
365
323k
    }
366
486
    else
367
486
    {
368
486
        buf[0] = 0xF0 + (val>>18);
369
486
        buf[1] = 0x80 + ((val>>12) & 0x3F);
370
486
        buf[2] = 0x80 + ((val>>6) & 0x3F);
371
486
        buf[3] = 0x80 + (val & 0x3F);
372
486
        n = 4;
373
486
    }
374
2.20M
    return send_bytes(instance, buf, n);
375
2.20M
}
376
377
/* All our actual codepoints should flow through here. So this is where
378
 * we do the housekeeping. */
379
static int
380
send_codepoint(txt_interp_instance_t *instance, int val)
381
1.51M
{
382
1.51M
    int code;
383
384
#ifdef DEBUG_CODEPOINTS
385
    dprintf3("Sending codepoint %d (%x) %c\n", val, val, val >= 32 && val <= 255 && val != 127 ? val : '.');
386
#endif
387
388
1.51M
    instance->sent = 1;
389
    /* Tidy up whatever mess of CR/LF we are passed. */
390
1.51M
    if (val == '\r')
391
36.8k
    {
392
        /* If we've got a CR and we've just had a LF, swallow this. */
393
36.8k
        if (instance->just_had_lf)
394
18.3k
        {
395
18.3k
            instance->just_had_lf = 0;
396
18.3k
            return 0;
397
18.3k
        }
398
18.4k
        instance->just_had_cr = 1;
399
18.4k
        val = '\n';
400
18.4k
    }
401
1.47M
    else if (val == '\n')
402
42.4k
    {
403
        /* If we've got a LF and we've just had a CR, swallow this. */
404
42.4k
        if (instance->just_had_cr)
405
12.5k
        {
406
12.5k
            instance->just_had_cr = 0;
407
12.5k
            return 0;
408
12.5k
        }
409
29.9k
        instance->just_had_lf = 1;
410
29.9k
    }
411
1.43M
    else
412
1.43M
    {
413
1.43M
        instance->just_had_cr = 0;
414
1.43M
        instance->just_had_lf = 0;
415
1.43M
    }
416
417
    /* Keep track of what column we're at to so we can do tab handling. */
418
1.48M
    if (val == '\n')
419
48.3k
    {
420
48.3k
        instance->col = 0;
421
48.3k
        code = send_utf8(instance, '\n');
422
48.3k
        if (code < 0 && code != gs_error_NeedInput)
423
0
            return code;
424
48.3k
        return send_utf8(instance, '\r');
425
48.3k
    }
426
1.43M
    if (val == '\t')
427
97.4k
    {
428
97.4k
        int spaces = 8 - (instance->col & 7);
429
867k
        while (spaces--)
430
769k
        {
431
769k
            int code = send_utf8(instance, ' ');
432
769k
            if (code < 0 && code != gs_error_NeedInput)
433
0
                return code;
434
769k
            instance->col++;
435
769k
        }
436
97.4k
        return 0;
437
97.4k
    }
438
1.33M
    instance->col++;
439
440
#if 0
441
    /* No need for this as PCL line wrapping works for us. If PCL ever
442
     * decides to wrap at a number of columns that aren't a multiple of
443
     * 8 then we'll need to do it manually again!. */
444
    if (instance->col == 80)
445
    {
446
        instance->col = 0;
447
        code = send_utf8(instance, '\n');
448
        if (code < 0 && code != gs_error_NeedInput))
449
            return code;
450
        return send_utf8(instance, '\r');
451
    }
452
#endif
453
454
1.33M
    return send_utf8(instance, val);
455
1.43M
}
456
457
static int
458
process_block(txt_interp_instance_t *instance, const byte *ptr, int n)
459
2.04k
{
460
2.04k
    int code;
461
2.04k
    byte *s = &instance->buffer[0];
462
2.04k
    int old_state = instance->state;
463
2.04k
    int val;
464
465
2.04k
    if (instance->detected == TXT_UNDETECTED)
466
830
    {
467
830
        instance->detected = identify_from_buffer(ptr, n);
468
        /* If we're thinking we're ASCII, go straight there. Otherwise, we'll let the
469
         * BOM detection below run its course. */
470
830
        if (instance->detected == TXT_ASCII)
471
145
            instance->state = TXT_STATE_ASCII;
472
830
    }
473
474
2.04k
    instance->sent = 0;
475
1.90M
    while (n)
476
1.90M
    {
477
        /* instance->sent records whether we pulled anything out of the buffer
478
         * last time round the loop. If we changed state, then don't refill the
479
         * buffer. Otherwise only fill the buffer if we didn't a char last time
480
         * (maybe we need char 2 of a 2 char sequence?) or if we haven't got
481
         * anything in the buffer already. */
482
1.90M
        if (instance->state == old_state && (!instance->sent || instance->buffered == 0))
483
1.90M
        {
484
1.90M
            assert(instance->buffered < 4);
485
1.90M
            s[instance->buffered++] = *ptr++;
486
1.90M
            n--;
487
1.90M
        }
488
1.90M
        old_state = instance->state;
489
490
1.90M
        instance->sent = 0;
491
1.90M
        switch (instance->state)
492
1.90M
        {
493
1.85k
        case TXT_STATE_INIT:
494
495
1.85k
            if (instance->buffered == 3 && s[0] == 0xef && s[1] == 0xbb && s[2] == 0xbf)
496
12
            {
497
12
                instance->state = TXT_STATE_UTF8;
498
12
                drop_buffered(instance, 3);
499
12
            }
500
1.84k
            else if (instance->buffered == 2 && s[0] == 0xff && s[1] == 0xfe)
501
81
            {
502
81
                instance->state = TXT_STATE_UTF16_LE;
503
81
                drop_buffered(instance, 2);
504
81
            }
505
1.76k
            else if (instance->buffered == 2 && s[0] == 0xfe && s[1] == 0xff)
506
87
            {
507
87
                instance->state = TXT_STATE_UTF16_BE;
508
87
                drop_buffered(instance, 2);
509
87
            }
510
1.67k
            else if (instance->buffered >= 3)
511
489
            {
512
                /* We haven't found a BOM, try for utf8. */
513
489
                instance->state = TXT_STATE_UTF8_MAYBE;
514
489
            }
515
516
            /* If we've recognised the BOM, then send the init string. */
517
1.85k
            if (instance->state != TXT_STATE_INIT)
518
669
            {
519
669
                code = send_pcl_init(instance);
520
669
                if (code < 0) {
521
669
                    if (code != gs_error_NeedInput || n == 0)
522
4
                        return code;
523
669
                }
524
669
            }
525
1.85k
            break;
526
1.85k
        case TXT_STATE_UTF8:
527
511k
        case TXT_STATE_UTF8_MAYBE:
528
511k
            if ((s[0] & 0xF8) == 0xF0)
529
1.93k
            {
530
                /* 3 following bytes */
531
1.93k
                if (instance->buffered >= 2 && (s[1] & 0xC0) != 0x80)
532
2
                {
533
2
                    code = send_urc(instance, 1);
534
2
                    if (code < 0) {
535
0
                        if (code != gs_error_NeedInput || n == 0)
536
0
                            return code;
537
0
                    }
538
2
                }
539
1.93k
                else if (instance->buffered >= 3 && (s[2] & 0xC0) != 0x80)
540
1
                {
541
1
                    code = send_urc(instance, 2);
542
1
                    if (code < 0) {
543
0
                        if (code != gs_error_NeedInput || n == 0)
544
0
                            return code;
545
0
                    }
546
1
                }
547
1.93k
                else if (instance->buffered == 4 && (s[3] & 0xC0) != 0x80)
548
1
                {
549
1
                    code = send_urc(instance, 3);
550
1
                    if (code < 0) {
551
0
                        if (code != gs_error_NeedInput || n == 0)
552
0
                            return code;
553
0
                    }
554
1
                }
555
1.93k
                else if (instance->buffered == 4)
556
486
                {
557
                    /* Valid encoding of 4 bytes */
558
486
                    val = ((s[0] & 0x7)<<18) | ((s[1] & 0x3f)<<12) | ((s[2] & 0x3f)<<6) |  (s[3] & 0x3f);
559
486
                    drop_buffered(instance, 4);
560
486
                    code = send_codepoint(instance, val);
561
486
                    if (code < 0) {
562
486
                        if (code != gs_error_NeedInput || n == 0)
563
16
                            return code;
564
486
                    }
565
486
                }
566
1.44k
                else if (instance->buffered != 1 && instance->buffered != 2 && instance->buffered != 3)
567
0
                {
568
                    /* Should never happen. */
569
0
                    return_error(gs_error_Fatal);
570
0
                }
571
1.93k
            }
572
509k
            else if ((s[0] & 0xF0) == 0xE0)
573
43.6k
            {
574
                /* 2 following bytes */
575
43.6k
                if (instance->buffered >= 2 && (s[1] & 0xC0) != 0x80)
576
5
                {
577
5
                    code = send_urc(instance, 1);
578
5
                    if (code < 0) {
579
0
                        if (code != gs_error_NeedInput || n == 0)
580
0
                            return code;
581
0
                    }
582
5
                }
583
43.6k
                else if (instance->buffered >= 3 && (s[2] & 0xC0) != 0x80)
584
1
                {
585
1
                    code = send_urc(instance, 2);
586
1
                    if (code < 0) {
587
0
                        if (code != gs_error_NeedInput || n == 0)
588
0
                            return code;
589
0
                    }
590
1
                }
591
43.6k
                else if (instance->buffered == 3)
592
14.5k
                {
593
                    /* Valid encoding of 3 bytes */
594
14.5k
                    val = ((s[0] & 0xF)<<12) | ((s[1] & 0x3f)<<6) | (s[2] & 0x3f);
595
14.5k
                    drop_buffered(instance, 3);
596
14.5k
                    code = send_codepoint(instance, val);
597
14.5k
                    if (code < 0) {
598
14.5k
                        if (code != gs_error_NeedInput || n == 0)
599
25
                            return code;
600
14.5k
                    }
601
14.5k
                }
602
29.0k
                else if (instance->buffered != 1 && instance->buffered != 2)
603
12
                {
604
                    /* Should never happen. */
605
12
                    return_error(gs_error_Fatal);
606
12
                }
607
43.6k
            }
608
465k
            else if ((s[0] & 0xE0) == 0xC0)
609
11.2k
            {
610
                /* 1 following bytes */
611
11.2k
                if (instance->buffered >= 2 && (s[1] & 0xC0) != 0x80)
612
9
                {
613
9
                    code = send_urc(instance, 1);
614
9
                    if (code < 0) {
615
0
                        if (code != gs_error_NeedInput || n == 0)
616
0
                            return code;
617
0
                    }
618
9
                }
619
11.2k
                else if (instance->buffered == 2)
620
5.60k
                {
621
                    /* Valid encoding of 2 bytes */
622
5.60k
                    val = ((s[0] & 0x1F)<<6) | (s[1] & 0x3f);
623
5.60k
                    drop_buffered(instance, 2);
624
5.60k
                    code = send_codepoint(instance, val);
625
5.60k
                    if (code < 0) {
626
5.60k
                        if (code != gs_error_NeedInput || n == 0)
627
14
                            return code;
628
5.60k
                    }
629
5.60k
                }
630
5.61k
                else if (instance->buffered != 1)
631
4
                {
632
                    /* Should never happen. */
633
4
                    return_error(gs_error_Fatal);
634
4
                }
635
11.2k
            }
636
454k
            else if ((s[0] & 0xC0) == 0x80)
637
5
            {
638
                /* A continuation byte at the start. Should never see this. */
639
5
                code = send_urc(instance, 1);
640
5
                if (code < 0) {
641
0
                    if (code != gs_error_NeedInput || n == 0)
642
0
                        return code;
643
0
                }
644
5
            }
645
454k
            else if (s[0] < 0x80)
646
454k
            {
647
                /* Simple byte. */
648
454k
                val = s[0];
649
454k
                drop_buffered(instance, 1);
650
454k
                code = send_codepoint(instance, val);
651
454k
                if (code < 0) {
652
435k
                    if (code != gs_error_NeedInput || n == 0)
653
495
                        return code;
654
435k
                }
655
454k
            }
656
37
            else
657
37
            {
658
                /* Bytes we should never see in a UTF-8 file! (0xf8-0xff) */
659
37
                code = send_urc(instance, 1);
660
37
                if (code < 0) {
661
0
                    if (code != gs_error_NeedInput || n == 0)
662
0
                        return code;
663
0
                }
664
37
            }
665
510k
            break;
666
510k
        case TXT_STATE_UTF16_LE:
667
358k
            if (instance->buffered < 2)
668
178k
                break;
669
179k
            if (s[1] >= 0xD8 && s[1] < 0xDC)
670
1.12k
            {
671
                /* High surrogate */
672
1.12k
                if (instance->buffered < 4)
673
744
                    break;
674
382
                if (s[3] < 0xDC || s[3] > 0xDF)
675
221
                {
676
                    /* Not followed by a low surrogate! Ignore the high surrogate. */
677
221
                    code = send_urc(instance, 2);
678
221
                    if (code < 0)
679
221
                        return code;
680
0
                    break;
681
221
                }
682
161
                val = (((s[0] | (s[1]<<8)) - 0xdc00)<<10) + (s[2] | (s[3]<<8)) - 0xdc00 + 0x10000;
683
161
                drop_buffered(instance, 4);
684
161
            }
685
178k
            else
686
178k
            {
687
178k
                val = s[0] | (s[1]<<8);
688
178k
                drop_buffered(instance, 2);
689
178k
            }
690
178k
            code = send_codepoint(instance, val);
691
178k
            if (code < 0) {
692
178k
                if (code != gs_error_NeedInput || n == 0)
693
159
                    return code;
694
178k
            }
695
178k
            break;
696
357k
        case TXT_STATE_UTF16_BE:
697
357k
            if (instance->buffered < 2)
698
178k
                break;
699
179k
            if (s[0] >= 0xD8 && s[0] < 0xDC)
700
820
            {
701
                /* High surrogate */
702
820
                if (instance->buffered < 4)
703
535
                    break;
704
285
                if (s[2] < 0xDC || s[2] > 0xDF)
705
226
                {
706
                    /* Not followed by a low surrogate! Ignore the high surrogate. */
707
226
                    code = send_urc(instance, 2);
708
226
                    if (code < 0)
709
226
                        return code;
710
0
                    break;
711
226
                }
712
59
                val = (((s[1] | (s[0]<<8)) - 0xdc00)<<10) + (s[3] | (s[2]<<8)) - 0xdc00 + 0x10000;
713
59
                drop_buffered(instance, 4);
714
59
            }
715
178k
            else
716
178k
            {
717
178k
                val = s[1] | (s[0]<<8);
718
178k
                drop_buffered(instance, 2);
719
178k
            }
720
178k
            code = send_codepoint(instance, val);
721
178k
            if (code < 0) {
722
177k
                if (code != gs_error_NeedInput || n == 0)
723
189
                    return code;
724
177k
            }
725
178k
            break;
726
678k
        case TXT_STATE_ASCII:
727
1.35M
            while (instance->buffered > 0)
728
678k
            {
729
678k
                code = send_codepoint(instance, s[0]);
730
678k
                if (code < 0) {
731
570k
                    if (code != gs_error_NeedInput || n == 0)
732
436
                        return code;
733
570k
                }
734
678k
                drop_buffered(instance, 1);
735
678k
            }
736
678k
            break;
737
678k
        default:
738
0
            return_error(gs_error_Fatal);
739
1.90M
        }
740
1.90M
    }
741
241
    return 0;
742
2.04k
}
743
744
/* Parse an entire random access file */
745
#if 0
746
static int
747
txt_impl_process_file(pl_interp_implementation_t *impl, const char *filename)
748
{
749
    txt_interp_instance_t *instance = impl->interp_client_data;
750
    int code, code1;
751
    gp_file *file;
752
753
    file = gp_fopen(instance->memory, filename, "rb");
754
    if (file == 0)
755
        return_error(gs_error_ioerror);
756
757
    instance->sub = pl_main_get_pcl_instance(instance->memory);
758
759
    code = pl_init_job(instance->sub, instance->device);
760
    if (code >= 0)
761
    {
762
        code = pl_process_file(instance->sub, filename);
763
    }
764
765
    code1 = pl_dnit_job(instance->sub);
766
    if (code >= 0)
767
        code = code1;
768
769
    gp_fclose(file);
770
771
    return code;
772
}
773
#endif
774
775
/* Do any setup for parser per-cursor */
776
static int                      /* ret 0 or +ve if ok, else -ve error code */
777
txt_impl_process_begin(pl_interp_implementation_t * impl)
778
830
{
779
830
    return 0;
780
830
}
781
782
/* Parse a cursor-full of data */
783
static int
784
txt_impl_process(pl_interp_implementation_t *impl, stream_cursor_read *cursor)
785
2.04k
{
786
2.04k
    txt_interp_instance_t *instance = impl->interp_client_data;
787
2.04k
    int avail;
788
2.04k
    int code;
789
790
2.04k
    avail = cursor->limit - cursor->ptr;
791
2.04k
    code = process_block(instance, cursor->ptr + 1, avail);
792
2.04k
    cursor->ptr = cursor->limit;
793
794
2.04k
    return code;
795
2.04k
}
796
797
static int                      /* ret 0 or +ve if ok, else -ve error code */
798
txt_impl_process_end(pl_interp_implementation_t * impl)
799
830
{
800
830
    return 0;
801
830
}
802
803
/* Skip to end of job.
804
 * Return 1 if done, 0 ok but EOJ not found, else negative error code.
805
 */
806
static int
807
txt_impl_flush_to_eoj(pl_interp_implementation_t *impl, stream_cursor_read *pcursor)
808
104
{
809
    /* assume SO files cannot be pjl embedded */
810
104
    pcursor->ptr = pcursor->limit;
811
104
    return 0;
812
104
}
813
814
/* Parser action for end-of-file */
815
static int
816
txt_impl_process_eof(pl_interp_implementation_t *impl)
817
814
{
818
814
    txt_interp_instance_t *instance = impl->interp_client_data;
819
820
814
    if (instance->sub)
821
814
        return pl_process_eof(instance->sub);
822
823
0
    return 0;
824
814
}
825
826
/* Report any errors after running a job */
827
static int
828
txt_impl_report_errors(pl_interp_implementation_t *impl,
829
                       int code,           /* prev termination status */
830
                       long file_position, /* file position of error, -1 if unknown */
831
                       bool force_to_cout  /* force errors to cout */
832
                       )
833
16
{
834
16
    txt_interp_instance_t *instance = impl->interp_client_data;
835
16
    int ret = 0;
836
837
16
    if (instance->sub)
838
16
        ret = pl_report_errors(instance->sub, code, file_position, force_to_cout);
839
840
16
    return ret;
841
16
}
842
843
/* Wrap up interp instance after a "job" */
844
static int
845
txt_impl_dnit_job(pl_interp_implementation_t *impl)
846
830
{
847
830
    txt_interp_instance_t *instance = impl->interp_client_data;
848
830
    int code = 0;
849
850
830
    if (instance->sub)
851
830
        code = pl_dnit_job(instance->sub);
852
830
    instance->sub = NULL;
853
830
    instance->device = NULL;
854
855
830
    return code;
856
830
}
857
858
/* Deallocate a interpreter instance */
859
static int
860
txt_impl_deallocate_interp_instance(pl_interp_implementation_t *impl)
861
18.4k
{
862
18.4k
    txt_interp_instance_t *instance = impl->interp_client_data;
863
864
18.4k
    gs_free_object(instance->memory, instance, "so_impl_deallocate_interp_instance");
865
866
18.4k
    return 0;
867
18.4k
}
868
869
/* Parser implementation descriptor */
870
pl_interp_implementation_t txt_implementation =
871
{
872
    txt_impl_characteristics,
873
    txt_impl_allocate_interp_instance,
874
    NULL,                       /* get_device_memory */
875
    NULL,                       /* set_param */
876
    NULL,                       /* add_path */
877
    NULL,                       /* post_args_init */
878
    txt_impl_init_job,
879
    NULL,                       /* run_prefix_commands */
880
    NULL,                       /* txt_impl_process_file, */
881
    txt_impl_process_begin,
882
    txt_impl_process,
883
    txt_impl_process_end,
884
    txt_impl_flush_to_eoj,
885
    txt_impl_process_eof,
886
    txt_impl_report_errors,
887
    txt_impl_dnit_job,
888
    txt_impl_deallocate_interp_instance,
889
    NULL,
890
};