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
|
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N N TTTTT %
% NN N T %
% N N N T %
% N NN T %
% N N T %
% %
% %
% Windows NT Utility Methods for MagickCore %
% %
% Software Design %
% Cristy %
% December 1996 %
% %
% %
% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
% dedicated to making software imaging solutions freely available. %
% %
% You may not use this file except in compliance with the License. You may %
% obtain a copy of the License at %
% %
% https://imagemagick.org/script/license.php %
% %
% Unless required by applicable law or agreed to in writing, software %
% distributed under the License is distributed on an "AS IS" BASIS, %
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
% See the License for the specific language governing permissions and %
% limitations under the License. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/
/*
Include declarations.
*/
#include "MagickCore/studio.h"
#if defined(MAGICKCORE_WINDOWS_SUPPORT)
#include "MagickCore/client.h"
#include "MagickCore/distribute-cache-private.h"
#include "MagickCore/exception-private.h"
#include "MagickCore/image-private.h"
#include "MagickCore/locale_.h"
#include "MagickCore/log.h"
#include "MagickCore/magick.h"
#include "MagickCore/memory_.h"
#include "MagickCore/memory-private.h"
#include "MagickCore/nt-base.h"
#include "MagickCore/nt-base-private.h"
#include "MagickCore/resource_.h"
#include "MagickCore/resource-private.h"
#include "MagickCore/timer.h"
#include "MagickCore/string_.h"
#include "MagickCore/string-private.h"
#include "MagickCore/utility.h"
#include "MagickCore/utility-private.h"
#include "MagickCore/version.h"
#if defined(MAGICKCORE_LTDL_DELEGATE)
# include "ltdl.h"
#endif
#if defined(MAGICKCORE_CIPHER_SUPPORT)
#include <ntsecapi.h>
#include <wincrypt.h>
#endif
/*
Define declarations.
*/
#if !defined(MAP_FAILED)
#define MAP_FAILED ((void *)(LONG_PTR)-1)
#endif
#define MaxWideByteExtent 100
/*
Typedef declarations.
*/
/*
We need to make sure only one instance is created for each process and that
is why we wrap the new/delete instance methods.
From: http://www.ghostscript.com/doc/current/API.htm
"The Win32 DLL gsdll32.dll can be used by multiple programs simultaneously,
but only once within each process"
*/
typedef struct _NTGhostInfo
{
void
(MagickDLLCall *delete_instance)(gs_main_instance *);
int
(MagickDLLCall *new_instance)(gs_main_instance **,void *);
MagickBooleanType
has_instance;
} NTGhostInfo;
/*
Static declarations.
*/
static NTGhostInfo
nt_ghost_info;
static GhostInfo
ghost_info;
static void
*ghost_handle = (void *) NULL;
static SemaphoreInfo
*ghost_semaphore = (SemaphoreInfo *) NULL;
struct
{
const HKEY
hkey;
const char
*name;
}
const registry_roots[2] =
{
{ HKEY_CURRENT_USER, "HKEY_CURRENT_USER" },
{ HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" }
};
/*
External declarations.
*/
static void MagickDLLCall NTGhostscriptDeleteInstance(
gs_main_instance *instance)
{
LockSemaphoreInfo(ghost_semaphore);
nt_ghost_info.delete_instance(instance);
nt_ghost_info.has_instance=MagickFalse;
UnlockSemaphoreInfo(ghost_semaphore);
}
static int MagickDLLCall NTGhostscriptNewInstance(gs_main_instance **pinstance,
void *caller_handle)
{
int
status;
LockSemaphoreInfo(ghost_semaphore);
status=-1;
if (nt_ghost_info.has_instance == MagickFalse)
{
status=nt_ghost_info.new_instance(pinstance,caller_handle);
if (status >= 0)
nt_ghost_info.has_instance=MagickTrue;
}
UnlockSemaphoreInfo(ghost_semaphore);
return(status);
}
static inline char *create_utf8_string(const wchar_t *wide)
{
char
*utf8;
int
count;
count=WideCharToMultiByte(CP_UTF8,0,wide,-1,NULL,0,NULL,NULL);
if (count == 0)
return((char *) NULL);
utf8=(char *) NTAcquireQuantumMemory(count+1,sizeof(*utf8));
if (utf8 == (char *) NULL)
return((char *) NULL);
count=WideCharToMultiByte(CP_UTF8,0,wide,-1,utf8,count,NULL,NULL);
if (count == 0)
{
utf8=(char *) RelinquishMagickMemory(utf8);
return((char *) NULL);
}
utf8[count]=0;
return(utf8);
}
static unsigned char *NTGetRegistryValue(HKEY root,const char *key,DWORD flags,
const char *name)
{
unsigned char
*value;
HKEY
registry_key;
DWORD
size,
type;
LSTATUS
status;
wchar_t
wide_name[MaxWideByteExtent];
value=(unsigned char *) NULL;
status=RegOpenKeyExA(root,key,0,(KEY_READ | flags),®istry_key);
if (status != ERROR_SUCCESS)
return(value);
if (MultiByteToWideChar(CP_UTF8,0,name,-1,wide_name,MaxWideByteExtent) == 0)
{
RegCloseKey(registry_key);
return(value);
}
status=RegQueryValueExW(registry_key,wide_name,0,&type,0,&size);
if ((status == ERROR_SUCCESS) && (type == REG_SZ))
{
LPBYTE
wide;
wide=(LPBYTE) NTAcquireQuantumMemory((const size_t) size,sizeof(*wide));
if (wide != (LPBYTE) NULL)
{
status=RegQueryValueExW(registry_key,wide_name,0,&type,wide,&size);
if ((status == ERROR_SUCCESS) && (type == REG_SZ))
value=(unsigned char *) create_utf8_string((const wchar_t *) wide);
wide=(LPBYTE) RelinquishMagickMemory(wide);
}
}
RegCloseKey(registry_key);
return(value);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% D l l M a i n %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DllMain() is an entry point to the DLL which is called when processes and
% threads are initialized and terminated, or upon calls to the Windows
% LoadLibrary and FreeLibrary functions.
%
% The function returns TRUE of it succeeds, or FALSE if initialization fails.
%
% The format of the DllMain method is:
%
% BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
%
% A description of each parameter follows:
%
% o handle: handle to the DLL module
%
% o reason: reason for calling function:
%
% DLL_PROCESS_ATTACH - DLL is being loaded into virtual address
% space of current process.
% DLL_THREAD_ATTACH - Indicates that the current process is
% creating a new thread. Called under the
% context of the new thread.
% DLL_THREAD_DETACH - Indicates that the thread is exiting.
% Called under the context of the exiting
% thread.
% DLL_PROCESS_DETACH - Indicates that the DLL is being unloaded
% from the virtual address space of the
% current process.
%
% o lpvReserved: Used for passing additional info during DLL_PROCESS_ATTACH
% and DLL_PROCESS_DETACH.
%
*/
#if defined(_DLL) && defined(ProvideDllMain)
BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
{
char
*module_path;
ssize_t
count;
wchar_t
*wide_path;
MagickCoreGenesis((const char *) NULL,MagickFalse);
wide_path=(wchar_t *) NTAcquireQuantumMemory(MagickPathExtent,
sizeof(*wide_path));
if (wide_path == (wchar_t *) NULL)
return(FALSE);
count=(ssize_t) GetModuleFileNameW(handle,wide_path,MagickPathExtent);
if (count != 0)
{
char
*path;
module_path=create_utf8_string(wide_path);
for ( ; count > 0; count--)
if (module_path[count] == '\\')
{
module_path[count+1]='\0';
break;
}
path=(char *) NTAcquireQuantumMemory(MagickPathExtent,
16*sizeof(*path));
if (path == (char *) NULL)
{
module_path=DestroyString(module_path);
wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
return(FALSE);
}
count=(ssize_t) GetEnvironmentVariable("PATH",path,16*
MagickPathExtent);
if ((count != 0) && (strstr(path,module_path) == (char *) NULL))
{
if ((strlen(module_path)+count+1) < (16*MagickPathExtent-1))
{
char
*variable;
variable=(char *) NTAcquireQuantumMemory(MagickPathExtent,
16*sizeof(*variable));
if (variable == (char *) NULL)
{
path=DestroyString(path);
module_path=DestroyString(module_path);
wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
return(FALSE);
}
(void) FormatLocaleString(variable,16*MagickPathExtent,
"%s;%s",module_path,path);
SetEnvironmentVariable("PATH",variable);
variable=DestroyString(variable);
}
}
path=DestroyString(path);
module_path=DestroyString(module_path);
}
wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
break;
}
case DLL_PROCESS_DETACH:
{
MagickCoreTerminus();
break;
}
default:
break;
}
return(TRUE);
}
#endif
#if !defined(__MINGW32__)
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% g e t t i m e o f d a y %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% The gettimeofday() method get the time of day.
%
% The format of the gettimeofday method is:
%
% int gettimeofday(struct timeval *time_value,struct timezone *time_zone)
%
% A description of each parameter follows:
%
% o time_value: the time value.
%
% o time_zone: the time zone.
%
*/
MagickPrivate int gettimeofday (struct timeval *time_value,
struct timezone *time_zone)
{
#define EpochFiletime MagickLLConstant(116444736000000000)
static int
is_tz_set;
if (time_value != (struct timeval *) NULL)
{
FILETIME
file_time;
__int64
time;
LARGE_INTEGER
date_time;
GetSystemTimeAsFileTime(&file_time);
date_time.LowPart=file_time.dwLowDateTime;
date_time.HighPart=file_time.dwHighDateTime;
time=date_time.QuadPart;
time-=EpochFiletime;
time/=10;
time_value->tv_sec=(ssize_t) (time / 1000000);
time_value->tv_usec=(ssize_t) (time % 1000000);
}
if (time_zone != (struct timezone *) NULL)
{
if (is_tz_set == 0)
{
_tzset();
is_tz_set++;
}
time_zone->tz_minuteswest=_timezone/60;
time_zone->tz_dsttime=_daylight;
}
return(0);
}
#endif
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T A r g v T o U T F 8 %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTArgvToUTF8() converts the wide command line arguments to UTF-8 to ensure
% compatibility with Linux.
%
% The format of the NTArgvToUTF8 method is:
%
% char **NTArgvToUTF8(const int argc,wchar_t **argv)
%
% A description of each parameter follows:
%
% o argc: the number of command line arguments.
%
% o argv: the wide-character command line arguments.
%
*/
MagickPrivate char **NTArgvToUTF8(const int argc,wchar_t **argv)
{
char
**utf8;
ssize_t
i;
utf8=(char **) NTAcquireQuantumMemory(argc,sizeof(*utf8));
if (utf8 == (char **) NULL)
ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
for (i=0; i < (ssize_t) argc; i++)
{
utf8[i]=create_utf8_string(argv[i]);
if (utf8[i] == (char *) NULL)
{
for (i--; i >= 0; i--)
utf8[i]=DestroyString(utf8[i]);
ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertStringToARGV");
}
}
return(utf8);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T C l o s e D i r e c t o r y %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTCloseDirectory() closes the named directory stream and frees the DIR
% structure.
%
% The format of the NTCloseDirectory method is:
%
% int NTCloseDirectory(DIR *entry)
%
% A description of each parameter follows:
%
% o entry: Specifies a pointer to a DIR structure.
%
*/
MagickPrivate int NTCloseDirectory(DIR *entry)
{
assert(entry != (DIR *) NULL);
if (IsEventLogging() != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
FindClose(entry->hSearch);
entry=(DIR *) RelinquishMagickMemory(entry);
return(0);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T C l o s e L i b r a r y %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTCloseLibrary() unloads the module associated with the passed handle.
%
% The format of the NTCloseLibrary method is:
%
% void NTCloseLibrary(void *handle)
%
% A description of each parameter follows:
%
% o handle: Specifies a handle to a previously loaded dynamic module.
%
*/
MagickPrivate int NTCloseLibrary(void *handle)
{
return(!(FreeLibrary((HINSTANCE) handle)));
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T E l a p s e d T i m e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTElapsedTime() returns the elapsed time (in seconds) since the last call to
% StartTimer().
%
% The format of the ElapsedTime method is:
%
% double NTElapsedTime(void)
%
*/
MagickPrivate double NTElapsedTime(void)
{
union
{
FILETIME
filetime;
__int64
filetime64;
} elapsed_time;
LARGE_INTEGER
performance_count;
static LARGE_INTEGER
frequency = { 0 };
SYSTEMTIME
system_time;
if (frequency.QuadPart == 0)
{
if (QueryPerformanceFrequency(&frequency) == 0)
frequency.QuadPart=1;
}
if (frequency.QuadPart > 1)
{
QueryPerformanceCounter(&performance_count);
return((double) performance_count.QuadPart/frequency.QuadPart);
}
GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
return((double) 1.0e-7*elapsed_time.filetime64);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T E r f %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTErf() computes the error function of x.
%
% The format of the NTErf method is:
%
% double NTErf(double x)
%
% A description of each parameter follows:
%
% o x: The value to compute the error function for.
%
*/
MagickPrivate double NTErf(double x)
{
double
a1,
a2,
a3,
a4,
a5,
p,
t,
y;
int
sign;
a1=0.254829592;
a2=-0.284496736;
a3=1.421413741;
a4=-1.453152027;
a5=1.061405429;
p=0.3275911;
sign=x < 0 ? -1 : 1;
x=fabs(x);
t=1.0/(1.0+p*x);
y=1.0-(((((a5*t+a4)*t)+a3)*t+a2)*t+a1)*t*exp(-x*x);
return(sign*y);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
+ N T E r r o r H a n d l e r %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTErrorHandler() displays an error reason and then terminates the program.
%
% The format of the NTErrorHandler method is:
%
% void NTErrorHandler(const ExceptionType severity,const char *reason,
% const char *description)
%
% A description of each parameter follows:
%
% o severity: Specifies the numeric error category.
%
% o reason: Specifies the reason to display before terminating the
% program.
%
% o description: Specifies any description to the reason.
%
*/
MagickPrivate void NTErrorHandler(const ExceptionType severity,
const char *reason,const char *description)
{
char
buffer[3*MagickPathExtent],
*message;
(void) severity;
if (reason == (char *) NULL)
{
MagickCoreTerminus();
exit(0);
}
message=GetExceptionMessage(errno);
if ((description != (char *) NULL) && errno)
(void) FormatLocaleString(buffer,MagickPathExtent,"%s: %s (%s) [%s].\n",
GetClientName(),reason,description,message);
else
if (description != (char *) NULL)
(void) FormatLocaleString(buffer,MagickPathExtent,"%s: %s (%s).\n",
GetClientName(),reason,description);
else
if (errno != 0)
(void) FormatLocaleString(buffer,MagickPathExtent,"%s: %s [%s].\n",
GetClientName(),reason,message);
else
(void) FormatLocaleString(buffer,MagickPathExtent,"%s: %s.\n",
GetClientName(),reason);
message=DestroyString(message);
(void) MessageBox(NULL,buffer,"ImageMagick Exception",MB_OK | MB_TASKMODAL |
MB_SETFOREGROUND | MB_ICONEXCLAMATION);
MagickCoreTerminus();
exit(0);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T G a t h e r R a n d o m D a t a %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTGatherRandomData() gathers random data and returns it.
%
% The format of the GatherRandomData method is:
%
% MagickBooleanType NTGatherRandomData(const size_t length,
% unsigned char *random)
%
% A description of each parameter follows:
%
% length: the length of random data buffer
%
% random: the random data is returned here.
%
*/
MagickPrivate MagickBooleanType NTGatherRandomData(const size_t length,
unsigned char *random)
{
#if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER)
HCRYPTPROV
handle;
int
status;
handle=(HCRYPTPROV) NULL;
status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
(CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
if (status == 0)
status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
(CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
if (status == 0)
return(MagickFalse);
status=CryptGenRandom(handle,(DWORD) length,random);
if (status == 0)
{
status=CryptReleaseContext(handle,0);
return(MagickFalse);
}
status=CryptReleaseContext(handle,0);
if (status == 0)
return(MagickFalse);
#else
(void) random;
(void) length;
#endif
return(MagickTrue);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T G e t E n v i r o n m e n t V a l u e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTGetEnvironmentValue() returns the environment string that matches the
% specified name.
%
% The format of the NTGetEnvironmentValue method is:
%
% char *GetEnvironmentValue(const char *name)
%
% A description of each parameter follows:
%
% o name: the environment name.
%
*/
extern MagickPrivate char *NTGetEnvironmentValue(const char *name)
{
char
*environment = (char *) NULL;
DWORD
size;
LPWSTR
wide;
wchar_t
wide_name[MaxWideByteExtent];
if (MultiByteToWideChar(CP_UTF8,0,name,-1,wide_name,MaxWideByteExtent) == 0)
return(environment);
size=GetEnvironmentVariableW(wide_name,(LPWSTR) NULL,0);
if (size == 0)
return(environment);
wide=(LPWSTR) NTAcquireQuantumMemory((const size_t) size,sizeof(*wide));
if (wide == (LPWSTR) NULL)
return(environment);
if (GetEnvironmentVariableW(wide_name,wide,size) != 0)
environment=create_utf8_string(wide);
wide=(LPWSTR) RelinquishMagickMemory(wide);
return(environment);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T G e t E x e c u t i o n P a t h %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTGetExecutionPath() returns the execution path of a program.
%
% The format of the GetExecutionPath method is:
%
% MagickBooleanType NTGetExecutionPath(char *path,const size_t extent)
%
% A description of each parameter follows:
%
% o path: the pathname of the executable that started the process.
%
% o extent: the maximum extent of the path.
%
*/
MagickPrivate MagickBooleanType NTGetExecutionPath(char *path,
const size_t extent)
{
wchar_t
wide_path[MagickPathExtent];
(void) GetModuleFileNameW((HMODULE) NULL,wide_path,(DWORD) extent);
(void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,
NULL);
return(MagickTrue);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T G e t L a s t E r r o r M e s s a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTGetLastErrorMessage() returns the last error that occurred.
%
% The format of the NTGetLastErrorMessage method is:
%
% char *NTGetLastErrorMessage(DWORD last_error)
%
% A description of each parameter follows:
%
% o last_error: The value of GetLastError.
%
*/
static char *NTGetLastErrorMessage(DWORD last_error)
{
char
*reason;
int
status;
LPVOID
buffer = (LPVOID) NULL;
status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,NULL,last_error,
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
if (!status)
reason=AcquireString("An unknown error occurred");
else
{
reason=AcquireString((const char *) buffer);
LocalFree(buffer);
}
return(reason);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T G e t L i b r a r y E r r o r %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Lt_dlerror() returns a pointer to a string describing the last error
% associated with a lt_dl method. Note that this function is not thread
% safe so it should only be used under the protection of a lock.
%
% The format of the NTGetLibraryError method is:
%
% const char *NTGetLibraryError(void)
%
*/
MagickPrivate const char *NTGetLibraryError(void)
{
static char
last_error[MagickPathExtent];
char
*error;
*last_error='\0';
error=NTGetLastErrorMessage(GetLastError());
if (error)
(void) CopyMagickString(last_error,error,MagickPathExtent);
error=DestroyString(error);
return(last_error);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T G e t L i b r a r y S y m b o l %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTGetLibrarySymbol() retrieve the procedure address of the method
% specified by the passed character string.
%
% The format of the NTGetLibrarySymbol method is:
%
% void *NTGetLibrarySymbol(void *handle,const char *name)
%
% A description of each parameter follows:
%
% o handle: Specifies a handle to the previously loaded dynamic module.
%
% o name: Specifies the procedure entry point to be returned.
%
*/
void *NTGetLibrarySymbol(void *handle,const char *name)
{
FARPROC
proc_address;
proc_address=GetProcAddress((HMODULE) handle,(LPCSTR) name);
if (proc_address == (FARPROC) NULL)
return((void *) NULL);
return((void *) proc_address);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T G e t M o d u l e P a t h %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTGetModulePath() returns the path of the specified module.
%
% The format of the GetModulePath method is:
%
% MagickBooleanType NTGetModulePath(const char *module,char *path)
%
% A description of each parameter follows:
%
% modith: the module name.
%
% path: the module path is returned here.
%
*/
MagickPrivate MagickBooleanType NTGetModulePath(const char *module,char *path)
{
char
module_path[MagickPathExtent];
HMODULE
handle;
ssize_t
length;
*path='\0';
handle=GetModuleHandle(module);
if (handle == (HMODULE) NULL)
return(MagickFalse);
length=GetModuleFileName(handle,module_path,MagickPathExtent);
if (length != 0)
GetPathComponent(module_path,HeadPath,path);
return(MagickTrue);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T G h o s t s c r i p t D L L V e c t o r s %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTGhostscriptDLLVectors() returns a GhostInfo structure that includes
% function vectors to invoke Ghostscript DLL functions. A null pointer is
% returned if there is an error when loading the DLL or retrieving the
% function vectors.
%
% The format of the NTGhostscriptDLLVectors method is:
%
% const GhostInfo *NTGhostscriptDLLVectors(void)
%
*/
static int NTLocateGhostscript(DWORD flags,int *root_index,
const char **product_family,int *major_version,int *minor_version,
int *patch_version)
{
int
i;
MagickBooleanType
status;
static const char
*products[2] =
{
"Artifex Ghostscript",
"GPL Ghostscript"
};
/*
Find the most recent version of Ghostscript.
*/
status=MagickFalse;
*root_index=0;
*product_family=NULL;
*major_version=5;
*minor_version=49; /* min version of Ghostscript is 5.50 */
for (i=0; i < (ssize_t) (sizeof(products)/sizeof(products[0])); i++)
{
char
key[MagickPathExtent];
HKEY
hkey;
int
j;
REGSAM
mode;
(void) FormatLocaleString(key,MagickPathExtent,"SOFTWARE\\%s",products[i]);
for (j=0; j < (ssize_t) (sizeof(registry_roots)/sizeof(registry_roots[0]));
j++)
{
mode=KEY_READ | flags;
if (RegOpenKeyExA(registry_roots[j].hkey,key,0,mode,&hkey) ==
ERROR_SUCCESS)
{
DWORD
extent;
int
k;
/*
Now enumerate the keys.
*/
extent=sizeof(key)/sizeof(char);
for (k=0; RegEnumKeyA(hkey,k,key,extent) == ERROR_SUCCESS; k++)
{
int
major,
minor,
patch;
major=0;
minor=0;
patch=0;
if (sscanf(key,"%d.%d.%d",&major,&minor,&patch) != 3)
if (sscanf(key,"%d.%d",&major,&minor) != 2)
continue;
if ((major > *major_version) ||
((major == *major_version) && (minor > *minor_version)) ||
((minor == *minor_version) && (patch > *patch_version)))
{
*root_index=j;
*product_family=products[i];
*major_version=major;
*minor_version=minor;
*patch_version=patch;
status=MagickTrue;
}
}
(void) RegCloseKey(hkey);
}
}
}
if (status == MagickFalse)
{
*major_version=0;
*minor_version=0;
*patch_version=0;
}
(void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"Ghostscript (%s) "
"version %d.%d.%d",*product_family,*major_version,*minor_version,*patch_version);
return(status);
}
static MagickBooleanType NTGhostscriptGetString(const char *name,
BOOL *is_64_bit,char *value,const size_t length)
{
char
buffer[MagickPathExtent],
*directory;
static const char
*product_family = (const char *) NULL;
static BOOL
is_64_bit_version = FALSE;
static int
flags = 0,
major_version = 0,
minor_version = 0,
patch_version = 0,
root_index = 0;
unsigned char
*registry_value;
/*
Get a string from the installed Ghostscript.
*/
*value='\0';
directory=(char *) NULL;
if (LocaleCompare(name,"GS_DLL") == 0)
{
directory=GetEnvironmentValue("MAGICK_GHOSTSCRIPT_PATH");
if (directory != (char *) NULL)
{
(void) FormatLocaleString(buffer,MagickPathExtent,"%s%sgsdll64.dll",
directory,DirectorySeparator);
if (IsPathAccessible(buffer) != MagickFalse)
{
directory=DestroyString(directory);
(void) CopyMagickString(value,buffer,length);
if (is_64_bit != NULL)
*is_64_bit=TRUE;
return(MagickTrue);
}
(void) FormatLocaleString(buffer,MagickPathExtent,"%s%sgsdll32.dll",
directory,DirectorySeparator);
if (IsPathAccessible(buffer) != MagickFalse)
{
directory=DestroyString(directory);
(void) CopyMagickString(value,buffer,length);
if (is_64_bit != NULL)
*is_64_bit=FALSE;
return(MagickTrue);
}
return(MagickFalse);
}
}
if (product_family == (const char *) NULL)
{
flags=0;
#if defined(KEY_WOW64_32KEY)
#if defined(_WIN64)
flags=KEY_WOW64_64KEY;
#else
flags=KEY_WOW64_32KEY;
#endif
(void) NTLocateGhostscript(flags,&root_index,&product_family,
&major_version,&minor_version,&patch_version);
if (product_family == (const char *) NULL)
#if defined(_WIN64)
flags=KEY_WOW64_32KEY;
else
is_64_bit_version=TRUE;
#else
flags=KEY_WOW64_64KEY;
#endif
#endif
}
if (product_family == (const char *) NULL)
{
(void) NTLocateGhostscript(flags,&root_index,&product_family,
&major_version,&minor_version,&patch_version);
#if !defined(_WIN64)
is_64_bit_version=TRUE;
#endif
}
if (product_family == (const char *) NULL)
return(MagickFalse);
if (is_64_bit != NULL)
*is_64_bit=is_64_bit_version;
(void) FormatLocaleString(buffer,MagickPathExtent,"SOFTWARE\\%s\\%d.%.2d.%d",
product_family,major_version,minor_version,patch_version);
registry_value=NTGetRegistryValue(registry_roots[root_index].hkey,buffer,
flags,name);
if (registry_value == (unsigned char *) NULL)
{
(void) FormatLocaleString(buffer,MagickPathExtent,"SOFTWARE\\%s\\%d.%02d",
product_family,major_version,minor_version);
registry_value=NTGetRegistryValue(registry_roots[root_index].hkey,buffer,
flags,name);
}
if (registry_value == (unsigned char *) NULL)
return(MagickFalse);
(void) CopyMagickString(value,(const char *) registry_value,length);
registry_value=(unsigned char *) RelinquishMagickMemory(registry_value);
(void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
"registry: \"%s\\%s\\%s\"=\"%s\"",registry_roots[root_index].name,
buffer,name,value);
return(MagickTrue);
}
static MagickBooleanType NTGhostscriptDLL(char *path,int length)
{
static char
dll[MagickPathExtent] = { "" };
static BOOL
is_64_bit;
*path='\0';
if ((*dll == '\0') &&
(NTGhostscriptGetString("GS_DLL",&is_64_bit,dll,sizeof(dll)) != MagickTrue))
return(MagickFalse);
#if defined(_WIN64)
if (!is_64_bit)
return(MagickFalse);
#else
if (is_64_bit)
return(MagickFalse);
#endif
(void) CopyMagickString(path,dll,length);
return(MagickTrue);
}
static inline MagickBooleanType NTGhostscriptHasValidHandle()
{
if ((nt_ghost_info.delete_instance == NULL) || (ghost_info.exit == NULL) ||
(nt_ghost_info.new_instance == NULL) || (ghost_info.set_stdio == NULL) ||
(ghost_info.init_with_args == NULL) || (ghost_info.revision == NULL))
return(MagickFalse);
return(MagickTrue);
}
MagickPrivate const GhostInfo *NTGhostscriptDLLVectors(void)
{
char
path[MagickPathExtent];
if (ghost_semaphore == (SemaphoreInfo *) NULL)
ActivateSemaphoreInfo(&ghost_semaphore);
LockSemaphoreInfo(ghost_semaphore);
if (ghost_handle != (void *) NULL)
{
UnlockSemaphoreInfo(ghost_semaphore);
if (NTGhostscriptHasValidHandle() == MagickTrue)
return(&ghost_info);
return((GhostInfo *) NULL);
}
if (NTGhostscriptDLL(path,sizeof(path)) != MagickTrue)
{
UnlockSemaphoreInfo(ghost_semaphore);
return((GhostInfo *) NULL);
}
ghost_handle=lt_dlopen(path);
if (ghost_handle == (void *) NULL)
{
UnlockSemaphoreInfo(ghost_semaphore);
return((GhostInfo *) NULL);
}
(void) memset((void *) &nt_ghost_info,0,sizeof(NTGhostInfo));
nt_ghost_info.delete_instance=(void (MagickDLLCall *)(gs_main_instance *)) (
lt_dlsym(ghost_handle,"gsapi_delete_instance"));
nt_ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,
void *)) (lt_dlsym(ghost_handle,"gsapi_new_instance"));
nt_ghost_info.has_instance=MagickFalse;
(void) memset((void *) &ghost_info,0,sizeof(GhostInfo));
ghost_info.delete_instance=NTGhostscriptDeleteInstance;
ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
lt_dlsym(ghost_handle,"gsapi_exit");
ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
char **)) (lt_dlsym(ghost_handle,"gsapi_init_with_args"));
ghost_info.new_instance=NTGhostscriptNewInstance;
ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,const char *,
int,int *)) (lt_dlsym(ghost_handle,"gsapi_run_string"));
ghost_info.set_arg_encoding=(int (MagickDLLCall*)(gs_main_instance*, int)) (
lt_dlsym(ghost_handle, "gsapi_set_arg_encoding"));
ghost_info.set_stdio=(int (MagickDLLCall *)(gs_main_instance *,int(
MagickDLLCall *)(void *,char *,int),int(MagickDLLCall *)(void *,
const char *,int),int(MagickDLLCall *)(void *,const char *,int)))
(lt_dlsym(ghost_handle,"gsapi_set_stdio"));
ghost_info.revision=(int (MagickDLLCall *)(gsapi_revision_t *,int)) (
lt_dlsym(ghost_handle,"gsapi_revision"));
UnlockSemaphoreInfo(ghost_semaphore);
if (NTGhostscriptHasValidHandle() == MagickTrue)
return(&ghost_info);
return((GhostInfo *) NULL);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T G h o s t s c r i p t E X E %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTGhostscriptEXE() obtains the path to the latest Ghostscript executable.
% The method returns FALSE if a full path value is not obtained and returns
% a default path of gswin32c.exe.
%
% The format of the NTGhostscriptEXE method is:
%
% void NTGhostscriptEXE(char *path,int length)
%
% A description of each parameter follows:
%
% o path: return the Ghostscript executable path here.
%
% o length: length of buffer.
%
*/
MagickPrivate void NTGhostscriptEXE(char *path,int length)
{
char
*p;
static char
program[MagickPathExtent] = { "" };
static BOOL
is_64_bit_version = FALSE;
if (*program == '\0')
{
if (ghost_semaphore == (SemaphoreInfo *) NULL)
ActivateSemaphoreInfo(&ghost_semaphore);
LockSemaphoreInfo(ghost_semaphore);
if (*program == '\0')
{
if (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,program,
sizeof(program)) == MagickFalse)
{
UnlockSemaphoreInfo(ghost_semaphore);
#if defined(_WIN64)
(void) CopyMagickString(program,"gswin64c.exe",sizeof(program));
#else
(void) CopyMagickString(program,"gswin32c.exe",sizeof(program));
#endif
(void) CopyMagickString(path,program,length);
return;
}
p=strrchr(program,'\\');
if (p != (char *) NULL)
{
p++;
*p='\0';
(void) ConcatenateMagickString(program,is_64_bit_version ?
"gswin64c.exe" : "gswin32c.exe",sizeof(program));
}
}
UnlockSemaphoreInfo(ghost_semaphore);
}
(void) CopyMagickString(path,program,length);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T G h o s t s c r i p t F o n t s %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTGhostscriptFonts() obtains the path to the Ghostscript fonts. The method
% returns false if it cannot determine the font path.
%
% The format of the NTGhostscriptFonts method is:
%
% MagickBooleanType NTGhostscriptFonts(char *path,int length)
%
% A description of each parameter follows:
%
% o path: return the font path here.
%
% o length: length of the path buffer.
%
*/
MagickPrivate MagickBooleanType NTGhostscriptFonts(char *path,int length)
{
char
buffer[MagickPathExtent],
*directory,
filename[MagickPathExtent];
char
*p,
*q;
*path='\0';
directory=GetEnvironmentValue("MAGICK_GHOSTSCRIPT_FONT_PATH");
if (directory != (char *) NULL)
{
(void) CopyMagickString(buffer,directory,MagickPathExtent);
directory=DestroyString(directory);
}
else
{
if (NTGhostscriptGetString("GS_LIB",NULL,buffer,
MagickPathExtent) == MagickFalse)
return(MagickFalse);
}
for (p=buffer-1; p != (char *) NULL; p=strchr(p+1,DirectoryListSeparator))
{
(void) CopyMagickString(path,p+1,length+1);
q=strchr(path,DirectoryListSeparator);
if (q != (char *) NULL)
*q='\0';
(void) FormatLocaleString(filename,MagickPathExtent,"%s%sfonts.dir",path,
DirectorySeparator);
if (IsPathAccessible(filename) != MagickFalse)
return(MagickTrue);
(void) FormatLocaleString(filename,MagickPathExtent,"%s%sn019003l.pfb",path,
DirectorySeparator);
if (IsPathAccessible(filename) != MagickFalse)
return(MagickTrue);
}
*path='\0';
return(MagickFalse);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T G h o s t s c r i p t U n L o a d D L L %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTGhostscriptUnLoadDLL() unloads the Ghostscript DLL and returns TRUE if
% it succeeds.
%
% The format of the NTGhostscriptUnLoadDLL method is:
%
% int NTGhostscriptUnLoadDLL(void)
%
*/
MagickPrivate void NTGhostscriptUnLoadDLL(void)
{
if (ghost_semaphore == (SemaphoreInfo *) NULL)
ActivateSemaphoreInfo(&ghost_semaphore);
LockSemaphoreInfo(ghost_semaphore);
if (ghost_handle != (void *) NULL)
{
(void) lt_dlclose(ghost_handle);
ghost_handle=(void *) NULL;
(void) memset((void *) &ghost_info,0,sizeof(GhostInfo));
}
UnlockSemaphoreInfo(ghost_semaphore);
RelinquishSemaphoreInfo(&ghost_semaphore);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T L o n g P a t h s E n a b l e d %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTLongPathsEnabled() returns a boolean indicating whether long paths are
$ enabled.
%
% The format of the NTLongPathsEnabled method is:
%
% MagickBooleanType NTLongPathsEnabled()
%
*/
MagickExport MagickBooleanType NTLongPathsEnabled()
{
static size_t
long_paths_enabled = 2;
if (long_paths_enabled == 2)
{
DWORD
size,
type,
value;
HKEY
registry_key;
LONG
status;
registry_key=(HKEY) INVALID_HANDLE_VALUE;
status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Control\\FileSystem",0,KEY_READ,
®istry_key);
if (status != ERROR_SUCCESS)
{
RegCloseKey(registry_key);
long_paths_enabled=0;
return(MagickFalse);
}
value=0;
status=RegQueryValueExA(registry_key,"LongPathsEnabled",0,&type,NULL,
NULL);
if ((status != ERROR_SUCCESS) || (type != REG_DWORD))
{
RegCloseKey(registry_key);
long_paths_enabled=0;
return(MagickFalse);
}
size=0;
status=RegQueryValueExA(registry_key,"LongPathsEnabled",0,&type,
(LPBYTE) &value,&size);
RegCloseKey(registry_key);
if (status != ERROR_SUCCESS)
{
long_paths_enabled=0;
return(MagickFalse);
}
long_paths_enabled=(size_t) value;
}
return(long_paths_enabled == 1 ? MagickTrue : MagickFalse);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
+ N T M a p M e m o r y %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Mmap() emulates the Unix method of the same name.
%
% The format of the NTMapMemory method is:
%
% MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
% int access,int file,MagickOffsetType offset)
%
*/
MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
int flags,int file,MagickOffsetType offset)
{
DWORD
access_mode,
high_length,
high_offset,
low_length,
low_offset,
protection_mode;
HANDLE
file_handle,
map_handle;
void
*map;
(void) address;
access_mode=0;
file_handle=INVALID_HANDLE_VALUE;
low_length=(DWORD) (length & 0xFFFFFFFFUL);
high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
map_handle=INVALID_HANDLE_VALUE;
map=(void *) NULL;
low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
protection_mode=0;
if (protection & PROT_WRITE)
{
access_mode=FILE_MAP_WRITE;
if (!(flags & MAP_PRIVATE))
protection_mode=PAGE_READWRITE;
else
{
access_mode=FILE_MAP_COPY;
protection_mode=PAGE_WRITECOPY;
}
}
else
if (protection & PROT_READ)
{
access_mode=FILE_MAP_READ;
protection_mode=PAGE_READONLY;
}
if ((file == -1) && (flags & MAP_ANONYMOUS))
file_handle=INVALID_HANDLE_VALUE;
else
file_handle=(HANDLE) _get_osfhandle(file);
map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
low_length,0);
if (map_handle)
{
map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
length);
CloseHandle(map_handle);
}
if (map == (void *) NULL)
return((void *) ((char *) MAP_FAILED));
return((void *) ((char *) map));
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T O p e n D i r e c t o r y %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTOpenDirectory() opens the directory named by filename and associates a
% directory stream with it.
%
% The format of the NTOpenDirectory method is:
%
% DIR *NTOpenDirectory(const char *path)
%
% A description of each parameter follows:
%
% o entry: Specifies a pointer to a DIR structure.
%
*/
MagickPrivate DIR *NTOpenDirectory(const char *path)
{
DIR
*entry;
size_t
length;
wchar_t
file_specification[MagickPathExtent];
assert(path != (const char *) NULL);
length=MultiByteToWideChar(CP_UTF8,0,path,-1,file_specification,
MagickPathExtent);
if (length == 0)
return((DIR *) NULL);
if(wcsncat(file_specification,L"\\*.*",MagickPathExtent-wcslen(
file_specification)-1) == (wchar_t *) NULL)
return((DIR *) NULL);
entry=(DIR *) AcquireCriticalMemory(sizeof(DIR));
entry->firsttime=TRUE;
entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
if (entry->hSearch == INVALID_HANDLE_VALUE)
{
entry=(DIR *) RelinquishMagickMemory(entry);
return((DIR *) NULL);
}
return(entry);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T O p e n L i b r a r y %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTOpenLibrary() loads a dynamic module into memory and returns a handle that
% can be used to access the various procedures in the module.
%
% The format of the NTOpenLibrary method is:
%
% void *NTOpenLibrary(const char *filename)
%
% A description of each parameter follows:
%
% o path: Specifies a pointer to string representing dynamic module that
% is to be loaded.
%
*/
static UINT ChangeErrorMode(void)
{
typedef UINT
(CALLBACK *GETERRORMODE)(void);
GETERRORMODE
getErrorMode;
HMODULE
handle;
UINT
mode;
mode=SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
handle=GetModuleHandle("kernel32.dll");
if (handle == (HMODULE) NULL)
return SetErrorMode(mode);
getErrorMode=(GETERRORMODE) NTGetLibrarySymbol(handle,"GetErrorMode");
if (getErrorMode != (GETERRORMODE) NULL)
mode=getErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
return SetErrorMode(mode);
}
static inline void *NTLoadLibrary(const char *filename)
{
void
*library;
wchar_t
*path;
library=(void *) NULL;
path=create_wchar_path(filename);
if (path != (wchar_t *) NULL)
{
library=LoadLibraryExW(path,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
path=(wchar_t *) RelinquishMagickMemory(path);
}
return(library);
}
MagickPrivate void *NTOpenLibrary(const char *filename)
{
UINT
mode;
void
*handle;
mode=ChangeErrorMode();
handle=NTLoadLibrary(filename);
#if defined(MAGICKCORE_LTDL_DELEGATE)
if (handle == (void *) NULL)
{
char
path[MagickPathExtent];
const char
*p,
*q;
p=lt_dlgetsearchpath();
while (p != (const char*) NULL)
{
q=strchr(p,DirectoryListSeparator);
if (q != (const char*) NULL)
(void) CopyMagickString(path,p,q-p+1);
else
(void) CopyMagickString(path,p,MagickPathExtent);
(void) ConcatenateMagickString(path,DirectorySeparator,MagickPathExtent);
(void) ConcatenateMagickString(path,filename,MagickPathExtent);
handle=NTLoadLibrary(path);
if (handle != (void *) NULL || q == (const char*) NULL)
break;
p=q+1;
}
}
#endif
SetErrorMode(mode);
return(handle);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T R e a d D i r e c t o r y %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTReadDirectory() returns a pointer to a structure representing the
% directory entry at the current position in the directory stream to which
% entry refers.
%
% The format of the NTReadDirectory
%
% NTReadDirectory(entry)
%
% A description of each parameter follows:
%
% o entry: Specifies a pointer to a DIR structure.
%
*/
MagickPrivate struct dirent *NTReadDirectory(DIR *entry)
{
int
status;
size_t
length;
if (entry == (DIR *) NULL)
return((struct dirent *) NULL);
if (!entry->firsttime)
{
status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
if (status == 0)
return((struct dirent *) NULL);
}
length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
entry->file_info.d_name,sizeof(entry->file_info.d_name),NULL,NULL);
if (length == 0)
return((struct dirent *) NULL);
entry->firsttime=FALSE;
entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
return(&entry->file_info);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T R e g i s t r y K e y L o o k u p %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTRegistryKeyLookup() returns ImageMagick installation path settings
% stored in the Windows Registry. Path settings are specific to the
% installed ImageMagick version so that multiple Image Magick installations
% may coexist.
%
% Values are stored in the registry under a base path similar to
% "HKEY_LOCAL_MACHINE/SOFTWARE\ImageMagick\6.7.4\Q:16" or
% "HKEY_CURRENT_USER/SOFTWARE\ImageMagick\6.7.4\Q:16". The provided subkey
% is appended to this base path to form the full key.
%
% The format of the NTRegistryKeyLookup method is:
%
% unsigned char *NTRegistryKeyLookup(const char *subkey)
%
% A description of each parameter follows:
%
% o subkey: Specifies a string that identifies the registry object.
% Currently supported sub-keys include: "BinPath", "ConfigurePath",
% "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
%
*/
MagickPrivate unsigned char *NTRegistryKeyLookup(const char *subkey)
{
char
package_key[MagickPathExtent] = "";
unsigned char
*value;
(void) FormatLocaleString(package_key,MagickPathExtent,
"SOFTWARE\\%s\\%s\\Q:%d",MagickPackageName,MagickLibVersionText,
MAGICKCORE_QUANTUM_DEPTH);
value=NTGetRegistryValue(HKEY_LOCAL_MACHINE,package_key,0,subkey);
if (value == (unsigned char *) NULL)
value=NTGetRegistryValue(HKEY_CURRENT_USER,package_key,0,subkey);
return(value);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T R e p o r t E v e n t %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTReportEvent() reports an event.
%
% The format of the NTReportEvent method is:
%
% MagickBooleanType NTReportEvent(const char *event,
% const MagickBooleanType error)
%
% A description of each parameter follows:
%
% o event: the event.
%
% o error: MagickTrue the event is an error.
%
*/
MagickPrivate MagickBooleanType NTReportEvent(const char *event,
const MagickBooleanType error)
{
const char
*events[1];
HANDLE
handle;
WORD
type;
handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
if (handle == NULL)
return(MagickFalse);
events[0]=event;
type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
DeregisterEventSource(handle);
return(MagickTrue);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T R e s o u r c e T o B l o b %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTResourceToBlob() returns a blob containing the contents of the resource
% in the current executable specified by the id parameter. This currently
% used to retrieve MGK files tha have been embedded into the various command
% line utilities.
%
% The format of the NTResourceToBlob method is:
%
% unsigned char *NTResourceToBlob(const char *id)
%
% A description of each parameter follows:
%
% o id: Specifies a string that identifies the resource.
%
*/
MagickPrivate unsigned char *NTResourceToBlob(const char *id)
{
#ifndef MAGICKCORE_LIBRARY_NAME
char
path[MagickPathExtent];
#endif
DWORD
length;
HGLOBAL
global;
HMODULE
handle;
HRSRC
resource;
unsigned char
*blob,
*value;
assert(id != (const char *) NULL);
if (IsEventLogging() != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
#ifdef MAGICKCORE_LIBRARY_NAME
handle=GetModuleHandle(MAGICKCORE_LIBRARY_NAME);
#else
(void) FormatLocaleString(path,MagickPathExtent,"%s%s%s",GetClientPath(),
DirectorySeparator,GetClientName());
if (IsPathAccessible(path) != MagickFalse)
handle=GetModuleHandle(path);
else
handle=GetModuleHandle(0);
#endif
if (!handle)
return((unsigned char *) NULL);
resource=FindResource(handle,id,"IMAGEMAGICK");
if (!resource)
return((unsigned char *) NULL);
global=LoadResource(handle,resource);
if (!global)
return((unsigned char *) NULL);
length=SizeofResource(handle,resource);
value=(unsigned char *) LockResource(global);
if (!value)
{
FreeResource(global);
return((unsigned char *) NULL);
}
blob=(unsigned char *) AcquireQuantumMemory(length+MagickPathExtent,
sizeof(*blob));
if (blob != (unsigned char *) NULL)
{
(void) memcpy(blob,value,length);
blob[length]='\0';
}
UnlockResource(global);
FreeResource(global);
return(blob);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T S y s t e m C o m m a n d %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTSystemCommand() executes the specified command and waits until it
% terminates. The returned value is the exit status of the command.
%
% The format of the NTSystemCommand method is:
%
% int NTSystemCommand(MagickFalse,const char *command)
%
% A description of each parameter follows:
%
% o command: This string is the command to execute.
%
% o output: an optional buffer to store the output from stderr/stdout.
%
*/
MagickPrivate int NTSystemCommand(const char *command,char *output)
{
#define CleanupOutputHandles \
if (read_output != (HANDLE) NULL) \
{ \
CloseHandle(read_output); \
read_output=(HANDLE) NULL; \
CloseHandle(write_output); \
write_output=(HANDLE) NULL; \
}
#define CopyLastError \
last_error=GetLastError(); \
if (output != (char *) NULL) \
{ \
error=NTGetLastErrorMessage(last_error); \
if (error != (char *) NULL) \
{ \
CopyMagickString(output,error,MagickPathExtent); \
error=DestroyString(error); \
} \
}
char
*error,
local_command[MagickPathExtent];
DWORD
child_status,
last_error;
int
status;
MagickBooleanType
asynchronous;
HANDLE
read_output,
write_output;
PROCESS_INFORMATION
process_info;
size_t
output_offset;
STARTUPINFO
startup_info;
if (command == (char *) NULL)
return(-1);
read_output=(HANDLE) NULL;
write_output=(HANDLE) NULL;
GetStartupInfo(&startup_info);
startup_info.dwFlags=STARTF_USESHOWWINDOW;
startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
(void) CopyMagickString(local_command,command,MagickPathExtent);
asynchronous=command[strlen(command)-1] == '&' ? MagickTrue : MagickFalse;
if (asynchronous != MagickFalse)
{
local_command[strlen(command)-1]='\0';
startup_info.wShowWindow=SW_SHOWDEFAULT;
}
else
{
if (command[strlen(command)-1] == '|')
local_command[strlen(command)-1]='\0';
else
startup_info.wShowWindow=SW_HIDE;
read_output=(HANDLE) NULL;
if (output != (char *) NULL)
{
if (CreatePipe(&read_output,&write_output,NULL,0))
{
if (SetHandleInformation(write_output,HANDLE_FLAG_INHERIT,
HANDLE_FLAG_INHERIT))
{
startup_info.dwFlags|=STARTF_USESTDHANDLES;
startup_info.hStdOutput=write_output;
startup_info.hStdError=write_output;
}
else
CleanupOutputHandles;
}
else
read_output=(HANDLE) NULL;
}
}
status=CreateProcess((LPCTSTR) NULL,local_command,(LPSECURITY_ATTRIBUTES)
NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) TRUE,(DWORD)
NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
&process_info);
if (status == 0)
{
CopyLastError;
CleanupOutputHandles;
return(last_error == ERROR_FILE_NOT_FOUND ? 127 : -1);
}
if (output != (char *) NULL)
*output='\0';
if (asynchronous != MagickFalse)
return(status == 0);
output_offset=0;
status=STATUS_TIMEOUT;
while (status == STATUS_TIMEOUT)
{
DWORD
size;
status=WaitForSingleObject(process_info.hProcess,1000);
size=0;
if (read_output != (HANDLE) NULL)
if (!PeekNamedPipe(read_output,NULL,0,NULL,&size,NULL))
break;
while (size > 0)
{
char
buffer[MagickPathExtent];
DWORD
bytes_read;
if (ReadFile(read_output,buffer,MagickPathExtent-1,&bytes_read,NULL))
{
size_t
count;
count=MagickMin(MagickPathExtent-output_offset,
(size_t) bytes_read+1);
if (count > 0)
{
CopyMagickString(output+output_offset,buffer,count);
output_offset+=count-1;
}
}
if (!PeekNamedPipe(read_output,NULL,0,NULL,&size,NULL))
break;
}
}
if (status != WAIT_OBJECT_0)
{
CopyLastError;
CleanupOutputHandles;
return(status);
}
status=GetExitCodeProcess(process_info.hProcess,&child_status);
if (status == 0)
{
CopyLastError;
CleanupOutputHandles;
return(-1);
}
CloseHandle(process_info.hProcess);
CloseHandle(process_info.hThread);
CleanupOutputHandles;
return((int) child_status);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T S y s t e m C o n i f i g u r a t i o n %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTSystemConfiguration() provides a way for the application to determine
% values for system limits or options at runtime.
%
% The format of the exit method is:
%
% ssize_t NTSystemConfiguration(int name)
%
% A description of each parameter follows:
%
% o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
%
*/
MagickPrivate ssize_t NTSystemConfiguration(int name)
{
switch (name)
{
case _SC_PAGE_SIZE:
{
SYSTEM_INFO
system_info;
GetSystemInfo(&system_info);
return(system_info.dwPageSize);
}
case _SC_PHYS_PAGES:
{
MEMORYSTATUSEX
status;
SYSTEM_INFO
system_info;
status.dwLength=sizeof(status);
if (GlobalMemoryStatusEx(&status) == 0)
return(0L);
GetSystemInfo(&system_info);
return((ssize_t) status.ullTotalPhys/system_info.dwPageSize);
}
case _SC_OPEN_MAX:
return(2048);
default:
break;
}
return(-1);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T T r u n c a t e F i l e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTTruncateFile() truncates a file to a specified length.
%
% The format of the NTTruncateFile method is:
%
% int NTTruncateFile(int file,off_t length)
%
% A description of each parameter follows:
%
% o file: the file.
%
% o length: the file length.
%
*/
MagickPrivate int NTTruncateFile(int file,off_t length)
{
DWORD
file_pointer;
HANDLE
file_handle;
long
high,
low;
file_handle=(HANDLE) _get_osfhandle(file);
if (file_handle == INVALID_HANDLE_VALUE)
return(-1);
low=(long) (length & 0xffffffffUL);
high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
file_pointer=SetFilePointer(file_handle,low,&high,FILE_BEGIN);
if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
return(-1);
if (SetEndOfFile(file_handle) == 0)
return(-1);
return(0);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
+ N T U n m a p M e m o r y %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTUnmapMemory() emulates the Unix munmap method.
%
% The format of the NTUnmapMemory method is:
%
% int NTUnmapMemory(void *map,size_t length)
%
% A description of each parameter follows:
%
% o map: the address of the binary large object.
%
% o length: the length of the binary large object.
%
*/
MagickPrivate int NTUnmapMemory(void *map,size_t length)
{
(void) length;
if (UnmapViewOfFile(map) == 0)
return(-1);
return(0);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T W a r n i n g H a n d l e r %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTWarningHandler() displays a warning reason.
%
% The format of the NTWarningHandler method is:
%
% void NTWarningHandler(const ExceptionType severity,const char *reason,
% const char *description)
%
% A description of each parameter follows:
%
% o severity: Specifies the numeric warning category.
%
% o reason: Specifies the reason to display before terminating the
% program.
%
% o description: Specifies any description to the reason.
%
*/
MagickPrivate void NTWarningHandler(const ExceptionType severity,
const char *reason,const char *description)
{
char
buffer[2*MagickPathExtent];
(void) severity;
if (reason == (char *) NULL)
return;
if (description == (char *) NULL)
(void) FormatLocaleString(buffer,MagickPathExtent,"%s: %s.\n",GetClientName(),
reason);
else
(void) FormatLocaleString(buffer,MagickPathExtent,"%s: %s (%s).\n",
GetClientName(),reason,description);
(void) MessageBox(NULL,buffer,"ImageMagick Warning",MB_OK | MB_TASKMODAL |
MB_SETFOREGROUND | MB_ICONINFORMATION);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T W i n d o w s G e n e s i s %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTWindowsGenesis() initializes the MagickCore Windows environment.
%
% The format of the NTWindowsGenesis method is:
%
% void NTWindowsGenesis(void)
%
*/
MagickPrivate void NTWindowsGenesis(void)
{
char
*mode;
mode=GetEnvironmentValue("MAGICK_ERRORMODE");
if (mode != (char *) NULL)
{
(void) SetErrorMode(StringToInteger(mode));
mode=DestroyString(mode);
}
#if defined(_DEBUG) && !defined(__MINGW32__)
if (IsEventLogging() != MagickFalse)
{
int
debug;
debug=_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
//debug |= _CRTDBG_CHECK_ALWAYS_DF;
debug |= _CRTDBG_DELAY_FREE_MEM_DF;
debug |= _CRTDBG_LEAK_CHECK_DF;
(void) _CrtSetDbgFlag(debug);
//_ASSERTE(_CrtCheckMemory());
//_CrtSetBreakAlloc(42);
}
#endif
#if defined(MAGICKCORE_INSTALLED_SUPPORT)
{
unsigned char
*path;
path=NTRegistryKeyLookup("LibPath");
if (path != (unsigned char *) NULL)
{
wchar_t
*lib_path;
lib_path=create_wchar_path((const char *) path);
if (lib_path != (wchar_t *) NULL)
{
SetDllDirectoryW(lib_path);
lib_path=(wchar_t *) RelinquishMagickMemory(lib_path);
}
path=(unsigned char *) RelinquishMagickMemory(path);
}
}
#endif
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% N T W i n d o w s T e r m i n u s %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% NTWindowsTerminus() terminates the MagickCore Windows environment.
%
% The format of the NTWindowsTerminus method is:
%
% void NTWindowsTerminus(void)
%
*/
MagickPrivate void NTWindowsTerminus(void)
{
NTGhostscriptUnLoadDLL();
DistributeCacheTerminus();
}
#endif
|