771
|
1 /* Code to handle Unicode conversion.
|
3025
|
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 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 NB With run-time loading, we load in init-mule-at-startup, in
|
|
150 mule-cmds.el. This is called from startup.el, which is quite late in
|
|
151 the initialization process -- but data-directory isn't set until then.
|
|
152 With dump-time loading, you still can't dump in a Japanese directory
|
|
153 (again, until we move to Unicode internally), but this is not such an
|
|
154 imposition.
|
|
155
|
|
156
|
|
157 */
|
|
158
|
771
|
159 /* #### WARNING! The current sledgehammer routines have a fundamental
|
|
160 problem in that they can't handle two characters mapping to a
|
|
161 single Unicode codepoint or vice-versa in a single charset table.
|
|
162 It's not clear there is any way to handle this and still make the
|
877
|
163 sledgehammer routines useful.
|
|
164
|
|
165 Inquiring Minds Want To Know Dept: does the above WARNING mean that
|
|
166 _if_ it happens, then it will signal error, or then it will do
|
|
167 something evil and unpredictable? Signaling an error is OK: for
|
|
168 all national standards, the national to Unicode map is an inclusion
|
|
169 (1-to-1). Any character set that does not behave that way is
|
1318
|
170 broken according to the Unicode standard.
|
|
171
|
2500
|
172 Answer: You will get an ABORT(), since the purpose of the sledgehammer
|
1318
|
173 routines is self-checking. The above problem with non-1-to-1 mapping
|
|
174 occurs in the Big5 tables, as provided by the Unicode Consortium. */
|
877
|
175
|
771
|
176 /* #define SLEDGEHAMMER_CHECK_UNICODE */
|
|
177
|
|
178 /* When MULE is not defined, we may still need some Unicode support --
|
|
179 in particular, some Windows API's always want Unicode, and the way
|
|
180 we've set up the Unicode encapsulation, we may as well go ahead and
|
|
181 always use the Unicode versions of split API's. (It would be
|
|
182 trickier to not use them, and pointless -- under NT, the ANSI API's
|
|
183 call the Unicode ones anyway, so in the case of structures, we'd be
|
|
184 converting from Unicode to ANSI structures, only to have the OS
|
|
185 convert them back.) */
|
|
186
|
|
187 Lisp_Object Qunicode;
|
4096
|
188 Lisp_Object Qutf_16, Qutf_8, Qucs_4, Qutf_7, Qutf_32;
|
771
|
189 Lisp_Object Qneed_bom;
|
|
190
|
|
191 Lisp_Object Qutf_16_little_endian, Qutf_16_bom;
|
|
192 Lisp_Object Qutf_16_little_endian_bom;
|
|
193
|
985
|
194 Lisp_Object Qutf_8_bom;
|
|
195
|
3952
|
196 /* See the Unicode FAQ, http://www.unicode.org/faq/utf_bom.html#35 for this
|
|
197 algorithm.
|
|
198
|
|
199 (They also give another, really verbose one, as part of their explanation
|
|
200 of the various planes of the encoding, but we won't use that.) */
|
|
201
|
|
202 #define UTF_16_LEAD_OFFSET (0xD800 - (0x10000 >> 10))
|
|
203 #define UTF_16_SURROGATE_OFFSET (0x10000 - (0xD800 << 10) - 0xDC00)
|
|
204
|
|
205 #define utf_16_surrogates_to_code(lead, trail) \
|
|
206 (((lead) << 10) + (trail) + UTF_16_SURROGATE_OFFSET)
|
|
207
|
|
208 #define CODE_TO_UTF_16_SURROGATES(codepoint, lead, trail) do { \
|
|
209 int __ctu16s_code = (codepoint); \
|
|
210 lead = UTF_16_LEAD_OFFSET + (__ctu16s_code >> 10); \
|
|
211 trail = 0xDC00 + (__ctu16s_code & 0x3FF); \
|
|
212 } while (0)
|
|
213
|
771
|
214 #ifdef MULE
|
|
215
|
3352
|
216 /* Using ints for to_unicode is OK (as long as they are >= 32 bits).
|
|
217 In from_unicode, we're converting from Mule characters, which means
|
|
218 that the values being converted to are only 96x96, and we can save
|
|
219 space by using shorts (signedness doesn't matter). */
|
771
|
220 static int *to_unicode_blank_1;
|
|
221 static int **to_unicode_blank_2;
|
|
222
|
|
223 static short *from_unicode_blank_1;
|
|
224 static short **from_unicode_blank_2;
|
|
225 static short ***from_unicode_blank_3;
|
|
226 static short ****from_unicode_blank_4;
|
|
227
|
1204
|
228 static const struct memory_description to_unicode_level_0_desc_1[] = {
|
771
|
229 { XD_END }
|
|
230 };
|
|
231
|
1204
|
232 static const struct sized_memory_description to_unicode_level_0_desc = {
|
|
233 sizeof (int), to_unicode_level_0_desc_1
|
771
|
234 };
|
|
235
|
1204
|
236 static const struct memory_description to_unicode_level_1_desc_1[] = {
|
2551
|
237 { XD_BLOCK_PTR, 0, 96, { &to_unicode_level_0_desc } },
|
771
|
238 { XD_END }
|
|
239 };
|
|
240
|
1204
|
241 static const struct sized_memory_description to_unicode_level_1_desc = {
|
|
242 sizeof (void *), to_unicode_level_1_desc_1
|
771
|
243 };
|
|
244
|
1204
|
245 static const struct memory_description to_unicode_description_1[] = {
|
2551
|
246 { XD_BLOCK_PTR, 1, 96, { &to_unicode_level_0_desc } },
|
|
247 { XD_BLOCK_PTR, 2, 96, { &to_unicode_level_1_desc } },
|
771
|
248 { XD_END }
|
|
249 };
|
|
250
|
|
251 /* Not static because each charset has a set of to and from tables and
|
|
252 needs to describe them to pdump. */
|
1204
|
253 const struct sized_memory_description to_unicode_description = {
|
|
254 sizeof (void *), to_unicode_description_1
|
|
255 };
|
|
256
|
2367
|
257 /* Used only for to_unicode_blank_2 */
|
|
258 static const struct memory_description to_unicode_level_2_desc_1[] = {
|
2551
|
259 { XD_BLOCK_PTR, 0, 96, { &to_unicode_level_1_desc } },
|
2367
|
260 { XD_END }
|
|
261 };
|
|
262
|
1204
|
263 static const struct memory_description from_unicode_level_0_desc_1[] = {
|
771
|
264 { XD_END }
|
|
265 };
|
|
266
|
1204
|
267 static const struct sized_memory_description from_unicode_level_0_desc = {
|
|
268 sizeof (short), from_unicode_level_0_desc_1
|
771
|
269 };
|
|
270
|
1204
|
271 static const struct memory_description from_unicode_level_1_desc_1[] = {
|
2551
|
272 { XD_BLOCK_PTR, 0, 256, { &from_unicode_level_0_desc } },
|
771
|
273 { XD_END }
|
|
274 };
|
|
275
|
1204
|
276 static const struct sized_memory_description from_unicode_level_1_desc = {
|
|
277 sizeof (void *), from_unicode_level_1_desc_1
|
771
|
278 };
|
|
279
|
1204
|
280 static const struct memory_description from_unicode_level_2_desc_1[] = {
|
2551
|
281 { XD_BLOCK_PTR, 0, 256, { &from_unicode_level_1_desc } },
|
771
|
282 { XD_END }
|
|
283 };
|
|
284
|
1204
|
285 static const struct sized_memory_description from_unicode_level_2_desc = {
|
|
286 sizeof (void *), from_unicode_level_2_desc_1
|
771
|
287 };
|
|
288
|
1204
|
289 static const struct memory_description from_unicode_level_3_desc_1[] = {
|
2551
|
290 { XD_BLOCK_PTR, 0, 256, { &from_unicode_level_2_desc } },
|
771
|
291 { XD_END }
|
|
292 };
|
|
293
|
1204
|
294 static const struct sized_memory_description from_unicode_level_3_desc = {
|
|
295 sizeof (void *), from_unicode_level_3_desc_1
|
771
|
296 };
|
|
297
|
1204
|
298 static const struct memory_description from_unicode_description_1[] = {
|
2551
|
299 { XD_BLOCK_PTR, 1, 256, { &from_unicode_level_0_desc } },
|
|
300 { XD_BLOCK_PTR, 2, 256, { &from_unicode_level_1_desc } },
|
|
301 { XD_BLOCK_PTR, 3, 256, { &from_unicode_level_2_desc } },
|
|
302 { XD_BLOCK_PTR, 4, 256, { &from_unicode_level_3_desc } },
|
771
|
303 { XD_END }
|
|
304 };
|
|
305
|
|
306 /* Not static because each charset has a set of to and from tables and
|
|
307 needs to describe them to pdump. */
|
1204
|
308 const struct sized_memory_description from_unicode_description = {
|
|
309 sizeof (void *), from_unicode_description_1
|
771
|
310 };
|
|
311
|
2367
|
312 /* Used only for from_unicode_blank_4 */
|
|
313 static const struct memory_description from_unicode_level_4_desc_1[] = {
|
2551
|
314 { XD_BLOCK_PTR, 0, 256, { &from_unicode_level_3_desc } },
|
2367
|
315 { XD_END }
|
|
316 };
|
|
317
|
771
|
318 static Lisp_Object_dynarr *unicode_precedence_dynarr;
|
|
319
|
1204
|
320 static const struct memory_description lod_description_1[] = {
|
|
321 XD_DYNARR_DESC (Lisp_Object_dynarr, &lisp_object_description),
|
771
|
322 { XD_END }
|
|
323 };
|
|
324
|
1204
|
325 static const struct sized_memory_description lisp_object_dynarr_description = {
|
771
|
326 sizeof (Lisp_Object_dynarr),
|
|
327 lod_description_1
|
|
328 };
|
|
329
|
|
330 Lisp_Object Vlanguage_unicode_precedence_list;
|
|
331 Lisp_Object Vdefault_unicode_precedence_list;
|
|
332
|
|
333 Lisp_Object Qignore_first_column;
|
|
334
|
3439
|
335 Lisp_Object Vcurrent_jit_charset;
|
|
336 Lisp_Object Qlast_allocated_character;
|
|
337 Lisp_Object Qccl_encode_to_ucs_2;
|
|
338
|
771
|
339
|
|
340 /************************************************************************/
|
|
341 /* Unicode implementation */
|
|
342 /************************************************************************/
|
|
343
|
|
344 #define BREAKUP_UNICODE_CODE(val, u1, u2, u3, u4, levels) \
|
|
345 do { \
|
|
346 int buc_val = (val); \
|
|
347 \
|
|
348 (u1) = buc_val >> 24; \
|
|
349 (u2) = (buc_val >> 16) & 255; \
|
|
350 (u3) = (buc_val >> 8) & 255; \
|
|
351 (u4) = buc_val & 255; \
|
|
352 (levels) = (buc_val <= 0xFF ? 1 : \
|
|
353 buc_val <= 0xFFFF ? 2 : \
|
|
354 buc_val <= 0xFFFFFF ? 3 : \
|
|
355 4); \
|
|
356 } while (0)
|
|
357
|
|
358 static void
|
|
359 init_blank_unicode_tables (void)
|
|
360 {
|
|
361 int i;
|
|
362
|
|
363 from_unicode_blank_1 = xnew_array (short, 256);
|
|
364 from_unicode_blank_2 = xnew_array (short *, 256);
|
|
365 from_unicode_blank_3 = xnew_array (short **, 256);
|
|
366 from_unicode_blank_4 = xnew_array (short ***, 256);
|
|
367 for (i = 0; i < 256; i++)
|
|
368 {
|
877
|
369 /* #### IMWTK: Why does using -1 here work? Simply because there are
|
1318
|
370 no existing 96x96 charsets?
|
|
371
|
|
372 Answer: I don't understand the concern. -1 indicates there is no
|
|
373 entry for this particular codepoint, which is always the case for
|
|
374 blank tables. */
|
771
|
375 from_unicode_blank_1[i] = (short) -1;
|
|
376 from_unicode_blank_2[i] = from_unicode_blank_1;
|
|
377 from_unicode_blank_3[i] = from_unicode_blank_2;
|
|
378 from_unicode_blank_4[i] = from_unicode_blank_3;
|
|
379 }
|
|
380
|
|
381 to_unicode_blank_1 = xnew_array (int, 96);
|
|
382 to_unicode_blank_2 = xnew_array (int *, 96);
|
|
383 for (i = 0; i < 96; i++)
|
|
384 {
|
877
|
385 /* Here -1 is guaranteed OK. */
|
771
|
386 to_unicode_blank_1[i] = -1;
|
|
387 to_unicode_blank_2[i] = to_unicode_blank_1;
|
|
388 }
|
|
389 }
|
|
390
|
|
391 static void *
|
|
392 create_new_from_unicode_table (int level)
|
|
393 {
|
|
394 switch (level)
|
|
395 {
|
|
396 /* WARNING: If you are thinking of compressing these, keep in
|
|
397 mind that sizeof (short) does not equal sizeof (short *). */
|
|
398 case 1:
|
|
399 {
|
|
400 short *newtab = xnew_array (short, 256);
|
|
401 memcpy (newtab, from_unicode_blank_1, 256 * sizeof (short));
|
|
402 return newtab;
|
|
403 }
|
|
404 case 2:
|
|
405 {
|
|
406 short **newtab = xnew_array (short *, 256);
|
|
407 memcpy (newtab, from_unicode_blank_2, 256 * sizeof (short *));
|
|
408 return newtab;
|
|
409 }
|
|
410 case 3:
|
|
411 {
|
|
412 short ***newtab = xnew_array (short **, 256);
|
|
413 memcpy (newtab, from_unicode_blank_3, 256 * sizeof (short **));
|
|
414 return newtab;
|
|
415 }
|
|
416 case 4:
|
|
417 {
|
|
418 short ****newtab = xnew_array (short ***, 256);
|
|
419 memcpy (newtab, from_unicode_blank_4, 256 * sizeof (short ***));
|
|
420 return newtab;
|
|
421 }
|
|
422 default:
|
2500
|
423 ABORT ();
|
771
|
424 return 0;
|
|
425 }
|
|
426 }
|
|
427
|
877
|
428 /* Allocate and blank the tables.
|
1318
|
429 Loading them up is done by load-unicode-mapping-table. */
|
771
|
430 void
|
|
431 init_charset_unicode_tables (Lisp_Object charset)
|
|
432 {
|
|
433 if (XCHARSET_DIMENSION (charset) == 1)
|
|
434 {
|
|
435 int *to_table = xnew_array (int, 96);
|
|
436 memcpy (to_table, to_unicode_blank_1, 96 * sizeof (int));
|
|
437 XCHARSET_TO_UNICODE_TABLE (charset) = to_table;
|
|
438 }
|
|
439 else
|
|
440 {
|
|
441 int **to_table = xnew_array (int *, 96);
|
|
442 memcpy (to_table, to_unicode_blank_2, 96 * sizeof (int *));
|
|
443 XCHARSET_TO_UNICODE_TABLE (charset) = to_table;
|
|
444 }
|
|
445
|
|
446 {
|
2367
|
447 XCHARSET_FROM_UNICODE_TABLE (charset) =
|
|
448 create_new_from_unicode_table (1);
|
771
|
449 XCHARSET_FROM_UNICODE_LEVELS (charset) = 1;
|
|
450 }
|
|
451 }
|
|
452
|
|
453 static void
|
|
454 free_from_unicode_table (void *table, int level)
|
|
455 {
|
|
456 int i;
|
|
457
|
|
458 switch (level)
|
|
459 {
|
|
460 case 2:
|
|
461 {
|
|
462 short **tab = (short **) table;
|
|
463 for (i = 0; i < 256; i++)
|
|
464 {
|
|
465 if (tab[i] != from_unicode_blank_1)
|
|
466 free_from_unicode_table (tab[i], 1);
|
|
467 }
|
|
468 break;
|
|
469 }
|
|
470 case 3:
|
|
471 {
|
|
472 short ***tab = (short ***) table;
|
|
473 for (i = 0; i < 256; i++)
|
|
474 {
|
|
475 if (tab[i] != from_unicode_blank_2)
|
|
476 free_from_unicode_table (tab[i], 2);
|
|
477 }
|
|
478 break;
|
|
479 }
|
|
480 case 4:
|
|
481 {
|
|
482 short ****tab = (short ****) table;
|
|
483 for (i = 0; i < 256; i++)
|
|
484 {
|
|
485 if (tab[i] != from_unicode_blank_3)
|
|
486 free_from_unicode_table (tab[i], 3);
|
|
487 }
|
|
488 break;
|
|
489 }
|
|
490 }
|
|
491
|
1726
|
492 xfree (table, void *);
|
771
|
493 }
|
|
494
|
|
495 static void
|
|
496 free_to_unicode_table (void *table, int level)
|
|
497 {
|
|
498 if (level == 2)
|
|
499 {
|
|
500 int i;
|
|
501 int **tab = (int **) table;
|
|
502
|
|
503 for (i = 0; i < 96; i++)
|
|
504 {
|
|
505 if (tab[i] != to_unicode_blank_1)
|
|
506 free_to_unicode_table (tab[i], 1);
|
|
507 }
|
|
508 }
|
|
509
|
1726
|
510 xfree (table, void *);
|
771
|
511 }
|
|
512
|
|
513 void
|
|
514 free_charset_unicode_tables (Lisp_Object charset)
|
|
515 {
|
|
516 free_to_unicode_table (XCHARSET_TO_UNICODE_TABLE (charset),
|
|
517 XCHARSET_DIMENSION (charset));
|
|
518 free_from_unicode_table (XCHARSET_FROM_UNICODE_TABLE (charset),
|
|
519 XCHARSET_FROM_UNICODE_LEVELS (charset));
|
|
520 }
|
|
521
|
|
522 #ifdef MEMORY_USAGE_STATS
|
|
523
|
|
524 static Bytecount
|
|
525 compute_from_unicode_table_size_1 (void *table, int level,
|
|
526 struct overhead_stats *stats)
|
|
527 {
|
|
528 int i;
|
|
529 Bytecount size = 0;
|
|
530
|
|
531 switch (level)
|
|
532 {
|
|
533 case 2:
|
|
534 {
|
|
535 short **tab = (short **) table;
|
|
536 for (i = 0; i < 256; i++)
|
|
537 {
|
|
538 if (tab[i] != from_unicode_blank_1)
|
|
539 size += compute_from_unicode_table_size_1 (tab[i], 1, stats);
|
|
540 }
|
|
541 break;
|
|
542 }
|
|
543 case 3:
|
|
544 {
|
|
545 short ***tab = (short ***) table;
|
|
546 for (i = 0; i < 256; i++)
|
|
547 {
|
|
548 if (tab[i] != from_unicode_blank_2)
|
|
549 size += compute_from_unicode_table_size_1 (tab[i], 2, stats);
|
|
550 }
|
|
551 break;
|
|
552 }
|
|
553 case 4:
|
|
554 {
|
|
555 short ****tab = (short ****) table;
|
|
556 for (i = 0; i < 256; i++)
|
|
557 {
|
|
558 if (tab[i] != from_unicode_blank_3)
|
|
559 size += compute_from_unicode_table_size_1 (tab[i], 3, stats);
|
|
560 }
|
|
561 break;
|
|
562 }
|
|
563 }
|
|
564
|
3024
|
565 size += malloced_storage_size (table,
|
771
|
566 256 * (level == 1 ? sizeof (short) :
|
|
567 sizeof (void *)),
|
|
568 stats);
|
|
569 return size;
|
|
570 }
|
|
571
|
|
572 static Bytecount
|
|
573 compute_to_unicode_table_size_1 (void *table, int level,
|
|
574 struct overhead_stats *stats)
|
|
575 {
|
|
576 Bytecount size = 0;
|
|
577
|
|
578 if (level == 2)
|
|
579 {
|
|
580 int i;
|
|
581 int **tab = (int **) table;
|
|
582
|
|
583 for (i = 0; i < 96; i++)
|
|
584 {
|
|
585 if (tab[i] != to_unicode_blank_1)
|
|
586 size += compute_to_unicode_table_size_1 (tab[i], 1, stats);
|
|
587 }
|
|
588 }
|
|
589
|
3024
|
590 size += malloced_storage_size (table,
|
771
|
591 96 * (level == 1 ? sizeof (int) :
|
|
592 sizeof (void *)),
|
|
593 stats);
|
|
594 return size;
|
|
595 }
|
|
596
|
|
597 Bytecount
|
|
598 compute_from_unicode_table_size (Lisp_Object charset,
|
|
599 struct overhead_stats *stats)
|
|
600 {
|
|
601 return (compute_from_unicode_table_size_1
|
|
602 (XCHARSET_FROM_UNICODE_TABLE (charset),
|
|
603 XCHARSET_FROM_UNICODE_LEVELS (charset),
|
|
604 stats));
|
|
605 }
|
|
606
|
|
607 Bytecount
|
|
608 compute_to_unicode_table_size (Lisp_Object charset,
|
|
609 struct overhead_stats *stats)
|
|
610 {
|
|
611 return (compute_to_unicode_table_size_1
|
|
612 (XCHARSET_TO_UNICODE_TABLE (charset),
|
|
613 XCHARSET_DIMENSION (charset),
|
|
614 stats));
|
|
615 }
|
|
616
|
|
617 #endif
|
|
618
|
|
619 #ifdef SLEDGEHAMMER_CHECK_UNICODE
|
|
620
|
|
621 /* "Sledgehammer checks" are checks that verify the self-consistency
|
|
622 of an entire structure every time a change is about to be made or
|
|
623 has been made to the structure. Not fast but a pretty much
|
|
624 sure-fire way of flushing out any incorrectnesses in the algorithms
|
|
625 that create the structure.
|
|
626
|
|
627 Checking only after a change has been made will speed things up by
|
|
628 a factor of 2, but it doesn't absolutely prove that the code just
|
|
629 checked caused the problem; perhaps it happened elsewhere, either
|
|
630 in some code you forgot to sledgehammer check or as a result of
|
|
631 data corruption. */
|
|
632
|
|
633 static void
|
|
634 assert_not_any_blank_table (void *tab)
|
|
635 {
|
|
636 assert (tab != from_unicode_blank_1);
|
|
637 assert (tab != from_unicode_blank_2);
|
|
638 assert (tab != from_unicode_blank_3);
|
|
639 assert (tab != from_unicode_blank_4);
|
|
640 assert (tab != to_unicode_blank_1);
|
|
641 assert (tab != to_unicode_blank_2);
|
|
642 assert (tab);
|
|
643 }
|
|
644
|
|
645 static void
|
|
646 sledgehammer_check_from_table (Lisp_Object charset, void *table, int level,
|
|
647 int codetop)
|
|
648 {
|
|
649 int i;
|
|
650
|
|
651 switch (level)
|
|
652 {
|
|
653 case 1:
|
|
654 {
|
|
655 short *tab = (short *) table;
|
|
656 for (i = 0; i < 256; i++)
|
|
657 {
|
|
658 if (tab[i] != -1)
|
|
659 {
|
|
660 Lisp_Object char_charset;
|
|
661 int c1, c2;
|
|
662
|
867
|
663 assert (valid_ichar_p (tab[i]));
|
|
664 BREAKUP_ICHAR (tab[i], char_charset, c1, c2);
|
771
|
665 assert (EQ (charset, char_charset));
|
|
666 if (XCHARSET_DIMENSION (charset) == 1)
|
|
667 {
|
|
668 int *to_table =
|
|
669 (int *) XCHARSET_TO_UNICODE_TABLE (charset);
|
|
670 assert_not_any_blank_table (to_table);
|
|
671 assert (to_table[c1 - 32] == (codetop << 8) + i);
|
|
672 }
|
|
673 else
|
|
674 {
|
|
675 int **to_table =
|
|
676 (int **) XCHARSET_TO_UNICODE_TABLE (charset);
|
|
677 assert_not_any_blank_table (to_table);
|
|
678 assert_not_any_blank_table (to_table[c1 - 32]);
|
|
679 assert (to_table[c1 - 32][c2 - 32] == (codetop << 8) + i);
|
|
680 }
|
|
681 }
|
|
682 }
|
|
683 break;
|
|
684 }
|
|
685 case 2:
|
|
686 {
|
|
687 short **tab = (short **) table;
|
|
688 for (i = 0; i < 256; i++)
|
|
689 {
|
|
690 if (tab[i] != from_unicode_blank_1)
|
|
691 sledgehammer_check_from_table (charset, tab[i], 1,
|
|
692 (codetop << 8) + i);
|
|
693 }
|
|
694 break;
|
|
695 }
|
|
696 case 3:
|
|
697 {
|
|
698 short ***tab = (short ***) table;
|
|
699 for (i = 0; i < 256; i++)
|
|
700 {
|
|
701 if (tab[i] != from_unicode_blank_2)
|
|
702 sledgehammer_check_from_table (charset, tab[i], 2,
|
|
703 (codetop << 8) + i);
|
|
704 }
|
|
705 break;
|
|
706 }
|
|
707 case 4:
|
|
708 {
|
|
709 short ****tab = (short ****) table;
|
|
710 for (i = 0; i < 256; i++)
|
|
711 {
|
|
712 if (tab[i] != from_unicode_blank_3)
|
|
713 sledgehammer_check_from_table (charset, tab[i], 3,
|
|
714 (codetop << 8) + i);
|
|
715 }
|
|
716 break;
|
|
717 }
|
|
718 default:
|
2500
|
719 ABORT ();
|
771
|
720 }
|
|
721 }
|
|
722
|
|
723 static void
|
|
724 sledgehammer_check_to_table (Lisp_Object charset, void *table, int level,
|
|
725 int codetop)
|
|
726 {
|
|
727 int i;
|
|
728
|
|
729 switch (level)
|
|
730 {
|
|
731 case 1:
|
|
732 {
|
|
733 int *tab = (int *) table;
|
|
734
|
|
735 if (XCHARSET_CHARS (charset) == 94)
|
|
736 {
|
|
737 assert (tab[0] == -1);
|
|
738 assert (tab[95] == -1);
|
|
739 }
|
|
740
|
|
741 for (i = 0; i < 96; i++)
|
|
742 {
|
|
743 if (tab[i] != -1)
|
|
744 {
|
|
745 int u4, u3, u2, u1, levels;
|
867
|
746 Ichar ch;
|
|
747 Ichar this_ch;
|
771
|
748 short val;
|
|
749 void *frtab = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
750
|
|
751 if (XCHARSET_DIMENSION (charset) == 1)
|
867
|
752 this_ch = make_ichar (charset, i + 32, 0);
|
771
|
753 else
|
867
|
754 this_ch = make_ichar (charset, codetop + 32, i + 32);
|
771
|
755
|
|
756 assert (tab[i] >= 0);
|
|
757 BREAKUP_UNICODE_CODE (tab[i], u4, u3, u2, u1, levels);
|
|
758 assert (levels <= XCHARSET_FROM_UNICODE_LEVELS (charset));
|
|
759
|
|
760 switch (XCHARSET_FROM_UNICODE_LEVELS (charset))
|
|
761 {
|
|
762 case 1: val = ((short *) frtab)[u1]; break;
|
|
763 case 2: val = ((short **) frtab)[u2][u1]; break;
|
|
764 case 3: val = ((short ***) frtab)[u3][u2][u1]; break;
|
|
765 case 4: val = ((short ****) frtab)[u4][u3][u2][u1]; break;
|
2500
|
766 default: ABORT ();
|
771
|
767 }
|
|
768
|
867
|
769 ch = make_ichar (charset, val >> 8, val & 0xFF);
|
771
|
770 assert (ch == this_ch);
|
|
771
|
|
772 switch (XCHARSET_FROM_UNICODE_LEVELS (charset))
|
|
773 {
|
|
774 case 4:
|
|
775 assert_not_any_blank_table (frtab);
|
|
776 frtab = ((short ****) frtab)[u4];
|
|
777 /* fall through */
|
|
778 case 3:
|
|
779 assert_not_any_blank_table (frtab);
|
|
780 frtab = ((short ***) frtab)[u3];
|
|
781 /* fall through */
|
|
782 case 2:
|
|
783 assert_not_any_blank_table (frtab);
|
|
784 frtab = ((short **) frtab)[u2];
|
|
785 /* fall through */
|
|
786 case 1:
|
|
787 assert_not_any_blank_table (frtab);
|
|
788 break;
|
2500
|
789 default: ABORT ();
|
771
|
790 }
|
|
791 }
|
|
792 }
|
|
793 break;
|
|
794 }
|
|
795 case 2:
|
|
796 {
|
|
797 int **tab = (int **) table;
|
|
798
|
|
799 if (XCHARSET_CHARS (charset) == 94)
|
|
800 {
|
|
801 assert (tab[0] == to_unicode_blank_1);
|
|
802 assert (tab[95] == to_unicode_blank_1);
|
|
803 }
|
|
804
|
|
805 for (i = 0; i < 96; i++)
|
|
806 {
|
|
807 if (tab[i] != to_unicode_blank_1)
|
|
808 sledgehammer_check_to_table (charset, tab[i], 1, i);
|
|
809 }
|
|
810 break;
|
|
811 }
|
|
812 default:
|
2500
|
813 ABORT ();
|
771
|
814 }
|
|
815 }
|
|
816
|
|
817 static void
|
|
818 sledgehammer_check_unicode_tables (Lisp_Object charset)
|
|
819 {
|
|
820 /* verify that the blank tables have not been modified */
|
|
821 int i;
|
|
822 int from_level = XCHARSET_FROM_UNICODE_LEVELS (charset);
|
|
823 int to_level = XCHARSET_FROM_UNICODE_LEVELS (charset);
|
|
824
|
|
825 for (i = 0; i < 256; i++)
|
|
826 {
|
|
827 assert (from_unicode_blank_1[i] == (short) -1);
|
|
828 assert (from_unicode_blank_2[i] == from_unicode_blank_1);
|
|
829 assert (from_unicode_blank_3[i] == from_unicode_blank_2);
|
|
830 assert (from_unicode_blank_4[i] == from_unicode_blank_3);
|
|
831 }
|
|
832
|
|
833 for (i = 0; i < 96; i++)
|
|
834 {
|
|
835 assert (to_unicode_blank_1[i] == -1);
|
|
836 assert (to_unicode_blank_2[i] == to_unicode_blank_1);
|
|
837 }
|
|
838
|
|
839 assert (from_level >= 1 && from_level <= 4);
|
|
840
|
|
841 sledgehammer_check_from_table (charset,
|
|
842 XCHARSET_FROM_UNICODE_TABLE (charset),
|
|
843 from_level, 0);
|
|
844
|
|
845 sledgehammer_check_to_table (charset,
|
|
846 XCHARSET_TO_UNICODE_TABLE (charset),
|
|
847 XCHARSET_DIMENSION (charset), 0);
|
|
848 }
|
|
849
|
|
850 #endif /* SLEDGEHAMMER_CHECK_UNICODE */
|
|
851
|
|
852 static void
|
867
|
853 set_unicode_conversion (Ichar chr, int code)
|
771
|
854 {
|
|
855 Lisp_Object charset;
|
|
856 int c1, c2;
|
|
857
|
867
|
858 BREAKUP_ICHAR (chr, charset, c1, c2);
|
771
|
859
|
877
|
860 /* I tried an assert on code > 255 || chr == code, but that fails because
|
|
861 Mule gives many Latin characters separate code points for different
|
|
862 ISO 8859 coded character sets. Obvious in hindsight.... */
|
|
863 assert (!EQ (charset, Vcharset_ascii) || chr == code);
|
|
864 assert (!EQ (charset, Vcharset_latin_iso8859_1) || chr == code);
|
|
865 assert (!EQ (charset, Vcharset_control_1) || chr == code);
|
|
866
|
|
867 /* This assert is needed because it is simply unimplemented. */
|
771
|
868 assert (!EQ (charset, Vcharset_composite));
|
|
869
|
|
870 #ifdef SLEDGEHAMMER_CHECK_UNICODE
|
|
871 sledgehammer_check_unicode_tables (charset);
|
|
872 #endif
|
|
873
|
2704
|
874 if (EQ(charset, Vcharset_ascii) || EQ(charset, Vcharset_control_1))
|
|
875 return;
|
|
876
|
771
|
877 /* First, the char -> unicode translation */
|
|
878
|
|
879 if (XCHARSET_DIMENSION (charset) == 1)
|
|
880 {
|
|
881 int *to_table = (int *) XCHARSET_TO_UNICODE_TABLE (charset);
|
|
882 to_table[c1 - 32] = code;
|
|
883 }
|
|
884 else
|
|
885 {
|
|
886 int **to_table_2 = (int **) XCHARSET_TO_UNICODE_TABLE (charset);
|
|
887 int *to_table_1;
|
|
888
|
|
889 assert (XCHARSET_DIMENSION (charset) == 2);
|
|
890 to_table_1 = to_table_2[c1 - 32];
|
|
891 if (to_table_1 == to_unicode_blank_1)
|
|
892 {
|
|
893 to_table_1 = xnew_array (int, 96);
|
|
894 memcpy (to_table_1, to_unicode_blank_1, 96 * sizeof (int));
|
|
895 to_table_2[c1 - 32] = to_table_1;
|
|
896 }
|
|
897 to_table_1[c2 - 32] = code;
|
|
898 }
|
|
899
|
|
900 /* Then, unicode -> char: much harder */
|
|
901
|
|
902 {
|
|
903 int charset_levels;
|
|
904 int u4, u3, u2, u1;
|
|
905 int code_levels;
|
|
906 BREAKUP_UNICODE_CODE (code, u4, u3, u2, u1, code_levels);
|
|
907
|
|
908 charset_levels = XCHARSET_FROM_UNICODE_LEVELS (charset);
|
|
909
|
|
910 /* Make sure the charset's tables have at least as many levels as
|
|
911 the code point has: Note that the charset is guaranteed to have
|
|
912 at least one level, because it was created that way */
|
|
913 if (charset_levels < code_levels)
|
|
914 {
|
|
915 int i;
|
|
916
|
|
917 assert (charset_levels > 0);
|
|
918 for (i = 2; i <= code_levels; i++)
|
|
919 {
|
|
920 if (charset_levels < i)
|
|
921 {
|
|
922 void *old_table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
923 void *table = create_new_from_unicode_table (i);
|
|
924 XCHARSET_FROM_UNICODE_TABLE (charset) = table;
|
|
925
|
|
926 switch (i)
|
|
927 {
|
|
928 case 2:
|
|
929 ((short **) table)[0] = (short *) old_table;
|
|
930 break;
|
|
931 case 3:
|
|
932 ((short ***) table)[0] = (short **) old_table;
|
|
933 break;
|
|
934 case 4:
|
|
935 ((short ****) table)[0] = (short ***) old_table;
|
|
936 break;
|
2500
|
937 default: ABORT ();
|
771
|
938 }
|
|
939 }
|
|
940 }
|
|
941
|
|
942 charset_levels = code_levels;
|
|
943 XCHARSET_FROM_UNICODE_LEVELS (charset) = code_levels;
|
|
944 }
|
|
945
|
|
946 /* Now, make sure there is a non-default table at each level */
|
|
947 {
|
|
948 int i;
|
|
949 void *table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
950
|
|
951 for (i = charset_levels; i >= 2; i--)
|
|
952 {
|
|
953 switch (i)
|
|
954 {
|
|
955 case 4:
|
|
956 if (((short ****) table)[u4] == from_unicode_blank_3)
|
|
957 ((short ****) table)[u4] =
|
|
958 ((short ***) create_new_from_unicode_table (3));
|
|
959 table = ((short ****) table)[u4];
|
|
960 break;
|
|
961 case 3:
|
|
962 if (((short ***) table)[u3] == from_unicode_blank_2)
|
|
963 ((short ***) table)[u3] =
|
|
964 ((short **) create_new_from_unicode_table (2));
|
|
965 table = ((short ***) table)[u3];
|
|
966 break;
|
|
967 case 2:
|
|
968 if (((short **) table)[u2] == from_unicode_blank_1)
|
|
969 ((short **) table)[u2] =
|
|
970 ((short *) create_new_from_unicode_table (1));
|
|
971 table = ((short **) table)[u2];
|
|
972 break;
|
2500
|
973 default: ABORT ();
|
771
|
974 }
|
|
975 }
|
|
976 }
|
|
977
|
|
978 /* Finally, set the character */
|
|
979
|
|
980 {
|
|
981 void *table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
982 switch (charset_levels)
|
|
983 {
|
|
984 case 1: ((short *) table)[u1] = (c1 << 8) + c2; break;
|
|
985 case 2: ((short **) table)[u2][u1] = (c1 << 8) + c2; break;
|
|
986 case 3: ((short ***) table)[u3][u2][u1] = (c1 << 8) + c2; break;
|
|
987 case 4: ((short ****) table)[u4][u3][u2][u1] = (c1 << 8) + c2; break;
|
2500
|
988 default: ABORT ();
|
771
|
989 }
|
|
990 }
|
|
991 }
|
|
992
|
|
993 #ifdef SLEDGEHAMMER_CHECK_UNICODE
|
|
994 sledgehammer_check_unicode_tables (charset);
|
|
995 #endif
|
|
996 }
|
|
997
|
788
|
998 int
|
867
|
999 ichar_to_unicode (Ichar chr)
|
771
|
1000 {
|
|
1001 Lisp_Object charset;
|
|
1002 int c1, c2;
|
|
1003
|
867
|
1004 type_checking_assert (valid_ichar_p (chr));
|
877
|
1005 /* This shortcut depends on the representation of an Ichar, see text.c. */
|
771
|
1006 if (chr < 256)
|
|
1007 return (int) chr;
|
|
1008
|
867
|
1009 BREAKUP_ICHAR (chr, charset, c1, c2);
|
771
|
1010 if (EQ (charset, Vcharset_composite))
|
|
1011 return -1; /* #### don't know how to handle */
|
|
1012 else if (XCHARSET_DIMENSION (charset) == 1)
|
|
1013 return ((int *) XCHARSET_TO_UNICODE_TABLE (charset))[c1 - 32];
|
|
1014 else
|
|
1015 return ((int **) XCHARSET_TO_UNICODE_TABLE (charset))[c1 - 32][c2 - 32];
|
|
1016 }
|
|
1017
|
867
|
1018 static Ichar
|
3439
|
1019 get_free_codepoint(Lisp_Object charset)
|
|
1020 {
|
|
1021 Lisp_Object name = Fcharset_name(charset);
|
|
1022 Lisp_Object zeichen = Fget(name, Qlast_allocated_character, Qnil);
|
|
1023 Ichar res;
|
|
1024
|
|
1025 /* Only allow this with the 96x96 character sets we are using for
|
|
1026 temporary Unicode support. */
|
|
1027 assert(2 == XCHARSET_DIMENSION(charset) && 96 == XCHARSET_CHARS(charset));
|
|
1028
|
|
1029 if (!NILP(zeichen))
|
|
1030 {
|
|
1031 int c1, c2;
|
|
1032
|
|
1033 BREAKUP_ICHAR(XCHAR(zeichen), charset, c1, c2);
|
|
1034
|
|
1035 if (127 == c1 && 127 == c2)
|
|
1036 {
|
|
1037 /* We've already used the hightest-numbered character in this
|
|
1038 set--tell our caller to create another. */
|
|
1039 return -1;
|
|
1040 }
|
|
1041
|
|
1042 if (127 == c2)
|
|
1043 {
|
|
1044 ++c1;
|
|
1045 c2 = 0x20;
|
|
1046 }
|
|
1047 else
|
|
1048 {
|
|
1049 ++c2;
|
|
1050 }
|
|
1051
|
|
1052 res = make_ichar(charset, c1, c2);
|
|
1053 Fput(name, Qlast_allocated_character, make_char(res));
|
|
1054 }
|
|
1055 else
|
|
1056 {
|
|
1057 res = make_ichar(charset, 32, 32);
|
|
1058 Fput(name, Qlast_allocated_character, make_char(res));
|
|
1059 }
|
|
1060 return res;
|
|
1061 }
|
|
1062
|
|
1063 /* The just-in-time creation of XEmacs characters that correspond to unknown
|
|
1064 Unicode code points happens when:
|
|
1065
|
|
1066 1. The lookup would otherwise fail.
|
|
1067
|
|
1068 2. The charsets array is the nil or the default.
|
|
1069
|
|
1070 If there are no free code points in the just-in-time Unicode character
|
|
1071 set, and the charsets array is the default unicode precedence list,
|
|
1072 create a new just-in-time Unicode character set, add it at the end of the
|
|
1073 unicode precedence list, create the XEmacs character in that character
|
|
1074 set, and return it. */
|
|
1075
|
|
1076 static Ichar
|
877
|
1077 unicode_to_ichar (int code, Lisp_Object_dynarr *charsets)
|
771
|
1078 {
|
|
1079 int u1, u2, u3, u4;
|
|
1080 int code_levels;
|
|
1081 int i;
|
|
1082 int n = Dynarr_length (charsets);
|
3439
|
1083 static int number_of_jit_charsets;
|
|
1084 static Ascbyte last_jit_charset_final;
|
771
|
1085
|
|
1086 type_checking_assert (code >= 0);
|
877
|
1087 /* This shortcut depends on the representation of an Ichar, see text.c.
|
|
1088 Note that it may _not_ be extended to U+00A0 to U+00FF (many ISO 8859
|
893
|
1089 coded character sets have points that map into that region, so this
|
|
1090 function is many-valued). */
|
877
|
1091 if (code < 0xA0)
|
867
|
1092 return (Ichar) code;
|
771
|
1093
|
|
1094 BREAKUP_UNICODE_CODE (code, u4, u3, u2, u1, code_levels);
|
|
1095
|
|
1096 for (i = 0; i < n; i++)
|
|
1097 {
|
|
1098 Lisp_Object charset = Dynarr_at (charsets, i);
|
|
1099 int charset_levels = XCHARSET_FROM_UNICODE_LEVELS (charset);
|
|
1100 if (charset_levels >= code_levels)
|
|
1101 {
|
|
1102 void *table = XCHARSET_FROM_UNICODE_TABLE (charset);
|
|
1103 short retval;
|
|
1104
|
|
1105 switch (charset_levels)
|
|
1106 {
|
|
1107 case 1: retval = ((short *) table)[u1]; break;
|
|
1108 case 2: retval = ((short **) table)[u2][u1]; break;
|
|
1109 case 3: retval = ((short ***) table)[u3][u2][u1]; break;
|
|
1110 case 4: retval = ((short ****) table)[u4][u3][u2][u1]; break;
|
2500
|
1111 default: ABORT (); retval = 0;
|
771
|
1112 }
|
|
1113
|
|
1114 if (retval != -1)
|
867
|
1115 return make_ichar (charset, retval >> 8, retval & 0xFF);
|
771
|
1116 }
|
|
1117 }
|
3439
|
1118
|
|
1119 /* Only do the magic just-in-time assignment if we're using the default
|
|
1120 list. */
|
|
1121 if (unicode_precedence_dynarr == charsets)
|
|
1122 {
|
|
1123 if (NILP (Vcurrent_jit_charset) ||
|
|
1124 (-1 == (i = get_free_codepoint(Vcurrent_jit_charset))))
|
|
1125 {
|
3452
|
1126 Ibyte setname[32];
|
3439
|
1127 Lisp_Object charset_descr = build_string
|
|
1128 ("Mule charset for otherwise unknown Unicode code points.");
|
771
|
1129
|
3659
|
1130 struct gcpro gcpro1;
|
3439
|
1131
|
|
1132 if ('\0' == last_jit_charset_final)
|
|
1133 {
|
|
1134 /* This final byte shit is, umm, not that cool. */
|
|
1135 last_jit_charset_final = 0x30;
|
|
1136 }
|
|
1137
|
3452
|
1138 /* Assertion added partly because our Win32 layer doesn't
|
|
1139 support snprintf; with this, we're sure it won't overflow
|
|
1140 the buffer. */
|
|
1141 assert(100 > number_of_jit_charsets);
|
|
1142
|
|
1143 qxesprintf(setname, "jit-ucs-charset-%d", number_of_jit_charsets++);
|
3439
|
1144
|
|
1145 /* Aside: GCPROing here would be overkill according to the FSF's
|
|
1146 philosophy. make-charset cannot currently GC, but is intended
|
|
1147 to be called from Lisp, with its arguments protected by the
|
|
1148 Lisp reader. We GCPRO in case it GCs in the future and no-one
|
|
1149 checks all the C callers. */
|
|
1150
|
3659
|
1151 GCPRO1 (charset_descr);
|
3439
|
1152 Vcurrent_jit_charset = Fmake_charset
|
3452
|
1153 (intern((const CIbyte *)setname), charset_descr,
|
3439
|
1154 /* Set encode-as-utf-8 to t, to have this character set written
|
|
1155 using UTF-8 escapes in escape-quoted and ctext. This
|
|
1156 sidesteps the fact that our internal character -> Unicode
|
|
1157 mapping is not stable from one invocation to the next. */
|
|
1158 nconc2 (list2(Qencode_as_utf_8, Qt),
|
|
1159 nconc2 (list6(Qcolumns, make_int(1), Qchars, make_int(96),
|
|
1160 Qdimension, make_int(2)),
|
3659
|
1161 list6(Qregistries, Qunicode_registries,
|
3439
|
1162 Qfinal, make_char(last_jit_charset_final++),
|
|
1163 /* This CCL program is initialised in
|
|
1164 unicode.el. */
|
|
1165 Qccl_program, Qccl_encode_to_ucs_2))));
|
|
1166 UNGCPRO;
|
|
1167
|
|
1168 i = get_free_codepoint(Vcurrent_jit_charset);
|
|
1169 }
|
|
1170
|
|
1171 if (-1 != i)
|
|
1172 {
|
|
1173 set_unicode_conversion((Ichar)i, code);
|
|
1174 /* No need to add the charset to the end of the list; it's done
|
|
1175 automatically. */
|
|
1176 }
|
|
1177 }
|
|
1178 return (Ichar) i;
|
771
|
1179 }
|
|
1180
|
877
|
1181 /* Add charsets to precedence list.
|
|
1182 LIST must be a list of charsets. Charsets which are in the list more
|
|
1183 than once are given the precedence implied by their earliest appearance.
|
|
1184 Later appearances are ignored. */
|
771
|
1185 static void
|
|
1186 add_charsets_to_precedence_list (Lisp_Object list, int *lbs,
|
|
1187 Lisp_Object_dynarr *dynarr)
|
|
1188 {
|
|
1189 {
|
|
1190 EXTERNAL_LIST_LOOP_2 (elt, list)
|
|
1191 {
|
|
1192 Lisp_Object charset = Fget_charset (elt);
|
778
|
1193 int lb = XCHARSET_LEADING_BYTE (charset);
|
771
|
1194 if (lbs[lb - MIN_LEADING_BYTE] == 0)
|
|
1195 {
|
877
|
1196 Dynarr_add (dynarr, charset);
|
771
|
1197 lbs[lb - MIN_LEADING_BYTE] = 1;
|
|
1198 }
|
|
1199 }
|
|
1200 }
|
|
1201 }
|
|
1202
|
877
|
1203 /* Rebuild the charset precedence array.
|
|
1204 The "charsets preferred for the current language" get highest precedence,
|
|
1205 followed by the "charsets preferred by default", ordered as in
|
|
1206 Vlanguage_unicode_precedence_list and Vdefault_unicode_precedence_list,
|
|
1207 respectively. All remaining charsets follow in an arbitrary order. */
|
771
|
1208 void
|
|
1209 recalculate_unicode_precedence (void)
|
|
1210 {
|
|
1211 int lbs[NUM_LEADING_BYTES];
|
|
1212 int i;
|
|
1213
|
|
1214 for (i = 0; i < NUM_LEADING_BYTES; i++)
|
|
1215 lbs[i] = 0;
|
|
1216
|
|
1217 Dynarr_reset (unicode_precedence_dynarr);
|
|
1218
|
|
1219 add_charsets_to_precedence_list (Vlanguage_unicode_precedence_list,
|
|
1220 lbs, unicode_precedence_dynarr);
|
|
1221 add_charsets_to_precedence_list (Vdefault_unicode_precedence_list,
|
|
1222 lbs, unicode_precedence_dynarr);
|
|
1223
|
|
1224 for (i = 0; i < NUM_LEADING_BYTES; i++)
|
|
1225 {
|
|
1226 if (lbs[i] == 0)
|
|
1227 {
|
826
|
1228 Lisp_Object charset = charset_by_leading_byte (i + MIN_LEADING_BYTE);
|
771
|
1229 if (!NILP (charset))
|
|
1230 Dynarr_add (unicode_precedence_dynarr, charset);
|
|
1231 }
|
|
1232 }
|
|
1233 }
|
|
1234
|
877
|
1235 DEFUN ("unicode-precedence-list",
|
|
1236 Funicode_precedence_list,
|
|
1237 0, 0, 0, /*
|
|
1238 Return the precedence order among charsets used for Unicode decoding.
|
|
1239
|
|
1240 Value is a list of charsets, which are searched in order for a translation
|
|
1241 matching a given Unicode character.
|
|
1242
|
|
1243 The highest precedence is given to the language-specific precedence list of
|
|
1244 charsets, defined by `set-language-unicode-precedence-list'. These are
|
|
1245 followed by charsets in the default precedence list, defined by
|
|
1246 `set-default-unicode-precedence-list'. Charsets occurring multiple times are
|
|
1247 given precedence according to their first occurrance in either list. These
|
|
1248 are followed by the remaining charsets, in some arbitrary order.
|
771
|
1249
|
|
1250 The language-specific precedence list is meant to be set as part of the
|
|
1251 language environment initialization; the default precedence list is meant
|
|
1252 to be set by the user.
|
1318
|
1253
|
|
1254 #### NOTE: This interface may be changed.
|
771
|
1255 */
|
877
|
1256 ())
|
|
1257 {
|
|
1258 int i;
|
|
1259 Lisp_Object list = Qnil;
|
|
1260
|
|
1261 for (i = Dynarr_length (unicode_precedence_dynarr) - 1; i >= 0; i--)
|
|
1262 list = Fcons (Dynarr_at (unicode_precedence_dynarr, i), list);
|
|
1263 return list;
|
|
1264 }
|
|
1265
|
|
1266
|
|
1267 /* #### This interface is wrong. Cyrillic users and Chinese users are going
|
|
1268 to have varying opinions about whether ISO Cyrillic, KOI8-R, or Windows
|
|
1269 1251 should take precedence, and whether Big Five or CNS should take
|
|
1270 precedence, respectively. This means that users are sometimes going to
|
|
1271 want to set Vlanguage_unicode_precedence_list.
|
|
1272 Furthermore, this should be language-local (buffer-local would be a
|
1318
|
1273 reasonable approximation).
|
|
1274
|
|
1275 Answer: You are right, this needs rethinking. */
|
877
|
1276 DEFUN ("set-language-unicode-precedence-list",
|
|
1277 Fset_language_unicode_precedence_list,
|
|
1278 1, 1, 0, /*
|
|
1279 Set the language-specific precedence of charsets in Unicode decoding.
|
|
1280 LIST is a list of charsets.
|
|
1281 See `unicode-precedence-list' for more information.
|
1318
|
1282
|
|
1283 #### NOTE: This interface may be changed.
|
877
|
1284 */
|
771
|
1285 (list))
|
|
1286 {
|
|
1287 {
|
|
1288 EXTERNAL_LIST_LOOP_2 (elt, list)
|
|
1289 Fget_charset (elt);
|
|
1290 }
|
|
1291
|
|
1292 Vlanguage_unicode_precedence_list = list;
|
|
1293 recalculate_unicode_precedence ();
|
|
1294 return Qnil;
|
|
1295 }
|
|
1296
|
|
1297 DEFUN ("language-unicode-precedence-list",
|
|
1298 Flanguage_unicode_precedence_list,
|
|
1299 0, 0, 0, /*
|
|
1300 Return the language-specific precedence list used for Unicode decoding.
|
877
|
1301 See `unicode-precedence-list' for more information.
|
1318
|
1302
|
|
1303 #### NOTE: This interface may be changed.
|
771
|
1304 */
|
|
1305 ())
|
|
1306 {
|
|
1307 return Vlanguage_unicode_precedence_list;
|
|
1308 }
|
|
1309
|
|
1310 DEFUN ("set-default-unicode-precedence-list",
|
|
1311 Fset_default_unicode_precedence_list,
|
|
1312 1, 1, 0, /*
|
|
1313 Set the default precedence list used for Unicode decoding.
|
877
|
1314 This is intended to be set by the user. See
|
|
1315 `unicode-precedence-list' for more information.
|
1318
|
1316
|
|
1317 #### NOTE: This interface may be changed.
|
771
|
1318 */
|
|
1319 (list))
|
|
1320 {
|
|
1321 {
|
|
1322 EXTERNAL_LIST_LOOP_2 (elt, list)
|
|
1323 Fget_charset (elt);
|
|
1324 }
|
|
1325
|
|
1326 Vdefault_unicode_precedence_list = list;
|
|
1327 recalculate_unicode_precedence ();
|
|
1328 return Qnil;
|
|
1329 }
|
|
1330
|
|
1331 DEFUN ("default-unicode-precedence-list",
|
|
1332 Fdefault_unicode_precedence_list,
|
|
1333 0, 0, 0, /*
|
|
1334 Return the default precedence list used for Unicode decoding.
|
877
|
1335 See `unicode-precedence-list' for more information.
|
1318
|
1336
|
|
1337 #### NOTE: This interface may be changed.
|
771
|
1338 */
|
|
1339 ())
|
|
1340 {
|
|
1341 return Vdefault_unicode_precedence_list;
|
|
1342 }
|
|
1343
|
|
1344 DEFUN ("set-unicode-conversion", Fset_unicode_conversion,
|
|
1345 2, 2, 0, /*
|
|
1346 Add conversion information between Unicode codepoints and characters.
|
877
|
1347 Conversions for U+0000 to U+00FF are hardwired to ASCII, Control-1, and
|
|
1348 Latin-1. Attempts to set these values will raise an error.
|
|
1349
|
771
|
1350 CHARACTER is one of the following:
|
|
1351
|
|
1352 -- A character (in which case CODE must be a non-negative integer; values
|
|
1353 above 2^20 - 1 are allowed for the purpose of specifying private
|
877
|
1354 characters, but are illegal in standard Unicode---they will cause errors
|
|
1355 when converted to utf-16)
|
771
|
1356 -- A vector of characters (in which case CODE must be a vector of integers
|
|
1357 of the same length)
|
|
1358 */
|
|
1359 (character, code))
|
|
1360 {
|
|
1361 Lisp_Object charset;
|
877
|
1362 int ichar, unicode;
|
771
|
1363
|
|
1364 CHECK_CHAR (character);
|
|
1365 CHECK_NATNUM (code);
|
|
1366
|
877
|
1367 unicode = XINT (code);
|
|
1368 ichar = XCHAR (character);
|
|
1369 charset = ichar_charset (ichar);
|
|
1370
|
|
1371 /* The translations of ASCII, Control-1, and Latin-1 code points are
|
|
1372 hard-coded in ichar_to_unicode and unicode_to_ichar.
|
|
1373
|
|
1374 Checking unicode < 256 && ichar != unicode is wrong because Mule gives
|
|
1375 many Latin characters code points in a few different character sets. */
|
|
1376 if ((EQ (charset, Vcharset_ascii) ||
|
|
1377 EQ (charset, Vcharset_control_1) ||
|
|
1378 EQ (charset, Vcharset_latin_iso8859_1))
|
|
1379 && unicode != ichar)
|
893
|
1380 signal_error (Qinvalid_argument, "Can't change Unicode translation for ASCII, Control-1 or Latin-1 character",
|
771
|
1381 character);
|
|
1382
|
877
|
1383 /* #### Composite characters are not properly implemented yet. */
|
|
1384 if (EQ (charset, Vcharset_composite))
|
|
1385 signal_error (Qinvalid_argument, "Can't set Unicode translation for Composite char",
|
|
1386 character);
|
|
1387
|
|
1388 set_unicode_conversion (ichar, unicode);
|
771
|
1389 return Qnil;
|
|
1390 }
|
|
1391
|
|
1392 #endif /* MULE */
|
|
1393
|
800
|
1394 DEFUN ("char-to-unicode", Fchar_to_unicode, 1, 1, 0, /*
|
771
|
1395 Convert character to Unicode codepoint.
|
3025
|
1396 When there is no international support (i.e. the `mule' feature is not
|
877
|
1397 present), this function simply does `char-to-int'.
|
771
|
1398 */
|
|
1399 (character))
|
|
1400 {
|
|
1401 CHECK_CHAR (character);
|
|
1402 #ifdef MULE
|
867
|
1403 return make_int (ichar_to_unicode (XCHAR (character)));
|
771
|
1404 #else
|
|
1405 return Fchar_to_int (character);
|
|
1406 #endif /* MULE */
|
|
1407 }
|
|
1408
|
800
|
1409 DEFUN ("unicode-to-char", Funicode_to_char, 1, 2, 0, /*
|
771
|
1410 Convert Unicode codepoint to character.
|
|
1411 CODE should be a non-negative integer.
|
|
1412 If CHARSETS is given, it should be a list of charsets, and only those
|
|
1413 charsets will be consulted, in the given order, for a translation.
|
|
1414 Otherwise, the default ordering of all charsets will be given (see
|
|
1415 `set-unicode-charset-precedence').
|
|
1416
|
3025
|
1417 When there is no international support (i.e. the `mule' feature is not
|
877
|
1418 present), this function simply does `int-to-char' and ignores the CHARSETS
|
|
1419 argument.
|
2622
|
1420
|
3439
|
1421 If the CODE would not otherwise be converted to an XEmacs character, and the
|
|
1422 list of character sets to be consulted is nil or the default, a new XEmacs
|
|
1423 character will be created for it in one of the `jit-ucs-charset' Mule
|
|
1424 character sets, and that character will be returned. There is scope for
|
|
1425 tens of thousands of separate Unicode code points in every session using
|
|
1426 this technique, so despite XEmacs' internal encoding not being based on
|
|
1427 Unicode, your data won't be trashed.
|
771
|
1428 */
|
2333
|
1429 (code, USED_IF_MULE (charsets)))
|
771
|
1430 {
|
|
1431 #ifdef MULE
|
|
1432 Lisp_Object_dynarr *dyn;
|
|
1433 int lbs[NUM_LEADING_BYTES];
|
|
1434 int c;
|
|
1435
|
|
1436 CHECK_NATNUM (code);
|
|
1437 c = XINT (code);
|
|
1438 {
|
|
1439 EXTERNAL_LIST_LOOP_2 (elt, charsets)
|
|
1440 Fget_charset (elt);
|
|
1441 }
|
|
1442
|
|
1443 if (NILP (charsets))
|
|
1444 {
|
877
|
1445 Ichar ret = unicode_to_ichar (c, unicode_precedence_dynarr);
|
771
|
1446 if (ret == -1)
|
|
1447 return Qnil;
|
|
1448 return make_char (ret);
|
|
1449 }
|
|
1450
|
|
1451 dyn = Dynarr_new (Lisp_Object);
|
|
1452 memset (lbs, 0, NUM_LEADING_BYTES * sizeof (int));
|
|
1453 add_charsets_to_precedence_list (charsets, lbs, dyn);
|
|
1454 {
|
877
|
1455 Ichar ret = unicode_to_ichar (c, dyn);
|
771
|
1456 Dynarr_free (dyn);
|
|
1457 if (ret == -1)
|
|
1458 return Qnil;
|
|
1459 return make_char (ret);
|
|
1460 }
|
|
1461 #else
|
|
1462 CHECK_NATNUM (code);
|
|
1463 return Fint_to_char (code);
|
|
1464 #endif /* MULE */
|
|
1465 }
|
|
1466
|
872
|
1467 #ifdef MULE
|
|
1468
|
771
|
1469 static Lisp_Object
|
|
1470 cerrar_el_fulano (Lisp_Object fulano)
|
|
1471 {
|
|
1472 FILE *file = (FILE *) get_opaque_ptr (fulano);
|
|
1473 retry_fclose (file);
|
|
1474 return Qnil;
|
|
1475 }
|
|
1476
|
1318
|
1477 DEFUN ("load-unicode-mapping-table", Fload_unicode_mapping_table,
|
771
|
1478 2, 6, 0, /*
|
877
|
1479 Load Unicode tables with the Unicode mapping data in FILENAME for CHARSET.
|
771
|
1480 Data is text, in the form of one translation per line -- charset
|
|
1481 codepoint followed by Unicode codepoint. Numbers are decimal or hex
|
|
1482 \(preceded by 0x). Comments are marked with a #. Charset codepoints
|
877
|
1483 for two-dimensional charsets have the first octet stored in the
|
771
|
1484 high 8 bits of the hex number and the second in the low 8 bits.
|
|
1485
|
|
1486 If START and END are given, only charset codepoints within the given
|
877
|
1487 range will be processed. (START and END apply to the codepoints in the
|
|
1488 file, before OFFSET is applied.)
|
771
|
1489
|
877
|
1490 If OFFSET is given, that value will be added to all charset codepoints
|
|
1491 in the file to obtain the internal charset codepoint. \(We assume
|
|
1492 that octets in the table are in the range 33 to 126 or 32 to 127. If
|
|
1493 you have a table in ku-ten form, with octets in the range 1 to 94, you
|
|
1494 will have to use an offset of 5140, i.e. 0x2020.)
|
771
|
1495
|
|
1496 FLAGS, if specified, control further how the tables are interpreted
|
877
|
1497 and are used to special-case certain known format deviations in the
|
|
1498 Unicode tables or in the charset:
|
771
|
1499
|
|
1500 `ignore-first-column'
|
877
|
1501 The JIS X 0208 tables have 3 columns of data instead of 2. The first
|
|
1502 column contains the Shift-JIS codepoint, which we ignore.
|
771
|
1503 `big5'
|
877
|
1504 The charset codepoints are Big Five codepoints; convert it to the
|
|
1505 hacked-up Mule codepoint in `chinese-big5-1' or `chinese-big5-2'.
|
771
|
1506 */
|
|
1507 (filename, charset, start, end, offset, flags))
|
|
1508 {
|
|
1509 int st = 0, en = INT_MAX, of = 0;
|
|
1510 FILE *file;
|
|
1511 struct gcpro gcpro1;
|
|
1512 char line[1025];
|
|
1513 int fondo = specpdl_depth ();
|
|
1514 int ignore_first_column = 0;
|
|
1515 int big5 = 0;
|
|
1516
|
|
1517 CHECK_STRING (filename);
|
|
1518 charset = Fget_charset (charset);
|
|
1519 if (!NILP (start))
|
|
1520 {
|
|
1521 CHECK_INT (start);
|
|
1522 st = XINT (start);
|
|
1523 }
|
|
1524 if (!NILP (end))
|
|
1525 {
|
|
1526 CHECK_INT (end);
|
|
1527 en = XINT (end);
|
|
1528 }
|
|
1529 if (!NILP (offset))
|
|
1530 {
|
|
1531 CHECK_INT (offset);
|
|
1532 of = XINT (offset);
|
|
1533 }
|
|
1534
|
|
1535 if (!LISTP (flags))
|
|
1536 flags = list1 (flags);
|
|
1537
|
|
1538 {
|
|
1539 EXTERNAL_LIST_LOOP_2 (elt, flags)
|
|
1540 {
|
|
1541 if (EQ (elt, Qignore_first_column))
|
|
1542 ignore_first_column = 1;
|
|
1543 else if (EQ (elt, Qbig5))
|
|
1544 big5 = 1;
|
|
1545 else
|
|
1546 invalid_constant
|
1318
|
1547 ("Unrecognized `load-unicode-mapping-table' flag", elt);
|
771
|
1548 }
|
|
1549 }
|
|
1550
|
|
1551 GCPRO1 (filename);
|
|
1552 filename = Fexpand_file_name (filename, Qnil);
|
|
1553 file = qxe_fopen (XSTRING_DATA (filename), READ_TEXT);
|
|
1554 if (!file)
|
|
1555 report_file_error ("Cannot open", filename);
|
|
1556 record_unwind_protect (cerrar_el_fulano, make_opaque_ptr (file));
|
|
1557 while (fgets (line, sizeof (line), file))
|
|
1558 {
|
|
1559 char *p = line;
|
|
1560 int cp1, cp2, endcount;
|
|
1561 int cp1high, cp1low;
|
|
1562 int dummy;
|
|
1563
|
|
1564 while (*p) /* erase all comments out of the line */
|
|
1565 {
|
|
1566 if (*p == '#')
|
|
1567 *p = '\0';
|
|
1568 else
|
|
1569 p++;
|
|
1570 }
|
|
1571 /* see if line is nothing but whitespace and skip if so */
|
|
1572 p = line + strspn (line, " \t\n\r\f");
|
|
1573 if (!*p)
|
|
1574 continue;
|
|
1575 /* NOTE: It appears that MS Windows and Newlib sscanf() have
|
|
1576 different interpretations for whitespace (== "skip all whitespace
|
|
1577 at processing point"): Newlib requires at least one corresponding
|
|
1578 whitespace character in the input, but MS allows none. The
|
|
1579 following would be easier to write if we could count on the MS
|
|
1580 interpretation.
|
|
1581
|
|
1582 Also, the return value does NOT include %n storage. */
|
|
1583 if ((!ignore_first_column ?
|
|
1584 sscanf (p, "%i %i%n", &cp1, &cp2, &endcount) < 2 :
|
|
1585 sscanf (p, "%i %i %i%n", &dummy, &cp1, &cp2, &endcount) < 3)
|
2367
|
1586 /* #### Temporary code! Cygwin newlib fucked up scanf() handling
|
|
1587 of numbers beginning 0x0... starting in 04/2004, in an attempt
|
|
1588 to fix another bug. A partial fix for this was put in in
|
|
1589 06/2004, but as of 10/2004 the value of ENDCOUNT returned in
|
|
1590 such case is still wrong. If this gets fixed soon, remove
|
|
1591 this code. --ben */
|
|
1592 #ifndef CYGWIN_SCANF_BUG
|
|
1593 || *(p + endcount + strspn (p + endcount, " \t\n\r\f"))
|
|
1594 #endif
|
|
1595 )
|
771
|
1596 {
|
793
|
1597 warn_when_safe (Qunicode, Qwarning,
|
771
|
1598 "Unrecognized line in translation file %s:\n%s",
|
|
1599 XSTRING_DATA (filename), line);
|
|
1600 continue;
|
|
1601 }
|
|
1602 if (cp1 >= st && cp1 <= en)
|
|
1603 {
|
|
1604 cp1 += of;
|
|
1605 if (cp1 < 0 || cp1 >= 65536)
|
|
1606 {
|
|
1607 out_of_range:
|
793
|
1608 warn_when_safe (Qunicode, Qwarning,
|
|
1609 "Out of range first codepoint 0x%x in "
|
|
1610 "translation file %s:\n%s",
|
771
|
1611 cp1, XSTRING_DATA (filename), line);
|
|
1612 continue;
|
|
1613 }
|
|
1614
|
|
1615 cp1high = cp1 >> 8;
|
|
1616 cp1low = cp1 & 255;
|
|
1617
|
|
1618 if (big5)
|
|
1619 {
|
867
|
1620 Ichar ch = decode_big5_char (cp1high, cp1low);
|
771
|
1621 if (ch == -1)
|
793
|
1622
|
|
1623 warn_when_safe (Qunicode, Qwarning,
|
|
1624 "Out of range Big5 codepoint 0x%x in "
|
|
1625 "translation file %s:\n%s",
|
771
|
1626 cp1, XSTRING_DATA (filename), line);
|
|
1627 else
|
|
1628 set_unicode_conversion (ch, cp2);
|
|
1629 }
|
|
1630 else
|
|
1631 {
|
|
1632 int l1, h1, l2, h2;
|
867
|
1633 Ichar emch;
|
771
|
1634
|
|
1635 switch (XCHARSET_TYPE (charset))
|
|
1636 {
|
|
1637 case CHARSET_TYPE_94: l1 = 33; h1 = 126; l2 = 0; h2 = 0; break;
|
|
1638 case CHARSET_TYPE_96: l1 = 32; h1 = 127; l2 = 0; h2 = 0; break;
|
|
1639 case CHARSET_TYPE_94X94: l1 = 33; h1 = 126; l2 = 33; h2 = 126;
|
|
1640 break;
|
|
1641 case CHARSET_TYPE_96X96: l1 = 32; h1 = 127; l2 = 32; h2 = 127;
|
|
1642 break;
|
2500
|
1643 default: ABORT (); l1 = 0; h1 = 0; l2 = 0; h2 = 0;
|
771
|
1644 }
|
|
1645
|
|
1646 if (cp1high < l2 || cp1high > h2 || cp1low < l1 || cp1low > h1)
|
|
1647 goto out_of_range;
|
|
1648
|
867
|
1649 emch = (cp1high == 0 ? make_ichar (charset, cp1low, 0) :
|
|
1650 make_ichar (charset, cp1high, cp1low));
|
771
|
1651 set_unicode_conversion (emch, cp2);
|
|
1652 }
|
|
1653 }
|
|
1654 }
|
|
1655
|
|
1656 if (ferror (file))
|
|
1657 report_file_error ("IO error when reading", filename);
|
|
1658
|
|
1659 unbind_to (fondo); /* close file */
|
|
1660 UNGCPRO;
|
|
1661 return Qnil;
|
|
1662 }
|
|
1663
|
|
1664 #endif /* MULE */
|
|
1665
|
|
1666
|
|
1667 /************************************************************************/
|
|
1668 /* Unicode coding system */
|
|
1669 /************************************************************************/
|
|
1670
|
|
1671 struct unicode_coding_system
|
|
1672 {
|
|
1673 enum unicode_type type;
|
1887
|
1674 unsigned int little_endian :1;
|
|
1675 unsigned int need_bom :1;
|
771
|
1676 };
|
|
1677
|
|
1678 #define CODING_SYSTEM_UNICODE_TYPE(codesys) \
|
|
1679 (CODING_SYSTEM_TYPE_DATA (codesys, unicode)->type)
|
|
1680 #define XCODING_SYSTEM_UNICODE_TYPE(codesys) \
|
|
1681 CODING_SYSTEM_UNICODE_TYPE (XCODING_SYSTEM (codesys))
|
|
1682 #define CODING_SYSTEM_UNICODE_LITTLE_ENDIAN(codesys) \
|
|
1683 (CODING_SYSTEM_TYPE_DATA (codesys, unicode)->little_endian)
|
|
1684 #define XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN(codesys) \
|
|
1685 CODING_SYSTEM_UNICODE_LITTLE_ENDIAN (XCODING_SYSTEM (codesys))
|
|
1686 #define CODING_SYSTEM_UNICODE_NEED_BOM(codesys) \
|
|
1687 (CODING_SYSTEM_TYPE_DATA (codesys, unicode)->need_bom)
|
|
1688 #define XCODING_SYSTEM_UNICODE_NEED_BOM(codesys) \
|
|
1689 CODING_SYSTEM_UNICODE_NEED_BOM (XCODING_SYSTEM (codesys))
|
|
1690
|
|
1691 struct unicode_coding_stream
|
|
1692 {
|
|
1693 /* decode */
|
|
1694 unsigned char counter;
|
4096
|
1695 unsigned char indicated_length;
|
771
|
1696 int seen_char;
|
|
1697 /* encode */
|
|
1698 Lisp_Object current_charset;
|
|
1699 int current_char_boundary;
|
|
1700 int wrote_bom;
|
|
1701 };
|
|
1702
|
1204
|
1703 static const struct memory_description unicode_coding_system_description[] = {
|
771
|
1704 { XD_END }
|
|
1705 };
|
|
1706
|
1204
|
1707 DEFINE_CODING_SYSTEM_TYPE_WITH_DATA (unicode);
|
|
1708
|
771
|
1709 static void
|
|
1710 decode_unicode_char (int ch, unsigned_char_dynarr *dst,
|
1887
|
1711 struct unicode_coding_stream *data,
|
|
1712 unsigned int ignore_bom)
|
771
|
1713 {
|
|
1714 if (ch == 0xFEFF && !data->seen_char && ignore_bom)
|
|
1715 ;
|
|
1716 else
|
|
1717 {
|
|
1718 #ifdef MULE
|
877
|
1719 Ichar chr = unicode_to_ichar (ch, unicode_precedence_dynarr);
|
771
|
1720
|
|
1721 if (chr != -1)
|
|
1722 {
|
867
|
1723 Ibyte work[MAX_ICHAR_LEN];
|
771
|
1724 int len;
|
|
1725
|
867
|
1726 len = set_itext_ichar (work, chr);
|
771
|
1727 Dynarr_add_many (dst, work, len);
|
|
1728 }
|
|
1729 else
|
|
1730 {
|
|
1731 Dynarr_add (dst, LEADING_BYTE_JAPANESE_JISX0208);
|
|
1732 Dynarr_add (dst, 34 + 128);
|
|
1733 Dynarr_add (dst, 46 + 128);
|
|
1734 }
|
|
1735 #else
|
867
|
1736 Dynarr_add (dst, (Ibyte) ch);
|
771
|
1737 #endif /* MULE */
|
|
1738 }
|
|
1739
|
|
1740 data->seen_char = 1;
|
|
1741 }
|
|
1742
|
4096
|
1743 #define DECODE_ERROR_OCTET(octet, dst, data, ignore_bom) \
|
|
1744 decode_unicode_char ((octet) + UNICODE_ERROR_OCTET_RANGE_START, \
|
|
1745 dst, data, ignore_bom)
|
|
1746
|
|
1747 static inline void
|
|
1748 indicate_invalid_utf_8 (unsigned char indicated_length,
|
|
1749 unsigned char counter,
|
|
1750 int ch, unsigned_char_dynarr *dst,
|
|
1751 struct unicode_coding_stream *data,
|
|
1752 unsigned int ignore_bom)
|
|
1753 {
|
|
1754 Binbyte stored = indicated_length - counter;
|
|
1755 Binbyte mask = "\x00\x00\xC0\xE0\xF0\xF8\xFC"[indicated_length];
|
|
1756
|
|
1757 while (stored > 0)
|
|
1758 {
|
|
1759 DECODE_ERROR_OCTET (((ch >> (6 * (stored - 1))) & 0x3f) | mask,
|
|
1760 dst, data, ignore_bom);
|
|
1761 mask = 0x80, stored--;
|
|
1762 }
|
|
1763 }
|
|
1764
|
771
|
1765 static void
|
|
1766 encode_unicode_char_1 (int code, unsigned_char_dynarr *dst,
|
4096
|
1767 enum unicode_type type, unsigned int little_endian,
|
|
1768 int write_error_characters_as_such)
|
771
|
1769 {
|
|
1770 switch (type)
|
|
1771 {
|
|
1772 case UNICODE_UTF_16:
|
|
1773 if (little_endian)
|
|
1774 {
|
3952
|
1775 if (code < 0x10000) {
|
|
1776 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1777 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
4096
|
1778 } else if (write_error_characters_as_such &&
|
|
1779 code >= UNICODE_ERROR_OCTET_RANGE_START &&
|
|
1780 code < (UNICODE_ERROR_OCTET_RANGE_START + 0x100))
|
|
1781 {
|
|
1782 Dynarr_add (dst, (unsigned char) ((code & 0xFF)));
|
|
1783 }
|
|
1784 else if (code < 0x110000)
|
|
1785 {
|
|
1786 /* Little endian; least significant byte first. */
|
|
1787 int first, second;
|
|
1788
|
|
1789 CODE_TO_UTF_16_SURROGATES(code, first, second);
|
|
1790
|
|
1791 Dynarr_add (dst, (unsigned char) (first & 255));
|
|
1792 Dynarr_add (dst, (unsigned char) ((first >> 8) & 255));
|
|
1793
|
|
1794 Dynarr_add (dst, (unsigned char) (second & 255));
|
|
1795 Dynarr_add (dst, (unsigned char) ((second >> 8) & 255));
|
|
1796 }
|
|
1797 else
|
|
1798 {
|
|
1799 /* Not valid Unicode. Pass U+FFFD, least significant byte
|
|
1800 first. */
|
|
1801 Dynarr_add (dst, (unsigned char) 0xFD);
|
|
1802 Dynarr_add (dst, (unsigned char) 0xFF);
|
|
1803 }
|
771
|
1804 }
|
|
1805 else
|
|
1806 {
|
3952
|
1807 if (code < 0x10000) {
|
|
1808 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1809 Dynarr_add (dst, (unsigned char) (code & 255));
|
4096
|
1810 } else if (write_error_characters_as_such &&
|
|
1811 code >= UNICODE_ERROR_OCTET_RANGE_START &&
|
|
1812 code < (UNICODE_ERROR_OCTET_RANGE_START + 0x100))
|
|
1813 {
|
|
1814 Dynarr_add (dst, (unsigned char) ((code & 0xFF)));
|
|
1815 }
|
|
1816 else if (code < 0x110000)
|
|
1817 {
|
|
1818 /* Big endian; most significant byte first. */
|
|
1819 int first, second;
|
|
1820
|
|
1821 CODE_TO_UTF_16_SURROGATES(code, first, second);
|
|
1822
|
|
1823 Dynarr_add (dst, (unsigned char) ((first >> 8) & 255));
|
|
1824 Dynarr_add (dst, (unsigned char) (first & 255));
|
|
1825
|
|
1826 Dynarr_add (dst, (unsigned char) ((second >> 8) & 255));
|
|
1827 Dynarr_add (dst, (unsigned char) (second & 255));
|
|
1828 }
|
|
1829 else
|
|
1830 {
|
|
1831 /* Not valid Unicode. Pass U+FFFD, most significant byte
|
|
1832 first. */
|
|
1833 Dynarr_add (dst, (unsigned char) 0xFF);
|
|
1834 Dynarr_add (dst, (unsigned char) 0xFD);
|
|
1835 }
|
771
|
1836 }
|
|
1837 break;
|
|
1838
|
|
1839 case UNICODE_UCS_4:
|
4096
|
1840 case UNICODE_UTF_32:
|
771
|
1841 if (little_endian)
|
|
1842 {
|
4096
|
1843 if (write_error_characters_as_such &&
|
|
1844 code >= UNICODE_ERROR_OCTET_RANGE_START &&
|
|
1845 code < (UNICODE_ERROR_OCTET_RANGE_START + 0x100))
|
|
1846 {
|
|
1847 Dynarr_add (dst, (unsigned char) ((code & 0xFF)));
|
|
1848 }
|
|
1849 else
|
|
1850 {
|
|
1851 /* We generate and accept incorrect sequences here, which is
|
|
1852 okay, in the interest of preservation of the user's
|
|
1853 data. */
|
|
1854 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1855 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1856 Dynarr_add (dst, (unsigned char) ((code >> 16) & 255));
|
|
1857 Dynarr_add (dst, (unsigned char) (code >> 24));
|
|
1858 }
|
771
|
1859 }
|
|
1860 else
|
|
1861 {
|
4096
|
1862 if (write_error_characters_as_such &&
|
|
1863 code >= UNICODE_ERROR_OCTET_RANGE_START &&
|
|
1864 code < (UNICODE_ERROR_OCTET_RANGE_START + 0x100))
|
|
1865 {
|
|
1866 Dynarr_add (dst, (unsigned char) ((code & 0xFF)));
|
|
1867 }
|
|
1868 else
|
|
1869 {
|
|
1870 /* We generate and accept incorrect sequences here, which is okay,
|
|
1871 in the interest of preservation of the user's data. */
|
|
1872 Dynarr_add (dst, (unsigned char) (code >> 24));
|
|
1873 Dynarr_add (dst, (unsigned char) ((code >> 16) & 255));
|
|
1874 Dynarr_add (dst, (unsigned char) ((code >> 8) & 255));
|
|
1875 Dynarr_add (dst, (unsigned char) (code & 255));
|
|
1876 }
|
771
|
1877 }
|
|
1878 break;
|
|
1879
|
|
1880 case UNICODE_UTF_8:
|
|
1881 if (code <= 0x7f)
|
|
1882 {
|
|
1883 Dynarr_add (dst, (unsigned char) code);
|
|
1884 }
|
|
1885 else if (code <= 0x7ff)
|
|
1886 {
|
|
1887 Dynarr_add (dst, (unsigned char) ((code >> 6) | 0xc0));
|
|
1888 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1889 }
|
|
1890 else if (code <= 0xffff)
|
|
1891 {
|
|
1892 Dynarr_add (dst, (unsigned char) ((code >> 12) | 0xe0));
|
|
1893 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1894 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1895 }
|
|
1896 else if (code <= 0x1fffff)
|
|
1897 {
|
|
1898 Dynarr_add (dst, (unsigned char) ((code >> 18) | 0xf0));
|
|
1899 Dynarr_add (dst, (unsigned char) (((code >> 12) & 0x3f) | 0x80));
|
|
1900 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1901 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1902 }
|
|
1903 else if (code <= 0x3ffffff)
|
|
1904 {
|
4096
|
1905
|
|
1906 #if !(UNICODE_ERROR_OCTET_RANGE_START > 0x1fffff \
|
|
1907 && UNICODE_ERROR_OCTET_RANGE_START < 0x3ffffff)
|
|
1908 #error "This code needs to be rewritten. "
|
|
1909 #endif
|
|
1910 if (write_error_characters_as_such &&
|
|
1911 code >= UNICODE_ERROR_OCTET_RANGE_START &&
|
|
1912 code < (UNICODE_ERROR_OCTET_RANGE_START + 0x100))
|
|
1913 {
|
|
1914 Dynarr_add (dst, (unsigned char) ((code & 0xFF)));
|
|
1915 }
|
|
1916 else
|
|
1917 {
|
|
1918 Dynarr_add (dst, (unsigned char) ((code >> 24) | 0xf8));
|
|
1919 Dynarr_add (dst, (unsigned char) (((code >> 18) & 0x3f) | 0x80));
|
|
1920 Dynarr_add (dst, (unsigned char) (((code >> 12) & 0x3f) | 0x80));
|
|
1921 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1922 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1923 }
|
771
|
1924 }
|
|
1925 else
|
|
1926 {
|
|
1927 Dynarr_add (dst, (unsigned char) ((code >> 30) | 0xfc));
|
|
1928 Dynarr_add (dst, (unsigned char) (((code >> 24) & 0x3f) | 0x80));
|
|
1929 Dynarr_add (dst, (unsigned char) (((code >> 18) & 0x3f) | 0x80));
|
|
1930 Dynarr_add (dst, (unsigned char) (((code >> 12) & 0x3f) | 0x80));
|
|
1931 Dynarr_add (dst, (unsigned char) (((code >> 6) & 0x3f) | 0x80));
|
|
1932 Dynarr_add (dst, (unsigned char) ((code & 0x3f) | 0x80));
|
|
1933 }
|
|
1934 break;
|
|
1935
|
2500
|
1936 case UNICODE_UTF_7: ABORT ();
|
771
|
1937
|
2500
|
1938 default: ABORT ();
|
771
|
1939 }
|
|
1940 }
|
|
1941
|
3439
|
1942 /* Also used in mule-coding.c for UTF-8 handling in ISO 2022-oriented
|
|
1943 encodings. */
|
|
1944 void
|
2333
|
1945 encode_unicode_char (Lisp_Object USED_IF_MULE (charset), int h,
|
|
1946 int USED_IF_MULE (l), unsigned_char_dynarr *dst,
|
4096
|
1947 enum unicode_type type, unsigned int little_endian,
|
|
1948 int write_error_characters_as_such)
|
771
|
1949 {
|
|
1950 #ifdef MULE
|
867
|
1951 int code = ichar_to_unicode (make_ichar (charset, h & 127, l & 127));
|
771
|
1952
|
|
1953 if (code == -1)
|
|
1954 {
|
|
1955 if (type != UNICODE_UTF_16 &&
|
|
1956 XCHARSET_DIMENSION (charset) == 2 &&
|
|
1957 XCHARSET_CHARS (charset) == 94)
|
|
1958 {
|
|
1959 unsigned char final = XCHARSET_FINAL (charset);
|
|
1960
|
|
1961 if (('@' <= final) && (final < 0x7f))
|
|
1962 code = (0xe00000 + (final - '@') * 94 * 94
|
|
1963 + ((h & 127) - 33) * 94 + (l & 127) - 33);
|
|
1964 else
|
|
1965 code = '?';
|
|
1966 }
|
|
1967 else
|
|
1968 code = '?';
|
|
1969 }
|
|
1970 #else
|
|
1971 int code = h;
|
|
1972 #endif /* MULE */
|
|
1973
|
4096
|
1974 encode_unicode_char_1 (code, dst, type, little_endian,
|
|
1975 write_error_characters_as_such);
|
771
|
1976 }
|
|
1977
|
|
1978 static Bytecount
|
|
1979 unicode_convert (struct coding_stream *str, const UExtbyte *src,
|
|
1980 unsigned_char_dynarr *dst, Bytecount n)
|
|
1981 {
|
|
1982 unsigned int ch = str->ch;
|
|
1983 struct unicode_coding_stream *data = CODING_STREAM_TYPE_DATA (str, unicode);
|
|
1984 enum unicode_type type =
|
|
1985 XCODING_SYSTEM_UNICODE_TYPE (str->codesys);
|
1887
|
1986 unsigned int little_endian =
|
|
1987 XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (str->codesys);
|
|
1988 unsigned int ignore_bom = XCODING_SYSTEM_UNICODE_NEED_BOM (str->codesys);
|
771
|
1989 Bytecount orign = n;
|
|
1990
|
|
1991 if (str->direction == CODING_DECODE)
|
|
1992 {
|
|
1993 unsigned char counter = data->counter;
|
4096
|
1994 unsigned char indicated_length
|
|
1995 = data->indicated_length;
|
771
|
1996
|
|
1997 while (n--)
|
|
1998 {
|
|
1999 UExtbyte c = *src++;
|
|
2000
|
|
2001 switch (type)
|
|
2002 {
|
|
2003 case UNICODE_UTF_8:
|
4096
|
2004 if (0 == counter)
|
|
2005 {
|
|
2006 if (0 == (c & 0x80))
|
|
2007 {
|
|
2008 /* ASCII. */
|
|
2009 decode_unicode_char (c, dst, data, ignore_bom);
|
|
2010 }
|
|
2011 else if (0 == (c & 0x40))
|
|
2012 {
|
|
2013 /* Highest bit set, second highest not--there's
|
|
2014 something wrong. */
|
|
2015 DECODE_ERROR_OCTET (c, dst, data, ignore_bom);
|
|
2016 }
|
|
2017 else if (0 == (c & 0x20))
|
|
2018 {
|
|
2019 ch = c & 0x1f;
|
|
2020 counter = 1;
|
|
2021 indicated_length = 2;
|
|
2022 }
|
|
2023 else if (0 == (c & 0x10))
|
|
2024 {
|
|
2025 ch = c & 0x0f;
|
|
2026 counter = 2;
|
|
2027 indicated_length = 3;
|
|
2028 }
|
|
2029 else if (0 == (c & 0x08))
|
|
2030 {
|
|
2031 ch = c & 0x0f;
|
|
2032 counter = 3;
|
|
2033 indicated_length = 4;
|
|
2034 }
|
|
2035 else
|
|
2036 {
|
|
2037 /* We don't supports lengths longer than 4 in
|
|
2038 external-format data. */
|
|
2039 DECODE_ERROR_OCTET (c, dst, data, ignore_bom);
|
|
2040
|
|
2041 }
|
|
2042 }
|
|
2043 else
|
|
2044 {
|
|
2045 /* counter != 0 */
|
|
2046 if ((0 == (c & 0x80)) || (0 != (c & 0x40)))
|
|
2047 {
|
|
2048 indicate_invalid_utf_8(indicated_length,
|
|
2049 counter,
|
|
2050 ch, dst, data, ignore_bom);
|
|
2051 if (c & 0x80)
|
|
2052 {
|
|
2053 DECODE_ERROR_OCTET (c, dst, data, ignore_bom);
|
|
2054 }
|
|
2055 else
|
|
2056 {
|
|
2057 /* The character just read is ASCII. Treat it as
|
|
2058 such. */
|
|
2059 decode_unicode_char (c, dst, data, ignore_bom);
|
|
2060 }
|
|
2061 ch = 0;
|
|
2062 counter = 0;
|
|
2063 }
|
|
2064 else
|
|
2065 {
|
|
2066 ch = (ch << 6) | (c & 0x3f);
|
|
2067 counter--;
|
|
2068 /* Just processed the final byte. Emit the character. */
|
|
2069 if (!counter)
|
|
2070 {
|
|
2071 /* Don't accept over-long sequences, surrogates,
|
|
2072 or codes above #x10FFFF. */
|
|
2073 if ((ch < 0x80) ||
|
|
2074 ((ch < 0x800) && indicated_length > 2) ||
|
|
2075 ((ch < 0x10000) && indicated_length > 3) ||
|
|
2076 valid_utf_16_surrogate(ch) || (ch > 0x110000))
|
|
2077 {
|
|
2078 indicate_invalid_utf_8(indicated_length,
|
|
2079 counter,
|
|
2080 ch, dst, data,
|
|
2081 ignore_bom);
|
|
2082 }
|
|
2083 else
|
|
2084 {
|
|
2085 decode_unicode_char (ch, dst, data, ignore_bom);
|
|
2086 }
|
|
2087 ch = 0;
|
|
2088 }
|
|
2089 }
|
771
|
2090 }
|
|
2091 break;
|
|
2092
|
|
2093 case UNICODE_UTF_16:
|
3952
|
2094
|
771
|
2095 if (little_endian)
|
|
2096 ch = (c << counter) | ch;
|
|
2097 else
|
|
2098 ch = (ch << 8) | c;
|
4096
|
2099
|
771
|
2100 counter += 8;
|
3952
|
2101
|
4096
|
2102 if (16 == counter)
|
|
2103 {
|
771
|
2104 int tempch = ch;
|
4096
|
2105
|
|
2106 if (valid_utf_16_first_surrogate(ch))
|
|
2107 {
|
|
2108 break;
|
|
2109 }
|
771
|
2110 ch = 0;
|
|
2111 counter = 0;
|
|
2112 decode_unicode_char (tempch, dst, data, ignore_bom);
|
|
2113 }
|
4096
|
2114 else if (32 == counter)
|
3952
|
2115 {
|
|
2116 int tempch;
|
4096
|
2117
|
|
2118 if (!valid_utf_16_last_surrogate(ch & 0xFFFF))
|
3952
|
2119 {
|
4096
|
2120 DECODE_ERROR_OCTET ((ch >> 24) & 0xFF, dst, data,
|
|
2121 ignore_bom);
|
|
2122 DECODE_ERROR_OCTET ((ch >> 16) & 0xFF, dst, data,
|
|
2123 ignore_bom);
|
|
2124 DECODE_ERROR_OCTET ((ch >> 8) & 0xFF, dst, data,
|
|
2125 ignore_bom);
|
|
2126 DECODE_ERROR_OCTET (ch & 0xFF, dst, data,
|
|
2127 ignore_bom);
|
3952
|
2128 }
|
4096
|
2129 else
|
|
2130 {
|
|
2131 tempch = utf_16_surrogates_to_code((ch >> 16),
|
|
2132 (ch & 0xffff));
|
|
2133 decode_unicode_char(tempch, dst, data, ignore_bom);
|
|
2134 }
|
3952
|
2135 ch = 0;
|
|
2136 counter = 0;
|
4096
|
2137 }
|
|
2138 else
|
|
2139 assert(8 == counter || 24 == counter);
|
771
|
2140 break;
|
|
2141
|
|
2142 case UNICODE_UCS_4:
|
4096
|
2143 case UNICODE_UTF_32:
|
771
|
2144 if (little_endian)
|
|
2145 ch = (c << counter) | ch;
|
|
2146 else
|
|
2147 ch = (ch << 8) | c;
|
|
2148 counter += 8;
|
|
2149 if (counter == 32)
|
|
2150 {
|
4096
|
2151 if (ch > 0x10ffff)
|
|
2152 {
|
|
2153 /* ch is not a legal Unicode character. We're fine
|
|
2154 with that in UCS-4, though not in UTF-32. */
|
|
2155 if (UNICODE_UCS_4 == type && ch < 0x80000000)
|
|
2156 {
|
|
2157 decode_unicode_char (ch, dst, data, ignore_bom);
|
|
2158 }
|
|
2159 else if (little_endian)
|
|
2160 {
|
|
2161 DECODE_ERROR_OCTET (ch & 0xFF, dst, data,
|
|
2162 ignore_bom);
|
|
2163 DECODE_ERROR_OCTET ((ch >> 8) & 0xFF, dst, data,
|
|
2164 ignore_bom);
|
|
2165 DECODE_ERROR_OCTET ((ch >> 16) & 0xFF, dst, data,
|
|
2166 ignore_bom);
|
|
2167 DECODE_ERROR_OCTET ((ch >> 24) & 0xFF, dst, data,
|
|
2168 ignore_bom);
|
|
2169 }
|
|
2170 else
|
|
2171 {
|
|
2172 DECODE_ERROR_OCTET ((ch >> 24) & 0xFF, dst, data,
|
|
2173 ignore_bom);
|
|
2174 DECODE_ERROR_OCTET ((ch >> 16) & 0xFF, dst, data,
|
|
2175 ignore_bom);
|
|
2176 DECODE_ERROR_OCTET ((ch >> 8) & 0xFF, dst, data,
|
|
2177 ignore_bom);
|
|
2178 DECODE_ERROR_OCTET (ch & 0xFF, dst, data,
|
|
2179 ignore_bom);
|
|
2180 }
|
|
2181 }
|
|
2182 else
|
|
2183 {
|
|
2184 decode_unicode_char (ch, dst, data, ignore_bom);
|
|
2185 }
|
771
|
2186 ch = 0;
|
|
2187 counter = 0;
|
|
2188 }
|
|
2189 break;
|
|
2190
|
|
2191 case UNICODE_UTF_7:
|
2500
|
2192 ABORT ();
|
771
|
2193 break;
|
|
2194
|
2500
|
2195 default: ABORT ();
|
771
|
2196 }
|
|
2197
|
|
2198 }
|
4096
|
2199
|
|
2200 if (str->eof && ch)
|
|
2201 {
|
|
2202 switch (type)
|
|
2203 {
|
|
2204 case UNICODE_UTF_8:
|
|
2205 indicate_invalid_utf_8(indicated_length,
|
|
2206 counter, ch, dst, data,
|
|
2207 ignore_bom);
|
|
2208 break;
|
|
2209
|
|
2210 case UNICODE_UTF_16:
|
|
2211 case UNICODE_UCS_4:
|
|
2212 case UNICODE_UTF_32:
|
|
2213 if (8 == counter)
|
|
2214 {
|
|
2215 DECODE_ERROR_OCTET (ch, dst, data, ignore_bom);
|
|
2216 }
|
|
2217 else if (16 == counter)
|
|
2218 {
|
|
2219 if (little_endian)
|
|
2220 {
|
|
2221 DECODE_ERROR_OCTET (ch & 0xFF, dst, data, ignore_bom);
|
|
2222 DECODE_ERROR_OCTET ((ch >> 8) & 0xFF, dst, data,
|
|
2223 ignore_bom);
|
|
2224 }
|
|
2225 else
|
|
2226 {
|
|
2227 DECODE_ERROR_OCTET ((ch >> 8) & 0xFF, dst, data,
|
|
2228 ignore_bom);
|
|
2229 DECODE_ERROR_OCTET (ch & 0xFF, dst, data, ignore_bom);
|
|
2230 }
|
|
2231 }
|
|
2232 else if (24 == counter)
|
|
2233 {
|
|
2234 if (little_endian)
|
|
2235 {
|
|
2236 DECODE_ERROR_OCTET ((ch >> 16) & 0xFF, dst, data,
|
|
2237 ignore_bom);
|
|
2238 DECODE_ERROR_OCTET (ch & 0xFF, dst, data, ignore_bom);
|
|
2239 DECODE_ERROR_OCTET ((ch >> 8) & 0xFF, dst, data,
|
|
2240 ignore_bom);
|
|
2241 }
|
|
2242 else
|
|
2243 {
|
|
2244 DECODE_ERROR_OCTET ((ch >> 16) & 0xFF, dst, data,
|
|
2245 ignore_bom);
|
|
2246 DECODE_ERROR_OCTET ((ch >> 8) & 0xFF, dst, data,
|
|
2247 ignore_bom);
|
|
2248 DECODE_ERROR_OCTET (ch & 0xFF, dst, data,
|
|
2249 ignore_bom);
|
|
2250 }
|
|
2251 }
|
|
2252 else assert(0);
|
|
2253 break;
|
|
2254 }
|
|
2255 ch = 0;
|
|
2256 }
|
771
|
2257
|
|
2258 data->counter = counter;
|
4096
|
2259 data->indicated_length = indicated_length;
|
771
|
2260 }
|
|
2261 else
|
|
2262 {
|
|
2263 unsigned char char_boundary = data->current_char_boundary;
|
|
2264 Lisp_Object charset = data->current_charset;
|
|
2265
|
|
2266 #ifdef ENABLE_COMPOSITE_CHARS
|
|
2267 /* flags for handling composite chars. We do a little switcheroo
|
|
2268 on the source while we're outputting the composite char. */
|
|
2269 Bytecount saved_n = 0;
|
867
|
2270 const Ibyte *saved_src = NULL;
|
771
|
2271 int in_composite = 0;
|
|
2272
|
|
2273 back_to_square_n:
|
|
2274 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
2275
|
|
2276 if (XCODING_SYSTEM_UNICODE_NEED_BOM (str->codesys) && !data->wrote_bom)
|
|
2277 {
|
4096
|
2278 encode_unicode_char_1 (0xFEFF, dst, type, little_endian, 1);
|
771
|
2279 data->wrote_bom = 1;
|
|
2280 }
|
|
2281
|
|
2282 while (n--)
|
|
2283 {
|
867
|
2284 Ibyte c = *src++;
|
771
|
2285
|
|
2286 #ifdef MULE
|
826
|
2287 if (byte_ascii_p (c))
|
771
|
2288 #endif /* MULE */
|
|
2289 { /* Processing ASCII character */
|
|
2290 ch = 0;
|
|
2291 encode_unicode_char (Vcharset_ascii, c, 0, dst, type,
|
4096
|
2292 little_endian, 1);
|
771
|
2293
|
|
2294 char_boundary = 1;
|
|
2295 }
|
|
2296 #ifdef MULE
|
867
|
2297 else if (ibyte_leading_byte_p (c) || ibyte_leading_byte_p (ch))
|
771
|
2298 { /* Processing Leading Byte */
|
|
2299 ch = 0;
|
826
|
2300 charset = charset_by_leading_byte (c);
|
|
2301 if (leading_byte_prefix_p(c))
|
771
|
2302 ch = c;
|
|
2303 char_boundary = 0;
|
|
2304 }
|
|
2305 else
|
|
2306 { /* Processing Non-ASCII character */
|
|
2307 char_boundary = 1;
|
|
2308 if (EQ (charset, Vcharset_control_1))
|
2704
|
2309 /* See:
|
|
2310
|
|
2311 (Info-goto-node "(internals)Internal String Encoding")
|
|
2312
|
|
2313 for the rationale behind subtracting #xa0 from the
|
|
2314 character's code. */
|
|
2315 encode_unicode_char (Vcharset_control_1, c - 0xa0, 0, dst,
|
4096
|
2316 type, little_endian, 1);
|
771
|
2317 else
|
|
2318 {
|
|
2319 switch (XCHARSET_REP_BYTES (charset))
|
|
2320 {
|
|
2321 case 2:
|
|
2322 encode_unicode_char (charset, c, 0, dst, type,
|
4096
|
2323 little_endian, 1);
|
771
|
2324 break;
|
|
2325 case 3:
|
|
2326 if (XCHARSET_PRIVATE_P (charset))
|
|
2327 {
|
|
2328 encode_unicode_char (charset, c, 0, dst, type,
|
4096
|
2329 little_endian, 1);
|
771
|
2330 ch = 0;
|
|
2331 }
|
|
2332 else if (ch)
|
|
2333 {
|
|
2334 #ifdef ENABLE_COMPOSITE_CHARS
|
|
2335 if (EQ (charset, Vcharset_composite))
|
|
2336 {
|
|
2337 if (in_composite)
|
|
2338 {
|
|
2339 /* #### Bother! We don't know how to
|
|
2340 handle this yet. */
|
|
2341 encode_unicode_char (Vcharset_ascii, '~', 0,
|
|
2342 dst, type,
|
4096
|
2343 little_endian, 1);
|
771
|
2344 }
|
|
2345 else
|
|
2346 {
|
867
|
2347 Ichar emch = make_ichar (Vcharset_composite,
|
771
|
2348 ch & 0x7F,
|
|
2349 c & 0x7F);
|
|
2350 Lisp_Object lstr =
|
|
2351 composite_char_string (emch);
|
|
2352 saved_n = n;
|
|
2353 saved_src = src;
|
|
2354 in_composite = 1;
|
|
2355 src = XSTRING_DATA (lstr);
|
|
2356 n = XSTRING_LENGTH (lstr);
|
|
2357 }
|
|
2358 }
|
|
2359 else
|
|
2360 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
2361 encode_unicode_char (charset, ch, c, dst, type,
|
4096
|
2362 little_endian, 1);
|
771
|
2363 ch = 0;
|
|
2364 }
|
|
2365 else
|
|
2366 {
|
|
2367 ch = c;
|
|
2368 char_boundary = 0;
|
|
2369 }
|
|
2370 break;
|
|
2371 case 4:
|
|
2372 if (ch)
|
|
2373 {
|
|
2374 encode_unicode_char (charset, ch, c, dst, type,
|
4096
|
2375 little_endian, 1);
|
771
|
2376 ch = 0;
|
|
2377 }
|
|
2378 else
|
|
2379 {
|
|
2380 ch = c;
|
|
2381 char_boundary = 0;
|
|
2382 }
|
|
2383 break;
|
|
2384 default:
|
2500
|
2385 ABORT ();
|
771
|
2386 }
|
|
2387 }
|
|
2388 }
|
|
2389 #endif /* MULE */
|
|
2390 }
|
|
2391
|
|
2392 #ifdef ENABLE_COMPOSITE_CHARS
|
|
2393 if (in_composite)
|
|
2394 {
|
|
2395 n = saved_n;
|
|
2396 src = saved_src;
|
|
2397 in_composite = 0;
|
|
2398 goto back_to_square_n; /* Wheeeeeeeee ..... */
|
|
2399 }
|
|
2400 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
2401
|
|
2402 data->current_char_boundary = char_boundary;
|
|
2403 data->current_charset = charset;
|
|
2404
|
|
2405 /* La palabra se hizo carne! */
|
|
2406 /* A palavra fez-se carne! */
|
|
2407 /* Whatever. */
|
|
2408 }
|
|
2409
|
|
2410 str->ch = ch;
|
|
2411 return orign;
|
|
2412 }
|
|
2413
|
|
2414 /* DEFINE_DETECTOR (utf_7); */
|
|
2415 DEFINE_DETECTOR (utf_8);
|
|
2416 DEFINE_DETECTOR_CATEGORY (utf_8, utf_8);
|
985
|
2417 DEFINE_DETECTOR_CATEGORY (utf_8, utf_8_bom);
|
771
|
2418 DEFINE_DETECTOR (ucs_4);
|
|
2419 DEFINE_DETECTOR_CATEGORY (ucs_4, ucs_4);
|
|
2420 DEFINE_DETECTOR (utf_16);
|
|
2421 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16);
|
|
2422 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian);
|
|
2423 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16_bom);
|
|
2424 DEFINE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian_bom);
|
|
2425
|
|
2426 struct ucs_4_detector
|
|
2427 {
|
|
2428 int in_ucs_4_byte;
|
|
2429 };
|
|
2430
|
|
2431 static void
|
|
2432 ucs_4_detect (struct detection_state *st, const UExtbyte *src,
|
|
2433 Bytecount n)
|
|
2434 {
|
|
2435 struct ucs_4_detector *data = DETECTION_STATE_DATA (st, ucs_4);
|
|
2436
|
|
2437 while (n--)
|
|
2438 {
|
|
2439 UExtbyte c = *src++;
|
|
2440 switch (data->in_ucs_4_byte)
|
|
2441 {
|
|
2442 case 0:
|
|
2443 if (c >= 128)
|
|
2444 {
|
|
2445 DET_RESULT (st, ucs_4) = DET_NEARLY_IMPOSSIBLE;
|
|
2446 return;
|
|
2447 }
|
|
2448 else
|
|
2449 data->in_ucs_4_byte++;
|
|
2450 break;
|
|
2451 case 3:
|
|
2452 data->in_ucs_4_byte = 0;
|
|
2453 break;
|
|
2454 default:
|
|
2455 data->in_ucs_4_byte++;
|
|
2456 }
|
|
2457 }
|
|
2458
|
|
2459 /* !!#### write this for real */
|
|
2460 DET_RESULT (st, ucs_4) = DET_AS_LIKELY_AS_UNLIKELY;
|
|
2461 }
|
|
2462
|
|
2463 struct utf_16_detector
|
|
2464 {
|
|
2465 unsigned int seen_ffff:1;
|
|
2466 unsigned int seen_forward_bom:1;
|
|
2467 unsigned int seen_rev_bom:1;
|
|
2468 int byteno;
|
|
2469 int prev_char;
|
|
2470 int text, rev_text;
|
1267
|
2471 int sep, rev_sep;
|
|
2472 int num_ascii;
|
771
|
2473 };
|
|
2474
|
|
2475 static void
|
|
2476 utf_16_detect (struct detection_state *st, const UExtbyte *src,
|
|
2477 Bytecount n)
|
|
2478 {
|
|
2479 struct utf_16_detector *data = DETECTION_STATE_DATA (st, utf_16);
|
|
2480
|
|
2481 while (n--)
|
|
2482 {
|
|
2483 UExtbyte c = *src++;
|
|
2484 int prevc = data->prev_char;
|
|
2485 if (data->byteno == 1 && c == 0xFF && prevc == 0xFE)
|
|
2486 data->seen_forward_bom = 1;
|
|
2487 else if (data->byteno == 1 && c == 0xFE && prevc == 0xFF)
|
|
2488 data->seen_rev_bom = 1;
|
|
2489
|
|
2490 if (data->byteno & 1)
|
|
2491 {
|
|
2492 if (c == 0xFF && prevc == 0xFF)
|
|
2493 data->seen_ffff = 1;
|
|
2494 if (prevc == 0
|
|
2495 && (c == '\r' || c == '\n'
|
|
2496 || (c >= 0x20 && c <= 0x7E)))
|
|
2497 data->text++;
|
|
2498 if (c == 0
|
|
2499 && (prevc == '\r' || prevc == '\n'
|
|
2500 || (prevc >= 0x20 && prevc <= 0x7E)))
|
|
2501 data->rev_text++;
|
1267
|
2502 /* #### 0x2028 is LINE SEPARATOR and 0x2029 is PARAGRAPH SEPARATOR.
|
|
2503 I used to count these in text and rev_text but that is very bad,
|
|
2504 as 0x2028 is also space + left-paren in ASCII, which is extremely
|
|
2505 common. So, what do we do with these? */
|
771
|
2506 if (prevc == 0x20 && (c == 0x28 || c == 0x29))
|
1267
|
2507 data->sep++;
|
771
|
2508 if (c == 0x20 && (prevc == 0x28 || prevc == 0x29))
|
1267
|
2509 data->rev_sep++;
|
771
|
2510 }
|
|
2511
|
1267
|
2512 if ((c >= ' ' && c <= '~') || c == '\n' || c == '\r' || c == '\t' ||
|
|
2513 c == '\f' || c == '\v')
|
|
2514 data->num_ascii++;
|
771
|
2515 data->byteno++;
|
|
2516 data->prev_char = c;
|
|
2517 }
|
|
2518
|
|
2519 {
|
|
2520 int variance_indicates_big_endian =
|
|
2521 (data->text >= 10
|
|
2522 && (data->rev_text == 0
|
|
2523 || data->text / data->rev_text >= 10));
|
|
2524 int variance_indicates_little_endian =
|
|
2525 (data->rev_text >= 10
|
|
2526 && (data->text == 0
|
|
2527 || data->rev_text / data->text >= 10));
|
|
2528
|
|
2529 if (data->seen_ffff)
|
|
2530 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2531 else if (data->seen_forward_bom)
|
|
2532 {
|
|
2533 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2534 if (variance_indicates_big_endian)
|
|
2535 DET_RESULT (st, utf_16_bom) = DET_NEAR_CERTAINTY;
|
|
2536 else if (variance_indicates_little_endian)
|
|
2537 DET_RESULT (st, utf_16_bom) = DET_SOMEWHAT_LIKELY;
|
|
2538 else
|
|
2539 DET_RESULT (st, utf_16_bom) = DET_QUITE_PROBABLE;
|
|
2540 }
|
|
2541 else if (data->seen_forward_bom)
|
|
2542 {
|
|
2543 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2544 if (variance_indicates_big_endian)
|
|
2545 DET_RESULT (st, utf_16_bom) = DET_NEAR_CERTAINTY;
|
|
2546 else if (variance_indicates_little_endian)
|
|
2547 /* #### may need to rethink */
|
|
2548 DET_RESULT (st, utf_16_bom) = DET_SOMEWHAT_LIKELY;
|
|
2549 else
|
|
2550 /* #### may need to rethink */
|
|
2551 DET_RESULT (st, utf_16_bom) = DET_QUITE_PROBABLE;
|
|
2552 }
|
|
2553 else if (data->seen_rev_bom)
|
|
2554 {
|
|
2555 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2556 if (variance_indicates_little_endian)
|
|
2557 DET_RESULT (st, utf_16_little_endian_bom) = DET_NEAR_CERTAINTY;
|
|
2558 else if (variance_indicates_big_endian)
|
|
2559 /* #### may need to rethink */
|
|
2560 DET_RESULT (st, utf_16_little_endian_bom) = DET_SOMEWHAT_LIKELY;
|
|
2561 else
|
|
2562 /* #### may need to rethink */
|
|
2563 DET_RESULT (st, utf_16_little_endian_bom) = DET_QUITE_PROBABLE;
|
|
2564 }
|
|
2565 else if (variance_indicates_big_endian)
|
|
2566 {
|
|
2567 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2568 DET_RESULT (st, utf_16) = DET_SOMEWHAT_LIKELY;
|
|
2569 DET_RESULT (st, utf_16_little_endian) = DET_SOMEWHAT_UNLIKELY;
|
|
2570 }
|
|
2571 else if (variance_indicates_little_endian)
|
|
2572 {
|
|
2573 SET_DET_RESULTS (st, utf_16, DET_NEARLY_IMPOSSIBLE);
|
|
2574 DET_RESULT (st, utf_16) = DET_SOMEWHAT_UNLIKELY;
|
|
2575 DET_RESULT (st, utf_16_little_endian) = DET_SOMEWHAT_LIKELY;
|
|
2576 }
|
|
2577 else
|
1267
|
2578 {
|
|
2579 /* #### FUCKME! There should really be an ASCII detector. This
|
|
2580 would rule out the need to have this built-in here as
|
|
2581 well. --ben */
|
1292
|
2582 int pct_ascii = data->byteno ? (100 * data->num_ascii) / data->byteno
|
|
2583 : 100;
|
1267
|
2584
|
|
2585 if (pct_ascii > 90)
|
|
2586 SET_DET_RESULTS (st, utf_16, DET_QUITE_IMPROBABLE);
|
|
2587 else if (pct_ascii > 75)
|
|
2588 SET_DET_RESULTS (st, utf_16, DET_SOMEWHAT_UNLIKELY);
|
|
2589 else
|
|
2590 SET_DET_RESULTS (st, utf_16, DET_AS_LIKELY_AS_UNLIKELY);
|
|
2591 }
|
771
|
2592 }
|
|
2593 }
|
|
2594
|
|
2595 struct utf_8_detector
|
|
2596 {
|
985
|
2597 int byteno;
|
|
2598 int first_byte;
|
|
2599 int second_byte;
|
1267
|
2600 int prev_byte;
|
771
|
2601 int in_utf_8_byte;
|
1267
|
2602 int recent_utf_8_sequence;
|
|
2603 int seen_bogus_utf8;
|
|
2604 int seen_really_bogus_utf8;
|
|
2605 int seen_2byte_sequence;
|
|
2606 int seen_longer_sequence;
|
|
2607 int seen_iso2022_esc;
|
|
2608 int seen_iso_shift;
|
1887
|
2609 unsigned int seen_utf_bom:1;
|
771
|
2610 };
|
|
2611
|
|
2612 static void
|
|
2613 utf_8_detect (struct detection_state *st, const UExtbyte *src,
|
|
2614 Bytecount n)
|
|
2615 {
|
|
2616 struct utf_8_detector *data = DETECTION_STATE_DATA (st, utf_8);
|
|
2617
|
|
2618 while (n--)
|
|
2619 {
|
|
2620 UExtbyte c = *src++;
|
985
|
2621 switch (data->byteno)
|
|
2622 {
|
|
2623 case 0:
|
|
2624 data->first_byte = c;
|
|
2625 break;
|
|
2626 case 1:
|
|
2627 data->second_byte = c;
|
|
2628 break;
|
|
2629 case 2:
|
|
2630 if (data->first_byte == 0xef &&
|
|
2631 data->second_byte == 0xbb &&
|
|
2632 c == 0xbf)
|
1267
|
2633 data->seen_utf_bom = 1;
|
985
|
2634 break;
|
|
2635 }
|
|
2636
|
771
|
2637 switch (data->in_utf_8_byte)
|
|
2638 {
|
|
2639 case 0:
|
1267
|
2640 if (data->prev_byte == ISO_CODE_ESC && c >= 0x28 && c <= 0x2F)
|
|
2641 data->seen_iso2022_esc++;
|
|
2642 else if (c == ISO_CODE_SI || c == ISO_CODE_SO)
|
|
2643 data->seen_iso_shift++;
|
771
|
2644 else if (c >= 0xfc)
|
|
2645 data->in_utf_8_byte = 5;
|
|
2646 else if (c >= 0xf8)
|
|
2647 data->in_utf_8_byte = 4;
|
|
2648 else if (c >= 0xf0)
|
|
2649 data->in_utf_8_byte = 3;
|
|
2650 else if (c >= 0xe0)
|
|
2651 data->in_utf_8_byte = 2;
|
|
2652 else if (c >= 0xc0)
|
|
2653 data->in_utf_8_byte = 1;
|
|
2654 else if (c >= 0x80)
|
1267
|
2655 data->seen_bogus_utf8++;
|
|
2656 if (data->in_utf_8_byte > 0)
|
|
2657 data->recent_utf_8_sequence = data->in_utf_8_byte;
|
771
|
2658 break;
|
|
2659 default:
|
|
2660 if ((c & 0xc0) != 0x80)
|
1267
|
2661 data->seen_really_bogus_utf8++;
|
|
2662 else
|
771
|
2663 {
|
1267
|
2664 data->in_utf_8_byte--;
|
|
2665 if (data->in_utf_8_byte == 0)
|
|
2666 {
|
|
2667 if (data->recent_utf_8_sequence == 1)
|
|
2668 data->seen_2byte_sequence++;
|
|
2669 else
|
|
2670 {
|
|
2671 assert (data->recent_utf_8_sequence >= 2);
|
|
2672 data->seen_longer_sequence++;
|
|
2673 }
|
|
2674 }
|
771
|
2675 }
|
|
2676 }
|
985
|
2677
|
|
2678 data->byteno++;
|
1267
|
2679 data->prev_byte = c;
|
771
|
2680 }
|
1267
|
2681
|
|
2682 /* either BOM or no BOM, but not both */
|
|
2683 SET_DET_RESULTS (st, utf_8, DET_NEARLY_IMPOSSIBLE);
|
|
2684
|
|
2685
|
|
2686 if (data->seen_utf_bom)
|
|
2687 DET_RESULT (st, utf_8_bom) = DET_NEAR_CERTAINTY;
|
|
2688 else
|
|
2689 {
|
|
2690 if (data->seen_really_bogus_utf8 ||
|
|
2691 data->seen_bogus_utf8 >= 2)
|
|
2692 ; /* bogus */
|
|
2693 else if (data->seen_bogus_utf8)
|
|
2694 DET_RESULT (st, utf_8) = DET_SOMEWHAT_UNLIKELY;
|
|
2695 else if ((data->seen_longer_sequence >= 5 ||
|
|
2696 data->seen_2byte_sequence >= 10) &&
|
|
2697 (!(data->seen_iso2022_esc + data->seen_iso_shift) ||
|
|
2698 (data->seen_longer_sequence * 2 + data->seen_2byte_sequence) /
|
|
2699 (data->seen_iso2022_esc + data->seen_iso_shift) >= 10))
|
|
2700 /* heuristics, heuristics, we love heuristics */
|
|
2701 DET_RESULT (st, utf_8) = DET_QUITE_PROBABLE;
|
|
2702 else if (data->seen_iso2022_esc ||
|
|
2703 data->seen_iso_shift >= 3)
|
|
2704 DET_RESULT (st, utf_8) = DET_SOMEWHAT_UNLIKELY;
|
|
2705 else if (data->seen_longer_sequence ||
|
|
2706 data->seen_2byte_sequence)
|
|
2707 DET_RESULT (st, utf_8) = DET_SOMEWHAT_LIKELY;
|
|
2708 else if (data->seen_iso_shift)
|
|
2709 DET_RESULT (st, utf_8) = DET_SOMEWHAT_UNLIKELY;
|
|
2710 else
|
|
2711 DET_RESULT (st, utf_8) = DET_AS_LIKELY_AS_UNLIKELY;
|
|
2712 }
|
771
|
2713 }
|
|
2714
|
|
2715 static void
|
|
2716 unicode_init_coding_stream (struct coding_stream *str)
|
|
2717 {
|
|
2718 struct unicode_coding_stream *data =
|
|
2719 CODING_STREAM_TYPE_DATA (str, unicode);
|
|
2720 xzero (*data);
|
|
2721 data->current_charset = Qnil;
|
|
2722 }
|
|
2723
|
|
2724 static void
|
|
2725 unicode_rewind_coding_stream (struct coding_stream *str)
|
|
2726 {
|
|
2727 unicode_init_coding_stream (str);
|
|
2728 }
|
|
2729
|
|
2730 static int
|
|
2731 unicode_putprop (Lisp_Object codesys, Lisp_Object key, Lisp_Object value)
|
|
2732 {
|
3767
|
2733 if (EQ (key, Qunicode_type))
|
771
|
2734 {
|
|
2735 enum unicode_type type;
|
|
2736
|
|
2737 if (EQ (value, Qutf_8))
|
|
2738 type = UNICODE_UTF_8;
|
|
2739 else if (EQ (value, Qutf_16))
|
|
2740 type = UNICODE_UTF_16;
|
|
2741 else if (EQ (value, Qutf_7))
|
|
2742 type = UNICODE_UTF_7;
|
|
2743 else if (EQ (value, Qucs_4))
|
|
2744 type = UNICODE_UCS_4;
|
4096
|
2745 else if (EQ (value, Qutf_32))
|
|
2746 type = UNICODE_UTF_32;
|
771
|
2747 else
|
|
2748 invalid_constant ("Invalid Unicode type", key);
|
|
2749
|
|
2750 XCODING_SYSTEM_UNICODE_TYPE (codesys) = type;
|
|
2751 }
|
|
2752 else if (EQ (key, Qlittle_endian))
|
|
2753 XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (codesys) = !NILP (value);
|
|
2754 else if (EQ (key, Qneed_bom))
|
|
2755 XCODING_SYSTEM_UNICODE_NEED_BOM (codesys) = !NILP (value);
|
|
2756 else
|
|
2757 return 0;
|
|
2758 return 1;
|
|
2759 }
|
|
2760
|
|
2761 static Lisp_Object
|
|
2762 unicode_getprop (Lisp_Object coding_system, Lisp_Object prop)
|
|
2763 {
|
3767
|
2764 if (EQ (prop, Qunicode_type))
|
771
|
2765 {
|
|
2766 switch (XCODING_SYSTEM_UNICODE_TYPE (coding_system))
|
|
2767 {
|
|
2768 case UNICODE_UTF_16: return Qutf_16;
|
|
2769 case UNICODE_UTF_8: return Qutf_8;
|
|
2770 case UNICODE_UTF_7: return Qutf_7;
|
|
2771 case UNICODE_UCS_4: return Qucs_4;
|
4096
|
2772 case UNICODE_UTF_32: return Qutf_32;
|
2500
|
2773 default: ABORT ();
|
771
|
2774 }
|
|
2775 }
|
|
2776 else if (EQ (prop, Qlittle_endian))
|
|
2777 return XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (coding_system) ? Qt : Qnil;
|
|
2778 else if (EQ (prop, Qneed_bom))
|
|
2779 return XCODING_SYSTEM_UNICODE_NEED_BOM (coding_system) ? Qt : Qnil;
|
|
2780 return Qunbound;
|
|
2781 }
|
|
2782
|
|
2783 static void
|
2286
|
2784 unicode_print (Lisp_Object cs, Lisp_Object printcharfun,
|
|
2785 int UNUSED (escapeflag))
|
771
|
2786 {
|
3767
|
2787 write_fmt_string_lisp (printcharfun, "(%s", 1,
|
|
2788 unicode_getprop (cs, Qunicode_type));
|
771
|
2789 if (XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (cs))
|
826
|
2790 write_c_string (printcharfun, ", little-endian");
|
771
|
2791 if (XCODING_SYSTEM_UNICODE_NEED_BOM (cs))
|
826
|
2792 write_c_string (printcharfun, ", need-bom");
|
|
2793 write_c_string (printcharfun, ")");
|
771
|
2794 }
|
|
2795
|
|
2796 int
|
2286
|
2797 dfc_coding_system_is_unicode (
|
|
2798 #ifdef WIN32_ANY
|
|
2799 Lisp_Object codesys
|
|
2800 #else
|
|
2801 Lisp_Object UNUSED (codesys)
|
|
2802 #endif
|
|
2803 )
|
771
|
2804 {
|
1315
|
2805 #ifdef WIN32_ANY
|
771
|
2806 codesys = Fget_coding_system (codesys);
|
|
2807 return (EQ (XCODING_SYSTEM_TYPE (codesys), Qunicode) &&
|
|
2808 XCODING_SYSTEM_UNICODE_TYPE (codesys) == UNICODE_UTF_16 &&
|
|
2809 XCODING_SYSTEM_UNICODE_LITTLE_ENDIAN (codesys));
|
|
2810
|
|
2811 #else
|
|
2812 return 0;
|
|
2813 #endif
|
|
2814 }
|
|
2815
|
|
2816
|
|
2817 /************************************************************************/
|
|
2818 /* Initialization */
|
|
2819 /************************************************************************/
|
|
2820
|
|
2821 void
|
|
2822 syms_of_unicode (void)
|
|
2823 {
|
|
2824 #ifdef MULE
|
877
|
2825 DEFSUBR (Funicode_precedence_list);
|
771
|
2826 DEFSUBR (Fset_language_unicode_precedence_list);
|
|
2827 DEFSUBR (Flanguage_unicode_precedence_list);
|
|
2828 DEFSUBR (Fset_default_unicode_precedence_list);
|
|
2829 DEFSUBR (Fdefault_unicode_precedence_list);
|
|
2830 DEFSUBR (Fset_unicode_conversion);
|
|
2831
|
1318
|
2832 DEFSUBR (Fload_unicode_mapping_table);
|
771
|
2833
|
3439
|
2834 DEFSYMBOL (Qccl_encode_to_ucs_2);
|
|
2835 DEFSYMBOL (Qlast_allocated_character);
|
771
|
2836 DEFSYMBOL (Qignore_first_column);
|
3659
|
2837
|
|
2838 DEFSYMBOL (Qunicode_registries);
|
771
|
2839 #endif /* MULE */
|
|
2840
|
800
|
2841 DEFSUBR (Fchar_to_unicode);
|
|
2842 DEFSUBR (Funicode_to_char);
|
771
|
2843
|
|
2844 DEFSYMBOL (Qunicode);
|
|
2845 DEFSYMBOL (Qucs_4);
|
|
2846 DEFSYMBOL (Qutf_16);
|
4096
|
2847 DEFSYMBOL (Qutf_32);
|
771
|
2848 DEFSYMBOL (Qutf_8);
|
|
2849 DEFSYMBOL (Qutf_7);
|
|
2850
|
|
2851 DEFSYMBOL (Qneed_bom);
|
|
2852
|
|
2853 DEFSYMBOL (Qutf_16);
|
|
2854 DEFSYMBOL (Qutf_16_little_endian);
|
|
2855 DEFSYMBOL (Qutf_16_bom);
|
|
2856 DEFSYMBOL (Qutf_16_little_endian_bom);
|
985
|
2857
|
|
2858 DEFSYMBOL (Qutf_8);
|
|
2859 DEFSYMBOL (Qutf_8_bom);
|
771
|
2860 }
|
|
2861
|
|
2862 void
|
|
2863 coding_system_type_create_unicode (void)
|
|
2864 {
|
|
2865 INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA (unicode, "unicode-coding-system-p");
|
|
2866 CODING_SYSTEM_HAS_METHOD (unicode, print);
|
|
2867 CODING_SYSTEM_HAS_METHOD (unicode, convert);
|
|
2868 CODING_SYSTEM_HAS_METHOD (unicode, init_coding_stream);
|
|
2869 CODING_SYSTEM_HAS_METHOD (unicode, rewind_coding_stream);
|
|
2870 CODING_SYSTEM_HAS_METHOD (unicode, putprop);
|
|
2871 CODING_SYSTEM_HAS_METHOD (unicode, getprop);
|
|
2872
|
|
2873 INITIALIZE_DETECTOR (utf_8);
|
|
2874 DETECTOR_HAS_METHOD (utf_8, detect);
|
|
2875 INITIALIZE_DETECTOR_CATEGORY (utf_8, utf_8);
|
985
|
2876 INITIALIZE_DETECTOR_CATEGORY (utf_8, utf_8_bom);
|
771
|
2877
|
|
2878 INITIALIZE_DETECTOR (ucs_4);
|
|
2879 DETECTOR_HAS_METHOD (ucs_4, detect);
|
|
2880 INITIALIZE_DETECTOR_CATEGORY (ucs_4, ucs_4);
|
|
2881
|
|
2882 INITIALIZE_DETECTOR (utf_16);
|
|
2883 DETECTOR_HAS_METHOD (utf_16, detect);
|
|
2884 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16);
|
|
2885 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian);
|
|
2886 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16_bom);
|
|
2887 INITIALIZE_DETECTOR_CATEGORY (utf_16, utf_16_little_endian_bom);
|
|
2888 }
|
|
2889
|
|
2890 void
|
|
2891 reinit_coding_system_type_create_unicode (void)
|
|
2892 {
|
|
2893 REINITIALIZE_CODING_SYSTEM_TYPE (unicode);
|
|
2894 }
|
|
2895
|
|
2896 void
|
|
2897 vars_of_unicode (void)
|
|
2898 {
|
|
2899 Fprovide (intern ("unicode"));
|
|
2900
|
|
2901 #ifdef MULE
|
|
2902 staticpro (&Vlanguage_unicode_precedence_list);
|
|
2903 Vlanguage_unicode_precedence_list = Qnil;
|
|
2904
|
|
2905 staticpro (&Vdefault_unicode_precedence_list);
|
|
2906 Vdefault_unicode_precedence_list = Qnil;
|
|
2907
|
|
2908 unicode_precedence_dynarr = Dynarr_new (Lisp_Object);
|
2367
|
2909 dump_add_root_block_ptr (&unicode_precedence_dynarr,
|
771
|
2910 &lisp_object_dynarr_description);
|
2367
|
2911
|
3659
|
2912
|
|
2913
|
2367
|
2914 init_blank_unicode_tables ();
|
|
2915
|
3439
|
2916 staticpro (&Vcurrent_jit_charset);
|
|
2917 Vcurrent_jit_charset = Qnil;
|
|
2918
|
2367
|
2919 /* Note that the "block" we are describing is a single pointer, and hence
|
|
2920 we could potentially use dump_add_root_block_ptr(). However, given
|
|
2921 the way the descriptions are written, we couldn't use them, and would
|
|
2922 have to write new descriptions for each of the pointers below, since
|
|
2923 we would have to make use of a description with an XD_BLOCK_ARRAY
|
|
2924 in it. */
|
|
2925
|
|
2926 dump_add_root_block (&to_unicode_blank_1, sizeof (void *),
|
|
2927 to_unicode_level_1_desc_1);
|
|
2928 dump_add_root_block (&to_unicode_blank_2, sizeof (void *),
|
|
2929 to_unicode_level_2_desc_1);
|
|
2930
|
|
2931 dump_add_root_block (&from_unicode_blank_1, sizeof (void *),
|
|
2932 from_unicode_level_1_desc_1);
|
|
2933 dump_add_root_block (&from_unicode_blank_2, sizeof (void *),
|
|
2934 from_unicode_level_2_desc_1);
|
|
2935 dump_add_root_block (&from_unicode_blank_3, sizeof (void *),
|
|
2936 from_unicode_level_3_desc_1);
|
|
2937 dump_add_root_block (&from_unicode_blank_4, sizeof (void *),
|
|
2938 from_unicode_level_4_desc_1);
|
3659
|
2939
|
|
2940 DEFVAR_LISP ("unicode-registries", &Qunicode_registries /*
|
|
2941 Vector describing the X11 registries searched when using fallback fonts.
|
|
2942
|
|
2943 "Fallback fonts" here includes by default those fonts used by redisplay when
|
|
2944 displaying charsets for which the `encode-as-utf-8' property is true, and
|
|
2945 those used when no font matching the charset's registries property has been
|
|
2946 found (that is, they're probably Mule-specific charsets like Ethiopic or
|
|
2947 IPA.)
|
|
2948 */ );
|
|
2949 Qunicode_registries = vector1(build_string("iso10646-1"));
|
771
|
2950 #endif /* MULE */
|
|
2951 }
|