Debian Codegraph @

src/utf7.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 #include "base64.h"
24
25 /* Classification of first 128 UCS-2 characters. */
26
27 #define D 1 /* set D -- direct character */
28 #define O 2 /* set O -- optional direct character */
29 #define W 4 /* direct white space */
30
31 #define z 0
32 static char classification[128] =
33 {
34 z, z, z, z, z, z, z, z, z, W, W, z, z, W, z, z, z, z, z, z, /* 0- 19 */
35 z, z, z, z, z, z, z, z, z, z, z, z, W, O, O, O, O, O, O, D, /* 20- 39 */
36 D, D, O, z, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, O, /* 40- 59 */
37 O, O, O, D, O, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, /* 60- 79 */
38 D, D, D, D, D, D, D, D, D, D, D, O, z, O, O, O, O, D, D, D, /* 80- 99 */
39 D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, /* 100-119 */
40 D, D, D, O, O, O, z, z /* 120-127 */
41 };
42 #undef z
43
44 /* Should the character be directly represented in message bodies?
45 Here, & (D | O | W) is implied, since these are the only flags. */
46 #define IS_BODY_DIRECT(Character) \
47 (IS_ASCII (Character) && classification[Character])
48
49 /* Transformation routines. */
50
51 /* The following diagram shows the logical steps by which three UCS-2
52 characters get transformed into eight Base64 characters. It helps
53 understanding shifts and masks in the transformation functions.
54
55 .--------+--------. .--------+--------. .--------+--------.
56 |aaaaaabb|bbbbcccc| |ccdddddd|eeeeeeff| |ffffgggg|gghhhhhh|
57 `--------+--------' `--------+--------' `--------+--------'
58 6 2 4 4 2 6 6 2 4 4 2 6
59 .--------+--------+--------+--------+--------+--------+--------+--------.
60 |00aaaaaa|00bbbbbb|00cccccc|00dddddd|00eeeeee|00ffffff|00gggggg|00hhhhhh|
61 `--------+--------+--------+--------+--------+--------+--------+--------'
62
63 .--------+--------+--------+--------+--------+--------+--------+--------.
64 |AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|EEEEEEEE|FFFFFFFF|GGGGGGGG|HHHHHHHH|
65 `--------+--------+--------+--------+--------+--------+--------+--------'
66
67 The UCS-2 characters are divided into 6 bit chunks, which are then
68 encoded into Base64 characters. */
69
70 static bool
71 transform_utf16_utf7 (RECODE_SUBTASK subtask)
72 {
73 unsigned value;
74
75 if (!recode_get_ucs2 (&value, subtask))
76 SUBTASK_RETURN (subtask);
77
78 while (true)
79
80 if (IS_BODY_DIRECT (value))
81 {
82 /* Copy one direct character. */
83
84 recode_put_byte (value, subtask);
85 if (!recode_get_ucs2 (&value, subtask))
86 SUBTASK_RETURN (subtask);
87 }
88 else
89 {
90 /* Copy a string of non-direct characters. */
91
92 recode_put_byte ('+', subtask);
93
94 while (!IS_BODY_DIRECT (value))
95 {
96 unsigned split;
97
98 /* Process first UCS-2 value of a triplet. */
99
100 recode_put_byte (base64_value_to_char[BIT_MASK (6) & value >> 10], subtask);
101 recode_put_byte (base64_value_to_char[BIT_MASK (6) & value >> 4], subtask);
102 split = (value & BIT_MASK (4)) << 2;
103 if (!recode_get_ucs2 (&value, subtask))
104 {
105 recode_put_byte (base64_value_to_char[split], subtask);
106 SUBTASK_RETURN (subtask);
107 }
108
109 /* Process second UCS-2 value of a triplet. */
110
111 if (IS_BODY_DIRECT (value))
112 {
113 recode_put_byte (base64_value_to_char[split], subtask);
114 break;
115 }
116 recode_put_byte (base64_value_to_char[split | (BIT_MASK (2) & value >> 14)],
117 subtask);
118 recode_put_byte (base64_value_to_char[BIT_MASK (6) & value >> 8], subtask);
119 recode_put_byte (base64_value_to_char[BIT_MASK (6) & value >> 2], subtask);
120 split = (value & BIT_MASK (2)) << 4;
121 if (!recode_get_ucs2 (&value, subtask))
122 {
123 recode_put_byte (base64_value_to_char[split], subtask);
124 SUBTASK_RETURN (subtask);
125 }
126
127 /* Process third UCS-2 value of a triplet. */
128
129 if (IS_BODY_DIRECT (value))
130 {
131 recode_put_byte (base64_value_to_char[split], subtask);
132 break;
133 }
134 recode_put_byte (base64_value_to_char[split | (BIT_MASK (4) & value >> 12)],
135 subtask);
136 recode_put_byte (base64_value_to_char[BIT_MASK (6) & value >> 6], subtask);
137 recode_put_byte (base64_value_to_char[BIT_MASK (6) & value], subtask);
138 if (!recode_get_ucs2 (&value, subtask))
139 SUBTASK_RETURN (subtask);
140 }
141
142 if (IS_BASE64 (value))
143 recode_put_byte ('-', subtask);
144 }
145
146 SUBTASK_RETURN (subtask);
147 }
148
149 static bool
150 transform_utf7_utf16 (RECODE_SUBTASK subtask)
151 {
152 int character;
153 unsigned value;
154 unsigned split;
155
156 character = recode_get_byte (subtask);
157
158 if (character != EOF && subtask->task->byte_order_mark)
159 recode_put_ucs2 (BYTE_ORDER_MARK, subtask);
160
161 while (character != EOF)
162 if (character == '+')
163 {
164 character = recode_get_byte (subtask);
165 while (IS_BASE64 (character))
166 {
167 /* Process first byte of first quadruplet. */
168
169 value = base64_char_to_value[character] << 10;
170 character = recode_get_byte (subtask);
171
172 /* Process second byte of first quadruplet. */
173
174 if (!IS_BASE64 (character))
175 {
176 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
177 break;
178 }
179 value |= base64_char_to_value[character] << 4;
180 character = recode_get_byte (subtask);
181
182 /* Process third byte of first quadruplet. */
183
184 if (!IS_BASE64 (character))
185 {
186 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
187 break;
188 }
189 split = base64_char_to_value[character];
190 value |= split >> 2;
191 if (IS_BODY_DIRECT (value))
192 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
193 recode_put_ucs2 (value, subtask);
194 character = recode_get_byte (subtask);
195
196 /* Process fourth byte of first quadruplet. */
197
198 if (!IS_BASE64 (character))
199 {
200 if (BIT_MASK (2) & split)
201 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
202 break;
203 }
204 value = ((BIT_MASK (2) & split) << 14
205 | base64_char_to_value[character] << 8);
206 character = recode_get_byte (subtask);
207
208 /* Process first byte of second quadruplet. */
209
210 if (!IS_BASE64 (character))
211 {
212 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
213 break;
214 }
215 value |= base64_char_to_value[character] << 2;
216 character = recode_get_byte (subtask);
217
218 /* Process second byte of second quadruplet. */
219
220 if (!IS_BASE64 (character))
221 {
222 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
223 break;
224 }
225 split = base64_char_to_value[character];
226 value |= split >> 4;
227 if (IS_BODY_DIRECT (value))
228 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
229 recode_put_ucs2 (value, subtask);
230 character = recode_get_byte (subtask);
231
232 /* Process third byte of second quadruplet. */
233
234 if (!IS_BASE64 (character))
235 {
236 if (BIT_MASK (4) & split)
237 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
238 break;
239 }
240 value = ((BIT_MASK (4) & split) << 12
241 | base64_char_to_value[character] << 6);
242 character = recode_get_byte (subtask);
243
244 /* Process fourth byte of second quadruplet. */
245
246 if (!IS_BASE64 (character))
247 {
248 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
249 break;
250 }
251 value |= base64_char_to_value[character];
252 if (IS_BODY_DIRECT (value))
253 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
254 recode_put_ucs2 (value, subtask);
255 character = recode_get_byte (subtask);
256 }
257 if (character == '-')
258 {
259 character = recode_get_byte (subtask);
260 if (!IS_BASE64 (character))
261 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
262 }
263 }
264 else
265 {
266 if (!IS_BODY_DIRECT (character))
267 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
268 recode_put_byte (NUL, subtask);
269 recode_put_byte (character, subtask);
270 character = recode_get_byte (subtask);
271 }
272
273 SUBTASK_RETURN (subtask);
274 }
275
276 bool
277 module_utf7 (RECODE_OUTER outer)
278 {
279 return
280 recode_declare_single (outer, "UTF-16", "UNICODE-1-1-UTF-7", /* RFC1642 */
281 outer->quality_variable_to_variable,
282 NULL, transform_utf16_utf7)
283 && recode_declare_single (outer, "UNICODE-1-1-UTF-7", "UTF-16",
284 outer->quality_variable_to_variable,
285 NULL, transform_utf7_utf16)
286
287 && recode_declare_alias (outer, "UTF-7", "UNICODE-1-1-UTF-7")
288 && recode_declare_alias (outer, "TF-7", "UNICODE-1-1-UTF-7")
289 && recode_declare_alias (outer, "u7", "UNICODE-1-1-UTF-7")
290
291 /* Simple UCS-2 does not have to go through UTF-16. */
292 && recode_declare_single (outer, "ISO-10646-UCS-2", "UNICODE-1-1-UTF-7",
293 outer->quality_variable_to_variable,
294 NULL, transform_utf16_utf7);
295 }
296
297 void
298 delmodule_utf7 (_GL_UNUSED RECODE_OUTER outer)
299 {
300 }