Coverage Report

Created: 2025-06-10 07:26

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