Coverage Report

Created: 2026-04-01 07:17

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.75k
{
33
8.75k
    stream_AXE_state *const ss = (stream_AXE_state *) st;
34
35
8.75k
    return s_AXE_init_inline(ss);
36
8.75k
}
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.43M
{
43
1.43M
    stream_AXE_state *const ss = (stream_AXE_state *) st;
44
1.43M
    const byte *p = pr->ptr;
45
1.43M
    byte *q = pw->ptr;
46
1.43M
    int rcount = pr->limit - p;
47
1.43M
    int wcount = pw->limit - q;
48
1.43M
    int count;
49
1.43M
    int pos = ss->count;
50
1.43M
    const char *hex_digits = "0123456789ABCDEF";
51
1.43M
    int status = 0;
52
53
1.43M
    if (last && ss->EndOfData)
54
811k
        wcount--;   /* leave room for '>' */
55
1.43M
    wcount -= (wcount + pos * 2) / 64; /* leave room for \n */
56
1.43M
    wcount >>= 1;   /* 2 chars per input byte */
57
1.43M
    count = (wcount < rcount ? (status = 1, wcount) : rcount);
58
27.5M
    while (--count >= 0) {
59
26.0M
        *++q = hex_digits[*++p >> 4];
60
26.0M
        *++q = hex_digits[*p & 0xf];
61
26.0M
        if (!(++pos & 31) && (count != 0 || !last))
62
755k
            *++q = '\n';
63
26.0M
    }
64
1.43M
    if (last && status == 0 && ss->EndOfData)
65
810k
        *++q = '>';
66
1.43M
    pr->ptr = p;
67
1.43M
    pw->ptr = q;
68
1.43M
    ss->count = pos & 31;
69
1.43M
    return status;
70
1.43M
}
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.64k
{
85
1.64k
    stream_AXD_state *const ss = (stream_AXD_state *) st;
86
87
1.64k
    return s_AXD_init_inline(ss);
88
1.64k
}
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.97M
{
95
5.97M
    stream_AXD_state *const ss = (stream_AXD_state *) st;
96
5.97M
    int code = s_hex_process(pr, pw, &ss->odd, hex_ignore_whitespace);
97
98
5.97M
    switch (code) {
99
9.01k
        case 0:
100
9.01k
            if (ss->odd >= 0 && last) {
101
396
                if (pw->ptr == pw->limit)
102
0
                    return 1;
103
396
                *++(pw->ptr) = ss->odd << 4;
104
396
                ss->odd = -1;
105
396
            }
106
            /* falls through */
107
11.2k
        case 1:
108
            /* We still need to read ahead and check for EOD. */
109
11.3k
            for (; pr->ptr < pr->limit; pr->ptr++)
110
2.15k
                if (scan_char_decoder[pr->ptr[1]] != ctype_space) {
111
2.08k
                    if (pr->ptr[1] == '>') {
112
0
                        pr->ptr++;
113
0
                        goto eod;
114
0
                    }
115
2.08k
                    return 1;
116
2.08k
                }
117
9.21k
            return 0;    /* still need to scan ahead */
118
0
        default:
119
0
            return code;
120
5.96M
        case ERRC:
121
5.96M
            ;
122
5.97M
    }
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.96M
    if (*pr->ptr != '>') { /* EOD */
130
1.27k
        --(pr->ptr);
131
1.27k
        return ERRC;
132
1.27k
    }
133
5.96M
  eod:if (ss->odd >= 0) {
134
46.1k
        if (pw->ptr == pw->limit)
135
0
            return 1;
136
46.1k
        *++(pw->ptr) = ss->odd << 4;
137
46.1k
    }
138
5.96M
    return EOFC;
139
5.96M
}
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
4.76M
{
153
4.76M
    const byte *p = pr->ptr;
154
4.76M
    const byte *rlimit = pr->limit;
155
4.76M
    byte *q = pw->ptr;
156
4.76M
    byte *wlimit = pw->limit;
157
4.76M
    int status = 0;
158
159
    /* This doesn't have to be very efficient. */
160
45.8M
    while (p < rlimit) {
161
41.4M
        int c = *++p;
162
163
41.4M
        if (c < 32 || c >= 127) {
164
21.3M
            const char *pesc;
165
21.3M
            const char *const esc = "\n\r\t\b\f";
166
167
21.3M
            if (c < 32 && c != 0 && (pesc = strchr(esc, c)) != 0) {
168
497k
                if (wlimit - q < 2) {
169
7.25k
                    --p;
170
7.25k
                    status = 1;
171
7.25k
                    break;
172
7.25k
                }
173
490k
                *++q = '\\';
174
490k
                *++q = "nrtbf"[pesc - esc];
175
490k
                continue;
176
497k
            }
177
20.8M
            if (wlimit - q < 4) {
178
244k
                --p;
179
244k
                status = 1;
180
244k
                break;
181
244k
            }
182
20.5M
            q[1] = '\\';
183
20.5M
            q[2] = (c >> 6) + '0';
184
20.5M
            q[3] = ((c >> 3) & 7) + '0';
185
20.5M
            q[4] = (c & 7) + '0';
186
20.5M
            q += 4;
187
20.5M
            continue;
188
20.8M
        } else if (c == '(' || c == ')' || c == '\\') {
189
1.11M
            if (wlimit - q < 2) {
190
3.41k
                --p;
191
3.41k
                status = 1;
192
3.41k
                break;
193
3.41k
            }
194
1.11M
            *++q = '\\';
195
18.9M
        } else {
196
18.9M
            if (q == wlimit) {
197
31.2k
                --p;
198
31.2k
                status = 1;
199
31.2k
                break;
200
31.2k
            }
201
18.9M
        }
202
20.0M
        *++q = c;
203
20.0M
    }
204
4.76M
    if (last && status == 0) {
205
1.99M
        if (q == wlimit)
206
827
            status = 1;
207
1.99M
        else
208
1.99M
            *++q = ')';
209
1.99M
    }
210
4.76M
    pr->ptr = p;
211
4.76M
    pw->ptr = q;
212
4.76M
    return status;
213
4.76M
}
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
77.3k
{
228
77.3k
    stream_PSSD_state *const ss = (stream_PSSD_state *) st;
229
230
77.3k
    ss->from_string = false;
231
77.3k
    return s_PSSD_partially_init_inline(ss);
232
77.3k
}
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
190M
{
239
190M
    stream_PSSD_state *const ss = (stream_PSSD_state *) st;
240
190M
    const byte *p = pr->ptr;
241
190M
    const byte *rlimit = pr->limit;
242
190M
    byte *q = pw->ptr;
243
190M
    byte *wlimit = pw->limit;
244
190M
    int status = 0;
245
190M
    int c;
246
247
190M
#define check_p(n)\
248
190M
  if ( p == rlimit ) { p -= n; goto out; }
249
190M
#define check_q(n)\
250
2.84G
  if ( q == wlimit ) { p -= n; status = 1; goto out; }
251
3.03G
    while (p < rlimit) {
252
3.03G
        c = *++p;
253
3.03G
        if (c == '\\' && !ss->from_string) {
254
46.2M
            check_p(1);
255
46.2M
            switch ((c = *++p)) {
256
29.0M
                case 'n':
257
29.0M
                    c = '\n';
258
29.0M
                    goto put;
259
215k
                case 'r':
260
215k
                    c = '\r';
261
215k
                    goto put;
262
744k
                case 't':
263
744k
                    c = '\t';
264
744k
                    goto put;
265
7.60k
                case 'b':
266
7.60k
                    c = '\b';
267
7.60k
                    goto put;
268
284k
                case 'f':
269
284k
                    c = '\f';
270
284k
                    goto put;
271
10.0M
                default:  /* ignore the \ */
272
40.3M
                  put:check_q(2);
273
40.3M
                    *++q = c;
274
40.3M
                    continue;
275
575k
                case char_CR: /* ignore, check for following \n */
276
575k
                    check_p(2);
277
574k
                    if (p[1] == char_EOL)
278
30.5k
                        p++;
279
574k
                    continue;
280
231k
                case char_EOL: /* ignore */
281
231k
                    continue;
282
2.89M
                case '0':
283
3.94M
                case '1':
284
3.99M
                case '2':
285
5.03M
                case '3':
286
5.03M
                case '4':
287
5.04M
                case '5':
288
5.05M
                case '6':
289
5.05M
                case '7':
290
5.05M
                    {
291
5.05M
                        int d;
292
293
5.05M
                        check_p(2);
294
5.04M
                        d = p[1];
295
5.04M
                        c -= '0';
296
5.04M
                        if (d >= '0' && d <= '7') {
297
3.97M
                            if (p + 1 == rlimit) {
298
730
                                p -= 2;
299
730
                                goto out;
300
730
                            }
301
3.97M
                            check_q(2);
302
3.97M
                            c = (c << 3) + d - '0';
303
3.97M
                            d = p[2];
304
3.97M
                            if (d >= '0' && d <= '7') {
305
3.92M
                                c = (c << 3) + d - '0';
306
3.92M
                                p += 2;
307
3.92M
                            } else
308
48.6k
                                p++;
309
3.97M
                        } else
310
5.04M
                            check_q(2);
311
5.04M
                        *++q = c;
312
5.04M
                        continue;
313
5.04M
                    }
314
46.2M
            }
315
46.2M
        } else
316
2.98G
            switch (c) {
317
5.31M
                case '(':
318
5.31M
                    check_q(1);
319
5.31M
                    ss->depth++;
320
5.31M
                    break;
321
193M
                case ')':
322
193M
                    if (ss->depth == 0) {
323
190M
                        status = EOFC;
324
190M
                        goto out;
325
190M
                    }
326
3.42M
                    check_q(1);
327
3.42M
                    ss->depth--;
328
3.42M
                    break;
329
3.89M
                case char_CR: /* convert to \n */
330
3.89M
                    check_p(1);
331
3.89M
                    check_q(1);
332
3.89M
                    if (p[1] == char_EOL)
333
112k
                        p++;
334
3.89M
                    *++q = '\n';
335
3.89M
                    continue;
336
840k
                case char_EOL:
337
840k
                    c = '\n';
338
                    /* fall through */
339
2.78G
                default:
340
2.78G
                    check_q(1);
341
2.78G
                    break;
342
2.98G
            }
343
2.79G
        *++q = c;
344
2.79G
    }
345
427k
#undef check_p
346
427k
#undef check_q
347
190M
  out:pr->ptr = p;
348
190M
    pw->ptr = q;
349
190M
    if (last && status == 0 && p != rlimit)
350
1.04k
        status = ERRC;
351
190M
    return status;
352
190M
}
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
33.7M
{
377
33.7M
    const byte *p = pr->ptr;
378
33.7M
    const byte *rlimit = pr->limit;
379
33.7M
    byte *q = pw->ptr;
380
33.7M
    byte *wlimit = pw->limit;
381
33.7M
    byte *q0 = q;
382
33.7M
    byte val1 = (byte) * odd_digit;
383
33.7M
    byte val2;
384
33.7M
    uint rcount;
385
33.7M
    byte *flimit;
386
33.7M
    const byte *const decoder = scan_char_decoder;
387
33.7M
    int code = 0;
388
389
33.7M
    if (q >= wlimit)
390
1.82k
        return 1;
391
33.7M
    if (val1 <= 0xf)
392
32.7k
        goto d2;
393
34.4M
    do {
394
        /* No digits read */
395
34.4M
        if ((rcount = (rlimit - p) >> 1) != 0)
396
34.4M
        {
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
34.4M
            flimit = (rcount < wlimit - q ? q + rcount : wlimit);
400
191M
            while (1) {
401
191M
                if ((val1 = decoder[p[1]]) <= 0xf &&
402
185M
                    (val2 = decoder[p[2]]) <= 0xf) {
403
185M
                    p += 2;
404
185M
                    *++q = (val1 << 4) + val2;
405
185M
                    if (q < flimit)
406
157M
                        continue;
407
27.6M
                    if (q >= wlimit)
408
38.7k
                        goto px;
409
27.6M
                }
410
34.4M
                break;
411
191M
            }
412
34.4M
        }
413
        /* About to read the first digit */
414
38.1M
        while (1) {
415
38.1M
            if (p >= rlimit)
416
27.5M
                goto end1;
417
10.6M
            if ((val1 = decoder[*++p]) > 0xf) {
418
9.70M
                if (val1 == ctype_space) {
419
777k
                    switch (syntax) {
420
290k
                        case hex_ignore_garbage:
421
711k
                        case hex_ignore_whitespace:
422
711k
                            continue;
423
54.3k
                        case hex_ignore_leading_whitespace:
424
54.3k
                            if (q == q0 && *odd_digit < 0)
425
34.8k
                                continue;
426
                            /* pass through */
427
31.2k
                        case hex_break_on_whitespace:
428
31.2k
                            --p;
429
31.2k
                            code = 2;
430
31.2k
                            goto end1;
431
777k
                    }
432
8.92M
                } else if (syntax == hex_ignore_garbage)
433
2.99M
                    continue;
434
5.93M
                code = ERRC;
435
5.93M
                goto end1;
436
9.70M
            }
437
922k
            break;
438
10.6M
        }
439
954k
  d2:
440
        /* About to read the second hex digit of a pair */
441
4.08M
        while (1) {
442
4.08M
            if (p >= rlimit) {
443
93.9k
                *odd_digit = val1;
444
93.9k
                goto ended;
445
93.9k
            }
446
3.98M
            if ((val2 = decoder[*++p]) > 0xf) {
447
3.20M
                if (val2 == ctype_space)
448
423k
                    switch (syntax) {
449
260k
                        case hex_ignore_garbage:
450
382k
                        case hex_ignore_whitespace:
451
382k
                            continue;
452
31.7k
                        case hex_ignore_leading_whitespace:
453
31.7k
                            if (q == q0)
454
25.0k
                                continue;
455
                            /* pass through */
456
15.8k
                        case hex_break_on_whitespace:
457
15.8k
                            --p;
458
15.8k
                            *odd_digit = val1;
459
15.8k
                            code = 2;
460
15.8k
                            goto ended;
461
423k
                    }
462
2.77M
                if (syntax == hex_ignore_garbage)
463
2.71M
                    continue;
464
59.3k
                *odd_digit = val1;
465
59.3k
                code = ERRC;
466
59.3k
                goto ended;
467
2.77M
            }
468
785k
            break;
469
3.98M
        }
470
785k
        *++q = (val1 << 4) + val2;
471
785k
    } while (q < wlimit);
472
38.8k
  px:code = 1;
473
33.5M
  end1:*odd_digit = -1;
474
33.7M
  ended:pr->ptr = p;
475
33.7M
    pw->ptr = q;
476
33.7M
    return code;
477
33.5M
}