Coverage Report

Created: 2022-10-31 07:00

/src/ghostpdl/base/sstring.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2022 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.,  1305 Grant Avenue - Suite 200, Novato,
13
   CA 94945, U.S.A., +1(415)492-9861, 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
3.02k
{
33
3.02k
    stream_AXE_state *const ss = (stream_AXE_state *) st;
34
35
3.02k
    return s_AXE_init_inline(ss);
36
3.02k
}
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
712k
{
43
712k
    stream_AXE_state *const ss = (stream_AXE_state *) st;
44
712k
    const byte *p = pr->ptr;
45
712k
    byte *q = pw->ptr;
46
712k
    int rcount = pr->limit - p;
47
712k
    int wcount = pw->limit - q;
48
712k
    int count;
49
712k
    int pos = ss->count;
50
712k
    const char *hex_digits = "0123456789ABCDEF";
51
712k
    int status = 0;
52
53
712k
    if (last && ss->EndOfData)
54
345k
        wcount--;   /* leave room for '>' */
55
712k
    wcount -= (wcount + pos * 2) / 64; /* leave room for \n */
56
712k
    wcount >>= 1;   /* 2 chars per input byte */
57
712k
    count = (wcount < rcount ? (status = 1, wcount) : rcount);
58
15.4M
    while (--count >= 0) {
59
14.7M
        *++q = hex_digits[*++p >> 4];
60
14.7M
        *++q = hex_digits[*p & 0xf];
61
14.7M
        if (!(++pos & 31) && (count != 0 || !last))
62
437k
            *++q = '\n';
63
14.7M
    }
64
712k
    if (last && status == 0 && ss->EndOfData)
65
345k
        *++q = '>';
66
712k
    pr->ptr = p;
67
712k
    pw->ptr = q;
68
712k
    ss->count = pos & 31;
69
712k
    return status;
70
712k
}
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
405
{
85
405
    stream_AXD_state *const ss = (stream_AXD_state *) st;
86
87
405
    return s_AXD_init_inline(ss);
88
405
}
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
5.65M
{
95
5.65M
    stream_AXD_state *const ss = (stream_AXD_state *) st;
96
5.65M
    int code = s_hex_process(pr, pw, &ss->odd, hex_ignore_whitespace);
97
98
5.65M
    switch (code) {
99
3.82k
        case 0:
100
3.82k
            if (ss->odd >= 0 && last) {
101
260
                if (pw->ptr == pw->limit)
102
0
                    return 1;
103
260
                *++(pw->ptr) = ss->odd << 4;
104
260
                ss->odd = -1;
105
260
            }
106
            /* falls through */
107
4.48k
        case 1:
108
            /* We still need to read ahead and check for EOD. */
109
4.49k
            for (; pr->ptr < pr->limit; pr->ptr++)
110
604
                if (scan_char_decoder[pr->ptr[1]] != ctype_space) {
111
591
                    if (pr->ptr[1] == '>') {
112
0
                        pr->ptr++;
113
0
                        goto eod;
114
0
                    }
115
591
                    return 1;
116
591
                }
117
3.89k
            return 0;    /* still need to scan ahead */
118
0
        default:
119
0
            return code;
120
5.65M
        case ERRC:
121
5.65M
            ;
122
5.65M
    }
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
5.65M
    if (*pr->ptr != '>') { /* EOD */
130
613
        --(pr->ptr);
131
613
        return ERRC;
132
613
    }
133
5.64M
  eod:if (ss->odd >= 0) {
134
11.4k
        if (pw->ptr == pw->limit)
135
0
            return 1;
136
11.4k
        *++(pw->ptr) = ss->odd << 4;
137
11.4k
    }
138
5.64M
    return EOFC;
139
5.64M
}
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.21M
{
153
3.21M
    const byte *p = pr->ptr;
154
3.21M
    const byte *rlimit = pr->limit;
155
3.21M
    byte *q = pw->ptr;
156
3.21M
    byte *wlimit = pw->limit;
157
3.21M
    int status = 0;
158
159
    /* This doesn't have to be very efficient. */
160
57.3M
    while (p < rlimit) {
161
55.2M
        int c = *++p;
162
163
55.2M
        if (c < 32 || c >= 127) {
164
47.8M
            const char *pesc;
165
47.8M
            const char *const esc = "\n\r\t\b\f";
166
167
47.8M
            if (c < 32 && c != 0 && (pesc = strchr(esc, c)) != 0) {
168
85.6k
                if (wlimit - q < 2) {
169
1.21k
                    --p;
170
1.21k
                    status = 1;
171
1.21k
                    break;
172
1.21k
                }
173
84.4k
                *++q = '\\';
174
84.4k
                *++q = "nrtbf"[pesc - esc];
175
84.4k
                continue;
176
85.6k
            }
177
47.7M
            if (wlimit - q < 4) {
178
1.06M
                --p;
179
1.06M
                status = 1;
180
1.06M
                break;
181
1.06M
            }
182
46.6M
            q[1] = '\\';
183
46.6M
            q[2] = (c >> 6) + '0';
184
46.6M
            q[3] = ((c >> 3) & 7) + '0';
185
46.6M
            q[4] = (c & 7) + '0';
186
46.6M
            q += 4;
187
46.6M
            continue;
188
47.7M
        } else if (c == '(' || c == ')' || c == '\\') {
189
131k
            if (wlimit - q < 2) {
190
1.11k
                --p;
191
1.11k
                status = 1;
192
1.11k
                break;
193
1.11k
            }
194
130k
            *++q = '\\';
195
7.25M
        } else {
196
7.25M
            if (q == wlimit) {
197
9.75k
                --p;
198
9.75k
                status = 1;
199
9.75k
                break;
200
9.75k
            }
201
7.25M
        }
202
7.37M
        *++q = c;
203
7.37M
    }
204
3.21M
    if (last && status == 0) {
205
1.84M
        if (q == wlimit)
206
3.92k
            status = 1;
207
1.84M
        else
208
1.84M
            *++q = ')';
209
1.84M
    }
210
3.21M
    pr->ptr = p;
211
3.21M
    pw->ptr = q;
212
3.21M
    return status;
213
3.21M
}
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
86.2k
{
228
86.2k
    stream_PSSD_state *const ss = (stream_PSSD_state *) st;
229
230
86.2k
    ss->from_string = false;
231
86.2k
    return s_PSSD_partially_init_inline(ss);
232
86.2k
}
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
166M
{
239
166M
    stream_PSSD_state *const ss = (stream_PSSD_state *) st;
240
166M
    const byte *p = pr->ptr;
241
166M
    const byte *rlimit = pr->limit;
242
166M
    byte *q = pw->ptr;
243
166M
    byte *wlimit = pw->limit;
244
166M
    int status = 0;
245
166M
    int c;
246
247
166M
#define check_p(n)\
248
166M
  if ( p == rlimit ) { p -= n; goto out; }
249
166M
#define check_q(n)\
250
4.09G
  if ( q == wlimit ) { p -= n; status = 1; goto out; }
251
4.26G
    while (p < rlimit) {
252
4.26G
        c = *++p;
253
4.26G
        if (c == '\\' && !ss->from_string) {
254
71.2M
            check_p(1);
255
71.2M
            switch ((c = *++p)) {
256
58.3M
                case 'n':
257
58.3M
                    c = '\n';
258
58.3M
                    goto put;
259
279k
                case 'r':
260
279k
                    c = '\r';
261
279k
                    goto put;
262
454k
                case 't':
263
454k
                    c = '\t';
264
454k
                    goto put;
265
4.96k
                case 'b':
266
4.96k
                    c = '\b';
267
4.96k
                    goto put;
268
116k
                case 'f':
269
116k
                    c = '\f';
270
116k
                    goto put;
271
9.17M
                default:  /* ignore the \ */
272
68.4M
                  put:check_q(2);
273
68.4M
                    *++q = c;
274
68.4M
                    continue;
275
194k
                case char_CR: /* ignore, check for following \n */
276
194k
                    check_p(2);
277
194k
                    if (p[1] == char_EOL)
278
345
                        p++;
279
194k
                    continue;
280
830k
                case char_EOL: /* ignore */
281
830k
                    continue;
282
1.44M
                case '0':
283
1.54M
                case '1':
284
1.55M
                case '2':
285
1.83M
                case '3':
286
1.83M
                case '4':
287
1.84M
                case '5':
288
1.84M
                case '6':
289
1.84M
                case '7':
290
1.84M
                    {
291
1.84M
                        int d;
292
293
1.84M
                        check_p(2);
294
1.84M
                        d = p[1];
295
1.84M
                        c -= '0';
296
1.84M
                        if (d >= '0' && d <= '7') {
297
1.72M
                            if (p + 1 == rlimit) {
298
97
                                p -= 2;
299
97
                                goto out;
300
97
                            }
301
1.72M
                            check_q(2);
302
1.72M
                            c = (c << 3) + d - '0';
303
1.72M
                            d = p[2];
304
1.72M
                            if (d >= '0' && d <= '7') {
305
1.71M
                                c = (c << 3) + d - '0';
306
1.71M
                                p += 2;
307
1.71M
                            } else
308
6.39k
                                p++;
309
1.72M
                        } else
310
1.84M
                            check_q(2);
311
1.84M
                        *++q = c;
312
1.84M
                        continue;
313
1.84M
                    }
314
71.2M
            }
315
71.2M
        } else
316
4.19G
            switch (c) {
317
2.85M
                case '(':
318
2.85M
                    check_q(1);
319
2.85M
                    ss->depth++;
320
2.85M
                    break;
321
166M
                case ')':
322
166M
                    if (ss->depth == 0) {
323
165M
                        status = EOFC;
324
165M
                        goto out;
325
165M
                    }
326
1.02M
                    check_q(1);
327
1.02M
                    ss->depth--;
328
1.02M
                    break;
329
3.37M
                case char_CR: /* convert to \n */
330
3.37M
                    check_p(1);
331
3.36M
                    check_q(1);
332
3.36M
                    if (p[1] == char_EOL)
333
17.1k
                        p++;
334
3.36M
                    *++q = '\n';
335
3.36M
                    continue;
336
2.40M
                case char_EOL:
337
2.40M
                    c = '\n';
338
                    /* fall through */
339
4.02G
                default:
340
4.02G
                    check_q(1);
341
4.02G
                    break;
342
4.19G
            }
343
4.02G
        *++q = c;
344
4.02G
    }
345
447k
#undef check_p
346
447k
#undef check_q
347
166M
  out:pr->ptr = p;
348
166M
    pw->ptr = q;
349
166M
    if (last && status == 0 && p != rlimit)
350
385
        status = ERRC;
351
166M
    return status;
352
166M
}
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
5.68M
{
377
5.68M
    const byte *p = pr->ptr;
378
5.68M
    const byte *rlimit = pr->limit;
379
5.68M
    byte *q = pw->ptr;
380
5.68M
    byte *wlimit = pw->limit;
381
5.68M
    byte *q0 = q;
382
5.68M
    byte val1 = (byte) * odd_digit;
383
5.68M
    byte val2;
384
5.68M
    uint rcount;
385
5.68M
    byte *flimit;
386
5.68M
    const byte *const decoder = scan_char_decoder;
387
5.68M
    int code = 0;
388
389
5.68M
    if (q >= wlimit)
390
60
        return 1;
391
5.68M
    if (val1 <= 0xf)
392
5.19k
        goto d2;
393
5.74M
    do {
394
        /* No digits read */
395
5.74M
        if ((rcount = (rlimit - p) >> 1) != 0)
396
5.74M
        {
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
5.74M
            flimit = (rcount < wlimit - q ? q + rcount : wlimit);
400
78.1M
            while (1) {
401
78.1M
                if ((val1 = decoder[p[1]]) <= 0xf &&
402
78.1M
                    (val2 = decoder[p[2]]) <= 0xf) {
403
72.3M
                    p += 2;
404
72.3M
                    *++q = (val1 << 4) + val2;
405
72.3M
                    if (q < flimit)
406
72.3M
                        continue;
407
12.8k
                    if (q >= wlimit)
408
10.4k
                        goto px;
409
12.8k
                }
410
5.73M
                break;
411
78.1M
            }
412
5.74M
        }
413
        /* About to read the first digit */
414
6.34M
        while (1) {
415
6.34M
            if (p >= rlimit)
416
2.52k
                goto end1;
417
6.33M
            if ((val1 = decoder[*++p]) > 0xf) {
418
6.25M
                if (val1 == ctype_space) {
419
542k
                    switch (syntax) {
420
34.0k
                        case hex_ignore_garbage:
421
516k
                        case hex_ignore_whitespace:
422
516k
                            continue;
423
24.6k
                        case hex_ignore_leading_whitespace:
424
24.6k
                            if (q == q0 && *odd_digit < 0)
425
13.3k
                                continue;
426
                            /* pass through */
427
12.6k
                        case hex_break_on_whitespace:
428
12.6k
                            --p;
429
12.6k
                            code = 2;
430
12.6k
                            goto end1;
431
542k
                    }
432
5.71M
                } else if (syntax == hex_ignore_garbage)
433
76.8k
                    continue;
434
5.63M
                code = ERRC;
435
5.63M
                goto end1;
436
6.25M
            }
437
79.2k
            break;
438
6.33M
        }
439
84.4k
  d2:
440
        /* About to read the second hex digit of a pair */
441
436k
        while (1) {
442
436k
            if (p >= rlimit) {
443
1.71k
                *odd_digit = val1;
444
1.71k
                goto ended;
445
1.71k
            }
446
435k
            if ((val2 = decoder[*++p]) > 0xf) {
447
367k
                if (val2 == ctype_space)
448
267k
                    switch (syntax) {
449
31.5k
                        case hex_ignore_garbage:
450
261k
                        case hex_ignore_whitespace:
451
261k
                            continue;
452
4.34k
                        case hex_ignore_leading_whitespace:
453
4.34k
                            if (q == q0)
454
3.20k
                                continue;
455
                            /* pass through */
456
2.47k
                        case hex_break_on_whitespace:
457
2.47k
                            --p;
458
2.47k
                            *odd_digit = val1;
459
2.47k
                            code = 2;
460
2.47k
                            goto ended;
461
267k
                    }
462
100k
                if (syntax == hex_ignore_garbage)
463
87.8k
                    continue;
464
12.9k
                *odd_digit = val1;
465
12.9k
                code = ERRC;
466
12.9k
                goto ended;
467
100k
            }
468
67.2k
            break;
469
435k
        }
470
67.2k
        *++q = (val1 << 4) + val2;
471
67.2k
    } while (q < wlimit);
472
10.4k
  px:code = 1;
473
5.66M
  end1:*odd_digit = -1;
474
5.68M
  ended:pr->ptr = p;
475
5.68M
    pw->ptr = q;
476
5.68M
    return code;
477
5.66M
}