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,
887char *NTGetLastError(
void)
896 buffer = (LPVOID) NULL;
898 status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
899 FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),
900 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
902 reason=AcquireString(
"An unknown error occurred");
905 reason=AcquireString((
const char *) buffer);
931MagickPrivate
const char *NTGetLibraryError(
void)
934 last_error[MaxTextExtent];
940 error=NTGetLastError();
942 (void) CopyMagickString(last_error,error,MaxTextExtent);
943 error=DestroyString(error);
972void *NTGetLibrarySymbol(
void *handle,
const char *name)
977 proc_address=GetProcAddress((HMODULE) handle,(LPCSTR) name);
978 if (proc_address == (FARPROC) NULL)
979 return((
void *) NULL);
980 return((
void *) proc_address);
1007MagickPrivate MagickBooleanType NTGetModulePath(
const char *module,
char *path)
1010 module_path[MaxTextExtent];
1019 handle=GetModuleHandle(module);
1020 if (handle == (HMODULE) NULL)
1021 return(MagickFalse);
1022 length=GetModuleFileName(handle,module_path,MaxTextExtent);
1024 GetPathComponent(module_path,HeadPath,path);
1049static int NTLocateGhostscript(DWORD flags,
int *root_index,
1050 const char **product_family,
int *major_version,
int *minor_version,
1062 "Artifex Ghostscript",
1071 *product_family=NULL;
1074 for (i=0; i < (ssize_t) (
sizeof(products)/
sizeof(products[0])); i++)
1077 key[MagickPathExtent];
1088 (void) FormatLocaleString(key,MagickPathExtent,
"SOFTWARE\\%s",products[i]);
1089 for (j=0; j < (ssize_t) (
sizeof(registry_roots)/
sizeof(registry_roots[0]));
1092 mode=KEY_READ | flags;
1093 if (RegOpenKeyExA(registry_roots[j].hkey,key,0,mode,&hkey) ==
1105 extent=
sizeof(key)/
sizeof(
char);
1106 for (k=0; RegEnumKeyA(hkey,k,key,extent) == ERROR_SUCCESS; k++)
1116 if (sscanf(key,
"%d.%d.%d",&major,&minor,&patch) != 3)
1117 if (sscanf(key,
"%d.%d",&major,&minor) != 2)
1119 if ((major > *major_version) ||
1120 ((major == *major_version) && (minor > *minor_version)) ||
1121 ((minor == *minor_version) && (patch > *patch_version)))
1124 *product_family=products[i];
1125 *major_version=major;
1126 *minor_version=minor;
1127 *patch_version=patch;
1131 (void) RegCloseKey(hkey);
1135 if (status == MagickFalse)
1141 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
"Ghostscript (%s) "
1142 "version %d.%d.%d",*product_family,*major_version,*minor_version,*patch_version);
1146static MagickBooleanType NTGhostscriptGetString(
const char *name,
1147 BOOL *is_64_bit,
char *value,
const size_t length)
1150 buffer[MagickPathExtent],
1154 *product_family = (
const char *) NULL;
1157 is_64_bit_version = FALSE;
1173 directory=(
char *) NULL;
1174 if (LocaleCompare(name,
"GS_DLL") == 0)
1176 directory=GetEnvironmentValue(
"MAGICK_GHOSTSCRIPT_PATH");
1177 if (directory != (
char *) NULL)
1179 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s%sgsdll64.dll",
1180 directory,DirectorySeparator);
1181 if (IsPathAccessible(buffer) != MagickFalse)
1183 directory=DestroyString(directory);
1184 (void) CopyMagickString(value,buffer,length);
1185 if (is_64_bit != NULL)
1189 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s%sgsdll32.dll",
1190 directory,DirectorySeparator);
1191 if (IsPathAccessible(buffer) != MagickFalse)
1193 directory=DestroyString(directory);
1194 (void) CopyMagickString(value,buffer,length);
1195 if (is_64_bit != NULL)
1199 return(MagickFalse);
1202 if (product_family == (
const char *) NULL)
1205#if defined(KEY_WOW64_32KEY)
1207 flags=KEY_WOW64_64KEY;
1209 flags=KEY_WOW64_32KEY;
1211 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1212 &major_version,&minor_version,&patch_version);
1213 if (product_family == (
const char *) NULL)
1215 flags=KEY_WOW64_32KEY;
1217 is_64_bit_version=TRUE;
1219 flags=KEY_WOW64_64KEY;
1223 if (product_family == (
const char *) NULL)
1225 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1226 &major_version,&minor_version,&patch_version);
1228 is_64_bit_version=TRUE;
1231 if (product_family == (
const char *) NULL)
1232 return(MagickFalse);
1233 if (is_64_bit != NULL)
1234 *is_64_bit=is_64_bit_version;
1235 (void) FormatLocaleString(buffer,MagickPathExtent,
"SOFTWARE\\%s\\%d.%.2d.%d",
1236 product_family,major_version,minor_version,patch_version);
1237 registry_value=NTGetRegistryValue(registry_roots[root_index].hkey,buffer,
1239 if (registry_value == (
unsigned char *) NULL)
1241 (void) FormatLocaleString(buffer,MagickPathExtent,
"SOFTWARE\\%s\\%d.%02d",
1242 product_family,major_version,minor_version);
1243 registry_value=NTGetRegistryValue(registry_roots[root_index].hkey,buffer,
1246 if (registry_value == (
unsigned char *) NULL)
1247 return(MagickFalse);
1248 (void) CopyMagickString(value,(
const char *) registry_value,length);
1249 registry_value=(
unsigned char *) RelinquishMagickMemory(registry_value);
1250 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1251 "registry: \"%s\\%s\\%s\"=\"%s\"",registry_roots[root_index].name,
1256static MagickBooleanType NTGhostscriptDLL(
char *path,
int length)
1259 dll[MagickPathExtent] = {
"" };
1265 if ((*dll ==
'\0') &&
1266 (NTGhostscriptGetString(
"GS_DLL",&is_64_bit,dll,
sizeof(dll)) != MagickTrue))
1267 return(MagickFalse);
1270 return(MagickFalse);
1273 return(MagickFalse);
1275 (void) CopyMagickString(path,dll,length);
1279static inline MagickBooleanType NTGhostscriptHasValidHandle()
1281 if ((nt_ghost_info.delete_instance == NULL) || (ghost_info.exit == NULL) ||
1282 (nt_ghost_info.new_instance == NULL) || (ghost_info.set_stdio == NULL) ||
1283 (ghost_info.init_with_args == NULL) || (ghost_info.revision == NULL))
1284 return(MagickFalse);
1288MagickPrivate
const GhostInfo *NTGhostscriptDLLVectors(
void)
1291 path[MaxTextExtent];
1294 ActivateSemaphoreInfo(&ghost_semaphore);
1295 LockSemaphoreInfo(ghost_semaphore);
1296 if (ghost_handle != (
void *) NULL)
1298 UnlockSemaphoreInfo(ghost_semaphore);
1299 if (NTGhostscriptHasValidHandle() == MagickFalse)
1301 return(&ghost_info);
1303 if (NTGhostscriptDLL(path,
sizeof(path)) == MagickFalse)
1305 UnlockSemaphoreInfo(ghost_semaphore);
1308 ghost_handle=lt_dlopen(path);
1309 if (ghost_handle == (
void *) NULL)
1311 UnlockSemaphoreInfo(ghost_semaphore);
1314 (void) memset((
void *) &nt_ghost_info,0,
sizeof(NTGhostInfo));
1315 nt_ghost_info.delete_instance=(void (MagickDLLCall *)(gs_main_instance *)) (
1316 lt_dlsym(ghost_handle,
"gsapi_delete_instance"));
1317 nt_ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,
1318 void *)) (lt_dlsym(ghost_handle,
"gsapi_new_instance"));
1319 nt_ghost_info.has_instance=MagickFalse;
1320 (void) memset((
void *) &ghost_info,0,
sizeof(
GhostInfo));
1321 ghost_info.delete_instance=NTGhostscriptDeleteInstance;
1322 ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
1323 lt_dlsym(ghost_handle,
"gsapi_exit");
1324 ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
1325 char **)) (lt_dlsym(ghost_handle,
"gsapi_init_with_args"));
1326 ghost_info.new_instance=NTGhostscriptNewInstance;
1327 ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,
const char *,
1328 int,
int *)) (lt_dlsym(ghost_handle,
"gsapi_run_string"));
1329 ghost_info.set_stdio=(int (MagickDLLCall *)(gs_main_instance *,int(
1330 MagickDLLCall *)(
void *,
char *,int),
int(MagickDLLCall *)(
void *,
1331 const char *,int),
int(MagickDLLCall *)(
void *,
const char *,int)))
1332 (lt_dlsym(ghost_handle,
"gsapi_set_stdio"));
1334 lt_dlsym(ghost_handle,
"gsapi_revision"));
1335 UnlockSemaphoreInfo(ghost_semaphore);
1336 if (NTGhostscriptHasValidHandle() == MagickFalse)
1338 return(&ghost_info);
1367MagickPrivate
int NTGhostscriptEXE(
char *path,
int length)
1373 program[MaxTextExtent] = {
"" };
1376 is_64_bit_version = FALSE;
1378 if (*program ==
'\0')
1381 ActivateSemaphoreInfo(&ghost_semaphore);
1382 LockSemaphoreInfo(ghost_semaphore);
1383 if (*program ==
'\0')
1385 if (NTGhostscriptGetString(
"GS_DLL",&is_64_bit_version,program,
1386 sizeof(program)) == MagickFalse)
1388 UnlockSemaphoreInfo(ghost_semaphore);
1390 (void) CopyMagickString(program,
"gswin64c.exe",
sizeof(program));
1392 (void) CopyMagickString(program,
"gswin32c.exe",
sizeof(program));
1394 (void) CopyMagickString(path,program,length);
1397 p=strrchr(program,
'\\');
1398 if (p != (
char *) NULL)
1402 (void) ConcatenateMagickString(program,is_64_bit_version ?
1403 "gswin64c.exe" :
"gswin32c.exe",
sizeof(program));
1406 UnlockSemaphoreInfo(ghost_semaphore);
1408 (void) CopyMagickString(path,program,length);
1437MagickPrivate
int NTGhostscriptFonts(
char *path,
int length)
1440 buffer[MaxTextExtent],
1442 filename[MaxTextExtent];
1449 directory=GetEnvironmentValue(
"MAGICK_GHOSTSCRIPT_FONT_PATH");
1450 if (directory != (
char *) NULL)
1452 (void) CopyMagickString(buffer,directory,MaxTextExtent);
1453 directory=DestroyString(directory);
1457 if (NTGhostscriptGetString(
"GS_LIB",NULL,buffer,MaxTextExtent) == MagickFalse)
1460 for (p=buffer-1; p != (
char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1462 (void) CopyMagickString(path,p+1,length+1);
1463 q=strchr(path,DirectoryListSeparator);
1464 if (q != (
char *) NULL)
1466 (void) FormatLocaleString(filename,MaxTextExtent,
"%s%sfonts.dir",path,
1467 DirectorySeparator);
1468 if (IsPathAccessible(filename) != MagickFalse)
1470 (void) FormatLocaleString(filename,MaxTextExtent,
"%s%sn019003l.pfb",path,
1471 DirectorySeparator);
1472 if (IsPathAccessible(filename) != MagickFalse)
1498MagickPrivate
int NTGhostscriptUnLoadDLL(
void)
1504 ActivateSemaphoreInfo(&ghost_semaphore);
1505 LockSemaphoreInfo(ghost_semaphore);
1507 if (ghost_handle != (
void *) NULL)
1509 status=lt_dlclose(ghost_handle);
1510 ghost_handle=(
void *) NULL;
1511 (void) memset((
void *) &ghost_info,0,
sizeof(
GhostInfo));
1513 UnlockSemaphoreInfo(ghost_semaphore);
1514 DestroySemaphoreInfo(&ghost_semaphore);
1536MagickPrivate
int NTInitializeLibrary(
void)
1559MagickPrivate
void NTInitializeWinsock(MagickBooleanType use_lock)
1564 ActivateSemaphoreInfo(&winsock_semaphore);
1565 LockSemaphoreInfo(winsock_semaphore);
1567 if (wsaData == (WSADATA *) NULL)
1569 wsaData=(WSADATA *) AcquireMagickMemory(
sizeof(WSADATA));
1570 if (WSAStartup(MAKEWORD(2,2),wsaData) != 0)
1571 ThrowFatalException(CacheFatalError,
"WSAStartup failed");
1574 UnlockSemaphoreInfo(winsock_semaphore);
1596MagickExport MagickBooleanType NTLongPathsEnabled()
1598 if (long_paths_enabled == 2)
1611 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1612 status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1613 "SYSTEM\\CurrentControlSet\\Control\\FileSystem",0,KEY_READ,
1615 if (status != ERROR_SUCCESS)
1617 long_paths_enabled=0;
1618 RegCloseKey(registry_key);
1619 return(MagickFalse);
1622 status=RegQueryValueExA(registry_key,
"LongPathsEnabled",0,&type,NULL,
1624 if ((status != ERROR_SUCCESS) || (type != REG_DWORD))
1626 long_paths_enabled=0;
1627 RegCloseKey(registry_key);
1628 return(MagickFalse);
1630 status=RegQueryValueExA(registry_key,
"LongPathsEnabled",0,&type,
1631 (LPBYTE) &value,&size);
1632 RegCloseKey(registry_key);
1633 if (status != ERROR_SUCCESS)
1635 long_paths_enabled=0;
1636 return(MagickFalse);
1638 long_paths_enabled=(size_t) value;
1640 return(long_paths_enabled == 1 ? MagickTrue : MagickFalse);
1662MagickPrivate
void *NTMapMemory(
char *address,
size_t length,
int protection,
1663 int flags,
int file,MagickOffsetType offset)
1682 file_handle=INVALID_HANDLE_VALUE;
1683 low_length=(DWORD) (length & 0xFFFFFFFFUL);
1684 high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1685 map_handle=INVALID_HANDLE_VALUE;
1687 low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1688 high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1690 if (protection & PROT_WRITE)
1692 access_mode=FILE_MAP_WRITE;
1693 if (!(flags & MAP_PRIVATE))
1694 protection_mode=PAGE_READWRITE;
1697 access_mode=FILE_MAP_COPY;
1698 protection_mode=PAGE_WRITECOPY;
1702 if (protection & PROT_READ)
1704 access_mode=FILE_MAP_READ;
1705 protection_mode=PAGE_READONLY;
1707 if ((file == -1) && (flags & MAP_ANONYMOUS))
1708 file_handle=INVALID_HANDLE_VALUE;
1710 file_handle=(HANDLE) _get_osfhandle(file);
1711 map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1715 map=(
void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1717 CloseHandle(map_handle);
1719 if (map == (
void *) NULL)
1720 return((
void *) ((
char *) MAP_FAILED));
1721 return((
void *) ((
char *) map));
1747MagickPrivate
DIR *NTOpenDirectory(
const char *path)
1756 file_specification[MaxTextExtent];
1758 assert(path != (
const char *) NULL);
1759 length=MultiByteToWideChar(CP_UTF8,0,path,-1,file_specification,
1762 return((
DIR *) NULL);
1763 if(wcsncat(file_specification,L
"\\*.*",MaxTextExtent-wcslen(
1764 file_specification)-1) == (
wchar_t*) NULL)
1765 return((
DIR *) NULL);
1766 entry=(
DIR *) AcquireCriticalMemory(
sizeof(
DIR));
1767 entry->firsttime=TRUE;
1768 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1769 if (entry->hSearch == INVALID_HANDLE_VALUE)
1771 entry=(
DIR *) RelinquishMagickMemory(entry);
1772 return((
DIR *) NULL);
1802static inline const char *GetSearchPath(
void)
1804#if defined(MAGICKCORE_LTDL_DELEGATE)
1805 return(lt_dlgetsearchpath());
1807 return(lt_slsearchpath);
1811static UINT ChangeErrorMode(
void)
1814 (CALLBACK *GETERRORMODE)(void);
1825 mode=SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1827 handle=GetModuleHandle(
"kernel32.dll");
1828 if (handle == (HMODULE) NULL)
1829 return SetErrorMode(mode);
1831 getErrorMode=(GETERRORMODE) NTGetLibrarySymbol(handle,
"GetErrorMode");
1832 if (getErrorMode != (GETERRORMODE) NULL)
1833 mode=getErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1835 return SetErrorMode(mode);
1838static inline void *NTLoadLibrary(
const char *filename)
1844 path[MaxTextExtent];
1846 length=MultiByteToWideChar(CP_UTF8,0,filename,-1,path,MaxTextExtent);
1848 return((
void *) NULL);
1849 return (
void *) LoadLibraryExW(path,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1852MagickPrivate
void *NTOpenLibrary(
const char *filename)
1855 path[MaxTextExtent];
1867 mode=ChangeErrorMode();
1868 handle=NTLoadLibrary(filename);
1869 if (handle == (
void *) NULL)
1872 while (p != (
const char*) NULL)
1874 q=strchr(p,DirectoryListSeparator);
1875 if (q != (
const char*) NULL)
1876 (void) CopyMagickString(path,p,q-p+1);
1878 (
void) CopyMagickString(path,p,MaxTextExtent);
1879 (void) ConcatenateMagickString(path,DirectorySeparator,MaxTextExtent);
1880 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
1881 handle=NTLoadLibrary(path);
1882 if (handle != (
void *) NULL || q == (
const char*) NULL)
1915MagickPrivate
struct dirent *NTReadDirectory(
DIR *entry)
1923 if (entry == (
DIR *) NULL)
1924 return((
struct dirent *) NULL);
1925 if (!entry->firsttime)
1927 status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
1929 return((
struct dirent *) NULL);
1931 length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
1932 entry->file_info.d_name,
sizeof(entry->file_info.d_name),NULL,NULL);
1934 return((
struct dirent *) NULL);
1935 entry->firsttime=FALSE;
1936 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1937 return(&entry->file_info);
1972MagickPrivate
unsigned char *NTRegistryKeyLookup(
const char *subkey)
1975 package_key[MaxTextExtent] =
"";
1980 (void) FormatLocaleString(package_key,MagickPathExtent,
1981 "SOFTWARE\\%s\\%s\\Q:%d",MagickPackageName,MagickLibVersionText,
1982 MAGICKCORE_QUANTUM_DEPTH);
1983 value=NTGetRegistryValue(HKEY_LOCAL_MACHINE,package_key,0,subkey);
1984 if (value == (
unsigned char *) NULL)
1985 value=NTGetRegistryValue(HKEY_CURRENT_USER,package_key,0,subkey);
2014MagickPrivate MagickBooleanType NTReportEvent(
const char *event,
2015 const MagickBooleanType error)
2026 handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
2028 return(MagickFalse);
2030 type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
2031 ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
2032 DeregisterEventSource(handle);
2061MagickPrivate
unsigned char *NTResourceToBlob(
const char *
id)
2064#ifndef MAGICKCORE_LIBRARY_NAME
2066 path[MaxTextExtent];
2085 assert(
id != (
const char *) NULL);
2086 if (IsEventLogging() != MagickFalse)
2087 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",id);
2088#ifdef MAGICKCORE_LIBRARY_NAME
2089 handle=GetModuleHandle(MAGICKCORE_LIBRARY_NAME);
2091 (void) FormatLocaleString(path,MaxTextExtent,
"%s%s%s",GetClientPath(),
2092 DirectorySeparator,GetClientName());
2093 if (IsPathAccessible(path) != MagickFalse)
2094 handle=GetModuleHandle(path);
2096 handle=GetModuleHandle(0);
2099 return((
unsigned char *) NULL);
2100 resource=FindResource(handle,
id,
"IMAGEMAGICK");
2102 return((
unsigned char *) NULL);
2103 global=LoadResource(handle,resource);
2105 return((
unsigned char *) NULL);
2106 length=SizeofResource(handle,resource);
2107 value=(
unsigned char *) LockResource(global);
2110 FreeResource(global);
2111 return((
unsigned char *) NULL);
2113 blob=(
unsigned char *) AcquireQuantumMemory(length+MaxTextExtent,
2115 if (blob != (
unsigned char *) NULL)
2117 (void) memcpy(blob,value,length);
2120 UnlockResource(global);
2121 FreeResource(global);
2149MagickPrivate
int NTSetSearchPath(
const char *path)
2151#if defined(MAGICKCORE_LTDL_DELEGATE)
2152 lt_dlsetsearchpath(path);
2154 if (lt_slsearchpath != (
char *) NULL)
2155 lt_slsearchpath=DestroyString(lt_slsearchpath);
2156 if (path != (
char *) NULL)
2157 lt_slsearchpath=AcquireString(path);
2187MagickPrivate
int NTSystemCommand(
const char *command,
char *output)
2189#define CleanupOutputHandles \
2190 if (read_output != (HANDLE) NULL) \
2192 CloseHandle(read_output); \
2193 read_output=(HANDLE) NULL; \
2194 CloseHandle(write_output); \
2195 write_output=(HANDLE) NULL; \
2198#define CopyLastError \
2199 if (output != (char *) NULL) \
2201 error=NTGetLastError(); \
2202 if (error != (char *) NULL) \
2204 CopyMagickString(output,error,MaxTextExtent); \
2205 error=DestroyString(error); \
2211 local_command[MaxTextExtent];
2235 if (command == (
char *) NULL)
2237 read_output=(HANDLE) NULL;
2238 write_output=(HANDLE) NULL;
2239 GetStartupInfo(&startup_info);
2240 startup_info.dwFlags=STARTF_USESHOWWINDOW;
2241 startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
2242 (void) CopyMagickString(local_command,command,MaxTextExtent);
2243 asynchronous=command[strlen(command)-1] ==
'&' ? MagickTrue : MagickFalse;
2244 if (asynchronous != MagickFalse)
2246 local_command[strlen(command)-1]=
'\0';
2247 startup_info.wShowWindow=SW_SHOWDEFAULT;
2251 if (command[strlen(command)-1] ==
'|')
2252 local_command[strlen(command)-1]=
'\0';
2254 startup_info.wShowWindow=SW_HIDE;
2255 read_output=(HANDLE) NULL;
2256 if (output != (
char *) NULL)
2258 if (CreatePipe(&read_output,&write_output,NULL,0))
2260 if (SetHandleInformation(write_output,HANDLE_FLAG_INHERIT,
2261 HANDLE_FLAG_INHERIT))
2263 startup_info.dwFlags|=STARTF_USESTDHANDLES;
2264 startup_info.hStdOutput=write_output;
2265 startup_info.hStdError=write_output;
2268 CleanupOutputHandles;
2271 read_output=(HANDLE) NULL;
2274 status=CreateProcess((LPCTSTR) NULL,local_command,(LPSECURITY_ATTRIBUTES)
2275 NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) TRUE,(DWORD)
2276 NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
2281 CleanupOutputHandles;
2284 if (output != (
char *) NULL)
2286 if (asynchronous != MagickFalse)
2287 return(status == 0);
2289 status=STATUS_TIMEOUT;
2290 while (status == STATUS_TIMEOUT)
2295 status=WaitForSingleObject(process_info.hProcess,1000);
2297 if (read_output != (HANDLE) NULL)
2298 if (!PeekNamedPipe(read_output,NULL,0,NULL,&size,NULL))
2303 buffer[MagickPathExtent];
2308 if (ReadFile(read_output,buffer,MagickPathExtent-1,&bytes_read,NULL))
2313 count=MagickMin(MagickPathExtent-output_offset,
2314 (
size_t) bytes_read+1);
2317 CopyMagickString(output+output_offset,buffer,count);
2318 output_offset+=count-1;
2321 if (!PeekNamedPipe(read_output,NULL,0,NULL,&size,NULL))
2325 if (status != WAIT_OBJECT_0)
2328 CleanupOutputHandles;
2331 status=GetExitCodeProcess(process_info.hProcess,&child_status);
2335 CleanupOutputHandles;
2338 CloseHandle(process_info.hProcess);
2339 CloseHandle(process_info.hThread);
2340 CleanupOutputHandles;
2341 return((
int) child_status);
2367MagickPrivate ssize_t NTSystemConfiguration(
int name)
2376 GetSystemInfo(&system_info);
2377 return(system_info.dwPageSize);
2379 case _SC_PHYS_PAGES:
2387 status.dwLength=
sizeof(status);
2388 if (GlobalMemoryStatusEx(&status) == 0)
2390 GetSystemInfo(&system_info);
2391 return((ssize_t) status.ullTotalPhys/system_info.dwPageSize);
2425MagickPrivate
int NTTruncateFile(
int file,off_t length)
2437 file_handle=(HANDLE) _get_osfhandle(file);
2438 if (file_handle == INVALID_HANDLE_VALUE)
2440 low=(long) (length & 0xffffffffUL);
2441 high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2442 file_pointer=SetFilePointer(file_handle,low,&high,FILE_BEGIN);
2443 if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2445 if (SetEndOfFile(file_handle) == 0)
2474MagickPrivate
int NTUnmapMemory(
void *map,
size_t length)
2477 if (UnmapViewOfFile(map) == 0)
2501MagickPrivate
double NTUserTime(
void)
2531 OsVersionInfo.dwOSVersionInfoSize=
sizeof(OSVERSIONINFO);
2532 GetVersionEx(&OsVersionInfo);
2533 if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
2534 return(NTElapsedTime());
2535 status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
2536 &kernel_time.filetime,&user_time.filetime);
2539 return((
double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
2570MagickPrivate
void NTWarningHandler(
const ExceptionType severity,
2571 const char *reason,
const char *description)
2574 buffer[2*MaxTextExtent];
2577 if (reason == (
char *) NULL)
2579 if (description == (
char *) NULL)
2580 (
void) FormatLocaleString(buffer,MaxTextExtent,
"%s: %s.\n",GetClientName(),
2583 (
void) FormatLocaleString(buffer,MaxTextExtent,
"%s: %s (%s).\n",
2584 GetClientName(),reason,description);
2585 (void) MessageBox(NULL,buffer,
"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2586 MB_SETFOREGROUND | MB_ICONINFORMATION);
2607MagickPrivate
void NTWindowsGenesis(
void)
2612 mode=GetEnvironmentValue(
"MAGICK_ERRORMODE");
2613 if (mode != (
char *) NULL)
2615 (void) SetErrorMode(StringToInteger(mode));
2616 mode=DestroyString(mode);
2618#if defined(_DEBUG) && !defined(__BORLANDC__) && !defined(__MINGW32__)
2619 if (IsEventLogging() != MagickFalse)
2624 debug=_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
2626 debug |= _CRTDBG_DELAY_FREE_MEM_DF;
2627 debug |= _CRTDBG_LEAK_CHECK_DF;
2628 (void) _CrtSetDbgFlag(debug);
2635#if defined(MAGICKCORE_INSTALLED_SUPPORT)
2640 path=NTRegistryKeyLookup(
"LibPath");
2641 if (path != (
unsigned char *) NULL)
2647 lib_path[MagickPathExtent];
2649 length=MultiByteToWideChar(CP_UTF8,0,(
char *) path,-1,lib_path,
2652 SetDllDirectoryW(lib_path);
2653 path=(
unsigned char *) RelinquishMagickMemory(path);
2677MagickPrivate
void NTWindowsTerminus(
void)
2679 NTGhostscriptUnLoadDLL();
2681 ActivateSemaphoreInfo(&winsock_semaphore);
2682 LockSemaphoreInfo(winsock_semaphore);
2683 if (wsaData != (WSADATA *) NULL)
2686 wsaData=(WSADATA *) RelinquishMagickMemory((
void *) wsaData);
2688 UnlockSemaphoreInfo(winsock_semaphore);
2689 DestroySemaphoreInfo(&winsock_semaphore);