Coverage Report

Created: 2025-06-10 06:49

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