Coverage Report

Created: 2026-08-08 08:00

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
8.70k
{
33
8.70k
    stream_AXE_state *const ss = (stream_AXE_state *) st;
34
35
8.70k
    return s_AXE_init_inline(ss);
36
8.70k
}
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
1.38M
{
43
1.38M
    stream_AXE_state *const ss = (stream_AXE_state *) st;
44
1.38M
    const byte *p = pr->ptr;
45
1.38M
    byte *q = pw->ptr;
46
1.38M
    int rcount = pr->limit - p;
47
1.38M
    int wcount = pw->limit - q;
48
1.38M
    int count;
49
1.38M
    int pos = ss->count;
50
1.38M
    const char *hex_digits = "0123456789ABCDEF";
51
1.38M
    int status = 0;
52
53
1.38M
    if (last && ss->EndOfData)
54
769k
        wcount--;   /* leave room for '>' */
55
1.38M
    wcount -= (wcount + pos * 2) / 64; /* leave room for \n */
56
1.38M
    wcount >>= 1;   /* 2 chars per input byte */
57
1.38M
    count = (wcount < rcount ? (status = 1, wcount) : rcount);
58
26.6M
    while (--count >= 0) {
59
25.2M
        *++q = hex_digits[*++p >> 4];
60
25.2M
        *++q = hex_digits[*p & 0xf];
61
25.2M
        if (!(++pos & 31) && (count != 0 || !last))
62
741k
            *++q = '\n';
63
25.2M
    }
64
1.38M
    if (last && status == 0 && ss->EndOfData)
65
768k
        *++q = '>';
66
1.38M
    pr->ptr = p;
67
1.38M
    pw->ptr = q;
68
1.38M
    ss->count = pos & 31;
69
1.38M
    return status;
70
1.38M
}
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.46k
{
85
1.46k
    stream_AXD_state *const ss = (stream_AXD_state *) st;
86
87
1.46k
    return s_AXD_init_inline(ss);
88
1.46k
}
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
5.03M
{
95
5.03M
    stream_AXD_state *const ss = (stream_AXD_state *) st;
96
5.03M
    int code = s_hex_process(pr, pw, &ss->odd, hex_ignore_whitespace);
97
98
5.03M
    switch (code) {
99
7.51k
        case 0:
100
7.51k
            if (ss->odd >= 0 && last) {
101
315
                if (pw->ptr == pw->limit)
102
0
                    return 1;
103
315
                *++(pw->ptr) = ss->odd << 4;
104
315
                ss->odd = -1;
105
315
            }
106
            /* falls through */
107
9.42k
        case 1:
108
            /* We still need to read ahead and check for EOD. */
109
9.46k
            for (; pr->ptr < pr->limit; pr->ptr++)
110
1.77k
                if (scan_char_decoder[pr->ptr[1]] != ctype_space) {
111
1.73k
                    if (pr->ptr[1] == '>') {
112
0
                        pr->ptr++;
113
0
                        goto eod;
114
0
                    }
115
1.73k
                    return 1;
116
1.73k
                }
117
7.69k
            return 0;    /* still need to scan ahead */
118
0
        default:
119
0
            return code;
120
5.02M
        case ERRC:
121
5.02M
            ;
122
5.03M
    }
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
5.02M
    if (*pr->ptr != '>') { /* EOD */
130
1.10k
        --(pr->ptr);
131
1.10k
        return ERRC;
132
1.10k
    }
133
5.02M
  eod:if (ss->odd >= 0) {
134
32.3k
        if (pw->ptr == pw->limit)
135
0
            return 1;
136
32.3k
        *++(pw->ptr) = ss->odd << 4;
137
32.3k
    }
138
5.02M
    return EOFC;
139
5.02M
}
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
2.91M
{
153
2.91M
    const byte *p = pr->ptr;
154
2.91M
    const byte *rlimit = pr->limit;
155
2.91M
    byte *q = pw->ptr;
156
2.91M
    byte *wlimit = pw->limit;
157
2.91M
    int status = 0;
158
159
    /* This doesn't have to be very efficient. */
160
23.8M
    while (p < rlimit) {
161
21.0M
        int c = *++p;
162
163
21.0M
        if (c < 32 || c >= 127) {
164
7.06M
            const char *pesc;
165
7.06M
            const char *const esc = "\n\r\t\b\f";
166
167
7.06M
            if (c < 32 && c != 0 && (pesc = strchr(esc, c)) != 0) {
168
324k
                if (wlimit - q < 2) {
169
5.94k
                    --p;
170
5.94k
                    status = 1;
171
5.94k
                    break;
172
5.94k
                }
173
318k
                *++q = '\\';
174
318k
                *++q = "nrtbf"[pesc - esc];
175
318k
                continue;
176
324k
            }
177
6.73M
            if (wlimit - q < 4) {
178
121k
                --p;
179
121k
                status = 1;
180
121k
                break;
181
121k
            }
182
6.61M
            q[1] = '\\';
183
6.61M
            q[2] = (c >> 6) + '0';
184
6.61M
            q[3] = ((c >> 3) & 7) + '0';
185
6.61M
            q[4] = (c & 7) + '0';
186
6.61M
            q += 4;
187
6.61M
            continue;
188
13.9M
        } else if (c == '(' || c == ')' || c == '\\') {
189
1.00M
            if (wlimit - q < 2) {
190
2.80k
                --p;
191
2.80k
                status = 1;
192
2.80k
                break;
193
2.80k
            }
194
1.00M
            *++q = '\\';
195
12.9M
        } else {
196
12.9M
            if (q == wlimit) {
197
19.5k
                --p;
198
19.5k
                status = 1;
199
19.5k
                break;
200
19.5k
            }
201
12.9M
        }
202
13.9M
        *++q = c;
203
13.9M
    }
204
2.91M
    if (last && status == 0) {
205
1.89M
        if (q == wlimit)
206
769
            status = 1;
207
1.89M
        else
208
1.89M
            *++q = ')';
209
1.89M
    }
210
2.91M
    pr->ptr = p;
211
2.91M
    pw->ptr = q;
212
2.91M
    return status;
213
2.91M
}
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
18.1k
{
228
18.1k
    stream_PSSD_state *const ss = (stream_PSSD_state *) st;
229
230
18.1k
    ss->from_string = false;
231
18.1k
    return s_PSSD_partially_init_inline(ss);
232
18.1k
}
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
159M
{
239
159M
    stream_PSSD_state *const ss = (stream_PSSD_state *) st;
240
159M
    const byte *p = pr->ptr;
241
159M
    const byte *rlimit = pr->limit;
242
159M
    byte *q = pw->ptr;
243
159M
    byte *wlimit = pw->limit;
244
159M
    int status = 0;
245
159M
    int c;
246
247
159M
#define check_p(n)\
248
159M
  if ( p == rlimit ) { p -= n; goto out; }
249
159M
#define check_q(n)\
250
2.37G
  if ( q == wlimit ) { p -= n; status = 1; goto out; }
251
2.53G
    while (p < rlimit) {
252
2.53G
        c = *++p;
253
2.53G
        if (c == '\\' && !ss->from_string) {
254
39.0M
            check_p(1);
255
39.0M
            switch ((c = *++p)) {
256
23.7M
                case 'n':
257
23.7M
                    c = '\n';
258
23.7M
                    goto put;
259
422k
                case 'r':
260
422k
                    c = '\r';
261
422k
                    goto put;
262
631k
                case 't':
263
631k
                    c = '\t';
264
631k
                    goto put;
265
7.72k
                case 'b':
266
7.72k
                    c = '\b';
267
7.72k
                    goto put;
268
250k
                case 'f':
269
250k
                    c = '\f';
270
250k
                    goto put;
271
9.06M
                default:  /* ignore the \ */
272
34.1M
                  put:check_q(2);
273
34.1M
                    *++q = c;
274
34.1M
                    continue;
275
558k
                case char_CR: /* ignore, check for following \n */
276
558k
                    check_p(2);
277
557k
                    if (p[1] == char_EOL)
278
30.5k
                        p++;
279
557k
                    continue;
280
187k
                case char_EOL: /* ignore */
281
187k
                    continue;
282
2.39M
                case '0':
283
3.25M
                case '1':
284
3.26M
                case '2':
285
4.08M
                case '3':
286
4.09M
                case '4':
287
4.09M
                case '5':
288
4.10M
                case '6':
289
4.10M
                case '7':
290
4.10M
                    {
291
4.10M
                        int d;
292
293
4.10M
                        check_p(2);
294
4.10M
                        d = p[1];
295
4.10M
                        c -= '0';
296
4.10M
                        if (d >= '0' && d <= '7') {
297
3.22M
                            if (p + 1 == rlimit) {
298
565
                                p -= 2;
299
565
                                goto out;
300
565
                            }
301
3.22M
                            check_q(2);
302
3.22M
                            c = (c << 3) + d - '0';
303
3.22M
                            d = p[2];
304
3.22M
                            if (d >= '0' && d <= '7') {
305
3.19M
                                c = (c << 3) + d - '0';
306
3.19M
                                p += 2;
307
3.19M
                            } else
308
38.3k
                                p++;
309
3.22M
                        } else
310
4.10M
                            check_q(2);
311
4.10M
                        *++q = c;
312
4.10M
                        continue;
313
4.10M
                    }
314
39.0M
            }
315
39.0M
        } else
316
2.49G
            switch (c) {
317
5.33M
                case '(':
318
5.33M
                    check_q(1);
319
5.33M
                    ss->depth++;
320
5.33M
                    break;
321
162M
                case ')':
322
162M
                    if (ss->depth == 0) {
323
159M
                        status = EOFC;
324
159M
                        goto out;
325
159M
                    }
326
3.12M
                    check_q(1);
327
3.12M
                    ss->depth--;
328
3.12M
                    break;
329
3.63M
                case char_CR: /* convert to \n */
330
3.63M
                    check_p(1);
331
3.62M
                    check_q(1);
332
3.62M
                    if (p[1] == char_EOL)
333
141k
                        p++;
334
3.62M
                    *++q = '\n';
335
3.62M
                    continue;
336
915k
                case char_EOL:
337
915k
                    c = '\n';
338
                    /* fall through */
339
2.32G
                default:
340
2.32G
                    check_q(1);
341
2.32G
                    break;
342
2.49G
            }
343
2.33G
        *++q = c;
344
2.33G
    }
345
70.2k
#undef check_p
346
70.2k
#undef check_q
347
159M
  out:pr->ptr = p;
348
159M
    pw->ptr = q;
349
159M
    if (last && status == 0 && p != rlimit)
350
769
        status = ERRC;
351
159M
    return status;
352
159M
}
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
28.6M
{
377
28.6M
    const byte *p = pr->ptr;
378
28.6M
    const byte *rlimit = pr->limit;
379
28.6M
    byte *q = pw->ptr;
380
28.6M
    byte *wlimit = pw->limit;
381
28.6M
    byte *q0 = q;
382
28.6M
    byte val1 = (byte) * odd_digit;
383
28.6M
    byte val2;
384
28.6M
    uint rcount;
385
28.6M
    byte *flimit;
386
28.6M
    const byte *const decoder = scan_char_decoder;
387
28.6M
    int code = 0;
388
389
28.6M
    if (q >= wlimit)
390
1.51k
        return 1;
391
28.6M
    if (val1 <= 0xf)
392
28.8k
        goto d2;
393
29.3M
    do {
394
        /* No digits read */
395
29.3M
        if ((rcount = (rlimit - p) >> 1) != 0)
396
29.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
29.3M
            flimit = (rcount < wlimit - q ? q + rcount : wlimit);
400
162M
            while (1) {
401
162M
                if ((val1 = decoder[p[1]]) <= 0xf &&
402
156M
                    (val2 = decoder[p[2]]) <= 0xf) {
403
156M
                    p += 2;
404
156M
                    *++q = (val1 << 4) + val2;
405
156M
                    if (q < flimit)
406
133M
                        continue;
407
23.5M
                    if (q >= wlimit)
408
31.1k
                        goto px;
409
23.5M
                }
410
29.3M
                break;
411
162M
            }
412
29.3M
        }
413
        /* About to read the first digit */
414
32.6M
        while (1) {
415
32.6M
            if (p >= rlimit)
416
23.4M
                goto end1;
417
9.17M
            if ((val1 = decoder[*++p]) > 0xf) {
418
8.37M
                if (val1 == ctype_space) {
419
646k
                    switch (syntax) {
420
273k
                        case hex_ignore_garbage:
421
584k
                        case hex_ignore_whitespace:
422
584k
                            continue;
423
51.2k
                        case hex_ignore_leading_whitespace:
424
51.2k
                            if (q == q0 && *odd_digit < 0)
425
32.5k
                                continue;
426
                            /* pass through */
427
29.0k
                        case hex_break_on_whitespace:
428
29.0k
                            --p;
429
29.0k
                            code = 2;
430
29.0k
                            goto end1;
431
646k
                    }
432
7.73M
                } else if (syntax == hex_ignore_garbage)
433
2.72M
                    continue;
434
5.00M
                code = ERRC;
435
5.00M
                goto end1;
436
8.37M
            }
437
795k
            break;
438
9.17M
        }
439
824k
  d2:
440
        /* About to read the second hex digit of a pair */
441
3.63M
        while (1) {
442
3.63M
            if (p >= rlimit) {
443
78.2k
                *odd_digit = val1;
444
78.2k
                goto ended;
445
78.2k
            }
446
3.55M
            if ((val2 = decoder[*++p]) > 0xf) {
447
2.86M
                if (val2 == ctype_space)
448
346k
                    switch (syntax) {
449
241k
                        case hex_ignore_garbage:
450
309k
                        case hex_ignore_whitespace:
451
309k
                            continue;
452
29.4k
                        case hex_ignore_leading_whitespace:
453
29.4k
                            if (q == q0)
454
23.4k
                                continue;
455
                            /* pass through */
456
14.2k
                        case hex_break_on_whitespace:
457
14.2k
                            --p;
458
14.2k
                            *odd_digit = val1;
459
14.2k
                            code = 2;
460
14.2k
                            goto ended;
461
346k
                    }
462
2.52M
                if (syntax == hex_ignore_garbage)
463
2.47M
                    continue;
464
43.8k
                *odd_digit = val1;
465
43.8k
                code = ERRC;
466
43.8k
                goto ended;
467
2.52M
            }
468
687k
            break;
469
3.55M
        }
470
687k
        *++q = (val1 << 4) + val2;
471
687k
    } while (q < wlimit);
472
31.2k
  px:code = 1;
473
28.5M
  end1:*odd_digit = -1;
474
28.6M
  ended:pr->ptr = p;
475
28.6M
    pw->ptr = q;
476
28.6M
    return code;
477
28.5M
}