Coverage Report

Created: 2025-06-10 06:59

/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
72
{
85
72
    stream_AXD_state *const ss = (stream_AXD_state *) st;
86
87
72
    return s_AXD_init_inline(ss);
88
72
}
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
334k
{
95
334k
    stream_AXD_state *const ss = (stream_AXD_state *) st;
96
334k
    int code = s_hex_process(pr, pw, &ss->odd, hex_ignore_whitespace);
97
98
334k
    switch (code) {
99
422
        case 0:
100
422
            if (ss->odd >= 0 && last) {
101
24
                if (pw->ptr == pw->limit)
102
0
                    return 1;
103
24
                *++(pw->ptr) = ss->odd << 4;
104
24
                ss->odd = -1;
105
24
            }
106
            /* falls through */
107
512
        case 1:
108
            /* We still need to read ahead and check for EOD. */
109
512
            for (; pr->ptr < pr->limit; pr->ptr++)
110
82
                if (scan_char_decoder[pr->ptr[1]] != ctype_space) {
111
82
                    if (pr->ptr[1] == '>') {
112
0
                        pr->ptr++;
113
0
                        goto eod;
114
0
                    }
115
82
                    return 1;
116
82
                }
117
430
            return 0;    /* still need to scan ahead */
118
0
        default:
119
0
            return code;
120
333k
        case ERRC:
121
333k
            ;
122
334k
    }
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
333k
    if (*pr->ptr != '>') { /* EOD */
130
58
        --(pr->ptr);
131
58
        return ERRC;
132
58
    }
133
333k
  eod:if (ss->odd >= 0) {
134
15.0k
        if (pw->ptr == pw->limit)
135
0
            return 1;
136
15.0k
        *++(pw->ptr) = ss->odd << 4;
137
15.0k
    }
138
333k
    return EOFC;
139
333k
}
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.34M
{
153
2.34M
    const byte *p = pr->ptr;
154
2.34M
    const byte *rlimit = pr->limit;
155
2.34M
    byte *q = pw->ptr;
156
2.34M
    byte *wlimit = pw->limit;
157
2.34M
    int status = 0;
158
159
    /* This doesn't have to be very efficient. */
160
63.2M
    while (p < rlimit) {
161
62.0M
        int c = *++p;
162
163
62.0M
        if (c < 32 || c >= 127) {
164
56.8M
            const char *pesc;
165
56.8M
            const char *const esc = "\n\r\t\b\f";
166
167
56.8M
            if (c < 32 && c != 0 && (pesc = strchr(esc, c)) != 0) {
168
28.5k
                if (wlimit - q < 2) {
169
364
                    --p;
170
364
                    status = 1;
171
364
                    break;
172
364
                }
173
28.1k
                *++q = '\\';
174
28.1k
                *++q = "nrtbf"[pesc - esc];
175
28.1k
                continue;
176
28.5k
            }
177
56.8M
            if (wlimit - q < 4) {
178
1.09M
                --p;
179
1.09M
                status = 1;
180
1.09M
                break;
181
1.09M
            }
182
55.7M
            q[1] = '\\';
183
55.7M
            q[2] = (c >> 6) + '0';
184
55.7M
            q[3] = ((c >> 3) & 7) + '0';
185
55.7M
            q[4] = (c & 7) + '0';
186
55.7M
            q += 4;
187
55.7M
            continue;
188
56.8M
        } else if (c == '(' || c == ')' || c == '\\') {
189
165k
            if (wlimit - q < 2) {
190
160
                --p;
191
160
                status = 1;
192
160
                break;
193
160
            }
194
165k
            *++q = '\\';
195
4.97M
        } else {
196
4.97M
            if (q == wlimit) {
197
20.1k
                --p;
198
20.1k
                status = 1;
199
20.1k
                break;
200
20.1k
            }
201
4.97M
        }
202
5.12M
        *++q = c;
203
5.12M
    }
204
2.34M
    if (last && status == 0) {
205
0
        if (q == wlimit)
206
0
            status = 1;
207
0
        else
208
0
            *++q = ')';
209
0
    }
210
2.34M
    pr->ptr = p;
211
2.34M
    pw->ptr = q;
212
2.34M
    return status;
213
2.34M
}
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
10.3M
{
239
10.3M
    stream_PSSD_state *const ss = (stream_PSSD_state *) st;
240
10.3M
    const byte *p = pr->ptr;
241
10.3M
    const byte *rlimit = pr->limit;
242
10.3M
    byte *q = pw->ptr;
243
10.3M
    byte *wlimit = pw->limit;
244
10.3M
    int status = 0;
245
10.3M
    int c;
246
247
10.3M
#define check_p(n)\
248
10.3M
  if ( p == rlimit ) { p -= n; goto out; }
249
10.3M
#define check_q(n)\
250
158M
  if ( q == wlimit ) { p -= n; status = 1; goto out; }
251
168M
    while (p < rlimit) {
252
168M
        c = *++p;
253
168M
        if (c == '\\' && !ss->from_string) {
254
2.35M
            check_p(1);
255
2.35M
            switch ((c = *++p)) {
256
1.56M
                case 'n':
257
1.56M
                    c = '\n';
258
1.56M
                    goto put;
259
14.3k
                case 'r':
260
14.3k
                    c = '\r';
261
14.3k
                    goto put;
262
41.3k
                case 't':
263
41.3k
                    c = '\t';
264
41.3k
                    goto put;
265
272
                case 'b':
266
272
                    c = '\b';
267
272
                    goto put;
268
10.4k
                case 'f':
269
10.4k
                    c = '\f';
270
10.4k
                    goto put;
271
402k
                default:  /* ignore the \ */
272
2.03M
                  put:check_q(2);
273
2.03M
                    *++q = c;
274
2.03M
                    continue;
275
1.50k
                case char_CR: /* ignore, check for following \n */
276
1.50k
                    check_p(2);
277
1.49k
                    if (p[1] == char_EOL)
278
1
                        p++;
279
1.49k
                    continue;
280
5.98k
                case char_EOL: /* ignore */
281
5.98k
                    continue;
282
151k
                case '0':
283
232k
                case '1':
284
233k
                case '2':
285
310k
                case '3':
286
310k
                case '4':
287
311k
                case '5':
288
311k
                case '6':
289
311k
                case '7':
290
311k
                    {
291
311k
                        int d;
292
293
311k
                        check_p(2);
294
311k
                        d = p[1];
295
311k
                        c -= '0';
296
311k
                        if (d >= '0' && d <= '7') {
297
227k
                            if (p + 1 == rlimit) {
298
37
                                p -= 2;
299
37
                                goto out;
300
37
                            }
301
227k
                            check_q(2);
302
227k
                            c = (c << 3) + d - '0';
303
227k
                            d = p[2];
304
227k
                            if (d >= '0' && d <= '7') {
305
226k
                                c = (c << 3) + d - '0';
306
226k
                                p += 2;
307
226k
                            } else
308
882
                                p++;
309
227k
                        } else
310
311k
                            check_q(2);
311
311k
                        *++q = c;
312
311k
                        continue;
313
311k
                    }
314
2.35M
            }
315
2.35M
        } else
316
166M
            switch (c) {
317
645k
                case '(':
318
645k
                    check_q(1);
319
645k
                    ss->depth++;
320
645k
                    break;
321
10.7M
                case ')':
322
10.7M
                    if (ss->depth == 0) {
323
10.3M
                        status = EOFC;
324
10.3M
                        goto out;
325
10.3M
                    }
326
373k
                    check_q(1);
327
373k
                    ss->depth--;
328
373k
                    break;
329
504k
                case char_CR: /* convert to \n */
330
504k
                    check_p(1);
331
504k
                    check_q(1);
332
504k
                    if (p[1] == char_EOL)
333
460
                        p++;
334
504k
                    *++q = '\n';
335
504k
                    continue;
336
18.0k
                case char_EOL:
337
18.0k
                    c = '\n';
338
                    /* fall through */
339
154M
                default:
340
154M
                    check_q(1);
341
154M
                    break;
342
166M
            }
343
155M
        *++q = c;
344
155M
    }
345
6.30k
#undef check_p
346
6.30k
#undef check_q
347
10.3M
  out:pr->ptr = p;
348
10.3M
    pw->ptr = q;
349
10.3M
    if (last && status == 0 && p != rlimit)
350
48
        status = ERRC;
351
10.3M
    return status;
352
10.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
1.92M
{
377
1.92M
    const byte *p = pr->ptr;
378
1.92M
    const byte *rlimit = pr->limit;
379
1.92M
    byte *q = pw->ptr;
380
1.92M
    byte *wlimit = pw->limit;
381
1.92M
    byte *q0 = q;
382
1.92M
    byte val1 = (byte) * odd_digit;
383
1.92M
    byte val2;
384
1.92M
    uint rcount;
385
1.92M
    byte *flimit;
386
1.92M
    const byte *const decoder = scan_char_decoder;
387
1.92M
    int code = 0;
388
389
1.92M
    if (q >= wlimit)
390
43
        return 1;
391
1.92M
    if (val1 <= 0xf)
392
2.83k
        goto d2;
393
1.99M
    do {
394
        /* No digits read */
395
1.99M
        if ((rcount = (rlimit - p) >> 1) != 0)
396
1.99M
        {
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
1.99M
            flimit = (rcount < wlimit - q ? q + rcount : wlimit);
400
10.4M
            while (1) {
401
10.4M
                if ((val1 = decoder[p[1]]) <= 0xf &&
402
10.4M
                    (val2 = decoder[p[2]]) <= 0xf) {
403
10.0M
                    p += 2;
404
10.0M
                    *++q = (val1 << 4) + val2;
405
10.0M
                    if (q < flimit)
406
8.45M
                        continue;
407
1.58M
                    if (q >= wlimit)
408
1.31k
                        goto px;
409
1.58M
                }
410
1.99M
                break;
411
10.4M
            }
412
1.99M
        }
413
        /* About to read the first digit */
414
2.37M
        while (1) {
415
2.37M
            if (p >= rlimit)
416
1.58M
                goto end1;
417
794k
            if ((val1 = decoder[*++p]) > 0xf) {
418
704k
                if (val1 == ctype_space) {
419
67.6k
                    switch (syntax) {
420
53.7k
                        case hex_ignore_garbage:
421
64.7k
                        case hex_ignore_whitespace:
422
64.7k
                            continue;
423
1.69k
                        case hex_ignore_leading_whitespace:
424
1.69k
                            if (q == q0 && *odd_digit < 0)
425
1.52k
                                continue;
426
                            /* pass through */
427
1.42k
                        case hex_break_on_whitespace:
428
1.42k
                            --p;
429
1.42k
                            code = 2;
430
1.42k
                            goto end1;
431
67.6k
                    }
432
636k
                } else if (syntax == hex_ignore_garbage)
433
316k
                    continue;
434
320k
                code = ERRC;
435
320k
                goto end1;
436
704k
            }
437
90.0k
            break;
438
794k
        }
439
92.8k
  d2:
440
        /* About to read the second hex digit of a pair */
441
445k
        while (1) {
442
445k
            if (p >= rlimit) {
443
3.97k
                *odd_digit = val1;
444
3.97k
                goto ended;
445
3.97k
            }
446
441k
            if ((val2 = decoder[*++p]) > 0xf) {
447
370k
                if (val2 == ctype_space)
448
54.7k
                    switch (syntax) {
449
47.4k
                        case hex_ignore_garbage:
450
52.6k
                        case hex_ignore_whitespace:
451
52.6k
                            continue;
452
1.29k
                        case hex_ignore_leading_whitespace:
453
1.29k
                            if (q == q0)
454
1.13k
                                continue;
455
                            /* pass through */
456
983
                        case hex_break_on_whitespace:
457
983
                            --p;
458
983
                            *odd_digit = val1;
459
983
                            code = 2;
460
983
                            goto ended;
461
54.7k
                    }
462
315k
                if (syntax == hex_ignore_garbage)
463
298k
                    continue;
464
16.6k
                *odd_digit = val1;
465
16.6k
                code = ERRC;
466
16.6k
                goto ended;
467
315k
            }
468
71.2k
            break;
469
441k
        }
470
71.2k
        *++q = (val1 << 4) + val2;
471
71.2k
    } while (q < wlimit);
472
1.38k
  px:code = 1;
473
1.90M
  end1:*odd_digit = -1;
474
1.92M
  ended:pr->ptr = p;
475
1.92M
    pw->ptr = q;
476
1.92M
    return code;
477
1.90M
}