41#include "MagickCore/studio.h"
42#if defined(MAGICKCORE_WINDOWS_SUPPORT)
43#include "MagickCore/client.h"
44#include "MagickCore/distribute-cache-private.h"
45#include "MagickCore/exception-private.h"
46#include "MagickCore/image-private.h"
47#include "MagickCore/locale_.h"
48#include "MagickCore/log.h"
49#include "MagickCore/magick.h"
50#include "MagickCore/memory_.h"
51#include "MagickCore/memory-private.h"
52#include "MagickCore/nt-base.h"
53#include "MagickCore/nt-base-private.h"
54#include "MagickCore/resource_.h"
55#include "MagickCore/resource-private.h"
56#include "MagickCore/timer.h"
57#include "MagickCore/string_.h"
58#include "MagickCore/string-private.h"
59#include "MagickCore/utility.h"
60#include "MagickCore/utility-private.h"
61#include "MagickCore/version.h"
62#if defined(MAGICKCORE_LTDL_DELEGATE)
65#if defined(MAGICKCORE_CIPHER_SUPPORT)
73#if !defined(MAP_FAILED)
74#define MAP_FAILED ((void *)(LONG_PTR)-1)
76#define MaxWideByteExtent 100
90typedef struct _NTGhostInfo
93 (MagickDLLCall *delete_instance)(gs_main_instance *);
96 (MagickDLLCall *new_instance)(gs_main_instance **,
void *);
112 *ghost_handle = (
void *) NULL;
125const registry_roots[2] =
127 { HKEY_CURRENT_USER,
"HKEY_CURRENT_USER" },
128 { HKEY_LOCAL_MACHINE,
"HKEY_LOCAL_MACHINE" }
134static void MagickDLLCall NTGhostscriptDeleteInstance(
135 gs_main_instance *instance)
137 LockSemaphoreInfo(ghost_semaphore);
138 nt_ghost_info.delete_instance(instance);
139 nt_ghost_info.has_instance=MagickFalse;
140 UnlockSemaphoreInfo(ghost_semaphore);
143static int MagickDLLCall NTGhostscriptNewInstance(gs_main_instance **pinstance,
149 LockSemaphoreInfo(ghost_semaphore);
151 if (nt_ghost_info.has_instance == MagickFalse)
153 status=nt_ghost_info.new_instance(pinstance,caller_handle);
155 nt_ghost_info.has_instance=MagickTrue;
157 UnlockSemaphoreInfo(ghost_semaphore);
161static inline char *create_utf8_string(
const wchar_t *wide)
169 count=WideCharToMultiByte(CP_UTF8,0,wide,-1,NULL,0,NULL,NULL);
171 return((
char *) NULL);
172 utf8=(
char *) NTAcquireQuantumMemory(count+1,
sizeof(*utf8));
173 if (utf8 == (
char *) NULL)
174 return((
char *) NULL);
175 count=WideCharToMultiByte(CP_UTF8,0,wide,-1,utf8,count,NULL,NULL);
178 utf8=(
char *) RelinquishMagickMemory(utf8);
179 return((
char *) NULL);
185static unsigned char *NTGetRegistryValue(HKEY root,
const char *key,DWORD flags,
202 wide_name[MaxWideByteExtent];
204 value=(
unsigned char *) NULL;
205 status=RegOpenKeyExA(root,key,0,(KEY_READ | flags),®istry_key);
206 if (status != ERROR_SUCCESS)
208 if (MultiByteToWideChar(CP_UTF8,0,name,-1,wide_name,MaxWideByteExtent) == 0)
210 RegCloseKey(registry_key);
213 status=RegQueryValueExW(registry_key,wide_name,0,&type,0,&size);
214 if ((status == ERROR_SUCCESS) && (type == REG_SZ))
219 wide=(LPBYTE) NTAcquireQuantumMemory((
const size_t) size,
sizeof(*wide));
220 if (wide != (LPBYTE) NULL)
222 status=RegQueryValueExW(registry_key,wide_name,0,&type,wide,&size);
223 if ((status == ERROR_SUCCESS) && (type == REG_SZ))
224 value=(
unsigned char *) create_utf8_string((
const wchar_t *) wide);
225 wide=(LPBYTE) RelinquishMagickMemory(wide);
228 RegCloseKey(registry_key);
275#if defined(_DLL) && defined(ProvideDllMain)
276BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
280 case DLL_PROCESS_ATTACH:
291 MagickCoreGenesis((
const char *) NULL,MagickFalse);
292 wide_path=(
wchar_t *) NTAcquireQuantumMemory(MagickPathExtent,
294 if (wide_path == (
wchar_t *) NULL)
296 count=(ssize_t) GetModuleFileNameW(handle,wide_path,MagickPathExtent);
302 module_path=create_utf8_string(wide_path);
303 for ( ; count > 0; count--)
304 if (module_path[count] ==
'\\')
306 module_path[count+1]=
'\0';
309 path=(
char *) NTAcquireQuantumMemory(MagickPathExtent,
311 if (path == (
char *) NULL)
313 module_path=DestroyString(module_path);
314 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
317 count=(ssize_t) GetEnvironmentVariable(
"PATH",path,16*
319 if ((count != 0) && (strstr(path,module_path) == (
char *) NULL))
321 if ((strlen(module_path)+count+1) < (16*MagickPathExtent-1))
326 variable=(
char *) NTAcquireQuantumMemory(MagickPathExtent,
327 16*
sizeof(*variable));
328 if (variable == (
char *) NULL)
330 path=DestroyString(path);
331 module_path=DestroyString(module_path);
332 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
335 (void) FormatLocaleString(variable,16*MagickPathExtent,
336 "%s;%s",module_path,path);
337 SetEnvironmentVariable(
"PATH",variable);
338 variable=DestroyString(variable);
341 path=DestroyString(path);
342 module_path=DestroyString(module_path);
344 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
347 case DLL_PROCESS_DETACH:
349 MagickCoreTerminus();
359#if !defined(__MINGW32__)
384MagickPrivate
int gettimeofday (
struct timeval *time_value,
385 struct timezone *time_zone)
387#define EpochFiletime MagickLLConstant(116444736000000000)
392 if (time_value != (
struct timeval *) NULL)
403 GetSystemTimeAsFileTime(&file_time);
404 date_time.LowPart=file_time.dwLowDateTime;
405 date_time.HighPart=file_time.dwHighDateTime;
406 time=date_time.QuadPart;
409 time_value->tv_sec=(ssize_t) (time / 1000000);
410 time_value->tv_usec=(ssize_t) (time % 1000000);
412 if (time_zone != (
struct timezone *) NULL)
419 time_zone->tz_minuteswest=_timezone/60;
420 time_zone->tz_dsttime=_daylight;
451MagickPrivate
char **NTArgvToUTF8(
const int argc,
wchar_t **argv)
459 utf8=(
char **) NTAcquireQuantumMemory(argc,
sizeof(*utf8));
460 if (utf8 == (
char **) NULL)
461 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertStringToARGV");
462 for (i=0; i < (ssize_t) argc; i++)
464 utf8[i]=create_utf8_string(argv[i]);
465 if (utf8[i] == (
char *) NULL)
467 for (i--; i >= 0; i--)
468 utf8[i]=DestroyString(utf8[i]);
469 ThrowFatalException(ResourceLimitFatalError,
470 "UnableToConvertStringToARGV");
499MagickPrivate
int NTCloseDirectory(
DIR *entry)
501 assert(entry != (
DIR *) NULL);
502 if (IsEventLogging() != MagickFalse)
503 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
504 FindClose(entry->hSearch);
505 entry=(
DIR *) RelinquishMagickMemory(entry);
530MagickPrivate
int NTCloseLibrary(
void *handle)
532 return(!(FreeLibrary((HINSTANCE) handle)));
555MagickPrivate
double NTElapsedTime(
void)
575 if (frequency.QuadPart == 0)
577 if (QueryPerformanceFrequency(&frequency) == 0)
578 frequency.QuadPart=1;
580 if (frequency.QuadPart > 1)
582 QueryPerformanceCounter(&performance_count);
583 return((
double) performance_count.QuadPart/frequency.QuadPart);
585 GetSystemTime(&system_time);
586 SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
587 return((
double) 1.0e-7*elapsed_time.filetime64);
612MagickPrivate
double NTErf(
double x)
636 y=1.0-(((((a5*t+a4)*t)+a3)*t+a2)*t+a1)*t*exp(-x*x);
669MagickPrivate
void NTErrorHandler(
const ExceptionType severity,
670 const char *reason,
const char *description)
673 buffer[3*MagickPathExtent],
677 if (reason == (
char *) NULL)
679 MagickCoreTerminus();
682 message=GetExceptionMessage(errno);
683 if ((description != (
char *) NULL) && errno)
684 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s (%s) [%s].\n",
685 GetClientName(),reason,description,message);
687 if (description != (
char *) NULL)
688 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s (%s).\n",
689 GetClientName(),reason,description);
692 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s [%s].\n",
693 GetClientName(),reason,message);
695 (
void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s.\n",
696 GetClientName(),reason);
697 message=DestroyString(message);
698 (void) MessageBox(NULL,buffer,
"ImageMagick Exception",MB_OK | MB_TASKMODAL |
699 MB_SETFOREGROUND | MB_ICONEXCLAMATION);
700 MagickCoreTerminus();
729MagickPrivate MagickBooleanType NTGatherRandomData(
const size_t length,
730 unsigned char *random)
732#if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER)
739 handle=(HCRYPTPROV) NULL;
740 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
741 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
743 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
744 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
747 status=CryptGenRandom(handle,(DWORD) length,random);
750 status=CryptReleaseContext(handle,0);
753 status=CryptReleaseContext(handle,0);
786extern MagickPrivate
char *NTGetEnvironmentValue(
const char *name)
789 *environment = (
char *) NULL;
798 wide_name[MaxWideByteExtent];
800 if (MultiByteToWideChar(CP_UTF8,0,name,-1,wide_name,MaxWideByteExtent) == 0)
802 size=GetEnvironmentVariableW(wide_name,(LPWSTR) NULL,0);
805 wide=(LPWSTR) NTAcquireQuantumMemory((
const size_t) size,
sizeof(*wide));
806 if (wide == (LPWSTR) NULL)
808 if (GetEnvironmentVariableW(wide_name,wide,size) != 0)
809 environment=create_utf8_string(wide);
810 wide=(LPWSTR) RelinquishMagickMemory(wide);
838MagickPrivate MagickBooleanType NTGetExecutionPath(
char *path,
842 wide_path[MagickPathExtent];
844 (void) GetModuleFileNameW((HMODULE) NULL,wide_path,(DWORD) extent);
845 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,
872static char *NTGetLastErrorMessage(DWORD last_error)
881 buffer = (LPVOID) NULL;
883 status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
884 FORMAT_MESSAGE_FROM_SYSTEM,NULL,last_error,
885 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
887 reason=AcquireString(
"An unknown error occurred");
890 reason=AcquireString((
const char *) buffer);
916MagickPrivate
const char *NTGetLibraryError(
void)
919 last_error[MagickPathExtent];
925 error=NTGetLastErrorMessage(GetLastError());
927 (void) CopyMagickString(last_error,error,MagickPathExtent);
928 error=DestroyString(error);
957void *NTGetLibrarySymbol(
void *handle,
const char *name)
962 proc_address=GetProcAddress((HMODULE) handle,(LPCSTR) name);
963 if (proc_address == (FARPROC) NULL)
964 return((
void *) NULL);
965 return((
void *) proc_address);
993MagickPrivate MagickBooleanType NTGetModulePath(
const char *module,
char *path)
996 module_path[MagickPathExtent];
1005 handle=GetModuleHandle(module);
1006 if (handle == (HMODULE) NULL)
1007 return(MagickFalse);
1008 length=GetModuleFileName(handle,module_path,MagickPathExtent);
1010 GetPathComponent(module_path,HeadPath,path);
1035static int NTLocateGhostscript(DWORD flags,
int *root_index,
1036 const char **product_family,
int *major_version,
int *minor_version,
1048 "Artifex Ghostscript",
1057 *product_family=NULL;
1060 for (i=0; i < (ssize_t) (
sizeof(products)/
sizeof(products[0])); i++)
1063 key[MagickPathExtent];
1074 (void) FormatLocaleString(key,MagickPathExtent,
"SOFTWARE\\%s",products[i]);
1075 for (j=0; j < (ssize_t) (
sizeof(registry_roots)/
sizeof(registry_roots[0]));
1078 mode=KEY_READ | flags;
1079 if (RegOpenKeyExA(registry_roots[j].hkey,key,0,mode,&hkey) ==
1091 extent=
sizeof(key)/
sizeof(
char);
1092 for (k=0; RegEnumKeyA(hkey,k,key,extent) == ERROR_SUCCESS; k++)
1102 if (sscanf(key,
"%d.%d.%d",&major,&minor,&patch) != 3)
1103 if (sscanf(key,
"%d.%d",&major,&minor) != 2)
1105 if ((major > *major_version) ||
1106 ((major == *major_version) && (minor > *minor_version)) ||
1107 ((minor == *minor_version) && (patch > *patch_version)))
1110 *product_family=products[i];
1111 *major_version=major;
1112 *minor_version=minor;
1113 *patch_version=patch;
1117 (void) RegCloseKey(hkey);
1121 if (status == MagickFalse)
1127 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
"Ghostscript (%s) "
1128 "version %d.%d.%d",*product_family,*major_version,*minor_version,*patch_version);
1132static MagickBooleanType NTGhostscriptGetString(
const char *name,
1133 BOOL *is_64_bit,
char *value,
const size_t length)
1136 buffer[MagickPathExtent],
1140 *product_family = (
const char *) NULL;
1143 is_64_bit_version = FALSE;
1159 directory=(
char *) NULL;
1160 if (LocaleCompare(name,
"GS_DLL") == 0)
1162 directory=GetEnvironmentValue(
"MAGICK_GHOSTSCRIPT_PATH");
1163 if (directory != (
char *) NULL)
1165 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s%sgsdll64.dll",
1166 directory,DirectorySeparator);
1167 if (IsPathAccessible(buffer) != MagickFalse)
1169 directory=DestroyString(directory);
1170 (void) CopyMagickString(value,buffer,length);
1171 if (is_64_bit != NULL)
1175 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s%sgsdll32.dll",
1176 directory,DirectorySeparator);
1177 if (IsPathAccessible(buffer) != MagickFalse)
1179 directory=DestroyString(directory);
1180 (void) CopyMagickString(value,buffer,length);
1181 if (is_64_bit != NULL)
1185 return(MagickFalse);
1188 if (product_family == (
const char *) NULL)
1191#if defined(KEY_WOW64_32KEY)
1193 flags=KEY_WOW64_64KEY;
1195 flags=KEY_WOW64_32KEY;
1197 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1198 &major_version,&minor_version,&patch_version);
1199 if (product_family == (
const char *) NULL)
1201 flags=KEY_WOW64_32KEY;
1203 is_64_bit_version=TRUE;
1205 flags=KEY_WOW64_64KEY;
1209 if (product_family == (
const char *) NULL)
1211 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1212 &major_version,&minor_version,&patch_version);
1214 is_64_bit_version=TRUE;
1217 if (product_family == (
const char *) NULL)
1218 return(MagickFalse);
1219 if (is_64_bit != NULL)
1220 *is_64_bit=is_64_bit_version;
1221 (void) FormatLocaleString(buffer,MagickPathExtent,
"SOFTWARE\\%s\\%d.%.2d.%d",
1222 product_family,major_version,minor_version,patch_version);
1223 registry_value=NTGetRegistryValue(registry_roots[root_index].hkey,buffer,
1225 if (registry_value == (
unsigned char *) NULL)
1227 (void) FormatLocaleString(buffer,MagickPathExtent,
"SOFTWARE\\%s\\%d.%02d",
1228 product_family,major_version,minor_version);
1229 registry_value=NTGetRegistryValue(registry_roots[root_index].hkey,buffer,
1232 if (registry_value == (
unsigned char *) NULL)
1233 return(MagickFalse);
1234 (void) CopyMagickString(value,(
const char *) registry_value,length);
1235 registry_value=(
unsigned char *) RelinquishMagickMemory(registry_value);
1236 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1237 "registry: \"%s\\%s\\%s\"=\"%s\"",registry_roots[root_index].name,
1242static MagickBooleanType NTGhostscriptDLL(
char *path,
int length)
1245 dll[MagickPathExtent] = {
"" };
1251 if ((*dll ==
'\0') &&
1252 (NTGhostscriptGetString(
"GS_DLL",&is_64_bit,dll,
sizeof(dll)) != MagickTrue))
1253 return(MagickFalse);
1256 return(MagickFalse);
1259 return(MagickFalse);
1261 (void) CopyMagickString(path,dll,length);
1265static inline MagickBooleanType NTGhostscriptHasValidHandle()
1267 if ((nt_ghost_info.delete_instance == NULL) || (ghost_info.exit == NULL) ||
1268 (nt_ghost_info.new_instance == NULL) || (ghost_info.set_stdio == NULL) ||
1269 (ghost_info.init_with_args == NULL) || (ghost_info.revision == NULL))
1270 return(MagickFalse);
1274MagickPrivate
const GhostInfo *NTGhostscriptDLLVectors(
void)
1277 path[MagickPathExtent];
1280 ActivateSemaphoreInfo(&ghost_semaphore);
1281 LockSemaphoreInfo(ghost_semaphore);
1282 if (ghost_handle != (
void *) NULL)
1284 UnlockSemaphoreInfo(ghost_semaphore);
1285 if (NTGhostscriptHasValidHandle() == MagickTrue)
1286 return(&ghost_info);
1289 if (NTGhostscriptDLL(path,
sizeof(path)) != MagickTrue)
1291 UnlockSemaphoreInfo(ghost_semaphore);
1294 ghost_handle=lt_dlopen(path);
1295 if (ghost_handle == (
void *) NULL)
1297 UnlockSemaphoreInfo(ghost_semaphore);
1300 (void) memset((
void *) &nt_ghost_info,0,
sizeof(NTGhostInfo));
1301 nt_ghost_info.delete_instance=(void (MagickDLLCall *)(gs_main_instance *)) (
1302 lt_dlsym(ghost_handle,
"gsapi_delete_instance"));
1303 nt_ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,
1304 void *)) (lt_dlsym(ghost_handle,
"gsapi_new_instance"));
1305 nt_ghost_info.has_instance=MagickFalse;
1306 (void) memset((
void *) &ghost_info,0,
sizeof(
GhostInfo));
1307 ghost_info.delete_instance=NTGhostscriptDeleteInstance;
1308 ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
1309 lt_dlsym(ghost_handle,
"gsapi_exit");
1310 ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
1311 char **)) (lt_dlsym(ghost_handle,
"gsapi_init_with_args"));
1312 ghost_info.new_instance=NTGhostscriptNewInstance;
1313 ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,
const char *,
1314 int,
int *)) (lt_dlsym(ghost_handle,
"gsapi_run_string"));
1315 ghost_info.set_arg_encoding=(int (MagickDLLCall*)(gs_main_instance*, int)) (
1316 lt_dlsym(ghost_handle,
"gsapi_set_arg_encoding"));
1317 ghost_info.set_stdio=(int (MagickDLLCall *)(gs_main_instance *,int(
1318 MagickDLLCall *)(
void *,
char *,int),
int(MagickDLLCall *)(
void *,
1319 const char *,int),
int(MagickDLLCall *)(
void *,
const char *,int)))
1320 (lt_dlsym(ghost_handle,
"gsapi_set_stdio"));
1322 lt_dlsym(ghost_handle,
"gsapi_revision"));
1323 UnlockSemaphoreInfo(ghost_semaphore);
1324 if (NTGhostscriptHasValidHandle() == MagickTrue)
1325 return(&ghost_info);
1355MagickPrivate
void NTGhostscriptEXE(
char *path,
int length)
1361 program[MagickPathExtent] = {
"" };
1364 is_64_bit_version = FALSE;
1366 if (*program ==
'\0')
1369 ActivateSemaphoreInfo(&ghost_semaphore);
1370 LockSemaphoreInfo(ghost_semaphore);
1371 if (*program ==
'\0')
1373 if (NTGhostscriptGetString(
"GS_DLL",&is_64_bit_version,program,
1374 sizeof(program)) == MagickFalse)
1376 UnlockSemaphoreInfo(ghost_semaphore);
1378 (void) CopyMagickString(program,
"gswin64c.exe",
sizeof(program));
1380 (void) CopyMagickString(program,
"gswin32c.exe",
sizeof(program));
1382 (void) CopyMagickString(path,program,length);
1385 p=strrchr(program,
'\\');
1386 if (p != (
char *) NULL)
1390 (void) ConcatenateMagickString(program,is_64_bit_version ?
1391 "gswin64c.exe" :
"gswin32c.exe",
sizeof(program));
1394 UnlockSemaphoreInfo(ghost_semaphore);
1396 (void) CopyMagickString(path,program,length);
1424MagickPrivate MagickBooleanType NTGhostscriptFonts(
char *path,
int length)
1427 buffer[MagickPathExtent],
1429 filename[MagickPathExtent];
1436 directory=GetEnvironmentValue(
"MAGICK_GHOSTSCRIPT_FONT_PATH");
1437 if (directory != (
char *) NULL)
1439 (void) CopyMagickString(buffer,directory,MagickPathExtent);
1440 directory=DestroyString(directory);
1444 if (NTGhostscriptGetString(
"GS_LIB",NULL,buffer,
1445 MagickPathExtent) == MagickFalse)
1446 return(MagickFalse);
1448 for (p=buffer-1; p != (
char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1450 (void) CopyMagickString(path,p+1,length+1);
1451 q=strchr(path,DirectoryListSeparator);
1452 if (q != (
char *) NULL)
1454 (void) FormatLocaleString(filename,MagickPathExtent,
"%s%sfonts.dir",path,
1455 DirectorySeparator);
1456 if (IsPathAccessible(filename) != MagickFalse)
1458 (void) FormatLocaleString(filename,MagickPathExtent,
"%s%sn019003l.pfb",path,
1459 DirectorySeparator);
1460 if (IsPathAccessible(filename) != MagickFalse)
1464 return(MagickFalse);
1486MagickPrivate
void NTGhostscriptUnLoadDLL(
void)
1489 ActivateSemaphoreInfo(&ghost_semaphore);
1490 LockSemaphoreInfo(ghost_semaphore);
1491 if (ghost_handle != (
void *) NULL)
1493 (void) lt_dlclose(ghost_handle);
1494 ghost_handle=(
void *) NULL;
1495 (void) memset((
void *) &ghost_info,0,
sizeof(
GhostInfo));
1497 UnlockSemaphoreInfo(ghost_semaphore);
1498 RelinquishSemaphoreInfo(&ghost_semaphore);
1520MagickExport MagickBooleanType NTLongPathsEnabled()
1523 long_paths_enabled = 2;
1525 if (long_paths_enabled == 2)
1538 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1539 status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1540 "SYSTEM\\CurrentControlSet\\Control\\FileSystem",0,KEY_READ,
1542 if (status != ERROR_SUCCESS)
1544 RegCloseKey(registry_key);
1545 long_paths_enabled=0;
1546 return(MagickFalse);
1549 status=RegQueryValueExA(registry_key,
"LongPathsEnabled",0,&type,NULL,
1551 if ((status != ERROR_SUCCESS) || (type != REG_DWORD))
1553 RegCloseKey(registry_key);
1554 long_paths_enabled=0;
1555 return(MagickFalse);
1558 status=RegQueryValueExA(registry_key,
"LongPathsEnabled",0,&type,
1559 (LPBYTE) &value,&size);
1560 RegCloseKey(registry_key);
1561 if (status != ERROR_SUCCESS)
1563 long_paths_enabled=0;
1564 return(MagickFalse);
1566 long_paths_enabled=(size_t) value;
1568 return(long_paths_enabled == 1 ? MagickTrue : MagickFalse);
1590MagickPrivate
void *NTMapMemory(
char *address,
size_t length,
int protection,
1591 int flags,
int file,MagickOffsetType offset)
1610 file_handle=INVALID_HANDLE_VALUE;
1611 low_length=(DWORD) (length & 0xFFFFFFFFUL);
1612 high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1613 map_handle=INVALID_HANDLE_VALUE;
1615 low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1616 high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1618 if (protection & PROT_WRITE)
1620 access_mode=FILE_MAP_WRITE;
1621 if (!(flags & MAP_PRIVATE))
1622 protection_mode=PAGE_READWRITE;
1625 access_mode=FILE_MAP_COPY;
1626 protection_mode=PAGE_WRITECOPY;
1630 if (protection & PROT_READ)
1632 access_mode=FILE_MAP_READ;
1633 protection_mode=PAGE_READONLY;
1635 if ((file == -1) && (flags & MAP_ANONYMOUS))
1636 file_handle=INVALID_HANDLE_VALUE;
1638 file_handle=(HANDLE) _get_osfhandle(file);
1639 map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1643 map=(
void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1645 CloseHandle(map_handle);
1647 if (map == (
void *) NULL)
1648 return((
void *) ((
char *) MAP_FAILED));
1649 return((
void *) ((
char *) map));
1675MagickPrivate
DIR *NTOpenDirectory(
const char *path)
1684 file_specification[MagickPathExtent];
1686 assert(path != (
const char *) NULL);
1687 length=MultiByteToWideChar(CP_UTF8,0,path,-1,file_specification,
1690 return((
DIR *) NULL);
1691 if(wcsncat(file_specification,L
"\\*.*",MagickPathExtent-wcslen(
1692 file_specification)-1) == (
wchar_t *) NULL)
1693 return((
DIR *) NULL);
1694 entry=(
DIR *) AcquireCriticalMemory(
sizeof(
DIR));
1695 entry->firsttime=TRUE;
1696 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1697 if (entry->hSearch == INVALID_HANDLE_VALUE)
1699 entry=(
DIR *) RelinquishMagickMemory(entry);
1700 return((
DIR *) NULL);
1730static UINT ChangeErrorMode(
void)
1733 (CALLBACK *GETERRORMODE)(void);
1744 mode=SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1746 handle=GetModuleHandle(
"kernel32.dll");
1747 if (handle == (HMODULE) NULL)
1748 return SetErrorMode(mode);
1750 getErrorMode=(GETERRORMODE) NTGetLibrarySymbol(handle,
"GetErrorMode");
1751 if (getErrorMode != (GETERRORMODE) NULL)
1752 mode=getErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1754 return SetErrorMode(mode);
1757static inline void *NTLoadLibrary(
const char *filename)
1765 library=(
void *) NULL;
1766 path=create_wchar_path(filename);
1767 if (path != (
wchar_t *) NULL)
1769 library=LoadLibraryExW(path,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1770 path=(
wchar_t *) RelinquishMagickMemory(path);
1775MagickPrivate
void *NTOpenLibrary(
const char *filename)
1783 mode=ChangeErrorMode();
1784 handle=NTLoadLibrary(filename);
1785#if defined(MAGICKCORE_LTDL_DELEGATE)
1786 if (handle == (
void *) NULL)
1789 path[MagickPathExtent];
1795 p=lt_dlgetsearchpath();
1796 while (p != (
const char*) NULL)
1798 q=strchr(p,DirectoryListSeparator);
1799 if (q != (
const char*) NULL)
1800 (void) CopyMagickString(path,p,q-p+1);
1802 (
void) CopyMagickString(path,p,MagickPathExtent);
1803 (void) ConcatenateMagickString(path,DirectorySeparator,MagickPathExtent);
1804 (void) ConcatenateMagickString(path,filename,MagickPathExtent);
1805 handle=NTLoadLibrary(path);
1806 if (handle != (
void *) NULL || q == (
const char*) NULL)
1840MagickPrivate
struct dirent *NTReadDirectory(
DIR *entry)
1848 if (entry == (
DIR *) NULL)
1849 return((
struct dirent *) NULL);
1850 if (!entry->firsttime)
1852 status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
1854 return((
struct dirent *) NULL);
1856 length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
1857 entry->file_info.d_name,
sizeof(entry->file_info.d_name),NULL,NULL);
1859 return((
struct dirent *) NULL);
1860 entry->firsttime=FALSE;
1861 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1862 return(&entry->file_info);
1897MagickPrivate
unsigned char *NTRegistryKeyLookup(
const char *subkey)
1900 package_key[MagickPathExtent] =
"";
1905 (void) FormatLocaleString(package_key,MagickPathExtent,
1906 "SOFTWARE\\%s\\%s\\Q:%d",MagickPackageName,MagickLibVersionText,
1907 MAGICKCORE_QUANTUM_DEPTH);
1908 value=NTGetRegistryValue(HKEY_LOCAL_MACHINE,package_key,0,subkey);
1909 if (value == (
unsigned char *) NULL)
1910 value=NTGetRegistryValue(HKEY_CURRENT_USER,package_key,0,subkey);
1939MagickPrivate MagickBooleanType NTReportEvent(
const char *event,
1940 const MagickBooleanType error)
1951 handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
1953 return(MagickFalse);
1955 type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
1956 ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
1957 DeregisterEventSource(handle);
1986MagickPrivate
unsigned char *NTResourceToBlob(
const char *
id)
1989#ifndef MAGICKCORE_LIBRARY_NAME
1991 path[MagickPathExtent];
2010 assert(
id != (
const char *) NULL);
2011 if (IsEventLogging() != MagickFalse)
2012 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
id);
2013#ifdef MAGICKCORE_LIBRARY_NAME
2014 handle=GetModuleHandle(MAGICKCORE_LIBRARY_NAME);
2016 (void) FormatLocaleString(path,MagickPathExtent,
"%s%s%s",GetClientPath(),
2017 DirectorySeparator,GetClientName());
2018 if (IsPathAccessible(path) != MagickFalse)
2019 handle=GetModuleHandle(path);
2021 handle=GetModuleHandle(0);
2024 return((
unsigned char *) NULL);
2025 resource=FindResource(handle,
id,
"IMAGEMAGICK");
2027 return((
unsigned char *) NULL);
2028 global=LoadResource(handle,resource);
2030 return((
unsigned char *) NULL);
2031 length=SizeofResource(handle,resource);
2032 value=(
unsigned char *) LockResource(global);
2035 FreeResource(global);
2036 return((
unsigned char *) NULL);
2038 blob=(
unsigned char *) AcquireQuantumMemory(length+MagickPathExtent,
2040 if (blob != (
unsigned char *) NULL)
2042 (void) memcpy(blob,value,length);
2045 UnlockResource(global);
2046 FreeResource(global);
2075MagickPrivate
int NTSystemCommand(
const char *command,
char *output)
2077#define CleanupOutputHandles \
2078 if (read_output != (HANDLE) NULL) \
2080 CloseHandle(read_output); \
2081 read_output=(HANDLE) NULL; \
2082 CloseHandle(write_output); \
2083 write_output=(HANDLE) NULL; \
2086#define CopyLastError \
2087 last_error=GetLastError(); \
2088 if (output != (char *) NULL) \
2090 error=NTGetLastErrorMessage(last_error); \
2091 if (error != (char *) NULL) \
2093 CopyMagickString(output,error,MagickPathExtent); \
2094 error=DestroyString(error); \
2100 local_command[MagickPathExtent];
2125 if (command == (
char *) NULL)
2127 read_output=(HANDLE) NULL;
2128 write_output=(HANDLE) NULL;
2129 GetStartupInfo(&startup_info);
2130 startup_info.dwFlags=STARTF_USESHOWWINDOW;
2131 startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
2132 (void) CopyMagickString(local_command,command,MagickPathExtent);
2133 asynchronous=command[strlen(command)-1] ==
'&' ? MagickTrue : MagickFalse;
2134 if (asynchronous != MagickFalse)
2136 local_command[strlen(command)-1]=
'\0';
2137 startup_info.wShowWindow=SW_SHOWDEFAULT;
2141 if (command[strlen(command)-1] ==
'|')
2142 local_command[strlen(command)-1]=
'\0';
2144 startup_info.wShowWindow=SW_HIDE;
2145 read_output=(HANDLE) NULL;
2146 if (output != (
char *) NULL)
2148 if (CreatePipe(&read_output,&write_output,NULL,0))
2150 if (SetHandleInformation(write_output,HANDLE_FLAG_INHERIT,
2151 HANDLE_FLAG_INHERIT))
2153 startup_info.dwFlags|=STARTF_USESTDHANDLES;
2154 startup_info.hStdOutput=write_output;
2155 startup_info.hStdError=write_output;
2158 CleanupOutputHandles;
2161 read_output=(HANDLE) NULL;
2164 status=CreateProcess((LPCTSTR) NULL,local_command,(LPSECURITY_ATTRIBUTES)
2165 NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) TRUE,(DWORD)
2166 NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
2171 CleanupOutputHandles;
2172 return(last_error == ERROR_FILE_NOT_FOUND ? 127 : -1);
2174 if (output != (
char *) NULL)
2176 if (asynchronous != MagickFalse)
2177 return(status == 0);
2179 status=STATUS_TIMEOUT;
2180 while (status == STATUS_TIMEOUT)
2185 status=WaitForSingleObject(process_info.hProcess,1000);
2187 if (read_output != (HANDLE) NULL)
2188 if (!PeekNamedPipe(read_output,NULL,0,NULL,&size,NULL))
2193 buffer[MagickPathExtent];
2198 if (ReadFile(read_output,buffer,MagickPathExtent-1,&bytes_read,NULL))
2203 count=MagickMin(MagickPathExtent-output_offset,
2204 (
size_t) bytes_read+1);
2207 CopyMagickString(output+output_offset,buffer,count);
2208 output_offset+=count-1;
2211 if (!PeekNamedPipe(read_output,NULL,0,NULL,&size,NULL))
2215 if (status != WAIT_OBJECT_0)
2218 CleanupOutputHandles;
2221 status=GetExitCodeProcess(process_info.hProcess,&child_status);
2225 CleanupOutputHandles;
2228 CloseHandle(process_info.hProcess);
2229 CloseHandle(process_info.hThread);
2230 CleanupOutputHandles;
2231 return((
int) child_status);
2257MagickPrivate ssize_t NTSystemConfiguration(
int name)
2266 GetSystemInfo(&system_info);
2267 return(system_info.dwPageSize);
2269 case _SC_PHYS_PAGES:
2277 status.dwLength=
sizeof(status);
2278 if (GlobalMemoryStatusEx(&status) == 0)
2280 GetSystemInfo(&system_info);
2281 return((ssize_t) status.ullTotalPhys/system_info.dwPageSize);
2315MagickPrivate
int NTTruncateFile(
int file,off_t length)
2327 file_handle=(HANDLE) _get_osfhandle(file);
2328 if (file_handle == INVALID_HANDLE_VALUE)
2330 low=(long) (length & 0xffffffffUL);
2331 high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2332 file_pointer=SetFilePointer(file_handle,low,&high,FILE_BEGIN);
2333 if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2335 if (SetEndOfFile(file_handle) == 0)
2364MagickPrivate
int NTUnmapMemory(
void *map,
size_t length)
2367 if (UnmapViewOfFile(map) == 0)
2400MagickPrivate
void NTWarningHandler(
const ExceptionType severity,
2401 const char *reason,
const char *description)
2404 buffer[2*MagickPathExtent];
2407 if (reason == (
char *) NULL)
2409 if (description == (
char *) NULL)
2410 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s.\n",GetClientName(),
2413 (
void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s (%s).\n",
2414 GetClientName(),reason,description);
2415 (void) MessageBox(NULL,buffer,
"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2416 MB_SETFOREGROUND | MB_ICONINFORMATION);
2438MagickPrivate
void NTWindowsGenesis(
void)
2443 mode=GetEnvironmentValue(
"MAGICK_ERRORMODE");
2444 if (mode != (
char *) NULL)
2446 (void) SetErrorMode(StringToInteger(mode));
2447 mode=DestroyString(mode);
2449#if defined(_DEBUG) && !defined(__MINGW32__)
2450 if (IsEventLogging() != MagickFalse)
2455 debug=_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
2457 debug |= _CRTDBG_DELAY_FREE_MEM_DF;
2458 debug |= _CRTDBG_LEAK_CHECK_DF;
2459 (void) _CrtSetDbgFlag(debug);
2466#if defined(MAGICKCORE_INSTALLED_SUPPORT)
2471 path=NTRegistryKeyLookup(
"LibPath");
2472 if (path != (
unsigned char *) NULL)
2477 lib_path=create_wchar_path((
const char *) path);
2478 if (lib_path != (
wchar_t *) NULL)
2480 SetDllDirectoryW(lib_path);
2481 lib_path=(
wchar_t *) RelinquishMagickMemory(lib_path);
2483 path=(
unsigned char *) RelinquishMagickMemory(path);
2508MagickPrivate
void NTWindowsTerminus(
void)
2510 NTGhostscriptUnLoadDLL();
2511 DistributeCacheTerminus();