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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Authors: Michael Zucchi <notzed@ximian.com>
* Jeffrey Stedfast <fejj@ximian.com>
*
* Copyright 2000-2004 Ximian, Inc. (www.ximian.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <glib.h>
#ifdef G_OS_WIN32
#include <process.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h> /* for MAXHOSTNAMELEN */
#else
#define MAXHOSTNAMELEN 64
#endif
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#include <ctype.h>
#include <errno.h>
#include "gmime-utils.h"
#include "gmime-table-private.h"
#include "gmime-part.h"
#include "gmime-charset.h"
#include "gmime-iconv.h"
#include "gmime-iconv-utils.h"
#define d(x)
#define w(x) x
#ifndef HAVE_ISBLANK
#define isblank(c) (c == ' ' || c == '\t')
#endif
#define GMIME_UUENCODE_CHAR(c) ((c) ? (c) + ' ' : '`')
#define GMIME_UUDECODE_CHAR(c) (((c) - ' ') & 077)
#define GMIME_FOLD_PREENCODED (GMIME_FOLD_LEN / 2)
/* date parser macros */
#define NUMERIC_CHARS "1234567890"
#define WEEKDAY_CHARS "SundayMondayTuesdayWednesdayThursdayFridaySaturday"
#define MONTH_CHARS "JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember"
#define TIMEZONE_ALPHA_CHARS "UTCGMTESTEDTCSTCDTMSTPSTPDTZAMNY()"
#define TIMEZONE_NUMERIC_CHARS "-+1234567890"
#define TIME_CHARS "1234567890:"
#define DATE_TOKEN_NON_NUMERIC (1 << 0)
#define DATE_TOKEN_NON_WEEKDAY (1 << 1)
#define DATE_TOKEN_NON_MONTH (1 << 2)
#define DATE_TOKEN_NON_TIME (1 << 3)
#define DATE_TOKEN_HAS_COLON (1 << 4)
#define DATE_TOKEN_NON_TIMEZONE_ALPHA (1 << 5)
#define DATE_TOKEN_NON_TIMEZONE_NUMERIC (1 << 6)
#define DATE_TOKEN_HAS_SIGN (1 << 7)
static char *base64_alphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static unsigned char tohex[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
static unsigned char gmime_base64_rank[256] = {
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255,
255, 0, 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,255,255,255,255,255,
255, 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,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
};
static unsigned char gmime_uu_rank[256] = {
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,
0, 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,
0, 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,
0, 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,
0, 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,
};
static unsigned char gmime_datetok_table[256] = {
128,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
111,111,111,111,111,111,111,111, 79, 79,111,175,111,175,111,111,
38, 38, 38, 38, 38, 38, 38, 38, 38, 38,119,111,111,111,111,111,
111, 75,111, 79, 75, 79,105, 79,111,111,107,111,111, 73, 75,107,
79,111,111, 73, 77, 79,111,109,111, 79, 79,111,111,111,111,111,
111,105,107,107,109,105,111,107,105,105,111,111,107,107,105,105,
107,111,105,105,105,105,107,111,111,105,111,111,111,111,111,111,
111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
};
/* hrm, is there a library for this shit? */
static struct {
char *name;
int offset;
} tz_offsets [] = {
{ "UT", 0 },
{ "GMT", 0 },
{ "EST", -500 }, /* these are all US timezones. bloody yanks */
{ "EDT", -400 },
{ "CST", -600 },
{ "CDT", -500 },
{ "MST", -700 },
{ "MDT", -600 },
{ "PST", -800 },
{ "PDT", -700 },
{ "Z", 0 },
{ "A", -100 },
{ "M", -1200 },
{ "N", 100 },
{ "Y", 1200 },
};
static char *tm_months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static char *tm_days[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
/**
* g_mime_utils_header_format_date:
* @time: time_t date representation
* @offset: Timezone offset
*
* Allocates a string buffer containing the rfc822 formatted date
* string represented by @time and @offset.
*
* Returns a valid string representation of the date.
**/
char *
g_mime_utils_header_format_date (time_t time, int offset)
{
struct tm tm;
time += ((offset / 100) * (60 * 60)) + (offset % 100) * 60;
#ifdef HAVE_GMTIME_R
gmtime_r (&time, &tm);
#else
memcpy (&tm, gmtime (&time), sizeof (tm));
#endif
return g_strdup_printf ("%s, %02d %s %04d %02d:%02d:%02d %+05d",
tm_days[tm.tm_wday], tm.tm_mday,
tm_months[tm.tm_mon],
tm.tm_year + 1900,
tm.tm_hour, tm.tm_min, tm.tm_sec,
offset);
}
/* This is where it gets ugly... */
struct _date_token {
struct _date_token *next;
const unsigned char *start;
unsigned int len;
unsigned int mask;
};
static struct _date_token *
datetok (const char *date)
{
struct _date_token *tokens = NULL, *token, *tail = (struct _date_token *) &tokens;
const unsigned char *start, *end;
unsigned int mask;
start = date;
while (*start) {
/* kill leading whitespace */
while (*start && isspace ((int) *start))
start++;
if (*start == '\0')
break;
mask = gmime_datetok_table[*start];
/* find the end of this token */
end = start + 1;
while (*end && !strchr ("-/,\t\r\n ", *end))
mask |= gmime_datetok_table[*end++];
if (end != start) {
token = g_malloc (sizeof (struct _date_token));
token->next = NULL;
token->start = start;
token->len = end - start;
token->mask = mask;
tail->next = token;
tail = token;
}
if (*end)
start = end + 1;
else
break;
}
return tokens;
}
static int
decode_int (const unsigned char *in, unsigned int inlen)
{
register const unsigned char *inptr;
const unsigned char *inend;
int sign = 1, val = 0;
inptr = in;
inend = in + inlen;
if (*inptr == '-') {
sign = -1;
inptr++;
} else if (*inptr == '+')
inptr++;
for ( ; inptr < inend; inptr++) {
if (!isdigit ((int) *inptr))
return -1;
else
val = (val * 10) + (*inptr - '0');
}
val *= sign;
return val;
}
#if 0
static int
get_days_in_month (int month, int year)
{
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (g_date_is_leap_year (year))
return 29;
else
return 28;
default:
return 0;
}
}
#endif
static int
get_wday (const unsigned char *in, unsigned int inlen)
{
int wday;
g_return_val_if_fail (in != NULL, -1);
if (inlen < 3)
return -1;
for (wday = 0; wday < 7; wday++)
if (!g_strncasecmp (in, tm_days[wday], 3))
return wday;
return -1; /* unknown week day */
}
static int
get_mday (const unsigned char *in, unsigned int inlen)
{
int mday;
g_return_val_if_fail (in != NULL, -1);
mday = decode_int (in, inlen);
if (mday < 0 || mday > 31)
mday = -1;
return mday;
}
static int
get_month (const unsigned char *in, unsigned int inlen)
{
int i;
g_return_val_if_fail (in != NULL, -1);
if (inlen < 3)
return -1;
for (i = 0; i < 12; i++)
if (!g_strncasecmp (in, tm_months[i], 3))
return i;
return -1; /* unknown month */
}
static int
get_year (const unsigned char *in, unsigned int inlen)
{
int year;
g_return_val_if_fail (in != NULL, -1);
year = decode_int (in, inlen);
if (year == -1)
return -1;
if (year < 100)
year += (year < 70) ? 2000 : 1900;
if (year < 1969)
return -1;
return year;
}
static gboolean
get_time (const unsigned char *in, unsigned int inlen, int *hour, int *min, int *sec)
{
register const unsigned char *inptr;
const unsigned char *inend;
int *val, colons = 0;
*hour = *min = *sec = 0;
inend = in + inlen;
val = hour;
for (inptr = in; inptr < inend; inptr++) {
if (*inptr == ':') {
colons++;
switch (colons) {
case 1:
val = min;
break;
case 2:
val = sec;
break;
default:
return FALSE;
}
} else if (!isdigit ((int) *inptr))
return FALSE;
else
*val = (*val * 10) + (*inptr - '0');
}
return TRUE;
}
static int
get_tzone (struct _date_token **token)
{
const unsigned char *inptr, *inend;
unsigned int inlen;
int i, t;
for (i = 0; *token && i < 2; *token = (*token)->next, i++) {
inptr = (*token)->start;
inlen = (*token)->len;
inend = inptr + inlen;
if (*inptr == '+' || *inptr == '-') {
return decode_int (inptr, inlen);
} else {
if (*inptr == '(') {
inptr++;
if (*(inend - 1) == ')')
inlen -= 2;
else
inlen--;
}
for (t = 0; t < 15; t++) {
unsigned int len = strlen (tz_offsets[t].name);
if (len != inlen)
continue;
if (!strncmp (inptr, tz_offsets[t].name, len))
return tz_offsets[t].offset;
}
}
}
return -1;
}
static time_t
parse_rfc822_date (struct _date_token *tokens, int *tzone)
{
int hour, min, sec, offset, n;
struct _date_token *token;
struct tm tm;
time_t t;
g_return_val_if_fail (tokens != NULL, (time_t) 0);
token = tokens;
memset ((void *) &tm, 0, sizeof (struct tm));
if ((n = get_wday (token->start, token->len)) != -1) {
/* not all dates may have this... */
tm.tm_wday = n;
token = token->next;
}
/* get the mday */
if (!token || (n = get_mday (token->start, token->len)) == -1)
return (time_t) 0;
tm.tm_mday = n;
token = token->next;
/* get the month */
if (!token || (n = get_month (token->start, token->len)) == -1)
return (time_t) 0;
tm.tm_mon = n;
token = token->next;
/* get the year */
if (!token || (n = get_year (token->start, token->len)) == -1)
return (time_t) 0;
tm.tm_year = n - 1900;
token = token->next;
/* get the hour/min/sec */
if (!token || !get_time (token->start, token->len, &hour, &min, &sec))
return (time_t) 0;
tm.tm_hour = hour;
tm.tm_min = min;
tm.tm_sec = sec;
token = token->next;
/* get the timezone */
if (!token || (n = get_tzone (&token)) == -1) {
/* I guess we assume tz is GMT? */
offset = 0;
} else {
offset = n;
}
t = mktime (&tm);
#if defined(HAVE_TIMEZONE)
t -= timezone;
#elif defined(HAVE_TM_GMTOFF)
t += tm.tm_gmtoff;
#else
#error Neither HAVE_TIMEZONE nor HAVE_TM_GMTOFF defined. Rerun autoheader, autoconf, etc.
#endif
/* t is now GMT of the time we want, but not offset by the timezone ... */
/* this should convert the time to the GMT equiv time */
t -= ((offset / 100) * 60 * 60) + (offset % 100) * 60;
if (tzone)
*tzone = offset;
return t;
}
#define date_token_mask(t) (((struct _date_token *) t)->mask)
#define is_numeric(t) ((date_token_mask (t) & DATE_TOKEN_NON_NUMERIC) == 0)
#define is_weekday(t) ((date_token_mask (t) & DATE_TOKEN_NON_WEEKDAY) == 0)
#define is_month(t) ((date_token_mask (t) & DATE_TOKEN_NON_MONTH) == 0)
#define is_time(t) (((date_token_mask (t) & DATE_TOKEN_NON_TIME) == 0) && (date_token_mask (t) & DATE_TOKEN_HAS_COLON))
#define is_tzone_alpha(t) ((date_token_mask (t) & DATE_TOKEN_NON_TIMEZONE_ALPHA) == 0)
#define is_tzone_numeric(t) (((date_token_mask (t) & DATE_TOKEN_NON_TIMEZONE_NUMERIC) == 0) && (date_token_mask (t) & DATE_TOKEN_HAS_SIGN))
#define is_tzone(t) (is_tzone_alpha (t) || is_tzone_numeric (t))
static time_t
parse_broken_date (struct _date_token *tokens, int *tzone)
{
gboolean got_wday, got_month, got_tzone;
int hour, min, sec, offset, n;
struct _date_token *token;
struct tm tm;
time_t time;
memset ((void *) &tm, 0, sizeof (struct tm));
got_wday = got_month = got_tzone = FALSE;
offset = 0;
token = tokens;
while (token) {
if (is_weekday (token) && !got_wday) {
if ((n = get_wday (token->start, token->len)) != -1) {
d(printf ("weekday; "));
got_wday = TRUE;
tm.tm_wday = n;
goto next_token;
}
}
if (is_month (token) && !got_month) {
if ((n = get_month (token->start, token->len)) != -1) {
d(printf ("month; "));
got_month = TRUE;
tm.tm_mon = n;
goto next_token;
}
}
if (is_time (token) && !tm.tm_hour && !tm.tm_min && !tm.tm_sec) {
if (get_time (token->start, token->len, &hour, &min, &sec)) {
d(printf ("time; "));
tm.tm_hour = hour;
tm.tm_min = min;
tm.tm_sec = sec;
goto next_token;
}
}
if (is_tzone (token) && !got_tzone) {
struct _date_token *t = token;
if ((n = get_tzone (&t)) != -1) {
d(printf ("tzone; "));
got_tzone = TRUE;
offset = n;
goto next_token;
}
}
if (is_numeric (token)) {
if (token->len == 4 && !tm.tm_year) {
if ((n = get_year (token->start, token->len)) != -1) {
d(printf ("year; "));
tm.tm_year = n - 1900;
goto next_token;
}
} else {
if (!got_month && !got_wday && token->next && is_numeric (token->next)) {
d(printf ("mon; "));
n = decode_int (token->start, token->len);
got_month = TRUE;
tm.tm_mon = n - 1;
goto next_token;
} else if (!tm.tm_mday && (n = get_mday (token->start, token->len)) != -1) {
d(printf ("mday; "));
tm.tm_mday = n;
goto next_token;
} else if (!tm.tm_year) {
d(printf ("2-digit year; "));
n = get_year (token->start, token->len);
tm.tm_year = n - 1900;
goto next_token;
}
}
}
d(printf ("???; "));
next_token:
token = token->next;
}
d(printf ("\n"));
time = mktime (&tm);
#if defined(HAVE_TIMEZONE)
time -= timezone;
#elif defined(HAVE_TM_GMTOFF)
time += tm.tm_gmtoff;
#else
#error Neither HAVE_TIMEZONE nor HAVE_TM_GMTOFF defined. Rerun autoheader, autoconf, etc.
#endif
/* t is now GMT of the time we want, but not offset by the timezone ... */
/* this should convert the time to the GMT equiv time */
time -= ((offset / 100) * 60 * 60) + (offset % 100) * 60;
if (tzone)
*tzone = offset;
return time;
}
#if 0
static void
gmime_datetok_table_init ()
{
int i;
memset (gmime_datetok_table, 0, sizeof (gmime_datetok_table));
for (i = 0; i < 256; i++) {
if (!strchr (NUMERIC_CHARS, i))
gmime_datetok_table[i] |= DATE_TOKEN_NON_NUMERIC;
if (!strchr (WEEKDAY_CHARS, i))
gmime_datetok_table[i] |= DATE_TOKEN_NON_WEEKDAY;
if (!strchr (MONTH_CHARS, i))
gmime_datetok_table[i] |= DATE_TOKEN_NON_MONTH;
if (!strchr (TIME_CHARS, i))
gmime_datetok_table[i] |= DATE_TOKEN_NON_TIME;
if (!strchr (TIMEZONE_ALPHA_CHARS, i))
gmime_datetok_table[i] |= DATE_TOKEN_NON_TIMEZONE_ALPHA;
if (!strchr (TIMEZONE_NUMERIC_CHARS, i))
gmime_datetok_table[i] |= DATE_TOKEN_NON_TIMEZONE_NUMERIC;
if (((char) i) == ':')
gmime_datetok_table[i] |= DATE_TOKEN_HAS_COLON;
if (strchr ("+-", i))
gmime_datetok_table[i] |= DATE_TOKEN_HAS_SIGN;
}
printf ("static unsigned char gmime_datetok_table[256] = {");
for (i = 0; i < 256; i++) {
if (i % 16 == 0)
printf ("\n\t");
printf ("%3d,", gmime_datetok_table[i]);
}
printf ("\n};\n");
}
#endif
/**
* g_mime_utils_header_decode_date:
* @in: input date string
* @saveoffset:
*
* Decodes the rfc822 date string and saves the GMT offset into
* @saveoffset if non-NULL.
*
* Returns the time_t representation of the date string specified by
* @in. If 'saveoffset' is non-NULL, the value of the timezone offset
* will be stored.
**/
time_t
g_mime_utils_header_decode_date (const char *in, int *saveoffset)
{
struct _date_token *token, *tokens;
time_t date;
tokens = datetok (in);
date = parse_rfc822_date (tokens, saveoffset);
if (!date)
date = parse_broken_date (tokens, saveoffset);
/* cleanup */
while (tokens) {
token = tokens;
tokens = tokens->next;
g_free (token);
}
return date;
}
/**
* g_mime_utils_generate_message_id:
* @fqdn: Fully qualified domain name
*
* Generates a unique Message-Id.
*
* Returns a unique string in an addr-spec format suitable for use as
* a Message-Id.
**/
char *
g_mime_utils_generate_message_id (const char *fqdn)
{
#ifdef G_THREADS_ENABLED
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
#define MUTEX_LOCK() g_static_mutex_lock (&mutex)
#define MUTEX_UNLOCK() g_static_mutex_unlock (&mutex)
#else
#define MUTEX_LOCK()
#define MUTEX_UNLOCK()
#endif
static unsigned int count = 0;
char host[MAXHOSTNAMELEN + 1];
#ifdef HAVE_GETHOSTBYNAME
struct hostent *h = NULL;
#endif
char *msgid;
if (!fqdn) {
#ifdef HAVE_GETHOSTNAME
if (gethostname (host, sizeof (host)) == 0) {
#ifdef HAVE_GETHOSTBYNAME
h = gethostbyname (host);
#endif /* HAVE_GETHOSTBYNAME */
} else {
host[0] = '\0';
}
#else
host[0] = '\0';
#endif /* HAVE_GETHOSTNAME */
#ifdef HAVE_GETHOSTBYNAME
fqdn = h ? h->h_name : (*host ? host : "localhost.localdomain");
#else
fqdn = *host ? host : "localhost.localdomain";
#endif
}
MUTEX_LOCK ();
msgid = g_strdup_printf ("%ul.%ul.%ul@%s", (unsigned int) time (NULL), getpid (), count++, fqdn);
MUTEX_UNLOCK ();
return msgid;
}
/* external symbols from internet-address.c */
extern void decode_lwsp (const char **in);
extern char *decode_word (const char **in);
extern char *decode_addrspec (const char **in);
static char *
decode_msgid (const char **in)
{
const char *inptr = *in;
char *msgid = NULL;
decode_lwsp (&inptr);
if (*inptr == '<') {
inptr++;
decode_lwsp (&inptr);
if ((msgid = decode_addrspec (&inptr))) {
decode_lwsp (&inptr);
if (*inptr != '>') {
w(g_warning ("Invalid msg-id; missing '>': %s", *in));
} else {
inptr++;
}
*in = inptr;
} else {
w(g_warning ("Invalid msg-id; missing addr-spec: %s", *in));
}
} else {
w(g_warning ("Invalid msg-id; missing '<': %s", *in));
}
return msgid;
}
/**
* g_mime_utils_decode_message_id:
* @message_id: string containing a message-id
*
* Decodes a msg-id as defined by rfc822.
*
* Returns the addr-spec portion of the msg-id.
**/
char *
g_mime_utils_decode_message_id (const char *message_id)
{
g_return_val_if_fail (message_id != NULL, NULL);
return decode_msgid (&message_id);
}
/**
* g_mime_references_decode:
* @text: string containing a list of msg-ids
*
* Decodes a list of msg-ids as in the References and/or In-Reply-To
* headers defined in rfc822.
*
* Returns a list of referenced msg-ids.
**/
GMimeReferences *
g_mime_references_decode (const char *text)
{
GMimeReferences *refs, *tail, *ref;
const char *inptr = text;
char *word, *msgid;
g_return_val_if_fail (text != NULL, NULL);
refs = NULL;
tail = (GMimeReferences *) &refs;
while (*inptr) {
decode_lwsp (&inptr);
if (*inptr == '<') {
/* looks like a msg-id */
if ((msgid = decode_msgid (&inptr))) {
ref = g_new (GMimeReferences, 1);
ref->next = NULL;
ref->msgid = msgid;
tail->next = ref;
tail = ref;
} else {
w(g_warning ("Invalid References header: %s", inptr));
break;
}
} else if (*inptr) {
/* looks like part of a phrase */
if ((word = decode_word (&inptr))) {
g_free (word);
} else {
w(g_warning ("Invalid References header: %s", inptr));
break;
}
}
}
return refs;
}
/**
* g_mime_references_append:
* @refs: the address of a GMimeReferences list
* @msgid: a message-id string
*
* Appends a reference to msgid to the list of references.
**/
void
g_mime_references_append (GMimeReferences **refs, const char *msgid)
{
GMimeReferences *ref;
g_return_if_fail (refs != NULL);
g_return_if_fail (msgid != NULL);
ref = (GMimeReferences *) refs;
while (ref->next)
ref = ref->next;
ref->next = g_new (GMimeReferences, 1);
ref->next->msgid = g_strdup (msgid);
ref->next->next = NULL;
}
/**
* g_mime_references_clear:
* @refs: address of a GMimeReferences list
*
* Clears the GMimeReferences list and resets it to %NULL.
**/
void
g_mime_references_clear (GMimeReferences **refs)
{
GMimeReferences *ref, *next;
g_return_if_fail (refs != NULL);
ref = *refs;
while (ref) {
next = ref->next;
g_free (ref->msgid);
g_free (ref);
ref = next;
}
*refs = NULL;
}
/**
* g_mime_utils_header_fold:
* @in: input header string
*
* Folds a header according to the rules in rfc822.
*
* Returns an allocated string containing the folded header.
**/
char *
g_mime_utils_header_fold (const char *in)
{
gboolean last_was_lwsp = FALSE;
register const char *inptr;
size_t len, outlen, i;
GString *out;
char *ret;
inptr = in;
len = strlen (in);
if (len <= GMIME_FOLD_LEN)
return g_strdup (in);
out = g_string_new ("");
outlen = 0;
while (*inptr) {
len = strcspn (inptr, " \t");
if (outlen + len > GMIME_FOLD_LEN) {
if (last_was_lwsp)
g_string_truncate (out, out->len - 1);
g_string_append (out, "\n\t");
outlen = 1;
/* check for very long words, just cut them up */
while (outlen + len > GMIME_FOLD_LEN) {
for (i = 0; i < GMIME_FOLD_LEN - outlen; i++)
g_string_append_c (out, inptr[i]);
inptr += GMIME_FOLD_LEN - outlen;
len -= GMIME_FOLD_LEN - outlen;
g_string_append (out, "\n\t");
outlen = 1;
}
last_was_lwsp = FALSE;
} else if (len > 0) {
outlen += len;
g_string_append_len (out, inptr, len);
inptr += len;
last_was_lwsp = FALSE;
} else {
if (*inptr == '\t') {
/* tabs are a good place to fold, odds
are that this is where the previous
mailer folded it */
g_string_append (out, "\n\t");
outlen = 1;
inptr++;
last_was_lwsp = FALSE;
} else {
g_string_append_c (out, *inptr++);
outlen++;
last_was_lwsp = TRUE;
}
}
}
ret = out->str;
g_string_free (out, FALSE);
return ret;
}
/**
* g_mime_utils_header_printf:
* @format: string format
* @Varargs: arguments
*
* Allocates a buffer containing a formatted header specified by the
* @Varargs.
*
* Returns an allocated string containing the folded header specified
* by @format and the following arguments.
**/
char *
g_mime_utils_header_printf (const char *format, ...)
{
char *buf, *ret;
va_list ap;
va_start (ap, format);
buf = g_strdup_vprintf (format, ap);
va_end (ap);
ret = g_mime_utils_header_fold (buf);
g_free (buf);
return ret;
}
static gboolean
need_quotes (const char *string)
{
gboolean quoted = FALSE;
const char *inptr;
inptr = string;
while (*inptr) {
if (*inptr == '\\')
inptr++;
else if (*inptr == '"')
quoted = !quoted;
else if (!quoted && (is_tspecial (*inptr) || *inptr == '.'))
return TRUE;
if (*inptr)
inptr++;
}
return FALSE;
}
/**
* g_mime_utils_quote_string:
* @string: input string
*
* Quotes @string as needed according to the rules in rfc2045.
*
* Returns an allocated string containing the escaped and quoted (if
* needed to be) input string. The decision to quote the string is
* based on whether or not the input string contains any 'tspecials'
* as defined by rfc2045.
**/
char *
g_mime_utils_quote_string (const char *string)
{
gboolean quote;
const char *c;
char *qstring;
GString *out;
out = g_string_new ("");
quote = need_quotes (string);
for (c = string; *c; c++) {
if ((*c == '"' && quote) || *c == '\\')
g_string_append_c (out, '\\');
g_string_append_c (out, *c);
}
if (quote) {
g_string_prepend_c (out, '"');
g_string_append_c (out, '"');
}
qstring = out->str;
g_string_free (out, FALSE);
return qstring;
}
/**
* g_mime_utils_unquote_string: Unquote a string.
* @string: string
*
* Unquotes and unescapes a string.
**/
void
g_mime_utils_unquote_string (char *string)
{
/* if the string is quoted, unquote it */
char *inptr, *inend;
if (!string)
return;
inptr = string;
inend = string + strlen (string);
/* get rid of the wrapping quotes */
if (*inptr == '"' && *(inend - 1) == '"') {
inend--;
*inend = '\0';
if (*inptr)
memmove (inptr, inptr + 1, inend - inptr);
}
/* un-escape the string */
inend--;
while (inptr < inend) {
if (*inptr == '\\') {
memmove (inptr, inptr + 1, inend - inptr);
inend--;
}
inptr++;
}
}
/**
* g_mime_utils_text_is_8bit:
* @text: text to check for 8bit chars
* @len: text length
*
* Determines if @text contains 8bit characters within the first @len
* bytes.
*
* Returns %TRUE if the text contains 8bit characters or %FALSE
* otherwise.
**/
gboolean
g_mime_utils_text_is_8bit (const unsigned char *text, size_t len)
{
const unsigned char *c, *inend;
g_return_val_if_fail (text != NULL, FALSE);
inend = text + len;
for (c = text; c < inend; c++)
if (*c > (unsigned char) 127)
return TRUE;
return FALSE;
}
/**
* g_mime_utils_best_encoding:
* @text: text to encode
* @len: text length
*
* Determines the best content encoding for the first @len bytes of
* @text.
*
* Returns a #GMimePartEncodingType that is determined to be the best
* encoding type for the specified block of text. ("best" in this
* particular case means best compression)
**/
GMimePartEncodingType
g_mime_utils_best_encoding (const unsigned char *text, size_t len)
{
const unsigned char *ch, *inend;
size_t count = 0;
inend = text + len;
for (ch = text; ch < inend; ch++)
if (*ch > (unsigned char) 127)
count++;
if ((float) count <= len * 0.17)
return GMIME_PART_ENCODING_QUOTEDPRINTABLE;
else
return GMIME_PART_ENCODING_BASE64;
}
/* this decodes rfc2047's version of quoted-printable */
static ssize_t
quoted_decode (const unsigned char *in, size_t len, unsigned char *out)
{
register const unsigned char *inptr;
register unsigned char *outptr;
const unsigned char *inend;
unsigned char c, c1;
inend = in + len;
outptr = out;
inptr = in;
while (inptr < inend) {
c = *inptr++;
if (c == '=') {
if (inend - inptr >= 2) {
c = toupper (*inptr++);
c1 = toupper (*inptr++);
*outptr++ = (((c >= 'A' ? c - 'A' + 10 : c - '0') & 0x0f) << 4)
| ((c1 >= 'A' ? c1 - 'A' + 10 : c1 - '0') & 0x0f);
} else {
/* data was truncated */
return -1;
}
} else if (c == '_') {
/* _'s are an rfc2047 shortcut for encoding spaces */
*outptr++ = ' ';
} else {
*outptr++ = c;
}
}
return (outptr - out);
}
#define is_rfc2047_encoded_word(atom, len) (len >= 7 && !strncmp (atom, "=?", 2) && !strncmp (atom + len - 2, "?=", 2))
static unsigned char *
rfc2047_decode_word (const unsigned char *in, size_t inlen)
{
const register unsigned char *inptr;
const unsigned char *inend;
const char *charset;
unsigned char *buf;
char *charenc, *p;
size_t len;
iconv_t cd;
inptr = in + 2;
inend = in + inlen - 2;
inptr = memchr (inptr, '?', inend - inptr);
if (inptr && inptr[2] == '?') {
unsigned char *decoded;
ssize_t declen;
int state = 0;
int save = 0;
inptr++;
switch (*inptr) {
case 'B':
case 'b':
inptr += 2;
decoded = g_alloca (inend - inptr);
declen = g_mime_utils_base64_decode_step (inptr, inend - inptr, decoded, &state, &save);
break;
case 'Q':
case 'q':
inptr += 2;
decoded = g_alloca (inend - inptr);
declen = quoted_decode (inptr, inend - inptr, decoded);
if (declen == -1) {
d(fprintf (stderr, "encountered broken 'Q' encoding\n"));
return NULL;
}
break;
default:
d(fprintf (stderr, "unknown encoding\n"));
return NULL;
}
len = (inptr - 3) - (in + 2);
charenc = g_alloca (len + 1);
memcpy (charenc, in + 2, len);
charenc[len] = '\0';
charset = charenc;
/* rfc2231 updates rfc2047 encoded words...
* The ABNF given in RFC 2047 for encoded-words is:
* encoded-word := "=?" charset "?" encoding "?" encoded-text "?="
* This specification changes this ABNF to:
* encoded-word := "=?" charset ["*" language] "?" encoding "?" encoded-text "?="
*/
/* trim off the 'language' part if it's there... */
p = strchr (charset, '*');
if (p)
*p = '\0';
/* slight optimization */
if (!g_strcasecmp (charset, "UTF-8"))
return g_strndup (decoded, declen);
cd = g_mime_iconv_open ("UTF-8", charset);
if (cd == (iconv_t) -1) {
w(g_warning ("Cannot convert from %s to UTF-8, header display may "
"be corrupt: %s", charset, g_strerror (errno)));
charset = g_mime_charset_locale_name ();
cd = g_mime_iconv_open ("UTF-8", charset);
if (cd == (iconv_t) -1)
return NULL;
}
buf = g_mime_iconv_strndup (cd, decoded, declen);
g_mime_iconv_close (cd);
if (!buf) {
w(g_warning ("Failed to convert \"%.*s\" to UTF-8, display may be "
"corrupt: %s", declen, decoded, g_strerror (errno)));
}
return buf;
}
return NULL;
}
/**
* g_mime_utils_8bit_header_decode:
* @in: header to decode
*
* Decodes and rfc2047 encoded header.
*
* Returns the mime encoded header as 8bit text.
**/
char *
g_mime_utils_8bit_header_decode (const unsigned char *in)
{
GString *out, *lwsp, *atom;
const unsigned char *inptr;
unsigned char *decoded;
gboolean last_was_encoded = FALSE;
gboolean last_was_space = FALSE;
out = g_string_sized_new (256);
lwsp = g_string_sized_new (256);
atom = g_string_sized_new (256);
inptr = in;
while (inptr && *inptr) {
unsigned char c = *inptr++;
if (!is_atom (c) && !last_was_space) {
/* we reached the end of an atom */
unsigned char *dword = NULL;
const unsigned char *word;
gboolean was_encoded;
if ((was_encoded = is_rfc2047_encoded_word (atom->str, atom->len)))
word = dword = rfc2047_decode_word (atom->str, atom->len);
else
word = atom->str;
if (word) {
if (!(last_was_encoded && was_encoded)) {
/* rfc2047 states that you
must ignore all whitespace
between encoded words */
g_string_append (out, lwsp->str);
}
g_string_append (out, word);
g_free (dword);
} else {
was_encoded = FALSE;
g_string_append (out, lwsp->str);
g_string_append (out, atom->str);
}
last_was_encoded = was_encoded;
g_string_truncate (lwsp, 0);
g_string_truncate (atom, 0);
if (is_lwsp (c)) {
g_string_append_c (lwsp, c);
last_was_space = TRUE;
} else {
/* This is mostly here for interoperability with broken
mailers that might do something stupid like:
=?iso-8859-1?Q?blah?=:\t=?iso-8859-1?Q?I_am_broken?= */
g_string_append_c (out, c);
last_was_encoded = FALSE;
last_was_space = FALSE;
}
continue;
}
if (is_atom (c)) {
g_string_append_c (atom, c);
last_was_space = FALSE;
} else {
g_string_append_c (lwsp, c);
last_was_space = TRUE;
}
}
if (atom->len || lwsp->len) {
unsigned char *dword = NULL;
const unsigned char *word;
gboolean was_encoded;
if ((was_encoded = is_rfc2047_encoded_word (atom->str, atom->len)))
word = dword = rfc2047_decode_word (atom->str, atom->len);
else
word = atom->str;
if (word) {
if (!(last_was_encoded && was_encoded)) {
/* rfc2047 states that you
must ignore all whitespace
between encoded words */
g_string_append (out, lwsp->str);
}
g_string_append (out, word);
g_free (dword);
} else {
g_string_append (out, lwsp->str);
g_string_append (out, atom->str);
}
}
g_string_free (lwsp, TRUE);
g_string_free (atom, TRUE);
decoded = out->str;
g_string_free (out, FALSE);
return (char *) decoded;
}
/* rfc2047 version of quoted-printable */
static size_t
quoted_encode (const unsigned char *in, size_t len, unsigned char *out, gushort safemask)
{
register const unsigned char *inptr;
register unsigned char *outptr;
const unsigned char *inend;
unsigned char c;
inptr = in;
inend = in + len;
outptr = out;
while (inptr < inend) {
c = *inptr++;
if (c == ' ') {
*outptr++ = '_';
} else if (gmime_special_table[c] & safemask) {
*outptr++ = c;
} else {
*outptr++ = '=';
*outptr++ = tohex[(c >> 4) & 0xf];
*outptr++ = tohex[c & 0xf];
}
}
return (outptr - out);
}
static void
rfc2047_encode_word (GString *string, const unsigned char *word, size_t len,
const char *charset, gushort safemask)
{
unsigned char *encoded, *ptr;
unsigned char *uword = NULL;
iconv_t cd = (iconv_t) -1;
size_t enclen, pos;
int state = 0;
int save = 0;
char encoding;
if (g_strcasecmp (charset, "UTF-8") != 0)
cd = g_mime_iconv_open (charset, "UTF-8");
if (cd != (iconv_t) -1) {
uword = g_mime_iconv_strndup (cd, word, len);
g_mime_iconv_close (cd);
}
if (uword) {
len = strlen (uword);
word = uword;
} else {
charset = "UTF-8";
}
switch (g_mime_utils_best_encoding (word, len)) {
case GMIME_PART_ENCODING_BASE64:
enclen = BASE64_ENCODE_LEN (len);
encoded = g_alloca (enclen);
encoding = 'b';
pos = g_mime_utils_base64_encode_close (word, len, encoded, &state, &save);
encoded[pos] = '\0';
/* remove \n chars as headers need to be wrapped differently */
ptr = encoded;
while ((ptr = memchr (ptr, '\n', strlen (ptr))))
memmove (ptr, ptr + 1, strlen (ptr));
break;
case GMIME_PART_ENCODING_QUOTEDPRINTABLE:
enclen = QP_ENCODE_LEN (len);
encoded = g_alloca (enclen);
encoding = 'q';
pos = quoted_encode (word, len, encoded, safemask);
encoded[pos] = '\0';
break;
default:
encoded = NULL;
encoding = '\0';
g_assert_not_reached ();
}
g_free (uword);
g_string_append_printf (string, "=?%s?%c?%s?=", charset, encoding, encoded);
}
/**
* g_mime_utils_8bit_header_encode_phrase:
* @in: header to encode
*
* Encodes a header phrase according to the rules in rfc2047.
*
* Returns the header phrase as 1 encoded atom. Useful for encoding
* internet addresses.
**/
char *
g_mime_utils_8bit_header_encode_phrase (const unsigned char *in)
{
const char *charset;
GString *string;
size_t len;
char *str;
if (in == NULL)
return NULL;
len = strlen (in);
charset = g_mime_charset_best (in, len);
charset = charset ? charset : "iso-8859-1";
string = g_string_new ("");
rfc2047_encode_word (string, in, strlen (in), charset, IS_ESAFE);
str = string->str;
g_string_free (string, FALSE);
return str;
}
enum _phrase_word_t {
WORD_ATOM,
WORD_2047
};
struct _phrase_word {
struct _phrase_word *next;
const unsigned char *start, *end;
enum _phrase_word_t type;
int encoding;
};
static gboolean
word_types_compatable (enum _phrase_word_t type1, enum _phrase_word_t type2)
{
switch (type1) {
case WORD_ATOM:
return FALSE;
case WORD_2047:
return type2 == WORD_2047;
default:
return FALSE;
}
}
static struct _phrase_word *
rfc2047_encode_phrase_get_words (const unsigned char *in)
{
const unsigned char *inptr, *start, *last;
struct _phrase_word *words, *tail, *word;
enum _phrase_word_t type = WORD_ATOM;
int count = 0, encoding = 0;
words = NULL;
tail = (struct _phrase_word *) &words;
last = start = inptr = in;
while (inptr && *inptr) {
const char *newinptr;
gunichar c;
newinptr = g_utf8_next_char (inptr);
c = g_utf8_get_char (inptr);
if (newinptr == NULL || !g_unichar_validate (c)) {
w(g_warning ("Invalid UTF-8 sequence encountered"));
inptr++;
continue;
}
inptr = newinptr;
if (g_unichar_isspace (c)) {
if (count > 0) {
word = g_new (struct _phrase_word, 1);
word->next = NULL;
word->start = start;
word->end = last;
word->type = type;
word->encoding = encoding;
tail->next = word;
tail = word;
count = 0;
}
start = inptr;
type = WORD_ATOM;
encoding = 0;
} else {
count++;
if (c > 127 && c < 256) {
type = WORD_2047;
encoding = MAX (encoding, 2);
} else if (c >= 256) {
type = WORD_2047;
encoding = 2;
}
}
last = inptr;
}
if (count > 0) {
word = g_new (struct _phrase_word, 1);
word->next = NULL;
word->start = start;
word->end = last;
word->type = type;
word->encoding = encoding;
tail->next = word;
tail = word;
}
return words;
}
static gboolean
rfc2047_encode_phrase_merge_words (struct _phrase_word **wordsp)
{
struct _phrase_word *word, *next, *words = *wordsp;
gboolean merged = FALSE;
/* scan the list, checking for words of similar types that can be merged */
word = words;
while (word) {
next = word->next;
while (next) {
/* merge nodes of the same type AND we are not creating too long a string */
if (word_types_compatable (word->type, next->type)) {
if (next->end - word->start < GMIME_FOLD_PREENCODED) {
/* the resulting word type is the MAX of the 2 types */
word->type = MAX (word->type, next->type);
word->end = next->end;
word->next = next->next;
g_free (next);
next = word->next;
merged = TRUE;
} else {
/* if it is going to be too long, make sure we include the
separating whitespace */
word->end = next->start;
break;
}
} else {
break;
}
}
word = word->next;
}
*wordsp = words;
return merged;
}
static char *
rfc2047_encode_phrase (const unsigned char *in)
{
struct _phrase_word *words, *word, *prev = NULL;
GString *out;
char *outstr;
if (in == NULL)
return NULL;
words = rfc2047_encode_phrase_get_words (in);
if (!words)
return NULL;
while (rfc2047_encode_phrase_merge_words (&words))
;
out = g_string_new ("");
/* output words now with spaces between them */
word = words;
while (word) {
const char *start;
size_t len;
/* append correct number of spaces between words */
if (prev && !(prev->type == WORD_2047 && word->type == WORD_2047)) {
/* one or both of the words are not encoded so we write the spaces out untouched */
len = word->start - prev->end;
g_string_append_len (out, prev->end, len);
}
switch (word->type) {
case WORD_ATOM:
g_string_append_len (out, word->start, word->end - word->start);
break;
case WORD_2047:
if (prev && prev->type == WORD_2047) {
/* include the whitespace chars between these 2 words in the
resulting rfc2047 encoded word. */
len = word->end - prev->end;
start = prev->end;
/* encoded words need to be separated by linear whitespace */
g_string_append_c (out, ' ');
} else {
len = word->end - word->start;
start = word->start;
}
if (word->encoding == 1)
rfc2047_encode_word (out, start, len, "iso-8859-1", IS_PSAFE);
else
rfc2047_encode_word (out, start, len,
g_mime_charset_best (start, len), IS_PSAFE);
break;
}
g_free (prev);
prev = word;
word = word->next;
}
g_free (prev);
outstr = out->str;
g_string_free (out, FALSE);
return outstr;
}
/**
* g_mime_utils_8bit_header_encode:
* @in: header to encode
*
* Encodes a header according to the rules in rfc2047.
*
* Returns the header as several encoded atoms. Useful for encoding
* headers like "Subject".
**/
char *
g_mime_utils_8bit_header_encode (const unsigned char *in)
{
return rfc2047_encode_phrase (in);
}
/**
* g_mime_utils_base64_encode_close:
* @in: input stream
* @inlen: length of the input
* @out: output string
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been encoded
*
* Base64 encodes the input stream to the output stream. Call this
* when finished encoding data with #g_mime_utils_base64_encode_step
* to flush off the last little bit.
*
* Returns the number of bytes encoded.
**/
size_t
g_mime_utils_base64_encode_close (const unsigned char *in, size_t inlen, unsigned char *out, int *state, guint32 *save)
{
unsigned char *outptr = out;
int c1, c2;
if (inlen > 0)
outptr += g_mime_utils_base64_encode_step (in, inlen, outptr, state, save);
c1 = ((unsigned char *)save)[1];
c2 = ((unsigned char *)save)[2];
switch (((unsigned char *)save)[0]) {
case 2:
outptr[2] = base64_alphabet [(c2 & 0x0f) << 2];
goto skip;
case 1:
outptr[2] = '=';
skip:
outptr[0] = base64_alphabet [c1 >> 2];
outptr[1] = base64_alphabet [c2 >> 4 | ((c1 & 0x3) << 4)];
outptr[3] = '=';
outptr += 4;
break;
}
*outptr++ = '\n';
*save = 0;
*state = 0;
return (outptr - out);
}
/**
* g_mime_utils_base64_encode_step:
* @in: input stream
* @inlen: length of the input
* @out: output string
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been encoded
*
* Base64 encodes a chunk of data. Performs an 'encode step', only
* encodes blocks of 3 characters to the output at a time, saves
* left-over state in state and save (initialise to 0 on first
* invocation).
*
* Returns the number of bytes encoded.
**/
size_t
g_mime_utils_base64_encode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, guint32 *save)
{
const register unsigned char *inptr;
register unsigned char *outptr;
if (inlen <= 0)
return 0;
inptr = in;
outptr = out;
if (inlen + ((unsigned char *)save)[0] > 2) {
const unsigned char *inend = in + inlen - 2;
register int c1 = 0, c2 = 0, c3 = 0;
register int already;
already = *state;
switch (((char *)save)[0]) {
case 1: c1 = ((unsigned char *)save)[1]; goto skip1;
case 2: c1 = ((unsigned char *)save)[1];
c2 = ((unsigned char *)save)[2]; goto skip2;
}
/* yes, we jump into the loop, no i'm not going to change it, its beautiful! */
while (inptr < inend) {
c1 = *inptr++;
skip1:
c2 = *inptr++;
skip2:
c3 = *inptr++;
*outptr++ = base64_alphabet [c1 >> 2];
*outptr++ = base64_alphabet [(c2 >> 4) | ((c1 & 0x3) << 4)];
*outptr++ = base64_alphabet [((c2 & 0x0f) << 2) | (c3 >> 6)];
*outptr++ = base64_alphabet [c3 & 0x3f];
/* this is a bit ugly ... */
if ((++already) >= 19) {
*outptr++ = '\n';
already = 0;
}
}
((unsigned char *)save)[0] = 0;
inlen = 2 - (inptr - inend);
*state = already;
}
d(printf ("state = %d, inlen = %d\n", (int)((char *)save)[0], inlen));
if (inlen > 0) {
register char *saveout;
/* points to the slot for the next char to save */
saveout = & (((char *)save)[1]) + ((char *)save)[0];
/* inlen can only be 0 1 or 2 */
switch (inlen) {
case 2: *saveout++ = *inptr++;
case 1: *saveout++ = *inptr++;
}
((char *)save)[0] += inlen;
}
d(printf ("mode = %d\nc1 = %c\nc2 = %c\n",
(int)((char *)save)[0],
(int)((char *)save)[1],
(int)((char *)save)[2]));
return (outptr - out);
}
/**
* g_mime_utils_base64_decode_step:
* @in: input stream
* @inlen: max length of data to decode
* @out: output stream
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been decoded
*
* Decodes a chunk of base64 encoded data.
*
* Returns the number of bytes decoded (which have been dumped in @out).
**/
size_t
g_mime_utils_base64_decode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, guint32 *save)
{
const register unsigned char *inptr;
register unsigned char *outptr;
const unsigned char *inend;
register guint32 saved;
unsigned char c;
int i;
inend = in + inlen;
outptr = out;
/* convert 4 base64 bytes to 3 normal bytes */
saved = *save;
i = *state;
inptr = in;
while (inptr < inend) {
c = gmime_base64_rank[*inptr++];
if (c != 0xff) {
saved = (saved << 6) | c;
i++;
if (i == 4) {
*outptr++ = saved >> 16;
*outptr++ = saved >> 8;
*outptr++ = saved;
i = 0;
}
}
}
*save = saved;
*state = i;
/* quick scan back for '=' on the end somewhere */
/* fortunately we can drop 1 output char for each trailing = (upto 2) */
i = 2;
while (inptr > in && i) {
inptr--;
if (gmime_base64_rank[*inptr] != 0xff) {
if (*inptr == '=' && outptr > out)
outptr--;
i--;
}
}
/* if i != 0 then there is a truncation error! */
return (outptr - out);
}
/**
* g_mime_utils_uuencode_close:
* @in: input stream
* @inlen: input stream length
* @out: output stream
* @uubuf: temporary buffer of 60 bytes
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been encoded
*
* Uuencodes a chunk of data. Call this when finished encoding data
* with #g_mime_utils_uuencode_step to flush off the last little bit.
*
* Returns the number of bytes encoded.
**/
size_t
g_mime_utils_uuencode_close (const unsigned char *in, size_t inlen, unsigned char *out, unsigned char *uubuf, int *state, guint32 *save)
{
register unsigned char *outptr, *bufptr;
register guint32 saved;
int uulen, uufill, i;
outptr = out;
if (inlen > 0)
outptr += g_mime_utils_uuencode_step (in, inlen, out, uubuf, state, save);
uufill = 0;
saved = *save;
i = *state & 0xff;
uulen = (*state >> 8) & 0xff;
bufptr = uubuf + ((uulen / 3) * 4);
if (i > 0) {
while (i < 3) {
saved <<= 8 | 0;
uufill++;
i++;
}
if (i == 3) {
/* convert 3 normal bytes into 4 uuencoded bytes */
unsigned char b0, b1, b2;
b0 = saved >> 16;
b1 = saved >> 8 & 0xff;
b2 = saved & 0xff;
*bufptr++ = GMIME_UUENCODE_CHAR ((b0 >> 2) & 0x3f);
*bufptr++ = GMIME_UUENCODE_CHAR (((b0 << 4) | ((b1 >> 4) & 0xf)) & 0x3f);
*bufptr++ = GMIME_UUENCODE_CHAR (((b1 << 2) | ((b2 >> 6) & 0x3)) & 0x3f);
*bufptr++ = GMIME_UUENCODE_CHAR (b2 & 0x3f);
i = 0;
saved = 0;
uulen += 3;
}
}
if (uulen > 0) {
int cplen = ((uulen / 3) * 4);
*outptr++ = GMIME_UUENCODE_CHAR ((uulen - uufill) & 0xff);
memcpy (outptr, uubuf, cplen);
outptr += cplen;
*outptr++ = '\n';
uulen = 0;
}
*outptr++ = GMIME_UUENCODE_CHAR (uulen & 0xff);
*outptr++ = '\n';
*save = 0;
*state = 0;
return (outptr - out);
}
/**
* g_mime_utils_uuencode_step:
* @in: input stream
* @inlen: input stream length
* @out: output stream
* @uubuf: temporary buffer of 60 bytes
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been encoded
*
* Uuencodes a chunk of data. Performs an 'encode step', only encodes
* blocks of 45 characters to the output at a time, saves left-over
* state in @uubuf, @state and @save (initialize to 0 on first
* invocation).
*
* Returns the number of bytes encoded.
**/
size_t
g_mime_utils_uuencode_step (const unsigned char *in, size_t inlen, unsigned char *out, unsigned char *uubuf, int *state, guint32 *save)
{
register unsigned char *outptr, *bufptr;
const register unsigned char *inptr;
const unsigned char *inend;
register guint32 saved;
int uulen, i;
saved = *save;
i = *state & 0xff;
uulen = (*state >> 8) & 0xff;
inptr = in;
inend = in + inlen;
outptr = out;
bufptr = uubuf + ((uulen / 3) * 4);
while (inptr < inend) {
while (uulen < 45 && inptr < inend) {
while (i < 3 && inptr < inend) {
saved = (saved << 8) | *inptr++;
i++;
}
if (i == 3) {
/* convert 3 normal bytes into 4 uuencoded bytes */
unsigned char b0, b1, b2;
b0 = saved >> 16;
b1 = saved >> 8 & 0xff;
b2 = saved & 0xff;
*bufptr++ = GMIME_UUENCODE_CHAR ((b0 >> 2) & 0x3f);
*bufptr++ = GMIME_UUENCODE_CHAR (((b0 << 4) | ((b1 >> 4) & 0xf)) & 0x3f);
*bufptr++ = GMIME_UUENCODE_CHAR (((b1 << 2) | ((b2 >> 6) & 0x3)) & 0x3f);
*bufptr++ = GMIME_UUENCODE_CHAR (b2 & 0x3f);
i = 0;
saved = 0;
uulen += 3;
}
}
if (uulen >= 45) {
*outptr++ = GMIME_UUENCODE_CHAR (uulen & 0xff);
memcpy (outptr, uubuf, ((uulen / 3) * 4));
outptr += ((uulen / 3) * 4);
*outptr++ = '\n';
uulen = 0;
bufptr = uubuf;
}
}
*save = saved;
*state = ((uulen & 0xff) << 8) | (i & 0xff);
return (outptr - out);
}
/**
* g_mime_utils_uudecode_step:
* @in: input stream
* @inlen: max length of data to decode
* @out: output stream
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been decoded
*
* Uudecodes a chunk of data. Performs a 'decode step' on a chunk of
* uuencoded data. Assumes the "begin <mode> <file name>" line has
* been stripped off.
*
* Returns the number of bytes decoded.
**/
size_t
g_mime_utils_uudecode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, guint32 *save)
{
const register unsigned char *inptr;
register unsigned char *outptr;
const unsigned char *inend;
unsigned char ch;
register guint32 saved;
gboolean last_was_eoln;
int uulen, i;
if (*state & GMIME_UUDECODE_STATE_END)
return 0;
saved = *save;
i = *state & 0xff;
uulen = (*state >> 8) & 0xff;
if (uulen == 0)
last_was_eoln = TRUE;
else
last_was_eoln = FALSE;
inend = in + inlen;
outptr = out;
inptr = in;
while (inptr < inend) {
if (*inptr == '\n') {
last_was_eoln = TRUE;
inptr++;
continue;
} else if (!uulen || last_was_eoln) {
/* first octet on a line is the uulen octet */
uulen = gmime_uu_rank[*inptr];
last_was_eoln = FALSE;
if (uulen == 0) {
*state |= GMIME_UUDECODE_STATE_END;
break;
}
inptr++;
continue;
}
ch = *inptr++;
if (uulen > 0) {
/* save the byte */
saved = (saved << 8) | ch;
i++;
if (i == 4) {
/* convert 4 uuencoded bytes to 3 normal bytes */
unsigned char b0, b1, b2, b3;
b0 = saved >> 24;
b1 = saved >> 16 & 0xff;
b2 = saved >> 8 & 0xff;
b3 = saved & 0xff;
if (uulen >= 3) {
*outptr++ = gmime_uu_rank[b0] << 2 | gmime_uu_rank[b1] >> 4;
*outptr++ = gmime_uu_rank[b1] << 4 | gmime_uu_rank[b2] >> 2;
*outptr++ = gmime_uu_rank[b2] << 6 | gmime_uu_rank[b3];
} else {
if (uulen >= 1) {
*outptr++ = gmime_uu_rank[b0] << 2 | gmime_uu_rank[b1] >> 4;
}
if (uulen >= 2) {
*outptr++ = gmime_uu_rank[b1] << 4 | gmime_uu_rank[b2] >> 2;
}
}
i = 0;
saved = 0;
uulen -= 3;
}
} else {
break;
}
}
*save = saved;
*state = (*state & GMIME_UUDECODE_STATE_MASK) | ((uulen & 0xff) << 8) | (i & 0xff);
return (outptr - out);
}
/**
* g_mime_utils_quoted_encode_close:
* @in: input stream
* @inlen: length of the input
* @out: output string
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been encoded
*
* Quoted-printable encodes a block of text. Call this when finished
* encoding data with #g_mime_utils_quoted_encode_step to flush off
* the last little bit.
*
* Returns the number of bytes encoded.
**/
size_t
g_mime_utils_quoted_encode_close (const unsigned char *in, size_t inlen, unsigned char *out, int *state, int *save)
{
register unsigned char *outptr = out;
int last;
if (inlen > 0)
outptr += g_mime_utils_quoted_encode_step (in, inlen, outptr, state, save);
last = *state;
if (last != -1) {
/* space/tab must be encoded if its the last character on
the line */
if (is_qpsafe (last) && !isblank (last)) {
*outptr++ = last;
} else {
*outptr++ = '=';
*outptr++ = tohex[(last >> 4) & 0xf];
*outptr++ = tohex[last & 0xf];
}
}
*outptr++ = '\n';
*save = 0;
*state = -1;
return (outptr - out);
}
/**
* g_mime_utils_quoted_encode_step:
* @in: input stream
* @inlen: length of the input
* @out: output string
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been encoded
*
* Quoted-printable encodes a block of text. Performs an 'encode
* step', saves left-over state in state and save (initialise to -1 on
* first invocation).
*
* Returns the number of bytes encoded.
**/
size_t
g_mime_utils_quoted_encode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, int *save)
{
const register unsigned char *inptr, *inend;
register unsigned char *outptr;
unsigned char c;
register int sofar = *save; /* keeps track of how many chars on a line */
register int last = *state; /* keeps track if last char to end was a space cr etc */
inptr = in;
inend = in + inlen;
outptr = out;
while (inptr < inend) {
c = *inptr++;
if (c == '\r') {
if (last != -1) {
*outptr++ = '=';
*outptr++ = tohex[(last >> 4) & 0xf];
*outptr++ = tohex[last & 0xf];
sofar += 3;
}
last = c;
} else if (c == '\n') {
if (last != -1 && last != '\r') {
*outptr++ = '=';
*outptr++ = tohex[(last >> 4) & 0xf];
*outptr++ = tohex[last & 0xf];
}
*outptr++ = '\n';
sofar = 0;
last = -1;
} else {
if (last != -1) {
if (is_qpsafe (last)) {
*outptr++ = last;
sofar++;
} else {
*outptr++ = '=';
*outptr++ = tohex[(last >> 4) & 0xf];
*outptr++ = tohex[last & 0xf];
sofar += 3;
}
}
if (is_qpsafe (c)) {
if (sofar > 74) {
*outptr++ = '=';
*outptr++ = '\n';
sofar = 0;
}
/* delay output of space char */
if (isblank (c)) {
last = c;
} else {
*outptr++ = c;
sofar++;
last = -1;
}
} else {
if (sofar > 72) {
*outptr++ = '=';
*outptr++ = '\n';
sofar = 3;
} else
sofar += 3;
*outptr++ = '=';
*outptr++ = tohex[(c >> 4) & 0xf];
*outptr++ = tohex[c & 0xf];
last = -1;
}
}
}
*save = sofar;
*state = last;
return (outptr - out);
}
/**
* g_mime_utils_quoted_decode_step: decode a chunk of QP encoded data
* @in: input stream
* @inlen: max length of data to decode
* @out: output stream
* @savestate: holds the number of bits that are stored in @save
* @saved: leftover bits that have not yet been decoded
*
* Decodes a block of quoted-printable encoded data. Performs a
* 'decode step' on a chunk of QP encoded data.
*
* Returns the number of bytes decoded.
**/
size_t
g_mime_utils_quoted_decode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *savestate, int *saved)
{
/* FIXME: this does not strip trailing spaces from lines (as
* it should, rfc 2045, section 6.7) Should it also
* canonicalise the end of line to CR LF??
*
* Note: Trailing rubbish (at the end of input), like = or =x
* or =\r will be lost.
*/
const register unsigned char *inptr;
register unsigned char *outptr;
const unsigned char *inend;
unsigned char c;
int state, save;
inend = in + inlen;
outptr = out;
d(printf ("quoted-printable, decoding text '%.*s'\n", inlen, in));
state = *savestate;
save = *saved;
inptr = in;
while (inptr < inend) {
switch (state) {
case 0:
while (inptr < inend) {
c = *inptr++;
/* FIXME: use a specials table to avoid 3 comparisons for the common case */
if (c == '=') {
state = 1;
break;
}
#ifdef CANONICALISE_EOL
/*else if (c=='\r') {
state = 3;
} else if (c=='\n') {
*outptr++ = '\r';
*outptr++ = c;
} */
#endif
else {
*outptr++ = c;
}
}
break;
case 1:
c = *inptr++;
if (c == '\n') {
/* soft break ... unix end of line */
state = 0;
} else {
save = c;
state = 2;
}
break;
case 2:
c = *inptr++;
if (isxdigit (c) && isxdigit (save)) {
c = toupper (c);
save = toupper (save);
*outptr++ = (((save >= 'A' ? save - 'A' + 10 : save - '0') & 0x0f) << 4)
| ((c >= 'A' ? c - 'A' + 10 : c - '0') & 0x0f);
} else if (c == '\n' && save == '\r') {
/* soft break ... canonical end of line */
} else {
/* just output the data */
*outptr++ = '=';
*outptr++ = save;
*outptr++ = c;
}
state = 0;
break;
#ifdef CANONICALISE_EOL
case 3:
/* convert \n -> to \r\n, leaves \r\n alone */
c = *inptr++;
if (c == '\n') {
*outptr++ = '\r';
*outptr++ = c;
} else {
*outptr++ = '\r';
*outptr++ = '\n';
*outptr++ = c;
}
state = 0;
break;
#endif
}
}
*savestate = state;
*saved = save;
return (outptr - out);
}
|