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