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