Debian Codegraph @

src/quoted.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1996-2018 Free Software Foundation, Inc.
3 Contributed by François Pinard <[email protected]>, 1996.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 3 of the
8 License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the Recode Library; see the file `COPYING.LIB'.
17 If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21 #include "common.h"
22 #include "decsteps.h"
23
24 static const bool bitnet_flag = false;
25
26 /* Maximum number of characters per MIME line. */
27 #define MIME_LINE_LENGTH 76
28
29 /* Characters from 33 to 60 inclusive and from 62 to 126 inclusive do not
30 need be quoted. */
31 static const char safe_char_usual[1 << 7] =
32 {
33 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0- 15 */
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16- 31 */
35 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 32- 47 */
36 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, /* 48- 63 */
37 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 64- 79 */
38 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80- 95 */
39 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 96-111 */
40 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 /* 112-127 */
41 };
42
43 /* In a Bitnet environment, also quote: ! " # $ @ [ \ ] ^ ` { | } ~ */
44 static const char safe_char_bitnet[1 << 7] =
45 {
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0- 15 */
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16- 31 */
48 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 32- 47 */
49 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, /* 48- 63 */
50 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 64- 79 */
51 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 80- 95 */
52 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 96-111 */
53 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 112-127 */
54 };
55
56 #define PUT_QUOTED(Character) \
57 do { \
58 unsigned nibble; \
59 \
60 recode_put_byte ('=', subtask); \
61 nibble = BIT_MASK (4) & (Character) >> 4; \
62 recode_put_byte ((nibble < 10 ? '0' : 'A' - 10) + nibble, subtask); \
63 nibble = BIT_MASK (4) & (Character); \
64 recode_put_byte ((nibble < 10 ? '0' : 'A' - 10) + nibble, subtask); \
65 } while (false)
66
67 static bool
68 transform_data_quoted_printable (RECODE_SUBTASK subtask)
69 {
70 const char *safe_char = bitnet_flag ? safe_char_bitnet : safe_char_usual;
71 unsigned available = MIME_LINE_LENGTH;
72 int character = recode_get_byte (subtask);
73 int next_character;
74
75 /* Proper maximum filling of quoted-printable lines, avoiding a line buffer
76 nor much look-ahead, is a bit trickier than I initially expected. */
77
78 while (character != EOF)
79 if (!(character & (1 << 7)) && safe_char[character])
80
81 /* Case of a safe character. */
82
83 if (available > 1)
84 {
85 recode_put_byte (character, subtask);
86 available--;
87 character = recode_get_byte (subtask);
88 }
89 else
90 {
91 next_character = recode_get_byte (subtask);
92 if (next_character == '\n')
93 {
94 recode_put_byte (character, subtask);
95 recode_put_byte ('\n', subtask);
96 available = MIME_LINE_LENGTH;
97 character = recode_get_byte (subtask);
98 }
99 else if (next_character == EOF)
100 {
101 recode_put_byte (character, subtask);
102 available--;
103 character = EOF;
104 }
105 else
106 {
107 recode_put_byte ('=', subtask);
108 recode_put_byte ('\n', subtask);
109 recode_put_byte (character, subtask);
110 available = MIME_LINE_LENGTH - 1;
111 character = next_character;
112 }
113 }
114 else
115 switch (character)
116 {
117 case '\n':
118
119 /* Case of a newline. */
120
121 recode_put_byte ('\n', subtask);
122 available = MIME_LINE_LENGTH;
123 character = recode_get_byte (subtask);
124 break;
125
126 case '\t':
127 case ' ':
128
129 /* Case of a space character. */
130
131 next_character = recode_get_byte (subtask);
132 if (next_character == '\n')
133 {
134 if (available < 3)
135 {
136 recode_put_byte ('=', subtask);
137 recode_put_byte ('\n', subtask);
138 }
139 PUT_QUOTED (character);
140 recode_put_byte ('\n', subtask);
141 available = MIME_LINE_LENGTH;
142 character = recode_get_byte (subtask);
143 }
144 else if (next_character == EOF)
145 {
146 if (available < 3)
147 {
148 recode_put_byte ('=', subtask);
149 recode_put_byte ('\n', subtask);
150 available = MIME_LINE_LENGTH;
151 }
152 PUT_QUOTED (character);
153 available--;
154 character = EOF;
155 }
156 else
157 {
158 if (available == 1)
159 {
160 recode_put_byte ('=', subtask);
161 recode_put_byte ('\n', subtask);
162 available = MIME_LINE_LENGTH;
163 }
164 recode_put_byte (character, subtask);
165 available--;
166 character = next_character;
167 }
168 break;
169
170 default:
171
172 /* Case of an inconditional quotable character. */
173
174 if (available > 3)
175 {
176 PUT_QUOTED (character);
177 available -= 3;
178 character = recode_get_byte (subtask);
179 }
180 else
181 {
182 next_character = recode_get_byte (subtask);
183 if (available == 3 && next_character == '\n')
184 {
185 PUT_QUOTED (character);
186 recode_put_byte ('\n', subtask);
187 available = MIME_LINE_LENGTH;
188 character = recode_get_byte (subtask);
189 }
190 else if (next_character == EOF)
191 {
192 PUT_QUOTED (character);
193 available--;
194 character = EOF;
195 }
196 else
197 {
198 recode_put_byte ('=', subtask);
199 recode_put_byte ('\n', subtask);
200 PUT_QUOTED (character);
201 available = MIME_LINE_LENGTH - 3;
202 character = next_character;
203 }
204 }
205 }
206
207 if (available != MIME_LINE_LENGTH)
208 {
209 /* The last line was incomplete. */
210
211 recode_put_byte ('=', subtask);
212 recode_put_byte ('\n', subtask);
213 }
214
215 SUBTASK_RETURN (subtask);
216 }
217
218 static bool
219 transform_quoted_printable_data (RECODE_SUBTASK subtask)
220 {
221 const char *safe_char = bitnet_flag ? safe_char_bitnet : safe_char_usual;
222 unsigned counter = 0;
223 int character = recode_get_byte (subtask);
224 char buffer[MIME_LINE_LENGTH + 1];
225 char *cursor;
226 unsigned value;
227
228 while (character != EOF)
229 switch (character)
230 {
231 case '\n':
232 /* Process hard line break. */
233
234 if (counter > MIME_LINE_LENGTH)
235 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
236 counter = 0;
237 recode_put_byte ('\n', subtask);
238 character = recode_get_byte (subtask);
239 break;
240
241 case ' ':
242 case '\t':
243 /* Process white space. */
244
245 cursor = buffer;
246 while (character == ' ' || character == '\t')
247 {
248 if (cursor == buffer + MIME_LINE_LENGTH)
249 {
250 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
251 for (cursor = buffer;
252 cursor < buffer + MIME_LINE_LENGTH;
253 cursor++)
254 recode_put_byte (*cursor, subtask);
255 }
256 counter++;
257 *cursor++ = character;
258 character = recode_get_byte (subtask);
259 }
260 if (character == '\n' || character == EOF)
261 {
262 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
263 counter = 0;
264 break;
265 }
266 *cursor = '\0';
267 for (cursor = buffer; *cursor; cursor++)
268 recode_put_byte (*cursor, subtask);
269 break;
270
271 case '=':
272 counter++;
273 character = recode_get_byte (subtask);
274 if (character == ' ' || character == '\t' || character == '\n')
275 {
276 /* Process soft line break. */
277
278 if (character == ' ' || character == '\t')
279 {
280 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
281 while (character == ' ' || character == '\t')
282 {
283 counter++;
284 character = recode_get_byte (subtask);
285 }
286 }
287 if (character != '\n')
288 {
289 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
290 break;
291 }
292 if (counter > MIME_LINE_LENGTH)
293 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
294 counter = 0;
295 character = recode_get_byte (subtask);
296 break;
297 }
298
299 /* Process quoted value. */
300
301 counter++;
302 if (character >= '0' && character <= '9')
303 value = (character - '0') << 4;
304 else if (character >= 'a' && character <= 'f')
305 value = (character - 'a' + 10) << 4;
306 else if (character >= 'A' && character <= 'F')
307 value = (character - 'A' + 10) << 4;
308 else
309 {
310 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
311 break;
312 }
313 character = recode_get_byte (subtask);
314 counter++;
315 if (character >= '0' && character <= '9')
316 value |= character - '0';
317 else if (character >= 'a' && character <= 'f')
318 value |= character - 'a' + 10;
319 else if (character >= 'A' && character <= 'F')
320 value |= character - 'A' + 10;
321 else
322 {
323 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
324 break;
325 }
326 if (!(value & (1 << 7)) && safe_char[value])
327 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
328
329 recode_put_byte (value, subtask);
330 character = recode_get_byte (subtask);
331 break;
332
333 default:
334 /* Process safe character. */
335
336 counter++;
337 if (character & (1 << 7) || !safe_char[character])
338 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
339 recode_put_byte (character, subtask);
340 character = recode_get_byte (subtask);
341 }
342
343 if (counter != 0)
344 /* Last line is not terminated. */
345 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
346
347 SUBTASK_RETURN (subtask);
348 }
349
350 bool
351 module_quoted_printable (RECODE_OUTER outer)
352 {
353 return
354 recode_declare_single (outer, "data", "Quoted-Printable",
355 outer->quality_variable_to_variable,
356 NULL, transform_data_quoted_printable)
357 && recode_declare_single (outer, "Quoted-Printable", "data",
358 outer->quality_variable_to_variable,
359 NULL, transform_quoted_printable_data)
360 && recode_declare_alias (outer, "quote-printable", "Quoted-Printable")
361 && recode_declare_alias (outer, "QP", "Quoted-Printable");
362 }
363
364 void
365 delmodule_quoted_printable (_GL_UNUSED RECODE_OUTER outer)
366 {
367 }