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