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 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571
|
/*
** MimeUtility.m
**
** Copyright (c) 2001, 2002, 2003
**
** Author: Ludovic Marcotte <ludovic@Sophos.ca>
** Alexander Malmberg <alexander@malmberg.org>
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <Pantomime/MimeUtility.h>
#include <Pantomime/Constants.h>
#include <Pantomime/InternetAddress.h>
#include <Pantomime/ISO8859_1.h>
#include <Pantomime/ISO8859_2.h>
#include <Pantomime/ISO8859_3.h>
#include <Pantomime/ISO8859_4.h>
#include <Pantomime/ISO8859_5.h>
#include <Pantomime/ISO8859_6.h>
#include <Pantomime/ISO8859_7.h>
#include <Pantomime/ISO8859_8.h>
#include <Pantomime/ISO8859_9.h>
#include <Pantomime/ISO8859_10.h>
#include <Pantomime/ISO8859_11.h>
#include <Pantomime/ISO8859_13.h>
#include <Pantomime/ISO8859_14.h>
#include <Pantomime/ISO8859_15.h>
#include <Pantomime/UTF8.h>
#include <Pantomime/KOI8_R.h>
#include <Pantomime/KOI8_U.h>
#include <Pantomime/WINDOWS_1250.h>
#include <Pantomime/WINDOWS_1251.h>
#include <Pantomime/WINDOWS_1252.h>
#include <Pantomime/WINDOWS_1253.h>
#include <Pantomime/WINDOWS_1254.h>
#include <Pantomime/Message.h>
#include <Pantomime/MimeMultipart.h>
#include <Pantomime/NSString+Extensions.h>
#include <Pantomime/NSData+Extensions.h>
#include <Pantomime/Part.h>
#include <Pantomime/MD5.h>
#include <Pantomime/UUFile.h>
#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSBundle.h>
#include <Foundation/NSCharacterSet.h>
#include <Foundation/NSDebug.h>
#include <Foundation/NSFileManager.h>
#include <Foundation/NSHost.h>
#include <Foundation/NSScanner.h>
#include <Foundation/NSValue.h>
#include <ctype.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// We include the CoreFoundation headers under OS X so we can support
// more string encodings
#ifdef MACOSX
#include <CoreFoundation/CFString.h>
#include <CoreFoundation/CFStringEncodingExt.h>
#endif
#ifdef HAVE_ICONV
#include <iconv.h>
#endif
#define UUDECODE(c) (((c) - ' ') & 077)
static const char *hexDigit = "0123456789ABCDEF";
static char basis_64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static NSMutableDictionary *charsets = nil;
//
//
//
@implementation MimeUtility
+ (void) initialize
{
if ( !charsets )
{
NSDebugLog(@"Initializing the Charset cache.");
charsets = [[NSMutableDictionary alloc] init];
}
}
//
// This method is used to return the string value of a
// specified encoding. If the enconding isn't found,
// it simply returns the default encoding: 7bit.
//
+ (NSString *) stringValueOfTransferEncoding: (int) theEncoding
{
switch (theEncoding) {
case NONE: break;
case QUOTEDPRINTABLE: return @"quoted-printable";
case BASE64: return @"base64";
case EIGHTBIT: return @"8bit";
case BINARY: return @"binary";
default: break;
}
return @"7bit";
}
//
// This method is used to obtain a charset from the name
// of this charset. It caches this charset for future
// usage when it's found.
//
+ (Charset *) charsetForName: (NSString *) theName
{
Charset *theCharset;
theCharset = [charsets objectForKey: [theName lowercaseString]];
if ( !theCharset )
{
Charset *aCharset;
if ( [[theName lowercaseString] isEqualToString: @"iso-8859-2"] )
{
aCharset = (Charset *)[[ISO8859_2 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"iso-8859-3"] )
{
aCharset = (Charset *)[[ISO8859_3 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"iso-8859-4"] )
{
aCharset = (Charset *)[[ISO8859_4 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"iso-8859-5"] )
{
aCharset = (Charset *)[[ISO8859_5 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"iso-8859-6"] )
{
aCharset = (Charset *)[[ISO8859_6 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"iso-8859-7"] )
{
aCharset = (Charset *)[[ISO8859_7 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"iso-8859-8"] )
{
aCharset = (Charset *)[[ISO8859_8 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"iso-8859-9"] )
{
aCharset = (Charset *)[[ISO8859_9 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"iso-8859-10"] )
{
aCharset = (Charset *)[[ISO8859_10 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"iso-8859-11"] )
{
aCharset = (Charset *)[[ISO8859_11 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"iso-8859-13"] )
{
aCharset = (Charset *)[[ISO8859_13 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"iso-8859-14"] )
{
aCharset = (Charset *)[[ISO8859_14 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"iso-8859-15"] )
{
aCharset = (Charset *)[[ISO8859_15 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"koi8-r"] )
{
aCharset = (Charset *)[[KOI8_R alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"koi8-u"] )
{
aCharset = (Charset *)[[KOI8_U alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"windows-1250"] )
{
aCharset = (Charset *)[[WINDOWS_1250 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"windows-1251"] )
{
aCharset = (Charset *)[[WINDOWS_1251 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"windows-1252"] )
{
aCharset = (Charset *)[[WINDOWS_1252 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"windows-1253"] )
{
aCharset = (Charset *)[[WINDOWS_1253 alloc] init];
}
else if ( [[theName lowercaseString] isEqualToString: @"windows-1254"] )
{
aCharset = (Charset *)[[WINDOWS_1254 alloc] init];
}
else
{
aCharset = (Charset *)[[ISO8859_1 alloc] init];
}
[charsets setObject: aCharset
forKey: [theName lowercaseString]];
RELEASE(aCharset);
return aCharset;
}
return theCharset;
}
//
//
//
+ (int) stringEncodingForCharset: (NSData *) charset
{
// We define some aliases for the string encoding.
static struct { NSString *name; int encoding; BOOL fromCoreFoundation; } encodings[] = {
{@"ascii" ,NSASCIIStringEncoding ,NO},
{@"us-ascii" ,NSASCIIStringEncoding ,NO},
{@"utf-8" ,NSUTF8StringEncoding ,NO},
{@"iso-8859-1" ,NSISOLatin1StringEncoding ,NO},
{@"x-user-defined",NSISOLatin1StringEncoding ,NO}, // To prevent a lame bug in Outlook.
{@"x-unknown" ,NSISOLatin1StringEncoding ,NO}, // To prevent a lame bug in Pine 4.21.
{@"unknown-8bit" ,NSISOLatin1StringEncoding ,NO}, // To prevent a lame bug in Mutt/1.3.28i
{@"iso-8859-2" ,NSISOLatin2StringEncoding ,NO},
#ifdef MACOSX
{@"iso-8859-3" ,kCFStringEncodingISOLatin3 ,YES},
{@"iso-8859-4" ,kCFStringEncodingISOLatin4 ,YES},
{@"iso-8859-5" ,kCFStringEncodingISOLatinCyrillic ,YES},
{@"iso-8859-6" ,kCFStringEncodingISOLatinArabic ,YES},
{@"iso-8859-7" ,kCFStringEncodingISOLatinGreek ,YES},
{@"iso-8859-8" ,kCFStringEncodingISOLatinHebrew ,YES},
{@"iso-8859-9" ,kCFStringEncodingISOLatin5 ,YES},
{@"iso-8859-10" ,kCFStringEncodingISOLatin6 ,YES},
{@"iso-8859-11" ,kCFStringEncodingISOLatinThai ,YES},
{@"iso-8859-13" ,kCFStringEncodingISOLatin7 ,YES},
{@"iso-8859-14" ,kCFStringEncodingISOLatin8 ,YES},
{@"iso-8859-15" ,kCFStringEncodingISOLatin9 ,YES},
{@"koi8-r" ,kCFStringEncodingKOI8_R ,YES},
{@"big5" ,kCFStringEncodingBig5 ,YES},
{@"euc-kr" ,kCFStringEncodingEUC_KR ,YES},
{@"ks_c_5601-1987",kCFStringEncodingEUC_KR ,YES},
{@"gb2312" ,kCFStringEncodingHZ_GB_2312 ,YES},
{@"shift_jis" ,kCFStringEncodingShiftJIS ,YES},
{@"windows-1255" ,kCFStringEncodingWindowsHebrew ,YES},
{@"windows-1256" ,kCFStringEncodingWindowsArabic ,YES},
{@"windows-1257" ,kCFStringEncodingWindowsBalticRim ,YES},
{@"windows-1258" ,kCFStringEncodingWindowsVietnamese,YES},
#else
{@"iso-8859-3" ,NSISOLatin3StringEncoding ,NO},
{@"iso-8859-4" ,NSISOLatin4StringEncoding ,NO},
{@"iso-8859-5" ,NSISOCyrillicStringEncoding ,NO},
{@"iso-8859-6" ,NSISOArabicStringEncoding ,NO},
{@"iso-8859-7" ,NSISOGreekStringEncoding ,NO},
{@"iso-8859-8" ,NSISOHebrewStringEncoding ,NO},
{@"iso-8859-9" ,NSISOLatin5StringEncoding ,NO},
{@"iso-8859-10" ,NSISOLatin6StringEncoding ,NO},
{@"iso-8859-11" ,NSISOThaiStringEncoding ,NO},
{@"iso-8859-13" ,NSISOLatin7StringEncoding ,NO},
{@"iso-8859-14" ,NSISOLatin8StringEncoding ,NO},
{@"iso-8859-15" ,NSISOLatin9StringEncoding ,NO},
{@"koi8-r" ,NSKOI8RStringEncoding ,NO},
{@"big5" ,NSBIG5StringEncoding ,NO},
{@"gb2312" ,NSGB2312StringEncoding ,NO},
{@"utf-7" ,NSUTF7StringEncoding ,NO},
{@"unicode-1-1-utf-7", NSUTF7StringEncoding ,NO}, // To prever a bug (sort of) in MS Hotmail
#endif
{@"windows-1250" ,NSWindowsCP1250StringEncoding ,NO},
{@"windows-1251" ,NSWindowsCP1251StringEncoding ,NO},
{@"cyrillic (windows-1251)", NSWindowsCP1251StringEncoding ,NO}, // To prevent a bug in MS Hotmail
{@"windows-1252" ,NSWindowsCP1252StringEncoding ,NO},
{@"windows-1253" ,NSWindowsCP1253StringEncoding ,NO},
{@"windows-1254" ,NSWindowsCP1254StringEncoding ,NO},
{@"iso-2022-jp" ,NSISO2022JPStringEncoding ,NO},
{@"euc-jp" ,NSJapaneseEUCStringEncoding ,NO},
};
NSString *name;
int i;
name = [[NSString stringWithCString: [charset bytes] length: [charset length]] lowercaseString];
for ( i = 0; i < sizeof(encodings)/sizeof(encodings[0]); i++)
{
if ( [name isEqualToString: encodings[i].name] )
{
// Under OS X, we use the core foundation if necessary to convert the encoding
// to a NSString encoding.
#ifdef MACOSX
if ( encodings[i].fromCoreFoundation )
{
return CFStringConvertEncodingToNSStringEncoding(encodings[i].encoding);
}
else
{
return encodings[i].encoding;
}
#else
return encodings[i].encoding;
#endif
}
}
return -1;
}
//
//
//
+ (int) stringEncodingForPart: (Part *) thePart
{
int encoding;
// We get the encoding we are gonna use. We always favor the default encoding.
encoding = -1;
if ( [thePart defaultCharset] )
{
encoding = [MimeUtility stringEncodingForCharset: [[thePart defaultCharset]
dataUsingEncoding: NSASCIIStringEncoding]];
}
else if ( [thePart charset] )
{
encoding = [MimeUtility stringEncodingForCharset: [[thePart charset]
dataUsingEncoding: NSASCIIStringEncoding]];
}
else
{
encoding = [NSString defaultCStringEncoding];
}
if ( encoding == -1 || encoding == NSASCIIStringEncoding)
{
encoding = NSISOLatin1StringEncoding;
}
return encoding;
}
//
//
//
+ (NSString *) stringWithData: (NSData *) theData
charset: (NSData *) theCharset
{
int encoding;
if (theData == nil)
{
return nil;
}
#ifndef MACOSX
if ( [theCharset hasCaseInsensitiveCPrefix: "ks_c_5601-1987"] )
{
theCharset = [NSData dataWithCString: "euc-kr"];
}
#endif
encoding = [self stringEncodingForCharset: theCharset];
if (encoding == -1)
{
#ifdef HAVE_ICONV
NSString *aString;
const char *i_bytes, *from_code;
char *o_bytes;
int i_length, o_length, total_length, ret;
iconv_t conv;
// Instead of calling cString directly on theCharset, we first try
// to obtain the ASCII string of the data object.
from_code = [[theCharset asciiString] cString];
if ( !from_code )
{
NSDebugLog(@"MimeUtility: Failed to obtain the cString of the charset. Aborting.");
return nil;
}
conv = iconv_open("UTF-8", from_code);
if ( conv < 0 )
{
NSDebugLog(@"MimeUtility: Unable to initialize iconv. Aborting.");
return nil;
}
i_bytes = [theData bytes];
i_length = [theData length];
total_length = o_length = sizeof(unichar)*i_length;
o_bytes = (char *)malloc(o_length);
while ( i_length > 0 )
{
ret = iconv(conv, (char **)&i_bytes, &i_length, &o_bytes, &o_length);
if (ret == (size_t)-1)
{
NSDebugLog(@"MimeUtility: Conversion failed using iconv. Aborting");
iconv_close(conv);
total_length = total_length - o_length;
o_bytes -= total_length;
free(o_bytes);
return nil;
}
}
total_length = total_length - o_length;
o_bytes -= total_length;
aString = [[NSString alloc] initWithData: [NSData dataWithBytesNoCopy: o_bytes
length: total_length]
encoding: NSUTF8StringEncoding];
iconv_close(conv);
return AUTORELEASE(aString);
#else
NSDebugLog(@"MimeUtility: Unknown encoding and no iconv support. Aborting");
return nil;
#endif
}
return AUTORELEASE( [[NSString alloc] initWithData: theData
encoding: encoding] );
}
//
// See RFC2047.
// It supports:
//
// Abcd =?ISO-8859-1?Q?=E8fgh?=
// =?iso-8859-1?Q?ab=E7de?= =?iso-8859-1?Q?_?= =?iso-8859-1?Q?oo=F4oo?=
// Abd =?ISO-8859-1?Q?=E8?= fghijklmn
//
// If theCharset is not nil, it is used to decode the header. Otherwise,
// the charset used in the header is used.
//
+ (NSString *) decodeHeader: (NSData *) theData
charset: (NSString *) theCharset
{
NSMutableString *aMutableString;
NSData *charset, *encoded_text;
NSString *aString;
int i, length, start, i_charset, i_encoding, end;
const unsigned char *bytes;
BOOL ignore_span;
char encoding;
// We first verify for a nil value
if (theData == nil)
{
return @"";
}
length = [theData length];
bytes = [theData bytes];
aMutableString = [[NSMutableString alloc] initWithCapacity: length];
start = i = 0;
ignore_span = NO;
while (i < (length - 1))
{
if ( bytes[i] != '=' || bytes[i+1] != '?')
{
if ( bytes[i] > 32 )
{
ignore_span = NO;
}
i++;
continue;
}
if (i != start && !ignore_span)
{
aString = nil;
if ( theCharset )
{
aString = [self stringWithData: [NSData dataWithBytes: bytes+start length: i-start]
charset: [theCharset dataUsingEncoding: NSASCIIStringEncoding]];
RETAIN(aString);
}
if ( !aString )
{
aString = [[NSString alloc] initWithCString: bytes+start length: i-start];
}
[aMutableString appendString: aString];
DESTROY(aString);
}
start = i;
// skip the =? and one character (or it isn't valid)
i += 3;
while ( i < length && bytes[i] != '?')
{
i++;
}
if ( i == length)
{
break;
}
i_charset = i;
/* encoding is a single character */
if (i+2 >= length)
{
break;
}
encoding = bytes[i+1];
if (bytes[i+2] != '?')
{
break;
}
i += 3;
i_encoding = i;
while ( i <length && bytes[i] != '?')
{
i++;
}
if (i+1 >= length)
{
break;
}
if (bytes[i+1] != '=')
{
break;
}
end = i;
i += 2;
if ( theCharset )
{
charset = [theCharset dataUsingEncoding: NSASCIIStringEncoding];
}
else
{
charset = [theData subdataWithRange: NSMakeRange(start+2,i_charset-start-2)];
}
encoded_text = [theData subdataWithRange: NSMakeRange(i_encoding,end-i_encoding)];
if ( encoding == 'q' || encoding == 'Q')
{
aString = [self stringWithData: [self decodeQuotedPrintable: encoded_text inHeader: YES]
charset: charset];
}
else if ( encoding == 'b' || encoding== 'B' )
{
aString = [self stringWithData: [self decodeBase64: encoded_text]
charset: charset];
}
else
{
continue;
}
if ( !aString )
{
continue;
}
[aMutableString appendString: aString];
aString = nil;
start = i;
ignore_span = YES;
}
i = length;
if (i != start && !ignore_span)
{
aString = nil;
if ( theCharset )
{
aString = [self stringWithData: [NSData dataWithBytes: bytes+start length: i-start]
charset: [theCharset dataUsingEncoding: NSASCIIStringEncoding]];
RETAIN(aString);
}
if ( !aString )
{
aString = [[NSString alloc] initWithCString: bytes+start length: i-start];
}
[aMutableString appendString: aString];
DESTROY(aString);
}
return AUTORELEASE(aMutableString);
}
//
// This method is used to decode a string containing characters encoded
// using the Quoted Printable method with the charset "theCharset".
//
+ (NSData *) decodeQuotedPrintable: (NSData *) theData
inHeader: (BOOL) aBOOL
{
NSMutableData *result;
const unsigned char *bytes,*b;
unsigned char ch;
int i,len;
len = [theData length];
bytes = [theData bytes];
result = [[NSMutableData alloc] initWithCapacity: len];
b=bytes;
for (i = 0; i < len; i++,b++)
{
if (b[0]=='=' && i+1<len && b[1]=='\n')
{
b++,i++;
continue;
}
else if (*b=='=' && i+2<len)
{
b++,i++;
if (*b>='A' && *b<='F')
{
ch=16*(*b-'A'+10);
}
else if (*b>='a' && *b<='f')
{
ch=16*(*b-'a'+10);
}
else if (*b>='0' && *b<='9')
{
ch=16*(*b-'0');
}
b++,i++;
if (*b>='A' && *b<='F')
{
ch+=*b-'A'+10;
}
else if (*b>='a' && *b<='f')
{
ch+=*b-'a'+10;
}
else if (*b>='0' && *b<='9')
{
ch+=*b-'0';
}
[result appendBytes: &ch length: 1];
}
else if (aBOOL && *b=='_')
{
ch=0x20;
[result appendBytes: &ch length: 1];
}
else
{
[result appendBytes: b length: 1];
}
}
return AUTORELEASE(result);
}
//
// This method is used to decode a string that has been encoded
// using the base64 method and returns and array of bytes (NSData)
// corresponding to the decoded data.
//
// FIXME: this should ignore characters in the stream that aren't in
// the base64 alphabet (as per the spec). would remove need for
// ...removeLinefeeds... too
//
+ (NSData *) decodeBase64: (NSData *) theData
{
int i, j, length, rawIndex, block, pad, data_len;
const unsigned char *bytes;
char *raw;
NSData *result;
if ( !theData || [theData length] == 0 )
{
return nil;
}
data_len = [theData length];
bytes = [theData bytes];
pad = 0;
for (i = data_len - 1; bytes[i] == '='; i--)
{
pad++;
}
length = data_len * 6 / 8 - pad;
raw = (char*)malloc(length);
rawIndex = 0;
for (i = 0; i < data_len; i += 4)
{
block = (getValue(bytes[i]) << 18) +
(getValue(bytes[i + 1]) << 12) +
(getValue(bytes[i + 2]) << 6) +
(getValue(bytes[i + 3]));
for (j = 0; j < 3 && rawIndex + j < length; j++)
{
raw[rawIndex + j] = (char)((block >> (8 * (2 - j))) & 0xff);
}
rawIndex += 3;
}
result = [[NSData alloc] initWithBytesNoCopy: raw length: length];
return AUTORELEASE(result);
}
//
//
//
+ (NSData *) unfoldLinesFromData: (NSData *) theData
{
NSMutableData *aMutableData;
int i, length;
const unsigned char *bytes, *b;
if ( !theData )
{
return nil;
}
length = [theData length];
b = bytes = [theData bytes];
aMutableData = [[NSMutableData alloc] initWithCapacity: length];
[aMutableData appendBytes: b
length: 1];
b++;
for ( i = 1; i < length; i++,b++)
{
if ( b[-1]=='\n' && (*b==' ' || *b=='\t') )
{
[aMutableData setLength: ([aMutableData length] - 1)];
}
[aMutableData appendBytes: b length: 1];
}
return AUTORELEASE(aMutableData);
}
//
//
//
+ (NSData *) encodeBase64: (NSData *) theData
lineLength: (int) numChars
{
NSData *result;
const char *inBytes = [theData bytes];
const char *inBytesPtr = inBytes;
int inLength = [theData length];
char *outBytes = malloc(sizeof(char)*inLength*2);
char *outBytesPtr = outBytes;
int numWordsPerLine = numChars/4;
int wordCounter = 0;
// We memset 0 our buffer so with are sure to not have
// any garbage in it.
memset(outBytes, 0, sizeof(char)*inLength*2);
while (inLength > 0)
{
nb64ChunkFor3Characters(outBytesPtr, inBytesPtr, inLength);
outBytesPtr += 4;
inBytesPtr += 3;
inLength -= 3;
wordCounter ++;
if (numChars && wordCounter == numWordsPerLine)
{
wordCounter = 0;
*outBytesPtr = '\n';
outBytesPtr++;
}
}
result = [[NSData alloc] initWithBytesNoCopy: outBytes
length: (outBytesPtr-outBytes)];
return AUTORELEASE(result);
}
//
//
//
+ (NSData *) encodeQuotedPrintable: (NSData *) theData
lineLength: (int) numChars
inHeader: (BOOL) aBOOL
{
NSMutableData *aMutableData;
const unsigned char *b;
int i, length, line;
char buf[4];
aMutableData = [[NSMutableData alloc] initWithCapacity: [theData length]];
b = [theData bytes];
length = [theData length];
buf[3] = 0;
buf[0] = '=';
line = 0;
for ( i = 0; i < length; i++, b++ )
{
if (numChars && line >= numChars)
{
[aMutableData appendBytes: "=\n" length: 2];
line = 0;
}
// RFC says must encode space and tab right before end of line
if ( (*b == ' ' || *b == '\t') && i < length - 1 && b[1] == '\n')
{
buf[1] = hexDigit[(*b)>>4];
buf[2] = hexDigit[(*b)&15];
[aMutableData appendBytes: buf
length: 3];
line += 3;
}
// FIXME: really always pass \n through here?
else if (!aBOOL &&
(*b == '\n' || *b == ' ' || *b == '\t'
|| (*b >= 33 && *b <= 60)
|| (*b >= 62 && *b <= 126)))
{
[aMutableData appendBytes: b
length: 1];
if (*b == '\n')
{
line = 0;
}
else
{
line++;
}
}
else if (aBOOL && ((*b >= 'a' && *b <= 'z') || (*b >= 'A' && *b <= 'Z')))
{
[aMutableData appendBytes: b
length: 1];
if (*b == '\n')
{
line = 0;
}
else
{
line++;
}
}
else if (aBOOL && *b==' ')
{
[aMutableData appendBytes: "_"
length: 1];
}
else
{
buf[1] = hexDigit[(*b)>>4];
buf[2] = hexDigit[(*b)&15];
[aMutableData appendBytes: buf
length: 3];
line += 3;
}
}
return AUTORELEASE(aMutableData);
}
//
// This method is used to generate a MIME boundary (or any kind of boundary)
//
+ (NSData *) generateBoundary
{
NSMutableData *aMutableData;
MD5 *md5;
char random_data[9];
time_t curtime;
int i, pid;
pid = getpid();
time(&curtime);
for (i = 0; i < sizeof(random_data); i++)
{
random_data[i] = hexDigit[random()&0xff];
}
random_data[8] = '\0';
md5 = [[MD5 alloc] initWithString: [NSString stringWithFormat: @"%d.%d%s", pid, curtime, random_data]
encoding: NSASCIIStringEncoding];
[md5 computeDigest];
aMutableData = [[NSMutableData alloc] init];
[aMutableData appendBytes: "=_"
length: 2];
[aMutableData appendCFormat: @"%@", [md5 digestAsString]];
DESTROY(md5);
return AUTORELEASE(aMutableData);
}
//
// This method is used to generate a unique ID that Messages (or Mime Body Parts)
// use as their unique ID. (Message-ID, Content-ID, etc.)
//
+ (NSData *) generateOSID
{
NSMutableData *aMutableData;
MD5 *md5;
char random_data[9];
time_t curtime;
int i, pid;
pid = getpid();
time(&curtime);
for (i = 0; i < sizeof(random_data); i++)
{
random_data[i] = hexDigit[random()&0xff];
}
random_data[8] = '\0';
md5 = [[MD5 alloc] initWithString: [NSString stringWithFormat: @"%d.%d%s", pid, curtime, random_data]
encoding: NSASCIIStringEncoding];
[md5 computeDigest];
aMutableData = [[NSMutableData alloc] init];
[aMutableData appendCFormat: @"%@", [md5 digestAsString]];
[aMutableData appendCFormat: @"@%@", [[NSHost currentHost] name]];
DESTROY(md5);
return AUTORELEASE(aMutableData);
}
//
// This method is used to encode a text string using quoted-printable
// or base64.
//
//
+ (NSData *) encodeHeader: (NSString *) theText
{
// Initial verification
if (!theText || [theText length] == 0)
{
return [NSData data];
}
// If it's not an ASCII string, we encode it!
if (! [MimeUtility isASCIIString: theText] )
{
NSString *aCharset;
aCharset = [MimeUtility charsetForString: theText];
return [MimeUtility encodeHeader: theText
usingCharset: aCharset
encoding: QUOTEDPRINTABLE];
}
else
{
return [theText dataUsingEncoding: NSASCIIStringEncoding];
}
}
//
// FIXME: Should we really use NSUTF8StringEncoding in base64?
//
+ (NSData *) encodeHeader: (NSString *) theText
usingCharset: (NSString *) theCharset
encoding: (int) encoding
{
NSData *aData;
// Initial verification
if (!theText || [theText length] == 0)
{
return [NSData data];
}
aData = [theText dataUsingEncoding: [MimeUtility stringEncodingForCharset:
[theCharset dataUsingEncoding: NSASCIIStringEncoding]]];
if (encoding == QUOTEDPRINTABLE)
{
return [MimeUtility encodeQuotedPrintable: aData
lineLength: 0
inHeader: YES];
}
else if (encoding == BASE64)
{
return [MimeUtility encodeBase64: aData
lineLength: 0];
}
//
// FIXME: What should we do here, should we just return the 'aData' w/o
// encoding it or should we generate an exception?
else
{
return aData;
}
}
//
// The format returned is for example:
//
// =?ISO-8859-1?B?....?=
//
// This format is known as an encoded-word defined in RFC2047.
// If the word doesn't need to be encoded, it's just returned a
//
// FIXME: We should verify that the length doesn't exceed 75 chars.
//
+ (NSData *) encodeWordUsingBase64: (NSString *) theWord
prefixLength: (int) thePrefixLength
{
// Initial verification
if (!theWord || [theWord length] == 0)
{
return [NSData data];
}
else if ( [MimeUtility isASCIIString: theWord] )
{
return [theWord dataUsingEncoding: NSASCIIStringEncoding];
}
else
{
NSMutableData *aMutableData;
NSString *aCharset;
aMutableData = [[NSMutableString alloc] init];
aCharset = [MimeUtility charsetForString: theWord];
[aMutableData appendCFormat: @"=?%@?b?", aCharset];
[aMutableData appendData: [MimeUtility encodeHeader: theWord
usingCharset: aCharset
encoding: BASE64]];
[aMutableData appendCString: "?="];
return AUTORELEASE(aMutableData);
}
// Never reached.
return nil;
}
//
// The format returned is for example:
//
// =?ISO-8859-1?Q?=E8fgh?=
//
// This format is known as an encoded-word defined in RFC2047.
// If the word doesn't need to be encoded, it's just returned as it.
//
// The string returned will NOT be more than 75 characters long
// for each folded part of the original string.
//
+ (NSData *) encodeWordUsingQuotedPrintable: (NSString *) theWord
prefixLength: (int) thePrefixLength
{
NSMutableString *aMutableString;
NSMutableArray *aMutableArray;
NSMutableData *aMutableData;
NSScanner *aScanner;
NSString *aCharset;
int i, previousLocation, currentLocation;
BOOL mustUseEncoding;
// Initial verification
if ( !theWord ||
[theWord length] == 0 )
{
return [NSData data];
}
// If it's not an ASCII string, we must use the encoding!
mustUseEncoding = ( ![MimeUtility isASCIIString: theWord] );
aCharset = nil;
if ( mustUseEncoding )
{
aCharset = [MimeUtility charsetForString: theWord];
}
aMutableString = [[NSMutableString alloc] init];
aMutableArray = [[NSMutableArray alloc] init];
AUTORELEASE(aMutableArray);
// We initialize our scanner with the content of our word
aScanner = [[NSScanner alloc] initWithString: theWord];
currentLocation = previousLocation = 0;
while ( [aScanner scanUpToCharactersFromSet: [NSCharacterSet whitespaceCharacterSet]
intoString: NULL] )
{
NSString *aString;
int length;
currentLocation = [aScanner scanLocation];
// aString contains the substring WITH the spaces present BEFORE the word and AFTER the last aString.
//
// For example, the result of "this is a test" will be
//
// aString = "this"
// aString = " is"
// aString = " a"
// aString = " test"
//
aString = [theWord substringWithRange: NSMakeRange(previousLocation, currentLocation - previousLocation)];
if ( mustUseEncoding )
{
// Our 'initial' length contains =?iso-8859-x?q? and ?=
length = 18;
length += [[MimeUtility encodeHeader: [NSString stringWithFormat: @"%@%@", aMutableString, aString]
usingCharset: aCharset
encoding: QUOTEDPRINTABLE] length];
}
else
{
length = [aMutableString length] + [aString length];
}
// If we are on the first line, we must consider the prefix length.
// For example, the prefix length might be the length of the string "Subject: "
if ( [aMutableArray count] == 0 )
{
length += thePrefixLength;
}
if ( length > 75 )
{
[aMutableArray addObject: aMutableString];
RELEASE(aMutableString);
aMutableString = [[NSMutableString alloc] init];
}
[aMutableString appendString: aString];
previousLocation = currentLocation;
}
// We add our last string to the array.
[aMutableArray addObject: aMutableString];
RELEASE(aMutableString);
RELEASE(aScanner);
aMutableData = [[NSMutableData alloc] init];
for (i = 0; i < [aMutableArray count]; i++)
{
// We must append a space (' ') before each folded line, if we need to. We might not
// have to do that since a space might already be present since the scanner used
// previously returns " wordsPrefixedByASpaceCharacter".
if ( i > 0 && ![[aMutableArray objectAtIndex: i] hasPrefix: @" "])
{
[aMutableData appendCString: " "];
}
if ( mustUseEncoding )
{
[aMutableData appendCFormat: @"=?%@?q?", aCharset];
[aMutableData appendData: [MimeUtility encodeHeader: [aMutableArray objectAtIndex: i]
usingCharset: aCharset
encoding: QUOTEDPRINTABLE] ];
[aMutableData appendCString: "?="];
}
else
{
[aMutableData appendData: [[aMutableArray objectAtIndex: i] dataUsingEncoding: NSASCIIStringEncoding]];
}
// We if it is our last string, we must NOT append the \n
if ( !(i == ([aMutableArray count] - 1)) )
{
[aMutableData appendCString: "\n"];
}
}
return AUTORELEASE(aMutableData);
}
//
// This method is used to guess which charset is used in the string.
//
+ (NSString *) charsetForString: (NSString *) theString
{
NSMutableArray *aMutableArray;
NSString *aString;
Charset *aCharset;
unsigned int i, j;
aMutableArray = [[NSMutableArray alloc] init];
[aMutableArray addObject: [self charsetForName: @"iso-8859-1"]];
[aMutableArray addObject: [self charsetForName: @"iso-8859-2"]];
[aMutableArray addObject: [self charsetForName: @"iso-8859-3"]];
[aMutableArray addObject: [self charsetForName: @"iso-8859-4"]];
[aMutableArray addObject: [self charsetForName: @"iso-8859-5"]];
[aMutableArray addObject: [self charsetForName: @"iso-8859-6"]];
[aMutableArray addObject: [self charsetForName: @"iso-8859-7"]];
[aMutableArray addObject: [self charsetForName: @"iso-8859-8"]];
[aMutableArray addObject: [self charsetForName: @"iso-8859-9"]];
[aMutableArray addObject: [self charsetForName: @"iso-8859-10"]];
[aMutableArray addObject: [self charsetForName: @"iso-8859-11"]];
[aMutableArray addObject: [self charsetForName: @"iso-8859-13"]];
[aMutableArray addObject: [self charsetForName: @"iso-8859-14"]];
[aMutableArray addObject: [self charsetForName: @"iso-8859-15"]];
[aMutableArray addObject: [self charsetForName: @"koi8-r"]];
[aMutableArray addObject: [self charsetForName: @"koi8-u"]];
[aMutableArray addObject: [self charsetForName: @"windows-1250"]];
[aMutableArray addObject: [self charsetForName: @"windows-1251"]];
[aMutableArray addObject: [self charsetForName: @"windows-1252"]];
[aMutableArray addObject: [self charsetForName: @"windows-1253"]];
[aMutableArray addObject: [self charsetForName: @"windows-1254"]];
for ( i = 0; i < [theString length]; i++ )
{
for ( j = 0; j < [aMutableArray count] ; j++ )
{
if ( ![[aMutableArray objectAtIndex: j] characterIsInCharset: [theString characterAtIndex: i]] )
{
// Character is not in the charset
[aMutableArray removeObjectAtIndex: j];
j--;
}
}
// FIXME: can't break even if there is only one left. First we have to check
// whether that encoding will actually work for the entire string. If it
// doesn't we'll need to fall back to utf-8 (or something else that can encode
// _everything_).
//
// Intelligent string splitting would help, of course
//
if ( [aMutableArray count] < 1 )
{
// We have zero or one charset
break;
}
}
if ( [aMutableArray count] )
{
aCharset = [aMutableArray objectAtIndex: 0];
[aMutableArray removeAllObjects];
aString = [aCharset name];
}
else
{
// We have no charset, we try to "guess" a default charset
if ( [theString canBeConvertedToEncoding: NSISO2022JPStringEncoding] )
{
// ISO-2022-JP is the standard of Japanese character encoding
aString = @"iso-2022-jp";
}
else
{
// We have no charset, we return a default charset
aString = @"utf-8";
}
}
RELEASE(aMutableArray);
return aString;
}
//
//
//
+ (BOOL) isASCIIString: (NSString *) theString
{
int i, len;
if ( !theString )
{
return YES;
}
// We search for a non-ASCII character.
len = [theString length];
for (i = 0; i < len; i++)
{
if ( [theString characterAtIndex: i] > 0x007E )
{
return NO;
}
}
return YES;
}
//
// Wraps theString at the theLimit with respect to RFC 2646
//
+ (NSString *) wrapPlainTextString: (NSString *) theString
usingWrappingLimit: (int) theLimit
{
NSMutableString *aMutableString;
NSArray *lines;
NSString *aLine, *part;
int i, j, k, split;
int depth;
// We first verify if the string is valid
if (!theString || [theString length] == 0)
{
return @"";
}
// We then verify if the limit is valid
if (theLimit == 0 || theLimit > 998)
{
theLimit = 998;
}
// We initialize our local variables
aMutableString = [[NSMutableString alloc] initWithCapacity: [theString length]];
lines = [theString componentsSeparatedByString: @"\n"];
// We analyse each line
for (i = 0; i < [lines count]; i++)
{
aLine = [lines objectAtIndex: i];
// We compute the quote depth
for (depth = 0; depth < [aLine length] && [aLine characterAtIndex: depth] == '>'; depth++);
j = depth;
// We remove the leading whitespace if any
if (depth && [aLine length] > j && [aLine characterAtIndex: j] == 32)
{
j++;
}
aLine = [aLine substringFromIndex: j];
// If the line is NOT the signature separator, we remove the trailing space(s)
if (![aLine isEqual: @"-- "])
{
for (j = [aLine length]; j > 0 && [aLine characterAtIndex: j - 1] == 32; j--);
if (depth && j < [aLine length])
{
// If line is quoted, we preserve a whitespace for the soft-break
j++;
}
aLine = [aLine substringToIndex: j];
}
// If the line is the signature separator or if the line length with the
// quote characters and the space-stuffing is lower than the limit,
// we directly append the line to the buffer
if ([aLine isEqual: @"-- "] || depth + 1 + [aLine length] <= theLimit)
{
// We add the quote characters
for (j = 0; j < depth; j++)
{
[aMutableString appendString: @">"];
}
// We space-stuff the line if necesary. The conditions are:
// - the line is quoted or
// - the line starts with a quote character or
// - the line starts with a whitespace or
// - the line starts with the word From.
if (depth ||
([aLine length] && ([aLine characterAtIndex: 0] == '>' || [aLine characterAtIndex: 0] == ' ' || [aLine hasPrefix: @"From"])))
{
[aMutableString appendString: @" "];
}
// We append the line to the buffer
[aMutableString appendString: aLine];
[aMutableString appendString: @"\n"];
// We jump to the next line
continue;
}
// We look for the right place to split the line
for (j = 0; j < [aLine length];)
{
// We verify if the line after character j has a length lower than the limit
if ([aLine length] - j + depth + 1 < theLimit)
{
split = [aLine length];
}
// No it hasn't
else
{
split = j;
// We search for the last whitespace before the limit
for (k = j; k < [aLine length] && k - j + depth + 1 < theLimit; k++)
{
if ([aLine characterAtIndex: k] == 32)
{
split = k;
}
}
/*
Alexander Malmberg:
No good spot; include the entire next word. This isn't really
optimal, but the alternative is to split the word, and that
would be horribly ugly. Also, it'd mean that deeply quoted
text might appear with one letter on each row, which is even
uglier and means that the receiver won't be able to
reconstruct the text.
A proper fix would be to have both parameters for a 'soft'
line limit that we _try_ to break before, and a 'hard' line
limit that specifies an actual hard limit of a protocol or
something. In NNTP, the values would be 72 and 998
respectively. This means that text quoted 70 levels (and yes,
I have seen such posts) will appear with one unbroken word on
each line (as long as the word is shorter than 928
characters). This is still ugly, but:
a. invalid (protocol-wise) lines will never be generated
(unless something's quoted >998 levels)
b. a MIME decoder that handles format=flowed will be able to
reconstruct the text properly
(Additionally, it might turn out to be useful to have a lower
limit on wrapping length, eg. 20. If the effective line
length is shorter than this, wrap to quote-depth+soft-limit
(so eg. text quoted 60 times would be wrapped at 60+72
characters instead of 72). This wouldn't make any difference
on flowed capable MIME decoders, but might turn out to look
better when viewed with non-flowed handling programs.
Hopefully, such deeply quoted text won't be common enough to
be worth the trouble, so people with non-flowed capable
software will simply have to live with the ugly posts in
those cases.)
*/
if (split == j)
{
// No whitespace found before the limit;
// continue farther until a whitespace or the last character of the line
for (; k < [aLine length] && [aLine characterAtIndex: k] != 32; k++);
split = k;
}
}
// Since the line will be splitted, we must keep a whitespace for
// the soft-line break
if (split < [aLine length])
{
split++;
}
// Retrieve splitted part of line
part = [aLine substringWithRange: NSMakeRange(j, split - j)];
// We add the quote characters
for (k = 0; k < depth; k++)
{
[aMutableString appendString: @">"];
}
// We space-stuff the line if necesary.
if (depth ||
([part length] && ([part characterAtIndex: 0] == '>' || [part characterAtIndex: 0] == ' ' || [part hasPrefix: @"From"])))
{
[aMutableString appendString: @" "];
}
// Append line part to buffer
[aMutableString appendString: part];
[aMutableString appendString: @"\n"];
// Next iteration continues where current split occured
j = split;
}
}
if (i > 0)
{
[aMutableString deleteCharactersInRange: NSMakeRange([aMutableString length] - 1, 1)];
}
return AUTORELEASE(aMutableString);
}
//
// Quotes an unwrapped string.
//
+ (NSString *) quotePlainTextString: (NSString *) theString
quoteLevel: (int) theLevel
wrappingLimit: (int) theLimit
{
NSMutableString *aMutableString, *aQuotePrefix;
NSArray *lines;
NSString *aString, *aLine;
BOOL isQuoted;
int i;
// We verify if the wrapping limit is smaller then the quote level
if (theLevel > theLimit)
{
return @"";
}
// We initialize our local variables
// We wrap the text block
aMutableString = [[NSMutableString alloc] initWithCapacity: [theString length]];
aQuotePrefix = [[NSMutableString alloc] initWithCapacity: theLevel];
// We wrap the string to the proper limit
aString = [MimeUtility wrapPlainTextString: theString
usingWrappingLimit: (theLimit - theLevel)];
lines = [aString componentsSeparatedByString: @"\n"];
// We prepare the line prefix
for (i = 0; i < theLevel; i++)
{
[aQuotePrefix appendString: @">"];
}
// We add the line prefix to each wrapped line
for (i = 0; i < [lines count]; i++)
{
aLine = [lines objectAtIndex: i];
isQuoted = ([aLine length] > 0 && [aLine characterAtIndex: 0] == '>' );
[aMutableString appendString: aQuotePrefix];
if (!isQuoted)
{
[aMutableString appendString: @" "];
}
[aMutableString appendString: aLine];
[aMutableString appendString: @"\n"];
}
if (i > 0)
{
[aMutableString deleteCharactersInRange: NSMakeRange([aMutableString length] - 1, 1)];
}
RELEASE(aQuotePrefix);
return AUTORELEASE(aMutableString);
}
//
// Unwraps a string.
//
+ (NSString *) unwrapPlainTextString: (NSString *) theString
usingQuoteWrappingLimit: (int) theQuoteLimit
{
NSMutableString *aMutableString, *lines;
NSString *aLine;
BOOL isFlowed;
int quote_depth, line_quote_depth, line_start;
int i;
// We initialize our local variables
aMutableString = [[NSMutableString alloc] initWithCapacity: [theString length]];
lines = [[NSMutableString alloc] init];
quote_depth = -1;
// We analyse the string until the last character
for (i = 0; i < [theString length];)
{
// We analyse the quote depth of the current line
if ([theString characterAtIndex: i] == '>')
{
for (line_quote_depth = 0; [theString characterAtIndex: i] == '>'; i++)
{
line_quote_depth++;
}
}
else
{
line_quote_depth = 0;
}
// If the current quote depth is not defined, set it to quote depth of current line
if (quote_depth == -1)
{
quote_depth = line_quote_depth;
}
// We verify if the line has been space-stuffed
if ([theString characterAtIndex: i] == ' ')
{
i++;
}
line_start = i;
// We look for the next line break
for (; i < [theString length] && [theString characterAtIndex: i] != '\n'; i++);
// We get the actual content of the current line
aLine = [theString substringWithRange: NSMakeRange(line_start, i - line_start)];
// We verify if the line ends with a soft break
isFlowed = [aLine length] > 0 && [aLine characterAtIndex: [aLine length] - 1] == ' ';
// We must handle usenet signature as a special case
if (isFlowed && [aLine isEqualToString: @"-- "])
{
isFlowed = NO;
}
if (isFlowed && quote_depth == line_quote_depth)
{
// The current line is flowed;
// we append it to the buffer without quote characters
[lines appendString: aLine];
}
else if (isFlowed)
{
// The current line is flowed but has mis-matched quoting
// We first append the previous paragraph to the buffer with the necessary quote characters
if (quote_depth)
{
[lines replaceCharactersInRange: NSMakeRange(0, [lines length])
withString: [MimeUtility quotePlainTextString: lines
quoteLevel: quote_depth
wrappingLimit: theQuoteLimit]];
}
[aMutableString appendString: lines];
[aMutableString appendString: @"\n"];
// We initialize the current paragraph with the current line
[lines replaceCharactersInRange: NSMakeRange(0, [lines length])
withString: aLine];
// We set the paragraph depth with the current line depth
quote_depth = line_quote_depth;
}
else if (!isFlowed && quote_depth == line_quote_depth)
{
// The line is fixed
// We first append the fixed line
[lines appendString: aLine];
// We add the necessary quote characters in the paragraph
if (quote_depth)
{
[lines replaceCharactersInRange: NSMakeRange(0, [lines length])
withString: [MimeUtility quotePlainTextString: lines
quoteLevel: quote_depth
wrappingLimit: theQuoteLimit]];
}
// We append the paragraph (if any)
if ([lines length])
{
[aMutableString appendString: lines];
}
[aMutableString appendString: @"\n"];
// We empty the paragraph buffer
[lines replaceCharactersInRange: NSMakeRange(0,[lines length])
withString: @""];
// We reset the paragraph depth
quote_depth = -1;
}
else
{
// The line is fixed but has mis-matched quoting
// We first append the previous paragraph (if any) to the buffer with the necessary quote characters
if (quote_depth)
{
[lines replaceCharactersInRange: NSMakeRange(0, [lines length])
withString: [MimeUtility quotePlainTextString: lines
quoteLevel: quote_depth
wrappingLimit: theQuoteLimit]];
}
[aMutableString appendString: lines];
[aMutableString appendString: @"\n"];
// We append the fixed line to the buffer with the necessary quote characters
if (line_quote_depth)
{
aLine = [MimeUtility quotePlainTextString: aLine
quoteLevel: line_quote_depth
wrappingLimit: theQuoteLimit];
}
[aMutableString appendString: aLine];
[aMutableString appendString: @"\n"];
// We empty the paragraph buffer
[lines replaceCharactersInRange: NSMakeRange(0,[lines length])
withString: @""];
// We reset the paragraph depth
quote_depth = -1;
}
// The next iteration must starts after the line break
i++;
}
// We must handle flowed lines that don't have a fixed line break at the end of the message
if ([lines length])
{
if (quote_depth)
{
[lines replaceCharactersInRange: NSMakeRange(0, [lines length])
withString: [MimeUtility quotePlainTextString: lines
quoteLevel: quote_depth
wrappingLimit: theQuoteLimit]];
}
[aMutableString appendString: lines];
[aMutableString appendString: @"\n"];
}
DESTROY(lines);
return AUTORELEASE(aMutableString);
}
//
//
//
+ (Message *) compositeMessageContentFromRawSource: (NSData *) theData
{
Message *aMessage;
aMessage = [[Message alloc] initWithData: theData];
return AUTORELEASE(aMessage);
}
//
// FIXME: whitespace after boundary markers
//
+ (MimeMultipart *) compositeMultipartContentFromRawSource: (NSData *) theData
usingBoundary: (NSData *) theBoundary
{
MimeMultipart *aMimeMultipart;
NSMutableData *aMutableData;
NSArray *allParts;
NSRange aRange;
int i;
// We first create our MimeMultipart object that will hold our Part objects
aMimeMultipart = [[MimeMultipart alloc] init];
aMutableData=[[NSMutableData alloc] init];
[aMutableData appendBytes: "--" length: 2];
[aMutableData appendData: theBoundary];
// We first skip everything before the first boundary
aRange = [theData rangeOfData: aMutableData];
// Was if ( aRange.length > 0 ) ...
if ( aRange.length && aRange.location )
{
theData = [theData subdataFromIndex: (aRange.location + aRange.length)];
}
[aMutableData setLength: 0];
[aMutableData appendBytes: "\n--" length: 3];
[aMutableData appendData: theBoundary];
// Add terminating 0 so we can use it as a cstring below
[aMutableData appendBytes: "\0" length: 1];
// We split this mime body part into multiple parts since we have various representations
// of the actual body part.
allParts = [theData componentsSeparatedByCString: [aMutableData bytes] ];
DESTROY(aMutableData);
for (i = 0; i < [allParts count]; i++)
{
Part *aPart;
NSData *aData;
// We get the string corresponding to our alternative body part
aData = [allParts objectAtIndex: i];
if (aData && [aData length] > 0)
{
// this is the last part. Ignore everything past the end marker
if ( [aData hasCPrefix: "--\n"] ||
( [aData length] == 2 && [aData hasCPrefix: "--"] ) )
{
break;
}
// We then skip the first character since it's a \n (the one at the end of the boundary)
if ( [aData hasCPrefix: "\n"] )
{
aData = [aData subdataFromIndex: 1];
}
aPart = [[Part alloc] initWithData: aData];
[aPart setSize: [aData length]];
[aMimeMultipart addBodyPart: aPart];
RELEASE(aPart);
}
}
return AUTORELEASE(aMimeMultipart);
}
//
// We handle in this method discrete types likes text/*, application/*, image/*, etc..
//
// It might return a NSString or a NSData object (if the encoding is BASE64).
//
+ (id) discreteContentFromRawSource: (NSData *) theData
usingContentTransferEncoding: (int) theContentTransferEncoding
charset: (NSString *) theCharset
part: (Part *) thePart
{
NSString *aString;
// We get the right charset for our message
if ( !theCharset ||
[theCharset caseInsensitiveCompare: @"us-ascii"] == NSOrderedSame )
{
theCharset = @"iso-8859-1";
}
if ( theContentTransferEncoding == QUOTEDPRINTABLE )
{
NSData *aData;
// We decode our content from QP using our charset
aData = [MimeUtility decodeQuotedPrintable: theData inHeader: NO];
aString = [MimeUtility stringWithData: aData
charset: [theCharset dataUsingEncoding: NSASCIIStringEncoding]];
if ( aString )
{
return aString;
}
// Decoding failed, we return the raw data
return aData;
}
else if ( theContentTransferEncoding == BASE64 && [thePart isMimeType: @"text" : @"*"] )
{
NSData *aData;
aData = [theData dataByRemovingLineFeedCharacters];
aData = [MimeUtility decodeBase64: aData];
aString = [MimeUtility stringWithData: aData
charset: [theCharset dataUsingEncoding: NSASCIIStringEncoding]];
if ( aString )
{
return aString;
}
// Decoding failed, we return the raw data
return aData;
}
else if ( theContentTransferEncoding == BASE64 )
{
NSData *aData;
aData = [theData dataByRemovingLineFeedCharacters];
aData = [MimeUtility decodeBase64: aData];
[thePart setSize: [aData length]];
return aData;
}
aString= [MimeUtility stringWithData: theData
charset: [theCharset dataUsingEncoding: NSASCIIStringEncoding]];
// Decoding failed, we return the raw data
if ( !aString )
{
return theData;
}
// We have a 'standard encoding' but the format is "flowed"
if ( [thePart format] == FORMAT_FLOWED )
{
// We decode the flowed text
aString = [MimeUtility unwrapPlainTextString: aString usingQuoteWrappingLimit: 80];
return aString;
}
// Our format isn't flowed and we use NONE (7BIT), 8BIT or BINARY as our encoding.
return aString;
}
//
//
//
+ (void) setContentFromRawSource: (NSData *) theData
inPart: (Part *) thePart
{
NSAutoreleasePool *pool;
RETAIN(theData);
RETAIN(thePart);
// We create a temporary autorelease pool since this method can be
// memory consuming on our default autorelease pool.
pool = [[NSAutoreleasePool alloc] init];
//
// Composite types (message/multipart).
//
if ( [thePart isMimeType: @"message" : @"rfc822"] )
{
NSData *aData;
aData = theData;
// We verify the Content-Transfer-Encoding, this part could be base64 encoded.
if ( [thePart contentTransferEncoding] == BASE64 )
{
NSMutableData *aMutableData;
aData = [theData dataByRemovingLineFeedCharacters];
aData = [MimeUtility decodeBase64: aData];
aMutableData = [NSMutableData dataWithData: aData];
[aMutableData replaceCRLFWithLF];
aData = aMutableData;
}
[thePart setContent: [MimeUtility compositeMessageContentFromRawSource: aData] ];
}
else if ( [thePart isMimeType: @"multipart" : @"*"] )
{
[thePart setContent: [MimeUtility compositeMultipartContentFromRawSource: theData
usingBoundary: [thePart boundary]] ];
}
//
// Discrete types (text/application/audio/image/video) or any "unsupported Content-Type:s"
//
// text/*
// image/*
// application/*
// audio/*
// video/*
//
// We also treat those composite type as discrete types:
//
// message/delivery-status
// message/disposition-notification
//
else
{
[thePart setContent: [MimeUtility discreteContentFromRawSource: theData
usingContentTransferEncoding: [thePart contentTransferEncoding]
charset: ([thePart defaultCharset] ? [thePart defaultCharset] : [thePart charset])
part: thePart] ];
}
RELEASE(pool);
RELEASE(theData);
RELEASE(thePart);
}
//
//
//
+ (UUFile *) fileFromUUEncodedString: (NSString *) theString
{
NSString *aString, *aFilename;
NSNumber *theFilePermissions;
NSMutableData *aMutableData;
NSArray *allLines;
UUFile *aUUFile;
int i;
aMutableData = [NSMutableData dataWithCapacity: [theString length]];
allLines = [theString componentsSeparatedByString: @"\n"];
// We decode our filename and our mode
aString = [allLines objectAtIndex: 0];
theFilePermissions = [NSNumber numberWithInt: [[[aString componentsSeparatedByString: @" "] objectAtIndex: 1] intValue]];
aFilename = [[aString componentsSeparatedByString: @" "] objectAtIndex: 2];
// We now get the data representing our uuencoding string
for (i = 1; i < ([allLines count] - 1); i++)
{
NSString *aLine;
aLine = [allLines objectAtIndex: i];
uudecodeline((char *)[aLine cString], aMutableData);
}
// We finally initialize our file wrapper will all our informations
aUUFile = [[UUFile alloc] initWithName: aFilename
data: aMutableData
attributes: [NSDictionary dictionaryWithObject: theFilePermissions
forKey: NSFilePosixPermissions]];
return AUTORELEASE(aUUFile);
}
//
// FIXME, we currently ignore theRange
//
+ (NSRange) rangeOfUUEncodedStringFromString: (NSString *) theString
range: (NSRange) theRange
{
NSRange r1, r2;
r1 = [theString rangeOfString: @"begin "];
if ( r1.length == 0 )
{
return NSMakeRange(NSNotFound, 0);
}
r2 = [theString rangeOfString: @"\nend"
options: 0
range: NSMakeRange(r1.location, [theString length] - r1.location)];
if ( r2.length == 0 )
{
return NSMakeRange(NSNotFound, 0);
}
return NSMakeRange(r1.location, (r2.location + r2.length) - r1.location);
}
//
//
//
+ (NSDictionary *) allCharsets
{
NSMutableDictionary *aMutableDictionary;
aMutableDictionary = [NSMutableDictionary dictionaryWithCapacity: 25];
[aMutableDictionary setObject: _(@"Western European (ISO Latin 1)") forKey: @"iso-8859-1"];
[aMutableDictionary setObject: _(@"Western European (ISO Latin 9)") forKey: @"iso-8859-15"];
[aMutableDictionary setObject: _(@"Western European (Windows Latin 1)") forKey: @"windows-1252"];
[aMutableDictionary setObject: _(@"Japanese (ISO 2022-JP)") forKey: @"iso-2022-jp"];
[aMutableDictionary setObject: _(@"Japanese (EUC-JP)") forKey: @"euc-jp"];
[aMutableDictionary setObject: _(@"Traditional Chinese (BIG5)") forKey: @"big5"];
[aMutableDictionary setObject: _(@"Arabic (ISO 8859-6)") forKey: @"iso-8859-6"];
[aMutableDictionary setObject: _(@"Greek (ISO 8859-7)") forKey: @"iso-8859-7"];
[aMutableDictionary setObject: _(@"Greek (Windows)") forKey: @"windows-1253"];
[aMutableDictionary setObject: _(@"Hebrew (ISO 8859-8)") forKey: @"iso-8859-8"];
[aMutableDictionary setObject: _(@"Cyrillic (ISO 8859-5)") forKey: @"iso-8859-5"];
[aMutableDictionary setObject: _(@"Cyrillic (KOI8-R)") forKey: @"koi8-r"];
[aMutableDictionary setObject: _(@"Cyrillic (Windows)") forKey: @"windows-1251"];
[aMutableDictionary setObject: _(@"Thai (ISO 8859-11)") forKey: @"iso-8859-11"];
[aMutableDictionary setObject: _(@"Central European (ISO Latin 2)") forKey: @"iso-8859-2"];
[aMutableDictionary setObject: _(@"Central European (Windows Latin 2)") forKey: @"windows-1250"];
[aMutableDictionary setObject: _(@"Turkish (Latin 5)") forKey: @"iso-8859-9"];
[aMutableDictionary setObject: _(@"Turkish (Windows)") forKey: @"windows-1254"];
[aMutableDictionary setObject: _(@"South European (ISO Latin 3)") forKey: @"iso-8859-3"];
[aMutableDictionary setObject: _(@"North European (ISO Latin 4)") forKey: @"iso-8859-4"];
[aMutableDictionary setObject: _(@"Nordic (ISO Latin 6)") forKey: @"iso-8859-10"];
[aMutableDictionary setObject: _(@"Baltic Rim (ISO Latin 7)") forKey: @"iso-8859-13"];
[aMutableDictionary setObject: _(@"Celtic (ISO Latin 8)") forKey: @"iso-8859-14"];
[aMutableDictionary setObject: _(@"Simplified Chinese (GB2312)") forKey: @"gb2312"];
[aMutableDictionary setObject: _(@"UTF-8") forKey: @"utf-8"];
#ifdef MACOSX
[aMutableDictionary setObject: _(@"Korean (EUC-KR/KS C 5601)") forKey: @"euc-kr"];
[aMutableDictionary setObject: _(@"Japanese (Win/Mac)") forKey: @"shift_jis"];
#endif
return aMutableDictionary;
}
//
//
//
+ (NSString *) stringFromRecipients: (NSArray *) allRecipients
type: (int) recipientType
{
InternetAddress *anInternetAddress;
NSMutableString *aMutableString;
int i;
aMutableString = [[NSMutableString alloc] init];
for (i = 0; i < [allRecipients count]; i++)
{
anInternetAddress = [allRecipients objectAtIndex: i];
if ([anInternetAddress type] == recipientType)
{
[aMutableString appendFormat: @"%@, ", [anInternetAddress unicodeStringValue]];
}
}
return AUTORELEASE(aMutableString);
}
//
//
//
+ (NSString *) plainTextContentFromPart: (Part *) thePart
{
NSString *aString, *aContent;
// If our part was base64 encoded, we must convert the
// NSData object into a NSString
if ( [thePart contentTransferEncoding] == BASE64 &&
[[thePart content] isKindOfClass: [NSData class]] )
{
aString = [[NSString alloc] initWithData: (NSData *)[thePart content]
encoding: [MimeUtility stringEncodingForPart: thePart]];
AUTORELEASE(aString);
}
else
{
aString = (NSString *)[thePart content];
}
// If it's a text/enriched or a text/html, we must remove all the
// formatting codes and keep only the 'real' text contained in that part.
// FIXME - choose the right encoding. lossyCString will lose informations.
if ( [thePart isMimeType: @"text": @"html"] )
{
NSData *aData;
char *buf, *bytes;
int encoding;
// We get the encoding we are gonna use. We always favor the default encoding.
encoding = [MimeUtility stringEncodingForPart: thePart];
// We get our bytes. Using the right encoding to not lose any information.
// FIXME: This needs optimizations. UTF8String should be used here but it seems to
// bork for no good reasons.
aData = [aString dataUsingEncoding: encoding];
buf = (char *)malloc(([aData length]+1)*sizeof(char));
memset(buf, 0, [aData length]+1);
memcpy(buf, [aData bytes], [aData length]);
bytes = striphtml(buf);
free(buf);
// We create our data object from our bytes and we get the right string object from
// it after, using the proper encoding.
aData = [NSData dataWithBytesNoCopy: bytes length: strlen(bytes)];
aContent = [[NSString alloc] initWithData: aData encoding: encoding];
AUTORELEASE(aContent);
}
else
{
aContent = aString;
}
return aContent;
}
@end
//
// C functions
//
int getValue(char c) {
if (c >= 'A' && c <= 'Z') return (c - 'A');
if (c >= 'a' && c <= 'z') return (c - 'a' + 26);
if (c >= '0' && c <= '9') return (c - '0' + 52);
if (c == '+') return 62;
if (c == '/') return 63;
if (c == '=') return 0;
return -1;
}
//
//
//
void nb64ChunkFor3Characters(char *buf, const char *inBuf, int numChars) {
if (numChars >= 3)
{
buf[0] = basis_64[inBuf[0]>>2 & 0x3F];
buf[1] = basis_64[(((inBuf[0] & 0x3)<< 4) | ((inBuf[1] & 0xF0) >> 4)) & 0x3F];
buf[2] = basis_64[(((inBuf[1] & 0xF) << 2) | ((inBuf[2] & 0xC0) >>6)) & 0x3F];
buf[3] = basis_64[inBuf[2] & 0x3F];
}
else if(numChars == 2)
{
buf[0] = basis_64[inBuf[0]>>2 & 0x3F];
buf[1] = basis_64[(((inBuf[0] & 0x3)<< 4) | ((inBuf[1] & 0xF0) >> 4)) & 0x3F];
buf[2] = basis_64[(((inBuf[1] & 0xF) << 2) | ((0 & 0xC0) >>6)) & 0x3F];
buf[3] = '=';
}
else
{
buf[0] = basis_64[inBuf[0]>>2 & 0x3F];
buf[1] = basis_64[(((inBuf[0] & 0x3)<< 4) | ((0 & 0xF0) >> 4)) & 0x3F];
buf[2] = '=';
buf[3] = '=';
}
}
//
//
//
void uudecodeline(char *line, NSMutableData *data)
{
int c, len;
len = UUDECODE(*line++);
while (len)
{
c = UUDECODE(*line) << 2 | UUDECODE(line[1]) >> 4;
[data appendBytes: &c
length: 1];
if (--len)
{
c = UUDECODE(line[1]) << 4 | UUDECODE(line[2]) >> 2;
[data appendBytes: &c
length: 1];
if (--len)
{
c = UUDECODE(line[2]) << 6 | UUDECODE(line[3]);
[data appendBytes: &c
length: 1];
len--;
}
}
line += 4;
}
return;
}
//
// This C function has been written by Abhijit Menon-Sen <ams@wiw.org>
// This code is in the public domain.
//
char *striphtml(char *s)
{
int sgml = 0, tag = 0;
char c, last = '\0', quote = '\0', *t, *text;
if ((t = text = malloc(strlen(s)+1)) == NULL)
return NULL;
while ((c = *s++)) {
if (c == quote) {
if (c == '-' && last != '-')
goto next;
else
last = '\0';
quote = '\0';
}
else if (!quote) {
switch (c) {
case '<':
tag = 1;
if (*s++ == '!')
sgml = 1;
break;
case '>':
if (tag)
sgml = tag = 0;
break;
case '-':
if (sgml && last == '-')
quote = '-';
break;
/* case '"':
case '\'':
if (tag)
quote = c;
break; */
case '&':
*t++ = ent(&s);
break;
default:
if (!tag)
*t++ = c;
break;
}
}
next:
last = c;
}
*t++ = '\0';
return text;
}
//
// This C function has been written by Abhijit Menon-Sen <ams@wiw.org>
// This code is in the public domain.
//
char ent(char **ref)
{
int i;
char c = ' ', *s = *ref, *t = s;
struct {
char *name;
char chr;
} refs[] = {
{ "lt" , '<' },
{ "gt" , '>' },
{ "amp" , '&' },
{ "quot" , '"' },
{ "nbsp" , (char)160 },
{ "iexcl" , (char)161 },
{ "cent" , (char)162 },
{ "pound" , (char)163 },
{ "curren", (char)164 },
{ "yen" , (char)165 },
{ "brvbar", (char)166 },
{ "sect" , (char)167 },
{ "uml" , (char)168 },
{ "copy" , (char)169 },
{ "ordf" , (char)170 },
{ "laquo" , (char)171 },
{ "not" , (char)172 },
{ "shy" , (char)173 },
{ "reg" , (char)174 },
{ "macr" , (char)175 },
{ "deg" , (char)176 },
{ "plusmn", (char)177 },
{ "sup2" , (char)178 },
{ "sup3" , (char)179 },
{ "acute" , (char)180 },
{ "micro" , (char)181 },
{ "para" , (char)182 },
{ "middot", (char)183 },
{ "cedil" , (char)184 },
{ "sup1" , (char)185 },
{ "ordm" , (char)186 },
{ "raquo" , (char)187 },
{ "frac14", (char)188 },
{ "frac12", (char)189 },
{ "frac34", (char)190 },
{ "iquest", (char)191 },
{ "Agrave", (char)192 },
{ "Aacute", (char)193 },
{ "Acirc" , (char)194 },
{ "Atilde", (char)195 },
{ "Auml" , (char)196 },
{ "Aring" , (char)197 },
{ "AElig" , (char)198 },
{ "Ccedil", (char)199 },
{ "Egrave", (char)200 },
{ "Eacute", (char)201 },
{ "Ecirc" , (char)202 },
{ "Euml" , (char)203 },
{ "Igrave", (char)204 },
{ "Iacute", (char)205 },
{ "Icirc" , (char)206 },
{ "Iuml" , (char)207 },
{ "ETH" , (char)208 },
{ "Ntilde", (char)209 },
{ "Ograve", (char)210 },
{ "Oacute", (char)211 },
{ "Ocirc" , (char)212 },
{ "Otilde", (char)213 },
{ "Ouml" , (char)214 },
{ "times" , (char)215 },
{ "Oslash", (char)216 },
{ "Ugrave", (char)217 },
{ "Uacute", (char)218 },
{ "Ucirc" , (char)219 },
{ "Uuml" , (char)220 },
{ "Yacute", (char)221 },
{ "THORN" , (char)222 },
{ "szlig" , (char)223 },
{ "agrave", (char)224 },
{ "aacute", (char)225 },
{ "acirc" , (char)226 },
{ "atilde", (char)227 },
{ "auml" , (char)228 },
{ "aring" , (char)229 },
{ "aelig" , (char)230 },
{ "ccedil", (char)231 },
{ "egrave", (char)232 },
{ "eacute", (char)233 },
{ "ecirc" , (char)234 },
{ "euml" , (char)235 },
{ "igrave", (char)236 },
{ "iacute", (char)237 },
{ "icirc" , (char)238 },
{ "iuml" , (char)239 },
{ "eth" , (char)240 },
{ "ntilde", (char)241 },
{ "ograve", (char)242 },
{ "oacute", (char)243 },
{ "ocirc" , (char)244 },
{ "otilde", (char)245 },
{ "ouml" , (char)246 },
{ "divide", (char)247 },
{ "oslash", (char)248 },
{ "ugrave", (char)249 },
{ "uacute", (char)250 },
{ "ucirc" , (char)251 },
{ "uuml" , (char)252 },
{ "yacute", (char)253 },
{ "thorn" , (char)254 },
{ "yuml" , (char)255 }
};
while (isalpha(*s) || isdigit(*s) || *s == '#')
s++;
for (i = 0; i < sizeof(refs)/sizeof(refs[0]); i++) {
if (strncmp(refs[i].name, t, s-t) == 0) {
c = refs[i].chr;
break;
}
}
if (*s == ';')
s++;
*ref = s;
return c;
}
|