Coverage Report

Created: 2025-06-10 07:27

/src/ghostpdl/base/sstring.c
Line
Count
Source (jump to first uncovered line)
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
0
{
33
0
    stream_AXE_state *const ss = (stream_AXE_state *) st;
34
35
0
    return s_AXE_init_inline(ss);
36
0
}
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
0
{
43
0
    stream_AXE_state *const ss = (stream_AXE_state *) st;
44
0
    const byte *p = pr->ptr;
45
0
    byte *q = pw->ptr;
46
0
    int rcount = pr->limit - p;
47
0
    int wcount = pw->limit - q;
48
0
    int count;
49
0
    int pos = ss->count;
50
0
    const char *hex_digits = "0123456789ABCDEF";
51
0
    int status = 0;
52
53
0
    if (last && ss->EndOfData)
54
0
        wcount--;   /* leave room for '>' */
55
0
    wcount -= (wcount + pos * 2) / 64; /* leave room for \n */
56
0
    wcount >>= 1;   /* 2 chars per input byte */
57
0
    count = (wcount < rcount ? (status = 1, wcount) : rcount);
58
0
    while (--count >= 0) {
59
0
        *++q = hex_digits[*++p >> 4];
60
0
        *++q = hex_digits[*p & 0xf];
61
0
        if (!(++pos & 31) && (count != 0 || !last))
62
0
            *++q = '\n';
63
0
    }
64
0
    if (last && status == 0 && ss->EndOfData)
65
0
        *++q = '>';
66
0
    pr->ptr = p;
67
0
    pw->ptr = q;
68
0
    ss->count = pos & 31;
69
0
    return status;
70
0
}
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
97
{
85
97
    stream_AXD_state *const ss = (stream_AXD_state *) st;
86
87
97
    return s_AXD_init_inline(ss);
88
97
}
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
361k
{
95
361k
    stream_AXD_state *const ss = (stream_AXD_state *) st;
96
361k
    int code = s_hex_process(pr, pw, &ss->odd, hex_ignore_whitespace);
97
98
361k
    switch (code) {
99
719
        case 0:
100
719
            if (ss->odd >= 0 && last) {
101
28
                if (pw->ptr == pw->limit)
102
0
                    return 1;
103
28
                *++(pw->ptr) = ss->odd << 4;
104
28
                ss->odd = -1;
105
28
            }
106
            /* falls through */
107
861
        case 1:
108
            /* We still need to read ahead and check for EOD. */
109
879
            for (; pr->ptr < pr->limit; pr->ptr++)
110
149
                if (scan_char_decoder[pr->ptr[1]] != ctype_space) {
111
131
                    if (pr->ptr[1] == '>') {
112
0
                        pr->ptr++;
113
0
                        goto eod;
114
0
                    }
115
131
                    return 1;
116
131
                }
117
730
            return 0;    /* still need to scan ahead */
118
0
        default:
119
0
            return code;
120
360k
        case ERRC:
121
360k
            ;
122
361k
    }
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
360k
    if (*pr->ptr != '>') { /* EOD */
130
76
        --(pr->ptr);
131
76
        return ERRC;
132
76
    }
133
360k
  eod:if (ss->odd >= 0) {
134
6.58k
        if (pw->ptr == pw->limit)
135
0
            return 1;
136
6.58k
        *++(pw->ptr) = ss->odd << 4;
137
6.58k
    }
138
360k
    return EOFC;
139
360k
}
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
3.16M
{
153
3.16M
    const byte *p = pr->ptr;
154
3.16M
    const byte *rlimit = pr->limit;
155
3.16M
    byte *q = pw->ptr;
156
3.16M
    byte *wlimit = pw->limit;
157
3.16M
    int status = 0;
158
159
    /* This doesn't have to be very efficient. */
160
53.9M
    while (p < rlimit) {
161
51.2M
        int c = *++p;
162
163
51.2M
        if (c < 32 || c >= 127) {
164
35.2M
            const char *pesc;
165
35.2M
            const char *const esc = "\n\r\t\b\f";
166
167
35.2M
            if (c < 32 && c != 0 && (pesc = strchr(esc, c)) != 0) {
168
213k
                if (wlimit - q < 2) {
169
310
                    --p;
170
310
                    status = 1;
171
310
                    break;
172
310
                }
173
212k
                *++q = '\\';
174
212k
                *++q = "nrtbf"[pesc - esc];
175
212k
                continue;
176
213k
            }
177
35.0M
            if (wlimit - q < 4) {
178
439k
                --p;
179
439k
                status = 1;
180
439k
                break;
181
439k
            }
182
34.5M
            q[1] = '\\';
183
34.5M
            q[2] = (c >> 6) + '0';
184
34.5M
            q[3] = ((c >> 3) & 7) + '0';
185
34.5M
            q[4] = (c & 7) + '0';
186
34.5M
            q += 4;
187
34.5M
            continue;
188
35.0M
        } else if (c == '(' || c == ')' || c == '\\') {
189
382k
            if (wlimit - q < 2) {
190
71
                --p;
191
71
                status = 1;
192
71
                break;
193
71
            }
194
382k
            *++q = '\\';
195
15.6M
        } else {
196
15.6M
            if (q == wlimit) {
197
22.5k
                --p;
198
22.5k
                status = 1;
199
22.5k
                break;
200
22.5k
            }
201
15.6M
        }
202
15.9M
        *++q = c;
203
15.9M
    }
204
3.16M
    if (last && status == 0) {
205
0
        if (q == wlimit)
206
0
            status = 1;
207
0
        else
208
0
            *++q = ')';
209
0
    }
210
3.16M
    pr->ptr = p;
211
3.16M
    pw->ptr = q;
212
3.16M
    return status;
213
3.16M
}
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
0
{
228
0
    stream_PSSD_state *const ss = (stream_PSSD_state *) st;
229
230
0
    ss->from_string = false;
231
0
    return s_PSSD_partially_init_inline(ss);
232
0
}
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
11.3M
{
239
11.3M
    stream_PSSD_state *const ss = (stream_PSSD_state *) st;
240
11.3M
    const byte *p = pr->ptr;
241
11.3M
    const byte *rlimit = pr->limit;
242
11.3M
    byte *q = pw->ptr;
243
11.3M
    byte *wlimit = pw->limit;
244
11.3M
    int status = 0;
245
11.3M
    int c;
246
247
11.3M
#define check_p(n)\
248
11.3M
  if ( p == rlimit ) { p -= n; goto out; }
249
11.3M
#define check_q(n)\
250
174M
  if ( q == wlimit ) { p -= n; status = 1; goto out; }
251
185M
    while (p < rlimit) {
252
185M
        c = *++p;
253
185M
        if (c == '\\' && !ss->from_string) {
254
2.55M
            check_p(1);
255
2.55M
            switch ((c = *++p)) {
256
1.71M
                case 'n':
257
1.71M
                    c = '\n';
258
1.71M
                    goto put;
259
16.2k
                case 'r':
260
16.2k
                    c = '\r';
261
16.2k
                    goto put;
262
45.5k
                case 't':
263
45.5k
                    c = '\t';
264
45.5k
                    goto put;
265
4.47k
                case 'b':
266
4.47k
                    c = '\b';
267
4.47k
                    goto put;
268
11.1k
                case 'f':
269
11.1k
                    c = '\f';
270
11.1k
                    goto put;
271
471k
                default:  /* ignore the \ */
272
2.26M
                  put:check_q(2);
273
2.26M
                    *++q = c;
274
2.26M
                    continue;
275
126k
                case char_CR: /* ignore, check for following \n */
276
126k
                    check_p(2);
277
126k
                    if (p[1] == char_EOL)
278
6.63k
                        p++;
279
126k
                    continue;
280
2.20k
                case char_EOL: /* ignore */
281
2.20k
                    continue;
282
165k
                case '0':
283
165k
                case '1':
284
165k
                case '2':
285
166k
                case '3':
286
166k
                case '4':
287
167k
                case '5':
288
168k
                case '6':
289
168k
                case '7':
290
168k
                    {
291
168k
                        int d;
292
293
168k
                        check_p(2);
294
168k
                        d = p[1];
295
168k
                        c -= '0';
296
168k
                        if (d >= '0' && d <= '7') {
297
165k
                            if (p + 1 == rlimit) {
298
5
                                p -= 2;
299
5
                                goto out;
300
5
                            }
301
165k
                            check_q(2);
302
165k
                            c = (c << 3) + d - '0';
303
165k
                            d = p[2];
304
165k
                            if (d >= '0' && d <= '7') {
305
164k
                                c = (c << 3) + d - '0';
306
164k
                                p += 2;
307
164k
                            } else
308
1.24k
                                p++;
309
165k
                        } else
310
168k
                            check_q(2);
311
168k
                        *++q = c;
312
168k
                        continue;
313
168k
                    }
314
2.55M
            }
315
2.55M
        } else
316
183M
            switch (c) {
317
1.11M
                case '(':
318
1.11M
                    check_q(1);
319
1.11M
                    ss->depth++;
320
1.11M
                    break;
321
11.3M
                case ')':
322
11.3M
                    if (ss->depth == 0) {
323
11.3M
                        status = EOFC;
324
11.3M
                        goto out;
325
11.3M
                    }
326
30.8k
                    check_q(1);
327
30.8k
                    ss->depth--;
328
30.8k
                    break;
329
530k
                case char_CR: /* convert to \n */
330
530k
                    check_p(1);
331
529k
                    check_q(1);
332
529k
                    if (p[1] == char_EOL)
333
5.25k
                        p++;
334
529k
                    *++q = '\n';
335
529k
                    continue;
336
21.4k
                case char_EOL:
337
21.4k
                    c = '\n';
338
                    /* fall through */
339
170M
                default:
340
170M
                    check_q(1);
341
170M
                    break;
342
183M
            }
343
171M
        *++q = c;
344
171M
    }
345
8.03k
#undef check_p
346
8.03k
#undef check_q
347
11.3M
  out:pr->ptr = p;
348
11.3M
    pw->ptr = q;
349
11.3M
    if (last && status == 0 && p != rlimit)
350
66
        status = ERRC;
351
11.3M
    return status;
352
11.3M
}
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
2.34M
{
377
2.34M
    const byte *p = pr->ptr;
378
2.34M
    const byte *rlimit = pr->limit;
379
2.34M
    byte *q = pw->ptr;
380
2.34M
    byte *wlimit = pw->limit;
381
2.34M
    byte *q0 = q;
382
2.34M
    byte val1 = (byte) * odd_digit;
383
2.34M
    byte val2;
384
2.34M
    uint rcount;
385
2.34M
    byte *flimit;
386
2.34M
    const byte *const decoder = scan_char_decoder;
387
2.34M
    int code = 0;
388
389
2.34M
    if (q >= wlimit)
390
71
        return 1;
391
2.34M
    if (val1 <= 0xf)
392
3.82k
        goto d2;
393
2.41M
    do {
394
        /* No digits read */
395
2.41M
        if ((rcount = (rlimit - p) >> 1) != 0)
396
2.41M
        {
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
2.41M
            flimit = (rcount < wlimit - q ? q + rcount : wlimit);
400
12.2M
            while (1) {
401
12.2M
                if ((val1 = decoder[p[1]]) <= 0xf &&
402
12.2M
                    (val2 = decoder[p[2]]) <= 0xf) {
403
11.7M
                    p += 2;
404
11.7M
                    *++q = (val1 << 4) + val2;
405
11.7M
                    if (q < flimit)
406
9.81M
                        continue;
407
1.97M
                    if (q >= wlimit)
408
2.76k
                        goto px;
409
1.97M
                }
410
2.41M
                break;
411
12.2M
            }
412
2.41M
        }
413
        /* About to read the first digit */
414
2.76M
        while (1) {
415
2.76M
            if (p >= rlimit)
416
1.96M
                goto end1;
417
805k
            if ((val1 = decoder[*++p]) > 0xf) {
418
717k
                if (val1 == ctype_space) {
419
50.1k
                    switch (syntax) {
420
16.3k
                        case hex_ignore_garbage:
421
39.7k
                        case hex_ignore_whitespace:
422
39.7k
                            continue;
423
8.97k
                        case hex_ignore_leading_whitespace:
424
8.97k
                            if (q == q0 && *odd_digit < 0)
425
5.40k
                                continue;
426
                            /* pass through */
427
4.99k
                        case hex_break_on_whitespace:
428
4.99k
                            --p;
429
4.99k
                            code = 2;
430
4.99k
                            goto end1;
431
50.1k
                    }
432
667k
                } else if (syntax == hex_ignore_garbage)
433
311k
                    continue;
434
355k
                code = ERRC;
435
355k
                goto end1;
436
717k
            }
437
87.5k
            break;
438
805k
        }
439
91.4k
  d2:
440
        /* About to read the second hex digit of a pair */
441
384k
        while (1) {
442
384k
            if (p >= rlimit) {
443
10.5k
                *odd_digit = val1;
444
10.5k
                goto ended;
445
10.5k
            }
446
373k
            if ((val2 = decoder[*++p]) > 0xf) {
447
302k
                if (val2 == ctype_space)
448
22.0k
                    switch (syntax) {
449
11.1k
                        case hex_ignore_garbage:
450
17.9k
                        case hex_ignore_whitespace:
451
17.9k
                            continue;
452
3.02k
                        case hex_ignore_leading_whitespace:
453
3.02k
                            if (q == q0)
454
2.22k
                                continue;
455
                            /* pass through */
456
1.87k
                        case hex_break_on_whitespace:
457
1.87k
                            --p;
458
1.87k
                            *odd_digit = val1;
459
1.87k
                            code = 2;
460
1.87k
                            goto ended;
461
22.0k
                    }
462
280k
                if (syntax == hex_ignore_garbage)
463
272k
                    continue;
464
8.25k
                *odd_digit = val1;
465
8.25k
                code = ERRC;
466
8.25k
                goto ended;
467
280k
            }
468
70.7k
            break;
469
373k
        }
470
70.7k
        *++q = (val1 << 4) + val2;
471
70.7k
    } while (q < wlimit);
472
2.76k
  px:code = 1;
473
2.32M
  end1:*odd_digit = -1;
474
2.34M
  ended:pr->ptr = p;
475
2.34M
    pw->ptr = q;
476
2.34M
    return code;
477
2.32M
}