1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814
|
/*
helpdeco -- utility program to dissect Windows help files
Copyright (C) 1997 Manfred Winterhoff
Copyright (C) 2001 Ben Collver
This file is part of helpdeco; 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
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 Place, Suite 330, Boston, MA, 02111-1307, USA or visit:
http://www.gnu.org
*/
#include "helpdeco.h"
/* neccessary compiler options for 16 bit version using Borland C/C++:
// bcc -ml -K -Os -p helpdeco.c helpdec1.c
// Don't compile using Register Calling Convention in BC3.1: compiler bug.
// To compile 32 bit version using Microsoft VC4.0 create a new workspace
// for a WIN32 console application, insert HELPDECO.C and HELPDEC1.C into
// it and compile. Use /KNOWEAS 16 bit version as stub to create dual-mode
// application.
// Byte align ! Portable to little endian machines only.
*/
FILEREF *external;
char HelpFileName[NAME_MAX];
char name[NAME_MAX];
char ext[_MAX_EXT];
FILE *AnnoFile;
HASHREC *hashrec;
int hashrecs=0;
BROWSE *browse;
int browses;
int browsenums;
long scaling;
int rounderr;
START *start;
int starts;
BOOL lzcompressed,Hall;
BOOL before31,after31;
BOOL win95;
BOOL mvp,multi;
BOOL warnings,missing;
long *Topic;
int Topics; /* 16 bit: max. 16348 Topics */
GROUP *group;
int groups;
CONTEXTREC *ContextRec;
int ContextRecs; /* 16 bit: max. 8191 Context Records */
ALTERNATIVE *alternative;
int alternatives;
BOOL overwrite=FALSE;
BOOL exportLZ77=FALSE;
BOOL extractmacros=TRUE;
BOOL guessing=TRUE;
long guessed=0L;
BOOL listtopic=FALSE;
BOOL nopagebreak=FALSE;
BOOL resolvebrowse=TRUE;
BOOL reportderived=FALSE;
BOOL checkexternal=FALSE;
BOOL exportplain=FALSE;
#define MAXKEYWORDS (64<<(sizeof(int)*2)) /* 16 bit: 1024, 32 bit: 16348 */
int NextKeywordRec,KeywordRecs;
KEYWORDREC *KeywordRec;
TOPICOFFSET NextKeywordOffset;
char helpcomp[13];
char HelpFileTitle[NAME_MAX];
char TopicTitle[256];
char *Phrases;
unsigned int *PhraseOffsets;
unsigned int PhraseCount;
long TopicFileLength;
int TopicBlockSize; /* 2k or 4k */
int DecompressSize; /* 4k or 16k */
char buffer[4096];
char keyword[512];
char index_separators[40]=",;";
char *extension;
int extensions=0;
/* index into bmpext: bit 0=multiresolution bit 1=bitmap, bit 2=metafile, bit 3=hotspot data, bit 4=embedded, bit 5=transparent */
char *bmpext[]={"???","mrb","bmp","mrb","wmf","mrb","mrb","mrb","shg","mrb","shg","mrb","shg","mrb","shg","mrb"};
char **stopwordfilename;
int stopwordfiles;
char **fontname;
int fontnames;
unsigned char DefFont;
FONTDESCRIPTOR *font;
int fonts;
unsigned char lookup[]={0,3,1,2,4,5}; /* to translate font styles */
struct { unsigned char r,g,b; } color[128];
int colors;
char **windowname;
int windownames;
BOOL NotInAnyTopic;
int TopicsPerRTF;
BOOL lists['z'-'0'+1];
BOOL keyindex['z'-'0'+1];
static signed char table[256]=
{
'\x00', '\xD1', '\xD2', '\xD3', '\xD4', '\xD5', '\xD6', '\xD7', '\xD8', '\xD9', '\xDA', '\xDB', '\xDC', '\xDD', '\xDE', '\xDF',
'\xE0', '\xE1', '\xE2', '\xE3', '\xE4', '\xE5', '\xE6', '\xE7', '\xE8', '\xE9', '\xEA', '\xEB', '\xEC', '\xED', '\xEE', '\xEF',
'\xF0', '\x0B', '\xF2', '\xF3', '\xF4', '\xF5', '\xF6', '\xF7', '\xF8', '\xF9', '\xFA', '\xFB', '\xFC', '\xFD', '\x0C', '\xFF',
'\x0A', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F',
'\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F',
'\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27', '\x28', '\x29', '\x2A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0D',
'\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F',
'\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27', '\x28', '\x29', '\x2A', '\x2B', '\x2C', '\x2D', '\x2E', '\x2F',
'\x50', '\x51', '\x52', '\x53', '\x54', '\x55', '\x56', '\x57', '\x58', '\x59', '\x5A', '\x5B', '\x5C', '\x5D', '\x5E', '\x5F',
'\x60', '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67', '\x68', '\x69', '\x6A', '\x6B', '\x6C', '\x6D', '\x6E', '\x6F',
'\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77', '\x78', '\x79', '\x7A', '\x7B', '\x7C', '\x7D', '\x7E', '\x7F',
'\x80', '\x81', '\x82', '\x83', '\x0B', '\x85', '\x86', '\x87', '\x88', '\x89', '\x8A', '\x8B', '\x8C', '\x8D', '\x8E', '\x8F',
'\x90', '\x91', '\x92', '\x93', '\x94', '\x95', '\x96', '\x97', '\x98', '\x99', '\x9A', '\x9B', '\x9C', '\x9D', '\x9E', '\x9F',
'\xA0', '\xA1', '\xA2', '\xA3', '\xA4', '\xA5', '\xA6', '\xA7', '\xA8', '\xA9', '\xAA', '\xAB', '\xAC', '\xAD', '\xAE', '\xAF',
'\xB0', '\xB1', '\xB2', '\xB3', '\xB4', '\xB5', '\xB6', '\xB7', '\xB8', '\xB9', '\xBA', '\xBB', '\xBC', '\xBD', '\xBE', '\xBF',
'\xC0', '\xC1', '\xC2', '\xC3', '\xC4', '\xC5', '\xC6', '\xC7', '\xC8', '\xC9', '\xCA', '\xCB', '\xCC', '\xCD', '\xCE', '\xCF'
};
char oldtable[256];
unsigned char untable[]={0,'1','2','3','4','5','6','7','8','9','0',0,'.','_',0,0,0,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char *prefix[]={"","idh_","helpid_",NULL,NULL,NULL,NULL,NULL};
long prefixhash[sizeof(prefix)/sizeof(prefix[0])];
FONTDESCRIPTOR CurrentFont;
long hash(char *name) /* convert 3.1/'95 topic name to hash value */
{
long hash;
unsigned char *ptr;
if(*name=='\0') return 1L;
for(hash=0L,ptr=(unsigned char *)name;*ptr;ptr++)
{
hash=hash*43L+table[*ptr];
}
return hash;
}
char *unhash(unsigned long hash) /* deliver 3.1 context id that fits hash value */
{
static char buffer[15];
int i,j,k;
unsigned long hashlo,divlo,result,mask;
unsigned char hashhi,divhi;
char ch;
i=0;
j=hashrecs;
while(i<j)
{
k=(i+j)/2;
if(hashrec[k].hash<(long)hash)
{
i=k+1;
}
else if(hashrec[k].hash>(long)hash)
{
j=k;
}
else return hashrec[k].name;
}
for(i=0;i<43;i++)
{
buffer[j=14]='\0';
hashlo=hash;
hashhi=i;
while(1)
{
divhi=21;
divlo=0x80000000UL;
result=0UL;
for(mask=0x80000000UL;mask;mask>>=1)
{
if(hashhi>divhi||(hashhi==divhi&&hashlo>=divlo))
{
result|=mask;
hashhi-=divhi;
if(divlo>hashlo) hashhi--;
hashlo-=divlo;
}
divlo>>=1;
if(divhi&1) divlo|=0x80000000UL;
divhi>>=1;
}
ch=untable[(int)hashlo];
if(!ch) break;
buffer[--j]=ch;
if(result==0) return buffer+j;
hashlo=result;
}
}
/* should never happen */
error("Can not find a matching string for hash value %08lx",hash);
sprintf(buffer,"HASH%08lx",hash);
return buffer;
}
char *ContextId(unsigned long hash) /* unhash and verify for legal entry point */
{
char *ptr;
int i;
for(i=0;i<ContextRecs;i++)
{
if(ContextRec[i].HashValue==hash)
{
return unhash(hash);
}
}
ptr=unhash(hash);
printf("Help Compiler will issue Warning 4113: Unresolved jump or popup '%s'\n",ptr);
return ptr;
}
void AddTopic(char *TopicName,BOOL derived) /* adds a known topic name to hash decode list */
{
long x;
int i,j,k;
x=hash(TopicName);
i=0;
j=hashrecs;
while(i<j)
{
k=(i+j)/2;
if(hashrec[k].hash<x)
{
i=k+1;
}
else if(hashrec[k].hash>x)
{
j=k;
}
else
{
if(stricmp(TopicName,hashrec[k].name)!=0)
{
if(!hashrec[k].derived)
{
if(!derived) fprintf(stderr,"ContextId %s already defined as %s\n",TopicName,hashrec[k].name);
return;
}
if(derived) return;
free(hashrec[k].name);
hashrec[k].name=my_strdup(TopicName);
}
if(!derived&&hashrec[k].derived)
{
guessed--;
hashrec[k].derived=FALSE;
}
return;
}
}
/* %100 to decrease memory fragmentation */
if(hashrecs%100==0) hashrec=my_realloc(hashrec,(hashrecs+100)*sizeof(HASHREC));
if(i<hashrecs) memmove(hashrec+i+1,hashrec+i,sizeof(HASHREC)*(hashrecs-i));
hashrec[i].name=my_strdup(TopicName);
hashrec[i].derived=derived;
hashrec[i].hash=x;
if(derived) guessed++;
hashrecs++;
}
/* ContextRec is sorted by TopicOffset. Binary search for an entry for
// TopicOffset topic. If more than one entry with identical TopicOffset
// is stored, return index of the first one, if not found return -1 */
int FindContext(long topic)
{
int i,lwb,upb;
lwb=0;
upb=ContextRecs;
while(lwb<upb)
{
i=(lwb+upb)/2;
if(ContextRec[i].TopicOffset>topic)
{
upb=i;
}
else if(ContextRec[i].TopicOffset<topic)
{
lwb=i+1;
}
else
{
while(i>0&&ContextRec[i-1].TopicOffset==topic) i--;
return i;
}
}
return -1;
}
/* Look if buf contains str (caseinsensitive) and update case of str so it is
// matching case appearance in buf and return TRUE, FALSE if not found */
int FindString(unsigned char *buf,int buflen,unsigned char *str,int len)
{
int i,j;
unsigned char ch;
buflen-=len;
for(i=0;i<=buflen;i++)
{
for(j=0;j<len;j++)
{
if(table[ch=buf[j]]!=table[str[j]]) break;
str[j]=ch;
}
if(j==len)
{
return TRUE;
}
buf++;
}
return FALSE;
}
/* Many people and authoring systems create context ids from topic titles,
// either by replacing every illegal char with _ or . or leaving it out,
// sometimes using only part of the topic title or prefixing it with idh_
// or helpid_. This function tries all variants and may find a context id
// matching the hash value.
// And if it doesn't derive a context id, don't bother, unhash delivers
// one matching the hash code. This code is just to please the user.
// Win95 allows for nearly every char to be used as a context ident. */
BOOL Derive(unsigned char *str,unsigned long desiredhash,char *buffer)
{
int i,j,k,l,m,n,o,p,s;
unsigned long hash,h,x,y;
char *ptr;
unsigned char ch;
l=2;
s=strlen(str);
for(i=!win95;i<l;i++) /* three variants what to do with illegal characters */
{ /* but only if an illegal character found (see below) */
for(j=0;prefix[j];j++)
{
k=0;
while(str[k])
{
hash=prefixhash[j];
strcpy(buffer,prefix[j]);
ptr=strchr(buffer,'\0');
for(m=k;str[m];m++)
{
ch=str[m];
if(i)
{
n=oldtable[ch];
if(n==0)
{
if(i==2)
{
n=oldtable[ch='_'];
}
else
{
continue;
}
}
}
else /* Win95 */
{
n=table[ch];
if(n==0) continue;
}
*ptr++=ch;
hash=43L*hash+n;
for(x=1L,o=0;o<6;o++,x*=43L)
{
h=desiredhash-x*hash;
if(h<x)
{
y=x;
p=0;
while(h)
{
y/=43L;
ch=untable[(unsigned char)(h/y)];
if(!ch) break;
h%=y;
ptr[p++]=ch;
}
if(h==0&&(FindString(str,s,ptr,p)||p<3))
{
ptr[p]='\0';
return TRUE;
}
}
}
}
if(i)
{
while(oldtable[ch=str[k]]) k++;
if(ch) l=3;
while((ch=str[k])!='\0'&&!oldtable[ch]) k++;
}
else /* win95 */
{
while(table[ch=str[k]]) k++;
if(ch) l=3;
while((ch=str[k])!='\0'&&!table[ch]) k++;
}
}
}
}
return FALSE;
}
void Guess(char *str,TOPICOFFSET topic)
{
int i,j,k,m;
long hash;
i=FindContext(topic);
if(i!=-1) /* iff there is a # footnote assigned to that topic offset */
{
do
{
hash=ContextRec[i].HashValue;
j=0;
k=hashrecs;
while(j<k)
{
m=(j+k)/2;
if(hashrec[m].hash<hash)
{
j=m+1;
}
else if(hashrec[m].hash>hash)
{
k=m;
}
else break;
}
if(j>=k) if(Derive(str,hash,buffer))
{
if(reportderived) printf("Derived %s\n",buffer);
AddTopic(buffer,TRUE);
}
}
while(++i<ContextRecs&&ContextRec[i].TopicOffset==topic);
}
}
void SysLoad(FILE *HelpFile) /* gets global values from SYSTEM file */
{
SYSTEMRECORD *SysRec;
SYSTEMHEADER SysHdr;
SECWINDOW *SWin;
int i;
char kwdata[10];
char kwbtree[10];
if(!SearchFile(HelpFile,"|SYSTEM",NULL))
{
fputs("Internal |SYSTEM file not found. Can't continue.\n",stderr);
exit(1);
}
my_fread(&SysHdr,sizeof(SysHdr),HelpFile);
before31=SysHdr.Minor<16;
after31=SysHdr.Minor>21;
multi=SysHdr.Minor==27;
lzcompressed=!before31&&(SysHdr.Flags==4||SysHdr.Flags==8);
win95=SysHdr.Minor==33&&!mvp;
windownames=0;
windowname=NULL;
if(before31)
{
DecompressSize=TopicBlockSize=2048;
}
else
{
DecompressSize=0x4000;
if(SysHdr.Flags==8)
{
TopicBlockSize=2048;
}
else
{
TopicBlockSize=4096;
}
}
if(before31)
{
my_gets(HelpFileTitle,33,HelpFile);
}
else
{
for(SysRec=GetFirstSystemRecord(HelpFile);SysRec;SysRec=GetNextSystemRecord(SysRec))
{
switch(SysRec->RecordType)
{
case 0x0001:
strlcpy(HelpFileTitle,SysRec->Data,sizeof(HelpFileTitle));
break;
case 0x0006:
SWin=(SECWINDOW *)SysRec->Data;
windowname=my_realloc(windowname,(windownames+1)*sizeof(char *));
windowname[windownames]=NULL;
if(SWin->Flags&WSYSFLAG_NAME)
{
windowname[windownames]=my_strdup(SWin->Name);
}
windownames++;
break;
case 0x000E:
keyindex[SysRec->Data[1]-'0']=TRUE;
break;
}
}
}
for(i='A';i<='z';i++)
{
sprintf(kwdata,"|%cWDATA",i);
sprintf(kwbtree,"|%cWBTREE",i);
if(SearchFile(HelpFile,kwdata,NULL)&&SearchFile(HelpFile,kwbtree,NULL))
{
lists[i-'0']=TRUE;
}
}
}
/*********************************************************************************
GRAPHICS STUFF
==============
ExtractBitmap(..) extracts the graphics stored as |bmXXX in the Windows
HelpFile to separate files. In this version multiple resolution pictures
(with and without hotspots) are saved as MRB's, pictures with hotspots are
converted to the SHG-format, single resolution Bitmaps without hotspots
to standard BMP-format and all Metafiles without hotspots to the Aldus
Placeable Metafile WMF format.
GENERAL NOTES ABOUT |bmXXX
--------------------------
|bmXXX contains one MRB-File. This can be directly recompiled. If hotspots
in the graphics contained in the MRB-File shall be added or changed with
SHED, the MRB-File has to be split into one SHG file for each graphic. This
is very easy as the graphics-data will only be slightly rewrapped. If the
graphics themselves shall be edited with a standard drawing program, the
SHG files have to be converted into bitmaps or metafiles (as flagged in
the SHG). The hotspot data is lost by this step.
MRB-FILES
---------
MRBC takes the input-file(s), converts them to "lp"-Files if needed, adds
a resolution specification if none is given in the file and finally outputs
a "lp"-File with the ending ".MRB". Depending on the given display type
MRBC sets the following resolutions:
| | | x-resolution | y-resolution
display-type | extension | std-extension | (ppi) | (ppi)
-------------+-----------+---------------+--------------+-------------
CGA | ".C*" | ".CGA" | 96 | 48
EGA | ".E*" | ".EGA" | 96 | 72
VGA | ".V*" | ".VGA" | 96 | 96
8514 | ".8*" | ".854" | 120 | 120
SHG-Files
---------
Structure of SHG-Data:
Offset | Size | Ref | Description
----------+------------+-----+-----------------------------------------------
0 | 1 Byte | | always 0x01, maybe a version-number
1 | 1 Word | N | the number of hotspots
3 | 1 DWord | M | size of macro-data (in bytes)
7 | 15xN Bytes | HSD | Hotspot-Data (see below)
7 + 15*N | M Bytes | | Macro-Data (ASCIIZ-Strings)
7+15*N+M | 2*N*ASCIIZ | | Hotspot-Id and Context- or Macro-Name as
| | | ASCIIZ-String
Structure of Hotspot-Data:
Offset | Size | Ref | Description
----------+------------+-----+-------------------------------------------------
0 | 3 Bytes | | Hotspot-Type: 0xE3 0x00 0x00 = jump, visible
| | | 0xE7 0x04 0x00 = jump, invisible
| | | 0xE2 0x00 0x00 = pop-up, visible
| | | 0xE6 0x04 0x00 = pop-up, invisible
| | | 0xC8 0x00 0x00 = macro, visible
| | | 0xCC 0x04 0x00 = macro, invisible
3 | 1 Word | | Left border of hotspot
5 | 1 Word | | Top border of hotspot
7 | 1 Word | | Right border - Left border
9 | 1 Word | | Bottom border - Top border
11 | 1 DWord | | 0x00000000 => nothing
| | | 0x00000001 => this is a macro-hotspot
| | | others => hash-value of the context-string
| | | according to the WinHelp standard
03-06-1995 Holger Haase
**********************************************************************************/
/* compares filename a[.HLP] and filename b[.HLP], returning 0 if match */
int filenamecmp(const char *a,const char *b)
{
char aname[NAME_MAX],bname[NAME_MAX];
char aext[_MAX_EXT],bext[_MAX_EXT];
int i;
_splitpath(a,NULL,NULL,aname,aext);
_splitpath(b,NULL,NULL,bname,bext);
if(aext[0]=='\0') strcpy(aext,".hlp");
if(bext[0]=='\0') strcpy(bext,".hlp");
i=strcmpi(aname,bname);
if(i) return i;
return strcmpi(aext,bext);
}
/* store external reference in list, checked later */
void StoreReference(char *filename,int type,char *id,long hash)
{
CHECKREC *ptr;
FILEREF *ref;
PLACEREC *place;
for(ref=external;ref;ref=ref->next)
{
if(filenamecmp(filename,ref->filename)==0) break;
}
if(!ref)
{
ref=my_malloc(sizeof(FILEREF)+strlen(filename));
strcpy(ref->filename,filename);
ref->check=NULL;
ref->next=external;
external=ref;
}
for(ptr=ref->check;ptr;ptr=ptr->next)
{
if(ptr->type==type&&ptr->hash==hash) break;
}
if(!ptr)
{
ptr=my_malloc(sizeof(CHECKREC));
ptr->type=type;
ptr->hash=hash;
ptr->id=my_strdup(id);
ptr->here=NULL;
ptr->next=ref->check;
ref->check=ptr;
}
if(listtopic&&TopicTitle[0])
{
place=my_malloc(sizeof(PLACEREC)+strlen(TopicTitle));
strcpy(place->topicname,TopicTitle);
place->next=ptr->here;
ptr->here=place;
}
}
/* validate each entry in list of external references */
void CheckReferences(void)
{
FILEREF *ref;
CHECKREC *ptr;
FILE *f;
BOOL found;
long offset;
int i,n;
CTXOMAPREC CTXORec;
BTREEHEADER BTreeHdr;
BTREENODEHEADER CurrNode;
CONTEXTREC ContextRec;
for(ref=external;ref;ref=ref->next)
{
f=fopen(ref->filename,"rb");
if(!f)
{
printf("%s not found\n",ref->filename);
}
else
{
if(SearchFile(f,NULL,NULL))
{
for(ptr=ref->check;ptr;ptr=ptr->next)
{
found=FALSE;
if(ptr->type==CONTEXT)
{
if(SearchFile(f,"|CTXOMAP",NULL))
{
n=my_getw(f);
for(i=0;i<n;i++)
{
my_fread(&CTXORec,sizeof(CTXORec),f);
if(CTXORec.MapID==ptr->hash) /* hash is context id */
{
found=TRUE;
break;
}
}
}
}
else
{
if(SearchFile(f,"|CONTEXT",NULL))
{
my_fread(&BTreeHdr,sizeof(BTreeHdr),f);
offset=ftell(f);
CurrNode.PreviousPage=BTreeHdr.RootPage;
for(n=1;n<BTreeHdr.NLevels;n++)
{
fseek(f,offset+CurrNode.PreviousPage*(long)BTreeHdr.PageSize,SEEK_SET);
my_fread(&CurrNode,sizeof(BTREEINDEXHEADER),f);
for(i=0;i<CurrNode.NEntries;i++)
{
ContextRec.HashValue=getdw(f);
if(ContextRec.HashValue>ptr->hash) break;
CurrNode.PreviousPage=my_getw(f); /* Page */
}
}
fseek(f,offset+CurrNode.PreviousPage*(long)BTreeHdr.PageSize,SEEK_SET);
my_fread(&CurrNode,sizeof(BTREENODEHEADER),f);
for(i=0;i<CurrNode.NEntries;i++)
{
my_fread(&ContextRec,sizeof(ContextRec),f);
if(ContextRec.HashValue==ptr->hash) found=TRUE;
if(ContextRec.HashValue>=ptr->hash) break;
}
}
}
if(!found)
{
if(ptr->id)
{
printf("%s@%s not found\n",ptr->id,ref->filename);
}
else
{
printf("0x%08lx@%s not found\n",ptr->hash,ref->filename);
}
while(ptr->here)
{
printf(" %s\n",ptr->here->topicname);
ptr->here=ptr->here->next;
}
}
}
}
else
{
printf("%s isn't a valid WinHelp file !\n",ref->filename);
}
fclose(f);
}
}
}
/* list each entry in list of external references */
void ListReferences(void)
{
FILEREF *ref;
CHECKREC *ptr;
hashrecs=0;
for(ref=external;ref;ref=ref->next)
{
for(ptr=ref->check;ptr;ptr=ptr->next)
{
printf("%s ",ref->filename);
switch(ptr->type)
{
case TOPIC:
printf("topic id ");
if(ptr->id)
{
printf("'%s'",ptr->id);
}
else
{
printf("0x%08lx='%s'",ptr->hash,unhash(ptr->hash));
}
break;
case CONTEXT:
printf("[MAP] id ");
if(ptr->id)
{
printf("'%s'",ptr->id);
}
else
{
printf("0x%08lx=(%ld)",ptr->hash,ptr->hash);
}
break;
}
if(ptr->here)
{
printf(" referenced from:");
while(ptr->here)
{
printf("\n %s",ptr->here->topicname);
ptr->here=ptr->here->next;
}
}
putchar('\n');
}
}
}
void unquote(char *ptr)
{
char *dest;
dest=ptr;
do
{
if(*ptr=='\\'&&ptr[1]!='\0')
{
ptr++;
}
}
while((*dest++=*ptr++)!='\0');
}
char *findMatchingChar(char *p) /* searches for matching '`', '"', or '('. */
{
char match = '\'';
if(*p=='"') match = '"';
else if(*p=='(') match = ')';
else if(*p!='`') return p; /* no match for this string */
for(p++;*p!='\0'&&*p!=match;p++)
{
if(*p=='\\'&&p[1]!='\0')
{
p++;
}
else if(*p=='`'||*p=='"'||*p=='(')
{
if(!*(p=findMatchingChar(p))) break; /* error: end of string */
}
}
return p;
}
/* scan macro string for topic names and external references */
BOOL CheckMacroX(char *ptr)
{
static char *macro[]=
{
"AddAccelerator(ssm","AA(ssm",
"AppendItem(sssm","AI(sssm",
"AL(ssts",
"ChangeButtonBinding(sm","CBB(sm",
"CB(ssm",
"CE(sm",
"ChangeItemBinding(sm","CIB(sm",
"ExtInsertItem(sssmss",
"IfThen(mm","IT(mm",
"IfThenElse(mmm","IE(mmm",
"ExecFile(sssm","EF(sssm",
"InsertItem(sssms","II(sssms",
"JumpContext(x","JC(x","JumpContext(fx","JC(fx",
"JumpHash(h","JH(h","JumpHash(fh","JH(fh",
"JumpId(c","JI(c","JumpId(fc","JI(fc",
"KL(ssts",
"MPrintID(c",
"Not(m",
"PopupId(c","PI(c","PopupId(fc","PI(fc",
"PopupContext(x","PC(x","PopupContext(fx","PC(fx",
"PopupHash(h","PopupHash(fh",
"SetContents(fx",
"SE(sssssc",
"SH(sssst",
"UpdateWindow(fc","UW(fc"
};
char *name;
int namelen,parms,i,n,l;
char *parm[20];
while(1)
{
while(*ptr==' ') ptr++;
if(*ptr=='\0') return TRUE;
if(!isalpha(*ptr)) return FALSE;
for(name=ptr,namelen=0;isalnum(*ptr)||*ptr=='_';namelen++) ptr++;
while(*ptr==' ') ptr++;
if(*ptr!='(') return FALSE;
ptr++;
parms=0;
while(1)
{
while(*ptr==' ') ptr++;
parm[parms]=ptr; /* save after skip spaces */
if(*ptr=='`'||*ptr=='"')
{
parm[parms++]=ptr+1; /* advance past quote */
ptr=findMatchingChar(ptr);
if(*ptr=='\0') return FALSE; /* unexpected end of string */
*ptr++='\0'; /* zap terminating quote */
while(*ptr==' '||*ptr=='\t') ptr++;
}
else if(*ptr!=')'&&*ptr!='\0')
{
parm[parms++]=ptr;
while(*ptr!=','&&*ptr!=')'&&*ptr!='\0')
{
if(*ptr=='('||*ptr=='`'||*ptr=='"') /* `, " probably not needed */
{
ptr=findMatchingChar(ptr);
if(*ptr=='\0') return FALSE;
}
ptr++;
}
}
if(*ptr==')') break;
if(*ptr!=',') return FALSE;
*ptr++='\0';
}
*ptr++='\0';
for(i=0;i<sizeof(macro)/sizeof(macro[0]);i++)
{
if(strcspn(macro[i],"(")==namelen&&memicmp(macro[i],name,namelen)==0&&strlen(macro[i]+namelen+1)>=parms) break;
}
if(i<sizeof(macro)/sizeof(macro[0])) /* macro of interest */
{
char *at;
for(n=0;n<parms;n++) unquote(parm[n]);
for(n=0;n<parms;n++)
{
if(macro[i][namelen+1+n]=='m')
{
CheckMacroX(parm[n]); /* recursive */
}
else if(macro[i][namelen+1+n]=='c')
{
if(extractmacros)
{
while(parm[n][0]==' ') parm[n]++;
for(l=strlen(parm[n]);l>0&&parm[n][l-1]==' ';l--) ;
parm[n][l]='\0';
AddTopic(parm[n],FALSE);
}
}
else if(macro[i][namelen+1+n]=='f')
{
at=strchr(parm[n],'>');
if(at)
{
if(filenamecmp(parm[n],name)!=0)
{
if(macro[i][namelen+2+n]=='c')
{
StoreReference(parm[n],TOPIC,parm[n+1],hash(parm[n+1]));
n++;
}
else if(macro[i][namelen+2+n]=='x')
{
StoreReference(parm[n],CONTEXT,parm[n+1],strtoul(parm[n+1],NULL,0));
n++;
}
else if(macro[i][namelen+2+n]=='h')
{
StoreReference(parm[n],TOPIC,parm[n+1],strtoul(parm[n+1],NULL,0));
n++;
}
}
}
}
else if(macro[i][namelen+1+n]=='t')
{
at=strchr(parm[n],'@');
if(at)
{
if(filenamecmp(at+1,name)!=0)
{
*at='\0';
StoreReference(at+1,TOPIC,parm[n],hash(parm[n]));
}
else
{
AddTopic(parm[n],FALSE);
}
}
else
{
AddTopic(parm[n],FALSE);
}
}
}
}
while(*ptr==' ') ptr++;
if(*ptr!=':'&&*ptr!=';') break;
ptr++;
}
return TRUE;
}
void CheckMacro(char *ptr)
{
char *temp;
if(!multi)
{
temp=my_strdup(ptr);
if(!CheckMacroX(temp)) fprintf(stderr,"Bad macro: %s\n",ptr);
free(temp);
}
}
/* check hotspot info of bitmap for topic names and external references and
// extract (write to file) if checkexternal not set, write out first bitmap
// or metafile only (no SHG/MRB) if exportplain (create lookalike rtf) set */
int ExtractBitmap(char *szFilename,MFILE *f)
{
FILE *fTarget;
char *filename;
int type;
unsigned int i,n,j;
unsigned char byType,byPacked;
long l,pos,offset,nextpict,FileStart;
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
APMFILEHEADER afh;
unsigned short *wp;
unsigned short wMagic,mapmode,colors;
unsigned long dwRawSize,dwDataSize,dwHotspotOffset,dwOffsBitmap,dwHotspotSize,dwPictureOffset,xPels,yPels;
FileStart=f->tell(f);
wMagic=GetWord(f);
if(wMagic!=0x506C /* SHG */ &&wMagic!=0x706C /* MRB */)
{
error("Unknown magic 0x%04X (%c%c)",wMagic,wMagic&0x00FF,wMagic>>8);
return 0;
}
fTarget=NULL;
n=GetWord(f);
type=!exportplain&&n>1; /* contains multiple resolutions */
/* do not depend on wMagic, because it is sometimes incorrect */
nextpict=4+4*n;
for(j=0;j<n;j++)
{
f->seek(f,FileStart+4+4*j);
dwOffsBitmap=GetDWord(f);
f->seek(f,FileStart+dwOffsBitmap);
byType=f->get(f); /* type of picture: 5=DDB, 6=DIB, 8=METAFILE */
byPacked=f->get(f); /* packing method: 0=unpacked, 1=RunLen, 2=LZ77, 3=both */
if((byType==6&&byPacked<4)||(byType==5&&byPacked<2))
{
type|=2; /* contains bitmap */
memset(&bmfh,0,sizeof(bmfh));
memset(&bmih,0,sizeof(bmih));
bmfh.bfType=0x4D42; /* bitmap magic ("BM") */
bmih.biSize=sizeof(bmih);
xPels=GetCDWord(f);
/* HC30 doesn't like certain PelsPerMeter */
if(!before31) bmih.biXPelsPerMeter=(xPels*79L+1)/2;
yPels=GetCDWord(f);
if(!before31) bmih.biYPelsPerMeter=(yPels*79L+1)/2;
bmih.biPlanes=GetCWord(f);
bmih.biBitCount=GetCWord(f);
bmih.biWidth=GetCDWord(f);
bmih.biHeight=GetCDWord(f);
colors=(int)(bmih.biClrUsed=GetCDWord(f));
if(!colors) colors=(unsigned short)1<<bmih.biBitCount;
bmih.biClrImportant=GetCDWord(f);
if(after31&&bmih.biClrImportant==1) type|=0x20; /* contains transparent bitmap */
dwDataSize=GetCDWord(f);
dwHotspotSize=GetCDWord(f);
dwPictureOffset=GetDWord(f);
dwHotspotOffset=GetDWord(f);
if((exportplain||n==1)&&(dwHotspotOffset==0L||dwHotspotSize==0L))
{
if(checkexternal) break;
strcat(szFilename,".bmp");
fTarget=my_fopen(szFilename,"wb");
if(fTarget)
{
fwrite(&bmfh,1,sizeof(bmfh),fTarget);
fwrite(&bmih,1,sizeof(bmih),fTarget);
if(byType==6)
{
CopyBytes(f,colors*4L,fTarget);
}
else
{
putdw(0x000000L,fTarget);
putdw(0xFFFFFFL,fTarget);
}
bmfh.bfOffBits=sizeof(bmfh)+sizeof(bmih)+colors*4L;
bmih.biSizeImage=(((bmih.biWidth*bmih.biBitCount+31)/32)*4)*bmih.biHeight;
if(byType==5) /* convert DDB to DIB */
{
long width,length;
unsigned char count,value;
int pad;
width=((bmih.biWidth*bmih.biBitCount+15)/16)*2;
pad=(int)(((width+3)/4)*4-width);
count=value=0;
for(l=0;l<bmih.biHeight;l++)
{
if(byPacked==1)
{
for(length=0;length<width;length++)
{
if((count&0x7F)==0)
{
count=f->get(f);
value=f->get(f);
}
else if(count&0x80)
{
value=f->get(f);
}
putc(value,fTarget);
count--;
}
}
else
{
CopyBytes(f,width,fTarget);
}
if(pad) fwrite(buffer,pad,1,fTarget);
}
}
else
{
DecompressIntoFile(byPacked,f,dwDataSize,fTarget);
}
/* update bitmap headers */
bmfh.bfSize=ftell(fTarget);
fseek(fTarget,0L,SEEK_SET);
fwrite(&bmfh,1,sizeof(bmfh),fTarget);
fwrite(&bmih,1,sizeof(bmih),fTarget);
}
break;
}
}
else if(byType==8&&byPacked<4) /* Windows MetaFile */
{
type|=4; /* contains metafile */
memset(&afh,0,sizeof(afh));
mapmode=GetCWord(f); /* mapping mode */
afh.rcBBox.right=GetWord(f); /* width of metafile-picture */
afh.rcBBox.bottom=GetWord(f); /* height of metafile-picture */
dwRawSize=GetCDWord(f);
dwDataSize=GetCDWord(f);
dwHotspotSize=GetCDWord(f);
dwPictureOffset=GetDWord(f);
dwHotspotOffset=GetDWord(f);
if((exportplain||n==1)&&(dwHotspotOffset==0L||dwHotspotSize==0L))
{
if(checkexternal) break;
afh.dwKey=0x9AC6CDD7L;
afh.wInch=2540;
wp=(unsigned short *)&afh;
for(i=0;i<10;i++) afh.wChecksum^=*wp++;
strcat(szFilename,".wmf");
fTarget=my_fopen(szFilename,"wb");
if(fTarget)
{
fwrite(&afh,1,sizeof(afh),fTarget);
DecompressIntoFile(byPacked,f,dwDataSize,fTarget);
}
break;
}
}
else
{
error("Unknown format (%d) or packing method (%d)",byType,byPacked);
break;
}
type|=8; /* contains hotspot info (set before accessing bmpext) */
if(!checkexternal)
{
if(!fTarget)
{
strcat(szFilename,".");
strcat(szFilename,bmpext[type&0x0F]);
fTarget=my_fopen(szFilename,"wb");
if(!fTarget) break;
my_putw(wMagic,fTarget);
my_putw(n,fTarget);
}
fseek(fTarget,4+4*j,SEEK_SET);
putdw(nextpict,fTarget);
fseek(fTarget,nextpict,SEEK_SET);
putc(byType,fTarget);
if(exportLZ77)
{
putc(byPacked,fTarget);
}
else
{
putc(byPacked&1,fTarget);
}
if(byType==8)
{
putcw(mapmode,fTarget); /* mapping mode */
my_putw(afh.rcBBox.right,fTarget); /* width of metafile-picture */
my_putw(afh.rcBBox.bottom,fTarget); /* height of metafile-picture */
putcdw(dwRawSize,fTarget);
}
else
{
putcdw(xPels,fTarget);
putcdw(yPels,fTarget);
putcw(bmih.biPlanes,fTarget);
putcw(bmih.biBitCount,fTarget);
putcdw(bmih.biWidth,fTarget);
putcdw(bmih.biHeight,fTarget);
putcdw(bmih.biClrUsed,fTarget);
putcdw(bmih.biClrImportant,fTarget);
}
pos=ftell(fTarget);
putdw(0,fTarget); /* changed later ! */
putdw(0,fTarget); /* changed later ! */
putdw(0,fTarget); /* changed later ! */
putdw(0,fTarget); /* changed later ! */
if(byType==6) CopyBytes(f,colors*4L,fTarget);
offset=ftell(fTarget);
f->seek(f,FileStart+dwOffsBitmap+dwPictureOffset);
if(exportLZ77)
{
dwDataSize=CopyBytes(f,dwDataSize,fTarget);
}
else
{
dwDataSize=DecompressIntoFile(byPacked&2,f,dwDataSize,fTarget);
}
}
if(dwHotspotSize)
{
f->seek(f,FileStart+dwOffsBitmap+dwHotspotOffset);
if(f->get(f)!=1)
{
fputs("No hotspots\n",stderr);
dwHotspotSize=0L;
}
else
{
unsigned int hotspots,n,j,l;
unsigned long MacroDataSize;
char *ptr;
HOTSPOT *hotspot;
hotspots=GetWord(f);
MacroDataSize=GetDWord(f);
hotspot=my_malloc(hotspots*sizeof(HOTSPOT));
f->read(f,hotspot,sizeof(HOTSPOT)*hotspots);
if(checkexternal)
{
while(MacroDataSize-->0) f->get(f);
}
else
{
putc(1,fTarget);
my_putw(hotspots,fTarget);
putdw(MacroDataSize,fTarget);
fwrite(hotspot,sizeof(HOTSPOT),hotspots,fTarget);
if(MacroDataSize) CopyBytes(f,MacroDataSize,fTarget);
}
for(n=0;n<hotspots;n++)
{
j=StringRead(buffer,sizeof(buffer),f)+1;
l=j+StringRead(buffer+j,sizeof(buffer)-j,f)+1;
if(fTarget) fwrite(buffer,l,1,fTarget);
if(extractmacros) switch(hotspot[n].id0)
{
case 0xC8: /* macro (never seen) */
case 0xCC: /* macro without font change */
CheckMacro(buffer+j);
break;
case 0xE0: /* popup jump HC30 */
case 0xE1: /* topic jump HC30 */
case 0xE2: /* popup jump HC31 */
case 0xE3: /* topic jump HC31 */
case 0xE6: /* popup jump without font change */
case 0xE7: /* topic jump without font change */
if(hash(buffer+j)!=hotspot[n].hash)
{
fprintf(stderr,"Wrong hash %08lx instead %08lx for '%s'\n",hotspot[n].hash,hash(buffer+j),buffer+j);
}
AddTopic(buffer+j,FALSE);
break;
case 0xEA: /* popup jump into external file */
case 0xEB: /* topic jump into external file / secondary window */
case 0xEE: /* popup jump into external file without font change */
case 0xEF: /* topic jump into external file / secondary window without font change */
if((hotspot[n].id1!=0&&hotspot[n].id1!=1&&hotspot[n].id1!=4&&hotspot[n].id1!=6)||(hotspot[n].id2!=0))
{
}
else
{
filename=strchr(buffer+j,'@');
if(filename) *filename++='\0';
ptr=strchr(buffer+j,'>');
if(ptr) *ptr='\0';
if(filename)
{
StoreReference(filename,TOPIC,buffer+j,hash(buffer+j));
}
else
{
AddTopic(buffer+j,FALSE);
}
break;
}
default:
error("Unknown hotspot %02x %02x %02x X=%u Y=%u W=%u H=%u %08lx,%s,%s",hotspot[n].id0,hotspot[n].id1,hotspot[n].id2,hotspot[n].x,hotspot[n].y,hotspot[n].w,hotspot[n].h,hotspot[n].hash,buffer,buffer+j);
}
}
free(hotspot);
}
}
if(!checkexternal)
{
dwPictureOffset=offset-nextpict;
nextpict=ftell(fTarget);
/* fix up some locations */
fseek(fTarget,pos,SEEK_SET);
putdw((dwDataSize<<1)+1,fTarget);
putdw((dwHotspotSize<<1)+1,fTarget);
putdw(dwPictureOffset,fTarget);
if(dwHotspotSize) putdw(dwPictureOffset+dwDataSize,fTarget);
}
}
if(fTarget) my_fclose(fTarget);
return type;
}
/****************************************************************************
END OF GRAPHICS STUFF
**************************************************************************/
char *getbitmapname(unsigned int n) /* retrieve extension of exported bitmap n */
{
static char name[12];
if(n<extensions&&extension[n])
{
sprintf(name,"bm%u.%s",n,bmpext[extension[n]&0x0F]);
}
else if(n==65535U)
{
missing=TRUE;
fputs("There was a picture file rejected on creation of helpfile.\n",stderr);
strcpy(name,"missing.bmp");
}
else /* should never happen */
{
warnings=TRUE;
fprintf(stderr,"Bitmap bm%u not exported\n",n);
sprintf(name,"bm%u.bmp",n);
}
return name;
}
void ListBitmaps(FILE *hpj) /* writes out [BITMAPS] section */
{
int i;
if(hpj&&extensions)
{
fputs("[BITMAPS]\n",hpj);
for(i=0;i<extensions;i++) if(extension[i])
{
fprintf(hpj,"bm%u.%s\n",i,bmpext[extension[i]&0x0F]);
}
putc('\n',hpj);
}
}
void ExportBitmaps(FILE *HelpFile) /* export all bitmaps */
{
BUFFER buf;
MFILE *mf;
char *leader;
char FileName[20];
long FileLength;
int i,num,n,type;
long savepos;
leader="|bm"+before31;
SearchFile(HelpFile,NULL,NULL);
for(n=GetFirstPage(HelpFile,&buf,NULL);n;n=GetNextPage(HelpFile,&buf))
{
for(i=0;i<n;i++)
{
my_gets(FileName,sizeof(FileName),HelpFile);
getdw(HelpFile);
if(memcmp(FileName,leader,strlen(leader))==0)
{
savepos=ftell(HelpFile);
if(SearchFile(HelpFile,FileName,&FileLength))
{
mf=CreateVirtual(HelpFile);
type=ExtractBitmap(FileName+(FileName[0]=='|'),mf);
CloseMap(mf);
if(type)
{
num=atoi(FileName+(FileName[0]=='|')+2);
if(num>=extensions)
{
extension=my_realloc(extension,(num+1)*sizeof(char));
while(extensions<=num) extension[extensions++]=0;
}
extension[num]=type;
}
}
fseek(HelpFile,savepos,SEEK_SET);
}
}
}
}
char *TopicName(long topic)
{
static char name[20];
int i;
if(before31)
{
if(topic==0L) topic=Topic[0];
for(i=16;i<Topics;i++) if(Topic[i]==topic)
{
sprintf(name,"TOPIC%d",i);
return name;
}
}
else
{
if(topic==-1L)
{
NotInAnyTopic=TRUE;
return "21KSYK4"; /* evaluates to -1 without generating help compiler warning */
}
i=FindContext(topic);
if(i!=-1)
{
return unhash(ContextRec[i].HashValue);
}
}
if(topic) fprintf(stderr,"Can not find topic offset %08lx\n",topic);
return NULL;
}
char *GetWindowName(long n) /* secondary window name from window number */
{
if(windowname==NULL||n<0||n>=windownames||windowname[n]==NULL) return "main";
return windowname[n];
}
/* create HPJ file from contents of |SYSTEM internal file */
void SysList(FILE *HelpFile,FILE *hpj,char *IconFileName)
{
long FileLength;
SYSTEMHEADER SysHdr;
SYSTEMRECORD *SysRec;
STOPHEADER StopHdr;
char name[51];
char *ptr;
long color;
FILE *f;
int fbreak,macro,windows,i,keywords,dllmaps,n;
if(hpj&&SearchFile(HelpFile,"|SYSTEM",NULL))
{
my_fread(&SysHdr,sizeof(SysHdr),HelpFile);
if(SysHdr.Minor==15)
{
strcpy(helpcomp,"HC30");
}
else if(SysHdr.Minor==21)
{
strcpy(helpcomp,"HC31/HCP");
}
else if(SysHdr.Minor==27)
{
strcpy(helpcomp,"WMVC/MVCC");
}
else if(SysHdr.Minor==33)
{
if(mvp)
{
strcpy(helpcomp,"MVC");
}
else
{
strcpy(helpcomp,"HCRTF");
}
}
fputs("[OPTIONS]\n",hpj);
if(before31) /* If 3.0 get title */
{
my_gets(HelpFileTitle,33,HelpFile);
if(HelpFileTitle[0]!='\0'&&HelpFileTitle[0]!='\n')
{
fprintf(hpj,"TITLE=%s\n",HelpFileTitle);
}
fprintf(hpj,"INDEX=%s\n",TopicName(0L));
if(PhraseCount)
{
fputs("COMPRESS=TRUE\n",hpj);
}
else
{
fputs("COMPRESS=FALSE\n",hpj);
}
for(i='A';i<='z';i++) if(lists[i-'0']&&i!='K')
{
fprintf(hpj,"MULTIKEY=%c\n",i);
}
putc('\n',hpj);
}
else /* else get 3.1 System records */
{
macro=0;
fbreak=0;
windows=0;
keywords=0;
dllmaps=0;
for(SysRec=GetFirstSystemRecord(HelpFile);SysRec;SysRec=GetNextSystemRecord(SysRec))
{
switch(SysRec->RecordType)
{
case 0x0001:
if(SysRec->Data[0]) fprintf(hpj,"TITLE=%s\n",SysRec->Data);
break;
case 0x0002:
ptr=strchr(SysRec->Data,'\r');
if(ptr) strcpy(ptr,"%date");
if(SysRec->Data[0]) fprintf(hpj,"COPYRIGHT=%s\n",SysRec->Data);
break;
case 0x0003:
ptr=TopicName(*(long *)SysRec->Data);
if(ptr) fprintf(hpj,"CONTENTS=%s\n",ptr);
break;
case 0x0004:
macro=1;
break;
case 0x0005:
fprintf(hpj,"ICON=%s\n",IconFileName);
f=my_fopen(IconFileName,"wb");
if(f)
{
fwrite(SysRec->Data,SysRec->DataSize,1,f);
my_fclose(f);
}
break;
case 0x0006:
windows++;
break;
case 0x0008:
if(SysRec->Data[0]) fprintf(hpj,"CITATION=%s\n",SysRec->Data);
break;
case 0x0009:
if(!mvp) fprintf(hpj,"LCID=0x%X 0x%X 0x%X\n",*(short *)(SysRec->Data+8),*(short *)SysRec->Data,*(short *)(SysRec->Data+2));
break;
case 0x000A:
if(!mvp&&SysRec->Data[0]) fprintf(hpj,"CNT=%s\n",SysRec->Data);
break;
case 0x000B:
// if(!mvp) fprintf(hpj,"CHARSET=%d\n",*(unsigned char *)(SysRec->Data+1));
break;
case 0x000C:
if(mvp)
{
fbreak=1;
}
else
{
fprintf(hpj,"DEFFONT=%s,%d,%d\n",SysRec->Data+2,*(unsigned char *)SysRec->Data,*(unsigned char *)(SysRec->Data+1));
}
break;
case 0x000D:
if(mvp) groups++;
break;
case 0x000E:
if(mvp)
{
keywords=1;
}
else
{
fprintf(hpj,"INDEX_SEPARATORS=\"%s\"\n",SysRec->Data);
strlcpy(index_separators,SysRec->Data,sizeof(index_separators));
}
break;
case 0x0012:
if(SysRec->Data[0]) fprintf(hpj,"LANGUAGE=%s\n",SysRec->Data);
break;
case 0x0013:
dllmaps=1;
break;
}
}
if(win95)
{
i=0;
if(lzcompressed) i|=8;
if(Hall) i|=4; else if(PhraseCount) i|=2;
fprintf(hpj,"COMPRESS=%d\n",i);
}
else if(!lzcompressed)
{
fputs("COMPRESS=OFF\n",hpj);
}
else if(PhraseCount)
{
fputs("COMPRESS=HIGH\n",hpj);
}
else
{
fputs("COMPRESS=MEDIUM\n",hpj);
}
if(SysHdr.Flags==8) fputs("CDROMOPT=TRUE\n",hpj);
for(i='A';i<='z';i++) if(lists[i-'0']&&i!='K'&&(i!='A'||!win95))
{
fprintf(hpj,"MULTIKEY=%c\n",i);
}
putc('\n',hpj);
if(windows)
{
fputs("[WINDOWS]\n",hpj);
for(SysRec=GetFirstSystemRecord(HelpFile);SysRec;SysRec=GetNextSystemRecord(SysRec))
{
if(SysRec->RecordType==0x0006)
{
if(SysRec->DataSize==sizeof(SECWINDOW))
{
PrintWindow(hpj,(SECWINDOW *)SysRec->Data);
}
else
{
PrintMVBWindow(hpj,(MVBWINDOW *)SysRec->Data);
}
}
}
putc('\n',hpj);
}
if(macro)
{
fputs("[CONFIG]\n",hpj);
for(SysRec=GetFirstSystemRecord(HelpFile);SysRec;SysRec=GetNextSystemRecord(SysRec))
{
if(SysRec->RecordType==0x0004)
{
if(sscanf(SysRec->Data,"SPC(%ld)%n",&color,&n)>0)
{
fprintf(hpj,"SPC(%u,%u,%u)%s\n",(unsigned char)(color),(unsigned char)(color>>8),(unsigned char)(color>>16),SysRec->Data+n);
}
else
{
fprintf(hpj,"%s\n",SysRec->Data);
}
}
}
putc('\n',hpj);
}
if(fbreak)
{
fputs("[FTINDEX]\n",hpj);
for(SysRec=GetFirstSystemRecord(HelpFile);SysRec;SysRec=GetNextSystemRecord(SysRec))
{
if(SysRec->RecordType==0x000C)
{
ptr=strtok(SysRec->Data," ");
if(ptr)
{
fprintf(hpj,"dtype%s",ptr);
ptr=strtok(NULL," ");
if(ptr)
{
fprintf(hpj,"=%s",ptr);
ptr=strtok(NULL," ");
if(ptr)
{
fprintf(hpj,"!%s",ptr);
ptr=strtok(NULL," ");
if(ptr)
{
fprintf(hpj,",%s",ptr+1);
if(SearchFile(HelpFile,ptr,NULL))
{
for(n=0;n<stopwordfiles;n++)
{
if(strcmp(stopwordfilename[n],ptr)==0) break;
}
if(n==stopwordfiles)
{
stopwordfilename=my_realloc(stopwordfilename,(stopwordfiles+1)*sizeof(char *));
stopwordfilename[stopwordfiles++]=my_strdup(ptr);
f=my_fopen(ptr+1,"wt");
if(f)
{
my_fread(&StopHdr,sizeof(StopHdr),HelpFile);
for(n=0;n<StopHdr.BytesUsed;n+=1+strlen(buffer))
{
i=getc(HelpFile);
my_fread(buffer,i,HelpFile);
buffer[i]='\0';
fprintf(f,"%s\n",buffer);
}
my_fclose(f);
}
}
}
ptr=strtok(NULL," ");
if(ptr) fprintf(hpj,",%s",ptr);
}
}
}
putc('\n',hpj);
}
}
}
putc('\n',hpj);
}
if((groups||multi)&&(browsenums>1))
{
group=my_malloc(groups*sizeof(GROUP));
fputs("[GROUPS]\n",hpj);
i=0;
for(SysRec=GetFirstSystemRecord(HelpFile);SysRec;SysRec=GetNextSystemRecord(SysRec))
{
if(SysRec->RecordType==0x000D)
{
ptr=strchr(SysRec->Data,' ');
if(ptr) *ptr++='\0';
groups=SearchFile(HelpFile,SysRec->Data,NULL);
n=strcspn(SysRec->Data,".");
SysRec->Data[n]='\0';
if(ptr&&strcmp(ptr,"\"\" ")==0)
{
fprintf(hpj,"group=%s\n",SysRec->Data);
}
else
{
fprintf(hpj,"group=%s,%s\n",SysRec->Data,ptr);
}
group[i].Name=my_strdup(SysRec->Data);
if(groups)
{
my_fread(&group[i].GroupHeader,sizeof(group[i].GroupHeader),HelpFile);
if(group[i].GroupHeader.GroupType==2)
{
group[i].Bitmap=my_malloc(group[i].GroupHeader.BitmapSize);
my_fread(group[i].Bitmap,group[i].GroupHeader.BitmapSize,HelpFile);
}
}
else
{
group[i].GroupHeader.GroupType=0;
}
i++;
}
}
if(multi) for(i=1;i<browsenums;i++) fprintf(hpj,"group=BROWSE%04x\n",i);
if(SearchFile(HelpFile,"|GMACROS",&FileLength))
{
long len;
long pos;
long off;
getdw(HelpFile); /* possible count or group number */
for(pos=4L;pos<FileLength;pos+=len)
{
len=getdw(HelpFile); /* length of record containing two longs and two strings */
off=getdw(HelpFile); /* offset of second string in record (first is at pos 8) */
if(off<=0L) off=len;
if(len<8) break;
if(len<off) break;
if(off>8)
{
my_fread(buffer,off-8,HelpFile);
buffer[off-8]='\0';
fprintf(hpj,"entry=%s\n",buffer);
}
if(len>off)
{
my_fread(buffer,len-off,HelpFile);
buffer[len-off]='\0';
fprintf(hpj,"exit=%s\n",buffer);
}
}
}
putc('\n',hpj);
}
if(dllmaps)
{
fputs("[DLLMAPS]\n",hpj);
for(SysRec=GetFirstSystemRecord(HelpFile);SysRec;SysRec=GetNextSystemRecord(SysRec))
{
if(SysRec->RecordType==0x0013)
{
if(strcmp(SysRec->Data,"MVMCI")!=0&&strcmp(SysRec->Data,"MVIMAGE")!=0&&strcmp(SysRec->Data,"MVBRKR")!=0)
{
ptr=SysRec->Data+strlen(SysRec->Data)+1;
fprintf(hpj,"%s=%s,",SysRec->Data,ptr);
ptr=ptr+strlen(ptr)+1;
fprintf(hpj,"%s,",ptr);
ptr=ptr+strlen(ptr)+1;
fprintf(hpj,"%s,",ptr);
ptr=ptr+strlen(ptr)+1;
fprintf(hpj,"%s\n",ptr);
}
}
}
putc('\n',hpj);
}
if(keywords)
{
fputs("[KEYINDEX]\n",hpj);
for(SysRec=GetFirstSystemRecord(HelpFile);SysRec;SysRec=GetNextSystemRecord(SysRec))
{
if(SysRec->RecordType==0x000E)
{
fprintf(hpj,"keyword=%c,\"%s\"\n",SysRec->Data[1],SysRec->Data+30);
}
}
putc('\n',hpj);
}
for(i=0;i<windows;i++)
{
sprintf(name,"|CF%d",i);
if(SearchFile(HelpFile,name,&FileLength))
{
fprintf(hpj,"[CONFIG:%d]\n",i);
/* may use [CONFIG-GetWindowName] instead, but WindowName need not be defined */
for(n=0;n<FileLength;n+=strlen(buffer)+1)
{
my_gets(buffer,sizeof(buffer),HelpFile);
fprintf(hpj,"%s\n",buffer);
}
putc('\n',hpj);
}
}
}
}
}
/* load phrases for decompression from old Phrases file or new PhrIndex,
// PhrImage files of HCRTF */
BOOL PhraseLoad(FILE *HelpFile)
{
long FileLength;
char junk[30];
BOOL newphrases;
PHRINDEXHDR PhrIndexHdr;
unsigned int n;
long l,offset;
long SavePos;
if(SearchFile(HelpFile,"|PhrIndex",NULL))
{
my_fread(&PhrIndexHdr,sizeof(PhrIndexHdr),HelpFile);
SavePos=ftell(HelpFile);
if(SearchFile(HelpFile,"|PhrImage",&FileLength))
{
if(FileLength!=PhrIndexHdr.phrimagecompressedsize)
{
fprintf(stderr,"PhrImage FileSize %ld, in PhrIndex.FileHdr %ld\n",PhrIndexHdr.phrimagecompressedsize,FileLength);
}
PhraseCount=(unsigned int)PhrIndexHdr.entries;
PhraseOffsets=my_malloc(sizeof(unsigned int)*(PhraseCount+1));
Phrases=my_malloc(PhrIndexHdr.phrimagesize);
if(PhrIndexHdr.phrimagesize==PhrIndexHdr.phrimagecompressedsize)
{
my_fread(Phrases,PhrIndexHdr.phrimagesize,HelpFile);
}
else
{
DecompressIntoBuffer(2,HelpFile,FileLength,Phrases,PhrIndexHdr.phrimagesize);
}
fseek(HelpFile,SavePos,SEEK_SET);
GetBit(NULL);
offset=0;
PhraseOffsets[0]=offset;
for(l=0;l<PhrIndexHdr.entries;l++)
{
for(n=1;GetBit(HelpFile);n+=1<<PhrIndexHdr.bits) ;
if(GetBit(HelpFile)) n+=1;
if(PhrIndexHdr.bits>1) if(GetBit(HelpFile)) n+=2;
if(PhrIndexHdr.bits>2) if(GetBit(HelpFile)) n+=4;
if(PhrIndexHdr.bits>3) if(GetBit(HelpFile)) n+=8;
if(PhrIndexHdr.bits>4) if(GetBit(HelpFile)) n+=16;
offset+=n;
PhraseOffsets[(int)l+1]=offset;
}
}
Hall=TRUE;
fprintf(stderr,"%u phrases loaded\n",PhraseCount);
}
else if(SearchFile(HelpFile,"|Phrases",&FileLength))
{
PhraseCount=my_getw(HelpFile);
newphrases=PhraseCount==0x0800; /* VC4.0: MSDEV\HELP\MSDEV40.MVB */
if(newphrases) PhraseCount=my_getw(HelpFile);
if(my_getw(HelpFile)!=0x0100)
{
error("Unknown |Phrases file structure");
return FALSE;
}
if(PhraseCount)
{
if(before31)
{
offset=(PhraseCount+1)*sizeof(short);
FileLength-=(PhraseCount+1)*sizeof(short)+4;
l=FileLength;
}
else
{
l=getdw(HelpFile);
if(newphrases)
{
my_fread(&junk,sizeof(junk),HelpFile);
offset=(PhraseCount+1)*sizeof(short);
FileLength-=(PhraseCount+1)*sizeof(short)+sizeof(junk)+10;
}
else
{
offset=(PhraseCount+1)*sizeof(short);
FileLength-=(PhraseCount+1)*sizeof(short)+8;
}
}
PhraseOffsets=my_malloc(sizeof(unsigned int)*(PhraseCount+1));
for(n=0;n<=PhraseCount;n++) PhraseOffsets[n]=my_getw(HelpFile)-offset;
Phrases=my_malloc(l);
DecompressIntoBuffer((before31?0:2),HelpFile,FileLength,Phrases,l);
fprintf(stderr,"%u phrases loaded\n",PhraseCount);
}
Hall=FALSE;
}
return TRUE;
}
/* write phrase PhraseNum to out and returns advanced out
// or to f it out = NULL or uses PrintString if f = NULL, returns NULL then */
char *PrintPhrase(unsigned int PhraseNum,char *out,FILE *f)
{
char *ptr;
size_t len;
if(PhraseNum>=PhraseCount)
{
error("Phrase %u does not exist",PhraseNum);
return out;
}
ptr=Phrases+PhraseOffsets[PhraseNum];
len=PhraseOffsets[PhraseNum+1]-PhraseOffsets[PhraseNum];
if(out)
{
memcpy(out,ptr,len);
return out+len;
}
if(f)
{
fwrite(ptr,len,1,f);
}
else
{
PrintString(ptr,len);
}
return NULL;
}
/* writeout .PH file from already loaded phrases */
void PhraseList(char *FileName)
{
FILE *f;
unsigned int n;
if(PhraseCount)
{
f=my_fopen(FileName,"wt");
if(f)
{
for(n=0;n<PhraseCount;n++)
{
PrintPhrase(n,NULL,f);
putc('\n',f);
}
my_fclose(f);
}
}
}
char *FontFamily(unsigned int i)
{
static char *familyname[]={"nil","roman","swiss","modern","script","decor","tech"};
if(i>0&&i<7) return familyname[i];
return familyname[0];
}
/* collect another color into color table */
unsigned char AddColor(unsigned char r,unsigned char g,unsigned char b)
{
int n;
for(n=0;n<colors;n++)
{
if(r==color[n].r&&g==color[n].g&&b==color[n].b) break;
}
if(n==colors)
{
color[colors].r=r;
color[colors].g=g;
color[colors].b=b;
colors++;
}
return n;
}
/* load fonts from help file, filling up internal font structure,
// writing fonttbl, colortbl, and styletbl to rtf file */
void FontLoad(FILE *HelpFile,FILE *rtf,FILE *hpj)
{
static char *BestFonts[]={"Arial","Times New Roman","MS Sans Serif","MS Serif","Helv","TmsRmn","MS Sans Serif","Helvetica","Times Roman","Times"};
int default_font = 0;
CHARMAPHEADER CharmapHeader;
FONTHEADER FontHdr;
FILE *f;
char FontName[33];
char CharMap[33];
char *ptr;
char *p;
long FontStart;
int i,j,k,l,len;
unsigned char *family;
BOOL charmap;
OLDFONT oldfont;
NEWFONT newfont;
MVBFONT mvbfont;
MVBSTYLE *mvbstyle;
NEWSTYLE *newstyle;
FONTDESCRIPTOR *fd;
if(SearchFile(HelpFile,"|FONT",NULL))
{
FontStart=ftell(HelpFile);
my_fread(&FontHdr,sizeof(FontHdr),HelpFile);
fontnames=FontHdr.NumFacenames;
len=(FontHdr.DescriptorsOffset-FontHdr.FacenamesOffset)/fontnames;
fontname=my_malloc(fontnames*sizeof(char *));
family=my_malloc(fontnames*sizeof(unsigned char));
memset(family,0,fontnames*sizeof(unsigned char));
charmap=FALSE;
mvbstyle=NULL;
newstyle=NULL;
for(i=0;i<fontnames;i++)
{
fseek(HelpFile,FontStart+FontHdr.FacenamesOffset+len*i,SEEK_SET);
my_fread(FontName,len,HelpFile);
FontName[len]='\0';
if (FontName[0] == '\000') {
strcpy(FontName, BestFonts[default_font]);
}
ptr=strchr(FontName,',');
if(ptr&&FontHdr.FacenamesOffset>=16)
{
*ptr++='\0';
fseek(HelpFile,FontStart+FontHdr.CharmapsOffset,SEEK_SET);
for(j=0;hpj&&j<FontHdr.NumCharmaps;j++)
{
my_fread(CharMap,32,HelpFile);
CharMap[32]='\0';
p=strchr(CharMap,',');
if(p&&strcmp(p+1,ptr)==0&&strcmp(CharMap,"|MVCHARTAB,0")!=0)
{
if(!charmap)
{
fputs("[CHARMAP]\n",hpj);
charmap=TRUE;
}
*p++='\0';
if(strcmp(p,"0")==0)
{
fprintf(hpj,"DEFAULT=%s\n",CharMap);
}
else
{
fprintf(hpj,"%s=%s\n",FontName,CharMap);
}
break;
}
}
}
fontname[i]=my_strdup(FontName);
}
if(charmap) putc('\n',hpj);
if(hpj&&FontHdr.FacenamesOffset>=16) for(j=0;j<FontHdr.NumCharmaps;j++)
{
fseek(HelpFile,FontStart+FontHdr.CharmapsOffset+j*32,SEEK_SET);
my_fread(CharMap,32,HelpFile);
CharMap[32]='\0';
p=strchr(CharMap,',');
if(p&&strcmp(CharMap,"|MVCHARTAB,0")!=0)
{
*p++='\0';
if(SearchFile(HelpFile,CharMap,NULL))
{
my_fread(&CharmapHeader,sizeof(CHARMAPHEADER),HelpFile);
f=my_fopen(CharMap,"wt");
if(f)
{
fprintf(f,"%d,\n",CharmapHeader.Entries);
for(k=0;k<CharmapHeader.Entries;k++)
{
fprintf(f,"%5u,",my_getw(HelpFile));
fprintf(f,"%5u,",my_getw(HelpFile));
fprintf(f,"%3u,",getc(HelpFile));
fprintf(f,"%3u,",getc(HelpFile));
fprintf(f,"%3u,",getc(HelpFile));
fprintf(f,"%3u,\n",getc(HelpFile));
my_getw(HelpFile);
}
fprintf(f,"%d,\n",CharmapHeader.Ligatures);
for(k=0;k<CharmapHeader.Ligatures;k++)
{
for(l=0;l<CharmapHeader.LigLen;l++)
{
fprintf(f,"%3u,",getc(HelpFile));
}
putc('\n',f);
}
my_fclose(f);
}
}
}
}
fseek(HelpFile,FontStart+FontHdr.DescriptorsOffset,SEEK_SET);
colors=1; /* auto */
color[0].r=1;
color[0].g=1;
color[0].b=0;
fonts=FontHdr.NumDescriptors;
if(font) free(font);
font=my_malloc(fonts*sizeof(FONTDESCRIPTOR));
memset(font,0,fonts*sizeof(FONTDESCRIPTOR));
if(FontHdr.FacenamesOffset>=16)
{
scaling=1L;
rounderr=0;
for(i=0;i<FontHdr.NumDescriptors;i++)
{
my_fread(&mvbfont,sizeof(mvbfont),HelpFile);
fd=font+i;
fd->FontName=mvbfont.FontName;
fd->HalfPoints=-2L*mvbfont.Height;
fd->Bold=mvbfont.Weight>500;
fd->Italic=mvbfont.Italic!=0;
fd->Underline=mvbfont.Underline!=0;
fd->StrikeOut=mvbfont.StrikeOut!=0;
fd->DoubleUnderline=mvbfont.DoubleUnderline!=0;
fd->SmallCaps=mvbfont.SmallCaps!=0;
fd->textcolor=AddColor(mvbfont.FGRGB[0],mvbfont.FGRGB[1],mvbfont.FGRGB[2]);
fd->backcolor=AddColor(mvbfont.BGRGB[0],mvbfont.BGRGB[1],mvbfont.BGRGB[2]);
fd->FontFamily=mvbfont.PitchAndFamily>>4;
fd->style=mvbfont.style;
fd->up=mvbfont.up;
fd->expndtw=mvbfont.expndtw;
}
fseek(HelpFile,FontStart+FontHdr.FormatsOffset,SEEK_SET);
mvbstyle=my_malloc(FontHdr.NumFormats*sizeof(MVBSTYLE));
my_fread(mvbstyle,FontHdr.NumFormats*sizeof(MVBSTYLE),HelpFile);
for(i=0;i<FontHdr.NumFormats;i++)
{
MVBSTYLE *m;
m=mvbstyle+i;
m->font.FGRGB[0]=AddColor(m->font.FGRGB[0],m->font.FGRGB[1],m->font.FGRGB[2]);
m->font.BGRGB[0]=AddColor(m->font.BGRGB[0],m->font.BGRGB[1],m->font.BGRGB[2]);
}
}
else if(FontHdr.FacenamesOffset>=12)
{
scaling=1L;
rounderr=0;
for(i=0;i<FontHdr.NumDescriptors;i++)
{
my_fread(&newfont,sizeof(newfont),HelpFile);
fd=font+i;
fd->Bold=newfont.Weight>500;
fd->Italic=newfont.Italic!=0;
fd->Underline=newfont.Underline!=0;
fd->StrikeOut=newfont.StrikeOut!=0;
fd->DoubleUnderline=newfont.DoubleUnderline!=0;
fd->SmallCaps=newfont.SmallCaps!=0;
fd->FontName=newfont.FontName;
fd->HalfPoints=-2L*newfont.Height;
fd->textcolor=AddColor(newfont.FGRGB[0],newfont.FGRGB[1],newfont.FGRGB[2]);
fd->backcolor=AddColor(newfont.BGRGB[0],newfont.BGRGB[1],newfont.BGRGB[2]);
fd->FontFamily=newfont.PitchAndFamily>>4;
}
fseek(HelpFile,FontStart+FontHdr.FormatsOffset,SEEK_SET);
newstyle=my_malloc(FontHdr.NumFormats*sizeof(NEWSTYLE));
my_fread(newstyle,FontHdr.NumFormats*sizeof(NEWSTYLE),HelpFile);
for(i=0;i<FontHdr.NumFormats;i++)
{
NEWSTYLE *m;
m=newstyle+i;
m->font.FGRGB[0]=AddColor(m->font.FGRGB[0],m->font.FGRGB[1],m->font.FGRGB[2]);
m->font.BGRGB[0]=AddColor(m->font.BGRGB[0],m->font.BGRGB[1],m->font.BGRGB[2]);
}
}
else
{
scaling=10L;
rounderr=5;
for(i=0;i<FontHdr.NumDescriptors;i++)
{
my_fread(&oldfont,sizeof(oldfont),HelpFile);
fd=font+i;
fd->Bold=(oldfont.Attributes&FONT_BOLD)!=0;
fd->Italic=(oldfont.Attributes&FONT_ITAL)!=0;
fd->Underline=(oldfont.Attributes&FONT_UNDR)!=0;
fd->StrikeOut=(oldfont.Attributes&FONT_STRK)!=0;
fd->DoubleUnderline=(oldfont.Attributes&FONT_DBUN)!=0;
fd->SmallCaps=(oldfont.Attributes&FONT_SMCP)!=0;
fd->FontName=oldfont.FontName;
fd->HalfPoints=oldfont.HalfPoints;
fd->textcolor=AddColor(oldfont.FGRGB[0],oldfont.FGRGB[1],oldfont.FGRGB[2]);
fd->backcolor=AddColor(oldfont.BGRGB[0],oldfont.BGRGB[1],oldfont.BGRGB[2]);
if(oldfont.FontFamily<6)
{
fd->FontFamily=lookup[oldfont.FontFamily];
}
else
{
fd->FontFamily=oldfont.FontFamily;
}
}
}
for(i=0;i<FontHdr.NumDescriptors;i++)
{
if(font[i].FontName<fontnames)
{
family[font[i].FontName]=font[i].FontFamily;
}
}
DefFont=0;
l=sizeof(BestFonts)/sizeof(BestFonts[0]);
if(fontname)
{
for(i=0;i<fontnames;i++) if(family[i])
{
for(j=0;j<l;j++)
{
if(stricmp(fontname[i],BestFonts[j])==0)
{
DefFont=i;
l=j;
break;
}
}
}
}
fprintf(rtf,"{\\rtf1\\ansi\\deff%d\n{\\fonttbl",DefFont);
for(i=0;i<fontnames;i++)
{
fprintf(rtf,"{\\f%d\\f%s %s;}",i,FontFamily(family[i]),fontname[i]);
free(fontname[i]);
}
free(fontname);
fputs("}\n",rtf);
if(colors>1)
{
fputs("{\\colortbl;",rtf);
for(i=1;i<colors;i++) fprintf(rtf,"\\red%d\\green%d\\blue%d;",color[i].r,color[i].g,color[i].b);
fputs("}\n",rtf);
}
fprintf(rtf,"{\\stylesheet{\\fs%d \\snext0 Normal;}\n",font[0].HalfPoints);
if(mvbstyle)
{
for(i=0;i<FontHdr.NumFormats;i++)
{
MVBSTYLE *m,*n;
m=mvbstyle+i;
fprintf(rtf,"{\\*\\cs%u \\additive",m->StyleNum+9);
if(m->BasedOn)
{
n=mvbstyle+(m->BasedOn-1);
if(m->font.FontName!=n->font.FontName) fprintf(rtf,"\\f%d",m->font.FontName);
if(m->font.expndtw!=n->font.expndtw) fprintf(rtf,"\\expndtw%d",m->font.expndtw);
if(m->font.FGRGB[0]!=n->font.FGRGB[0]) fprintf(rtf,"\\cf%d",m->font.FGRGB[0]);
if(m->font.BGRGB[0]!=n->font.BGRGB[0]) fprintf(rtf,"\\cb%d",m->font.BGRGB[0]);
if(m->font.Height!=n->font.Height) fprintf(rtf,"\\fs%ld",-2L*m->font.Height);
if((m->font.Weight>500)!=(n->font.Weight>500)) fprintf(rtf,"\\b%d",m->font.Weight>500);
if(m->font.Italic!=n->font.Italic) fprintf(rtf,"\\i%d",m->font.Italic);
if(m->font.Underline!=n->font.Underline) fprintf(rtf,"\\ul%d",m->font.Underline);
if(m->font.StrikeOut!=n->font.StrikeOut) fprintf(rtf,"\\strike%d",m->font.StrikeOut);
if(m->font.DoubleUnderline!=n->font.DoubleUnderline) fprintf(rtf,"\\uldb%d",m->font.DoubleUnderline);
if(m->font.SmallCaps!=n->font.SmallCaps) fprintf(rtf,"\\scaps%d",m->font.SmallCaps);
if(m->font.up!=n->font.up) fprintf(rtf,"\\up%d",abs(m->font.up));
fprintf(rtf," \\sbasedon%u",m->BasedOn+9);
}
else
{
fprintf(rtf,"\\f%d",m->font.FontName);
if(m->font.Italic) fputs("\\i",rtf);
if(m->font.Weight>500) fputs("\\b",rtf);
if(m->font.Underline) fputs("\\ul",rtf);
if(m->font.StrikeOut) fputs("\\strike",rtf);
if(m->font.DoubleUnderline) fputs("\\uldb",rtf);
if(m->font.SmallCaps) fputs("\\scaps",rtf);
if(m->font.expndtw) fprintf(rtf,"\\expndtw%d",m->font.expndtw);
if(m->font.up>0) fprintf(rtf,"\\up%d",m->font.up);
else if(m->font.up<0) fprintf(rtf,"\\dn%d",-m->font.up);
fprintf(rtf,"\\fs%ld",-2*m->font.Height);
if(m->font.FGRGB[0]) fprintf(rtf,"\\cf%d",m->font.FGRGB[0]);
if(m->font.BGRGB[0]) fprintf(rtf,"\\cb%d",m->font.BGRGB[0]);
}
fprintf(rtf," %s;}\n",m->StyleName);
}
free(mvbstyle);
}
else if(newstyle)
{
for(i=0;i<FontHdr.NumFormats;i++)
{
NEWSTYLE *m,*n;
m=newstyle+i;
fprintf(rtf,"{\\*\\cs%u \\additive",m->StyleNum+9);
if(m->BasedOn)
{
n=newstyle+(m->BasedOn-1);
if(m->font.FontName!=n->font.FontName) fprintf(rtf,"\\f%d",m->font.FontName);
if(m->font.FGRGB[0]!=n->font.FGRGB[0]) fprintf(rtf,"\\cf%d",m->font.FGRGB[0]);
if(m->font.BGRGB[0]!=n->font.BGRGB[0]) fprintf(rtf,"\\cb%d",m->font.BGRGB[0]);
if(m->font.Height!=n->font.Height) fprintf(rtf,"\\fs%ld",-2L*m->font.Height);
if((m->font.Weight>500)!=(n->font.Weight>500)) fprintf(rtf,"\\b%d",m->font.Weight>500);
if(m->font.Italic!=n->font.Italic) fprintf(rtf,"\\i%d",m->font.Italic);
if(m->font.Underline!=n->font.Underline) fprintf(rtf,"\\ul%d",m->font.Underline);
if(m->font.StrikeOut!=n->font.StrikeOut) fprintf(rtf,"\\strike%d",m->font.StrikeOut);
if(m->font.DoubleUnderline!=n->font.DoubleUnderline) fprintf(rtf,"\\uldb%d",m->font.DoubleUnderline);
if(m->font.SmallCaps!=n->font.SmallCaps) fprintf(rtf,"\\scaps%d",m->font.SmallCaps);
fprintf(rtf," \\sbasedon%u",m->BasedOn+9);
}
else
{
fprintf(rtf,"\\f%d",m->font.FontName);
if(m->font.Italic) fputs("\\i",rtf);
if(m->font.Weight>500) fputs("\\b",rtf);
if(m->font.Underline) fputs("\\ul",rtf);
if(m->font.StrikeOut) fputs("\\strike",rtf);
if(m->font.DoubleUnderline) fputs("\\uldb",rtf);
if(m->font.SmallCaps) fputs("\\scaps",rtf);
fprintf(rtf,"\\fs%ld",-2*m->font.Height);
if(m->font.FGRGB[0]) fprintf(rtf,"\\cf%d",m->font.FGRGB[0]);
if(m->font.BGRGB[0]) fprintf(rtf,"\\cb%d",m->font.BGRGB[0]);
}
fprintf(rtf," %s;}\n",m->StyleName);
}
free(newstyle);
}
if(family) free(family);
fputs("}\\pard\\plain\n",rtf);
memset(&CurrentFont,0,sizeof(CurrentFont));
CurrentFont.FontName=DefFont;
if(hpj)
{
fprintf(stderr,"%u font names, %u font descriptors",fontnames,FontHdr.NumDescriptors);
if(FontHdr.FacenamesOffset>=12) printf(", %u font styles",FontHdr.NumFormats);
fputs(" loaded\n",stderr);
}
}
}
/* read NumBytes from |TOPIC starting at TopicPos (or if TopicPos is 0
// where last left off) into dest, returning number of bytes read.
// TopicRead handles LZ77 decompression and the crossing of topic blocks */
long TopicRead(FILE *HelpFile,long TopicPos,void *dest,long NumBytes)
{
static TOPICBLOCKHEADER TopicBlockHeader;
static unsigned char TopicBuffer[0x4000];
static long TopicFileStart;
static long TopicBlockNum;
unsigned int TopicBlockOffset;
static unsigned int DecompSize;
static long LastTopicPos;
unsigned int n;
if(!TopicFileStart) /* first call: HelpFile is at start of |TOPIC */
{
TopicFileStart=ftell(HelpFile);
TopicBlockNum=-1L;
}
if(!TopicPos) TopicPos=LastTopicPos; /* continue where left off */
if((TopicPos-sizeof(TOPICBLOCKHEADER))/DecompressSize!=TopicBlockNum) /* other topic block */
{
TopicBlockNum=(TopicPos-sizeof(TOPICBLOCKHEADER))/DecompressSize;
if(TopicBlockNum*TopicBlockSize>=TopicFileLength) return 0;
fseek(HelpFile,TopicFileStart+TopicBlockNum*TopicBlockSize,SEEK_SET);
n=TopicBlockSize;
if(n+TopicBlockNum*TopicBlockSize>TopicFileLength)
{
n=(unsigned int)(TopicFileLength-TopicBlockNum*TopicBlockSize);
}
my_fread(&TopicBlockHeader,sizeof(TOPICBLOCKHEADER),HelpFile);
n-=sizeof(TOPICBLOCKHEADER);
if(lzcompressed)
{
DecompSize=DecompressIntoBuffer(2,HelpFile,n,TopicBuffer,sizeof(TopicBuffer));
}
else
{
DecompSize=my_fread(TopicBuffer,n,HelpFile);
}
}
TopicBlockOffset=(TopicPos-sizeof(TOPICBLOCKHEADER))%DecompressSize;
if(TopicBlockOffset+NumBytes>DecompSize) /* more than available in this block */
{
n=DecompSize-TopicBlockOffset;
if(n) memcpy(dest,TopicBuffer+TopicBlockOffset,n);
return n+TopicRead(HelpFile,(TopicBlockNum+1)*DecompressSize+sizeof(TOPICBLOCKHEADER),(char *)dest+n,NumBytes-n);
}
if(NumBytes) memcpy(dest,TopicBuffer+TopicBlockOffset,NumBytes);
LastTopicPos=TopicPos+NumBytes;
return NumBytes;
}
/* Hall or oldstyle Phrase replacement of str into out */
char *PhraseReplace(unsigned char *str,long len,char *out)
{
int CurChar;
if(Hall)
{
while(len)
{
CurChar=*str++;
len--;
if((CurChar&1)==0) /* phrases 0..127 */
{
out=PrintPhrase(CurChar/2,out,NULL);
}
else if((CurChar&3)==1) /* phrases 128..16511 */
{
CurChar=128+(CurChar/4)*256+*str++;
len--;
out=PrintPhrase(CurChar,out,NULL);
}
else if((CurChar&7)==3) /* copy next n characters */
{
while(CurChar>0)
{
*out++=*str++;
len--;
CurChar-=8;
}
}
else if((CurChar&0x0F)==0x07)
{
while(CurChar>0)
{
*out++=' ';
CurChar-=16;
}
}
else /* if((CurChar&0x0F)==0x0F) */
{
while(CurChar>0)
{
*out++='\0';
CurChar-=16;
}
}
}
}
else
{
while(len)
{
CurChar=*str++;
len--;
if(CurChar>0&&CurChar<16) /* phrase 0..1919 */
{
CurChar=256*(CurChar-1)+*str++;
len--;
out=PrintPhrase(CurChar/2,out,NULL);
if(CurChar&1) *out++=' ';
}
else
{
*out++=CurChar;
}
}
}
return out;
}
/* reads next chunk from |TOPIC like TopicRead, but does phrase decompression
// if Length > NumBytes, suitable to read LinkData2. If phrase decompression
// doesn't expands to Length bytes, buffer is padded using 0. TopicPhraseRead
// always NUL-terminates at dest[Length] just to be save */
long TopicPhraseRead(FILE *HelpFile,long TopicPos,char *dest,long NumBytes,long Length)
{
char *buffer;
long BytesRead;
long i;
if(Length<=NumBytes) /* no phrase compression in this case */
{
BytesRead=TopicRead(HelpFile,TopicPos,dest,Length);
for (i = BytesRead; i <= Length; i++) dest[i] = '\0';
if(BytesRead==Length&&Length<NumBytes) /* some trailing bytes are not used (bug in HCRTF ?) */
{
buffer=my_malloc(NumBytes-Length);
BytesRead+=TopicRead(HelpFile,0L,buffer,NumBytes-Length);
free(buffer);
}
}
else
{
buffer=my_malloc(NumBytes);
BytesRead=TopicRead(HelpFile,TopicPos,buffer,NumBytes);
NumBytes=PhraseReplace(buffer,NumBytes,dest)-dest;
free(buffer);
if(NumBytes>Length)
{
error("Phrase replacement delivers %ld bytes instead of %ld",NumBytes,Length);
exit(1);
}
}
while(NumBytes<=Length) dest[NumBytes++]='\0';
return BytesRead;
}
void Annotate(long pos,FILE *rtf)
{
long FileLength;
char FileName[19];
int i;
long l;
sprintf(FileName,"%ld!0",pos);
if(SearchFile(AnnoFile,FileName,&FileLength))
{
fputs("{\\v {\\*\\atnid ANN}\\chatn {\\*\\annotation \\pard\\plain {\\chatn }",rtf);
for(l=0;l<FileLength&&(i=getc(AnnoFile))!=-1;l++)
{
if(i==0x0D)
{
fputs("\\par\n",rtf);
}
else if(i!='{'&&i!='}'&&i!='\\'&&isprint(i))
{
putc(i,rtf);
}
else if(i=='{')
{
fputs("\\{\\-",rtf);
}
else if(i!='\0'&&i!=0x0A)
{
fprintf(rtf,"\\'%02x",i);
}
}
fputs("}}",rtf);
}
}
/* collect all keywords assigned to positions starting at NextKeywordOffset
// from all keyword lists, saving the first MAXKEYWORDS in KeywordRec
// (which is allocated to MAXKEYWORDS if NULL) for use in ListKeywords.
// updates NextKeywordOffset, clears NextKeywordRec, sets KeywordRecs. */
void CollectKeywords(FILE *HelpFile)
{
unsigned short j,m;
int i,n,k,l,map;
long FileLength,savepos,KWDataOffset,from;
long *keytopic;
BUFFER buf;
char kwdata[10];
char kwbtree[10];
fputs("Collecting keywords...",stderr);
savepos=ftell(HelpFile);
if(KeywordRec) /* free old keywords */
{
for(i=0;i<KeywordRecs;i++)
{
if(KeywordRec[i].Keyword) free(KeywordRec[i].Keyword);
}
}
else
{
KeywordRec=my_malloc(MAXKEYWORDS*sizeof(KEYWORDREC));
}
NextKeywordRec=KeywordRecs=0;
from=NextKeywordOffset;
NextKeywordOffset=0x7FFFFFFFL;
for(k=0;k<2;k++) for(map='0';map<='z';map++)
{
if(k)
{
if(!keyindex[map-'0']) continue;
sprintf(kwdata,"|%cKWDATA",map);
sprintf(kwbtree,"|%cKWBTREE",map);
}
else
{
if(!lists[map-'0']) continue;
sprintf(kwdata,"|%cWDATA",map);
sprintf(kwbtree,"|%cWBTREE",map);
}
if(SearchFile(HelpFile,kwdata,&FileLength))
{
keytopic=my_malloc(FileLength);
my_fread(keytopic,FileLength,HelpFile);
if(SearchFile(HelpFile,kwbtree,NULL))
{
for(n=GetFirstPage(HelpFile,&buf,NULL);n;n=GetNextPage(HelpFile,&buf))
{
for(i=0;i<n;i++)
{
my_gets(keyword,sizeof(keyword),HelpFile);
m=my_getw(HelpFile);
KWDataOffset=getdw(HelpFile);
for(j=0;j<m;j++)
{
if(keytopic[KWDataOffset/4+j]>=from)
{
if(KeywordRecs>=MAXKEYWORDS)
{
NextKeywordOffset=KeywordRec[KeywordRecs-1].TopicOffset;
while(KeywordRec[KeywordRecs-1].TopicOffset==NextKeywordOffset)
{
KeywordRecs--;
if(KeywordRec[KeywordRecs].Keyword) free(KeywordRec[KeywordRecs].Keyword);
}
}
l=KeywordRecs;
while(l>0&&KeywordRec[l-1].TopicOffset>keytopic[KWDataOffset/4+j])
{
KeywordRec[l].KeyIndex=KeywordRec[l-1].KeyIndex;
KeywordRec[l].Footnote=KeywordRec[l-1].Footnote;
KeywordRec[l].Keyword=KeywordRec[l-1].Keyword;
KeywordRec[l].TopicOffset=KeywordRec[l-1].TopicOffset;
l--;
}
KeywordRec[l].KeyIndex=k>0;
KeywordRec[l].Footnote=map;
KeywordRec[l].Keyword=my_strdup(keyword);
KeywordRec[l].TopicOffset=keytopic[KWDataOffset/4+j];
KeywordRecs++;
}
}
}
}
free(keytopic);
}
}
}
fseek(HelpFile,savepos,SEEK_SET);
for(i=0;i<22;i++) fputs("\b \b",stderr);
}
/* writes out all keywords appearing up to position TopicOffset and eats
// them up so they are not written out again. Merges keywords if possible */
void ListKeywords(FILE *HelpFile,FILE *rtf,long TopicOffset)
{
int len,footnote,keyindex;
if(NextKeywordRec>=KeywordRecs)
{
if(NextKeywordOffset==0x7FFFFFFFL) return;
CollectKeywords(HelpFile);
}
footnote=keyindex=len=0;
while(NextKeywordRec<KeywordRecs&&KeywordRec[NextKeywordRec].TopicOffset<=TopicOffset)
{
if(len>0&&(KeywordRec[NextKeywordRec].Footnote!=footnote||KeywordRec[NextKeywordRec].KeyIndex!=keyindex||len+strlen(KeywordRec[NextKeywordRec].Keyword)>(after31?1023:254)))
{
fputs("}\n",rtf);
len=0;
}
if(len>0)
{
putc(';',rtf);
}
else if(KeywordRec[NextKeywordRec].KeyIndex)
{
fprintf(rtf,"{\\up K}{\\footnote\\pard\\plain{\\up K} %c:",KeywordRec[NextKeywordRec].Footnote);
}
else
{
fprintf(rtf,"{\\up %c}{\\footnote\\pard\\plain{\\up %c} ",KeywordRec[NextKeywordRec].Footnote,KeywordRec[NextKeywordRec].Footnote);
}
len+=strlen(KeywordRec[NextKeywordRec].Keyword)+1;
putrtf(rtf,KeywordRec[NextKeywordRec].Keyword);
footnote=KeywordRec[NextKeywordRec].Footnote;
keyindex=KeywordRec[NextKeywordRec].KeyIndex;
NextKeywordRec++;
}
if(len) fputs("}\n",rtf);
}
/* create > footnote if topic at TopicOffset has a window assigned to
// using the |VIOLA internal file. Read VIOLA sequentially, reloading
// next page only if neccessary, because it is properly ordered. */
int ListWindows(FILE *HelpFile,long TopicOffset)
{
long savepos;
static int n,i;
static BUFFER buf;
static int VIOLAfound=-1;
static VIOLAREC *Viola;
int result;
if(VIOLAfound==0) return -1;
savepos=ftell(HelpFile);
if(VIOLAfound==-1)
{
VIOLAfound=0;
if(SearchFile(HelpFile,"|VIOLA",NULL))
{
n=GetFirstPage(HelpFile,&buf,NULL);
if(n)
{
Viola=my_malloc(n*sizeof(VIOLAREC));
my_fread(Viola,n*sizeof(VIOLAREC),HelpFile);
i=0;
VIOLAfound=1;
}
}
}
result=-1;
if(VIOLAfound==1)
{
while(i>=n||TopicOffset>Viola[i].TopicOffset)
{
if(i>=n)
{
free(Viola);
n=GetNextPage(HelpFile,&buf);
if(n==0)
{
VIOLAfound=0;
break;
}
Viola=my_malloc(n*sizeof(VIOLAREC));
my_fread(Viola,n*sizeof(VIOLAREC),HelpFile);
i=0;
}
else
{
i++;
}
}
if(i<n&&Viola[i].TopicOffset==TopicOffset)
{
result=Viola[i].WindowNumber;
}
}
fseek(HelpFile,savepos,SEEK_SET);
return result;
}
/* Browse sequence handling support functions. As an efficient means to
// resolve browse sequences, FirstPass numbers and saves all browse
// sequence start positions (that are topics where BrowseNext and
// BrowsePrev both point to some topic behind) and links them with the
// following topics. Whenever a topic is merged to the beginning of a
// browse sequence, it's start topics browse subnumber is incremented.
// The interesting part is a topic where BrowsePrev and BrowseNext both
// point to earlier topics. Two different browse sequences need to be
// merged at this point, that is will get the same browse start number
// and one start topics subnumber needs to accommodate the other browse
// sequence. Using the start records, TopicDump does know which browse
// sequence starts at which topic and knows the browse sequence number
// and subnumber assigned. */
void AddStart(long StartTopic,int BrowseNum,int Count)
{
start=my_realloc(start,(starts+1)*sizeof(START));
start[starts].StartTopic=StartTopic;
start[starts].BrowseNum=BrowseNum;
start[starts].Start=Count;
starts++;
}
void FixStart(int BrowseNum,int NewBrowseNum,int AddCount)
{
int i;
for(i=0;i<starts;i++) if(start[i].BrowseNum==BrowseNum)
{
start[i].BrowseNum=NewBrowseNum;
start[i].Start+=AddCount;
}
}
void AddBrowse(long StartTopic,long NextTopic,long PrevTopic)
{
int i;
for(i=0;i<browses;i++) if(browse[i].StartTopic==-1L) break; /* empty space in array ? */
if(i==browses) /* no empty space, add to array */
{
browse=my_realloc(browse,++browses*sizeof(BROWSE));
}
browse[i].StartTopic=StartTopic;
browse[i].NextTopic=NextTopic;
browse[i].PrevTopic=PrevTopic;
browse[i].BrowseNum=browsenums++;
browse[i].Start=1;
browse[i].Count=1;
}
void MergeBrowse(long TopicOffset,long OtherTopicOffset,long NextTopic,long PrevTopic)
{
int i,j;
for(i=0;i<browses;i++) if(browse[i].StartTopic!=-1L)
{
if(browse[i].NextTopic==TopicOffset||browse[i].NextTopic==OtherTopicOffset) break;
}
for(j=0;j<browses;j++) if(browse[j].StartTopic!=-1L)
{
if(browse[j].PrevTopic==TopicOffset||browse[j].PrevTopic==OtherTopicOffset) break;
}
if(i<browses&&j<browses)
{
browse[i].Count++;
browse[i].NextTopic=browse[j].NextTopic;
FixStart(browse[j].BrowseNum,browse[i].BrowseNum,browse[i].Count);
browse[j].Start+=browse[i].Count;
AddStart(browse[j].StartTopic,browse[i].BrowseNum,browse[j].Start);
browse[i].Count+=browse[j].Count;
browse[j].StartTopic=-1L;
if(browse[i].NextTopic==-1L&&browse[i].PrevTopic==-1L)
{
AddStart(browse[i].StartTopic,browse[i].BrowseNum,browse[i].Start);
browse[i].StartTopic=-1L;
}
}
else
{
warnings=TRUE;
fprintf(stderr,"Can not merge %08lx %08lx %08lx\n",TopicOffset,NextTopic,PrevTopic);
}
}
void LinkBrowse(long TopicOffset,long OtherTopicOffset,long NextTopic,long PrevTopic)
{
int i;
for(i=0;i<browses;i++) if(browse[i].StartTopic!=-1L)
{
if(browse[i].NextTopic==TopicOffset||browse[i].NextTopic==OtherTopicOffset) break;
}
if(i<browses)
{
browse[i].NextTopic=NextTopic;
browse[i].Count++;
if(browse[i].NextTopic==-1L&&browse[i].PrevTopic==-1L)
{
AddStart(browse[i].StartTopic,browse[i].BrowseNum,browse[i].Start);
browse[i].StartTopic=-1L;
}
}
else
{
warnings=TRUE;
fprintf(stderr,"Can not link %08lx %08lx %08lx\n",TopicOffset,NextTopic,PrevTopic);
for(i=0;i<browses;i++) if(browse[i].StartTopic!=-1L)
{
fprintf(stderr,"Open browse %08lx %08lx\n",browse[i].PrevTopic,browse[i].NextTopic);
}
}
}
void BackLinkBrowse(long TopicOffset,long OtherTopicOffset,long NextTopic,long PrevTopic)
{
int i;
for(i=0;i<browses;i++) if(browse[i].StartTopic!=-1L)
{
if(browse[i].PrevTopic==TopicOffset||browse[i].PrevTopic==OtherTopicOffset) break;
}
if(i<browses)
{
browse[i].PrevTopic=PrevTopic;
browse[i].Count++;
browse[i].Start++;
FixStart(browse[i].BrowseNum,browse[i].BrowseNum,1);
if(browse[i].NextTopic==-1L&&browse[i].PrevTopic==-1L)
{
AddStart(browse[i].StartTopic,browse[i].BrowseNum,browse[i].Start);
browse[i].StartTopic=-1L;
}
}
else
{
warnings=TRUE;
fprintf(stderr,"Can not backlink %08lx %08lx %08lx\n",TopicOffset,NextTopic,PrevTopic);
}
}
unsigned long AddLink(long StartTopic,long NextTopic,long PrevTopic)
{
int i,j;
unsigned long result;
result=0L;
for(i=0;i<browses;i++) if(browse[i].StartTopic==-1L) break;
if(i==browses) browse=my_realloc(browse,++browses*sizeof(BROWSE));
for(j=0;j<starts;j++) if(start[j].StartTopic==StartTopic) break;
if(j<starts)
{
browse[i].StartTopic=start[j].StartTopic;
browse[i].BrowseNum=start[j].BrowseNum;
browse[i].Start=start[j].Start;
browse[i].Count=start[j].Start;
browse[i].NextTopic=NextTopic;
browse[i].PrevTopic=PrevTopic;
result=browse[i].BrowseNum+((long)browse[i].Start<<16);
}
else
{
warnings=TRUE;
fprintf(stderr,"Browse start %08lx not found\n",StartTopic);
}
return result;
}
unsigned long MergeLink(long TopicOffset,long OtherTopicOffset,long NextTopic,long PrevTopic)
{
int i,j;
unsigned long result;
result=0L;
for(i=0;i<browses;i++) if(browse[i].StartTopic!=-1L)
{
if(browse[i].NextTopic==TopicOffset||browse[i].NextTopic==OtherTopicOffset) break;
}
for(j=0;j<browses;j++) if(browse[j].StartTopic!=-1L)
{
if(browse[j].PrevTopic==TopicOffset||browse[j].PrevTopic==OtherTopicOffset) break;
}
if(i<browses&&j<browses)
{
browse[i].Count++;
browse[j].Start--;
if(browse[i].Count!=browse[j].Start)
{
warnings=TRUE;
fprintf(stderr,"Prev browse end %d doen't match next browse start %d\n",browse[i].Count,browse[j].Start);
}
result=browse[i].BrowseNum+((long)browse[i].Count<<16);
browse[i].NextTopic=browse[j].NextTopic;
browse[i].Count=browse[j].Count;
browse[j].StartTopic=-1L;
}
else
{
warnings=TRUE;
fprintf(stderr,"Can not merge %08lx %08lx %08lx\n",TopicOffset,NextTopic,PrevTopic);
}
return result;
}
unsigned long LinkLink(long TopicOffset,long OtherTopicOffset,long NextTopic,long PrevTopic)
{
int i;
unsigned long result;
result=0L;
for(i=0;i<browses;i++) if(browse[i].StartTopic!=-1L)
{
if(browse[i].NextTopic==TopicOffset||browse[i].NextTopic==OtherTopicOffset) break;
}
if(i<browses)
{
browse[i].NextTopic=NextTopic;
browse[i].Count++;
result=browse[i].BrowseNum+((long)browse[i].Count<<16);
if(browse[i].NextTopic==-1L&&browse[i].PrevTopic==-1L)
{
browse[i].StartTopic=-1L;
}
}
else
{
warnings=TRUE;
fprintf(stderr,"Can not link %08lx %08lx %08lx\n",TopicOffset,NextTopic,PrevTopic);
}
return result;
}
unsigned long BackLinkLink(long TopicOffset,long OtherTopicOffset,long NextTopic,long PrevTopic)
{
int i;
unsigned long result;
result=0L;
for(i=0;i<browses;i++) if(browse[i].StartTopic!=-1L)
{
if(browse[i].PrevTopic==TopicOffset||browse[i].PrevTopic==OtherTopicOffset) break;
}
if(i<browses)
{
browse[i].PrevTopic=PrevTopic;
browse[i].Start--;
result=browse[i].BrowseNum+((long)browse[i].Start<<16);
if(browse[i].NextTopic==-1L&&browse[i].PrevTopic==-1L)
{
browse[i].StartTopic=-1L;
}
}
else
{
warnings=TRUE;
fprintf(stderr,"Can not backlink %08lx %08lx %08lx\n",TopicOffset,NextTopic,PrevTopic);
}
return result;
}
/* create numbered rtf file names, no numbering if i=0 */
void BuildName(char *buffer,int i)
{
char num[7];
strcpy(buffer,name);
if(i)
{
snprintf(num, 7, "%d", i);
if(strlen(buffer)+strlen(num)>8)
{
buffer[8-strlen(num)]='\0';
}
strcat(buffer,num);
}
strcat(buffer,".rtf");
}
/* emit rtf commands to change to font i.
// ul forces underline on, uldb forces doubleunderline on */
void ChangeFont(FILE *rtf,unsigned int i,BOOL ul,BOOL uldb)
{
FONTDESCRIPTOR *f;
long pos;
if(i<fonts)
{
pos=ftell(rtf);
f=font+i;
if(f->style)
{
fprintf(rtf,"\\plain\\cs%d",f->style+9);
if(uldb) fputs("\\uldb",rtf); else if(ul) fputs("\\ul",rtf);
}
else
{
/* HC30 can't reset, so reset using \plain */
if((CurrentFont.Bold&&!f->Bold)
|| (CurrentFont.Italic&&!f->Italic)
|| (CurrentFont.Underline&&!(!uldb&&(ul||f->Underline)))
|| (CurrentFont.StrikeOut&&!f->StrikeOut)
|| (CurrentFont.DoubleUnderline&&!(uldb||f->DoubleUnderline))
|| (CurrentFont.SmallCaps&&!f->SmallCaps)
|| (CurrentFont.FontName&&!f->FontName)
|| (CurrentFont.textcolor&&!f->textcolor)
|| (CurrentFont.backcolor&&!f->backcolor)
|| (CurrentFont.up&&!f->up)
|| (CurrentFont.style&&!f->style))
{
fputs("\\plain",rtf);
memset(&CurrentFont,0,sizeof(CurrentFont));
CurrentFont.FontName=DefFont;
}
if(f->FontName!=CurrentFont.FontName) fprintf(rtf,"\\f%d",f->FontName);
if(f->Italic&&!CurrentFont.Italic) fputs("\\i",rtf);
if(f->Bold&&!CurrentFont.Bold) fputs("\\b",rtf);
if(!uldb&&(ul||f->Underline)&&!CurrentFont.Bold) fputs("\\ul",rtf);
if(f->StrikeOut&&!CurrentFont.StrikeOut) fputs("\\strike",rtf);
if((uldb||f->DoubleUnderline)&&!CurrentFont.DoubleUnderline) fputs("\\uldb",rtf);
if(f->SmallCaps&&!CurrentFont.SmallCaps) fputs("\\scaps",rtf);
if(f->expndtw!=CurrentFont.expndtw) fprintf(rtf,"\\expndtw%d",f->expndtw);
if(f->up!=CurrentFont.up)
{
if(f->up>0) fprintf(rtf,"\\up%d",f->up);
else if(f->up<0) fprintf(rtf,"\\dn%d",-f->up);
}
if(f->HalfPoints!=CurrentFont.HalfPoints) fprintf(rtf,"\\fs%d",f->HalfPoints);
if(f->textcolor!=CurrentFont.textcolor) fprintf(rtf,"\\cf%d",f->textcolor);
if(f->backcolor!=CurrentFont.backcolor) fprintf(rtf,"\\cb%d",f->backcolor);
}
memcpy(&CurrentFont,f,sizeof(CurrentFont));
if(ul) CurrentFont.Underline=1;
if(uldb)
{
CurrentFont.Underline=0;
CurrentFont.DoubleUnderline=1;
}
if(ftell(rtf)!=pos) putc(' ',rtf);
}
}
/* list all groups the topic TopicNum is assigned to and/or emit footnote
// for browse sequence of this topic as + footnote into rtf file */
void ListGroups(FILE *rtf,long TopicNum,unsigned long BrowseNum)
{
int i;
BOOL grouplisted;
grouplisted=FALSE;
for(i=0;i<groups;i++) if(group[i].GroupHeader.GroupType==1||group[i].GroupHeader.GroupType==2)
{
if((TopicNum>=group[i].GroupHeader.FirstTopic&&TopicNum<=group[i].GroupHeader.LastTopic)&&((group[i].GroupHeader.GroupType==1||group[i].GroupHeader.GroupType==2)&&(group[i].Bitmap[TopicNum>>3]&(1<<(TopicNum&7)))))
{
if(!grouplisted)
{
fputs("{\\up +}{\\footnote\\pard\\plain{\\up +} ",rtf);
if(BrowseNum) fprintf(rtf,"BROWSE%04x:%04x",(unsigned short)BrowseNum,(unsigned short)(BrowseNum>>16));
grouplisted=TRUE;
}
fprintf(rtf,";%s",group[i].Name);
}
}
if(grouplisted)
{
fputs("}\n",rtf);
}
else if(BrowseNum)
{
fprintf(rtf,"{\\up +}{\\footnote\\pard\\plain{\\up +} BROWSE%04x:%04x}\n",(unsigned short)BrowseNum,(unsigned short)(BrowseNum>>16));
}
}
/* advances TopicOffset to next block in |TOPIC if setting of TopicPos to
// NextBlock crosses TOPICBLOCKHEADER */
TOPICOFFSET NextTopicOffset(TOPICOFFSET TopicOffset,TOPICPOS NextBlock,TOPICPOS TopicPos)
{
/* it should never be neccessary to subtract sizeof(TOPICBLOCKHEADER), as no
// TOPICLINK may start in the last (12..21) bytes, but just to make shure... */
if((NextBlock-sizeof(TOPICBLOCKHEADER))/DecompressSize!=(TopicPos-sizeof(TOPICBLOCKHEADER))/DecompressSize)
{
return ((NextBlock-sizeof(TOPICBLOCKHEADER))/DecompressSize)*0x8000L;
}
return TopicOffset;
}
/* TopicDump: converts the internal |TOPIC file to RTF format suitable for
// recompilation inserting footnotes with information from other internal
// files as required */
FILE *TopicDump(FILE *HelpFile,FILE *rtf,FILE *hpj,BOOL makertf)
{
TOPICLINK TopicLink;
char *LinkData1; /* Data associated with this link */
long nonscroll=-1L;
char *LinkData2; /* Second set of data */
int fontset,i;
int NextContextRec;
unsigned long BrowseNum;
char *hotspot;
char *arg;
BOOL firsttopic=TRUE;
BOOL ul,uldb;
int nextbitmap,TopicInRTF,NumberOfRTF;
long TopicNum,TopicOffset,TopicPos;
int col,cols,lastcol;
short *iptr;
unsigned short x1,x2,x3;
short y1;
long l1;
char *ptr;
char *cmd;
char *str;
long ActualTopicOffset,MaxTopicOffset;
TOPICHEADER30 *TopicHdr30;
TOPICHEADER *TopicHdr;
long BogusTopicOffset;
if(SearchFile(HelpFile,"|TOPIC",&TopicFileLength))
{
fontset=-1;
nextbitmap=1;
if(browse) free(browse);
browse=NULL;
browses=0;
NextContextRec=0;
ul=uldb=FALSE;
hotspot=NULL;
TopicOffset=0L;
TopicPos=12L;
TopicNum=16;
TopicInRTF=0;
NumberOfRTF=1;
while(TopicRead(HelpFile,TopicPos,&TopicLink,sizeof(TopicLink))==sizeof(TOPICLINK))
{
if(before31)
{
if(TopicPos+TopicLink.NextBlock>=TopicFileLength) break;
}
else
{
if(TopicLink.NextBlock<=0) break;
}
if(TopicLink.DataLen1>sizeof(TOPICLINK))
{
LinkData1=my_malloc(TopicLink.DataLen1-sizeof(TOPICLINK)+1);
if(TopicRead(HelpFile,0L,LinkData1,TopicLink.DataLen1-sizeof(TOPICLINK))!=TopicLink.DataLen1-sizeof(TOPICLINK)) break;
}
else LinkData1=NULL;
if(TopicLink.DataLen1<TopicLink.BlockSize) /* read LinkData2 using phrase replacement */
{
LinkData2=my_malloc(TopicLink.DataLen2+1);
if(TopicPhraseRead(HelpFile,0L,LinkData2,TopicLink.BlockSize-TopicLink.DataLen1,TopicLink.DataLen2)!=TopicLink.BlockSize-TopicLink.DataLen1) break;
}
else LinkData2=NULL;
if(LinkData1&&TopicLink.RecordType==TL_TOPICHDR) /* display a Topic Header record */
{
if(TopicsPerRTF&&++TopicInRTF>=TopicsPerRTF)
{
putc('}',rtf);
my_fclose(rtf);
BuildName(buffer,++NumberOfRTF);
if(hpj) fprintf(hpj,"%s\n",buffer);
rtf=my_fopen(buffer,"wt");
FontLoad(HelpFile,rtf,NULL);
TopicInRTF=0;
}
else if(!firsttopic)
{
if(makertf&&nopagebreak)
{
fputs("\\par\n",rtf);
}
else
{
fputs("\\page\n",rtf);
}
}
firsttopic=FALSE;
fprintf(stderr,"\rTopic %ld...",TopicNum-15);
if(!makertf)
{
BrowseNum=0L;
if(before31)
{
TopicHdr30=(TOPICHEADER30 *)LinkData1;
fprintf(rtf,"{\\up #}{\\footnote\\pard\\plain{\\up #} TOPIC%ld}\n",TopicNum);
if(resolvebrowse)
{
if((TopicHdr30->NextTopicNum>TopicNum&&TopicHdr30->PrevTopicNum>TopicNum)
|| (TopicHdr30->NextTopicNum==-1&&TopicHdr30->PrevTopicNum>TopicNum)
|| (TopicHdr30->NextTopicNum>TopicNum&&TopicHdr30->PrevTopicNum==-1))
{
BrowseNum=AddLink(TopicNum,TopicHdr30->NextTopicNum,TopicHdr30->PrevTopicNum);
}
else if(TopicHdr30->NextTopicNum!=-1&&TopicHdr30->NextTopicNum<TopicNum&&TopicHdr30->PrevTopicNum!=-1&&TopicHdr30->PrevTopicNum<TopicNum)
{
BrowseNum=MergeLink(TopicNum,TopicNum,TopicHdr30->NextTopicNum,TopicHdr30->PrevTopicNum);
}
else if(TopicHdr30->NextTopicNum!=-1&&TopicHdr30->NextTopicNum<TopicNum&&(TopicHdr30->PrevTopicNum==-1||TopicHdr30->PrevTopicNum>TopicNum))
{
BrowseNum=BackLinkLink(TopicNum,TopicNum,TopicHdr30->NextTopicNum,TopicHdr30->PrevTopicNum);
}
else if(TopicHdr30->PrevTopicNum!=-1&&TopicHdr30->PrevTopicNum<TopicNum&&(TopicHdr30->NextTopicNum==-1||TopicHdr30->NextTopicNum>TopicNum))
{
BrowseNum=LinkLink(TopicNum,TopicNum,TopicHdr30->NextTopicNum,TopicHdr30->PrevTopicNum);
}
}
ListKeywords(HelpFile,rtf,TopicPos);
}
else
{
BogusTopicOffset=NextTopicOffset(TopicOffset,TopicLink.NextBlock,TopicPos);
TopicHdr=(TOPICHEADER *)LinkData1;
if(TopicHdr->Scroll!=-1L)
{
nonscroll=TopicHdr->Scroll;
}
else
{
nonscroll=TopicHdr->NextTopic;
}
if(resolvebrowse)
{
if((TopicHdr->BrowseFor>TopicOffset&&TopicHdr->BrowseBck>TopicOffset)
|| (TopicHdr->BrowseFor==-1L&&TopicHdr->BrowseBck>TopicOffset)
|| (TopicHdr->BrowseFor>TopicOffset&&TopicHdr->BrowseBck==-1L))
{
BrowseNum=AddLink(TopicOffset,TopicHdr->BrowseFor,TopicHdr->BrowseBck);
}
else if(TopicHdr->BrowseFor!=-1L&&TopicHdr->BrowseFor<TopicOffset&&TopicHdr->BrowseBck!=-1L&&TopicHdr->BrowseBck<TopicOffset)
{
BrowseNum=MergeLink(TopicOffset,BogusTopicOffset,TopicHdr->BrowseFor,TopicHdr->BrowseBck);
}
else if(TopicHdr->BrowseFor!=-1L&&TopicHdr->BrowseFor<TopicOffset&&(TopicHdr->BrowseBck==-1L||TopicHdr->BrowseBck>TopicOffset))
{
BrowseNum=BackLinkLink(TopicOffset,BogusTopicOffset,TopicHdr->BrowseFor,TopicHdr->BrowseBck);
}
else if(TopicHdr->BrowseBck!=-1L&&TopicHdr->BrowseBck<TopicOffset&&(TopicHdr->BrowseFor==-1L||TopicHdr->BrowseFor>TopicOffset))
{
BrowseNum=LinkLink(TopicOffset,BogusTopicOffset,TopicHdr->BrowseFor,TopicHdr->BrowseBck);
}
}
}
ListGroups(rtf,TopicNum-16,BrowseNum);
if(LinkData2&&TopicLink.DataLen2>0)
{
if(*LinkData2)
{
fputs("{\\up $}{\\footnote\\pard\\plain{\\up $} ",rtf);
putrtf(rtf,LinkData2);
fputs("}\n",rtf);
}
for(i=strlen(LinkData2)+1;i<TopicLink.DataLen2;i+=strlen(LinkData2+i)+1)
{
fputs("{\\up !}{\\footnote\\pard\\plain{\\up !} ",rtf);
if(!after31&&strlen(LinkData2+i)>254)
{
printf("Help compiler will issue Warning 3511: Macro '%s' exceeds limit of 254 characters\n",LinkData2+i);
}
putrtf(rtf,LinkData2+i);
fputs("}\n",rtf);
}
}
while(NextContextRec<ContextRecs&&ContextRec[NextContextRec].TopicOffset<=TopicOffset)
{
fputs("{\\up #}{\\footnote\\pard\\plain{\\up #} ",rtf);
putrtf(rtf,unhash(ContextRec[NextContextRec].HashValue));
fputs("}\n",rtf);
if(!mvp) while(NextContextRec+1<ContextRecs&&ContextRec[NextContextRec].TopicOffset==ContextRec[NextContextRec+1].TopicOffset)
{
NextContextRec++;
}
NextContextRec++;
}
i=ListWindows(HelpFile,TopicOffset);
if(i!=-1) fprintf(rtf,"{\\up >}{\\footnote\\pard\\plain{\\up >} %s}\n",GetWindowName(i));
}
TopicNum++;
}
else if(LinkData1&&LinkData2&&(TopicLink.RecordType==TL_DISPLAY30||TopicLink.RecordType==TL_DISPLAY||TopicLink.RecordType==TL_TABLE))
{
if(AnnoFile) Annotate(TopicPos,rtf);
ptr=LinkData1;
scanlong(&ptr);
if(TopicLink.RecordType==TL_DISPLAY||TopicLink.RecordType==TL_TABLE)
{
x1=scanword(&ptr);
ActualTopicOffset=TopicOffset;
MaxTopicOffset=ActualTopicOffset+x1;
TopicOffset+=x1;
}
if(TopicLink.RecordType==TL_TABLE)
{
fputs("\\trowd",rtf);
cols=(unsigned char)*ptr++;
x1=(unsigned char)*ptr++;
switch(x1)
{
case 0:
case 2:
l1=*(short *)ptr; /* min table width */
ptr+=2;
fputs("\\trqc",rtf);
break;
case 1:
case 3:
l1=32767L;
break;
}
iptr=(short *)ptr;
if(cols>1)
{
x1=iptr[0]+iptr[1]+iptr[3]/2;
fprintf(rtf,"\\trgaph%ld\\trleft%ld \\cellx%ld\\cellx%ld",((iptr[3]*scaling-rounderr)*l1)/32767,(((iptr[1]-iptr[3])*scaling-rounderr)*l1-32767)/32767,((x1*scaling-rounderr)*l1)/32767,(((x1+iptr[2]+iptr[3])*scaling-rounderr)*l1)/32767);
x1+=iptr[2]+iptr[3];
for(col=2;col<cols;col++)
{
x1+=iptr[2*col]+iptr[2*col+1];
fprintf(rtf,"\\cellx%ld",((x1*scaling-rounderr)*l1)/32767);
}
}
else
{
fprintf(rtf,"\\trleft%ld \\cellx%ld ",((iptr[1]*scaling-rounderr)*l1-32767)/32767,((iptr[0]*scaling-rounderr)*l1)/32767);
}
ptr=(char *)(iptr+2*cols);
}
lastcol=-1;
str=LinkData2;
for(col=0;(TopicLink.RecordType==TL_TABLE?*(short *)ptr!=-1:col==0)&&ptr<LinkData1+TopicLink.DataLen1-sizeof(TOPICLINK);col++)
{
fputs("\\pard",rtf);
if(TopicPos<nonscroll) fputs("\\keepn",rtf);
if(TopicLink.RecordType==TL_TABLE)
{
fputs("\\intbl",rtf);
lastcol=*(short *)ptr;
ptr+=5;
}
ptr+=4;
x2=*(unsigned short *)ptr;
ptr+=2;
if(x2&0x1000) fputs("\\keep",rtf);
if(x2&0x0400) fputs("\\qr",rtf);
if(x2&0x0800) fputs("\\qc",rtf);
if(x2&0x0001) scanlong(&ptr);
if(x2&0x0002) fprintf(rtf,"\\sb%ld",scanint(&ptr)*scaling-rounderr);
if(x2&0x0004) fprintf(rtf,"\\sa%ld",scanint(&ptr)*scaling-rounderr);
if(x2&0x0008) fprintf(rtf,"\\sl%ld",scanint(&ptr)*scaling-rounderr);
if(x2&0x0010) fprintf(rtf,"\\li%ld",scanint(&ptr)*scaling-rounderr);
if(x2&0x0020) fprintf(rtf,"\\ri%ld",scanint(&ptr)*scaling-rounderr);
if(x2&0x0040) fprintf(rtf,"\\fi%ld",scanint(&ptr)*scaling-rounderr);
if(x2&0x0100)
{
x1=(unsigned char)*ptr++;
if(x1&1) fputs("\\box",rtf);
if(x1&2) fputs("\\brdrt",rtf);
if(x1&4) fputs("\\brdrl",rtf);
if(x1&8) fputs("\\brdrb",rtf);
if(x1&0x10) fputs("\\brdrr",rtf);
if(x1&0x20) fputs("\\brdrth",rtf); else fputs("\\brdrs",rtf);
if(x1&0x40) fputs("\\brdrdb",rtf);
ptr+=2;
}
if(x2&0x0200)
{
y1=scanint(&ptr);
while(y1-->0)
{
x1=scanword(&ptr);
if(x1&0x4000)
{
switch(scanword(&ptr))
{
case 1:
fputs("\\tqr",rtf);
break;
case 2:
fputs("\\tqc",rtf);
break;
}
}
fprintf(rtf,"\\tx%ld",(x1&0x3FFF)*scaling-rounderr);
}
}
putc(' ',rtf);
while(1) /* ptr<LinkData1+TopicLink.DataLen1-sizeof(TOPICLINK)&&str<end) */
{
if(*str&&fontset>=0&&fontset<fonts&&font&&font[fontset].SmallCaps) strlwr(str);
do
{
if(!makertf)
{
while(NextContextRec<ContextRecs&&ContextRec[NextContextRec].TopicOffset<=ActualTopicOffset&&ContextRec[NextContextRec].TopicOffset<MaxTopicOffset)
{
fputs("{\\up #}{\\footnote\\pard\\plain{\\up #} ",rtf);
putrtf(rtf,unhash(ContextRec[NextContextRec].HashValue));
fputs("}\n",rtf);
if(!mvp) while(NextContextRec+1<ContextRecs&&ContextRec[NextContextRec].TopicOffset==ContextRec[NextContextRec+1].TopicOffset)
{
NextContextRec++;
}
NextContextRec++;
}
if(!before31) ListKeywords(HelpFile,rtf,ActualTopicOffset<MaxTopicOffset?ActualTopicOffset:MaxTopicOffset-1);
}
if(*str)
{
if(*str!='{'&&*str!='}'&&*str!='\\'&&isprint((unsigned char)*str))
{
putc(*str,rtf);
}
else if(!makertf&&*str=='{')
{
fputs("\\{\\-",rtf); /* emit invisible dash after { brace */
/* because bmc or another legal command may follow, but this */
/* command was not parsed the help file was build, so it was */
/* used just as an example. The dash will be eaten up by the */
/* help compiler on recompile. */
}
else
{
fprintf(rtf,"\\'%02x",(unsigned char)*str);
}
}
if(ActualTopicOffset<MaxTopicOffset) ActualTopicOffset++;
}
while(*str++);
if((unsigned char)ptr[0]==0xFF)
{
ptr++;
break;
}
else switch((unsigned char)ptr[0])
{
case 0x20: /* vfld MVB */
if(*(long *)(ptr+1))
{
fprintf(rtf,"\\{vfld%ld\\}",*(long *)(ptr+1));
}
else
{
fputs("\\{vfld\\}",rtf);
}
ptr+=5;
break;
case 0x21: /* dtype MVB */
if(*(short *)(ptr+1))
{
fprintf(rtf,"\\{dtype%d\\}",*(short *)(ptr+1));
}
else
{
fputs("\\{dtype\\}",rtf);
}
ptr+=3;
break;
case 0x80: /* font change */
ChangeFont(rtf,fontset=*(short *)(ptr+1),ul,uldb);
ptr+=3;
break;
case 0x81:
fputs("\\line\n",rtf);
ptr++;
break;
case 0x82:
if(TopicLink.RecordType==TL_TABLE)
{
if((unsigned char)ptr[1]!=0xFF)
{
fputs("\n\\par\\intbl ",rtf);
}
else if(*(short *)(ptr+2)==-1)
{
fputs("\\cell\\intbl\\row\n",rtf);
}
else if(*(short *)(ptr+2)==lastcol)
{
fputs("\\par\\pard ",rtf);
}
else
{
fputs("\\cell\\pard ",rtf);
}
}
else
{
fputs("\n\\par ",rtf);
}
ptr++;
break;
case 0x83:
fputs("\\tab ",rtf);
ptr++;
break;
case 0x86:
x3=(unsigned char)*ptr++;
x1=*ptr++;
if(x1==0x05) cmd="ewc"; else cmd="bmc";
goto picture;
case 0x87:
x3=(unsigned char)*ptr++;
x1=*ptr++;
if(x1==0x05) cmd="ewl"; else cmd="bml";
goto picture;
case 0x88:
x3=(unsigned char)*ptr++;
x1=*ptr++;
if(x1==0x05) cmd="ewr"; else cmd="bmr";
goto picture;
picture:
l1=scanlong(&ptr);
switch(x1)
{
case 0x22: /* HC31 */
ActualTopicOffset+=scanword(&ptr); /* number of hotspots in picture */
if(ActualTopicOffset>MaxTopicOffset) ActualTopicOffset=MaxTopicOffset;
/* fall thru */
case 0x03: /* HC30 */
x1=((unsigned short *)ptr)[0];
switch(x1)
{
case 1:
while(nextbitmap<extensions&&extension[nextbitmap]<0x10) nextbitmap++;
if(nextbitmap>=extensions)
{
error("Bitmap never saved");
break;
}
x2=nextbitmap++;
goto other;
case 0:
x2=((unsigned short *)ptr)[1];
other:
if(makertf)
{
switch(x3)
{
case 0x86:
fprintf(rtf,"{\\field {\\*\\fldinst import %s}}",getbitmapname(x2));
break;
case 0x87:
fprintf(rtf,"{\\pvpara {\\field {\\*\\fldinst import %s}}\\par}\n",getbitmapname(x2));
break;
case 0x88:
fprintf(rtf,"{\\pvpara\\posxr{\\field {\\*\\fldinst import %s}}\\par}\n",getbitmapname(x2));
break;
}
}
else
{
if(x2<extensions&&(extension[x2]&0x20))
{
if(strcmp(cmd,"bmc")==0) cmd="bmct";
else if(strcmp(cmd,"bml")==0) cmd="bmlt";
else if(strcmp(cmd,"bmr")==0) cmd="bmrt";
}
fprintf(rtf,"\\{%s %s\\}",cmd,getbitmapname(x2));
}
break;
}
break;
case 0x05: /* ewc,ewl,ewr */
if(ptr[6]=='!')
{
fprintf(rtf,"\\{button %s\\}",ptr+7);
}
else if(ptr[6]=='*')
{
char *plus;
int n,c1,c2;
sscanf(ptr+7,"%d,%d,%n",&c1,&c2,&n);
plus=strchr(ptr+7+n,'+');
if((c1&0xFFF5)!=0x8400) fprintf(stderr,"mci c1=%04x\n",c1);
fputs("\\{mci",rtf);
if(cmd[2]=='r') fputs("_right",rtf);
if(cmd[2]=='l') fputs("_left",rtf);
if(c2==1) fputs(" REPEAT",rtf);
if(c2==2) fputs(" PLAY",rtf);
if(!plus) fputs(" EXTERNAL",rtf);
if(c1&8) fputs(" NOMENU",rtf);
if(c1&2) fputs(" NOPLAYBAR",rtf);
fprintf(rtf,",%s\\}\n",plus?plus+1:ptr+7+n);
}
else
{
fprintf(rtf,"\\{%s %s\\}",cmd,ptr+6);
}
break;
}
ptr+=l1;
break;
case 0x89: /* end of hotspot */
if(!makertf)
{
if(hotspot[0]=='%'&&fontset>=0&&fontset<fonts&&font[fontset].Underline)
{
hotspot[0]='*';
}
}
ChangeFont(rtf,fontset,ul=FALSE,uldb=FALSE);
if(!makertf)
{
if(!after31&&strlen(hotspot)>255)
{
puts("Help compiler will issue Warning 4072: Context string exceeds limit of 255 characters");
}
fputs("{\\v ",rtf);
putrtf(rtf,multi&&(hotspot[0]=='%'||hotspot[0]=='*')?hotspot+1:hotspot);
fputc('}',rtf);
}
ptr++;
break;
case 0xC8: /* macro */
ChangeFont(rtf,fontset,FALSE,uldb=TRUE);
if(!makertf)
{
hotspot=my_realloc(hotspot,strlen(ptr+3)+2);
sprintf(hotspot,"!%s",ptr+3);
}
ptr+=*(short *)(ptr+1)+3;
break;
case 0xCC: /* macro without font change */
ChangeFont(rtf,fontset,FALSE,uldb=TRUE);
if(!makertf)
{
hotspot=my_realloc(hotspot,strlen(ptr+3)+3);
sprintf(hotspot,"%%!%s",ptr+3);
}
ptr+=*(short *)(ptr+1)+3;
break;
case 0xE0: /* popup jump HC30 */
ChangeFont(rtf,fontset,ul=TRUE,FALSE);
goto label0;
case 0xE1: /* topic jump HC30 */
ChangeFont(rtf,fontset,FALSE,uldb=TRUE);
label0:
if(!makertf)
{
hotspot=my_realloc(hotspot,128);
sprintf(hotspot,"TOPIC%ld",*(long *)(ptr+1));
}
ptr+=5;
break;
case 0xE2: /* popup jump HC31 */
ChangeFont(rtf,fontset,ul=TRUE,FALSE);
goto label1;
case 0xE3: /* topic jump HC31 */
ChangeFont(rtf,fontset,FALSE,uldb=TRUE);
label1:
if(!makertf)
{
arg=ContextId(*(long *)(ptr+1));
hotspot=my_realloc(hotspot,strlen(arg)+1);
sprintf(hotspot,"%s",arg);
}
ptr+=5;
break;
case 0xE6: /* popup jump without font change */
ChangeFont(rtf,fontset,ul=TRUE,FALSE);
goto label2;
case 0xE7: /* topic jump without font change */
ChangeFont(rtf,fontset,FALSE,uldb=TRUE);
label2:
if(!makertf)
{
arg=ContextId(*(long *)(ptr+1));
hotspot=my_realloc(hotspot,strlen(arg)+2);
sprintf(hotspot,"%%%s",arg);
}
ptr+=5;
break;
case 0xEA: /* popup jump into external file */
case 0xEE:
ChangeFont(rtf,fontset,ul=TRUE,FALSE);
goto label3;
case 0xEB: /* topic jump into external file / secondary window */
case 0xEF:
ChangeFont(rtf,fontset,FALSE,uldb=TRUE);
label3:
if(!makertf)
{
if((unsigned char)ptr[0]==0xEE||(unsigned char)ptr[0]==0xEF)
{
cmd="%";
}
else
{
cmd="";
}
arg=unhash(*(long *)(ptr+4)); // no ContextId, it may jump into external file
switch((unsigned char)ptr[3])
{
case 0:
hotspot=my_realloc(hotspot,strlen(cmd)+strlen(arg)+1);
sprintf(hotspot,"%s%s",cmd,arg);
break;
case 1:
hotspot=my_realloc(hotspot,strlen(cmd)+strlen(arg)+1+strlen(GetWindowName(ptr[8]))+1);
sprintf(hotspot,"%s%s>%s",cmd,arg,GetWindowName(ptr[8]));
break;
case 4:
hotspot=my_realloc(hotspot,strlen(cmd)+strlen(arg)+1+strlen(ptr+8)+1);
sprintf(hotspot,"%s%s@%s",cmd,arg,ptr+8);
break;
case 6:
hotspot=my_realloc(hotspot,strlen(cmd)+strlen(arg)+1+strlen(ptr+8)+1+strlen(strchr(ptr+8,'\0')+1)+1);
sprintf(hotspot,"%s%s>%s@%s",cmd,arg,ptr+8,strchr(ptr+8,'\0')+1);
break;
}
}
ptr+=*(short *)(ptr+1)+3;
break;
case 0x8B:
fputs("\\~",rtf);
ptr++;
break;
case 0x8C:
fputs("\\-",rtf);
ptr++;
break;
default:
ptr++;
}
}
}
}
if(LinkData1) free(LinkData1);
if(LinkData2) free(LinkData2);
if(before31)
{
TopicPos+=TopicLink.NextBlock;
}
else
{
TopicOffset=NextTopicOffset(TopicOffset,TopicLink.NextBlock,TopicPos);
TopicPos=TopicLink.NextBlock;
}
}
}
return rtf;
}
int ContextRecCmp(const void *a,const void *b)
{
if(((const CONTEXTREC *)a)->TopicOffset<((const CONTEXTREC *)b)->TopicOffset) return -1;
if(((const CONTEXTREC *)a)->TopicOffset>((const CONTEXTREC *)b)->TopicOffset) return 1;
return 0;
}
void ContextLoad(FILE *HelpFile)
{
BUFFER buf;
int n;
long entries;
if(SearchFile(HelpFile,"|CONTEXT",NULL))
{
n=GetFirstPage(HelpFile,&buf,&entries);
if(entries)
{
ContextRec=my_malloc(entries*sizeof(CONTEXTREC));
ContextRecs=0;
while(n)
{
my_fread(ContextRec+ContextRecs,n*sizeof(CONTEXTREC),HelpFile);
ContextRecs+=n;
n=GetNextPage(HelpFile,&buf);
}
fprintf(stderr,"%d topic offsets and hash values loaded\n",ContextRecs);
qsort(ContextRec,ContextRecs,sizeof(CONTEXTREC),ContextRecCmp);
}
}
else if(SearchFile(HelpFile,"|TOMAP",&entries))
{
Topic=my_malloc(entries);
my_fread(Topic,entries,HelpFile);
Topics=(int)(entries/sizeof(long));
}
}
void GenerateContent(FILE *HelpFile,FILE *ContentFile) /* create a simple Win95 contents file */
{
VIOLAREC *WindowRec;
long FileLength,offset;
int n,i,j,WindowRecs;
BUFFER buf;
char *ptr;
fprintf(ContentFile,":Base %s%s>main\n",name,ext);
if(HelpFileTitle[0]) fprintf(ContentFile,":Title %s\n",HelpFileTitle);
WindowRecs=0;
if(SearchFile(HelpFile,"|VIOLA",NULL))
{
n=GetFirstPage(HelpFile,&buf,&FileLength);
if(FileLength)
{
WindowRec=my_malloc(FileLength*sizeof(VIOLAREC));
while(n)
{
my_fread(WindowRec+WindowRecs,n*sizeof(VIOLAREC),HelpFile);
WindowRecs+=n;
n=GetNextPage(HelpFile,&buf);
}
}
}
if(SearchFile(HelpFile,"|TTLBTREE",NULL))
{
for(n=GetFirstPage(HelpFile,&buf,NULL);n;n=GetNextPage(HelpFile,&buf))
{
for(i=0;i<n;i++)
{
offset=getdw(HelpFile);
if(my_gets(buffer,sizeof(buffer),HelpFile))
{
ptr=TopicName(offset);
if(ptr)
{
fprintf(ContentFile,"1 %s=%s",buffer,ptr);
for(j=0;j<WindowRecs;j++)
{
if(WindowRec[j].TopicOffset==offset)
{
fprintf(ContentFile,">%s",GetWindowName(WindowRec[j].WindowNumber));
break;
}
}
putc('\n',ContentFile);
}
}
}
}
}
}
void ListRose(FILE *HelpFile,FILE *hpj)
{
long FileLength,offset,hash,h,pos,savepos;
unsigned char *ptr;
long *keytopic;
int n,i,l,e;
unsigned short j,count;
BUFFER buf,buf2;
if(SearchFile(HelpFile,"|Rose",NULL))
{
savepos=ftell(HelpFile);
if(SearchFile(HelpFile,"|KWDATA",&FileLength))
{
keytopic=my_malloc(FileLength);
my_fread(keytopic,FileLength,HelpFile);
if(SearchFile(HelpFile,"|KWBTREE",NULL))
{
fputs("[MACROS]\n",hpj);
for(n=GetFirstPage(HelpFile,&buf,NULL);n;n=GetNextPage(HelpFile,&buf))
{
for(i=0;i<n;i++)
{
my_gets(keyword,sizeof(keyword),HelpFile);
for(hash=0L,ptr=(unsigned char *)keyword;*ptr;ptr++)
{
hash=hash*43L+table[*ptr];
}
count=my_getw(HelpFile);
offset=getdw(HelpFile);
for(j=0;j<count;j++)
{
if(keytopic[offset/4+j]==-1L)
{
pos=ftell(HelpFile);
fseek(HelpFile,savepos,SEEK_SET);
for(l=GetFirstPage(HelpFile,&buf2,NULL);l;l=GetNextPage(HelpFile,&buf2))
{
for(e=0;e<l;e++)
{
h=getdw(HelpFile);
my_gets(buffer,sizeof(buffer),HelpFile);
if(h==hash)
{
fprintf(hpj,"%s\n%s\n",keyword,buffer);
my_gets(buffer,sizeof(buffer),HelpFile);
fprintf(hpj,"%s\n",buffer);
}
else
{
my_gets(buffer,sizeof(buffer),HelpFile);
}
}
}
fseek(HelpFile,pos,SEEK_SET);
break;
}
}
}
}
putc('\n',hpj);
}
free(keytopic);
}
}
}
/* dump section: all the dump-routines are used to display internal files
// of the help file with known format of contents for debugging reasons */
void PrintNewFont(int i,NEWFONT *newfont)
{
printf("%3d: %-32.32s %6ld %-6s %02X%02X%02X %02X%02X%02X ",i,fontname[newfont->FontName],newfont->Height,FontFamily(newfont->PitchAndFamily>>4),newfont->FGRGB[2],newfont->FGRGB[1],newfont->FGRGB[0],newfont->BGRGB[2],newfont->BGRGB[1],newfont->BGRGB[0]);
if(newfont->Weight>500) putchar('b');
if(newfont->Italic) putchar('i');
if(newfont->Underline) putchar('u');
if(newfont->StrikeOut) putchar('s');
if(newfont->DoubleUnderline) putchar('d');
if(newfont->SmallCaps) putchar('c');
putchar('\n');
}
void PrintMvbFont(int i,MVBFONT *mvbfont)
{
printf("%3d: %-32.32s %6ld %-6s %02X%02X%02X %02X%02X%02X ",i,fontname[mvbfont->FontName],mvbfont->Height,FontFamily(mvbfont->PitchAndFamily>>4),mvbfont->FGRGB[2],mvbfont->FGRGB[1],mvbfont->FGRGB[0],mvbfont->BGRGB[2],mvbfont->BGRGB[1],mvbfont->BGRGB[0]);
if(mvbfont->Weight>500) putchar('b');
if(mvbfont->Italic) putchar('i');
if(mvbfont->Underline) putchar('u');
if(mvbfont->StrikeOut) putchar('s');
if(mvbfont->DoubleUnderline) putchar('d');
if(mvbfont->SmallCaps) putchar('c');
putchar('\n');
}
void FontDump(FILE *HelpFile)
{
FONTHEADER FontHdr;
long FileStart;
OLDFONT oldfont;
NEWFONT newfont;
NEWSTYLE newstyle;
MVBFONT mvbfont;
MVBSTYLE mvbstyle;
int i,n;
/* Go to the FONT file and get the headers */
FileStart=ftell(HelpFile);
my_fread(&FontHdr,sizeof(FontHdr),HelpFile);
n=(FontHdr.DescriptorsOffset-FontHdr.FacenamesOffset)/FontHdr.NumFacenames;
fontname=my_malloc(FontHdr.NumFacenames*sizeof(char *));
fseek(HelpFile,FileStart+FontHdr.FacenamesOffset,SEEK_SET);
for(i=0;i<FontHdr.NumFacenames;i++)
{
my_fread(buffer,n,HelpFile);
buffer[n]='\0';
printf("Font name %d: %s\n",i,buffer);
fontname[i]=my_strdup(buffer);
}
puts("Font Facename Height Family Foregr Backgr Style");
fseek(HelpFile,FileStart+FontHdr.DescriptorsOffset,SEEK_SET);
if(FontHdr.FacenamesOffset>=16)
{
for(i=0;i<FontHdr.NumDescriptors;i++)
{
my_fread(&mvbfont,sizeof(mvbfont),HelpFile);
PrintMvbFont(i,&mvbfont);
}
fseek(HelpFile,FileStart+FontHdr.FormatsOffset,SEEK_SET);
for(i=0;i<FontHdr.NumFormats;i++)
{
my_fread(&mvbstyle,sizeof(mvbstyle),HelpFile);
printf("Style %d",mvbstyle.StyleNum);
if(mvbstyle.BasedOn) printf(" based on %d",mvbstyle.BasedOn);
printf(" named '%s':\n",mvbstyle.StyleName);
PrintMvbFont(i,&mvbstyle.font);
}
}
else if(FontHdr.FacenamesOffset>=12)
{
for(i=0;i<FontHdr.NumDescriptors;i++)
{
my_fread(&newfont,sizeof(newfont),HelpFile);
PrintNewFont(i,&newfont);
}
fseek(HelpFile,FileStart+FontHdr.FormatsOffset,SEEK_SET);
for(i=0;i<FontHdr.NumFormats;i++)
{
my_fread(&newstyle,sizeof(newstyle),HelpFile);
printf("Style %d",newstyle.StyleNum);
if(newstyle.BasedOn) printf(" based on %d",newstyle.BasedOn);
printf(" named '%s':\n",newstyle.StyleName);
PrintNewFont(i,&newstyle.font);
}
}
else
{
for(i=0;i<FontHdr.NumDescriptors;i++)
{
my_fread(&oldfont,sizeof(oldfont),HelpFile);
printf("%3d: %-32.32s %4d.%d %-6s %02X%02X%02X %02X%02X%02X ",i,fontname[oldfont.FontName],oldfont.HalfPoints/2,(oldfont.HalfPoints&1)*5,FontFamily(oldfont.FontFamily<6?lookup[oldfont.FontFamily]:oldfont.FontFamily),oldfont.FGRGB[2],oldfont.FGRGB[1],oldfont.FGRGB[0],oldfont.BGRGB[2],oldfont.BGRGB[1],oldfont.BGRGB[0]);
if(oldfont.Attributes&FONT_BOLD) putchar('b');
if(oldfont.Attributes&FONT_ITAL) putchar('i');
if(oldfont.Attributes&FONT_UNDR) putchar('u');
if(oldfont.Attributes&FONT_STRK) putchar('s');
if(oldfont.Attributes&FONT_DBUN) putchar('d');
if(oldfont.Attributes&FONT_SMCP) putchar('c');
putchar('\n');
}
}
}
void PhrImageDump(FILE *HelpFile)
{
long FileLength;
unsigned int bytes;
PHRINDEXHDR PhrIndexHdr;
unsigned char *ptr;
if(SearchFile(HelpFile,"|PhrIndex",NULL))
{
my_fread(&PhrIndexHdr,sizeof(PhrIndexHdr),HelpFile);
if(SearchFile(HelpFile,"|PhrImage",&FileLength))
{
if(PhrIndexHdr.phrimagesize==PhrIndexHdr.phrimagecompressedsize)
{
HexDump(HelpFile,FileLength,0L);
}
else
{
if(FileLength!=PhrIndexHdr.phrimagecompressedsize)
{
fprintf(stderr,"PhrImage FileSize %ld, in PhrIndex.FileHdr %ld\n",PhrIndexHdr.phrimagecompressedsize,FileLength);
}
ptr=my_malloc(PhrIndexHdr.phrimagesize);
bytes=DecompressIntoBuffer(2,HelpFile,FileLength,ptr,PhrIndexHdr.phrimagesize);
HexDumpMemory(ptr,bytes);
free(ptr);
}
}
}
}
void BTreeDump(FILE *HelpFile,char text[])
{
int n,i,j;
long count;
BUFFER buf;
char format[10];
char *ptr;
n=GetFirstPage(HelpFile,&buf,NULL);
while(n)
{
for(i=0;i<n;i++)
{
for(ptr=text;*ptr;ptr++)
{
if(*ptr=='%')
{
j=strcspn(ptr,"hsdiouxX!");
memcpy(format,ptr,j+1);
format[j+1]='\0';
if(format[j]=='!')
{
count=getdw(HelpFile);
while(count>=8)
{
printf(" (%ld)",getdw(HelpFile));
printf("%08lx",getdw(HelpFile));
count-=8;
}
}
else if(format[j]=='h')
{
format[j]='s';
printf(format,unhash(getdw(HelpFile)));
}
else if(format[j]=='s')
{
my_gets(buffer,sizeof(buffer),HelpFile);
printf(format,buffer);
}
else if(strchr(format,'l'))
{
printf(format,getdw(HelpFile));
}
else
{
printf(format,my_getw(HelpFile));
}
ptr+=j;
}
else
{
putchar(*ptr);
}
}
}
n=GetNextPage(HelpFile,&buf);
}
}
void PhraseDump(void)
{
unsigned int n;
for(n=0;n<PhraseCount;n++)
{
printf("%-5d - ",n);
PrintPhrase(n,NULL,NULL);
putchar('\n');
}
}
void SysDump(FILE *HelpFile)
{
SYSTEMHEADER SysHdr;
SYSTEMRECORD *SysRec;
struct tm *TimeRec;
char *ptr;
my_fread(&SysHdr,sizeof(SysHdr),HelpFile);
if(SysHdr.Minor==15)
{
ptr="HC30";
}
else if(SysHdr.Minor==21)
{
ptr="HC31/HCP";
}
else if(SysHdr.Minor==27)
{
ptr="WMVC/MVCC";
}
else if(SysHdr.Minor==33)
{
if(mvp)
{
ptr="MVC";
}
else
{
ptr="HCRTF";
}
}
else ptr="Unknown";
printf("%s Help Compiler used.\n",ptr);
printf("System Flags & Compression Method=0x%04x\n",SysHdr.Flags);
if(SysHdr.GenDate)
{
TimeRec=localtime(&SysHdr.GenDate);
printf("Help File Generated: %s",asctime(TimeRec));
}
if(SysHdr.Minor<16)
{
my_gets(HelpFileTitle,33,HelpFile);
printf("TITLE=%s\n",HelpFileTitle);
}
else for(SysRec=GetFirstSystemRecord(HelpFile);SysRec;SysRec=GetNextSystemRecord(SysRec))
{
switch(SysRec->RecordType)
{
case 0x0001:
printf("TITLE=%s\n",SysRec->Data);
break;
case 0x0002:
printf("COPYRIGHT=%s\n",SysRec->Data);
break;
case 0x0003:
printf("CONTENTS=0x%08lX\n",*(long *)SysRec->Data);
break;
case 0x0004:
printf("[MACRO] %s\n",SysRec->Data);
break;
case 0x0005:
puts("Icon in System record");
break;
case 0x0006:
printf("[WINDOW] ");
if(SysRec->DataSize==sizeof(SECWINDOW))
{
PrintWindow(stdout,(SECWINDOW *)SysRec->Data);
}
else if(SysRec->DataSize==sizeof(MVBWINDOW))
{
PrintMVBWindow(stdout,(MVBWINDOW *)SysRec->Data);
}
else
{
HexDumpMemory(SysRec->Data,SysRec->DataSize);
error("[WINDOW] data size does not match");
}
break;
case 0x0008:
printf("CITATION=%s\n",SysRec->Data);
break;
case 0x0009:
if(!mvp) printf("LCID=0x%X 0x%X 0x%X\n",*(short *)(SysRec->Data+8),*(short *)SysRec->Data,*(short *)(SysRec->Data+2));
break;
case 0x000A:
if(!mvp) printf("CNT=%s\n",SysRec->Data);
break;
case 0x000B:
// if(!mvp) printf("CHARSET=%d\n",*(unsigned char *)(SysRec->Data+1));
break;
case 0x000C:
if(mvp)
{
printf("[FTINDEX] dtype %s\n",SysRec->Data);
}
else
{
printf("DEFFONT=%s,%d,%d\n",SysRec->Data+2,*(unsigned char *)SysRec->Data,*(unsigned char *)(SysRec->Data+1));
}
break;
case 0x000D:
if(mvp) printf("[GROUPS] %s\n",SysRec->Data);
break;
case 0x000E:
if(mvp)
{
printf("[KEYINDEX] keyword=%c, \"%s\"\n",SysRec->Data[1],SysRec->Data+30);
}
else
{
printf("INDEX_SEPARATORS=\"%s\"\n",SysRec->Data);
}
break;
case 0x0012:
if(SysRec->Data[0]) printf("LANGUAGE=%s\n",SysRec->Data);
break;
case 0x0013:
ptr=SysRec->Data+strlen(SysRec->Data)+1;
printf("[DLLMAPS] %s=%s,",SysRec->Data,ptr);
ptr+=strlen(ptr)+1;
printf("%s,",ptr);
ptr+=strlen(ptr)+1;
printf("%s,",ptr);
ptr+=strlen(ptr)+1;
printf("%s\n",ptr);
break;
default:
fprintf(stderr,"Unknown record type: 0x%04X\n",SysRec->RecordType);
HexDumpMemory(SysRec->Data,SysRec->DataSize);
}
}
}
/* dump the contents of |TOPIC for debugging */
void DumpTopic(FILE *HelpFile,long TopicPos)
{
TOPICLINK TopicLink;
TOPICHEADER30 *TopicHdr30;
TOPICHEADER *TopicHdr;
char *ptr;
char *str;
char *cmd;
long l;
unsigned short x1,x2;
unsigned char b;
int cols,col;
short i;
char *LinkData1;
char *LinkData2;
long TopicNum;
long TopicOffset;
if(!SearchFile(HelpFile,"|TOPIC",&TopicFileLength)) return;
TopicOffset=0L;
if(TopicPos<12) TopicPos=12L;
TopicNum=16;
while(TopicRead(HelpFile,TopicPos,&TopicLink,sizeof(TopicLink))==sizeof(TOPICLINK))
{
puts("----------------------------------------------------------------------------");
printf("TopicLink Type %02x: BlockSize=%08lx DataLen1=%08lx DataLen2=%08lx\n",TopicLink.RecordType,TopicLink.BlockSize,TopicLink.DataLen1,TopicLink.DataLen2);
printf("TopicPos=%08lx TopicOffset=%08lx PrevBlock=%08lx NextBlock=%08lx\n",TopicPos,TopicOffset,TopicLink.PrevBlock,TopicLink.NextBlock);
if(TopicLink.DataLen1>sizeof(TOPICLINK))
{
LinkData1=my_malloc(TopicLink.DataLen1-sizeof(TOPICLINK));
if(TopicRead(HelpFile,0L,LinkData1,TopicLink.DataLen1-sizeof(TOPICLINK))!=TopicLink.DataLen1-sizeof(TOPICLINK)) break;
}
else LinkData1=NULL;
if(TopicLink.DataLen1<TopicLink.BlockSize) /* read LinkData2 using phrase replacement */
{
LinkData2=my_malloc(TopicLink.DataLen2+1);
if(TopicPhraseRead(HelpFile,0L,LinkData2,TopicLink.BlockSize-TopicLink.DataLen1,TopicLink.DataLen2)!=TopicLink.BlockSize-TopicLink.DataLen1) break;
}
else LinkData2=NULL;
if(LinkData1) HexDumpMemory(LinkData1,TopicLink.DataLen1-sizeof(TOPICLINK));
if(TopicLink.RecordType==TL_TOPICHDR)
{
if(before31)
{
TopicHdr30=(TOPICHEADER30 *)LinkData1;
puts("============================================================================");
printf("TopicHeader TopicNum=%ld BlockSize=%ld PrevTopicNum=%d NextTopicNum=%d\n",TopicNum,TopicHdr30->BlockSize,TopicHdr30->PrevTopicNum,TopicHdr30->NextTopicNum);
TopicNum++;
}
else
{
TopicHdr=(TOPICHEADER *)LinkData1;
puts("============================================================================");
printf("TopicHeader TopicNum=%ld BlockSize=%ld NextTopicOffset=%08lx\n",TopicHdr->TopicNum,TopicHdr->BlockSize,TopicHdr->NextTopic);
printf("NonScroll=%08lx Scroll=%08lx BrowseBck=%08lx BrowseFor=%08lx\n",TopicHdr->NonScroll,TopicHdr->Scroll,TopicHdr->BrowseBck,TopicHdr->BrowseFor);
}
}
else if(TopicLink.RecordType==TL_DISPLAY30||TopicLink.RecordType==TL_DISPLAY||TopicLink.RecordType==TL_TABLE)
{
switch(TopicLink.RecordType)
{
case TL_DISPLAY30:
fputs("Text ",stdout);
break;
case TL_DISPLAY:
fputs("Display ",stdout);
break;
case TL_TABLE:
fputs("Table ",stdout);
break;
}
ptr=LinkData1;
printf("expandedsize=%ld ",scanlong(&ptr));
if(TopicLink.RecordType==TL_DISPLAY||TopicLink.RecordType==TL_TABLE)
{
x1=scanword(&ptr);
TopicOffset+=x1;
printf("topicoffsetincrement=%u ",x1);
}
if(TopicLink.RecordType==TL_TABLE)
{
cols=(unsigned char)*ptr++;
x1=*ptr++;
printf("columns=%d type=%d ",cols,x1);
switch(x1)
{
case 0:
case 2:
printf("minwidth=%d ",*(short *)ptr);
ptr+=2;
case 1:
case 3:
break;
default:
error("Unknown TableType %d",x1);
}
for(col=0;col<cols;col++)
{
printf("width=%d gap=%d ",*(short *)ptr,*(short *)(ptr+2));
ptr+=4;
}
}
putchar('\n');
str=LinkData2;
for(col=0;(TopicLink.RecordType==TL_TABLE?*(short *)ptr!=-1:col==0)&&ptr<LinkData1+TopicLink.DataLen1-sizeof(TOPICLINK);col++)
{
if(TopicLink.RecordType==TL_TABLE)
{
printf("column=%d %04x %d ",*(short *)ptr,*(unsigned short *)(ptr+2),*(unsigned char *)(ptr+4)-0x80);
ptr+=5;
}
printf("%02x %d id=%04x ",*(unsigned char *)ptr,*(unsigned char *)(ptr+1)-0x80,*(unsigned short *)(ptr+2));
ptr+=4;
x2=*((unsigned short *)ptr);
ptr=((unsigned short *)ptr)+1;
if(x2&0x0001) printf("unknownbit01=%ld ",scanlong(&ptr)); /* found in MVBs, purpose unknown, may mean that x2 is compressed long */
if(x2&0x0002) printf("topspacing=%d ",scanint(&ptr));
if(x2&0x0004) printf("bottomspacing=%d ",scanint(&ptr));
if(x2&0x0008) printf("linespacing=%d ",scanint(&ptr));
if(x2&0x0010) printf("leftindent=%d ",scanint(&ptr));
if(x2&0x0020) printf("rightindent=%d ",scanint(&ptr));
if(x2&0x0040) printf("firstlineindent=%d ",scanint(&ptr));
if(x2&0x0080) fputs("unknownbit80set",stdout);
if(x2&0x0100)
{
x1=(unsigned char)*ptr++;
if(x1&1) fputs("box ",stdout);
if(x1&2) fputs("topborder ",stdout);
if(x1&4) fputs("leftborder ",stdout);
if(x1&8) fputs("bottomborder ",stdout);
if(x1&0x10) fputs("rightborder ",stdout);
if(x1&0x20) fputs("thickborder ",stdout);
if(x1&0x40) fputs("doubleborder ",stdout);
if(x1&0x80) fputs("unknownborder",stdout);
printf("%04x ",*((unsigned short *)ptr));
ptr=(unsigned short*)ptr+1;
}
if(x2&0x0200)
{
i=scanint(&ptr);
printf("tabs=%d ",i);
while(i-->0)
{
x1=scanword(&ptr);
printf("stop=%d ",x1&0x3FFF);
if(x1&0x4000)
{
x1=scanword(&ptr);
if(x1==1)
{
fputs("right ",stdout);
}
else if(x1==2)
{
fputs("center ",stdout);
}
else
{
error("unknowntabmodifier=%02x",x1);
}
}
}
}
if(x2&0x0400) fputs("rightalign ",stdout);
if(x2&0x0800) fputs("centeralign ",stdout);
if(x2&0x1000) fputs("keeplinestogether ",stdout);
if(x2&0x2000) fputs("unknownbit2000set ",stdout); /* found in PRINTMAN.HLP */
if(x2&0x4000) fputs("unknownbit4000set ",stdout); /* found in PRINTMAN.HLP, RATTLER.HLP */
if(x2&0x8000) fputs("unknownbit8000set",stdout);
putchar('\n');
while(1)
{
str=PrintString(str,strlen(str))+1;
if((unsigned char)ptr[0]==0xFF)
{
ptr++;
break;
}
else switch((unsigned char)ptr[0])
{
case 0x20:
printf("{vfld%ld}",*(long *)(ptr+1));
ptr+=5;
break;
case 0x21:
printf("{dtype%d}",*(short *)(ptr+1));
ptr+=3;
break;
case 0x80: /* font change */
printf("[font=%u]",*(short *)(ptr+1));
ptr+=3;
break;
case 0x81:
puts("[LF]");
ptr++;
break;
case 0x82:
puts("[CR]");
ptr++;
break;
case 0x83:
fputs("[TAB]",stdout);
ptr++;
break;
case 0x86:
ptr++;
b=*ptr++;
if(b==0x05) cmd="ewc"; else cmd="bmc";
goto picture;
case 0x87:
ptr++;
b=*ptr++;
if(b==0x05) cmd="ewl"; else cmd="bml";
goto picture;
case 0x88:
ptr++;
b=*ptr++;
if(b==0x05) cmd="ewr"; else cmd="bmr";
picture:
printf("[%s %02x ",cmd,b);
l=scanlong(&ptr);
switch(b)
{
case 0x22: /* HC31 */
x1=scanword(&ptr);
printf("hotspots=%u ",x1);
case 0x03: /* HC30 */
switch(*(unsigned short *)ptr)
{
case 0:
fputs("baggage ",stdout);
break;
case 1:
fputs("embedded ",stdout);
break;
default:
error("Unknown %04x",((unsigned short *)ptr)[0]);
}
printf("bm%u]",((unsigned short *)ptr)[1]);
break;
case 0x05:
printf("%04x ",((unsigned short *)ptr)[0]);
printf("%04x ",((unsigned short *)ptr)[1]);
printf("%04x ",((unsigned short *)ptr)[2]);
printf("%s]",ptr+6);
break;
default:
error("Unknown picture flag %02x",b);
}
ptr+=l;
break;
case 0x89: /* end of hot spot */
fputs("[U]",stdout);
ptr++;
break;
case 0x8B: /* non-break-space */
fputs("[~]",stdout);
ptr++;
break;
case 0x8C: /* non-break-hyphen */
fputs("[-]",stdout);
ptr++;
break;
case 0xC8: /* macro */
printf("[!%s]",ptr+3);
ptr+=*(short *)(ptr+1)+3;
break;
case 0xCC: /* macro without font change */
printf("[*!%s]",ptr+3);
ptr+=*(short *)(ptr+1)+3;
break;
case 0xE0: /* Popup HC30 */
printf("[^TOPIC%ld]",*(long *)(ptr+1));
ptr+=5;
break;
case 0xE1: /* Jump HC30 */
printf("[TOPIC%ld]",*(long *)(ptr+1));
ptr+=5;
break;
case 0xE2: /* Popup HC31 */
printf("[^%08lx]",*(long *)(ptr+1));
ptr+=5;
break;
case 0xE3: /* Jump HC31 */
printf("[%08lx]",*(long *)(ptr+1));
ptr+=5;
break;
case 0xE6: /* Popup without font change */
printf("[*^%08lx]",*(long *)(ptr+1));
ptr+=5;
break;
case 0xE7: /* Jump without font change */
printf("[*%08lx]",*(long *)(ptr+1));
ptr+=5;
break;
case 0xEA: /* Popup into external file / secondary window */
cmd="^";
goto jump;
case 0xEB: /* Jump into external file / secondary window */
cmd="";
goto jump;
case 0xEE: /* Popup into external file / secondary window without font change */
cmd="^*";
goto jump;
case 0xEF: /* Jump into external file / secondary window without font change */
cmd="*";
jump:
switch(ptr[3])
{
case 0:
printf("[%s%08lx] ",cmd,*(long *)(ptr+4));
break;
case 1: /* Popup into secondary window (silly) */
printf("[%s%08lx>%d]",cmd,*(long *)(ptr+4),(unsigned char)ptr[8]);
break;
case 4:
printf("[%s%08lx@%s] ",cmd,*(long *)(ptr+4),ptr+8);
break;
case 6: /* Popup into external file / secondary window (silly) */
printf("[%s%08lx>%s@%s] ",cmd,*(long *)(ptr+4),ptr+8,strchr(ptr+8,'\0')+1);
break;
default:
putchar('[');
for(i=0;i<*(short *)(ptr+1);i++) printf("%02x",(unsigned char)ptr[i]);
putchar(']');
}
ptr+=*(short *)(ptr+1)+3;
break;
default:
printf("[%02x]",(unsigned char)*ptr++);
}
}
putchar('\n');
}
}
if(LinkData2&&(TopicLink.RecordType!=TL_DISPLAY30&&TopicLink.RecordType!=TL_DISPLAY&&TopicLink.RecordType!=TL_TABLE))
{
PrintString(LinkData2,TopicLink.DataLen2);
putchar('\n');
}
if(LinkData1) free(LinkData1);
if(LinkData2) free(LinkData2);
if(before31)
{
TopicPos+=TopicLink.NextBlock;
if(TopicPos>=TopicFileLength) break;
}
else
{
if(TopicLink.NextBlock<=0) break;
TopicOffset=NextTopicOffset(TopicOffset,TopicLink.NextBlock,TopicPos);
TopicPos=TopicLink.NextBlock;
}
}
}
void AliasList(FILE *hpj) /* write [ALIAS] section to HPJ file */
{
int i,n;
BOOL headerwritten;
headerwritten=FALSE;
for(i=0;i<ContextRecs;i=n)
{
for(n=i+1;n<ContextRecs&&ContextRec[i].TopicOffset==ContextRec[n].TopicOffset;n++)
{
if(!headerwritten)
{
fputs("Creating [ALIAS] section...\n",stderr);
fputs("[ALIAS]\n",hpj);
headerwritten=TRUE;
}
fprintf(hpj,"%s=",unhash(ContextRec[n].HashValue));
fprintf(hpj,"%s\n",unhash(ContextRec[i].HashValue));
}
}
if(headerwritten) putc('\n',hpj);
}
void CTXOMAPList(FILE *HelpFile,FILE *hpj) /* write [MAP] section to HPJ file */
{
CTXOMAPREC CTXORec;
unsigned short n,i;
char *ptr;
if(SearchFile(HelpFile,"|CTXOMAP",NULL))
{
n=my_getw(HelpFile);
if(n)
{
fputs("Creating [MAP] section...\n",stderr);
fputs("[MAP]\n",hpj);
for(i=0;i<n;i++)
{
my_fread(&CTXORec,sizeof(CTXORec),HelpFile);
ptr=TopicName(CTXORec.TopicOffset);
if(ptr)
{
fprintf(hpj,"%s %ld\n",ptr,CTXORec.MapID);
}
else
{
fprintf(hpj,"TOPIC%08lx %ld\n",CTXORec.TopicOffset,CTXORec.MapID);
}
}
putc('\n',hpj);
}
}
}
void GuessFromKeywords(FILE *HelpFile)
{
long *keytopic;
char kwdata[10];
char kwbtree[10];
int m,i,n,k,l,j,map;
long FileLength,KWDataOffset,TopicOffset;
BUFFER buf;
fputs("Guessing...",stderr);
for(k=0;k<2;k++) for(map='0';map<='z';map++)
{
if(k)
{
if(!keyindex[map-'0']) continue;
sprintf(kwdata,"|%cKWDATA",map);
sprintf(kwbtree,"|%cKWBTREE",map);
}
else
{
if(!lists[map-'0']) continue;
sprintf(kwdata,"|%cWDATA",map);
sprintf(kwbtree,"|%cWBTREE",map);
}
if(SearchFile(HelpFile,kwdata,&FileLength))
{
keytopic=my_malloc(FileLength);
my_fread(keytopic,FileLength,HelpFile);
if(SearchFile(HelpFile,kwbtree,NULL))
{
for(n=GetFirstPage(HelpFile,&buf,NULL);n;n=GetNextPage(HelpFile,&buf))
{
for(i=0;i<n;i++)
{
my_gets(keyword,sizeof(keyword),HelpFile);
m=my_getw(HelpFile);
KWDataOffset=getdw(HelpFile);
for(j=0;j<m;j++)
{
TopicOffset=keytopic[KWDataOffset/4+j];
Guess(keyword,TopicOffset);
for(l=0;l<alternatives;l++)
{
if(alternative[l].OtherTopicOffset==TopicOffset)
{
Guess(keyword,alternative[l].TopicOffset);
}
}
}
}
fputc('.',stderr);
}
free(keytopic);
}
}
}
if(guessed>0)
{
fprintf(stderr,"%ld context ids found\n",guessed);
}
else
{
fputs("no context ids found\n(you may use option /g to turn off guessing on this help file)\n",stderr);
}
}
/* 1. extract topic names from topic macros, embedded pictures, and hotspot macros */
/* 2. build browse sequence start list */
/* 3. extract embedded pictures */
void FirstPass(FILE *HelpFile)
{
SYSTEMRECORD *SysRec;
TOPICLINK TopicLink;
char *LinkData1;
char *LinkData2;
char *ptr;
long l1,TopicNum,TopicPos,TopicOffset,BogusTopicOffset;
int n,i,col,cols;
BUFFER buf;
TOPICHEADER30 *TopicHdr30;
TOPICHEADER *TopicHdr;
char filename[20];
unsigned short x1,x2;
short y1;
MFILE *f;
if(extractmacros)
{
for(SysRec=GetFirstSystemRecord(HelpFile);SysRec;SysRec=GetNextSystemRecord(SysRec))
{
if(SysRec->RecordType==0x0004)
{
strcpy(TopicTitle,"[CONFIG] section");
CheckMacro(SysRec->Data);
}
}
if(SearchFile(HelpFile,"|TopicId",NULL))
{
for(n=GetFirstPage(HelpFile,&buf,NULL);n;n=GetNextPage(HelpFile,&buf))
{
for(i=0;i<n;i++)
{
getdw(HelpFile);
my_gets(buffer,sizeof(buffer),HelpFile);
AddTopic(buffer,FALSE);
}
}
guessing=FALSE; /* it's not neccessary to guess context ids if you know them */
}
}
browses=0;
browsenums=1;
if(!SearchFile(HelpFile,"|TOPIC",&TopicFileLength)) return;
TopicOffset=0L;
TopicPos=12L;
TopicNum=16;
while(TopicRead(HelpFile,TopicPos,&TopicLink,sizeof(TopicLink))==sizeof(TOPICLINK))
{
if(before31)
{
if(TopicPos+TopicLink.NextBlock>=TopicFileLength) break;
}
else
{
if(TopicLink.NextBlock<=0) break;
}
if(TopicLink.DataLen1>sizeof(TOPICLINK))
{
LinkData1=my_malloc(TopicLink.DataLen1-sizeof(TOPICLINK)+1);
if(TopicRead(HelpFile,0L,LinkData1,TopicLink.DataLen1-sizeof(TOPICLINK))!=TopicLink.DataLen1-sizeof(TOPICLINK)) break;
}
else LinkData1=NULL;
if(TopicLink.DataLen1<TopicLink.BlockSize) /* read LinkData2 using phrase replacement */
{
LinkData2=my_malloc(TopicLink.DataLen2+1);
if(TopicPhraseRead(HelpFile,0L,LinkData2,TopicLink.BlockSize-TopicLink.DataLen1,TopicLink.DataLen2)!=TopicLink.BlockSize-TopicLink.DataLen1) break;
}
else LinkData2=NULL;
if(TopicLink.RecordType==TL_TOPICHDR) /* display a topic header record */
{
fprintf(stderr,"\rTopic %ld...",TopicNum-15);
if(before31)
{
TopicHdr30=(TOPICHEADER30 *)LinkData1;
if(resolvebrowse)
{
if((TopicHdr30->NextTopicNum>TopicNum&&TopicHdr30->PrevTopicNum>TopicNum)
|| (TopicHdr30->NextTopicNum==-1&&TopicHdr30->PrevTopicNum>TopicNum)
|| (TopicHdr30->NextTopicNum>TopicNum&&TopicHdr30->PrevTopicNum==-1))
{
AddBrowse(TopicNum,TopicHdr30->NextTopicNum,TopicHdr30->PrevTopicNum);
}
else if(TopicHdr30->NextTopicNum!=-1&&TopicHdr30->NextTopicNum<TopicNum&&TopicHdr30->PrevTopicNum!=-1&&TopicHdr30->PrevTopicNum<TopicNum)
{
MergeBrowse(TopicNum,TopicNum,TopicHdr30->NextTopicNum,TopicHdr30->PrevTopicNum);
}
else if(TopicHdr30->NextTopicNum!=-1&&TopicHdr30->NextTopicNum<TopicNum&&(TopicHdr30->PrevTopicNum==-1||TopicHdr30->PrevTopicNum>TopicNum))
{
BackLinkBrowse(TopicNum,TopicNum,TopicHdr30->NextTopicNum,TopicHdr30->PrevTopicNum);
}
else if(TopicHdr30->PrevTopicNum!=-1&&TopicHdr30->PrevTopicNum<TopicNum&&(TopicHdr30->NextTopicNum==-1||TopicHdr30->NextTopicNum>TopicNum))
{
LinkBrowse(TopicNum,TopicNum,TopicHdr30->NextTopicNum,TopicHdr30->PrevTopicNum);
}
}
}
else
{
BogusTopicOffset=NextTopicOffset(TopicOffset,TopicLink.NextBlock,TopicPos);
if(BogusTopicOffset!=TopicOffset)
{
alternative=my_realloc(alternative,(alternatives+1)*sizeof(ALTERNATIVE));
alternative[alternatives].TopicOffset=TopicOffset;
alternative[alternatives].OtherTopicOffset=BogusTopicOffset;
alternatives++;
}
TopicHdr=(TOPICHEADER *)LinkData1;
if(resolvebrowse)
{
if((TopicHdr->BrowseFor>TopicOffset&&TopicHdr->BrowseBck>TopicOffset)
|| (TopicHdr->BrowseFor==-1L&&TopicHdr->BrowseBck>TopicOffset)
|| (TopicHdr->BrowseFor>TopicOffset&&TopicHdr->BrowseBck==-1L))
{
AddBrowse(TopicOffset,TopicHdr->BrowseFor,TopicHdr->BrowseBck);
}
else if(TopicHdr->BrowseFor!=-1L&&TopicHdr->BrowseFor<TopicOffset&&TopicHdr->BrowseBck!=-1L&&TopicHdr->BrowseBck<TopicOffset)
{
MergeBrowse(TopicOffset,BogusTopicOffset,TopicHdr->BrowseFor,TopicHdr->BrowseBck);
}
else if(TopicHdr->BrowseFor!=-1L&&TopicHdr->BrowseFor<TopicOffset&&(TopicHdr->BrowseBck==-1L||TopicHdr->BrowseBck>TopicOffset))
{
BackLinkBrowse(TopicOffset,BogusTopicOffset,TopicHdr->BrowseFor,TopicHdr->BrowseBck);
}
else if(TopicHdr->BrowseBck!=-1L&&TopicHdr->BrowseBck<TopicOffset&&(TopicHdr->BrowseFor==-1L||TopicHdr->BrowseFor>TopicOffset))
{
LinkBrowse(TopicOffset,BogusTopicOffset,TopicHdr->BrowseFor,TopicHdr->BrowseBck);
}
}
if(extractmacros)
{
if(TopicLink.DataLen2&&*LinkData2)
{
strlcpy(TopicTitle,LinkData2,sizeof(TopicTitle));
if(guessing)
{
Guess(LinkData2,TopicOffset);
if(BogusTopicOffset!=TopicOffset)
{
Guess(LinkData2,BogusTopicOffset);
}
}
}
else
{
strcpy(TopicTitle,"<< untitled topic >>");
}
if(TopicLink.DataLen2)
{
for(i=strlen(LinkData2)+1;i<TopicLink.DataLen2;i+=strlen(LinkData2+i)+1)
{
CheckMacro(LinkData2+i);
}
}
}
}
TopicNum++;
}
else if(TopicLink.RecordType==TL_DISPLAY30||TopicLink.RecordType==TL_DISPLAY||TopicLink.RecordType==TL_TABLE)
{
ptr=LinkData1;
scanlong(&ptr);
if(TopicLink.RecordType==TL_DISPLAY||TopicLink.RecordType==TL_TABLE)
{
TopicOffset+=scanword(&ptr);
}
if(TopicLink.RecordType==TL_TABLE)
{
cols=(unsigned char)*ptr++;
x1=(unsigned char)*ptr++;
switch(x1)
{
case 0: /* found in CALC.HLP and TERMINAL.HLP */
case 2:
ptr+=2;
case 1:
case 3:
break;
default:
error("Unknown TableType %d",x1);
}
ptr+=4*cols;
}
for(col=0;(TopicLink.RecordType==TL_TABLE?*(short *)ptr!=-1:col==0)&&ptr<LinkData1+TopicLink.DataLen1-sizeof(TOPICLINK);col++)
{
if(TopicLink.RecordType==TL_TABLE) ptr+=5;
ptr+=4;
x2=*(unsigned short *)ptr;
ptr+=2;
if(x2&0x0001) scanlong(&ptr); /* found in MVBs, purpose */
/* unknown, may mean that x2 is really compressed long */
if(x2&0x0002) scanint(&ptr);
if(x2&0x0004) scanint(&ptr);
if(x2&0x0008) scanint(&ptr);
if(x2&0x0010) scanint(&ptr);
if(x2&0x0020) scanint(&ptr);
if(x2&0x0040) scanint(&ptr);
if(x2&0x0100) ptr+=3;
if(x2&0x0200)
{
y1=scanint(&ptr);
while(y1-->0) if(scanword(&ptr)&0x4000) scanword(&ptr);
}
while(ptr<LinkData1+TopicLink.DataLen1-sizeof(TOPICLINK))
{
if((unsigned char)ptr[0]==0xFF)
{
ptr++;
break;
}
else switch((unsigned char)ptr[0])
{
case 0x21: /* dtype (MVB) */
case 0x80: /* font change */
ptr+=3;
break;
case 0x81:
case 0x82:
case 0x83:
case 0x89: /* end of hotspot */
case 0x8B: /* non-break-space */
case 0x8C: /* non-break-hyphen */
ptr++;
break;
case 0x86:
case 0x87:
case 0x88:
ptr++;
x1=*ptr++;
l1=scanlong(&ptr);
switch(x1)
{
case 0x22: /* HC31 */
x1=scanword(&ptr);
/* fall thru */
case 0x03: /* HC30 */
switch(((unsigned short *)ptr)[0])
{
case 1:
for(x2=1;x2<extensions;x2++) if(!extension[x2]) break;
if(x2>=extensions)
{
extension=my_realloc(extension,(x2+1)*sizeof(char));
while(extensions<=x2) extension[extensions++]=0;
}
sprintf(filename,"bm%u",x2);
f=CreateMap(ptr+2,l1-2);
x1=ExtractBitmap(filename,f);
CloseMap(f);
extension[x2]=x1|0x10;
break;
}
break;
case 0x05:
if(ptr[6]=='!'&&strchr(ptr+7,','))
{
CheckMacro(strchr(ptr+7,',')+1);
}
break;
}
ptr+=l1;
break;
case 0xC8: /* macro */
case 0xCC: /* macro without font change */
CheckMacro(ptr+3);
ptr+=*(short *)(ptr+1)+3;
break;
case 0x20: /* vfld (MVC) */
case 0xE0: /* popup jump HC30 */
case 0xE1: /* topic jump HC30 */
case 0xE2: /* popup jump HC31 */
case 0xE3: /* topic jump HC31 */
case 0xE6: /* popup jump without font change */
case 0xE7: /* topic jump without font change */
ptr+=5;
break;
case 0xEA: /* popup jump into external file */
case 0xEB: /* topic jump into external file / secondary window */
case 0xEE: /* popup jump into external file without font change */
case 0xEF: /* topic jump into external file / secondary window without font change */
switch((unsigned char)ptr[3])
{
case 0:
case 1:
break;
case 4:
StoreReference(ptr+8,TOPIC,NULL,*(long *)(ptr+4));
break;
case 6:
StoreReference(strchr(ptr+8,'\0')+1,TOPIC,NULL,*(long *)(ptr+4));
break;
default:
error("Unknown modifier %02x in tag %02x",(unsigned char)ptr[3],(unsigned char)ptr[0]);
}
ptr+=*(short *)(ptr+1)+3;
break;
default:
error("Unknown %02x",*(unsigned char *)ptr);
ptr++;
}
}
}
}
if(LinkData1) free(LinkData1);
if(LinkData2) free(LinkData2);
if(before31)
{
TopicPos+=TopicLink.NextBlock;
}
else
{
TopicOffset=NextTopicOffset(TopicOffset,TopicLink.NextBlock,TopicPos);
TopicPos=TopicLink.NextBlock;
}
}
}
int CTXOMAPRecCmp(const void *a,const void *b)
{
if(((CTXOMAPREC *)a)->TopicOffset<((CTXOMAPREC *)b)->TopicOffset) return -1;
if(((CTXOMAPREC *)a)->TopicOffset>((CTXOMAPREC *)b)->TopicOffset) return 1;
return 0;
}
void ContextList(FILE *HelpFile)
{
unsigned short maprecs,m;
int j,window,len;
BOOL morekeywords;
CTXOMAPREC *map;
char filename[13];
TOPICLINK TopicLink;
char *LinkData1;
char *LinkData2;
char *ptr;
long TopicPos,TopicNum,TopicOffset;
if(SearchFile(HelpFile,"|CTXOMAP",NULL))
{
maprecs=my_getw(HelpFile);
if(maprecs)
{
map=my_malloc((long)maprecs*sizeof(CTXOMAPREC));
my_fread(map,(long)maprecs*sizeof(CTXOMAPREC),HelpFile);
qsort(map,maprecs,sizeof(CTXOMAPREC),CTXOMAPRecCmp);
}
}
else
{
maprecs=0;
}
strcpy(filename,name);
strcat(filename,ext);
if(!SearchFile(HelpFile,"|TOPIC",&TopicFileLength)) return;
TopicOffset=0L;
TopicPos=12L;
TopicNum=1;
j=0;
len=80;
window=-1;
morekeywords=TRUE;
m=0;
while(TopicRead(HelpFile,TopicPos,&TopicLink,sizeof(TopicLink))==sizeof(TOPICLINK))
{
if(TopicLink.DataLen1>sizeof(TOPICLINK))
{
LinkData1=my_malloc(TopicLink.DataLen1-sizeof(TOPICLINK)+1);
if(TopicRead(HelpFile,0L,LinkData1,TopicLink.DataLen1-sizeof(TOPICLINK))!=TopicLink.DataLen1-sizeof(TOPICLINK)) break;
}
else LinkData1=NULL;
if(TopicLink.DataLen1<TopicLink.BlockSize) /* read LinkData2 using phrase replacement */
{
LinkData2=my_malloc(TopicLink.DataLen2+1);
if(TopicPhraseRead(HelpFile,0L,LinkData2,TopicLink.BlockSize-TopicLink.DataLen1,TopicLink.DataLen2)!=TopicLink.BlockSize-TopicLink.DataLen1) break;
}
else LinkData2=NULL;
if(TopicLink.NextBlock<=0||TopicLink.RecordType==TL_TOPICHDR) /* display a topic header record */
{
if(TopicNum>1) putchar('\n');
if(before31) TopicOffset=TopicPos;
while(m<maprecs&&map[m].TopicOffset<TopicOffset)
{
printf(" WinHelp(wnd,\"%s\",HELP_CONTEXT,%lu)\n",filename,map[m].MapID);
m++;
}
if(!before31)
{
while(j<ContextRecs&&ContextRec[j].TopicOffset<TopicOffset)
{
if(len==80)
{
fputs(" Jump",stdout);
}
else
{
fputs(" Popup",stdout);
}
printf("Id(`%s",filename);
if(window!=-1) printf(">%s",GetWindowName(window));
printf("',`%s')\n",unhash(ContextRec[j].HashValue));
j++;
}
if(morekeywords)
{
if(NextKeywordRec>=KeywordRecs)
{
if(NextKeywordOffset<0x7FFFFFFFL)
{
CollectKeywords(HelpFile);
}
else
{
morekeywords=FALSE;
}
}
while(NextKeywordRec<KeywordRecs&&KeywordRec[NextKeywordRec].TopicOffset<TopicOffset)
{
if(KeywordRec[NextKeywordRec].Footnote=='K')
{
printf(" JumpKeyword(`%s',`%s')\n",filename,KeywordRec[NextKeywordRec].Keyword);
}
else if(KeywordRec[NextKeywordRec].Footnote=='A')
{
printf(" ALink(`%s@%s')\n",KeywordRec[NextKeywordRec].Keyword,filename);
}
NextKeywordRec++;
}
}
window=ListWindows(HelpFile,TopicOffset);
}
if(TopicLink.NextBlock<=0) break;
if(LinkData2&&*LinkData2)
{
printf("Topic %ld: %s",TopicNum,LinkData2);
len=80;
}
else
{
len=printf("Topic %ld: untitled: ",TopicNum);
}
TopicNum++;
}
else if(LinkData2&&len<79)
{
for(ptr=LinkData2;len<79&&ptr<LinkData2+TopicLink.DataLen2;ptr++) if(*ptr)
{
putchar(*ptr);
len++;
}
}
if(TopicLink.RecordType==TL_DISPLAY||TopicLink.RecordType==TL_TABLE)
{
ptr=LinkData1;
scanlong(&ptr);
TopicOffset+=scanword(&ptr);
}
if(LinkData1) free(LinkData1);
if(LinkData2) free(LinkData2);
if(before31)
{
if(TopicPos+TopicLink.NextBlock>=TopicFileLength) break;
TopicPos+=TopicLink.NextBlock;
}
else
{
if(TopicLink.NextBlock<=0) break;
TopicOffset=NextTopicOffset(TopicOffset,TopicLink.NextBlock,TopicPos);
TopicPos=TopicLink.NextBlock;
}
}
}
BOOL HelpDeCompile(FILE *HelpFile,char *dumpfile,int mode,char *exportname,long offset)
{
char filename[PATH_MAX];
char hpjfilename[PATH_MAX];
long FileLength;
FILE *rtf;
FILE *hpj;
int d;
long topic;
if(!SearchFile(HelpFile,NULL,&FileLength)) return FALSE;
if(!dumpfile)
{
switch(mode)
{
case 0:
SysLoad(HelpFile);
fprintf(stderr,"Decompiling %s...\n",HelpFileTitle);
ContextLoad(HelpFile);
PhraseLoad(HelpFile);
ExportBitmaps(HelpFile);
fputs("Pass 1...\n",stderr);
FirstPass(HelpFile); /* valid only after ExportBitmaps */
putc('\n',stderr);
if(!before31&&guessing) GuessFromKeywords(HelpFile); /* after FirstPass, before SysList */
strcpy(hpjfilename,name);
if(mvp)
{
strcat(hpjfilename,".mvp");
}
else
{
strcat(hpjfilename,".hpj");
}
hpj=my_fopen(hpjfilename,"wt");
if(hpj)
{
strcpy(filename,name);
strcat(filename,".ico");
SysList(HelpFile,hpj,filename); /* after ContextLoad */
ListBaggage(HelpFile,hpj,before31);
if(!mvp) AliasList(hpj); /* after ContextLoad, before TopicDump */
strcpy(filename,name);
strcat(filename,".ph");
PhraseList(filename); /* after PhraseLoad */
BuildName(filename,TopicsPerRTF>0);
rtf=my_fopen(filename,"wt");
if(rtf)
{
FontLoad(HelpFile,rtf,hpj);
fputs("Pass 2...\n",stderr);
fprintf(hpj,"[FILES]\n%s\n\n",filename);
rtf=TopicDump(HelpFile,rtf,hpj,FALSE);
putc('}',rtf);
putc('\n',stderr);
my_fclose(rtf);
}
NotInAnyTopic=FALSE;
CTXOMAPList(HelpFile,hpj);
if(extensions&&before31) ListBitmaps(hpj);
if(win95) ListRose(HelpFile,hpj);
my_fclose(hpj);
}
if(PhraseOffsets)
{
if(win95)
{
puts("Help Compiler will issue Note HC1002: Using existing phrase table");
}
else
{
puts("Help Compiler will issue Warning 5098: Using old key-phrase table");
}
}
if(missing) puts("Help Compiler will issue Error 1230: File 'missing.bmp' not found");
if(NotInAnyTopic) puts("Help Compiler will issue Warning 4098: Context string(s) in [MAP] section not defined in any topic");
if(!extractmacros) puts("Help Compiler may issue Warning 4131: Hash conflict between 'x' and 'y'.");
if(warnings)
{
printf("HELPDECO had problems with %s. Rebuilt helpfile may behave bad.\n",HelpFileName);
}
if(helpcomp[0])
{
if(win95&&SearchFile(HelpFile,"|Petra",NULL)) strcat(helpcomp," /a");
printf("Use %s %s to recompile ",helpcomp,hpjfilename);
if(AnnoFile) fputs("annotated ",stdout);
puts("helpfile.");
}
break;
case 1:
HexDump(HelpFile,FileLength,offset);
break;
case 2:
ListFiles(HelpFile);
break;
case 3: /* create lookalike RTF */
SysLoad(HelpFile);
fprintf(stderr,"Writing %s...\n",HelpFileTitle);
exportplain=TRUE;
ExportBitmaps(HelpFile);
PhraseLoad(HelpFile);
BuildName(filename,TopicsPerRTF>0);
rtf=my_fopen(filename,"wt");
if(rtf)
{
FontLoad(HelpFile,rtf,NULL);
rtf=TopicDump(HelpFile,rtf,NULL,TRUE);
putc('}',rtf);
putc('\n',stderr);
my_fclose(rtf);
}
break;
case 4: /* generate contents file */
SysLoad(HelpFile);
fprintf(stderr,"Scanning %s...\n",HelpFileTitle);
ContextLoad(HelpFile);
PhraseLoad(HelpFile);
checkexternal=TRUE;
ExportBitmaps(HelpFile);
FirstPass(HelpFile);
putc('\n',stderr);
if(!before31&&guessing) GuessFromKeywords(HelpFile); /* after FirstPass, before SysList */
strcpy(filename,name);
strcat(filename,".cnt");
rtf=my_fopen(filename,"wt");
if(rtf)
{
GenerateContent(HelpFile,rtf);
my_fclose(rtf);
}
break;
case 5: /* create entry point list */
resolvebrowse=FALSE;
checkexternal=TRUE;
SysLoad(HelpFile);
fprintf(stderr,"Parsing %s...\n",HelpFileTitle);
ContextLoad(HelpFile);
PhraseLoad(HelpFile);
ExportBitmaps(HelpFile);
FirstPass(HelpFile);
putc('\n',stderr);
if(!before31&&guessing) GuessFromKeywords(HelpFile); /* after FirstPass, before SysList */
ContextList(HelpFile);
break;
case 6: /* check external references */
case 7:
resolvebrowse=FALSE;
checkexternal=TRUE;
SysLoad(HelpFile);
fprintf(stderr,"Checking %s...\n",HelpFileTitle);
PhraseLoad(HelpFile);
FirstPass(HelpFile);
putc('\n',stderr);
if(!external)
{
printf("No references to external files found in %s.\n",HelpFileName);
}
else if(mode==6)
{
CheckReferences();
}
else
{
ListReferences();
}
break;
}
}
else
{
if(!SearchFile(HelpFile,dumpfile,&FileLength))
{
filename[0]='|';
strlcpy(filename+1,dumpfile,sizeof(filename)-1);
if(!SearchFile(HelpFile,filename,&FileLength))
{
fprintf(stderr,"Internal file %s not found.\n",dumpfile);
return TRUE;
}
dumpfile=filename;
}
printf("FileName: %s FileSize: %ld\n",dumpfile,FileLength);
if(exportname) /* export internal file */
{
FILE *f;
f=my_fopen(exportname,"wb");
if(f)
{
copy(HelpFile,FileLength,f);
my_fclose(f);
}
}
else if(mode==1)
{
HexDump(HelpFile,FileLength,offset);
}
else if(strcmp(dumpfile,"|TOPIC")==0)
{
SysLoad(HelpFile);
PhraseLoad(HelpFile);
DumpTopic(HelpFile,offset);
}
else if(strcmp(dumpfile+strlen(dumpfile)-4,".grp")==0)
{
GroupDump(HelpFile);
}
else if(strcmp(dumpfile,"@LINK")==0)
{
LinkDump(HelpFile);
}
else if(sscanf(dumpfile,"%ld!%d",&topic,&d)==2&&topic!=0L&&d==0)
{
AnnotationDump(HelpFile,FileLength,dumpfile);
}
else if(strcmp(dumpfile,"|Phrases")==0||strcmp(dumpfile,"|PhrIndex")==0)
{
SysLoad(HelpFile);
PhraseLoad(HelpFile);
PhraseDump();
}
else if(strcmp(dumpfile,"|SYSTEM")==0)
{
SysDump(HelpFile);
}
else if(strcmp(dumpfile,"|TOMAP")==0)
{
ToMapDump(HelpFile,FileLength);
}
else if(strcmp(dumpfile,"|CONTEXT")==0)
{
BTreeDump(HelpFile,"ContextId: %h TopicOffset: 0x%08lx\n");
}
else if(dumpfile[0]=='|'&&(strcmp(dumpfile+2,"WBTREE")==0||strcmp(dumpfile+2,"KWBTREE")==0))
{
fseek(HelpFile,7,SEEK_CUR);
d=getc(HelpFile);
fseek(HelpFile,-8,SEEK_CUR);
if(d=='!')
{
BTreeDump(HelpFile,"Keyword: '%s' Occurrance: %!\n");
}
else
{
BTreeDump(HelpFile,"Keyword: '%s' Count: %u KWDataAddress: 0x%08lx\n");
}
}
else if(dumpfile[0]=='|'&&(strcmp(dumpfile+2,"WMAP")==0||strcmp(dumpfile+2,"KWMAP")==0))
{
KWMapDump(HelpFile);
}
else if(dumpfile[0]=='|'&&(strcmp(dumpfile+2,"WDATA")==0||strcmp(dumpfile+2,"KWDATA")==0))
{
KWDataDump(HelpFile,FileLength);
}
else if(strcmp(dumpfile,"|VIOLA")==0)
{
BTreeDump(HelpFile,"TopicOffset: 0x%08lx WindowNumber: %ld\n");
}
else if(strcmp(dumpfile,"|CTXOMAP")==0)
{
CTXOMAPDump(HelpFile);
}
else if(strcmp(dumpfile,"|CATALOG")==0)
{
CatalogDump(HelpFile);
}
else if(strcmp(dumpfile,"|Petra")==0)
{
BTreeDump(HelpFile,"TopicOffset: 0x%08lx SourceFileName: %s\n");
}
else if(strcmp(dumpfile,"|TopicId")==0)
{
BTreeDump(HelpFile,"TopicOffset: 0x%08lx ContextId: %s\n");
}
else if(strcmp(dumpfile,"|Rose")==0)
{
BTreeDump(HelpFile,"KeywordHashValue: 0x%08lx\nMacro: %s\nTitle: %s\n");
}
else if(strcmp(dumpfile,"|TTLBTREE")==0)
{
BTreeDump(HelpFile,"TopicOffset: 0x%08lx TopicTitle: %s\n");
}
else if(strcmp(dumpfile,"|FONT")==0)
{
FontDump(HelpFile);
}
else /* generic */
{
topic=ftell(HelpFile);
if(my_getw(HelpFile)==0x293B) /* if it's a B+ tree */
{
my_getw(HelpFile);
my_getw(HelpFile);
filename[0]='\0';
while((d=getc(HelpFile))>0) /* format according to Structure */
{
switch(d)
{
case 'L':
case '4':
case 'a':
strcat(filename,"0x%08lx ");
break;
case '2':
strcat(filename,"%5u ");
break;
case 'F':
case 'i':
case 'z':
strcat(filename,"'%s' ");
break;
case '!':
strcat(filename,"%!");
break;
default:
error("Unknown Btree field type '%c'",d);
}
}
strcat(filename,"\n");
fseek(HelpFile,topic,SEEK_SET);
BTreeDump(HelpFile,filename);
}
else
{
fseek(HelpFile,topic,SEEK_SET);
HexDump(HelpFile,FileLength,offset);
}
}
}
return TRUE;
}
int main(int argc,char *argv[])
{
char AnnoFileName[NAME_MAX];
char drive[_MAX_DRIVE];
char dir[PATH_MAX];
FILE *f;
int mode;
BOOL annotate;
char *filename;
char *dumpfile;
char *exportname;
long offset;
int i,j;
/* initialize hash value coding oldtable */
memset(oldtable,0,sizeof(oldtable));
for(i=0;i<9;i++) oldtable['1'+i]=i+1;
oldtable['0']=10;
oldtable['.']=12;
oldtable['_']=13;
for(i=0;i<26;i++) oldtable['A'+i]=oldtable['a'+i]=17+i;
exportname=dumpfile=filename=NULL;
AnnoFileName[0]='\0';
mode=0;
offset=0;
annotate=FALSE;
/* scan arguments */
for(i=1;i<argc;i++)
{
if(argv[i][0]=='/'||argv[i][0]=='-')
{
switch(tolower((unsigned char)argv[i][1]))
{
case 'a':
if(argv[i][2])
{
strlcpy(AnnoFileName,argv[i]+2,sizeof(AnnoFileName));
}
else if(argv[i+1]&&argv[i+1][0]!='/'&&argv[i+1][0]!='-')
{
strlcpy(AnnoFileName,argv[i+1],sizeof(AnnoFileName));
i++;
}
annotate=TRUE;
break;
case 'b':
resolvebrowse=FALSE;
break;
case 'c':
mode=4;
break;
case 'd':
mode=2;
break;
case 'e':
mode=7;
break;
case 'f':
listtopic=TRUE;
break;
case 'g':
guessing=FALSE;
break;
case 'h': // add entry to prefix table
for(j=0;j<sizeof(prefix)/sizeof(prefix[0])&&prefix[j];j++) ;
if(j<sizeof(prefix)/sizeof(prefix[0]))
{
if(argv[i][2])
{
prefix[j]=argv[i]+2;
}
else if(argv[i+1]&&argv[i+1][0]!='/'&&argv[i+1][0]!='-')
{
prefix[j]=argv[i+1];
i++;
}
}
else
{
fprintf(stderr,"Prefix table full.\n");
}
break;
case 'i':
reportderived=TRUE;
break;
case 'l':
mode=5;
break;
case 'm':
extractmacros=FALSE;
break;
case 'n':
nopagebreak=TRUE;
break;
case 'p':
mode=6;
break;
case 'r':
mode=3;
break;
case 's':
if(argv[i][2])
{
TopicsPerRTF=atoi(argv[i]+2);
}
else if(argv[i+1]&&argv[i+1][0]!='/'&&argv[i+1][0]!='-')
{
TopicsPerRTF=atoi(argv[i+1]);
i++;
}
break;
case 't':
if(argv[i][2])
{
offset=strtoul(argv[i]+2,NULL,0);
}
else if(argv[i+1]&&argv[i+1][0]!='/'&&argv[i+1][0]!='-')
{
offset=strtoul(argv[i+1],NULL,0);
i++;
}
break;
case 'x':
mode=1;
break;
case 'y':
overwrite=TRUE;
break;
case 'z':
exportLZ77=TRUE;
break;
default:
fprintf(stderr,"unknown option '%s' ignored\n",argv[i]);
}
}
else if(exportname)
{
fprintf(stderr,"additional parameter '%s' ignored\n",argv[i]);
}
else if(dumpfile)
{
exportname=argv[i];
}
else if(filename)
{
dumpfile=argv[i];
}
else
{
filename=argv[i];
}
}
if(filename)
{
_splitpath(filename,drive,dir,name,ext);
if(ext[0]=='\0') strcpy(ext,".hlp");
mvp=ext[1]=='M';
_makepath(HelpFileName,drive,dir,name,ext);
f=fopen(HelpFileName,"rb");
if(f)
{
if(annotate)
{
if(AnnoFileName[0]=='\0') _makepath(AnnoFileName,drive,dir,name,".ann");
AnnoFile=fopen(AnnoFileName,"rb");
if(!AnnoFile)
{
fprintf(stderr,"Couldn't find annotation file '%s'\n",AnnoFileName);
}
}
prefixhash[0]=0L;
for(i=1;prefix[i];i++)
{
prefixhash[i]=hash(prefix[i]);
}
if(!HelpDeCompile(f,dumpfile,mode,exportname,offset))
{
fprintf(stderr,"%s isn't a valid WinHelp file !\n",HelpFileName);
}
if(annotate&&AnnoFile) fclose(AnnoFile);
my_fclose(f);
}
else
{
fprintf(stderr,"Can not open '%s'\n",HelpFileName);
}
}
else
{
fprintf(stderr,"HELPDECO - decompile *.HLP/*.MVB files of Windows 3.x / 95 - %d bit Version 2.1.3\n"
"M.Winterhoff <mawin@gmx.net>, Geschw.-Scholl-Ring 17, 38444 Wolfsburg, Germany\n"
"\n"
"usage: HELPDECO helpfile[.hlp] [/y] - decompile helpfile into all sources\n"
" HELPDECO helpfile[.hlp] [/y] /a[annfile.ANN] - and add annotations\n"
" HELPDECO helpfile[.hlp] /r [/y] [/n] - decompile into lookalike RTF\n"
" HELPDECO helpfile[.hlp] /c [/y] - generate Win95 .CNT content file\n"
" HELPDECO helpfile[.hlp] /l - list entry points of this helpfile\n"
" HELPDECO helpfile[.hlp] /e [/f] - list references to other helpfiles\n"
" HELPDECO helpfile[.hlp] /p [/f] - check references to other helpfiles\n"
" HELPDECO helpfile[.hlp] /d [/x] - display internal directory\n"
" HELPDECO helpfile[.hlp] \"internalfile\" [/x] - display internal file\n"
" HELPDECO helpfile[.hlp] \"internalfile\" filename - export internal file\n"
"options: /y overwrite without warning, /f list referencing topics, /x hex dump\n"
" /g no guessing, /hprefix add known contextid prefix, /n no page breaks\n"
"To recreate all source files neccessary to rebuild a Windows helpfile, create\n"
"a directory, change to this directory and call HELPDECO with the path and name\n"
"of the helpfile to dissect. HELPDECO will extract all files contained in the\n"
"helpfile in two passes and deposit them in the current directory. You may then\n"
"rebuild the helpfile using the appropriate help compiler HC30, HC31, HCP, HCW,\n"
"HCRTF, MVC, WMVC or MVCC. The file will not be identical, but should look and\n"
"work like the original.\n"
#ifndef _WIN32
#endif
,sizeof(int)*8);
}
return 0;
}
|