| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/tads3/charmap.cpp,v 1.3 1999/07/11 00:46:58 MJRoberts Exp $"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | charmap.cpp - character mapper |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 10/17/98 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "t3std.h" |
| 27 | #include "os.h" |
| 28 | #include "utf8.h" |
| 29 | #include "resload.h" |
| 30 | #include "charmap.h" |
| 31 | #include "vmdatasrc.h" |
| 32 | |
| 33 | |
| 34 | /* ------------------------------------------------------------------------ */ |
| 35 | /* |
| 36 | * Basic Mapper Class |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | * match a name prefix (for the various Latin-X synonym names) |
| 41 | */ |
| 42 | static int prefixeq(const char *table_name, size_t digit_ofs, |
| 43 | const char *prefix) |
| 44 | { |
| 45 | return (digit_ofs == strlen(prefix) |
| 46 | && memicmp(table_name, prefix, digit_ofs) == 0); |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Open a mapping file, applying synonyms for character set names |
| 51 | */ |
| 52 | osfildef *CCharmap::open_map_file_syn(class CResLoader *res_loader, |
| 53 | const char *table_name, |
| 54 | charmap_type_t *map_type) |
| 55 | { |
| 56 | osfildef *fp; |
| 57 | |
| 58 | /* try opening the file/resource with the exact name given */ |
| 59 | if ((fp = open_map_file(res_loader, table_name, map_type)) != 0) |
| 60 | return fp; |
| 61 | |
| 62 | /* |
| 63 | * We didn't find a file with the exact name given. Try various |
| 64 | * synonyms for the Latin-X character sets. |
| 65 | */ |
| 66 | const char *p; |
| 67 | for (p = table_name + strlen(table_name) ; |
| 68 | p > table_name && is_digit(*(p-1)) ; --p) ; |
| 69 | size_t ofs = p - table_name; |
| 70 | int num = atoi(p); |
| 71 | int found_suffix = (ofs != strlen(p) && num != 0); |
| 72 | if (found_suffix) |
| 73 | { |
| 74 | if (prefixeq(table_name, ofs, "latin") |
| 75 | || prefixeq(table_name, ofs, "latin-") |
| 76 | || prefixeq(table_name, ofs, "iso") |
| 77 | || prefixeq(table_name, ofs, "iso-") |
| 78 | || prefixeq(table_name, ofs, "8859-") |
| 79 | || prefixeq(table_name, ofs, "iso8859-") |
| 80 | || prefixeq(table_name, ofs, "iso-8859-") |
| 81 | || prefixeq(table_name, ofs, "iso_8859-") |
| 82 | || prefixeq(table_name, ofs, "iso_8859_") |
| 83 | || prefixeq(table_name, ofs, "l")) |
| 84 | { |
| 85 | /* rebuild the name as "isoX" */ |
| 86 | char buf[25]; |
| 87 | sprintf(buf, "iso%d", num); |
| 88 | |
| 89 | /* try loading it */ |
| 90 | if ((fp = open_map_file(res_loader, buf, map_type)) != 0) |
| 91 | return fp; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * if that didn't work, and the whole string looks like just a number, |
| 97 | * try putting "cp" in front of it - i.e., allow loading cp1252 as just |
| 98 | * "1252" |
| 99 | */ |
| 100 | if (found_suffix && p == table_name) |
| 101 | { |
| 102 | /* it's all digits - try making it "cpXXXX" */ |
| 103 | char buf[25]; |
| 104 | sprintf(buf, "cp%d", num); |
| 105 | if ((fp = open_map_file(res_loader, buf, map_type)) != 0) |
| 106 | return fp; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * if we still don't have a file, check for "winXXXX" and "dosXXXX", as |
| 111 | * synonyms for cpXXXX |
| 112 | */ |
| 113 | if (found_suffix |
| 114 | && (prefixeq(table_name, ofs, "win") |
| 115 | || prefixeq(table_name, ofs, "win-") |
| 116 | || prefixeq(table_name, ofs, "windows") |
| 117 | || prefixeq(table_name, ofs, "windows-") |
| 118 | || prefixeq(table_name, ofs, "dos") |
| 119 | || prefixeq(table_name, ofs, "dos-"))) |
| 120 | { |
| 121 | /* try making it "cpXXXX" */ |
| 122 | char buf[25]; |
| 123 | sprintf(buf, "cp%d", num); |
| 124 | if ((fp = open_map_file(res_loader, buf, map_type)) != 0) |
| 125 | return fp; |
| 126 | } |
| 127 | |
| 128 | /* we couldn't find an alternative name; give up */ |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Open and characterize a mapping file |
| 134 | */ |
| 135 | osfildef *CCharmap::open_map_file(class CResLoader *res_loader, |
| 136 | const char *table_name, |
| 137 | charmap_type_t *map_type) |
| 138 | { |
| 139 | osfildef *fp; |
| 140 | char respath[100]; |
| 141 | ulong startpos; |
| 142 | uchar buf[256]; |
| 143 | uint entry_cnt; |
| 144 | int found_single; |
| 145 | int found_double; |
| 146 | |
| 147 | /* |
| 148 | * Generate the full resource path - character mapping resource paths |
| 149 | * always start with "charmap/" followed by the table name, plus the |
| 150 | * ".tcm" extension. We use lower-case names for the mapping files, so |
| 151 | * explicitly convert to lower, in case we're on a case-sensitive file |
| 152 | * system like Unix. |
| 153 | */ |
| 154 | t3sprintf(respath, sizeof(respath), "charmap/%s.tcm", table_name); |
| 155 | t3strlwr(respath); |
| 156 | |
| 157 | /* open the file for the character set */ |
| 158 | fp = res_loader->open_res_file(respath, "charmap/cmaplib", "CLIB"); |
| 159 | |
| 160 | /* if we couldn't open the mapping file, return failure */ |
| 161 | if (fp == 0) |
| 162 | return 0; |
| 163 | |
| 164 | /* note the initial seek position */ |
| 165 | startpos = osfpos(fp); |
| 166 | |
| 167 | /* read the header and the local-to-unicode header */ |
| 168 | if (osfrb(fp, buf, 6)) |
| 169 | goto fail; |
| 170 | |
| 171 | /* get the number of entries from the local-to-unicode header */ |
| 172 | entry_cnt = osrp2(buf + 4); |
| 173 | |
| 174 | /* |
| 175 | * Scan the entries to determine if we have single-byte, |
| 176 | * double-byte, or both. |
| 177 | */ |
| 178 | found_single = found_double = FALSE; |
| 179 | while (entry_cnt > 0) |
| 180 | { |
| 181 | size_t cur; |
| 182 | const uchar *p; |
| 183 | |
| 184 | /* read up to a buffer-full or the remaining size */ |
| 185 | cur = sizeof(buf)/4; |
| 186 | if (cur > entry_cnt) |
| 187 | cur = entry_cnt; |
| 188 | |
| 189 | /* read it */ |
| 190 | if (osfrb(fp, buf, cur*4)) |
| 191 | goto fail; |
| 192 | |
| 193 | /* deduct the amount we just read from the amount remaining */ |
| 194 | entry_cnt -= cur; |
| 195 | |
| 196 | /* scan the entries */ |
| 197 | for (p = buf ; cur > 0 ; --cur, p += 4) |
| 198 | { |
| 199 | /* |
| 200 | * Note whether this is a single-byte or double-byte entry. |
| 201 | * If the high-order byte is non-zero, it's a double-byte |
| 202 | * entry; otherwise, it's a single-byte entry. |
| 203 | * |
| 204 | * Note that we read the UINT2 at (p+2), because that's the |
| 205 | * local character-set code point in this tuple. |
| 206 | */ |
| 207 | if (((uint)osrp2(p + 2)) > 0xFF) |
| 208 | found_double = TRUE; |
| 209 | else |
| 210 | found_single = TRUE; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * if we've found both single- and double-byte characters so |
| 215 | * far, there's no need to look any further, since we know |
| 216 | * everything about the file now |
| 217 | */ |
| 218 | if (found_single && found_double) |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * create the appropriate mapper, depending on whether we found |
| 224 | * single, double, or mixed characters |
| 225 | */ |
| 226 | if (found_single && found_double) |
| 227 | { |
| 228 | /* it's mixed */ |
| 229 | *map_type = CHARMAP_TYPE_MB; |
| 230 | } |
| 231 | else if (found_double) |
| 232 | { |
| 233 | /* it's all double-byte */ |
| 234 | *map_type = CHARMAP_TYPE_DB; |
| 235 | } |
| 236 | else if (found_single) |
| 237 | { |
| 238 | /* it's all single-byte */ |
| 239 | *map_type = CHARMAP_TYPE_SB; |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | /* no mappings found at all - presume it's a single-byte mapper */ |
| 244 | *map_type = CHARMAP_TYPE_SB; |
| 245 | } |
| 246 | |
| 247 | /* seek back to the start of the table */ |
| 248 | osfseek(fp, startpos, OSFSK_SET); |
| 249 | |
| 250 | /* return the file pointer */ |
| 251 | return fp; |
| 252 | |
| 253 | fail: |
| 254 | /* close the file and return failure */ |
| 255 | osfcls(fp); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /* ------------------------------------------------------------------------ */ |
| 260 | /* |
| 261 | * Special built-in mapper to 7-bit ASCII. This is available as a last |
| 262 | * resort when no external mapping file can be found. |
| 263 | */ |
| 264 | |
| 265 | /* |
| 266 | * create a plain ascii translator |
| 267 | */ |
| 268 | CCharmapToLocalASCII::CCharmapToLocalASCII() |
| 269 | { |
| 270 | unsigned char *dst; |
| 271 | wchar_t *exp_dst; |
| 272 | size_t siz; |
| 273 | size_t exp_siz; |
| 274 | struct ascii_map_t |
| 275 | { |
| 276 | wchar_t uni; |
| 277 | char asc[5]; |
| 278 | }; |
| 279 | ascii_map_t *p; |
| 280 | static ascii_map_t ascii_mapping[] = |
| 281 | { |
| 282 | /* regular ASCII characters */ |
| 283 | { 1, { 1 } }, |
| 284 | { 2, { 2 } }, |
| 285 | { 3, { 3 } }, |
| 286 | { 4, { 4 } }, |
| 287 | { 5, { 5 } }, |
| 288 | { 6, { 6 } }, |
| 289 | { 7, { 7 } }, |
| 290 | { 8, { 8 } }, |
| 291 | { 9, { 9 } }, |
| 292 | { 10, { 10 } }, |
| 293 | { 11, { 11 } }, |
| 294 | { 12, { 12 } }, |
| 295 | { 13, { 13 } }, |
| 296 | { 14, { 14 } }, |
| 297 | { 15, { 15 } }, |
| 298 | { 16, { 16 } }, |
| 299 | { 17, { 17 } }, |
| 300 | { 18, { 18 } }, |
| 301 | { 19, { 19 } }, |
| 302 | { 20, { 20 } }, |
| 303 | { 21, { 21 } }, |
| 304 | { 22, { 22 } }, |
| 305 | { 23, { 23 } }, |
| 306 | { 24, { 24 } }, |
| 307 | { 25, { 25 } }, |
| 308 | { 26, { 26 } }, |
| 309 | { 27, { 27 } }, |
| 310 | { 28, { 28 } }, |
| 311 | { 29, { 29 } }, |
| 312 | { 30, { 30 } }, |
| 313 | { 31, { 31 } }, |
| 314 | { 32, { 32 } }, |
| 315 | { 33, { 33 } }, |
| 316 | { 34, { 34 } }, |
| 317 | { 35, { 35 } }, |
| 318 | { 36, { 36 } }, |
| 319 | { 37, { 37 } }, |
| 320 | { 38, { 38 } }, |
| 321 | { 39, { 39 } }, |
| 322 | { 40, { 40 } }, |
| 323 | { 41, { 41 } }, |
| 324 | { 42, { 42 } }, |
| 325 | { 43, { 43 } }, |
| 326 | { 44, { 44 } }, |
| 327 | { 45, { 45 } }, |
| 328 | { 46, { 46 } }, |
| 329 | { 47, { 47 } }, |
| 330 | { 48, { 48 } }, |
| 331 | { 49, { 49 } }, |
| 332 | { 50, { 50 } }, |
| 333 | { 51, { 51 } }, |
| 334 | { 52, { 52 } }, |
| 335 | { 53, { 53 } }, |
| 336 | { 54, { 54 } }, |
| 337 | { 55, { 55 } }, |
| 338 | { 56, { 56 } }, |
| 339 | { 57, { 57 } }, |
| 340 | { 58, { 58 } }, |
| 341 | { 59, { 59 } }, |
| 342 | { 60, { 60 } }, |
| 343 | { 61, { 61 } }, |
| 344 | { 62, { 62 } }, |
| 345 | { 63, { 63 } }, |
| 346 | { 64, { 64 } }, |
| 347 | { 65, { 65 } }, |
| 348 | { 66, { 66 } }, |
| 349 | { 67, { 67 } }, |
| 350 | { 68, { 68 } }, |
| 351 | { 69, { 69 } }, |
| 352 | { 70, { 70 } }, |
| 353 | { 71, { 71 } }, |
| 354 | { 72, { 72 } }, |
| 355 | { 73, { 73 } }, |
| 356 | { 74, { 74 } }, |
| 357 | { 75, { 75 } }, |
| 358 | { 76, { 76 } }, |
| 359 | { 77, { 77 } }, |
| 360 | { 78, { 78 } }, |
| 361 | { 79, { 79 } }, |
| 362 | { 80, { 80 } }, |
| 363 | { 81, { 81 } }, |
| 364 | { 82, { 82 } }, |
| 365 | { 83, { 83 } }, |
| 366 | { 84, { 84 } }, |
| 367 | { 85, { 85 } }, |
| 368 | { 86, { 86 } }, |
| 369 | { 87, { 87 } }, |
| 370 | { 88, { 88 } }, |
| 371 | { 89, { 89 } }, |
| 372 | { 90, { 90 } }, |
| 373 | { 91, { 91 } }, |
| 374 | { 92, { 92 } }, |
| 375 | { 93, { 93 } }, |
| 376 | { 94, { 94 } }, |
| 377 | { 95, { 95 } }, |
| 378 | { 96, { 96 } }, |
| 379 | { 97, { 97 } }, |
| 380 | { 98, { 98 } }, |
| 381 | { 99, { 99 } }, |
| 382 | { 100, { 100 } }, |
| 383 | { 101, { 101 } }, |
| 384 | { 102, { 102 } }, |
| 385 | { 103, { 103 } }, |
| 386 | { 104, { 104 } }, |
| 387 | { 105, { 105 } }, |
| 388 | { 106, { 106 } }, |
| 389 | { 107, { 107 } }, |
| 390 | { 108, { 108 } }, |
| 391 | { 109, { 109 } }, |
| 392 | { 110, { 110 } }, |
| 393 | { 111, { 111 } }, |
| 394 | { 112, { 112 } }, |
| 395 | { 113, { 113 } }, |
| 396 | { 114, { 114 } }, |
| 397 | { 115, { 115 } }, |
| 398 | { 116, { 116 } }, |
| 399 | { 117, { 117 } }, |
| 400 | { 118, { 118 } }, |
| 401 | { 119, { 119 } }, |
| 402 | { 120, { 120 } }, |
| 403 | { 121, { 121 } }, |
| 404 | { 122, { 122 } }, |
| 405 | { 123, { 123 } }, |
| 406 | { 124, { 124 } }, |
| 407 | { 125, { 125 } }, |
| 408 | { 126, { 126 } }, |
| 409 | { 127, { 127 } }, |
| 410 | |
| 411 | /* Latin-1 accented characters and symbols */ |
| 412 | { 353, "s" }, |
| 413 | { 352, "S" }, |
| 414 | { 8218, "\'" }, |
| 415 | { 8222, "\"" }, |
| 416 | { 8249, "<" }, |
| 417 | { 338, "OE" }, |
| 418 | { 8216, "\'" }, |
| 419 | { 8217, "\'" }, |
| 420 | { 8220, "\"" }, |
| 421 | { 8221, "\"" }, |
| 422 | { 8211, "-" }, |
| 423 | { 8212, "--" }, |
| 424 | { 8482, "(tm)" }, |
| 425 | { 8250, ">" }, |
| 426 | { 339, "oe" }, |
| 427 | { 376, "Y" }, |
| 428 | { 162, "c" }, |
| 429 | { 163, "L" }, |
| 430 | { 165, "Y" }, |
| 431 | { 166, "|" }, |
| 432 | { 169, "(c)" }, |
| 433 | { 170, "a" }, |
| 434 | { 173, " " }, |
| 435 | { 174, "(R)" }, |
| 436 | { 175, "-" }, |
| 437 | { 177, "+/-" }, |
| 438 | { 178, "2" }, |
| 439 | { 179, "3" }, |
| 440 | { 180, "\'" }, |
| 441 | { 181, "u" }, |
| 442 | { 182, "P" }, |
| 443 | { 183, "*" }, |
| 444 | { 184, "," }, |
| 445 | { 185, "1" }, |
| 446 | { 186, "o" }, |
| 447 | { 171, "<<" }, |
| 448 | { 187, ">>" }, |
| 449 | { 188, "1/4" }, |
| 450 | { 189, "1/2" }, |
| 451 | { 190, "3/4" }, |
| 452 | { 192, "A" }, |
| 453 | { 193, "A" }, |
| 454 | { 194, "A" }, |
| 455 | { 195, "A" }, |
| 456 | { 196, "A" }, |
| 457 | { 197, "A" }, |
| 458 | { 198, "AE" }, |
| 459 | { 199, "C" }, |
| 460 | { 200, "E" }, |
| 461 | { 201, "E" }, |
| 462 | { 202, "E" }, |
| 463 | { 203, "E" }, |
| 464 | { 204, "I" }, |
| 465 | { 205, "I" }, |
| 466 | { 206, "I" }, |
| 467 | { 207, "I" }, |
| 468 | { 209, "N" }, |
| 469 | { 210, "O" }, |
| 470 | { 211, "O" }, |
| 471 | { 212, "O" }, |
| 472 | { 213, "O" }, |
| 473 | { 214, "O" }, |
| 474 | { 215, "x" }, |
| 475 | { 216, "O" }, |
| 476 | { 217, "U" }, |
| 477 | { 218, "U" }, |
| 478 | { 219, "U" }, |
| 479 | { 220, "U" }, |
| 480 | { 221, "Y" }, |
| 481 | { 223, "ss" }, |
| 482 | { 224, "a" }, |
| 483 | { 225, "a" }, |
| 484 | { 226, "a" }, |
| 485 | { 227, "a" }, |
| 486 | { 228, "a" }, |
| 487 | { 229, "a" }, |
| 488 | { 230, "ae" }, |
| 489 | { 231, "c" }, |
| 490 | { 232, "e" }, |
| 491 | { 233, "e" }, |
| 492 | { 234, "e" }, |
| 493 | { 235, "e" }, |
| 494 | { 236, "i" }, |
| 495 | { 237, "i" }, |
| 496 | { 238, "i" }, |
| 497 | { 239, "i" }, |
| 498 | { 241, "n" }, |
| 499 | { 242, "o" }, |
| 500 | { 243, "o" }, |
| 501 | { 244, "o" }, |
| 502 | { 245, "o" }, |
| 503 | { 246, "o" }, |
| 504 | { 247, "/" }, |
| 505 | { 248, "o" }, |
| 506 | { 249, "u" }, |
| 507 | { 250, "u" }, |
| 508 | { 251, "u" }, |
| 509 | { 252, "u" }, |
| 510 | { 253, "y" }, |
| 511 | { 255, "y" }, |
| 512 | { 710, "^" }, |
| 513 | { 732, "~" }, |
| 514 | |
| 515 | /* math symbols */ |
| 516 | { 402, "f" }, |
| 517 | |
| 518 | /* other symbols */ |
| 519 | { 8226, "*" }, |
| 520 | |
| 521 | /* arrows */ |
| 522 | { 8592, "<-" }, |
| 523 | { 8594, "->" }, |
| 524 | |
| 525 | /* several capital Greek letters look a lot like Roman letters */ |
| 526 | { 913, "A" }, |
| 527 | { 914, "B" }, |
| 528 | { 918, "Z" }, |
| 529 | { 919, "H" }, |
| 530 | { 921, "I" }, |
| 531 | { 922, "K" }, |
| 532 | { 924, "M" }, |
| 533 | { 925, "N" }, |
| 534 | { 927, "O" }, |
| 535 | { 929, "P" }, |
| 536 | { 932, "T" }, |
| 537 | { 933, "Y" }, |
| 538 | { 935, "X" }, |
| 539 | |
| 540 | /* Latin-2 accented characters */ |
| 541 | { 260, "A" }, |
| 542 | { 321, "L" }, |
| 543 | { 317, "L" }, |
| 544 | { 346, "S" }, |
| 545 | { 350, "S" }, |
| 546 | { 356, "T" }, |
| 547 | { 377, "Z" }, |
| 548 | { 381, "Z" }, |
| 549 | { 379, "Z" }, |
| 550 | { 261, "a" }, |
| 551 | { 731, "o" }, |
| 552 | { 322, "l" }, |
| 553 | { 318, "l" }, |
| 554 | { 347, "s" }, |
| 555 | { 351, "s" }, |
| 556 | { 357, "t" }, |
| 557 | { 378, "z" }, |
| 558 | { 733, "\"" }, |
| 559 | { 382, "z" }, |
| 560 | { 380, "z" }, |
| 561 | { 340, "R" }, |
| 562 | { 258, "A" }, |
| 563 | { 313, "L" }, |
| 564 | { 262, "C" }, |
| 565 | { 268, "C" }, |
| 566 | { 280, "E" }, |
| 567 | { 282, "E" }, |
| 568 | { 270, "D" }, |
| 569 | { 272, "D" }, |
| 570 | { 323, "N" }, |
| 571 | { 327, "N" }, |
| 572 | { 336, "O" }, |
| 573 | { 344, "R" }, |
| 574 | { 366, "U" }, |
| 575 | { 368, "U" }, |
| 576 | { 354, "T" }, |
| 577 | { 341, "r" }, |
| 578 | { 259, "a" }, |
| 579 | { 314, "l" }, |
| 580 | { 263, "c" }, |
| 581 | { 269, "c" }, |
| 582 | { 281, "e" }, |
| 583 | { 283, "e" }, |
| 584 | { 271, "d" }, |
| 585 | { 273, "d" }, |
| 586 | { 324, "n" }, |
| 587 | { 328, "n" }, |
| 588 | { 337, "o" }, |
| 589 | { 345, "r" }, |
| 590 | { 367, "u" }, |
| 591 | { 369, "u" }, |
| 592 | { 355, "t" }, |
| 593 | { 0, { 0 } } |
| 594 | }; |
| 595 | |
| 596 | /* determine how much space we'll need in the translation array */ |
| 597 | for (p = ascii_mapping, siz = 0, exp_siz = 0 ; p->uni != 0 ; ++p) |
| 598 | { |
| 599 | /* we need space for this mapping string, plus a length prefix byte */ |
| 600 | siz += strlen(p->asc) + 1; |
| 601 | |
| 602 | /* |
| 603 | * if this is a multi-character expansion, count it in the |
| 604 | * expansion array size |
| 605 | */ |
| 606 | if (strlen(p->asc) > 1) |
| 607 | exp_siz += strlen(p->asc) + 1; |
| 608 | } |
| 609 | |
| 610 | /* add in space for the default entry */ |
| 611 | siz += 2; |
| 612 | |
| 613 | /* allocate the translation array */ |
| 614 | xlat_array_ = (unsigned char *)t3malloc(siz); |
| 615 | |
| 616 | /* |
| 617 | * allocate the expansion array; allocate one extra entry for the null |
| 618 | * mapping at index zero |
| 619 | */ |
| 620 | exp_array_ = (wchar_t *)t3malloc((exp_siz + 1) * sizeof(wchar_t)); |
| 621 | |
| 622 | /* |
| 623 | * start at element 1 of the expansion array (element zero is reserved |
| 624 | * to indicate the null mapping) |
| 625 | */ |
| 626 | dst = xlat_array_; |
| 627 | exp_dst = exp_array_ + 1; |
| 628 | |
| 629 | /* |
| 630 | * Add the zeroeth entry, which serves as the default mapping for |
| 631 | * characters that aren't otherwise mappable. |
| 632 | */ |
| 633 | set_mapping(0, 0); |
| 634 | *dst++ = 1; |
| 635 | *dst++ = '?'; |
| 636 | |
| 637 | /* set up the arrays */ |
| 638 | for (p = ascii_mapping ; p->uni != 0 ; ++p) |
| 639 | { |
| 640 | size_t len; |
| 641 | |
| 642 | /* set the mapping's offset in the translation array */ |
| 643 | set_mapping(p->uni, dst - xlat_array_); |
| 644 | |
| 645 | /* get the length of this mapping */ |
| 646 | len = strlen(p->asc); |
| 647 | |
| 648 | /* set this mapping's length */ |
| 649 | *dst++ = (unsigned char)len; |
| 650 | |
| 651 | /* copy the mapping */ |
| 652 | memcpy(dst, p->asc, len); |
| 653 | |
| 654 | /* move past the mapping in the translation array */ |
| 655 | dst += len; |
| 656 | |
| 657 | /* add the expansion mapping if necessary */ |
| 658 | if (len > 1) |
| 659 | { |
| 660 | size_t i; |
| 661 | |
| 662 | /* add an expansion mapping */ |
| 663 | set_exp_mapping(p->uni, exp_dst - exp_array_); |
| 664 | |
| 665 | /* set the length prefix */ |
| 666 | *exp_dst++ = (wchar_t)len; |
| 667 | |
| 668 | /* add the mapping */ |
| 669 | for (i = 0 ; i < len ; ++i) |
| 670 | *exp_dst++ = (wchar_t)p->asc[i]; |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | /* ------------------------------------------------------------------------ */ |
| 676 | /* |
| 677 | * Special built-in mapper to ISO-8859-1. Because of the widespread use |
| 678 | * of this character set, we make this mapping available even when no |
| 679 | * external mapping file is available. |
| 680 | */ |
| 681 | |
| 682 | /* |
| 683 | * create an 8859-1 mapper |
| 684 | */ |
| 685 | CCharmapToLocal8859_1::CCharmapToLocal8859_1() |
| 686 | { |
| 687 | unsigned char *dst; |
| 688 | size_t siz; |
| 689 | wchar_t c; |
| 690 | |
| 691 | /* |
| 692 | * Determine how much space we'll need in the translation array - we |
| 693 | * need one byte for each character, plus one byte for the length of |
| 694 | * each character. We also need two bytes for the default entry. |
| 695 | */ |
| 696 | siz = 256 + 256 + 2; |
| 697 | |
| 698 | /* allocate the mapping */ |
| 699 | xlat_array_ = (unsigned char *)t3malloc(siz); |
| 700 | |
| 701 | /* start at the start of the array */ |
| 702 | dst = xlat_array_; |
| 703 | |
| 704 | /* |
| 705 | * Add the zeroeth entry, which serves as the default mapping for |
| 706 | * characters that aren't otherwise mappable. |
| 707 | */ |
| 708 | set_mapping(0, 0); |
| 709 | *dst++ = 1; |
| 710 | *dst++ = '?'; |
| 711 | |
| 712 | /* |
| 713 | * Set up the mappings - this is easy because each Unicode code point |
| 714 | * from 0 to 255 maps to the same ISO 8859-1 code point. |
| 715 | */ |
| 716 | for (c = 0 ; c < 256 ; ++c) |
| 717 | { |
| 718 | /* set the mapping's offset in the translation array */ |
| 719 | set_mapping(c, dst - xlat_array_); |
| 720 | |
| 721 | /* store the length (always 1) and translated character */ |
| 722 | *dst++ = 1; |
| 723 | *dst++ = (unsigned char)c; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | |
| 728 | /* ------------------------------------------------------------------------ */ |
| 729 | /* |
| 730 | * Character mapping for Unicode to Local |
| 731 | */ |
| 732 | |
| 733 | /* |
| 734 | * create the translator |
| 735 | */ |
| 736 | CCharmapToLocal::CCharmapToLocal() |
| 737 | { |
| 738 | /* no mapping sub-tables yet */ |
| 739 | memset(map_, 0, sizeof(map_)); |
| 740 | memset(exp_map_, 0, sizeof(exp_map_)); |
| 741 | |
| 742 | /* no translation or expansion arrays yet */ |
| 743 | xlat_array_ = 0; |
| 744 | exp_array_ = 0; |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * delete the translator |
| 749 | */ |
| 750 | CCharmapToLocal::~CCharmapToLocal() |
| 751 | { |
| 752 | size_t i; |
| 753 | |
| 754 | /* delete the translation array */ |
| 755 | if (xlat_array_ != 0) |
| 756 | t3free(xlat_array_); |
| 757 | |
| 758 | /* delete the expansion array */ |
| 759 | if (exp_array_ != 0) |
| 760 | t3free(exp_array_); |
| 761 | |
| 762 | /* delete any mapping tables we've allocated */ |
| 763 | for (i = 0 ; i < sizeof(map_)/sizeof(map_[0]) ; ++i) |
| 764 | { |
| 765 | /* delete this mapping if allocated */ |
| 766 | if (map_[i] != 0) |
| 767 | t3free(map_[i]); |
| 768 | } |
| 769 | |
| 770 | /* delete any expansion mapping tables */ |
| 771 | for (i = 0 ; i < sizeof(exp_map_)/sizeof(exp_map_[0]) ; ++i) |
| 772 | { |
| 773 | /* delete this expansion mapping if allocated */ |
| 774 | if (exp_map_[i] != 0) |
| 775 | t3free(exp_map_[i]); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * Set a mapping |
| 781 | */ |
| 782 | void CCharmapToLocal::set_mapping(wchar_t unicode_char, |
| 783 | unsigned int xlat_offset) |
| 784 | { |
| 785 | int master_idx; |
| 786 | |
| 787 | /* get the master table index for this unicode character */ |
| 788 | master_idx = (int)((unicode_char >> 8) & 0xff); |
| 789 | |
| 790 | /* if there's no sub-table here yet, create one */ |
| 791 | if (map_[master_idx] == 0) |
| 792 | { |
| 793 | int i; |
| 794 | |
| 795 | /* allocate it */ |
| 796 | map_[master_idx] = |
| 797 | (unsigned int *)t3malloc(256 * sizeof(unsigned int)); |
| 798 | |
| 799 | /* |
| 800 | * Set each entry to the default character, so that it will |
| 801 | * produce valid results if no mapping is ever specified for the |
| 802 | * character. The default character is always at offset zero in |
| 803 | * the translation array. |
| 804 | */ |
| 805 | for (i = 0 ; i < 256 ; ++i) |
| 806 | map_[master_idx][i] = 0; |
| 807 | } |
| 808 | |
| 809 | /* set the mapping for the character's entry in the sub-table */ |
| 810 | map_[master_idx][unicode_char & 0xff] = xlat_offset; |
| 811 | } |
| 812 | |
| 813 | /* |
| 814 | * Set an expansion mapping |
| 815 | */ |
| 816 | void CCharmapToLocal::set_exp_mapping(wchar_t unicode_char, |
| 817 | unsigned int exp_offset) |
| 818 | { |
| 819 | int master_idx; |
| 820 | |
| 821 | /* get the master table index for this unicode character */ |
| 822 | master_idx = (int)((unicode_char >> 8) & 0xff); |
| 823 | |
| 824 | /* if there's no sub-table here yet, create one */ |
| 825 | if (exp_map_[master_idx] == 0) |
| 826 | { |
| 827 | int i; |
| 828 | |
| 829 | /* allocate it */ |
| 830 | exp_map_[master_idx] = |
| 831 | (unsigned int *)t3malloc(256 * sizeof(unsigned int)); |
| 832 | |
| 833 | /* |
| 834 | * Set each entry to the default character, so that it will produce |
| 835 | * valid results if no mapping is ever specified for the character. |
| 836 | * The default character is always at offset zero in the expansion |
| 837 | * array. |
| 838 | */ |
| 839 | for (i = 0 ; i < 256 ; ++i) |
| 840 | exp_map_[master_idx][i] = 0; |
| 841 | } |
| 842 | |
| 843 | /* set the mapping for the character's entry in the sub-table */ |
| 844 | exp_map_[master_idx][unicode_char & 0xff] = exp_offset; |
| 845 | } |
| 846 | |
| 847 | /* |
| 848 | * Map a UTF-8 string of known byte length to the local character set |
| 849 | */ |
| 850 | size_t CCharmapToLocal::map_utf8(char *dest, size_t dest_len, |
| 851 | utf8_ptr src, size_t src_byte_len, |
| 852 | size_t *src_bytes_used) const |
| 853 | { |
| 854 | utf8_ptr src_start; |
| 855 | size_t cur_total; |
| 856 | char *srcend; |
| 857 | |
| 858 | /* remember where we started */ |
| 859 | src_start = src; |
| 860 | |
| 861 | /* compute where the source buffer ends */ |
| 862 | srcend = src.getptr() + src_byte_len; |
| 863 | |
| 864 | /* copy characters until we reach the end of the source string */ |
| 865 | for (cur_total = 0 ; src.getptr() < srcend ; src.inc()) |
| 866 | { |
| 867 | char mapbuf[10]; |
| 868 | size_t maplen = sizeof(mapbuf); |
| 869 | char *mapp = mapbuf; |
| 870 | |
| 871 | /* map this character */ |
| 872 | maplen = map(src.getch(), &mapp, &maplen); |
| 873 | |
| 874 | /* determine how to store the character */ |
| 875 | if (dest == 0) |
| 876 | { |
| 877 | /* we're just counting */ |
| 878 | } |
| 879 | else if (dest_len >= maplen) |
| 880 | { |
| 881 | /* we have room for it - add it in */ |
| 882 | memcpy(dest, mapbuf, maplen); |
| 883 | |
| 884 | /* advance past it */ |
| 885 | dest += maplen; |
| 886 | dest_len -= maplen; |
| 887 | } |
| 888 | else |
| 889 | { |
| 890 | /* there's no more room - stop now */ |
| 891 | break; |
| 892 | } |
| 893 | |
| 894 | /* add this into the total */ |
| 895 | cur_total += maplen; |
| 896 | } |
| 897 | |
| 898 | /* if the caller wants to know how much space we used, tell them */ |
| 899 | if (src_bytes_used != 0) |
| 900 | *src_bytes_used = src.getptr() - src_start.getptr(); |
| 901 | |
| 902 | /* return the total length of the result */ |
| 903 | return cur_total; |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | * Map a null-terminated UTF-8 string to the local character set |
| 908 | */ |
| 909 | size_t CCharmapToLocal::map_utf8z(char *dest, size_t dest_len, |
| 910 | utf8_ptr src) const |
| 911 | { |
| 912 | size_t cur_total; |
| 913 | |
| 914 | /* copy characters until we find the terminating null */ |
| 915 | for (cur_total = 0 ; src.getch() != 0 ; src.inc()) |
| 916 | { |
| 917 | /* |
| 918 | * map this character into the output, if it will fit, but in |
| 919 | * any case count the space it needs in the output |
| 920 | */ |
| 921 | cur_total += map(src.getch(), &dest, &dest_len); |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * add a null terminator if there's room, but don't count it in the |
| 926 | * result length |
| 927 | */ |
| 928 | map(0, &dest, &dest_len); |
| 929 | |
| 930 | /* return the total length of the result */ |
| 931 | return cur_total; |
| 932 | } |
| 933 | |
| 934 | /* |
| 935 | * Map a string, allocating a new buffer if the caller doesn't provide one. |
| 936 | */ |
| 937 | size_t CCharmapToLocal::map_utf8_alo( |
| 938 | char **buf, const char *src, size_t srclen) const |
| 939 | { |
| 940 | /* figure out how much space we need */ |
| 941 | size_t buflen = map_utf8(0, 0, src, srclen, 0); |
| 942 | |
| 943 | /* allocate the buffer, adding space for null termination */ |
| 944 | *buf = (char *)t3malloc(buflen + 1); |
| 945 | |
| 946 | /* if that failed, return null */ |
| 947 | if (buf == 0) |
| 948 | return 0; |
| 949 | |
| 950 | /* do the mapping */ |
| 951 | buflen = map_utf8(*buf, buflen, src, srclen, 0); |
| 952 | |
| 953 | /* fill in the null terminator */ |
| 954 | (*buf)[buflen] = '\0'; |
| 955 | |
| 956 | /* return the mapped length */ |
| 957 | return buflen; |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | * Map a null-terminated UTF-8 string to the local character set, escaping |
| 962 | * characters that aren't part of the local character set. |
| 963 | */ |
| 964 | size_t CCharmapToLocal::map_utf8z_esc( |
| 965 | char *dest, size_t dest_len, utf8_ptr src, |
| 966 | size_t (*esc_fn)(wchar_t, char **, size_t *)) const |
| 967 | { |
| 968 | size_t cur_total; |
| 969 | |
| 970 | /* copy characters until we find the terminating null */ |
| 971 | for (cur_total = 0 ; src.getch() != 0 ; src.inc()) |
| 972 | { |
| 973 | wchar_t ch = src.getch(); |
| 974 | |
| 975 | /* if this character is mappable, map it; otherwise, escape it */ |
| 976 | if (is_mappable(src.getch())) |
| 977 | { |
| 978 | /* map the character */ |
| 979 | cur_total += map(ch, &dest, &dest_len); |
| 980 | } |
| 981 | else |
| 982 | { |
| 983 | /* we can't map it, so let the escape callback handle it */ |
| 984 | cur_total += (*esc_fn)(ch, &dest, &dest_len); |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | /* |
| 989 | * add a null terminator if there's room, but don't count it in the |
| 990 | * result length |
| 991 | */ |
| 992 | map(0, &dest, &dest_len); |
| 993 | |
| 994 | /* return the total length of the result */ |
| 995 | return cur_total; |
| 996 | } |
| 997 | |
| 998 | /* |
| 999 | * Escape callback for map_utf8z_esc() - prepares source-code-style |
| 1000 | * 'backslash' escape sequences for unmappable characters. |
| 1001 | */ |
| 1002 | size_t CCharmapToLocal::source_esc_cb(wchar_t ch, char **dest, size_t *len) |
| 1003 | { |
| 1004 | char buf[7]; |
| 1005 | size_t copylen; |
| 1006 | |
| 1007 | /* prepare our own representation */ |
| 1008 | sprintf(buf, "\\u%04x", (unsigned int)ch); |
| 1009 | |
| 1010 | /* copy the whole thing if possible, but limit to the available space */ |
| 1011 | copylen = 6; |
| 1012 | if (copylen > *len) |
| 1013 | copylen = *len; |
| 1014 | |
| 1015 | /* copy the bytes */ |
| 1016 | memcpy(*dest, buf, copylen); |
| 1017 | |
| 1018 | /* advance the buffer pointers */ |
| 1019 | *dest += copylen; |
| 1020 | *len -= copylen; |
| 1021 | |
| 1022 | /* return the full space needed */ |
| 1023 | return 6; |
| 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | * Map to UTF8 |
| 1028 | */ |
| 1029 | size_t CCharmapToLocal::map_utf8(char *dest, size_t dest_len, |
| 1030 | const char *src, size_t src_byte_len, |
| 1031 | size_t *src_bytes_used) const |
| 1032 | { |
| 1033 | utf8_ptr src_ptr; |
| 1034 | |
| 1035 | /* set up the source UTF-8 pointer */ |
| 1036 | src_ptr.set((char *)src); |
| 1037 | |
| 1038 | /* map it and return the result */ |
| 1039 | return map_utf8(dest, dest_len, src_ptr, src_byte_len, src_bytes_used); |
| 1040 | } |
| 1041 | |
| 1042 | /* |
| 1043 | * Create a mapper and load a mapping file |
| 1044 | */ |
| 1045 | CCharmapToLocal *CCharmapToLocal::load(CResLoader *res_loader, |
| 1046 | const char *table_name) |
| 1047 | { |
| 1048 | osfildef *fp; |
| 1049 | CCharmapToLocal *mapper; |
| 1050 | charmap_type_t map_type; |
| 1051 | |
| 1052 | /* if they want a trivial UTF-8 translator, return one */ |
| 1053 | if (name_is_utf8_synonym(table_name)) |
| 1054 | return new CCharmapToLocalUTF8(); |
| 1055 | |
| 1056 | /* if they want a Unicode 16-bit encoding, return one */ |
| 1057 | if (name_is_ucs2le_synonym(table_name)) |
| 1058 | return new CCharmapToLocalUcs2Little(); |
| 1059 | |
| 1060 | if (name_is_ucs2be_synonym(table_name)) |
| 1061 | return new CCharmapToLocalUcs2Big(); |
| 1062 | |
| 1063 | /* presume failure */ |
| 1064 | mapper = 0; |
| 1065 | |
| 1066 | /* open and characterize the mapping file */ |
| 1067 | fp = open_map_file_syn(res_loader, table_name, &map_type); |
| 1068 | |
| 1069 | /* if we didn't find a map file, check for built-in mappings */ |
| 1070 | if (fp == 0) |
| 1071 | { |
| 1072 | /* if they want a plain ASCII translator, return a default one */ |
| 1073 | if (name_is_ascii_synonym(table_name)) |
| 1074 | return new CCharmapToLocalASCII(); |
| 1075 | |
| 1076 | /* if they want a plain ISO-8859-1 translator, return a default one */ |
| 1077 | if (name_is_8859_1_synonym(table_name)) |
| 1078 | return new CCharmapToLocal8859_1(); |
| 1079 | |
| 1080 | /* no map file - return failure */ |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | /* create an appropriate mapper */ |
| 1085 | switch(map_type) |
| 1086 | { |
| 1087 | case CHARMAP_TYPE_SB: |
| 1088 | /* create a single-byte mapper */ |
| 1089 | mapper = new CCharmapToLocalSB(); |
| 1090 | break; |
| 1091 | |
| 1092 | case CHARMAP_TYPE_DB: |
| 1093 | /* create a double-byte mapper */ |
| 1094 | mapper = new CCharmapToLocalDB(); |
| 1095 | break; |
| 1096 | |
| 1097 | case CHARMAP_TYPE_MB: |
| 1098 | /* create a mixed multi-byte mapper */ |
| 1099 | mapper = new CCharmapToLocalMB(); |
| 1100 | break; |
| 1101 | |
| 1102 | default: |
| 1103 | /* other mapper types are currently unknown */ |
| 1104 | break; |
| 1105 | } |
| 1106 | |
| 1107 | /* if we successfully created a mapper, tell it to load the table */ |
| 1108 | if (mapper != 0) |
| 1109 | { |
| 1110 | /* load the table */ |
| 1111 | mapper->load_table(fp); |
| 1112 | } |
| 1113 | |
| 1114 | /* close the file */ |
| 1115 | osfcls(fp); |
| 1116 | |
| 1117 | /* return the mapper, if any */ |
| 1118 | return mapper; |
| 1119 | } |
| 1120 | |
| 1121 | /* |
| 1122 | * Load the character set translation table |
| 1123 | */ |
| 1124 | void CCharmapToLocal::load_table(osfildef *fp) |
| 1125 | { |
| 1126 | ulong startpos; |
| 1127 | ulong ofs; |
| 1128 | uchar buf[256]; |
| 1129 | uint cnt; |
| 1130 | ulong xbytes; |
| 1131 | ulong xchars; |
| 1132 | uint next_ofs; |
| 1133 | |
| 1134 | /* note the initial seek position */ |
| 1135 | startpos = osfpos(fp); |
| 1136 | |
| 1137 | /* read the first entry, which gives the offset of the to-local table */ |
| 1138 | if (osfrb(fp, buf, 4)) |
| 1139 | return; |
| 1140 | ofs = t3rp4u(buf); |
| 1141 | |
| 1142 | /* seek to the to-local table */ |
| 1143 | osfseek(fp, startpos + ofs, OSFSK_SET); |
| 1144 | |
| 1145 | /* read the number of entries and number of bytes needed */ |
| 1146 | if (osfrb(fp, buf, 6)) |
| 1147 | return; |
| 1148 | cnt = osrp2(buf); |
| 1149 | xbytes = t3rp4u(buf + 2); |
| 1150 | |
| 1151 | /* |
| 1152 | * Allocate space for the translation table. Note that we cannot |
| 1153 | * handle translation tables bigger than the maximum allowed in a |
| 1154 | * single allocation unit on the operating system. |
| 1155 | */ |
| 1156 | if (xbytes > OSMALMAX) |
| 1157 | return; |
| 1158 | xlat_array_ = (unsigned char *)t3malloc(xbytes); |
| 1159 | if (xlat_array_ == 0) |
| 1160 | return; |
| 1161 | |
| 1162 | /* |
| 1163 | * Read each mapping |
| 1164 | */ |
| 1165 | for (next_ofs = 0 ; cnt > 0 ; --cnt) |
| 1166 | { |
| 1167 | wchar_t codept; |
| 1168 | uint xlen; |
| 1169 | |
| 1170 | /* read the code point and translation length */ |
| 1171 | if (osfrb(fp, buf, 3)) |
| 1172 | return; |
| 1173 | |
| 1174 | /* decode the code point and translation length */ |
| 1175 | codept = osrp2(buf); |
| 1176 | xlen = (unsigned int)buf[2]; |
| 1177 | |
| 1178 | /* assign the mapping */ |
| 1179 | set_mapping(codept, next_ofs); |
| 1180 | |
| 1181 | /* store the translation length */ |
| 1182 | xlat_array_[next_ofs++] = buf[2]; |
| 1183 | |
| 1184 | /* read the translation bytes */ |
| 1185 | if (osfrb(fp, xlat_array_ + next_ofs, xlen)) |
| 1186 | return; |
| 1187 | |
| 1188 | /* skip past the translation bytes we've read */ |
| 1189 | next_ofs += xlen; |
| 1190 | } |
| 1191 | |
| 1192 | /* |
| 1193 | * Next, read the expansions, if present. |
| 1194 | * |
| 1195 | * If we find the $EOF marker, it means it's an old-format file without |
| 1196 | * the separate expansion definitions. Otherwise, we'll have the |
| 1197 | * expansion entry count and the aggregate number of unicode characters |
| 1198 | * in all of the expansions. |
| 1199 | */ |
| 1200 | if (osfrb(fp, buf, 6) || memcmp(buf, "$EOF", 4) == 0) |
| 1201 | return; |
| 1202 | |
| 1203 | /* decode the expansion entry count and aggregate length */ |
| 1204 | cnt = osrp2(buf); |
| 1205 | xchars = t3rp4u(buf + 2); |
| 1206 | |
| 1207 | /* |
| 1208 | * add one entry so that we can leave index zero unused, to indicate |
| 1209 | * unmapped characters |
| 1210 | */ |
| 1211 | ++xchars; |
| 1212 | |
| 1213 | /* add one array slot per entry, for the length prefix slots */ |
| 1214 | xchars += cnt; |
| 1215 | |
| 1216 | /* allocate space for the expansions */ |
| 1217 | exp_array_ = (wchar_t *)t3malloc(xchars * sizeof(wchar_t)); |
| 1218 | if (exp_array_ == 0) |
| 1219 | return; |
| 1220 | |
| 1221 | /* |
| 1222 | * read the mappings; start loading them at index 1, since we want to |
| 1223 | * leave index 0 unused so that it can indicate unused mappings |
| 1224 | */ |
| 1225 | for (next_ofs = 1 ; cnt > 0 ; --cnt) |
| 1226 | { |
| 1227 | wchar_t codept; |
| 1228 | uint xlen; |
| 1229 | size_t i; |
| 1230 | |
| 1231 | /* read this entry's unicode value and expansion character length */ |
| 1232 | if (osfrb(fp, buf, 3)) |
| 1233 | return; |
| 1234 | |
| 1235 | /* decode the code point and expansion length */ |
| 1236 | codept = osrp2(buf); |
| 1237 | xlen = (uint)buf[2]; |
| 1238 | |
| 1239 | /* assign the expansion mapping */ |
| 1240 | set_exp_mapping(codept, next_ofs); |
| 1241 | |
| 1242 | /* set the length prefix */ |
| 1243 | exp_array_[next_ofs++] = (wchar_t)xlen; |
| 1244 | |
| 1245 | /* read and store the expansion characters */ |
| 1246 | for (i = 0 ; i < xlen ; ++i) |
| 1247 | { |
| 1248 | /* read this translation */ |
| 1249 | if (osfrb(fp, buf, 2)) |
| 1250 | return; |
| 1251 | |
| 1252 | /* decode and store this translation */ |
| 1253 | exp_array_[next_ofs++] = osrp2(buf); |
| 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | /* |
| 1259 | * Write to a file |
| 1260 | */ |
| 1261 | int CCharmapToLocal::write_file(CVmDataSource *fp, |
| 1262 | const char *buf, size_t bufl) |
| 1263 | { |
| 1264 | utf8_ptr p; |
| 1265 | |
| 1266 | /* set up to read from the buffer */ |
| 1267 | p.set((char *)buf); |
| 1268 | |
| 1269 | /* map and write one buffer-full at a time */ |
| 1270 | while (bufl > 0) |
| 1271 | { |
| 1272 | char conv_buf[256]; |
| 1273 | size_t conv_len; |
| 1274 | size_t used_src_len; |
| 1275 | |
| 1276 | /* map as much as we can fit into our buffer */ |
| 1277 | conv_len = map_utf8(conv_buf, sizeof(conv_buf), p, bufl, |
| 1278 | &used_src_len); |
| 1279 | |
| 1280 | /* write out this chunk */ |
| 1281 | if (fp->write(conv_buf, conv_len)) |
| 1282 | return 1; |
| 1283 | |
| 1284 | /* advance past this chunk in the input */ |
| 1285 | p.set(p.getptr() + used_src_len); |
| 1286 | bufl -= used_src_len; |
| 1287 | } |
| 1288 | |
| 1289 | /* no errors */ |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | |
| 1294 | /* ------------------------------------------------------------------------ */ |
| 1295 | /* |
| 1296 | * Character mapper - trivial UTF8-to-UTF8 conversion |
| 1297 | */ |
| 1298 | |
| 1299 | /* |
| 1300 | * map a character |
| 1301 | */ |
| 1302 | size_t CCharmapToLocalUTF8::map(wchar_t unicode_char, char **output_ptr, |
| 1303 | size_t *output_len) const |
| 1304 | { |
| 1305 | /* get the character size */ |
| 1306 | size_t map_len = utf8_ptr::s_wchar_size(unicode_char); |
| 1307 | |
| 1308 | /* if we don't have room for one more character, abort */ |
| 1309 | if (*output_len < map_len) |
| 1310 | { |
| 1311 | *output_len = 0; |
| 1312 | return map_len; |
| 1313 | } |
| 1314 | |
| 1315 | /* store the mapping */ |
| 1316 | utf8_ptr::s_putch(*output_ptr, unicode_char); |
| 1317 | |
| 1318 | /* increment the pointer by the number of characters we copied */ |
| 1319 | *output_ptr += map_len; |
| 1320 | |
| 1321 | /* adjust the remaining output length */ |
| 1322 | *output_len -= map_len; |
| 1323 | |
| 1324 | /* return the size of the result */ |
| 1325 | return map_len; |
| 1326 | } |
| 1327 | |
| 1328 | /* |
| 1329 | * Map a UTF-8 string of known byte length |
| 1330 | */ |
| 1331 | size_t CCharmapToLocalUTF8::map_utf8(char *dest, size_t dest_len, |
| 1332 | utf8_ptr src, size_t src_byte_len, |
| 1333 | size_t *src_bytes_used) const |
| 1334 | { |
| 1335 | /* |
| 1336 | * if they didn't give us a destination buffer, tell them how much |
| 1337 | * space is needed for the copy - this is identical to the length of |
| 1338 | * the source string since we make no changes to it |
| 1339 | */ |
| 1340 | if (dest == 0) |
| 1341 | { |
| 1342 | if (src_bytes_used != 0) |
| 1343 | *src_bytes_used = 0; |
| 1344 | return src_byte_len; |
| 1345 | } |
| 1346 | |
| 1347 | /* assume we'll be able to copy the whole string */ |
| 1348 | size_t copy_len = src_byte_len; |
| 1349 | |
| 1350 | /* if there's not enough space for the whole string, limit it */ |
| 1351 | if (copy_len > dest_len) |
| 1352 | { |
| 1353 | /* copy no more than the output buffer length */ |
| 1354 | copy_len = dest_len; |
| 1355 | |
| 1356 | /* |
| 1357 | * If this splits a character into two pieces, back up until we're |
| 1358 | * at a character boundary - that is, the first byte of the next |
| 1359 | * chunk is a non-continuation character. |
| 1360 | */ |
| 1361 | while (copy_len > 0 |
| 1362 | && utf8_ptr::s_is_continuation(src.getptr() + copy_len)) |
| 1363 | --copy_len; |
| 1364 | |
| 1365 | /* |
| 1366 | * If that left us with zero bytes, just do the partial copy to |
| 1367 | * ensure we don't get stuck in a zero-at-a-time infinite loop. |
| 1368 | */ |
| 1369 | if (copy_len == 0) |
| 1370 | copy_len = dest_len; |
| 1371 | } |
| 1372 | |
| 1373 | /* if we have an output buffer, copy the data */ |
| 1374 | if (dest != 0) |
| 1375 | memcpy(dest, src.getptr(), copy_len); |
| 1376 | |
| 1377 | /* set the amount we copied, if the caller is interested */ |
| 1378 | if (src_bytes_used != 0) |
| 1379 | *src_bytes_used = copy_len; |
| 1380 | |
| 1381 | /* return the number of bytes we put in the destination buffer */ |
| 1382 | return copy_len; |
| 1383 | } |
| 1384 | |
| 1385 | /* |
| 1386 | * Map a null-terminated UTF-8 string |
| 1387 | */ |
| 1388 | size_t CCharmapToLocalUTF8::map_utf8z(char *dest, size_t dest_len, |
| 1389 | utf8_ptr src) const |
| 1390 | { |
| 1391 | /* get the source length */ |
| 1392 | size_t src_len = strlen(src.getptr()); |
| 1393 | |
| 1394 | /* copy the bytes */ |
| 1395 | map_utf8(dest, dest_len, src, src_len, 0); |
| 1396 | |
| 1397 | /* |
| 1398 | * if there's room for the null terminator (which takes up just one |
| 1399 | * byte in UTF-8), add it |
| 1400 | */ |
| 1401 | if (dest_len > src_len) |
| 1402 | *(dest + src_len) = '\0'; |
| 1403 | |
| 1404 | /* |
| 1405 | * return the amount of space needed to copy the whole string -- |
| 1406 | * this is identical to the source length, since we don't make any |
| 1407 | * changes to it |
| 1408 | */ |
| 1409 | return src_len; |
| 1410 | } |
| 1411 | |
| 1412 | |
| 1413 | /* ------------------------------------------------------------------------ */ |
| 1414 | /* |
| 1415 | * Character mapper - Unicode to Single-byte |
| 1416 | */ |
| 1417 | |
| 1418 | /* |
| 1419 | * map a character |
| 1420 | */ |
| 1421 | size_t CCharmapToLocalSB::map(wchar_t unicode_char, char **output_ptr, |
| 1422 | size_t *output_len) const |
| 1423 | { |
| 1424 | /* get the mapping */ |
| 1425 | size_t map_len; |
| 1426 | const unsigned char *mapping = get_xlation(unicode_char, &map_len); |
| 1427 | |
| 1428 | /* if we don't have room for one more character, abort */ |
| 1429 | if (*output_len < map_len) |
| 1430 | { |
| 1431 | *output_len = 0; |
| 1432 | return map_len; |
| 1433 | } |
| 1434 | |
| 1435 | /* copy the mapping */ |
| 1436 | memcpy(*output_ptr, mapping, map_len); |
| 1437 | |
| 1438 | /* increment the pointer by the number of characters we copied */ |
| 1439 | *output_ptr += map_len; |
| 1440 | |
| 1441 | /* adjust the remaining output length */ |
| 1442 | *output_len -= map_len; |
| 1443 | |
| 1444 | /* return the size of the result */ |
| 1445 | return map_len; |
| 1446 | } |
| 1447 | |
| 1448 | /* |
| 1449 | * Map a UTF-8 string of known byte length to the local character set |
| 1450 | */ |
| 1451 | size_t CCharmapToLocalSB::map_utf8(char *dest, size_t dest_len, |
| 1452 | utf8_ptr src, size_t src_byte_len, |
| 1453 | size_t *src_bytes_used) const |
| 1454 | { |
| 1455 | /* remember where we started */ |
| 1456 | utf8_ptr src_start = src; |
| 1457 | |
| 1458 | /* compute where the source buffer ends */ |
| 1459 | char *srcend = src.getptr() + src_byte_len; |
| 1460 | |
| 1461 | /* copy characters until we reach the end of the source string */ |
| 1462 | size_t cur_total; |
| 1463 | for (cur_total = 0 ; src.getptr() < srcend ; src.inc()) |
| 1464 | { |
| 1465 | const unsigned char *mapping; |
| 1466 | size_t map_len; |
| 1467 | |
| 1468 | /* get the mapping for this character */ |
| 1469 | mapping = get_xlation(src.getch(), &map_len); |
| 1470 | |
| 1471 | /* |
| 1472 | * if we have room, add it; otherwise, zero the output length |
| 1473 | * remaining so we don't try to add anything more |
| 1474 | */ |
| 1475 | if (dest == 0) |
| 1476 | { |
| 1477 | /* we're just counting */ |
| 1478 | } |
| 1479 | else if (map_len <= dest_len) |
| 1480 | { |
| 1481 | /* add the sequence */ |
| 1482 | memcpy(dest, mapping, map_len); |
| 1483 | |
| 1484 | /* adjust the output pointer and length remaining */ |
| 1485 | dest += map_len; |
| 1486 | dest_len -= map_len; |
| 1487 | } |
| 1488 | else |
| 1489 | { |
| 1490 | /* it doesn't fit - stop now */ |
| 1491 | break; |
| 1492 | } |
| 1493 | |
| 1494 | /* count the length in the total */ |
| 1495 | cur_total += map_len; |
| 1496 | } |
| 1497 | |
| 1498 | /* if the caller wants to know how much space we used, tell them */ |
| 1499 | if (src_bytes_used != 0) |
| 1500 | *src_bytes_used = src.getptr() - src_start.getptr(); |
| 1501 | |
| 1502 | /* return the total length of the result */ |
| 1503 | return cur_total; |
| 1504 | } |
| 1505 | |
| 1506 | /* |
| 1507 | * Map a null-terminated UTF-8 string to the local character set |
| 1508 | */ |
| 1509 | size_t CCharmapToLocalSB::map_utf8z(char *dest, size_t dest_len, |
| 1510 | utf8_ptr src) const |
| 1511 | { |
| 1512 | size_t cur_total; |
| 1513 | |
| 1514 | /* copy characters until we find the terminating null */ |
| 1515 | for (cur_total = 0 ; src.getch() != 0 ; src.inc()) |
| 1516 | { |
| 1517 | const unsigned char *mapping; |
| 1518 | size_t map_len; |
| 1519 | |
| 1520 | /* get the mapping for this character */ |
| 1521 | mapping = get_xlation(src.getch(), &map_len); |
| 1522 | |
| 1523 | /* |
| 1524 | * if we have room, add it; otherwise, zero the output length |
| 1525 | * remaining so we don't try to add anything more |
| 1526 | */ |
| 1527 | if (map_len <= dest_len) |
| 1528 | { |
| 1529 | /* add the sequence */ |
| 1530 | memcpy(dest, mapping, map_len); |
| 1531 | |
| 1532 | /* adjust the output pointer and length remaining */ |
| 1533 | dest += map_len; |
| 1534 | dest_len -= map_len; |
| 1535 | } |
| 1536 | else |
| 1537 | { |
| 1538 | /* it doesn't fit - zero the output length remaining */ |
| 1539 | dest_len = 0; |
| 1540 | } |
| 1541 | |
| 1542 | /* count the length in the total */ |
| 1543 | cur_total += map_len; |
| 1544 | } |
| 1545 | |
| 1546 | /* |
| 1547 | * add a null terminator, if there's room, but don't count it in the |
| 1548 | * output length |
| 1549 | */ |
| 1550 | if (dest_len > 0) |
| 1551 | *dest = '\0'; |
| 1552 | |
| 1553 | /* return the total length of the result */ |
| 1554 | return cur_total; |
| 1555 | } |
| 1556 | |
| 1557 | |
| 1558 | /* ------------------------------------------------------------------------ */ |
| 1559 | /* |
| 1560 | * Character mapper - Unicode to 16-bit Wide Unicode local character set |
| 1561 | */ |
| 1562 | |
| 1563 | /* |
| 1564 | * map a character |
| 1565 | */ |
| 1566 | size_t CCharmapToLocalWideUnicode::map(wchar_t unicode_char, |
| 1567 | char **output_ptr, |
| 1568 | size_t *output_len) const |
| 1569 | { |
| 1570 | /* if we don't have room for another wchar_t, abort */ |
| 1571 | if (*output_len < sizeof(wchar_t)) |
| 1572 | { |
| 1573 | *output_len = 0; |
| 1574 | return sizeof(wchar_t); |
| 1575 | } |
| 1576 | |
| 1577 | /* |
| 1578 | * Set the wide character to the unicode value, with no translation |
| 1579 | * - unicode is the same everywhere. |
| 1580 | * |
| 1581 | * Note that the need to perform this trivial translation for this |
| 1582 | * character set is a secondary reason that this routine is virtual |
| 1583 | * (the primary reason is to handle the default ASCII translation). |
| 1584 | */ |
| 1585 | **(wchar_t **)output_ptr = unicode_char; |
| 1586 | |
| 1587 | /* increment the pointer by the size of a wide character */ |
| 1588 | ++(*(wchar_t **)output_ptr); |
| 1589 | |
| 1590 | /* return the size of the result */ |
| 1591 | return sizeof(wchar_t); |
| 1592 | } |
| 1593 | |
| 1594 | /* |
| 1595 | * Map a UTF-8 string of known byte length to the local character set |
| 1596 | */ |
| 1597 | size_t CCharmapToLocalWideUnicode:: |
| 1598 | map_utf8(char *dest, size_t dest_len, |
| 1599 | utf8_ptr src, size_t src_byte_len, |
| 1600 | size_t *src_bytes_used) const |
| 1601 | { |
| 1602 | utf8_ptr src_start; |
| 1603 | size_t cur_total; |
| 1604 | char *srcend; |
| 1605 | wchar_t *destw; |
| 1606 | |
| 1607 | /* remember where we started */ |
| 1608 | src_start = src; |
| 1609 | |
| 1610 | /* compute where the source buffer ends */ |
| 1611 | srcend = src.getptr() + src_byte_len; |
| 1612 | |
| 1613 | /* set up a wchar_t output pointer for convenience */ |
| 1614 | destw = (wchar_t *)dest; |
| 1615 | |
| 1616 | /* copy characters until we reach the end of the source string */ |
| 1617 | for (cur_total = 0 ; src.getptr() < srcend ; src.inc()) |
| 1618 | { |
| 1619 | /* |
| 1620 | * if we have room, add it; otherwise, zero the output length |
| 1621 | * remaining so we don't try to add anything more |
| 1622 | */ |
| 1623 | if (dest == 0) |
| 1624 | { |
| 1625 | /* we're just counting - don't store anything */ |
| 1626 | } |
| 1627 | else if (dest_len >= sizeof(wchar_t)) |
| 1628 | { |
| 1629 | /* add the sequence */ |
| 1630 | *destw++ = src.getch(); |
| 1631 | |
| 1632 | /* adjust the length remaining */ |
| 1633 | dest_len -= sizeof(wchar_t); |
| 1634 | } |
| 1635 | else |
| 1636 | { |
| 1637 | /* it doesn't fit - stop now */ |
| 1638 | break; |
| 1639 | } |
| 1640 | |
| 1641 | /* count the length in the total */ |
| 1642 | cur_total += sizeof(wchar_t); |
| 1643 | } |
| 1644 | |
| 1645 | /* if the caller wants to know how much space we used, tell them */ |
| 1646 | if (src_bytes_used != 0) |
| 1647 | *src_bytes_used = src.getptr() - src_start.getptr(); |
| 1648 | |
| 1649 | /* return the total length of the result */ |
| 1650 | return cur_total; |
| 1651 | } |
| 1652 | |
| 1653 | /* |
| 1654 | * Map a null-terminated UTF-8 string to the local character set |
| 1655 | */ |
| 1656 | size_t CCharmapToLocalWideUnicode:: |
| 1657 | map_utf8z(char *dest, size_t dest_len, utf8_ptr src) const |
| 1658 | { |
| 1659 | size_t cur_total; |
| 1660 | wchar_t *destw; |
| 1661 | |
| 1662 | /* set up a wchar_t output pointer for convenience */ |
| 1663 | destw = (wchar_t *)dest; |
| 1664 | |
| 1665 | /* copy characters until we find the terminating null */ |
| 1666 | for (cur_total = 0 ; src.getch() != 0 ; src.inc()) |
| 1667 | { |
| 1668 | /* |
| 1669 | * if we have room, add it; otherwise, zero the output length |
| 1670 | * remaining so we don't try to add anything more |
| 1671 | */ |
| 1672 | if (dest_len >= sizeof(wchar_t)) |
| 1673 | { |
| 1674 | /* add the sequence */ |
| 1675 | *destw++ = src.getch(); |
| 1676 | |
| 1677 | /* adjust the length remaining */ |
| 1678 | dest_len -= sizeof(wchar_t); |
| 1679 | } |
| 1680 | else |
| 1681 | { |
| 1682 | /* it doesn't fit - zero the output length remaining */ |
| 1683 | dest_len = 0; |
| 1684 | } |
| 1685 | |
| 1686 | /* count the length in the total */ |
| 1687 | cur_total += sizeof(wchar_t); |
| 1688 | } |
| 1689 | |
| 1690 | /* |
| 1691 | * if there's room for a null terminator character (not byte - we need |
| 1692 | * to add an entire wide character), add it, but don't count it in the |
| 1693 | * return length |
| 1694 | */ |
| 1695 | if (dest_len >= sizeof(wchar_t)) |
| 1696 | *destw = '\0'; |
| 1697 | |
| 1698 | /* return the total length of the result */ |
| 1699 | return cur_total; |
| 1700 | } |
| 1701 | |
| 1702 | |
| 1703 | /* ------------------------------------------------------------------------ */ |
| 1704 | /* |
| 1705 | * Character mapper for 16-bit Wide Unicode, big-endian. Stores the |
| 1706 | * characters in big-endian UCS-2 representation. |
| 1707 | */ |
| 1708 | size_t CCharmapToLocalUcs2Big::map(wchar_t unicode_char, char **output_ptr, |
| 1709 | size_t *output_len) const |
| 1710 | { |
| 1711 | /* |
| 1712 | * If we don't have room for another byte pair, abort. Note that we |
| 1713 | * really do want to store exactly two bytes, not sizeof(anything), |
| 1714 | * since we're storing to the UCS-2 file format, which encodes each |
| 1715 | * character in two bytes. |
| 1716 | */ |
| 1717 | if (*output_len < 2) |
| 1718 | { |
| 1719 | *output_len = 0; |
| 1720 | return 2; |
| 1721 | } |
| 1722 | |
| 1723 | /* |
| 1724 | * Store the big-endian 16-bit value with no translation - unicode |
| 1725 | * is the same everywhere. |
| 1726 | * |
| 1727 | * Note that the need to perform this trivial translation for this |
| 1728 | * character set is a secondary reason that this routine is virtual |
| 1729 | * (the primary reason is to handle the default ASCII translation). |
| 1730 | * |
| 1731 | * Store the high-order 8 bits in the first byte, and the low-order |
| 1732 | * 8 bits in the second byte. |
| 1733 | */ |
| 1734 | **output_ptr = ((unicode_char >> 8) & 0xff); |
| 1735 | *(*output_ptr + 1) = (unicode_char & 0xff); |
| 1736 | |
| 1737 | /* skip two bytes in the output */ |
| 1738 | *output_ptr += 2; |
| 1739 | *output_len -= 2; |
| 1740 | |
| 1741 | /* return the size of the result */ |
| 1742 | return 2; |
| 1743 | } |
| 1744 | |
| 1745 | /* |
| 1746 | * Map a UTF-8 string of known byte length to the local character set |
| 1747 | */ |
| 1748 | size_t CCharmapToLocalUcs2Big:: |
| 1749 | map_utf8(char *dest, size_t dest_len, |
| 1750 | utf8_ptr src, size_t src_byte_len, |
| 1751 | size_t *src_bytes_used) const |
| 1752 | { |
| 1753 | utf8_ptr src_start; |
| 1754 | size_t cur_total; |
| 1755 | char *srcend; |
| 1756 | |
| 1757 | /* remember where we started */ |
| 1758 | src_start = src; |
| 1759 | |
| 1760 | /* compute where the source buffer ends */ |
| 1761 | srcend = src.getptr() + src_byte_len; |
| 1762 | |
| 1763 | /* copy characters until we reach the end of the source string */ |
| 1764 | for (cur_total = 0 ; src.getptr() < srcend ; src.inc()) |
| 1765 | { |
| 1766 | /* |
| 1767 | * if we have room, add it; otherwise, zero the output length |
| 1768 | * remaining so we don't try to add anything more |
| 1769 | */ |
| 1770 | if (dest == 0) |
| 1771 | { |
| 1772 | /* we're not storing anything */ |
| 1773 | } |
| 1774 | else if (dest_len >= 2) |
| 1775 | { |
| 1776 | wchar_t unicode_char; |
| 1777 | |
| 1778 | /* get the current character */ |
| 1779 | unicode_char = src.getch(); |
| 1780 | |
| 1781 | /* add the sequence */ |
| 1782 | *dest++ = ((unicode_char >> 8) & 0xff); |
| 1783 | *dest++ = (unicode_char & 0xff); |
| 1784 | |
| 1785 | /* adjust the length remaining */ |
| 1786 | dest_len -= 2; |
| 1787 | } |
| 1788 | else |
| 1789 | { |
| 1790 | /* it doesn't fit - stop now */ |
| 1791 | break; |
| 1792 | } |
| 1793 | |
| 1794 | /* count the length in the total */ |
| 1795 | cur_total += 2; |
| 1796 | } |
| 1797 | |
| 1798 | /* if the caller wants to know how much space we used, tell them */ |
| 1799 | if (src_bytes_used != 0) |
| 1800 | *src_bytes_used = src.getptr() - src_start.getptr(); |
| 1801 | |
| 1802 | /* return the total length of the result */ |
| 1803 | return cur_total; |
| 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | * Map a null-terminated UTF-8 string to the local character set |
| 1808 | */ |
| 1809 | size_t CCharmapToLocalUcs2Big:: |
| 1810 | map_utf8z(char *dest, size_t dest_len, utf8_ptr src) const |
| 1811 | { |
| 1812 | size_t cur_total; |
| 1813 | |
| 1814 | /* copy characters until we find the terminating null */ |
| 1815 | for (cur_total = 0 ; src.getch() != 0 ; src.inc()) |
| 1816 | { |
| 1817 | /* |
| 1818 | * if we have room, add it; otherwise, zero the output length |
| 1819 | * remaining so we don't try to add anything more |
| 1820 | */ |
| 1821 | if (dest_len >= 2) |
| 1822 | { |
| 1823 | wchar_t unicode_char; |
| 1824 | |
| 1825 | /* get the current character */ |
| 1826 | unicode_char = src.getch(); |
| 1827 | |
| 1828 | /* add the sequence */ |
| 1829 | *dest++ = ((unicode_char >> 8) & 0xff); |
| 1830 | *dest++ = (unicode_char & 0xff); |
| 1831 | |
| 1832 | /* adjust the length remaining */ |
| 1833 | dest_len -= 2; |
| 1834 | } |
| 1835 | else |
| 1836 | { |
| 1837 | /* it doesn't fit - zero the output length remaining */ |
| 1838 | dest_len = 0; |
| 1839 | } |
| 1840 | |
| 1841 | /* count the length in the total */ |
| 1842 | cur_total += 2; |
| 1843 | } |
| 1844 | |
| 1845 | /* return the total length of the result */ |
| 1846 | return cur_total; |
| 1847 | } |
| 1848 | |
| 1849 | |
| 1850 | /* ------------------------------------------------------------------------ */ |
| 1851 | /* |
| 1852 | * Character mapper for 16-bit Wide Unicode, little-endian. Stores the |
| 1853 | * characters in little-endian UCS-2 representation. |
| 1854 | */ |
| 1855 | size_t CCharmapToLocalUcs2Little::map(wchar_t unicode_char, |
| 1856 | char **output_ptr, |
| 1857 | size_t *output_len) const |
| 1858 | { |
| 1859 | /* |
| 1860 | * If we don't have room for another byte pair, abort. Note that we |
| 1861 | * really do want to store exactly two bytes, not sizeof(anything), |
| 1862 | * since we're storing to the UCS-2 file format, which encodes each |
| 1863 | * character in two bytes. |
| 1864 | */ |
| 1865 | if (*output_len < 2) |
| 1866 | { |
| 1867 | *output_len = 0; |
| 1868 | return 2; |
| 1869 | } |
| 1870 | |
| 1871 | /* |
| 1872 | * Store the little-endian 16-bit value with no translation - |
| 1873 | * unicode is the same everywhere. |
| 1874 | * |
| 1875 | * Note that the need to perform this trivial translation for this |
| 1876 | * character set is a secondary reason that this routine is virtual |
| 1877 | * (the primary reason is to handle the default ASCII translation). |
| 1878 | * |
| 1879 | * Store the low-order 8 bits in the first byte, and the high-order |
| 1880 | * 8 bits in the second byte. |
| 1881 | */ |
| 1882 | **output_ptr = (unicode_char & 0xff); |
| 1883 | *(*output_ptr + 1) = ((unicode_char >> 8) & 0xff); |
| 1884 | |
| 1885 | /* skip two bytes in the output */ |
| 1886 | *output_ptr += 2; |
| 1887 | *output_len -= 2; |
| 1888 | |
| 1889 | /* return the size of the result */ |
| 1890 | return 2; |
| 1891 | } |
| 1892 | |
| 1893 | /* |
| 1894 | * Map a UTF-8 string of known byte length to the local character set |
| 1895 | */ |
| 1896 | size_t CCharmapToLocalUcs2Little:: |
| 1897 | map_utf8(char *dest, size_t dest_len, |
| 1898 | utf8_ptr src, size_t src_byte_len, |
| 1899 | size_t *src_bytes_used) const |
| 1900 | { |
| 1901 | utf8_ptr src_start; |
| 1902 | size_t cur_total; |
| 1903 | char *srcend; |
| 1904 | |
| 1905 | /* remember where we started */ |
| 1906 | src_start = src; |
| 1907 | |
| 1908 | /* compute where the source buffer ends */ |
| 1909 | srcend = src.getptr() + src_byte_len; |
| 1910 | |
| 1911 | /* copy characters until we reach the end of the source string */ |
| 1912 | for (cur_total = 0 ; src.getptr() < srcend ; src.inc()) |
| 1913 | { |
| 1914 | /* |
| 1915 | * if we have room, add it; otherwise, zero the output length |
| 1916 | * remaining so we don't try to add anything more |
| 1917 | */ |
| 1918 | if (dest == 0) |
| 1919 | { |
| 1920 | /* we're just counting - don't store anything */ |
| 1921 | } |
| 1922 | else if (dest_len >= 2) |
| 1923 | { |
| 1924 | wchar_t unicode_char; |
| 1925 | |
| 1926 | /* get the current character */ |
| 1927 | unicode_char = src.getch(); |
| 1928 | |
| 1929 | /* add the sequence */ |
| 1930 | *dest++ = (unicode_char & 0xff); |
| 1931 | *dest++ = ((unicode_char >> 8) & 0xff); |
| 1932 | |
| 1933 | /* adjust the length remaining */ |
| 1934 | dest_len -= 2; |
| 1935 | } |
| 1936 | else |
| 1937 | { |
| 1938 | /* it doesn't fit - stop now */ |
| 1939 | break; |
| 1940 | } |
| 1941 | |
| 1942 | /* count the length in the total */ |
| 1943 | cur_total += 2; |
| 1944 | } |
| 1945 | |
| 1946 | /* if the caller wants to know how much space we used, tell them */ |
| 1947 | if (src_bytes_used != 0) |
| 1948 | *src_bytes_used = src.getptr() - src_start.getptr(); |
| 1949 | |
| 1950 | /* return the total length of the result */ |
| 1951 | return cur_total; |
| 1952 | } |
| 1953 | |
| 1954 | /* |
| 1955 | * Map a null-terminated UTF-8 string to the local character set |
| 1956 | */ |
| 1957 | size_t CCharmapToLocalUcs2Little:: |
| 1958 | map_utf8z(char *dest, size_t dest_len, utf8_ptr src) const |
| 1959 | { |
| 1960 | size_t cur_total; |
| 1961 | |
| 1962 | /* copy characters until we find the terminating null */ |
| 1963 | for (cur_total = 0 ; src.getch() != 0 ; src.inc()) |
| 1964 | { |
| 1965 | /* |
| 1966 | * if we have room, add it; otherwise, zero the output length |
| 1967 | * remaining so we don't try to add anything more |
| 1968 | */ |
| 1969 | if (dest_len >= 2) |
| 1970 | { |
| 1971 | wchar_t unicode_char; |
| 1972 | |
| 1973 | /* get the current character */ |
| 1974 | unicode_char = src.getch(); |
| 1975 | |
| 1976 | /* add the sequence */ |
| 1977 | *dest++ = (unicode_char & 0xff); |
| 1978 | *dest++ = ((unicode_char >> 8) & 0xff); |
| 1979 | |
| 1980 | /* adjust the length remaining */ |
| 1981 | dest_len -= 2; |
| 1982 | } |
| 1983 | else |
| 1984 | { |
| 1985 | /* it doesn't fit - zero the output length remaining */ |
| 1986 | dest_len = 0; |
| 1987 | } |
| 1988 | |
| 1989 | /* count the length in the total */ |
| 1990 | cur_total += 2; |
| 1991 | } |
| 1992 | |
| 1993 | /* |
| 1994 | * if there's room for a null terminator character (which takes two |
| 1995 | * bytes in UCS-2), add it, but don't count it in the return length |
| 1996 | */ |
| 1997 | if (dest_len >= 2) |
| 1998 | { |
| 1999 | *dest++ = '\0'; |
| 2000 | *dest++ = '\0'; |
| 2001 | } |
| 2002 | |
| 2003 | /* return the total length of the result */ |
| 2004 | return cur_total; |
| 2005 | } |
| 2006 | |
| 2007 | |
| 2008 | /* ------------------------------------------------------------------------ */ |
| 2009 | /* |
| 2010 | * Character mapper - local to UTF-8 |
| 2011 | */ |
| 2012 | |
| 2013 | /* |
| 2014 | * create an appropriate mapping object for the given mapping file |
| 2015 | */ |
| 2016 | CCharmapToUni *CCharmapToUni::load(class CResLoader *res_loader, |
| 2017 | const char *table_name) |
| 2018 | { |
| 2019 | osfildef *fp; |
| 2020 | CCharmapToUni *mapper; |
| 2021 | charmap_type_t map_type; |
| 2022 | |
| 2023 | /* if they want a trivial UTF-8 translator, return one */ |
| 2024 | if (name_is_utf8_synonym(table_name)) |
| 2025 | return new CCharmapToUniUTF8(); |
| 2026 | |
| 2027 | /* if they want a 16-bit Unicode mapping, return one */ |
| 2028 | if (name_is_ucs2le_synonym(table_name)) |
| 2029 | return new CCharmapToUniUcs2Little(); |
| 2030 | if (name_is_ucs2be_synonym(table_name)) |
| 2031 | return new CCharmapToUniUcs2Big(); |
| 2032 | |
| 2033 | /* presume failure */ |
| 2034 | mapper = 0; |
| 2035 | |
| 2036 | /* open and characterize the mapping file */ |
| 2037 | fp = open_map_file_syn(res_loader, table_name, &map_type); |
| 2038 | |
| 2039 | /* check to make sure we opened a file */ |
| 2040 | if (fp == 0) |
| 2041 | { |
| 2042 | /* |
| 2043 | * if there was no file, and they want a plain ASCII translator, |
| 2044 | * return a default ASCII translator |
| 2045 | */ |
| 2046 | if (name_is_ascii_synonym(table_name)) |
| 2047 | return new CCharmapToUniASCII(); |
| 2048 | |
| 2049 | /* if they want an ISO-8859-1 translator, return a default one */ |
| 2050 | if (name_is_8859_1_synonym(table_name)) |
| 2051 | return new CCharmapToUni8859_1(); |
| 2052 | |
| 2053 | /* return failure */ |
| 2054 | return 0; |
| 2055 | } |
| 2056 | |
| 2057 | /* create an appropriate mapper */ |
| 2058 | switch(map_type) |
| 2059 | { |
| 2060 | case CHARMAP_TYPE_SB: |
| 2061 | /* create a single-byte mapper */ |
| 2062 | mapper = new CCharmapToUniSB(); |
| 2063 | break; |
| 2064 | |
| 2065 | case CHARMAP_TYPE_DB: |
| 2066 | /* create a double-byte mapper */ |
| 2067 | mapper = new CCharmapToUniDB(); |
| 2068 | break; |
| 2069 | |
| 2070 | case CHARMAP_TYPE_MB: |
| 2071 | /* create a mixed multi-byte mapper */ |
| 2072 | mapper = new CCharmapToUniMB(); |
| 2073 | break; |
| 2074 | |
| 2075 | default: |
| 2076 | /* other mapper types are currently unknown */ |
| 2077 | break; |
| 2078 | } |
| 2079 | |
| 2080 | /* if we successfully created a mapper, tell it to load the table */ |
| 2081 | if (mapper != 0) |
| 2082 | { |
| 2083 | /* load the table */ |
| 2084 | mapper->load_table(fp); |
| 2085 | } |
| 2086 | |
| 2087 | /* close the file */ |
| 2088 | osfcls(fp); |
| 2089 | |
| 2090 | /* return the mapper, if any */ |
| 2091 | return mapper; |
| 2092 | } |
| 2093 | |
| 2094 | |
| 2095 | /* |
| 2096 | * load a mapping table |
| 2097 | */ |
| 2098 | void CCharmapToUni::load_table(osfildef *fp) |
| 2099 | { |
| 2100 | uchar buf[256]; |
| 2101 | uint entry_cnt; |
| 2102 | |
| 2103 | /* read the header and the local table header */ |
| 2104 | if (osfrb(fp, buf, 6)) |
| 2105 | return; |
| 2106 | |
| 2107 | /* get the local table size from the local table header */ |
| 2108 | entry_cnt = osrp2(buf + 4); |
| 2109 | |
| 2110 | /* read the mappings */ |
| 2111 | while (entry_cnt > 0) |
| 2112 | { |
| 2113 | size_t cur; |
| 2114 | const uchar *p; |
| 2115 | |
| 2116 | /* figure out how many entries we can read this time */ |
| 2117 | cur = sizeof(buf)/4; |
| 2118 | if (cur > entry_cnt) |
| 2119 | cur = entry_cnt; |
| 2120 | |
| 2121 | /* read the entries */ |
| 2122 | if (osfrb(fp, buf, cur*4)) |
| 2123 | return; |
| 2124 | |
| 2125 | /* deduct this number from the remaining count */ |
| 2126 | entry_cnt -= cur; |
| 2127 | |
| 2128 | /* scan the entries */ |
| 2129 | for (p = buf ; cur > 0 ; p += 4, --cur) |
| 2130 | { |
| 2131 | /* map this entry */ |
| 2132 | set_mapping(osrp2(p), osrp2(p+2)); |
| 2133 | } |
| 2134 | } |
| 2135 | } |
| 2136 | |
| 2137 | /* |
| 2138 | * Map a null-terminated string into a buffer |
| 2139 | */ |
| 2140 | size_t CCharmapToUni::map_str(char *outbuf, size_t outbuflen, |
| 2141 | const char *input_str) |
| 2142 | { |
| 2143 | /* map the string to the output buffer */ |
| 2144 | size_t output_len = map(&outbuf, &outbuflen, input_str, strlen(input_str)); |
| 2145 | |
| 2146 | /* if there's space remaining in the output buffer, add a null byte */ |
| 2147 | if (outbuflen != 0) |
| 2148 | *outbuf = '\0'; |
| 2149 | |
| 2150 | /* return the number of bytes needed for the conversion */ |
| 2151 | return output_len; |
| 2152 | } |
| 2153 | |
| 2154 | /* |
| 2155 | * Map a counted-length string into a buffer |
| 2156 | */ |
| 2157 | size_t CCharmapToUni::map_str(char *outbuf, size_t outbuflen, |
| 2158 | const char *input_str, size_t input_len) |
| 2159 | { |
| 2160 | /* map the string to the output buffer and return the result */ |
| 2161 | return map(&outbuf, &outbuflen, input_str, input_len); |
| 2162 | } |
| 2163 | |
| 2164 | /* |
| 2165 | * Map a null-terminated string, allocating space for the result |
| 2166 | */ |
| 2167 | size_t CCharmapToUni::map_str_alo(char **outbuf, const char *input_str) |
| 2168 | { |
| 2169 | /* figure the space needed */ |
| 2170 | size_t outlen = map_str(0, 0, input_str); |
| 2171 | |
| 2172 | /* allocate the buffer */ |
| 2173 | *outbuf = (char *)t3malloc(outlen + 1); |
| 2174 | |
| 2175 | /* map the sring */ |
| 2176 | return map_str(*outbuf, outlen, input_str); |
| 2177 | } |
| 2178 | |
| 2179 | /* |
| 2180 | * Validate a buffer of utf-8 characters |
| 2181 | */ |
| 2182 | void CCharmapToUni::validate(char *buf, size_t len) |
| 2183 | { |
| 2184 | for ( ; len != 0 ; ++buf, --len) |
| 2185 | { |
| 2186 | /* check the type of the character */ |
| 2187 | char c = *buf; |
| 2188 | if ((c & 0x80) == 0) |
| 2189 | { |
| 2190 | /* 0..127 are one-byte characters, so this is valid */ |
| 2191 | } |
| 2192 | else if ((c & 0xC0) == 0x80) |
| 2193 | { |
| 2194 | /* |
| 2195 | * This byte has the pattern 10xxxxxx, which makes it a |
| 2196 | * continuation byte. Since we didn't just come from a |
| 2197 | * multi-byte intro byte, this is invalid. Change this byte to |
| 2198 | * '?'. |
| 2199 | */ |
| 2200 | *buf = '?'; |
| 2201 | } |
| 2202 | else if ((c & 0xE0) == 0xC0) |
| 2203 | { |
| 2204 | /* |
| 2205 | * This byte has the pattern 110xxxxx, which makes it the first |
| 2206 | * byte of a two-byte character sequence. The next byte must |
| 2207 | * have the pattern 10xxxxxx - if not, mark the current |
| 2208 | * character as invalid, since it's not part of a valid |
| 2209 | * sequence, and deal with the next byte separately. |
| 2210 | */ |
| 2211 | if (len > 1 && (*(buf+1) & 0xC0) == 0x80) |
| 2212 | { |
| 2213 | /* we have a valid two-byte sequence - skip it */ |
| 2214 | ++buf; |
| 2215 | --len; |
| 2216 | } |
| 2217 | else |
| 2218 | { |
| 2219 | /* |
| 2220 | * the next byte isn't a continuation, so the current byte |
| 2221 | * is invalid |
| 2222 | */ |
| 2223 | *buf = '?'; |
| 2224 | } |
| 2225 | } |
| 2226 | else |
| 2227 | { |
| 2228 | /* |
| 2229 | * This byte has the pattern 111xxxxx, which makes it the first |
| 2230 | * byte of a three-byte sequence. The next two bytes must be |
| 2231 | * marked as continuation bytes. |
| 2232 | */ |
| 2233 | if (len > 2 |
| 2234 | && (*(buf+1) & 0xC0) == 0x80 |
| 2235 | && (*(buf+2) & 0xC0) == 0x80) |
| 2236 | { |
| 2237 | /* we have a valid three-byte sequence - skip it */ |
| 2238 | buf += 2; |
| 2239 | len -= 2; |
| 2240 | } |
| 2241 | else |
| 2242 | { |
| 2243 | /* this is not a valid three-byte sequence */ |
| 2244 | *buf = '?'; |
| 2245 | } |
| 2246 | } |
| 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | |
| 2251 | /* ------------------------------------------------------------------------ */ |
| 2252 | /* |
| 2253 | * Basic single-byte character set to UTF-8 mapper |
| 2254 | */ |
| 2255 | |
| 2256 | /* |
| 2257 | * read from a single-byte file and translate to UTF-8 |
| 2258 | */ |
| 2259 | size_t CCharmapToUniSB_basic::read_file(CVmDataSource *fp, |
| 2260 | char *buf, size_t bufl) |
| 2261 | { |
| 2262 | size_t inlen; |
| 2263 | |
| 2264 | /* |
| 2265 | * Compute how much to read from the file. The input file is |
| 2266 | * composed of single-byte characters, so only read up to one third |
| 2267 | * of the buffer length; this will ensure that we can always fit |
| 2268 | * what we read into the caller's buffer. |
| 2269 | */ |
| 2270 | inlen = bufl / 3; |
| 2271 | |
| 2272 | /* in any case, we can't read more than our own buffer size */ |
| 2273 | if (inlen > sizeof(inbuf_)) |
| 2274 | inlen = sizeof(inbuf_); |
| 2275 | |
| 2276 | /* read from the file */ |
| 2277 | inlen = fp->readc(inbuf_, inlen); |
| 2278 | |
| 2279 | /* |
| 2280 | * Map data to the caller's buffer, and return the result. We're |
| 2281 | * certain that the data will fit in the caller's buffer: we're |
| 2282 | * mapping only a third as many characters as we have bytes |
| 2283 | * available, and each character can take up at most three bytes, |
| 2284 | * hence the worst case is that we fill the buffer completely. |
| 2285 | * |
| 2286 | * On the other hand, we may only fill the buffer to a third of its |
| 2287 | * capacity, but this is okay too, since we're not required to give |
| 2288 | * the caller everything they asked for. |
| 2289 | */ |
| 2290 | return map(&buf, &bufl, inbuf_, inlen); |
| 2291 | } |
| 2292 | |
| 2293 | |
| 2294 | /* ------------------------------------------------------------------------ */ |
| 2295 | /* |
| 2296 | * Plain ASCII local to UTF-8 mapper |
| 2297 | */ |
| 2298 | |
| 2299 | /* |
| 2300 | * map a string from the single-byte local character set to UTF-8 |
| 2301 | */ |
| 2302 | size_t CCharmapToUniASCII::map(char **outp, size_t *outlen, |
| 2303 | const char *inp, size_t inlen) const |
| 2304 | { |
| 2305 | size_t tot_outlen; |
| 2306 | |
| 2307 | /* we haven't written any characters to the output buffer yet */ |
| 2308 | tot_outlen = 0; |
| 2309 | |
| 2310 | /* scan each character (character == byte) in the input string */ |
| 2311 | for ( ; inlen > 0 ; --inlen, ++inp) |
| 2312 | { |
| 2313 | wchar_t uni; |
| 2314 | size_t csiz; |
| 2315 | |
| 2316 | /* |
| 2317 | * map any character outside of the 7-bit range to U+FFFD, the |
| 2318 | * Unicode REPLACEMENT CHARACTER, which is the standard way to |
| 2319 | * represent characters that can't be mapped from an incoming |
| 2320 | * character set |
| 2321 | */ |
| 2322 | if (((unsigned char)*inp) > 127) |
| 2323 | uni = 0xfffd; |
| 2324 | else |
| 2325 | uni = ((wchar_t)(unsigned char)*inp); |
| 2326 | |
| 2327 | /* get the size of this character */ |
| 2328 | csiz = utf8_ptr::s_wchar_size(uni); |
| 2329 | |
| 2330 | /* add it to the total output length */ |
| 2331 | tot_outlen += csiz; |
| 2332 | |
| 2333 | /* if there's room, add it to our output buffer */ |
| 2334 | if (*outlen >= csiz) |
| 2335 | { |
| 2336 | /* write it out */ |
| 2337 | *outp += utf8_ptr::s_putch(*outp, uni); |
| 2338 | |
| 2339 | /* deduct it from the remaining output length */ |
| 2340 | *outlen -= csiz; |
| 2341 | } |
| 2342 | else |
| 2343 | { |
| 2344 | /* there's no room - set the remaining output length to zero */ |
| 2345 | *outlen = 0; |
| 2346 | } |
| 2347 | } |
| 2348 | |
| 2349 | /* return the total output length */ |
| 2350 | return tot_outlen; |
| 2351 | } |
| 2352 | |
| 2353 | |
| 2354 | /* ------------------------------------------------------------------------ */ |
| 2355 | /* |
| 2356 | * Single-byte mapped local to UTF-8 mapper |
| 2357 | */ |
| 2358 | |
| 2359 | /* |
| 2360 | * map a string from the single-byte local character set to UTF-8 |
| 2361 | */ |
| 2362 | size_t CCharmapToUniSB::map(char **outp, size_t *outlen, |
| 2363 | const char *inp, size_t inlen) const |
| 2364 | { |
| 2365 | size_t tot_outlen; |
| 2366 | |
| 2367 | /* we haven't written any characters to the output buffer yet */ |
| 2368 | tot_outlen = 0; |
| 2369 | |
| 2370 | /* scan each character (character == byte) in the input string */ |
| 2371 | for ( ; inlen > 0 ; --inlen, ++inp) |
| 2372 | { |
| 2373 | wchar_t uni; |
| 2374 | size_t csiz; |
| 2375 | |
| 2376 | /* get the unicode mapping for this character */ |
| 2377 | uni = map_[(unsigned char)*inp]; |
| 2378 | |
| 2379 | /* get the size of this character */ |
| 2380 | csiz = utf8_ptr::s_wchar_size(uni); |
| 2381 | |
| 2382 | /* add it to the total output lenght */ |
| 2383 | tot_outlen += csiz; |
| 2384 | |
| 2385 | /* if there's room, add it to our output buffer */ |
| 2386 | if (*outlen >= csiz) |
| 2387 | { |
| 2388 | /* write it out */ |
| 2389 | *outp += utf8_ptr::s_putch(*outp, uni); |
| 2390 | |
| 2391 | /* deduct it from the remaining output length */ |
| 2392 | *outlen -= csiz; |
| 2393 | } |
| 2394 | else |
| 2395 | { |
| 2396 | /* there's no room - set the remaining output length to zero */ |
| 2397 | *outlen = 0; |
| 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | /* return the total output length */ |
| 2402 | return tot_outlen; |
| 2403 | } |
| 2404 | |
| 2405 | /* ------------------------------------------------------------------------ */ |
| 2406 | /* |
| 2407 | * Trivial UTF8-to-UTF8 input mapper |
| 2408 | */ |
| 2409 | |
| 2410 | /* |
| 2411 | * map a string |
| 2412 | */ |
| 2413 | size_t CCharmapToUniUTF8::map2(char **outp, size_t *outlen, |
| 2414 | const char *inp, size_t inlen, |
| 2415 | size_t *partial_len) const |
| 2416 | { |
| 2417 | size_t copy_len; |
| 2418 | |
| 2419 | /* |
| 2420 | * Make sure we copy only whole characters, by truncating the string |
| 2421 | * to a length that includes only whole characters. |
| 2422 | */ |
| 2423 | copy_len = utf8_ptr::s_trunc(inp, inlen); |
| 2424 | |
| 2425 | /* |
| 2426 | * note the length of any partial characters at the end of the buffer |
| 2427 | * for the caller - this is simply the difference between the original |
| 2428 | * length and the truncated copy length, since the truncation length |
| 2429 | * is simply the length excluding the partial last character bytes |
| 2430 | */ |
| 2431 | *partial_len = inlen - copy_len; |
| 2432 | |
| 2433 | /* limit the copying to what will fit in the output buffer */ |
| 2434 | if (copy_len > *outlen) |
| 2435 | { |
| 2436 | /* don't copy more than will fit, and don't copy partial characters */ |
| 2437 | copy_len = utf8_ptr::s_trunc(inp, *outlen); |
| 2438 | |
| 2439 | /* we don't have enough room, so set the output size to zero */ |
| 2440 | *outlen = 0; |
| 2441 | } |
| 2442 | else |
| 2443 | { |
| 2444 | /* we have room, so decrement the output size by the copy size */ |
| 2445 | *outlen -= copy_len; |
| 2446 | } |
| 2447 | |
| 2448 | /* copy the data, if we have an output buffer */ |
| 2449 | if (outp != 0) |
| 2450 | { |
| 2451 | /* copy the bytes */ |
| 2452 | memcpy(*outp, inp, copy_len); |
| 2453 | |
| 2454 | /* validate that the bytes we copied are well-formed UTF-8 */ |
| 2455 | validate(*outp, copy_len); |
| 2456 | |
| 2457 | /* advance the output pointer past the copied data */ |
| 2458 | *outp += copy_len; |
| 2459 | } |
| 2460 | |
| 2461 | /* |
| 2462 | * return the total input length -- the total output length is |
| 2463 | * always identical to the input length, because we don't change |
| 2464 | * anything |
| 2465 | */ |
| 2466 | return inlen; |
| 2467 | } |
| 2468 | |
| 2469 | /* |
| 2470 | * read a file |
| 2471 | */ |
| 2472 | size_t CCharmapToUniUTF8::read_file(CVmDataSource *fp, |
| 2473 | char *buf, size_t bufl) |
| 2474 | { |
| 2475 | size_t read_len; |
| 2476 | char *last_start; |
| 2477 | size_t last_got_len; |
| 2478 | size_t last_need_len; |
| 2479 | |
| 2480 | /* |
| 2481 | * Read directly from the file, up the buffer size minus two bytes. |
| 2482 | * We want to leave two extra bytes so that we can read any extra |
| 2483 | * continuation bytes for the last character, in order to ensure |
| 2484 | * that we always read whole characters; in the worst case, the last |
| 2485 | * character could be three bytes long, in which case we'd need to |
| 2486 | * read two extra bytes. |
| 2487 | * |
| 2488 | * If the available buffer size is less than three bytes, just read |
| 2489 | * the number of bytes they asked for and don't bother trying to |
| 2490 | * keep continuation sequences intact. |
| 2491 | */ |
| 2492 | if (bufl < 3) |
| 2493 | { |
| 2494 | read_len = fp->readc(buf, bufl); |
| 2495 | validate(buf, read_len); |
| 2496 | return read_len; |
| 2497 | } |
| 2498 | |
| 2499 | /* |
| 2500 | * read up to the buffer size, less two bytes for possible |
| 2501 | * continuation bytes |
| 2502 | */ |
| 2503 | read_len = fp->readc(buf, bufl - 2); |
| 2504 | |
| 2505 | /* |
| 2506 | * if we didn't satisfy the entire request, we're at the end of the |
| 2507 | * file, so there's no point in trying to finish off any |
| 2508 | * continuation sequences - in this case, just return what we have |
| 2509 | */ |
| 2510 | if (read_len < bufl - 2) |
| 2511 | { |
| 2512 | validate(buf, read_len); |
| 2513 | return read_len; |
| 2514 | } |
| 2515 | |
| 2516 | /* |
| 2517 | * Check the last byte we read to see if there's another byte or two |
| 2518 | * following. |
| 2519 | * |
| 2520 | * If the last byte is a continuation byte, this is a bit trickier. |
| 2521 | * We must back up to the preceding lead byte to figure out what we |
| 2522 | * have in this case. |
| 2523 | */ |
| 2524 | last_start = &buf[read_len - 1]; |
| 2525 | last_got_len = 1; |
| 2526 | if (utf8_ptr::s_is_continuation(last_start)) |
| 2527 | { |
| 2528 | /* |
| 2529 | * if we only read one byte, simply return the one byte - we |
| 2530 | * started in the middle of a sequence, so there's no way we can |
| 2531 | * read a complete sequence |
| 2532 | */ |
| 2533 | if (read_len == 1) |
| 2534 | { |
| 2535 | validate(buf, read_len); |
| 2536 | return read_len; |
| 2537 | } |
| 2538 | |
| 2539 | /* back up to the byte we're continuing from */ |
| 2540 | --last_start; |
| 2541 | ++last_got_len; |
| 2542 | |
| 2543 | /* |
| 2544 | * if this is another continuation byte, we've reached the maximum |
| 2545 | * byte length of three for a single character, so there's no way |
| 2546 | * we could need to read anything more |
| 2547 | */ |
| 2548 | if (utf8_ptr::s_is_continuation(last_start)) |
| 2549 | { |
| 2550 | validate(buf, read_len); |
| 2551 | return read_len; |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | /* |
| 2556 | * Okay: we have last_start pointing to the start of the last |
| 2557 | * character, and last_got_len the number of bytes we actually have for |
| 2558 | * that last character. If the needed length differs from the length |
| 2559 | * we actually have, we need to read more. |
| 2560 | */ |
| 2561 | last_need_len = utf8_ptr::s_charsize(*last_start); |
| 2562 | if (last_need_len > last_got_len) |
| 2563 | { |
| 2564 | /* |
| 2565 | * we need more than we actually read, so read the remaining |
| 2566 | * characters |
| 2567 | */ |
| 2568 | read_len += fp->readc(buf + read_len, last_need_len - last_got_len); |
| 2569 | } |
| 2570 | |
| 2571 | /* validate the buffer - ensure that it's well-formed UTF-8 */ |
| 2572 | validate(buf, read_len); |
| 2573 | |
| 2574 | /* return the length we read */ |
| 2575 | return read_len; |
| 2576 | } |
| 2577 | |
| 2578 | /* ------------------------------------------------------------------------ */ |
| 2579 | /* |
| 2580 | * Basic UCS-2 to UTF-8 mapper |
| 2581 | */ |
| 2582 | |
| 2583 | /* |
| 2584 | * Read from a file, translating to UTF-8 encoding |
| 2585 | */ |
| 2586 | size_t CCharmapToUniUcs2::read_file(CVmDataSource *fp, |
| 2587 | char *buf, size_t bufl) |
| 2588 | { |
| 2589 | size_t inlen; |
| 2590 | |
| 2591 | /* |
| 2592 | * Compute how much to read from the file. The input file is composed |
| 2593 | * of two-byte characters, so only read up to two thirds of the buffer |
| 2594 | * length; this will ensure that we can always fit what we read into |
| 2595 | * the caller's buffer. |
| 2596 | * |
| 2597 | * Note that we divide by three first, then double the result, to |
| 2598 | * ensure that we read an even number of bytes. Each UCS-2 character |
| 2599 | * is represented in exactly two bytes, so we must always read pairs of |
| 2600 | * bytes to be sure we're reading whole characters. |
| 2601 | */ |
| 2602 | inlen = bufl / 3; |
| 2603 | inlen *= 2; |
| 2604 | |
| 2605 | /* in any case, we can't read more than our own buffer size */ |
| 2606 | if (inlen > sizeof(inbuf_)) |
| 2607 | inlen = sizeof(inbuf_); |
| 2608 | |
| 2609 | /* read from the file */ |
| 2610 | inlen = fp->readc(inbuf_, inlen); |
| 2611 | |
| 2612 | /* |
| 2613 | * Map data to the caller's buffer, and return the result. We're |
| 2614 | * certain that the data will fit in the caller's buffer: we're |
| 2615 | * mapping only a third as many characters as we have bytes |
| 2616 | * available, and each character can take up at most three bytes, |
| 2617 | * hence the worst case is that we fill the buffer completely. |
| 2618 | * |
| 2619 | * On the other hand, we may only fill the buffer to a third of its |
| 2620 | * capacity, but this is okay too, since we're not required to give |
| 2621 | * the caller everything they asked for. |
| 2622 | */ |
| 2623 | return map(&buf, &bufl, inbuf_, inlen); |
| 2624 | } |
| 2625 | |
| 2626 | /* ------------------------------------------------------------------------ */ |
| 2627 | /* |
| 2628 | * UCS-2 little-endian to UTF-8 mapper |
| 2629 | */ |
| 2630 | |
| 2631 | /* |
| 2632 | * map a string |
| 2633 | */ |
| 2634 | size_t CCharmapToUniUcs2Little::map(char **outp, size_t *outlen, |
| 2635 | const char *inp, size_t inlen) const |
| 2636 | { |
| 2637 | size_t tot_outlen; |
| 2638 | |
| 2639 | /* we haven't written any characters to the output buffer yet */ |
| 2640 | tot_outlen = 0; |
| 2641 | |
| 2642 | /* scan each character (character == byte pair) in the input string */ |
| 2643 | for ( ; inlen > 1 ; inlen -= 2, inp += 2) |
| 2644 | { |
| 2645 | wchar_t uni; |
| 2646 | size_t csiz; |
| 2647 | |
| 2648 | /* |
| 2649 | * read the little-endian two-byte value - no mapping is |
| 2650 | * required, since UCS-2 uses the same code point assignments as |
| 2651 | * UTF-8 |
| 2652 | */ |
| 2653 | uni = ((wchar_t)(unsigned char)*inp) |
| 2654 | + (((wchar_t)(unsigned char)*(inp + 1)) << 8); |
| 2655 | |
| 2656 | /* get the size of this character */ |
| 2657 | csiz = utf8_ptr::s_wchar_size(uni); |
| 2658 | |
| 2659 | /* add it to the total output lenght */ |
| 2660 | tot_outlen += csiz; |
| 2661 | |
| 2662 | /* if there's room, add it to our output buffer */ |
| 2663 | if (*outlen >= csiz) |
| 2664 | { |
| 2665 | /* write it out */ |
| 2666 | *outp += utf8_ptr::s_putch(*outp, uni); |
| 2667 | |
| 2668 | /* deduct it from the remaining output length */ |
| 2669 | *outlen -= csiz; |
| 2670 | } |
| 2671 | else |
| 2672 | { |
| 2673 | /* there's no room - set the remaining output length to zero */ |
| 2674 | *outlen = 0; |
| 2675 | } |
| 2676 | } |
| 2677 | |
| 2678 | /* return the total output length */ |
| 2679 | return tot_outlen; |
| 2680 | } |
| 2681 | |
| 2682 | /* ------------------------------------------------------------------------ */ |
| 2683 | /* |
| 2684 | * UCS-2 big-endian to UTF-8 mapper |
| 2685 | */ |
| 2686 | |
| 2687 | /* |
| 2688 | * map a string |
| 2689 | */ |
| 2690 | size_t CCharmapToUniUcs2Big::map(char **outp, size_t *outlen, |
| 2691 | const char *inp, size_t inlen) const |
| 2692 | { |
| 2693 | size_t tot_outlen; |
| 2694 | |
| 2695 | /* we haven't written any characters to the output buffer yet */ |
| 2696 | tot_outlen = 0; |
| 2697 | |
| 2698 | /* scan each character (character == byte pair) in the input string */ |
| 2699 | for ( ; inlen > 1 ; inlen -= 2, inp += 2) |
| 2700 | { |
| 2701 | wchar_t uni; |
| 2702 | size_t csiz; |
| 2703 | |
| 2704 | /* |
| 2705 | * read the big-endian two-byte value - no mapping is required, |
| 2706 | * since UCS-2 uses the same code point assignments as UTF-8 |
| 2707 | */ |
| 2708 | uni = (((wchar_t)(unsigned char)*inp) << 8) |
| 2709 | + ((wchar_t)(unsigned char)*(inp + 1)); |
| 2710 | |
| 2711 | /* get the size of this character */ |
| 2712 | csiz = utf8_ptr::s_wchar_size(uni); |
| 2713 | |
| 2714 | /* add it to the total output lenght */ |
| 2715 | tot_outlen += csiz; |
| 2716 | |
| 2717 | /* if there's room, add it to our output buffer */ |
| 2718 | if (*outlen >= csiz) |
| 2719 | { |
| 2720 | /* write it out */ |
| 2721 | *outp += utf8_ptr::s_putch(*outp, uni); |
| 2722 | |
| 2723 | /* deduct it from the remaining output length */ |
| 2724 | *outlen -= csiz; |
| 2725 | } |
| 2726 | else |
| 2727 | { |
| 2728 | /* there's no room - set the remaining output length to zero */ |
| 2729 | *outlen = 0; |
| 2730 | } |
| 2731 | } |
| 2732 | |
| 2733 | /* return the total output length */ |
| 2734 | return tot_outlen; |
| 2735 | } |
| 2736 | |
| 2737 | /* ------------------------------------------------------------------------ */ |
| 2738 | /* |
| 2739 | * Multi-byte character set translation to Unicode |
| 2740 | */ |
| 2741 | |
| 2742 | /* |
| 2743 | * construct the mapper |
| 2744 | */ |
| 2745 | CCharmapToUniMB::CCharmapToUniMB() |
| 2746 | { |
| 2747 | int i; |
| 2748 | cmap_mb_entry *p; |
| 2749 | |
| 2750 | /* clear out the mapping table */ |
| 2751 | for (i = 0, p = map_ ; i < 256 ; ++i, ++p) |
| 2752 | { |
| 2753 | /* assume this lead byte won't have a sub-table */ |
| 2754 | p->sub = 0; |
| 2755 | |
| 2756 | /* |
| 2757 | * we don't have a mapping for this lead byte yet, so use U+FFFD |
| 2758 | * (the Unicode REPLACEMENT CHARACTER) as the default mapping in |
| 2759 | * case we never assign it any other mapping |
| 2760 | */ |
| 2761 | p->ch = 0xFFFD; |
| 2762 | } |
| 2763 | } |
| 2764 | |
| 2765 | /* |
| 2766 | * delete the mapper |
| 2767 | */ |
| 2768 | CCharmapToUniMB::~CCharmapToUniMB() |
| 2769 | { |
| 2770 | int i; |
| 2771 | cmap_mb_entry *p; |
| 2772 | |
| 2773 | /* delete all of our sub-tables */ |
| 2774 | for (i = 0, p = map_ ; i < 256 ; ++i, ++p) |
| 2775 | { |
| 2776 | /* if this sub-table was allocated, delete it */ |
| 2777 | if (p->sub != 0) |
| 2778 | t3free(p->sub); |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | /* |
| 2783 | * Set a mapping |
| 2784 | */ |
| 2785 | void CCharmapToUniMB::set_mapping(wchar_t uni_code_pt, wchar_t local_code_pt) |
| 2786 | { |
| 2787 | /* |
| 2788 | * Check to see if it's a one-byte or two-byte mapping. If the local |
| 2789 | * code point is in the range 0-255, it's a one-byte character; |
| 2790 | * otherwise, it's a two-byte character. |
| 2791 | */ |
| 2792 | if (local_code_pt <= 255) |
| 2793 | { |
| 2794 | /* it's a single-byte character, so simply set the mapping */ |
| 2795 | map_[(unsigned char)local_code_pt].ch = uni_code_pt; |
| 2796 | } |
| 2797 | else |
| 2798 | { |
| 2799 | cmap_mb_entry *entp; |
| 2800 | wchar_t *subp; |
| 2801 | |
| 2802 | /* |
| 2803 | * Get the mapping table entry for the lead byte. The lead byte of |
| 2804 | * the local code point is given by the high-order byte of the |
| 2805 | * local code point. (Note that this doesn't have anything to do |
| 2806 | * with the endian-ness of the local platform. The generic Unicode |
| 2807 | * mapping tables are specifically designed this way, independently |
| 2808 | * of endian-ness.) |
| 2809 | */ |
| 2810 | entp = &map_[(unsigned char)((local_code_pt >> 8) & 0xff)]; |
| 2811 | |
| 2812 | /* |
| 2813 | * It's a two-byte character. The high-order byte is the lead |
| 2814 | * byte, and the low-order byte is the trailing byte of the |
| 2815 | * two-byte sequence. |
| 2816 | * |
| 2817 | * If we haven't previously set up a sub-table for the lead byte, |
| 2818 | * do so now. |
| 2819 | */ |
| 2820 | if ((subp = entp->sub) == 0) |
| 2821 | { |
| 2822 | size_t i; |
| 2823 | wchar_t *p; |
| 2824 | |
| 2825 | /* allocate a new sub-mapping table for the lead byte */ |
| 2826 | subp = entp->sub = (wchar_t *)t3malloc(256 * sizeof(wchar_t)); |
| 2827 | |
| 2828 | /* initialize each entry to U+FFFD, in case we never map them */ |
| 2829 | for (i = 256, p = subp ; i != 0 ; --i, *p++ = 0xFFFD) ; |
| 2830 | } |
| 2831 | |
| 2832 | /* set the mapping in the sub-table for the second byte */ |
| 2833 | subp[(unsigned char)(local_code_pt & 0xff)] = uni_code_pt; |
| 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | /* |
| 2838 | * map a string, providing partial character info |
| 2839 | */ |
| 2840 | size_t CCharmapToUniMB::map2(char **output_ptr, size_t *output_buf_len, |
| 2841 | const char *input_ptr, size_t input_len, |
| 2842 | size_t *partial_len) const |
| 2843 | { |
| 2844 | size_t needed_out_len; |
| 2845 | |
| 2846 | /* presume we won't have a partial last character */ |
| 2847 | *partial_len = 0; |
| 2848 | |
| 2849 | /* we haven't found anything to store in the output yet */ |
| 2850 | needed_out_len = 0; |
| 2851 | |
| 2852 | /* keep going until we've mapped each character */ |
| 2853 | while (input_len != 0) |
| 2854 | { |
| 2855 | unsigned char c; |
| 2856 | const cmap_mb_entry *entp; |
| 2857 | wchar_t wc; |
| 2858 | size_t wlen; |
| 2859 | |
| 2860 | /* get the lead byte of the next input character */ |
| 2861 | c = *input_ptr; |
| 2862 | |
| 2863 | /* get the primary mapping table entry for the lead byte */ |
| 2864 | entp = &map_[c]; |
| 2865 | |
| 2866 | /* check for a one-byte or two-byte mapping */ |
| 2867 | if (entp->sub == 0) |
| 2868 | { |
| 2869 | /* it's a one-byte character - get the mapping */ |
| 2870 | wc = entp->ch; |
| 2871 | |
| 2872 | /* skip the single byte of input */ |
| 2873 | ++input_ptr; |
| 2874 | --input_len; |
| 2875 | } |
| 2876 | else |
| 2877 | { |
| 2878 | /* |
| 2879 | * it's a two-byte character lead byte - make sure we have a |
| 2880 | * complete input character |
| 2881 | */ |
| 2882 | if (input_len < 2) |
| 2883 | { |
| 2884 | /* we have an incomplete last character - tell the caller */ |
| 2885 | *partial_len = 1; |
| 2886 | |
| 2887 | /* we're done mapping it */ |
| 2888 | break; |
| 2889 | } |
| 2890 | |
| 2891 | /* get the second byte of the sequence */ |
| 2892 | c = input_ptr[1]; |
| 2893 | |
| 2894 | /* get the translation from the sub-table */ |
| 2895 | wc = entp->sub[c]; |
| 2896 | |
| 2897 | /* skip the two-byte sequence */ |
| 2898 | input_ptr += 2; |
| 2899 | input_len -= 2; |
| 2900 | } |
| 2901 | |
| 2902 | /* we have the translation - note its stored UTF-8 byte size */ |
| 2903 | wlen = utf8_ptr::s_wchar_size(wc); |
| 2904 | |
| 2905 | /* check for room to store the output character */ |
| 2906 | if (wlen > *output_buf_len) |
| 2907 | { |
| 2908 | /* |
| 2909 | * there's no room to store this character - zero out the |
| 2910 | * output buffer length so that we know not to try storing |
| 2911 | * anything else in the buffer |
| 2912 | */ |
| 2913 | *output_buf_len = 0; |
| 2914 | } |
| 2915 | else |
| 2916 | { |
| 2917 | /* there's room - store it */ |
| 2918 | wlen = utf8_ptr::s_putch(*output_ptr, wc); |
| 2919 | |
| 2920 | /* consume output buffer space */ |
| 2921 | *output_ptr += wlen; |
| 2922 | *output_buf_len -= wlen; |
| 2923 | } |
| 2924 | |
| 2925 | /* count the needed length, whether we stored it or not */ |
| 2926 | needed_out_len += wlen; |
| 2927 | } |
| 2928 | |
| 2929 | /* return the required output length */ |
| 2930 | return needed_out_len; |
| 2931 | } |
| 2932 | |
| 2933 | /* |
| 2934 | * read from a multi-byte input file, translating to UTF-8 |
| 2935 | */ |
| 2936 | size_t CCharmapToUniMB::read_file(CVmDataSource *fp, char *buf, size_t bufl) |
| 2937 | { |
| 2938 | size_t inlen; |
| 2939 | size_t outlen; |
| 2940 | size_t partial; |
| 2941 | |
| 2942 | /* |
| 2943 | * Compute how much to read from the file. The input file is composed |
| 2944 | * of one-byte or two-byte characters, so only read up to one-third of |
| 2945 | * the caller's buffer length; this will ensure that in the worst case |
| 2946 | * we can always fit what we read into the caller's buffer. (The worst |
| 2947 | * case is that the input is entirely single-byte local characters that |
| 2948 | * translate into three-byte UTF-8 characters.) |
| 2949 | */ |
| 2950 | inlen = bufl / 3; |
| 2951 | |
| 2952 | /* in any case, we can't read more than our own buffer size */ |
| 2953 | if (inlen >= sizeof(inbuf_)) |
| 2954 | inlen = sizeof(inbuf_); |
| 2955 | |
| 2956 | /* read raw bytes from the file */ |
| 2957 | inlen = fp->readc(inbuf_, inlen); |
| 2958 | |
| 2959 | /* |
| 2960 | * Map data to the caller's buffer. Note if we have a partial |
| 2961 | * character at the end of the buffer (i.e., the last byte of the |
| 2962 | * buffer is a lead byte that requires a second byte to make up a |
| 2963 | * complete two-byte local character), so that we can read an |
| 2964 | * additional byte to complete the two-byte final character if |
| 2965 | * necessary. |
| 2966 | */ |
| 2967 | outlen = map2(&buf, &bufl, inbuf_, inlen, &partial); |
| 2968 | |
| 2969 | /* |
| 2970 | * if we have a partial trailing character, read the other half of the |
| 2971 | * final character |
| 2972 | */ |
| 2973 | if (partial != 0) |
| 2974 | { |
| 2975 | /* move the lead byte to the start of our buffer */ |
| 2976 | inbuf_[0] = inbuf_[inlen - 1]; |
| 2977 | |
| 2978 | /* read the extra byte to form a complete character */ |
| 2979 | inlen = 1 + fp->readc(inbuf_ + 1, 1); |
| 2980 | |
| 2981 | /* if we got the second byte, map the complete final character */ |
| 2982 | if (inlen == 2) |
| 2983 | outlen += map(&buf, &bufl, inbuf_, inlen); |
| 2984 | } |
| 2985 | |
| 2986 | /* return the result length */ |
| 2987 | return outlen; |
| 2988 | } |
| 2989 |