/src/ghostpdl/base/sa85d.c
Line | Count | Source |
1 | | /* Copyright (C) 2001-2026 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 | 804 | { |
31 | 804 | stream_A85D_state *const ss = (stream_A85D_state *) st; |
32 | | |
33 | 804 | s_A85D_init_inline(ss); |
34 | 804 | return 0; |
35 | 804 | } |
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 | 534k | { |
43 | 534k | stream_A85D_state *const ss = (stream_A85D_state *) st; |
44 | 534k | register const byte *p = pr->ptr; |
45 | 534k | 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 | 534k | const byte *rlimit = pr->limit - (last ? 0 : 7); /* max EOD len + 1 */ |
51 | 534k | const byte *r = max(p, rlimit); |
52 | 534k | byte *wlimit = pw->limit; |
53 | 534k | int ccount = ss->odd; |
54 | 534k | ulong word = ss->word; |
55 | 534k | 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.20M | while (r < pr->limit) { |
60 | 674k | if (*++r == '~') |
61 | 3.57M | while (r < pr->limit) |
62 | 3.05M | if (*++r == '>') { |
63 | | /* we have both characters of a complete EOD. */ |
64 | 407 | rlimit = pr->limit; /* go ahead and process everything */ |
65 | 407 | r = rlimit; /* break out of the while loops */ |
66 | 407 | break; |
67 | 407 | } |
68 | 674k | } |
69 | 5.98M | while (p < rlimit) { |
70 | 5.97M | int ch = *++p; |
71 | 5.97M | uint ccode = ch - '!'; |
72 | | |
73 | 5.97M | if (ccode < 85) { /* catches ch < '!' as well */ |
74 | 5.42M | 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.08M | if (wlimit - q < 4) { |
80 | 1.26k | p--; |
81 | 1.26k | status = 1; |
82 | 1.26k | break; |
83 | 1.26k | } |
84 | | /* Check for overflow condition, throw ioerror if so */ |
85 | 1.08M | if (word >= 0x03030303 && ccode > 0) { |
86 | 183 | status = ERRC; |
87 | 183 | break; |
88 | 183 | } |
89 | 1.08M | word = word * 85 + ccode; |
90 | 1.08M | q[1] = (byte) (word >> 24); |
91 | 1.08M | q[2] = (byte) (word >> 16); |
92 | 1.08M | q[3] = (byte) ((uint) word >> 8); |
93 | 1.08M | q[4] = (byte) word; |
94 | 1.08M | q += 4; |
95 | 1.08M | word = 0; |
96 | 1.08M | ccount = 0; |
97 | 4.34M | } else { |
98 | 4.34M | word = word * 85 + ccode; |
99 | 4.34M | ++ccount; |
100 | 4.34M | } |
101 | 5.42M | } else if (ch == 'z' && ccount == 0) { |
102 | 497 | if (wlimit - q < 4) { |
103 | 0 | p--; |
104 | 0 | status = 1; |
105 | 0 | break; |
106 | 0 | } |
107 | 497 | q[1] = q[2] = q[3] = q[4] = 0, |
108 | 497 | q += 4; |
109 | 549k | } else if (scan_char_decoder[ch] == ctype_space) |
110 | 549k | DO_NOTHING; |
111 | 528k | else if (ch == '~') { |
112 | 527k | int i = 1; |
113 | | |
114 | 527k | if ((int)(wlimit - q) < ccount - 1) { |
115 | 114 | status = 1; |
116 | 114 | p--; |
117 | 114 | break; |
118 | 114 | } |
119 | | |
120 | 527k | rlimit = pr->limit; /* Here we use the real "limit" */ |
121 | | /* Handle odd bytes. */ |
122 | 527k | if (p == rlimit) { |
123 | 17 | if (!last) |
124 | 0 | p--; |
125 | 17 | else if (ss->pdf_rules) |
126 | 0 | goto finish; |
127 | 17 | else |
128 | 17 | status = ERRC; |
129 | 17 | break; |
130 | 17 | } |
131 | | |
132 | | /* According to PLRM 3rd, if the A85 filter encounters '~', |
133 | | * the next character must be '>'. |
134 | | * And any other characters should raise an ioerror. |
135 | | * But Adobe Acrobat allows CR/LF between ~ and >. |
136 | | * So we allow CR/LF between them. */ |
137 | | /* PDF further relaxes the requirements and accepts bare '~'. |
138 | | */ |
139 | 529k | while ((p + i <= rlimit) && (p[i] == 13 || p[i] == 10)) |
140 | 1.43k | i++; |
141 | 527k | if (p + i <= rlimit && p[i] != '>') { |
142 | 527k | if (ss->pdf_rules) { |
143 | 2 | if (p[i] == 13 || p[i] == 10) { |
144 | 0 | if (!last) |
145 | 0 | break; |
146 | 2 | } else { |
147 | 2 | p--; |
148 | 2 | } |
149 | 527k | } else { |
150 | 527k | if (p + i == rlimit) { |
151 | 11 | if (last) |
152 | 11 | status = ERRC; |
153 | 0 | else |
154 | 0 | p--; /* we'll see the '~' after filling the buffer */ |
155 | 11 | } |
156 | 527k | break; |
157 | 527k | } |
158 | 527k | } |
159 | 343 | finish: |
160 | 343 | if (p + i <= rlimit) |
161 | 307 | p += i; /* advance to the '>' */ |
162 | 36 | else |
163 | 36 | p = rlimit; /* Can happen if the '>' is missing */ |
164 | 343 | pw->ptr = q; |
165 | 343 | status = a85d_finish(ccount, word, pw); |
166 | 343 | q = pw->ptr; |
167 | 343 | break; |
168 | 527k | } else { /* syntax error or exception */ |
169 | 439 | status = ERRC; |
170 | 439 | break; |
171 | 439 | } |
172 | 5.97M | } |
173 | 534k | pw->ptr = q; |
174 | 534k | if (status == 0 && last) { |
175 | 497 | if ((int)(wlimit - q) < ccount - 1) |
176 | 0 | status = 1; |
177 | 497 | else if (ss->require_eod) |
178 | 490 | status = ERRC; |
179 | 7 | else |
180 | 7 | status = a85d_finish(ccount, word, pw); |
181 | 497 | } |
182 | 534k | pr->ptr = p; |
183 | 534k | ss->odd = ccount; |
184 | 534k | ss->word = word; |
185 | 534k | return status; |
186 | 534k | } |
187 | | /* Handle the end of input data. */ |
188 | | static int |
189 | | a85d_finish(int ccount, ulong word, stream_cursor_write * pw) |
190 | 350 | { |
191 | | /* Assume there is enough room in the output buffer! */ |
192 | 350 | byte *q = pw->ptr; |
193 | 350 | int status = EOFC; |
194 | | |
195 | 350 | switch (ccount) { |
196 | 67 | case 0: |
197 | 67 | break; |
198 | 20 | case 1: /* syntax error */ |
199 | 20 | status = ERRC; |
200 | 20 | break; |
201 | 67 | case 2: /* 1 odd byte */ |
202 | 67 | word = word * (85L * 85 * 85) + 85L * 85 * 85 - 1L; |
203 | 67 | goto o1; |
204 | 112 | case 3: /* 2 odd bytes */ |
205 | 112 | word = word * (85L * 85) + 85L * 85L - 1L; |
206 | 112 | goto o2; |
207 | 84 | case 4: /* 3 odd bytes */ |
208 | 84 | word = word * 85L + 84L; |
209 | 84 | q[3] = (byte) (word >> 8); |
210 | 196 | o2: q[2] = (byte) (word >> 16); |
211 | 263 | o1: q[1] = (byte) (word >> 24); |
212 | 263 | q += ccount - 1; |
213 | 263 | pw->ptr = q; |
214 | 350 | } |
215 | 350 | return status; |
216 | 350 | } |
217 | | |
218 | | /* Stream template */ |
219 | | const stream_template s_A85D_template = { |
220 | | &st_A85D_state, s_A85D_init, s_A85D_process, 2, 4 |
221 | | }; |