Coverage Report

Created: 2025-11-16 07:40

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
10.0k
{
33
10.0k
    stream_AXE_state *const ss = (stream_AXE_state *) st;
34
35
10.0k
    return s_AXE_init_inline(ss);
36
10.0k
}
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.09M
{
43
2.09M
    stream_AXE_state *const ss = (stream_AXE_state *) st;
44
2.09M
    const byte *p = pr->ptr;
45
2.09M
    byte *q = pw->ptr;
46
2.09M
    int rcount = pr->limit - p;
47
2.09M
    int wcount = pw->limit - q;
48
2.09M
    int count;
49
2.09M
    int pos = ss->count;
50
2.09M
    const char *hex_digits = "0123456789ABCDEF";
51
2.09M
    int status = 0;
52
53
2.09M
    if (last && ss->EndOfData)
54
1.24M
        wcount--;   /* leave room for '>' */
55
2.09M
    wcount -= (wcount + pos * 2) / 64; /* leave room for \n */
56
2.09M
    wcount >>= 1;   /* 2 chars per input byte */
57
2.09M
    count = (wcount < rcount ? (status = 1, wcount) : rcount);
58
37.5M
    while (--count >= 0) {
59
35.4M
        *++q = hex_digits[*++p >> 4];
60
35.4M
        *++q = hex_digits[*p & 0xf];
61
35.4M
        if (!(++pos & 31) && (count != 0 || !last))
62
1.02M
            *++q = '\n';
63
35.4M
    }
64
2.09M
    if (last && status == 0 && ss->EndOfData)
65
1.24M
        *++q = '>';
66
2.09M
    pr->ptr = p;
67
2.09M
    pw->ptr = q;
68
2.09M
    ss->count = pos & 31;
69
2.09M
    return status;
70
2.09M
}
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
2.00k
{
85
2.00k
    stream_AXD_state *const ss = (stream_AXD_state *) st;
86
87
2.00k
    return s_AXD_init_inline(ss);
88
2.00k
}
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.36M
{
95
6.36M
    stream_AXD_state *const ss = (stream_AXD_state *) st;
96
6.36M
    int code = s_hex_process(pr, pw, &ss->odd, hex_ignore_whitespace);
97
98
6.36M
    switch (code) {
99
11.0k
        case 0:
100
11.0k
            if (ss->odd >= 0 && last) {
101
336
                if (pw->ptr == pw->limit)
102
0
                    return 1;
103
336
                *++(pw->ptr) = ss->odd << 4;
104
336
                ss->odd = -1;
105
336
            }
106
            /* falls through */
107
13.7k
        case 1:
108
            /* We still need to read ahead and check for EOD. */
109
13.8k
            for (; pr->ptr < pr->limit; pr->ptr++)
110
2.54k
                if (scan_char_decoder[pr->ptr[1]] != ctype_space) {
111
2.45k
                    if (pr->ptr[1] == '>') {
112
0
                        pr->ptr++;
113
0
                        goto eod;
114
0
                    }
115
2.45k
                    return 1;
116
2.45k
                }
117
11.2k
            return 0;    /* still need to scan ahead */
118
0
        default:
119
0
            return code;
120
6.34M
        case ERRC:
121
6.34M
            ;
122
6.36M
    }
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.34M
    if (*pr->ptr != '>') { /* EOD */
130
1.55k
        --(pr->ptr);
131
1.55k
        return ERRC;
132
1.55k
    }
133
6.34M
  eod:if (ss->odd >= 0) {
134
51.9k
        if (pw->ptr == pw->limit)
135
0
            return 1;
136
51.9k
        *++(pw->ptr) = ss->odd << 4;
137
51.9k
    }
138
6.34M
    return EOFC;
139
6.34M
}
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
24.3M
{
153
24.3M
    const byte *p = pr->ptr;
154
24.3M
    const byte *rlimit = pr->limit;
155
24.3M
    byte *q = pw->ptr;
156
24.3M
    byte *wlimit = pw->limit;
157
24.3M
    int status = 0;
158
159
    /* This doesn't have to be very efficient. */
160
514M
    while (p < rlimit) {
161
498M
        int c = *++p;
162
163
498M
        if (c < 32 || c >= 127) {
164
409M
            const char *pesc;
165
409M
            const char *const esc = "\n\r\t\b\f";
166
167
409M
            if (c < 32 && c != 0 && (pesc = strchr(esc, c)) != 0) {
168
1.39M
                if (wlimit - q < 2) {
169
7.01k
                    --p;
170
7.01k
                    status = 1;
171
7.01k
                    break;
172
7.01k
                }
173
1.38M
                *++q = '\\';
174
1.38M
                *++q = "nrtbf"[pesc - esc];
175
1.38M
                continue;
176
1.39M
            }
177
408M
            if (wlimit - q < 4) {
178
7.79M
                --p;
179
7.79M
                status = 1;
180
7.79M
                break;
181
7.79M
            }
182
400M
            q[1] = '\\';
183
400M
            q[2] = (c >> 6) + '0';
184
400M
            q[3] = ((c >> 3) & 7) + '0';
185
400M
            q[4] = (c & 7) + '0';
186
400M
            q += 4;
187
400M
            continue;
188
408M
        } else if (c == '(' || c == ')' || c == '\\') {
189
2.79M
            if (wlimit - q < 2) {
190
3.27k
                --p;
191
3.27k
                status = 1;
192
3.27k
                break;
193
3.27k
            }
194
2.78M
            *++q = '\\';
195
85.9M
        } else {
196
85.9M
            if (q == wlimit) {
197
158k
                --p;
198
158k
                status = 1;
199
158k
                break;
200
158k
            }
201
85.9M
        }
202
88.6M
        *++q = c;
203
88.6M
    }
204
24.3M
    if (last && status == 0) {
205
5.04M
        if (q == wlimit)
206
62
            status = 1;
207
5.04M
        else
208
5.04M
            *++q = ')';
209
5.04M
    }
210
24.3M
    pr->ptr = p;
211
24.3M
    pw->ptr = q;
212
24.3M
    return status;
213
24.3M
}
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
99.0k
{
228
99.0k
    stream_PSSD_state *const ss = (stream_PSSD_state *) st;
229
230
99.0k
    ss->from_string = false;
231
99.0k
    return s_PSSD_partially_init_inline(ss);
232
99.0k
}
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
203M
{
239
203M
    stream_PSSD_state *const ss = (stream_PSSD_state *) st;
240
203M
    const byte *p = pr->ptr;
241
203M
    const byte *rlimit = pr->limit;
242
203M
    byte *q = pw->ptr;
243
203M
    byte *wlimit = pw->limit;
244
203M
    int status = 0;
245
203M
    int c;
246
247
203M
#define check_p(n)\
248
203M
  if ( p == rlimit ) { p -= n; goto out; }
249
203M
#define check_q(n)\
250
3.04G
  if ( q == wlimit ) { p -= n; status = 1; goto out; }
251
3.25G
    while (p < rlimit) {
252
3.25G
        c = *++p;
253
3.25G
        if (c == '\\' && !ss->from_string) {
254
49.7M
            check_p(1);
255
49.6M
            switch ((c = *++p)) {
256
30.8M
                case 'n':
257
30.8M
                    c = '\n';
258
30.8M
                    goto put;
259
243k
                case 'r':
260
243k
                    c = '\r';
261
243k
                    goto put;
262
793k
                case 't':
263
793k
                    c = '\t';
264
793k
                    goto put;
265
10.3k
                case 'b':
266
10.3k
                    c = '\b';
267
10.3k
                    goto put;
268
316k
                case 'f':
269
316k
                    c = '\f';
270
316k
                    goto put;
271
11.3M
                default:  /* ignore the \ */
272
43.6M
                  put:check_q(2);
273
43.6M
                    *++q = c;
274
43.6M
                    continue;
275
589k
                case char_CR: /* ignore, check for following \n */
276
589k
                    check_p(2);
277
588k
                    if (p[1] == char_EOL)
278
75.4k
                        p++;
279
588k
                    continue;
280
218k
                case char_EOL: /* ignore */
281
218k
                    continue;
282
3.11M
                case '0':
283
4.17M
                case '1':
284
4.22M
                case '2':
285
5.25M
                case '3':
286
5.25M
                case '4':
287
5.26M
                case '5':
288
5.27M
                case '6':
289
5.27M
                case '7':
290
5.27M
                    {
291
5.27M
                        int d;
292
293
5.27M
                        check_p(2);
294
5.26M
                        d = p[1];
295
5.26M
                        c -= '0';
296
5.26M
                        if (d >= '0' && d <= '7') {
297
4.20M
                            if (p + 1 == rlimit) {
298
734
                                p -= 2;
299
734
                                goto out;
300
734
                            }
301
4.20M
                            check_q(2);
302
4.20M
                            c = (c << 3) + d - '0';
303
4.20M
                            d = p[2];
304
4.20M
                            if (d >= '0' && d <= '7') {
305
4.15M
                                c = (c << 3) + d - '0';
306
4.15M
                                p += 2;
307
4.15M
                            } else
308
50.4k
                                p++;
309
4.20M
                        } else
310
5.26M
                            check_q(2);
311
5.26M
                        *++q = c;
312
5.26M
                        continue;
313
5.26M
                    }
314
49.6M
            }
315
49.6M
        } else
316
3.20G
            switch (c) {
317
5.84M
                case '(':
318
5.84M
                    check_q(1);
319
5.84M
                    ss->depth++;
320
5.84M
                    break;
321
206M
                case ')':
322
206M
                    if (ss->depth == 0) {
323
202M
                        status = EOFC;
324
202M
                        goto out;
325
202M
                    }
326
3.59M
                    check_q(1);
327
3.59M
                    ss->depth--;
328
3.59M
                    break;
329
5.03M
                case char_CR: /* convert to \n */
330
5.03M
                    check_p(1);
331
5.03M
                    check_q(1);
332
5.03M
                    if (p[1] == char_EOL)
333
117k
                        p++;
334
5.03M
                    *++q = '\n';
335
5.03M
                    continue;
336
1.10M
                case char_EOL:
337
1.10M
                    c = '\n';
338
                    /* fall through */
339
2.98G
                default:
340
2.98G
                    check_q(1);
341
2.98G
                    break;
342
3.20G
            }
343
2.99G
        *++q = c;
344
2.99G
    }
345
96.1k
#undef check_p
346
96.1k
#undef check_q
347
203M
  out:pr->ptr = p;
348
203M
    pw->ptr = q;
349
203M
    if (last && status == 0 && p != rlimit)
350
941
        status = ERRC;
351
203M
    return status;
352
203M
}
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.9M
{
377
42.9M
    const byte *p = pr->ptr;
378
42.9M
    const byte *rlimit = pr->limit;
379
42.9M
    byte *q = pw->ptr;
380
42.9M
    byte *wlimit = pw->limit;
381
42.9M
    byte *q0 = q;
382
42.9M
    byte val1 = (byte) * odd_digit;
383
42.9M
    byte val2;
384
42.9M
    uint rcount;
385
42.9M
    byte *flimit;
386
42.9M
    const byte *const decoder = scan_char_decoder;
387
42.9M
    int code = 0;
388
389
42.9M
    if (q >= wlimit)
390
2.31k
        return 1;
391
42.9M
    if (val1 <= 0xf)
392
33.2k
        goto d2;
393
43.9M
    do {
394
        /* No digits read */
395
43.9M
        if ((rcount = (rlimit - p) >> 1) != 0)
396
43.9M
        {
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.9M
            flimit = (rcount < wlimit - q ? q + rcount : wlimit);
400
218M
            while (1) {
401
218M
                if ((val1 = decoder[p[1]]) <= 0xf &&
402
211M
                    (val2 = decoder[p[2]]) <= 0xf) {
403
211M
                    p += 2;
404
211M
                    *++q = (val1 << 4) + val2;
405
211M
                    if (q < flimit)
406
174M
                        continue;
407
36.4M
                    if (q >= wlimit)
408
43.7k
                        goto px;
409
36.4M
                }
410
43.8M
                break;
411
218M
            }
412
43.9M
        }
413
        /* About to read the first digit */
414
49.7M
        while (1) {
415
49.7M
            if (p >= rlimit)
416
36.3M
                goto end1;
417
13.3M
            if ((val1 = decoder[*++p]) > 0xf) {
418
12.1M
                if (val1 == ctype_space) {
419
1.32M
                    switch (syntax) {
420
419k
                        case hex_ignore_garbage:
421
1.24M
                        case hex_ignore_whitespace:
422
1.24M
                            continue;
423
74.0k
                        case hex_ignore_leading_whitespace:
424
74.0k
                            if (q == q0 && *odd_digit < 0)
425
43.8k
                                continue;
426
                            /* pass through */
427
40.4k
                        case hex_break_on_whitespace:
428
40.4k
                            --p;
429
40.4k
                            code = 2;
430
40.4k
                            goto end1;
431
1.32M
                    }
432
10.8M
                } else if (syntax == hex_ignore_garbage)
433
4.49M
                    continue;
434
6.31M
                code = ERRC;
435
6.31M
                goto end1;
436
12.1M
            }
437
1.17M
            break;
438
13.3M
        }
439
1.20M
  d2:
440
        /* About to read the second hex digit of a pair */
441
5.84M
        while (1) {
442
5.84M
            if (p >= rlimit) {
443
112k
                *odd_digit = val1;
444
112k
                goto ended;
445
112k
            }
446
5.73M
            if ((val2 = decoder[*++p]) > 0xf) {
447
4.71M
                if (val2 == ctype_space)
448
565k
                    switch (syntax) {
449
372k
                        case hex_ignore_garbage:
450
522k
                        case hex_ignore_whitespace:
451
522k
                            continue;
452
35.7k
                        case hex_ignore_leading_whitespace:
453
35.7k
                            if (q == q0)
454
26.4k
                                continue;
455
                            /* pass through */
456
17.1k
                        case hex_break_on_whitespace:
457
17.1k
                            --p;
458
17.1k
                            *odd_digit = val1;
459
17.1k
                            code = 2;
460
17.1k
                            goto ended;
461
565k
                    }
462
4.15M
                if (syntax == hex_ignore_garbage)
463
4.08M
                    continue;
464
63.4k
                *odd_digit = val1;
465
63.4k
                code = ERRC;
466
63.4k
                goto ended;
467
4.15M
            }
468
1.01M
            break;
469
5.73M
        }
470
1.01M
        *++q = (val1 << 4) + val2;
471
1.01M
    } while (q < wlimit);
472
43.9k
  px:code = 1;
473
42.7M
  end1:*odd_digit = -1;
474
42.9M
  ended:pr->ptr = p;
475
42.9M
    pw->ptr = q;
476
42.9M
    return code;
477
42.7M
}