Debian Codegraph @

src/html.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1990-2018 Free Software Foundation, Inc.
3 Contributed by François Pinard <[email protected]>, 1988.
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 "hash.h"
24
25 /* FIXME: An @code{HTML} text which has spurious semi-colons to end entities
26 (in strict mode) or does not always have them (in non-strict mode) is
27 not canonical. */
28
29 /* Tables. */
30
31 /* The following entities, said to be from Emacs-w3, are ignored for the
32 time being, as recode is not too fond of graphical approximations:
33
34 &ensp; \
35 &emsp; \ \
36 &ndash; -
37 &mdash; --
38 &lsquo; `
39 &rsquo; '
40 &ldquo; ``
41 &rdquo; ''
42 &frac18; 1/8
43 &frac38; 3/8
44 &frac58; 5/8
45 &frac78; 7/8
46 &hellip; . . .
47 &larr; <--
48 &rarr; -->
49 &trade; (TM)
50 */
51
52 /* XML with stand-alone=yes */
53 #define V00 (1 << 0)
54 /* Old Emacs-W3, HTML 1.1 ? */
55 #define V11 (1 << 1)
56 /* RFC1866, HTML 2.0 */
57 #define V20 (1 << 2)
58 /* RFC2070, HTML-i18n */
59 #define V27 (1 << 3)
60 /* HTML 3.2 */
61 #define V32 (1 << 4)
62 /* HTML 4.0 */
63 #define V40 (1 << 5)
64
65 #define ENTRY(Code, String, Flags) \
66 { Code, Flags, String }
67
68 static struct ucs2_to_string translations [] =
69 {
70 ENTRY (33, "excl", 0 ),
71 ENTRY (34, "quot", 0 | V00 | V20 | V27 | V32 | V40),
72 ENTRY (35, "num", 0 ),
73 ENTRY (36, "dollar", 0 ),
74 ENTRY (37, "percnt", 0 ),
75 ENTRY (38, "amp", 0 | V00 | V11 | V20 | V27 | V32 | V40),
76 ENTRY (39, "apos", 0 | V00 ),
77 ENTRY (40, "lpar", 0 ),
78 ENTRY (41, "rpar", 0 ),
79 ENTRY (42, "ast", 0 ),
80 ENTRY (43, "plus", 0 ),
81 ENTRY (44, "comma", 0 ),
82 ENTRY (45, "horbar", 0 ),
83 ENTRY (46, "period", 0 ),
84 ENTRY (58, "colon", 0 ),
85 ENTRY (59, "semi", 0 ),
86 ENTRY (60, "lt", 0 | V00 | V11 | V20 | V27 | V32 | V40),
87 ENTRY (61, "equals", 0 ),
88 ENTRY (62, "gt", 0 | V00 | V11 | V20 | V27 | V32 | V40),
89 ENTRY (63, "quest", 0 ),
90 ENTRY (64, "commat", 0 ),
91 ENTRY (91, "lsqb", 0 ),
92 ENTRY (93, "rsqb", 0 ),
93 ENTRY (94, "uarr", 0 ),
94 ENTRY (95, "lowbar", 0 ),
95 ENTRY (96, "grave", 0 ),
96 ENTRY (123, "lcub", 0 ),
97 ENTRY (124, "verbar", 0 ),
98 ENTRY (125, "rcub", 0 ),
99 ENTRY (126, "tilde", 0 ),
100 ENTRY (160, "nbsp", 0 | V27 | V32 | V40),
101 ENTRY (161, "iexcl", 0 | V27 | V32 | V40),
102 ENTRY (162, "cent", 0 | V27 | V32 | V40),
103 ENTRY (163, "pound", 0 | V27 | V32 | V40),
104 ENTRY (164, "curren", 0 | V27 | V32 | V40),
105 ENTRY (165, "yen", 0 | V27 | V32 | V40),
106 ENTRY (166, "brkbar", 0 | V11 ),
107 ENTRY (166, "brvbar", 0 | V27 | V32 | V40),
108 ENTRY (167, "sect", 0 | V27 | V32 | V40),
109 ENTRY (168, "die", 0 | V11 ),
110 ENTRY (168, "uml", 0 | V27 | V32 | V40),
111 ENTRY (169, "copy", 0 | V27 | V32 | V40),
112 ENTRY (170, "ordf", 0 | V27 | V32 | V40),
113 ENTRY (171, "laquo", 0 | V27 | V32 | V40),
114 ENTRY (172, "not", 0 | V27 | V32 | V40),
115 ENTRY (173, "hyphen", 0 | V11 ),
116 ENTRY (173, "shy", 0 | V27 | V32 | V40),
117 ENTRY (174, "reg", 0 | V27 | V32 | V40),
118 ENTRY (175, "hibar", 0 | V11 ),
119 ENTRY (175, "macr", 0 | V27 | V32 | V40),
120 ENTRY (176, "deg", 0 | V27 | V32 | V40),
121 ENTRY (177, "plusmn", 0 | V27 | V32 | V40),
122 ENTRY (178, "sup2", 0 | V27 | V32 | V40),
123 ENTRY (179, "sup3", 0 | V27 | V32 | V40),
124 ENTRY (180, "acute", 0 | V27 | V32 | V40),
125 ENTRY (181, "micro", 0 | V27 | V32 | V40),
126 ENTRY (182, "para", 0 | V27 | V32 | V40),
127 ENTRY (183, "middot", 0 | V27 | V32 | V40),
128 ENTRY (184, "cedil", 0 | V27 | V32 | V40),
129 ENTRY (185, "sup1", 0 | V27 | V32 | V40),
130 ENTRY (186, "ordm", 0 | V27 | V32 | V40),
131 ENTRY (187, "raquo", 0 | V27 | V32 | V40),
132 ENTRY (188, "frac14", 0 | V27 | V32 | V40),
133 ENTRY (189, "half", 0 | V11 ),
134 ENTRY (189, "frac12", 0 | V27 | V32 | V40),
135 ENTRY (190, "frac34", 0 | V27 | V32 | V40),
136 ENTRY (191, "iquest", 0 | V27 | V32 | V40),
137 ENTRY (192, "Agrave", 0 | V11 | V20 | V27 | V32 | V40),
138 ENTRY (193, "Aacute", 0 | V11 | V20 | V27 | V32 | V40),
139 ENTRY (194, "Acircu", 0 | V11 ),
140 ENTRY (194, "Acirc", 0 | V20 | V27 | V32 | V40),
141 ENTRY (195, "Atilde", 0 | V11 | V20 | V27 | V32 | V40),
142 ENTRY (196, "Adiaer", 0 | V11 ),
143 ENTRY (196, "Auml", 0 | V20 | V27 | V32 | V40),
144 ENTRY (197, "Aring", 0 | V11 | V20 | V27 | V32 | V40),
145 ENTRY (198, "AE", 0 | V11 ),
146 ENTRY (198, "AElig", 0 | V20 | V27 | V32 | V40),
147 ENTRY (199, "Ccedil", 0 | V11 | V20 | V27 | V32 | V40),
148 ENTRY (200, "Egrave", 0 | V11 | V20 | V27 | V32 | V40),
149 ENTRY (201, "Eacute", 0 | V11 | V20 | V27 | V32 | V40),
150 ENTRY (202, "Ecircu", 0 | V11 ),
151 ENTRY (202, "Ecirc", 0 | V20 | V27 | V32 | V40),
152 ENTRY (203, "Ediaer", 0 | V11 ),
153 ENTRY (203, "Euml", 0 | V20 | V27 | V32 | V40),
154 ENTRY (204, "Igrave", 0 | V11 | V20 | V27 | V32 | V40),
155 ENTRY (205, "Iacute", 0 | V11 | V20 | V27 | V32 | V40),
156 ENTRY (206, "Icircu", 0 | V11 ),
157 ENTRY (206, "Icirc", 0 | V20 | V27 | V32 | V40),
158 ENTRY (207, "Idiaer", 0 | V11 ),
159 ENTRY (207, "Iuml", 0 | V20 | V27 | V32 | V40),
160 ENTRY (208, "ETH", 0 | V11 | V20 | V27 | V32 | V40),
161 ENTRY (209, "Ntilde", 0 | V11 | V20 | V27 | V32 | V40),
162 ENTRY (210, "Ograve", 0 | V11 | V20 | V27 | V32 | V40),
163 ENTRY (211, "Oacute", 0 | V11 | V20 | V27 | V32 | V40),
164 ENTRY (212, "Ocircu", 0 | V11 ),
165 ENTRY (212, "Ocirc", 0 | V20 | V27 | V32 | V40),
166 ENTRY (213, "Otilde", 0 | V11 | V20 | V27 | V32 | V40),
167 ENTRY (214, "Odiaer", 0 | V11 ),
168 ENTRY (214, "Ouml", 0 | V20 | V27 | V32 | V40),
169 ENTRY (215, "MULT", 0 | V11 ),
170 ENTRY (215, "times", 0 | V27 | V32 | V40),
171 ENTRY (216, "Ostroke", 0 | V11 ),
172 ENTRY (216, "Oslash", 0 | V20 | V27 | V32 | V40),
173 ENTRY (217, "Ugrave", 0 | V11 | V20 | V27 | V32 | V40),
174 ENTRY (218, "Uacute", 0 | V11 | V20 | V27 | V32 | V40),
175 ENTRY (219, "Ucircu", 0 | V11 ),
176 ENTRY (219, "Ucirc", 0 | V20 | V27 | V32 | V40),
177 ENTRY (220, "Udiaer", 0 | V11 ),
178 ENTRY (220, "Uuml", 0 | V20 | V27 | V32 | V40),
179 ENTRY (221, "Yacute", 0 | V11 | V20 | V27 | V32 | V40),
180 ENTRY (222, "THORN", 0 | V11 | V20 | V27 | V32 | V40),
181 ENTRY (223, "ssharp", 0 | V11 ),
182 ENTRY (223, "szlig", 0 | V20 | V27 | V32 | V40),
183 ENTRY (224, "agrave", 0 | V11 | V20 | V27 | V32 | V40),
184 ENTRY (225, "aacute", 0 | V11 | V20 | V27 | V32 | V40),
185 ENTRY (226, "acircu", 0 | V11 ),
186 ENTRY (226, "acirc", 0 | V20 | V27 | V32 | V40),
187 ENTRY (227, "atilde", 0 | V11 | V20 | V27 | V32 | V40),
188 ENTRY (228, "adiaer", 0 | V11 ),
189 ENTRY (228, "auml", 0 | V20 | V27 | V32 | V40),
190 ENTRY (229, "aring", 0 | V11 | V20 | V27 | V32 | V40),
191 ENTRY (230, "ae", 0 | V11 ),
192 ENTRY (230, "aelig", 0 | V20 | V27 | V32 | V40),
193 ENTRY (231, "ccedil", 0 | V11 | V20 | V27 | V32 | V40),
194 ENTRY (232, "egrave", 0 | V11 | V20 | V27 | V32 | V40),
195 ENTRY (233, "eacute", 0 | V11 | V20 | V27 | V32 | V40),
196 ENTRY (234, "ecircu", 0 | V11 ),
197 ENTRY (234, "ecirc", 0 | V20 | V27 | V32 | V40),
198 ENTRY (235, "ediaer", 0 | V11 ),
199 ENTRY (235, "euml", 0 | V20 | V27 | V32 | V40),
200 ENTRY (236, "igrave", 0 | V11 | V20 | V27 | V32 | V40),
201 ENTRY (237, "iacute", 0 | V11 | V20 | V27 | V32 | V40),
202 ENTRY (238, "icircu", 0 | V11 ),
203 ENTRY (238, "icirc", 0 | V20 | V27 | V32 | V40),
204 ENTRY (239, "idiaer", 0 | V11 ),
205 ENTRY (239, "iuml", 0 | V20 | V27 | V32 | V40),
206 ENTRY (240, "eth", 0 | V11 | V20 | V27 | V32 | V40),
207 ENTRY (241, "ntilde", 0 | V11 | V20 | V27 | V32 | V40),
208 ENTRY (242, "ograve", 0 | V11 | V20 | V27 | V32 | V40),
209 ENTRY (243, "oacute", 0 | V11 | V20 | V27 | V32 | V40),
210 ENTRY (244, "ocircu", 0 | V11 ),
211 ENTRY (244, "ocirc", 0 | V20 | V27 | V32 | V40),
212 ENTRY (245, "otilde", 0 | V11 | V20 | V27 | V32 | V40),
213 ENTRY (246, "odiaer", 0 | V11 ),
214 ENTRY (246, "ouml", 0 | V20 | V27 | V32 | V40),
215 ENTRY (247, "DIVIS", 0 | V11 ),
216 ENTRY (247, "divide", 0 | V27 | V32 | V40),
217 ENTRY (248, "ostroke", 0 | V11 ),
218 ENTRY (248, "oslash", 0 | V20 | V27 | V32 | V40),
219 ENTRY (249, "ugrave", 0 | V11 | V20 | V27 | V32 | V40),
220 ENTRY (250, "uacute", 0 | V11 | V20 | V27 | V32 | V40),
221 ENTRY (251, "ucircu", 0 | V11 ),
222 ENTRY (251, "ucirc", 0 | V20 | V27 | V32 | V40),
223 ENTRY (252, "udiaer", 0 | V11 ),
224 ENTRY (252, "uuml", 0 | V20 | V27 | V32 | V40),
225 ENTRY (253, "yacute", 0 | V11 | V20 | V27 | V32 | V40),
226 ENTRY (254, "thorn", 0 | V11 | V20 | V27 | V32 | V40),
227 ENTRY (255, "ydiaer", 0 | V11 ),
228 ENTRY (255, "yuml", 0 | V20 | V27 | V32 | V40),
229 ENTRY (338, "OElig", 0 | V40),
230 ENTRY (339, "oelig", 0 | V40),
231 ENTRY (352, "Scaron", 0 | V40),
232 ENTRY (353, "scaron", 0 | V40),
233 ENTRY (376, "Yuml", 0 | V40),
234 ENTRY (402, "fnof", 0 | V40),
235 ENTRY (710, "circ", 0 | V40),
236 ENTRY (732, "tilde", 0 | V40),
237 ENTRY (913, "Alpha", 0 | V40),
238 ENTRY (914, "Beta", 0 | V40),
239 ENTRY (915, "Gamma", 0 | V40),
240 ENTRY (916, "Delta", 0 | V40),
241 ENTRY (917, "Epsilon", 0 | V40),
242 ENTRY (918, "Zeta", 0 | V40),
243 ENTRY (919, "Eta", 0 | V40),
244 ENTRY (920, "Theta", 0 | V40),
245 ENTRY (921, "Iota", 0 | V40),
246 ENTRY (922, "Kappa", 0 | V40),
247 ENTRY (923, "Lambda", 0 | V40),
248 ENTRY (924, "Mu", 0 | V40),
249 ENTRY (925, "Nu", 0 | V40),
250 ENTRY (926, "Xi", 0 | V40),
251 ENTRY (927, "Omicron", 0 | V40),
252 ENTRY (928, "Pi", 0 | V40),
253 ENTRY (929, "Rho", 0 | V40),
254 ENTRY (931, "Sigma", 0 | V40),
255 ENTRY (932, "Tau", 0 | V40),
256 ENTRY (933, "Upsilon", 0 | V40),
257 ENTRY (934, "Phi", 0 | V40),
258 ENTRY (935, "Chi", 0 | V40),
259 ENTRY (936, "Psi", 0 | V40),
260 ENTRY (937, "Omega", 0 | V40),
261 ENTRY (945, "alpha", 0 | V40),
262 ENTRY (946, "beta", 0 | V40),
263 ENTRY (947, "gamma", 0 | V40),
264 ENTRY (948, "delta", 0 | V40),
265 ENTRY (949, "epsilon", 0 | V40),
266 ENTRY (950, "zeta", 0 | V40),
267 ENTRY (951, "eta", 0 | V40),
268 ENTRY (952, "theta", 0 | V40),
269 ENTRY (953, "iota", 0 | V40),
270 ENTRY (954, "kappa", 0 | V40),
271 ENTRY (955, "lambda", 0 | V40),
272 ENTRY (956, "mu", 0 | V40),
273 ENTRY (957, "nu", 0 | V40),
274 ENTRY (958, "xi", 0 | V40),
275 ENTRY (959, "omicron", 0 | V40),
276 ENTRY (960, "pi", 0 | V40),
277 ENTRY (961, "rho", 0 | V40),
278 ENTRY (962, "sigmaf", 0 | V40),
279 ENTRY (963, "sigma", 0 | V40),
280 ENTRY (964, "tau", 0 | V40),
281 ENTRY (965, "upsilon", 0 | V40),
282 ENTRY (966, "phi", 0 | V40),
283 ENTRY (967, "chi", 0 | V40),
284 ENTRY (968, "psi", 0 | V40),
285 ENTRY (969, "omega", 0 | V40),
286 ENTRY (977, "thetasym", 0 | V40),
287 ENTRY (978, "upsih", 0 | V40),
288 ENTRY (982, "piv", 0 | V40),
289 ENTRY (8194, "ensp", 0 | V40),
290 ENTRY (8195, "emsp", 0 | V40),
291 ENTRY (8201, "thinsp", 0 | V40),
292 ENTRY (8204, "zwnj", 0 | V27 | V40),
293 ENTRY (8205, "zwj", 0 | V27 | V40),
294 ENTRY (8206, "lrm", 0 | V27 | V40),
295 ENTRY (8207, "rlm", 0 | V27 | V40),
296 ENTRY (8211, "ndash", 0 | V40),
297 ENTRY (8212, "mdash", 0 | V40),
298 ENTRY (8216, "lsquo", 0 | V40),
299 ENTRY (8217, "rsquo", 0 | V40),
300 ENTRY (8218, "sbquo", 0 | V40),
301 ENTRY (8220, "ldquo", 0 | V40),
302 ENTRY (8221, "rdquo", 0 | V40),
303 ENTRY (8222, "bdquo", 0 | V40),
304 ENTRY (8224, "dagger", 0 | V40),
305 ENTRY (8225, "Dagger", 0 | V40),
306 ENTRY (8226, "bull", 0 | V40),
307 ENTRY (8230, "hellip", 0 | V40),
308 ENTRY (8240, "permil", 0 | V40),
309 ENTRY (8242, "prime", 0 | V40),
310 ENTRY (8243, "Prime", 0 | V40),
311 ENTRY (8249, "lsaquo", 0 | V40),
312 ENTRY (8250, "rsaquo", 0 | V40),
313 ENTRY (8254, "oline", 0 | V40),
314 ENTRY (8260, "frasl", 0 | V40),
315 ENTRY (8364, "euro", 0 | V40),
316 ENTRY (8465, "image", 0 | V40),
317 ENTRY (8472, "weierp", 0 | V40),
318 ENTRY (8476, "real", 0 | V40),
319 ENTRY (8482, "trade", 0 | V40),
320 ENTRY (8501, "alefsym", 0 | V40),
321 ENTRY (8592, "larr", 0 | V40),
322 ENTRY (8593, "uarr", 0 | V40),
323 ENTRY (8594, "rarr", 0 | V40),
324 ENTRY (8595, "darr", 0 | V40),
325 ENTRY (8596, "harr", 0 | V40),
326 ENTRY (8629, "crarr", 0 | V40),
327 ENTRY (8656, "lArr", 0 | V40),
328 ENTRY (8657, "uArr", 0 | V40),
329 ENTRY (8658, "rArr", 0 | V40),
330 ENTRY (8659, "dArr", 0 | V40),
331 ENTRY (8660, "hArr", 0 | V40),
332 ENTRY (8704, "forall", 0 | V40),
333 ENTRY (8706, "part", 0 | V40),
334 ENTRY (8707, "exist", 0 | V40),
335 ENTRY (8709, "empty", 0 | V40),
336 ENTRY (8711, "nabla", 0 | V40),
337 ENTRY (8712, "isin", 0 | V40),
338 ENTRY (8713, "notin", 0 | V40),
339 ENTRY (8715, "ni", 0 | V40),
340 ENTRY (8719, "prod", 0 | V40),
341 ENTRY (8721, "sum", 0 | V40),
342 ENTRY (8722, "minus", 0 | V40),
343 ENTRY (8727, "lowast", 0 | V40),
344 ENTRY (8730, "radic", 0 | V40),
345 ENTRY (8733, "prop", 0 | V40),
346 ENTRY (8734, "infin", 0 | V40),
347 ENTRY (8736, "ang", 0 | V40),
348 ENTRY (8743, "and", 0 | V40),
349 ENTRY (8744, "or", 0 | V40),
350 ENTRY (8745, "cap", 0 | V40),
351 ENTRY (8746, "cup", 0 | V40),
352 ENTRY (8747, "int", 0 | V40),
353 ENTRY (8756, "there4", 0 | V40),
354 ENTRY (8764, "sim", 0 | V40),
355 ENTRY (8773, "cong", 0 | V40),
356 ENTRY (8776, "asymp", 0 | V40),
357 ENTRY (8800, "ne", 0 | V40),
358 ENTRY (8801, "equiv", 0 | V40),
359 ENTRY (8804, "le", 0 | V40),
360 ENTRY (8805, "ge", 0 | V40),
361 ENTRY (8834, "sub", 0 | V40),
362 ENTRY (8835, "sup", 0 | V40),
363 ENTRY (8836, "nsub", 0 | V40),
364 ENTRY (8838, "sube", 0 | V40),
365 ENTRY (8839, "supe", 0 | V40),
366 ENTRY (8853, "oplus", 0 | V40),
367 ENTRY (8855, "otimes", 0 | V40),
368 ENTRY (8869, "perp", 0 | V40),
369 ENTRY (8901, "sdot", 0 | V40),
370 ENTRY (8968, "lceil", 0 | V40),
371 ENTRY (8969, "rceil", 0 | V40),
372 ENTRY (8970, "lfloor", 0 | V40),
373 ENTRY (8971, "rfloor", 0 | V40),
374 ENTRY (9001, "lang", 0 | V40),
375 ENTRY (9002, "rang", 0 | V40),
376 ENTRY (9674, "loz", 0 | V40),
377 ENTRY (9824, "spades", 0 | V40),
378 ENTRY (9827, "clubs", 0 | V40),
379 ENTRY (9829, "hearts", 0 | V40),
380 ENTRY (9830, "diams", 0 | V40),
381 ENTRY (0, NULL, 0 )
382 };
383
384 #undef ENTRY
385
386 /* UCS-2 to HTML. */
387
388 /*-------------------------------------.
389 | Return hash value given TABLE_SIZE. |
390 `-------------------------------------*/
391
392 static size_t
393 code_hash (const void *void_data, size_t table_size)
394 {
395 struct ucs2_to_string const *data = (struct ucs2_to_string const *) void_data;
396
397 return data->code % table_size;
398 }
399
400 /*----------------------------------------.
401 | Say if two translations are identical. |
402 `----------------------------------------*/
403
404 static bool
405 code_compare (const void *void_first, const void *void_second)
406 {
407 struct ucs2_to_string const *first
408 = (struct ucs2_to_string const *) void_first;
409 struct ucs2_to_string const *second
410 = (struct ucs2_to_string const *) void_second;
411
412 return first->code == second->code;
413 }
414
415 /*-----------------.
416 | Initialisation. |
417 `-----------------*/
418
419 static bool
420 init_ucs2_html (RECODE_STEP step,
421 RECODE_CONST_REQUEST request,
422 RECODE_CONST_OPTION_LIST before_options,
423 RECODE_CONST_OPTION_LIST after_options,
424 unsigned mask)
425 {
426 Hash_table *table;
427 struct ucs2_to_string const *cursor;
428
429 if (before_options || after_options)
430 return false;
431
432 table = hash_initialize (0, NULL, code_hash, code_compare, NULL);
433 if (!table)
434 return false;
435
436 for (cursor = translations; cursor->code; cursor++)
437 if (cursor->flags & mask
438 && (!request->diacritics_only || cursor->code > 128))
439 if (!hash_insert (table, cursor))
440 {
441 hash_free (table);
442 return false;
443 }
444
445 step->step_type = RECODE_UCS2_TO_STRING;
446 step->step_table = table;
447 step->step_table_term_routine = (void (*) (void *)) hash_free;
448 return true;
449 }
450
451 static bool
452 init_ucs2_html_v00 (RECODE_STEP step,
453 RECODE_CONST_REQUEST request,
454 RECODE_CONST_OPTION_LIST before_options,
455 RECODE_CONST_OPTION_LIST after_options)
456 {
457 return
458 init_ucs2_html (step, request, before_options, after_options, V00);
459 }
460
461 static bool
462 init_ucs2_html_v11 (RECODE_STEP step,
463 RECODE_CONST_REQUEST request,
464 RECODE_CONST_OPTION_LIST before_options,
465 RECODE_CONST_OPTION_LIST after_options)
466 {
467 return
468 init_ucs2_html (step, request, before_options, after_options, V11);
469 }
470
471 static bool
472 init_ucs2_html_v20 (RECODE_STEP step,
473 RECODE_CONST_REQUEST request,
474 RECODE_CONST_OPTION_LIST before_options,
475 RECODE_CONST_OPTION_LIST after_options)
476 {
477 return
478 init_ucs2_html (step, request, before_options, after_options, V20);
479 }
480
481 static bool
482 init_ucs2_html_v27 (RECODE_STEP step,
483 RECODE_CONST_REQUEST request,
484 RECODE_CONST_OPTION_LIST before_options,
485 RECODE_CONST_OPTION_LIST after_options)
486 {
487 return
488 init_ucs2_html (step, request, before_options, after_options, V27);
489 }
490
491 static bool
492 init_ucs2_html_v32 (RECODE_STEP step,
493 RECODE_CONST_REQUEST request,
494 RECODE_CONST_OPTION_LIST before_options,
495 RECODE_CONST_OPTION_LIST after_options)
496 {
497 return
498 init_ucs2_html (step, request, before_options, after_options, V32);
499 }
500
501 static bool
502 init_ucs2_html_v40 (RECODE_STEP step,
503 RECODE_CONST_REQUEST request,
504 RECODE_CONST_OPTION_LIST before_options,
505 RECODE_CONST_OPTION_LIST after_options)
506 {
507 return
508 init_ucs2_html (step, request, before_options, after_options, V40);
509 }
510
511 /*-----------------.
512 | Transformation. |
513 `-----------------*/
514
515 static bool
516 transform_ucs2_html (RECODE_SUBTASK subtask)
517 {
518 Hash_table *table = (Hash_table *) subtask->step->step_table;
519 unsigned value;
520
521 while (recode_get_ucs2 (&value, subtask))
522 {
523 struct ucs2_to_string lookup;
524 struct ucs2_to_string *entry;
525
526 lookup.code = value;
527 entry = (struct ucs2_to_string *) hash_lookup (table, &lookup);
528 if (entry)
529 {
530 const char *cursor = entry->string;
531
532 recode_put_byte ('&', subtask);
533 while (*cursor)
534 {
535 recode_put_byte (*cursor, subtask);
536 cursor++;
537 }
538 recode_put_byte (';', subtask);
539 }
540 else if ((value < 32 && value != '\n' && value != '\t') || value >= 127)
541 {
542 unsigned divider = 10000;
543
544 recode_put_byte ('&', subtask);
545 recode_put_byte ('#', subtask);
546 while (divider > value)
547 divider /= 10;
548 while (divider > 1)
549 {
550 recode_put_byte ('0' + value / divider, subtask);
551 value %= divider;
552 divider /= 10;
553 }
554 recode_put_byte ('0' + value, subtask);
555 recode_put_byte (';', subtask);
556 }
557 else
558 recode_put_byte(value, subtask);
559 }
560
561 SUBTASK_RETURN (subtask);
562 }
563
564 /* HTML to UCS-2. */
565
566 #define ENTITY_BUFFER_LENGTH 20
567
568 /*
569 &quot; { if (request->diacritics_only) ECHO; else recode_put_ucs2 (34, subtask); }
570 &amp; { if (request->diacritics_only) ECHO; else recode_put_ucs2 (38, subtask); }
571 &lt; { if (request->diacritics_only) ECHO; else recode_put_ucs2 (60, subtask); }
572 &gt; { if (request->diacritics_only) ECHO; else recode_put_ucs2 (62, subtask); }
573 */
574
575 /*-------------------------------------.
576 | Return hash value given TABLE_SIZE. |
577 `-------------------------------------*/
578
579 static size_t
580 string_hash (const void *void_data, size_t table_size)
581 {
582 struct ucs2_to_string const *data = (struct ucs2_to_string const *) void_data;
583
584 return hash_string (data->string, table_size);
585 }
586
587 /*----------------------------------------.
588 | Say if two translations are identical. |
589 `----------------------------------------*/
590
591 static bool
592 string_compare (const void *void_first, const void *void_second)
593 {
594 struct ucs2_to_string const *first = (struct ucs2_to_string const *) void_first;
595 struct ucs2_to_string const *second = (struct ucs2_to_string const *) void_second;
596
597 return strcmp (first->string, second->string) == 0;
598 }
599
600 /*-----------------.
601 | Initialisation. |
602 `-----------------*/
603
604 static bool
605 init_html_ucs2 (RECODE_STEP step,
606 RECODE_CONST_REQUEST request,
607 RECODE_CONST_OPTION_LIST before_options,
608 RECODE_CONST_OPTION_LIST after_options,
609 unsigned mask)
610 {
611 Hash_table *table;
612 struct ucs2_to_string const *cursor;
613
614 if (before_options || after_options)
615 return false;
616
617 table = hash_initialize (0, NULL, string_hash, string_compare, NULL);
618 if (!table)
619 return false;
620
621 for (cursor = translations; cursor->code; cursor++)
622 if (cursor->flags & mask
623 && (!request->diacritics_only || cursor->code > 128))
624 if (!hash_insert (table, cursor))
625 {
626 hash_free (table);
627 return false;
628 }
629
630 step->step_type = RECODE_STRING_TO_UCS2;
631 step->step_table = table;
632 step->step_table_term_routine = (void (*) (void *)) hash_free;
633 return true;
634 }
635
636 static bool
637 init_html_v00_ucs2 (RECODE_STEP step,
638 RECODE_CONST_REQUEST request,
639 RECODE_CONST_OPTION_LIST before_options,
640 RECODE_CONST_OPTION_LIST after_options)
641 {
642 return
643 init_html_ucs2 (step, request, before_options, after_options, V00);
644 }
645
646 static bool
647 init_html_v11_ucs2 (RECODE_STEP step,
648 RECODE_CONST_REQUEST request,
649 RECODE_CONST_OPTION_LIST before_options,
650 RECODE_CONST_OPTION_LIST after_options)
651 {
652 return
653 init_html_ucs2 (step, request, before_options, after_options, V11);
654 }
655
656 static bool
657 init_html_v20_ucs2 (RECODE_STEP step,
658 RECODE_CONST_REQUEST request,
659 RECODE_CONST_OPTION_LIST before_options,
660 RECODE_CONST_OPTION_LIST after_options)
661 {
662 return
663 init_html_ucs2 (step, request, before_options, after_options, V20);
664 }
665
666 static bool
667 init_html_v27_ucs2 (RECODE_STEP step,
668 RECODE_CONST_REQUEST request,
669 RECODE_CONST_OPTION_LIST before_options,
670 RECODE_CONST_OPTION_LIST after_options)
671 {
672 return
673 init_html_ucs2 (step, request, before_options, after_options, V27);
674 }
675
676 static bool
677 init_html_v32_ucs2 (RECODE_STEP step,
678 RECODE_CONST_REQUEST request,
679 RECODE_CONST_OPTION_LIST before_options,
680 RECODE_CONST_OPTION_LIST after_options)
681 {
682 return
683 init_html_ucs2 (step, request, before_options, after_options, V32);
684 }
685
686 static bool
687 init_html_v40_ucs2 (RECODE_STEP step,
688 RECODE_CONST_REQUEST request,
689 RECODE_CONST_OPTION_LIST before_options,
690 RECODE_CONST_OPTION_LIST after_options)
691 {
692 return
693 init_html_ucs2 (step, request, before_options, after_options, V40);
694 }
695
696 /*-----------------.
697 | Transformation. |
698 `-----------------*/
699
700 static bool
701 transform_html_ucs2 (RECODE_SUBTASK subtask)
702 {
703 RECODE_CONST_REQUEST request = subtask->task->request;
704 int input_char;
705
706 input_char = recode_get_byte (subtask);
707 if (input_char != EOF)
708 recode_put_ucs2 (BYTE_ORDER_MARK, subtask); /* FIXME: experimental */
709
710 /* According to RFC 2718 and the Unicode Standard, if you declare the
711 character encoding of your page using HTTP as either "UTF-16LE" or
712 "UTF-16BE" then you should not use a byte-order mark at the beginning
713 of the page. Only if the page is labelled in HTTP using IANA charset
714 name "UTF-16" is a byte-order mark appropriate. */
715
716 while (input_char != EOF)
717
718 if (input_char == '&')
719 {
720 char buffer[ENTITY_BUFFER_LENGTH];
721 char *cursor = buffer;
722 bool valid = true;
723 bool echo = false;
724
725 input_char = recode_get_byte (subtask);
726 if (input_char == '#')
727 {
728 input_char = recode_get_byte (subtask);
729 if (input_char == 'x' || input_char == 'X')
730 {
731 unsigned value = 0;
732
733 /* Scan &#[xX][0-9a-fA-F]+; notation. */
734
735 *cursor++ = '#';
736 *cursor++ = input_char;
737 input_char = recode_get_byte (subtask);
738
739 while (valid)
740 {
741 if (input_char >= '0' && input_char <= '9')
742 value = 16 * value + input_char - '0';
743 else if (input_char >= 'A' && input_char <= 'F')
744 value = 16 * value + input_char - 'A' + 10;
745 else if (input_char >= 'a' && input_char <= 'f')
746 value = 16 * value + input_char - 'a' + 10;
747 else
748 break;
749
750 if (value >= 65535)
751 valid = false;
752 else if (cursor == buffer + ENTITY_BUFFER_LENGTH - 2)
753 valid = false;
754 else
755 {
756 *cursor++ = input_char;
757 input_char = recode_get_byte (subtask);
758 }
759 }
760
761 /* No digit found at all? */
762 if ((cursor - buffer) == 2)
763 valid = false;
764
765 if (valid)
766 if (request->diacritics_only)
767 {
768 echo = true;
769 *cursor = '\0';
770 }
771 else
772 {
773 recode_put_ucs2 (value, subtask);
774 if (input_char == ';')
775 input_char = recode_get_byte (subtask);
776 }
777 else
778 *cursor = '\0';
779 }
780 else
781 {
782 unsigned value = 0;
783
784 /* Scan &#[0-9]+; notation. */
785
786 *cursor++ = '#';
787
788 while (valid)
789 {
790 if (input_char >= '0' && input_char <= '9')
791 value = 10 * value + input_char - '0';
792 else
793 break;
794
795 if (value >= 65535)
796 valid = false;
797 else if (cursor == buffer + ENTITY_BUFFER_LENGTH - 2)
798 valid = false;
799 else
800 {
801 *cursor++ = input_char;
802 input_char = recode_get_byte (subtask);
803 }
804 }
805
806 /* No digit found at all? */
807 if ((cursor - buffer) == 1)
808 valid = false;
809
810 if (valid)
811 if (request->diacritics_only)
812 {
813 echo = true;
814 *cursor = '\0';
815 }
816 else
817 {
818 recode_put_ucs2 (value, subtask);
819 if (input_char == ';')
820 input_char = recode_get_byte (subtask);
821 }
822 else
823 *cursor = '\0';
824 }
825 }
826 else if ((input_char >= 'A' && input_char <= 'Z')
827 || (input_char >= 'a' && input_char <= 'z'))
828 {
829 /* Scan &[A-Za-z][A-Za-z0-9]*; notation. */
830
831 *cursor++ = input_char;
832 input_char = recode_get_byte (subtask);
833
834 while (valid
835 && input_char != EOF
836 && ((input_char >= 'A' && input_char <= 'Z')
837 || (input_char >= 'a' && input_char <= 'z')
838 || (input_char >= '0' && input_char <= '9')))
839 if (cursor == buffer + ENTITY_BUFFER_LENGTH - 2)
840 valid = false;
841 else
842 {
843 *cursor++ = input_char;
844 input_char = recode_get_byte (subtask);
845 }
846 *cursor = '\0';
847
848 if (valid)
849 {
850 struct ucs2_to_string lookup;
851 struct ucs2_to_string *entry;
852
853 lookup.string = buffer;
854 entry = (struct ucs2_to_string *) hash_lookup
855 ((const Hash_table *) subtask->step->step_table, &lookup);
856 if (entry)
857 {
858 recode_put_ucs2 (entry->code, subtask);
859 if (input_char == ';')
860 input_char = recode_get_byte (subtask);
861 }
862 else
863 valid = false;
864 }
865 }
866 else /* Not '#' or A-z, so not a valid entity. */
867 {
868 valid = false;
869 /* Push back '&', but do not output current input_char yet. */
870 *cursor = '\0';
871 }
872
873 if (echo || !valid)
874 {
875 recode_put_ucs2 ('&', subtask);
876 for (cursor = buffer; *cursor; cursor++)
877 recode_put_ucs2 (*cursor, subtask);
878 /* input_char is processed again in the next iteration. */
879 }
880 }
881 else
882 {
883 recode_put_ucs2 (input_char, subtask);
884 input_char = recode_get_byte (subtask);
885 }
886
887 SUBTASK_RETURN (subtask);
888 }
889
890 /* Module declaration. */
891 bool
892 module_html (RECODE_OUTER outer)
893 {
894 return
895 recode_declare_single (outer, "ISO-10646-UCS-2", "XML-standalone",
896 outer->quality_byte_to_variable,
897 init_ucs2_html_v00, transform_ucs2_html)
898 && recode_declare_single (outer, "XML-standalone", "ISO-10646-UCS-2",
899 outer->quality_variable_to_byte,
900 init_html_v00_ucs2, transform_html_ucs2)
901 && recode_declare_single (outer, "ISO-10646-UCS-2", "HTML_1.1",
902 outer->quality_byte_to_variable,
903 init_ucs2_html_v11, transform_ucs2_html)
904 && recode_declare_single (outer, "HTML_1.1", "ISO-10646-UCS-2",
905 outer->quality_variable_to_byte,
906 init_html_v11_ucs2, transform_html_ucs2)
907 && recode_declare_single (outer, "ISO-10646-UCS-2", "HTML_2.0",
908 outer->quality_byte_to_variable,
909 init_ucs2_html_v20, transform_ucs2_html)
910 && recode_declare_single (outer, "HTML_2.0", "ISO-10646-UCS-2",
911 outer->quality_variable_to_byte,
912 init_html_v20_ucs2, transform_html_ucs2)
913 && recode_declare_single (outer, "ISO-10646-UCS-2", "HTML-i18n",
914 outer->quality_byte_to_variable,
915 init_ucs2_html_v27, transform_ucs2_html)
916 && recode_declare_single (outer, "HTML-i18n", "ISO-10646-UCS-2",
917 outer->quality_variable_to_byte,
918 init_html_v27_ucs2, transform_html_ucs2)
919 && recode_declare_single (outer, "ISO-10646-UCS-2", "HTML_3.2",
920 outer->quality_byte_to_variable,
921 init_ucs2_html_v32, transform_ucs2_html)
922 && recode_declare_single (outer, "HTML_3.2", "ISO-10646-UCS-2",
923 outer->quality_variable_to_byte,
924 init_html_v32_ucs2, transform_html_ucs2)
925 && recode_declare_single (outer, "ISO-10646-UCS-2", "HTML_4.0",
926 outer->quality_byte_to_variable,
927 init_ucs2_html_v40, transform_ucs2_html)
928 && recode_declare_single (outer, "HTML_4.0", "ISO-10646-UCS-2",
929 outer->quality_variable_to_byte,
930 init_html_v40_ucs2, transform_html_ucs2)
931
932 && recode_declare_alias (outer, "h0", "XML-standalone")
933 && recode_declare_alias (outer, "h1", "HTML_1.1")
934 && recode_declare_alias (outer, "RFC1866", "HTML_2.0")
935 && recode_declare_alias (outer, "1866", "HTML_2.0")
936 && recode_declare_alias (outer, "h2", "HTML_2.0")
937 && recode_declare_alias (outer, "RFC2070", "HTML-i18n")
938 && recode_declare_alias (outer, "2070", "HTML-i18n")
939 && recode_declare_alias (outer, "h3", "HTML_3.2")
940 && recode_declare_alias (outer, "h4", "HTML_4.0")
941 /* HTML defaults to the highest level available. */
942 && recode_declare_alias (outer, "HTML", "HTML_4.0")
943 && recode_declare_alias (outer, "h", "HTML_4.0");
944 }
945
946 void
947 delmodule_html (_GL_UNUSED RECODE_OUTER outer)
948 {
949 }