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