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
|
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
Copyright (c) 1999-2001 Palm, Inc. or its subsidiaries.
All rights reserved.
This file is part of the Palm OS Emulator.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
\* ===================================================================== */
#include "EmCommon.h"
#include "Miscellaneous.h"
#include "Byteswapping.h" // Canonical
#include "ChunkFile.h" // Chunk::GetPointer
#include "EmBankMapped.h" // EmBankMapped::GetEmulatedAddress
#include "EmErrCodes.h" // kError_UnimplementedTrap
#include "EmHAL.h" // EmHAL::ResetTimer, EmHAL::ResetRTC
#include "EmLowMem.h" // EmLowMem_SetGlobal, EmLowMem_GetGlobal
#include "EmMemory.h" // Memory::MapPhysicalMemory, EmMem_strcpy, EmMem_memcmp
#include "EmPalmFunction.h" // GetFunctionAddress
#include "EmPatchState.h" // EmPatchState::OSMajorVersion
#include "EmSession.h" // ScheduleDeferredError
#include "EmStreamFile.h" // EmStreamFile, kOpenExistingForRead
#include "ErrorHandling.h" // Errors::Throw
#include "Logging.h" // LogDump
#include "Platform.h" // Platform::AllocateMemory
#include "ROMStubs.h" // WinGetDisplayExtent, FrmGetNumberOfObjects, FrmGetObjectType, FrmGetObjectId, ...
#include "Strings.r.h" // kStr_INetLibTrapBase, etc.
#include "UAE.h" // m68k_dreg, etc.
#include <algorithm> // sort()
#include <locale.h> // localeconv, lconv
#include <strstream> // strstream
#include <time.h> // time, localtime
extern "C" {
// These are defined in machdep_maccess.h, too
#undef get_byte
#undef put_byte
#undef put_long
#include "gzip.h"
#include "lzw.h"
int (*write_buf_proc)(char *buf, unsigned size);
DECLARE(uch, inbuf, INBUFSIZ +INBUF_EXTRA);
DECLARE(uch, outbuf, OUTBUFSIZ+OUTBUF_EXTRA);
DECLARE(ush, d_buf, DIST_BUFSIZE);
DECLARE(uch, window, 2L*WSIZE);
DECLARE(ush, tab_prefix, 1L<<BITS);
int test = 0; /* test .gz file integrity */
int level = 1; /* compression level */
int exit_code = OK; /* program exit code */
int verbose = 2; /* be verbose (-v) */
int quiet = 0; /* be quiet (-q) */
char ifname[] = "ifname";
char ofname[] = "ofname";
char* progname = "progname";
long bytes_in; /* number of input bytes */
long bytes_out; /* number of output bytes */
int ifd; /* input file descriptor */
int ofd; /* output file descriptor */
unsigned insize; /* valid bytes in inbuf */
unsigned inptr; /* index of next byte to be processed in inbuf */
unsigned outcnt; /* bytes in output buffer */
#include <setjmp.h>
jmp_buf env;
RETSIGTYPE abort_gzip()
{
LogDump ();
abort ();
// longjmp (env, 1);
}
int my_fprintf (FILE*, const char* fmt, ...)
{
int n;
va_list arg;
va_start (arg, fmt);
n = LogGetStdLog ()->VPrintf (fmt, arg);
va_end (arg);
return n;
}
} // extern "C"
extern "C"
{
int PrvGzipReadProc (char* buf, unsigned size);
int PrvGzipWriteProc (char* buf, unsigned size);
}
static void* gSrcP;
static void* gDstP;
static long gSrcBytes;
static long gDstBytes;
static long gSrcOffset;
static long gDstOffset;
// ===========================================================================
// StMemory Class
// ===========================================================================
// Constructor allocates the Ptr
// Destructor disposes of the Ptr
StMemory::StMemory(
char* inPtr)
{
mIsOwner = (inPtr != NULL);
mPtr = inPtr;
}
StMemory::StMemory(
long inSize, // Bytes to allocate
Bool inClearBytes) // Whether to clear all bytes to zero
{
mIsOwner = true;
if (inClearBytes)
mPtr = (char*) Platform::AllocateMemoryClear (inSize);
else
mPtr = (char*) Platform::AllocateMemory (inSize);
}
StMemory::StMemory(
const StMemory &inPointerBlock)
{
UNUSED_PARAM(inPointerBlock)
}
StMemory::~StMemory()
{
Dispose ();
}
StMemory&
StMemory::operator = (
const StMemory &inPointerBlock)
{
UNUSED_PARAM(inPointerBlock)
return *this;
}
void
StMemory::Adopt(
char* inPtr)
{
if (inPtr != mPtr)
{
Dispose ();
mIsOwner = (inPtr != NULL);
mPtr = inPtr;
}
}
char*
StMemory::Release() const
{
mIsOwner = false;
return mPtr;
}
void
StMemory::Dispose()
{
if (mIsOwner && (mPtr != NULL))
{
Platform::DisposeMemory (mPtr);
}
mIsOwner = false;
mPtr = NULL;
}
StMemoryMapper::StMemoryMapper (const void* memory, long size) :
fMemory (memory)
{
if (fMemory)
Memory::MapPhysicalMemory (fMemory, size);
}
StMemoryMapper::~StMemoryMapper (void)
{
if (fMemory)
Memory::UnmapPhysicalMemory (fMemory);
}
void* Platform_AllocateMemory (size_t size)
{
return Platform::AllocateMemory (size);
}
void* Platform_ReallocMemory (void* p, size_t size)
{
return Platform::ReallocMemory (p, size);
}
void Platform_DisposeMemory (void* p)
{
Platform::DisposeMemory (p);
}
StWordSwapper::StWordSwapper (void* memory, long length) :
fMemory (memory),
fLength (length)
{
::ByteswapWords (fMemory, fLength);
}
StWordSwapper::~StWordSwapper (void)
{
::ByteswapWords (fMemory, fLength);
}
/***********************************************************************
*
* FUNCTION: PrvFormObjectHasValidSize
*
* DESCRIPTION: Determine whether or not a form object's size is valid.
*
* PARAMETERS: frm - form containing the object
*
* objType - the object's type
*
* objIndex - the object's index in the form
*
* bounds - the bounding rectangle around the object
*
* RETURNED: True if the object's size is valid.
*
***********************************************************************/
static Bool PrvFormObjectHasValidSize (FormPtr frm, FormObjectKind objType, UInt16 objIndex,
RectangleType bounds)
{
EmAssert (frm);
CEnableFullAccess munge; // Remove blocks on memory access.
// Return valid right away if we have height and width
if (bounds.extent.x > 0 &&
bounds.extent.y > 0)
{
return true;
}
// Allow zero-sized gadgets and tables. The former are often
// used as dummy objects merely to hold references to custom
// data. The latter exist because there's no other way to
// hide a table (there's no "usable" bit).
if (objType == frmGadgetObj ||
objType == frmTableObj)
{
return true;
}
// Allow zero-width (but not zero-height) popup triggers.
if (objType == frmControlObj)
{
emuptr ctrlPtr = (emuptr) ::FrmGetObjectPtr (frm, objIndex);
uint8 style = EmMemGet8 (ctrlPtr + offsetof (ControlType, style));
if (style == popupTriggerCtl)
{
if (bounds.extent.x == 0 &&
bounds.extent.y > 0)
{
return true;
}
}
}
// Allow zero-height lists if the number of objects in them is zero.
if (objType == frmListObj)
{
emuptr listPtr = (emuptr) FrmGetObjectPtr (frm, objIndex);
Int16 numItems = ::LstGetNumberOfItems ((ListType*)listPtr);
if (numItems == 0)
{
if (bounds.extent.x > 0 &&
bounds.extent.y == 0)
{
return true;
}
}
}
// Failed all the special cases.
return false;
}
/***********************************************************************
*
* FUNCTION: PrvFormObjectIsOffscreen
*
* DESCRIPTION: Determine whether or not an object is off-screen.
*
* PARAMETERS: bounds - the bounding rectangle of the object
*
* winWidth - width of the window
*
* winHeight - width of the window
*
* RETURNED: True if the object is off-screen.
*
***********************************************************************/
static Bool PrvFormObjectIsOffscreen (RectangleType bounds, Int16 winWidth, Int16 winHeight)
{
// Ignore objects with a zero extent
if (bounds.extent.x <= 0 ||
bounds.extent.y <= 0)
return false;
return (bounds.topLeft.x >= winWidth ||
bounds.topLeft.y >= winHeight ||
bounds.topLeft.x + bounds.extent.x <= 0 ||
bounds.topLeft.y + bounds.extent.y <= 0);
}
/***********************************************************************
*
* FUNCTION: PrvFormObjectIsUsable
*
* DESCRIPTION: Determines whether or not a form object is usable, ie
* should be considered part of the UI.
*
* PARAMETERS: frmP - form containing the object in question
*
* index - index of the object
*
* kind - type of the object
*
* RETURNED: True if the object is usable.
*
***********************************************************************/
#define ControlAttrType_usable 0x8000 // set if part of ui
static Bool PrvFormObjectIsUsable (FormPtr frmP, uint16 index, FormObjectKind kind)
{
EmAssert (frmP);
CEnableFullAccess munge; // Remove blocks on memory access.
emuptr objP = (emuptr)::FrmGetObjectPtr (frmP, index);
if (objP == EmMemNULL)
{
return false;
}
switch (kind)
{
// Objects with special 'usable' flag:
case frmFieldObj:
{
FieldAttrType attr;
::FldGetAttributes ((FieldType*)objP, &attr);
return attr.usable == true;
}
case frmControlObj:
{
EmAliasControlType<PAS> control ((emuptr)objP);
return control.attr.flags & ControlAttrType_usable;
}
case frmGadgetObj:
{
EmAliasFormGadgetType<PAS> gadget ((emuptr)objP);
return gadget.attr.flags & ControlAttrType_usable;
}
case frmListObj:
{
EmAliasListType<PAS> list ((emuptr)objP);
return list.attr.flags & ControlAttrType_usable;
}
case frmScrollBarObj:
{
EmAliasScrollBarType<PAS> scrollbar ((emuptr)objP);
return scrollbar.attr.flags & ControlAttrType_usable;
}
// Objects assumed to be usable:
case frmTableObj:
case frmGraffitiStateObj:
return true;
// Objects assumed to be unusable:
case frmBitmapObj:
case frmLineObj:
case frmFrameObj:
case frmRectangleObj:
case frmLabelObj:
case frmPopupObj:
case frmTitleObj:
return false;
}
// Everything else:
return false;
}
/***********************************************************************
*
* FUNCTION: ValidateFormObjects
*
* DESCRIPTION: Iterate over all the objects in a form and complain
* if we find one that is invalid in some way.
*
* PARAMETERS: frm - the form to validate
*
* RETURNED: Nothing.
*
***********************************************************************/
void ValidateFormObjects (FormPtr frm)
{
if (!frm)
return;
Int16 winWidth, winHeight;
::WinGetDisplayExtent (&winWidth, &winHeight);
UInt16 numObjects = ::FrmGetNumberOfObjects (frm);
for (UInt16 objIndex = 0; objIndex < numObjects; ++objIndex)
{
FormObjectKind objType = ::FrmGetObjectType (frm, objIndex);
UInt16 objID = ::FrmGetObjectId (frm, objIndex);
switch (objType)
{
case frmBitmapObj:
case frmLineObj:
case frmFrameObj:
case frmRectangleObj:
case frmLabelObj:
case frmTitleObj:
case frmPopupObj:
// do nothing for these
break;
default:
{
// Check for completely offscreen objects.
// (The jury is still out on partially offscreen objects.)
RectangleType bounds;
::FrmGetObjectBounds (frm, objIndex, &bounds);
if (!::PrvFormObjectHasValidSize (frm, objType, objIndex, bounds))
{
// Report any errors. For now, don't report errors on 1.0
// devices. They may not follow the rules, either. In
// particular, someone noticed that the Graffiti state
// indicator has a size of 0,0.
if (EmPatchState::OSMajorVersion () > 1)
{
EmAssert (gSession);
gSession->ScheduleDeferredError (
new EmDeferredErrSizelessObject (objID, bounds));
}
}
else if (::PrvFormObjectIsOffscreen (bounds, winWidth, winHeight))
{
if (EmPatchState::OSMajorVersion () > 1)
{
EmAssert (gSession);
gSession->ScheduleDeferredError (
new EmDeferredErrOffscreenObject (objID, bounds));
}
}
}
}
}
}
/***********************************************************************
*
* FUNCTION: CollectOKObjects
*
* DESCRIPTION: Iterate over the objects in a form and make a list of
* the ones that are fair game for tapping on. Exclude
* objects that aren't interactive (ie a label), aren't
* usable, or aren't valid.
*
* PARAMETERS: frm - the form in question
*
* okObjects - vector list of objects that are deemed 'ok'.
*
* RETURNED: Nothing.
*
***********************************************************************/
void CollectOKObjects (FormPtr frm, vector<UInt16>& okObjects)
{
if (!frm)
return;
Int16 winWidth, winHeight;
::WinGetDisplayExtent (&winWidth, &winHeight);
UInt16 numObjects = ::FrmGetNumberOfObjects (frm);
for (UInt16 objIndex = 0; objIndex < numObjects; ++objIndex)
{
FormObjectKind objType = ::FrmGetObjectType (frm, objIndex);
switch (objType)
{
case frmBitmapObj:
case frmLineObj:
case frmFrameObj:
case frmRectangleObj:
case frmLabelObj:
case frmTitleObj:
case frmPopupObj:
// do nothing for these
break;
default:
{
RectangleType bounds;
::FrmGetObjectBounds (frm, objIndex, &bounds);
if (!::PrvFormObjectHasValidSize (frm, objType, objIndex, bounds) ||
::PrvFormObjectIsOffscreen (bounds, winWidth, winHeight) ||
!::PrvFormObjectIsUsable (frm, objIndex, objType))
{
break;
}
else
{
okObjects.push_back (objIndex);
}
break;
}
}
}
}
// ---------------------------------------------------------------------------
// PinRectInRect
// ---------------------------------------------------------------------------
Bool PinRectInRect (EmRect& inner, const EmRect& outer)
{
// Do this in a way such that if the incoming rectangle is
// taller or wider than gdRect that we ensure we see the
// top and left.
Bool result = false;
if (inner.fBottom > outer.fBottom)
{
inner.Offset (0, outer.fBottom - inner.fBottom); // Move it up
result = true;
}
if (inner.fRight > outer.fRight)
{
inner.Offset (outer.fRight - inner.fRight, 0); // Move it left
result = true;
}
if (inner.fTop < outer.fTop)
{
inner.Offset (0, outer.fTop - inner.fTop); // Move it down
result = true;
}
if (inner.fLeft < outer.fLeft)
{
inner.Offset (outer.fLeft - inner.fLeft, 0); // Move it right
result = true;
}
return result;
}
// For "data" databases, we look to see if they have the following structure
// in their appInfo block. If so, we grab the icon and version string from here.
// WARNING: This structure contains variable size fields and must be generated
// and parsed at programmatically.
#define dataAppInfoSignature 'lnch'
#define dataAppInfoVersion 3 // current version of header
#include "PalmPack.h"
struct DataAppInfoType
{
UInt32 signature; // must be 'lnch' (0x6C6E6338)
UInt16 hdrVersion; // version of this header - must be 3
UInt16 encVersion; // encoder version
UInt16 verStrWords; // length of version string array that
// follows in 16-bit words.
//UInt8 verStr[verStrWords]; // 0 terminated version string with
// possible extra NULL byte at end for
// padding
//--- The title is only present in version 2 or later
UInt16 titleWords; // length of title string array that follows
// in 16-bit words.
//UInt8 title[titleWords]; // 0 terminated title string with possible
// extra NULL at end for padding.
UInt16 iconWords; // length of icon data that follows in 16-bit
// words.
//UInt8 icon[iconWords]; // icon in "BitmapType" format with possible NULL
// byte at end for even UInt16 padding
UInt16 smIconWords; // length of small icon data that follows in
// 16-bit words
//UInt8 smIcon[smIconWords]; // small icon in "BitmapType" format with
// possible NULL byte at end for even UInt16
// padding
//--------- Version 2 Fields ------------
};
// Size of appInfo block.
#define dataAppInfoVersionSize (sizeof(DataAppInfoType))
#include "PalmPackPop.h"
/***********************************************************************
*
* FUNCTION: IsExecutable
*
* DESCRIPTION: Returns whether or not the given database contains an
* application in which case we want to present only
* the database with the most recent version number (in
* case there's more than one database with this one's
* type and creator).
*
* PARAMETERS: dbType - type of the database
*
* dbCreator - creator signature of the database
*
* dbAttrs - attributes of the database
*
* RETURNED: True if this database is an executable.
*
***********************************************************************/
Bool IsExecutable (UInt32 dbType, UInt32 dbCreator, UInt16 dbAttrs)
{
UNUSED_PARAM(dbCreator)
UNUSED_PARAM(dbAttrs)
if (dbType == sysFileTApplication)
return true;
if (dbType == sysFileTPanel)
return true;
return false;
}
/***********************************************************************
*
* FUNCTION: IsVisible
*
* DESCRIPTION: Returns whether or not the given database represents
* an item we want to display.
*
* PARAMETERS: dbType - type of the database
*
* dbCreator - creator signature of the database
*
* dbAttrs - attributes of the database
*
* RETURNED: True if we should include this database in our list.
*
***********************************************************************/
Bool IsVisible (UInt32 dbType, UInt32 dbCreator, UInt16 dbAttrs)
{
UNUSED_PARAM(dbCreator)
// Don't show anything concerning the Launcher
// (That comment and the following commented out code was from the
// Launcher application. I've take it out so that we can run
// Gremlins over the Launcher).
// if (dbCreator == sysFileCLauncher)
// return false;
// The following test can come and go. Currently, it's here
// so that things like Clipper don't show up in the list (just
// as it doesn't show up in the Launcher). However, there may
// be time when we want to show it. An example would be in
// an experiemental version of the New Gremlin dialog that
// separated apps and documents. Selecting an app in one list
// would display a list of its documents in the other list. In
// that case, we'd need to show clipper in order to be able to
// display its documents.
// OK, the test is now gone. From Scott Johnson:
//
// The New Gremlin list doesn't show apps with the dmHdrAttrHidden attribute.
// This is a problem for mine, which is a sort of runtime forms engine. The
// runtime is a hidden app and the user-built apps are visible. The user app
// launches by just doing an app switch to the runtime. (Sort of a launchable
// database concept for pre-3.2 systems.) To Gremlin this, both apps need to
// be selected in the New Gremlin list. But the hidden one isn't shown. Oops.
// if (dbAttrs & dmHdrAttrHidden)
// return false;
if (dbAttrs & dmHdrAttrLaunchableData)
return true;
if (dbType == sysFileTApplication)
return true;
if (dbType == sysFileTPanel)
return true;
return false;
}
/***********************************************************************
*
* FUNCTION: GetLoadableFileList
*
* DESCRIPTION: Scan the given directory for files that can be loaded
* into the Palm OS environment and add them to the
* given collection object. Loadable files are PRCs,
* PDBs, and PQAs.
*
* PARAMETERS: directoryName - name of the directory to search.
* This directory is assumed to be in the emulator's
* containing directory.
*
* fileList - collection to receive the found files.
*
* RETURNED: nothing
*
***********************************************************************/
void GetLoadableFileList (string directoryName, EmFileRefList& fileList)
{
// Get the application's directory.
EmDirRef poserDir = EmDirRef::GetEmulatorDirectory ();
// Get the directory we're asked to look into.
EmDirRef searchDir (poserDir, directoryName);
if (!searchDir.Exists ())
return;
// Get all of its children.
EmFileRefList children;
searchDir.GetChildren (&children, NULL);
// Filter for the types that we want.
EmFileRefList::iterator iter = children.begin ();
while (iter != children.end ())
{
if (iter->IsType (kFileTypePalmApp) ||
iter->IsType (kFileTypePalmDB) ||
iter->IsType (kFileTypePalmQA))
{
fileList.push_back (*iter);
}
++iter;
}
}
/***********************************************************************
*
* FUNCTION: GetFileContents
*
* DESCRIPTION: .
*
* PARAMETERS:
*
* RETURNED:
*
***********************************************************************/
void GetFileContents (const EmFileRef& file, Chunk& contents)
{
EmStreamFile stream (file, kOpenExistingForRead);
int32 length = stream.GetLength ();
contents.SetLength (length);
stream.GetBytes (contents.GetPointer (), length);
}
/***********************************************************************
*
* FUNCTION: AppGetExtraInfo
*
* DESCRIPTION: Returns additional information on the application. This
* information is usually pretty expensive to get, so we
* defer getting it until as late as possible.
*
* This function is derived from one in the Launcher with
* the same name. That function returned a lot more
* information. This one has been simplified to return
* only the application's (or special database's) name.
*
* PARAMETERS: infoP - pointer to the DatabaseInfo struct for the application
* we need to get more information on.
*
* RETURNED: Any errors encountered while processing this request.
* The requested information is returned back in the DatabaseInfo
* struct.
*
***********************************************************************/
static Err AppGetExtraInfo (DatabaseInfo* infoP)
{
Err err = errNone;
infoP->name[0] = 0;
//====================================================================
// If it's a resource database, we must open it to get the appName
//====================================================================
if (infoP->dbAttrs & dmHdrAttrResDB)
{
DmOpenRef appDB = NULL;
MemHandle strH;
// Open database
appDB = DmOpenDatabase (infoP->cardNo, infoP->dbID, dmModeReadOnly);
if (appDB == NULL)
{
err = DmGetLastErr ();
goto Exit;
}
//...............................
// Get app name if we don't already have it.
//...............................
strH = DmGet1Resource (ainRsc, ainID);
// copy launcher name, if present
if (strH != NULL)
{
emuptr strP = (emuptr) MemHandleLock (strH);
EmMem_strcpy (infoP->name, strP);
MemHandleUnlock (strH);
DmReleaseResource (strH);
}
::DmCloseDatabase (appDB);
} // if resource database
//====================================================================
// If it's a record database, we look in the appInfo block.
//====================================================================
else
{
LocalID appInfoID;
MemHandle appInfoH = 0;
MemPtr appInfoP = 0;
emuptr specialInfoP;
emuptr bP;
UInt16 verStrWords, titleWords;
// Look for app info
err = DmDatabaseInfo (infoP->cardNo, infoP->dbID, 0,
0, 0, 0, 0, 0, 0, &appInfoID, 0, 0, 0);
if (!err && appInfoID)
{
// Get handle (if RAM based) and ptr to app Info
if (MemLocalIDKind (appInfoID) == memIDHandle)
{
appInfoH = (MemHandle) MemLocalIDToGlobal (appInfoID, infoP->cardNo);
appInfoP = MemHandleLock (appInfoH);
}
else
{
appInfoP = MemLocalIDToGlobal(appInfoID, infoP->cardNo);
}
// See if this is the special launcher info and if so, get the icons
// out of that.
specialInfoP = (emuptr) appInfoP;
DataAppInfoType specialInfo;
specialInfo.signature = EmMemGet32 (specialInfoP + offsetof (DataAppInfoType, signature));
specialInfo.hdrVersion = EmMemGet16 (specialInfoP + offsetof (DataAppInfoType, hdrVersion));
specialInfo.encVersion = EmMemGet16 (specialInfoP + offsetof (DataAppInfoType, encVersion));
if (MemPtrSize (appInfoP) >= dataAppInfoVersionSize &&
specialInfo.signature == dataAppInfoSignature &&
specialInfo.hdrVersion >= dataAppInfoVersion)
{
// Get ptr to version string
bP = specialInfoP + offsetof (DataAppInfoType, verStrWords);
verStrWords = EmMemGet16 (bP);
bP += sizeof(UInt16);
bP += verStrWords * sizeof(UInt16);
// Get ptr to name string
titleWords = EmMemGet16 (bP);
bP += sizeof(UInt16);
if (titleWords)
{
EmMem_strcpy (infoP->name, bP);
}
} // If valid appInfo
if (appInfoH)
{
MemHandleUnlock(appInfoH);
}
} // if (!err && appInfoID)
} // Record Database.
Exit:
// If no luck getting the visible name, put in default
if (infoP->name[0] == 0)
{
// Get DB name
strcpy (infoP->name, infoP->dbName);
}
return err;
}
/***********************************************************************
*
* FUNCTION: AppCompareDataBaseNames
*
* DESCRIPTION: sort() callback function to sort entries by name.
*
* PARAMETERS: a, b - references to two DatabaseInfo's to compare.
*
* RETURNED: True if a should appear before b, false otherwise.
*
***********************************************************************/
static bool AppCompareDataBaseNames (const DatabaseInfo& a, const DatabaseInfo& b)
{
return _stricmp (a.name, b.name) < 0;
}
/***********************************************************************
*
* FUNCTION: GetDatabases
*
* DESCRIPTION: Collects the list of entries that should be displayed
* in the New Gremlin dialog box.
*
* This function is derived from the Launcher function
* AppCreateDataBaseList, as rewritten by Ron for the
* 3.2 ROMs.
*
* PARAMETERS: dbList -- collection into which we store the found
* DatabaseInfo entries.
*
* RETURNED: nothing.
*
***********************************************************************/
void GetDatabases (DatabaseInfoList& dbList, Bool applicationsOnly)
{
UInt16 cardNo;
UInt16 numCards;
UInt16 numDBs;
Int16 dbIndex; // UInt16 results in a bug
LocalID dbID;
Err err = errNone;
DatabaseInfo dbInfo;
Boolean needToAddNewEntry;
//=======================================================================
// Cycle through all databases in the ROM and RAM and place them into our list.
//=======================================================================
numCards = ::MemNumCards ();
for (cardNo = 0; cardNo < numCards; ++cardNo)
{
numDBs = ::DmNumDatabases (cardNo);
//---------------------------------------------------------------
// Loop through databases on this card, DmGetDatabase() returns ROM
// databases first, followed by RAM databases.
//---------------------------------------------------------------
for (dbIndex = 0; dbIndex < numDBs; ++dbIndex)
{
//--------------------------------------------------------
// Get info on the next database and see if it should be visible.
//--------------------------------------------------------
dbID = ::DmGetDatabase (cardNo, dbIndex);
err = ::DmDatabaseInfo (
cardNo,
dbID,
dbInfo.dbName, /*nameP*/
&dbInfo.dbAttrs,
&dbInfo.version,
NULL, /*create date*/
&dbInfo.modDate,
NULL, /*backup date*/
NULL, /*modNum*/
NULL, /*appInfoID*/
NULL, /*sortInfoID*/
&dbInfo.type,
&dbInfo.creator);
Errors::ThrowIfPalmError (err);
// If it's not supposed to be visible, skip it
if (applicationsOnly && !::IsVisible (dbInfo.type, dbInfo.creator, dbInfo.dbAttrs))
{
continue;
}
//--------------------------------------------------------------
// Save info on this database
//--------------------------------------------------------------
dbInfo.dbID = dbID;
dbInfo.cardNo = cardNo;
//--------------------------------------------------------------
// If it's an executable, make sure it's the most recent version in our
// list
//--------------------------------------------------------------
needToAddNewEntry = true;
if (applicationsOnly && ::IsExecutable (dbInfo.type, dbInfo.creator, dbInfo.dbAttrs))
{
// Search for database of same type and creator and check version
DatabaseInfoList::iterator thisIter = dbList.begin ();
while (thisIter != dbList.end ())
{
if ((*thisIter).type == dbInfo.type &&
(*thisIter).creator == dbInfo.creator)
{
// If this new one is a newer or same version than the previous one,
// replace the previous entry. Checking for == version allows RAM
// executables to override ROM ones.
if (dbInfo.version >= (*thisIter).version)
{
::AppGetExtraInfo (&dbInfo);
*thisIter = dbInfo;
}
// Since there's already an item with this type/creator
// already in the list, there's no need to add another one.
needToAddNewEntry = false;
break;
}
++thisIter;
}
}
//--------------------------------------------------------------
// If we still need to add this entry, do so now.
//--------------------------------------------------------------
if (needToAddNewEntry)
{
::AppGetExtraInfo (&dbInfo);
dbList.push_back (dbInfo);
}
} // for (dbIndex = 0; dbIndex < numDBs; dbIndex++)
} // for (cardNo = 0; cardNo < MemNumCards(); cardNo++)
//===========================================================================
// Sort the list by name
//===========================================================================
// Sort the databases by their name.
sort (dbList.begin (), dbList.end (), AppCompareDataBaseNames);
}
/***********************************************************************
*
* FUNCTION: InstallCalibrationInfo
*
* DESCRIPTION: Sets the pen calibration info to be "perfect": no
* translation or scaling.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void InstallCalibrationInfo (void)
{
// Open the preferences database. If the new version of PrefOpenPreferenceDB
// exists, then call it. Otherwise, call the old version. We can't just
// unconditionally call the old version, as that has a bug in the newer
// ROMs that causes it to create the database incorrectly if it doesn't
// already exist.
DmOpenRef dbP;
if (EmLowMem::TrapExists (sysTrapPrefOpenPreferenceDB))
dbP = ::PrefOpenPreferenceDB (false);
else
dbP = ::PrefOpenPreferenceDBV10 ();
if (dbP)
{
// Get the calibration information.
MemHandle resourceH = ::DmGetResource (sysResTSysPref, sysResIDSysPrefCalibration);
// If that information doesn't exist, go about creating it.
if (!resourceH)
{
resourceH = ::DmNewResource (dbP, sysResTSysPref, sysResIDSysPrefCalibration,
4 * sizeof(UInt16));
}
if (resourceH)
{
// Write in the calibration information. The information has the
// following format and values:
//
// scaleX : 256
// scaleY : 256
// offsetX : 0
// offsetY : 0
//
// We encode that data here as a string of bytes to avoid endian problems.
MemPtr resP = ::MemHandleLock (resourceH);
unsigned char data[] = { 1, 0, 1, 0, 0, 0, 0, 0 };
::DmWrite (resP, 0, data, 4 * sizeof (UInt16));
::MemHandleUnlock (resourceH);
::DmReleaseResource (resourceH);
}
::DmCloseDatabase (dbP);
}
}
/***********************************************************************
*
* FUNCTION: ResetCalibrationInfo
*
* DESCRIPTION: Sets the pen calibration info to be "perfect": no
* translation or scaling.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void ResetCalibrationInfo (void)
{
// Reset the pen calibration info by calling PenCalibrate with the right
// parameters. Unfortunately, due presumably to division rounding errors,
// doing this just once doesn't necessarily get the scaling and offset
// values exactly. However, making a few calls to PenCalibrate seems
// to get us to home in on the perfect calibration values.
Bool perfect = false;
for (int kk = 0; kk < 3 && !perfect; ++kk)
{
#define target0X 10 // top left
#define target0Y 10
#define target1X (160-10) // bottom right
#define target1Y (160-10)
Err err;
PointType digPoints[2];
PointType scrPoints[2];
scrPoints[0].x = target0X;
scrPoints[0].y = target0Y;
scrPoints[1].x = target1X;
scrPoints[1].y = target1Y;
digPoints[0].x = 0x100 - target0X;
digPoints[0].y = 0x100 - target0Y;
digPoints[1].x = 0x100 - target1X;
digPoints[1].y = 0x100 - target1Y;
err = ::PenRawToScreen(&digPoints[0]);
err = ::PenRawToScreen(&digPoints[1]);
err = ::PenCalibrate(&digPoints[0], &digPoints[1], &scrPoints[0], &scrPoints[1]);
DmOpenRef dbP;
if (EmLowMem::TrapExists (sysTrapPrefOpenPreferenceDB))
dbP = ::PrefOpenPreferenceDB (false);
else
dbP = ::PrefOpenPreferenceDBV10 ();
if (dbP)
{
MemHandle resourceH = ::DmGetResource (sysResTSysPref, sysResIDSysPrefCalibration);
if (resourceH)
{
MemPtr resP = ::MemHandleLock (resourceH);
unsigned char perfect_pattern[] = { 1, 0, 1, 0, 0, 0, 0, 0 };
perfect = (EmMem_memcmp ((void*) perfect_pattern, (emuptr) resP, 8) == 0);
::MemHandleUnlock (resourceH);
::DmReleaseResource (resourceH);
}
::DmCloseDatabase (dbP);
}
}
}
/***********************************************************************
*
* FUNCTION: ResetClocks
*
* DESCRIPTION: .
*
* PARAMETERS: .
*
* RETURNED: nothing
*
***********************************************************************/
void ResetClocks (void)
{
EmHAL::ResetTimer ();
EmLowMem_SetGlobal (hwrCurTicks, 0);
emuptr sysKernelDataP = EmLowMem_GetGlobal (sysKernelDataP);
EmMemPut32 (sysKernelDataP + 0x20, 0);
EmHAL::ResetRTC ();
}
/***********************************************************************
*
* FUNCTION: SetHotSyncUserName
*
* DESCRIPTION: Calls the Data Link Manager to set the user's HotSync
* name. Many applications key off this name for things
* like copy protection, so we set this value when they
* boot up.
*
* PARAMETERS: userNameP - the user's name.
*
* RETURNED: nothing
*
***********************************************************************/
const UInt16 kMagicRefNum = 0x666; // See comments in HtalLibSendReply.
void SetHotSyncUserName (const char* userNameP)
{
if (EmLowMem::GetTrapAddress (sysTrapDlkDispatchRequest) == EmMemNULL)
return;
if (!userNameP)
return;
size_t userNameLen = strlen (userNameP) + 1;
// If the name is too long, just return. This should really only
// happen if the user hand-edits the preference file to contain
// a name that's too long. The Preferences dialog box handler
// checks as well, so the name shouldn't get too long from that path.
if (userNameLen > dlkMaxUserNameLength + 1)
return;
// We need to prepare a command block for the DataLink Manager.
// Define one large enough to hold all the data we'll pass in.
//
// The format of the data block is as follows:
//
// [byte] DlpReqHeaderType.id : Command request number (== dlpWriteUserInfo)
// [byte] DlpReqHeaderType.argc : # of arguments for this command (== 1)
//
// [byte] DlpTinyArgWrapperType.bID : ID of first argument (== dlpWriteUserInfoReqArgID)
// [byte] DlpTinyArgWrapperType.bSize : Size in bytes of first argument (== whatever)
//
// [long] DlpWriteUserInfoReqHdrType.userID : Not used here - set to zero
// [long] DlpWriteUserInfoReqHdrType.viewerID : Not used here - set to zero
// [long] DlpWriteUserInfoReqHdrType.lastSyncPC : Not used here - set to zero
// [8byt] DlpWriteUserInfoReqHdrType.lastSyncDate : Not used here - set to zero
// [long] DlpWriteUserInfoReqHdrType.modFlags : Bits saying what values are being set
// [byte] DlpWriteUserInfoReqHdrType.userNameLen : Length of user name + NULL
//
// [str ] userName
char buffer[ sizeof (DlpReqHeaderType) +
sizeof (DlpTinyArgWrapperType) +
sizeof (DlpWriteUserInfoReqHdrType) +
dlpMaxUserNameSize];
// Get handy pointers to all of the above.
DlpReqHeaderType* reqHdr = (DlpReqHeaderType*) buffer;
DlpTinyArgWrapperType* reqWrapper = (DlpTinyArgWrapperType*) (((char*) reqHdr) + sizeof(DlpReqHeaderType));
DlpWriteUserInfoReqHdrType* reqArgHdr = (DlpWriteUserInfoReqHdrType*) (((char*) reqWrapper) + sizeof(DlpTinyArgWrapperType));
char* reqName = ((char*) reqArgHdr) + sizeof (DlpWriteUserInfoReqHdrType);
// Fill in request header
reqHdr->id = dlpWriteUserInfo;
reqHdr->argc = 1;
// Fill in the request arg wrapper
reqWrapper->bID = (UInt8) dlpWriteUserInfoReqArgID;
reqWrapper->bSize = (UInt8) (sizeof (*reqArgHdr) + userNameLen);
// Fill in request arg header
reqArgHdr->modFlags = dlpUserInfoModName;
reqArgHdr->userNameLen = userNameLen;
// Copy in the user name.
strcpy (reqName, userNameP);
// Build up a session block to hold the command block.
DlkServerSessionType session;
memset (&session, 0, sizeof (session));
session.htalLibRefNum = kMagicRefNum; // See comments in HtalLibSendReply.
session.gotCommand = true;
session.cmdLen = sizeof (buffer);
session.cmdP = buffer;
// For simplicity, byteswap here so that we don't have to reparse all
// that above data in DlkDispatchRequest.
Canonical (reqHdr->id);
Canonical (reqHdr->argc);
Canonical (reqWrapper->bID);
Canonical (reqWrapper->bSize);
Canonical (reqArgHdr->modFlags);
Canonical (reqArgHdr->userNameLen);
// Patch up cmdP and map in the buffer it points to.
StMemoryMapper mapper (session.cmdP, session.cmdLen);
session.cmdP = (void*) EmBankMapped::GetEmulatedAddress (session.cmdP);
// Finally, install the name.
/*Err err =*/ DlkDispatchRequest (&session);
#if 0
ULong lastSyncDate;
char userName[dlkUserNameBufSize];
Err err = DlkGetSyncInfo(0/*succSyncDateP*/, &lastSyncDate, 0/*syncStateP*/,
userName, 0/*logBufP*/, 0/*logLenP*/);
#endif
}
/***********************************************************************
*
* FUNCTION: SeparateList
*
* DESCRIPTION: Break up a comma-delimited list of items, returning the
* pieces in a StringList.
*
* PARAMETERS: stringList - the StringList to receive the broken-up
* pieces of the comma-delimited list. This collection
* is *not* first cleared out, so it's possible to add
* to the collection with this function.
*
* str - the string containing the comma-delimited items.
*
* RETURNED: Nothing
*
***********************************************************************/
void SeparateList (StringList& stringList, string str, char delimiter)
{
string::size_type offset;
while ((offset = str.find (delimiter)) != string::npos)
{
string nextElement = str.substr (0, offset);
str = str.substr (offset + 1);
stringList.push_back (nextElement);
}
stringList.push_back (str);
}
/***********************************************************************
*
* FUNCTION: RunLengthEncode
*
* DESCRIPTION: Pack data according to the scheme used in QuickDraw's
* PackBits routine. This is the format, according to
* Macintosh Technote 1023:
*
* The first byte is a flag-counter byte that specifies
* whether or not the following data is packed, and the
* number of bytes involved.
*
* If this first byte is a negative number, the following
* data is packed and the number is a zero-based count of
* the number of times the data byte repeats when expanded.
* There is one data byte following the flag-counter byte
* in packed data; the byte after the data byte is the next
* flag-counter byte.
*
* If the flag-counter byte is a positive number, then the
* following data is unpacked and the number is a zero-based
* count of the number of incompressible data bytes that
* follow. There are (flag-counter+1) data bytes following
* the flag-counter byte. The byte after the last data byte
* is the next flag-counter byte.
*
* Consider the following example:
*
* Unpacked data:
*
* AA AA AA 80 00 2A AA AA AA AA 80 00
* 2A 22 AA AA AA AA AA AA AA AA AA AA
*
* After being packed by PackBits:
*
* FE AA ; (-(-2)+1) = 3 bytes of the pattern $AA
* 02 80 00 2A ; (2)+1 = 3 bytes of discrete data
* FD AA ; (-(-3)+1) = 4 bytes of the pattern $AA
* 03 80 00 2A 22 ; (3)+1 = 4 bytes of discrete data
* F7 AA ; (-(-9)+1) = 10 bytes of the pattern $AA
*
* or
*
* FE AA 02 80 00 2A FD AA 03 80 00 2A 22 F7 AA
* * * * * *
*
* The bytes with the asterisk (*) under them are the
* flag-counter bytes. PackBits packs the data only when
* there are three or more consecutive bytes with the same
* data; otherwise it just copies the data byte for byte
* (and adds the count byte).
*
* PARAMETERS: srcPP - pointer to the pointer to the source bytes. The
* referenced pointer gets udpated to point past the
* last byte included the packed output.
*
* dstPP - pointer to the pointer to the destination buffer.
* The referenced pointer gets updated to point past
* the last byte stored in the output buffer.
*
* srcBytes - length of the buffer referenced by srcPP
*
* RETURNED: Nothing.
*
***********************************************************************/
void RunLengthEncode (void** srcPP, void** dstPP, long srcBytes, long dstBytes)
{
UNUSED_PARAM(dstBytes)
enum { kBeginRun, kRepeatRun, kCopyRun };
uint8* srcP = (uint8*) *srcPP;
uint8* dstP = (uint8*) *dstPP;
uint8* opP = NULL;
long sample[3] = { -1, -1, -1 }; // Type must be > uint8 so that it can hold -1.
long opCount = 0;
long state = kBeginRun;
for (srcBytes += 1; srcBytes >= 0; --srcBytes)
{
sample[0] = sample[1];
sample[1] = sample[2];
sample[2] = -1;
if (srcBytes > 1)
{
sample[2] = *srcP++;
}
switch (state)
{
case kBeginRun: // Determine whether or not to pack the bytes
if (sample[2] == sample[0] && sample[2] == sample[1])
{
state = kRepeatRun;
opCount = -2;
}
else if (sample[0] != -1)
{
state = kCopyRun;
opCount = 0;
opP = dstP++;
*dstP++ = (uint8) sample[0];
}
break;
case kRepeatRun: // We're packing bytes
if (sample[2] == sample[1])
{
--opCount;
if (opCount > -127)
{
break;
}
sample[2] = -1;
}
sample[1] = -1;
*dstP++ = (uint8) opCount;
*dstP++ = (uint8) sample[0];
state = kBeginRun;
break;
case kCopyRun: // We're copying bytes
if (sample[0] != sample[1] || sample[0] != sample[2])
{
*dstP++ = (uint8) sample[0];
++opCount;
if (opCount >= 127)
{
*opP = (uint8) opCount;
state = kBeginRun;
break;
}
}
else
{
*opP = (uint8) opCount;
state = kRepeatRun;
opCount = -2;
}
break;
default:
EmAssert (false);
break;
}
}
if (state == kCopyRun)
{
*opP = (uint8) opCount;
}
*srcPP = (void*) srcP;
*dstPP = (void*) dstP;
}
/***********************************************************************
*
* FUNCTION: RunLengthDecode
*
* DESCRIPTION: Decode the data packed by RunLengthEncode.
*
* PARAMETERS: srcPP -
*
* dstPP -
*
* dstBytes -
*
* RETURNED: Nothing.
*
***********************************************************************/
void RunLengthDecode (void** srcPP, void** dstPP, long srcBytes, long dstBytes)
{
UNUSED_PARAM(srcBytes)
int8* srcP = (int8*) *srcPP;
int8* dstP = (int8*) *dstPP;
int8* limitP = dstP + dstBytes;
while (dstP < limitP)
{
int op = *srcP++;
if (op == -128)
{
// Nothing
}
else if (op >= 0)
{
int count = op + 1;
do
{
*dstP++ = *srcP++;
} while (--count);
}
else
{
int count = 1 - op;
uint8 fillData = *srcP++;
do
{
*dstP++ = fillData;
} while (--count);
}
}
*srcPP = (void*) srcP;
*dstPP = (void*) dstP;
}
/***********************************************************************
*
* FUNCTION: RunLengthWorstSize
*
* DESCRIPTION: Calculate the largest buffer needed when packing a
* buffer "srcBytes" long. The algorithm is based on
* that found in Macintosh Technote 1023.
*
* PARAMETERS: srcBytes - number of bytes in the buffer to be encoded.
*
* RETURNED: Largest buffer size needed to encode source buffer.
*
***********************************************************************/
long RunLengthWorstSize (long srcBytes)
{
long maxDestBytes = (srcBytes + (srcBytes + 126) / 127);
return maxDestBytes;
}
/***********************************************************************
*
* FUNCTION: GzipEncode
*
* DESCRIPTION: .
*
* PARAMETERS: srcPP - pointer to the pointer to the source bytes. The
* referenced pointer gets udpated to point past the
* last byte included the packed output.
*
* dstPP - pointer to the pointer to the destination buffer.
* The referenced pointer gets updated to point past
* the last byte stored in the output buffer.
*
* srcBytes - length of the buffer referenced by srcPP
*
* RETURNED: Nothing.
*
***********************************************************************/
void GzipEncode (void** srcPP, void** dstPP, long srcBytes, long dstBytes)
{
gSrcP = *srcPP;
gDstP = *dstPP;
gSrcBytes = srcBytes;
gDstBytes = dstBytes;
gSrcOffset = 0;
gDstOffset = 0;
bytes_in = srcBytes; // (for gzip internal debugging)
ush attr = 0; /* ascii/binary flag */
ush deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
int method;
clear_bufs ();
read_buf = &::PrvGzipReadProc;
write_buf_proc = &::PrvGzipWriteProc;
bi_init (NO_FILE);
ct_init (&attr, &method);
lm_init (level, &deflate_flags);
deflate ();
// Perform a put_byte(0) to pad out the
// compressed buffer. gzip apparently can skid off the
// end of the compressed data when inflating it, so we need
// an extra zero.
put_byte (0);
flush_outbuf ();
*srcPP = ((char*) gSrcP) + gSrcOffset;
*dstPP = ((char*) gDstP) + gDstOffset;
}
/***********************************************************************
*
* FUNCTION: GzipDecode
*
* DESCRIPTION: .
*
* PARAMETERS: srcPP -
*
* dstPP -
*
* dstBytes -
*
* RETURNED: Nothing.
*
***********************************************************************/
void GzipDecode (void** srcPP, void** dstPP, long srcBytes, long dstBytes)
{
gSrcP = *srcPP;
gDstP = *dstPP;
gSrcBytes = srcBytes;
gDstBytes = dstBytes;
gSrcOffset = 0;
gDstOffset = 0;
clear_bufs ();
read_buf = &::PrvGzipReadProc;
write_buf_proc = &::PrvGzipWriteProc;
inflate ();
*srcPP = ((char*) gSrcP) + gSrcOffset;
*dstPP = ((char*) gDstP) + gDstOffset;
}
/***********************************************************************
*
* FUNCTION: GzipWorstSize
*
* DESCRIPTION: .
*
* PARAMETERS: srcBytes - number of bytes in the buffer to be encoded.
*
* RETURNED: Largest buffer size needed to encode source buffer.
*
***********************************************************************/
long GzipWorstSize (long srcBytes)
{
long maxDestBytes = srcBytes * 2;
return maxDestBytes;
}
/***********************************************************************
*
* FUNCTION: PrvGzipReadProc
*
* DESCRIPTION: .
*
* PARAMETERS: .
*
* RETURNED: .
*
***********************************************************************/
int PrvGzipReadProc (char* buf, unsigned size)
{
if (gSrcOffset == gSrcBytes)
return EOF;
if (size > (unsigned) (gSrcBytes - gSrcOffset))
size = gSrcBytes - gSrcOffset;
if (size > 0)
{
memcpy (buf, ((char*) gSrcP) + gSrcOffset, size);
gSrcOffset += size;
}
return size;
}
/***********************************************************************
*
* FUNCTION: PrvGzipWriteProc
*
* DESCRIPTION: .
*
* PARAMETERS: .
*
* RETURNED: .
*
***********************************************************************/
int PrvGzipWriteProc (char* buf, unsigned size)
{
if (gDstOffset == gDstBytes)
return EOF;
if (size > (unsigned) (gDstBytes - gDstOffset))
size = gDstBytes - gDstOffset;
if (size > 0)
{
memcpy (((char*) gDstP) + gDstOffset, buf, size);
gDstOffset += size;
}
return size;
}
/***********************************************************************
*
* FUNCTION: StackCrawlStrings
*
* DESCRIPTION: .
*
* PARAMETERS: .
*
* RETURNED: .
*
***********************************************************************/
void StackCrawlStrings (const EmStackFrameList& stackCrawl, StringList& stackCrawlStrings)
{
EmStackFrameList::const_iterator iter = stackCrawl.begin ();
while (iter != stackCrawl.end ())
{
// Get the function name.
char funcName[256] = {0};
::FindFunctionName (iter->fAddressInFunction, funcName, NULL, NULL, 255);
// If we can't find the name, dummy one up.
if (strlen (funcName) == 0)
{
sprintf (funcName, "<Unknown @ 0x%08lX>", iter->fAddressInFunction);
}
stackCrawlStrings.push_back (string (funcName));
++iter;
}
}
/***********************************************************************
*
* FUNCTION: StackCrawlString
*
* DESCRIPTION: .
*
* PARAMETERS: .
*
* RETURNED: .
*
***********************************************************************/
string StackCrawlString (const EmStackFrameList& stackCrawl, long maxLen, Bool includeFrameSize, emuptr oldStackLow)
{
StringList strings;
::StackCrawlStrings (stackCrawl, strings);
string stackCrawlString;
EmStackFrameList::const_iterator iter = stackCrawl.begin ();
StringList::const_iterator s_iter = strings.begin ();
while (iter != stackCrawl.end ())
{
// Catenate the function name to the built-up string.
if (iter != stackCrawl.begin ())
{
stackCrawlString += ", ";
}
stackCrawlString += *s_iter;
if (includeFrameSize)
{
// Get the stack size used by the function.
char stackSize[20];
sprintf (stackSize, "%ld", iter->fA6 - oldStackLow);
stackCrawlString += string ("(") + string (stackSize) + ")";
}
// If the string looks long enough, stop.
if (maxLen > 0 && (long) stackCrawlString.size () > maxLen)
{
stackCrawlString += "...";
break;
}
oldStackLow = iter->fA6;
++iter;
++s_iter;
}
return stackCrawlString;
}
#pragma mark -
static int kBitCount[16] =
{
0, 1, 1, 2,
1, 2, 2, 3,
1, 2, 2, 3,
2, 3, 3, 4
};
int CountBits (uint32 v)
{
return kBitCount[ (v >> 0) & 0x0F ] +
kBitCount[ (v >> 4) & 0x0F ] +
kBitCount[ (v >> 8) & 0x0F ] +
kBitCount[ (v >> 12) & 0x0F ];
}
/***********************************************************************
*
* FUNCTION: NextPowerOf2
*
* DESCRIPTION: Calculates the next power of two above the given number
* If the given number is already a power of two, it is
* returned.
*
* PARAMETERS: n - probe number
*
* RETURNED: Next power of two above the probe number, or the number
* itself if it is a power of two.
*
***********************************************************************/
// Seven implementations! No waiting!
uint32 NextPowerOf2 (uint32 n)
{
// Smear down the upper 1 bit to all bits lower than it.
uint32 n2 = n;
n2 |= n2 >> 1;
n2 |= n2 >> 2;
n2 |= n2 >> 4;
n2 |= n2 >> 8;
n2 |= n2 >> 16;
// Now use itself to clear all the lower bits.
n2 &= ~(n2 >> 1);
// If n2 ends up being the same as what we started with, keep it.
// Otherwise, we need to bump it by a factor of two (round up).
if (n2 != n)
n2 <<= 1;
return n2;
}
#if 0
uint32 NextPowerOf2 (uint32 n)
{
uint32 startn = n;
uint32 prevn = 0;
while (n) // Loop until we're out of bits
{
prevn = n; // Remember what we're starting with. When "n"
// reaches zero, prevn will hold the previous value,
// which will have a single bit in it. Since we're
// whacking off bits from the bottom, this will be
// the highest bit.
n &= n - 1; // Mask off the low bit
}
// If prevn ends up being the same as what we started with, keep it.
// Otherwise, we need to bump it by a factor of two.
if (prevn != startn)
prevn <<= 1;
return prevn;
}
#endif
#if 0
// This was my own first attempt. Pretty lame...
uint32 NextPowerOf2 (uint32 x)
{
// Figure out the next power-of-2 higher than or equal to 'x'. We do
// this by continually shifting x to the left until we get a '1' in
// the upper bit. At the same time, we shift 0x80000000 to the right.
// When we find that '1' in the upper bit, the shifted 0x80000000
// pattern should hold our power-of-two.
//
// That approach is good for finding the next power-of-2 higher than
// a given value, so in order to deal with the 'or equal to' part
// of the task, we decrement x by 1 before embarking on this adventure.
//
// This function'll fail for x == 0 or x == 1, as well x > 0x80000000,
// so handle those cases up front:
if (x > 0x80000000)
return -1;
if (x == 0 || x == 1)
return x;
--x;
unsigned long result = 0x80000000;
while (((x <<= 1) & 0x80000000) == 0) // check for the highest set bit.
result >>= 1;
return result;
}
#endif
#if 0
// This one was posted to the net. Seems most reasonable.
// Fails when n == 0.
uint32 HighBitNumber (uint32 n)
{
uint32 i = (n & 0xffff0000) ? 16 : 0;
if ((n >>= i) & 0xff00)
{
i |= 8;
n >>= 8;
}
if (n & 0xf0)
{
i |= 4;
n >>= 4;
}
if (n & 0xc)
{
i |= 2;
n >>= 2;
}
return (i | (n >> 1));
}
#endif
#if 0
// This one was posted to the net. Uses a loop; not quite as effecient.
// Seems pretty buggy, since it doesn't work for x == 0, 1, or 2.
uint32 HighBitNumber (uint32 x)
{
unsigned long mask=2, numBits=1;
while (mask < x)
{
mask += mask;
numBits++;
}
return numBits;
}
#endif
#if 0
// This one was posted to the net. Makes up to 5 comparisons, which is
// more than the one we're using.
uint32 HighBitNumber (uint32 x)
{
#define hi_bit(n)\
((n)>=1<<16?(n)>=1<<24?(n)>=1<<28?(n)>=1<<30?(n)>=1<<31?31:30:(n)>=1<<29?\
29:28:(n)>=1<<26?(n)>=1<<27?27:26:(n)>=1<<25?25:24:(n)>=1<<20?(n)>=1<<22?\
(n)>=1<<23?23:22:(n)>=1<<21?21:20:(n)>=1<<18?(n)>=1<<19?19:18:(n)>=1<<17?\
17:16:(n)>=1<<8?(n)>=1<<12?(n)>=1<<14?(n)>=1<<15?15:14:(n)>=1<<13?13:12:(\
n)>=1<<10?(n)>=1<<11?11:10:(n)>=1<<9?9:8:(n)>=1<<4?(n)>=1<<6?(n)>=1<<7?7:\
6:(n)>=1<<5?5:4:(n)>=1<<2?(n)>=1<<3?3:2:(n)>=1<<1?1:(n)>=1<<0?0:-1)
return hi_bit (x);
}
#endif
#if 0
// This one was posted to the net (by the same guy posting that macro).
// Pretty neat until that divide by 37.
uint32 HighBitNumber (uint32 x)
{
static const int t[] =
{
-1, 0, 25, 1, 22, 26, 31, 2, 15, 23, 29, 27, 10, -1, 12, 3, 6, 16,
-1, 24, 21, 30, 14, 28, 9, 11, 5, -1, 20, 13, 8, 4, 19, 7, 18, 17
};
return t[(n |= n >> 1, n |= n >> 2, n |= n >> 4, n |= n >> 8, n |= n >> 16) % 37];
}
#endif
/***********************************************************************
*
* FUNCTION: DateToDays
*
* DESCRIPTION: Convert a year, month, and day into the number of days
* since 1/1/1904.
*
* Parameters are not checked for valid dates, so it's
* possible to feed in things like March 35, 1958. This
* function also assumes that year is at least 1904, and
* will only work up until 2040 or so.
*
* PARAMETERS: year - full year
*
* month - 1..12
*
* day - 1..31
*
* RETURNED: Number of days since 1/1/1904.
*
***********************************************************************/
uint32 DateToDays (uint32 year, uint32 month, uint32 day)
{
static const int month2days[] =
{
0, 31, 59, 90, 120, 151,
181, 212, 243, 273, 304, 334
};
// Normalize the values.
year -= 1904;
month -= 1;
day -= 1;
// Not counting any possible leap-day in the current year, figure out
// the number of days between now and 1/1/1904.
const uint32 kNumDaysInLeapCycle = 4 * 365 + 1;
uint32 days = day + month2days[month] +
(year * kNumDaysInLeapCycle + 3) / 4;
// Now add in this year's leap-day, if there is one.
if ((month >= 2) && ((year & 3) == 0))
days++;
return days;
}
/***********************************************************************
*
* FUNCTION: GetLibraryName
*
* DESCRIPTION:
*
* PARAMETERS: none
*
* RETURNED: The libraries name, or an empty string if the library
* could not be found.
*
***********************************************************************/
string GetLibraryName (uint16 refNum)
{
if (refNum == sysInvalidRefNum)
return string();
CEnableFullAccess munge; // Remove blocks on memory access.
/*
The System Library Table (sysLibTableP) is an array of
sysLibTableEntries entries. Each entry has the following
format:
Ptr* dispatchTblP; // pointer to library dispatch table
void* globalsP; // Library globals
LocalID dbID; // database id of the library
MemPtr codeRscH; // library code resource handle for RAM-based libraries
The latter two fields are present only in Palm OS 2.0 and
later. So our first steps are to (a) get the pointer to
the array, (b) make sure that the index into the array (the
refNum passed as the first parameter to all library calls)
is within range, (c) get a pointer to the right entry,
taking into account the Palm OS version, and (d) getting the
dispatchTblP field.
The "library dispatch table" is an array of 16-bit offsets. The
values are all relative to the beginning of the table (dispatchTblP).
The first entry in the array corresponds to the library name. All
subsequent entries are offsets to the various library functions,
starting with the required four: sysLibTrapOpen, sysLibTrapClose,
sysLibTrapSleep, and sysLibTrapWake.
*/
emuptr sysLibTableP = EmLowMem_GetGlobal (sysLibTableP);
UInt16 sysLibTableEntries = EmLowMem_GetGlobal (sysLibTableEntries);
if (sysLibTableP == EmMemNULL)
{
// !!! No library table!
EmAssert (false);
return string();
}
if (refNum >= sysLibTableEntries)
{
if (refNum != 0x0666)
{
// !!! RefNum out of range!
EmAssert (false);
}
return string();
}
emuptr libEntry;
emuptr dispatchTblP;
if (EmPatchState::OSMajorVersion () > 1)
{
libEntry = sysLibTableP + refNum * sizeof (SysLibTblEntryType);
dispatchTblP = EmMemGet32 (libEntry + offsetof (SysLibTblEntryType, dispatchTblP));
}
else
{
libEntry = sysLibTableP + refNum * sizeof (SysLibTblEntryTypeV10);
dispatchTblP = EmMemGet32 (libEntry + offsetof (SysLibTblEntryTypeV10, dispatchTblP));
}
// The first entry in the table is always the offset from the
// start of the table to the library name. Use this information
// get the library name.
int16 offset = EmMemGet16 (dispatchTblP + LibTrapIndex (sysLibTrapName) * 2);
emuptr libNameP = dispatchTblP + offset;
char libName[256];
EmMem_strcpy (libName, libNameP);
return string (libName);
}
/***********************************************************************
*
* FUNCTION: GetSystemCallContext
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
Bool GetSystemCallContext (emuptr pc, SystemCallContext& context)
{
context.fPC = pc;
// Determine how the system function is being called. There are two ways:
//
// * Via SYS_TRAP macro:
//
// TRAP $F
// DC.W $Axxx
//
// * Via SYS_TRAP_FAST macro:
//
// MOVE.L struct(LowMemType.fixed.globals.sysDispatchTableP), A1
// MOVE.L ((trapNum-sysTrapBase)*4)(A1), A1
// JSR (A1) ; opcode == 0x4e91
//
// The PC is current pointing to either the TRAP $F or the JSR (A1),
// so we can look at the opcode to determine how we got here.
uint8* realMem = EmMemGetRealAddress (pc);
uint16 opcode = EmMemDoGet16 (realMem);
context.fViaTrap = opcode == (m68kTrapInstr + sysDispatchTrapNum);
context.fViaJsrA1 = opcode == (0x4e91);
if (context.fViaTrap)
{
// Not all development systems generate the correct dispatch
// numbers; some leave off the preceding "A". Make sure it's
// set so that we can recognize it as a trap dispatch number.
// (This code is here specifically so that the profiling routines
// will work, which check for trap numbers masquerading as function
// addresses by checking to see if they are in the sysTrapBase range.)
context.fTrapWord = EmMemGet16 (pc + 2) | sysTrapBase;
context.fNextPC = pc + 4;
}
else if (context.fViaJsrA1)
{
context.fTrapWord = (EmMemGet16 (pc - 2) / 4) | sysTrapBase;
context.fNextPC = pc + 2;
}
else
{
EmAssert (false);
return false;
}
if (::IsSystemTrap (context.fTrapWord))
{
context.fTrapIndex = SysTrapIndex (context.fTrapWord);
context.fExtra = m68k_dreg (regs, 2);
}
else
{
context.fTrapIndex = LibTrapIndex (context.fTrapWord);
context.fExtra = EmMemGet16 (m68k_areg (regs, 7));
}
EmAssert ((context.fTrapWord >= sysTrapBase) && (context.fTrapWord < sysTrapBase + 0x1000));
try
{
context.fError = 0;
context.fDestPC1 = ::GetFunctionAddress (context.fTrapWord, context.fExtra, false);
context.fDestPC2 = ::GetFunctionAddress (context.fTrapWord, context.fExtra, true);
}
catch (EmUnimplementedFunctionException& e)
{
context.fDestPC1 = EmMemNULL;
context.fDestPC2 = EmMemNULL;
context.fError = kError_UnimplementedTrap;
context.fLibIndex = e.fLibIndex;
return false;
}
catch (EmInvalidRefNumException& e)
{
context.fDestPC1 = EmMemNULL;
context.fDestPC2 = EmMemNULL;
context.fError = kError_InvalidLibraryRefNum;
context.fLibIndex = e.fLibIndex;
return false;
}
return true;
}
/***********************************************************************
*
* FUNCTION: GetHostTime
*
* DESCRIPTION: Returns the current time in hours, minutes, and seconds.
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void GetHostTime (long* hour, long* min, long* sec)
{
time_t t;
struct tm tm;
time (&t);
tm = *localtime (&t);
*hour = tm.tm_hour; // 0...23
*min = tm.tm_min; // 0...59
*sec = tm.tm_sec; // 0...59
}
/***********************************************************************
*
* FUNCTION: GetHostDate
*
* DESCRIPTION: Returns years since 1900, month as 0-11, and day as 1-31
*
* PARAMETERS: none
*
* RETURNED: nothing
*
***********************************************************************/
void GetHostDate (long* year, long* month, long* day)
{
time_t t;
struct tm tm;
time (&t);
tm = *localtime (&t);
*year = tm.tm_year + 1900; // 1904...2040
*month = tm.tm_mon + 1; // 1...12
*day = tm.tm_mday; // 1...31
}
/***********************************************************************
*
* FUNCTION: StartsWith
*
* DESCRIPTION: Determine if a string starts with the given pattern.
*
* PARAMETERS: s - string to test.
*
* p - pattern to test with.
*
* RETURNED: True if "s" starts with "p".
*
***********************************************************************/
Bool StartsWith (const char* s, const char* p)
{
if (strlen (s) < strlen (p))
return false;
return (_strnicmp (s, p, strlen (p)) == 0);
}
/***********************************************************************
*
* FUNCTION: EndsWith
*
* DESCRIPTION: Determine if a string end with the given pattern.
*
* PARAMETERS: s - string to test.
*
* p - pattern to test with.
*
* RETURNED: True if "s" ends with "p".
*
***********************************************************************/
Bool EndsWith (const char* s, const char* p)
{
if (strlen (s) < strlen (p))
return false;
const char* buffer = s + strlen(s) - strlen(p);
return (_stricmp (buffer, p) == 0);
}
/***********************************************************************
*
* FUNCTION: Strip
*
* DESCRIPTION: Remove leading or trailing instances of the given
* character(s).
*
* PARAMETERS: s - string to modify.
*
* ch - character(s) to remove.
*
* leading - true if leading characters should be removed.
*
* trailing - true if trailing characters should be removed.
*
* RETURNED: Modified string.
*
***********************************************************************/
string Strip (const char* s, const char* ch, Bool leading, Bool trailing)
{
string result (s);
string chars (ch);
if (leading)
{
// Iterate over the string, looking to see if the leading character
// is in the given character set.
string::iterator iter = result.begin ();
while (iter != result.end ())
{
if (chars.find (*iter) == string::npos)
break;
result.erase (iter);
iter = result.begin ();
}
}
if (trailing)
{
// Iterate over the string, looking to see if the trailing character
// is in the given character set.
string::reverse_iterator iter = result.rbegin ();
while (iter != result.rend ())
{
if (chars.find (*iter) == string::npos)
break;
result.erase (iter.base () - 1);
iter = result.rbegin ();
}
}
return result;
}
string Strip (const string& s, const char* ch, Bool leading, Bool trailing)
{
return ::Strip (s.c_str (), ch, leading, trailing);
}
/***********************************************************************
*
* FUNCTION: ReplaceString
*
* DESCRIPTION: Replace all occurances of one string inside of a second
* string with a third string.
*
* PARAMETERS: source - the string on which the replacements are to
* be made.
*
* pattern - substring to search for.
*
* replacement - string with which to replace the patterm.
*
* RETURNED: The changed string.
*
***********************************************************************/
string ReplaceString (const string& source,
const string& pattern,
const string& replacement)
{
string result (source);
string::size_type pos = 0;
for (;;)
{
pos = result.find (pattern, pos);
if (pos == string::npos)
break;
result.replace (pos, pattern.size (), replacement);
pos += replacement.size ();
}
return result;
}
/***********************************************************************
*
* FUNCTION: PrvInsertString
*
* DESCRIPTION: Insert one string into the middle of another. The
* buffer containing the string being modified should be
* large enough to hold the resulting string.
*
* PARAMETERS: dest - buffer containing the string to receive the
* inserted text.
*
* insertBefore - character index at which the inserted
* text should appear. Assumed to be [0, strlen(dest)].
*
* src - text to be inserted.
*
* RETURNED: nothing
*
***********************************************************************/
static void PrvInsertString (char* dest, int insertBefore, const char* src)
{
size_t destLen = strlen (dest);
size_t srcLen = strlen (src);
// Move down the part of the string after the insertion point. Don't
// use strcpy or memcpy, as we're dealing with an overlapping range.
// Add 1 to the range length to get the terminating NULL.
memmove (dest + insertBefore + srcLen, dest + insertBefore, destLen - insertBefore + 1);
// Insert the new string. *Don't* copy the NULL!
memmove (dest + insertBefore, src, srcLen);
}
/***********************************************************************
*
* FUNCTION: FormatInteger
*
* DESCRIPTION: Convert an integral value into a formatted string,
* adding thousands seperators as necessary.
*
* PARAMETERS: dest - buffer for the outputted string
*
* integer - input value
*
* RETURNED: nothing
*
***********************************************************************/
void FormatInteger (char* dest, uint32 integer)
{
sprintf (dest, "%ld", integer);
// Get the thousands separator character(s).
struct lconv* locale_data = localeconv ();
char* thousands_sep = locale_data->thousands_sep;
if (strlen (thousands_sep) == 0)
{
thousands_sep = ",";
}
// Insert the thousands separator(s).
// Divide by three to get the number of 3 digit groupings remaining
// (subtracting one to get the math to come out right)
//
// 1 -> 0
// 2 -> 0
// 3 -> 0
// 4 -> 1
// 5 -> 1
// 6 -> 1
// 7 -> 2
// 8 -> 2
// 9 -> 2
// 10 -> 3
//
// etc...
int numCommas = (strlen (dest) - 1) / 3;
// Special case the stupid rule about not putting a comma
// in a number like xxxx.
if (strlen (dest) <= 4)
{
numCommas = 0;
}
for (int ii = 1; ii <= numCommas; ++ii)
{
// Back up four for every comma (skip past every ",xxx" pattern).
::PrvInsertString (dest, strlen (dest) + 1 - (4 * ii), thousands_sep);
}
}
string FormatInteger (uint32 integer)
{
// Format the integer as a plain string.
strstream stream;
stream << integer;
string result (stream.str (), stream.pcount ());
// Unfreeze the stream, or else its storage will be leaked.
stream.freeze (false);
// Get the thousands separator character(s).
struct lconv* locale_data = localeconv ();
char* thousands_sep = locale_data->thousands_sep;
if (strlen (thousands_sep) == 0)
{
thousands_sep = ",";
}
// Insert the thousands separator(s).
// Divide by three to get the number of 3 digit groupings remaining
// (subtracting one to get the math to come out right)
//
// 1 -> 0
// 2 -> 0
// 3 -> 0
// 4 -> 1
// 5 -> 1
// 6 -> 1
// 7 -> 2
// 8 -> 2
// 9 -> 2
// 10 -> 3
//
// etc...
int numCommas = (result.size () - 1) / 3;
// Special case the stupid rule about not putting a comma
// in a number like xxxx.
if (result.size () <= 4)
{
numCommas = 0;
}
for (int ii = 1; ii <= numCommas; ++ii)
{
// Back up four for every comma (skip past every ",xxx" pattern).
result.insert (result.size () + 1 - (4 * ii), thousands_sep);
}
return result;
}
string FormatElapsedTime (uint32 mSecs)
{
// Get hours, minutes, and seconds.
const long kMillisecondsPerSecond = 1000;
const long kSecondsPerMinute = 60;
const long kMinutesPerHour = 60;
const long kMillisecondsPerMinute = kMillisecondsPerSecond * kSecondsPerMinute;
const long kMillisecondsPerHour = kMillisecondsPerMinute * kMinutesPerHour;
long hours = mSecs / kMillisecondsPerHour; mSecs -= hours * kMillisecondsPerHour;
long minutes = mSecs / kMillisecondsPerMinute; mSecs -= minutes * kMillisecondsPerMinute;
long seconds = mSecs / kMillisecondsPerSecond; mSecs -= seconds * kMillisecondsPerSecond;
// Format them into a string.
char formattedTime[20];
sprintf (formattedTime, "%ld:%02ld:%02ld", hours, minutes, seconds);
return string (formattedTime);
}
/***********************************************************************
*
* FUNCTION: LaunchCmdToString
*
* DESCRIPTION: Convert the given launch command (the command that's
* passed to PilotMain in a Palm OS application) into a
* text form suitable for displaying to a user.
*
* PARAMETERS: cmd - the launch command (from SystemMgr.h)
*
* RETURNED: A pointer to a string representing the command. If
* the command is unrecognized (probably because this
* function is out-of-date and needs to be updated for
* newly-added commands), a default string containing
* the command number is returned.
*
***********************************************************************/
const char* LaunchCmdToString (UInt16 cmd)
{
#undef DOCASE
#define DOCASE(name) \
case name: \
return #name;
switch (cmd)
{
DOCASE (sysAppLaunchCmdNormalLaunch)
DOCASE (sysAppLaunchCmdFind)
DOCASE (sysAppLaunchCmdGoTo)
DOCASE (sysAppLaunchCmdSyncNotify)
DOCASE (sysAppLaunchCmdTimeChange)
DOCASE (sysAppLaunchCmdSystemReset)
DOCASE (sysAppLaunchCmdAlarmTriggered)
DOCASE (sysAppLaunchCmdDisplayAlarm)
DOCASE (sysAppLaunchCmdCountryChange)
DOCASE (sysAppLaunchCmdSyncRequestLocal)
DOCASE (sysAppLaunchCmdSaveData)
DOCASE (sysAppLaunchCmdInitDatabase)
DOCASE (sysAppLaunchCmdSyncCallApplicationV10)
DOCASE (sysAppLaunchCmdPanelCalledFromApp)
DOCASE (sysAppLaunchCmdReturnFromPanel)
DOCASE (sysAppLaunchCmdLookup)
DOCASE (sysAppLaunchCmdSystemLock)
DOCASE (sysAppLaunchCmdSyncRequestRemote)
DOCASE (sysAppLaunchCmdHandleSyncCallApp)
DOCASE (sysAppLaunchCmdAddRecord)
DOCASE (sysSvcLaunchCmdSetServiceID)
DOCASE (sysSvcLaunchCmdGetServiceID)
DOCASE (sysSvcLaunchCmdGetServiceList)
DOCASE (sysSvcLaunchCmdGetServiceInfo)
DOCASE (sysAppLaunchCmdFailedAppNotify)
DOCASE (sysAppLaunchCmdEventHook)
DOCASE (sysAppLaunchCmdExgReceiveData)
DOCASE (sysAppLaunchCmdExgAskUser)
DOCASE (sysDialLaunchCmdDial)
DOCASE (sysDialLaunchCmdHangUp)
DOCASE (sysSvcLaunchCmdGetQuickEditLabel)
DOCASE (sysAppLaunchCmdURLParams)
DOCASE (sysAppLaunchCmdNotify)
DOCASE (sysAppLaunchCmdOpenDB)
DOCASE (sysAppLaunchCmdAntennaUp)
DOCASE (sysAppLaunchCmdGoToURL)
}
static char buffer[20];
sprintf (buffer, "#%ld", (long) cmd);
return buffer;
}
/***********************************************************************
*
* FUNCTION: GetMemoryList
*
* DESCRIPTION: Return the list of items that should appear in the
* various listboxes and menus that allow the user to
* select a memory size. The list is returned as a
* collection of pairs. The first element of the pair is
* the memory size (in K), and the second element of the
* pair is the same thing in textform, suitable for
* displaying to the user.
*
* PARAMETERS: memoryList - reference to the collection to receive
* the results.
*
* RETURNED: Nothing
*
***********************************************************************/
void GetMemoryTextList (MemoryTextList& memoryList)
{
memoryList.push_back (make_pair (RAMSizeType (128), string ("128K")));
memoryList.push_back (make_pair (RAMSizeType (256), string ("256K")));
memoryList.push_back (make_pair (RAMSizeType (512), string ("512K")));
memoryList.push_back (make_pair (RAMSizeType (1024), string ("1024K")));
memoryList.push_back (make_pair (RAMSizeType (2048), string ("2048K")));
memoryList.push_back (make_pair (RAMSizeType (4096), string ("4096K")));
memoryList.push_back (make_pair (RAMSizeType (8192), string ("8192K")));
memoryList.push_back (make_pair (RAMSizeType (16384), string ("16,384K")));
}
/***********************************************************************
*
* FUNCTION: MyAssertFailed
*
* DESCRIPTION: Called by the EmAssert macro if NDEBUG is defined.
* EmAssert and MyAssertFailed are moral replacements for
* the assert macro of the Standard C Library and the
* underlying function it calls. However, those facilities
* don't necessarily work the way we'd like. In
* particular, on Linux, they will abort the application.
* Sometimes, we'd like the ability to continue application
* execution.
*
* PARAMETERS: exp - expression that evaluate to false.
* file - name of file containing the expression.
* line - line number in the file containing the expression.
*
* RETURNED: Nothing
*
***********************************************************************/
void MyAssertFailed (const char* expr, const char* file, unsigned int line)
{
char message[2000];
sprintf (message, "Assertion Failure: Expression: \"%s\", File: %s, Line: %d",
expr, file, line);
LogDump ();
Platform::Debugger (message);
}
|