41#include "magick/studio.h"
42#if defined(MAGICKCORE_WINDOWS_SUPPORT)
43#include "magick/client.h"
44#include "magick/exception-private.h"
45#include "magick/image-private.h"
46#include "magick/locale_.h"
47#include "magick/log.h"
48#include "magick/magick.h"
49#include "magick/memory_.h"
50#include "magick/memory-private.h"
51#include "magick/nt-base.h"
52#include "magick/nt-base-private.h"
53#include "magick/resource_.h"
54#include "magick/timer.h"
55#include "magick/string_.h"
56#include "magick/string-private.h"
57#include "magick/utility.h"
58#include "magick/utility-private.h"
59#include "magick/version.h"
60#if defined(MAGICKCORE_LTDL_DELEGATE)
63#if defined(MAGICKCORE_CIPHER_SUPPORT)
71#if !defined(MAP_FAILED)
72#define MAP_FAILED ((void *)(LONG_PTR) -1)
74#define MaxWideByteExtent 100
88typedef struct _NTGhostInfo
91 (MagickDLLCall *delete_instance)(gs_main_instance *);
94 (MagickDLLCall *new_instance)(gs_main_instance **,
void *);
103#if !defined(MAGICKCORE_LTDL_DELEGATE)
105 *lt_slsearchpath = (
char *) NULL;
115 *ghost_handle = (
void *) NULL;
122 *wsaData = (WSADATA*) NULL;
125 long_paths_enabled = 2;
135const registry_roots[2] =
137 { HKEY_CURRENT_USER,
"HKEY_CURRENT_USER" },
138 { HKEY_LOCAL_MACHINE,
"HKEY_LOCAL_MACHINE" }
144#if !defined(MAGICKCORE_WINDOWS_SUPPORT)
145extern "C" BOOL WINAPI
146 DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved);
149static void MagickDLLCall NTGhostscriptDeleteInstance(
150 gs_main_instance *instance)
152 LockSemaphoreInfo(ghost_semaphore);
153 nt_ghost_info.delete_instance(instance);
154 nt_ghost_info.has_instance=MagickFalse;
155 UnlockSemaphoreInfo(ghost_semaphore);
158static int MagickDLLCall NTGhostscriptNewInstance(gs_main_instance **pinstance,
164 LockSemaphoreInfo(ghost_semaphore);
166 if (nt_ghost_info.has_instance == MagickFalse)
168 status=nt_ghost_info.new_instance(pinstance,caller_handle);
170 nt_ghost_info.has_instance=MagickTrue;
172 UnlockSemaphoreInfo(ghost_semaphore);
176static inline char *create_utf8_string(
const wchar_t *wideChar)
184 count=WideCharToMultiByte(CP_UTF8,0,wideChar,-1,NULL,0,NULL,NULL);
186 return((
char *) NULL);
187 utf8=(
char *) NTAcquireQuantumMemory(count+1,
sizeof(*utf8));
188 if (utf8 == (
char *) NULL)
189 return((
char *) NULL);
190 count=WideCharToMultiByte(CP_UTF8,0,wideChar,-1,utf8,count,NULL,NULL);
193 utf8=DestroyString(utf8);
194 return((
char *) NULL);
200static unsigned char *NTGetRegistryValue(HKEY root,
const char *key,DWORD flags,
217 wide_name[MaxWideByteExtent];
219 value=(
unsigned char *) NULL;
220 status=RegOpenKeyExA(root,key,0,(KEY_READ | flags),®istry_key);
221 if (status != ERROR_SUCCESS)
223 if (MultiByteToWideChar(CP_UTF8,0,name,-1,wide_name,MaxWideByteExtent) == 0)
225 RegCloseKey(registry_key);
228 status=RegQueryValueExW(registry_key,wide_name,0,&type,0,&size);
229 if ((status == ERROR_SUCCESS) && (type == REG_SZ))
234 wide=(LPBYTE) NTAcquireQuantumMemory((
const size_t) size,
sizeof(*wide));
235 if (wide != (LPBYTE) NULL)
237 status=RegQueryValueExW(registry_key,wide_name,0,&type,wide,&size);
238 if ((status == ERROR_SUCCESS) && (type == REG_SZ))
239 value=(
unsigned char *) create_utf8_string((
const wchar_t *) wide);
240 wide=(LPBYTE) RelinquishMagickMemory(wide);
243 RegCloseKey(registry_key);
290#if defined(_DLL) && defined(ProvideDllMain)
291BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
293 magick_unreferenced(lpvReserved);
297 case DLL_PROCESS_ATTACH:
308 MagickCoreGenesis((
const char*) NULL,MagickFalse);
309 wide_path=(
wchar_t *) NTAcquireQuantumMemory(MaxTextExtent,
311 if (wide_path == (
wchar_t *) NULL)
313 count=(ssize_t) GetModuleFileNameW(handle,wide_path,MaxTextExtent);
319 module_path=create_utf8_string(wide_path);
320 for ( ; count > 0; count--)
321 if (module_path[count] ==
'\\')
323 module_path[count+1]=
'\0';
326 path=(
char *) NTAcquireQuantumMemory(MaxTextExtent,16*
sizeof(*path));
327 if (path == (
char *) NULL)
329 module_path=DestroyString(module_path);
330 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
333 count=(ssize_t) GetEnvironmentVariable(
"PATH",path,16*MaxTextExtent);
334 if ((count != 0) && (strstr(path,module_path) == (
char *) NULL))
336 if ((strlen(module_path)+count+1) < (16*MaxTextExtent-1))
341 variable=(
char *) NTAcquireQuantumMemory(MaxTextExtent,
342 16*
sizeof(*variable));
343 if (variable == (
char *) NULL)
345 path=DestroyString(path);
346 module_path=DestroyString(module_path);
347 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
350 (void) FormatLocaleString(variable,16*MaxTextExtent,
351 "%s;%s",module_path,path);
352 SetEnvironmentVariable(
"PATH",variable);
353 variable=DestroyString(variable);
356 path=DestroyString(path);
357 module_path=DestroyString(module_path);
359 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
362 case DLL_PROCESS_DETACH:
364 MagickCoreTerminus();
374#if !defined(__MINGW32__)
399MagickPrivate
int gettimeofday (
struct timeval *time_value,
400 struct timezone *time_zone)
402#define EpochFiletime MagickLLConstant(116444736000000000)
407 if (time_value != (
struct timeval *) NULL)
418 GetSystemTimeAsFileTime(&file_time);
419 date_time.LowPart=file_time.dwLowDateTime;
420 date_time.HighPart=file_time.dwHighDateTime;
421 time=date_time.QuadPart;
424 time_value->tv_sec=(ssize_t) (time / 1000000);
425 time_value->tv_usec=(ssize_t) (time % 1000000);
427 if (time_zone != (
struct timezone *) NULL)
434 time_zone->tz_minuteswest=_timezone/60;
435 time_zone->tz_dsttime=_daylight;
466MagickPrivate
char **NTArgvToUTF8(
const int argc,
wchar_t **argv)
474 utf8=(
char **) NTAcquireQuantumMemory(argc,
sizeof(*utf8));
475 if (utf8 == (
char **) NULL)
476 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertStringToARGV");
477 for (i=0; i < (ssize_t) argc; i++)
479 utf8[i]=create_utf8_string(argv[i]);
480 if (utf8[i] == (
char *) NULL)
482 for (i--; i >= 0; i--)
483 utf8[i]=DestroyString(utf8[i]);
484 ThrowFatalException(ResourceLimitFatalError,
485 "UnableToConvertStringToARGV");
514MagickPrivate
int NTCloseDirectory(
DIR *entry)
516 assert(entry != (
DIR *) NULL);
517 if (IsEventLogging() != MagickFalse)
518 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
519 FindClose(entry->hSearch);
520 entry=(
DIR *) RelinquishMagickMemory(entry);
546MagickPrivate
int NTCloseLibrary(
void *handle)
548 return(!(FreeLibrary((HINSTANCE) handle)));
571static BOOL ControlHandler(DWORD type)
574 AsynchronousResourceComponentTerminus();
578MagickPrivate
int NTControlHandler(
void)
580 return(SetConsoleCtrlHandler((PHANDLER_ROUTINE) ControlHandler,TRUE));
602MagickPrivate
double NTElapsedTime(
void)
622 if (frequency.QuadPart == 0)
624 if (QueryPerformanceFrequency(&frequency) == 0)
625 frequency.QuadPart=1;
627 if (frequency.QuadPart > 1)
629 QueryPerformanceCounter(&performance_count);
630 return((
double) performance_count.QuadPart/frequency.QuadPart);
632 GetSystemTime(&system_time);
633 SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
634 return((
double) 1.0e-7*elapsed_time.filetime64);
665MagickPrivate
void NTErrorHandler(
const ExceptionType severity,
666 const char *reason,
const char *description)
669 buffer[3*MaxTextExtent],
673 if (reason == (
char *) NULL)
675 MagickCoreTerminus();
678 message=GetExceptionMessage(errno);
679 if ((description != (
char *) NULL) && errno)
680 (void) FormatLocaleString(buffer,MaxTextExtent,
"%s: %s (%s) [%s].\n",
681 GetClientName(),reason,description,message);
683 if (description != (
char *) NULL)
684 (void) FormatLocaleString(buffer,MaxTextExtent,
"%s: %s (%s).\n",
685 GetClientName(),reason,description);
688 (void) FormatLocaleString(buffer,MaxTextExtent,
"%s: %s [%s].\n",
689 GetClientName(),reason,message);
691 (
void) FormatLocaleString(buffer,MaxTextExtent,
"%s: %s.\n",
692 GetClientName(),reason);
693 message=DestroyString(message);
694 (void) MessageBox(NULL,buffer,
"ImageMagick Exception",MB_OK | MB_TASKMODAL |
695 MB_SETFOREGROUND | MB_ICONEXCLAMATION);
696 MagickCoreTerminus();
718MagickPrivate
int NTExitLibrary(
void)
748MagickPrivate MagickBooleanType NTGatherRandomData(
const size_t length,
749 unsigned char *random)
751#if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER) && (_MSC_VER > 1200)
758 handle=(HCRYPTPROV) NULL;
759 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
760 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
762 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
763 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
766 status=CryptGenRandom(handle,(DWORD) length,random);
769 status=CryptReleaseContext(handle,0);
772 status=CryptReleaseContext(handle,0);
805extern MagickPrivate
char *NTGetEnvironmentValue(
const char *name)
808 *environment = (
char *) NULL;
817 wide_name[MaxWideByteExtent];
819 if (MultiByteToWideChar(CP_UTF8,0,name,-1,wide_name,MaxWideByteExtent) == 0)
821 size=GetEnvironmentVariableW(wide_name,(LPWSTR) NULL,0);
824 wide=(LPWSTR) NTAcquireQuantumMemory((
const size_t) size,
sizeof(*wide));
825 if (wide == (LPWSTR) NULL)
827 if (GetEnvironmentVariableW(wide_name,wide,size) != 0)
828 environment=create_utf8_string(wide);
829 wide=(LPWSTR) RelinquishMagickMemory(wide);
857MagickPrivate MagickBooleanType NTGetExecutionPath(
char *path,
861 wide_path[MaxTextExtent];
863 (void) GetModuleFileNameW((HMODULE) NULL,wide_path,(DWORD) extent);
864 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,
891static char *NTGetLastErrorMessage(DWORD last_error)
900 buffer = (LPVOID) NULL;
902 status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
903 FORMAT_MESSAGE_FROM_SYSTEM,NULL,last_error,
904 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
906 reason=AcquireString(
"An unknown error occurred");
909 reason=AcquireString((
const char *) buffer);
935MagickPrivate
const char *NTGetLibraryError(
void)
938 last_error[MaxTextExtent];
944 error=NTGetLastErrorMessage(GetLastError());
946 (void) CopyMagickString(last_error,error,MaxTextExtent);
947 error=DestroyString(error);
976void *NTGetLibrarySymbol(
void *handle,
const char *name)
981 proc_address=GetProcAddress((HMODULE) handle,(LPCSTR) name);
982 if (proc_address == (FARPROC) NULL)
983 return((
void *) NULL);
984 return((
void *) proc_address);
1011MagickPrivate MagickBooleanType NTGetModulePath(
const char *module,
char *path)
1014 module_path[MaxTextExtent];
1023 handle=GetModuleHandle(module);
1024 if (handle == (HMODULE) NULL)
1025 return(MagickFalse);
1026 length=GetModuleFileName(handle,module_path,MaxTextExtent);
1028 GetPathComponent(module_path,HeadPath,path);
1053static int NTLocateGhostscript(DWORD flags,
int *root_index,
1054 const char **product_family,
int *major_version,
int *minor_version,
1066 "Artifex Ghostscript",
1075 *product_family=NULL;
1078 for (i=0; i < (ssize_t) (
sizeof(products)/
sizeof(products[0])); i++)
1081 key[MagickPathExtent];
1092 (void) FormatLocaleString(key,MagickPathExtent,
"SOFTWARE\\%s",products[i]);
1093 for (j=0; j < (ssize_t) (
sizeof(registry_roots)/
sizeof(registry_roots[0]));
1096 mode=KEY_READ | flags;
1097 if (RegOpenKeyExA(registry_roots[j].hkey,key,0,mode,&hkey) ==
1109 extent=
sizeof(key)/
sizeof(
char);
1110 for (k=0; RegEnumKeyA(hkey,k,key,extent) == ERROR_SUCCESS; k++)
1120 if (sscanf(key,
"%d.%d.%d",&major,&minor,&patch) != 3)
1121 if (sscanf(key,
"%d.%d",&major,&minor) != 2)
1123 if ((major > *major_version) ||
1124 ((major == *major_version) && (minor > *minor_version)) ||
1125 ((minor == *minor_version) && (patch > *patch_version)))
1128 *product_family=products[i];
1129 *major_version=major;
1130 *minor_version=minor;
1131 *patch_version=patch;
1135 (void) RegCloseKey(hkey);
1139 if (status == MagickFalse)
1145 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
"Ghostscript (%s) "
1146 "version %d.%d.%d",*product_family,*major_version,*minor_version,*patch_version);
1150static MagickBooleanType NTGhostscriptGetString(
const char *name,
1151 BOOL *is_64_bit,
char *value,
const size_t length)
1154 buffer[MagickPathExtent],
1158 *product_family = (
const char *) NULL;
1161 is_64_bit_version = FALSE;
1177 directory=(
char *) NULL;
1178 if (LocaleCompare(name,
"GS_DLL") == 0)
1180 directory=GetEnvironmentValue(
"MAGICK_GHOSTSCRIPT_PATH");
1181 if (directory != (
char *) NULL)
1183 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s%sgsdll64.dll",
1184 directory,DirectorySeparator);
1185 if (IsPathAccessible(buffer) != MagickFalse)
1187 directory=DestroyString(directory);
1188 (void) CopyMagickString(value,buffer,length);
1189 if (is_64_bit != NULL)
1193 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s%sgsdll32.dll",
1194 directory,DirectorySeparator);
1195 if (IsPathAccessible(buffer) != MagickFalse)
1197 directory=DestroyString(directory);
1198 (void) CopyMagickString(value,buffer,length);
1199 if (is_64_bit != NULL)
1203 return(MagickFalse);
1206 if (product_family == (
const char *) NULL)
1209#if defined(KEY_WOW64_32KEY)
1211 flags=KEY_WOW64_64KEY;
1213 flags=KEY_WOW64_32KEY;
1215 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1216 &major_version,&minor_version,&patch_version);
1217 if (product_family == (
const char *) NULL)
1219 flags=KEY_WOW64_32KEY;
1221 is_64_bit_version=TRUE;
1223 flags=KEY_WOW64_64KEY;
1227 if (product_family == (
const char *) NULL)
1229 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1230 &major_version,&minor_version,&patch_version);
1232 is_64_bit_version=TRUE;
1235 if (product_family == (
const char *) NULL)
1236 return(MagickFalse);
1237 if (is_64_bit != NULL)
1238 *is_64_bit=is_64_bit_version;
1239 (void) FormatLocaleString(buffer,MagickPathExtent,
"SOFTWARE\\%s\\%d.%.2d.%d",
1240 product_family,major_version,minor_version,patch_version);
1241 registry_value=NTGetRegistryValue(registry_roots[root_index].hkey,buffer,
1243 if (registry_value == (
unsigned char *) NULL)
1245 (void) FormatLocaleString(buffer,MagickPathExtent,
"SOFTWARE\\%s\\%d.%02d",
1246 product_family,major_version,minor_version);
1247 registry_value=NTGetRegistryValue(registry_roots[root_index].hkey,buffer,
1250 if (registry_value == (
unsigned char *) NULL)
1251 return(MagickFalse);
1252 (void) CopyMagickString(value,(
const char *) registry_value,length);
1253 registry_value=(
unsigned char *) RelinquishMagickMemory(registry_value);
1254 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1255 "registry: \"%s\\%s\\%s\"=\"%s\"",registry_roots[root_index].name,
1260static MagickBooleanType NTGhostscriptDLL(
char *path,
int length)
1263 dll[MagickPathExtent] = {
"" };
1269 if ((*dll ==
'\0') &&
1270 (NTGhostscriptGetString(
"GS_DLL",&is_64_bit,dll,
sizeof(dll)) != MagickTrue))
1271 return(MagickFalse);
1274 return(MagickFalse);
1277 return(MagickFalse);
1279 (void) CopyMagickString(path,dll,length);
1283static inline MagickBooleanType NTGhostscriptHasValidHandle()
1285 if ((nt_ghost_info.delete_instance == NULL) || (ghost_info.exit == NULL) ||
1286 (nt_ghost_info.new_instance == NULL) || (ghost_info.set_stdio == NULL) ||
1287 (ghost_info.init_with_args == NULL) || (ghost_info.revision == NULL))
1288 return(MagickFalse);
1292MagickPrivate
const GhostInfo *NTGhostscriptDLLVectors(
void)
1295 path[MaxTextExtent];
1298 ActivateSemaphoreInfo(&ghost_semaphore);
1299 LockSemaphoreInfo(ghost_semaphore);
1300 if (ghost_handle != (
void *) NULL)
1302 UnlockSemaphoreInfo(ghost_semaphore);
1303 if (NTGhostscriptHasValidHandle() == MagickFalse)
1305 return(&ghost_info);
1307 if (NTGhostscriptDLL(path,
sizeof(path)) == MagickFalse)
1309 UnlockSemaphoreInfo(ghost_semaphore);
1312 ghost_handle=lt_dlopen(path);
1313 if (ghost_handle == (
void *) NULL)
1315 UnlockSemaphoreInfo(ghost_semaphore);
1318 (void) memset((
void *) &nt_ghost_info,0,
sizeof(NTGhostInfo));
1319 nt_ghost_info.delete_instance=(void (MagickDLLCall *)(gs_main_instance *)) (
1320 lt_dlsym(ghost_handle,
"gsapi_delete_instance"));
1321 nt_ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,
1322 void *)) (lt_dlsym(ghost_handle,
"gsapi_new_instance"));
1323 nt_ghost_info.has_instance=MagickFalse;
1324 (void) memset((
void *) &ghost_info,0,
sizeof(
GhostInfo));
1325 ghost_info.delete_instance=NTGhostscriptDeleteInstance;
1326 ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
1327 lt_dlsym(ghost_handle,
"gsapi_exit");
1328 ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
1329 char **)) (lt_dlsym(ghost_handle,
"gsapi_init_with_args"));
1330 ghost_info.new_instance=NTGhostscriptNewInstance;
1331 ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,
const char *,
1332 int,
int *)) (lt_dlsym(ghost_handle,
"gsapi_run_string"));
1333 ghost_info.set_stdio=(int (MagickDLLCall *)(gs_main_instance *,int(
1334 MagickDLLCall *)(
void *,
char *,int),
int(MagickDLLCall *)(
void *,
1335 const char *,int),
int(MagickDLLCall *)(
void *,
const char *,int)))
1336 (lt_dlsym(ghost_handle,
"gsapi_set_stdio"));
1338 lt_dlsym(ghost_handle,
"gsapi_revision"));
1339 UnlockSemaphoreInfo(ghost_semaphore);
1340 if (NTGhostscriptHasValidHandle() == MagickFalse)
1342 return(&ghost_info);
1371MagickPrivate
int NTGhostscriptEXE(
char *path,
int length)
1377 program[MaxTextExtent] = {
"" };
1380 is_64_bit_version = FALSE;
1382 if (*program ==
'\0')
1385 ActivateSemaphoreInfo(&ghost_semaphore);
1386 LockSemaphoreInfo(ghost_semaphore);
1387 if (*program ==
'\0')
1389 if (NTGhostscriptGetString(
"GS_DLL",&is_64_bit_version,program,
1390 sizeof(program)) == MagickFalse)
1392 UnlockSemaphoreInfo(ghost_semaphore);
1394 (void) CopyMagickString(program,
"gswin64c.exe",
sizeof(program));
1396 (void) CopyMagickString(program,
"gswin32c.exe",
sizeof(program));
1398 (void) CopyMagickString(path,program,length);
1401 p=strrchr(program,
'\\');
1402 if (p != (
char *) NULL)
1406 (void) ConcatenateMagickString(program,is_64_bit_version ?
1407 "gswin64c.exe" :
"gswin32c.exe",
sizeof(program));
1410 UnlockSemaphoreInfo(ghost_semaphore);
1412 (void) CopyMagickString(path,program,length);
1441MagickPrivate
int NTGhostscriptFonts(
char *path,
int length)
1444 buffer[MaxTextExtent],
1446 filename[MaxTextExtent];
1453 directory=GetEnvironmentValue(
"MAGICK_GHOSTSCRIPT_FONT_PATH");
1454 if (directory != (
char *) NULL)
1456 (void) CopyMagickString(buffer,directory,MaxTextExtent);
1457 directory=DestroyString(directory);
1461 if (NTGhostscriptGetString(
"GS_LIB",NULL,buffer,MaxTextExtent) == MagickFalse)
1464 for (p=buffer-1; p != (
char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1466 (void) CopyMagickString(path,p+1,length+1);
1467 q=strchr(path,DirectoryListSeparator);
1468 if (q != (
char *) NULL)
1470 (void) FormatLocaleString(filename,MaxTextExtent,
"%s%sfonts.dir",path,
1471 DirectorySeparator);
1472 if (IsPathAccessible(filename) != MagickFalse)
1474 (void) FormatLocaleString(filename,MaxTextExtent,
"%s%sn019003l.pfb",path,
1475 DirectorySeparator);
1476 if (IsPathAccessible(filename) != MagickFalse)
1502MagickPrivate
int NTGhostscriptUnLoadDLL(
void)
1508 ActivateSemaphoreInfo(&ghost_semaphore);
1509 LockSemaphoreInfo(ghost_semaphore);
1511 if (ghost_handle != (
void *) NULL)
1513 status=lt_dlclose(ghost_handle);
1514 ghost_handle=(
void *) NULL;
1515 (void) memset((
void *) &ghost_info,0,
sizeof(
GhostInfo));
1517 UnlockSemaphoreInfo(ghost_semaphore);
1518 DestroySemaphoreInfo(&ghost_semaphore);
1540MagickPrivate
int NTInitializeLibrary(
void)
1563MagickPrivate
void NTInitializeWinsock(MagickBooleanType use_lock)
1568 ActivateSemaphoreInfo(&winsock_semaphore);
1569 LockSemaphoreInfo(winsock_semaphore);
1571 if (wsaData == (WSADATA *) NULL)
1573 wsaData=(WSADATA *) AcquireMagickMemory(
sizeof(WSADATA));
1574 if (WSAStartup(MAKEWORD(2,2),wsaData) != 0)
1575 ThrowFatalException(CacheFatalError,
"WSAStartup failed");
1578 UnlockSemaphoreInfo(winsock_semaphore);
1600MagickExport MagickBooleanType NTLongPathsEnabled()
1602 if (long_paths_enabled == 2)
1615 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1616 status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1617 "SYSTEM\\CurrentControlSet\\Control\\FileSystem",0,KEY_READ,
1619 if (status != ERROR_SUCCESS)
1621 long_paths_enabled=0;
1622 RegCloseKey(registry_key);
1623 return(MagickFalse);
1626 status=RegQueryValueExA(registry_key,
"LongPathsEnabled",0,&type,NULL,
1628 if ((status != ERROR_SUCCESS) || (type != REG_DWORD))
1630 long_paths_enabled=0;
1631 RegCloseKey(registry_key);
1632 return(MagickFalse);
1634 status=RegQueryValueExA(registry_key,
"LongPathsEnabled",0,&type,
1635 (LPBYTE) &value,&size);
1636 RegCloseKey(registry_key);
1637 if (status != ERROR_SUCCESS)
1639 long_paths_enabled=0;
1640 return(MagickFalse);
1642 long_paths_enabled=(size_t) value;
1644 return(long_paths_enabled == 1 ? MagickTrue : MagickFalse);
1666MagickPrivate
void *NTMapMemory(
char *address,
size_t length,
int protection,
1667 int flags,
int file,MagickOffsetType offset)
1686 file_handle=INVALID_HANDLE_VALUE;
1687 low_length=(DWORD) (length & 0xFFFFFFFFUL);
1688 high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1689 map_handle=INVALID_HANDLE_VALUE;
1691 low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1692 high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1694 if (protection & PROT_WRITE)
1696 access_mode=FILE_MAP_WRITE;
1697 if (!(flags & MAP_PRIVATE))
1698 protection_mode=PAGE_READWRITE;
1701 access_mode=FILE_MAP_COPY;
1702 protection_mode=PAGE_WRITECOPY;
1706 if (protection & PROT_READ)
1708 access_mode=FILE_MAP_READ;
1709 protection_mode=PAGE_READONLY;
1711 if ((file == -1) && (flags & MAP_ANONYMOUS))
1712 file_handle=INVALID_HANDLE_VALUE;
1714 file_handle=(HANDLE) _get_osfhandle(file);
1715 map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1719 map=(
void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1721 CloseHandle(map_handle);
1723 if (map == (
void *) NULL)
1724 return((
void *) ((
char *) MAP_FAILED));
1725 return((
void *) ((
char *) map));
1751MagickPrivate
DIR *NTOpenDirectory(
const char *path)
1760 file_specification[MaxTextExtent];
1762 assert(path != (
const char *) NULL);
1763 length=MultiByteToWideChar(CP_UTF8,0,path,-1,file_specification,
1766 return((
DIR *) NULL);
1767 if(wcsncat(file_specification,L
"\\*.*",MaxTextExtent-wcslen(
1768 file_specification)-1) == (
wchar_t*) NULL)
1769 return((
DIR *) NULL);
1770 entry=(
DIR *) AcquireCriticalMemory(
sizeof(
DIR));
1771 entry->firsttime=TRUE;
1772 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1773 if (entry->hSearch == INVALID_HANDLE_VALUE)
1775 entry=(
DIR *) RelinquishMagickMemory(entry);
1776 return((
DIR *) NULL);
1806static inline const char *GetSearchPath(
void)
1808#if defined(MAGICKCORE_LTDL_DELEGATE)
1809 return(lt_dlgetsearchpath());
1811 return(lt_slsearchpath);
1815static UINT ChangeErrorMode(
void)
1818 (CALLBACK *GETERRORMODE)(void);
1829 mode=SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1831 handle=GetModuleHandle(
"kernel32.dll");
1832 if (handle == (HMODULE) NULL)
1833 return SetErrorMode(mode);
1835 getErrorMode=(GETERRORMODE) NTGetLibrarySymbol(handle,
"GetErrorMode");
1836 if (getErrorMode != (GETERRORMODE) NULL)
1837 mode=getErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1839 return SetErrorMode(mode);
1842static inline void *NTLoadLibrary(
const char *filename)
1848 path[MaxTextExtent];
1850 length=MultiByteToWideChar(CP_UTF8,0,filename,-1,path,MaxTextExtent);
1852 return((
void *) NULL);
1853 return (
void *) LoadLibraryExW(path,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1856MagickPrivate
void *NTOpenLibrary(
const char *filename)
1859 path[MaxTextExtent];
1871 mode=ChangeErrorMode();
1872 handle=NTLoadLibrary(filename);
1873 if (handle == (
void *) NULL)
1876 while (p != (
const char*) NULL)
1878 q=strchr(p,DirectoryListSeparator);
1879 if (q != (
const char*) NULL)
1880 (void) CopyMagickString(path,p,q-p+1);
1882 (
void) CopyMagickString(path,p,MaxTextExtent);
1883 (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
1884 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
1885 handle=NTLoadLibrary(path);
1886 if (handle != (
void *) NULL || q == (
const char*) NULL)
1919MagickPrivate
struct dirent *NTReadDirectory(
DIR *entry)
1927 if (entry == (
DIR *) NULL)
1928 return((
struct dirent *) NULL);
1929 if (!entry->firsttime)
1931 status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
1933 return((
struct dirent *) NULL);
1935 length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
1936 entry->file_info.d_name,
sizeof(entry->file_info.d_name),NULL,NULL);
1938 return((
struct dirent *) NULL);
1939 entry->firsttime=FALSE;
1940 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1941 return(&entry->file_info);
1976MagickPrivate
unsigned char *NTRegistryKeyLookup(
const char *subkey)
1979 package_key[MaxTextExtent] =
"";
1984 (void) FormatLocaleString(package_key,MagickPathExtent,
1985 "SOFTWARE\\%s\\%s\\Q:%d",MagickPackageName,MagickLibVersionText,
1986 MAGICKCORE_QUANTUM_DEPTH);
1987 value=NTGetRegistryValue(HKEY_LOCAL_MACHINE,package_key,0,subkey);
1988 if (value == (
unsigned char *) NULL)
1989 value=NTGetRegistryValue(HKEY_CURRENT_USER,package_key,0,subkey);
2018MagickPrivate MagickBooleanType NTReportEvent(
const char *event,
2019 const MagickBooleanType error)
2030 handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
2032 return(MagickFalse);
2034 type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
2035 ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
2036 DeregisterEventSource(handle);
2065MagickPrivate
unsigned char *NTResourceToBlob(
const char *
id)
2068#ifndef MAGICKCORE_LIBRARY_NAME
2070 path[MaxTextExtent];
2089 assert(
id != (
const char *) NULL);
2090 if (IsEventLogging() != MagickFalse)
2091 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
id);
2092#ifdef MAGICKCORE_LIBRARY_NAME
2093 handle=GetModuleHandle(MAGICKCORE_LIBRARY_NAME);
2095 (void) FormatLocaleString(path,MaxTextExtent,
"%s%s%s",GetClientPath(),
2096 DirectorySeparator,GetClientName());
2097 if (IsPathAccessible(path) != MagickFalse)
2098 handle=GetModuleHandle(path);
2100 handle=GetModuleHandle(0);
2103 return((
unsigned char *) NULL);
2104 resource=FindResource(handle,
id,
"IMAGEMAGICK");
2106 return((
unsigned char *) NULL);
2107 global=LoadResource(handle,resource);
2109 return((
unsigned char *) NULL);
2110 length=SizeofResource(handle,resource);
2111 value=(
unsigned char *) LockResource(global);
2114 FreeResource(global);
2115 return((
unsigned char *) NULL);
2117 blob=(
unsigned char *) AcquireQuantumMemory(length+MaxTextExtent,
2119 if (blob != (
unsigned char *) NULL)
2121 (void) memcpy(blob,value,length);
2124 UnlockResource(global);
2125 FreeResource(global);
2153MagickPrivate
int NTSetSearchPath(
const char *path)
2155#if defined(MAGICKCORE_LTDL_DELEGATE)
2156 lt_dlsetsearchpath(path);
2158 if (lt_slsearchpath != (
char *) NULL)
2159 lt_slsearchpath=DestroyString(lt_slsearchpath);
2160 if (path != (
char *) NULL)
2161 lt_slsearchpath=AcquireString(path);
2191MagickPrivate
int NTSystemCommand(
const char *command,
char *output)
2193#define CleanupOutputHandles \
2194 if (read_output != (HANDLE) NULL) \
2196 CloseHandle(read_output); \
2197 read_output=(HANDLE) NULL; \
2198 CloseHandle(write_output); \
2199 write_output=(HANDLE) NULL; \
2202#define CopyLastError \
2203 last_error=GetLastError(); \
2204 if (output != (char *) NULL) \
2206 error=NTGetLastErrorMessage(last_error); \
2207 if (error != (char *) NULL) \
2209 CopyMagickString(output,error,MaxTextExtent); \
2210 error=DestroyString(error); \
2216 local_command[MaxTextExtent];
2241 if (command == (
char *) NULL)
2243 read_output=(HANDLE) NULL;
2244 write_output=(HANDLE) NULL;
2245 GetStartupInfo(&startup_info);
2246 startup_info.dwFlags=STARTF_USESHOWWINDOW;
2247 startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
2248 (void) CopyMagickString(local_command,command,MaxTextExtent);
2249 asynchronous=command[strlen(command)-1] ==
'&' ? MagickTrue : MagickFalse;
2250 if (asynchronous != MagickFalse)
2252 local_command[strlen(command)-1]=
'\0';
2253 startup_info.wShowWindow=SW_SHOWDEFAULT;
2257 if (command[strlen(command)-1] ==
'|')
2258 local_command[strlen(command)-1]=
'\0';
2260 startup_info.wShowWindow=SW_HIDE;
2261 read_output=(HANDLE) NULL;
2262 if (output != (
char *) NULL)
2264 if (CreatePipe(&read_output,&write_output,NULL,0))
2266 if (SetHandleInformation(write_output,HANDLE_FLAG_INHERIT,
2267 HANDLE_FLAG_INHERIT))
2269 startup_info.dwFlags|=STARTF_USESTDHANDLES;
2270 startup_info.hStdOutput=write_output;
2271 startup_info.hStdError=write_output;
2274 CleanupOutputHandles;
2277 read_output=(HANDLE) NULL;
2280 status=CreateProcess((LPCTSTR) NULL,local_command,(LPSECURITY_ATTRIBUTES)
2281 NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) TRUE,(DWORD)
2282 NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
2287 CleanupOutputHandles;
2288 return(last_error == ERROR_FILE_NOT_FOUND ? 127 : -1);
2290 if (output != (
char *) NULL)
2292 if (asynchronous != MagickFalse)
2293 return(status == 0);
2295 status=STATUS_TIMEOUT;
2296 while (status == STATUS_TIMEOUT)
2301 status=WaitForSingleObject(process_info.hProcess,1000);
2303 if (read_output != (HANDLE) NULL)
2304 if (!PeekNamedPipe(read_output,NULL,0,NULL,&size,NULL))
2309 buffer[MagickPathExtent];
2314 if (ReadFile(read_output,buffer,MagickPathExtent-1,&bytes_read,NULL))
2319 count=MagickMin(MagickPathExtent-output_offset,
2320 (
size_t) bytes_read+1);
2323 CopyMagickString(output+output_offset,buffer,count);
2324 output_offset+=count-1;
2327 if (!PeekNamedPipe(read_output,NULL,0,NULL,&size,NULL))
2331 if (status != WAIT_OBJECT_0)
2334 CleanupOutputHandles;
2337 status=GetExitCodeProcess(process_info.hProcess,&child_status);
2341 CleanupOutputHandles;
2344 CloseHandle(process_info.hProcess);
2345 CloseHandle(process_info.hThread);
2346 CleanupOutputHandles;
2347 return((
int) child_status);
2373MagickPrivate ssize_t NTSystemConfiguration(
int name)
2382 GetSystemInfo(&system_info);
2383 return(system_info.dwPageSize);
2385 case _SC_PHYS_PAGES:
2393 status.dwLength=
sizeof(status);
2394 if (GlobalMemoryStatusEx(&status) == 0)
2396 GetSystemInfo(&system_info);
2397 return((ssize_t) status.ullTotalPhys/system_info.dwPageSize);
2431MagickPrivate
int NTTruncateFile(
int file,off_t length)
2443 file_handle=(HANDLE) _get_osfhandle(file);
2444 if (file_handle == INVALID_HANDLE_VALUE)
2446 low=(long) (length & 0xffffffffUL);
2447 high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2448 file_pointer=SetFilePointer(file_handle,low,&high,FILE_BEGIN);
2449 if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2451 if (SetEndOfFile(file_handle) == 0)
2480MagickPrivate
int NTUnmapMemory(
void *map,
size_t length)
2483 if (UnmapViewOfFile(map) == 0)
2507MagickPrivate
double NTUserTime(
void)
2537 OsVersionInfo.dwOSVersionInfoSize=
sizeof(OSVERSIONINFO);
2538 GetVersionEx(&OsVersionInfo);
2539 if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
2540 return(NTElapsedTime());
2541 status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
2542 &kernel_time.filetime,&user_time.filetime);
2545 return((
double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
2576MagickPrivate
void NTWarningHandler(
const ExceptionType severity,
2577 const char *reason,
const char *description)
2580 buffer[2*MaxTextExtent];
2583 if (reason == (
char *) NULL)
2585 if (description == (
char *) NULL)
2586 (void) FormatLocaleString(buffer,MaxTextExtent,
"%s: %s.\n",GetClientName(),
2589 (
void) FormatLocaleString(buffer,MaxTextExtent,
"%s: %s (%s).\n",
2590 GetClientName(),reason,description);
2591 (void) MessageBox(NULL,buffer,
"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2592 MB_SETFOREGROUND | MB_ICONINFORMATION);
2613MagickPrivate
void NTWindowsGenesis(
void)
2618 mode=GetEnvironmentValue(
"MAGICK_ERRORMODE");
2619 if (mode != (
char *) NULL)
2621 (void) SetErrorMode(StringToInteger(mode));
2622 mode=DestroyString(mode);
2624#if defined(_DEBUG) && !defined(__MINGW32__)
2625 if (IsEventLogging() != MagickFalse)
2630 debug=_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
2632 debug |= _CRTDBG_DELAY_FREE_MEM_DF;
2633 debug |= _CRTDBG_LEAK_CHECK_DF;
2634 (void) _CrtSetDbgFlag(debug);
2641#if defined(MAGICKCORE_INSTALLED_SUPPORT)
2646 path=NTRegistryKeyLookup(
"LibPath");
2647 if (path != (
unsigned char *) NULL)
2653 lib_path[MagickPathExtent];
2655 length=MultiByteToWideChar(CP_UTF8,0,(
char *) path,-1,lib_path,
2658 SetDllDirectoryW(lib_path);
2659 path=(
unsigned char *) RelinquishMagickMemory(path);
2683MagickPrivate
void NTWindowsTerminus(
void)
2685 NTGhostscriptUnLoadDLL();
2687 ActivateSemaphoreInfo(&winsock_semaphore);
2688 LockSemaphoreInfo(winsock_semaphore);
2689 if (wsaData != (WSADATA *) NULL)
2692 wsaData=(WSADATA *) RelinquishMagickMemory((
void *) wsaData);
2694 UnlockSemaphoreInfo(winsock_semaphore);
2695 DestroySemaphoreInfo(&winsock_semaphore);