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