771
|
1 /* Code to handle Unicode conversion.
|
1267
|
2 Copyright (C) 2000, 2001, 2002, 2003 Ben Wing.
|
771
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: FSF 20.3. Not in FSF. */
|
|
22
|
|
23 /* Authorship:
|
|
24
|
|
25 Current primary author: Ben Wing <ben@xemacs.org>
|
|
26
|
|
27 Written by Ben Wing <ben@xemacs.org>, June, 2001.
|
|
28 Separated out into this file, August, 2001.
|
|
29 Includes Unicode coding systems, some parts of which have been written
|
877
|
30 by someone else. #### Morioka and Hayashi, I think.
|
771
|
31
|
|
32 As of September 2001, the detection code is here and abstraction of the
|
877
|
33 detection system is finished. The unicode detectors have been rewritten
|
771
|
34 to include multiple levels of likelihood.
|
|
35 */
|
|
36
|
|
37 #include <config.h>
|
|
38 #include "lisp.h"
|
|
39
|
|
40 #include "charset.h"
|
|
41 #include "file-coding.h"
|
|
42 #include "opaque.h"
|
|
43
|
|
44 #include "sysfile.h"
|
|
45
|
|
46 /* #### WARNING! The current sledgehammer routines have a fundamental
|
|
47 problem in that they can't handle two characters mapping to a
|
|
48 single Unicode codepoint or vice-versa in a single charset table.
|
|
49 It's not clear there is any way to handle this and still make the
|
877
|
50 sledgehammer routines useful.
|
|
51
|
|
52 Inquiring Minds Want To Know Dept: does the above WARNING mean that
|
|
53 _if_ it happens, then it will signal error, or then it will do
|
|
54 something evil and unpredictable? Signaling an error is OK: for
|
|
55 all national standards, the national to Unicode map is an inclusion
|
|
56 (1-to-1). Any character set that does not behave that way is
|
1318
|
57 broken according to the Unicode standard.
|
|
58
|
|
59 Answer: You will get an abort(), since the purpose of the sledgehammer
|
|
60 routines is self-checking. The above problem with non-1-to-1 mapping
|
|
61 occurs in the Big5 tables, as provided by the Unicode Consortium. */
|
877
|
62
|
771
|
63 /* #define SLEDGEHAMMER_CHECK_UNICODE */
|
|
64
|
|
65 /* We currently use the following format for tables:
|
|
66
|
|
67 If dimension == 1, to_unicode_table is a 96-element array of ints
|
|
68 (Unicode code points); else, it's a 96-element array of int *
|
|
69 pointers, each of which points to a 96-element array of ints. If no
|
|
70 elements in a row have been filled in, the pointer will point to a
|
|
71 default empty table; that way, memory usage is more reasonable but
|
|
72 lookup still fast.
|
|
73
|
|
74 -- If from_unicode_levels == 1, from_unicode_table is a 256-element
|
|
75 array of shorts (octet 1 in high byte, octet 2 in low byte; we don't
|
867
|
76 store Ichars directly to save space).
|
771
|
77
|
|
78 -- If from_unicode_levels == 2, from_unicode_table is a
|
|
79 256-element array of short * pointers, each of which points to a
|
|
80 256-element array of shorts.
|
|
81
|
|
82 -- If from_unicode_levels == 3, from_unicode_table is a
|
|
83 256-element array of short ** pointers, each of which points to
|
|
84 a 256-element array of short * pointers, each of which points to
|
|
85 a 256-element array of shorts.
|
|
86
|
|
87 -- If from_unicode_levels == 4, same thing but one level deeper.
|
|
88
|
|
89 Just as for to_unicode_table, we use default tables to fill in
|
|
90 all entries with no values in them.
|
|
91
|
|
92 #### An obvious space-saving optimization is to use variable-sized
|
|
93 tables, where each table instead of just being a 256-element array,
|
|
94 is a structure with a start value, an end value, and a variable
|
|
95 number of entries (END - START + 1). Only 8 bits are needed for
|
|
96 END and START, and could be stored at the end to avoid alignment
|
|
97 problems. However, before charging off and implementing this,
|
|
98 we need to consider whether it's worth it:
|
|
99
|
|
100 (1) Most tables will be highly localized in which code points are
|
|
101 defined, heavily reducing the possible memory waste. Before
|
|
102 doing any rewriting, write some code to see how much memory is
|
|
103 actually being wasted (i.e. ratio of empty entries to total # of
|
|
104 entries) and only start rewriting if it's unacceptably high. You
|
|
105 have to check over all charsets.
|
|
106
|
|
107 (2) Since entries are usually added one at a time, you have to be
|
|
108 very careful when creating the tables to avoid realloc()/free()
|
|
109 thrashing in the common case when you are in an area of high
|
|
110 localization and are going to end up using most entries in the
|
|
111 table. You'd certainly want to allow only certain sizes, not
|
|
112 arbitrary ones (probably powers of 2, where you want the entire
|
|
113 block including the START/END values to fit into a power of 2,
|
|
114 minus any malloc overhead if there is any -- there's none under
|
|
115 gmalloc.c, and probably most system malloc() functions are quite
|
|
116 smart nowadays and also have no overhead). You could optimize
|
|
117 somewhat during the in-C initializations, because you can compute
|
|
118 the actual usage of various tables by scanning the entries you're
|
|
119 going to add in a separate pass before adding them. (You could
|
|
120 actually do the same thing when entries are added on the Lisp
|
|
121 level by making the assumption that all the entries will come in
|
|
122 one after another before any use is made of the data. So as
|
|
123 they're coming in, you just store them in a big long list, and
|
|
124 the first time you need to retrieve an entry, you compute the
|
|
125 whole table at once.) You'd still have to deal with the
|
|
126 possibility of later entries coming in, though.
|
|
127
|
|
128 (3) You do lose some speed using START/END values, since you need
|
|
129 a couple of comparisons at each level. This could easily make
|
|
130 each single lookup become 3-4 times slower. The Unicode book
|
|
131 considers this a big issue, and recommends against variable-sized
|
|
132 tables for this reason; however, they almost certainly have in
|
|
133 mind applications that primarily involve conversion of large
|
|
134 amounts of data. Most Unicode strings that are translated in
|
|
135 XEmacs are fairly small. The only place where this might matter
|
|
136 is in loading large files -- e.g. a 3-megabyte Unicode-encoded
|
|
137 file. So think about this, and maybe do a trial implementation
|
|
138 where you don't worry too much about the intricacies of (2) and
|
|
139 just implement some basic "multiply by 1.5" trick or something to
|
|
140 do the resizing. There is a very good FAQ on Unicode called
|
|
141 something like the Linux-Unicode How-To (it should be part of the
|
|
142 Linux How-To's, I think), that lists the url of a guy with a
|
|
143 whole bunch of unicode files you can use to stress-test your
|
|
144 implementations, and he's highly likely to have a good
|
|
145 multi-megabyte Unicode-encoded file (with normal text in it -- if
|
|
146 you created your own just by creating repeated strings of letters
|
|
147 and numbers, you probably wouldn't get accurate results).
|
|
148 */
|
|
149
|
|
150 /* When MULE is not defined, we may still need some Unicode support --
|
|
151 in particular, some Windows API's always want Unicode, and the way
|
|
152 we've set up the Unicode encapsulation, we may as well go ahead and
|
|
153 always use the Unicode versions of split API's. (It would be
|
|
154 trickier to not use them, and pointless -- under NT, the ANSI API's
|
|
155 call the Unicode ones anyway, so in the case of structures, we'd be
|
|
156 converting from Unicode to ANSI structures, only to have the OS
|
|
157 convert them back.) */
|
|
158
|
|
159 Lisp_Object Qunicode;
|
|
160 Lisp_Object Qutf_16, Qutf_8, Qucs_4, Qutf_7;
|
|
161 Lisp_Object Qneed_bom;
|
|
162
|
|
163 Lisp_Object Qutf_16_little_endian, Qutf_16_bom;
|
|
164 Lisp_Object Qutf_16_little_endian_bom;
|
|
165
|
985
|
166 Lisp_Object Qutf_8_bom;
|
|
167
|
771
|
168 #ifdef MULE
|
|
169
|
877
|
170 /* #### Using ints for to_unicode is OK (as long as they are >= 32 bits).
|
1318
|
171 However, shouldn't the shorts below be unsigned?
|
|
172
|
|
173 Answer: Doesn't matter because the values being converted to are only
|
|
174 96x96. */
|
771
|
175 static int *to_unicode_blank_1;
|
|
176 static int **to_unicode_blank_2;
|
|
177
|
|
178 static short *from_unicode_blank_1;
|
|
179 static short **from_unicode_blank_2;
|
|
180 static short ***from_unicode_blank_3;
|
|
181 static short ****from_unicode_blank_4;
|
|
182
|
1204
|
183 static const struct memory_description to_unicode_level_0_desc_1[] = {
|
771
|
184 { XD_END }
|
|
185 };
|
|
186
|
1204
|
187 static const struct sized_memory_description to_unicode_level_0_desc = {
|
|
188 sizeof (int), to_unicode_level_0_desc_1
|
771
|
189 };
|
|
190
|
1204
|
191 static const struct memory_description to_unicode_level_1_desc_1[] = {
|
|
192 { XD_STRUCT_PTR, 0, 96, &to_unicode_level_0_desc },
|
771
|
193 { XD_END }
|
|
194 };
|
|
195
|
1204
|
196 static const struct sized_memory_description to_unicode_level_1_desc = {
|
|
197 sizeof (void *), to_unicode_level_1_desc_1
|
771
|
198 };
|
|
199
|
1204
|
200 static const struct memory_description to_unicode_description_1[] = {
|
|
201 { XD_STRUCT_PTR, 1, 96, &to_unicode_level_0_desc },
|
|
202 { XD_STRUCT_PTR, 2, 96, &to_unicode_level_1_desc },
|
771
|
203 { XD_END }
|
|
204 };
|
|
205
|
|
206 /* Not static because each charset has a set of to and from tables and
|
|
207 needs to describe them to pdump. */
|
1204
|
208 const struct sized_memory_description to_unicode_description = {
|
|
209 sizeof (void *), to_unicode_description_1
|
|
210 };
|
|
211
|
|
212 static const struct memory_description from_unicode_level_0_desc_1[] = {
|
771
|
213 { XD_END }
|
|
214 };
|
|
215
|
1204
|
216 static const struct sized_memory_description from_unicode_level_0_desc = {
|
|
217 sizeof (short), from_unicode_level_0_desc_1
|
771
|
218 };
|
|
219
|
1204
|
220 static const struct memory_description from_unicode_level_1_desc_1[] = {
|
|
221 { XD_STRUCT_PTR, 0, 256, &from_unicode_level_0_desc },
|
771
|
222 { XD_END }
|
|
223 };
|
|
224
|
1204
|
225 static const struct sized_memory_description from_unicode_level_1_desc = {
|
|
226 sizeof (void *), from_unicode_level_1_desc_1
|
771
|
227 };
|
|
228
|
1204
|
229 static const struct memory_description from_unicode_level_2_desc_1[] = {
|
|
230 { XD_STRUCT_PTR, 0, 256, &from_unicode_level_1_desc },
|
771
|
231 { XD_END }
|
|
232 };
|
|
233
|
1204
|
234 static const struct sized_memory_description from_unicode_level_2_desc = {
|
|
235 sizeof (void *), from_unicode_level_2_desc_1
|
771
|
236 };
|
|
237
|
1204
|
238 static const struct memory_description from_unicode_level_3_desc_1[] = {
|
|
239 { XD_STRUCT_PTR, 0, 256, &from_unicode_level_2_desc },
|
771
|
240 { XD_END }
|
|
241 };
|
|
242
|
1204
|
243 static const struct sized_memory_description from_unicode_level_3_desc = {
|
|
244 sizeof (void *), from_unicode_level_3_desc_1
|
771
|
245 };
|
|
246
|
1204
|
247 static const struct memory_description from_unicode_description_1[] = {
|
|
248 { XD_STRUCT_PTR, 1, 256, &from_unicode_level_0_desc },
|
|
249 { XD_STRUCT_PTR, 2, 256, &from_unicode_level_1_desc },
|
|
250 { XD_STRUCT_PTR, 3, 256, &from_unicode_level_2_desc },
|
|
251 { XD_STRUCT_PTR, 4, 256, &from_unicode_level_3_desc },
|
771
|
252 { XD_END }
|
|
253 };
|
|
254
|
|
255 /* Not static because each charset has a set of to and from tables and
|
|
256 needs to describe them to pdump. */
|
1204
|
257 const struct sized_memory_description from_unicode_description = {
|
|
258 sizeof (void *), from_unicode_description_1
|
771
|
259 };
|
|
260
|
|
261 static Lisp_Object_dynarr *unicode_precedence_dynarr;
|
|
262
|
1204
|
263 static const struct memory_description lod_description_1[] = {
|
|
264 XD_DYNARR_DESC (Lisp_Object_dynarr, &lisp_object_description),
|
771
|
265 { XD_END }
|
|
266 };
|
|
267
|
1204
|
268 static const struct sized_memory_description lisp_object_dynarr_description = {
|
771
|
269 sizeof (Lisp_Object_dynarr),
|
|
270 lod_description_1
|
|
271 };
|
|
272
|
|
273 Lisp_Object Vlanguage_unicode_precedence_list;
|
|
274 Lisp_Object Vdefault_unicode_precedence_list;
|
|
275
|
|
276 Lisp_Object Qignore_first_column;
|
|
277
|
|
278
|
|
279 /************************************************************************/
|
|
280 /* Unicode implementation */
|
|
281 /************************************************************************/
|
|
282
|
|
283 #define BREAKUP_UNICODE_CODE(val, u1, u2, u3, u4, levels) \
|
|
284 do { \
|
|
285 int buc_val = (val); \
|
|
286 \
|
|
287 (u1) = buc_val >> 24; \
|
|
288 (u2) = (buc_val >> 16) & 255; \
|
|
289 (u3) = (buc_val >> 8) & 255; \
|
|
290 (u4) = buc_val & 255; \
|
|
291 (levels) = (buc_val <= 0xFF ? 1 : \
|
|
292 buc_val <= 0xFFFF ? 2 : \
|
|
293 buc_val <= 0xFFFFFF ? 3 : \
|
|
294 4); \
|
|
295 } while (0)
|
|
296
|
|
297 static void
|
|
298 init_blank_unicode_tables (void)
|
|
299 {
|
|
300 int i;
|
|
301
|
|
302 from_unicode_blank_1 = xnew_array (short, 256);
|
|
303 from_unicode_blank_2 = xnew_array (short *, 256);
|
|
304 from_unicode_blank_3 = xnew_array (short **, 256);
|
|
305 from_unicode_blank_4 = xnew_array (short ***, 256);
|
|
306 for (i = 0; i < 256; i++)
|
|
307 {
|
877
|
308 /* #### IMWTK: Why does using -1 here work? Simply because there are
|
1318
|
309 no existing 96x96 charsets?
|
|
310
|
|
311 Answer: I don't understand the concern. -1 indicates there is no
|
|
312 entry for this particular codepoint, which is always the case for
|
|
313 blank tables. */
|
771
|
314 from_unicode_blank_1[i] = (short) -1;
|
|
315 from_unicode_blank_2[i] = from_unicode_blank_1;
|
|
316 from_unicode_blank_3[i] = from_unicode_blank_2;
|
|
317 from_unicode_blank_4[i] = from_unicode_blank_3;
|
|
318 }
|
|
319
|
|
320 to_unicode_blank_1 = xnew_array (int, 96);
|
|
321 to_unicode_blank_2 = xnew_array (int *, 96);
|
|
322 for (i = 0; i < 96; i++)
|
|
323 {
|
877
|
324 /* Here -1 is guaranteed OK. */
|
771
|
325 to_unicode_blank_1[i] = -1;
|
|
326 to_unicode_blank_2[i] = to_unicode_blank_1;
|
|
327 }
|
|
328 }
|
|
329
|
|
330 static void *
|
|
331 create_new_from_unicode_table (int level)
|
|
332 {
|
|
333 switch (level)
|
|
334 {
|
|
335 /* WARNING: If you are thinking of compressing these, keep in
|
|
336 mind that sizeof (short) does not equal sizeof (short *). */
|
|
337 case 1:
|
|
338 {
|
|
339 short *newtab = xnew_array (short, 256);
|
|
340 memcpy (newtab, from_unicode_blank_1, 256 * sizeof (short));
|
|
341 return newtab;
|
|
342 }
|
|
343 case 2:
|
|
344 {
|
|
345 short **newtab = xnew_array (short *, 256);
|
|
346 memcpy (newtab, from_unicode_blank_2, 256 * sizeof (short *));
|
|
347 return newtab;
|
|
348 }
|
|
349 case 3:
|
|
350 {
|
|
351 short ***newtab = xnew_array (short **, 256);
|
|
352 memcpy (newtab, from_unicode_blank_3, 256 * sizeof (short **));
|
|
353 return newtab;
|
|
354 }
|
|
355 case 4:
|
|
356 {
|
|
357 short ****newtab = xnew_array (short ***, 256);
|
|
358 memcpy (newtab, from_unicode_blank_4, 256 * sizeof (short ***));
|
|
359 return newtab;
|
|
360 }
|
|
361 default:
|
|
362 abort ();
|
|
363 return 0;
|
|
364 }
|
|
365 }
|
|
366
|
877
|
367 /* Allocate and blank the tables.
|
1318
|
368 Loading them up is done by load-unicode-mapping-table. */
|
771
|
369 void
|
|
370 init_charset_unicode_tables (Lisp_Object charset)
|
|
371 {
|
|
372 if (XCHARSET_DIMENSION (charset) == 1)
|
|
373 {
|
|
374 int *to_table = xnew_array (int, 96);
|
|
375 memcpy (to_table, to_unicode_blank_1, 96 * sizeof (int));
|
|
376 XCHARSET_TO_UNICODE_TABLE (charset) = to_table;
|
|
377 }
|
|
378 else
|
|
379 {
|
|
380 int **to_table = xnew_array (int *, 96);
|
|
381 memcpy (to_table, to_unicode_blank_2, 96 * sizeof (int *));
|
|
382 XCHARSET_TO_UNICODE_TABLE (charset) = to_table;
|
|
383 }
|
|
384
|
|
385 {
|
|
386 XCHARSET_FROM_UNICODE_TABLE (charset) = create_new_from_unicode_table (1);
|
|
387 XCHARSET_FROM_UNICODE_LEVELS (charset) = 1;
|
|
388 }
|
|
389 }
|
|
390
|
|
391 static void
|
|
392 free_from_unicode_table (void *table, int level)
|
|
393 {
|
|
394 int i;
|
|
395
|
|
396 switch (level)
|
|
397 {
|
|
398 case 2:
|
|
399 {
|
|
400 short **tab = (short **) table;
|
|
401 for (i = 0; i < 256; i++)
|
|
402 {
|
|
403 if (tab[i] != from_unicode_blank_1)
|
|
404 free_from_unicode_table (tab[i], 1);
|
|
405 }
|
|
406 break;
|
|
407 }
|
|
408 case 3:
|
|
409 {
|
|
410 short ***tab = (short ***) table;
|
|
411 for (i = 0; i < 256; i++)
|
|
412 {
|
|
413 if (tab[i] != from_unicode_blank_2)
|
|
414 free_from_unicode_table (tab[i], 2);
|
|
415 }
|
|
416 break;
|
|
417 }
|
|
418 case 4:
|
|
419 {
|
|
420 short ****tab = (short ****) table;
|
|
421 for (i = 0; i < 256; i++)
|
|
422 {
|
|
423 if (tab[i] != from_unicode_blank_3)
|
|
424 free_from_unicode_table (tab[i], 3);
|
|
425 }
|
|
426 break;
|
|
427 }
|
|
428 }
|
|
429
|
1726
|
430 xfree (table, void *);
|
771
|
431 }
|
|
432
|
|
433 static void
|
|
434 free_to_unicode_table (void *table, int level)
|
|
435 {
|
|
436 if (level == 2)
|
|
437 {
|
|
438 int i;
|
|
439 int **tab = (int **) table;
|
|
440
|
|
441 for (i = 0; i < 96; i++)
|
|
442 {
|
|
443 if (tab[i] != to_unicode_blank_1)
|
|
444 free_to_unicode_table (tab[i], 1);
|
|
445 }
|
|
446 }
|
|
447
|
1726
|
448 xfree (table, void *);
|
771
|
449 }
|
|
450
|
|
451 void
|
|
452 free_charset_unicode_tables (Lisp_Object charset)
|
|
453 {
|
|
454 free_to_unicode_table (XCHARSET_TO_UNICODE_TABLE (charset),
|
|
455 XCHARSET_DIMENSION (charset));
|
|
456 free_from_unicode_table (XCHARSET_FROM_UNICODE_TABLE (charset),
|
|
457 XCHARSET_FROM_UNICODE_LEVELS (charset));
|
|
458 }
|
|
459
|
|
460 #ifdef MEMORY_USAGE_STATS
|
|
461
|
|
462 static Bytecount
|
|
463 compute_from_unicode_table_size_1 (void *table, int level,
|
|
464 struct overhead_stats *stats)
|
|
465 {
|
|
466 int i;
|
|
467 Bytecount size = 0;
|
|
468
|
|
469 switch (level)
|
|
470 {
|
|
471 case 2:
|
|
472 {
|
|
473 short **tab = (short **) table;
|
|
474 for (i = 0; i < 256; i++)
|
|
475 {
|
|
476 if (tab[i] != from_unicode_blank_1)
|
|
477 size += compute_from_unicode_table_size_1 (tab[i], 1, stats);
|
|
478 }
|
|
479 break;
|
|
480 }
|
|
481 case 3:
|
|
482 {
|
|
483 short ***tab = (short ***) table;
|
|
484 for (i = 0; i < 256; i++)
|
|
485 {
|
|
486 if (tab[i] != from_unicode_blank_2)
|
|
487 size += compute_from_unicode_table_size_1 (tab[i], 2, stats);
|
|
488 }
|
|
489 break;
|
|
490 }
|
|
491 case 4:
|
|
492 {
|
|
493 short ****tab = (short ****) table;
|
|
494 for (i = 0; i < 256; i++)
|
|
495 {
|
|
496 if (tab[i] != from_unicode_blank_3)
|
|
497 size += compute_from_unicode_table_size_1 (tab[i], 3, stats);
|
|
498 }
|
|
499 break;
|
|
500 }
|
|
501 }
|
|
502
|
|
503 size += malloced_storage_size (table,
|
|
504 256 * (level == 1 ? sizeof (short) :
|
|
505 sizeof (void *)),
|
|
506 stats);
|
|
507 return size;
|
|
508 }
|
|
509
|
|
510 static Bytecount
|
|
511 compute_to_unicode_table_size_1 (void *table, int level,
|
|
512 struct overhead_stats *stats)
|
|
513 {
|
|
514 Bytecount size = 0;
|
|
515
|
|
516 if (level == 2)
|
|
517 {
|
|
518 int i;
|
|
519 int **tab = (int **) table;
|
|
520
|
|
521 for (i = 0; i < 96; i++)
|
|
522 {
|
|
523 if (tab[i] != to_unicode_blank_1)
|
|
524 size += compute_to_unicode_table_size_1 (tab[i], 1, stats);
|
|
525 }
|
|
526 }
|
|
527
|
|
528 size += malloced_storage_size (table,
|
|
529 96 * (level == 1 ? sizeof (int) :
|
|
530 sizeof (void *)),
|
|
531 stats);
|
|
532 return size;
|
|
533 }
|
|
534
|
|
535 Bytecount
|
|
536 compute_from_unicode_table_size (Lisp_Object charset,
|
|
537 struct overhead_stats *stats)
|
|
538 {
|
|
539 return (compute_from_unicode_table_size_1
|
|
540 (XCHARSET_FROM_UNICODE_TABLE (charset),
|
|
541 XCHARSET_FROM_UNICODE_LEVELS (charset),
|
|
542 stats));
|
|
543 }
|
|
544
|
|
545 Bytecount
|
|
546 compute_to_unicode_table_size (Lisp_Object charset,
|
|
547 struct overhead_stats *stats)
|
|
548 {
|
|
549 return (compute_to_unicode_table_size_1
|
|
550 (XCHARSET_TO_UNICODE_TABLE (charset),
|
|
551 XCHARSET_DIMENSION (charset),
|
|
552 stats));
|
|
553 }
|
|
554
|
|
555 #endif
|
|
556
|
|
557 #ifdef SLEDGEHAMMER_CHECK_UNICODE
|
|
558
|
|
559 /* "Sledgehammer checks" are checks that verify the self-consistency
|
|
560 of an entire structure every time a change is about to be made or
|
|
561 has been made to the structure. Not fast but a pretty much
|
|
562 sure-fire way of flushing out any incorrectnesses in the algorithms
|
|
563 that create the structure.
|
|
564
|
|
565 Checking only after a change has been made will speed things up by
|
|
566 a factor of 2, but it doesn't absolutely prove that the code just
|
|
567 checked caused the problem; perhaps it happened elsewhere, either
|
|
568 in some code you forgot to sledgehammer check or as a result of
|
|
569 data corruption. */
|
|
570
|
|
571 static void
|
|
572 assert_not_any_blank_table (void *tab)
|
|
573 {
|
|
574 assert (tab != from_unicode_blank_1);
|
|
575 assert (tab != from_unicode_blank_2);
|
|
576 assert (tab != from_unicode_blank_3);
|
|
577 assert (tab != from_unicode_blank_4);
|
|
578 assert (tab != to_unicode_blank_1);
|
|
579 assert (tab != to_unicode_blank_2);
|
|
580 assert (tab);
|
|
581 }
|
|
582
|
|
583 static void
|
|
584 sledgehammer_check_from_table (Lisp_Object charset, void *table, int level,
|
|
585 int codetop)
|
|
586 {
|
|
587 int i;
|
|
588
|
|
589 switch (level)
|
|
590 {
|
|
591 case 1:
|
|
592 {
|
|
593 short *tab = (short *) table;
|
|
594 for (i = 0; i < 256; i++)
|
|
595 {
|
|
596 if (tab[i] != -1)
|
|
597 {
|
|
598 Lisp_Object char_charset;
|
|
599 int c1, c2;
|
|
600
|
867
|
601 assert (valid_ichar_p (tab[i]));
|
|
602 BREAKUP_ICHAR (tab[i], char_charset, c1, c2);
|
771
|
603 assert (EQ (charset, char_charset));
|
|
604 if (XCHARSET_DIMENSION (charset) == 1)
|
|
605 {
|
|
606 int *to_table =
|
|
607 (int *) XCHARSET_TO_UNICODE_TABLE (charset);
|
|
608 assert_not_any_blank_table (to_table);
|
|
609 assert (to_table[c1 - 32] == (codetop << 8) + i);
|
|
610 }
|
|
611 else
|
|
612 {
|
|
613 int **to_table =
|
|
614 (int **) XCHARSET_TO_UNICODE_TABLE (charset);
|
|
615 assert_not_any_blank_table (to_table);
|
|
616 assert_not_any_blank_table (to_table[c1 - 32]);
|
|
617 assert (to_table[c1 - 32][c2 - 32] == (codetop << 8) + i);
|
|
618 }
|
|
619 }
|
|
620 }
|
|
621 break;
|
|
622 }
|
|
623 case 2:
|
|
624 {
|
|
625 short **tab = (short **) table;
|
|
626 for (i = 0; i < 256; i++)
|
|
627 {
|
|
628 if (tab[i] != from_unicode_blank_1)
|
|
629 sledgehammer_check_from_table (charset, tab[i], 1,
|
|
630 (codetop << 8) + i);
|
|
631 }
|
|
632 break;
|
|
633 }
|
|
634 case 3:
|
|
635 {
|
|
636 short ***tab = (short ***) table;
|
|
637 for (i = 0; i < 256; i++)
|
|
638 {
|
|
639 if (tab[i] != from_unicode_blank_2)
|
|
640 sledgehammer_check_from_table (charset, tab[i], 2,
|
|
641 (codetop << 8) + i);
|
|
642 }
|
|
643 break;
|
|
644 }
|
|
645 case 4:
|
|
646 {
|
|
647 short ****tab = (short ****) table;
|
|
648 for (i = 0; i < 256; i++)
|
|
649 {
|
|
650 if (tab[i] != from_unicode_blank_3)
|
|
651 sledgehammer_check_from_table (charset, tab[i], 3,
|
|
652 (codetop << 8) + i);
|
|
653 }
|
|
654 break;
|
|
655 }
|
|
656 default:
|
|
657 abort ();
|
|
658 }
|
|
659 }
|
|
660
|
|
661 static void
|
|
662 sledgehammer_check_to_table (Lisp_Object charset, void *table, int level,
|
|
663 int codetop)
|
|
664 {
|
|
665 int i;
|
|
666
|
|
667 switch (level)
|
|
668 {
|
|
669 case 1:
|
|
670 {
|
|
671 int *tab = (int *) table;
|
|
672
|
|
673 if (XCHARSET_CHARS (charset) == 94)
|
|
674 {
|
|
675 assert (tab[0] == -1);
|
|
676 assert (tab[95] == -1);
|
|
677 }
|
|
678
|
|
679 for (i = 0; i < 96; i++)
|
|
680 {
|
|
681 if (tab[i] != -1)
|
|
682 {
|
|
683 int u4, u3, u2, u1, levels;
|
867
|
684 Ichar ch;
|
|
685 Ichar this_ch;
|
771
|
686 short val;
|
|
687 void *frtab = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
688
|
|
689 if (XCHARSET_DIMENSION (charset) == 1)
|
867
|
690 this_ch = make_ichar (charset, i + 32, 0);
|
771
|
691 else
|
867
|
692 this_ch = make_ichar (charset, codetop + 32, i + 32);
|
771
|
693
|
|
694 assert (tab[i] >= 0);
|
|
695 BREAKUP_UNICODE_CODE (tab[i], u4, u3, u2, u1, levels);
|
|
696 assert (levels <= XCHARSET_FROM_UNICODE_LEVELS (charset));
|
|
697
|
|
698 switch (XCHARSET_FROM_UNICODE_LEVELS (charset))
|
|
699 {
|
|
700 case 1: val = ((short *) frtab)[u1]; break;
|
|
701 case 2: val = ((short **) frtab)[u2][u1]; break;
|
|
702 case 3: val = ((short ***) frtab)[u3][u2][u1]; break;
|
|
703 case 4: val = ((short ****) frtab)[u4][u3][u2][u1]; break;
|
|
704 default: abort ();
|
|
705 }
|
|
706
|
867
|
707 ch = make_ichar (charset, val >> 8, val & 0xFF);
|
771
|
708 assert (ch == this_ch);
|
|
709
|
|
710 switch (XCHARSET_FROM_UNICODE_LEVELS (charset))
|
|
711 {
|
|
712 case 4:
|
|
713 assert_not_any_blank_table (frtab);
|
|
714 frtab = ((short ****) frtab)[u4];
|
|
715 /* fall through */
|
|
716 case 3:
|
|
717 assert_not_any_blank_table (frtab);
|
|
718 frtab = ((short ***) frtab)[u3];
|
|
719 /* fall through */
|
|
720 case 2:
|
|
721 assert_not_any_blank_table (frtab);
|
|
722 frtab = ((short **) frtab)[u2];
|
|
723 /* fall through */
|
|
724 case 1:
|
|
725 assert_not_any_blank_table (frtab);
|
|
726 break;
|
|
727 default: abort ();
|
|
728 }
|
|
729 }
|
|
730 }
|
|
731 break;
|
|
732 }
|
|
733 case 2:
|
|
734 {
|
|
735 int **tab = (int **) table;
|
|
736
|
|
737 if (XCHARSET_CHARS (charset) == 94)
|
|
738 {
|
|
739 assert (tab[0] == to_unicode_blank_1);
|
|
740 assert (tab[95] == to_unicode_blank_1);
|
|
741 }
|
|
742
|
|
743 for (i = 0; i < 96; i++)
|
|
744 {
|
|
745 if (tab[i] != to_unicode_blank_1)
|
|
746 sledgehammer_check_to_table (charset, tab[i], 1, i);
|
|
747 }
|
|
748 break;
|
|
749 }
|
|
750 default:
|
|
751 abort ();
|
|
752 }
|
|
753 }
|
|
754
|
|
755 static void
|
|
756 sledgehammer_check_unicode_tables (Lisp_Object charset)
|
|
757 {
|
|
758 /* verify that the blank tables have not been modified */
|
|
759 int i;
|
|
760 int from_level = XCHARSET_FROM_UNICODE_LEVELS (charset);
|
|
761 int to_level = XCHARSET_FROM_UNICODE_LEVELS (charset);
|
|
762
|
|
763 for (i = 0; i < 256; i++)
|
|
764 {
|
|
765 assert (from_unicode_blank_1[i] == (short) -1);
|
|
766 assert (from_unicode_blank_2[i] == from_unicode_blank_1);
|
|
767 assert (from_unicode_blank_3[i] == from_unicode_blank_2);
|
|
768 assert (from_unicode_blank_4[i] == from_unicode_blank_3);
|
|
769 }
|
|
770
|
|
771 for (i = 0; i < 96; i++)
|
|
772 {
|
|
773 assert (to_unicode_blank_1[i] == -1);
|
|
774 assert (to_unicode_blank_2[i] == to_unicode_blank_1);
|
|
775 }
|
|
776
|
|
777 assert (from_level >= 1 && from_level <= 4);
|
|
778
|
|
779 sledgehammer_check_from_table (charset,
|
|
780 XCHARSET_FROM_UNICODE_TABLE (charset),
|
|
781 from_level, 0);
|
|
782
|
|
783 sledgehammer_check_to_table (charset,
|
|
784 XCHARSET_TO_UNICODE_TABLE (charset),
|
|
785 XCHARSET_DIMENSION (charset), 0);
|
|
786 }
|
|
787
|
|
788 #endif /* SLEDGEHAMMER_CHECK_UNICODE */
|
|
789
|
|
790 static void
|
867
|
791 set_unicode_conversion (Ichar chr, int code)
|
771
|
792 {
|
|
793 Lisp_Object charset;
|
|
794 int c1, c2;
|
|
795
|
867
|
796 BREAKUP_ICHAR (chr, charset, c1, c2);
|
771
|
797
|
877
|
798 /* I tried an assert on code > 255 || chr == code, but that fails because
|
|
799 Mule gives many Latin characters separate code points for different
|
|
800 ISO 8859 coded character sets. Obvious in hindsight.... */
|
|
801 assert (!EQ (charset, Vcharset_ascii) || chr == code);
|
|
802 assert (!EQ (charset, Vcharset_latin_iso8859_1) || chr == code);
|
|
803 assert (!EQ (charset, Vcharset_control_1) || chr == code);
|
|
804
|
|
805 /* This assert is needed because it is simply unimplemented. */
|
771
|
806 assert (!EQ (charset, Vcharset_composite));
|
|
807
|
|
808 #ifdef SLEDGEHAMMER_CHECK_UNICODE
|
|
809 sledgehammer_check_unicode_tables (charset);
|
|
810 #endif
|
|
811
|
|
812 /* First, the char -> unicode translation */
|
|
813
|
|
814 if (XCHARSET_DIMENSION (charset) == 1)
|
|
815 {
|
|
816 int *to_table = (int *) XCHARSET_TO_UNICODE_TABLE (charset);
|
|
817 to_table[c1 - 32] = code;
|
|
818 }
|
|
819 else
|
|
820 {
|
|
821 int **to_table_2 = (int **) XCHARSET_TO_UNICODE_TABLE (charset);
|
|
822 int *to_table_1;
|
|
823
|
|
824 assert (XCHARSET_DIMENSION (charset) == 2);
|
|
825 to_table_1 = to_table_2[c1 - 32];
|
|
826 if (to_table_1 == to_unicode_blank_1)
|
|
827 {
|
|
828 to_table_1 = xnew_array (int, 96);
|
|
829 memcpy (to_table_1, to_unicode_blank_1, 96 * sizeof (int));
|
|
830 to_table_2[c1 - 32] = to_table_1;
|
|
831 }
|
|
832 to_table_1[c2 - 32] = code;
|
|
833 }
|
|
834
|
|
835 /* Then, unicode -> char: much harder */
|
|
836
|
|
837 {
|
|
838 int charset_levels;
|
|
839 int u4, u3, u2, u1;
|
|
840 int code_levels;
|
|
841 BREAKUP_UNICODE_CODE (code, u4, u3, u2, u1, code_levels);
|
|
842
|
|
843 charset_levels = XCHARSET_FROM_UNICODE_LEVELS (charset);
|
|
844
|
|
845 /* Make sure the charset's tables have at least as many levels as
|
|
846 the code point has: Note that the charset is guaranteed to have
|
|
847 at least one level, because it was created that way */
|
|
848 if (charset_levels < code_levels)
|
|
849 {
|
|
850 int i;
|
|
851
|
|
852 assert (charset_levels > 0);
|
|
853 for (i = 2; i <= code_levels; i++)
|
|
854 {
|
|
855 if (charset_levels < i)
|
|
856 {
|
|
857 void *old_table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
858 void *table = create_new_from_unicode_table (i);
|
|
859 XCHARSET_FROM_UNICODE_TABLE (charset) = table;
|
|
860
|
|
861 switch (i)
|
|
862 {
|
|
863 case 2:
|
|
864 ((short **) table)[0] = (short *) old_table;
|
|
865 break;
|
|
866 case 3:
|
|
867 ((short ***) table)[0] = (short **) old_table;
|
|
868 break;
|
|
869 case 4:
|
|
870 ((short ****) table)[0] = (short ***) old_table;
|
|
871 break;
|
|
872 default: abort ();
|
|
873 }
|
|
874 }
|
|
875 }
|
|
876
|
|
877 charset_levels = code_levels;
|
|
878 XCHARSET_FROM_UNICODE_LEVELS (charset) = code_levels;
|
|
879 }
|
|
880
|
|
881 /* Now, make sure there is a non-default table at each level */
|
|
882 {
|
|
883 int i;
|
|
884 void *table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
885
|
|
886 for (i = charset_levels; i >= 2; i--)
|
|
887 {
|
|
888 switch (i)
|
|
889 {
|
|
890 case 4:
|
|
891 if (((short ****) table)[u4] == from_unicode_blank_3)
|
|
892 ((short ****) table)[u4] =
|
|
893 ((short ***) create_new_from_unicode_table (3));
|
|
894 table = ((short ****) table)[u4];
|
|
895 break;
|
|
896 case 3:
|
|
897 if (((short ***) table)[u3] == from_unicode_blank_2)
|
|
898 ((short ***) table)[u3] =
|
|
899 ((short **) create_new_from_unicode_table (2));
|
|
900 table = ((short ***) table)[u3];
|
|
901 break;
|
|
902 case 2:
|
|
903 if (((short **) table)[u2] == from_unicode_blank_1)
|
|
904 ((short **) table)[u2] =
|
|
905 ((short *) create_new_from_unicode_table (1));
|
|
906 table = ((short **) table)[u2];
|
|
907 break;
|
|
908 default: abort ();
|
|
909 }
|
|
910 }
|
|
911 }
|
|
912
|
|
913 /* Finally, set the character */
|
|
914
|
|
915 {
|
|
916 void *table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
917 switch (charset_levels)
|
|
918 {
|
|
919 case 1: ((short *) table)[u1] = (c1 << 8) + c2; break;
|
|
920 case 2: ((short **) table)[u2][u1] = (c1 << 8) + c2; break;
|
|
921 case 3: ((short ***) table)[u3][u2][u1] = (c1 << 8) + c2; break;
|
|
922 case 4: ((short ****) table)[u4][u3][u2][u1] = (c1 << 8) + c2; break;
|
|
923 default: abort ();
|
|
924 }
|
|
925 }
|
|
926 }
|
|
927
|
|
928 #ifdef SLEDGEHAMMER_CHECK_UNICODE
|
|
929 sledgehammer_check_unicode_tables (charset);
|
|
930 #endif
|
|
931 }
|
|
932
|
788
|
933 int
|
867
|
934 ichar_to_unicode (Ichar chr)
|
771
|
935 {
|
|
936 Lisp_Object charset;
|
|
937 int c1, c2;
|
|
938
|
867
|
939 type_checking_assert (valid_ichar_p (chr));
|
877
|
940 /* This shortcut depends on the representation of an Ichar, see text.c. */
|
771
|
941 if (chr < 256)
|
|
942 return (int) chr;
|
|
943
|
867
|
944 BREAKUP_ICHAR (chr, charset, c1, c2);
|
771
|
945 if (EQ (charset, Vcharset_composite))
|
|
946 return -1; /* #### don't know how to handle */
|
|
947 else if (XCHARSET_DIMENSION (charset) == 1)
|
|
948 return ((int *) XCHARSET_TO_UNICODE_TABLE (charset))[c1 - 32];
|
|
949 else
|
|
950 return ((int **) XCHARSET_TO_UNICODE_TABLE (charset))[c1 - 32][c2 - 32];
|
|
951 }
|
|
952
|
867
|
953 static Ichar
|
877
|
954 unicode_to_ichar (int code, Lisp_Object_dynarr *charsets)
|
771
|
955 {
|
|
956 int u1, u2, u3, u4;
|
|
957 int code_levels;
|
|
958 int i;
|
|
959 int n = Dynarr_length (charsets);
|
|
960
|
|
961 type_checking_assert (code >= 0);
|
877
|
962 /* This shortcut depends on the representation of an Ichar, see text.c.
|
|
963 Note that it may _not_ be extended to U+00A0 to U+00FF (many ISO 8859
|
893
|
964 coded character sets have points that map into that region, so this
|
|
965 function is many-valued). */
|
877
|
966 if (code < 0xA0)
|
867
|
967 return (Ichar) code;
|
771
|
968
|
|
969 BREAKUP_UNICODE_CODE (code, u4, u3, u2, u1, code_levels);
|
|
970
|
|
971 for (i = 0; i < n; i++)
|
|
972 {
|
|
973 Lisp_Object charset = Dynarr_at (charsets, i);
|
|
974 int charset_levels = XCHARSET_FROM_UNICODE_LEVELS (charset);
|
|
975 if (charset_levels >= code_levels)
|
|
976 {
|
|
977 void *table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
978 short retval;
|
|
979
|
|
980 switch (charset_levels)
|
|
981 {
|
|
982 case 1: retval = ((short *) table)[u1]; break;
|
|
983 case 2: retval = ((short **) table)[u2][u1]; break;
|
|
984 case 3: retval = ((short ***) table)[u3][u2][u1]; break;
|
|
985 case 4: retval = ((short ****) table)[u4][u3][u2][u1]; break;
|
|
986 default: abort (); retval = 0;
|
|
987 }
|
|
988
|
|
989 if (retval != -1)
|
867
|
990 return make_ichar (charset, retval >> 8, retval & 0xFF);
|
771
|
991 }
|
|
992 }
|
|
993
|
867
|
994 return (Ichar) -1;
|
771
|
995 }
|
|
996
|
877
|
997 /* Add charsets to precedence list.
|
|
998 LIST must be a list of charsets. Charsets which are in the list more
|
|
999 than once are given the precedence implied by their earliest appearance.
|
|
1000 Later appearances are ignored. */
|
771
|
1001 static void
|
|
1002 add_charsets_to_precedence_list (Lisp_Object list, int *lbs,
|
|
1003 Lisp_Object_dynarr *dynarr)
|
|
1004 {
|
|
1005 {
|
|
1006 EXTERNAL_LIST_LOOP_2 (elt, list)
|
|
1007 {
|
|
1008 Lisp_Object charset = Fget_charset (elt);
|
778
|
1009 int lb = XCHARSET_LEADING_BYTE (charset);
|
771
|
1010 if (lbs[lb - MIN_LEADING_BYTE] == 0)
|
|
1011 {
|
877
|
1012 Dynarr_add (dynarr, charset);
|
771
|
1013 lbs[lb - MIN_LEADING_BYTE] = 1;
|
|
1014 }
|
|
1015 }
|
|
1016 }
|
|
1017 }
|
|
1018
|
877
|
1019 /* Rebuild the charset precedence array.
|
|
1020 The "charsets preferred for the current language" get highest precedence,
|
|
1021 followed by the "charsets preferred by default", ordered as in
|
|
1022 Vlanguage_unicode_precedence_list and Vdefault_unicode_precedence_list,
|
|
1023 respectively. All remaining charsets follow in an arbitrary order. */
|
771
|
1024 void
|
|
1025 recalculate_unicode_precedence (void)
|
|
1026 {
|
|
1027 int lbs[NUM_LEADING_BYTES];
|
|
1028 int i;
|
|
1029
|
|
1030 for (i = 0; i < NUM_LEADING_BYTES; i++)
|
|
1031 lbs[i] = 0;
|
|
1032
|
|
1033 Dynarr_reset (unicode_precedence_dynarr);
|
|
1034
|
|
1035 add_charsets_to_precedence_list (Vlanguage_unicode_precedence_list,
|
|
1036 lbs, unicode_precedence_dynarr);
|
|
1037 add_charsets_to_precedence_list (Vdefault_unicode_precedence_list,
|
|
1038 lbs, unicode_precedence_dynarr);
|
|
1039
|
|
1040 for (i = 0; i < NUM_LEADING_BYTES; i++)
|
|
1041 {
|
|
1042 if (lbs[i] == 0)
|
|
1043 {
|
826
|
1044 Lisp_Object charset = charset_by_leading_byte (i + MIN_LEADING_BYTE);
|
771
|
1045 if (!NILP (charset))
|
|
1046 Dynarr_add (unicode_precedence_dynarr, charset);
|
|
1047 }
|
|
1048 }
|
|
1049 }
|
|
1050
|
877
|
1051 DEFUN ("unicode-precedence-list",
|
|
1052 Funicode_precedence_list,
|
|
1053 0, 0, 0, /*
|
|
1054 Return the precedence order among charsets used for Unicode decoding.
|
|
1055
|
|
1056 Value is a list of charsets, which are searched in order for a translation
|
|
1057 matching a given Unicode character.
|
|
1058
|
|
1059 The highest precedence is given to the language-specific precedence list of
|
|
1060 charsets, defined by `set-language-unicode-precedence-list'. These are
|
|
1061 followed by charsets in the default precedence list, defined by
|
|
1062 `set-default-unicode-precedence-list'. Charsets occurring multiple times are
|
|
1063 given precedence according to their first occurrance in either list. These
|
|
1064 are followed by the remaining charsets, in some arbitrary order.
|
771
|
1065
|
|
1066 The language-specific precedence list is meant to be set as part of the
|
|
1067 language environment initialization; the default precedence list is meant
|
|
1068 to be set by the user.
|
1318
|
1069
|
|
1070 #### NOTE: This interface may be changed.
|
771
|
1071 */
|
877
|
1072 ())
|
|
1073 {
|
|
1074 int i;
|
|
1075 Lisp_Object list = Qnil;
|
|
1076
|
|
1077 for (i = Dynarr_length (unicode_precedence_dynarr) - 1; i >= 0; i--)
|
|
1078 list = Fcons (Dynarr_at (unicode_precedence_dynarr, i), list);
|
|
1079 return list;
|
|
1080 }
|
|
1081
|
|
1082
|
|
1083 /* #### This interface is wrong. Cyrillic users and Chinese users are going
|
|
1084 to have varying opinions about whether ISO Cyrillic, KOI8-R, or Windows
|
|
1085 1251 should take precedence, and whether Big Five or CNS should take
|
|
1086 precedence, respectively. This means that users are sometimes going to
|
|
1087 want to set Vlanguage_unicode_precedence_list.
|
|
1088 Furthermore, this should be language-local (buffer-local would be a
|
1318
|
1089 reasonable approximation).
|
|
1090
|
|
1091 Answer: You are right, this needs rethinking. */
|
877
|
1092 DEFUN ("set-language-unicode-precedence-list",
|
|
1093 Fset_language_unicode_precedence_list,
|
|
1094 1, 1, 0, /*
|
|
1095 Set the language-specific precedence of charsets in Unicode decoding.
|
|
1096 LIST is a list of charsets.
|
|
1097 See `unicode-precedence-list' for more information.
|
1318
|
1098
|
|
1099 #### NOTE: This interface may be changed.
|
877
|
1100 */
|
771
|
1101 (list))
|
|
1102 {
|
|
1103 {
|
|
1104 EXTERNAL_LIST_LOOP_2 (elt, list)
|
|
1105 Fget_charset (elt);
|
|
1106 }
|
|
1107
|
|
1108 Vlanguage_unicode_precedence_list = list;
|
|
1109 recalculate_unicode_precedence ();
|
|
1110 return Qnil;
|
|
1111 }
|
|
1112
|
|
1113 DEFUN ("language-unicode-precedence-list",
|
|
1114 Flanguage_unicode_precedence_list,
|
|
1115 0, 0, 0, /*
|
|
1116 Return the language-specific precedence list used for Unicode decoding.
|
877
|
1117 See `unicode-precedence-list' for more information.
|
1318
|
1118
|
|
1119 #### NOTE: This interface may be changed.
|
771
|
1120 */
|
|
1121 ())
|
|
1122 {
|
|
1123 return Vlanguage_unicode_precedence_list;
|
|
1124 }
|
|
1125
|
|
1126 DEFUN ("set-default-unicode-precedence-list",
|
|
1127 Fset_default_unicode_precedence_list,
|
|
1128 1, 1, 0, /*
|
|
1129 Set the default precedence list used for Unicode decoding.
|
877
|
1130 This is intended to be set by the user. See
|
|
1131 `unicode-precedence-list' for more information.
|
1318
|
1132
|
|
1133 #### NOTE: This interface may be changed.
|
771
|
1134 */
|
|
1135 (list))
|
|
1136 {
|
|
1137 {
|
|
1138 EXTERNAL_LIST_LOOP_2 (elt, list)
|
|
1139 Fget_charset (elt);
|
|
1140 }
|
|
1141
|
|
1142 Vdefault_unicode_precedence_list = list;
|
|
1143 recalculate_unicode_precedence ();
|
|
1144 return Qnil;
|
|
1145 }
|
|
1146
|
|
1147 DEFUN ("default-unicode-precedence-list",
|
|
1148 Fdefault_unicode_precedence_list,
|
|
1149 0, 0, 0, /*
|
|
1150 Return the default precedence list used for Unicode decoding.
|
877
|
1151 See `unicode-precedence-list' for more information.
|
1318
|
1152
|
|
1153 #### NOTE: This interface may be changed.
|
771
|
1154 */
|
|
1155 ())
|
|
1156 {
|
|
1157 return Vdefault_unicode_precedence_list;
|
|
1158 }
|
|
1159
|
|
1160 DEFUN ("set-unicode-conversion", Fset_unicode_conversion,
|
|
1161 2, 2, 0, /*
|
|
1162 Add conversion information between Unicode codepoints and characters.
|
877
|
1163 Conversions for U+0000 to U+00FF are hardwired to ASCII, Control-1, and
|
|
1164 Latin-1. Attempts to set these values will raise an error.
|
|
1165
|
771
|
1166 CHARACTER is one of the following:
|
|
1167
|
|
1168 -- A character (in which case CODE must be a non-negative integer; values
|
|
1169 above 2^20 - 1 are allowed for the purpose of specifying private
|
877
|
1170 characters, but are illegal in standard Unicode---they will cause errors
|
|
1171 when converted to utf-16)
|
771
|
1172 -- A vector of characters (in which case CODE must be a vector of integers
|
|
1173 of the same length)
|
|
1174 */
|
|
1175 (character, code))
|
|
1176 {
|
|
1177 Lisp_Object charset;
|
877
|
1178 int ichar, unicode;
|
771
|
1179
|
|
1180 CHECK_CHAR (character);
|
|
1181 CHECK_NATNUM (code);
|
|
1182
|
877
|
1183 unicode = XINT (code);
|
|
1184 ichar = XCHAR (character);
|
|
1185 charset = ichar_charset (ichar);
|
|
1186
|
|
1187 /* The translations of ASCII, Control-1, and Latin-1 code points are
|
|
1188 hard-coded in ichar_to_unicode and unicode_to_ichar.
|
|
1189
|
|
1190 Checking unicode < 256 && ichar != unicode is wrong because Mule gives
|
|
1191 many Latin characters code points in a few different character sets. */
|
|
1192 if ((EQ (charset, Vcharset_ascii) ||
|
|
1193 EQ (charset, Vcharset_control_1) ||
|
|
1194 EQ (charset, Vcharset_latin_iso8859_1))
|
|
1195 && unicode != ichar)
|
893
|
1196 signal_error (Qinvalid_argument, "Can't change Unicode translation for ASCII, Control-1 or Latin-1 character",
|
771
|
1197 character);
|
|
1198
|
877
|
1199 /* #### Composite characters are not properly implemented yet. */
|
|
1200 if (EQ (charset, Vcharset_composite))
|
|
1201 signal_error (Qinvalid_argument, "Can't set Unicode translation for Composite char",
|
|
1202 character);
|
|
1203
|
|
1204 set_unicode_conversion (ichar, unicode);
|
771
|
1205 return Qnil;
|
|
1206 }
|
|
1207
|
|
1208 #endif /* MULE */
|
|
1209
|
800
|
1210 DEFUN ("char-to-unicode", Fchar_to_unicode, 1, 1, 0, /*
|
771
|
1211 Convert character to Unicode codepoint.
|
877
|
1212 When there is no international support (i.e. the 'mule feature is not
|
|
1213 present), this function simply does `char-to-int'.
|
771
|
1214 */
|
|
1215 (character))
|
|
1216 {
|
|
1217 CHECK_CHAR (character);
|
|
1218 #ifdef MULE
|
867
|
1219 return make_int (ichar_to_unicode (XCHAR (character)));
|
771
|
1220 #else
|
|
1221 return Fchar_to_int (character);
|
|
1222 #endif /* MULE */
|
|
1223 }
|
|
1224
|
800
|
1225 DEFUN ("unicode-to-char", Funicode_to_char, 1, 2, 0, /*
|
771
|
1226 Convert Unicode codepoint to character.
|
|
1227 CODE should be a non-negative integer.
|
|
1228 If CHARSETS is given, it should be a list of charsets, and only those
|
|
1229 charsets will be consulted, in the given order, for a translation.
|
|
1230 Otherwise, the default ordering of all charsets will be given (see
|
|
1231 `set-unicode-charset-precedence').
|
|
1232
|
877
|
1233 When there is no international support (i.e. the 'mule feature is not
|
|
1234 present), this function simply does `int-to-char' and ignores the CHARSETS
|
|
1235 argument.
|
771
|
1236 */
|
|
1237 (code, charsets))
|
|
1238 {
|
|
1239 #ifdef MULE
|
|
1240 Lisp_Object_dynarr *dyn;
|
|
1241 int lbs[NUM_LEADING_BYTES];
|
|
1242 int c;
|
|
1243
|
|
1244 CHECK_NATNUM (code);
|
|
1245 c = XINT (code);
|
|
1246 {
|
|
1247 EXTERNAL_LIST_LOOP_2 (elt, charsets)
|
|
1248 Fget_charset (elt);
|
|
1249 }
|
|
1250
|
|
1251 if (NILP (charsets))
|
|
1252 {
|
877
|
1253 Ichar ret = unicode_to_ichar (c, unicode_precedence_dynarr);
|
771
|
1254 if (ret == -1)
|
|
1255 return Qnil;
|
|
1256 return make_char (ret);
|
|
1257 }
|
|
1258
|
|
1259 dyn = Dynarr_new (Lisp_Object);
|
|
1260 memset (lbs, 0, NUM_LEADING_BYTES * sizeof (int));
|
|
1261 add_charsets_to_precedence_list (charsets, lbs, dyn);
|
|
1262 {
|
877
|
1263 Ichar ret = unicode_to_ichar (c, dyn);
|
771
|
1264 Dynarr_free (dyn);
|
|
1265 if (ret == -1)
|
|
1266 return Qnil;
|
|
1267 return make_char (ret);
|
|
1268 }
|
|
1269 #else
|
|
1270 CHECK_NATNUM (code);
|
|
1271 return Fint_to_char (code);
|
|
1272 #endif /* MULE */
|
|
1273 }
|
|
1274
|
872
|
1275 #ifdef MULE
|
|
1276
|
771
|
1277 static Lisp_Object
|
|
1278 cerrar_el_fulano (Lisp_Object fulano)
|
|
1279 {
|
|
1280 FILE *file = (FILE *) get_opaque_ptr (fulano);
|
|
1281 retry_fclose (file);
|
|
1282 return Qnil;
|
|
1283 }
|
|
1284
|
1318
|
1285 DEFUN ("load-unicode-mapping-table", Fload_unicode_mapping_table,
|
771
|
1286 2, 6, 0, /*
|
877
|
1287 Load Unicode tables with the Unicode mapping data in FILENAME for CHARSET.
|
771
|
1288 Data is text, in the form of one translation per line -- charset
|
|
1289 codepoint followed by Unicode codepoint. Numbers are decimal or hex
|
|
1290 \(preceded by 0x). Comments are marked with a #. Charset codepoints
|
877
|
1291 for two-dimensional charsets have the first octet stored in the
|
771
|
1292 high 8 bits of the hex number and the second in the low 8 bits.
|
|
1293
|
|
1294 If START and END are given, only charset codepoints within the given
|
877
|
1295 range will be processed. (START and END apply to the codepoints in the
|
|
1296 file, before OFFSET is applied.)
|
771
|
1297
|
877
|
1298 If OFFSET is given, that value will be added to all charset codepoints
|
|
1299 in the file to obtain the internal charset codepoint. \(We assume
|
|
1300 that octets in the table are in the range 33 to 126 or 32 to 127. If
|
|
1301 you have a table in ku-ten form, with octets in the range 1 to 94, you
|
|
1302 will have to use an offset of 5140, i.e. 0x2020.)
|
771
|
1303
|
|
1304 FLAGS, if specified, control further how the tables are interpreted
|
877
|
1305 and are used to special-case certain known format deviations in the
|
|
1306 Unicode tables or in the charset:
|
771
|
1307
|
|
1308 `ignore-first-column'
|
877
|
1309 The JIS X 0208 tables have 3 columns of data instead of 2. The first
|
|
1310 column contains the Shift-JIS codepoint, which we ignore.
|
771
|
1311 `big5'
|
877
|
1312 The charset codepoints are Big Five codepoints; convert it to the
|
|
1313 hacked-up Mule codepoint in `chinese-big5-1' or `chinese-big5-2'.
|
771
|
1314 */
|
|
1315 (filename, charset, start, end, offset, flags))
|
|
1316 {
|
|
1317 int st = 0, en = INT_MAX, of = 0;
|
|
1318 FILE *file;
|
|
1319 struct gcpro gcpro1;
|
|
1320 char line[1025];
|
|
1321 int fondo = specpdl_depth ();
|
|
1322 int ignore_first_column = 0;
|
|
1323 int big5 = 0;
|
|
1324
|
|
1325 CHECK_STRING (filename);
|
|
1326 charset = Fget_charset (charset);
|
|
1327 if (!NILP (start))
|
|
1328 {
|
|
1329 CHECK_INT (start);
|
|
1330 st = XINT (start);
|
|
1331 }
|
|
1332 if (!NILP (end))
|
|
1333 {
|
|
1334 CHECK_INT (end);
|
|
1335 en = XINT (end);
|
|
1336 }
|
|
1337 if (!NILP (offset))
|
|
1338 {
|
|
1339 CHECK_INT (offset);
|
|
1340 of = XINT (offset);
|
|
1341 }
|
|
1342
|
|
1343 if (!LISTP (flags))
|
|
1344 flags = list1 (flags);
|
|
1345
|
|
1346 {
|
|
1347 EXTERNAL_LIST_LOOP_2 (elt, flags)
|
|
1348 {
|
|
1349 if (EQ (elt, Qignore_first_column))
|
|
1350 ignore_first_column = 1;
|
|
1351 else if (EQ (elt, Qbig5))
|
|
1352 big5 = 1;
|
|
1353 else
|
|
1354 invalid_constant
|
1318
|
1355 ("Unrecognized `load-unicode-mapping-table' flag", elt);
|
771
|
1356 }
|
|
1357 }
|
|
1358
|
|
1359 GCPRO1 (filename);
|
|
1360 filename = Fexpand_file_name (filename, Qnil);
|
|
1361 file = qxe_fopen (XSTRING_DATA (filename), READ_TEXT);
|
|
1362 if (!file)
|
|
1363 report_file_error ("Cannot open", filename);
|
|
1364 record_unwind_protect (cerrar_el_fulano, make_opaque_ptr (file));
|
|
1365 while (fgets (line, sizeof (line), file))
|
|
1366 {
|
|
1367 char *p = line;
|
|
1368 int cp1, cp2, endcount;
|
|
1369 int cp1high, cp1low;
|
|
1370 int dummy;
|
|
1371
|
|
1372 while (*p) /* erase all comments out of the line */
|
|
1373 {
|
|
1374 if (*p == '#')
|
|
1375 *p = '\0';
|
|
1376 else
|
|
1377 p++;
|
|
1378 }
|
|
1379 /* see if line is nothing but whitespace and skip if so */
|
|
1380 p = line + strspn (line, " \t\n\r\f");
|
|
1381 if (!*p)
|
|
1382 continue;
|
|
1383 /* NOTE: It appears that MS Windows and Newlib sscanf() have
|
|
1384 different interpretations for whitespace (== "skip all whitespace
|
|
1385 at processing point"): Newlib requires at least one corresponding
|
|
1386 whitespace character in the input, but MS allows none. The
|
|
1387 following would be easier to write if we could count on the MS
|
|
1388 interpretation.
|
|
1389
|
|
1390 Also, the return value does NOT include %n storage. */
|
|
1391 if ((!ignore_first_column ?
|
|
1392 sscanf (p, "%i %i%n", &cp1, &cp2, &endcount) < 2 :
|
|
1393 sscanf (p, "%i %i %i%n", &dummy, &cp1, &cp2, &endcount) < 3)
|
|
1394 || *(p + endcount + strspn (p + endcount, " \t\n\r\f")))
|
|
1395 {
|
793
|
1396 warn_when_safe (Qunicode, Qwarning,
|
771
|
1397 "Unrecognized line in translation file %s:\n%s",
|
|
1398 XSTRING_DATA (filename), line);
|
|
1399 continue;
|
|
1400 }
|
|
1401 if (cp1 >= st && cp1 <= en)
|
|
1402 {
|
|
1403 cp1 += of;
|
|
1404 if (cp1 < 0 || cp1 >= 65536)
|
|
1405 {
|
|
1406 out_of_range:
|
793
|
1407 warn_when_safe (Qunicode, Qwarning,
|
|
1408 "Out of range first codepoint 0x%x in "
|
|
1409 "translation file %s:\n%s",
|
771
|
1410 cp1, XSTRING_DATA (filename), line);
|
|
1411 continue;
|
|
1412 }
|
|
1413
|
|
1414 cp1high = cp1 >> 8;
|
|
1415 cp1low = cp1 & 255;
|
|
1416
|
|
1417 if (big5)
|
|
1418 {
|
867
|
1419 Ichar ch = decode_big5_char (cp1high, cp1low);
|
771
|
1420 if (ch == -1)
|
793
|
1421
|
|
1422 warn_when_safe (Qunicode, Qwarning,
|
|
1423 "Out of range Big5 codepoint 0x%x in "
|
|
1424 "translation file %s:\n%s",
|
771
|
1425 cp1, XSTRING_DATA (filename), line);
|
|
1426 else
|
|
1427 set_unicode_conversion (ch, cp2);
|
|
1428 }
|
|
1429 else
|
|
1430 {
|
|
1431 int l1, h1, l2, h2;
|
867
|
1432 Ichar emch;
|
771
|
1433
|
|
1434 switch (XCHARSET_TYPE (charset))
|
|
1435 {
|
|
1436 case CHARSET_TYPE_94: l1 = 33; h1 = 126; l2 = 0; h2 = 0; break;
|
|
1437 case CHARSET_TYPE_96: l1 = 32; h1 = 127; l2 = 0; h2 = 0; break;
|
|
1438 case CHARSET_TYPE_94X94: l1 = 33; h1 = 126; l2 = 33; h2 = 126;
|
|
1439 break;
|
|
1440 case CHARSET_TYPE_96X96: l1 = 32; h1 = 127; l2 = 32; h2 = 127;
|
|
1441 break;
|
|
1442 default: abort (); l1 = 0; h1 = 0; l2 = 0; h2 = 0;
|
|
1443 }
|
|
1444
|
|
1445 if (cp1high < l2 || cp1high > h2 || cp1low < l1 || cp1low > h1)
|
|
1446 goto out_of_range;
|
|
1447
|
867
|
1448 emch = (cp1high == 0 ? make_ichar (charset, cp1low, 0) :
|
|
1449 make_ichar (charset, cp1high, cp1low));
|
771
|
1450 set_unicode_conversion (emch, cp2);
|
|
1451 }
|
|
1452 }
|
|
1453 }
|
|
1454
|
|
1455 if (ferror (file))
|
|
1456 report_file_error ("IO error when reading", filename);
|
|
1457
|
|
1458 unbind_to (fondo); /* close file */
|
|
1459 UNGCPRO;
|
|
1460 return Qnil;
|
|
1461 }
|
|
1462
|
|
1463 #endif /* MULE */
|
|
1464
|
|
1465
|
|
1466 /************************************************************************/
|
|
1467 /* Unicode coding system */
|
|
1468 /************************************************************************/
|
|
1469
|
|
1470 /* ISO 10646 UTF-16, UCS-4, UTF-8, UTF-7, etc. */
|
|
1471
|
|
1472 enum unicode_type
|
|
1473 {
|
|
1474 UNICODE_UTF_16,
|
|
1475 UNICODE_UTF_8,
|
|
1476 UNICODE_UTF_7,
|
1429
|
1477 UNICODE_UCS_4
|
771
|
1478 };
|
|
1479
|
|
1480 struct unicode_coding_system
|
|
1481 {
|
|
1482 enum unicode_type type;
|
1887
|
1483 unsigned int little_endian :1;
|
|
1484 unsigned int need_bom :1;
|
771
|
1485 };
|
|
1486
|
|
1487 #define CODING_SYSTEM_UNICODE_TYPE(codesys) \
|
|
1488 (CODING_SYSTEM_TYPE_DATA (codesys, unicode)->type)
|
|
1489 #define XCODING_SYSTEM_UNICODE_TYPE(codesys) \
|
|
1490 CODING_SYSTEM_UNICODE_TYPE (XCODING_SYSTEM (codesys))
|
|
1491 #define CODING_SYSTEM_UNICODE_LITTLE_ENDIAN(codesys) \
|
|
1492 (CODING_SYSTEM_TYPE_DATA (codesys, unicode)->little_endian)
|
|
1493 #define XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN(codesys) \
|
|
1494 CODING_SYSTEM_UNICODE_LITTLE_ENDIAN (XCODING_SYSTEM (codesys))
|
|
1495 #define CODING_SYSTEM_UNICODE_NEED_BOM(codesys) \
|
|
1496 (CODING_SYSTEM_TYPE_DATA (codesys, unicode)->need_bom)
|
|
1497 #define XCODING_SYSTEM_UNICODE_NEED_BOM(codesys) \
|
|
1498 CODING_SYSTEM_UNICODE_NEED_BOM (XCODING_SYSTEM (codesys))
|
|
1499
|
|
1500 struct unicode_coding_stream
|
|
1501 {
|
|
1502 /* decode */
|
|
1503 unsigned char counter;
|
|
1504 int seen_char;
|
|
1505 /* encode */
|
|
1506 Lisp_Object current_charset;
|
|
1507 int current_char_boundary;
|
|
1508 int wrote_bom;
|
|
1509 };
|
|
1510
|
1204
|
1511 static const struct memory_description unicode_coding_system_description[] = {
|
771
|
1512 { XD_END }
|
|
1513 };
|
|
1514
|
1204
|
1515 DEFINE_CODING_SYSTEM_TYPE_WITH_DATA (unicode);
|
|
1516
|
771
|
1517 /* Decode a UCS-2 or UCS-4 character into a buffer. If the lookup fails, use
|
|
1518 <GETA MARK> (U+3013) of JIS X 0208, which means correct character
|
|
1519 is not found, instead.
|
|
1520 #### do something more appropriate (use blob?)
|
|
1521 Danger, Will Robinson! Data loss. Should we signal user? */
|
|
1522 static void
|
|
1523 decode_unicode_char (int ch, unsigned_char_dynarr *dst,
|
1887
|
1524 struct unicode_coding_stream *data,
|
|
1525 unsigned int ignore_bom)
|
771
|
1526 {
|
|
1527 if (ch == 0xFEFF && !data->seen_char && ignore_bom)
|
|
1528 ;
|
|
1529 else
|
|
1530 {
|
|
1531 #ifdef MULE
|
877
|
1532 Ichar chr = unicode_to_ichar (ch, unicode_precedence_dynarr);
|
771
|
1533
|
|
1534 if (chr != -1)
|
|
1535 {
|
867
|
1536 Ibyte work[MAX_ICHAR_LEN];
|
771
|
1537 int len;
|
|
1538
|
867
|
1539 len = set_itext_ichar (work, chr);
|
771
|
1540 Dynarr_add_many (dst, work, len);
|
|
1541 }
|
|
1542 else
|
|
1543 {
|
|
1544 Dynarr_add (dst, LEADING_BYTE_JAPANESE_JISX0208);
|
|
1545 Dynarr_add (dst, 34 + 128);
|
|
1546 Dynarr_add (dst, 46 + 128);
|
|
1547 }
|
|
1548 #else
|
867
|
1549 Dynarr_add (dst, (Ibyte) ch);
|
771
|
1550 #endif /* MULE */
|
|
1551 }
|
|
1552
|
|
1553 data->seen_char = 1;
|
|
1554 }
|
|
1555
|
|
1556 static void
|
|
1557 encode_unicode_char_1 (int code, unsigned_char_dynarr *dst,
|
1887
|
1558 enum unicode_type type, unsigned int little_endian)
|
771
|
1559 {
|
|
1560 switch (type)
|
|
1561 {
|
|
1562 case UNICODE_UTF_16:
|
|
1563 if (little_endian)
|
|
1564 {
|
|
1565 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1566 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1567 }
|
|
1568 else
|
|
1569 {
|
|
1570 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1571 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1572 }
|
|
1573 break;
|
|
1574
|
|
1575 case UNICODE_UCS_4:
|
|
1576 if (little_endian)
|
|
1577 {
|
|
1578 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1579 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1580 Dynarr_add (dst, (unsigned char) ((code >> 16) & 255));
|
|
1581 Dynarr_add (dst, (unsigned char) (code >> 24));
|
|
1582 }
|
|
1583 else
|
|
1584 {
|
|
1585 Dynarr_add (dst, (unsigned char) (code >> 24));
|
|
1586 Dynarr_add (dst, (unsigned char) ((code >> 16) & 255));
|
|
1587 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1588 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1589 }
|
|
1590 break;
|
|
1591
|
|
1592 case UNICODE_UTF_8:
|
|
1593 if (code <= 0x7f)
|
|
1594 {
|
|
1595 Dynarr_add (dst, (unsigned char) code);
|
|
1596 }
|
|
1597 else if (code <= 0x7ff)
|
|
1598 {
|
|
1599 Dynarr_add (dst, (unsigned char) ((code >> 6) | 0xc0));
|
|
1600 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1601 }
|
|
1602 else if (code <= 0xffff)
|
|
1603 {
|
|
1604 Dynarr_add (dst, (unsigned char) ((code >> 12) | 0xe0));
|
|
1605 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1606 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1607 }
|
|
1608 else if (code <= 0x1fffff)
|
|
1609 {
|
|
1610 Dynarr_add (dst, (unsigned char) ((code >> 18) | 0xf0));
|
|
1611 Dynarr_add (dst, (unsigned char) (((code >> 12) & 0x3f) | 0x80));
|
|
1612 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1613 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1614 }
|
|
1615 else if (code <= 0x3ffffff)
|
|
1616 {
|
|
1617 Dynarr_add (dst, (unsigned char) ((code >> 24) | 0xf8));
|
|
1618 Dynarr_add (dst, (unsigned char) (((code >> 18) & 0x3f) | 0x80));
|
|
1619 Dynarr_add (dst, (unsigned char) (((code >> 12) & 0x3f) | 0x80));
|
|
1620 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1621 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1622 }
|
|
1623 else
|
|
1624 {
|
|
1625 Dynarr_add (dst, (unsigned char) ((code >> 30) | 0xfc));
|
|
1626 Dynarr_add (dst, (unsigned char) (((code >> 24) & 0x3f) | 0x80));
|
|
1627 Dynarr_add (dst, (unsigned char) (((code >> 18) & 0x3f) | 0x80));
|
|
1628 Dynarr_add (dst, (unsigned char) (((code >> 12) & 0x3f) | 0x80));
|
|
1629 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1630 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1631 }
|
|
1632 break;
|
|
1633
|
|
1634 case UNICODE_UTF_7: abort ();
|
|
1635
|
|
1636 default: abort ();
|
|
1637 }
|
|
1638 }
|
|
1639
|
|
1640 static void
|
|
1641 encode_unicode_char (Lisp_Object charset, int h, int l,
|
|
1642 unsigned_char_dynarr *dst, enum unicode_type type,
|
1887
|
1643 unsigned int little_endian)
|
771
|
1644 {
|
|
1645 #ifdef MULE
|
867
|
1646 int code = ichar_to_unicode (make_ichar (charset, h & 127, l & 127));
|
771
|
1647
|
|
1648 if (code == -1)
|
|
1649 {
|
|
1650 if (type != UNICODE_UTF_16 &&
|
|
1651 XCHARSET_DIMENSION (charset) == 2 &&
|
|
1652 XCHARSET_CHARS (charset) == 94)
|
|
1653 {
|
|
1654 unsigned char final = XCHARSET_FINAL (charset);
|
|
1655
|
|
1656 if (('@' <= final) && (final < 0x7f))
|
|
1657 code = (0xe00000 + (final - '@') * 94 * 94
|
|
1658 + ((h & 127) - 33) * 94 + (l & 127) - 33);
|
|
1659 else
|
|
1660 code = '?';
|
|
1661 }
|
|
1662 else
|
|
1663 code = '?';
|
|
1664 }
|
|
1665 #else
|
|
1666 int code = h;
|
|
1667 #endif /* MULE */
|
|
1668
|
|
1669 encode_unicode_char_1 (code, dst, type, little_endian);
|
|
1670 }
|
|
1671
|
|
1672 static Bytecount
|
|
1673 unicode_convert (struct coding_stream *str, const UExtbyte *src,
|
|
1674 unsigned_char_dynarr *dst, Bytecount n)
|
|
1675 {
|
|
1676 unsigned int ch = str->ch;
|
|
1677 struct unicode_coding_stream *data = CODING_STREAM_TYPE_DATA (str, unicode);
|
|
1678 enum unicode_type type =
|
|
1679 XCODING_SYSTEM_UNICODE_TYPE (str->codesys);
|
1887
|
1680 unsigned int little_endian =
|
|
1681 XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (str->codesys);
|
|
1682 unsigned int ignore_bom = XCODING_SYSTEM_UNICODE_NEED_BOM (str->codesys);
|
771
|
1683 Bytecount orign = n;
|
|
1684
|
|
1685 if (str->direction == CODING_DECODE)
|
|
1686 {
|
|
1687 unsigned char counter = data->counter;
|
|
1688
|
|
1689 while (n--)
|
|
1690 {
|
|
1691 UExtbyte c = *src++;
|
|
1692
|
|
1693 switch (type)
|
|
1694 {
|
|
1695 case UNICODE_UTF_8:
|
|
1696 switch (counter)
|
|
1697 {
|
|
1698 case 0:
|
|
1699 if (c >= 0xfc)
|
|
1700 {
|
|
1701 ch = c & 0x01;
|
|
1702 counter = 5;
|
|
1703 }
|
|
1704 else if (c >= 0xf8)
|
|
1705 {
|
|
1706 ch = c & 0x03;
|
|
1707 counter = 4;
|
|
1708 }
|
|
1709 else if (c >= 0xf0)
|
|
1710 {
|
|
1711 ch = c & 0x07;
|
|
1712 counter = 3;
|
|
1713 }
|
|
1714 else if (c >= 0xe0)
|
|
1715 {
|
|
1716 ch = c & 0x0f;
|
|
1717 counter = 2;
|
|
1718 }
|
|
1719 else if (c >= 0xc0)
|
|
1720 {
|
|
1721 ch = c & 0x1f;
|
|
1722 counter = 1;
|
|
1723 }
|
|
1724 else
|
|
1725 decode_unicode_char (c, dst, data, ignore_bom);
|
|
1726 break;
|
|
1727 case 1:
|
|
1728 ch = (ch << 6) | (c & 0x3f);
|
|
1729 decode_unicode_char (ch, dst, data, ignore_bom);
|
|
1730 ch = 0;
|
|
1731 counter = 0;
|
|
1732 break;
|
|
1733 default:
|
|
1734 ch = (ch << 6) | (c & 0x3f);
|
|
1735 counter--;
|
|
1736 }
|
|
1737 break;
|
|
1738
|
|
1739 case UNICODE_UTF_16:
|
|
1740 if (little_endian)
|
|
1741 ch = (c << counter) | ch;
|
|
1742 else
|
|
1743 ch = (ch << 8) | c;
|
|
1744 counter += 8;
|
|
1745 if (counter == 16)
|
|
1746 {
|
|
1747 int tempch = ch;
|
|
1748 ch = 0;
|
|
1749 counter = 0;
|
|
1750 decode_unicode_char (tempch, dst, data, ignore_bom);
|
|
1751 }
|
|
1752 break;
|
|
1753
|
|
1754 case UNICODE_UCS_4:
|
|
1755 if (little_endian)
|
|
1756 ch = (c << counter) | ch;
|
|
1757 else
|
|
1758 ch = (ch << 8) | c;
|
|
1759 counter += 8;
|
|
1760 if (counter == 32)
|
|
1761 {
|
|
1762 int tempch = ch;
|
|
1763 ch = 0;
|
|
1764 counter = 0;
|
|
1765 if (tempch < 0)
|
|
1766 {
|
|
1767 /* !!#### indicate an error */
|
|
1768 tempch = '~';
|
|
1769 }
|
|
1770 decode_unicode_char (tempch, dst, data, ignore_bom);
|
|
1771 }
|
|
1772 break;
|
|
1773
|
|
1774 case UNICODE_UTF_7:
|
|
1775 abort ();
|
|
1776 break;
|
|
1777
|
|
1778 default: abort ();
|
|
1779 }
|
|
1780
|
|
1781 }
|
|
1782 if (str->eof)
|
|
1783 DECODE_OUTPUT_PARTIAL_CHAR (ch, dst);
|
|
1784
|
|
1785 data->counter = counter;
|
|
1786 }
|
|
1787 else
|
|
1788 {
|
|
1789 unsigned char char_boundary = data->current_char_boundary;
|
|
1790 Lisp_Object charset = data->current_charset;
|
|
1791
|
|
1792 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1793 /* flags for handling composite chars. We do a little switcheroo
|
|
1794 on the source while we're outputting the composite char. */
|
|
1795 Bytecount saved_n = 0;
|
867
|
1796 const Ibyte *saved_src = NULL;
|
771
|
1797 int in_composite = 0;
|
|
1798
|
|
1799 back_to_square_n:
|
|
1800 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
1801
|
|
1802 if (XCODING_SYSTEM_UNICODE_NEED_BOM (str->codesys) && !data->wrote_bom)
|
|
1803 {
|
|
1804 encode_unicode_char_1 (0xFEFF, dst, type, little_endian);
|
|
1805 data->wrote_bom = 1;
|
|
1806 }
|
|
1807
|
|
1808 while (n--)
|
|
1809 {
|
867
|
1810 Ibyte c = *src++;
|
771
|
1811
|
|
1812 #ifdef MULE
|
826
|
1813 if (byte_ascii_p (c))
|
771
|
1814 #endif /* MULE */
|
|
1815 { /* Processing ASCII character */
|
|
1816 ch = 0;
|
|
1817 encode_unicode_char (Vcharset_ascii, c, 0, dst, type,
|
|
1818 little_endian);
|
|
1819
|
|
1820 char_boundary = 1;
|
|
1821 }
|
|
1822 #ifdef MULE
|
867
|
1823 else if (ibyte_leading_byte_p (c) || ibyte_leading_byte_p (ch))
|
771
|
1824 { /* Processing Leading Byte */
|
|
1825 ch = 0;
|
826
|
1826 charset = charset_by_leading_byte (c);
|
|
1827 if (leading_byte_prefix_p(c))
|
771
|
1828 ch = c;
|
|
1829 char_boundary = 0;
|
|
1830 }
|
|
1831 else
|
|
1832 { /* Processing Non-ASCII character */
|
|
1833 char_boundary = 1;
|
|
1834 if (EQ (charset, Vcharset_control_1))
|
|
1835 encode_unicode_char (Vcharset_control_1, c, 0, dst,
|
|
1836 type, little_endian);
|
|
1837 else
|
|
1838 {
|
|
1839 switch (XCHARSET_REP_BYTES (charset))
|
|
1840 {
|
|
1841 case 2:
|
|
1842 encode_unicode_char (charset, c, 0, dst, type,
|
|
1843 little_endian);
|
|
1844 break;
|
|
1845 case 3:
|
|
1846 if (XCHARSET_PRIVATE_P (charset))
|
|
1847 {
|
|
1848 encode_unicode_char (charset, c, 0, dst, type,
|
|
1849 little_endian);
|
|
1850 ch = 0;
|
|
1851 }
|
|
1852 else if (ch)
|
|
1853 {
|
|
1854 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1855 if (EQ (charset, Vcharset_composite))
|
|
1856 {
|
|
1857 if (in_composite)
|
|
1858 {
|
|
1859 /* #### Bother! We don't know how to
|
|
1860 handle this yet. */
|
|
1861 encode_unicode_char (Vcharset_ascii, '~', 0,
|
|
1862 dst, type,
|
|
1863 little_endian);
|
|
1864 }
|
|
1865 else
|
|
1866 {
|
867
|
1867 Ichar emch = make_ichar (Vcharset_composite,
|
771
|
1868 ch & 0x7F,
|
|
1869 c & 0x7F);
|
|
1870 Lisp_Object lstr =
|
|
1871 composite_char_string (emch);
|
|
1872 saved_n = n;
|
|
1873 saved_src = src;
|
|
1874 in_composite = 1;
|
|
1875 src = XSTRING_DATA (lstr);
|
|
1876 n = XSTRING_LENGTH (lstr);
|
|
1877 }
|
|
1878 }
|
|
1879 else
|
|
1880 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
1881 encode_unicode_char (charset, ch, c, dst, type,
|
|
1882 little_endian);
|
|
1883 ch = 0;
|
|
1884 }
|
|
1885 else
|
|
1886 {
|
|
1887 ch = c;
|
|
1888 char_boundary = 0;
|
|
1889 }
|
|
1890 break;
|
|
1891 case 4:
|
|
1892 if (ch)
|
|
1893 {
|
|
1894 encode_unicode_char (charset, ch, c, dst, type,
|
|
1895 little_endian);
|
|
1896 ch = 0;
|
|
1897 }
|
|
1898 else
|
|
1899 {
|
|
1900 ch = c;
|
|
1901 char_boundary = 0;
|
|
1902 }
|
|
1903 break;
|
|
1904 default:
|
|
1905 abort ();
|
|
1906 }
|
|
1907 }
|
|
1908 }
|
|
1909 #endif /* MULE */
|
|
1910 }
|
|
1911
|
|
1912 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1913 if (in_composite)
|
|
1914 {
|
|
1915 n = saved_n;
|
|
1916 src = saved_src;
|
|
1917 in_composite = 0;
|
|
1918 goto back_to_square_n; /* Wheeeeeeeee ..... */
|
|
1919 }
|
|
1920 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
1921
|
|
1922 data->current_char_boundary = char_boundary;
|
|
1923 data->current_charset = charset;
|
|
1924
|
|
1925 /* La palabra se hizo carne! */
|
|
1926 /* A palavra fez-se carne! */
|
|
1927 /* Whatever. */
|
|
1928 }
|
|
1929
|
|
1930 str->ch = ch;
|
|
1931 return orign;
|
|
1932 }
|
|
1933
|
|
1934 /* DEFINE_DETECTOR (utf_7); */
|
|
1935 DEFINE_DETECTOR (utf_8);
|
|
1936 DEFINE_DETECTOR_CATEGORY (utf_8, utf_8);
|
985
|
1937 DEFINE_DETECTOR_CATEGORY (utf_8, utf_8_bom);
|
771
|
1938 DEFINE_DETECTOR (ucs_4);
|
|
1939 DEFINE_DETECTOR_CATEGORY (ucs_4, ucs_4);
|
|
1940 DEFINE_DETECTOR (utf_16);
|
|
1941 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16);
|
|
1942 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian);
|
|
1943 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16_bom);
|
|
1944 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian_bom);
|
|
1945
|
|
1946 struct ucs_4_detector
|
|
1947 {
|
|
1948 int in_ucs_4_byte;
|
|
1949 };
|
|
1950
|
|
1951 static void
|
|
1952 ucs_4_detect (struct detection_state *st, const UExtbyte *src,
|
|
1953 Bytecount n)
|
|
1954 {
|
|
1955 struct ucs_4_detector *data = DETECTION_STATE_DATA (st, ucs_4);
|
|
1956
|
|
1957 while (n--)
|
|
1958 {
|
|
1959 UExtbyte c = *src++;
|
|
1960 switch (data->in_ucs_4_byte)
|
|
1961 {
|
|
1962 case 0:
|
|
1963 if (c >= 128)
|
|
1964 {
|
|
1965 DET_RESULT (st, ucs_4) = DET_NEARLY_IMPOSSIBLE;
|
|
1966 return;
|
|
1967 }
|
|
1968 else
|
|
1969 data->in_ucs_4_byte++;
|
|
1970 break;
|
|
1971 case 3:
|
|
1972 data->in_ucs_4_byte = 0;
|
|
1973 break;
|
|
1974 default:
|
|
1975 data->in_ucs_4_byte++;
|
|
1976 }
|
|
1977 }
|
|
1978
|
|
1979 /* !!#### write this for real */
|
|
1980 DET_RESULT (st, ucs_4) = DET_AS_LIKELY_AS_UNLIKELY;
|
|
1981 }
|
|
1982
|
|
1983 struct utf_16_detector
|
|
1984 {
|
|
1985 unsigned int seen_ffff:1;
|
|
1986 unsigned int seen_forward_bom:1;
|
|
1987 unsigned int seen_rev_bom:1;
|
|
1988 int byteno;
|
|
1989 int prev_char;
|
|
1990 int text, rev_text;
|
1267
|
1991 int sep, rev_sep;
|
|
1992 int num_ascii;
|
771
|
1993 };
|
|
1994
|
|
1995 static void
|
|
1996 utf_16_detect (struct detection_state *st, const UExtbyte *src,
|
|
1997 Bytecount n)
|
|
1998 {
|
|
1999 struct utf_16_detector *data = DETECTION_STATE_DATA (st, utf_16);
|
|
2000
|
|
2001 while (n--)
|
|
2002 {
|
|
2003 UExtbyte c = *src++;
|
|
2004 int prevc = data->prev_char;
|
|
2005 if (data->byteno == 1 && c == 0xFF && prevc == 0xFE)
|
|
2006 data->seen_forward_bom = 1;
|
|
2007 else if (data->byteno == 1 && c == 0xFE && prevc == 0xFF)
|
|
2008 data->seen_rev_bom = 1;
|
|
2009
|
|
2010 if (data->byteno & 1)
|
|
2011 {
|
|
2012 if (c == 0xFF && prevc == 0xFF)
|
|
2013 data->seen_ffff = 1;
|
|
2014 if (prevc == 0
|
|
2015 && (c == '\r' || c == '\n'
|
|
2016 || (c >= 0x20 && c <= 0x7E)))
|
|
2017 data->text++;
|
|
2018 if (c == 0
|
|
2019 && (prevc == '\r' || prevc == '\n'
|
|
2020 || (prevc >= 0x20 && prevc <= 0x7E)))
|
|
2021 data->rev_text++;
|
1267
|
2022 /* #### 0x2028 is LINE SEPARATOR and 0x2029 is PARAGRAPH SEPARATOR.
|
|
2023 I used to count these in text and rev_text but that is very bad,
|
|
2024 as 0x2028 is also space + left-paren in ASCII, which is extremely
|
|
2025 common. So, what do we do with these? */
|
771
|
2026 if (prevc == 0x20 && (c == 0x28 || c == 0x29))
|
1267
|
2027 data->sep++;
|
771
|
2028 if (c == 0x20 && (prevc == 0x28 || prevc == 0x29))
|
1267
|
2029 data->rev_sep++;
|
771
|
2030 }
|
|
2031
|
1267
|
2032 if ((c >= ' ' && c <= '~') || c == '\n' || c == '\r' || c == '\t' ||
|
|
2033 c == '\f' || c == '\v')
|
|
2034 data->num_ascii++;
|
771
|
2035 data->byteno++;
|
|
2036 data->prev_char = c;
|
|
2037 }
|
|
2038
|
|
2039 {
|
|
2040 int variance_indicates_big_endian =
|
|
2041 (data->text >= 10
|
|
2042 && (data->rev_text == 0
|
|
2043 || data->text / data->rev_text >= 10));
|
|
2044 int variance_indicates_little_endian =
|
|
2045 (data->rev_text >= 10
|
|
2046 && (data->text == 0
|
|
2047 || data->rev_text / data->text >= 10));
|
|
2048
|
|
2049 if (data->seen_ffff)
|
|
2050 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2051 else if (data->seen_forward_bom)
|
|
2052 {
|
|
2053 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2054 if (variance_indicates_big_endian)
|
|
2055 DET_RESULT (st, utf_16_bom) = DET_NEAR_CERTAINTY;
|
|
2056 else if (variance_indicates_little_endian)
|
|
2057 DET_RESULT (st, utf_16_bom) = DET_SOMEWHAT_LIKELY;
|
|
2058 else
|
|
2059 DET_RESULT (st, utf_16_bom) = DET_QUITE_PROBABLE;
|
|
2060 }
|
|
2061 else if (data->seen_forward_bom)
|
|
2062 {
|
|
2063 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2064 if (variance_indicates_big_endian)
|
|
2065 DET_RESULT (st, utf_16_bom) = DET_NEAR_CERTAINTY;
|
|
2066 else if (variance_indicates_little_endian)
|
|
2067 /* #### may need to rethink */
|
|
2068 DET_RESULT (st, utf_16_bom) = DET_SOMEWHAT_LIKELY;
|
|
2069 else
|
|
2070 /* #### may need to rethink */
|
|
2071 DET_RESULT (st, utf_16_bom) = DET_QUITE_PROBABLE;
|
|
2072 }
|
|
2073 else if (data->seen_rev_bom)
|
|
2074 {
|
|
2075 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2076 if (variance_indicates_little_endian)
|
|
2077 DET_RESULT (st, utf_16_little_endian_bom) = DET_NEAR_CERTAINTY;
|
|
2078 else if (variance_indicates_big_endian)
|
|
2079 /* #### may need to rethink */
|
|
2080 DET_RESULT (st, utf_16_little_endian_bom) = DET_SOMEWHAT_LIKELY;
|
|
2081 else
|
|
2082 /* #### may need to rethink */
|
|
2083 DET_RESULT (st, utf_16_little_endian_bom) = DET_QUITE_PROBABLE;
|
|
2084 }
|
|
2085 else if (variance_indicates_big_endian)
|
|
2086 {
|
|
2087 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2088 DET_RESULT (st, utf_16) = DET_SOMEWHAT_LIKELY;
|
|
2089 DET_RESULT (st, utf_16_little_endian) = DET_SOMEWHAT_UNLIKELY;
|
|
2090 }
|
|
2091 else if (variance_indicates_little_endian)
|
|
2092 {
|
|
2093 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2094 DET_RESULT (st, utf_16) = DET_SOMEWHAT_UNLIKELY;
|
|
2095 DET_RESULT (st, utf_16_little_endian) = DET_SOMEWHAT_LIKELY;
|
|
2096 }
|
|
2097 else
|
1267
|
2098 {
|
|
2099 /* #### FUCKME! There should really be an ASCII detector. This
|
|
2100 would rule out the need to have this built-in here as
|
|
2101 well. --ben */
|
1292
|
2102 int pct_ascii = data->byteno ? (100 * data->num_ascii) / data->byteno
|
|
2103 : 100;
|
1267
|
2104
|
|
2105 if (pct_ascii > 90)
|
|
2106 SET_DET_RESULTS (st, utf_16, DET_QUITE_IMPROBABLE);
|
|
2107 else if (pct_ascii > 75)
|
|
2108 SET_DET_RESULTS (st, utf_16, DET_SOMEWHAT_UNLIKELY);
|
|
2109 else
|
|
2110 SET_DET_RESULTS (st, utf_16, DET_AS_LIKELY_AS_UNLIKELY);
|
|
2111 }
|
771
|
2112 }
|
|
2113 }
|
|
2114
|
|
2115 struct utf_8_detector
|
|
2116 {
|
985
|
2117 int byteno;
|
|
2118 int first_byte;
|
|
2119 int second_byte;
|
1267
|
2120 int prev_byte;
|
771
|
2121 int in_utf_8_byte;
|
1267
|
2122 int recent_utf_8_sequence;
|
|
2123 int seen_bogus_utf8;
|
|
2124 int seen_really_bogus_utf8;
|
|
2125 int seen_2byte_sequence;
|
|
2126 int seen_longer_sequence;
|
|
2127 int seen_iso2022_esc;
|
|
2128 int seen_iso_shift;
|
1887
|
2129 unsigned int seen_utf_bom:1;
|
771
|
2130 };
|
|
2131
|
|
2132 static void
|
|
2133 utf_8_detect (struct detection_state *st, const UExtbyte *src,
|
|
2134 Bytecount n)
|
|
2135 {
|
|
2136 struct utf_8_detector *data = DETECTION_STATE_DATA (st, utf_8);
|
|
2137
|
|
2138 while (n--)
|
|
2139 {
|
|
2140 UExtbyte c = *src++;
|
985
|
2141 switch (data->byteno)
|
|
2142 {
|
|
2143 case 0:
|
|
2144 data->first_byte = c;
|
|
2145 break;
|
|
2146 case 1:
|
|
2147 data->second_byte = c;
|
|
2148 break;
|
|
2149 case 2:
|
|
2150 if (data->first_byte == 0xef &&
|
|
2151 data->second_byte == 0xbb &&
|
|
2152 c == 0xbf)
|
1267
|
2153 data->seen_utf_bom = 1;
|
985
|
2154 break;
|
|
2155 }
|
|
2156
|
771
|
2157 switch (data->in_utf_8_byte)
|
|
2158 {
|
|
2159 case 0:
|
1267
|
2160 if (data->prev_byte == ISO_CODE_ESC && c >= 0x28 && c <= 0x2F)
|
|
2161 data->seen_iso2022_esc++;
|
|
2162 else if (c == ISO_CODE_SI || c == ISO_CODE_SO)
|
|
2163 data->seen_iso_shift++;
|
771
|
2164 else if (c >= 0xfc)
|
|
2165 data->in_utf_8_byte = 5;
|
|
2166 else if (c >= 0xf8)
|
|
2167 data->in_utf_8_byte = 4;
|
|
2168 else if (c >= 0xf0)
|
|
2169 data->in_utf_8_byte = 3;
|
|
2170 else if (c >= 0xe0)
|
|
2171 data->in_utf_8_byte = 2;
|
|
2172 else if (c >= 0xc0)
|
|
2173 data->in_utf_8_byte = 1;
|
|
2174 else if (c >= 0x80)
|
1267
|
2175 data->seen_bogus_utf8++;
|
|
2176 if (data->in_utf_8_byte > 0)
|
|
2177 data->recent_utf_8_sequence = data->in_utf_8_byte;
|
771
|
2178 break;
|
|
2179 default:
|
|
2180 if ((c & 0xc0) != 0x80)
|
1267
|
2181 data->seen_really_bogus_utf8++;
|
|
2182 else
|
771
|
2183 {
|
1267
|
2184 data->in_utf_8_byte--;
|
|
2185 if (data->in_utf_8_byte == 0)
|
|
2186 {
|
|
2187 if (data->recent_utf_8_sequence == 1)
|
|
2188 data->seen_2byte_sequence++;
|
|
2189 else
|
|
2190 {
|
|
2191 assert (data->recent_utf_8_sequence >= 2);
|
|
2192 data->seen_longer_sequence++;
|
|
2193 }
|
|
2194 }
|
771
|
2195 }
|
|
2196 }
|
985
|
2197
|
|
2198 data->byteno++;
|
1267
|
2199 data->prev_byte = c;
|
771
|
2200 }
|
1267
|
2201
|
|
2202 /* either BOM or no BOM, but not both */
|
|
2203 SET_DET_RESULTS (st, utf_8, DET_NEARLY_IMPOSSIBLE);
|
|
2204
|
|
2205
|
|
2206 if (data->seen_utf_bom)
|
|
2207 DET_RESULT (st, utf_8_bom) = DET_NEAR_CERTAINTY;
|
|
2208 else
|
|
2209 {
|
|
2210 if (data->seen_really_bogus_utf8 ||
|
|
2211 data->seen_bogus_utf8 >= 2)
|
|
2212 ; /* bogus */
|
|
2213 else if (data->seen_bogus_utf8)
|
|
2214 DET_RESULT (st, utf_8) = DET_SOMEWHAT_UNLIKELY;
|
|
2215 else if ((data->seen_longer_sequence >= 5 ||
|
|
2216 data->seen_2byte_sequence >= 10) &&
|
|
2217 (!(data->seen_iso2022_esc + data->seen_iso_shift) ||
|
|
2218 (data->seen_longer_sequence * 2 + data->seen_2byte_sequence) /
|
|
2219 (data->seen_iso2022_esc + data->seen_iso_shift) >= 10))
|
|
2220 /* heuristics, heuristics, we love heuristics */
|
|
2221 DET_RESULT (st, utf_8) = DET_QUITE_PROBABLE;
|
|
2222 else if (data->seen_iso2022_esc ||
|
|
2223 data->seen_iso_shift >= 3)
|
|
2224 DET_RESULT (st, utf_8) = DET_SOMEWHAT_UNLIKELY;
|
|
2225 else if (data->seen_longer_sequence ||
|
|
2226 data->seen_2byte_sequence)
|
|
2227 DET_RESULT (st, utf_8) = DET_SOMEWHAT_LIKELY;
|
|
2228 else if (data->seen_iso_shift)
|
|
2229 DET_RESULT (st, utf_8) = DET_SOMEWHAT_UNLIKELY;
|
|
2230 else
|
|
2231 DET_RESULT (st, utf_8) = DET_AS_LIKELY_AS_UNLIKELY;
|
|
2232 }
|
771
|
2233 }
|
|
2234
|
|
2235 static void
|
|
2236 unicode_init_coding_stream (struct coding_stream *str)
|
|
2237 {
|
|
2238 struct unicode_coding_stream *data =
|
|
2239 CODING_STREAM_TYPE_DATA (str, unicode);
|
|
2240 xzero (*data);
|
|
2241 data->current_charset = Qnil;
|
|
2242 }
|
|
2243
|
|
2244 static void
|
|
2245 unicode_rewind_coding_stream (struct coding_stream *str)
|
|
2246 {
|
|
2247 unicode_init_coding_stream (str);
|
|
2248 }
|
|
2249
|
|
2250 static int
|
|
2251 unicode_putprop (Lisp_Object codesys, Lisp_Object key, Lisp_Object value)
|
|
2252 {
|
|
2253 if (EQ (key, Qtype))
|
|
2254 {
|
|
2255 enum unicode_type type;
|
|
2256
|
|
2257 if (EQ (value, Qutf_8))
|
|
2258 type = UNICODE_UTF_8;
|
|
2259 else if (EQ (value, Qutf_16))
|
|
2260 type = UNICODE_UTF_16;
|
|
2261 else if (EQ (value, Qutf_7))
|
|
2262 type = UNICODE_UTF_7;
|
|
2263 else if (EQ (value, Qucs_4))
|
|
2264 type = UNICODE_UCS_4;
|
|
2265 else
|
|
2266 invalid_constant ("Invalid Unicode type", key);
|
|
2267
|
|
2268 XCODING_SYSTEM_UNICODE_TYPE (codesys) = type;
|
|
2269 }
|
|
2270 else if (EQ (key, Qlittle_endian))
|
|
2271 XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (codesys) = !NILP (value);
|
|
2272 else if (EQ (key, Qneed_bom))
|
|
2273 XCODING_SYSTEM_UNICODE_NEED_BOM (codesys) = !NILP (value);
|
|
2274 else
|
|
2275 return 0;
|
|
2276 return 1;
|
|
2277 }
|
|
2278
|
|
2279 static Lisp_Object
|
|
2280 unicode_getprop (Lisp_Object coding_system, Lisp_Object prop)
|
|
2281 {
|
|
2282 if (EQ (prop, Qtype))
|
|
2283 {
|
|
2284 switch (XCODING_SYSTEM_UNICODE_TYPE (coding_system))
|
|
2285 {
|
|
2286 case UNICODE_UTF_16: return Qutf_16;
|
|
2287 case UNICODE_UTF_8: return Qutf_8;
|
|
2288 case UNICODE_UTF_7: return Qutf_7;
|
|
2289 case UNICODE_UCS_4: return Qucs_4;
|
|
2290 default: abort ();
|
|
2291 }
|
|
2292 }
|
|
2293 else if (EQ (prop, Qlittle_endian))
|
|
2294 return XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (coding_system) ? Qt : Qnil;
|
|
2295 else if (EQ (prop, Qneed_bom))
|
|
2296 return XCODING_SYSTEM_UNICODE_NEED_BOM (coding_system) ? Qt : Qnil;
|
|
2297 return Qunbound;
|
|
2298 }
|
|
2299
|
|
2300 static void
|
|
2301 unicode_print (Lisp_Object cs, Lisp_Object printcharfun, int escapeflag)
|
|
2302 {
|
800
|
2303 write_fmt_string_lisp (printcharfun, "(%s", 1, unicode_getprop (cs, Qtype));
|
771
|
2304 if (XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (cs))
|
826
|
2305 write_c_string (printcharfun, ", little-endian");
|
771
|
2306 if (XCODING_SYSTEM_UNICODE_NEED_BOM (cs))
|
826
|
2307 write_c_string (printcharfun, ", need-bom");
|
|
2308 write_c_string (printcharfun, ")");
|
771
|
2309 }
|
|
2310
|
|
2311 int
|
|
2312 dfc_coding_system_is_unicode (Lisp_Object codesys)
|
|
2313 {
|
1315
|
2314 #ifdef WIN32_ANY
|
771
|
2315 codesys = Fget_coding_system (codesys);
|
|
2316 return (EQ (XCODING_SYSTEM_TYPE (codesys), Qunicode) &&
|
|
2317 XCODING_SYSTEM_UNICODE_TYPE (codesys) == UNICODE_UTF_16 &&
|
|
2318 XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (codesys));
|
|
2319
|
|
2320 #else
|
|
2321 return 0;
|
|
2322 #endif
|
|
2323 }
|
|
2324
|
|
2325
|
|
2326 /************************************************************************/
|
|
2327 /* Initialization */
|
|
2328 /************************************************************************/
|
|
2329
|
|
2330 void
|
|
2331 syms_of_unicode (void)
|
|
2332 {
|
|
2333 #ifdef MULE
|
877
|
2334 DEFSUBR (Funicode_precedence_list);
|
771
|
2335 DEFSUBR (Fset_language_unicode_precedence_list);
|
|
2336 DEFSUBR (Flanguage_unicode_precedence_list);
|
|
2337 DEFSUBR (Fset_default_unicode_precedence_list);
|
|
2338 DEFSUBR (Fdefault_unicode_precedence_list);
|
|
2339 DEFSUBR (Fset_unicode_conversion);
|
|
2340
|
1318
|
2341 DEFSUBR (Fload_unicode_mapping_table);
|
771
|
2342
|
|
2343 DEFSYMBOL (Qignore_first_column);
|
|
2344 #endif /* MULE */
|
|
2345
|
800
|
2346 DEFSUBR (Fchar_to_unicode);
|
|
2347 DEFSUBR (Funicode_to_char);
|
771
|
2348
|
|
2349 DEFSYMBOL (Qunicode);
|
|
2350 DEFSYMBOL (Qucs_4);
|
|
2351 DEFSYMBOL (Qutf_16);
|
|
2352 DEFSYMBOL (Qutf_8);
|
|
2353 DEFSYMBOL (Qutf_7);
|
|
2354
|
|
2355 DEFSYMBOL (Qneed_bom);
|
|
2356
|
|
2357 DEFSYMBOL (Qutf_16);
|
|
2358 DEFSYMBOL (Qutf_16_little_endian);
|
|
2359 DEFSYMBOL (Qutf_16_bom);
|
|
2360 DEFSYMBOL (Qutf_16_little_endian_bom);
|
985
|
2361
|
|
2362 DEFSYMBOL (Qutf_8);
|
|
2363 DEFSYMBOL (Qutf_8_bom);
|
771
|
2364 }
|
|
2365
|
|
2366 void
|
|
2367 coding_system_type_create_unicode (void)
|
|
2368 {
|
|
2369 INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA (unicode, "unicode-coding-system-p");
|
|
2370 CODING_SYSTEM_HAS_METHOD (unicode, print);
|
|
2371 CODING_SYSTEM_HAS_METHOD (unicode, convert);
|
|
2372 CODING_SYSTEM_HAS_METHOD (unicode, init_coding_stream);
|
|
2373 CODING_SYSTEM_HAS_METHOD (unicode, rewind_coding_stream);
|
|
2374 CODING_SYSTEM_HAS_METHOD (unicode, putprop);
|
|
2375 CODING_SYSTEM_HAS_METHOD (unicode, getprop);
|
|
2376
|
|
2377 INITIALIZE_DETECTOR (utf_8);
|
|
2378 DETECTOR_HAS_METHOD (utf_8, detect);
|
|
2379 INITIALIZE_DETECTOR_CATEGORY (utf_8, utf_8);
|
985
|
2380 INITIALIZE_DETECTOR_CATEGORY (utf_8, utf_8_bom);
|
771
|
2381
|
|
2382 INITIALIZE_DETECTOR (ucs_4);
|
|
2383 DETECTOR_HAS_METHOD (ucs_4, detect);
|
|
2384 INITIALIZE_DETECTOR_CATEGORY (ucs_4, ucs_4);
|
|
2385
|
|
2386 INITIALIZE_DETECTOR (utf_16);
|
|
2387 DETECTOR_HAS_METHOD (utf_16, detect);
|
|
2388 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16);
|
|
2389 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian);
|
|
2390 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16_bom);
|
|
2391 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian_bom);
|
|
2392 }
|
|
2393
|
|
2394 void
|
|
2395 reinit_coding_system_type_create_unicode (void)
|
|
2396 {
|
|
2397 REINITIALIZE_CODING_SYSTEM_TYPE (unicode);
|
|
2398 }
|
|
2399
|
|
2400 void
|
|
2401 reinit_vars_of_unicode (void)
|
|
2402 {
|
|
2403 #ifdef MULE
|
|
2404 init_blank_unicode_tables ();
|
|
2405 #endif /* MULE */
|
|
2406 }
|
|
2407
|
|
2408 void
|
|
2409 vars_of_unicode (void)
|
|
2410 {
|
|
2411 reinit_vars_of_unicode ();
|
|
2412
|
|
2413 Fprovide (intern ("unicode"));
|
|
2414
|
|
2415 #ifdef MULE
|
|
2416 staticpro (&Vlanguage_unicode_precedence_list);
|
|
2417 Vlanguage_unicode_precedence_list = Qnil;
|
|
2418
|
|
2419 staticpro (&Vdefault_unicode_precedence_list);
|
|
2420 Vdefault_unicode_precedence_list = Qnil;
|
|
2421
|
|
2422 unicode_precedence_dynarr = Dynarr_new (Lisp_Object);
|
|
2423 dump_add_root_struct_ptr (&unicode_precedence_dynarr,
|
|
2424 &lisp_object_dynarr_description);
|
|
2425 #endif /* MULE */
|
|
2426 }
|