Debian Codegraph @

src/rfc1345.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1993-2018 Free Software Foundation, Inc.
3 Contributed by François Pinard <[email protected]>, 1993.
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 "rfc1345.h"
24
25 /* This module takes care only of short RFC 1345 forms. Module charname.c
26 takes care of the full descriptive name for characters. */
27
28 /*----------------------------------------------------------------------.
29 | Return an RFC 1345 short form in a CHARSET for a given UCS2 value, or |
30 | NULL if this value has no such known short form. |
31 `----------------------------------------------------------------------*/
32
33 _GL_ATTRIBUTE_CONST const char *
34 recode_ucs2_to_rfc1345 (recode_ucs2 code)
35 {
36 int first = 0;
37 int last = TABLE_LENGTH;
38
39 while (first < last)
40 {
41 int middle = (first + last) / 2;
42 const struct entry *entry = &table[middle];
43
44 if (entry->code < code)
45 first = middle + 1;
46 else if (entry->code > code)
47 last = middle;
48 else
49 return entry->rfc1345;
50 }
51
52 return NULL;
53 }
54
55 /*---------------------------------------------------------------------.
56 | Return an UCS-2 value, given an RFC 1345 short form in a CHARSET, or |
57 | BIT_MASK (16) if the short form is unknown. |
58 `---------------------------------------------------------------------*/
59
60 static _GL_ATTRIBUTE_PURE recode_ucs2
61 rfc1345_to_ucs2 (const char *string)
62 {
63 int first = 0;
64 int last = TABLE_LENGTH;
65
66 while (first < last)
67 {
68 int middle = (first + last) / 2;
69 const struct entry *entry = &table[inverse[middle]];
70 int value = strcmp (entry->rfc1345, string);
71
72 if (value < 0)
73 first = middle + 1;
74 else if (value > 0)
75 last = middle;
76 else
77 return entry->code;
78 }
79
80 return NOT_A_CHARACTER;
81 }
82
83 /* Steps. */
84
85 struct local
86 {
87 char intro; /* RFC 1345 intro character */
88 };
89
90 /*-----------------------------------------------.
91 | Transform a whole file from UCS-2 to RFC1345. |
92 `-----------------------------------------------*/
93
94 static bool
95 transform_ucs2_rfc1345 (RECODE_SUBTASK subtask)
96 {
97 struct local *local = (struct local *) subtask->step->local;
98 const char intro = local->intro;
99 unsigned value;
100
101 while (recode_get_ucs2 (&value, subtask))
102 if (IS_ASCII (value))
103 if (value == (unsigned)intro)
104 {
105 recode_put_byte (intro, subtask);
106 recode_put_byte (intro, subtask);
107 }
108 else
109 recode_put_byte (value, subtask);
110 else
111 {
112 const char *string = recode_ucs2_to_rfc1345 (value);
113
114 if (!string || !string[0])
115 RETURN_IF_NOGO (RECODE_UNTRANSLATABLE, subtask);
116 else if (!string[1])
117 recode_put_byte (string[0], subtask);
118 else if (!string[2])
119 {
120 recode_put_byte (intro, subtask);
121 recode_put_byte (string[0], subtask);
122 recode_put_byte (string[1], subtask);
123 }
124 else
125 {
126 const char *cursor = string;
127
128 recode_put_byte (intro, subtask);
129 recode_put_byte ('_', subtask);
130 while (*cursor)
131 {
132 recode_put_byte (*cursor, subtask);
133 cursor++;
134 }
135 recode_put_byte ('_', subtask);
136 }
137 }
138
139 SUBTASK_RETURN (subtask);
140 }
141
142 /*-----------------------------------------------.
143 | Transform a whole file from RFC1345 to UCS-2. |
144 `-----------------------------------------------*/
145
146 static bool
147 transform_rfc1345_ucs2 (RECODE_SUBTASK subtask)
148 {
149 struct local *local = (struct local *) subtask->step->local;
150 const char intro = local->intro;
151 int character;
152
153 while (character = recode_get_byte (subtask), character != EOF)
154
155 if (character == intro)
156 {
157 character = recode_get_byte (subtask);
158 if (character == EOF)
159 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
160
161 if (character == intro)
162 recode_put_ucs2 (intro, subtask);
163 else if (character == '_')
164 {
165 char buffer[MAX_MNEMONIC_LENGTH + 1];
166 char *cursor = buffer;
167
168 character = recode_get_byte (subtask);
169 while (true)
170 if (character == EOF)
171 {
172 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
173 break;
174 }
175 else if (character == '_')
176 {
177 recode_ucs2 value;
178
179 *cursor = NUL;
180 value = rfc1345_to_ucs2 (buffer);
181 if (value == NOT_A_CHARACTER)
182 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
183 else
184 recode_put_ucs2 (value, subtask);
185 break;
186 }
187 else if (cursor == buffer + MAX_MNEMONIC_LENGTH)
188 {
189 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
190 break;
191 }
192 else
193 {
194 *cursor++ = character;
195 character = recode_get_byte (subtask);
196 }
197 }
198 else
199 {
200 char buffer[3];
201 recode_ucs2 value;
202
203 buffer[0] = character;
204 character = recode_get_byte (subtask);
205 if (character == EOF)
206 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
207 buffer[1] = character;
208 buffer[2] = NUL;
209
210 value = rfc1345_to_ucs2 (buffer);
211 if (value == NOT_A_CHARACTER)
212 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
213 else
214 {
215 if (IS_ASCII (value))
216 RETURN_IF_NOGO (RECODE_AMBIGUOUS_OUTPUT, subtask);
217 recode_put_ucs2 (value, subtask);
218 }
219 }
220 }
221 else
222 recode_put_ucs2 (character, subtask);
223
224 SUBTASK_RETURN (subtask);
225 }
226
227 /*-----------------------.
228 | Initialise the steps. |
229 `-----------------------*/
230
231 static bool
232 term_rfc1345 (RECODE_STEP step)
233 {
234 free (step->local);
235 return true;
236 }
237
238 static bool
239 init_rfc1345 (RECODE_CONST_REQUEST request,
240 RECODE_STEP step,
241 RECODE_CONST_OPTION_LIST options _GL_UNUSED)
242 {
243 RECODE_OUTER outer = request->outer;
244 struct local *local;
245
246 if (!ALLOC (local, 1, struct local))
247 return false;
248
249 local->intro = '&';
250
251 step->local = local;
252 step->term_routine = term_rfc1345;
253 return true;
254 }
255
256 static bool
257 init_ucs2_rfc1345 (RECODE_STEP step,
258 RECODE_CONST_REQUEST request,
259 RECODE_CONST_OPTION_LIST before_options,
260 RECODE_CONST_OPTION_LIST after_options)
261 {
262 if (before_options)
263 return false;
264
265 return init_rfc1345 (request, step, after_options);
266 }
267
268 static bool
269 init_rfc1345_ucs2 (RECODE_STEP step,
270 RECODE_CONST_REQUEST request,
271 RECODE_CONST_OPTION_LIST before_options,
272 RECODE_CONST_OPTION_LIST after_options)
273 {
274 if (after_options)
275 return false;
276
277 return init_rfc1345 (request, step, before_options);
278 }
279
280 /*---------------------.
281 | Declare the module. |
282 `---------------------*/
283
284 bool
285 module_rfc1345 (RECODE_OUTER outer)
286 {
287 RECODE_ALIAS alias;
288
289 if
290 (!recode_declare_single (outer, "ISO-10646-UCS-2", "RFC1345",
291 outer->quality_variable_to_variable,
292 init_ucs2_rfc1345, transform_ucs2_rfc1345)
293 || !recode_declare_single (outer, "RFC1345", "ISO-10646-UCS-2",
294 outer->quality_variable_to_variable,
295 init_rfc1345_ucs2, transform_rfc1345_ucs2)
296 || !recode_declare_alias (outer, "1345", "RFC1345")
297 || !recode_declare_alias (outer, "mnemonic", "RFC1345"))
298 return false;
299
300 /* Aliases for obsolete built-in encodings */
301 if (alias = recode_declare_alias (outer, "Apple-Mac", "macintosh"), !alias)
302 return false;
303 if (!recode_declare_implied_surface (outer, alias, outer->cr_surface))
304 return false;
305
306 return true;
307 }
308
309 void
310 delmodule_rfc1345 (_GL_UNUSED RECODE_OUTER outer)
311 {
312 }