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
|
|
551 size += malloced_storage_size (table,
|
|
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
|
|
576 size += malloced_storage_size (table,
|
|
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
|
|
860 /* First, the char -> unicode translation */
|
|
861
|
|
862 if (XCHARSET_DIMENSION (charset) == 1)
|
|
863 {
|
|
864 int *to_table = (int *) XCHARSET_TO_UNICODE_TABLE (charset);
|
|
865 to_table[c1 - 32] = code;
|
|
866 }
|
|
867 else
|
|
868 {
|
|
869 int **to_table_2 = (int **) XCHARSET_TO_UNICODE_TABLE (charset);
|
|
870 int *to_table_1;
|
|
871
|
|
872 assert (XCHARSET_DIMENSION (charset) == 2);
|
|
873 to_table_1 = to_table_2[c1 - 32];
|
|
874 if (to_table_1 == to_unicode_blank_1)
|
|
875 {
|
|
876 to_table_1 = xnew_array (int, 96);
|
|
877 memcpy (to_table_1, to_unicode_blank_1, 96 * sizeof (int));
|
|
878 to_table_2[c1 - 32] = to_table_1;
|
|
879 }
|
|
880 to_table_1[c2 - 32] = code;
|
|
881 }
|
|
882
|
|
883 /* Then, unicode -> char: much harder */
|
|
884
|
|
885 {
|
|
886 int charset_levels;
|
|
887 int u4, u3, u2, u1;
|
|
888 int code_levels;
|
|
889 BREAKUP_UNICODE_CODE (code, u4, u3, u2, u1, code_levels);
|
|
890
|
|
891 charset_levels = XCHARSET_FROM_UNICODE_LEVELS (charset);
|
|
892
|
|
893 /* Make sure the charset's tables have at least as many levels as
|
|
894 the code point has: Note that the charset is guaranteed to have
|
|
895 at least one level, because it was created that way */
|
|
896 if (charset_levels < code_levels)
|
|
897 {
|
|
898 int i;
|
|
899
|
|
900 assert (charset_levels > 0);
|
|
901 for (i = 2; i <= code_levels; i++)
|
|
902 {
|
|
903 if (charset_levels < i)
|
|
904 {
|
|
905 void *old_table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
906 void *table = create_new_from_unicode_table (i);
|
|
907 XCHARSET_FROM_UNICODE_TABLE (charset) = table;
|
|
908
|
|
909 switch (i)
|
|
910 {
|
|
911 case 2:
|
|
912 ((short **) table)[0] = (short *) old_table;
|
|
913 break;
|
|
914 case 3:
|
|
915 ((short ***) table)[0] = (short **) old_table;
|
|
916 break;
|
|
917 case 4:
|
|
918 ((short ****) table)[0] = (short ***) old_table;
|
|
919 break;
|
2500
|
920 default: ABORT ();
|
771
|
921 }
|
|
922 }
|
|
923 }
|
|
924
|
|
925 charset_levels = code_levels;
|
|
926 XCHARSET_FROM_UNICODE_LEVELS (charset) = code_levels;
|
|
927 }
|
|
928
|
|
929 /* Now, make sure there is a non-default table at each level */
|
|
930 {
|
|
931 int i;
|
|
932 void *table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
933
|
|
934 for (i = charset_levels; i >= 2; i--)
|
|
935 {
|
|
936 switch (i)
|
|
937 {
|
|
938 case 4:
|
|
939 if (((short ****) table)[u4] == from_unicode_blank_3)
|
|
940 ((short ****) table)[u4] =
|
|
941 ((short ***) create_new_from_unicode_table (3));
|
|
942 table = ((short ****) table)[u4];
|
|
943 break;
|
|
944 case 3:
|
|
945 if (((short ***) table)[u3] == from_unicode_blank_2)
|
|
946 ((short ***) table)[u3] =
|
|
947 ((short **) create_new_from_unicode_table (2));
|
|
948 table = ((short ***) table)[u3];
|
|
949 break;
|
|
950 case 2:
|
|
951 if (((short **) table)[u2] == from_unicode_blank_1)
|
|
952 ((short **) table)[u2] =
|
|
953 ((short *) create_new_from_unicode_table (1));
|
|
954 table = ((short **) table)[u2];
|
|
955 break;
|
2500
|
956 default: ABORT ();
|
771
|
957 }
|
|
958 }
|
|
959 }
|
|
960
|
|
961 /* Finally, set the character */
|
|
962
|
|
963 {
|
|
964 void *table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
965 switch (charset_levels)
|
|
966 {
|
|
967 case 1: ((short *) table)[u1] = (c1 << 8) + c2; break;
|
|
968 case 2: ((short **) table)[u2][u1] = (c1 << 8) + c2; break;
|
|
969 case 3: ((short ***) table)[u3][u2][u1] = (c1 << 8) + c2; break;
|
|
970 case 4: ((short ****) table)[u4][u3][u2][u1] = (c1 << 8) + c2; break;
|
2500
|
971 default: ABORT ();
|
771
|
972 }
|
|
973 }
|
|
974 }
|
|
975
|
|
976 #ifdef SLEDGEHAMMER_CHECK_UNICODE
|
|
977 sledgehammer_check_unicode_tables (charset);
|
|
978 #endif
|
|
979 }
|
|
980
|
788
|
981 int
|
867
|
982 ichar_to_unicode (Ichar chr)
|
771
|
983 {
|
|
984 Lisp_Object charset;
|
|
985 int c1, c2;
|
|
986
|
867
|
987 type_checking_assert (valid_ichar_p (chr));
|
877
|
988 /* This shortcut depends on the representation of an Ichar, see text.c. */
|
771
|
989 if (chr < 256)
|
|
990 return (int) chr;
|
|
991
|
867
|
992 BREAKUP_ICHAR (chr, charset, c1, c2);
|
771
|
993 if (EQ (charset, Vcharset_composite))
|
|
994 return -1; /* #### don't know how to handle */
|
|
995 else if (XCHARSET_DIMENSION (charset) == 1)
|
|
996 return ((int *) XCHARSET_TO_UNICODE_TABLE (charset))[c1 - 32];
|
|
997 else
|
|
998 return ((int **) XCHARSET_TO_UNICODE_TABLE (charset))[c1 - 32][c2 - 32];
|
|
999 }
|
|
1000
|
867
|
1001 static Ichar
|
877
|
1002 unicode_to_ichar (int code, Lisp_Object_dynarr *charsets)
|
771
|
1003 {
|
|
1004 int u1, u2, u3, u4;
|
|
1005 int code_levels;
|
|
1006 int i;
|
|
1007 int n = Dynarr_length (charsets);
|
|
1008
|
|
1009 type_checking_assert (code >= 0);
|
877
|
1010 /* This shortcut depends on the representation of an Ichar, see text.c.
|
|
1011 Note that it may _not_ be extended to U+00A0 to U+00FF (many ISO 8859
|
893
|
1012 coded character sets have points that map into that region, so this
|
|
1013 function is many-valued). */
|
877
|
1014 if (code < 0xA0)
|
867
|
1015 return (Ichar) code;
|
771
|
1016
|
|
1017 BREAKUP_UNICODE_CODE (code, u4, u3, u2, u1, code_levels);
|
|
1018
|
|
1019 for (i = 0; i < n; i++)
|
|
1020 {
|
|
1021 Lisp_Object charset = Dynarr_at (charsets, i);
|
|
1022 int charset_levels = XCHARSET_FROM_UNICODE_LEVELS (charset);
|
|
1023 if (charset_levels >= code_levels)
|
|
1024 {
|
|
1025 void *table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
1026 short retval;
|
|
1027
|
|
1028 switch (charset_levels)
|
|
1029 {
|
|
1030 case 1: retval = ((short *) table)[u1]; break;
|
|
1031 case 2: retval = ((short **) table)[u2][u1]; break;
|
|
1032 case 3: retval = ((short ***) table)[u3][u2][u1]; break;
|
|
1033 case 4: retval = ((short ****) table)[u4][u3][u2][u1]; break;
|
2500
|
1034 default: ABORT (); retval = 0;
|
771
|
1035 }
|
|
1036
|
|
1037 if (retval != -1)
|
867
|
1038 return make_ichar (charset, retval >> 8, retval & 0xFF);
|
771
|
1039 }
|
|
1040 }
|
|
1041
|
867
|
1042 return (Ichar) -1;
|
771
|
1043 }
|
|
1044
|
877
|
1045 /* Add charsets to precedence list.
|
|
1046 LIST must be a list of charsets. Charsets which are in the list more
|
|
1047 than once are given the precedence implied by their earliest appearance.
|
|
1048 Later appearances are ignored. */
|
771
|
1049 static void
|
|
1050 add_charsets_to_precedence_list (Lisp_Object list, int *lbs,
|
|
1051 Lisp_Object_dynarr *dynarr)
|
|
1052 {
|
|
1053 {
|
|
1054 EXTERNAL_LIST_LOOP_2 (elt, list)
|
|
1055 {
|
|
1056 Lisp_Object charset = Fget_charset (elt);
|
778
|
1057 int lb = XCHARSET_LEADING_BYTE (charset);
|
771
|
1058 if (lbs[lb - MIN_LEADING_BYTE] == 0)
|
|
1059 {
|
877
|
1060 Dynarr_add (dynarr, charset);
|
771
|
1061 lbs[lb - MIN_LEADING_BYTE] = 1;
|
|
1062 }
|
|
1063 }
|
|
1064 }
|
|
1065 }
|
|
1066
|
877
|
1067 /* Rebuild the charset precedence array.
|
|
1068 The "charsets preferred for the current language" get highest precedence,
|
|
1069 followed by the "charsets preferred by default", ordered as in
|
|
1070 Vlanguage_unicode_precedence_list and Vdefault_unicode_precedence_list,
|
|
1071 respectively. All remaining charsets follow in an arbitrary order. */
|
771
|
1072 void
|
|
1073 recalculate_unicode_precedence (void)
|
|
1074 {
|
|
1075 int lbs[NUM_LEADING_BYTES];
|
|
1076 int i;
|
|
1077
|
|
1078 for (i = 0; i < NUM_LEADING_BYTES; i++)
|
|
1079 lbs[i] = 0;
|
|
1080
|
|
1081 Dynarr_reset (unicode_precedence_dynarr);
|
|
1082
|
|
1083 add_charsets_to_precedence_list (Vlanguage_unicode_precedence_list,
|
|
1084 lbs, unicode_precedence_dynarr);
|
|
1085 add_charsets_to_precedence_list (Vdefault_unicode_precedence_list,
|
|
1086 lbs, unicode_precedence_dynarr);
|
|
1087
|
|
1088 for (i = 0; i < NUM_LEADING_BYTES; i++)
|
|
1089 {
|
|
1090 if (lbs[i] == 0)
|
|
1091 {
|
826
|
1092 Lisp_Object charset = charset_by_leading_byte (i + MIN_LEADING_BYTE);
|
771
|
1093 if (!NILP (charset))
|
|
1094 Dynarr_add (unicode_precedence_dynarr, charset);
|
|
1095 }
|
|
1096 }
|
|
1097 }
|
|
1098
|
877
|
1099 DEFUN ("unicode-precedence-list",
|
|
1100 Funicode_precedence_list,
|
|
1101 0, 0, 0, /*
|
|
1102 Return the precedence order among charsets used for Unicode decoding.
|
|
1103
|
|
1104 Value is a list of charsets, which are searched in order for a translation
|
|
1105 matching a given Unicode character.
|
|
1106
|
|
1107 The highest precedence is given to the language-specific precedence list of
|
|
1108 charsets, defined by `set-language-unicode-precedence-list'. These are
|
|
1109 followed by charsets in the default precedence list, defined by
|
|
1110 `set-default-unicode-precedence-list'. Charsets occurring multiple times are
|
|
1111 given precedence according to their first occurrance in either list. These
|
|
1112 are followed by the remaining charsets, in some arbitrary order.
|
771
|
1113
|
|
1114 The language-specific precedence list is meant to be set as part of the
|
|
1115 language environment initialization; the default precedence list is meant
|
|
1116 to be set by the user.
|
1318
|
1117
|
|
1118 #### NOTE: This interface may be changed.
|
771
|
1119 */
|
877
|
1120 ())
|
|
1121 {
|
|
1122 int i;
|
|
1123 Lisp_Object list = Qnil;
|
|
1124
|
|
1125 for (i = Dynarr_length (unicode_precedence_dynarr) - 1; i >= 0; i--)
|
|
1126 list = Fcons (Dynarr_at (unicode_precedence_dynarr, i), list);
|
|
1127 return list;
|
|
1128 }
|
|
1129
|
|
1130
|
|
1131 /* #### This interface is wrong. Cyrillic users and Chinese users are going
|
|
1132 to have varying opinions about whether ISO Cyrillic, KOI8-R, or Windows
|
|
1133 1251 should take precedence, and whether Big Five or CNS should take
|
|
1134 precedence, respectively. This means that users are sometimes going to
|
|
1135 want to set Vlanguage_unicode_precedence_list.
|
|
1136 Furthermore, this should be language-local (buffer-local would be a
|
1318
|
1137 reasonable approximation).
|
|
1138
|
|
1139 Answer: You are right, this needs rethinking. */
|
877
|
1140 DEFUN ("set-language-unicode-precedence-list",
|
|
1141 Fset_language_unicode_precedence_list,
|
|
1142 1, 1, 0, /*
|
|
1143 Set the language-specific precedence of charsets in Unicode decoding.
|
|
1144 LIST is a list of charsets.
|
|
1145 See `unicode-precedence-list' for more information.
|
1318
|
1146
|
|
1147 #### NOTE: This interface may be changed.
|
877
|
1148 */
|
771
|
1149 (list))
|
|
1150 {
|
|
1151 {
|
|
1152 EXTERNAL_LIST_LOOP_2 (elt, list)
|
|
1153 Fget_charset (elt);
|
|
1154 }
|
|
1155
|
|
1156 Vlanguage_unicode_precedence_list = list;
|
|
1157 recalculate_unicode_precedence ();
|
|
1158 return Qnil;
|
|
1159 }
|
|
1160
|
|
1161 DEFUN ("language-unicode-precedence-list",
|
|
1162 Flanguage_unicode_precedence_list,
|
|
1163 0, 0, 0, /*
|
|
1164 Return the language-specific precedence list used for Unicode decoding.
|
877
|
1165 See `unicode-precedence-list' for more information.
|
1318
|
1166
|
|
1167 #### NOTE: This interface may be changed.
|
771
|
1168 */
|
|
1169 ())
|
|
1170 {
|
|
1171 return Vlanguage_unicode_precedence_list;
|
|
1172 }
|
|
1173
|
|
1174 DEFUN ("set-default-unicode-precedence-list",
|
|
1175 Fset_default_unicode_precedence_list,
|
|
1176 1, 1, 0, /*
|
|
1177 Set the default precedence list used for Unicode decoding.
|
877
|
1178 This is intended to be set by the user. See
|
|
1179 `unicode-precedence-list' for more information.
|
1318
|
1180
|
|
1181 #### NOTE: This interface may be changed.
|
771
|
1182 */
|
|
1183 (list))
|
|
1184 {
|
|
1185 {
|
|
1186 EXTERNAL_LIST_LOOP_2 (elt, list)
|
|
1187 Fget_charset (elt);
|
|
1188 }
|
|
1189
|
|
1190 Vdefault_unicode_precedence_list = list;
|
|
1191 recalculate_unicode_precedence ();
|
|
1192 return Qnil;
|
|
1193 }
|
|
1194
|
|
1195 DEFUN ("default-unicode-precedence-list",
|
|
1196 Fdefault_unicode_precedence_list,
|
|
1197 0, 0, 0, /*
|
|
1198 Return the default precedence list used for Unicode decoding.
|
877
|
1199 See `unicode-precedence-list' for more information.
|
1318
|
1200
|
|
1201 #### NOTE: This interface may be changed.
|
771
|
1202 */
|
|
1203 ())
|
|
1204 {
|
|
1205 return Vdefault_unicode_precedence_list;
|
|
1206 }
|
|
1207
|
|
1208 DEFUN ("set-unicode-conversion", Fset_unicode_conversion,
|
|
1209 2, 2, 0, /*
|
|
1210 Add conversion information between Unicode codepoints and characters.
|
877
|
1211 Conversions for U+0000 to U+00FF are hardwired to ASCII, Control-1, and
|
|
1212 Latin-1. Attempts to set these values will raise an error.
|
|
1213
|
771
|
1214 CHARACTER is one of the following:
|
|
1215
|
|
1216 -- A character (in which case CODE must be a non-negative integer; values
|
|
1217 above 2^20 - 1 are allowed for the purpose of specifying private
|
877
|
1218 characters, but are illegal in standard Unicode---they will cause errors
|
|
1219 when converted to utf-16)
|
771
|
1220 -- A vector of characters (in which case CODE must be a vector of integers
|
|
1221 of the same length)
|
|
1222 */
|
|
1223 (character, code))
|
|
1224 {
|
|
1225 Lisp_Object charset;
|
877
|
1226 int ichar, unicode;
|
771
|
1227
|
|
1228 CHECK_CHAR (character);
|
|
1229 CHECK_NATNUM (code);
|
|
1230
|
877
|
1231 unicode = XINT (code);
|
|
1232 ichar = XCHAR (character);
|
|
1233 charset = ichar_charset (ichar);
|
|
1234
|
|
1235 /* The translations of ASCII, Control-1, and Latin-1 code points are
|
|
1236 hard-coded in ichar_to_unicode and unicode_to_ichar.
|
|
1237
|
|
1238 Checking unicode < 256 && ichar != unicode is wrong because Mule gives
|
|
1239 many Latin characters code points in a few different character sets. */
|
|
1240 if ((EQ (charset, Vcharset_ascii) ||
|
|
1241 EQ (charset, Vcharset_control_1) ||
|
|
1242 EQ (charset, Vcharset_latin_iso8859_1))
|
|
1243 && unicode != ichar)
|
893
|
1244 signal_error (Qinvalid_argument, "Can't change Unicode translation for ASCII, Control-1 or Latin-1 character",
|
771
|
1245 character);
|
|
1246
|
877
|
1247 /* #### Composite characters are not properly implemented yet. */
|
|
1248 if (EQ (charset, Vcharset_composite))
|
|
1249 signal_error (Qinvalid_argument, "Can't set Unicode translation for Composite char",
|
|
1250 character);
|
|
1251
|
|
1252 set_unicode_conversion (ichar, unicode);
|
771
|
1253 return Qnil;
|
|
1254 }
|
|
1255
|
|
1256 #endif /* MULE */
|
|
1257
|
800
|
1258 DEFUN ("char-to-unicode", Fchar_to_unicode, 1, 1, 0, /*
|
771
|
1259 Convert character to Unicode codepoint.
|
877
|
1260 When there is no international support (i.e. the 'mule feature is not
|
|
1261 present), this function simply does `char-to-int'.
|
771
|
1262 */
|
|
1263 (character))
|
|
1264 {
|
|
1265 CHECK_CHAR (character);
|
|
1266 #ifdef MULE
|
867
|
1267 return make_int (ichar_to_unicode (XCHAR (character)));
|
771
|
1268 #else
|
|
1269 return Fchar_to_int (character);
|
|
1270 #endif /* MULE */
|
|
1271 }
|
|
1272
|
800
|
1273 DEFUN ("unicode-to-char", Funicode_to_char, 1, 2, 0, /*
|
771
|
1274 Convert Unicode codepoint to character.
|
|
1275 CODE should be a non-negative integer.
|
|
1276 If CHARSETS is given, it should be a list of charsets, and only those
|
|
1277 charsets will be consulted, in the given order, for a translation.
|
|
1278 Otherwise, the default ordering of all charsets will be given (see
|
|
1279 `set-unicode-charset-precedence').
|
|
1280
|
877
|
1281 When there is no international support (i.e. the 'mule feature is not
|
|
1282 present), this function simply does `int-to-char' and ignores the CHARSETS
|
|
1283 argument.
|
771
|
1284 */
|
2333
|
1285 (code, USED_IF_MULE (charsets)))
|
771
|
1286 {
|
|
1287 #ifdef MULE
|
|
1288 Lisp_Object_dynarr *dyn;
|
|
1289 int lbs[NUM_LEADING_BYTES];
|
|
1290 int c;
|
|
1291
|
|
1292 CHECK_NATNUM (code);
|
|
1293 c = XINT (code);
|
|
1294 {
|
|
1295 EXTERNAL_LIST_LOOP_2 (elt, charsets)
|
|
1296 Fget_charset (elt);
|
|
1297 }
|
|
1298
|
|
1299 if (NILP (charsets))
|
|
1300 {
|
877
|
1301 Ichar ret = unicode_to_ichar (c, unicode_precedence_dynarr);
|
771
|
1302 if (ret == -1)
|
|
1303 return Qnil;
|
|
1304 return make_char (ret);
|
|
1305 }
|
|
1306
|
|
1307 dyn = Dynarr_new (Lisp_Object);
|
|
1308 memset (lbs, 0, NUM_LEADING_BYTES * sizeof (int));
|
|
1309 add_charsets_to_precedence_list (charsets, lbs, dyn);
|
|
1310 {
|
877
|
1311 Ichar ret = unicode_to_ichar (c, dyn);
|
771
|
1312 Dynarr_free (dyn);
|
|
1313 if (ret == -1)
|
|
1314 return Qnil;
|
|
1315 return make_char (ret);
|
|
1316 }
|
|
1317 #else
|
|
1318 CHECK_NATNUM (code);
|
|
1319 return Fint_to_char (code);
|
|
1320 #endif /* MULE */
|
|
1321 }
|
|
1322
|
872
|
1323 #ifdef MULE
|
|
1324
|
771
|
1325 static Lisp_Object
|
|
1326 cerrar_el_fulano (Lisp_Object fulano)
|
|
1327 {
|
|
1328 FILE *file = (FILE *) get_opaque_ptr (fulano);
|
|
1329 retry_fclose (file);
|
|
1330 return Qnil;
|
|
1331 }
|
|
1332
|
1318
|
1333 DEFUN ("load-unicode-mapping-table", Fload_unicode_mapping_table,
|
771
|
1334 2, 6, 0, /*
|
877
|
1335 Load Unicode tables with the Unicode mapping data in FILENAME for CHARSET.
|
771
|
1336 Data is text, in the form of one translation per line -- charset
|
|
1337 codepoint followed by Unicode codepoint. Numbers are decimal or hex
|
|
1338 \(preceded by 0x). Comments are marked with a #. Charset codepoints
|
877
|
1339 for two-dimensional charsets have the first octet stored in the
|
771
|
1340 high 8 bits of the hex number and the second in the low 8 bits.
|
|
1341
|
|
1342 If START and END are given, only charset codepoints within the given
|
877
|
1343 range will be processed. (START and END apply to the codepoints in the
|
|
1344 file, before OFFSET is applied.)
|
771
|
1345
|
877
|
1346 If OFFSET is given, that value will be added to all charset codepoints
|
|
1347 in the file to obtain the internal charset codepoint. \(We assume
|
|
1348 that octets in the table are in the range 33 to 126 or 32 to 127. If
|
|
1349 you have a table in ku-ten form, with octets in the range 1 to 94, you
|
|
1350 will have to use an offset of 5140, i.e. 0x2020.)
|
771
|
1351
|
|
1352 FLAGS, if specified, control further how the tables are interpreted
|
877
|
1353 and are used to special-case certain known format deviations in the
|
|
1354 Unicode tables or in the charset:
|
771
|
1355
|
|
1356 `ignore-first-column'
|
877
|
1357 The JIS X 0208 tables have 3 columns of data instead of 2. The first
|
|
1358 column contains the Shift-JIS codepoint, which we ignore.
|
771
|
1359 `big5'
|
877
|
1360 The charset codepoints are Big Five codepoints; convert it to the
|
|
1361 hacked-up Mule codepoint in `chinese-big5-1' or `chinese-big5-2'.
|
771
|
1362 */
|
|
1363 (filename, charset, start, end, offset, flags))
|
|
1364 {
|
|
1365 int st = 0, en = INT_MAX, of = 0;
|
|
1366 FILE *file;
|
|
1367 struct gcpro gcpro1;
|
|
1368 char line[1025];
|
|
1369 int fondo = specpdl_depth ();
|
|
1370 int ignore_first_column = 0;
|
|
1371 int big5 = 0;
|
|
1372
|
|
1373 CHECK_STRING (filename);
|
|
1374 charset = Fget_charset (charset);
|
|
1375 if (!NILP (start))
|
|
1376 {
|
|
1377 CHECK_INT (start);
|
|
1378 st = XINT (start);
|
|
1379 }
|
|
1380 if (!NILP (end))
|
|
1381 {
|
|
1382 CHECK_INT (end);
|
|
1383 en = XINT (end);
|
|
1384 }
|
|
1385 if (!NILP (offset))
|
|
1386 {
|
|
1387 CHECK_INT (offset);
|
|
1388 of = XINT (offset);
|
|
1389 }
|
|
1390
|
|
1391 if (!LISTP (flags))
|
|
1392 flags = list1 (flags);
|
|
1393
|
|
1394 {
|
|
1395 EXTERNAL_LIST_LOOP_2 (elt, flags)
|
|
1396 {
|
|
1397 if (EQ (elt, Qignore_first_column))
|
|
1398 ignore_first_column = 1;
|
|
1399 else if (EQ (elt, Qbig5))
|
|
1400 big5 = 1;
|
|
1401 else
|
|
1402 invalid_constant
|
1318
|
1403 ("Unrecognized `load-unicode-mapping-table' flag", elt);
|
771
|
1404 }
|
|
1405 }
|
|
1406
|
|
1407 GCPRO1 (filename);
|
|
1408 filename = Fexpand_file_name (filename, Qnil);
|
|
1409 file = qxe_fopen (XSTRING_DATA (filename), READ_TEXT);
|
|
1410 if (!file)
|
|
1411 report_file_error ("Cannot open", filename);
|
|
1412 record_unwind_protect (cerrar_el_fulano, make_opaque_ptr (file));
|
|
1413 while (fgets (line, sizeof (line), file))
|
|
1414 {
|
|
1415 char *p = line;
|
|
1416 int cp1, cp2, endcount;
|
|
1417 int cp1high, cp1low;
|
|
1418 int dummy;
|
|
1419
|
|
1420 while (*p) /* erase all comments out of the line */
|
|
1421 {
|
|
1422 if (*p == '#')
|
|
1423 *p = '\0';
|
|
1424 else
|
|
1425 p++;
|
|
1426 }
|
|
1427 /* see if line is nothing but whitespace and skip if so */
|
|
1428 p = line + strspn (line, " \t\n\r\f");
|
|
1429 if (!*p)
|
|
1430 continue;
|
|
1431 /* NOTE: It appears that MS Windows and Newlib sscanf() have
|
|
1432 different interpretations for whitespace (== "skip all whitespace
|
|
1433 at processing point"): Newlib requires at least one corresponding
|
|
1434 whitespace character in the input, but MS allows none. The
|
|
1435 following would be easier to write if we could count on the MS
|
|
1436 interpretation.
|
|
1437
|
|
1438 Also, the return value does NOT include %n storage. */
|
|
1439 if ((!ignore_first_column ?
|
|
1440 sscanf (p, "%i %i%n", &cp1, &cp2, &endcount) < 2 :
|
|
1441 sscanf (p, "%i %i %i%n", &dummy, &cp1, &cp2, &endcount) < 3)
|
2367
|
1442 /* #### Temporary code! Cygwin newlib fucked up scanf() handling
|
|
1443 of numbers beginning 0x0... starting in 04/2004, in an attempt
|
|
1444 to fix another bug. A partial fix for this was put in in
|
|
1445 06/2004, but as of 10/2004 the value of ENDCOUNT returned in
|
|
1446 such case is still wrong. If this gets fixed soon, remove
|
|
1447 this code. --ben */
|
|
1448 #ifndef CYGWIN_SCANF_BUG
|
|
1449 || *(p + endcount + strspn (p + endcount, " \t\n\r\f"))
|
|
1450 #endif
|
|
1451 )
|
771
|
1452 {
|
793
|
1453 warn_when_safe (Qunicode, Qwarning,
|
771
|
1454 "Unrecognized line in translation file %s:\n%s",
|
|
1455 XSTRING_DATA (filename), line);
|
|
1456 continue;
|
|
1457 }
|
|
1458 if (cp1 >= st && cp1 <= en)
|
|
1459 {
|
|
1460 cp1 += of;
|
|
1461 if (cp1 < 0 || cp1 >= 65536)
|
|
1462 {
|
|
1463 out_of_range:
|
793
|
1464 warn_when_safe (Qunicode, Qwarning,
|
|
1465 "Out of range first codepoint 0x%x in "
|
|
1466 "translation file %s:\n%s",
|
771
|
1467 cp1, XSTRING_DATA (filename), line);
|
|
1468 continue;
|
|
1469 }
|
|
1470
|
|
1471 cp1high = cp1 >> 8;
|
|
1472 cp1low = cp1 & 255;
|
|
1473
|
|
1474 if (big5)
|
|
1475 {
|
867
|
1476 Ichar ch = decode_big5_char (cp1high, cp1low);
|
771
|
1477 if (ch == -1)
|
793
|
1478
|
|
1479 warn_when_safe (Qunicode, Qwarning,
|
|
1480 "Out of range Big5 codepoint 0x%x in "
|
|
1481 "translation file %s:\n%s",
|
771
|
1482 cp1, XSTRING_DATA (filename), line);
|
|
1483 else
|
|
1484 set_unicode_conversion (ch, cp2);
|
|
1485 }
|
|
1486 else
|
|
1487 {
|
|
1488 int l1, h1, l2, h2;
|
867
|
1489 Ichar emch;
|
771
|
1490
|
|
1491 switch (XCHARSET_TYPE (charset))
|
|
1492 {
|
|
1493 case CHARSET_TYPE_94: l1 = 33; h1 = 126; l2 = 0; h2 = 0; break;
|
|
1494 case CHARSET_TYPE_96: l1 = 32; h1 = 127; l2 = 0; h2 = 0; break;
|
|
1495 case CHARSET_TYPE_94X94: l1 = 33; h1 = 126; l2 = 33; h2 = 126;
|
|
1496 break;
|
|
1497 case CHARSET_TYPE_96X96: l1 = 32; h1 = 127; l2 = 32; h2 = 127;
|
|
1498 break;
|
2500
|
1499 default: ABORT (); l1 = 0; h1 = 0; l2 = 0; h2 = 0;
|
771
|
1500 }
|
|
1501
|
|
1502 if (cp1high < l2 || cp1high > h2 || cp1low < l1 || cp1low > h1)
|
|
1503 goto out_of_range;
|
|
1504
|
867
|
1505 emch = (cp1high == 0 ? make_ichar (charset, cp1low, 0) :
|
|
1506 make_ichar (charset, cp1high, cp1low));
|
771
|
1507 set_unicode_conversion (emch, cp2);
|
|
1508 }
|
|
1509 }
|
|
1510 }
|
|
1511
|
|
1512 if (ferror (file))
|
|
1513 report_file_error ("IO error when reading", filename);
|
|
1514
|
|
1515 unbind_to (fondo); /* close file */
|
|
1516 UNGCPRO;
|
|
1517 return Qnil;
|
|
1518 }
|
|
1519
|
|
1520 #endif /* MULE */
|
|
1521
|
|
1522
|
|
1523 /************************************************************************/
|
|
1524 /* Unicode coding system */
|
|
1525 /************************************************************************/
|
|
1526
|
|
1527 /* ISO 10646 UTF-16, UCS-4, UTF-8, UTF-7, etc. */
|
|
1528
|
|
1529 enum unicode_type
|
|
1530 {
|
|
1531 UNICODE_UTF_16,
|
|
1532 UNICODE_UTF_8,
|
|
1533 UNICODE_UTF_7,
|
1429
|
1534 UNICODE_UCS_4
|
771
|
1535 };
|
|
1536
|
|
1537 struct unicode_coding_system
|
|
1538 {
|
|
1539 enum unicode_type type;
|
1887
|
1540 unsigned int little_endian :1;
|
|
1541 unsigned int need_bom :1;
|
771
|
1542 };
|
|
1543
|
|
1544 #define CODING_SYSTEM_UNICODE_TYPE(codesys) \
|
|
1545 (CODING_SYSTEM_TYPE_DATA (codesys, unicode)->type)
|
|
1546 #define XCODING_SYSTEM_UNICODE_TYPE(codesys) \
|
|
1547 CODING_SYSTEM_UNICODE_TYPE (XCODING_SYSTEM (codesys))
|
|
1548 #define CODING_SYSTEM_UNICODE_LITTLE_ENDIAN(codesys) \
|
|
1549 (CODING_SYSTEM_TYPE_DATA (codesys, unicode)->little_endian)
|
|
1550 #define XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN(codesys) \
|
|
1551 CODING_SYSTEM_UNICODE_LITTLE_ENDIAN (XCODING_SYSTEM (codesys))
|
|
1552 #define CODING_SYSTEM_UNICODE_NEED_BOM(codesys) \
|
|
1553 (CODING_SYSTEM_TYPE_DATA (codesys, unicode)->need_bom)
|
|
1554 #define XCODING_SYSTEM_UNICODE_NEED_BOM(codesys) \
|
|
1555 CODING_SYSTEM_UNICODE_NEED_BOM (XCODING_SYSTEM (codesys))
|
|
1556
|
|
1557 struct unicode_coding_stream
|
|
1558 {
|
|
1559 /* decode */
|
|
1560 unsigned char counter;
|
|
1561 int seen_char;
|
|
1562 /* encode */
|
|
1563 Lisp_Object current_charset;
|
|
1564 int current_char_boundary;
|
|
1565 int wrote_bom;
|
|
1566 };
|
|
1567
|
1204
|
1568 static const struct memory_description unicode_coding_system_description[] = {
|
771
|
1569 { XD_END }
|
|
1570 };
|
|
1571
|
1204
|
1572 DEFINE_CODING_SYSTEM_TYPE_WITH_DATA (unicode);
|
|
1573
|
771
|
1574 /* Decode a UCS-2 or UCS-4 character into a buffer. If the lookup fails, use
|
|
1575 <GETA MARK> (U+3013) of JIS X 0208, which means correct character
|
|
1576 is not found, instead.
|
|
1577 #### do something more appropriate (use blob?)
|
|
1578 Danger, Will Robinson! Data loss. Should we signal user? */
|
|
1579 static void
|
|
1580 decode_unicode_char (int ch, unsigned_char_dynarr *dst,
|
1887
|
1581 struct unicode_coding_stream *data,
|
|
1582 unsigned int ignore_bom)
|
771
|
1583 {
|
|
1584 if (ch == 0xFEFF && !data->seen_char && ignore_bom)
|
|
1585 ;
|
|
1586 else
|
|
1587 {
|
|
1588 #ifdef MULE
|
877
|
1589 Ichar chr = unicode_to_ichar (ch, unicode_precedence_dynarr);
|
771
|
1590
|
|
1591 if (chr != -1)
|
|
1592 {
|
867
|
1593 Ibyte work[MAX_ICHAR_LEN];
|
771
|
1594 int len;
|
|
1595
|
867
|
1596 len = set_itext_ichar (work, chr);
|
771
|
1597 Dynarr_add_many (dst, work, len);
|
|
1598 }
|
|
1599 else
|
|
1600 {
|
|
1601 Dynarr_add (dst, LEADING_BYTE_JAPANESE_JISX0208);
|
|
1602 Dynarr_add (dst, 34 + 128);
|
|
1603 Dynarr_add (dst, 46 + 128);
|
|
1604 }
|
|
1605 #else
|
867
|
1606 Dynarr_add (dst, (Ibyte) ch);
|
771
|
1607 #endif /* MULE */
|
|
1608 }
|
|
1609
|
|
1610 data->seen_char = 1;
|
|
1611 }
|
|
1612
|
|
1613 static void
|
|
1614 encode_unicode_char_1 (int code, unsigned_char_dynarr *dst,
|
1887
|
1615 enum unicode_type type, unsigned int little_endian)
|
771
|
1616 {
|
|
1617 switch (type)
|
|
1618 {
|
|
1619 case UNICODE_UTF_16:
|
|
1620 if (little_endian)
|
|
1621 {
|
|
1622 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1623 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1624 }
|
|
1625 else
|
|
1626 {
|
|
1627 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1628 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1629 }
|
|
1630 break;
|
|
1631
|
|
1632 case UNICODE_UCS_4:
|
|
1633 if (little_endian)
|
|
1634 {
|
|
1635 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1636 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1637 Dynarr_add (dst, (unsigned char) ((code >> 16) & 255));
|
|
1638 Dynarr_add (dst, (unsigned char) (code >> 24));
|
|
1639 }
|
|
1640 else
|
|
1641 {
|
|
1642 Dynarr_add (dst, (unsigned char) (code >> 24));
|
|
1643 Dynarr_add (dst, (unsigned char) ((code >> 16) & 255));
|
|
1644 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1645 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1646 }
|
|
1647 break;
|
|
1648
|
|
1649 case UNICODE_UTF_8:
|
|
1650 if (code <= 0x7f)
|
|
1651 {
|
|
1652 Dynarr_add (dst, (unsigned char) code);
|
|
1653 }
|
|
1654 else if (code <= 0x7ff)
|
|
1655 {
|
|
1656 Dynarr_add (dst, (unsigned char) ((code >> 6) | 0xc0));
|
|
1657 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1658 }
|
|
1659 else if (code <= 0xffff)
|
|
1660 {
|
|
1661 Dynarr_add (dst, (unsigned char) ((code >> 12) | 0xe0));
|
|
1662 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1663 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1664 }
|
|
1665 else if (code <= 0x1fffff)
|
|
1666 {
|
|
1667 Dynarr_add (dst, (unsigned char) ((code >> 18) | 0xf0));
|
|
1668 Dynarr_add (dst, (unsigned char) (((code >> 12) & 0x3f) | 0x80));
|
|
1669 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1670 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1671 }
|
|
1672 else if (code <= 0x3ffffff)
|
|
1673 {
|
|
1674 Dynarr_add (dst, (unsigned char) ((code >> 24) | 0xf8));
|
|
1675 Dynarr_add (dst, (unsigned char) (((code >> 18) & 0x3f) | 0x80));
|
|
1676 Dynarr_add (dst, (unsigned char) (((code >> 12) & 0x3f) | 0x80));
|
|
1677 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1678 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1679 }
|
|
1680 else
|
|
1681 {
|
|
1682 Dynarr_add (dst, (unsigned char) ((code >> 30) | 0xfc));
|
|
1683 Dynarr_add (dst, (unsigned char) (((code >> 24) & 0x3f) | 0x80));
|
|
1684 Dynarr_add (dst, (unsigned char) (((code >> 18) & 0x3f) | 0x80));
|
|
1685 Dynarr_add (dst, (unsigned char) (((code >> 12) & 0x3f) | 0x80));
|
|
1686 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1687 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1688 }
|
|
1689 break;
|
|
1690
|
2500
|
1691 case UNICODE_UTF_7: ABORT ();
|
771
|
1692
|
2500
|
1693 default: ABORT ();
|
771
|
1694 }
|
|
1695 }
|
|
1696
|
|
1697 static void
|
2333
|
1698 encode_unicode_char (Lisp_Object USED_IF_MULE (charset), int h,
|
|
1699 int USED_IF_MULE (l), unsigned_char_dynarr *dst,
|
|
1700 enum unicode_type type, unsigned int little_endian)
|
771
|
1701 {
|
|
1702 #ifdef MULE
|
867
|
1703 int code = ichar_to_unicode (make_ichar (charset, h & 127, l & 127));
|
771
|
1704
|
|
1705 if (code == -1)
|
|
1706 {
|
|
1707 if (type != UNICODE_UTF_16 &&
|
|
1708 XCHARSET_DIMENSION (charset) == 2 &&
|
|
1709 XCHARSET_CHARS (charset) == 94)
|
|
1710 {
|
|
1711 unsigned char final = XCHARSET_FINAL (charset);
|
|
1712
|
|
1713 if (('@' <= final) && (final < 0x7f))
|
|
1714 code = (0xe00000 + (final - '@') * 94 * 94
|
|
1715 + ((h & 127) - 33) * 94 + (l & 127) - 33);
|
|
1716 else
|
|
1717 code = '?';
|
|
1718 }
|
|
1719 else
|
|
1720 code = '?';
|
|
1721 }
|
|
1722 #else
|
|
1723 int code = h;
|
|
1724 #endif /* MULE */
|
|
1725
|
|
1726 encode_unicode_char_1 (code, dst, type, little_endian);
|
|
1727 }
|
|
1728
|
|
1729 static Bytecount
|
|
1730 unicode_convert (struct coding_stream *str, const UExtbyte *src,
|
|
1731 unsigned_char_dynarr *dst, Bytecount n)
|
|
1732 {
|
|
1733 unsigned int ch = str->ch;
|
|
1734 struct unicode_coding_stream *data = CODING_STREAM_TYPE_DATA (str, unicode);
|
|
1735 enum unicode_type type =
|
|
1736 XCODING_SYSTEM_UNICODE_TYPE (str->codesys);
|
1887
|
1737 unsigned int little_endian =
|
|
1738 XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (str->codesys);
|
|
1739 unsigned int ignore_bom = XCODING_SYSTEM_UNICODE_NEED_BOM (str->codesys);
|
771
|
1740 Bytecount orign = n;
|
|
1741
|
|
1742 if (str->direction == CODING_DECODE)
|
|
1743 {
|
|
1744 unsigned char counter = data->counter;
|
|
1745
|
|
1746 while (n--)
|
|
1747 {
|
|
1748 UExtbyte c = *src++;
|
|
1749
|
|
1750 switch (type)
|
|
1751 {
|
|
1752 case UNICODE_UTF_8:
|
|
1753 switch (counter)
|
|
1754 {
|
|
1755 case 0:
|
|
1756 if (c >= 0xfc)
|
|
1757 {
|
|
1758 ch = c & 0x01;
|
|
1759 counter = 5;
|
|
1760 }
|
|
1761 else if (c >= 0xf8)
|
|
1762 {
|
|
1763 ch = c & 0x03;
|
|
1764 counter = 4;
|
|
1765 }
|
|
1766 else if (c >= 0xf0)
|
|
1767 {
|
|
1768 ch = c & 0x07;
|
|
1769 counter = 3;
|
|
1770 }
|
|
1771 else if (c >= 0xe0)
|
|
1772 {
|
|
1773 ch = c & 0x0f;
|
|
1774 counter = 2;
|
|
1775 }
|
|
1776 else if (c >= 0xc0)
|
|
1777 {
|
|
1778 ch = c & 0x1f;
|
|
1779 counter = 1;
|
|
1780 }
|
|
1781 else
|
|
1782 decode_unicode_char (c, dst, data, ignore_bom);
|
|
1783 break;
|
|
1784 case 1:
|
|
1785 ch = (ch << 6) | (c & 0x3f);
|
|
1786 decode_unicode_char (ch, dst, data, ignore_bom);
|
|
1787 ch = 0;
|
|
1788 counter = 0;
|
|
1789 break;
|
|
1790 default:
|
|
1791 ch = (ch << 6) | (c & 0x3f);
|
|
1792 counter--;
|
|
1793 }
|
|
1794 break;
|
|
1795
|
|
1796 case UNICODE_UTF_16:
|
|
1797 if (little_endian)
|
|
1798 ch = (c << counter) | ch;
|
|
1799 else
|
|
1800 ch = (ch << 8) | c;
|
|
1801 counter += 8;
|
|
1802 if (counter == 16)
|
|
1803 {
|
|
1804 int tempch = ch;
|
|
1805 ch = 0;
|
|
1806 counter = 0;
|
|
1807 decode_unicode_char (tempch, dst, data, ignore_bom);
|
|
1808 }
|
|
1809 break;
|
|
1810
|
|
1811 case UNICODE_UCS_4:
|
|
1812 if (little_endian)
|
|
1813 ch = (c << counter) | ch;
|
|
1814 else
|
|
1815 ch = (ch << 8) | c;
|
|
1816 counter += 8;
|
|
1817 if (counter == 32)
|
|
1818 {
|
|
1819 int tempch = ch;
|
|
1820 ch = 0;
|
|
1821 counter = 0;
|
|
1822 if (tempch < 0)
|
|
1823 {
|
|
1824 /* !!#### indicate an error */
|
|
1825 tempch = '~';
|
|
1826 }
|
|
1827 decode_unicode_char (tempch, dst, data, ignore_bom);
|
|
1828 }
|
|
1829 break;
|
|
1830
|
|
1831 case UNICODE_UTF_7:
|
2500
|
1832 ABORT ();
|
771
|
1833 break;
|
|
1834
|
2500
|
1835 default: ABORT ();
|
771
|
1836 }
|
|
1837
|
|
1838 }
|
|
1839 if (str->eof)
|
|
1840 DECODE_OUTPUT_PARTIAL_CHAR (ch, dst);
|
|
1841
|
|
1842 data->counter = counter;
|
|
1843 }
|
|
1844 else
|
|
1845 {
|
|
1846 unsigned char char_boundary = data->current_char_boundary;
|
|
1847 Lisp_Object charset = data->current_charset;
|
|
1848
|
|
1849 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1850 /* flags for handling composite chars. We do a little switcheroo
|
|
1851 on the source while we're outputting the composite char. */
|
|
1852 Bytecount saved_n = 0;
|
867
|
1853 const Ibyte *saved_src = NULL;
|
771
|
1854 int in_composite = 0;
|
|
1855
|
|
1856 back_to_square_n:
|
|
1857 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
1858
|
|
1859 if (XCODING_SYSTEM_UNICODE_NEED_BOM (str->codesys) && !data->wrote_bom)
|
|
1860 {
|
|
1861 encode_unicode_char_1 (0xFEFF, dst, type, little_endian);
|
|
1862 data->wrote_bom = 1;
|
|
1863 }
|
|
1864
|
|
1865 while (n--)
|
|
1866 {
|
867
|
1867 Ibyte c = *src++;
|
771
|
1868
|
|
1869 #ifdef MULE
|
826
|
1870 if (byte_ascii_p (c))
|
771
|
1871 #endif /* MULE */
|
|
1872 { /* Processing ASCII character */
|
|
1873 ch = 0;
|
|
1874 encode_unicode_char (Vcharset_ascii, c, 0, dst, type,
|
|
1875 little_endian);
|
|
1876
|
|
1877 char_boundary = 1;
|
|
1878 }
|
|
1879 #ifdef MULE
|
867
|
1880 else if (ibyte_leading_byte_p (c) || ibyte_leading_byte_p (ch))
|
771
|
1881 { /* Processing Leading Byte */
|
|
1882 ch = 0;
|
826
|
1883 charset = charset_by_leading_byte (c);
|
|
1884 if (leading_byte_prefix_p(c))
|
771
|
1885 ch = c;
|
|
1886 char_boundary = 0;
|
|
1887 }
|
|
1888 else
|
|
1889 { /* Processing Non-ASCII character */
|
|
1890 char_boundary = 1;
|
|
1891 if (EQ (charset, Vcharset_control_1))
|
|
1892 encode_unicode_char (Vcharset_control_1, c, 0, dst,
|
|
1893 type, little_endian);
|
|
1894 else
|
|
1895 {
|
|
1896 switch (XCHARSET_REP_BYTES (charset))
|
|
1897 {
|
|
1898 case 2:
|
|
1899 encode_unicode_char (charset, c, 0, dst, type,
|
|
1900 little_endian);
|
|
1901 break;
|
|
1902 case 3:
|
|
1903 if (XCHARSET_PRIVATE_P (charset))
|
|
1904 {
|
|
1905 encode_unicode_char (charset, c, 0, dst, type,
|
|
1906 little_endian);
|
|
1907 ch = 0;
|
|
1908 }
|
|
1909 else if (ch)
|
|
1910 {
|
|
1911 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1912 if (EQ (charset, Vcharset_composite))
|
|
1913 {
|
|
1914 if (in_composite)
|
|
1915 {
|
|
1916 /* #### Bother! We don't know how to
|
|
1917 handle this yet. */
|
|
1918 encode_unicode_char (Vcharset_ascii, '~', 0,
|
|
1919 dst, type,
|
|
1920 little_endian);
|
|
1921 }
|
|
1922 else
|
|
1923 {
|
867
|
1924 Ichar emch = make_ichar (Vcharset_composite,
|
771
|
1925 ch & 0x7F,
|
|
1926 c & 0x7F);
|
|
1927 Lisp_Object lstr =
|
|
1928 composite_char_string (emch);
|
|
1929 saved_n = n;
|
|
1930 saved_src = src;
|
|
1931 in_composite = 1;
|
|
1932 src = XSTRING_DATA (lstr);
|
|
1933 n = XSTRING_LENGTH (lstr);
|
|
1934 }
|
|
1935 }
|
|
1936 else
|
|
1937 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
1938 encode_unicode_char (charset, ch, c, dst, type,
|
|
1939 little_endian);
|
|
1940 ch = 0;
|
|
1941 }
|
|
1942 else
|
|
1943 {
|
|
1944 ch = c;
|
|
1945 char_boundary = 0;
|
|
1946 }
|
|
1947 break;
|
|
1948 case 4:
|
|
1949 if (ch)
|
|
1950 {
|
|
1951 encode_unicode_char (charset, ch, c, dst, type,
|
|
1952 little_endian);
|
|
1953 ch = 0;
|
|
1954 }
|
|
1955 else
|
|
1956 {
|
|
1957 ch = c;
|
|
1958 char_boundary = 0;
|
|
1959 }
|
|
1960 break;
|
|
1961 default:
|
2500
|
1962 ABORT ();
|
771
|
1963 }
|
|
1964 }
|
|
1965 }
|
|
1966 #endif /* MULE */
|
|
1967 }
|
|
1968
|
|
1969 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1970 if (in_composite)
|
|
1971 {
|
|
1972 n = saved_n;
|
|
1973 src = saved_src;
|
|
1974 in_composite = 0;
|
|
1975 goto back_to_square_n; /* Wheeeeeeeee ..... */
|
|
1976 }
|
|
1977 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
1978
|
|
1979 data->current_char_boundary = char_boundary;
|
|
1980 data->current_charset = charset;
|
|
1981
|
|
1982 /* La palabra se hizo carne! */
|
|
1983 /* A palavra fez-se carne! */
|
|
1984 /* Whatever. */
|
|
1985 }
|
|
1986
|
|
1987 str->ch = ch;
|
|
1988 return orign;
|
|
1989 }
|
|
1990
|
|
1991 /* DEFINE_DETECTOR (utf_7); */
|
|
1992 DEFINE_DETECTOR (utf_8);
|
|
1993 DEFINE_DETECTOR_CATEGORY (utf_8, utf_8);
|
985
|
1994 DEFINE_DETECTOR_CATEGORY (utf_8, utf_8_bom);
|
771
|
1995 DEFINE_DETECTOR (ucs_4);
|
|
1996 DEFINE_DETECTOR_CATEGORY (ucs_4, ucs_4);
|
|
1997 DEFINE_DETECTOR (utf_16);
|
|
1998 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16);
|
|
1999 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian);
|
|
2000 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16_bom);
|
|
2001 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian_bom);
|
|
2002
|
|
2003 struct ucs_4_detector
|
|
2004 {
|
|
2005 int in_ucs_4_byte;
|
|
2006 };
|
|
2007
|
|
2008 static void
|
|
2009 ucs_4_detect (struct detection_state *st, const UExtbyte *src,
|
|
2010 Bytecount n)
|
|
2011 {
|
|
2012 struct ucs_4_detector *data = DETECTION_STATE_DATA (st, ucs_4);
|
|
2013
|
|
2014 while (n--)
|
|
2015 {
|
|
2016 UExtbyte c = *src++;
|
|
2017 switch (data->in_ucs_4_byte)
|
|
2018 {
|
|
2019 case 0:
|
|
2020 if (c >= 128)
|
|
2021 {
|
|
2022 DET_RESULT (st, ucs_4) = DET_NEARLY_IMPOSSIBLE;
|
|
2023 return;
|
|
2024 }
|
|
2025 else
|
|
2026 data->in_ucs_4_byte++;
|
|
2027 break;
|
|
2028 case 3:
|
|
2029 data->in_ucs_4_byte = 0;
|
|
2030 break;
|
|
2031 default:
|
|
2032 data->in_ucs_4_byte++;
|
|
2033 }
|
|
2034 }
|
|
2035
|
|
2036 /* !!#### write this for real */
|
|
2037 DET_RESULT (st, ucs_4) = DET_AS_LIKELY_AS_UNLIKELY;
|
|
2038 }
|
|
2039
|
|
2040 struct utf_16_detector
|
|
2041 {
|
|
2042 unsigned int seen_ffff:1;
|
|
2043 unsigned int seen_forward_bom:1;
|
|
2044 unsigned int seen_rev_bom:1;
|
|
2045 int byteno;
|
|
2046 int prev_char;
|
|
2047 int text, rev_text;
|
1267
|
2048 int sep, rev_sep;
|
|
2049 int num_ascii;
|
771
|
2050 };
|
|
2051
|
|
2052 static void
|
|
2053 utf_16_detect (struct detection_state *st, const UExtbyte *src,
|
|
2054 Bytecount n)
|
|
2055 {
|
|
2056 struct utf_16_detector *data = DETECTION_STATE_DATA (st, utf_16);
|
|
2057
|
|
2058 while (n--)
|
|
2059 {
|
|
2060 UExtbyte c = *src++;
|
|
2061 int prevc = data->prev_char;
|
|
2062 if (data->byteno == 1 && c == 0xFF && prevc == 0xFE)
|
|
2063 data->seen_forward_bom = 1;
|
|
2064 else if (data->byteno == 1 && c == 0xFE && prevc == 0xFF)
|
|
2065 data->seen_rev_bom = 1;
|
|
2066
|
|
2067 if (data->byteno & 1)
|
|
2068 {
|
|
2069 if (c == 0xFF && prevc == 0xFF)
|
|
2070 data->seen_ffff = 1;
|
|
2071 if (prevc == 0
|
|
2072 && (c == '\r' || c == '\n'
|
|
2073 || (c >= 0x20 && c <= 0x7E)))
|
|
2074 data->text++;
|
|
2075 if (c == 0
|
|
2076 && (prevc == '\r' || prevc == '\n'
|
|
2077 || (prevc >= 0x20 && prevc <= 0x7E)))
|
|
2078 data->rev_text++;
|
1267
|
2079 /* #### 0x2028 is LINE SEPARATOR and 0x2029 is PARAGRAPH SEPARATOR.
|
|
2080 I used to count these in text and rev_text but that is very bad,
|
|
2081 as 0x2028 is also space + left-paren in ASCII, which is extremely
|
|
2082 common. So, what do we do with these? */
|
771
|
2083 if (prevc == 0x20 && (c == 0x28 || c == 0x29))
|
1267
|
2084 data->sep++;
|
771
|
2085 if (c == 0x20 && (prevc == 0x28 || prevc == 0x29))
|
1267
|
2086 data->rev_sep++;
|
771
|
2087 }
|
|
2088
|
1267
|
2089 if ((c >= ' ' && c <= '~') || c == '\n' || c == '\r' || c == '\t' ||
|
|
2090 c == '\f' || c == '\v')
|
|
2091 data->num_ascii++;
|
771
|
2092 data->byteno++;
|
|
2093 data->prev_char = c;
|
|
2094 }
|
|
2095
|
|
2096 {
|
|
2097 int variance_indicates_big_endian =
|
|
2098 (data->text >= 10
|
|
2099 && (data->rev_text == 0
|
|
2100 || data->text / data->rev_text >= 10));
|
|
2101 int variance_indicates_little_endian =
|
|
2102 (data->rev_text >= 10
|
|
2103 && (data->text == 0
|
|
2104 || data->rev_text / data->text >= 10));
|
|
2105
|
|
2106 if (data->seen_ffff)
|
|
2107 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2108 else if (data->seen_forward_bom)
|
|
2109 {
|
|
2110 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2111 if (variance_indicates_big_endian)
|
|
2112 DET_RESULT (st, utf_16_bom) = DET_NEAR_CERTAINTY;
|
|
2113 else if (variance_indicates_little_endian)
|
|
2114 DET_RESULT (st, utf_16_bom) = DET_SOMEWHAT_LIKELY;
|
|
2115 else
|
|
2116 DET_RESULT (st, utf_16_bom) = DET_QUITE_PROBABLE;
|
|
2117 }
|
|
2118 else if (data->seen_forward_bom)
|
|
2119 {
|
|
2120 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2121 if (variance_indicates_big_endian)
|
|
2122 DET_RESULT (st, utf_16_bom) = DET_NEAR_CERTAINTY;
|
|
2123 else if (variance_indicates_little_endian)
|
|
2124 /* #### may need to rethink */
|
|
2125 DET_RESULT (st, utf_16_bom) = DET_SOMEWHAT_LIKELY;
|
|
2126 else
|
|
2127 /* #### may need to rethink */
|
|
2128 DET_RESULT (st, utf_16_bom) = DET_QUITE_PROBABLE;
|
|
2129 }
|
|
2130 else if (data->seen_rev_bom)
|
|
2131 {
|
|
2132 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2133 if (variance_indicates_little_endian)
|
|
2134 DET_RESULT (st, utf_16_little_endian_bom) = DET_NEAR_CERTAINTY;
|
|
2135 else if (variance_indicates_big_endian)
|
|
2136 /* #### may need to rethink */
|
|
2137 DET_RESULT (st, utf_16_little_endian_bom) = DET_SOMEWHAT_LIKELY;
|
|
2138 else
|
|
2139 /* #### may need to rethink */
|
|
2140 DET_RESULT (st, utf_16_little_endian_bom) = DET_QUITE_PROBABLE;
|
|
2141 }
|
|
2142 else if (variance_indicates_big_endian)
|
|
2143 {
|
|
2144 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2145 DET_RESULT (st, utf_16) = DET_SOMEWHAT_LIKELY;
|
|
2146 DET_RESULT (st, utf_16_little_endian) = DET_SOMEWHAT_UNLIKELY;
|
|
2147 }
|
|
2148 else if (variance_indicates_little_endian)
|
|
2149 {
|
|
2150 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2151 DET_RESULT (st, utf_16) = DET_SOMEWHAT_UNLIKELY;
|
|
2152 DET_RESULT (st, utf_16_little_endian) = DET_SOMEWHAT_LIKELY;
|
|
2153 }
|
|
2154 else
|
1267
|
2155 {
|
|
2156 /* #### FUCKME! There should really be an ASCII detector. This
|
|
2157 would rule out the need to have this built-in here as
|
|
2158 well. --ben */
|
1292
|
2159 int pct_ascii = data->byteno ? (100 * data->num_ascii) / data->byteno
|
|
2160 : 100;
|
1267
|
2161
|
|
2162 if (pct_ascii > 90)
|
|
2163 SET_DET_RESULTS (st, utf_16, DET_QUITE_IMPROBABLE);
|
|
2164 else if (pct_ascii > 75)
|
|
2165 SET_DET_RESULTS (st, utf_16, DET_SOMEWHAT_UNLIKELY);
|
|
2166 else
|
|
2167 SET_DET_RESULTS (st, utf_16, DET_AS_LIKELY_AS_UNLIKELY);
|
|
2168 }
|
771
|
2169 }
|
|
2170 }
|
|
2171
|
|
2172 struct utf_8_detector
|
|
2173 {
|
985
|
2174 int byteno;
|
|
2175 int first_byte;
|
|
2176 int second_byte;
|
1267
|
2177 int prev_byte;
|
771
|
2178 int in_utf_8_byte;
|
1267
|
2179 int recent_utf_8_sequence;
|
|
2180 int seen_bogus_utf8;
|
|
2181 int seen_really_bogus_utf8;
|
|
2182 int seen_2byte_sequence;
|
|
2183 int seen_longer_sequence;
|
|
2184 int seen_iso2022_esc;
|
|
2185 int seen_iso_shift;
|
1887
|
2186 unsigned int seen_utf_bom:1;
|
771
|
2187 };
|
|
2188
|
|
2189 static void
|
|
2190 utf_8_detect (struct detection_state *st, const UExtbyte *src,
|
|
2191 Bytecount n)
|
|
2192 {
|
|
2193 struct utf_8_detector *data = DETECTION_STATE_DATA (st, utf_8);
|
|
2194
|
|
2195 while (n--)
|
|
2196 {
|
|
2197 UExtbyte c = *src++;
|
985
|
2198 switch (data->byteno)
|
|
2199 {
|
|
2200 case 0:
|
|
2201 data->first_byte = c;
|
|
2202 break;
|
|
2203 case 1:
|
|
2204 data->second_byte = c;
|
|
2205 break;
|
|
2206 case 2:
|
|
2207 if (data->first_byte == 0xef &&
|
|
2208 data->second_byte == 0xbb &&
|
|
2209 c == 0xbf)
|
1267
|
2210 data->seen_utf_bom = 1;
|
985
|
2211 break;
|
|
2212 }
|
|
2213
|
771
|
2214 switch (data->in_utf_8_byte)
|
|
2215 {
|
|
2216 case 0:
|
1267
|
2217 if (data->prev_byte == ISO_CODE_ESC && c >= 0x28 && c <= 0x2F)
|
|
2218 data->seen_iso2022_esc++;
|
|
2219 else if (c == ISO_CODE_SI || c == ISO_CODE_SO)
|
|
2220 data->seen_iso_shift++;
|
771
|
2221 else if (c >= 0xfc)
|
|
2222 data->in_utf_8_byte = 5;
|
|
2223 else if (c >= 0xf8)
|
|
2224 data->in_utf_8_byte = 4;
|
|
2225 else if (c >= 0xf0)
|
|
2226 data->in_utf_8_byte = 3;
|
|
2227 else if (c >= 0xe0)
|
|
2228 data->in_utf_8_byte = 2;
|
|
2229 else if (c >= 0xc0)
|
|
2230 data->in_utf_8_byte = 1;
|
|
2231 else if (c >= 0x80)
|
1267
|
2232 data->seen_bogus_utf8++;
|
|
2233 if (data->in_utf_8_byte > 0)
|
|
2234 data->recent_utf_8_sequence = data->in_utf_8_byte;
|
771
|
2235 break;
|
|
2236 default:
|
|
2237 if ((c & 0xc0) != 0x80)
|
1267
|
2238 data->seen_really_bogus_utf8++;
|
|
2239 else
|
771
|
2240 {
|
1267
|
2241 data->in_utf_8_byte--;
|
|
2242 if (data->in_utf_8_byte == 0)
|
|
2243 {
|
|
2244 if (data->recent_utf_8_sequence == 1)
|
|
2245 data->seen_2byte_sequence++;
|
|
2246 else
|
|
2247 {
|
|
2248 assert (data->recent_utf_8_sequence >= 2);
|
|
2249 data->seen_longer_sequence++;
|
|
2250 }
|
|
2251 }
|
771
|
2252 }
|
|
2253 }
|
985
|
2254
|
|
2255 data->byteno++;
|
1267
|
2256 data->prev_byte = c;
|
771
|
2257 }
|
1267
|
2258
|
|
2259 /* either BOM or no BOM, but not both */
|
|
2260 SET_DET_RESULTS (st, utf_8, DET_NEARLY_IMPOSSIBLE);
|
|
2261
|
|
2262
|
|
2263 if (data->seen_utf_bom)
|
|
2264 DET_RESULT (st, utf_8_bom) = DET_NEAR_CERTAINTY;
|
|
2265 else
|
|
2266 {
|
|
2267 if (data->seen_really_bogus_utf8 ||
|
|
2268 data->seen_bogus_utf8 >= 2)
|
|
2269 ; /* bogus */
|
|
2270 else if (data->seen_bogus_utf8)
|
|
2271 DET_RESULT (st, utf_8) = DET_SOMEWHAT_UNLIKELY;
|
|
2272 else if ((data->seen_longer_sequence >= 5 ||
|
|
2273 data->seen_2byte_sequence >= 10) &&
|
|
2274 (!(data->seen_iso2022_esc + data->seen_iso_shift) ||
|
|
2275 (data->seen_longer_sequence * 2 + data->seen_2byte_sequence) /
|
|
2276 (data->seen_iso2022_esc + data->seen_iso_shift) >= 10))
|
|
2277 /* heuristics, heuristics, we love heuristics */
|
|
2278 DET_RESULT (st, utf_8) = DET_QUITE_PROBABLE;
|
|
2279 else if (data->seen_iso2022_esc ||
|
|
2280 data->seen_iso_shift >= 3)
|
|
2281 DET_RESULT (st, utf_8) = DET_SOMEWHAT_UNLIKELY;
|
|
2282 else if (data->seen_longer_sequence ||
|
|
2283 data->seen_2byte_sequence)
|
|
2284 DET_RESULT (st, utf_8) = DET_SOMEWHAT_LIKELY;
|
|
2285 else if (data->seen_iso_shift)
|
|
2286 DET_RESULT (st, utf_8) = DET_SOMEWHAT_UNLIKELY;
|
|
2287 else
|
|
2288 DET_RESULT (st, utf_8) = DET_AS_LIKELY_AS_UNLIKELY;
|
|
2289 }
|
771
|
2290 }
|
|
2291
|
|
2292 static void
|
|
2293 unicode_init_coding_stream (struct coding_stream *str)
|
|
2294 {
|
|
2295 struct unicode_coding_stream *data =
|
|
2296 CODING_STREAM_TYPE_DATA (str, unicode);
|
|
2297 xzero (*data);
|
|
2298 data->current_charset = Qnil;
|
|
2299 }
|
|
2300
|
|
2301 static void
|
|
2302 unicode_rewind_coding_stream (struct coding_stream *str)
|
|
2303 {
|
|
2304 unicode_init_coding_stream (str);
|
|
2305 }
|
|
2306
|
|
2307 static int
|
|
2308 unicode_putprop (Lisp_Object codesys, Lisp_Object key, Lisp_Object value)
|
|
2309 {
|
|
2310 if (EQ (key, Qtype))
|
|
2311 {
|
|
2312 enum unicode_type type;
|
|
2313
|
|
2314 if (EQ (value, Qutf_8))
|
|
2315 type = UNICODE_UTF_8;
|
|
2316 else if (EQ (value, Qutf_16))
|
|
2317 type = UNICODE_UTF_16;
|
|
2318 else if (EQ (value, Qutf_7))
|
|
2319 type = UNICODE_UTF_7;
|
|
2320 else if (EQ (value, Qucs_4))
|
|
2321 type = UNICODE_UCS_4;
|
|
2322 else
|
|
2323 invalid_constant ("Invalid Unicode type", key);
|
|
2324
|
|
2325 XCODING_SYSTEM_UNICODE_TYPE (codesys) = type;
|
|
2326 }
|
|
2327 else if (EQ (key, Qlittle_endian))
|
|
2328 XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (codesys) = !NILP (value);
|
|
2329 else if (EQ (key, Qneed_bom))
|
|
2330 XCODING_SYSTEM_UNICODE_NEED_BOM (codesys) = !NILP (value);
|
|
2331 else
|
|
2332 return 0;
|
|
2333 return 1;
|
|
2334 }
|
|
2335
|
|
2336 static Lisp_Object
|
|
2337 unicode_getprop (Lisp_Object coding_system, Lisp_Object prop)
|
|
2338 {
|
|
2339 if (EQ (prop, Qtype))
|
|
2340 {
|
|
2341 switch (XCODING_SYSTEM_UNICODE_TYPE (coding_system))
|
|
2342 {
|
|
2343 case UNICODE_UTF_16: return Qutf_16;
|
|
2344 case UNICODE_UTF_8: return Qutf_8;
|
|
2345 case UNICODE_UTF_7: return Qutf_7;
|
|
2346 case UNICODE_UCS_4: return Qucs_4;
|
2500
|
2347 default: ABORT ();
|
771
|
2348 }
|
|
2349 }
|
|
2350 else if (EQ (prop, Qlittle_endian))
|
|
2351 return XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (coding_system) ? Qt : Qnil;
|
|
2352 else if (EQ (prop, Qneed_bom))
|
|
2353 return XCODING_SYSTEM_UNICODE_NEED_BOM (coding_system) ? Qt : Qnil;
|
|
2354 return Qunbound;
|
|
2355 }
|
|
2356
|
|
2357 static void
|
2286
|
2358 unicode_print (Lisp_Object cs, Lisp_Object printcharfun,
|
|
2359 int UNUSED (escapeflag))
|
771
|
2360 {
|
800
|
2361 write_fmt_string_lisp (printcharfun, "(%s", 1, unicode_getprop (cs, Qtype));
|
771
|
2362 if (XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (cs))
|
826
|
2363 write_c_string (printcharfun, ", little-endian");
|
771
|
2364 if (XCODING_SYSTEM_UNICODE_NEED_BOM (cs))
|
826
|
2365 write_c_string (printcharfun, ", need-bom");
|
|
2366 write_c_string (printcharfun, ")");
|
771
|
2367 }
|
|
2368
|
|
2369 int
|
2286
|
2370 dfc_coding_system_is_unicode (
|
|
2371 #ifdef WIN32_ANY
|
|
2372 Lisp_Object codesys
|
|
2373 #else
|
|
2374 Lisp_Object UNUSED (codesys)
|
|
2375 #endif
|
|
2376 )
|
771
|
2377 {
|
1315
|
2378 #ifdef WIN32_ANY
|
771
|
2379 codesys = Fget_coding_system (codesys);
|
|
2380 return (EQ (XCODING_SYSTEM_TYPE (codesys), Qunicode) &&
|
|
2381 XCODING_SYSTEM_UNICODE_TYPE (codesys) == UNICODE_UTF_16 &&
|
|
2382 XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (codesys));
|
|
2383
|
|
2384 #else
|
|
2385 return 0;
|
|
2386 #endif
|
|
2387 }
|
|
2388
|
|
2389
|
|
2390 /************************************************************************/
|
|
2391 /* Initialization */
|
|
2392 /************************************************************************/
|
|
2393
|
|
2394 void
|
|
2395 syms_of_unicode (void)
|
|
2396 {
|
|
2397 #ifdef MULE
|
877
|
2398 DEFSUBR (Funicode_precedence_list);
|
771
|
2399 DEFSUBR (Fset_language_unicode_precedence_list);
|
|
2400 DEFSUBR (Flanguage_unicode_precedence_list);
|
|
2401 DEFSUBR (Fset_default_unicode_precedence_list);
|
|
2402 DEFSUBR (Fdefault_unicode_precedence_list);
|
|
2403 DEFSUBR (Fset_unicode_conversion);
|
|
2404
|
1318
|
2405 DEFSUBR (Fload_unicode_mapping_table);
|
771
|
2406
|
|
2407 DEFSYMBOL (Qignore_first_column);
|
|
2408 #endif /* MULE */
|
|
2409
|
800
|
2410 DEFSUBR (Fchar_to_unicode);
|
|
2411 DEFSUBR (Funicode_to_char);
|
771
|
2412
|
|
2413 DEFSYMBOL (Qunicode);
|
|
2414 DEFSYMBOL (Qucs_4);
|
|
2415 DEFSYMBOL (Qutf_16);
|
|
2416 DEFSYMBOL (Qutf_8);
|
|
2417 DEFSYMBOL (Qutf_7);
|
|
2418
|
|
2419 DEFSYMBOL (Qneed_bom);
|
|
2420
|
|
2421 DEFSYMBOL (Qutf_16);
|
|
2422 DEFSYMBOL (Qutf_16_little_endian);
|
|
2423 DEFSYMBOL (Qutf_16_bom);
|
|
2424 DEFSYMBOL (Qutf_16_little_endian_bom);
|
985
|
2425
|
|
2426 DEFSYMBOL (Qutf_8);
|
|
2427 DEFSYMBOL (Qutf_8_bom);
|
771
|
2428 }
|
|
2429
|
|
2430 void
|
|
2431 coding_system_type_create_unicode (void)
|
|
2432 {
|
|
2433 INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA (unicode, "unicode-coding-system-p");
|
|
2434 CODING_SYSTEM_HAS_METHOD (unicode, print);
|
|
2435 CODING_SYSTEM_HAS_METHOD (unicode, convert);
|
|
2436 CODING_SYSTEM_HAS_METHOD (unicode, init_coding_stream);
|
|
2437 CODING_SYSTEM_HAS_METHOD (unicode, rewind_coding_stream);
|
|
2438 CODING_SYSTEM_HAS_METHOD (unicode, putprop);
|
|
2439 CODING_SYSTEM_HAS_METHOD (unicode, getprop);
|
|
2440
|
|
2441 INITIALIZE_DETECTOR (utf_8);
|
|
2442 DETECTOR_HAS_METHOD (utf_8, detect);
|
|
2443 INITIALIZE_DETECTOR_CATEGORY (utf_8, utf_8);
|
985
|
2444 INITIALIZE_DETECTOR_CATEGORY (utf_8, utf_8_bom);
|
771
|
2445
|
|
2446 INITIALIZE_DETECTOR (ucs_4);
|
|
2447 DETECTOR_HAS_METHOD (ucs_4, detect);
|
|
2448 INITIALIZE_DETECTOR_CATEGORY (ucs_4, ucs_4);
|
|
2449
|
|
2450 INITIALIZE_DETECTOR (utf_16);
|
|
2451 DETECTOR_HAS_METHOD (utf_16, detect);
|
|
2452 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16);
|
|
2453 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian);
|
|
2454 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16_bom);
|
|
2455 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian_bom);
|
|
2456 }
|
|
2457
|
|
2458 void
|
|
2459 reinit_coding_system_type_create_unicode (void)
|
|
2460 {
|
|
2461 REINITIALIZE_CODING_SYSTEM_TYPE (unicode);
|
|
2462 }
|
|
2463
|
|
2464 void
|
|
2465 vars_of_unicode (void)
|
|
2466 {
|
|
2467 Fprovide (intern ("unicode"));
|
|
2468
|
|
2469 #ifdef MULE
|
|
2470 staticpro (&Vlanguage_unicode_precedence_list);
|
|
2471 Vlanguage_unicode_precedence_list = Qnil;
|
|
2472
|
|
2473 staticpro (&Vdefault_unicode_precedence_list);
|
|
2474 Vdefault_unicode_precedence_list = Qnil;
|
|
2475
|
|
2476 unicode_precedence_dynarr = Dynarr_new (Lisp_Object);
|
2367
|
2477 dump_add_root_block_ptr (&unicode_precedence_dynarr,
|
771
|
2478 &lisp_object_dynarr_description);
|
2367
|
2479
|
|
2480 init_blank_unicode_tables ();
|
|
2481
|
|
2482 /* Note that the "block" we are describing is a single pointer, and hence
|
|
2483 we could potentially use dump_add_root_block_ptr(). However, given
|
|
2484 the way the descriptions are written, we couldn't use them, and would
|
|
2485 have to write new descriptions for each of the pointers below, since
|
|
2486 we would have to make use of a description with an XD_BLOCK_ARRAY
|
|
2487 in it. */
|
|
2488
|
|
2489 dump_add_root_block (&to_unicode_blank_1, sizeof (void *),
|
|
2490 to_unicode_level_1_desc_1);
|
|
2491 dump_add_root_block (&to_unicode_blank_2, sizeof (void *),
|
|
2492 to_unicode_level_2_desc_1);
|
|
2493
|
|
2494 dump_add_root_block (&from_unicode_blank_1, sizeof (void *),
|
|
2495 from_unicode_level_1_desc_1);
|
|
2496 dump_add_root_block (&from_unicode_blank_2, sizeof (void *),
|
|
2497 from_unicode_level_2_desc_1);
|
|
2498 dump_add_root_block (&from_unicode_blank_3, sizeof (void *),
|
|
2499 from_unicode_level_3_desc_1);
|
|
2500 dump_add_root_block (&from_unicode_blank_4, sizeof (void *),
|
|
2501 from_unicode_level_4_desc_1);
|
771
|
2502 #endif /* MULE */
|
|
2503 }
|