1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
|
/*
* "$Id: transcode.c 6115 2006-11-15 22:21:18Z mike $"
*
* Transcoding support for the Common UNIX Printing System (CUPS).
*
* Copyright 1997-2006 by Easy Software Products.
*
* These coded instructions, statements, and computer programs are
* the property of Easy Software Products and are protected by Federal
* copyright law. Distribution and use rights are outlined in the
* file "LICENSE.txt" which should have been included with this file.
* If this file is missing or damaged please contact Easy Software
* Products at:
*
* Attn: CUPS Licensing Information
* Easy Software Products
* 44141 Airport View Drive, Suite 204
* Hollywood, Maryland 20636 USA
*
* Voice: (301) 373-9600
* EMail: cups-info@cups.org
* WWW: http://www.cups.org
*
* Contents:
*
* _cupsCharmapFlush() - Flush all character set maps out of cache.
* _cupsCharmapFree() - Free a character set map.
* _cupsCharmapGet() - Get a character set map.
* cupsCharsetToUTF8() - Convert legacy character set to UTF-8.
* cupsUTF8ToCharset() - Convert UTF-8 to legacy character set.
* cupsUTF8ToUTF32() - Convert UTF-8 to UTF-32.
* cupsUTF32ToUTF8() - Convert UTF-32 to UTF-8.
* compare_wide() - Compare key for wide (VBCS) match.
* conv_sbcs_to_utf8() - Convert legacy SBCS to UTF-8.
* conv_utf8_to_sbcs() - Convert UTF-8 to legacy SBCS.
* conv_utf8_to_vbcs() - Convert UTF-8 to legacy DBCS/VBCS.
* conv_vbcs_to_utf8() - Convert legacy DBCS/VBCS to UTF-8.
* free_sbcs_charmap() - Free memory used by a single byte character set.
* free_vbcs_charmap() - Free memory used by a variable byte character set.
* get_charmap() - Lookup or get a character set map (private).
* get_charmap_count() - Count lines in a charmap file.
* get_sbcs_charmap() - Get SBCS Charmap.
* get_vbcs_charmap() - Get DBCS/VBCS Charmap.
*/
/*
* Include necessary headers...
*/
#include "globals.h"
#include "debug.h"
#include <limits.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
/*
* Local globals...
*/
#ifdef HAVE_PTHREAD_H
static pthread_mutex_t map_mutex = PTHREAD_MUTEX_INITIALIZER;
/* Mutex to control access to maps */
#endif /* HAVE_PTHREAD_H */
static _cups_cmap_t *cmap_cache = NULL;
/* SBCS Charmap Cache */
static _cups_vmap_t *vmap_cache = NULL;
/* VBCS Charmap Cache */
/*
* Local functions...
*/
static int compare_wide(const void *k1, const void *k2);
static int conv_sbcs_to_utf8(cups_utf8_t *dest,
const cups_sbcs_t *src,
int maxout,
const cups_encoding_t encoding);
static int conv_utf8_to_sbcs(cups_sbcs_t *dest,
const cups_utf8_t *src,
int maxout,
const cups_encoding_t encoding);
static int conv_utf8_to_vbcs(cups_sbcs_t *dest,
const cups_utf8_t *src,
int maxout,
const cups_encoding_t encoding);
static int conv_vbcs_to_utf8(cups_utf8_t *dest,
const cups_sbcs_t *src,
int maxout,
const cups_encoding_t encoding);
static void free_sbcs_charmap(_cups_cmap_t *sbcs);
static void free_vbcs_charmap(_cups_vmap_t *vbcs);
static void *get_charmap(const cups_encoding_t encoding);
static int get_charmap_count(cups_file_t *fp);
static _cups_cmap_t *get_sbcs_charmap(const cups_encoding_t encoding,
const char *filename);
static _cups_vmap_t *get_vbcs_charmap(const cups_encoding_t encoding,
const char *filename);
/*
* '_cupsCharmapFlush()' - Flush all character set maps out of cache.
*/
void
_cupsCharmapFlush(void)
{
_cups_cmap_t *cmap, /* Legacy SBCS / Unicode Charset Map */
*cnext; /* Next Legacy SBCS Charset Map */
_cups_vmap_t *vmap, /* Legacy VBCS / Unicode Charset Map */
*vnext; /* Next Legacy VBCS Charset Map */
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&map_mutex);
#endif /* HAVE_PTHREAD_H */
/*
* Loop through SBCS charset map cache, free all memory...
*/
for (cmap = cmap_cache; cmap; cmap = cnext)
{
cnext = cmap->next;
free_sbcs_charmap(cmap);
}
cmap_cache = NULL;
/*
* Loop through DBCS/VBCS charset map cache, free all memory...
*/
for (vmap = vmap_cache; vmap; vmap = vnext)
{
vnext = vmap->next;
free_vbcs_charmap(vmap);
free(vmap);
}
vmap_cache = NULL;
#ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&map_mutex);
#endif /* HAVE_PTHREAD_H */
}
/*
* '_cupsCharmapFree()' - Free a character set map.
*
* This does not actually free; use '_cupsCharmapFlush()' for that.
*/
void
_cupsCharmapFree(
const cups_encoding_t encoding) /* I - Encoding */
{
_cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */
_cups_vmap_t *vmap; /* Legacy VBCS / Unicode Charset Map */
/*
* See if we already have this SBCS charset map loaded...
*/
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&map_mutex);
#endif /* HAVE_PTHREAD_H */
for (cmap = cmap_cache; cmap; cmap = cmap->next)
{
if (cmap->encoding == encoding)
{
if (cmap->used > 0)
cmap->used --;
break;
}
}
/*
* See if we already have this DBCS/VBCS charset map loaded...
*/
for (vmap = vmap_cache; vmap; vmap = vmap->next)
{
if (vmap->encoding == encoding)
{
if (vmap->used > 0)
vmap->used --;
break;
}
}
#ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&map_mutex);
#endif /* HAVE_PTHREAD_H */
}
/*
* '_cupsCharmapGet()' - Get a character set map.
*
* This code handles single-byte (SBCS), double-byte (DBCS), and
* variable-byte (VBCS) character sets _without_ charset escapes...
* This code does not handle multiple-byte character sets (MBCS)
* (such as ISO-2022-JP) with charset switching via escapes...
*/
void * /* O - Charset map pointer */
_cupsCharmapGet(
const cups_encoding_t encoding) /* I - Encoding */
{
void *charmap; /* Charset map pointer */
DEBUG_printf(("_cupsCharmapGet(encoding=%d)\n", encoding));
/*
* Check for valid arguments...
*/
if (encoding < 0 || encoding >= CUPS_ENCODING_VBCS_END)
{
DEBUG_puts(" Bad encoding, returning NULL!");
return (NULL);
}
/*
* Lookup or get the charset map pointer and return...
*/
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&map_mutex);
#endif /* HAVE_PTHREAD_H */
charmap = get_charmap(encoding);
#ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&map_mutex);
#endif /* HAVE_PTHREAD_H */
return (charmap);
}
/*
* 'cupsCharsetToUTF8()' - Convert legacy character set to UTF-8.
*
* This code handles single-byte (SBCS), double-byte (DBCS), and
* variable-byte (VBCS) character sets _without_ charset escapes...
* This code does not handle multiple-byte character sets (MBCS)
* (such as ISO-2022-JP) with charset switching via escapes...
*/
int /* O - Count or -1 on error */
cupsCharsetToUTF8(
cups_utf8_t *dest, /* O - Target string */
const char *src, /* I - Source string */
const int maxout, /* I - Max output */
const cups_encoding_t encoding) /* I - Encoding */
{
int bytes; /* Number of bytes converted */
/*
* Check for valid arguments...
*/
DEBUG_printf(("cupsCharsetToUTF8(dest=%p, src=\"%s\", maxout=%d, encoding=%d)\n",
dest, src, maxout, encoding));
if (dest)
*dest = '\0';
if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
{
DEBUG_puts(" Bad arguments, returning -1");
return (-1);
}
/*
* Handle identity conversions...
*/
if (encoding == CUPS_UTF8 ||
encoding < 0 || encoding >= CUPS_ENCODING_VBCS_END)
{
strlcpy((char *)dest, src, maxout);
return (strlen((char *)dest));
}
/*
* Handle ISO-8859-1 to UTF-8 directly...
*/
if (encoding == CUPS_ISO8859_1)
{
int ch; /* Character from string */
cups_utf8_t *destptr, /* Pointer into UTF-8 buffer */
*destend; /* End of UTF-8 buffer */
destptr = dest;
destend = dest + maxout - 2;
while (*src && destptr < destend)
{
ch = *src++ & 255;
if (ch & 128)
{
*destptr++ = 0xc0 | (ch >> 6);
*destptr++ = 0x80 | (ch & 0x3f);
}
else
*destptr++ = ch;
}
*destptr = '\0';
return (destptr - dest);
}
/*
* Convert input legacy charset to UTF-8...
*/
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&map_mutex);
#endif /* HAVE_PTHREAD_H */
if (encoding < CUPS_ENCODING_SBCS_END)
bytes = conv_sbcs_to_utf8(dest, (cups_sbcs_t *)src, maxout, encoding);
else if (encoding < CUPS_ENCODING_VBCS_END)
bytes = conv_vbcs_to_utf8(dest, (cups_sbcs_t *)src, maxout, encoding);
else
{
DEBUG_puts(" Bad encoding, returning -1");
bytes = -1;
}
#ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&map_mutex);
#endif /* HAVE_PTHREAD_H */
return (bytes);
}
/*
* 'cupsUTF8ToCharset()' - Convert UTF-8 to legacy character set.
*
* This code handles single-byte (SBCS), double-byte (DBCS), and
* variable-byte (VBCS) character sets _without_ charset escapes...
* This code does not handle multiple-byte character sets (MBCS)
* (such as ISO-2022-JP) with charset switching via escapes...
*/
int /* O - Count or -1 on error */
cupsUTF8ToCharset(
char *dest, /* O - Target string */
const cups_utf8_t *src, /* I - Source string */
const int maxout, /* I - Max output */
const cups_encoding_t encoding) /* I - Encoding */
{
int bytes; /* Number of bytes converted */
/*
* Check for valid arguments...
*/
if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
{
if (dest)
*dest = '\0';
return (-1);
}
/*
* Handle identity conversions...
*/
if (encoding == CUPS_UTF8 ||
encoding < 0 || encoding >= CUPS_ENCODING_VBCS_END)
{
strlcpy(dest, (char *)src, maxout);
return (strlen(dest));
}
/*
* Handle UTF-8 to ISO-8859-1 directly...
*/
if (encoding == CUPS_ISO8859_1)
{
int ch; /* Character from string */
char *destptr, /* Pointer into ISO-8859-1 buffer */
*destend; /* End of ISO-8859-1 buffer */
destptr = dest;
destend = dest + maxout - 1;
while (*src && destptr < destend)
{
ch = *src++;
if ((ch & 0xe0) == 0xc0)
{
ch = ((ch & 0x1f) << 6) | (*src++ & 0x3f);
if (ch < 256)
*destptr++ = ch;
else
*destptr++ = '?';
}
else if ((ch & 0xf0) == 0xe0 ||
(ch & 0xf8) == 0xf0)
*destptr++ = '?';
else if (!(ch & 0x80))
*destptr++ = ch;
}
*destptr = '\0';
return (destptr - dest);
}
/*
* Convert input UTF-8 to legacy charset...
*/
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&map_mutex);
#endif /* HAVE_PTHREAD_H */
if (encoding < CUPS_ENCODING_SBCS_END)
bytes = conv_utf8_to_sbcs((cups_sbcs_t *)dest, src, maxout, encoding);
else if (encoding < CUPS_ENCODING_VBCS_END)
bytes = conv_utf8_to_vbcs((cups_sbcs_t *)dest, src, maxout, encoding);
else
bytes = -1;
#ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&map_mutex);
#endif /* HAVE_PTHREAD_H */
return (bytes);
}
/*
* 'cupsUTF8ToUTF32()' - Convert UTF-8 to UTF-32.
*
* 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
*
* UTF-32 char UTF-8 char(s)
* --------------------------------------------------
* 0 to 127 = 0xxxxxxx (US-ASCII)
* 128 to 2047 = 110xxxxx 10yyyyyy
* 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
* > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
*
* UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
* which would convert to five- or six-octet UTF-8 sequences...
*/
int /* O - Count or -1 on error */
cupsUTF8ToUTF32(
cups_utf32_t *dest, /* O - Target string */
const cups_utf8_t *src, /* I - Source string */
const int maxout) /* I - Max output */
{
int i; /* Looping variable */
cups_utf8_t ch; /* Character value */
cups_utf8_t next; /* Next character value */
cups_utf32_t ch32; /* UTF-32 character value */
/*
* Check for valid arguments and clear output...
*/
if (dest)
*dest = 0;
if (!dest || !src || maxout < 1 || maxout > CUPS_MAX_USTRING)
return (-1);
/*
* Convert input UTF-8 to output UTF-32 (and insert BOM)...
*/
*dest++ = 0xfeff;
for (i = maxout - 1; *src && i > 0; i --)
{
ch = *src++;
/*
* Convert UTF-8 character(s) to UTF-32 character...
*/
if (!(ch & 0x80))
{
/*
* One-octet UTF-8 <= 127 (US-ASCII)...
*/
*dest++ = ch;
continue;
}
else if ((ch & 0xe0) == 0xc0)
{
/*
* Two-octet UTF-8 <= 2047 (Latin-x)...
*/
next = *src++;
if (!next)
return (-1);
ch32 = ((ch & 0x1f) << 6) | (next & 0x3f);
/*
* Check for non-shortest form (invalid UTF-8)...
*/
if (ch32 < 0x80)
return (-1);
*dest++ = ch32;
}
else if ((ch & 0xf0) == 0xe0)
{
/*
* Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
*/
next = *src++;
if (!next)
return (-1);
ch32 = ((ch & 0x0f) << 6) | (next & 0x3f);
next = *src++;
if (!next)
return (-1);
ch32 = (ch32 << 6) | (next & 0x3f);
/*
* Check for non-shortest form (invalid UTF-8)...
*/
if (ch32 < 0x800)
return (-1);
*dest++ = ch32;
}
else if ((ch & 0xf8) == 0xf0)
{
/*
* Four-octet UTF-8...
*/
next = *src++;
if (!next)
return (-1);
ch32 = ((ch & 0x07) << 6) | (next & 0x3f);
next = *src++;
if (!next)
return (-1);
ch32 = (ch32 << 6) | (next & 0x3f);
next = *src++;
if (!next)
return (-1);
ch32 = (ch32 << 6) | (next & 0x3f);
/*
* Check for non-shortest form (invalid UTF-8)...
*/
if (ch32 < 0x10000)
return (-1);
*dest++ = ch32;
}
else
{
/*
* More than 4-octet (invalid UTF-8 sequence)...
*/
return (-1);
}
/*
* Check for UTF-16 surrogate (illegal UTF-8)...
*/
if (ch32 >= 0xd800 && ch32 <= 0xdfff)
return (-1);
}
*dest = 0;
return (i);
}
/*
* 'cupsUTF32ToUTF8()' - Convert UTF-32 to UTF-8.
*
* 32-bit UTF-32 (actually 21-bit) maps to UTF-8 as follows...
*
* UTF-32 char UTF-8 char(s)
* --------------------------------------------------
* 0 to 127 = 0xxxxxxx (US-ASCII)
* 128 to 2047 = 110xxxxx 10yyyyyy
* 2048 to 65535 = 1110xxxx 10yyyyyy 10zzzzzz
* > 65535 = 11110xxx 10yyyyyy 10zzzzzz 10xxxxxx
*
* UTF-32 prohibits chars beyond Plane 16 (> 0x10ffff) in UCS-4,
* which would convert to five- or six-octet UTF-8 sequences...
*/
int /* O - Count or -1 on error */
cupsUTF32ToUTF8(
cups_utf8_t *dest, /* O - Target string */
const cups_utf32_t *src, /* I - Source string */
const int maxout) /* I - Max output */
{
cups_utf8_t *start; /* Start of destination string */
int i; /* Looping variable */
int swap; /* Byte-swap input to output */
cups_utf32_t ch; /* Character value */
/*
* Check for valid arguments and clear output...
*/
if (dest)
*dest = '\0';
if (!dest || !src || maxout < 1)
return (-1);
/*
* Check for leading BOM in UTF-32 and inverted BOM...
*/
start = dest;
swap = *src == 0xfffe0000;
if (*src == 0xfffe0000 || *src == 0xfeff)
src ++;
/*
* Convert input UTF-32 to output UTF-8...
*/
for (i = maxout - 1; *src && i > 0;)
{
ch = *src++;
/*
* Byte swap input UTF-32, if necessary...
* (only byte-swapping 24 of 32 bits)
*/
if (swap)
ch = ((ch >> 24) | ((ch >> 8) & 0xff00) | ((ch << 8) & 0xff0000));
/*
* Check for beyond Plane 16 (invalid UTF-32)...
*/
if (ch > 0x10ffff)
return (-1);
/*
* Convert UTF-32 character to UTF-8 character(s)...
*/
if (ch < 0x80)
{
/*
* One-octet UTF-8 <= 127 (US-ASCII)...
*/
*dest++ = (cups_utf8_t)ch;
i --;
}
else if (ch < 0x800)
{
/*
* Two-octet UTF-8 <= 2047 (Latin-x)...
*/
if (i < 2)
return (-1);
*dest++ = (cups_utf8_t)(0xc0 | ((ch >> 6) & 0x1f));
*dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
i -= 2;
}
else if (ch < 0x10000)
{
/*
* Three-octet UTF-8 <= 65535 (Plane 0 - BMP)...
*/
if (i < 3)
return (-1);
*dest++ = (cups_utf8_t)(0xe0 | ((ch >> 12) & 0x0f));
*dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f));
*dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
i -= 3;
}
else
{
/*
* Four-octet UTF-8...
*/
if (i < 4)
return (-1);
*dest++ = (cups_utf8_t)(0xf0 | ((ch >> 18) & 0x07));
*dest++ = (cups_utf8_t)(0x80 | ((ch >> 12) & 0x3f));
*dest++ = (cups_utf8_t)(0x80 | ((ch >> 6) & 0x3f));
*dest++ = (cups_utf8_t)(0x80 | (ch & 0x3f));
i -= 4;
}
}
*dest = '\0';
return ((int)(dest - start));
}
/*
* 'compare_wide()' - Compare key for wide (VBCS) match.
*/
static int
compare_wide(const void *k1, /* I - Key char */
const void *k2) /* I - Map char */
{
cups_vbcs_t key; /* Legacy key character */
cups_vbcs_t map; /* Legacy map character */
key = *((cups_vbcs_t *)k1);
map = ((_cups_wide2uni_t *)k2)->widechar;
return ((int)(key - map));
}
/*
* 'conv_sbcs_to_utf8()' - Convert legacy SBCS to UTF-8.
*/
static int /* O - Count or -1 on error */
conv_sbcs_to_utf8(
cups_utf8_t *dest, /* O - Target string */
const cups_sbcs_t *src, /* I - Source string */
int maxout, /* I - Max output */
const cups_encoding_t encoding) /* I - Encoding */
{
_cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */
cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */
cups_sbcs_t legchar; /* Legacy character value */
cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */
*workptr; /* Pointer into string */
/*
* Find legacy charset map in cache...
*/
if ((cmap = (_cups_cmap_t *)get_charmap(encoding)) == NULL)
return (-1);
/*
* Convert input legacy charset to internal UCS-4 (and insert BOM)...
*/
work[0] = 0xfeff;
for (workptr = work + 1; *src && workptr < (work + CUPS_MAX_USTRING - 1);)
{
legchar = *src++;
/*
* Convert ASCII verbatim (optimization)...
*/
if (legchar < 0x80)
*workptr++ = (cups_utf32_t)legchar;
else
{
/*
* Convert unknown character to Replacement Character...
*/
crow = cmap->char2uni + legchar;
if (!*crow)
*workptr++ = 0xfffd;
else
*workptr++ = (cups_utf32_t)*crow;
}
}
*workptr = 0;
/*
* Convert internal UCS-4 to output UTF-8 (and delete BOM)...
*/
cmap->used --;
return (cupsUTF32ToUTF8(dest, work, maxout));
}
/*
* 'conv_utf8_to_sbcs()' - Convert UTF-8 to legacy SBCS.
*/
static int /* O - Count or -1 on error */
conv_utf8_to_sbcs(
cups_sbcs_t *dest, /* O - Target string */
const cups_utf8_t *src, /* I - Source string */
int maxout, /* I - Max output */
const cups_encoding_t encoding) /* I - Encoding */
{
cups_sbcs_t *start; /* Start of destination string */
_cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */
cups_sbcs_t *srow; /* Pointer to SBCS row in 'uni2char' */
cups_utf32_t unichar; /* Character value */
cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */
*workptr; /* Pointer into string */
/*
* Find legacy charset map in cache...
*/
if ((cmap = (_cups_cmap_t *)get_charmap(encoding)) == NULL)
return (-1);
/*
* Convert input UTF-8 to internal UCS-4 (and insert BOM)...
*/
if (cupsUTF8ToUTF32(work, src, CUPS_MAX_USTRING) < 0)
return (-1);
/*
* Convert internal UCS-4 to SBCS legacy charset (and delete BOM)...
*/
for (workptr = work + 1, start = dest; *workptr && maxout > 1; maxout --)
{
unichar = *workptr++;
if (!unichar)
break;
/*
* Convert ASCII verbatim (optimization)...
*/
if (unichar < 0x80)
{
*dest++ = (cups_sbcs_t)unichar;
continue;
}
/*
* Convert unknown character to visible replacement...
*/
srow = cmap->uni2char[(int)((unichar >> 8) & 0xff)];
if (srow)
srow += (int)(unichar & 0xff);
if (!srow || !*srow)
*dest++ = '?';
else
*dest++ = *srow;
}
*dest = '\0';
cmap->used --;
return ((int)(dest - start));
}
/*
* 'conv_utf8_to_vbcs()' - Convert UTF-8 to legacy DBCS/VBCS.
*/
static int /* O - Count or -1 on error */
conv_utf8_to_vbcs(
cups_sbcs_t *dest, /* O - Target string */
const cups_utf8_t *src, /* I - Source string */
int maxout, /* I - Max output */
const cups_encoding_t encoding) /* I - Encoding */
{
cups_sbcs_t *start; /* Start of destination string */
_cups_vmap_t *vmap; /* Legacy DBCS / Unicode Charset Map */
cups_vbcs_t *vrow; /* Pointer to VBCS row in 'uni2char' */
cups_utf32_t unichar; /* Character value */
cups_vbcs_t legchar; /* Legacy character value */
cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */
*workptr; /* Pointer into string */
/*
* Find legacy charset map in cache...
*/
if ((vmap = (_cups_vmap_t *)get_charmap(encoding)) == NULL)
return (-1);
/*
* Convert input UTF-8 to internal UCS-4 (and insert BOM)...
*/
if (cupsUTF8ToUTF32(work, src, CUPS_MAX_USTRING) < 0)
return (-1);
/*
* Convert internal UCS-4 to VBCS legacy charset (and delete BOM)...
*/
for (start = dest, workptr = work + 1; *workptr && maxout > 1; maxout --)
{
unichar = *workptr++;
if (!unichar)
break;
/*
* Convert ASCII verbatim (optimization)...
*/
if (unichar < 0x80)
{
*dest++ = (cups_vbcs_t)unichar;
continue;
}
/*
* Convert unknown character to visible replacement...
*/
vrow = vmap->uni2char[(int)((unichar >> 8) & 0xff)];
if (vrow)
vrow += (int)(unichar & 0xff);
if (!vrow || !*vrow)
legchar = (cups_vbcs_t)'?';
else
legchar = (cups_vbcs_t)*vrow;
/*
* Save n-byte legacy character...
*/
if (legchar > 0xffffff)
{
if (maxout < 5)
return (-1);
*dest++ = (cups_sbcs_t)(legchar >> 24);
*dest++ = (cups_sbcs_t)(legchar >> 16);
*dest++ = (cups_sbcs_t)(legchar >> 8);
*dest++ = (cups_sbcs_t)legchar;
maxout -= 3;
}
else if (legchar > 0xffff)
{
if (maxout < 4)
return (-1);
*dest++ = (cups_sbcs_t)(legchar >> 16);
*dest++ = (cups_sbcs_t)(legchar >> 8);
*dest++ = (cups_sbcs_t)legchar;
maxout -= 2;
}
else if (legchar > 0xff)
{
*dest++ = (cups_sbcs_t)(legchar >> 8);
*dest++ = (cups_sbcs_t)legchar;
maxout --;
}
}
*dest = '\0';
vmap->used --;
return ((int)(dest - start));
}
/*
* 'conv_vbcs_to_utf8()' - Convert legacy DBCS/VBCS to UTF-8.
*/
static int /* O - Count or -1 on error */
conv_vbcs_to_utf8(
cups_utf8_t *dest, /* O - Target string */
const cups_sbcs_t *src, /* I - Source string */
int maxout, /* I - Max output */
const cups_encoding_t encoding) /* I - Encoding */
{
_cups_vmap_t *vmap; /* Legacy VBCS / Unicode Charset Map */
cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */
_cups_wide2uni_t *wide2uni; /* Pointer to row in 'wide2uni' */
cups_sbcs_t leadchar; /* Lead char of n-byte legacy char */
cups_vbcs_t legchar; /* Legacy character value */
cups_utf32_t work[CUPS_MAX_USTRING], /* Internal UCS-4 string */
*workptr; /* Pointer into string */
/*
* Find legacy charset map in cache...
*/
if ((vmap = (_cups_vmap_t *)get_charmap(encoding)) == NULL)
return (-1);
/*
* Convert input legacy charset to internal UCS-4 (and insert BOM)...
*/
work[0] = 0xfeff;
for (workptr = work + 1; *src && workptr < (work + CUPS_MAX_USTRING - 1);)
{
legchar = *src++;
leadchar = (cups_sbcs_t)legchar;
/*
* Convert ASCII verbatim (optimization)...
*/
if (legchar < 0x80)
{
*workptr++ = (cups_utf32_t)legchar;
continue;
}
/*
* Convert 2-byte legacy character...
*/
if (vmap->lead2char[(int)leadchar] == leadchar)
{
if (!*src)
return (-1);
legchar = (legchar << 8) | *src++;
/*
* Convert unknown character to Replacement Character...
*/
crow = vmap->char2uni[(int)((legchar >> 8) & 0xff)];
if (crow)
crow += (int) (legchar & 0xff);
if (!crow || !*crow)
*workptr++ = 0xfffd;
else
*workptr++ = (cups_utf32_t)*crow;
continue;
}
/*
* Fetch 3-byte or 4-byte legacy character...
*/
if (vmap->lead3char[(int)leadchar] == leadchar)
{
if (!*src || !src[1])
return (-1);
legchar = (legchar << 8) | *src++;
legchar = (legchar << 8) | *src++;
}
else if (vmap->lead4char[(int)leadchar] == leadchar)
{
if (!*src || !src[1] || !src[2])
return (-1);
legchar = (legchar << 8) | *src++;
legchar = (legchar << 8) | *src++;
legchar = (legchar << 8) | *src++;
}
else
return (-1);
/*
* Find 3-byte or 4-byte legacy character...
*/
wide2uni = (_cups_wide2uni_t *)bsearch(&legchar,
vmap->wide2uni,
vmap->widecount,
sizeof(_cups_wide2uni_t),
compare_wide);
/*
* Convert unknown character to Replacement Character...
*/
if (!wide2uni || !wide2uni->unichar)
*workptr++ = 0xfffd;
else
*workptr++ = wide2uni->unichar;
}
*workptr = 0;
vmap->used --;
/*
* Convert internal UCS-4 to output UTF-8 (and delete BOM)...
*/
return (cupsUTF32ToUTF8(dest, work, maxout));
}
/*
* 'free_sbcs_charmap()' - Free memory used by a single byte character set.
*/
static void
free_sbcs_charmap(_cups_cmap_t *cmap) /* I - Character set */
{
int i; /* Looping variable */
for (i = 0; i < 256; i ++)
if (cmap->uni2char[i])
free(cmap->uni2char[i]);
free(cmap);
}
/*
* 'free_vbcs_charmap()' - Free memory used by a variable byte character set.
*/
static void
free_vbcs_charmap(_cups_vmap_t *vmap) /* I - Character set */
{
int i; /* Looping variable */
for (i = 0; i < 256; i ++)
if (vmap->char2uni[i])
free(vmap->char2uni[i]);
for (i = 0; i < 256; i ++)
if (vmap->uni2char[i])
free(vmap->uni2char[i]);
if (vmap->wide2uni)
free(vmap->wide2uni);
free(vmap);
}
/*
* 'get_charmap()' - Lookup or get a character set map (private).
*
* This code handles single-byte (SBCS), double-byte (DBCS), and
* variable-byte (VBCS) character sets _without_ charset escapes...
* This code does not handle multiple-byte character sets (MBCS)
* (such as ISO-2022-JP) with charset switching via escapes...
*/
static void * /* O - Charset map pointer */
get_charmap(
const cups_encoding_t encoding) /* I - Encoding */
{
char filename[1024]; /* Filename for charset map file */
_cups_globals_t *cg = _cupsGlobals(); /* Global data */
/*
* Get the data directory and charset map name...
*/
snprintf(filename, sizeof(filename), "%s/charmaps/%s.txt",
cg->cups_datadir, _cupsEncodingName(encoding));
DEBUG_printf((" filename=\"%s\"\n", filename));
/*
* Read charset map input file into cache...
*/
if (encoding < CUPS_ENCODING_SBCS_END)
return (get_sbcs_charmap(encoding, filename));
else if (encoding < CUPS_ENCODING_VBCS_END)
return (get_vbcs_charmap(encoding, filename));
else
return (NULL);
}
/*
* 'get_charmap_count()' - Count lines in a charmap file.
*/
static int /* O - Count or -1 on error */
get_charmap_count(cups_file_t *fp) /* I - File to read from */
{
int count; /* Number of lines */
char line[256]; /* Line from input map file */
/*
* Count lines in map input file...
*/
count = 0;
while (cupsFileGets(fp, line, sizeof(line)))
if (line[0] == '0')
count ++;
/*
* Return the number of lines...
*/
if (count > 0)
return (count);
else
return (-1);
}
/*
* 'get_sbcs_charmap()' - Get SBCS Charmap.
*/
static _cups_cmap_t * /* O - Charmap or 0 on error */
get_sbcs_charmap(
const cups_encoding_t encoding, /* I - Charmap Encoding */
const char *filename) /* I - Charmap Filename */
{
unsigned long legchar; /* Legacy character value */
cups_utf32_t unichar; /* Unicode character value */
_cups_cmap_t *cmap; /* Legacy SBCS / Unicode Charset Map */
cups_file_t *fp; /* Charset map file pointer */
char *s; /* Line parsing pointer */
cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */
cups_sbcs_t *srow; /* Pointer to SBCS row in 'uni2char' */
char line[256]; /* Line from charset map file */
/*
* See if we already have this SBCS charset map loaded...
*/
for (cmap = cmap_cache; cmap; cmap = cmap->next)
{
if (cmap->encoding == encoding)
{
cmap->used ++;
DEBUG_printf((" returning existing cmap=%p\n", cmap));
return ((void *)cmap);
}
}
/*
* Open SBCS charset map input file...
*/
if ((fp = cupsFileOpen(filename, "r")) == NULL)
return (NULL);
/*
* Allocate memory for SBCS charset map...
*/
if ((cmap = (_cups_cmap_t *)calloc(1, sizeof(_cups_cmap_t))) == NULL)
{
cupsFileClose(fp);
DEBUG_puts(" Unable to allocate memory!");
return (NULL);
}
cmap->used ++;
cmap->encoding = encoding;
/*
* Save SBCS charset map into memory for transcoding...
*/
while (cupsFileGets(fp, line, sizeof(line)))
{
if (line[0] != '0')
continue;
legchar = strtol(line, &s, 16);
if (legchar < 0 || legchar > 0xff)
goto sbcs_error;
unichar = strtol(s, NULL, 16);
if (unichar < 0 || unichar > 0xffff)
goto sbcs_error;
/*
* Save legacy to Unicode mapping in direct lookup table...
*/
crow = cmap->char2uni + legchar;
*crow = (cups_ucs2_t)(unichar & 0xffff);
/*
* Save Unicode to legacy mapping in indirect lookup table...
*/
srow = cmap->uni2char[(unichar >> 8) & 0xff];
if (!srow)
{
srow = (cups_sbcs_t *)calloc(256, sizeof(cups_sbcs_t));
if (!srow)
goto sbcs_error;
cmap->uni2char[(unichar >> 8) & 0xff] = srow;
}
srow += unichar & 0xff;
/*
* Convert Replacement Character to visible replacement...
*/
if (unichar == 0xfffd)
legchar = (unsigned long)'?';
/*
* First (oldest) legacy character uses Unicode mapping cell...
*/
if (!*srow)
*srow = (cups_sbcs_t)legchar;
}
cupsFileClose(fp);
/*
* Add it to the cache and return...
*/
cmap->next = cmap_cache;
cmap_cache = cmap;
DEBUG_printf((" returning new cmap=%p\n", cmap));
return (cmap);
/*
* If we get here, there was an error in the cmap file...
*/
sbcs_error:
free_sbcs_charmap(cmap);
cupsFileClose(fp);
DEBUG_puts(" Error, returning NULL!");
return (NULL);
}
/*
* 'get_vbcs_charmap()' - Get DBCS/VBCS Charmap.
*/
static _cups_vmap_t * /* O - Charmap or 0 on error */
get_vbcs_charmap(
const cups_encoding_t encoding, /* I - Charmap Encoding */
const char *filename) /* I - Charmap Filename */
{
_cups_vmap_t *vmap; /* Legacy VBCS / Unicode Charset Map */
cups_ucs2_t *crow; /* Pointer to UCS-2 row in 'char2uni' */
cups_vbcs_t *vrow; /* Pointer to VBCS row in 'uni2char' */
_cups_wide2uni_t *wide2uni; /* Pointer to row in 'wide2uni' */
cups_sbcs_t leadchar; /* Lead char of 2-byte legacy char */
unsigned long legchar; /* Legacy character value */
cups_utf32_t unichar; /* Unicode character value */
int mapcount; /* Count of lines in charmap file */
cups_file_t *fp; /* Charset map file pointer */
char *s; /* Line parsing pointer */
char line[256]; /* Line from charset map file */
int i; /* Loop variable */
int wide; /* 32-bit legacy char */
DEBUG_printf(("get_vbcs_charmap(encoding=%d, filename=\"%s\")\n",
encoding, filename));
/*
* See if we already have this DBCS/VBCS charset map loaded...
*/
for (vmap = vmap_cache; vmap; vmap = vmap->next)
{
if (vmap->encoding == encoding)
{
vmap->used ++;
DEBUG_printf((" returning existing vmap=%p\n", vmap));
return ((void *)vmap);
}
}
/*
* Open VBCS charset map input file...
*/
if ((fp = cupsFileOpen(filename, "r")) == NULL)
{
DEBUG_printf((" Unable to open file: %s\n", strerror(errno)));
return (NULL);
}
/*
* Count lines in charmap file...
*/
if ((mapcount = get_charmap_count(fp)) <= 0)
{
DEBUG_puts(" Unable to get charmap count!");
return (NULL);
}
DEBUG_printf((" mapcount=%d\n", mapcount));
/*
* Allocate memory for DBCS/VBCS charset map...
*/
if ((vmap = (_cups_vmap_t *)calloc(1, sizeof(_cups_vmap_t))) == NULL)
{
cupsFileClose(fp);
DEBUG_puts(" Unable to allocate memory!");
return (NULL);
}
vmap->used ++;
vmap->encoding = encoding;
/*
* Save DBCS/VBCS charset map into memory for transcoding...
*/
leadchar = 0;
wide2uni = NULL;
cupsFileRewind(fp);
i = 0;
wide = 0;
while (cupsFileGets(fp, line, sizeof(line)))
{
if (line[0] != '0')
continue;
legchar = strtoul(line, &s, 16);
if (legchar == ULONG_MAX)
goto vbcs_error;
unichar = strtol(s, NULL, 16);
if (unichar < 0 || unichar > 0xffff)
goto vbcs_error;
i ++;
/* DEBUG_printf((" i=%d, legchar=0x%08lx, unichar=0x%04x\n", i,
legchar, (unsigned)unichar)); */
/*
* Save lead char of 2/3/4-byte legacy char...
*/
if (legchar > 0xff && legchar <= 0xffff)
{
leadchar = (cups_sbcs_t)(legchar >> 8);
vmap->lead2char[leadchar] = leadchar;
}
if (legchar > 0xffff && legchar <= 0xffffff)
{
leadchar = (cups_sbcs_t)(legchar >> 16);
vmap->lead3char[leadchar] = leadchar;
}
if (legchar > 0xffffff)
{
leadchar = (cups_sbcs_t)(legchar >> 24);
vmap->lead4char[leadchar] = leadchar;
}
/*
* Save Legacy to Unicode mapping...
*/
if (legchar <= 0xffff)
{
/*
* Save DBCS 16-bit to Unicode mapping in indirect lookup table...
*/
crow = vmap->char2uni[(int)leadchar];
if (!crow)
{
crow = (cups_ucs2_t *)calloc(256, sizeof(cups_ucs2_t));
if (!crow)
goto vbcs_error;
vmap->char2uni[(int)leadchar] = crow;
}
crow[(int)(legchar & 0xff)] = (cups_ucs2_t)unichar;
}
else
{
/*
* Save VBCS 32-bit to Unicode mapping in sorted list table...
*/
if (!wide)
{
wide = 1;
vmap->widecount = (mapcount - i + 1);
wide2uni = (_cups_wide2uni_t *)calloc(vmap->widecount,
sizeof(_cups_wide2uni_t));
if (!wide2uni)
goto vbcs_error;
vmap->wide2uni = wide2uni;
}
wide2uni->widechar = (cups_vbcs_t)legchar;
wide2uni->unichar = (cups_ucs2_t)unichar;
wide2uni ++;
}
/*
* Save Unicode to legacy mapping in indirect lookup table...
*/
vrow = vmap->uni2char[(int)((unichar >> 8) & 0xff)];
if (!vrow)
{
vrow = (cups_vbcs_t *)calloc(256, sizeof(cups_vbcs_t));
if (!vrow)
goto vbcs_error;
vmap->uni2char[(int) ((unichar >> 8) & 0xff)] = vrow;
}
vrow += (int)(unichar & 0xff);
/*
* Convert Replacement Character to visible replacement...
*/
if (unichar == 0xfffd)
legchar = (unsigned long)'?';
/*
* First (oldest) legacy character uses Unicode mapping cell...
*/
if (!*vrow)
*vrow = (cups_vbcs_t)legchar;
}
vmap->charcount = (i - vmap->widecount);
cupsFileClose(fp);
/*
* Add it to the cache and return...
*/
vmap->next = vmap_cache;
vmap_cache = vmap;
DEBUG_printf((" returning new vmap=%p\n", vmap));
return (vmap);
/*
* If we get here, the file contains errors...
*/
vbcs_error:
free_vbcs_charmap(vmap);
cupsFileClose(fp);
DEBUG_puts(" Error, returning NULL!");
return (NULL);
}
/*
* End of "$Id: transcode.c 6115 2006-11-15 22:21:18Z mike $"
*/
|