Coverage Report

Created: 2025-12-31 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/sstring.c
Line
Count
Source
1
/* Copyright (C) 2001-2023 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* String and hexstring streams (filters) */
18
#include "stdio_.h"   /* includes std.h */
19
#include "memory_.h"
20
#include "string_.h"
21
#include "strimpl.h"
22
#include "sstring.h"
23
#include "scanchar.h"
24
25
/* ------ ASCIIHexEncode ------ */
26
27
private_st_AXE_state();
28
29
/* Initialize the state */
30
static int
31
s_AXE_init(stream_state * st)
32
9.96k
{
33
9.96k
    stream_AXE_state *const ss = (stream_AXE_state *) st;
34
35
9.96k
    return s_AXE_init_inline(ss);
36
9.96k
}
37
38
/* Process a buffer */
39
static int
40
s_AXE_process(stream_state * st, stream_cursor_read * pr,
41
              stream_cursor_write * pw, bool last)
42
2.10M
{
43
2.10M
    stream_AXE_state *const ss = (stream_AXE_state *) st;
44
2.10M
    const byte *p = pr->ptr;
45
2.10M
    byte *q = pw->ptr;
46
2.10M
    int rcount = pr->limit - p;
47
2.10M
    int wcount = pw->limit - q;
48
2.10M
    int count;
49
2.10M
    int pos = ss->count;
50
2.10M
    const char *hex_digits = "0123456789ABCDEF";
51
2.10M
    int status = 0;
52
53
2.10M
    if (last && ss->EndOfData)
54
1.27M
        wcount--;   /* leave room for '>' */
55
2.10M
    wcount -= (wcount + pos * 2) / 64; /* leave room for \n */
56
2.10M
    wcount >>= 1;   /* 2 chars per input byte */
57
2.10M
    count = (wcount < rcount ? (status = 1, wcount) : rcount);
58
36.9M
    while (--count >= 0) {
59
34.8M
        *++q = hex_digits[*++p >> 4];
60
34.8M
        *++q = hex_digits[*p & 0xf];
61
34.8M
        if (!(++pos & 31) && (count != 0 || !last))
62
1.00M
            *++q = '\n';
63
34.8M
    }
64
2.10M
    if (last && status == 0 && ss->EndOfData)
65
1.26M
        *++q = '>';
66
2.10M
    pr->ptr = p;
67
2.10M
    pw->ptr = q;
68
2.10M
    ss->count = pos & 31;
69
2.10M
    return status;
70
2.10M
}
71
72
/* Stream template */
73
const stream_template s_AXE_template =
74
{&st_AXE_state, s_AXE_init, s_AXE_process, 1, 3
75
};
76
77
/* ------ ASCIIHexDecode ------ */
78
79
private_st_AXD_state();
80
81
/* Initialize the state */
82
static int
83
s_AXD_init(stream_state * st)
84
1.89k
{
85
1.89k
    stream_AXD_state *const ss = (stream_AXD_state *) st;
86
87
1.89k
    return s_AXD_init_inline(ss);
88
1.89k
}
89
90
/* Process a buffer */
91
static int
92
s_AXD_process(stream_state * st, stream_cursor_read * pr,
93
              stream_cursor_write * pw, bool last)
94
6.05M
{
95
6.05M
    stream_AXD_state *const ss = (stream_AXD_state *) st;
96
6.05M
    int code = s_hex_process(pr, pw, &ss->odd, hex_ignore_whitespace);
97
98
6.05M
    switch (code) {
99
11.5k
        case 0:
100
11.5k
            if (ss->odd >= 0 && last) {
101
314
                if (pw->ptr == pw->limit)
102
0
                    return 1;
103
314
                *++(pw->ptr) = ss->odd << 4;
104
314
                ss->odd = -1;
105
314
            }
106
            /* falls through */
107
14.3k
        case 1:
108
            /* We still need to read ahead and check for EOD. */
109
14.4k
            for (; pr->ptr < pr->limit; pr->ptr++)
110
2.67k
                if (scan_char_decoder[pr->ptr[1]] != ctype_space) {
111
2.58k
                    if (pr->ptr[1] == '>') {
112
0
                        pr->ptr++;
113
0
                        goto eod;
114
0
                    }
115
2.58k
                    return 1;
116
2.58k
                }
117
11.7k
            return 0;    /* still need to scan ahead */
118
0
        default:
119
0
            return code;
120
6.04M
        case ERRC:
121
6.04M
            ;
122
6.05M
    }
123
    /*
124
     * Check for EOD.  ERRC implies at least one more character
125
     * was read; we must unread it, since the caller might have
126
     * invoked the filter with exactly the right count to read all
127
     * the available data, and we might be reading past the end.
128
     */
129
6.04M
    if (*pr->ptr != '>') { /* EOD */
130
1.47k
        --(pr->ptr);
131
1.47k
        return ERRC;
132
1.47k
    }
133
6.04M
  eod:if (ss->odd >= 0) {
134
46.9k
        if (pw->ptr == pw->limit)
135
0
            return 1;
136
46.9k
        *++(pw->ptr) = ss->odd << 4;
137
46.9k
    }
138
6.04M
    return EOFC;
139
6.04M
}
140
141
/* Stream template */
142
const stream_template s_AXD_template =
143
{&st_AXD_state, s_AXD_init, s_AXD_process, 2, 1
144
};
145
146
/* ------ PSStringEncode ------ */
147
148
/* Process a buffer */
149
static int
150
s_PSSE_process(stream_state * st, stream_cursor_read * pr,
151
               stream_cursor_write * pw, bool last)
152
21.1M
{
153
21.1M
    const byte *p = pr->ptr;
154
21.1M
    const byte *rlimit = pr->limit;
155
21.1M
    byte *q = pw->ptr;
156
21.1M
    byte *wlimit = pw->limit;
157
21.1M
    int status = 0;
158
159
    /* This doesn't have to be very efficient. */
160
406M
    while (p < rlimit) {
161
390M
        int c = *++p;
162
163
390M
        if (c < 32 || c >= 127) {
164
305M
            const char *pesc;
165
305M
            const char *const esc = "\n\r\t\b\f";
166
167
305M
            if (c < 32 && c != 0 && (pesc = strchr(esc, c)) != 0) {
168
1.37M
                if (wlimit - q < 2) {
169
7.22k
                    --p;
170
7.22k
                    status = 1;
171
7.22k
                    break;
172
7.22k
                }
173
1.36M
                *++q = '\\';
174
1.36M
                *++q = "nrtbf"[pesc - esc];
175
1.36M
                continue;
176
1.37M
            }
177
304M
            if (wlimit - q < 4) {
178
5.55M
                --p;
179
5.55M
                status = 1;
180
5.55M
                break;
181
5.55M
            }
182
298M
            q[1] = '\\';
183
298M
            q[2] = (c >> 6) + '0';
184
298M
            q[3] = ((c >> 3) & 7) + '0';
185
298M
            q[4] = (c & 7) + '0';
186
298M
            q += 4;
187
298M
            continue;
188
304M
        } else if (c == '(' || c == ')' || c == '\\') {
189
2.66M
            if (wlimit - q < 2) {
190
2.99k
                --p;
191
2.99k
                status = 1;
192
2.99k
                break;
193
2.99k
            }
194
2.66M
            *++q = '\\';
195
82.3M
        } else {
196
82.3M
            if (q == wlimit) {
197
146k
                --p;
198
146k
                status = 1;
199
146k
                break;
200
146k
            }
201
82.3M
        }
202
84.9M
        *++q = c;
203
84.9M
    }
204
21.1M
    if (last && status == 0) {
205
4.93M
        if (q == wlimit)
206
62
            status = 1;
207
4.93M
        else
208
4.93M
            *++q = ')';
209
4.93M
    }
210
21.1M
    pr->ptr = p;
211
21.1M
    pw->ptr = q;
212
21.1M
    return status;
213
21.1M
}
214
215
/* Stream template */
216
const stream_template s_PSSE_template =
217
{&st_stream_state, NULL, s_PSSE_process, 1, 4
218
};
219
220
/* ------ PSStringDecode ------ */
221
222
private_st_PSSD_state();
223
224
/* Initialize the state */
225
int
226
s_PSSD_init(stream_state * st)
227
98.6k
{
228
98.6k
    stream_PSSD_state *const ss = (stream_PSSD_state *) st;
229
230
98.6k
    ss->from_string = false;
231
98.6k
    return s_PSSD_partially_init_inline(ss);
232
98.6k
}
233
234
/* Process a buffer */
235
static int
236
s_PSSD_process(stream_state * st, stream_cursor_read * pr,
237
               stream_cursor_write * pw, bool last)
238
192M
{
239
192M
    stream_PSSD_state *const ss = (stream_PSSD_state *) st;
240
192M
    const byte *p = pr->ptr;
241
192M
    const byte *rlimit = pr->limit;
242
192M
    byte *q = pw->ptr;
243
192M
    byte *wlimit = pw->limit;
244
192M
    int status = 0;
245
192M
    int c;
246
247
192M
#define check_p(n)\
248
192M
  if ( p == rlimit ) { p -= n; goto out; }
249
192M
#define check_q(n)\
250
2.91G
  if ( q == wlimit ) { p -= n; status = 1; goto out; }
251
3.11G
    while (p < rlimit) {
252
3.11G
        c = *++p;
253
3.11G
        if (c == '\\' && !ss->from_string) {
254
47.9M
            check_p(1);
255
47.9M
            switch ((c = *++p)) {
256
29.3M
                case 'n':
257
29.3M
                    c = '\n';
258
29.3M
                    goto put;
259
235k
                case 'r':
260
235k
                    c = '\r';
261
235k
                    goto put;
262
755k
                case 't':
263
755k
                    c = '\t';
264
755k
                    goto put;
265
10.2k
                case 'b':
266
10.2k
                    c = '\b';
267
10.2k
                    goto put;
268
299k
                case 'f':
269
299k
                    c = '\f';
270
299k
                    goto put;
271
11.4M
                default:  /* ignore the \ */
272
42.0M
                  put:check_q(2);
273
42.0M
                    *++q = c;
274
42.0M
                    continue;
275
583k
                case char_CR: /* ignore, check for following \n */
276
583k
                    check_p(2);
277
582k
                    if (p[1] == char_EOL)
278
77.1k
                        p++;
279
582k
                    continue;
280
213k
                case char_EOL: /* ignore */
281
213k
                    continue;
282
2.97M
                case '0':
283
4.01M
                case '1':
284
4.07M
                case '2':
285
5.07M
                case '3':
286
5.08M
                case '4':
287
5.08M
                case '5':
288
5.09M
                case '6':
289
5.10M
                case '7':
290
5.10M
                    {
291
5.10M
                        int d;
292
293
5.10M
                        check_p(2);
294
5.09M
                        d = p[1];
295
5.09M
                        c -= '0';
296
5.09M
                        if (d >= '0' && d <= '7') {
297
4.05M
                            if (p + 1 == rlimit) {
298
725
                                p -= 2;
299
725
                                goto out;
300
725
                            }
301
4.04M
                            check_q(2);
302
4.04M
                            c = (c << 3) + d - '0';
303
4.04M
                            d = p[2];
304
4.04M
                            if (d >= '0' && d <= '7') {
305
4.00M
                                c = (c << 3) + d - '0';
306
4.00M
                                p += 2;
307
4.00M
                            } else
308
42.3k
                                p++;
309
4.04M
                        } else
310
5.08M
                            check_q(2);
311
5.08M
                        *++q = c;
312
5.08M
                        continue;
313
5.09M
                    }
314
47.9M
            }
315
47.9M
        } else
316
3.06G
            switch (c) {
317
6.60M
                case '(':
318
6.60M
                    check_q(1);
319
6.60M
                    ss->depth++;
320
6.60M
                    break;
321
196M
                case ')':
322
196M
                    if (ss->depth == 0) {
323
192M
                        status = EOFC;
324
192M
                        goto out;
325
192M
                    }
326
3.84M
                    check_q(1);
327
3.84M
                    ss->depth--;
328
3.84M
                    break;
329
5.26M
                case char_CR: /* convert to \n */
330
5.26M
                    check_p(1);
331
5.25M
                    check_q(1);
332
5.25M
                    if (p[1] == char_EOL)
333
120k
                        p++;
334
5.25M
                    *++q = '\n';
335
5.25M
                    continue;
336
1.36M
                case char_EOL:
337
1.36M
                    c = '\n';
338
                    /* fall through */
339
2.85G
                default:
340
2.85G
                    check_q(1);
341
2.85G
                    break;
342
3.06G
            }
343
2.86G
        *++q = c;
344
2.86G
    }
345
115k
#undef check_p
346
115k
#undef check_q
347
192M
  out:pr->ptr = p;
348
192M
    pw->ptr = q;
349
192M
    if (last && status == 0 && p != rlimit)
350
860
        status = ERRC;
351
192M
    return status;
352
192M
}
353
354
/* Stream template */
355
const stream_template s_PSSD_template =
356
{&st_PSSD_state, s_PSSD_init, s_PSSD_process, 4, 1
357
};
358
359
/* ------ Utilities ------ */
360
361
/*
362
 * Convert hex data to binary.
363
 * Return 1 if we filled the string,
364
 *        0 if we ran out of input data before filling the string,
365
 *        2 if hex_break_on_whitespace is on and we encounrered
366
 *          a white space.
367
 *        ERRC on error.
368
 * The caller must set *odd_digit to -1 before the first call;
369
 * after each call, if an odd number of hex digits has been read (total),
370
 * *odd_digit is the odd digit value, otherwise *odd_digit = -1.
371
 * See strimpl.h for the definition of syntax.
372
 */
373
int
374
s_hex_process(stream_cursor_read * pr, stream_cursor_write * pw,
375
              int *odd_digit, hex_syntax syntax)
376
42.4M
{
377
42.4M
    const byte *p = pr->ptr;
378
42.4M
    const byte *rlimit = pr->limit;
379
42.4M
    byte *q = pw->ptr;
380
42.4M
    byte *wlimit = pw->limit;
381
42.4M
    byte *q0 = q;
382
42.4M
    byte val1 = (byte) * odd_digit;
383
42.4M
    byte val2;
384
42.4M
    uint rcount;
385
42.4M
    byte *flimit;
386
42.4M
    const byte *const decoder = scan_char_decoder;
387
42.4M
    int code = 0;
388
389
42.4M
    if (q >= wlimit)
390
2.37k
        return 1;
391
42.4M
    if (val1 <= 0xf)
392
31.3k
        goto d2;
393
43.3M
    do {
394
        /* No digits read */
395
43.3M
        if ((rcount = (rlimit - p) >> 1) != 0)
396
43.3M
        {
397
            /* Set up a fast end-of-loop check, so we don't have to test */
398
            /* both p and q against their respective limits. */
399
43.3M
            flimit = (rcount < wlimit - q ? q + rcount : wlimit);
400
212M
            while (1) {
401
212M
                if ((val1 = decoder[p[1]]) <= 0xf &&
402
205M
                    (val2 = decoder[p[2]]) <= 0xf) {
403
205M
                    p += 2;
404
205M
                    *++q = (val1 << 4) + val2;
405
205M
                    if (q < flimit)
406
168M
                        continue;
407
36.2M
                    if (q >= wlimit)
408
44.0k
                        goto px;
409
36.2M
                }
410
43.3M
                break;
411
212M
            }
412
43.3M
        }
413
        /* About to read the first digit */
414
49.0M
        while (1) {
415
49.0M
            if (p >= rlimit)
416
36.1M
                goto end1;
417
12.9M
            if ((val1 = decoder[*++p]) > 0xf) {
418
11.7M
                if (val1 == ctype_space) {
419
1.30M
                    switch (syntax) {
420
396k
                        case hex_ignore_garbage:
421
1.21M
                        case hex_ignore_whitespace:
422
1.21M
                            continue;
423
74.9k
                        case hex_ignore_leading_whitespace:
424
74.9k
                            if (q == q0 && *odd_digit < 0)
425
43.4k
                                continue;
426
                            /* pass through */
427
40.7k
                        case hex_break_on_whitespace:
428
40.7k
                            --p;
429
40.7k
                            code = 2;
430
40.7k
                            goto end1;
431
1.30M
                    }
432
10.4M
                } else if (syntax == hex_ignore_garbage)
433
4.46M
                    continue;
434
6.00M
                code = ERRC;
435
6.00M
                goto end1;
436
11.7M
            }
437
1.15M
            break;
438
12.9M
        }
439
1.18M
  d2:
440
        /* About to read the second hex digit of a pair */
441
5.77M
        while (1) {
442
5.77M
            if (p >= rlimit) {
443
112k
                *odd_digit = val1;
444
112k
                goto ended;
445
112k
            }
446
5.66M
            if ((val2 = decoder[*++p]) > 0xf) {
447
4.65M
                if (val2 == ctype_space)
448
538k
                    switch (syntax) {
449
350k
                        case hex_ignore_garbage:
450
497k
                        case hex_ignore_whitespace:
451
497k
                            continue;
452
33.6k
                        case hex_ignore_leading_whitespace:
453
33.6k
                            if (q == q0)
454
24.7k
                                continue;
455
                            /* pass through */
456
16.0k
                        case hex_break_on_whitespace:
457
16.0k
                            --p;
458
16.0k
                            *odd_digit = val1;
459
16.0k
                            code = 2;
460
16.0k
                            goto ended;
461
538k
                    }
462
4.12M
                if (syntax == hex_ignore_garbage)
463
4.06M
                    continue;
464
57.2k
                *odd_digit = val1;
465
57.2k
                code = ERRC;
466
57.2k
                goto ended;
467
4.12M
            }
468
1.00M
            break;
469
5.66M
        }
470
1.00M
        *++q = (val1 << 4) + val2;
471
1.00M
    } while (q < wlimit);
472
44.2k
  px:code = 1;
473
42.2M
  end1:*odd_digit = -1;
474
42.4M
  ended:pr->ptr = p;
475
42.4M
    pw->ptr = q;
476
42.4M
    return code;
477
42.2M
}