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