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