Coverage Report

Created: 2025-06-10 07:19

/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
85
{
85
85
    stream_AXD_state *const ss = (stream_AXD_state *) st;
86
87
85
    return s_AXD_init_inline(ss);
88
85
}
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
312k
{
95
312k
    stream_AXD_state *const ss = (stream_AXD_state *) st;
96
312k
    int code = s_hex_process(pr, pw, &ss->odd, hex_ignore_whitespace);
97
98
312k
    switch (code) {
99
363
        case 0:
100
363
            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
456
        case 1:
108
            /* We still need to read ahead and check for EOD. */
109
456
            for (; pr->ptr < pr->limit; pr->ptr++)
110
85
                if (scan_char_decoder[pr->ptr[1]] != ctype_space) {
111
85
                    if (pr->ptr[1] == '>') {
112
0
                        pr->ptr++;
113
0
                        goto eod;
114
0
                    }
115
85
                    return 1;
116
85
                }
117
371
            return 0;    /* still need to scan ahead */
118
0
        default:
119
0
            return code;
120
311k
        case ERRC:
121
311k
            ;
122
312k
    }
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
311k
    if (*pr->ptr != '>') { /* EOD */
130
73
        --(pr->ptr);
131
73
        return ERRC;
132
73
    }
133
311k
  eod:if (ss->odd >= 0) {
134
1.44k
        if (pw->ptr == pw->limit)
135
0
            return 1;
136
1.44k
        *++(pw->ptr) = ss->odd << 4;
137
1.44k
    }
138
311k
    return EOFC;
139
311k
}
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.73M
{
153
2.73M
    const byte *p = pr->ptr;
154
2.73M
    const byte *rlimit = pr->limit;
155
2.73M
    byte *q = pw->ptr;
156
2.73M
    byte *wlimit = pw->limit;
157
2.73M
    int status = 0;
158
159
    /* This doesn't have to be very efficient. */
160
65.3M
    while (p < rlimit) {
161
63.3M
        int c = *++p;
162
163
63.3M
        if (c < 32 || c >= 127) {
164
45.5M
            const char *pesc;
165
45.5M
            const char *const esc = "\n\r\t\b\f";
166
167
45.5M
            if (c < 32 && c != 0 && (pesc = strchr(esc, c)) != 0) {
168
379k
                if (wlimit - q < 2) {
169
638
                    --p;
170
638
                    status = 1;
171
638
                    break;
172
638
                }
173
378k
                *++q = '\\';
174
378k
                *++q = "nrtbf"[pesc - esc];
175
378k
                continue;
176
379k
            }
177
45.2M
            if (wlimit - q < 4) {
178
743k
                --p;
179
743k
                status = 1;
180
743k
                break;
181
743k
            }
182
44.4M
            q[1] = '\\';
183
44.4M
            q[2] = (c >> 6) + '0';
184
44.4M
            q[3] = ((c >> 3) & 7) + '0';
185
44.4M
            q[4] = (c & 7) + '0';
186
44.4M
            q += 4;
187
44.4M
            continue;
188
45.2M
        } else if (c == '(' || c == ')' || c == '\\') {
189
365k
            if (wlimit - q < 2) {
190
39
                --p;
191
39
                status = 1;
192
39
                break;
193
39
            }
194
365k
            *++q = '\\';
195
17.4M
        } else {
196
17.4M
            if (q == wlimit) {
197
24.7k
                --p;
198
24.7k
                status = 1;
199
24.7k
                break;
200
24.7k
            }
201
17.4M
        }
202
17.7M
        *++q = c;
203
17.7M
    }
204
2.73M
    if (last && status == 0) {
205
0
        if (q == wlimit)
206
0
            status = 1;
207
0
        else
208
0
            *++q = ')';
209
0
    }
210
2.73M
    pr->ptr = p;
211
2.73M
    pw->ptr = q;
212
2.73M
    return status;
213
2.73M
}
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.0M
{
239
10.0M
    stream_PSSD_state *const ss = (stream_PSSD_state *) st;
240
10.0M
    const byte *p = pr->ptr;
241
10.0M
    const byte *rlimit = pr->limit;
242
10.0M
    byte *q = pw->ptr;
243
10.0M
    byte *wlimit = pw->limit;
244
10.0M
    int status = 0;
245
10.0M
    int c;
246
247
10.0M
#define check_p(n)\
248
10.0M
  if ( p == rlimit ) { p -= n; goto out; }
249
10.0M
#define check_q(n)\
250
155M
  if ( q == wlimit ) { p -= n; status = 1; goto out; }
251
165M
    while (p < rlimit) {
252
165M
        c = *++p;
253
165M
        if (c == '\\' && !ss->from_string) {
254
2.59M
            check_p(1);
255
2.59M
            switch ((c = *++p)) {
256
1.51M
                case 'n':
257
1.51M
                    c = '\n';
258
1.51M
                    goto put;
259
15.0k
                case 'r':
260
15.0k
                    c = '\r';
261
15.0k
                    goto put;
262
39.8k
                case 't':
263
39.8k
                    c = '\t';
264
39.8k
                    goto put;
265
93
                case 'b':
266
93
                    c = '\b';
267
93
                    goto put;
268
10.0k
                case 'f':
269
10.0k
                    c = '\f';
270
10.0k
                    goto put;
271
733k
                default:  /* ignore the \ */
272
2.31M
                  put:check_q(2);
273
2.31M
                    *++q = c;
274
2.31M
                    continue;
275
19.0k
                case char_CR: /* ignore, check for following \n */
276
19.0k
                    check_p(2);
277
18.9k
                    if (p[1] == char_EOL)
278
340
                        p++;
279
18.9k
                    continue;
280
62.5k
                case char_EOL: /* ignore */
281
62.5k
                    continue;
282
144k
                case '0':
283
172k
                case '1':
284
173k
                case '2':
285
197k
                case '3':
286
197k
                case '4':
287
197k
                case '5':
288
198k
                case '6':
289
198k
                case '7':
290
198k
                    {
291
198k
                        int d;
292
293
198k
                        check_p(2);
294
198k
                        d = p[1];
295
198k
                        c -= '0';
296
198k
                        if (d >= '0' && d <= '7') {
297
170k
                            if (p + 1 == rlimit) {
298
14
                                p -= 2;
299
14
                                goto out;
300
14
                            }
301
170k
                            check_q(2);
302
170k
                            c = (c << 3) + d - '0';
303
170k
                            d = p[2];
304
170k
                            if (d >= '0' && d <= '7') {
305
167k
                                c = (c << 3) + d - '0';
306
167k
                                p += 2;
307
167k
                            } else
308
3.25k
                                p++;
309
170k
                        } else
310
198k
                            check_q(2);
311
198k
                        *++q = c;
312
198k
                        continue;
313
198k
                    }
314
2.59M
            }
315
2.59M
        } else
316
163M
            switch (c) {
317
468k
                case '(':
318
468k
                    check_q(1);
319
468k
                    ss->depth++;
320
468k
                    break;
321
10.2M
                case ')':
322
10.2M
                    if (ss->depth == 0) {
323
10.0M
                        status = EOFC;
324
10.0M
                        goto out;
325
10.0M
                    }
326
146k
                    check_q(1);
327
146k
                    ss->depth--;
328
146k
                    break;
329
406k
                case char_CR: /* convert to \n */
330
406k
                    check_p(1);
331
405k
                    check_q(1);
332
405k
                    if (p[1] == char_EOL)
333
1.61k
                        p++;
334
405k
                    *++q = '\n';
335
405k
                    continue;
336
49.2k
                case char_EOL:
337
49.2k
                    c = '\n';
338
                    /* fall through */
339
152M
                default:
340
152M
                    check_q(1);
341
152M
                    break;
342
163M
            }
343
152M
        *++q = c;
344
152M
    }
345
8.68k
#undef check_p
346
8.68k
#undef check_q
347
10.0M
  out:pr->ptr = p;
348
10.0M
    pw->ptr = q;
349
10.0M
    if (last && status == 0 && p != rlimit)
350
56
        status = ERRC;
351
10.0M
    return status;
352
10.0M
}
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.97M
{
377
1.97M
    const byte *p = pr->ptr;
378
1.97M
    const byte *rlimit = pr->limit;
379
1.97M
    byte *q = pw->ptr;
380
1.97M
    byte *wlimit = pw->limit;
381
1.97M
    byte *q0 = q;
382
1.97M
    byte val1 = (byte) * odd_digit;
383
1.97M
    byte val2;
384
1.97M
    uint rcount;
385
1.97M
    byte *flimit;
386
1.97M
    const byte *const decoder = scan_char_decoder;
387
1.97M
    int code = 0;
388
389
1.97M
    if (q >= wlimit)
390
26
        return 1;
391
1.97M
    if (val1 <= 0xf)
392
3.65k
        goto d2;
393
2.00M
    do {
394
        /* No digits read */
395
2.00M
        if ((rcount = (rlimit - p) >> 1) != 0)
396
2.00M
        {
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.00M
            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.39M
                        continue;
407
1.64M
                    if (q >= wlimit)
408
2.42k
                        goto px;
409
1.64M
                }
410
2.00M
                break;
411
10.4M
            }
412
2.00M
        }
413
        /* About to read the first digit */
414
2.26M
        while (1) {
415
2.26M
            if (p >= rlimit)
416
1.64M
                goto end1;
417
622k
            if ((val1 = decoder[*++p]) > 0xf) {
418
571k
                if (val1 == ctype_space) {
419
30.0k
                    switch (syntax) {
420
17.3k
                        case hex_ignore_garbage:
421
25.1k
                        case hex_ignore_whitespace:
422
25.1k
                            continue;
423
3.43k
                        case hex_ignore_leading_whitespace:
424
3.43k
                            if (q == q0 && *odd_digit < 0)
425
2.69k
                                continue;
426
                            /* pass through */
427
2.28k
                        case hex_break_on_whitespace:
428
2.28k
                            --p;
429
2.28k
                            code = 2;
430
2.28k
                            goto end1;
431
30.0k
                    }
432
541k
                } else if (syntax == hex_ignore_garbage)
433
229k
                    continue;
434
312k
                code = ERRC;
435
312k
                goto end1;
436
571k
            }
437
51.0k
            break;
438
622k
        }
439
54.7k
  d2:
440
        /* About to read the second hex digit of a pair */
441
270k
        while (1) {
442
270k
            if (p >= rlimit) {
443
6.90k
                *odd_digit = val1;
444
6.90k
                goto ended;
445
6.90k
            }
446
263k
            if ((val2 = decoder[*++p]) > 0xf) {
447
220k
                if (val2 == ctype_space)
448
16.8k
                    switch (syntax) {
449
10.0k
                        case hex_ignore_garbage:
450
12.6k
                        case hex_ignore_whitespace:
451
12.6k
                            continue;
452
2.95k
                        case hex_ignore_leading_whitespace:
453
2.95k
                            if (q == q0)
454
2.34k
                                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
16.8k
                    }
462
203k
                if (syntax == hex_ignore_garbage)
463
200k
                    continue;
464
3.09k
                *odd_digit = val1;
465
3.09k
                code = ERRC;
466
3.09k
                goto ended;
467
203k
            }
468
42.8k
            break;
469
263k
        }
470
42.8k
        *++q = (val1 << 4) + val2;
471
42.8k
    } while (q < wlimit);
472
2.43k
  px:code = 1;
473
1.95M
  end1:*odd_digit = -1;
474
1.97M
  ended:pr->ptr = p;
475
1.97M
    pw->ptr = q;
476
1.97M
    return code;
477
1.95M
}