Coverage Report

Created: 2026-02-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/sa85d.c
Line
Count
Source
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
/* ASCII85Decode filter */
18
#include "std.h"
19
#include "strimpl.h"
20
#include "sa85d.h"
21
#include "scanchar.h"
22
23
/* ------ ASCII85Decode ------ */
24
25
private_st_A85D_state();
26
27
/* Initialize the state */
28
static int
29
s_A85D_init(stream_state * st)
30
686
{
31
686
    stream_A85D_state *const ss = (stream_A85D_state *) st;
32
33
686
    s_A85D_init_inline(ss);
34
686
    return 0;
35
686
}
36
37
/* Process a buffer */
38
static int a85d_finish(int, ulong, stream_cursor_write *);
39
static int
40
s_A85D_process(stream_state * st, stream_cursor_read * pr,
41
               stream_cursor_write * pw, bool last)
42
729k
{
43
729k
    stream_A85D_state *const ss = (stream_A85D_state *) st;
44
729k
    register const byte *p = pr->ptr;
45
729k
    register byte *q = pw->ptr;
46
    /* stop processing early unless the target is empty (last == true)  */
47
    /* to make sure we consume the EOD marker correctly. The EOD marker */
48
    /* might be as many as 6 characters after the last valid data char  */
49
    /* D <cr> <lf> '~' <cr> <lf> '>' where 'D' is a data character.     */
50
729k
    const byte *rlimit = pr->limit - (last ? 0 : 7); /* max EOD len + 1 */
51
729k
    const byte *r = max(p, rlimit);
52
729k
    byte *wlimit = pw->limit;
53
729k
    int ccount = ss->odd;
54
729k
    ulong word = ss->word;
55
729k
    int status = 0;
56
57
    /* scan to make sure that an EOD isn't fully contained in the */
58
    /* last part of the buffer (between rlimit and pr->limit).    */
59
1.63M
    while (r < pr->limit) {
60
903k
        if (*++r == '~')
61
4.90M
            while (r < pr->limit)
62
4.18M
                if (*++r == '>') {
63
                    /* we have both characters of a complete EOD. */
64
641
                    rlimit = pr->limit; /* go ahead and process everything */
65
641
                    r = rlimit;   /* break out of the while loops */
66
641
                    break;
67
641
                }
68
903k
    }
69
7.22M
    while (p < rlimit) {
70
7.21M
        int ch = *++p;
71
7.21M
        uint ccode = ch - '!';
72
73
7.21M
        if (ccode < 85) { /* catches ch < '!' as well */
74
6.46M
            if (ccount == 4) {
75
                /*
76
                 * We've completed a 32-bit group.  Make sure we have
77
                 * room for it in the output.
78
                 */
79
1.29M
                if (wlimit - q < 4) {
80
1.43k
                    p--;
81
1.43k
                    status = 1;
82
1.43k
                    break;
83
1.43k
                }
84
                /* Check for overflow condition, throw ioerror if so */
85
1.29M
                if (word >= 0x03030303 && ccode > 0) {
86
184
                    status = ERRC;
87
184
                    break;
88
184
                }
89
1.29M
                word = word * 85 + ccode;
90
1.29M
                q[1] = (byte) (word >> 24);
91
1.29M
                q[2] = (byte) (word >> 16);
92
1.29M
                q[3] = (byte) ((uint) word >> 8);
93
1.29M
                q[4] = (byte) word;
94
1.29M
                q += 4;
95
1.29M
                word = 0;
96
1.29M
                ccount = 0;
97
5.17M
            } else {
98
5.17M
                word = word * 85 + ccode;
99
5.17M
                ++ccount;
100
5.17M
            }
101
6.46M
        } else if (ch == 'z' && ccount == 0) {
102
696
            if (wlimit - q < 4) {
103
0
                p--;
104
0
                status = 1;
105
0
                break;
106
0
            }
107
696
            q[1] = q[2] = q[3] = q[4] = 0,
108
696
                q += 4;
109
752k
        } else if (scan_char_decoder[ch] == ctype_space)
110
752k
            DO_NOTHING;
111
722k
        else if (ch == '~') {
112
722k
            int i = 1;
113
114
722k
            rlimit = pr->limit;   /* Here we use the real "limit" */
115
            /* Handle odd bytes. */
116
722k
            if (p == rlimit) {
117
22
                if (!last)
118
0
                  p--;
119
22
                else if (ss->pdf_rules)
120
0
                  goto finish;
121
22
                else
122
22
                    status = ERRC;
123
22
                break;
124
22
            }
125
722k
            if ((int)(wlimit - q) < ccount - 1) {
126
149
                status = 1;
127
149
                p--;
128
149
                break;
129
149
            }
130
131
            /* According to PLRM 3rd, if the A85 filter encounters '~',
132
             * the next character must be '>'.
133
             * And any other characters should raise an ioerror.
134
             * But Adobe Acrobat allows CR/LF between ~ and >.
135
             * So we allow CR/LF between them. */
136
            /* PDF further relaxes the requirements and accepts bare '~'.
137
             */
138
725k
            while ((p + i <= rlimit) && (p[i] == 13 || p[i] == 10))
139
3.00k
                i++;
140
722k
            if (p + i <= rlimit && p[i] != '>') {
141
721k
                if (ss->pdf_rules) {
142
1
                    if (p[i] == 13 || p[i] == 10) {
143
0
                        if (!last)
144
0
                            break;
145
1
                    } else {
146
1
                        p--;
147
1
                    }
148
721k
                } else {
149
721k
                    if (p + i == rlimit) {
150
20
                        if (last)
151
20
                            status = ERRC;
152
0
                        else
153
0
                            p--; /* we'll see the '~' after filling the buffer */
154
20
                    }
155
721k
                    break;
156
721k
                }
157
721k
            }
158
260
          finish:
159
260
            if (p + i <= rlimit)
160
208
                p += i;    /* advance to the '>' */
161
52
            else
162
52
                p = rlimit; /* Can happen if the '>' is missing */
163
260
            pw->ptr = q;
164
260
            status = a85d_finish(ccount, word, pw);
165
260
            q = pw->ptr;
166
260
            break;
167
722k
        } else {   /* syntax error or exception */
168
507
            status = ERRC;
169
507
            break;
170
507
        }
171
7.21M
    }
172
729k
    pw->ptr = q;
173
729k
    if (status == 0 && last) {
174
690
        if ((int)(wlimit - q) < ccount - 1)
175
0
            status = 1;
176
690
        else if (ss->require_eod)
177
682
            status = ERRC;
178
8
        else
179
8
            status = a85d_finish(ccount, word, pw);
180
690
    }
181
729k
    pr->ptr = p;
182
729k
    ss->odd = ccount;
183
729k
    ss->word = word;
184
729k
    return status;
185
729k
}
186
/* Handle the end of input data. */
187
static int
188
a85d_finish(int ccount, ulong word, stream_cursor_write * pw)
189
268
{
190
    /* Assume there is enough room in the output buffer! */
191
268
    byte *q = pw->ptr;
192
268
    int status = EOFC;
193
194
268
    switch (ccount) {
195
58
        case 0:
196
58
            break;
197
30
        case 1:   /* syntax error */
198
30
            status = ERRC;
199
30
            break;
200
59
        case 2:   /* 1 odd byte */
201
59
            word = word * (85L * 85 * 85) + 85L * 85 * 85 - 1L;
202
59
            goto o1;
203
65
        case 3:   /* 2 odd bytes */
204
65
            word = word * (85L * 85) + 85L * 85L - 1L;
205
65
            goto o2;
206
56
        case 4:   /* 3 odd bytes */
207
56
            word = word * 85L + 84L;
208
56
            q[3] = (byte) (word >> 8);
209
121
o2:     q[2] = (byte) (word >> 16);
210
180
o1:     q[1] = (byte) (word >> 24);
211
180
            q += ccount - 1;
212
180
            pw->ptr = q;
213
268
    }
214
268
    return status;
215
268
}
216
217
/* Stream template */
218
const stream_template s_A85D_template = {
219
    &st_A85D_state, s_A85D_init, s_A85D_process, 2, 4
220
};