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 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898
|
// NsisIn.cpp
#include "StdAfx.h"
#include "../../../Common/IntToString.h"
#include "../../../Common/StringToInt.h"
#include "../../Common/LimitedStreams.h"
#include "../../Common/StreamUtils.h"
#include "NsisIn.h"
#define Get16(p) GetUi16(p)
#define Get32(p) GetUi32(p)
// #define NUM_SPEED_TESTS 1000
namespace NArchive {
namespace NNsis {
static const size_t kInputBufSize = 1 << 20;
const Byte kSignature[kSignatureSize] = NSIS_SIGNATURE;
static const UInt32 kMask_IsCompressed = (UInt32)1 << 31;
static const unsigned kNumCommandParams = 6;
static const unsigned kCmdSize = 4 + kNumCommandParams * 4;
#ifdef NSIS_SCRIPT
#define CR_LF "\x0D\x0A"
#endif
static const char * const kErrorStr = "$_ERROR_STR_";
#define RINOZ(x) { int __tt = (x); if (__tt != 0) return __tt; }
/* There are several versions of NSIS:
1) Original NSIS:
NSIS-2 ANSI
NSIS-3 ANSI
NSIS-3 Unicode
2) NSIS from Jim Park that extends old NSIS-2 to Unicode support:
NSIS-Park-(1,2,3) ANSI
NSIS-Park-(1,2,3) Unicode
The command IDs layout is slightly different for different versions.
Also there are additional "log" versions of NSIS that support EW_LOG.
We use the layout of "NSIS-3 Unicode" without "log" as main layout.
And we transfer the command IDs to main layout, if another layout is detected. */
enum
{
EW_INVALID_OPCODE,
EW_RET, // Return
EW_NOP, // Nop, Goto
EW_ABORT, // Abort
EW_QUIT, // Quit
EW_CALL, // Call, InitPluginsDir
EW_UPDATETEXT, // DetailPrint
EW_SLEEP, // Sleep
EW_BRINGTOFRONT, // BringToFront
EW_CHDETAILSVIEW, // SetDetailsView
EW_SETFILEATTRIBUTES, // SetFileAttributes
EW_CREATEDIR, // CreateDirectory, SetOutPath
EW_IFFILEEXISTS, // IfFileExists
EW_SETFLAG, // SetRebootFlag, ...
EW_IFFLAG, // IfAbort, IfSilent, IfErrors, IfRebootFlag
EW_GETFLAG, // GetInstDirError, GetErrorLevel
EW_RENAME, // Rename
EW_GETFULLPATHNAME, // GetFullPathName
EW_SEARCHPATH, // SearchPath
EW_GETTEMPFILENAME, // GetTempFileName
EW_EXTRACTFILE, // File
EW_DELETEFILE, // Delete
EW_MESSAGEBOX, // MessageBox
EW_RMDIR, // RMDir
EW_STRLEN, // StrLen
EW_ASSIGNVAR, // StrCpy
EW_STRCMP, // StrCmp
EW_READENVSTR, // ReadEnvStr, ExpandEnvStrings
EW_INTCMP, // IntCmp, IntCmpU
EW_INTOP, // IntOp
EW_INTFMT, // IntFmt
EW_PUSHPOP, // Push/Pop/Exchange
EW_FINDWINDOW, // FindWindow
EW_SENDMESSAGE, // SendMessage
EW_ISWINDOW, // IsWindow
EW_GETDLGITEM, // GetDlgItem
EW_SETCTLCOLORS, // SetCtlColors
EW_SETBRANDINGIMAGE, // SetBrandingImage
EW_CREATEFONT, // CreateFont
EW_SHOWWINDOW, // ShowWindow, EnableWindow, HideWindow
EW_SHELLEXEC, // ExecShell
EW_EXECUTE, // Exec, ExecWait
EW_GETFILETIME, // GetFileTime
EW_GETDLLVERSION, // GetDLLVersion
// EW_GETFONTVERSION, // Park : 2.46.2
// EW_GETFONTNAME, // Park : 2.46.3
EW_REGISTERDLL, // RegDLL, UnRegDLL, CallInstDLL
EW_CREATESHORTCUT, // CreateShortCut
EW_COPYFILES, // CopyFiles
EW_REBOOT, // Reboot
EW_WRITEINI, // WriteINIStr, DeleteINISec, DeleteINIStr, FlushINI
EW_READINISTR, // ReadINIStr
EW_DELREG, // DeleteRegValue, DeleteRegKey
EW_WRITEREG, // WriteRegStr, WriteRegExpandStr, WriteRegBin, WriteRegDWORD
EW_READREGSTR, // ReadRegStr, ReadRegDWORD
EW_REGENUM, // EnumRegKey, EnumRegValue
EW_FCLOSE, // FileClose
EW_FOPEN, // FileOpen
EW_FPUTS, // FileWrite, FileWriteByte
EW_FGETS, // FileRead, FileReadByte
// Park
// EW_FPUTWS, // FileWriteUTF16LE, FileWriteWord
// EW_FGETWS, // FileReadUTF16LE, FileReadWord
EW_FSEEK, // FileSeek
EW_FINDCLOSE, // FindClose
EW_FINDNEXT, // FindNext
EW_FINDFIRST, // FindFirst
EW_WRITEUNINSTALLER, // WriteUninstaller
// Park : since 2.46.3 the log is enabled in main Park version
// EW_LOG, // LogSet, LogText
EW_SECTIONSET, // Get*, Set*
EW_INSTTYPESET, // InstTypeSetText, InstTypeGetText, SetCurInstType, GetCurInstType
// instructions not actually implemented in exehead, but used in compiler.
EW_GETLABELADDR, // both of these get converted to EW_ASSIGNVAR
EW_GETFUNCTIONADDR,
EW_LOCKWINDOW, // LockWindow
// 2 unicode commands available only in Unicode archive
EW_FPUTWS, // FileWriteUTF16LE, FileWriteWord
EW_FGETWS, // FileReadUTF16LE, FileReadWord
// The following IDs are not IDs in real order.
// We just need some IDs to translate eny extended layout to main layout.
EW_LOG, // LogSet, LogText
// Park
EW_FINDPROC, // FindProc
EW_GETFONTVERSION, // GetFontVersion
EW_GETFONTNAME, // GetFontName
kNumCmds
};
static const unsigned kNumAdditionalParkCmds = 3;
struct CCommandInfo
{
Byte NumParams;
};
static const CCommandInfo k_Commands[kNumCmds] =
{
{ 0 }, // "Invalid" },
{ 0 }, // Return
{ 1 }, // Nop, Goto
{ 1 }, // "Abort" },
{ 0 }, // "Quit" },
{ 2 }, // Call
{ 6 }, // "DetailPrint" }, // 1 param in new versions, 6 in old NSIS versions
{ 1 }, // "Sleep" },
{ 0 }, // "BringToFront" },
{ 2 }, // "SetDetailsView" },
{ 2 }, // "SetFileAttributes" },
{ 2 }, // CreateDirectory, SetOutPath
{ 3 }, // "IfFileExists" },
{ 3 }, // SetRebootFlag, ...
{ 4 }, // "If" }, // IfAbort, IfSilent, IfErrors, IfRebootFlag
{ 2 }, // "Get" }, // GetInstDirError, GetErrorLevel
{ 4 }, // "Rename" },
{ 3 }, // "GetFullPathName" },
{ 2 }, // "SearchPath" },
{ 2 }, // "GetTempFileName" },
{ 6 }, // "File"
{ 2 }, // "Delete" },
{ 6 }, // "MessageBox" },
{ 2 }, // "RMDir" },
{ 2 }, // "StrLen" },
{ 4 }, // StrCpy, GetCurrentAddress
{ 5 }, // "StrCmp" },
{ 3 }, // ReadEnvStr, ExpandEnvStrings
{ 6 }, // "IntCmp" },
{ 4 }, // "IntOp" },
{ 3 }, // "IntFmt" },
{ 6 }, // Push, Pop, Exch // it must be 3 params. But some multi-command write garbage.
{ 5 }, // "FindWindow" },
{ 6 }, // "SendMessage" },
{ 3 }, // "IsWindow" },
{ 3 }, // "GetDlgItem" },
{ 2 }, // "SetCtlColors" },
{ 3 }, // "SetBrandingImage" },
{ 5 }, // "CreateFont" },
{ 4 }, // ShowWindow, EnableWindow, HideWindow
{ 6 }, // "ExecShell" },
{ 3 }, // "Exec" }, // Exec, ExecWait
{ 3 }, // "GetFileTime" },
{ 3 }, // "GetDLLVersion" },
{ 6 }, // RegDLL, UnRegDLL, CallInstDLL // it must be 5 params. But some multi-command write garbage.
{ 6 }, // "CreateShortCut" },
{ 4 }, // "CopyFiles" },
{ 1 }, // "Reboot" },
{ 5 }, // WriteINIStr, DeleteINISec, DeleteINIStr, FlushINI
{ 4 }, // "ReadINIStr" },
{ 5 }, // "DeleteReg" }, // DeleteRegKey, DeleteRegValue
{ 6 }, // "WriteReg" }, // WriteRegStr, WriteRegExpandStr, WriteRegBin, WriteRegDWORD
{ 5 }, // "ReadReg" }, // ReadRegStr, ReadRegDWORD
{ 5 }, // "EnumReg" }, // EnumRegKey, EnumRegValue
{ 1 }, // "FileClose" },
{ 4 }, // "FileOpen" },
{ 3 }, // "FileWrite" }, // FileWrite, FileWriteByte
{ 4 }, // "FileRead" }, // FileRead, FileReadByte
{ 4 }, // "FileSeek" },
{ 1 }, // "FindClose" },
{ 2 }, // "FindNext" },
{ 3 }, // "FindFirst" },
{ 4 }, // "WriteUninstaller" },
{ 5 }, // "Section" }, // ***
{ 4 }, // InstTypeSetText, InstTypeGetText, SetCurInstType, GetCurInstType
{ 6 }, // "GetLabelAddr" },
{ 2 }, // "GetFunctionAddress" },
{ 1 }, // "LockWindow" },
{ 3 }, // "FileWrite" }, // FileWriteUTF16LE, FileWriteWord
{ 4 }, // "FileRead" }, // FileReadUTF16LE, FileReadWord
{ 2 }, // "Log" }, // LogSet, LogText
// Park
{ 2 }, // "FindProc" },
{ 2 }, // "GetFontVersion" },
{ 2 }, // "GetFontName" }
};
#ifdef NSIS_SCRIPT
static const char * const k_CommandNames[kNumCmds] =
{
"Invalid"
, NULL // Return
, NULL // Nop, Goto
, "Abort"
, "Quit"
, NULL // Call
, "DetailPrint" // 1 param in new versions, 6 in old NSIS versions
, "Sleep"
, "BringToFront"
, "SetDetailsView"
, "SetFileAttributes"
, NULL // CreateDirectory, SetOutPath
, "IfFileExists"
, NULL // SetRebootFlag, ...
, "If" // IfAbort, IfSilent, IfErrors, IfRebootFlag
, "Get" // GetInstDirError, GetErrorLevel
, "Rename"
, "GetFullPathName"
, "SearchPath"
, "GetTempFileName"
, NULL // File
, "Delete"
, "MessageBox"
, "RMDir"
, "StrLen"
, NULL // StrCpy, GetCurrentAddress
, "StrCmp"
, NULL // ReadEnvStr, ExpandEnvStrings
, "IntCmp"
, "IntOp"
, "IntFmt"
, NULL // Push, Pop, Exch // it must be 3 params. But some multi-command write garbage.
, "FindWindow"
, "SendMessage"
, "IsWindow"
, "GetDlgItem"
, "SetCtlColors"
, "SetBrandingImage"
, "CreateFont"
, NULL // ShowWindow, EnableWindow, HideWindow
, "ExecShell"
, "Exec" // Exec, ExecWait
, "GetFileTime"
, "GetDLLVersion"
, NULL // RegDLL, UnRegDLL, CallInstDLL // it must be 5 params. But some multi-command write garbage.
, "CreateShortCut"
, "CopyFiles"
, "Reboot"
, NULL // WriteINIStr, DeleteINISec, DeleteINIStr, FlushINI
, "ReadINIStr"
, "DeleteReg" // DeleteRegKey, DeleteRegValue
, "WriteReg" // WriteRegStr, WriteRegExpandStr, WriteRegBin, WriteRegDWORD
, "ReadReg" // ReadRegStr, ReadRegDWORD
, "EnumReg" // EnumRegKey, EnumRegValue
, "FileClose"
, "FileOpen"
, "FileWrite" // FileWrite, FileWriteByte
, "FileRead" // FileRead, FileReadByte
, "FileSeek"
, "FindClose"
, "FindNext"
, "FindFirst"
, "WriteUninstaller"
, "Section" // ***
, NULL // InstTypeSetText, InstTypeGetText, SetCurInstType, GetCurInstType
, "GetLabelAddr"
, "GetFunctionAddress"
, "LockWindow"
, "FileWrite" // FileWriteUTF16LE, FileWriteWord
, "FileRead" // FileReadUTF16LE, FileReadWord
, "Log" // LogSet, LogText
// Park
, "FindProc"
, "GetFontVersion"
, "GetFontName"
};
#endif
/* NSIS can use one name for two CSIDL_*** and CSIDL_COMMON_*** items (CurrentUser / AllUsers)
Some NSIS shell names are not identical to WIN32 CSIDL_* names.
NSIS doesn't use some CSIDL_* values. But we add name for all CSIDL_ (marked with '+'). */
static const char * const kShellStrings[] =
{
"DESKTOP" // +
, "INTERNET" // +
, "SMPROGRAMS" // CSIDL_PROGRAMS
, "CONTROLS" // +
, "PRINTERS" // +
, "DOCUMENTS" // CSIDL_PERSONAL
, "FAVORITES" // CSIDL_FAVORITES
, "SMSTARTUP" // CSIDL_STARTUP
, "RECENT" // CSIDL_RECENT
, "SENDTO" // CSIDL_SENDTO
, "BITBUCKET" // +
, "STARTMENU"
, NULL // CSIDL_MYDOCUMENTS = CSIDL_PERSONAL
, "MUSIC" // CSIDL_MYMUSIC
, "VIDEOS" // CSIDL_MYVIDEO
, NULL
, "DESKTOP" // CSIDL_DESKTOPDIRECTORY
, "DRIVES" // +
, "NETWORK" // +
, "NETHOOD"
, "FONTS"
, "TEMPLATES"
, "STARTMENU" // CSIDL_COMMON_STARTMENU
, "SMPROGRAMS" // CSIDL_COMMON_PROGRAMS
, "SMSTARTUP" // CSIDL_COMMON_STARTUP
, "DESKTOP" // CSIDL_COMMON_DESKTOPDIRECTORY
, "APPDATA" // CSIDL_APPDATA !!! "QUICKLAUNCH"
, "PRINTHOOD"
, "LOCALAPPDATA"
, "ALTSTARTUP"
, "ALTSTARTUP" // CSIDL_COMMON_ALTSTARTUP
, "FAVORITES" // CSIDL_COMMON_FAVORITES
, "INTERNET_CACHE"
, "COOKIES"
, "HISTORY"
, "APPDATA" // CSIDL_COMMON_APPDATA
, "WINDIR"
, "SYSDIR"
, "PROGRAM_FILES" // +
, "PICTURES" // CSIDL_MYPICTURES
, "PROFILE"
, "SYSTEMX86" // +
, "PROGRAM_FILESX86" // +
, "PROGRAM_FILES_COMMON" // +
, "PROGRAM_FILES_COMMONX8" // + CSIDL_PROGRAM_FILES_COMMONX86
, "TEMPLATES" // CSIDL_COMMON_TEMPLATES
, "DOCUMENTS" // CSIDL_COMMON_DOCUMENTS
, "ADMINTOOLS" // CSIDL_COMMON_ADMINTOOLS
, "ADMINTOOLS" // CSIDL_ADMINTOOLS
, "CONNECTIONS" // +
, NULL
, NULL
, NULL
, "MUSIC" // CSIDL_COMMON_MUSIC
, "PICTURES" // CSIDL_COMMON_PICTURES
, "VIDEOS" // CSIDL_COMMON_VIDEO
, "RESOURCES"
, "RESOURCES_LOCALIZED"
, "COMMON_OEM_LINKS" // +
, "CDBURN_AREA"
, NULL // unused
, "COMPUTERSNEARME" // +
};
static void UIntToString(AString &s, UInt32 v)
{
char sz[16];
ConvertUInt32ToString(v, sz);
s += sz;
}
#ifdef NSIS_SCRIPT
void CInArchive::Add_UInt(UInt32 v)
{
char sz[16];
ConvertUInt32ToString(v, sz);
Script += sz;
}
static void Add_SignedInt(CDynLimBuf &s, Int32 v)
{
char sz[32];
ConvertInt64ToString(v, sz);
s += sz;
}
static void Add_Hex(CDynLimBuf &s, UInt32 v)
{
char sz[16];
sz[0] = '0';
sz[1] = 'x';
ConvertUInt32ToHex(v, sz + 2);
s += sz;
}
static UInt32 GetUi16Str_Len(const Byte *p)
{
const Byte *pp = p;
for (; *pp != 0 || *(pp + 1) != 0; pp += 2);
return (UInt32)((pp - p) >> 1);
}
void CInArchive::AddLicense(UInt32 param, Int32 langID)
{
Space();
if (param >= NumStringChars ||
param + 1 >= NumStringChars)
{
Script += kErrorStr;
return;
}
strUsed[param] = 1;
UInt32 start = _stringsPos + (IsUnicode ? param * 2 : param);
UInt32 offset = start + (IsUnicode ? 2 : 1);
{
FOR_VECTOR (i, LicenseFiles)
{
const CLicenseFile &lic = LicenseFiles[i];
if (offset == lic.Offset)
{
Script += lic.Name;
return;
}
}
}
AString fileName = "[LICENSE]";
if (langID >= 0)
{
fileName += "\\license-";
// LangId_To_String(fileName, langID);
UIntToString(fileName, langID);
}
else if (++_numRootLicenses > 1)
{
fileName += '-';
UIntToString(fileName, _numRootLicenses);
}
const Byte *sz = (_data + start);
unsigned marker = IsUnicode ? Get16(sz) : *sz;
bool isRTF = (marker == 2);
fileName += isRTF ? ".rtf" : ".txt"; // if (*sz == 1) it's text;
Script += fileName;
CLicenseFile &lic = LicenseFiles.AddNew();
lic.Name = fileName;
lic.Offset = offset;
if (!IsUnicode)
lic.Size = (UInt32)strlen((const char *)sz + 1);
else
{
sz += 2;
UInt32 len = GetUi16Str_Len(sz);
lic.Size = len * 2;
if (isRTF)
{
lic.Text.Alloc((size_t)len);
for (UInt32 i = 0; i < len; i++, sz += 2)
{
unsigned c = Get16(sz);
if (c >= 256)
c = '?';
lic.Text[i] = (Byte)(c);
}
lic.Size = len;
lic.Offset = 0;
}
}
}
#endif
#define kVar_CMDLINE 20
#define kVar_INSTDIR 21
#define kVar_OUTDIR 22
#define kVar_EXEDIR 23
#define kVar_LANGUAGE 24
#define kVar_TEMP 25
#define kVar_PLUGINSDIR 26
#define kVar_EXEPATH 27 // NSIS 2.26+
#define kVar_EXEFILE 28 // NSIS 2.26+
#define kVar_HWNDPARENT_225 27
#define kVar_HWNDPARENT 29
// #define kVar__CLICK 30
#define kVar_Spec_OUTDIR_225 29 // NSIS 2.04 - 2.25
#define kVar_Spec_OUTDIR 31 // NSIS 2.26+
static const char * const kVarStrings[] =
{
"CMDLINE"
, "INSTDIR"
, "OUTDIR"
, "EXEDIR"
, "LANGUAGE"
, "TEMP"
, "PLUGINSDIR"
, "EXEPATH" // NSIS 2.26+
, "EXEFILE" // NSIS 2.26+
, "HWNDPARENT"
, "_CLICK" // is set from page->clicknext
, "_OUTDIR" // NSIS 2.04+
};
static const unsigned kNumInternalVars = 20 + ARRAY_SIZE(kVarStrings);
#define GET_NUM_INTERNAL_VARS (IsNsis200 ? kNumInternalVars - 3 : IsNsis225 ? kNumInternalVars - 2 : kNumInternalVars);
void CInArchive::GetVar2(AString &res, UInt32 index)
{
if (index < 20)
{
if (index >= 10)
{
res += 'R';
index -= 10;
}
UIntToString(res, index);
}
else
{
unsigned numInternalVars = GET_NUM_INTERNAL_VARS;
if (index < numInternalVars)
{
if (IsNsis225 && index >= kVar_EXEPATH)
index += 2;
res += kVarStrings[index - 20];
}
else
{
res += '_';
UIntToString(res, index - numInternalVars);
res += '_';
}
}
}
void CInArchive::GetVar(AString &res, UInt32 index)
{
res += '$';
GetVar2(res, index);
}
#ifdef NSIS_SCRIPT
void CInArchive::Add_Var(UInt32 index)
{
_tempString_for_GetVar.Empty();
GetVar(_tempString_for_GetVar, index);
Script += _tempString_for_GetVar;
}
void CInArchive::AddParam_Var(UInt32 index)
{
Space();
Add_Var(index);
}
void CInArchive::AddParam_UInt(UInt32 value)
{
Space();
Add_UInt(value);
}
#endif
#define NS_CODE_SKIP 252
#define NS_CODE_VAR 253
#define NS_CODE_SHELL 254
#define NS_CODE_LANG 255
#define NS_3_CODE_LANG 1
#define NS_3_CODE_SHELL 2
#define NS_3_CODE_VAR 3
#define NS_3_CODE_SKIP 4
#define PARK_CODE_SKIP 0xE000
#define PARK_CODE_VAR 0xE001
#define PARK_CODE_SHELL 0xE002
#define PARK_CODE_LANG 0xE003
#define IS_NS_SPEC_CHAR(c) ((c) >= NS_CODE_SKIP)
#define IS_PARK_SPEC_CHAR(c) ((c) >= PARK_CODE_SKIP && (c) <= PARK_CODE_LANG)
#define DECODE_NUMBER_FROM_2_CHARS(c0, c1) (((c0) & 0x7F) | (((unsigned)((c1) & 0x7F)) << 7))
#define CONVERT_NUMBER_NS_3_UNICODE(n) n = ((n & 0x7F) | (((n >> 8) & 0x7F) << 7))
#define CONVERT_NUMBER_PARK(n) n &= 0x7FFF
static bool AreStringsEqual_16and8(const Byte *p16, const char *p8)
{
for (;;)
{
unsigned c16 = Get16(p16); p16 += 2;
unsigned c = (Byte)(*p8++);
if (c16 != c)
return false;
if (c == 0)
return true;
}
}
void CInArchive::GetShellString(AString &s, unsigned index1, unsigned index2)
{
// zeros are not allowed here.
// if (index1 == 0 || index2 == 0) throw 333;
if ((index1 & 0x80) != 0)
{
unsigned offset = (index1 & 0x3F);
/* NSIS reads registry string:
keyName = HKLM Software\\Microsoft\\Windows\\CurrentVersion
mask = KEY_WOW64_64KEY, If 64-bit flag in index1 is set
valueName = string(offset)
If registry reading is failed, NSIS uses second parameter (index2)
to read string. The recursion is possible in that case in NSIS.
We don't parse index2 string. We only set strUsed status for that
string (but without recursion). */
if (offset >= NumStringChars)
{
s += kErrorStr;
return;
}
#ifdef NSIS_SCRIPT
strUsed[offset] = 1;
if (index2 < NumStringChars)
strUsed[index2] = 1;
#endif
const Byte *p = (const Byte *)(_data + _stringsPos);
int id = -1;
if (IsUnicode)
{
p += offset * 2;
if (AreStringsEqual_16and8(p, "ProgramFilesDir"))
id = 0;
else if (AreStringsEqual_16and8(p, "CommonFilesDir"))
id = 1;
}
else
{
p += offset;
if (strcmp((const char *)p, "ProgramFilesDir") == 0)
id = 0;
else if (strcmp((const char *)p, "CommonFilesDir") == 0)
id = 1;
}
s += ((id >= 0) ? (id == 0 ? "$PROGRAMFILES" : "$COMMONFILES") :
"$_ERROR_UNSUPPORTED_VALUE_REGISTRY_");
// s += ((index1 & 0x40) != 0) ? "64" : "32";
if ((index1 & 0x40) != 0)
s += "64";
if (id < 0)
{
s += '(';
if (IsUnicode)
{
for (unsigned i = 0; i < 256; i++)
{
wchar_t c = Get16(p + i * 2);
if (c == 0)
break;
if (c < 0x80)
s += (char)c;
}
}
else
s += (const char *)p;
s += ')';
}
return;
}
s += '$';
if (index1 < ARRAY_SIZE(kShellStrings))
{
const char *sz = kShellStrings[index1];
if (sz)
{
s += sz;
return;
}
}
if (index2 < ARRAY_SIZE(kShellStrings))
{
const char *sz = kShellStrings[index2];
if (sz)
{
s += sz;
return;
}
}
s += "_ERROR_UNSUPPORTED_SHELL_";
s += '[';
UIntToString(s, index1);
s += ',';
UIntToString(s, index2);
s += ']';
}
#ifdef NSIS_SCRIPT
void CInArchive::Add_LangStr_Simple(UInt32 id)
{
Script += "LSTR_";
Add_UInt(id);
}
#endif
void CInArchive::Add_LangStr(AString &res, UInt32 id)
{
#ifdef NSIS_SCRIPT
langStrIDs.Add(id);
#endif
res += "$(LSTR_";
UIntToString(res, id);
res += ')';
}
void CInArchive::GetNsisString_Raw(const Byte *s)
{
Raw_AString.Empty();
if (NsisType != k_NsisType_Nsis3)
{
for (;;)
{
Byte c = *s++;
if (c == 0)
return;
if (IS_NS_SPEC_CHAR(c))
{
Byte c0 = *s++;
if (c0 == 0)
return;
if (c != NS_CODE_SKIP)
{
Byte c1 = *s++;
if (c1 == 0)
return;
if (c == NS_CODE_SHELL)
GetShellString(Raw_AString, c0, c1);
else
{
unsigned n = DECODE_NUMBER_FROM_2_CHARS(c0, c1);
if (c == NS_CODE_VAR)
GetVar(Raw_AString, n);
else // if (c == NS_CODE_LANG)
Add_LangStr(Raw_AString, n);
}
continue;
}
c = c0;
}
Raw_AString += (char)c;
}
}
// NSIS-3 ANSI
for (;;)
{
Byte c = *s++;
if (c <= NS_3_CODE_SKIP)
{
if (c == 0)
return;
Byte c0 = *s++;
if (c0 == 0)
return;
if (c != NS_3_CODE_SKIP)
{
Byte c1 = *s++;
if (c1 == 0)
return;
if (c == NS_3_CODE_SHELL)
GetShellString(Raw_AString, c0, c1);
else
{
unsigned n = DECODE_NUMBER_FROM_2_CHARS(c0, c1);
if (c == NS_3_CODE_VAR)
GetVar(Raw_AString, n);
else // if (c == NS_3_CODE_LANG)
Add_LangStr(Raw_AString, n);
}
continue;
}
c = c0;
}
Raw_AString += (char)c;
}
}
#ifdef NSIS_SCRIPT
void CInArchive::GetNsisString(AString &res, const Byte *s)
{
for (;;)
{
Byte c = *s++;
if (c == 0)
return;
if (NsisType != k_NsisType_Nsis3)
{
if (IS_NS_SPEC_CHAR(c))
{
Byte c0 = *s++;
if (c0 == 0)
return;
if (c != NS_CODE_SKIP)
{
Byte c1 = *s++;
if (c1 == 0)
return;
if (c == NS_CODE_SHELL)
GetShellString(res, c0, c1);
else
{
unsigned n = DECODE_NUMBER_FROM_2_CHARS(c0, c1);
if (c == NS_CODE_VAR)
GetVar(res, n);
else // if (c == NS_CODE_LANG)
Add_LangStr(res, n);
}
continue;
}
c = c0;
}
}
else
{
// NSIS-3 ANSI
if (c <= NS_3_CODE_SKIP)
{
Byte c0 = *s++;
if (c0 == 0)
return;
if (c0 == 0)
break;
if (c != NS_3_CODE_SKIP)
{
Byte c1 = *s++;
if (c1 == 0)
return;
if (c == NS_3_CODE_SHELL)
GetShellString(res, c0, c1);
else
{
unsigned n = DECODE_NUMBER_FROM_2_CHARS(c0, c1);
if (c == NS_3_CODE_VAR)
GetVar(res, n);
else // if (c == NS_3_CODE_LANG)
Add_LangStr(res, n);
}
continue;
}
c = c0;
}
}
{
const char *e;
if (c == 9) e = "$\\t";
else if (c == 10) e = "$\\n";
else if (c == 13) e = "$\\r";
else if (c == '"') e = "$\\\"";
else if (c == '$') e = "$$";
else
{
res += (char)c;
continue;
}
res += e;
continue;
}
}
}
#endif
void CInArchive::GetNsisString_Unicode_Raw(const Byte *p)
{
Raw_UString.Empty();
if (IsPark())
{
for (;;)
{
unsigned c = Get16(p);
p += 2;
if (c == 0)
break;
if (c < 0x80)
{
Raw_UString += (wchar_t)c;
continue;
}
if (IS_PARK_SPEC_CHAR(c))
{
unsigned n = Get16(p);
p += 2;
if (n == 0)
break;
if (c != PARK_CODE_SKIP)
{
Raw_AString.Empty();
if (c == PARK_CODE_SHELL)
GetShellString(Raw_AString, n & 0xFF, n >> 8);
else
{
CONVERT_NUMBER_PARK(n);
if (c == PARK_CODE_VAR)
GetVar(Raw_AString, n);
else // if (c == PARK_CODE_LANG)
Add_LangStr(Raw_AString, n);
}
Raw_UString.AddAscii(Raw_AString);
continue;
}
c = n;
}
Raw_UString += (wchar_t)c;
}
return;
}
// NSIS-3 Unicode
for (;;)
{
unsigned c = Get16(p);
p += 2;
if (c > NS_3_CODE_SKIP)
{
Raw_UString += (wchar_t)c;
continue;
}
if (c == 0)
break;
unsigned n = Get16(p);
p += 2;
if (n == 0)
break;
if (c == NS_3_CODE_SKIP)
{
Raw_UString += (wchar_t)n;
continue;
}
Raw_AString.Empty();
if (c == NS_3_CODE_SHELL)
GetShellString(Raw_AString, n & 0xFF, n >> 8);
else
{
CONVERT_NUMBER_NS_3_UNICODE(n);
if (c == NS_3_CODE_VAR)
GetVar(Raw_AString, n);
else // if (c == NS_3_CODE_LANG)
Add_LangStr(Raw_AString, n);
}
Raw_UString.AddAscii(Raw_AString);
}
}
#ifdef NSIS_SCRIPT
static const Byte kUtf8Limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
void CInArchive::GetNsisString_Unicode(AString &res, const Byte *p)
{
for (;;)
{
unsigned c = Get16(p);
p += 2;
if (c == 0)
break;
if (IsPark())
{
if (IS_PARK_SPEC_CHAR(c))
{
unsigned n = Get16(p);
p += 2;
if (n == 0)
break;
if (c != PARK_CODE_SKIP)
{
if (c == PARK_CODE_SHELL)
GetShellString(res, n & 0xFF, n >> 8);
else
{
CONVERT_NUMBER_PARK(n);
if (c == PARK_CODE_VAR)
GetVar(res, n);
else // if (c == PARK_CODE_LANG)
Add_LangStr(res, n);
}
continue;
}
c = n;
}
}
else
{
// NSIS-3 Unicode
if (c <= NS_3_CODE_SKIP)
{
unsigned n = Get16(p);
p += 2;
if (n == 0)
break;
if (c != NS_3_CODE_SKIP)
{
if (c == NS_3_CODE_SHELL)
GetShellString(res, n & 0xFF, n >> 8);
else
{
CONVERT_NUMBER_NS_3_UNICODE(n);
if (c == NS_3_CODE_VAR)
GetVar(res, n);
else // if (c == NS_3_CODE_LANG)
Add_LangStr(res, n);
}
continue;
}
c = n;
}
}
if (c < 0x80)
{
const char *e;
if (c == 9) e = "$\\t";
else if (c == 10) e = "$\\n";
else if (c == 13) e = "$\\r";
else if (c == '"') e = "$\\\"";
else if (c == '$') e = "$$";
else
{
res += (char)c;
continue;
}
res += e;
continue;
}
UInt32 value = c;
/*
if (value >= 0xD800 && value < 0xE000)
{
UInt32 c2;
if (value >= 0xDC00 || srcPos == srcLen)
break;
c2 = src[srcPos++];
if (c2 < 0xDC00 || c2 >= 0xE000)
break;
value = (((value - 0xD800) << 10) | (c2 - 0xDC00)) + 0x10000;
}
*/
unsigned numAdds;
for (numAdds = 1; numAdds < 5; numAdds++)
if (value < (((UInt32)1) << (numAdds * 5 + 6)))
break;
res += (char)(kUtf8Limits[numAdds - 1] + (value >> (6 * numAdds)));
do
{
numAdds--;
res += (char)(0x80 + ((value >> (6 * numAdds)) & 0x3F));
// destPos++;
}
while (numAdds != 0);
// AddToUtf8(res, c);
}
}
#endif
void CInArchive::ReadString2_Raw(UInt32 pos)
{
Raw_AString.Empty();
Raw_UString.Empty();
if ((Int32)pos < 0)
Add_LangStr(Raw_AString, -((Int32)pos + 1));
else if (pos >= NumStringChars)
{
Raw_AString += kErrorStr;
// UIntToString(Raw_AString, pos);
}
else
{
if (IsUnicode)
GetNsisString_Unicode_Raw(_data + _stringsPos + pos * 2);
else
GetNsisString_Raw(_data + _stringsPos + pos);
return;
}
Raw_UString.SetFromAscii(Raw_AString);
}
bool CInArchive::IsGoodString(UInt32 param) const
{
if (param >= NumStringChars)
return false;
if (param == 0)
return true;
const Byte *p = _data + _stringsPos;
unsigned c;
if (IsUnicode)
c = Get16(p + param * 2 - 2);
else
c = p[param - 1];
// some files have '\\' character before string?
return (c == 0 || c == '\\');
}
bool CInArchive::AreTwoParamStringsEqual(UInt32 param1, UInt32 param2) const
{
if (param1 == param2)
return true;
/* NSIS-3.0a1 probably contains bug, so it can use 2 different strings
with same content. So we check real string also.
Also it's possible to check identical postfix parts of strings. */
if (param1 >= NumStringChars ||
param2 >= NumStringChars)
return false;
const Byte *p = _data + _stringsPos;
if (IsUnicode)
{
const Byte *p1 = p + param1 * 2;
const Byte *p2 = p + param2 * 2;
for (;;)
{
UInt16 c = Get16(p1);
if (c != Get16(p2))
return false;
if (c == 0)
return true;
p1 += 2;
p2 += 2;
}
}
else
{
const Byte *p1 = p + param1;
const Byte *p2 = p + param2;
for (;;)
{
Byte c = *p1++;
if (c != *p2++)
return false;
if (c == 0)
return true;
}
}
}
#ifdef NSIS_SCRIPT
UInt32 CInArchive::GetNumUsedVars() const
{
UInt32 numUsedVars = 0;
const Byte *data = (const Byte *)_data + _stringsPos;
unsigned npi = 0;
for (UInt32 i = 0; i < NumStringChars;)
{
bool process = true;
if (npi < noParseStringIndexes.Size() && noParseStringIndexes[npi] == i)
{
process = false;
npi++;
}
if (IsUnicode)
{
if (IsPark())
{
for (;;)
{
unsigned c = Get16(data + i * 2);
i++;
if (c == 0)
break;
if (IS_PARK_SPEC_CHAR(c))
{
UInt32 n = Get16(data + i * 2);
i++;
if (n == 0)
break;
if (process && c == PARK_CODE_VAR)
{
CONVERT_NUMBER_PARK(n);
n++;
if (numUsedVars < n)
numUsedVars = n;
}
}
}
}
else // NSIS-3 Unicode
{
for (;;)
{
unsigned c = Get16(data + i * 2);
i++;
if (c == 0)
break;
if (c > NS_3_CODE_SKIP)
continue;
UInt32 n = Get16(data + i * 2);
i++;
if (n == 0)
break;
if (process && c == NS_3_CODE_VAR)
{
CONVERT_NUMBER_NS_3_UNICODE(n);
n++;
if (numUsedVars < n)
numUsedVars = n;
}
}
}
}
else // not Unicode (ANSI)
{
if (NsisType != k_NsisType_Nsis3)
{
for (;;)
{
Byte c = data[i++];
if (c == 0)
break;
if (IS_NS_SPEC_CHAR(c))
{
Byte c0 = data[i++];
if (c0 == 0)
break;
if (c == NS_CODE_SKIP)
continue;
Byte c1 = data[i++];
if (c1 == 0)
break;
if (process && c == NS_CODE_VAR)
{
UInt32 n = DECODE_NUMBER_FROM_2_CHARS(c0, c1);
n++;
if (numUsedVars < n)
numUsedVars = n;
}
}
}
}
else
{
// NSIS-3 ANSI
for (;;)
{
Byte c = data[i++];
if (c == 0)
break;
if (c > NS_3_CODE_SKIP)
continue;
Byte c0 = data[i++];
if (c0 == 0)
break;
if (c == NS_3_CODE_SKIP)
continue;
Byte c1 = data[i++];
if (c1 == 0)
break;
if (process && c == NS_3_CODE_VAR)
{
UInt32 n = DECODE_NUMBER_FROM_2_CHARS(c0, c1);
n++;
if (numUsedVars < n)
numUsedVars = n;
}
}
}
}
}
return numUsedVars;
}
void CInArchive::ReadString2(AString &s, UInt32 pos)
{
if ((Int32)pos < 0)
{
Add_LangStr(s, -((Int32)pos + 1));
return;
}
if (pos >= NumStringChars)
{
s += kErrorStr;
// UIntToString(s, pos);
return;
}
#ifdef NSIS_SCRIPT
strUsed[pos] = 1;
#endif
if (IsUnicode)
GetNsisString_Unicode(s, _data + _stringsPos + pos * 2);
else
GetNsisString(s, _data + _stringsPos + pos);
}
#endif
#ifdef NSIS_SCRIPT
#define DEL_DIR 1
#define DEL_RECURSE 2
#define DEL_REBOOT 4
// #define DEL_SIMPLE 8
void CInArchive::AddRegRoot(UInt32 val)
{
Space();
const char *s;
switch (val)
{
case 0: s = "SHCTX"; break;
case 0x80000000: s = "HKCR"; break;
case 0x80000001: s = "HKCU"; break;
case 0x80000002: s = "HKLM"; break;
case 0x80000003: s = "HKU"; break;
case 0x80000004: s = "HKPD"; break;
case 0x80000005: s = "HKCC"; break;
case 0x80000006: s = "HKDD"; break;
case 0x80000050: s = "HKPT"; break;
case 0x80000060: s = "HKPN"; break;
default:
// Script += " RRRRR ";
// throw 1;
Add_Hex(Script, val); return;
}
Script += s;
}
static const char *g_WinAttrib[] =
{
"READONLY"
, "HIDDEN"
, "SYSTEM"
, NULL
, "DIRECTORY"
, "ARCHIVE"
, "DEVICE"
, "NORMAL"
, "TEMPORARY"
, "SPARSE_FILE"
, "REPARSE_POINT"
, "COMPRESSED"
, "OFFLINE"
, "NOT_CONTENT_INDEXED"
, "ENCRYPTED"
, NULL
, "VIRTUAL"
};
#define FLAGS_DELIMITER '|'
static void FlagsToString2(CDynLimBuf &s, const char * const *table, unsigned num, UInt32 flags)
{
bool filled = false;
for (unsigned i = 0; i < num; i++)
{
UInt32 f = (UInt32)1 << i;
if ((flags & f) != 0)
{
const char *name = table[i];
if (name)
{
if (filled)
s += FLAGS_DELIMITER;
filled = true;
s += name;
flags &= ~f;
}
}
}
if (flags != 0)
{
if (filled)
s += FLAGS_DELIMITER;
Add_Hex(s, flags);
}
}
static bool DoesNeedQuotes(const char *s)
{
char c = s[0];
if (c == 0 || c == '#' || c == ';' || (c == '/' && s[1] == '*'))
return true;
for (;;)
{
char c = *s++;
if (c == 0)
return false;
if (c == ' ')
return true;
}
}
void CInArchive::Add_QuStr(const AString &s)
{
bool needQuotes = DoesNeedQuotes(s);
if (needQuotes)
Script += '\"';
Script += s;
if (needQuotes)
Script += '\"';
}
void CInArchive::SpaceQuStr(const AString &s)
{
Space();
Add_QuStr(s);
}
void CInArchive::AddParam(UInt32 pos)
{
_tempString.Empty();
ReadString2(_tempString, pos);
SpaceQuStr(_tempString);
}
void CInArchive::AddParams(const UInt32 *params, unsigned num)
{
for (unsigned i = 0; i < num; i++)
AddParam(params[i]);
}
void CInArchive::AddOptionalParam(UInt32 pos)
{
if (pos != 0)
AddParam(pos);
}
static unsigned GetNumParams(const UInt32 *params, unsigned num)
{
for (; num > 0 && params[num - 1] == 0; num--);
return num;
}
void CInArchive::AddOptionalParams(const UInt32 *params, unsigned num)
{
AddParams(params, GetNumParams(params, num));
}
static const UInt32 CMD_REF_Goto = (1 << 0);
static const UInt32 CMD_REF_Call = (1 << 1);
static const UInt32 CMD_REF_Pre = (1 << 2);
static const UInt32 CMD_REF_Show = (1 << 3);
static const UInt32 CMD_REF_Leave = (1 << 4);
static const UInt32 CMD_REF_OnFunc = (1 << 5);
static const UInt32 CMD_REF_Section = (1 << 6);
static const UInt32 CMD_REF_InitPluginDir = (1 << 7);
// static const UInt32 CMD_REF_Creator = (1 << 5); // _Pre is used instead
static const unsigned CMD_REF_OnFunc_NumShifts = 28; // it uses for onFunc too
static const unsigned CMD_REF_Page_NumShifts = 16; // it uses for onFunc too
static const UInt32 CMD_REF_Page_Mask = 0x0FFF0000;
static const UInt32 CMD_REF_OnFunc_Mask = 0xF0000000;
inline bool IsPageFunc(UInt32 flag)
{
return (flag & (CMD_REF_Pre | CMD_REF_Show | CMD_REF_Leave)) != 0;
}
inline bool IsFunc(UInt32 flag)
{
// return (flag & (CMD_REF_Pre | CMD_REF_Show | CMD_REF_Leave | CMD_REF_OnFunc)) != 0;
return (flag & (CMD_REF_Call | CMD_REF_Pre | CMD_REF_Show | CMD_REF_Leave | CMD_REF_OnFunc)) != 0;
}
inline bool IsProbablyEndOfFunc(UInt32 flag)
{
return (flag != 0 && flag != CMD_REF_Goto);
}
static const char * const kOnFunc[] =
{
"Init"
, "InstSuccess"
, "InstFailed"
, "UserAbort"
, "GUIInit"
, "GUIEnd"
, "MouseOverSection"
, "VerifyInstDir"
, "SelChange"
, "RebootFailed"
};
void CInArchive::Add_FuncName(const UInt32 *labels, UInt32 index)
{
UInt32 mask = labels[index];
if (mask & CMD_REF_OnFunc)
{
Script += ".on";
Script += kOnFunc[labels[index] >> CMD_REF_OnFunc_NumShifts];
}
else if (mask & CMD_REF_InitPluginDir)
{
/*
if (!IsInstaller)
Script += "un."
*/
Script += "Initialize_____Plugins";
}
else
{
Script += "func_";
Add_UInt(index);
}
}
void CInArchive::AddParam_Func(const UInt32 *labels, UInt32 index)
{
Space();
if ((Int32)index >= 0)
Add_FuncName(labels, index);
else
AddQuotes();
}
void CInArchive::Add_LabelName(UInt32 index)
{
Script += "label_";
Add_UInt(index);
}
// param != 0
void CInArchive::Add_GotoVar(UInt32 param)
{
Space();
if ((Int32)param < 0)
Add_Var(-((Int32)param + 1));
else
Add_LabelName(param - 1);
}
void CInArchive::Add_GotoVar1(UInt32 param)
{
if (param == 0)
Script += " 0";
else
Add_GotoVar(param);
}
void CInArchive::Add_GotoVars2(const UInt32 *params)
{
Add_GotoVar1(params[0]);
if (params[1] != 0)
Add_GotoVar(params[1]);
}
static bool NoLabels(const UInt32 *labels, UInt32 num)
{
for (UInt32 i = 0; i < num; i++)
if (labels[i] != 0)
return false;
return true;
}
static const char * const k_REBOOTOK = " /REBOOTOK";
#define MY__MB_ABORTRETRYIGNORE 2
#define MY__MB_RETRYCANCEL 5
static const char * const k_MB_Buttons[] =
{
"OK"
, "OKCANCEL"
, "ABORTRETRYIGNORE"
, "YESNOCANCEL"
, "YESNO"
, "RETRYCANCEL"
, "CANCELTRYCONTINUE"
};
#define MY__MB_ICONSTOP (1 << 4)
static const char * const k_MB_Icons[] =
{
NULL
, "ICONSTOP"
, "ICONQUESTION"
, "ICONEXCLAMATION"
, "ICONINFORMATION"
};
static const char * const k_MB_Flags[] =
{
"HELP"
, "NOFOCUS"
, "SETFOREGROUND"
, "DEFAULT_DESKTOP_ONLY"
, "TOPMOST"
, "RIGHT"
, "RTLREADING"
// , "SERVICE_NOTIFICATION" // unsupported. That bit is used for NSIS purposes
};
#define MY__IDCANCEL 2
#define MY__IDIGNORE 5
static const char * const k_Button_IDs[] =
{
"0"
, "IDOK"
, "IDCANCEL"
, "IDABORT"
, "IDRETRY"
, "IDIGNORE"
, "IDYES"
, "IDNO"
, "IDCLOSE"
, "IDHELP"
, "IDTRYAGAIN"
, "IDCONTINUE"
};
void CInArchive::Add_ButtonID(UInt32 buttonID)
{
Space();
if (buttonID < ARRAY_SIZE(k_Button_IDs))
Script += k_Button_IDs[buttonID];
else
{
Script += "Button_";
Add_UInt(buttonID);
}
}
bool CInArchive::IsDirectString_Equal(UInt32 offset, const char *s) const
{
if (offset >= NumStringChars)
return false;
if (IsUnicode)
return AreStringsEqual_16and8(_data + _stringsPos + offset * 2, s);
else
return strcmp((const char *)(const Byte *)_data + _stringsPos + offset, s) == 0;
}
static bool StringToUInt32(const char *s, UInt32 &res)
{
const char *end;
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
res = ConvertHexStringToUInt32(s + 2, &end);
else
res = ConvertStringToUInt32(s, &end);
return (*end == 0);
}
static const unsigned k_CtlColors_Size = 24;
struct CNsis_CtlColors
{
UInt32 text; // COLORREF
UInt32 bkc; // COLORREF
UInt32 lbStyle;
UInt32 bkb; // HBRUSH
Int32 bkmode;
Int32 flags;
void Parse(const Byte *p);
};
void CNsis_CtlColors::Parse(const Byte *p)
{
text = Get32(p);
bkc = Get32(p + 4);
lbStyle = Get32(p + 8);
bkb = Get32(p + 12);
bkmode = (Int32)Get32(p + 16);
flags = (Int32)Get32(p + 20);
}
// Win32 constants
#define MY__TRANSPARENT 1
#define MY__OPAQUE 2
#define MY__GENERIC_READ (1 << 31)
#define MY__GENERIC_WRITE (1 << 30)
#define MY__GENERIC_EXECUTE (1 << 29)
#define MY__GENERIC_ALL (1 << 28)
#define MY__CREATE_NEW 1
#define MY__CREATE_ALWAYS 2
#define MY__OPEN_EXISTING 3
#define MY__OPEN_ALWAYS 4
#define MY__TRUNCATE_EXISTING 5
// text/bg colors
#define kColorsFlags_TEXT 1
#define kColorsFlags_TEXT_SYS 2
#define kColorsFlags_BK 4
#define kColorsFlags_BK_SYS 8
#define kColorsFlags_BKB 16
void CInArchive::Add_Color2(UInt32 v)
{
v = ((v & 0xFF) << 16) | (v & 0xFF00) | ((v >> 16) & 0xFF);
char sz[32];
for (int i = 5; i >= 0; i--)
{
unsigned t = v & 0xF;
v >>= 4;
sz[i] = (char)(((t < 10) ? ('0' + t) : ('A' + (t - 10))));
}
sz[6] = 0;
Script += sz;
}
void CInArchive::Add_ColorParam(UInt32 v)
{
Space();
Add_Color2(v);
}
void CInArchive::Add_Color(UInt32 v)
{
Script += "0x";
Add_Color2(v);
}
#define MY__SW_HIDE 0
#define MY__SW_SHOWNORMAL 1
#define MY__SW_SHOWMINIMIZED 2
#define MY__SW_SHOWMINNOACTIVE 7
#define MY__SW_SHOWNA 8
static const char * const kShowWindow_Commands[] =
{
"HIDE"
, "SHOWNORMAL" // "NORMAL"
, "SHOWMINIMIZED"
, "SHOWMAXIMIZED" // "MAXIMIZE"
, "SHOWNOACTIVATE"
, "SHOW"
, "MINIMIZE"
, "SHOWMINNOACTIVE"
, "SHOWNA"
, "RESTORE"
, "SHOWDEFAULT"
, "FORCEMINIMIZE" // "MAX"
};
static void Add_ShowWindow_Cmd_2(AString &s, UInt32 cmd)
{
if (cmd < ARRAY_SIZE(kShowWindow_Commands))
{
s += "SW_";
s += kShowWindow_Commands[cmd];
}
else
UIntToString(s, cmd);
}
void CInArchive::Add_ShowWindow_Cmd(UInt32 cmd)
{
if (cmd < ARRAY_SIZE(kShowWindow_Commands))
{
Script += "SW_";
Script += kShowWindow_Commands[cmd];
}
else
Add_UInt(cmd);
}
void CInArchive::Add_TypeFromList(const char * const *table, unsigned tableSize, UInt32 type)
{
if (type < tableSize)
Script += table[type];
else
{
Script += '_';
Add_UInt(type);
}
}
#define ADD_TYPE_FROM_LIST(table, type) Add_TypeFromList(table, ARRAY_SIZE(table), type)
enum
{
k_ExecFlags_AutoClose,
k_ExecFlags_ShellVarContext,
k_ExecFlags_Errors,
k_ExecFlags_Abort,
k_ExecFlags_RebootFlag,
k_ExecFlags_reboot_called,
k_ExecFlags_cur_insttype,
k_ExecFlags_plugin_api_version,
k_ExecFlags_Silent,
k_ExecFlags_InstDirError,
k_ExecFlags_rtl,
k_ExecFlags_ErrorLevel,
k_ExecFlags_RegView,
k_ExecFlags_DetailsPrint = 13,
};
// Names for NSIS exec_flags_t structure vars
static const char * const kExecFlags_VarsNames[] =
{
"AutoClose" // autoclose;
, "ShellVarContext" // all_user_var;
, "Errors" // exec_error;
, "Abort" // abort;
, "RebootFlag" // exec_reboot; // NSIS_SUPPORT_REBOOT
, "reboot_called" // reboot_called; // NSIS_SUPPORT_REBOOT
, "cur_insttype" // XXX_cur_insttype; // depreacted
, "plugin_api_version" // plugin_api_version; // see NSISPIAPIVER_CURR
// used to be XXX_insttype_changed
, "Silent" // silent; // NSIS_CONFIG_SILENT_SUPPORT
, "InstDirError" // instdir_error;
, "rtl" // rtl;
, "ErrorLevel" // errlvl;
, "RegView" // alter_reg_view;
, "DetailsPrint" // status_update;
};
void CInArchive::Add_ExecFlags(UInt32 flagsType)
{
ADD_TYPE_FROM_LIST(kExecFlags_VarsNames, flagsType);
}
// ---------- Page ----------
// page flags
#define PF_CANCEL_ENABLE 4
#define PF_LICENSE_FORCE_SELECTION 32
#define PF_LICENSE_NO_FORCE_SELECTION 64
#define PF_PAGE_EX 512
#define PF_DIR_NO_BTN_DISABLE 1024
/*
#define PF_LICENSE_SELECTED 1
#define PF_NEXT_ENABLE 2
#define PF_BACK_SHOW 8
#define PF_LICENSE_STREAM 16
#define PF_NO_NEXT_FOCUS 128
#define PF_BACK_ENABLE 256
*/
// page window proc
enum
{
PWP_LICENSE,
PWP_SELCOM,
PWP_DIR,
PWP_INSTFILES,
PWP_UNINST,
PWP_COMPLETED,
PWP_CUSTOM
};
static const char * const kPageTypes[] =
{
"license"
, "components"
, "directory"
, "instfiles"
, "uninstConfirm"
, "COMPLETED"
, "custom"
};
#define SET_FUNC_REF(x, flag) if ((Int32)(x) >= 0 && (x) < bh.Num) \
{ labels[x] = (labels[x] & ~CMD_REF_Page_Mask) | ((flag) | (pageIndex << CMD_REF_Page_NumShifts)); }
// #define IDD_LICENSE 102
#define IDD_LICENSE_FSRB 108
#define IDD_LICENSE_FSCB 109
void CInArchive::AddPageOption1(UInt32 param, const char *name)
{
if (param == 0)
return;
TabString(name);
AddParam(param);
NewLine();
}
void CInArchive::AddPageOption(const UInt32 *params, unsigned num, const char *name)
{
num = GetNumParams(params, num);
if (num == 0)
return;
TabString(name);
AddParams(params, num);
NewLine();
}
void CInArchive::Separator()
{
AddLF();
AddCommentAndString("--------------------");
AddLF();
}
void CInArchive::Space()
{
Script += ' ';
}
void CInArchive::Tab()
{
Script += " ";
}
void CInArchive::Tab(bool commented)
{
Script += commented ? " ; " : " ";
}
void CInArchive::BigSpaceComment()
{
Script += " ; ";
}
void CInArchive::SmallSpaceComment()
{
Script += " ; ";
}
void CInArchive::AddCommentAndString(const char *s)
{
Script += "; ";
Script += s;
}
void CInArchive::AddError(const char *s)
{
BigSpaceComment();
Script += "!!! ERROR: ";
Script += s;
}
void CInArchive::AddErrorLF(const char *s)
{
AddError(s);
AddLF();
}
void CInArchive::CommentOpen()
{
AddStringLF("/*");
}
void CInArchive::CommentClose()
{
AddStringLF("*/");
}
void CInArchive::AddLF()
{
Script += CR_LF;
}
void CInArchive::AddQuotes()
{
Script += "\"\"";
}
void CInArchive::TabString(const char *s)
{
Tab();
Script += s;
}
void CInArchive::AddStringLF(const char *s)
{
Script += s;
AddLF();
}
// ---------- Section ----------
static const char * const kSection_VarsNames[] =
{
"Text"
, "InstTypes"
, "Flags"
, "Code"
, "CodeSize"
, "Size" // size in KB
};
void CInArchive::Add_SectOp(UInt32 opType)
{
ADD_TYPE_FROM_LIST(kSection_VarsNames, opType);
}
void CSection::Parse(const Byte *p)
{
Name = Get32(p);
InstallTypes = Get32(p + 4);
Flags = Get32(p + 8);
StartCmdIndex = Get32(p + 12);
NumCommands = Get32(p + 16);
SizeKB = Get32(p + 20);
};
// used for section->flags
#define SF_SELECTED (1 << 0)
#define SF_SECGRP (1 << 1)
#define SF_SECGRPEND (1 << 2)
#define SF_BOLD (1 << 3)
#define SF_RO (1 << 4)
#define SF_EXPAND (1 << 5)
#define SF_PSELECTED (1 << 6)
#define SF_TOGGLED (1 << 7)
#define SF_NAMECHG (1 << 8)
bool CInArchive::PrintSectionBegin(const CSection §, unsigned index)
{
AString name;
if (sect.Flags & SF_BOLD)
name += '!';
AString s2;
ReadString2(s2, sect.Name);
if (!IsInstaller)
{
if (!StringsAreEqualNoCase_Ascii(s2, "uninstall"))
name += "un.";
}
name += s2;
if (sect.Flags & SF_SECGRPEND)
{
AddStringLF("SectionGroupEnd");
return true;
}
if (sect.Flags & SF_SECGRP)
{
Script += "SectionGroup";
if (sect.Flags & SF_EXPAND)
Script += " /e";
SpaceQuStr(name);
Script += " ; Section";
AddParam_UInt(index);
NewLine();
return true;
}
Script += "Section";
if ((sect.Flags & SF_SELECTED) == 0)
Script += " /o";
if (!name.IsEmpty())
SpaceQuStr(name);
/*
if (!name.IsEmpty())
Script += ' ';
else
*/
SmallSpaceComment();
Script += "Section_";
Add_UInt(index);
/*
Script += " ; flags = ";
Add_Hex(Script, sect.Flags);
*/
NewLine();
if (sect.SizeKB != 0)
{
// probably we must show AddSize, only if there is additional size.
Tab();
AddCommentAndString("AddSize");
AddParam_UInt(sect.SizeKB);
AddLF();
}
bool needSectionIn =
(sect.Name != 0 && sect.InstallTypes != 0) ||
(sect.Name == 0 && sect.InstallTypes != 0xFFFFFFFF);
if (needSectionIn || (sect.Flags & SF_RO) != 0)
{
TabString("SectionIn");
UInt32 instTypes = sect.InstallTypes;
for (int i = 0; i < 32; i++, instTypes >>= 1)
if ((instTypes & 1) != 0)
{
AddParam_UInt(i + 1);
}
if ((sect.Flags & SF_RO) != 0)
Script += " RO";
AddLF();
}
return false;
}
void CInArchive::PrintSectionEnd()
{
AddStringLF("SectionEnd");
AddLF();
}
// static const unsigned kOnFuncShift = 4;
void CInArchive::ClearLangComment()
{
langStrIDs.Clear();
}
void CInArchive::PrintNumComment(const char *name, UInt32 value)
{
// size_t len = Script.Len();
AddCommentAndString(name);
Script += ": ";
Add_UInt(value);
AddLF();
/*
len = Script.Len() - len;
char sz[16];
ConvertUInt32ToString(value, sz);
len += MyStringLen(sz);
for (; len < 20; len++)
Space();
AddStringLF(sz);
*/
}
void CInArchive::NewLine()
{
if (!langStrIDs.IsEmpty())
{
BigSpaceComment();
for (unsigned i = 0; i < langStrIDs.Size() && i < 20; i++)
{
/*
if (i != 0)
Script += ' ';
*/
UInt32 langStr = langStrIDs[i];
if (langStr >= _numLangStrings)
{
AddError("langStr");
break;
}
UInt32 param = Get32(_mainLang + langStr * 4);
if (param != 0)
AddParam(param);
}
ClearLangComment();
}
AddLF();
}
static const UInt32 kPageSize = 16 * 4;
static const char * const k_SetOverwrite_Modes[] =
{
"on"
, "off"
, "try"
, "ifnewer"
, "ifdiff"
// "lastused"
};
void CInArchive::MessageBox_MB_Part(UInt32 param)
{
{
UInt32 v = param & 0xF;
Script += " MB_";
if (v < ARRAY_SIZE(k_MB_Buttons))
Script += k_MB_Buttons[v];
else
{
Script += "Buttons_";
Add_UInt(v);
}
}
{
UInt32 icon = (param >> 4) & 0x7;
if (icon != 0)
{
Script += "|MB_";
if (icon < ARRAY_SIZE(k_MB_Icons) && k_MB_Icons[icon] != 0)
Script += k_MB_Icons[icon];
else
{
Script += "Icon_";
Add_UInt(icon);
}
}
}
if ((param & 0x80) != 0)
Script += "|MB_USERICON";
{
UInt32 defButton = (param >> 8) & 0xF;
if (defButton != 0)
{
Script += "|MB_DEFBUTTON";
Add_UInt(defButton + 1);
}
}
{
UInt32 modal = (param >> 12) & 0x3;
if (modal == 1) Script += "|MB_SYSTEMMODAL";
else if (modal == 2) Script += "|MB_TASKMODAL";
else if (modal == 3) Script += "|0x3000";
UInt32 flags = (param >> 14);
for (unsigned i = 0; i < ARRAY_SIZE(k_MB_Flags); i++)
if ((flags & (1 << i)) != 0)
{
Script += "|MB_";
Script += k_MB_Flags[i];
}
}
}
#define GET_CMD_PARAM(ppp, index) Get32((ppp) + 4 + (index) * 4)
static const Byte k_InitPluginDir_Commands[] =
{ 13, 26, 31, 13, 19, 21, 11, 14, 25, 31, 1, 22, 4, 1 };
bool CInArchive::CompareCommands(const Byte *rawCmds, const Byte *sequence, size_t numCommands)
{
for (UInt32 kkk = 0; kkk < numCommands; kkk++, rawCmds += kCmdSize)
if (GetCmd(Get32(rawCmds)) != sequence[kkk])
return false;
return true;
}
#endif
static const UInt32 kSectionSize_base = 6 * 4;
static const UInt32 kSectionSize_8bit = kSectionSize_base + 1024;
static const UInt32 kSectionSize_16bit = kSectionSize_base + 1024 * 2;
static const UInt32 kSectionSize_16bit_Big = kSectionSize_base + 8196 * 2;
// 8196 is default string length in NSIS-Unicode since 2.37.3
static void AddString(AString &dest, const char *src)
{
dest.Add_Space_if_NotEmpty();
dest += src;
}
AString CInArchive::GetFormatDescription() const
{
AString s = "NSIS-";
char c;
if (IsPark())
{
s += "Park-";
c = '1';
if (NsisType == k_NsisType_Park2) c = '2';
else if (NsisType == k_NsisType_Park3) c = '3';
}
else
{
c = '2';
if (NsisType == k_NsisType_Nsis3)
c = '3';
}
s += c;
if (IsNsis200)
s += ".00";
else if (IsNsis225)
s += ".25";
if (IsUnicode)
AddString(s, "Unicode");
if (LogCmdIsEnabled)
AddString(s, "log");
if (BadCmd >= 0)
{
AddString(s, "BadCmd=");
UIntToString(s, BadCmd);
}
return s;
}
#ifdef NSIS_SCRIPT
unsigned CInArchive::GetNumSupportedCommands() const
{
unsigned numCmds = IsPark() ? kNumCmds : kNumCmds - kNumAdditionalParkCmds;
if (!LogCmdIsEnabled)
numCmds--;
if (!IsUnicode)
numCmds -= 2;
return numCmds;
}
#endif
UInt32 CInArchive::GetCmd(UInt32 a)
{
if (!IsPark())
{
if (!LogCmdIsEnabled)
return a;
if (a < EW_SECTIONSET)
return a;
if (a == EW_SECTIONSET)
return EW_LOG;
return a - 1;
}
if (a < EW_REGISTERDLL)
return a;
if (NsisType >= k_NsisType_Park2)
{
if (a == EW_REGISTERDLL) return EW_GETFONTVERSION;
a--;
}
if (NsisType >= k_NsisType_Park3)
{
if (a == EW_REGISTERDLL) return EW_GETFONTNAME;
a--;
}
if (a >= EW_FSEEK)
{
if (IsUnicode)
{
if (a == EW_FSEEK) return EW_FPUTWS;
if (a == EW_FSEEK + 1) return EW_FPUTWS + 1;
a -= 2;
}
if (a >= EW_SECTIONSET && LogCmdIsEnabled)
{
if (a == EW_SECTIONSET)
return EW_LOG;
return a - 1;
}
if (a == EW_FPUTWS)
return EW_FINDPROC;
// if (a > EW_FPUTWS) return 0;
}
return a;
}
void CInArchive::FindBadCmd(const CBlockHeader &bh, const Byte *p)
{
BadCmd = -1;
for (UInt32 kkk = 0; kkk < bh.Num; kkk++, p += kCmdSize)
{
UInt32 id = GetCmd(Get32(p));
if (id >= kNumCmds)
continue;
if (BadCmd >= 0 && id >= (unsigned)BadCmd)
continue;
unsigned i;
if (id == EW_GETLABELADDR ||
id == EW_GETFUNCTIONADDR)
{
BadCmd = id;
continue;
}
for (i = 6; i != 0; i--)
{
UInt32 param = Get32(p + i * 4);
if (param != 0)
break;
}
if (id == EW_FINDPROC && i == 0)
{
BadCmd = id;
continue;
}
if (k_Commands[id].NumParams < i)
BadCmd = id;
}
}
/* We calculate the number of parameters in commands to detect
layout of commands. It's not very good way.
If you know simpler and more robust way to detect Version and layout,
please write to 7-Zip forum */
void CInArchive::DetectNsisType(const CBlockHeader &bh, const Byte *p)
{
bool strongPark = false;
bool strongNsis = false;
{
const Byte *strData = _data + _stringsPos;
if (IsUnicode)
{
UInt32 num = NumStringChars;
for (UInt32 i = 0; i < num; i++)
{
if (Get16(strData + i * 2) == 0)
{
unsigned c2 = Get16(strData + 2 + i * 2);
// if (c2 <= NS_3_CODE_SKIP && c2 != NS_3_CODE_SHELL)
if (c2 == NS_3_CODE_VAR)
{
// it can be TXT/RTF string with marker char (1 or 2). so we must next char
// const wchar_t *p2 = (const wchar_t *)(strData + i * 2 + 2);
// p2 = p2;
if ((Get16(strData + 3 + i * 2) & 0x8000) != 0)
{
NsisType = k_NsisType_Nsis3;
strongNsis = true;
break;
}
}
}
}
if (!strongNsis)
{
NsisType = k_NsisType_Park1;
strongPark = true;
}
}
else
{
UInt32 num = NumStringChars;
for (UInt32 i = 0; i < num; i++)
{
if (strData[i] == 0)
{
Byte c2 = strData[i + 1];
// it can be TXT/RTF with marker char (1 or 2). so we must check next char
// for marker=1 (txt)
if (c2 == NS_3_CODE_VAR)
// if (c2 <= NS_3_CODE_SKIP && c2 != NS_3_CODE_SHELL && c2 != 1)
{
if ((strData[i+ 2] & 0x80) != 0)
{
// const char *p2 = (const char *)(strData + i + 1);
// p2 = p2;
NsisType = k_NsisType_Nsis3;
strongNsis = true;
break;
}
}
}
}
}
}
if (NsisType == k_NsisType_Nsis2 && !IsUnicode)
{
const Byte *p2 = p;
for (UInt32 kkk = 0; kkk < bh.Num; kkk++, p2 += kCmdSize)
{
UInt32 cmd = GetCmd(Get32(p2));
if (cmd != EW_GETDLGITEM &&
cmd != EW_ASSIGNVAR)
continue;
UInt32 params[kNumCommandParams];
for (unsigned i = 0; i < kNumCommandParams; i++)
params[i] = Get32(p2 + 4 + 4 * i);
if (cmd == EW_GETDLGITEM)
{
// we can use also EW_SETCTLCOLORS
if (IsVarStr(params[1], kVar_HWNDPARENT_225))
{
IsNsis225 = true;
if (params[0] == kVar_Spec_OUTDIR_225)
{
IsNsis200 = true;
break;
}
}
}
else // if (cmd == EW_ASSIGNVAR)
{
if (params[0] == kVar_Spec_OUTDIR_225 &&
params[2] == 0 &&
params[3] == 0 &&
IsVarStr(params[1], kVar_OUTDIR))
IsNsis225 = true;
}
}
}
bool parkVer_WasDetected = false;
if (!strongNsis && !IsNsis225 && !IsNsis200)
{
// it must be before FindBadCmd(bh, p);
unsigned mask = 0;
unsigned numInsertMax = IsUnicode ? 4 : 2;
const Byte *p2 = p;
for (UInt32 kkk = 0; kkk < bh.Num; kkk++, p2 += kCmdSize)
{
UInt32 cmd = Get32(p2); // we use original (not converted) command
if (cmd < EW_WRITEUNINSTALLER ||
cmd > EW_WRITEUNINSTALLER + numInsertMax)
continue;
UInt32 params[kNumCommandParams];
for (unsigned i = 0; i < kNumCommandParams; i++)
params[i] = Get32(p2 + 4 + 4 * i);
if (params[4] != 0 ||
params[5] != 0 ||
params[0] <= 1 ||
params[3] <= 1)
continue;
UInt32 altParam = params[3];
if (!IsGoodString(params[0]) ||
!IsGoodString(altParam))
continue;
UInt32 additional = 0;
if (GetVarIndexFinished(altParam, '\\', additional) != kVar_INSTDIR)
continue;
if (AreTwoParamStringsEqual(altParam + additional, params[0]))
{
unsigned numInserts = cmd - EW_WRITEUNINSTALLER;
mask |= (1 << numInserts);
}
}
if (mask == 1)
{
parkVer_WasDetected = true; // it can be original NSIS or Park-1
}
else if (mask != 0)
{
ENsisType newType = NsisType;
if (IsUnicode)
switch (mask)
{
case (1 << 3): newType = k_NsisType_Park2; break;
case (1 << 4): newType = k_NsisType_Park3; break;
}
else
switch (mask)
{
case (1 << 1): newType = k_NsisType_Park2; break;
case (1 << 2): newType = k_NsisType_Park3; break;
}
if (newType != NsisType)
{
parkVer_WasDetected = true;
NsisType = newType;
}
}
}
FindBadCmd(bh, p);
/*
if (strongNsis)
return;
*/
if (BadCmd < EW_REGISTERDLL)
return;
/*
// in ANSI archive we don't check Park and log version
if (!IsUnicode)
return;
*/
// We can support Park-ANSI archives, if we remove if (strongPark) check
if (strongPark && !parkVer_WasDetected)
{
if (BadCmd < EW_SECTIONSET)
{
NsisType = k_NsisType_Park3;
LogCmdIsEnabled = true; // version 3 is provided with log enabled
FindBadCmd(bh, p);
if (BadCmd > 0 && BadCmd < EW_SECTIONSET)
{
NsisType = k_NsisType_Park2;
LogCmdIsEnabled = false;
FindBadCmd(bh, p);
if (BadCmd > 0 && BadCmd < EW_SECTIONSET)
{
NsisType = k_NsisType_Park1;
FindBadCmd(bh, p);
}
}
}
}
if (BadCmd >= EW_SECTIONSET)
{
LogCmdIsEnabled = !LogCmdIsEnabled;
FindBadCmd(bh, p);
if (BadCmd >= EW_SECTIONSET && LogCmdIsEnabled)
{
LogCmdIsEnabled = false;
FindBadCmd(bh, p);
}
}
}
Int32 CInArchive::GetVarIndex(UInt32 strPos) const
{
if (strPos >= NumStringChars)
return -1;
if (IsUnicode)
{
if (NumStringChars - strPos < 3 * 2)
return -1;
const Byte *p = _data + _stringsPos + strPos * 2;
unsigned code = Get16(p);
if (IsPark())
{
if (code != PARK_CODE_VAR)
return -1;
UInt32 n = Get16(p + 2);
if (n == 0)
return -1;
CONVERT_NUMBER_PARK(n);
return (Int32)n;
}
// NSIS-3
{
if (code != NS_3_CODE_VAR)
return -1;
UInt32 n = Get16(p + 2);
if (n == 0)
return -1;
CONVERT_NUMBER_NS_3_UNICODE(n);
return (Int32)n;
}
}
if (NumStringChars - strPos < 4)
return -1;
const Byte *p = _data + _stringsPos + strPos;
unsigned c = *p;
if (NsisType == k_NsisType_Nsis3)
{
if (c != NS_3_CODE_VAR)
return -1;
}
else if (c != NS_CODE_VAR)
return -1;
unsigned c0 = p[1];
if (c0 == 0)
return -1;
unsigned c1 = p[2];
if (c1 == 0)
return -1;
return DECODE_NUMBER_FROM_2_CHARS(c0, c1);
}
Int32 CInArchive::GetVarIndex(UInt32 strPos, UInt32 &resOffset) const
{
resOffset = 0;
Int32 varIndex = GetVarIndex(strPos);
if (varIndex < 0)
return varIndex;
if (IsUnicode)
{
if (NumStringChars - strPos < 2 * 2)
return -1;
resOffset = 2;
}
else
{
if (NumStringChars - strPos < 3)
return -1;
resOffset = 3;
}
return varIndex;
}
Int32 CInArchive::GetVarIndexFinished(UInt32 strPos, Byte endChar, UInt32 &resOffset) const
{
resOffset = 0;
Int32 varIndex = GetVarIndex(strPos);
if (varIndex < 0)
return varIndex;
if (IsUnicode)
{
if (NumStringChars - strPos < 3 * 2)
return -1;
const Byte *p = _data + _stringsPos + strPos * 2;
if (Get16(p + 4) != endChar)
return -1;
resOffset = 3;
}
else
{
if (NumStringChars - strPos < 4)
return -1;
const Byte *p = _data + _stringsPos + strPos;
if (p[3] != endChar)
return -1;
resOffset = 4;
}
return varIndex;
}
bool CInArchive::IsVarStr(UInt32 strPos, UInt32 varIndex) const
{
if (varIndex > (UInt32)0x7FFF)
return false;
UInt32 resOffset;
return GetVarIndexFinished(strPos, 0, resOffset) == (Int32)varIndex;
}
bool CInArchive::IsAbsolutePathVar(UInt32 strPos) const
{
Int32 varIndex = GetVarIndex(strPos);
if (varIndex < 0)
return false;
switch (varIndex)
{
case kVar_INSTDIR:
case kVar_EXEDIR:
case kVar_TEMP:
case kVar_PLUGINSDIR:
return true;
}
return false;
}
#define IS_LETTER_CHAR(c) ((c) >= 'a' && (c) <= 'z' || (c) >= 'A' && (c) <= 'Z')
// We use same check as in NSIS decoder
bool IsDrivePath(const wchar_t *s) { return IS_LETTER_CHAR(s[0]) && s[1] == ':' /* && s[2] == '\\' */ ; }
bool IsDrivePath(const char *s) { return IS_LETTER_CHAR(s[0]) && s[1] == ':' /* && s[2] == '\\' */ ; }
static bool IsAbsolutePath(const wchar_t *s)
{
return
s[0] == WCHAR_PATH_SEPARATOR &&
s[1] == WCHAR_PATH_SEPARATOR ||
IsDrivePath(s);
}
static bool IsAbsolutePath(const char *s)
{
return
s[0] == CHAR_PATH_SEPARATOR &&
s[1] == CHAR_PATH_SEPARATOR ||
IsDrivePath(s);
}
void CInArchive::SetItemName(CItem &item, UInt32 strPos)
{
ReadString2_Raw(strPos);
bool isAbs = IsAbsolutePathVar(strPos);
if (IsUnicode)
{
item.NameU = Raw_UString;
if (!isAbs && !IsAbsolutePath(Raw_UString))
item.Prefix = UPrefixes.Size() - 1;
}
else
{
item.NameA = Raw_AString;
if (!isAbs && !IsAbsolutePath(Raw_AString))
item.Prefix = APrefixes.Size() - 1;
}
}
HRESULT CInArchive::ReadEntries(const CBlockHeader &bh)
{
#ifdef NSIS_SCRIPT
CDynLimBuf &s = Script;
CObjArray<UInt32> labels;
labels.Alloc(bh.Num);
memset(labels, 0, bh.Num * sizeof(UInt32));
{
const Byte *p = _data;
UInt32 i;
for (i = 0; i < numOnFunc; i++)
{
UInt32 func = Get32(p + onFuncOffset + 4 * i);
if (func < bh.Num)
labels[func] = (labels[func] & ~CMD_REF_OnFunc_Mask) | (CMD_REF_OnFunc | (i << CMD_REF_OnFunc_NumShifts));
}
}
/*
{
for (int i = 0; i < OnFuncs.Size(); i++)
{
UInt32 address = OnFuncs[i] >> kOnFuncShift;
if (address < bh.Num)
}
}
*/
if (bhPages.Num != 0)
{
Separator();
PrintNumComment("PAGES", bhPages.Num);
if (bhPages.Num > (1 << 12)
|| bhPages.Offset > _size
|| bhPages.Num * kPageSize > _size - bhPages.Offset)
{
AddErrorLF("Pages error");
}
else
{
AddLF();
const Byte *p = _data + bhPages.Offset;
for (UInt32 pageIndex = 0; pageIndex < bhPages.Num; pageIndex++, p += kPageSize)
{
UInt32 dlgID = Get32(p);
UInt32 wndProcID = Get32(p + 4);
UInt32 preFunc = Get32(p + 8);
UInt32 showFunc = Get32(p + 12);
UInt32 leaveFunc = Get32(p + 16);
UInt32 flags = Get32(p + 20);
UInt32 caption = Get32(p + 24);
// UInt32 back = Get32(p + 28);
UInt32 next = Get32(p + 32);
// UInt32 clickNext = Get32(p + 36);
// UInt32 cancel = Get32(p + 40);
UInt32 params[5];
for (int i = 0; i < 5; i++)
params[i] = Get32(p + 44 + 4 * i);
SET_FUNC_REF(preFunc, CMD_REF_Pre);
SET_FUNC_REF(showFunc, CMD_REF_Show);
SET_FUNC_REF(leaveFunc, CMD_REF_Leave);
if (wndProcID == PWP_COMPLETED)
CommentOpen();
AddCommentAndString("Page ");
Add_UInt(pageIndex);
AddLF();
if (flags & PF_PAGE_EX)
{
s += "PageEx ";
if (!IsInstaller)
s += "un.";
}
else
s += IsInstaller ? "Page " : "UninstPage ";
if (wndProcID < ARRAY_SIZE(kPageTypes))
s += kPageTypes[wndProcID];
else
Add_UInt(wndProcID);
bool needCallbacks = (
(Int32)preFunc >= 0 ||
(Int32)showFunc >= 0 ||
(Int32)leaveFunc >= 0);
if (flags & PF_PAGE_EX)
{
AddLF();
if (needCallbacks)
TabString("PageCallbacks");
}
if (needCallbacks)
{
AddParam_Func(labels, preFunc); // it's creator_function for PWP_CUSTOM
if (wndProcID != PWP_CUSTOM)
{
AddParam_Func(labels, showFunc);
}
AddParam_Func(labels, leaveFunc);
}
if ((flags & PF_PAGE_EX) == 0)
{
// AddOptionalParam(caption);
if (flags & PF_CANCEL_ENABLE)
s += " /ENABLECANCEL";
AddLF();
}
else
{
AddLF();
AddPageOption1(caption, "Caption");
}
if (wndProcID == PWP_LICENSE)
{
if ((flags & PF_LICENSE_NO_FORCE_SELECTION) != 0 ||
(flags & PF_LICENSE_FORCE_SELECTION) != 0)
{
TabString("LicenseForceSelection ");
if (flags & PF_LICENSE_NO_FORCE_SELECTION)
s += "off";
else
{
if (dlgID == IDD_LICENSE_FSCB)
s += "checkbox";
else if (dlgID == IDD_LICENSE_FSRB)
s += "radiobuttons";
else
Add_UInt(dlgID);
AddOptionalParams(params + 2, 2);
}
NewLine();
}
if (params[0] != 0 || next != 0)
{
TabString("LicenseText");
AddParam(params[0]);
AddOptionalParam(next);
NewLine();
}
if (params[1] != 0)
{
TabString("LicenseData");
if ((Int32)params[1] < 0)
AddParam(params[1]);
else
AddLicense(params[1], -1);
ClearLangComment();
NewLine();
}
}
else if (wndProcID == PWP_SELCOM)
AddPageOption(params, 3, "ComponentsText");
else if (wndProcID == PWP_DIR)
{
AddPageOption(params, 4, "DirText");
if (params[4] != 0)
{
TabString("DirVar");
AddParam_Var(params[4] - 1);
AddLF();
}
if (flags & PF_DIR_NO_BTN_DISABLE)
{
TabString("DirVerify leave");
AddLF();
}
}
else if (wndProcID == PWP_INSTFILES)
{
AddPageOption1(params[2], "CompletedText");
AddPageOption1(params[1], "DetailsButtonText");
}
else if (wndProcID == PWP_UNINST)
{
if (params[4] != 0)
{
TabString("DirVar");
AddParam_Var(params[4] - 1);
AddLF();
}
AddPageOption(params, 2, "UninstallText");
}
if (flags & PF_PAGE_EX)
{
s += "PageExEnd";
NewLine();
}
if (wndProcID == PWP_COMPLETED)
CommentClose();
NewLine();
}
}
}
CObjArray<CSection> Sections;
{
Separator();
PrintNumComment("SECTIONS", bhSections.Num);
PrintNumComment("COMMANDS", bh.Num);
AddLF();
if (bhSections.Num > (1 << 15)
// || bhSections.Offset > _size
// || (bhSections.Num * SectionSize > _size - bhSections.Offset)
)
{
AddErrorLF("Sections error");
}
else if (bhSections.Num != 0)
{
Sections.Alloc((unsigned)bhSections.Num);
const Byte *p = _data + bhSections.Offset;
for (UInt32 i = 0; i < bhSections.Num; i++, p += SectionSize)
{
CSection §ion = Sections[i];
section.Parse(p);
if (section.StartCmdIndex < bh.Num)
labels[section.StartCmdIndex] |= CMD_REF_Section;
}
}
}
#endif
const Byte *p;
UInt32 kkk;
#ifdef NSIS_SCRIPT
p = _data + bh.Offset;
for (kkk = 0; kkk < bh.Num; kkk++, p += kCmdSize)
{
UInt32 commandId = GetCmd(Get32(p));
UInt32 mask;
switch (commandId)
{
case EW_NOP: mask = 1 << 0; break;
case EW_IFFILEEXISTS: mask = 3 << 1; break;
case EW_IFFLAG: mask = 3 << 0; break;
case EW_MESSAGEBOX: mask = 5 << 3; break;
case EW_STRCMP: mask = 3 << 2; break;
case EW_INTCMP: mask = 7 << 2; break;
case EW_ISWINDOW: mask = 3 << 1; break;
case EW_CALL:
{
if (Get32(p + 4 + 4) == 1) // it's Call :Label
{
mask = 1 << 0;
break;
}
UInt32 param0 = Get32(p + 4);
if ((Int32)param0 > 0)
labels[param0 - 1] |= CMD_REF_Call;
continue;
}
default: continue;
}
for (unsigned i = 0; mask != 0; i++, mask >>= 1)
if (mask & 1)
{
UInt32 param = Get32(p + 4 + 4 * i);
if ((Int32)param > 0 && (Int32)param <= (Int32)bh.Num)
labels[param - 1] |= CMD_REF_Goto;
}
}
int InitPluginsDir_Start = -1;
int InitPluginsDir_End = -1;
p = _data + bh.Offset;
for (kkk = 0; kkk < bh.Num; kkk++, p += kCmdSize)
{
UInt32 flg = labels[kkk];
/*
if (IsFunc(flg))
{
AddLF();
for (int i = 0; i < 14; i++)
{
UInt32 commandId = GetCmd(Get32(p + kCmdSize * i));
s += ", ";
UIntToString(s, commandId);
}
AddLF();
}
*/
if (IsFunc(flg)
&& bh.Num - kkk >= ARRAY_SIZE(k_InitPluginDir_Commands)
&& CompareCommands(p, k_InitPluginDir_Commands, ARRAY_SIZE(k_InitPluginDir_Commands)))
{
InitPluginsDir_Start = kkk;
InitPluginsDir_End = kkk + ARRAY_SIZE(k_InitPluginDir_Commands);
labels[kkk] |= CMD_REF_InitPluginDir;
break;
}
}
#endif
// AString prefixA_Temp;
// UString prefixU_Temp;
// const UInt32 kFindS = 158;
#ifdef NSIS_SCRIPT
UInt32 curSectionIndex = 0;
// UInt32 lastSectionEndCmd = 0xFFFFFFFF;
bool sectionIsOpen = false;
// int curOnFunc = 0;
bool onFuncIsOpen = false;
/*
for (unsigned yyy = 0; yyy + 3 < _data.Size(); yyy++)
{
UInt32 val = Get32(_data + yyy);
if (val == kFindS)
val = val;
}
*/
UInt32 overwrite_State = 0; // "SetOverwrite on"
Int32 allowSkipFiles_State = -1; // -1: on, -2: off, >=0 : RAW value
UInt32 endCommentIndex = 0;
unsigned numSupportedCommands = GetNumSupportedCommands();
#endif
p = _data + bh.Offset;
UString spec_outdir_U;
AString spec_outdir_A;
UPrefixes.Add(L"$INSTDIR");
APrefixes.Add("$INSTDIR");
p = _data + bh.Offset;
unsigned spec_outdir_VarIndex = IsNsis225 ?
kVar_Spec_OUTDIR_225 :
kVar_Spec_OUTDIR;
for (kkk = 0; kkk < bh.Num; kkk++, p += kCmdSize)
{
UInt32 commandId;
UInt32 params[kNumCommandParams];
commandId = GetCmd(Get32(p));
{
for (unsigned i = 0; i < kNumCommandParams; i++)
{
params[i] = Get32(p + 4 + 4 * i);
/*
if (params[i] == kFindS)
i = i;
*/
}
}
#ifdef NSIS_SCRIPT
bool IsSectionGroup = false;
while (curSectionIndex < bhSections.Num)
{
const CSection § = Sections[curSectionIndex];
if (sectionIsOpen)
{
if (sect.StartCmdIndex + sect.NumCommands + 1 != kkk)
break;
PrintSectionEnd();
sectionIsOpen = false;
// lastSectionEndCmd = kkk;
curSectionIndex++;
continue;
}
if (sect.StartCmdIndex != kkk)
break;
if (PrintSectionBegin(sect, curSectionIndex))
{
IsSectionGroup = true;
curSectionIndex++;
// do we need to flush prefixes in new section?
// FlushOutPathPrefixes();
}
else
sectionIsOpen = true;
}
/*
if (curOnFunc < OnFuncs.Size())
{
if ((OnFuncs[curOnFunc] >> kOnFuncShift) == kkk)
{
s += "Function .on";
s += kOnFunc[OnFuncs[curOnFunc++] & ((1 << kOnFuncShift) - 1)];
AddLF();
onFuncIsOpen = true;
}
}
*/
if (labels[kkk] != 0 && labels[kkk] != CMD_REF_Section)
{
UInt32 flg = labels[kkk];
if (IsFunc(flg))
{
if ((int)kkk == InitPluginsDir_Start)
CommentOpen();
onFuncIsOpen = true;
s += "Function ";
Add_FuncName(labels, kkk);
if (IsPageFunc(flg))
{
BigSpaceComment();
s += "Page ";
Add_UInt((flg & CMD_REF_Page_Mask) >> CMD_REF_Page_NumShifts);
// if (flg & CMD_REF_Creator) s += ", Creator";
if (flg & CMD_REF_Leave) s += ", Leave";
if (flg & CMD_REF_Pre) s += ", Pre";
if (flg & CMD_REF_Show) s += ", Show";
}
AddLF();
}
if (flg & CMD_REF_Goto)
{
Add_LabelName(kkk);
s += ':';
AddLF();
}
}
if (commandId != EW_RET)
{
Tab(kkk < endCommentIndex);
}
/*
UInt32 originalCmd = Get32(p);
if (originalCmd >= EW_REGISTERDLL)
{
UIntToString(s, originalCmd);
s += ' ';
if (originalCmd != commandId)
{
UIntToString(s, commandId);
s += ' ';
}
}
*/
unsigned numSkipParams = 0;
if (commandId < ARRAY_SIZE(k_Commands) && commandId < numSupportedCommands)
{
numSkipParams = k_Commands[commandId].NumParams;
const char *sz = k_CommandNames[commandId];
if (sz)
s += sz;
}
else
{
s += "Command";
Add_UInt(commandId);
/* We don't show wrong commands that use overlapped ids.
So we change commandId to big value */
if (commandId < (1 << 12))
commandId += (1 << 12);
}
#endif
switch (commandId)
{
case EW_CREATEDIR:
{
bool isSetOutPath = (params[1] != 0);
if (isSetOutPath)
{
UInt32 par0 = params[0];
UInt32 resOffset;
Int32 idx = GetVarIndex(par0, resOffset);
if (idx == (Int32)spec_outdir_VarIndex ||
idx == kVar_OUTDIR)
par0 += resOffset;
ReadString2_Raw(par0);
if (IsUnicode)
{
if (idx == (Int32)spec_outdir_VarIndex)
Raw_UString.Insert(0, spec_outdir_U);
else if (idx == kVar_OUTDIR)
Raw_UString.Insert(0, UPrefixes.Back());
UPrefixes.Add(Raw_UString);
}
else
{
if (idx == (Int32)spec_outdir_VarIndex)
Raw_AString.Insert(0, spec_outdir_A);
else if (idx == kVar_OUTDIR)
Raw_AString.Insert(0, APrefixes.Back());
APrefixes.Add(Raw_AString);
}
}
#ifdef NSIS_SCRIPT
s += isSetOutPath ? "SetOutPath" : "CreateDirectory";
AddParam(params[0]);
#endif
break;
}
case EW_ASSIGNVAR:
{
if (params[0] == spec_outdir_VarIndex)
{
spec_outdir_U.Empty();
spec_outdir_A.Empty();
if (IsVarStr(params[1], kVar_OUTDIR) &&
params[2] == 0 &&
params[3] == 0)
{
if (IsVarStr(params[1], kVar_OUTDIR))
{
spec_outdir_U = UPrefixes.Back(); // outdir_U;
spec_outdir_A = APrefixes.Back();// outdir_A;
}
}
}
#ifdef NSIS_SCRIPT
if (params[2] == 0 &&
params[3] == 0 &&
params[4] == 0 &&
params[5] == 0 &&
params[1] != 0 &&
params[1] < NumStringChars)
{
char sz[16];
ConvertUInt32ToString(kkk + 1, sz);
if (IsDirectString_Equal(params[1], sz))
{
// we suppose that it's GetCurrentAddress command
// but there is probability that it's StrCpy command
s += "GetCurrentAddress";
AddParam_Var(params[0]);
SmallSpaceComment();
}
}
s += "StrCpy";
AddParam_Var(params[0]);
AddParam(params[1]);
AddOptionalParams(params + 2, 2);
#endif
break;
}
case EW_EXTRACTFILE:
{
CItem &item = Items.AddNew();
UInt32 par1 = params[1];
SetItemName(item, par1);
item.Pos = params[2];
item.MTime.dwLowDateTime = params[3];
item.MTime.dwHighDateTime = params[4];
#ifdef NSIS_SCRIPT
{
UInt32 overwrite = params[0] & 0x7;
if (overwrite != overwrite_State)
{
s += "SetOverwrite ";
ADD_TYPE_FROM_LIST(k_SetOverwrite_Modes, overwrite);
overwrite_State = overwrite;
AddLF();
Tab(kkk < endCommentIndex);
}
}
{
UInt32 nsisMB = params[0] >> 3;
if ((Int32)nsisMB != allowSkipFiles_State)
{
UInt32 mb = nsisMB & ((1 << 20) - 1); // old/new NSIS
UInt32 b1 = nsisMB >> 21; // NSIS 2.06+
UInt32 b2 = nsisMB >> 20; // NSIS old
Int32 asf = (Int32)nsisMB;
if (mb == (MY__MB_ABORTRETRYIGNORE | MY__MB_ICONSTOP) && (b1 == MY__IDIGNORE || b2 == MY__IDIGNORE))
asf = -1;
else if (mb == (MY__MB_RETRYCANCEL | MY__MB_ICONSTOP) && (b1 == MY__IDCANCEL || b2 == MY__IDCANCEL))
asf = -2;
else
{
AddCommentAndString("AllowSkipFiles [Overwrite]: ");
MessageBox_MB_Part(mb);
if (b1 != 0)
{
s += " /SD";
Add_ButtonID(b1);
}
}
if (asf != allowSkipFiles_State)
{
if (asf < 0)
{
s += "AllowSkipFiles ";
s += (asf == -1) ? "on" : "off";
}
AddLF();
Tab(kkk < endCommentIndex);
}
allowSkipFiles_State = (Int32)nsisMB;
}
}
s += "File";
AddParam(params[1]);
/* params[5] contains link to LangString (negative value)
with NLF_FILE_ERROR or NLF_FILE_ERROR_NOIGNORE message for MessageBox.
We don't need to print it. */
#endif
if (IsVarStr(par1, 10)) // is $R0
{
// we parse InstallLib macro in 7-Zip installers
unsigned kBackOffset = 28;
if (kkk > 1)
{
// detect old version of InstallLib macro
if (Get32(p - 1 * kCmdSize) == EW_NOP) // goto command
kBackOffset -= 2;
}
if (kkk > kBackOffset)
{
const Byte *p2 = p - kBackOffset * kCmdSize;
UInt32 cmd = Get32(p2);
if (cmd == EW_ASSIGNVAR)
{
UInt32 pars[6];
for (int i = 0; i < 6; i++)
pars[i] = Get32(p2 + i * 4 + 4);
if (pars[0] == 10 + 4 && pars[2] == 0 && pars[3] == 0) // 10 + 4 means $R4
{
item.Prefix = -1;
item.NameA.Empty();
item.NameU.Empty();
SetItemName(item, pars[1]);
// maybe here we can restore original item name, if new name is empty
}
}
}
}
/* UInt32 allowIgnore = params[5]; */
break;
}
case EW_SETFILEATTRIBUTES:
{
if (kkk > 0 && Get32(p - kCmdSize) == EW_EXTRACTFILE)
{
if (params[0] == Get32(p - kCmdSize + 4 + 4 * 1)) // compare with PrevCmd.Params[1]
{
CItem &item = Items.Back();
item.Attrib_Defined = true;
item.Attrib = params[1];
}
}
#ifdef NSIS_SCRIPT
AddParam(params[0]);
Space();
FlagsToString2(s, g_WinAttrib, ARRAY_SIZE(g_WinAttrib), params[1]);
#endif
break;
}
case EW_WRITEUNINSTALLER:
{
/* NSIS 2.29+ writes alternative path to params[3]
"$INSTDIR\\" + Str(params[0])
NSIS installer uses alternative path, if main path
from params[0] is not absolute path */
bool pathOk = (params[0] > 0) && IsGoodString(params[0]);
if (!pathOk)
{
#ifdef NSIS_SCRIPT
AddError("bad path");
#endif
break;
}
bool altPathOk = true;
UInt32 altParam = params[3];
if (altParam != 0)
{
altPathOk = false;
UInt32 additional = 0;
if (GetVarIndexFinished(altParam, '\\', additional) == kVar_INSTDIR)
altPathOk = AreTwoParamStringsEqual(altParam + additional, params[0]);
}
#ifdef NSIS_SCRIPT
AddParam(params[0]);
/*
for (int i = 1; i < 3; i++)
AddParam_UInt(params[i]);
*/
if (params[3] != 0)
{
SmallSpaceComment();
AddParam(params[3]);
}
#endif
if (!altPathOk)
{
#ifdef NSIS_SCRIPT
AddError("alt path error");
#endif
}
if (BadCmd >= 0 && BadCmd <= EW_WRITEUNINSTALLER)
{
/* We don't cases with incorrect installer commands.
Such bad installer item can break unpacking for other items. */
#ifdef NSIS_SCRIPT
AddError("SKIP possible BadCmd");
#endif
break;
}
CItem &item = Items.AddNew();;
SetItemName(item, params[0]);
item.Pos = params[1];
item.PatchSize = params[2];
item.IsUninstaller = true;
/*
// we can add second time to test the code
CItem item2 = item;
item2.NameU += L'2';
item2.NameA += '2';
Items.Add(item2);
*/
break;
}
#ifdef NSIS_SCRIPT
case EW_RET:
{
// bool needComment = false;
if (onFuncIsOpen)
{
if (kkk == bh.Num - 1 || IsProbablyEndOfFunc(labels[kkk + 1]))
{
AddStringLF("FunctionEnd");
if ((int)kkk + 1 == InitPluginsDir_End)
CommentClose();
AddLF();
onFuncIsOpen = false;
// needComment = true;
break;
}
}
// if (!needComment)
if (IsSectionGroup)
break;
if (sectionIsOpen)
{
const CSection § = Sections[curSectionIndex];
if (sect.StartCmdIndex + sect.NumCommands == kkk)
{
PrintSectionEnd();
sectionIsOpen = false;
curSectionIndex++;
break;
}
// needComment = true;
// break;
}
/*
if (needComment)
s += " ;";
*/
TabString("Return");
AddLF();
break;
}
case EW_NOP:
{
if (params[0] == 0)
s += "Nop";
else
{
s += "Goto";
Add_GotoVar(params[0]);
}
break;
}
case EW_ABORT:
{
AddOptionalParam(params[0]);
break;
}
case EW_CALL:
{
if (kkk + 1 < bh.Num && GetCmd(Get32(p + kCmdSize)) == EW_EXTRACTFILE)
{
UInt32 par1 = GET_CMD_PARAM(p + kCmdSize, 1);
UInt32 pluginPar = 0;
if (GetVarIndexFinished(par1, '\\', pluginPar) == kVar_PLUGINSDIR)
{
pluginPar += par1;
UInt32 commandId2 = GetCmd(Get32(p + kCmdSize * 2));
if (commandId2 == EW_SETFLAG || commandId2 == EW_UPDATETEXT)
{
UInt32 i;
for (i = kkk + 3; i < bh.Num; i++)
{
const Byte *pCmd = p + kCmdSize * (i - kkk);
UInt32 commandId3 = GetCmd(Get32(pCmd));
if (commandId3 != EW_PUSHPOP
|| GET_CMD_PARAM(pCmd, 1) != 0
|| GET_CMD_PARAM(pCmd, 2) != 0)
break;
}
if (i < bh.Num)
{
const Byte *pCmd = p + kCmdSize * (i - kkk);
// UInt32 callDll_Param = GET_CMD_PARAM(pCmd, 0);
// UInt32 file_Param = GET_CMD_PARAM(p + kCmdSize, 1);
if (GetCmd(Get32(pCmd)) == EW_REGISTERDLL &&
AreTwoParamStringsEqual(
GET_CMD_PARAM(pCmd, 0),
GET_CMD_PARAM(p + kCmdSize, 1)))
{
// params[4] = 1 means GetModuleHandle attempt before default LoadLibraryEx;
/// new versions of NSIS use params[4] = 1 for Plugin command
if (GET_CMD_PARAM(pCmd, 2) == 0
// && GET_CMD_PARAM(pCmd, 4) != 0
)
{
{
AString s2;
ReadString2(s2, pluginPar);
if (s2.Len() >= 4 &&
StringsAreEqualNoCase_Ascii(s2.RightPtr(4), ".dll"))
s2.DeleteFrom(s2.Len() - 4);
s2 += "::";
AString func;
ReadString2(func, GET_CMD_PARAM(pCmd, 1));
s2 += func;
Add_QuStr(s2);
if (GET_CMD_PARAM(pCmd, 3) == 1)
s += " /NOUNLOAD";
for (UInt32 j = i - 1; j >= kkk + 3; j--)
{
const Byte *pCmd = p + kCmdSize * (j - kkk);
AddParam(GET_CMD_PARAM(pCmd, 0));
}
NewLine();
Tab(true);
endCommentIndex = i + 1;
}
}
}
}
}
}
}
{
const Byte *nextCmd = p + kCmdSize;
UInt32 commandId2 = GetCmd(Get32(nextCmd));
if (commandId2 == EW_SETFLAG
&& GET_CMD_PARAM(nextCmd, 0) == k_ExecFlags_DetailsPrint
&& GET_CMD_PARAM(nextCmd, 2) != 0) // is "lastused"
// || commandId2 == EW_UPDATETEXT)
{
if ((Int32)params[0] > 0 && labels[params[0] - 1] & CMD_REF_InitPluginDir)
{
s += "InitPluginsDir";
AddLF();
Tab(true);
endCommentIndex = kkk + 2;
}
}
}
s += "Call ";
if ((Int32)params[0] < 0)
Add_Var(-((Int32)params[0] + 1));
else if (params[0] == 0)
s += '0';
else
{
UInt32 val = params[0] - 1;
if (params[1] == 1) // it's Call :Label
{
s += ':';
Add_LabelName(val);
}
else // if (params[1] == 0) // it's Call Func
Add_FuncName(labels, val);
}
break;
}
case EW_UPDATETEXT:
case EW_SLEEP:
{
AddParam(params[0]);
break;
}
case EW_CHDETAILSVIEW:
{
if (params[0] == MY__SW_SHOWNA && params[1] == MY__SW_HIDE) s += " show";
else if (params[1] == MY__SW_SHOWNA && params[0] == MY__SW_HIDE) s += " hide";
else
for (int i = 0; i < 2; i++)
{
Space();
Add_ShowWindow_Cmd(params[i]);
}
break;
}
case EW_IFFILEEXISTS:
{
AddParam(params[0]);
Add_GotoVars2(¶ms[1]);
break;
}
case EW_SETFLAG:
{
AString temp;
ReadString2(temp, params[1]);
if (params[0] == k_ExecFlags_Errors && params[2] == 0)
{
s += (temp.Len() == 1 && temp[0] == '0') ? "ClearErrors" : "SetErrors";
break;
}
s += "Set";
Add_ExecFlags(params[0]);
if (params[2] != 0)
{
s += " lastused";
break;
}
UInt32 v;
if (StringToUInt32(temp, v))
{
const char *s2 = NULL;
switch (params[0])
{
case k_ExecFlags_AutoClose:
case k_ExecFlags_RebootFlag:
if (v < 2) s2 = (v == 0) ? "false" : "true"; break;
case k_ExecFlags_ShellVarContext:
if (v < 2) s2 = (v == 0) ? "current" : "all"; break;
case k_ExecFlags_Silent:
if (v < 2) s2 = (v == 0) ? "normal" : "silent"; break;
case k_ExecFlags_RegView:
if (v == 0) s2 = "32";
else if (v == 256) s2 = "64";
break;
case k_ExecFlags_DetailsPrint:
if (v == 0) s2 = "both";
else if (v == 2) s2 = "textonly";
else if (v == 4) s2 = "listonly";
else if (v == 6) s2 = "none";
}
if (s2)
{
s += ' ';
s += s2;
break;
}
}
SpaceQuStr(temp);
break;
}
case EW_IFFLAG:
{
Add_ExecFlags(params[2]);
Add_GotoVars2(¶ms[0]);
/*
static const unsigned kIfErrors = 2;
if (params[2] != kIfErrors && params[3] != 0xFFFFFFFF ||
params[2] == kIfErrors && params[3] != 0)
{
s += " # FLAG &= ";
AddParam_UInt(params[3]);
}
*/
break;
}
case EW_GETFLAG:
{
Add_ExecFlags(params[1]);
AddParam_Var(params[0]);
break;
}
case EW_RENAME:
{
if (params[2] != 0)
s += k_REBOOTOK;
AddParams(params, 2);
if (params[3] != 0)
{
SmallSpaceComment();
AddParam(params[3]); // rename comment for log file
}
break;
}
case EW_GETFULLPATHNAME:
{
if (params[2] == 0)
s += " /SHORT";
AddParam_Var(params[1]);
AddParam(params[0]);
break;
}
case EW_SEARCHPATH:
case EW_STRLEN:
{
AddParam_Var(params[0]);
AddParam(params[1]);
break;
}
case EW_GETTEMPFILENAME:
{
AddParam_Var(params[0]);
AString temp;
ReadString2(temp, params[1]);
if (temp != "$TEMP")
SpaceQuStr(temp);
break;
}
case EW_DELETEFILE:
{
UInt32 flag = params[1];
if ((flag & DEL_REBOOT) != 0)
s += k_REBOOTOK;
AddParam(params[0]);
break;
}
case EW_MESSAGEBOX:
{
MessageBox_MB_Part(params[0]);
AddParam(params[1]);
{
UInt32 buttonID = (params[0] >> 21); // NSIS 2.06+
if (buttonID != 0)
{
s += " /SD";
Add_ButtonID(buttonID);
}
}
for (int i = 2; i < 6; i += 2)
if (params[i] != 0)
{
Add_ButtonID(params[i]);
Add_GotoVar1(params[i + 1]);
}
break;
}
case EW_RMDIR:
{
UInt32 flag = params[1];
if ((flag & DEL_RECURSE) != 0)
s += " /r";
if ((flag & DEL_REBOOT) != 0)
s += k_REBOOTOK;
AddParam(params[0]);
break;
}
case EW_STRCMP:
{
if (params[4] != 0)
s += 'S';
AddParams(params, 2);
Add_GotoVars2(¶ms[2]);
break;
}
case EW_READENVSTR:
{
s += (params[2] != 0) ?
"ReadEnvStr" :
"ExpandEnvStrings";
AddParam_Var(params[0]);
AString temp;
ReadString2(temp, params[1]);
if (params[2] != 0 &&temp.Len() >= 2 && temp[0] == '%' && temp.Back() == '%')
{
temp.DeleteBack();
temp.Delete(0);
}
SpaceQuStr(temp);
break;
}
case EW_INTCMP:
{
if (params[5] != 0)
s += 'U';
AddParams(params, 2);
Add_GotoVar1(params[2]);
if (params[3] != 0 || params[4] != 0)
Add_GotoVars2(params + 3);
break;
}
case EW_INTOP:
{
AddParam_Var(params[0]);
const char *kOps = "+-*/|&^!|&%<>"; // NSIS 2.01+
// "+-*/|&^!|&%"; // NSIS 2.0b4+
// "+-*/|&^~!|&%"; // NSIS old
UInt32 opIndex = params[3];
char c = (opIndex < 13) ? kOps[opIndex] : '?';
char c2 = (opIndex < 8 || opIndex == 10) ? (char)0 : c;
int numOps = (opIndex == 7) ? 1 : 2;
AddParam(params[1]);
if (numOps == 2 && c == '^' && IsDirectString_Equal(params[2], "0xFFFFFFFF"))
s += " ~ ;";
Space();
s += c;
if (numOps != 1)
{
if (c2 != 0)
s += c2;
AddParam(params[2]);
}
break;
}
case EW_INTFMT:
{
AddParam_Var(params[0]);
AddParams(params + 1, 2);
break;
}
case EW_PUSHPOP:
{
if (params[2] != 0)
{
s += "Exch";
if (params[2] != 1)
AddParam_UInt(params[2]);
}
else if (params[1] != 0)
{
s += "Pop";
AddParam_Var(params[0]);
}
else
{
if (NoLabels(labels + kkk + 1, 2)
&& Get32(p + kCmdSize) == EW_PUSHPOP // Exch"
&& GET_CMD_PARAM(p + kCmdSize, 2) == 1
&& Get32(p + kCmdSize * 2) == EW_PUSHPOP // Pop $VAR
&& GET_CMD_PARAM(p + kCmdSize * 2, 1) != 0)
{
if (IsVarStr(params[0], GET_CMD_PARAM(p + kCmdSize * 2, 0)))
{
s += "Exch";
AddParam(params[0]);
NewLine();
Tab(true);
endCommentIndex = kkk + 3;
}
}
s += "Push";
AddParam(params[0]);
}
break;
}
case EW_FINDWINDOW:
{
AddParam_Var(params[0]);
AddParam(params[1]);
AddOptionalParams(params + 2, 3);
break;
}
case EW_SENDMESSAGE:
{
// SendMessage: 6 [output, hwnd, msg, wparam, lparam, [wparamstring?1:0 | lparamstring?2:0 | timeout<<2]
AddParam(params[1]);
const char *w = NULL;
AString t;
ReadString2(t, params[2]);
UInt32 wm;
if (StringToUInt32(t, wm))
{
switch (wm)
{
case 0x0C: w = "SETTEXT"; break;
case 0x10: w = "CLOSE"; break;
case 0x30: w = "SETFONT"; break;
}
}
if (w)
{
s += " ${WM_";
s += w;
s += '}';
}
else
SpaceQuStr(t);
UInt32 spec = params[5];
for (unsigned i = 0; i < 2; i++)
{
AString s2;
if (spec & ((UInt32)1 << i))
s2 += "STR:";
ReadString2(s2, params[3 + i]);
SpaceQuStr(s2);
}
if ((Int32)params[0] >= 0)
AddParam_Var(params[0]);
spec >>= 2;
if (spec != 0)
{
s += " /TIMEOUT=";
Add_UInt(spec);
}
break;
}
case EW_ISWINDOW:
{
AddParam(params[0]);
Add_GotoVars2(¶ms[1]);
break;
}
case EW_GETDLGITEM:
{
AddParam_Var(params[0]);
AddParams(params + 1, 2);
break;
}
case EW_SETCTLCOLORS:
{
AddParam(params[0]);
UInt32 offset = params[1];
if (_size < bhCtlColors.Offset
|| _size - bhCtlColors.Offset < offset
|| _size - bhCtlColors.Offset - offset < k_CtlColors_Size)
{
AddError("bad offset");
break;
}
const Byte *p2 = _data + bhCtlColors.Offset + offset;
CNsis_CtlColors colors;
colors.Parse(p2);
if ((colors.flags & kColorsFlags_BK_SYS) != 0 ||
(colors.flags & kColorsFlags_TEXT_SYS) != 0)
s += " /BRANDING";
AString bk;
bool bkc = false;
if (colors.bkmode == MY__TRANSPARENT)
bk += " transparent";
else if (colors.flags & kColorsFlags_BKB)
{
if ((colors.flags & kColorsFlags_BK_SYS) == 0 &&
(colors.flags & kColorsFlags_BK) != 0)
bkc = true;
}
if ((colors.flags & kColorsFlags_TEXT) != 0 || !bk.IsEmpty() || bkc)
{
Space();
if ((colors.flags & kColorsFlags_TEXT_SYS) != 0 || (colors.flags & kColorsFlags_TEXT) == 0)
AddQuotes();
else
Add_Color(colors.text);
}
s += bk;
if (bkc)
{
Space();
Add_Color(colors.bkc);
}
break;
}
case EW_SETBRANDINGIMAGE:
{
s += " /IMGID=";
Add_UInt(params[1]);
if (params[2] == 1)
s += " /RESIZETOFIT";
AddParam(params[0]);
break;
}
case EW_CREATEFONT:
{
AddParam_Var(params[0]);
AddParam(params[1]);
AddOptionalParams(params + 2, 2);
if (params[4] & 1) s += " /ITALIC";
if (params[4] & 2) s += " /UNDERLINE";
if (params[4] & 4) s += " /STRIKE";
break;
}
case EW_SHOWWINDOW:
{
AString hw, sw;
ReadString2(hw, params[0]);
ReadString2(sw, params[1]);
if (params[3] != 0)
s += "EnableWindow";
else
{
UInt32 val;
bool valDefined = false;
if (StringToUInt32(sw, val))
{
if (val < ARRAY_SIZE(kShowWindow_Commands))
{
sw.Empty();
sw += "${";
Add_ShowWindow_Cmd_2(sw, val);
sw += '}';
valDefined = true;
}
}
bool isHwndParent = IsVarStr(params[0], IsNsis225 ? kVar_HWNDPARENT_225 : kVar_HWNDPARENT);
if (params[2] != 0)
{
if (valDefined && val == 0 && isHwndParent)
{
s += "HideWindow";
break;
}
}
if (valDefined && val == 5 && isHwndParent &&
kkk + 1 < bh.Num && GetCmd(Get32(p + kCmdSize)) == EW_BRINGTOFRONT)
{
s += " ; ";
}
s += "ShowWindow";
}
SpaceQuStr(hw);
SpaceQuStr(sw);
break;
}
case EW_SHELLEXEC:
{
AddParams(params, 2);
if (params[2] != 0 || params[3] != MY__SW_SHOWNORMAL)
{
AddParam(params[2]);
if (params[3] != MY__SW_SHOWNORMAL)
{
Space();
Add_ShowWindow_Cmd(params[3]);
}
}
if (params[5] != 0)
{
s += " ;";
AddParam(params[5]); // it's tatus text update
}
break;
}
case EW_EXECUTE:
{
if (params[2] != 0)
s += "Wait";
AddParam(params[0]);
if (params[2] != 0)
if ((Int32)params[1] >= 0)
AddParam_Var(params[1]);
break;
}
case EW_GETFILETIME:
case EW_GETDLLVERSION:
{
AddParam(params[2]);
AddParam_Var(params[0]);
AddParam_Var(params[1]);
break;
}
case EW_REGISTERDLL:
{
AString func;
ReadString2(func, params[1]);
bool printFunc = true;
// params[4] = 1; for plugin command
if (params[2] == 0)
{
s += "CallInstDLL";
AddParam(params[0]);
if (params[3] == 1)
s += " /NOUNLOAD";
}
else
{
if (func == "DllUnregisterServer")
{
s += "UnRegDLL";
printFunc = false;
}
else
{
s += "RegDLL";
if (func == "DllRegisterServer")
printFunc = false;
}
AddParam(params[0]);
}
if (printFunc)
SpaceQuStr(func);
break;
}
case EW_CREATESHORTCUT:
{
unsigned numParams;
for (numParams = 6; numParams > 2; numParams--)
if (params[numParams - 1] != 0)
break;
UInt32 spec = params[4];
if (spec & 0x8000) // NSIS 3.0b0
s += " /NoWorkingDir";
AddParams(params, numParams > 4 ? 4 : numParams);
if (numParams <= 4)
break;
UInt32 icon = (spec & 0xFF);
Space();
if (icon != 0)
Add_UInt(icon);
else
AddQuotes();
if ((spec >> 8) == 0 && numParams < 6)
break;
UInt32 sw = (spec >> 8) & 0x7F;
Space();
// NSIS encoder replaces these names:
if (sw == MY__SW_SHOWMINNOACTIVE)
sw = MY__SW_SHOWMINIMIZED;
if (sw == 0)
AddQuotes();
else
Add_ShowWindow_Cmd(sw);
UInt32 modKey = spec >> 24;
UInt32 key = (spec >> 16) & 0xFF;
if (modKey == 0 && key == 0)
{
if (numParams < 6)
break;
Space();
AddQuotes();
}
else
{
Space();
if (modKey & 1) s += "SHIFT|"; // HOTKEYF_SHIFT
if (modKey & 2) s += "CONTROL|";
if (modKey & 4) s += "ALT|";
if (modKey & 8) s += "EXT|";
static const unsigned kMy_VK_F1 = 0x70;
if (key >= kMy_VK_F1 && key <= kMy_VK_F1 + 23)
{
s += 'F';
Add_UInt(key - kMy_VK_F1 + 1);
}
else if (key >= 'A' && key <= 'Z' || key >= '0' && key <= '9')
s += (char)key;
else
{
s += "Char_";
Add_UInt(key);
}
}
AddOptionalParam(params[5]); // description
break;
}
case EW_COPYFILES:
{
if (params[2] & 0x04) s += " /SILENT"; // FOF_SILENT
if (params[2] & 0x80) s += " /FILESONLY"; // FOF_FILESONLY
AddParams(params, 2);
if (params[3] != 0)
{
s += " ;";
AddParam(params[3]); // status text update
}
break;
}
case EW_REBOOT:
{
if (params[0] != 0xbadf00d)
s += " ; Corrupted ???";
else if (kkk + 1 < bh.Num && GetCmd(Get32(p + kCmdSize)) == EW_QUIT)
endCommentIndex = kkk + 2;
break;
}
case EW_WRITEINI:
{
unsigned numAlwaysParams = 0;
if (params[0] == 0) // Section
s += "FlushINI";
else if (params[4] != 0)
{
s += "WriteINIStr";
numAlwaysParams = 3;
}
else
{
s += "DeleteINI";
s += (params[1] == 0) ? "Sec" : "Str";
numAlwaysParams = 1;
}
AddParam(params[3]); // filename
// Section, EntryName, Value
AddParams(params, numAlwaysParams);
AddOptionalParams(params + numAlwaysParams, 3 - numAlwaysParams);
break;
}
case EW_READINISTR:
{
AddParam_Var(params[0]);
AddParam(params[3]); // FileName
AddParams(params +1, 2); // Section, EntryName
break;
}
case EW_DELREG:
{
// NSIS 2.00 used another scheme!
if (params[4] == 0)
s += "Value";
else
{
s += "Key";
if (params[4] & 2)
s += " /ifempty";
}
AddRegRoot(params[1]);
AddParam(params[2]);
AddOptionalParam(params[3]);
break;
}
case EW_WRITEREG:
{
const char *s2 = 0;
switch (params[4])
{
case 1: s2 = "Str"; break;
case 2: s2 = "ExpandStr"; break; // maybe unused
case 3: s2 = "Bin"; break;
case 4: s2 = "DWORD"; break;
default:
s += '?';
Add_UInt(params[4]);
}
if (params[4] == 1 && params[5] == 2)
s2 = "ExpandStr";
if (s2)
s += s2;
AddRegRoot(params[0]);
AddParams(params + 1, 2); // keyName, valueName
if (params[4] != 3)
AddParam(params[3]); // value
else
{
// Binary data.
Space();
UInt32 offset = params[3];
bool isSupported = false;
if (AfterHeaderSize >= 4
&& bhData.Offset <= AfterHeaderSize - 4
&& offset <= AfterHeaderSize - 4 - bhData.Offset)
{
// we support it for solid archives.
const Byte *p2 = _afterHeader + bhData.Offset + offset;
UInt32 size = Get32(p2);
if (size <= AfterHeaderSize - 4 - bhData.Offset - offset)
{
for (UInt32 i = 0; i < size; i++)
{
Byte b = (p2 + 4)[i];
unsigned t;
t = (b >> 4); s += (char)(((t < 10) ? ('0' + t) : ('A' + (t - 10))));
t = (b & 15); s += (char)(((t < 10) ? ('0' + t) : ('A' + (t - 10))));
}
isSupported = true;
}
}
if (!isSupported)
{
// we must read from file here;
s += "data[";
Add_UInt(offset);
s += " ... ]";
s += " ; !!! Unsupported";
}
}
break;
}
case EW_READREGSTR:
{
s += (params[4] == 1) ? "DWORD" : "Str";
AddParam_Var(params[0]);
AddRegRoot(params[1]);
AddParams(params + 2, 2);
break;
}
case EW_REGENUM:
{
s += (params[4] != 0) ? "Key" : "Value";
AddParam_Var(params[0]);
AddRegRoot(params[1]);
AddParams(params + 2, 2);
break;
}
case EW_FCLOSE:
case EW_FINDCLOSE:
{
AddParam_Var(params[0]);
break;
}
case EW_FOPEN:
{
AddParam_Var(params[0]);
AddParam(params[3]);
UInt32 acc = params[1]; // dwDesiredAccess
UInt32 creat = params[2]; // dwCreationDisposition
if (acc == 0 && creat == 0)
break;
char cc = 0;
if (acc == MY__GENERIC_READ && creat == OPEN_EXISTING)
cc = 'r';
else if (creat == CREATE_ALWAYS && acc == MY__GENERIC_WRITE)
cc = 'w';
else if (creat == OPEN_ALWAYS && (acc == (MY__GENERIC_WRITE | MY__GENERIC_READ)))
cc = 'a';
// cc = 0;
if (cc != 0)
{
Space();
s += cc;
break;
}
if (acc & MY__GENERIC_READ) s += " GENERIC_READ";
if (acc & MY__GENERIC_WRITE) s += " GENERIC_WRITE";
if (acc & MY__GENERIC_EXECUTE) s += " GENERIC_EXECUTE";
if (acc & MY__GENERIC_ALL) s += " GENERIC_ALL";
const char *s2 = NULL;
switch (creat)
{
case MY__CREATE_NEW: s2 = "CREATE_NEW"; break;
case MY__CREATE_ALWAYS: s2 = "CREATE_ALWAYS"; break;
case MY__OPEN_EXISTING: s2 = "OPEN_EXISTING"; break;
case MY__OPEN_ALWAYS: s2 = "OPEN_ALWAYS"; break;
case MY__TRUNCATE_EXISTING: s2 = "TRUNCATE_EXISTING"; break;
}
Space();
if (s2)
s += s2;
else
Add_UInt(creat);
break;
}
case EW_FPUTS:
case EW_FPUTWS:
{
if (commandId == EW_FPUTWS)
s += (params[2] == 0) ? "UTF16LE" : "Word";
else if (params[2] != 0)
s += "Byte";
AddParam_Var(params[0]);
AddParam(params[1]);
break;
}
case EW_FGETS:
case EW_FGETWS:
{
if (commandId == EW_FPUTWS)
s += (params[3] == 0) ? "UTF16LE" : "Word";
if (params[3] != 0)
s += "Byte";
AddParam_Var(params[0]);
AddParam_Var(params[1]);
AString maxLenStr;
ReadString2(maxLenStr, params[2]);
UInt32 maxLen;
if (StringToUInt32(maxLenStr, maxLen))
{
if (maxLen == 1 && params[3] != 0)
break;
if (maxLen == 1023 && params[3] == 0) // NSIS_MAX_STRLEN - 1; can be other value!!
break;
}
SpaceQuStr(maxLenStr);
break;
}
case EW_FSEEK:
{
AddParam_Var(params[0]);
AddParam(params[2]);
if (params[3] == 1) s += " CUR"; // FILE_CURRENT
if (params[3] == 2) s += " END"; // FILE_END
if ((Int32)params[1] >= 0)
{
if (params[3] == 0) s += " SET"; // FILE_BEGIN
AddParam_Var(params[1]);
}
break;
}
case EW_FINDNEXT:
{
AddParam_Var(params[1]);
AddParam_Var(params[0]);
break;
}
case EW_FINDFIRST:
{
AddParam_Var(params[1]);
AddParam_Var(params[0]);
AddParam(params[2]);
break;
}
case EW_LOG:
{
if (params[0] != 0)
{
s += "Set ";
s += (params[1] == 0) ? "off" : "on";
}
else
{
s += "Text";
AddParam(params[1]);
}
}
case EW_SECTIONSET:
{
if ((Int32)params[2] >= 0)
{
s += "Get";
Add_SectOp(params[2]);
AddParam(params[0]);
AddParam_Var(params[1]);
}
else
{
s += "Set";
UInt32 t = -(Int32)params[2] - 1;
Add_SectOp(t);
AddParam(params[0]);
AddParam(params[t == 0 ? 4 : 1]);
// params[3] != 0 means call SectionFlagsChanged in installer
// used by SECTIONSETFLAGS command
}
break;
}
case EW_INSTTYPESET:
{
int numQwParams = 0;
const char *s2;
if (params[3] == 0)
{
if (params[2] == 0)
{
s2 = "InstTypeGetText";
numQwParams = 1;
}
else
{
s2 = "InstTypeSetText";
numQwParams = 2;
}
}
else
{
if (params[2] == 0)
s2 = "GetCurInstType";
else
{
s2 = "SetCurInstType";
numQwParams = 1;
}
}
s += s2;
AddParams(params, numQwParams);
if (params[2] == 0)
AddParam_Var(params[1]);
break;
}
case EW_LOCKWINDOW:
{
s += (params[0] == 0) ? " on" : " off";
break;
}
case EW_FINDPROC:
{
AddParam_Var(params[0]);
AddParam(params[1]);
break;
}
default:
{
numSkipParams = 0;
}
#endif
}
#ifdef NSIS_SCRIPT
unsigned numParams = kNumCommandParams;
for (; numParams > 0; numParams--)
if (params[numParams - 1] != 0)
break;
if (numParams > numSkipParams)
{
s += " ; !!!! Unknown Params: ";
unsigned i;
for (i = 0; i < numParams; i++)
AddParam(params[i]);
s += " ;";
for (i = 0; i < numParams; i++)
{
Space();
UInt32 v = params[i];
if (v > 0xFFF00000)
Add_SignedInt(s, (Int32)v);
else
Add_UInt(v);
}
}
NewLine();
#endif
}
#ifdef NSIS_SCRIPT
if (sectionIsOpen)
{
if (curSectionIndex < bhSections.Num)
{
const CSection § = Sections[curSectionIndex];
if (sect.StartCmdIndex + sect.NumCommands + 1 == kkk)
{
PrintSectionEnd();
sectionIsOpen = false;
// lastSectionEndCmd = kkk;
curSectionIndex++;
}
}
}
while (curSectionIndex < bhSections.Num)
{
const CSection § = Sections[curSectionIndex];
if (sectionIsOpen)
{
if (sect.StartCmdIndex + sect.NumCommands != kkk)
AddErrorLF("SECTION ERROR");
PrintSectionEnd();
sectionIsOpen = false;
curSectionIndex++;
}
else
{
if (curSectionIndex == 49)
curSectionIndex = curSectionIndex;
if (PrintSectionBegin(sect, curSectionIndex))
curSectionIndex++;
else
sectionIsOpen = true;
}
}
#endif
return S_OK;
}
static int CompareItems(void *const *p1, void *const *p2, void *param)
{
const CItem &i1 = **(CItem **)p1;
const CItem &i2 = **(CItem **)p2;
RINOZ(MyCompare(i1.Pos, i2.Pos));
const CInArchive *inArchive = (const CInArchive *)param;
if (inArchive->IsUnicode)
{
if (i1.Prefix != i2.Prefix)
{
if (i1.Prefix < 0) return -1;
if (i2.Prefix < 0) return 1;
RINOZ(wcscmp(
inArchive->UPrefixes[i1.Prefix],
inArchive->UPrefixes[i2.Prefix]));
}
RINOZ(wcscmp(i1.NameU, i2.NameU));
}
else
{
if (i1.Prefix != i2.Prefix)
{
if (i1.Prefix < 0) return -1;
if (i2.Prefix < 0) return 1;
RINOZ(strcmp(
inArchive->APrefixes[i1.Prefix],
inArchive->APrefixes[i2.Prefix]));
}
RINOZ(strcmp(i1.NameA, i2.NameA));
}
return 0;
}
HRESULT CInArchive::SortItems()
{
{
Items.Sort(CompareItems, (void *)this);
unsigned i;
for (i = 0; i + 1 < Items.Size(); i++)
{
const CItem &i1 = Items[i];
const CItem &i2 = Items[i + 1];
if (i1.Pos != i2.Pos)
continue;
if (IsUnicode)
{
if (i1.NameU != i2.NameU) continue;
if (i1.Prefix != i2.Prefix)
{
if (i1.Prefix < 0 || i2.Prefix < 0) continue;
if (UPrefixes[i1.Prefix] != UPrefixes[i2.Prefix]) continue;
}
}
else
{
if (i1.NameA != i2.NameA) continue;
if (i1.Prefix != i2.Prefix)
{
if (i1.Prefix < 0 || i2.Prefix < 0) continue;
if (APrefixes[i1.Prefix] != APrefixes[i2.Prefix]) continue;
}
}
Items.Delete(i + 1);
i--;
}
for (i = 0; i < Items.Size(); i++)
{
CItem &item = Items[i];
UInt32 curPos = item.Pos + 4;
for (unsigned nextIndex = i + 1; nextIndex < Items.Size(); nextIndex++)
{
UInt32 nextPos = Items[nextIndex].Pos;
if (curPos <= nextPos)
{
item.EstimatedSize_Defined = true;
item.EstimatedSize = nextPos - curPos;
break;
}
}
}
if (!IsSolid)
{
for (i = 0; i < Items.Size(); i++)
{
CItem &item = Items[i];
RINOK(_stream->Seek(GetPosOfNonSolidItem(i), STREAM_SEEK_SET, NULL));
const UInt32 kSigSize = 4 + 1 + 1 + 4; // size,[flag],prop,dict
BYTE sig[kSigSize];
size_t processedSize = kSigSize;
RINOK(ReadStream(_stream, sig, &processedSize));
if (processedSize < 4)
return S_FALSE;
UInt32 size = Get32(sig);
if ((size & kMask_IsCompressed) != 0)
{
item.IsCompressed = true;
size &= ~kMask_IsCompressed;
if (Method == NMethodType::kLZMA)
{
if (processedSize < 9)
return S_FALSE;
/*
if (FilterFlag)
item.UseFilter = (sig[4] != 0);
*/
item.DictionarySize = Get32(sig + 4 + 1 + (FilterFlag ? 1 : 0));
}
}
else
{
item.IsCompressed = false;
item.Size = size;
item.Size_Defined = true;
}
item.CompressedSize = size;
item.CompressedSize_Defined = true;
}
}
}
return S_OK;
}
// Flags for common_header.flags
#define CH_FLAGS_DETAILS_SHOWDETAILS 1
#define CH_FLAGS_DETAILS_NEVERSHOW 2
#define CH_FLAGS_PROGRESS_COLORED 4
#define CH_FLAGS_SILENT 8
#define CH_FLAGS_SILENT_LOG 16
#define CH_FLAGS_AUTO_CLOSE 32
#define CH_FLAGS_DIR_NO_SHOW 64 // unused now
#define CH_FLAGS_NO_ROOT_DIR 128
#define CH_FLAGS_COMP_ONLY_ON_CUSTOM 256
#define CH_FLAGS_NO_CUSTOM 512
static const char * const k_PostStrings[] =
{
"install_directory_auto_append"
, "uninstchild" // NSIS 2.25+, used by uninstaller:
, "uninstcmd" // NSIS 2.25+, used by uninstaller:
, "wininit" // NSIS 2.25+, used by move file on reboot
};
HRESULT CInArchive::Parse()
{
// UInt32 offset = ReadUInt32();
// ???? offset == FirstHeader.HeaderSize
const Byte *p = _data;
CBlockHeader bhEntries, bhStrings, bhLangTables;
bhEntries.Parse(p + 4 + 8 * 2);
bhStrings.Parse(p + 4 + 8 * 3);
bhLangTables.Parse(p + 4 + 8 * 4);
#ifdef NSIS_SCRIPT
CBlockHeader bhFont;
bhPages.Parse(p + 4 + 8 * 0);
bhSections.Parse(p + 4 + 8 * 1);
bhCtlColors.Parse(p + 4 + 8 * 5);
bhFont.Parse(p + 4 + 8 * 6);
bhData.Parse(p + 4 + 8 * 7);
#endif
_stringsPos = bhStrings.Offset;
if (_stringsPos > _size)
return S_FALSE;
{
if (bhLangTables.Offset < bhStrings.Offset)
return S_FALSE;
UInt32 stringTableSize = bhLangTables.Offset - bhStrings.Offset;
if (stringTableSize < 2)
return S_FALSE;
const Byte *strData = _data + _stringsPos;
if (strData[stringTableSize - 1] != 0)
return S_FALSE;
IsUnicode = (Get16(strData) == 0);
NumStringChars = stringTableSize;
if (IsUnicode)
{
if ((stringTableSize & 1) != 0)
return S_FALSE;
NumStringChars >>= 1;
if (strData[stringTableSize - 2] != 0)
return S_FALSE;
}
}
if (bhEntries.Num > (1 << 25))
return S_FALSE;
if (bhEntries.Offset > _size)
return S_FALSE;
if (bhEntries.Num * kCmdSize > _size - bhEntries.Offset)
return S_FALSE;
DetectNsisType(bhEntries, _data + bhEntries.Offset);
#ifdef NSIS_SCRIPT
{
AddCommentAndString("NSIS script");
if (IsUnicode)
Script += " (UTF-8)";
Space();
Script += GetFormatDescription();
AddLF();
}
{
AddCommentAndString(IsInstaller ? "Install" : "Uninstall");
AddLF();
}
AddLF();
if (IsUnicode)
AddStringLF("Unicode true");
if (Method != NMethodType::kCopy)
{
const char *m = NULL;
switch (Method)
{
case NMethodType::kDeflate: m = "zlib"; break;
case NMethodType::kBZip2: m = "bzip2"; break;
case NMethodType::kLZMA: m = "lzma"; break;
}
Script += "SetCompressor";
if (IsSolid)
Script += " /SOLID";
if (m)
{
Space();
Script += m;
}
AddLF();
}
if (Method == NMethodType::kLZMA)
{
// if (DictionarySize != (8 << 20))
{
Script += "SetCompressorDictSize";
AddParam_UInt(DictionarySize >> 20);
AddLF();
}
}
Separator();
PrintNumComment("HEADER SIZE", FirstHeader.HeaderSize);
// if (bhPages.Offset != 300 && bhPages.Offset != 288)
if (bhPages.Offset != 0)
{
PrintNumComment("START HEADER SIZE", bhPages.Offset);
}
if (bhSections.Num > 0)
{
if (bhEntries.Offset < bhSections.Offset)
return S_FALSE;
SectionSize = (bhEntries.Offset - bhSections.Offset) / bhSections.Num;
if (bhSections.Offset + bhSections.Num * SectionSize != bhEntries.Offset)
return S_FALSE;
if (SectionSize < kSectionSize_base)
return S_FALSE;
UInt32 maxStringLen = SectionSize - kSectionSize_base;
if (IsUnicode)
{
if ((maxStringLen & 1) != 0)
return S_FALSE;
maxStringLen >>= 1;
}
// if (maxStringLen != 1024)
{
if (maxStringLen == 0)
PrintNumComment("SECTION SIZE", SectionSize);
else
PrintNumComment("MAX STRING LENGTH", maxStringLen);
}
}
PrintNumComment("STRING CHARS", NumStringChars);
// PrintNumComment("LANG TABLE SIZE", bhCtlColors.Offset - bhLangTables.Offset);
if (bhCtlColors.Offset > _size)
AddErrorLF("Bad COLORS TABLE");
// PrintNumComment("COLORS TABLE SIZE", bhFont.Offset - bhCtlColors.Offset);
if (bhCtlColors.Num != 0)
PrintNumComment("COLORS Num", bhCtlColors.Num);
// bhData uses offset in _afterHeader (not in _data)
// PrintNumComment("FONT TABLE SIZE", bhData.Offset - bhFont.Offset);
if (bhFont.Num != 0)
PrintNumComment("FONTS Num", bhFont.Num);
// PrintNumComment("DATA SIZE", FirstHeader.HeaderSize - bhData.Offset);
if (bhData.Num != 0)
PrintNumComment("DATA NUM", bhData.Num);
AddLF();
AddStringLF("OutFile [NSIS].exe");
AddStringLF("!include WinMessages.nsh");
AddLF();
strUsed.Alloc(NumStringChars);
memset(strUsed, 0, NumStringChars);
{
UInt32 ehFlags = Get32(p);
UInt32 showDetails = ehFlags & 3;// CH_FLAGS_DETAILS_SHOWDETAILS & CH_FLAGS_DETAILS_NEVERSHOW;
if (showDetails >= 1 && showDetails <= 2)
{
Script += IsInstaller ? "ShowInstDetails" : "ShowUninstDetails";
Script += (showDetails == 1) ? " show" : " nevershow";
AddLF();
}
if (ehFlags & CH_FLAGS_PROGRESS_COLORED) AddStringLF("InstProgressFlags colored" );
if ((ehFlags & (CH_FLAGS_SILENT | CH_FLAGS_SILENT_LOG)) != 0)
{
Script += IsInstaller ? "SilentInstall " : "SilentUnInstall ";
Script += (ehFlags & CH_FLAGS_SILENT_LOG) ? "silentlog" : "silent";
AddLF();
}
if (ehFlags & CH_FLAGS_AUTO_CLOSE) AddStringLF("AutoCloseWindow true");
if ((ehFlags & CH_FLAGS_NO_ROOT_DIR) == 0) AddStringLF("AllowRootDirInstall true");
if (ehFlags & CH_FLAGS_NO_CUSTOM) AddStringLF("InstType /NOCUSTOM");
if (ehFlags & CH_FLAGS_COMP_ONLY_ON_CUSTOM) AddStringLF("InstType /COMPONENTSONLYONCUSTOM");
}
// Separator();
// AddLF();
Int32 licenseLangIndex = -1;
{
const Byte *pp = _data + bhPages.Offset;
for (UInt32 pageIndex = 0; pageIndex < bhPages.Num; pageIndex++, pp += kPageSize)
{
UInt32 wndProcID = Get32(pp + 4);
UInt32 param1 = Get32(pp + 44 + 4 * 1);
if (wndProcID != PWP_LICENSE || param1 == 0)
continue;
if ((Int32)param1 < 0)
licenseLangIndex = - ((Int32)param1 + 1);
else
noParseStringIndexes.AddToUniqueSorted(param1);
}
}
unsigned paramsOffset = 4 + 8 * 8;
if (bhPages.Offset == 276)
paramsOffset -= 8;
const Byte *p2 = p + paramsOffset;
{
UInt32 rootKey = Get32(p2); // (rootKey = -1) in uninstaller by default (the bug in NSIS)
UInt32 subKey = Get32(p2 + 4);
UInt32 value = Get32(p2 + 8);
if ((rootKey != 0 && rootKey != (UInt32)(Int32)-1) || subKey != 0 || value != 0)
{
Script += "InstallDirRegKey";
AddRegRoot(rootKey);
AddParam(subKey);
AddParam(value);
AddLF();
}
}
{
UInt32 bg_color1 = Get32(p2 + 12);
UInt32 bg_color2 = Get32(p2 + 16);
UInt32 bg_textcolor = Get32(p2 + 20);
if (bg_color1 != (UInt32)(Int32)-1 || bg_color2 != (UInt32)(Int32)-1 || bg_textcolor != (UInt32)(Int32)-1)
{
Script += "BGGradient";
if (bg_color1 != 0 || bg_color2 != 0xFF0000 || bg_textcolor != (UInt32)(Int32)-1)
{
Add_ColorParam(bg_color1);
Add_ColorParam(bg_color2);
if (bg_textcolor != (UInt32)(Int32)-1)
Add_ColorParam(bg_textcolor);
}
AddLF();
}
}
{
UInt32 lb_bg = Get32(p2 + 24);
UInt32 lb_fg = Get32(p2 + 28);
if ((lb_bg != (UInt32)(Int32)-1 || lb_fg != (UInt32)(Int32)-1) &&
(lb_bg != 0 || lb_fg != 0xFF00))
{
Script += "InstallColors";
Add_ColorParam(lb_fg);
Add_ColorParam(lb_bg);
AddLF();
}
}
UInt32 license_bg = Get32(p2 + 36);
if (license_bg != (UInt32)(Int32)-1 && license_bg != -15) // COLOR_BTNFACE
{
Script += "LicenseBkColor";
if ((Int32)license_bg == -5) // COLOR_WINDOW
Script += " /windows";
/*
else if ((Int32)license_bg == -15)
Script += " /grey";
*/
else
Add_ColorParam(license_bg);
AddLF();
}
UInt32 langtable_size = Get32(p2 + 32);
if (bhLangTables.Num > 0)
{
if (langtable_size == (UInt32)(Int32)-1)
return E_NOTIMPL; // maybe it's old NSIS archive()
UInt32 numStrings = (langtable_size - 10) / 4;
_numLangStrings = numStrings;
AddLF();
Separator();
PrintNumComment("LANG TABLES", bhLangTables.Num);
PrintNumComment("LANG STRINGS", numStrings);
AddLF();
if (licenseLangIndex >= 0)
{
for (UInt32 i = 0; i < bhLangTables.Num; i++)
{
const Byte *p = _data + bhLangTables.Offset + langtable_size * i;
LANGID langID = Get16(p);
UInt32 val = Get32(p + 10 + (UInt32)licenseLangIndex * 4);
if (val != 0)
{
Script += "LicenseLangString ";
Add_LangStr_Simple(licenseLangIndex);
AddParam_UInt(langID);
AddLicense(val, langID);
noParseStringIndexes.AddToUniqueSorted(val);
NewLine();
}
}
AddLF();
}
UInt32 brandingText = 0;
UInt32 caption = 0;
UInt32 name = 0;
UInt32 i;
for (i = 0; i < bhLangTables.Num; i++)
{
const Byte *p = _data + bhLangTables.Offset + langtable_size * i;
LANGID langID = Get16(p);
if (i == 0 || langID == 1033)
_mainLang = p + 10;
{
UInt32 v = Get32(p + 10 + 0 * 4);
if (v != 0 && (langID == 1033 || brandingText == 0))
brandingText = v;
}
{
UInt32 v = Get32(p + 10 + 1 * 4);
if (v != 0 && (langID == 1033 || caption == 0))
caption = v;
}
{
UInt32 v = Get32(p + 10 + 2 * 4);
if (v != 0 && (langID == 1033 || name == 0))
name = v;
}
}
if (name != 0)
{
Script += "Name";
AddParam(name);
NewLine();
ReadString2(Name, name);
}
/*
if (caption != 0)
{
Script += "Caption";
AddParam(caption);
NewLine();
}
*/
if (brandingText != 0)
{
Script += "BrandingText";
AddParam(brandingText);
NewLine();
ReadString2(BrandingText, brandingText);
}
for (i = 0; i < bhLangTables.Num; i++)
{
const Byte *p = _data + bhLangTables.Offset + langtable_size * i;
LANGID langID = Get16(p);
AddLF();
AddCommentAndString("LANG:");
AddParam_UInt(langID);
/*
Script += " (";
LangId_To_String(Script, langID);
Script += ')';
*/
AddLF();
// UInt32 dlg_offset = Get32(p + 2);
// UInt32 g_exec_flags_rtl = Get32(p + 6);
for (UInt32 j = 0; j < numStrings; j++)
{
UInt32 val = Get32(p + 10 + j * 4);
if (val != 0)
{
if ((Int32)j != licenseLangIndex)
{
Script += "LangString ";
Add_LangStr_Simple(j);
AddParam_UInt(langID);
AddParam(val);
AddLF();
}
}
}
AddLF();
}
ClearLangComment();
}
{
unsigned numInternalVars = GET_NUM_INTERNAL_VARS;
UInt32 numUsedVars = GetNumUsedVars();
if (numUsedVars > numInternalVars)
{
Separator();
PrintNumComment("VARIABLES", numUsedVars - numInternalVars);
AddLF();
AString temp;
for (UInt32 i = numInternalVars; i < numUsedVars; i++)
{
Script += "Var ";
temp.Empty();
GetVar2(temp, i);
AddStringLF(temp);
}
AddLF();
}
}
onFuncOffset = paramsOffset + 40;
numOnFunc = ARRAY_SIZE(kOnFunc);
if (bhPages.Offset == 276)
numOnFunc--;
p2 += 40 + numOnFunc * 4;
#define NSIS_MAX_INST_TYPES 32
AddLF();
UInt32 i;
for (i = 0; i < NSIS_MAX_INST_TYPES + 1; i++, p2 += 4)
{
UInt32 instType = Get32(p2);
if (instType != 0)
{
Script += "InstType";
AString s2;
if (!IsInstaller)
s2 += "un.";
ReadString2(s2, instType);
SpaceQuStr(s2);
NewLine();
}
}
{
UInt32 installDir = Get32(p2);
p2 += 4;
if (installDir != 0)
{
Script += "InstallDir";
AddParam(installDir);
NewLine();
}
}
if (bhPages.Offset >= 288)
for (i = 0; i < 4; i++)
{
if (i != 0 && bhPages.Offset < 300)
break;
UInt32 param = Get32(p2 + 4 * i);
if (param == 0 || param == (UInt32)(Int32)-1)
continue;
/*
uninstaller:
UInt32 uninstChild = Get32(p2 + 8); // "$TEMP\\$1u_.exe"
UInt32 uninstCmd = Get32(p2 + 12); // "\"$TEMP\\$1u_.exe\" $0 _?=$INSTDIR\\"
int str_wininit = Get32(p2 + 16); // "$WINDIR\\wininit.ini"
*/
AddCommentAndString(k_PostStrings[i]);
Script += " =";
AddParam(param);
NewLine();
}
AddLF();
#endif
RINOK(ReadEntries(bhEntries));
#ifdef NSIS_SCRIPT
Separator();
AddCommentAndString("UNREFERENCED STRINGS:");
AddLF();
AddLF();
CommentOpen();
for (i = 0; i < NumStringChars;)
{
if (!strUsed[i] && i != 0)
// Script += "!!! ";
{
Add_UInt(i);
AddParam(i);
NewLine();
}
if (IsUnicode)
i += GetUi16Str_Len((const Byte *)_data + _stringsPos + i * 2);
else
i += (UInt32)strlen((const char *)(const Byte *)_data + _stringsPos + i);
i++;
}
CommentClose();
#endif
return SortItems();
}
static bool IsLZMA(const Byte *p, UInt32 &dictionary)
{
dictionary = Get32(p + 1);
return (p[0] == 0x5D &&
p[1] == 0x00 && p[2] == 0x00 &&
p[5] == 0x00 && (p[6] & 0x80) == 0x00);
}
static bool IsLZMA(const Byte *p, UInt32 &dictionary, bool &thereIsFlag)
{
if (IsLZMA(p, dictionary))
{
thereIsFlag = false;
return true;
}
if (p[0] <= 1 && IsLZMA(p + 1, dictionary))
{
thereIsFlag = true;
return true;
}
return false;
}
static bool IsBZip2(const Byte *p)
{
return (p[0] == 0x31 && p[1] < 14);
}
HRESULT CInArchive::Open2(const Byte *sig, size_t size)
{
const UInt32 kSigSize = 4 + 1 + 5 + 2; // size, flag, 5 - lzma props, 2 - lzma first bytes
if (size < kSigSize)
return S_FALSE;
_headerIsCompressed = true;
IsSolid = true;
FilterFlag = false;
UseFilter = false;
DictionarySize = 1;
#ifdef NSIS_SCRIPT
AfterHeaderSize = 0;
#endif
UInt32 compressedHeaderSize = Get32(sig);
/*
XX XX XX XX XX XX XX XX == FirstHeader.HeaderSize, nonsolid, uncompressed
5D 00 00 dd dd 00 solid LZMA
00 5D 00 00 dd dd 00 solid LZMA, empty filter (there are no such archives)
01 5D 00 00 dd dd 00 solid LZMA, BCJ filter (only 7-Zip installer used that format)
SS SS SS 80 00 5D 00 00 dd dd 00 non-solid LZMA, empty filter
SS SS SS 80 01 5D 00 00 dd dd 00 non-solid LZMA, BCJ filte
SS SS SS 80 01 tt non-solid BZip (tt < 14
SS SS SS 80 non-solid deflate
01 tt solid BZip (tt < 14
other solid Deflate
*/
if (compressedHeaderSize == FirstHeader.HeaderSize)
{
_headerIsCompressed = false;
IsSolid = false;
Method = NMethodType::kCopy;
}
else if (IsLZMA(sig, DictionarySize, FilterFlag))
Method = NMethodType::kLZMA;
else if (sig[3] == 0x80)
{
IsSolid = false;
if (IsLZMA(sig + 4, DictionarySize, FilterFlag) && sig[3] == 0x80)
Method = NMethodType::kLZMA;
else if (IsBZip2(sig + 4))
Method = NMethodType::kBZip2;
else
Method = NMethodType::kDeflate;
}
else if (IsBZip2(sig))
Method = NMethodType::kBZip2;
else
Method = NMethodType::kDeflate;
if (IsSolid)
{
RINOK(_stream->Seek(DataStreamOffset, STREAM_SEEK_SET, NULL));
}
else
{
_headerIsCompressed = ((compressedHeaderSize & kMask_IsCompressed) != 0);
compressedHeaderSize &= ~kMask_IsCompressed;
_nonSolidStartOffset = compressedHeaderSize;
RINOK(_stream->Seek(DataStreamOffset + 4, STREAM_SEEK_SET, NULL));
}
_data.Alloc(FirstHeader.HeaderSize);
_size = (size_t)FirstHeader.HeaderSize;
Decoder.Method = Method;
Decoder.FilterFlag = FilterFlag;
Decoder.Solid = IsSolid;
Decoder.InputStream = _stream;
Decoder.Buffer.Alloc(kInputBufSize);
Decoder.StreamPos = 0;
if (_headerIsCompressed)
{
RINOK(Decoder.Init(_stream, UseFilter));
if (IsSolid)
{
size_t processedSize = 4;
Byte buf[4];
RINOK(Decoder.Read(buf, &processedSize));
if (processedSize != 4)
return S_FALSE;
if (Get32((const Byte *)buf) != FirstHeader.HeaderSize)
return S_FALSE;
}
size_t processedSize = FirstHeader.HeaderSize;
RINOK(Decoder.Read(_data, &processedSize));
if (processedSize != FirstHeader.HeaderSize)
return S_FALSE;
#ifdef NSIS_SCRIPT
if (IsSolid)
{
/* we need additional bytes for data for WriteRegBin */
AfterHeaderSize = (1 << 12);
_afterHeader.Alloc(AfterHeaderSize);
size_t processedSize = AfterHeaderSize;
RINOK(Decoder.Read(_afterHeader, &processedSize));
AfterHeaderSize = (UInt32)processedSize;
}
#endif
}
else
{
size_t processedSize = FirstHeader.HeaderSize;
RINOK(ReadStream(_stream, (Byte *)_data, &processedSize));
if (processedSize < FirstHeader.HeaderSize)
return S_FALSE;
}
#ifdef NUM_SPEED_TESTS
for (unsigned i = 0; i < NUM_SPEED_TESTS; i++)
{
RINOK(Parse());
Clear2();
}
#endif
return Parse();
}
/*
NsisExe =
{
ExeStub
Archive // must start from 512 * N
#ifndef NSIS_CONFIG_CRC_ANAL
{
Some additional data
}
}
Archive
{
FirstHeader
Data
#ifdef NSIS_CONFIG_CRC_SUPPORT && FirstHeader.ThereIsCrc()
{
CRC
}
}
FirstHeader
{
UInt32 Flags;
Byte Signature[16];
// points to the header+sections+entries+stringtable in the datablock
UInt32 HeaderSize;
UInt32 ArcSize;
}
*/
// ---------- PE (EXE) parsing ----------
static const unsigned k_PE_StartSize = 0x40;
static const unsigned k_PE_HeaderSize = 4 + 20;
static const unsigned k_PE_OptHeader32_Size_MIN = 96;
static inline bool CheckPeOffset(UInt32 pe)
{
return (pe >= 0x40 && pe <= 0x1000 && (pe & 7) == 0);
}
static bool IsArc_Pe(const Byte *p, size_t size)
{
if (size < 2)
return false;
if (p[0] != 'M' || p[1] != 'Z')
return false;
if (size < k_PE_StartSize)
return false; // k_IsArc_Res_NEED_MORE;
UInt32 pe = Get32(p + 0x3C);
if (!CheckPeOffset(pe))
return false;
if (pe + k_PE_HeaderSize > size)
return false; // k_IsArc_Res_NEED_MORE;
p += pe;
if (Get32(p) != 0x00004550)
return false;
return Get16(p + 4 + 16) >= k_PE_OptHeader32_Size_MIN;
}
HRESULT CInArchive::Open(IInStream *inStream, const UInt64 *maxCheckStartPosition)
{
Clear();
RINOK(inStream->Seek(0, STREAM_SEEK_CUR, &StartOffset));
const UInt32 kStartHeaderSize = 4 * 7;
const unsigned kStep = 512; // nsis start is aligned for 512
Byte buf[kStep];
UInt64 pos = StartOffset;
size_t bufSize = 0;
UInt64 pePos = (UInt64)(Int64)-1;
for (;;)
{
bufSize = kStep;
RINOK(ReadStream(inStream, buf, &bufSize));
if (bufSize < kStartHeaderSize)
return S_FALSE;
if (memcmp(buf + 4, kSignature, kSignatureSize) == 0)
break;
if (IsArc_Pe(buf, bufSize))
pePos = pos;
pos += kStep;
UInt64 proc = pos - StartOffset;
if (maxCheckStartPosition && proc > *maxCheckStartPosition)
{
if (pePos == 0)
{
if (proc > (1 << 20))
return S_FALSE;
}
else
return S_FALSE;
}
}
if (pePos == (UInt64)(Int64)-1)
{
UInt64 posCur = StartOffset;
for (;;)
{
if (posCur < kStep)
break;
posCur -= kStep;
if (pos - posCur > (1 << 20))
break;
bufSize = kStep;
RINOK(inStream->Seek(posCur, STREAM_SEEK_SET, NULL));
RINOK(ReadStream(inStream, buf, &bufSize));
if (bufSize < kStep)
break;
if (IsArc_Pe(buf, bufSize))
{
pePos = posCur;
break;
}
}
// restore buf to nsis header
bufSize = kStep;
RINOK(inStream->Seek(pos, STREAM_SEEK_SET, NULL));
RINOK(ReadStream(inStream, buf, &bufSize));
if (bufSize < kStartHeaderSize)
return S_FALSE;
}
StartOffset = pos;
UInt32 peSize = 0;
if (pePos != (UInt64)(Int64)-1)
{
UInt64 peSize64 = (pos - pePos);
if (peSize64 < (1 << 20))
{
peSize = (UInt32)peSize64;
StartOffset = pePos;
}
}
DataStreamOffset = pos + kStartHeaderSize;
FirstHeader.Flags = Get32(buf);
if ((FirstHeader.Flags & (~kFlagsMask)) != 0)
return S_FALSE;
IsInstaller = (FirstHeader.Flags & NFlags::kUninstall) == 0;
FirstHeader.HeaderSize = Get32(buf + kSignatureSize + 4);
FirstHeader.ArcSize = Get32(buf + kSignatureSize + 8);
if (FirstHeader.ArcSize <= kStartHeaderSize)
return S_FALSE;
RINOK(inStream->Seek(0, STREAM_SEEK_END, &_fileSize));
IsArc = true;
if (peSize != 0)
{
ExeStub.Alloc(peSize);
RINOK(inStream->Seek(pePos, STREAM_SEEK_SET, NULL));
RINOK(ReadStream_FALSE(inStream, ExeStub, peSize));
}
HRESULT res = S_FALSE;
try
{
CLimitedInStream *_limitedStreamSpec = new CLimitedInStream;
_stream = _limitedStreamSpec;
_limitedStreamSpec->SetStream(inStream);
_limitedStreamSpec->InitAndSeek(pos, FirstHeader.ArcSize);
DataStreamOffset -= pos;
res = Open2(buf + kStartHeaderSize, bufSize - kStartHeaderSize);
}
catch(...)
{
_stream.Release();
throw;
// res = S_FALSE;
}
if (res != S_OK)
{
_stream.Release();
// Clear();
}
return res;
}
UString CInArchive::ConvertToUnicode(const AString &s) const
{
if (IsUnicode)
{
UString res;
if (ConvertUTF8ToUnicode(s, res))
return res;
}
return MultiByteToUnicodeString(s);
}
void CInArchive::Clear2()
{
IsUnicode = false;
NsisType = k_NsisType_Nsis2;
IsNsis225 = false;
IsNsis200 = false;
LogCmdIsEnabled = false;
BadCmd = -1;
#ifdef NSIS_SCRIPT
Name.Empty();
BrandingText.Empty();
Script.Empty();
LicenseFiles.Clear();
_numRootLicenses = 0;
_numLangStrings = 0;
langStrIDs.Clear();
LangComment.Empty();
noParseStringIndexes.Clear();
#endif
APrefixes.Clear();
UPrefixes.Clear();
Items.Clear();
IsUnicode = false;
ExeStub.Free();
}
void CInArchive::Clear()
{
Clear2();
IsArc = false;
_stream.Release();
}
}}
|