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.
|
2622
|
1284
|
|
1285 Note that the current XEmacs internal encoding has no mapping for many
|
|
1286 Unicode code points, and if you use characters that are vaguely obscure with
|
|
1287 XEmacs' Unicode coding systems, you will lose data.
|
|
1288
|
|
1289 To add support for some desired code point in the short term--note that our
|
|
1290 intention is to move to a Unicode-compatible internal encoding soon, for
|
|
1291 some value of soon--if you are a distributor, add something like the
|
|
1292 following to `site-start.el.'
|
|
1293
|
|
1294 (make-charset 'distro-name-private
|
|
1295 "Private character set for DISTRO"
|
|
1296 '(dimension 1
|
|
1297 chars 96
|
|
1298 columns 1
|
|
1299 final ?5 ;; Change this--see docs for make-charset
|
|
1300 long-name "Private charset for some Unicode char support."
|
|
1301 short-name "Distro-Private"))
|
|
1302
|
|
1303 (set-unicode-conversion
|
|
1304 (make-char 'distro-name-private #x20) #x263A) ;; WHITE SMILING FACE
|
|
1305
|
|
1306 (set-unicode-conversion
|
|
1307 (make-char 'distro-name-private #x21) #x3030) ;; WAVY DASH
|
|
1308
|
|
1309 ;; ...
|
|
1310 ;;; Repeat as necessary.
|
|
1311
|
|
1312 Redisplay will work on the sjt-xft branch, but not with server-side X11
|
|
1313 fonts as is the default. However, data read in will be preserved when they
|
|
1314 are written out again.
|
|
1315
|
771
|
1316 */
|
2333
|
1317 (code, USED_IF_MULE (charsets)))
|
771
|
1318 {
|
|
1319 #ifdef MULE
|
|
1320 Lisp_Object_dynarr *dyn;
|
|
1321 int lbs[NUM_LEADING_BYTES];
|
|
1322 int c;
|
|
1323
|
|
1324 CHECK_NATNUM (code);
|
|
1325 c = XINT (code);
|
|
1326 {
|
|
1327 EXTERNAL_LIST_LOOP_2 (elt, charsets)
|
|
1328 Fget_charset (elt);
|
|
1329 }
|
|
1330
|
|
1331 if (NILP (charsets))
|
|
1332 {
|
877
|
1333 Ichar ret = unicode_to_ichar (c, unicode_precedence_dynarr);
|
771
|
1334 if (ret == -1)
|
|
1335 return Qnil;
|
|
1336 return make_char (ret);
|
|
1337 }
|
|
1338
|
|
1339 dyn = Dynarr_new (Lisp_Object);
|
|
1340 memset (lbs, 0, NUM_LEADING_BYTES * sizeof (int));
|
|
1341 add_charsets_to_precedence_list (charsets, lbs, dyn);
|
|
1342 {
|
877
|
1343 Ichar ret = unicode_to_ichar (c, dyn);
|
771
|
1344 Dynarr_free (dyn);
|
|
1345 if (ret == -1)
|
|
1346 return Qnil;
|
|
1347 return make_char (ret);
|
|
1348 }
|
|
1349 #else
|
|
1350 CHECK_NATNUM (code);
|
|
1351 return Fint_to_char (code);
|
|
1352 #endif /* MULE */
|
|
1353 }
|
|
1354
|
872
|
1355 #ifdef MULE
|
|
1356
|
771
|
1357 static Lisp_Object
|
|
1358 cerrar_el_fulano (Lisp_Object fulano)
|
|
1359 {
|
|
1360 FILE *file = (FILE *) get_opaque_ptr (fulano);
|
|
1361 retry_fclose (file);
|
|
1362 return Qnil;
|
|
1363 }
|
|
1364
|
1318
|
1365 DEFUN ("load-unicode-mapping-table", Fload_unicode_mapping_table,
|
771
|
1366 2, 6, 0, /*
|
877
|
1367 Load Unicode tables with the Unicode mapping data in FILENAME for CHARSET.
|
771
|
1368 Data is text, in the form of one translation per line -- charset
|
|
1369 codepoint followed by Unicode codepoint. Numbers are decimal or hex
|
|
1370 \(preceded by 0x). Comments are marked with a #. Charset codepoints
|
877
|
1371 for two-dimensional charsets have the first octet stored in the
|
771
|
1372 high 8 bits of the hex number and the second in the low 8 bits.
|
|
1373
|
|
1374 If START and END are given, only charset codepoints within the given
|
877
|
1375 range will be processed. (START and END apply to the codepoints in the
|
|
1376 file, before OFFSET is applied.)
|
771
|
1377
|
877
|
1378 If OFFSET is given, that value will be added to all charset codepoints
|
|
1379 in the file to obtain the internal charset codepoint. \(We assume
|
|
1380 that octets in the table are in the range 33 to 126 or 32 to 127. If
|
|
1381 you have a table in ku-ten form, with octets in the range 1 to 94, you
|
|
1382 will have to use an offset of 5140, i.e. 0x2020.)
|
771
|
1383
|
|
1384 FLAGS, if specified, control further how the tables are interpreted
|
877
|
1385 and are used to special-case certain known format deviations in the
|
|
1386 Unicode tables or in the charset:
|
771
|
1387
|
|
1388 `ignore-first-column'
|
877
|
1389 The JIS X 0208 tables have 3 columns of data instead of 2. The first
|
|
1390 column contains the Shift-JIS codepoint, which we ignore.
|
771
|
1391 `big5'
|
877
|
1392 The charset codepoints are Big Five codepoints; convert it to the
|
|
1393 hacked-up Mule codepoint in `chinese-big5-1' or `chinese-big5-2'.
|
771
|
1394 */
|
|
1395 (filename, charset, start, end, offset, flags))
|
|
1396 {
|
|
1397 int st = 0, en = INT_MAX, of = 0;
|
|
1398 FILE *file;
|
|
1399 struct gcpro gcpro1;
|
|
1400 char line[1025];
|
|
1401 int fondo = specpdl_depth ();
|
|
1402 int ignore_first_column = 0;
|
|
1403 int big5 = 0;
|
|
1404
|
|
1405 CHECK_STRING (filename);
|
|
1406 charset = Fget_charset (charset);
|
|
1407 if (!NILP (start))
|
|
1408 {
|
|
1409 CHECK_INT (start);
|
|
1410 st = XINT (start);
|
|
1411 }
|
|
1412 if (!NILP (end))
|
|
1413 {
|
|
1414 CHECK_INT (end);
|
|
1415 en = XINT (end);
|
|
1416 }
|
|
1417 if (!NILP (offset))
|
|
1418 {
|
|
1419 CHECK_INT (offset);
|
|
1420 of = XINT (offset);
|
|
1421 }
|
|
1422
|
|
1423 if (!LISTP (flags))
|
|
1424 flags = list1 (flags);
|
|
1425
|
|
1426 {
|
|
1427 EXTERNAL_LIST_LOOP_2 (elt, flags)
|
|
1428 {
|
|
1429 if (EQ (elt, Qignore_first_column))
|
|
1430 ignore_first_column = 1;
|
|
1431 else if (EQ (elt, Qbig5))
|
|
1432 big5 = 1;
|
|
1433 else
|
|
1434 invalid_constant
|
1318
|
1435 ("Unrecognized `load-unicode-mapping-table' flag", elt);
|
771
|
1436 }
|
|
1437 }
|
|
1438
|
|
1439 GCPRO1 (filename);
|
|
1440 filename = Fexpand_file_name (filename, Qnil);
|
|
1441 file = qxe_fopen (XSTRING_DATA (filename), READ_TEXT);
|
|
1442 if (!file)
|
|
1443 report_file_error ("Cannot open", filename);
|
|
1444 record_unwind_protect (cerrar_el_fulano, make_opaque_ptr (file));
|
|
1445 while (fgets (line, sizeof (line), file))
|
|
1446 {
|
|
1447 char *p = line;
|
|
1448 int cp1, cp2, endcount;
|
|
1449 int cp1high, cp1low;
|
|
1450 int dummy;
|
|
1451
|
|
1452 while (*p) /* erase all comments out of the line */
|
|
1453 {
|
|
1454 if (*p == '#')
|
|
1455 *p = '\0';
|
|
1456 else
|
|
1457 p++;
|
|
1458 }
|
|
1459 /* see if line is nothing but whitespace and skip if so */
|
|
1460 p = line + strspn (line, " \t\n\r\f");
|
|
1461 if (!*p)
|
|
1462 continue;
|
|
1463 /* NOTE: It appears that MS Windows and Newlib sscanf() have
|
|
1464 different interpretations for whitespace (== "skip all whitespace
|
|
1465 at processing point"): Newlib requires at least one corresponding
|
|
1466 whitespace character in the input, but MS allows none. The
|
|
1467 following would be easier to write if we could count on the MS
|
|
1468 interpretation.
|
|
1469
|
|
1470 Also, the return value does NOT include %n storage. */
|
|
1471 if ((!ignore_first_column ?
|
|
1472 sscanf (p, "%i %i%n", &cp1, &cp2, &endcount) < 2 :
|
|
1473 sscanf (p, "%i %i %i%n", &dummy, &cp1, &cp2, &endcount) < 3)
|
2367
|
1474 /* #### Temporary code! Cygwin newlib fucked up scanf() handling
|
|
1475 of numbers beginning 0x0... starting in 04/2004, in an attempt
|
|
1476 to fix another bug. A partial fix for this was put in in
|
|
1477 06/2004, but as of 10/2004 the value of ENDCOUNT returned in
|
|
1478 such case is still wrong. If this gets fixed soon, remove
|
|
1479 this code. --ben */
|
|
1480 #ifndef CYGWIN_SCANF_BUG
|
|
1481 || *(p + endcount + strspn (p + endcount, " \t\n\r\f"))
|
|
1482 #endif
|
|
1483 )
|
771
|
1484 {
|
793
|
1485 warn_when_safe (Qunicode, Qwarning,
|
771
|
1486 "Unrecognized line in translation file %s:\n%s",
|
|
1487 XSTRING_DATA (filename), line);
|
|
1488 continue;
|
|
1489 }
|
|
1490 if (cp1 >= st && cp1 <= en)
|
|
1491 {
|
|
1492 cp1 += of;
|
|
1493 if (cp1 < 0 || cp1 >= 65536)
|
|
1494 {
|
|
1495 out_of_range:
|
793
|
1496 warn_when_safe (Qunicode, Qwarning,
|
|
1497 "Out of range first codepoint 0x%x in "
|
|
1498 "translation file %s:\n%s",
|
771
|
1499 cp1, XSTRING_DATA (filename), line);
|
|
1500 continue;
|
|
1501 }
|
|
1502
|
|
1503 cp1high = cp1 >> 8;
|
|
1504 cp1low = cp1 & 255;
|
|
1505
|
|
1506 if (big5)
|
|
1507 {
|
867
|
1508 Ichar ch = decode_big5_char (cp1high, cp1low);
|
771
|
1509 if (ch == -1)
|
793
|
1510
|
|
1511 warn_when_safe (Qunicode, Qwarning,
|
|
1512 "Out of range Big5 codepoint 0x%x in "
|
|
1513 "translation file %s:\n%s",
|
771
|
1514 cp1, XSTRING_DATA (filename), line);
|
|
1515 else
|
|
1516 set_unicode_conversion (ch, cp2);
|
|
1517 }
|
|
1518 else
|
|
1519 {
|
|
1520 int l1, h1, l2, h2;
|
867
|
1521 Ichar emch;
|
771
|
1522
|
|
1523 switch (XCHARSET_TYPE (charset))
|
|
1524 {
|
|
1525 case CHARSET_TYPE_94: l1 = 33; h1 = 126; l2 = 0; h2 = 0; break;
|
|
1526 case CHARSET_TYPE_96: l1 = 32; h1 = 127; l2 = 0; h2 = 0; break;
|
|
1527 case CHARSET_TYPE_94X94: l1 = 33; h1 = 126; l2 = 33; h2 = 126;
|
|
1528 break;
|
|
1529 case CHARSET_TYPE_96X96: l1 = 32; h1 = 127; l2 = 32; h2 = 127;
|
|
1530 break;
|
2500
|
1531 default: ABORT (); l1 = 0; h1 = 0; l2 = 0; h2 = 0;
|
771
|
1532 }
|
|
1533
|
|
1534 if (cp1high < l2 || cp1high > h2 || cp1low < l1 || cp1low > h1)
|
|
1535 goto out_of_range;
|
|
1536
|
867
|
1537 emch = (cp1high == 0 ? make_ichar (charset, cp1low, 0) :
|
|
1538 make_ichar (charset, cp1high, cp1low));
|
771
|
1539 set_unicode_conversion (emch, cp2);
|
|
1540 }
|
|
1541 }
|
|
1542 }
|
|
1543
|
|
1544 if (ferror (file))
|
|
1545 report_file_error ("IO error when reading", filename);
|
|
1546
|
|
1547 unbind_to (fondo); /* close file */
|
|
1548 UNGCPRO;
|
|
1549 return Qnil;
|
|
1550 }
|
|
1551
|
|
1552 #endif /* MULE */
|
|
1553
|
|
1554
|
|
1555 /************************************************************************/
|
|
1556 /* Unicode coding system */
|
|
1557 /************************************************************************/
|
|
1558
|
|
1559 /* ISO 10646 UTF-16, UCS-4, UTF-8, UTF-7, etc. */
|
|
1560
|
|
1561 enum unicode_type
|
|
1562 {
|
|
1563 UNICODE_UTF_16,
|
|
1564 UNICODE_UTF_8,
|
|
1565 UNICODE_UTF_7,
|
1429
|
1566 UNICODE_UCS_4
|
771
|
1567 };
|
|
1568
|
|
1569 struct unicode_coding_system
|
|
1570 {
|
|
1571 enum unicode_type type;
|
1887
|
1572 unsigned int little_endian :1;
|
|
1573 unsigned int need_bom :1;
|
771
|
1574 };
|
|
1575
|
|
1576 #define CODING_SYSTEM_UNICODE_TYPE(codesys) \
|
|
1577 (CODING_SYSTEM_TYPE_DATA (codesys, unicode)->type)
|
|
1578 #define XCODING_SYSTEM_UNICODE_TYPE(codesys) \
|
|
1579 CODING_SYSTEM_UNICODE_TYPE (XCODING_SYSTEM (codesys))
|
|
1580 #define CODING_SYSTEM_UNICODE_LITTLE_ENDIAN(codesys) \
|
|
1581 (CODING_SYSTEM_TYPE_DATA (codesys, unicode)->little_endian)
|
|
1582 #define XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN(codesys) \
|
|
1583 CODING_SYSTEM_UNICODE_LITTLE_ENDIAN (XCODING_SYSTEM (codesys))
|
|
1584 #define CODING_SYSTEM_UNICODE_NEED_BOM(codesys) \
|
|
1585 (CODING_SYSTEM_TYPE_DATA (codesys, unicode)->need_bom)
|
|
1586 #define XCODING_SYSTEM_UNICODE_NEED_BOM(codesys) \
|
|
1587 CODING_SYSTEM_UNICODE_NEED_BOM (XCODING_SYSTEM (codesys))
|
|
1588
|
|
1589 struct unicode_coding_stream
|
|
1590 {
|
|
1591 /* decode */
|
|
1592 unsigned char counter;
|
|
1593 int seen_char;
|
|
1594 /* encode */
|
|
1595 Lisp_Object current_charset;
|
|
1596 int current_char_boundary;
|
|
1597 int wrote_bom;
|
|
1598 };
|
|
1599
|
1204
|
1600 static const struct memory_description unicode_coding_system_description[] = {
|
771
|
1601 { XD_END }
|
|
1602 };
|
|
1603
|
1204
|
1604 DEFINE_CODING_SYSTEM_TYPE_WITH_DATA (unicode);
|
|
1605
|
771
|
1606 /* Decode a UCS-2 or UCS-4 character into a buffer. If the lookup fails, use
|
|
1607 <GETA MARK> (U+3013) of JIS X 0208, which means correct character
|
|
1608 is not found, instead.
|
|
1609 #### do something more appropriate (use blob?)
|
|
1610 Danger, Will Robinson! Data loss. Should we signal user? */
|
|
1611 static void
|
|
1612 decode_unicode_char (int ch, unsigned_char_dynarr *dst,
|
1887
|
1613 struct unicode_coding_stream *data,
|
|
1614 unsigned int ignore_bom)
|
771
|
1615 {
|
|
1616 if (ch == 0xFEFF && !data->seen_char && ignore_bom)
|
|
1617 ;
|
|
1618 else
|
|
1619 {
|
|
1620 #ifdef MULE
|
877
|
1621 Ichar chr = unicode_to_ichar (ch, unicode_precedence_dynarr);
|
771
|
1622
|
|
1623 if (chr != -1)
|
|
1624 {
|
867
|
1625 Ibyte work[MAX_ICHAR_LEN];
|
771
|
1626 int len;
|
|
1627
|
867
|
1628 len = set_itext_ichar (work, chr);
|
771
|
1629 Dynarr_add_many (dst, work, len);
|
|
1630 }
|
|
1631 else
|
|
1632 {
|
|
1633 Dynarr_add (dst, LEADING_BYTE_JAPANESE_JISX0208);
|
|
1634 Dynarr_add (dst, 34 + 128);
|
|
1635 Dynarr_add (dst, 46 + 128);
|
|
1636 }
|
|
1637 #else
|
867
|
1638 Dynarr_add (dst, (Ibyte) ch);
|
771
|
1639 #endif /* MULE */
|
|
1640 }
|
|
1641
|
|
1642 data->seen_char = 1;
|
|
1643 }
|
|
1644
|
|
1645 static void
|
|
1646 encode_unicode_char_1 (int code, unsigned_char_dynarr *dst,
|
1887
|
1647 enum unicode_type type, unsigned int little_endian)
|
771
|
1648 {
|
|
1649 switch (type)
|
|
1650 {
|
|
1651 case UNICODE_UTF_16:
|
|
1652 if (little_endian)
|
|
1653 {
|
|
1654 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1655 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1656 }
|
|
1657 else
|
|
1658 {
|
|
1659 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1660 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1661 }
|
|
1662 break;
|
|
1663
|
|
1664 case UNICODE_UCS_4:
|
|
1665 if (little_endian)
|
|
1666 {
|
|
1667 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1668 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1669 Dynarr_add (dst, (unsigned char) ((code >> 16) & 255));
|
|
1670 Dynarr_add (dst, (unsigned char) (code >> 24));
|
|
1671 }
|
|
1672 else
|
|
1673 {
|
|
1674 Dynarr_add (dst, (unsigned char) (code >> 24));
|
|
1675 Dynarr_add (dst, (unsigned char) ((code >> 16) & 255));
|
|
1676 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1677 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1678 }
|
|
1679 break;
|
|
1680
|
|
1681 case UNICODE_UTF_8:
|
|
1682 if (code <= 0x7f)
|
|
1683 {
|
|
1684 Dynarr_add (dst, (unsigned char) code);
|
|
1685 }
|
|
1686 else if (code <= 0x7ff)
|
|
1687 {
|
|
1688 Dynarr_add (dst, (unsigned char) ((code >> 6) | 0xc0));
|
|
1689 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1690 }
|
|
1691 else if (code <= 0xffff)
|
|
1692 {
|
|
1693 Dynarr_add (dst, (unsigned char) ((code >> 12) | 0xe0));
|
|
1694 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1695 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1696 }
|
|
1697 else if (code <= 0x1fffff)
|
|
1698 {
|
|
1699 Dynarr_add (dst, (unsigned char) ((code >> 18) | 0xf0));
|
|
1700 Dynarr_add (dst, (unsigned char) (((code >> 12) & 0x3f) | 0x80));
|
|
1701 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1702 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1703 }
|
|
1704 else if (code <= 0x3ffffff)
|
|
1705 {
|
|
1706 Dynarr_add (dst, (unsigned char) ((code >> 24) | 0xf8));
|
|
1707 Dynarr_add (dst, (unsigned char) (((code >> 18) & 0x3f) | 0x80));
|
|
1708 Dynarr_add (dst, (unsigned char) (((code >> 12) & 0x3f) | 0x80));
|
|
1709 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1710 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1711 }
|
|
1712 else
|
|
1713 {
|
|
1714 Dynarr_add (dst, (unsigned char) ((code >> 30) | 0xfc));
|
|
1715 Dynarr_add (dst, (unsigned char) (((code >> 24) & 0x3f) | 0x80));
|
|
1716 Dynarr_add (dst, (unsigned char) (((code >> 18) & 0x3f) | 0x80));
|
|
1717 Dynarr_add (dst, (unsigned char) (((code >> 12) & 0x3f) | 0x80));
|
|
1718 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1719 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1720 }
|
|
1721 break;
|
|
1722
|
2500
|
1723 case UNICODE_UTF_7: ABORT ();
|
771
|
1724
|
2500
|
1725 default: ABORT ();
|
771
|
1726 }
|
|
1727 }
|
|
1728
|
|
1729 static void
|
2333
|
1730 encode_unicode_char (Lisp_Object USED_IF_MULE (charset), int h,
|
|
1731 int USED_IF_MULE (l), unsigned_char_dynarr *dst,
|
|
1732 enum unicode_type type, unsigned int little_endian)
|
771
|
1733 {
|
|
1734 #ifdef MULE
|
867
|
1735 int code = ichar_to_unicode (make_ichar (charset, h & 127, l & 127));
|
771
|
1736
|
|
1737 if (code == -1)
|
|
1738 {
|
|
1739 if (type != UNICODE_UTF_16 &&
|
|
1740 XCHARSET_DIMENSION (charset) == 2 &&
|
|
1741 XCHARSET_CHARS (charset) == 94)
|
|
1742 {
|
|
1743 unsigned char final = XCHARSET_FINAL (charset);
|
|
1744
|
|
1745 if (('@' <= final) && (final < 0x7f))
|
|
1746 code = (0xe00000 + (final - '@') * 94 * 94
|
|
1747 + ((h & 127) - 33) * 94 + (l & 127) - 33);
|
|
1748 else
|
|
1749 code = '?';
|
|
1750 }
|
|
1751 else
|
|
1752 code = '?';
|
|
1753 }
|
|
1754 #else
|
|
1755 int code = h;
|
|
1756 #endif /* MULE */
|
|
1757
|
|
1758 encode_unicode_char_1 (code, dst, type, little_endian);
|
|
1759 }
|
|
1760
|
|
1761 static Bytecount
|
|
1762 unicode_convert (struct coding_stream *str, const UExtbyte *src,
|
|
1763 unsigned_char_dynarr *dst, Bytecount n)
|
|
1764 {
|
|
1765 unsigned int ch = str->ch;
|
|
1766 struct unicode_coding_stream *data = CODING_STREAM_TYPE_DATA (str, unicode);
|
|
1767 enum unicode_type type =
|
|
1768 XCODING_SYSTEM_UNICODE_TYPE (str->codesys);
|
1887
|
1769 unsigned int little_endian =
|
|
1770 XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (str->codesys);
|
|
1771 unsigned int ignore_bom = XCODING_SYSTEM_UNICODE_NEED_BOM (str->codesys);
|
771
|
1772 Bytecount orign = n;
|
|
1773
|
|
1774 if (str->direction == CODING_DECODE)
|
|
1775 {
|
|
1776 unsigned char counter = data->counter;
|
|
1777
|
|
1778 while (n--)
|
|
1779 {
|
|
1780 UExtbyte c = *src++;
|
|
1781
|
|
1782 switch (type)
|
|
1783 {
|
|
1784 case UNICODE_UTF_8:
|
|
1785 switch (counter)
|
|
1786 {
|
|
1787 case 0:
|
|
1788 if (c >= 0xfc)
|
|
1789 {
|
|
1790 ch = c & 0x01;
|
|
1791 counter = 5;
|
|
1792 }
|
|
1793 else if (c >= 0xf8)
|
|
1794 {
|
|
1795 ch = c & 0x03;
|
|
1796 counter = 4;
|
|
1797 }
|
|
1798 else if (c >= 0xf0)
|
|
1799 {
|
|
1800 ch = c & 0x07;
|
|
1801 counter = 3;
|
|
1802 }
|
|
1803 else if (c >= 0xe0)
|
|
1804 {
|
|
1805 ch = c & 0x0f;
|
|
1806 counter = 2;
|
|
1807 }
|
|
1808 else if (c >= 0xc0)
|
|
1809 {
|
|
1810 ch = c & 0x1f;
|
|
1811 counter = 1;
|
|
1812 }
|
|
1813 else
|
|
1814 decode_unicode_char (c, dst, data, ignore_bom);
|
|
1815 break;
|
|
1816 case 1:
|
|
1817 ch = (ch << 6) | (c & 0x3f);
|
|
1818 decode_unicode_char (ch, dst, data, ignore_bom);
|
|
1819 ch = 0;
|
|
1820 counter = 0;
|
|
1821 break;
|
|
1822 default:
|
|
1823 ch = (ch << 6) | (c & 0x3f);
|
|
1824 counter--;
|
|
1825 }
|
|
1826 break;
|
|
1827
|
|
1828 case UNICODE_UTF_16:
|
|
1829 if (little_endian)
|
|
1830 ch = (c << counter) | ch;
|
|
1831 else
|
|
1832 ch = (ch << 8) | c;
|
|
1833 counter += 8;
|
|
1834 if (counter == 16)
|
|
1835 {
|
|
1836 int tempch = ch;
|
|
1837 ch = 0;
|
|
1838 counter = 0;
|
|
1839 decode_unicode_char (tempch, dst, data, ignore_bom);
|
|
1840 }
|
|
1841 break;
|
|
1842
|
|
1843 case UNICODE_UCS_4:
|
|
1844 if (little_endian)
|
|
1845 ch = (c << counter) | ch;
|
|
1846 else
|
|
1847 ch = (ch << 8) | c;
|
|
1848 counter += 8;
|
|
1849 if (counter == 32)
|
|
1850 {
|
|
1851 int tempch = ch;
|
|
1852 ch = 0;
|
|
1853 counter = 0;
|
|
1854 if (tempch < 0)
|
|
1855 {
|
|
1856 /* !!#### indicate an error */
|
|
1857 tempch = '~';
|
|
1858 }
|
|
1859 decode_unicode_char (tempch, dst, data, ignore_bom);
|
|
1860 }
|
|
1861 break;
|
|
1862
|
|
1863 case UNICODE_UTF_7:
|
2500
|
1864 ABORT ();
|
771
|
1865 break;
|
|
1866
|
2500
|
1867 default: ABORT ();
|
771
|
1868 }
|
|
1869
|
|
1870 }
|
|
1871 if (str->eof)
|
|
1872 DECODE_OUTPUT_PARTIAL_CHAR (ch, dst);
|
|
1873
|
|
1874 data->counter = counter;
|
|
1875 }
|
|
1876 else
|
|
1877 {
|
|
1878 unsigned char char_boundary = data->current_char_boundary;
|
|
1879 Lisp_Object charset = data->current_charset;
|
|
1880
|
|
1881 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1882 /* flags for handling composite chars. We do a little switcheroo
|
|
1883 on the source while we're outputting the composite char. */
|
|
1884 Bytecount saved_n = 0;
|
867
|
1885 const Ibyte *saved_src = NULL;
|
771
|
1886 int in_composite = 0;
|
|
1887
|
|
1888 back_to_square_n:
|
|
1889 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
1890
|
|
1891 if (XCODING_SYSTEM_UNICODE_NEED_BOM (str->codesys) && !data->wrote_bom)
|
|
1892 {
|
|
1893 encode_unicode_char_1 (0xFEFF, dst, type, little_endian);
|
|
1894 data->wrote_bom = 1;
|
|
1895 }
|
|
1896
|
|
1897 while (n--)
|
|
1898 {
|
867
|
1899 Ibyte c = *src++;
|
771
|
1900
|
|
1901 #ifdef MULE
|
826
|
1902 if (byte_ascii_p (c))
|
771
|
1903 #endif /* MULE */
|
|
1904 { /* Processing ASCII character */
|
|
1905 ch = 0;
|
|
1906 encode_unicode_char (Vcharset_ascii, c, 0, dst, type,
|
|
1907 little_endian);
|
|
1908
|
|
1909 char_boundary = 1;
|
|
1910 }
|
|
1911 #ifdef MULE
|
867
|
1912 else if (ibyte_leading_byte_p (c) || ibyte_leading_byte_p (ch))
|
771
|
1913 { /* Processing Leading Byte */
|
|
1914 ch = 0;
|
826
|
1915 charset = charset_by_leading_byte (c);
|
|
1916 if (leading_byte_prefix_p(c))
|
771
|
1917 ch = c;
|
|
1918 char_boundary = 0;
|
|
1919 }
|
|
1920 else
|
|
1921 { /* Processing Non-ASCII character */
|
|
1922 char_boundary = 1;
|
|
1923 if (EQ (charset, Vcharset_control_1))
|
|
1924 encode_unicode_char (Vcharset_control_1, c, 0, dst,
|
|
1925 type, little_endian);
|
|
1926 else
|
|
1927 {
|
|
1928 switch (XCHARSET_REP_BYTES (charset))
|
|
1929 {
|
|
1930 case 2:
|
|
1931 encode_unicode_char (charset, c, 0, dst, type,
|
|
1932 little_endian);
|
|
1933 break;
|
|
1934 case 3:
|
|
1935 if (XCHARSET_PRIVATE_P (charset))
|
|
1936 {
|
|
1937 encode_unicode_char (charset, c, 0, dst, type,
|
|
1938 little_endian);
|
|
1939 ch = 0;
|
|
1940 }
|
|
1941 else if (ch)
|
|
1942 {
|
|
1943 #ifdef ENABLE_COMPOSITE_CHARS
|
|
1944 if (EQ (charset, Vcharset_composite))
|
|
1945 {
|
|
1946 if (in_composite)
|
|
1947 {
|
|
1948 /* #### Bother! We don't know how to
|
|
1949 handle this yet. */
|
|
1950 encode_unicode_char (Vcharset_ascii, '~', 0,
|
|
1951 dst, type,
|
|
1952 little_endian);
|
|
1953 }
|
|
1954 else
|
|
1955 {
|
867
|
1956 Ichar emch = make_ichar (Vcharset_composite,
|
771
|
1957 ch & 0x7F,
|
|
1958 c & 0x7F);
|
|
1959 Lisp_Object lstr =
|
|
1960 composite_char_string (emch);
|
|
1961 saved_n = n;
|
|
1962 saved_src = src;
|
|
1963 in_composite = 1;
|
|
1964 src = XSTRING_DATA (lstr);
|
|
1965 n = XSTRING_LENGTH (lstr);
|
|
1966 }
|
|
1967 }
|
|
1968 else
|
|
1969 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
1970 encode_unicode_char (charset, ch, c, dst, type,
|
|
1971 little_endian);
|
|
1972 ch = 0;
|
|
1973 }
|
|
1974 else
|
|
1975 {
|
|
1976 ch = c;
|
|
1977 char_boundary = 0;
|
|
1978 }
|
|
1979 break;
|
|
1980 case 4:
|
|
1981 if (ch)
|
|
1982 {
|
|
1983 encode_unicode_char (charset, ch, c, dst, type,
|
|
1984 little_endian);
|
|
1985 ch = 0;
|
|
1986 }
|
|
1987 else
|
|
1988 {
|
|
1989 ch = c;
|
|
1990 char_boundary = 0;
|
|
1991 }
|
|
1992 break;
|
|
1993 default:
|
2500
|
1994 ABORT ();
|
771
|
1995 }
|
|
1996 }
|
|
1997 }
|
|
1998 #endif /* MULE */
|
|
1999 }
|
|
2000
|
|
2001 #ifdef ENABLE_COMPOSITE_CHARS
|
|
2002 if (in_composite)
|
|
2003 {
|
|
2004 n = saved_n;
|
|
2005 src = saved_src;
|
|
2006 in_composite = 0;
|
|
2007 goto back_to_square_n; /* Wheeeeeeeee ..... */
|
|
2008 }
|
|
2009 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
2010
|
|
2011 data->current_char_boundary = char_boundary;
|
|
2012 data->current_charset = charset;
|
|
2013
|
|
2014 /* La palabra se hizo carne! */
|
|
2015 /* A palavra fez-se carne! */
|
|
2016 /* Whatever. */
|
|
2017 }
|
|
2018
|
|
2019 str->ch = ch;
|
|
2020 return orign;
|
|
2021 }
|
|
2022
|
|
2023 /* DEFINE_DETECTOR (utf_7); */
|
|
2024 DEFINE_DETECTOR (utf_8);
|
|
2025 DEFINE_DETECTOR_CATEGORY (utf_8, utf_8);
|
985
|
2026 DEFINE_DETECTOR_CATEGORY (utf_8, utf_8_bom);
|
771
|
2027 DEFINE_DETECTOR (ucs_4);
|
|
2028 DEFINE_DETECTOR_CATEGORY (ucs_4, ucs_4);
|
|
2029 DEFINE_DETECTOR (utf_16);
|
|
2030 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16);
|
|
2031 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian);
|
|
2032 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16_bom);
|
|
2033 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian_bom);
|
|
2034
|
|
2035 struct ucs_4_detector
|
|
2036 {
|
|
2037 int in_ucs_4_byte;
|
|
2038 };
|
|
2039
|
|
2040 static void
|
|
2041 ucs_4_detect (struct detection_state *st, const UExtbyte *src,
|
|
2042 Bytecount n)
|
|
2043 {
|
|
2044 struct ucs_4_detector *data = DETECTION_STATE_DATA (st, ucs_4);
|
|
2045
|
|
2046 while (n--)
|
|
2047 {
|
|
2048 UExtbyte c = *src++;
|
|
2049 switch (data->in_ucs_4_byte)
|
|
2050 {
|
|
2051 case 0:
|
|
2052 if (c >= 128)
|
|
2053 {
|
|
2054 DET_RESULT (st, ucs_4) = DET_NEARLY_IMPOSSIBLE;
|
|
2055 return;
|
|
2056 }
|
|
2057 else
|
|
2058 data->in_ucs_4_byte++;
|
|
2059 break;
|
|
2060 case 3:
|
|
2061 data->in_ucs_4_byte = 0;
|
|
2062 break;
|
|
2063 default:
|
|
2064 data->in_ucs_4_byte++;
|
|
2065 }
|
|
2066 }
|
|
2067
|
|
2068 /* !!#### write this for real */
|
|
2069 DET_RESULT (st, ucs_4) = DET_AS_LIKELY_AS_UNLIKELY;
|
|
2070 }
|
|
2071
|
|
2072 struct utf_16_detector
|
|
2073 {
|
|
2074 unsigned int seen_ffff:1;
|
|
2075 unsigned int seen_forward_bom:1;
|
|
2076 unsigned int seen_rev_bom:1;
|
|
2077 int byteno;
|
|
2078 int prev_char;
|
|
2079 int text, rev_text;
|
1267
|
2080 int sep, rev_sep;
|
|
2081 int num_ascii;
|
771
|
2082 };
|
|
2083
|
|
2084 static void
|
|
2085 utf_16_detect (struct detection_state *st, const UExtbyte *src,
|
|
2086 Bytecount n)
|
|
2087 {
|
|
2088 struct utf_16_detector *data = DETECTION_STATE_DATA (st, utf_16);
|
|
2089
|
|
2090 while (n--)
|
|
2091 {
|
|
2092 UExtbyte c = *src++;
|
|
2093 int prevc = data->prev_char;
|
|
2094 if (data->byteno == 1 && c == 0xFF && prevc == 0xFE)
|
|
2095 data->seen_forward_bom = 1;
|
|
2096 else if (data->byteno == 1 && c == 0xFE && prevc == 0xFF)
|
|
2097 data->seen_rev_bom = 1;
|
|
2098
|
|
2099 if (data->byteno & 1)
|
|
2100 {
|
|
2101 if (c == 0xFF && prevc == 0xFF)
|
|
2102 data->seen_ffff = 1;
|
|
2103 if (prevc == 0
|
|
2104 && (c == '\r' || c == '\n'
|
|
2105 || (c >= 0x20 && c <= 0x7E)))
|
|
2106 data->text++;
|
|
2107 if (c == 0
|
|
2108 && (prevc == '\r' || prevc == '\n'
|
|
2109 || (prevc >= 0x20 && prevc <= 0x7E)))
|
|
2110 data->rev_text++;
|
1267
|
2111 /* #### 0x2028 is LINE SEPARATOR and 0x2029 is PARAGRAPH SEPARATOR.
|
|
2112 I used to count these in text and rev_text but that is very bad,
|
|
2113 as 0x2028 is also space + left-paren in ASCII, which is extremely
|
|
2114 common. So, what do we do with these? */
|
771
|
2115 if (prevc == 0x20 && (c == 0x28 || c == 0x29))
|
1267
|
2116 data->sep++;
|
771
|
2117 if (c == 0x20 && (prevc == 0x28 || prevc == 0x29))
|
1267
|
2118 data->rev_sep++;
|
771
|
2119 }
|
|
2120
|
1267
|
2121 if ((c >= ' ' && c <= '~') || c == '\n' || c == '\r' || c == '\t' ||
|
|
2122 c == '\f' || c == '\v')
|
|
2123 data->num_ascii++;
|
771
|
2124 data->byteno++;
|
|
2125 data->prev_char = c;
|
|
2126 }
|
|
2127
|
|
2128 {
|
|
2129 int variance_indicates_big_endian =
|
|
2130 (data->text >= 10
|
|
2131 && (data->rev_text == 0
|
|
2132 || data->text / data->rev_text >= 10));
|
|
2133 int variance_indicates_little_endian =
|
|
2134 (data->rev_text >= 10
|
|
2135 && (data->text == 0
|
|
2136 || data->rev_text / data->text >= 10));
|
|
2137
|
|
2138 if (data->seen_ffff)
|
|
2139 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2140 else if (data->seen_forward_bom)
|
|
2141 {
|
|
2142 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2143 if (variance_indicates_big_endian)
|
|
2144 DET_RESULT (st, utf_16_bom) = DET_NEAR_CERTAINTY;
|
|
2145 else if (variance_indicates_little_endian)
|
|
2146 DET_RESULT (st, utf_16_bom) = DET_SOMEWHAT_LIKELY;
|
|
2147 else
|
|
2148 DET_RESULT (st, utf_16_bom) = DET_QUITE_PROBABLE;
|
|
2149 }
|
|
2150 else if (data->seen_forward_bom)
|
|
2151 {
|
|
2152 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2153 if (variance_indicates_big_endian)
|
|
2154 DET_RESULT (st, utf_16_bom) = DET_NEAR_CERTAINTY;
|
|
2155 else if (variance_indicates_little_endian)
|
|
2156 /* #### may need to rethink */
|
|
2157 DET_RESULT (st, utf_16_bom) = DET_SOMEWHAT_LIKELY;
|
|
2158 else
|
|
2159 /* #### may need to rethink */
|
|
2160 DET_RESULT (st, utf_16_bom) = DET_QUITE_PROBABLE;
|
|
2161 }
|
|
2162 else if (data->seen_rev_bom)
|
|
2163 {
|
|
2164 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2165 if (variance_indicates_little_endian)
|
|
2166 DET_RESULT (st, utf_16_little_endian_bom) = DET_NEAR_CERTAINTY;
|
|
2167 else if (variance_indicates_big_endian)
|
|
2168 /* #### may need to rethink */
|
|
2169 DET_RESULT (st, utf_16_little_endian_bom) = DET_SOMEWHAT_LIKELY;
|
|
2170 else
|
|
2171 /* #### may need to rethink */
|
|
2172 DET_RESULT (st, utf_16_little_endian_bom) = DET_QUITE_PROBABLE;
|
|
2173 }
|
|
2174 else if (variance_indicates_big_endian)
|
|
2175 {
|
|
2176 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2177 DET_RESULT (st, utf_16) = DET_SOMEWHAT_LIKELY;
|
|
2178 DET_RESULT (st, utf_16_little_endian) = DET_SOMEWHAT_UNLIKELY;
|
|
2179 }
|
|
2180 else if (variance_indicates_little_endian)
|
|
2181 {
|
|
2182 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2183 DET_RESULT (st, utf_16) = DET_SOMEWHAT_UNLIKELY;
|
|
2184 DET_RESULT (st, utf_16_little_endian) = DET_SOMEWHAT_LIKELY;
|
|
2185 }
|
|
2186 else
|
1267
|
2187 {
|
|
2188 /* #### FUCKME! There should really be an ASCII detector. This
|
|
2189 would rule out the need to have this built-in here as
|
|
2190 well. --ben */
|
1292
|
2191 int pct_ascii = data->byteno ? (100 * data->num_ascii) / data->byteno
|
|
2192 : 100;
|
1267
|
2193
|
|
2194 if (pct_ascii > 90)
|
|
2195 SET_DET_RESULTS (st, utf_16, DET_QUITE_IMPROBABLE);
|
|
2196 else if (pct_ascii > 75)
|
|
2197 SET_DET_RESULTS (st, utf_16, DET_SOMEWHAT_UNLIKELY);
|
|
2198 else
|
|
2199 SET_DET_RESULTS (st, utf_16, DET_AS_LIKELY_AS_UNLIKELY);
|
|
2200 }
|
771
|
2201 }
|
|
2202 }
|
|
2203
|
|
2204 struct utf_8_detector
|
|
2205 {
|
985
|
2206 int byteno;
|
|
2207 int first_byte;
|
|
2208 int second_byte;
|
1267
|
2209 int prev_byte;
|
771
|
2210 int in_utf_8_byte;
|
1267
|
2211 int recent_utf_8_sequence;
|
|
2212 int seen_bogus_utf8;
|
|
2213 int seen_really_bogus_utf8;
|
|
2214 int seen_2byte_sequence;
|
|
2215 int seen_longer_sequence;
|
|
2216 int seen_iso2022_esc;
|
|
2217 int seen_iso_shift;
|
1887
|
2218 unsigned int seen_utf_bom:1;
|
771
|
2219 };
|
|
2220
|
|
2221 static void
|
|
2222 utf_8_detect (struct detection_state *st, const UExtbyte *src,
|
|
2223 Bytecount n)
|
|
2224 {
|
|
2225 struct utf_8_detector *data = DETECTION_STATE_DATA (st, utf_8);
|
|
2226
|
|
2227 while (n--)
|
|
2228 {
|
|
2229 UExtbyte c = *src++;
|
985
|
2230 switch (data->byteno)
|
|
2231 {
|
|
2232 case 0:
|
|
2233 data->first_byte = c;
|
|
2234 break;
|
|
2235 case 1:
|
|
2236 data->second_byte = c;
|
|
2237 break;
|
|
2238 case 2:
|
|
2239 if (data->first_byte == 0xef &&
|
|
2240 data->second_byte == 0xbb &&
|
|
2241 c == 0xbf)
|
1267
|
2242 data->seen_utf_bom = 1;
|
985
|
2243 break;
|
|
2244 }
|
|
2245
|
771
|
2246 switch (data->in_utf_8_byte)
|
|
2247 {
|
|
2248 case 0:
|
1267
|
2249 if (data->prev_byte == ISO_CODE_ESC && c >= 0x28 && c <= 0x2F)
|
|
2250 data->seen_iso2022_esc++;
|
|
2251 else if (c == ISO_CODE_SI || c == ISO_CODE_SO)
|
|
2252 data->seen_iso_shift++;
|
771
|
2253 else if (c >= 0xfc)
|
|
2254 data->in_utf_8_byte = 5;
|
|
2255 else if (c >= 0xf8)
|
|
2256 data->in_utf_8_byte = 4;
|
|
2257 else if (c >= 0xf0)
|
|
2258 data->in_utf_8_byte = 3;
|
|
2259 else if (c >= 0xe0)
|
|
2260 data->in_utf_8_byte = 2;
|
|
2261 else if (c >= 0xc0)
|
|
2262 data->in_utf_8_byte = 1;
|
|
2263 else if (c >= 0x80)
|
1267
|
2264 data->seen_bogus_utf8++;
|
|
2265 if (data->in_utf_8_byte > 0)
|
|
2266 data->recent_utf_8_sequence = data->in_utf_8_byte;
|
771
|
2267 break;
|
|
2268 default:
|
|
2269 if ((c & 0xc0) != 0x80)
|
1267
|
2270 data->seen_really_bogus_utf8++;
|
|
2271 else
|
771
|
2272 {
|
1267
|
2273 data->in_utf_8_byte--;
|
|
2274 if (data->in_utf_8_byte == 0)
|
|
2275 {
|
|
2276 if (data->recent_utf_8_sequence == 1)
|
|
2277 data->seen_2byte_sequence++;
|
|
2278 else
|
|
2279 {
|
|
2280 assert (data->recent_utf_8_sequence >= 2);
|
|
2281 data->seen_longer_sequence++;
|
|
2282 }
|
|
2283 }
|
771
|
2284 }
|
|
2285 }
|
985
|
2286
|
|
2287 data->byteno++;
|
1267
|
2288 data->prev_byte = c;
|
771
|
2289 }
|
1267
|
2290
|
|
2291 /* either BOM or no BOM, but not both */
|
|
2292 SET_DET_RESULTS (st, utf_8, DET_NEARLY_IMPOSSIBLE);
|
|
2293
|
|
2294
|
|
2295 if (data->seen_utf_bom)
|
|
2296 DET_RESULT (st, utf_8_bom) = DET_NEAR_CERTAINTY;
|
|
2297 else
|
|
2298 {
|
|
2299 if (data->seen_really_bogus_utf8 ||
|
|
2300 data->seen_bogus_utf8 >= 2)
|
|
2301 ; /* bogus */
|
|
2302 else if (data->seen_bogus_utf8)
|
|
2303 DET_RESULT (st, utf_8) = DET_SOMEWHAT_UNLIKELY;
|
|
2304 else if ((data->seen_longer_sequence >= 5 ||
|
|
2305 data->seen_2byte_sequence >= 10) &&
|
|
2306 (!(data->seen_iso2022_esc + data->seen_iso_shift) ||
|
|
2307 (data->seen_longer_sequence * 2 + data->seen_2byte_sequence) /
|
|
2308 (data->seen_iso2022_esc + data->seen_iso_shift) >= 10))
|
|
2309 /* heuristics, heuristics, we love heuristics */
|
|
2310 DET_RESULT (st, utf_8) = DET_QUITE_PROBABLE;
|
|
2311 else if (data->seen_iso2022_esc ||
|
|
2312 data->seen_iso_shift >= 3)
|
|
2313 DET_RESULT (st, utf_8) = DET_SOMEWHAT_UNLIKELY;
|
|
2314 else if (data->seen_longer_sequence ||
|
|
2315 data->seen_2byte_sequence)
|
|
2316 DET_RESULT (st, utf_8) = DET_SOMEWHAT_LIKELY;
|
|
2317 else if (data->seen_iso_shift)
|
|
2318 DET_RESULT (st, utf_8) = DET_SOMEWHAT_UNLIKELY;
|
|
2319 else
|
|
2320 DET_RESULT (st, utf_8) = DET_AS_LIKELY_AS_UNLIKELY;
|
|
2321 }
|
771
|
2322 }
|
|
2323
|
|
2324 static void
|
|
2325 unicode_init_coding_stream (struct coding_stream *str)
|
|
2326 {
|
|
2327 struct unicode_coding_stream *data =
|
|
2328 CODING_STREAM_TYPE_DATA (str, unicode);
|
|
2329 xzero (*data);
|
|
2330 data->current_charset = Qnil;
|
|
2331 }
|
|
2332
|
|
2333 static void
|
|
2334 unicode_rewind_coding_stream (struct coding_stream *str)
|
|
2335 {
|
|
2336 unicode_init_coding_stream (str);
|
|
2337 }
|
|
2338
|
|
2339 static int
|
|
2340 unicode_putprop (Lisp_Object codesys, Lisp_Object key, Lisp_Object value)
|
|
2341 {
|
|
2342 if (EQ (key, Qtype))
|
|
2343 {
|
|
2344 enum unicode_type type;
|
|
2345
|
|
2346 if (EQ (value, Qutf_8))
|
|
2347 type = UNICODE_UTF_8;
|
|
2348 else if (EQ (value, Qutf_16))
|
|
2349 type = UNICODE_UTF_16;
|
|
2350 else if (EQ (value, Qutf_7))
|
|
2351 type = UNICODE_UTF_7;
|
|
2352 else if (EQ (value, Qucs_4))
|
|
2353 type = UNICODE_UCS_4;
|
|
2354 else
|
|
2355 invalid_constant ("Invalid Unicode type", key);
|
|
2356
|
|
2357 XCODING_SYSTEM_UNICODE_TYPE (codesys) = type;
|
|
2358 }
|
|
2359 else if (EQ (key, Qlittle_endian))
|
|
2360 XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (codesys) = !NILP (value);
|
|
2361 else if (EQ (key, Qneed_bom))
|
|
2362 XCODING_SYSTEM_UNICODE_NEED_BOM (codesys) = !NILP (value);
|
|
2363 else
|
|
2364 return 0;
|
|
2365 return 1;
|
|
2366 }
|
|
2367
|
|
2368 static Lisp_Object
|
|
2369 unicode_getprop (Lisp_Object coding_system, Lisp_Object prop)
|
|
2370 {
|
|
2371 if (EQ (prop, Qtype))
|
|
2372 {
|
|
2373 switch (XCODING_SYSTEM_UNICODE_TYPE (coding_system))
|
|
2374 {
|
|
2375 case UNICODE_UTF_16: return Qutf_16;
|
|
2376 case UNICODE_UTF_8: return Qutf_8;
|
|
2377 case UNICODE_UTF_7: return Qutf_7;
|
|
2378 case UNICODE_UCS_4: return Qucs_4;
|
2500
|
2379 default: ABORT ();
|
771
|
2380 }
|
|
2381 }
|
|
2382 else if (EQ (prop, Qlittle_endian))
|
|
2383 return XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (coding_system) ? Qt : Qnil;
|
|
2384 else if (EQ (prop, Qneed_bom))
|
|
2385 return XCODING_SYSTEM_UNICODE_NEED_BOM (coding_system) ? Qt : Qnil;
|
|
2386 return Qunbound;
|
|
2387 }
|
|
2388
|
|
2389 static void
|
2286
|
2390 unicode_print (Lisp_Object cs, Lisp_Object printcharfun,
|
|
2391 int UNUSED (escapeflag))
|
771
|
2392 {
|
800
|
2393 write_fmt_string_lisp (printcharfun, "(%s", 1, unicode_getprop (cs, Qtype));
|
771
|
2394 if (XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (cs))
|
826
|
2395 write_c_string (printcharfun, ", little-endian");
|
771
|
2396 if (XCODING_SYSTEM_UNICODE_NEED_BOM (cs))
|
826
|
2397 write_c_string (printcharfun, ", need-bom");
|
|
2398 write_c_string (printcharfun, ")");
|
771
|
2399 }
|
|
2400
|
|
2401 int
|
2286
|
2402 dfc_coding_system_is_unicode (
|
|
2403 #ifdef WIN32_ANY
|
|
2404 Lisp_Object codesys
|
|
2405 #else
|
|
2406 Lisp_Object UNUSED (codesys)
|
|
2407 #endif
|
|
2408 )
|
771
|
2409 {
|
1315
|
2410 #ifdef WIN32_ANY
|
771
|
2411 codesys = Fget_coding_system (codesys);
|
|
2412 return (EQ (XCODING_SYSTEM_TYPE (codesys), Qunicode) &&
|
|
2413 XCODING_SYSTEM_UNICODE_TYPE (codesys) == UNICODE_UTF_16 &&
|
|
2414 XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (codesys));
|
|
2415
|
|
2416 #else
|
|
2417 return 0;
|
|
2418 #endif
|
|
2419 }
|
|
2420
|
|
2421
|
|
2422 /************************************************************************/
|
|
2423 /* Initialization */
|
|
2424 /************************************************************************/
|
|
2425
|
|
2426 void
|
|
2427 syms_of_unicode (void)
|
|
2428 {
|
|
2429 #ifdef MULE
|
877
|
2430 DEFSUBR (Funicode_precedence_list);
|
771
|
2431 DEFSUBR (Fset_language_unicode_precedence_list);
|
|
2432 DEFSUBR (Flanguage_unicode_precedence_list);
|
|
2433 DEFSUBR (Fset_default_unicode_precedence_list);
|
|
2434 DEFSUBR (Fdefault_unicode_precedence_list);
|
|
2435 DEFSUBR (Fset_unicode_conversion);
|
|
2436
|
1318
|
2437 DEFSUBR (Fload_unicode_mapping_table);
|
771
|
2438
|
|
2439 DEFSYMBOL (Qignore_first_column);
|
|
2440 #endif /* MULE */
|
|
2441
|
800
|
2442 DEFSUBR (Fchar_to_unicode);
|
|
2443 DEFSUBR (Funicode_to_char);
|
771
|
2444
|
|
2445 DEFSYMBOL (Qunicode);
|
|
2446 DEFSYMBOL (Qucs_4);
|
|
2447 DEFSYMBOL (Qutf_16);
|
|
2448 DEFSYMBOL (Qutf_8);
|
|
2449 DEFSYMBOL (Qutf_7);
|
|
2450
|
|
2451 DEFSYMBOL (Qneed_bom);
|
|
2452
|
|
2453 DEFSYMBOL (Qutf_16);
|
|
2454 DEFSYMBOL (Qutf_16_little_endian);
|
|
2455 DEFSYMBOL (Qutf_16_bom);
|
|
2456 DEFSYMBOL (Qutf_16_little_endian_bom);
|
985
|
2457
|
|
2458 DEFSYMBOL (Qutf_8);
|
|
2459 DEFSYMBOL (Qutf_8_bom);
|
771
|
2460 }
|
|
2461
|
|
2462 void
|
|
2463 coding_system_type_create_unicode (void)
|
|
2464 {
|
|
2465 INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA (unicode, "unicode-coding-system-p");
|
|
2466 CODING_SYSTEM_HAS_METHOD (unicode, print);
|
|
2467 CODING_SYSTEM_HAS_METHOD (unicode, convert);
|
|
2468 CODING_SYSTEM_HAS_METHOD (unicode, init_coding_stream);
|
|
2469 CODING_SYSTEM_HAS_METHOD (unicode, rewind_coding_stream);
|
|
2470 CODING_SYSTEM_HAS_METHOD (unicode, putprop);
|
|
2471 CODING_SYSTEM_HAS_METHOD (unicode, getprop);
|
|
2472
|
|
2473 INITIALIZE_DETECTOR (utf_8);
|
|
2474 DETECTOR_HAS_METHOD (utf_8, detect);
|
|
2475 INITIALIZE_DETECTOR_CATEGORY (utf_8, utf_8);
|
985
|
2476 INITIALIZE_DETECTOR_CATEGORY (utf_8, utf_8_bom);
|
771
|
2477
|
|
2478 INITIALIZE_DETECTOR (ucs_4);
|
|
2479 DETECTOR_HAS_METHOD (ucs_4, detect);
|
|
2480 INITIALIZE_DETECTOR_CATEGORY (ucs_4, ucs_4);
|
|
2481
|
|
2482 INITIALIZE_DETECTOR (utf_16);
|
|
2483 DETECTOR_HAS_METHOD (utf_16, detect);
|
|
2484 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16);
|
|
2485 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian);
|
|
2486 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16_bom);
|
|
2487 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian_bom);
|
|
2488 }
|
|
2489
|
|
2490 void
|
|
2491 reinit_coding_system_type_create_unicode (void)
|
|
2492 {
|
|
2493 REINITIALIZE_CODING_SYSTEM_TYPE (unicode);
|
|
2494 }
|
|
2495
|
|
2496 void
|
|
2497 vars_of_unicode (void)
|
|
2498 {
|
|
2499 Fprovide (intern ("unicode"));
|
|
2500
|
|
2501 #ifdef MULE
|
|
2502 staticpro (&Vlanguage_unicode_precedence_list);
|
|
2503 Vlanguage_unicode_precedence_list = Qnil;
|
|
2504
|
|
2505 staticpro (&Vdefault_unicode_precedence_list);
|
|
2506 Vdefault_unicode_precedence_list = Qnil;
|
|
2507
|
|
2508 unicode_precedence_dynarr = Dynarr_new (Lisp_Object);
|
2367
|
2509 dump_add_root_block_ptr (&unicode_precedence_dynarr,
|
771
|
2510 &lisp_object_dynarr_description);
|
2367
|
2511
|
|
2512 init_blank_unicode_tables ();
|
|
2513
|
|
2514 /* Note that the "block" we are describing is a single pointer, and hence
|
|
2515 we could potentially use dump_add_root_block_ptr(). However, given
|
|
2516 the way the descriptions are written, we couldn't use them, and would
|
|
2517 have to write new descriptions for each of the pointers below, since
|
|
2518 we would have to make use of a description with an XD_BLOCK_ARRAY
|
|
2519 in it. */
|
|
2520
|
|
2521 dump_add_root_block (&to_unicode_blank_1, sizeof (void *),
|
|
2522 to_unicode_level_1_desc_1);
|
|
2523 dump_add_root_block (&to_unicode_blank_2, sizeof (void *),
|
|
2524 to_unicode_level_2_desc_1);
|
|
2525
|
|
2526 dump_add_root_block (&from_unicode_blank_1, sizeof (void *),
|
|
2527 from_unicode_level_1_desc_1);
|
|
2528 dump_add_root_block (&from_unicode_blank_2, sizeof (void *),
|
|
2529 from_unicode_level_2_desc_1);
|
|
2530 dump_add_root_block (&from_unicode_blank_3, sizeof (void *),
|
|
2531 from_unicode_level_3_desc_1);
|
|
2532 dump_add_root_block (&from_unicode_blank_4, sizeof (void *),
|
|
2533 from_unicode_level_4_desc_1);
|
771
|
2534 #endif /* MULE */
|
|
2535 }
|