DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (c68fe15a81fc)

VCS Links

Line Code
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "signtool.h"
#include <prmem.h>
#include <prio.h>
#include <prio.h>
#include <prenv.h>

static int javascript_fn(char *relpath, char *basedir, char *reldir,
                         char *filename, void *arg);
static int extract_js(char *filename);
static int extract_js(char *filename);
static int copyinto(char *from, char *to);
static PRStatus ensureExists(char *base, char *path);
static int make_dirs(char *path, PRInt32 file_perms);

static char *jartree = NULL;
static char *jartree = NULL;
static int idOrdinal;
static PRBool dumpParse = PR_FALSE;

static char *event_handlers[] = {
    "onAbort",
    "onAbort",
    "onBlur",
    "onChange",
    "onClick",
    "onDblClick",
    "onDragDrop",
    "onDragDrop",
    "onError",
    "onFocus",
    "onKeyDown",
    "onKeyPress",
    "onKeyUp",
    "onKeyUp",
    "onLoad",
    "onMouseDown",
    "onMouseMove",
    "onMouseOut",
    "onMouseOver",
    "onMouseOver",
    "onMouseUp",
    "onMove",
    "onReset",
    "onResize",
    "onSelect",
    "onSubmit",
    "onSubmit",
    "onUnload"
};

static int num_handlers = 23;


/*
 *  I n l i n e J a v a S c r i p t
 *
 *  Javascript signing. Instead of passing an archive to signtool,
 *  a directory containing html files is given. Archives are created
 *  a directory containing html files is given. Archives are created
 *  from the archive= and src= tag attributes inside the html,
 *  as appropriate. Then the archives are signed.
 *
 */
int
InlineJavaScript(char *dir, PRBool recurse)
{
    jartree = dir;
    if (verbosity >= 0) {
    if (verbosity >= 0) {
        PR_fprintf(outputFD, "\nGenerating inline signatures from HTML files in: %s\n",
                   dir);
    }
    if (PR_GetEnvSecure("SIGNTOOL_DUMP_PARSE")) {
        dumpParse = PR_TRUE;
        dumpParse = PR_TRUE;
    }

    return foreach (dir, "", javascript_fn, recurse, PR_FALSE /*include dirs*/,
                    (void *)NULL);
}
}

/************************************************************************
 *
 * j a v a s c r i p t _ f n
 */
static int
javascript_fn(char *relpath, char *basedir, char *reldir, char *filename, void *arg)
javascript_fn(char *relpath, char *basedir, char *reldir, char *filename, void *arg)
{
    char fullname[FNSIZE];

    /* only process inline scripts from .htm, .html, and .shtml*/


    if (!(PL_strcaserstr(filename, ".htm") == filename + strlen(filename) - 4) &&
        !(PL_strcaserstr(filename, ".html") == filename + strlen(filename) - 5) &&
        !(PL_strcaserstr(filename, ".shtml") == filename + strlen(filename) - 6)) {
        return 0;
    }
    }

    /* don't process scripts that signtool has already
     extracted (those that are inside .arc directories) */


    if (PL_strcaserstr(filename, ".arc") == filename + strlen(filename) - 4)
        return 0;

    if (verbosity >= 0) {
        PR_fprintf(outputFD, "Processing HTML file: %s\n", relpath);
        PR_fprintf(outputFD, "Processing HTML file: %s\n", relpath);
    }

    /* reset firstArchive at top of each HTML file */

    /* skip directories that contain extracted scripts */
    /* skip directories that contain extracted scripts */

    if (PL_strcaserstr(reldir, ".arc") == reldir + strlen(reldir) - 4)
        return 0;

    sprintf(fullname, "%s/%s", basedir, relpath);
    sprintf(fullname, "%s/%s", basedir, relpath);
    return extract_js(fullname);
}

/*===========================================================================
 =
 = D A T A   S T R U C T U R E S
 =
*/
typedef enum {
    TEXT_HTML_STATE = 0,
    SCRIPT_HTML_STATE
    SCRIPT_HTML_STATE
}

HTML_STATE;

typedef enum {
    /* we start in the start state */
    START_STATE,

    /* We are looking for or reading in an attribute */
    /* We are looking for or reading in an attribute */
    GET_ATT_STATE,

    /* We're burning ws before finding an attribute */
    PRE_ATT_WS_STATE,


    /* We're burning ws after an attribute.  Looking for an '='. */
    POST_ATT_WS_STATE,

    /* We're burning ws after an '=', waiting for a value */
    PRE_VAL_WS_STATE,
    PRE_VAL_WS_STATE,

    /* We're reading in a value */
    GET_VALUE_STATE,

    /* We're reading in a value that's inside quotes */
    GET_QUOTED_VAL_STATE,
    GET_QUOTED_VAL_STATE,

    /* We've encountered the closing '>' */
    DONE_STATE,

    /* Error state */
    /* Error state */
    ERR_STATE
}

TAG_STATE;


typedef struct AVPair_Str {
    char *attribute;
    char *value;
    unsigned int valueLine; /* the line that the value ends on */
    struct AVPair_Str *next;
    struct AVPair_Str *next;
} AVPair;

typedef enum {
    APPLET_TAG,
    SCRIPT_TAG,
    SCRIPT_TAG,
    LINK_TAG,
    STYLE_TAG,
    COMMENT_TAG,
    OTHER_TAG
}
}

TAG_TYPE;

typedef struct {
    TAG_TYPE type;
    AVPair *attList;
    AVPair *attList;
    AVPair *attListTail;
    char *text;
} TagItem;

typedef enum {
typedef enum {
    TAG_ITEM,
    TEXT_ITEM
}

ITEM_TYPE;

typedef struct HTMLItem_Str {
typedef struct HTMLItem_Str {
    unsigned int startLine;
    unsigned int endLine;
    ITEM_TYPE type;
    union {
        TagItem *tag;
        TagItem *tag;
        char *text;
    } item;
    struct HTMLItem_Str *next;
} HTMLItem;


typedef struct {
    PRFileDesc *fd;
    PRInt32 curIndex;
    PRInt32 curIndex;
    PRBool IsEOF;
#define FILE_BUFFER_BUFSIZE 512
    char buf[FILE_BUFFER_BUFSIZE];
    char buf[FILE_BUFFER_BUFSIZE];
    PRInt32 startOffset;
    PRInt32 maxIndex;
    unsigned int lineNum;
} FileBuffer;


/*===========================================================================
 =
 = F U N C T I O N S
 =
*/
*/
static HTMLItem *CreateTextItem(char *text, unsigned int startline,
                                unsigned int endline);
static HTMLItem *CreateTagItem(TagItem *ti, unsigned int startline,
                               unsigned int endline);
static TagItem *ProcessTag(FileBuffer *fb, char **errStr);
static TagItem *ProcessTag(FileBuffer *fb, char **errStr);
static void DestroyHTMLItem(HTMLItem *item);
static void DestroyTagItem(TagItem *ti);
static TAG_TYPE GetTagType(char *att);
static FileBuffer *FB_Create(PRFileDesc *fd);
static int FB_GetChar(FileBuffer *fb);
static PRInt32 FB_GetPointer(FileBuffer *fb);
static PRInt32 FB_GetRange(FileBuffer *fb, PRInt32 start, PRInt32 end,
                           char **buf);
static unsigned int FB_GetLineNum(FileBuffer *fb);
static unsigned int FB_GetLineNum(FileBuffer *fb);
static void FB_Destroy(FileBuffer *fb);
static void PrintTagItem(PRFileDesc *fd, TagItem *ti);
static void PrintHTMLStream(PRFileDesc *fd, HTMLItem *head);

/************************************************************************
/************************************************************************
 *
 * C r e a t e T e x t I t e m
 */
static HTMLItem *
CreateTextItem(char *text, unsigned int startline, unsigned int endline)
CreateTextItem(char *text, unsigned int startline, unsigned int endline)
{
    HTMLItem *item;

    item = PR_Malloc(sizeof(HTMLItem));
    if (!item) {
    if (!item) {
        return NULL;
    }

    item->type = TEXT_ITEM;
    item->item.text = text;
    item->item.text = text;
    item->next = NULL;
    item->startLine = startline;
    item->endLine = endline;
    item->endLine = endline;

    return item;
}

/************************************************************************
 *
 * C r e a t e T a g I t e m
 */
static HTMLItem *
static HTMLItem *
CreateTagItem(TagItem *ti, unsigned int startline, unsigned int endline)
{
    HTMLItem *item;

    item = PR_Malloc(sizeof(HTMLItem));
    item = PR_Malloc(sizeof(HTMLItem));
    if (!item) {
        return NULL;
    }

    item->type = TAG_ITEM;
    item->type = TAG_ITEM;
    item->item.tag = ti;
    item->next = NULL;
    item->startLine = startline;
    item->endLine = endline;


    return item;
}

static PRBool
static PRBool
isAttChar(int c)
{
    return (isalnum(c) || c == '/' || c == '-');
}


/************************************************************************
 *
 * P r o c e s s T a g
 */
static TagItem *
static TagItem *
ProcessTag(FileBuffer *fb, char **errStr)
{
    TAG_STATE state;
    PRInt32 startText, startID, curPos;
    PRBool firstAtt;
    PRBool firstAtt;
    int curchar;
    TagItem *ti = NULL;
    AVPair *curPair = NULL;
    char quotechar = '\0';
    unsigned int linenum;
    unsigned int linenum;
    unsigned int startline;

    state = START_STATE;

    startID = FB_GetPointer(fb);
    startText = startID;
    firstAtt = PR_TRUE;


    ti = (TagItem *)PR_Malloc(sizeof(TagItem));
    if (!ti)
        out_of_memory();
    ti->type = OTHER_TAG;
    ti->attList = NULL;
    ti->attList = NULL;
    ti->attListTail = NULL;
    ti->text = NULL;


    startline = FB_GetLineNum(fb);
    startline = FB_GetLineNum(fb);

    while (state != DONE_STATE && state != ERR_STATE) {
        linenum = FB_GetLineNum(fb);
        linenum = FB_GetLineNum(fb);
        curchar = FB_GetChar(fb);
        if (curchar == EOF) {
            *errStr = PR_smprintf(
                "line %d: Unexpected end-of-file while parsing tag starting at line %d.\n",
                linenum, startline);
                linenum, startline);
            state = ERR_STATE;
            continue;
        }

        switch (state) {
        switch (state) {
            case START_STATE:
                if (curchar == '!') {
                if (curchar == '!') {
                    /*
                     * SGML tag or comment
                     * Here's the general rule for SGML tags.  Everything from
                     * <! to > is the tag.  Inside the tag, comments are
                     * delimited with --.  So we are looking for the first '>'
                     * delimited with --.  So we are looking for the first '>'
                     * that is not commented out, that is, not inside a pair
                     * of --: <!DOCTYPE --this is a comment >(psyche!)   -->
                     */

                    PRBool inComment = PR_FALSE;
                    PRBool inComment = PR_FALSE;
                    short hyphenCount = 0; /* number of consecutive hyphens */

                    while (1) {
                        linenum = FB_GetLineNum(fb);
                        curchar = FB_GetChar(fb);
                        curchar = FB_GetChar(fb);
                        if (curchar == EOF) {
                            /* Uh oh, EOF inside comment */
                            *errStr = PR_smprintf(
                                "line %d: Unexpected end-of-file inside comment starting at line %d.\n",
                                linenum, startline);
                                linenum, startline);
                            state = ERR_STATE;
                            break;
                        }
                        if (curchar == '-') {
                            if (hyphenCount == 1) {
                            if (hyphenCount == 1) {
                                /* This is a comment delimiter */
                                inComment = !inComment;
                                hyphenCount = 0;
                            } else {
                                /* beginning of a comment delimiter? */
                                /* beginning of a comment delimiter? */
                                hyphenCount = 1;
                            }
                        } else if (curchar == '>') {
                            if (!inComment) {
                                /* This is the end of the tag */
                                /* This is the end of the tag */
                                state = DONE_STATE;
                                break;
                            } else {
                                /* The > is inside a comment, so it's not
                                                         * really the end of the tag */
                                hyphenCount = 0;
                            }
                            }
                        } else {
                            hyphenCount = 0;
                        }
                    }
                    ti->type = COMMENT_TAG;
                    ti->type = COMMENT_TAG;
                    break;
                }
            /* fall through */
            case GET_ATT_STATE:
                if (isspace(curchar) || curchar == '=' || curchar == '>') {
                    /* end of the current attribute */
                    curPos = FB_GetPointer(fb) - 2;
                    if (curPos >= startID) {
                        /* We have an attribute */
                        /* We have an attribute */
                        curPair = (AVPair *)PR_Malloc(sizeof(AVPair));
                        if (!curPair)
                            out_of_memory();
                        curPair->value = NULL;
                        curPair->next = NULL;
                        curPair->next = NULL;
                        FB_GetRange(fb, startID, curPos,
                                    &curPair->attribute);

                        /* Stick this attribute on the list */
                        if (ti->attListTail) {
                        if (ti->attListTail) {
                            ti->attListTail->next = curPair;
                            ti->attListTail = curPair;
                        } else {
                            ti->attList = ti->attListTail =
                                curPair;
                                curPair;
                        }

                        /* If this is the first attribute, find the type of tag
                         * based on it. Also, start saving the text of the tag. */
                        if (firstAtt) {
                        if (firstAtt) {
                            ti->type = GetTagType(curPair->attribute);
                            startText = FB_GetPointer(fb) -
                                        1;
                            firstAtt = PR_FALSE;
                        }
                        }
                    } else {
                        if (curchar == '=') {
                            /* If we don't have any attribute but we do have an
                         * equal sign, that's an error */
                            *errStr = PR_smprintf("line %d: Malformed tag starting at line %d.\n",
                            *errStr = PR_smprintf("line %d: Malformed tag starting at line %d.\n",
                                                  linenum, startline);
                            state = ERR_STATE;
                            break;
                        }
                        }
                    }

                    /* Compute next state */
                    if (curchar == '=') {
                        startID = FB_GetPointer(fb);
                        startID = FB_GetPointer(fb);
                        state = PRE_VAL_WS_STATE;
                    } else if (curchar == '>') {
                        state = DONE_STATE;
                        state = DONE_STATE;
                    } else if (curPair) {
                        state = POST_ATT_WS_STATE;
                    } else {
                        state = PRE_ATT_WS_STATE;
                        state = PRE_ATT_WS_STATE;
                    }
                } else if (isAttChar(curchar)) {
                    /* Just another char in the attribute. Do nothing */
                    state = GET_ATT_STATE;
                } else {
                } else {
                    /* bogus char */
                    *errStr = PR_smprintf("line %d: Bogus chararacter '%c' in tag.\n",
                                          linenum, curchar);
                    state = ERR_STATE;
                    break;
                    break;
                }
                break;
            case PRE_ATT_WS_STATE:
                if (curchar == '>') {
                    state = DONE_STATE;
                    state = DONE_STATE;
                } else if (isspace(curchar)) {
                    /* more whitespace, do nothing */
                } else if (isAttChar(curchar)) {
                    /* starting another attribute */
                    startID = FB_GetPointer(fb) - 1;
                    startID = FB_GetPointer(fb) - 1;
                    state = GET_ATT_STATE;
                } else {
                    /* bogus char */
                    *errStr = PR_smprintf("line %d: Bogus character '%c' in tag.\n",
                                          linenum, curchar);
                                          linenum, curchar);
                    state = ERR_STATE;
                    break;
                }
                break;
            case POST_ATT_WS_STATE:
            case POST_ATT_WS_STATE:
                if (curchar == '>') {
                    state = DONE_STATE;
                } else if (isspace(curchar)) {
                    /* more whitespace, do nothing */
                } else if (isAttChar(curchar)) {
                } else if (isAttChar(curchar)) {
                    /* starting another attribute */
                    startID = FB_GetPointer(fb) - 1;
                    state = GET_ATT_STATE;
                } else if (curchar == '=') {
                    /* there was whitespace between the attribute and its equal
                    /* there was whitespace between the attribute and its equal
                     * sign, which means there's a value coming up */
                    state = PRE_VAL_WS_STATE;
                } else {
                    /* bogus char */
                    *errStr = PR_smprintf("line %d: Bogus character '%c' in tag.\n",
                    *errStr = PR_smprintf("line %d: Bogus character '%c' in tag.\n",
                                          linenum, curchar);
                    state = ERR_STATE;
                    break;
                }
                break;
                break;
            case PRE_VAL_WS_STATE:
                if (curchar == '>') {
                    /* premature end-of-tag (sounds like a personal problem). */
                    *errStr = PR_smprintf(
                        "line %d: End of tag while waiting for value.\n",
                        "line %d: End of tag while waiting for value.\n",
                        linenum);
                    state = ERR_STATE;
                    break;
                } else if (isspace(curchar)) {
                    /* more whitespace, do nothing */
                    break;
                } else {
                    /* this must be some sort of value. Fall through
                                 * to GET_VALUE_STATE */
                                 * to GET_VALUE_STATE */
                    startID = FB_GetPointer(fb) - 1;
                    state = GET_VALUE_STATE;
                }
            /* Fall through if we didn't break on '>' or whitespace */
            case GET_VALUE_STATE:
                if (isspace(curchar) || curchar == '>') {
                if (isspace(curchar) || curchar == '>') {
                    /* end of value */
                    curPos = FB_GetPointer(fb) - 2;
                    if (curPos >= startID) {
                        /* Grab the value */
                        FB_GetRange(fb, startID, curPos,
                        FB_GetRange(fb, startID, curPos,
                                    &curPair->value);
                        curPair->valueLine = linenum;
                    } else {
                        /* empty value, leave as NULL */
                    }
                    }
                    if (isspace(curchar)) {
                        state = PRE_ATT_WS_STATE;
                    } else {
                        state = DONE_STATE;
                    }
                    }
                } else if (curchar == '\"' || curchar == '\'') {
                    /* quoted value.  Start recording the value inside the quote*/
                    startID = FB_GetPointer(fb);
                    state = GET_QUOTED_VAL_STATE;
                    PORT_Assert(quotechar == '\0');
                    PORT_Assert(quotechar == '\0');
                    quotechar = curchar; /* look for matching quote type */
                } else {
                    /* just more value */
                }
                break;
                break;
            case GET_QUOTED_VAL_STATE:
                PORT_Assert(quotechar != '\0');
                if (curchar == quotechar) {
                    /* end of quoted value */
                    curPos = FB_GetPointer(fb) - 2;
                    if (curPos >= startID) {
                        /* Grab the value */
                        /* Grab the value */
                        FB_GetRange(fb, startID, curPos,
                                    &curPair->value);
                        curPair->valueLine = linenum;
                    } else {
                        /* empty value, leave it as NULL */
                        /* empty value, leave it as NULL */
                    }
                    state = GET_ATT_STATE;
                    quotechar = '\0';
                    startID = FB_GetPointer(fb);
                } else {
                } else {
                    /* more quoted value, continue */
                }
                break;
            case DONE_STATE:
            case ERR_STATE:
            case ERR_STATE:
            default:; /* should never get here */
            default:; /* should never get here */
        }
    }


    if (state == DONE_STATE) {
        /* Get the text of the tag */
        curPos = FB_GetPointer(fb) - 1;
        FB_GetRange(fb, startText, curPos, &ti->text);


        /* Return the tag */
        return ti;
    }

    /* Uh oh, an error.  Kill the tag item*/
    /* Uh oh, an error.  Kill the tag item*/
    DestroyTagItem(ti);
    return NULL;
}

/************************************************************************
/************************************************************************
 *
 * D e s t r o y H T M L I t e m
 */
static void
DestroyHTMLItem(HTMLItem *item)
DestroyHTMLItem(HTMLItem *item)
{
    if (item->type == TAG_ITEM) {
        DestroyTagItem(item->item.tag);
    } else {
    } else {
        if (item->item.text) {
            PR_Free(item->item.text);
        }
    }
}
}

/************************************************************************
 *
 * D e s t r o y T a g I t e m
 * D e s t r o y T a g I t e m
 */
static void
DestroyTagItem(TagItem *ti)
{
    AVPair *temp;
    AVPair *temp;

    if (ti->text) {
        PR_Free(ti->text);
        ti->text = NULL;
    }
    }

    while (ti->attList) {
        temp = ti->attList;
        ti->attList = ti->attList->next;


        if (temp->attribute) {
            PR_Free(temp->attribute);
            temp->attribute = NULL;
        }
        if (temp->value) {
            PR_Free(temp->value);
            temp->value = NULL;
        }
        }
        PR_Free(temp);
    }

    PR_Free(ti);
}
}

/************************************************************************
 *
 * G e t T a g T y p e
 */
 */
static TAG_TYPE
GetTagType(char *att)
{
    if (!PORT_Strcasecmp(att, "APPLET")) {
        return APPLET_TAG;
        return APPLET_TAG;
    }
    if (!PORT_Strcasecmp(att, "SCRIPT")) {
        return SCRIPT_TAG;
    }
    if (!PORT_Strcasecmp(att, "LINK")) {
    if (!PORT_Strcasecmp(att, "LINK")) {
        return LINK_TAG;
    }
    }
    if (!PORT_Strcasecmp(att, "STYLE")) {
        return STYLE_TAG;
    }
    return OTHER_TAG;
}
}

/************************************************************************
 *
 * F B _ C r e a t e
 */
static FileBuffer *
FB_Create(PRFileDesc *fd)
FB_Create(PRFileDesc *fd)
{
    FileBuffer *fb;
    PRInt32 amountRead;
    PRInt32 storedOffset;


    fb = (FileBuffer *)PR_Malloc(sizeof(FileBuffer));
    fb->fd = fd;
    storedOffset = PR_Seek(fd, 0, PR_SEEK_CUR);
    PR_Seek(fd, 0, PR_SEEK_SET);
    fb->startOffset = 0;
    fb->startOffset = 0;
    amountRead = PR_Read(fd, fb->buf, FILE_BUFFER_BUFSIZE);
    if (amountRead == -1)
        goto loser;
    fb->maxIndex = amountRead - 1;
    fb->curIndex = 0;
    fb->curIndex = 0;
    fb->IsEOF = (fb->curIndex > fb->maxIndex) ? PR_TRUE : PR_FALSE;
    fb->lineNum = 1;

    PR_Seek(fd, storedOffset, PR_SEEK_SET);
    return fb;
loser:
    PR_Seek(fd, storedOffset, PR_SEEK_SET);
    PR_Seek(fd, storedOffset, PR_SEEK_SET);
    PR_Free(fb);
    return NULL;
}

/************************************************************************
/************************************************************************
 *
 * F B _ G e t C h a r
 */
static int
FB_GetChar(FileBuffer *fb)
FB_GetChar(FileBuffer *fb)
{
    PRInt32 storedOffset;
    PRInt32 amountRead;
    int retval = -1;


    if (fb->IsEOF) {
        return EOF;
    }

    storedOffset = PR_Seek(fb->fd, 0, PR_SEEK_CUR);
    storedOffset = PR_Seek(fb->fd, 0, PR_SEEK_CUR);

    retval = (unsigned char)fb->buf[fb->curIndex++];
    if (retval == '\n')
        fb->lineNum++;


    if (fb->curIndex > fb->maxIndex) {
        /* We're at the end of the buffer. Try to get some new data from the
                 * file */
        fb->startOffset += fb->maxIndex + 1;
        PR_Seek(fb->fd, fb->startOffset, PR_SEEK_SET);
        PR_Seek(fb->fd, fb->startOffset, PR_SEEK_SET);
        amountRead = PR_Read(fb->fd, fb->buf, FILE_BUFFER_BUFSIZE);
        if (amountRead == -1)
            goto loser;
        fb->maxIndex = amountRead - 1;
        fb->curIndex = 0;
        fb->curIndex = 0;
    }

    fb->IsEOF = (fb->curIndex > fb->maxIndex) ? PR_TRUE : PR_FALSE;

loser:
    PR_Seek(fb->fd, storedOffset, PR_SEEK_SET);
    return retval;
    return retval;
}

/************************************************************************
 *
 * F B _ G e t L i n e N u m
 * F B _ G e t L i n e N u m
 *
 *
 */
static unsigned int
FB_GetLineNum(FileBuffer *fb)
{
    return fb->lineNum;
}


/************************************************************************
 *
 * F B _ G e t P o i n t e r
 *
 */
 */
static PRInt32
FB_GetPointer(FileBuffer *fb)
{
    return fb->startOffset + fb->curIndex;
}
}

/************************************************************************
 *
 * F B _ G e t R a n g e
 *
 *
 */
static PRInt32
FB_GetRange(FileBuffer *fb, PRInt32 start, PRInt32 end, char **buf)
{
    PRInt32 amountRead;
    PRInt32 amountRead;
    PRInt32 storedOffset;

    *buf = PR_Malloc(end - start + 2);
    if (*buf == NULL) {
        return 0;
        return 0;
    }

    storedOffset = PR_Seek(fb->fd, 0, PR_SEEK_CUR);
    PR_Seek(fb->fd, start, PR_SEEK_SET);
    amountRead = PR_Read(fb->fd, *buf, end - start + 1);
    amountRead = PR_Read(fb->fd, *buf, end - start + 1);
    PR_Seek(fb->fd, storedOffset, PR_SEEK_SET);
    if (amountRead == -1) {
        PR_Free(*buf);
        *buf = NULL;
        return 0;
        return 0;
    }

    (*buf)[end - start + 1] = '\0';
    return amountRead;
}

/************************************************************************
 *
 * F B _ D e s t r o y
 * F B _ D e s t r o y
 *
 */
static void
FB_Destroy(FileBuffer *fb)
{
    if (fb) {
        PR_Free(fb);
        PR_Free(fb);
    }
}

/************************************************************************
 *
 *
 * P r i n t T a g I t e m
 *
 */
static void
PrintTagItem(PRFileDesc *fd, TagItem *ti)
PrintTagItem(PRFileDesc *fd, TagItem *ti)
{
    AVPair *pair;

    PR_fprintf(fd, "TAG:\n----\nType: ");
    switch (ti->type) {
    switch (ti->type) {
        case APPLET_TAG:
            PR_fprintf(fd, "applet\n");
            break;
        case SCRIPT_TAG:
            PR_fprintf(fd, "script\n");
            break;
        case LINK_TAG:
        case LINK_TAG:
            PR_fprintf(fd, "link\n");
            break;
        case STYLE_TAG:
            PR_fprintf(fd, "style\n");
            break;
            break;
        case COMMENT_TAG:
            PR_fprintf(fd, "comment\n");
            break;
        case OTHER_TAG:
        default:
        default:
            PR_fprintf(fd, "other\n");
            break;
    }

    PR_fprintf(fd, "Attributes:\n");
    PR_fprintf(fd, "Attributes:\n");
    for (pair = ti->attList; pair; pair = pair->next) {
        PR_fprintf(fd, "\t%s=%s\n", pair->attribute,
                   pair->value ? pair->value : "");
    }
    }
    PR_fprintf(fd, "Text:%s\n", ti->text ? ti->text : "");

    PR_fprintf(fd, "---End of tag---\n");
}
}

/************************************************************************
 *
 * P r i n t H T M L S t r e a m
 *
 *
 */
static void
PrintHTMLStream(PRFileDesc *fd, HTMLItem *head)
{
    while (head) {
    while (head) {
        if (head->type == TAG_ITEM) {
            PrintTagItem(fd, head->item.tag);
        } else {
            PR_fprintf(fd, "\nTEXT:\n-----\n%s\n-----\n\n", head->item.text);
        }
        }
        head = head->next;
    }
}

/************************************************************************
 *
 * S a v e I n l i n e S c r i p t
 *
 */
static int
SaveInlineScript(char *text, char *id, char *basedir, char *archiveDir)
{
{
    char *filename = NULL;
    PRFileDesc *fd = NULL;
    int retval = -1;
    PRInt32 writeLen;
    char *ilDir = NULL;
    char *ilDir = NULL;

    if (!text || !id || !archiveDir) {
        return -1;
    }


    if (dumpParse) {
        PR_fprintf(outputFD, "SaveInlineScript: text=%s, id=%s, \n"
                             "basedir=%s, archiveDir=%s\n",
                   text, id, basedir, archiveDir);
    }
    }

    /* Make sure the archive directory is around */
    if (ensureExists(basedir, archiveDir) != PR_SUCCESS) {
        PR_fprintf(errorFD,
                   "ERROR: Unable to create archive directory %s.\n", archiveDir);
                   "ERROR: Unable to create archive directory %s.\n", archiveDir);
        errorCount++;
        return -1;
        return -1;
    }

    /* Make sure the inline script directory is around */
    ilDir = PR_smprintf("%s/inlineScripts", archiveDir);
    scriptdir = "inlineScripts";
    scriptdir = "inlineScripts";
    if (ensureExists(basedir, ilDir) != PR_SUCCESS) {
        PR_fprintf(errorFD,
                   "ERROR: Unable to create directory %s.\n", ilDir);
        errorCount++;
        return -1;
        return -1;
    }

    filename = PR_smprintf("%s/%s/%s", basedir, ilDir, id);

    /* If the file already exists, give a warning, then blow it away */
    /* If the file already exists, give a warning, then blow it away */
    if (PR_Access(filename, PR_ACCESS_EXISTS) == PR_SUCCESS) {
        PR_fprintf(errorFD,
                   "warning: file \"%s\" already exists--will overwrite.\n",
                   filename);
        warningCount++;
        if (rm_dash_r(filename)) {
            PR_fprintf(errorFD, "ERROR: Unable to delete %s.\n", filename);
            errorCount++;
            goto finish;
        }
    }
    }

    /* Write text into file with name id */
    fd = PR_Open(filename, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0777);
    if (!fd) {
        PR_fprintf(errorFD, "ERROR: Unable to create file \"%s\".\n",
                   filename);
        errorCount++;
        errorCount++;
        goto finish;
    }
    writeLen = strlen(text);
    if (PR_Write(fd, text, writeLen) != writeLen) {
        PR_fprintf(errorFD, "ERROR: Unable to write to file \"%s\".\n",
        PR_fprintf(errorFD, "ERROR: Unable to write to file \"%s\".\n",
                   filename);
        errorCount++;
        goto finish;
    }


    retval = 0;
finish:
    if (filename) {
        PR_smprintf_free(filename);
    }
    }
    if (ilDir) {
        PR_smprintf_free(ilDir);
    }
    if (fd) {
        PR_Close(fd);
        PR_Close(fd);
    }
    return retval;
}

/************************************************************************
/************************************************************************
 *
 * S a v e U n n a m a b l e S c r i p t
 *
 */
static int
static int
SaveUnnamableScript(char *text, char *basedir, char *archiveDir,
                    char *HTMLfilename)
{
    char *id = NULL;
    char *ext = NULL;
    char *ext = NULL;
    char *start = NULL;
    int retval = -1;


    if (!text || !archiveDir || !HTMLfilename) {
    if (!text || !archiveDir || !HTMLfilename) {
        return -1;
    }


    if (dumpParse) {
        PR_fprintf(outputFD, "SaveUnnamableScript: text=%s, basedir=%s,\n"
                             "archiveDir=%s, filename=%s\n",
                   text, basedir, archiveDir,
                   HTMLfilename);
                   HTMLfilename);
    }

    /* Construct the filename */
    ext = PL_strrchr(HTMLfilename, '.');
    if (ext) {
    if (ext) {
        *ext = '\0';
    }
    for (start = HTMLfilename; strpbrk(start, "/\\");
         start = strpbrk(start, "/\\") + 1)
        /* do nothing */;
        /* do nothing */;
    if (*start == '\0')
        start = HTMLfilename;
    id = PR_smprintf("_%s%d", start, idOrdinal++);
    if (ext) {
    if (ext) {
        *ext = '.';
    }

    /* Now call SaveInlineScript to do the work */
    retval = SaveInlineScript(text, id, basedir, archiveDir);
    retval = SaveInlineScript(text, id, basedir, archiveDir);


    PR_Free(id);

    return retval;
}


/************************************************************************
 *
 * S a v e S o u r c e
 *
 */
 */
static int
SaveSource(char *src, char *codebase, char *basedir, char *archiveDir)
{
{
    char *from = NULL, *to = NULL;
    char *from = NULL, *to = NULL;
    int retval = -1;
    char *arcDir = NULL;


    if (!src || !archiveDir) {
        return -1;
    }

    if (dumpParse) {
    if (dumpParse) {
        PR_fprintf(outputFD, "SaveSource: src=%s, codebase=%s, basedir=%s,\n"
                             "archiveDir=%s\n",
                   src, codebase, basedir, archiveDir);
    }

    if (codebase) {
    if (codebase) {
        arcDir = PR_smprintf("%s/%s/%s/", basedir, codebase, archiveDir);
    } else {
        arcDir = PR_smprintf("%s/%s/", basedir, archiveDir);
    }


    if (codebase) {
        from = PR_smprintf("%s/%s/%s", basedir, codebase, src);
        to = PR_smprintf("%s%s", arcDir, src);
    } else {
        from = PR_smprintf("%s/%s", basedir, src);
        from = PR_smprintf("%s/%s", basedir, src);
        to = PR_smprintf("%s%s", arcDir, src);
    }

    if (make_dirs(to, 0777)) {
        PR_fprintf(errorFD,
                   "ERROR: Unable to create archive directory %s.\n", archiveDir);
        errorCount++;
        goto finish;
    }
    }

    retval = copyinto(from, to);
finish:
    if (from)
        PR_Free(from);
    if (to)
    if (to)
        PR_Free(to);
    if (arcDir)
        PR_Free(arcDir);
    return retval;
}
}

/************************************************************************
 *
 * T a g T y p e T o S t r i n g
 * T a g T y p e T o S t r i n g
 *
 */
char *
TagTypeToString(TAG_TYPE type)
{
{
    switch (type) {
        case APPLET_TAG:
            return "APPLET";
        case SCRIPT_TAG:
            return "SCRIPT";
            return "SCRIPT";
        case LINK_TAG:
            return "LINK";
        case STYLE_TAG:
        case STYLE_TAG:
            return "STYLE";
        default:
            break;
    }
    return "unknown";
    return "unknown";
}

/************************************************************************
 *
 * e x t r a c t _ j s
 * e x t r a c t _ j s
 *
 */
static int
extract_js(char *filename)
{
{
    PRFileDesc *fd = NULL;
    FileBuffer *fb = NULL;
    HTMLItem *head = NULL;
    HTMLItem *tail = NULL;
    HTMLItem *curitem = NULL;
    HTMLItem *curitem = NULL;
    HTMLItem *styleList = NULL;
    HTMLItem *styleListTail = NULL;
    HTMLItem *entityList = NULL;
    HTMLItem *entityListTail = NULL;
    TagItem *tagp = NULL;
    char *text = NULL;
    char *text = NULL;
    char *tagerr = NULL;
    char *archiveDir = NULL;
    char *firstArchiveDir = NULL;
    char *basedir = NULL;
    PRInt32 textStart;
    PRInt32 textStart;
    PRInt32 curOffset;
    HTML_STATE state;
    int curchar;
    int retval = -1;
    int retval = -1;
    unsigned int linenum, startLine;

    /* Initialize the implicit ID counter for each file */
    idOrdinal = 0;


    /*
     * First, parse the HTML into a stream of tags and text.
     */

    fd = PR_Open(filename, PR_RDONLY, 0);
    fd = PR_Open(filename, PR_RDONLY, 0);
    if (!fd) {
        PR_fprintf(errorFD, "Unable to open %s for reading.\n", filename);
        errorCount++;
        return -1;
    }
    }

    /* Construct base directory of filename. */
    {
        char *cp;
        char *cp;

        basedir = PL_strdup(filename);

        /* Remove trailing slashes */
        while ((cp = PL_strprbrk(basedir, "/\\")) ==
               (basedir + strlen(basedir) - 1)) {
            *cp = '\0';
            *cp = '\0';
        }

        /* Now remove everything from the last slash (which will be followed
         * by a filename) to the end */
        cp = PL_strprbrk(basedir, "/\\");
        cp = PL_strprbrk(basedir, "/\\");
        if (cp) {
            *cp = '\0';
        }
    }


    state = TEXT_HTML_STATE;

    fb = FB_Create(fd);

    textStart = 0;
    textStart = 0;
    startLine = 0;
    while (linenum = FB_GetLineNum(fb), (curchar = FB_GetChar(fb)) != EOF) {
        switch (state) {
        switch (state) {
            case TEXT_HTML_STATE:
                if (curchar == '<') {
                    /*
                     * Found a tag
                     */
                     */
                    /* Save the text so far to a new text item */
                    curOffset = FB_GetPointer(fb) - 2;
                    if (curOffset >= textStart) {
                        if (FB_GetRange(fb, textStart, curOffset,
                                        &text) !=
                                        &text) !=
                            curOffset - textStart + 1) {
                            PR_fprintf(errorFD,
                                       "Unable to read from %s.\n",
                                       filename);
                            errorCount++;
                            errorCount++;
                            goto loser;
                        }
                        /* little fudge here.  If the first character on a line
                         * is '<', meaning a new tag, the preceding text item
                         * actually ends on the previous line.  In this case
                         * actually ends on the previous line.  In this case
                         * we will be saying that the text segment ends on the
                         * next line. I don't think this matters for text items. */
                        curitem = CreateTextItem(text, startLine,
                                                 linenum);
                        text = NULL;
                        if (tail == NULL) {
                            head = tail = curitem;
                            head = tail = curitem;
                        } else {
                            tail->next = curitem;
                            tail = curitem;
                        }
                    }
                    }

                    /* Process the tag */
                    tagp = ProcessTag(fb, &tagerr);
                    if (!tagp) {
                        if (tagerr) {
                        if (tagerr) {
                            PR_fprintf(errorFD, "Error in file %s: %s\n",
                                       filename, tagerr);
                            errorCount++;
                        } else {
                            PR_fprintf(errorFD,
                            PR_fprintf(errorFD,
                                       "Error in file %s, in tag starting at line %d\n",
                                       filename, linenum);
                            errorCount++;
                        }
                        goto loser;
                    }
                    /* Add the tag to the list */
                    curitem = CreateTagItem(tagp, linenum, FB_GetLineNum(fb));
                    curitem = CreateTagItem(tagp, linenum, FB_GetLineNum(fb));
                    if (tail == NULL) {
                        head = tail = curitem;
                    } else {
                        tail->next = curitem;
                        tail->next = curitem;
                        tail = curitem;
                    }

                    /* What's the next state */
                    if (tagp->type == SCRIPT_TAG) {
                    if (tagp->type == SCRIPT_TAG) {
                        state = SCRIPT_HTML_STATE;
                    }

                    /* Start recording text from the new offset */
                    textStart = FB_GetPointer(fb);
                    textStart = FB_GetPointer(fb);
                    startLine = FB_GetLineNum(fb);
                } else {
                    /* regular character.  Next! */
                }
                break;
                break;
            case SCRIPT_HTML_STATE:
                if (curchar == '<') {
                    char *cp;
                    /*
                     * If this is a </script> tag, then we're at the end of the
                     * If this is a </script> tag, then we're at the end of the
                     * script.  Otherwise, ignore
                     */
                    curOffset = FB_GetPointer(fb) - 1;
                    cp = NULL;
                    if (FB_GetRange(fb, curOffset, curOffset + 8, &cp) != 9) {
                    if (FB_GetRange(fb, curOffset, curOffset + 8, &cp) != 9) {
                        if (cp) {
                            PR_Free(cp);
                            cp = NULL;
                        }
                    } else {
                    } else {
                        /* compare the strings */
                        if (!PORT_Strncasecmp(cp, "</script>", 9)) {
                            /* This is the end of the script. Record the text. */
                            curOffset--;
                            if (curOffset >= textStart) {
                            if (curOffset >= textStart) {
                                if (FB_GetRange(fb, textStart, curOffset, &text) !=
                                    curOffset - textStart + 1) {
                                    PR_fprintf(errorFD, "Unable to read from %s.\n",
                                               filename);
                                    errorCount++;
                                    errorCount++;
                                    goto loser;
                                }
                                curitem = CreateTextItem(text, startLine, linenum);
                                text = NULL;
                                if (tail == NULL) {
                                    head = tail = curitem;
                                    head = tail = curitem;
                                } else {
                                    tail->next = curitem;
                                    tail = curitem;
                                }
                            }
                            }

                            /* Now parse the /script tag and put it on the list */
                            tagp = ProcessTag(fb, &tagerr);
                            if (!tagp) {
                                if (tagerr) {
                                    PR_fprintf(errorFD, "Error in file %s: %s\n",
                                               filename, tagerr);
                                               filename, tagerr);
                                } else {
                                    PR_fprintf(errorFD,
                                               "Error in file %s, in tag starting at"
                                               " line %d\n",
                                               filename, linenum);
                                               filename, linenum);
                                }
                                errorCount++;
                                goto loser;
                            }
                            curitem = CreateTagItem(tagp, linenum,
                            curitem = CreateTagItem(tagp, linenum,
                                                    FB_GetLineNum(fb));
                            if (tail == NULL) {
                                head = tail = curitem;
                                head = tail = curitem;
                            } else {
                                tail->next = curitem;
                                tail = curitem;
                            }
                            }

                            /* go back to text state */
                            state = TEXT_HTML_STATE;

                            textStart = FB_GetPointer(fb);
                            textStart = FB_GetPointer(fb);
                            startLine = FB_GetLineNum(fb);
                        }
                    }
                }
                break;
                break;
        }
    }

    /* End of the file.  Wrap up any remaining text */
    if (state == SCRIPT_HTML_STATE) {
    if (state == SCRIPT_HTML_STATE) {
        if (tail && tail->type == TAG_ITEM) {
            PR_fprintf(errorFD, "ERROR: <SCRIPT> tag at %s:%d is not followed "
                                "by a </SCRIPT> tag.\n",
                       filename, tail->startLine);
        } else {
            PR_fprintf(errorFD, "ERROR: <SCRIPT> tag in file %s is not followed"
                                " by a </SCRIPT tag.\n",
                       filename);
                       filename);
        }
        errorCount++;
        goto loser;
    }
    curOffset = FB_GetPointer(fb) - 1;
    curOffset = FB_GetPointer(fb) - 1;
    if (curOffset >= textStart) {
        text = NULL;
        if (FB_GetRange(fb, textStart, curOffset, &text) !=
            curOffset - textStart + 1) {
            PR_fprintf(errorFD, "Unable to read from %s.\n", filename);
            PR_fprintf(errorFD, "Unable to read from %s.\n", filename);
            errorCount++;
            goto loser;
        }
        curitem = CreateTextItem(text, startLine, linenum);
        text = NULL;
        text = NULL;
        if (tail == NULL) {
            head = tail = curitem;
        } else {
            tail->next = curitem;
            tail = curitem;
            tail = curitem;
        }
    }

    if (dumpParse) {
    if (dumpParse) {
        PrintHTMLStream(outputFD, head);
    }

    /*
     * Now we have a stream of tags and text.  Go through and deal with each.
     * Now we have a stream of tags and text.  Go through and deal with each.
     */
    for (curitem = head; curitem; curitem = curitem->next) {
        AVPair *pairp = NULL;
        char *src = NULL, *id = NULL, *codebase = NULL;
        PRBool hasEventHandler = PR_FALSE;
        PRBool hasEventHandler = PR_FALSE;
        int i;

        /* Reset archive directory for each tag */
        if (archiveDir) {
            PR_Free(archiveDir);
            PR_Free(archiveDir);
            archiveDir = NULL;
        }

        /* We only analyze tags */
        if (curitem->type != TAG_ITEM) {
            continue;
        }
        }

        tagp = curitem->item.tag;

        /* go through the attributes to get information */
        for (pairp = tagp->attList; pairp; pairp = pairp->next) {
        for (pairp = tagp->attList; pairp; pairp = pairp->next) {

            /* ARCHIVE= */
            if (!PL_strcasecmp(pairp->attribute, "archive")) {
                if (archiveDir) {
                    /* Duplicate attribute.  Print warning */
                    /* Duplicate attribute.  Print warning */
                    PR_fprintf(errorFD,
                               "warning: \"%s\" attribute overwrites previous attribute"
                               " in tag starting at %s:%d.\n",
                               pairp->attribute, filename, curitem->startLine);
                    warningCount++;
                    warningCount++;
                    PR_Free(archiveDir);
                }
                archiveDir = PL_strdup(pairp->value);

                /* Substiture ".arc" for ".jar" */
                /* Substiture ".arc" for ".jar" */
                if ((PL_strlen(archiveDir) < 4) ||
                    PL_strcasecmp((archiveDir + strlen(archiveDir) - 4),
                                  ".jar")) {
                    PR_fprintf(errorFD,
                               "warning: ARCHIVE attribute should end in \".jar\" in tag"
                               "warning: ARCHIVE attribute should end in \".jar\" in tag"
                               " starting on %s:%d.\n",
                               " starting on %s:%d.\n",
                               filename, curitem->startLine);
                    warningCount++;
                    PR_Free(archiveDir);
                    archiveDir = PR_smprintf("%s.arc", archiveDir);
                } else {
                } else {
                    PL_strcpy(archiveDir + strlen(archiveDir) - 4, ".arc");
                }

                /* Record the first archive.  This will be used later if
                 * the archive is not specified */
                 * the archive is not specified */
                if (firstArchiveDir == NULL) {
                    firstArchiveDir = PL_strdup(archiveDir);
                }
            }
            }
            /* CODEBASE= */
            else if (!PL_strcasecmp(pairp->attribute, "codebase")) {
                if (codebase) {
                    /* Duplicate attribute.  Print warning */
                    PR_fprintf(errorFD,
                    PR_fprintf(errorFD,
                               "warning: \"%s\" attribute overwrites previous attribute"
                               " in tag staring at %s:%d.\n",
                               pairp->attribute, filename, curitem->startLine);
                    warningCount++;
                }
                codebase = pairp->value;
                codebase = pairp->value;
            }
            /* SRC= and HREF= */
            else if (!PORT_Strcasecmp(pairp->attribute, "src") ||
                     !PORT_Strcasecmp(pairp->attribute, "href")) {
                if (src) {
                if (src) {
                    /* Duplicate attribute.  Print warning */
                    PR_fprintf(errorFD,
                               "warning: \"%s\" attribute overwrites previous attribute"
                               " in tag staring at %s:%d.\n",
                               pairp->attribute, filename, curitem->startLine);
                               pairp->attribute, filename, curitem->startLine);
                    warningCount++;
                }
                src = pairp->value;
            }
            }
            /* CODE= */
            else if (!PORT_Strcasecmp(pairp->attribute, "code")) {
                /*!!!XXX Change PORT to PL all over this code !!! */
                if (src) {
                    /* Duplicate attribute.  Print warning */
                    /* Duplicate attribute.  Print warning */
                    PR_fprintf(errorFD,
                               "warning: \"%s\" attribute overwrites previous attribute"
                               " ,in tag staring at %s:%d.\n",
                               pairp->attribute, filename, curitem->startLine);
                    warningCount++;
                    warningCount++;
                }
                src = pairp->value;

                /* Append a .class if one is not already present */
                if ((PL_strlen(src) < 6) ||
                    PL_strcasecmp((src + PL_strlen(src) - 6), ".class")) {
                    src = PR_smprintf("%s.class", src);
                    /* Put this string back into the data structure so it
                     * will be deallocated properly */
                     * will be deallocated properly */
                    PR_Free(pairp->value);
                    pairp->value = src;
                }
            }
            /* ID= */
            /* ID= */
            else if (!PL_strcasecmp(pairp->attribute, "id")) {
                if (id) {
                    /* Duplicate attribute.  Print warning */
                    PR_fprintf(errorFD,
                               "warning: \"%s\" attribute overwrites previous attribute"
                               " in tag staring at %s:%d.\n",
                               pairp->attribute, filename, curitem->startLine);
                               pairp->attribute, filename, curitem->startLine);
                    warningCount++;
                }
                id = pairp->value;
            }


            /* STYLE= */
            /* style= attributes, along with JS entities, are stored into
             * files with dynamically generated names. The filenames are
             * based on the order in which the text is found in the file.
             * All JS entities on all lines up to and including the line
             * All JS entities on all lines up to and including the line
             * containing the end of the tag that has this style= attribute
             * will be processed before this style=attribute.  So we need
             * to record the line that this _tag_ (not the attribute) ends on.
             */
             */
            else if (!PL_strcasecmp(pairp->attribute, "style") && pairp->value) {
                HTMLItem *styleItem;
                /* Put this item on the style list */
                styleItem = CreateTextItem(PL_strdup(pairp->value),
                                           curitem->startLine, curitem->endLine);
                                           curitem->startLine, curitem->endLine);
                if (styleListTail == NULL) {
                    styleList = styleListTail = styleItem;
                } else {
                    styleListTail->next = styleItem;
                    styleListTail = styleItem;
                    styleListTail = styleItem;
                }
            }
            /* Event handlers */
            else {
            else {
                for (i = 0; i < num_handlers; i++) {
                    if (!PL_strcasecmp(event_handlers[i], pairp->attribute)) {
                        hasEventHandler = PR_TRUE;
                        break;
                    }
                    }
                }
            }

            /* JS Entity */
            {
            {
                char *entityStart, *entityEnd;
                HTMLItem *entityItem;

                /* go through each JavaScript entity ( &{...}; ) and store it
                 * in the entityList.  The important thing is to record what
                 * in the entityList.  The important thing is to record what
                 * line number it's on, so we can get it in the right order
                 * in relation to style= attributes.
                 * Apparently, these can't flow across lines, so the start and
                 * end line will be the same.  That helps matters.
                 */
                entityEnd = pairp->value;
                while (entityEnd &&
                while (entityEnd &&
                       (entityStart = PL_strstr(entityEnd, "&{")) /*}*/ != NULL) {
                    entityStart += 2; /* point at beginning of actual entity */
                    entityEnd = PL_strchr(entityStart, '}');
                    if (entityEnd) {
                        /* Put this item on the entity list */
                        /* Put this item on the entity list */
                        *entityEnd = '\0';
                        entityItem = CreateTextItem(PL_strdup(entityStart),
                                                    pairp->valueLine, pairp->valueLine);
                        *entityEnd = /* { */ '}';
                        if (entityListTail) {
                        if (entityListTail) {
                            entityListTail->next = entityItem;
                            entityListTail = entityItem;
                        } else {
                            entityList = entityListTail = entityItem;
                        }
                        }
                    }
                }
            }
        }
        }

        /* If no archive was supplied, we use the first one of the file */
        if (!archiveDir && firstArchiveDir) {
            archiveDir = PL_strdup(firstArchiveDir);
            archiveDir = PL_strdup(firstArchiveDir);
        }

        /* If we have an event handler, we need to archive this tag */
        if (hasEventHandler) {
            if (!id) {
            if (!id) {
                PR_fprintf(errorFD,
                           "warning: tag starting at %s:%d has event handler but"
                           " no ID attribute.  The tag will not be signed.\n",
                           filename, curitem->startLine);
                warningCount++;
                warningCount++;
            } else if (!archiveDir) {
                PR_fprintf(errorFD,
                           "warning: tag starting at %s:%d has event handler but"
                           " no ARCHIVE attribute.  The tag will not be signed.\n",
                           " no ARCHIVE attribute.  The tag will not be signed.\n",
                           filename, curitem->startLine);
                warningCount++;
            } else {
                if (SaveInlineScript(tagp->text, id, basedir, archiveDir)) {
                    goto loser;
                    goto loser;
                }
            }
        }


        switch (tagp->type) {
            case APPLET_TAG:
                if (!src) {
                    PR_fprintf(errorFD,
                               "error: APPLET tag starting on %s:%d has no CODE "
                               "error: APPLET tag starting on %s:%d has no CODE "
                               "attribute.\n",
                               filename, curitem->startLine);
                    errorCount++;
                    goto loser;
                } else if (!archiveDir) {
                } else if (!archiveDir) {
                    PR_fprintf(errorFD,
                               "error: APPLET tag starting on %s:%d has no ARCHIVE "
                               "attribute.\n",
                               filename, curitem->startLine);
                    errorCount++;
                    errorCount++;
                    goto loser;
                } else {
                    if (SaveSource(src, codebase, basedir, archiveDir)) {
                        goto loser;
                    }
                    }
                }
                break;
            case SCRIPT_TAG:
            case LINK_TAG:
            case LINK_TAG:
            case STYLE_TAG:
                if (!archiveDir) {
                    PR_fprintf(errorFD,
                               "error: %s tag starting on %s:%d has no ARCHIVE "
                               "attribute.\n",
                               "attribute.\n",
                               TagTypeToString(tagp->type),
                               filename, curitem->startLine);
                    errorCount++;
                    goto loser;
                } else if (src) {
                } else if (src) {
                    if (SaveSource(src, codebase, basedir, archiveDir)) {
                        goto loser;
                    }
                } else if (id) {
                    /* Save the next text item */
                    if (!curitem->next || (curitem->next->type !=
                                           TEXT_ITEM)) {
                        PR_fprintf(errorFD,
                                   "warning: %s tag starting on %s:%d is not followed"
                                   "warning: %s tag starting on %s:%d is not followed"
                                   " by script text.\n",
                                   TagTypeToString(tagp->type),
                                   filename, curitem->startLine);
                        warningCount++;
                        warningCount++;
                        /* just create empty file */
                        if (SaveInlineScript("", id, basedir, archiveDir)) {
                            goto loser;
                        }
                    } else {
                    } else {
                        curitem = curitem->next;
                        if (SaveInlineScript(curitem->item.text,
                                             id, basedir,
                                             archiveDir)) {
                            goto loser;
                        }
                    }
                    }
                } else {
                    /* No src or id tag--warning */
                    PR_fprintf(errorFD,
                               "warning: %s tag starting on %s:%d has no SRC or"
                               " ID attributes.  Will not sign.\n",
                               " ID attributes.  Will not sign.\n",
                               TagTypeToString(tagp->type), filename, curitem->startLine);
                    warningCount++;
                }
                break;
            default:
            default:
                /* do nothing for other tags */
                break;
                break;
        }
    }

    /* Now deal with all the unnamable scripts */
    /* Now deal with all the unnamable scripts */
    if (firstArchiveDir) {
        HTMLItem *style, *entity;

        /* Go through the lists of JS entities and style attributes.  Do them
         * in chronological order within a list.  Pick the list with the lower
         * in chronological order within a list.  Pick the list with the lower
         * endLine. In case of a tie, entities come first.
         */
        style = styleList;
        entity = entityList;
        while (style || entity) {
        while (style || entity) {
            if (!entity || (style && (style->endLine < entity->endLine))) {
                /* Process style */
                SaveUnnamableScript(style->item.text, basedir, firstArchiveDir,
                                    filename);
                style = style->next;
                style = style->next;
            } else {
                /* Process entity */
                SaveUnnamableScript(entity->item.text, basedir, firstArchiveDir,
                                    filename);
                entity = entity->next;
                entity = entity->next;
            }
        }
        }
    }

    retval = 0;
loser:
    /* Blow away the stream */
    /* Blow away the stream */
    while (head) {
        curitem = head;
        head = head->next;
        DestroyHTMLItem(curitem);
    }
    }
    while (styleList) {
        curitem = styleList;
        styleList = styleList->next;
        DestroyHTMLItem(curitem);
    }
    }
    while (entityList) {
        curitem = entityList;
        entityList = entityList->next;
        DestroyHTMLItem(curitem);
        DestroyHTMLItem(curitem);
    }
    if (text) {
        PR_Free(text);
        text = NULL;
    }
    }
    if (fb) {
        FB_Destroy(fb);
        fb = NULL;
    }
    if (fd) {
        PR_Close(fd);
    }
    }
    if (tagerr) {
        PR_smprintf_free(tagerr);
        tagerr = NULL;
    }
    if (archiveDir) {
    if (archiveDir) {
        PR_Free(archiveDir);
        archiveDir = NULL;
    }
    if (firstArchiveDir) {
        PR_Free(firstArchiveDir);
        PR_Free(firstArchiveDir);
        firstArchiveDir = NULL;
    }
    if (entityListTail) {
        PR_Free(entityListTail);
    }
    }
    if (curitem) {
        PR_Free(curitem);
    }
    if (basedir) {
        PR_Free(basedir);
        PR_Free(basedir);
    }
    return retval;
}

/**********************************************************************
/**********************************************************************
 *
 * e n s u r e E x i s t s
 * e n s u r e E x i s t s
 *
 * Check for existence of indicated directory.  If it doesn't exist,
 * it will be created.
 * Returns PR_SUCCESS if the directory is present, PR_FAILURE otherwise.
 */
 */
static PRStatus
ensureExists(char *basepath, char *path)
{
    char fn[FNSIZE];
    PRDir *dir;
    PRDir *dir;
    int c = snprintf(fn, sizeof(fn), "%s/%s", basepath, path);
    if (c >= sizeof(fn)) {
        return PR_FAILURE;
    }


    /*PR_fprintf(outputFD, "Trying to open directory %s.\n", fn);*/

    if ((dir = PR_OpenDir(fn))) {
        PR_CloseDir(dir);
        return PR_SUCCESS;
        return PR_SUCCESS;
    }
    return PR_MkDir(fn, 0777);
}


/***************************************************************************
 *
 * m a k e _ d i r s
 * m a k e _ d i r s
 *
 * Ensure that the directory portion of the path exists.  This may require
 * making the directory, and its parent, and its parent's parent, etc.
 */
static int
static int
make_dirs(char *path, int file_perms)
{
    char *Path;
    char *start;
    char *sep;
    char *sep;
    int ret = 0;
    PRFileInfo info;


    if (!path) {
    if (!path) {
        return 0;
    }


    Path = PL_strdup(path);
    if (!Path) {
        return 0;
    }


    start = strpbrk(Path, "/\\");
    if (!start) {
        goto loser;
    }
    start++; /* start right after first slash */


    /* Each time through the loop add one more directory. */
    while ((sep = strpbrk(start, "/\\"))) {
        *sep = '\0';

        if (PR_GetFileInfo(Path, &info) != PR_SUCCESS) {
        if (PR_GetFileInfo(Path, &info) != PR_SUCCESS) {
            /* No such dir, we have to create it */
            if (PR_MkDir(Path, file_perms) != PR_SUCCESS) {
                PR_fprintf(errorFD, "ERROR: Unable to create directory %s.\n",
                           Path);
                errorCount++;
                errorCount++;
                ret = -1;
                goto loser;
            }
        } else {
        } else {
            /* something exists by this name, make sure it's a directory */
            if (info.type != PR_FILE_DIRECTORY) {
                PR_fprintf(errorFD, "ERROR: Unable to create directory %s.\n",
                           Path);
                errorCount++;
                errorCount++;
                ret = -1;
                goto loser;
            }
        }


        start = sep + 1; /* start after the next slash */
        *sep = '/';
    }

loser:
loser:
    PR_Free(Path);
    return ret;
}

/*
/*
 *  c o p y i n t o
 *
 *  Function to copy file "from" to path "to".
 *
 */
 */
static int
copyinto(char *from, char *to)
{
    PRInt32 num;
    char buf[BUFSIZ];
    char buf[BUFSIZ];
    PRFileDesc *infp = NULL, *outfp = NULL;
    int retval = -1;

    if ((infp = PR_Open(from, PR_RDONLY, 0777)) == NULL) {
    if ((infp = PR_Open(from, PR_RDONLY, 0777)) == NULL) {
        PR_fprintf(errorFD, "ERROR: Unable to open \"%s\" for reading.\n",
                   from);
        errorCount++;
        goto finish;
    }
    }

    /* If to already exists, print a warning before deleting it */
    if (PR_Access(to, PR_ACCESS_EXISTS) == PR_SUCCESS) {
        PR_fprintf(errorFD, "warning: %s already exists--will overwrite\n", to);
        warningCount++;
        warningCount++;
        if (rm_dash_r(to)) {
            PR_fprintf(errorFD,
                       "ERROR: Unable to remove %s.\n", to);
            errorCount++;
            errorCount++;
            goto finish;
        }
    }

    if ((outfp = PR_Open(to, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0777)) ==
    if ((outfp = PR_Open(to, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0777)) ==
        NULL) {
        char *errBuf = NULL;

        errBuf = PR_Malloc(PR_GetErrorTextLength() + 1);
        PR_fprintf(errorFD, "ERROR: Unable to open \"%s\" for writing.\n", to);
        if (PR_GetErrorText(errBuf)) {
            PR_fprintf(errorFD, "Cause: %s\n", errBuf);
        }
        if (errBuf) {
        if (errBuf) {
            PR_Free(errBuf);
        }
        errorCount++;
        goto finish;
    }
    }

    while ((num = PR_Read(infp, buf, BUFSIZ)) > 0) {
        if (PR_Write(outfp, buf, num) != num) {
            PR_fprintf(errorFD, "ERROR: Error writing to %s.\n", to);
            errorCount++;
            errorCount++;
            goto finish;
        }
    }

    retval = 0;
    retval = 0;
finish:
    if (infp)
        PR_Close(infp);
    if (outfp)
    if (outfp)
        PR_Close(outfp);

    return retval;
}