41 #include "MagickCore/studio.h"
42 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
43 #include "MagickCore/client.h"
44 #include "MagickCore/exception-private.h"
45 #include "MagickCore/image-private.h"
46 #include "MagickCore/locale_.h"
47 #include "MagickCore/log.h"
48 #include "MagickCore/magick.h"
49 #include "MagickCore/memory_.h"
50 #include "MagickCore/memory-private.h"
51 #include "MagickCore/nt-base.h"
52 #include "MagickCore/nt-base-private.h"
53 #include "MagickCore/resource_.h"
54 #include "MagickCore/resource-private.h"
55 #include "MagickCore/timer.h"
56 #include "MagickCore/string_.h"
57 #include "MagickCore/string-private.h"
58 #include "MagickCore/utility.h"
59 #include "MagickCore/utility-private.h"
60 #include "MagickCore/version.h"
61 #if defined(MAGICKCORE_LTDL_DELEGATE)
64 #if defined(MAGICKCORE_CIPHER_SUPPORT)
72 #if !defined(MAP_FAILED)
73 #define MAP_FAILED ((void *)(LONG_PTR)-1)
88 typedef 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;
135 const registry_roots[2] =
137 { HKEY_CURRENT_USER,
"HKEY_CURRENT_USER" },
138 { HKEY_LOCAL_MACHINE,
"HKEY_LOCAL_MACHINE" }
144 #if !defined(MAGICKCORE_WINDOWS_SUPPORT)
145 extern "C" BOOL WINAPI
146 DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved);
149 static 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);
158 static 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);
176 static 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);
200 static unsigned char *NTGetRegistryValue(HKEY root,
const char *key,DWORD flags,
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,100) == 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)
291 BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
295 case DLL_PROCESS_ATTACH:
306 MagickCoreGenesis((
const char *) NULL,MagickFalse);
307 wide_path=(
wchar_t *) NTAcquireQuantumMemory(MagickPathExtent,
309 if (wide_path == (
wchar_t *) NULL)
311 count=(ssize_t) GetModuleFileNameW(handle,wide_path,MagickPathExtent);
317 module_path=create_utf8_string(wide_path);
318 for ( ; count > 0; count--)
319 if (module_path[count] ==
'\\')
321 module_path[count+1]=
'\0';
324 path=(
char *) NTAcquireQuantumMemory(MagickPathExtent,
326 if (path == (
char *) NULL)
328 module_path=DestroyString(module_path);
329 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
332 count=(ssize_t) GetEnvironmentVariable(
"PATH",path,16*
334 if ((count != 0) && (strstr(path,module_path) == (
char *) NULL))
336 if ((strlen(module_path)+count+1) < (16*MagickPathExtent-1))
341 variable=(
char *) NTAcquireQuantumMemory(MagickPathExtent,
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*MagickPathExtent,
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__)
399 MagickPrivate
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;
466 MagickPrivate
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");
514 MagickPrivate
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);
545 MagickPrivate
int NTCloseLibrary(
void *handle)
547 return(!(FreeLibrary((HINSTANCE) handle)));
570 static BOOL ControlHandler(DWORD type)
573 AsynchronousResourceComponentTerminus();
577 MagickPrivate
int NTControlHandler(
void)
579 return(SetConsoleCtrlHandler((PHANDLER_ROUTINE) ControlHandler,TRUE));
601 MagickPrivate
double NTElapsedTime(
void)
621 if (frequency.QuadPart == 0)
623 if (QueryPerformanceFrequency(&frequency) == 0)
624 frequency.QuadPart=1;
626 if (frequency.QuadPart > 1)
628 QueryPerformanceCounter(&performance_count);
629 return((
double) performance_count.QuadPart/frequency.QuadPart);
631 GetSystemTime(&system_time);
632 SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
633 return((
double) 1.0e-7*elapsed_time.filetime64);
658 MagickPrivate
double NTErf(
double x)
684 y=1.0-(((((a5*t+a4)*t)+a3)*t+a2)*t+a1)*t*exp(-x*x);
717 MagickPrivate
void NTErrorHandler(
const ExceptionType severity,
718 const char *reason,
const char *description)
721 buffer[3*MagickPathExtent],
725 if (reason == (
char *) NULL)
727 MagickCoreTerminus();
730 message=GetExceptionMessage(errno);
731 if ((description != (
char *) NULL) && errno)
732 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s (%s) [%s].\n",
733 GetClientName(),reason,description,message);
735 if (description != (
char *) NULL)
736 (
void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s (%s).\n",
737 GetClientName(),reason,description);
740 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s [%s].\n",
741 GetClientName(),reason,message);
743 (
void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s.\n",
744 GetClientName(),reason);
745 message=DestroyString(message);
746 (void) MessageBox(NULL,buffer,
"ImageMagick Exception",MB_OK | MB_TASKMODAL |
747 MB_SETFOREGROUND | MB_ICONEXCLAMATION);
748 MagickCoreTerminus();
770 MagickPrivate
int NTExitLibrary(
void)
800 MagickPrivate MagickBooleanType NTGatherRandomData(
const size_t length,
801 unsigned char *random)
803 #if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER)
810 handle=(HCRYPTPROV) NULL;
811 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
812 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
814 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
815 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
818 status=CryptGenRandom(handle,(DWORD) length,random);
821 status=CryptReleaseContext(handle,0);
824 status=CryptReleaseContext(handle,0);
858 MagickPrivate MagickBooleanType NTGetExecutionPath(
char *path,
862 wide_path[MagickPathExtent];
864 (void) GetModuleFileNameW((HMODULE) NULL,wide_path,(DWORD) extent);
865 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,
888 char *NTGetLastError(
void)
899 status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
900 FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),
901 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
903 reason=AcquireString(
"An unknown error occurred");
906 reason=AcquireString((
const char *) buffer);
932 MagickPrivate
const char *NTGetLibraryError(
void)
935 last_error[MagickPathExtent];
941 error=NTGetLastError();
943 (void) CopyMagickString(last_error,error,MagickPathExtent);
944 error=DestroyString(error);
973 void *NTGetLibrarySymbol(
void *handle,
const char *name)
978 proc_address=GetProcAddress((HMODULE) handle,(LPCSTR) name);
979 if (proc_address == (FARPROC) NULL)
980 return((
void *) NULL);
981 return((
void *) proc_address);
1009 MagickPrivate MagickBooleanType NTGetModulePath(
const char *module,
char *path)
1012 module_path[MagickPathExtent];
1021 handle=GetModuleHandle(module);
1022 if (handle == (HMODULE) NULL)
1023 return(MagickFalse);
1024 length=GetModuleFileName(handle,module_path,MagickPathExtent);
1026 GetPathComponent(module_path,HeadPath,path);
1051 static int NTLocateGhostscript(DWORD flags,
int *root_index,
1052 const char **product_family,
int *major_version,
int *minor_version,
1067 "Aladdin 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);
1150 static 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);
1231 #if !defined(_WIN64)
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.%d.%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,
1260 static 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);
1283 static 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);
1292 MagickPrivate
const GhostInfo *NTGhostscriptDLLVectors(
void)
1295 path[MagickPathExtent];
1298 ActivateSemaphoreInfo(&ghost_semaphore);
1299 LockSemaphoreInfo(ghost_semaphore);
1300 if (ghost_handle != (
void *) NULL)
1302 UnlockSemaphoreInfo(ghost_semaphore);
1303 if (NTGhostscriptHasValidHandle() == MagickTrue)
1304 return(&ghost_info);
1307 if (NTGhostscriptDLL(path,
sizeof(path)) != MagickTrue)
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_arg_encoding=(int (MagickDLLCall*)(gs_main_instance*, int)) (
1334 lt_dlsym(ghost_handle,
"gsapi_set_arg_encoding"));
1335 ghost_info.set_stdio=(int (MagickDLLCall *)(gs_main_instance *,int(
1336 MagickDLLCall *)(
void *,
char *,int),
int(MagickDLLCall *)(
void *,
1337 const char *,int),
int(MagickDLLCall *)(
void *,
const char *,int)))
1338 (lt_dlsym(ghost_handle,
"gsapi_set_stdio"));
1340 lt_dlsym(ghost_handle,
"gsapi_revision"));
1341 UnlockSemaphoreInfo(ghost_semaphore);
1342 if (NTGhostscriptHasValidHandle() == MagickTrue)
1343 return(&ghost_info);
1373 MagickPrivate
void NTGhostscriptEXE(
char *path,
int length)
1379 program[MagickPathExtent] = {
"" };
1382 is_64_bit_version = FALSE;
1384 if (*program ==
'\0')
1387 ActivateSemaphoreInfo(&ghost_semaphore);
1388 LockSemaphoreInfo(ghost_semaphore);
1389 if (*program ==
'\0')
1391 if (NTGhostscriptGetString(
"GS_DLL",&is_64_bit_version,program,
1392 sizeof(program)) == MagickFalse)
1394 UnlockSemaphoreInfo(ghost_semaphore);
1396 (void) CopyMagickString(program,
"gswin64c.exe",
sizeof(program));
1398 (void) CopyMagickString(program,
"gswin32c.exe",
sizeof(program));
1400 (void) CopyMagickString(path,program,length);
1403 p=strrchr(program,
'\\');
1404 if (p != (
char *) NULL)
1408 (void) ConcatenateMagickString(program,is_64_bit_version ?
1409 "gswin64c.exe" :
"gswin32c.exe",
sizeof(program));
1412 UnlockSemaphoreInfo(ghost_semaphore);
1414 (void) CopyMagickString(path,program,length);
1442 MagickPrivate MagickBooleanType NTGhostscriptFonts(
char *path,
int length)
1445 buffer[MagickPathExtent],
1447 filename[MagickPathExtent];
1454 directory=GetEnvironmentValue(
"MAGICK_GHOSTSCRIPT_FONT_PATH");
1455 if (directory != (
char *) NULL)
1457 (void) CopyMagickString(buffer,directory,MagickPathExtent);
1458 directory=DestroyString(directory);
1462 if (NTGhostscriptGetString(
"GS_LIB",NULL,buffer,
1463 MagickPathExtent) == MagickFalse)
1464 return(MagickFalse);
1466 for (p=buffer-1; p != (
char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1468 (void) CopyMagickString(path,p+1,length+1);
1469 q=strchr(path,DirectoryListSeparator);
1470 if (q != (
char *) NULL)
1472 (void) FormatLocaleString(filename,MagickPathExtent,
"%s%sfonts.dir",path,
1473 DirectorySeparator);
1474 if (IsPathAccessible(filename) != MagickFalse)
1476 (void) FormatLocaleString(filename,MagickPathExtent,
"%s%sn019003l.pfb",path,
1477 DirectorySeparator);
1478 if (IsPathAccessible(filename) != MagickFalse)
1482 return(MagickFalse);
1504 MagickPrivate
void NTGhostscriptUnLoadDLL(
void)
1507 ActivateSemaphoreInfo(&ghost_semaphore);
1508 LockSemaphoreInfo(ghost_semaphore);
1509 if (ghost_handle != (
void *) NULL)
1511 (void) lt_dlclose(ghost_handle);
1512 ghost_handle=(
void *) NULL;
1513 (void) memset((
void *) &ghost_info,0,
sizeof(
GhostInfo));
1515 UnlockSemaphoreInfo(ghost_semaphore);
1516 RelinquishSemaphoreInfo(&ghost_semaphore);
1537 MagickPrivate
int NTInitializeLibrary(
void)
1560 MagickPrivate
void NTInitializeWinsock(MagickBooleanType use_lock)
1565 ActivateSemaphoreInfo(&winsock_semaphore);
1566 LockSemaphoreInfo(winsock_semaphore);
1568 if (wsaData == (WSADATA *) NULL)
1570 wsaData=(WSADATA *) AcquireMagickMemory(
sizeof(WSADATA));
1571 if (WSAStartup(MAKEWORD(2,2),wsaData) != 0)
1572 ThrowFatalException(CacheFatalError,
"WSAStartup failed");
1575 UnlockSemaphoreInfo(winsock_semaphore);
1597 MagickExport MagickBooleanType NTLongPathsEnabled()
1599 if (long_paths_enabled == 2)
1612 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1613 status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1614 "SYSTEM\\CurrentControlSet\\Control\\FileSystem",0,KEY_READ,
1616 if (status != ERROR_SUCCESS)
1618 RegCloseKey(registry_key);
1619 long_paths_enabled=0;
1620 return(MagickFalse);
1623 status=RegQueryValueExA(registry_key,
"LongPathsEnabled",0,&type,NULL,
1625 if ((status != ERROR_SUCCESS) || (type != REG_DWORD))
1627 RegCloseKey(registry_key);
1628 long_paths_enabled=0;
1629 return(MagickFalse);
1632 status=RegQueryValueExA(registry_key,
"LongPathsEnabled",0,&type,
1633 (LPBYTE) &value,&size);
1634 RegCloseKey(registry_key);
1635 if (status != ERROR_SUCCESS)
1637 long_paths_enabled=0;
1638 return(MagickFalse);
1640 long_paths_enabled=(size_t) value;
1642 return(long_paths_enabled == 1 ? MagickTrue : MagickFalse);
1664 MagickPrivate
void *NTMapMemory(
char *address,
size_t length,
int protection,
1665 int flags,
int file,MagickOffsetType offset)
1684 file_handle=INVALID_HANDLE_VALUE;
1685 low_length=(DWORD) (length & 0xFFFFFFFFUL);
1686 high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1687 map_handle=INVALID_HANDLE_VALUE;
1689 low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1690 high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1692 if (protection & PROT_WRITE)
1694 access_mode=FILE_MAP_WRITE;
1695 if (!(flags & MAP_PRIVATE))
1696 protection_mode=PAGE_READWRITE;
1699 access_mode=FILE_MAP_COPY;
1700 protection_mode=PAGE_WRITECOPY;
1704 if (protection & PROT_READ)
1706 access_mode=FILE_MAP_READ;
1707 protection_mode=PAGE_READONLY;
1709 if ((file == -1) && (flags & MAP_ANONYMOUS))
1710 file_handle=INVALID_HANDLE_VALUE;
1712 file_handle=(HANDLE) _get_osfhandle(file);
1713 map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1717 map=(
void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1719 CloseHandle(map_handle);
1721 if (map == (
void *) NULL)
1722 return((
void *) ((
char *) MAP_FAILED));
1723 return((
void *) ((
char *) map));
1749 MagickPrivate
DIR *NTOpenDirectory(
const char *path)
1758 file_specification[MagickPathExtent];
1760 assert(path != (
const char *) NULL);
1761 length=MultiByteToWideChar(CP_UTF8,0,path,-1,file_specification,
1764 return((
DIR *) NULL);
1765 if(wcsncat(file_specification,L
"\\*.*",MagickPathExtent-wcslen(
1766 file_specification)-1) == (
wchar_t *) NULL)
1767 return((
DIR *) NULL);
1768 entry=(
DIR *) AcquireCriticalMemory(
sizeof(
DIR));
1769 entry->firsttime=TRUE;
1770 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1771 if (entry->hSearch == INVALID_HANDLE_VALUE)
1773 entry=(
DIR *) RelinquishMagickMemory(entry);
1774 return((
DIR *) NULL);
1804 static inline const char *GetSearchPath(
void)
1806 #if defined(MAGICKCORE_LTDL_DELEGATE)
1807 return(lt_dlgetsearchpath());
1809 return(lt_slsearchpath);
1813 static UINT ChangeErrorMode(
void)
1816 (CALLBACK *GETERRORMODE)(void);
1827 mode=SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1829 handle=GetModuleHandle(
"kernel32.dll");
1830 if (handle == (HMODULE) NULL)
1831 return SetErrorMode(mode);
1833 getErrorMode=(GETERRORMODE) NTGetLibrarySymbol(handle,
"GetErrorMode");
1834 if (getErrorMode != (GETERRORMODE) NULL)
1835 mode=getErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1837 return SetErrorMode(mode);
1840 static inline void *NTLoadLibrary(
const char *filename)
1846 path[MagickPathExtent];
1848 length=MultiByteToWideChar(CP_UTF8,0,filename,-1,path,MagickPathExtent);
1850 return((
void *) NULL);
1851 return (
void *) LoadLibraryExW(path,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1854 MagickPrivate
void *NTOpenLibrary(
const char *filename)
1857 path[MagickPathExtent];
1869 mode=ChangeErrorMode();
1870 handle=NTLoadLibrary(filename);
1871 if (handle == (
void *) NULL)
1874 while (p != (
const char*) NULL)
1876 q=strchr(p,DirectoryListSeparator);
1877 if (q != (
const char*) NULL)
1878 (void) CopyMagickString(path,p,q-p+1);
1880 (
void) CopyMagickString(path,p,MagickPathExtent);
1881 (void) ConcatenateMagickString(path,DirectorySeparator,MagickPathExtent);
1882 (void) ConcatenateMagickString(path,filename,MagickPathExtent);
1883 handle=NTLoadLibrary(path);
1884 if (handle != (
void *) NULL || q == (
const char*) NULL)
1917 MagickPrivate
struct dirent *NTReadDirectory(
DIR *entry)
1925 if (entry == (
DIR *) NULL)
1926 return((
struct dirent *) NULL);
1927 if (!entry->firsttime)
1929 status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
1931 return((
struct dirent *) NULL);
1933 length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
1934 entry->file_info.d_name,
sizeof(entry->file_info.d_name),NULL,NULL);
1936 return((
struct dirent *) NULL);
1937 entry->firsttime=FALSE;
1938 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1939 return(&entry->file_info);
1974 MagickPrivate
unsigned char *NTRegistryKeyLookup(
const char *subkey)
1977 package_key[MagickPathExtent];
1982 (void) FormatLocaleString(package_key,MagickPathExtent,
1983 "SOFTWARE\\%s\\%s\\Q:%d",MagickPackageName,MagickLibVersionText,
1984 MAGICKCORE_QUANTUM_DEPTH);
1985 value=NTGetRegistryValue(HKEY_LOCAL_MACHINE,package_key,0,subkey);
1986 if (value == (
unsigned char *) NULL)
1987 value=NTGetRegistryValue(HKEY_CURRENT_USER,package_key,0,subkey);
2016 MagickPrivate MagickBooleanType NTReportEvent(
const char *event,
2017 const MagickBooleanType error)
2028 handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
2030 return(MagickFalse);
2032 type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
2033 ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
2034 DeregisterEventSource(handle);
2063 MagickPrivate
unsigned char *NTResourceToBlob(
const char *
id)
2066 #ifndef MAGICKCORE_LIBRARY_NAME
2068 path[MagickPathExtent];
2087 assert(
id != (
const char *) NULL);
2088 if (IsEventLogging() != MagickFalse)
2089 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",id);
2090 #ifdef MAGICKCORE_LIBRARY_NAME
2091 handle=GetModuleHandle(MAGICKCORE_LIBRARY_NAME);
2093 (void) FormatLocaleString(path,MagickPathExtent,
"%s%s%s",GetClientPath(),
2094 DirectorySeparator,GetClientName());
2095 if (IsPathAccessible(path) != MagickFalse)
2096 handle=GetModuleHandle(path);
2098 handle=GetModuleHandle(0);
2101 return((
unsigned char *) NULL);
2102 resource=FindResource(handle,
id,
"IMAGEMAGICK");
2104 return((
unsigned char *) NULL);
2105 global=LoadResource(handle,resource);
2107 return((
unsigned char *) NULL);
2108 length=SizeofResource(handle,resource);
2109 value=(
unsigned char *) LockResource(global);
2112 FreeResource(global);
2113 return((
unsigned char *) NULL);
2115 blob=(
unsigned char *) AcquireQuantumMemory(length+MagickPathExtent,
2117 if (blob != (
unsigned char *) NULL)
2119 (void) memcpy(blob,value,length);
2122 UnlockResource(global);
2123 FreeResource(global);
2151 MagickPrivate
int NTSetSearchPath(
const char *path)
2153 #if defined(MAGICKCORE_LTDL_DELEGATE)
2154 lt_dlsetsearchpath(path);
2156 if (lt_slsearchpath != (
char *) NULL)
2157 lt_slsearchpath=DestroyString(lt_slsearchpath);
2158 if (path != (
char *) NULL)
2159 lt_slsearchpath=AcquireString(path);
2189 MagickPrivate
int NTSystemCommand(
const char *command,
char *output)
2191 #define CleanupOutputHandles \
2192 if (read_output != (HANDLE) NULL) \
2194 CloseHandle(read_output); \
2195 read_output=(HANDLE) NULL; \
2196 CloseHandle(write_output); \
2197 write_output=(HANDLE) NULL; \
2200 #define CopyLastError \
2201 if (output != (char *) NULL) \
2203 error=NTGetLastError(); \
2204 if (error != (char *) NULL) \
2206 CopyMagickString(output,error,MagickPathExtent); \
2207 error=DestroyString(error); \
2213 local_command[MagickPathExtent];
2237 if (command == (
char *) NULL)
2239 read_output=(HANDLE) NULL;
2240 write_output=(HANDLE) NULL;
2241 GetStartupInfo(&startup_info);
2242 startup_info.dwFlags=STARTF_USESHOWWINDOW;
2243 startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
2244 (void) CopyMagickString(local_command,command,MagickPathExtent);
2245 asynchronous=command[strlen(command)-1] ==
'&' ? MagickTrue : MagickFalse;
2246 if (asynchronous != MagickFalse)
2248 local_command[strlen(command)-1]=
'\0';
2249 startup_info.wShowWindow=SW_SHOWDEFAULT;
2253 if (command[strlen(command)-1] ==
'|')
2254 local_command[strlen(command)-1]=
'\0';
2256 startup_info.wShowWindow=SW_HIDE;
2257 read_output=(HANDLE) NULL;
2258 if (output != (
char *) NULL)
2260 if (CreatePipe(&read_output,&write_output,NULL,0))
2262 if (SetHandleInformation(write_output,HANDLE_FLAG_INHERIT,
2263 HANDLE_FLAG_INHERIT))
2265 startup_info.dwFlags|=STARTF_USESTDHANDLES;
2266 startup_info.hStdOutput=write_output;
2267 startup_info.hStdError=write_output;
2270 CleanupOutputHandles;
2273 read_output=(HANDLE) NULL;
2276 status=CreateProcess((LPCTSTR) NULL,local_command,(LPSECURITY_ATTRIBUTES)
2277 NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) TRUE,(DWORD)
2278 NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
2283 CleanupOutputHandles;
2286 if (output != (
char *) NULL)
2288 if (asynchronous != MagickFalse)
2289 return(status == 0);
2291 status=STATUS_TIMEOUT;
2292 while (status == STATUS_TIMEOUT)
2297 status=WaitForSingleObject(process_info.hProcess,1000);
2299 if (read_output != (HANDLE) NULL)
2300 if (!PeekNamedPipe(read_output,NULL,0,NULL,&size,NULL))
2305 buffer[MagickPathExtent];
2310 if (ReadFile(read_output,buffer,MagickPathExtent-1,&bytes_read,NULL))
2315 count=MagickMin(MagickPathExtent-output_offset,
2316 (
size_t) bytes_read+1);
2319 CopyMagickString(output+output_offset,buffer,count);
2320 output_offset+=count-1;
2323 if (!PeekNamedPipe(read_output,NULL,0,NULL,&size,NULL))
2327 if (status != WAIT_OBJECT_0)
2330 CleanupOutputHandles;
2333 status=GetExitCodeProcess(process_info.hProcess,&child_status);
2337 CleanupOutputHandles;
2340 CloseHandle(process_info.hProcess);
2341 CloseHandle(process_info.hThread);
2342 CleanupOutputHandles;
2343 return((
int) child_status);
2369 MagickPrivate ssize_t NTSystemConfiguration(
int name)
2378 GetSystemInfo(&system_info);
2379 return(system_info.dwPageSize);
2381 case _SC_PHYS_PAGES:
2389 status.dwLength=
sizeof(status);
2390 if (GlobalMemoryStatusEx(&status) == 0)
2392 GetSystemInfo(&system_info);
2393 return((ssize_t) status.ullTotalPhys/system_info.dwPageSize);
2427 MagickPrivate
int NTTruncateFile(
int file,off_t length)
2439 file_handle=(HANDLE) _get_osfhandle(file);
2440 if (file_handle == INVALID_HANDLE_VALUE)
2442 low=(long) (length & 0xffffffffUL);
2443 high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2444 file_pointer=SetFilePointer(file_handle,low,&high,FILE_BEGIN);
2445 if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2447 if (SetEndOfFile(file_handle) == 0)
2476 MagickPrivate
int NTUnmapMemory(
void *map,
size_t length)
2479 if (UnmapViewOfFile(map) == 0)
2503 MagickPrivate
double NTUserTime(
void)
2533 OsVersionInfo.dwOSVersionInfoSize=
sizeof(OSVERSIONINFO);
2534 GetVersionEx(&OsVersionInfo);
2535 if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
2536 return(NTElapsedTime());
2537 status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
2538 &kernel_time.filetime,&user_time.filetime);
2541 return((
double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
2572 MagickPrivate
void NTWarningHandler(
const ExceptionType severity,
2573 const char *reason,
const char *description)
2576 buffer[2*MagickPathExtent];
2579 if (reason == (
char *) NULL)
2581 if (description == (
char *) NULL)
2582 (
void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s.\n",GetClientName(),
2585 (
void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s (%s).\n",
2586 GetClientName(),reason,description);
2587 (void) MessageBox(NULL,buffer,
"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2588 MB_SETFOREGROUND | MB_ICONINFORMATION);
2610 static LONG WINAPI NTUncaughtException(EXCEPTION_POINTERS *info)
2612 magick_unreferenced(info);
2613 AsynchronousResourceComponentTerminus();
2614 return(EXCEPTION_CONTINUE_SEARCH);
2617 MagickPrivate
void NTWindowsGenesis(
void)
2622 SetUnhandledExceptionFilter(NTUncaughtException);
2623 mode=GetEnvironmentValue(
"MAGICK_ERRORMODE");
2624 if (mode != (
char *) NULL)
2626 (void) SetErrorMode(StringToInteger(mode));
2627 mode=DestroyString(mode);
2629 #if defined(_DEBUG) && !defined(__BORLANDC__) && !defined(__MINGW32__)
2630 if (IsEventLogging() != MagickFalse)
2635 debug=_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
2637 debug |= _CRTDBG_DELAY_FREE_MEM_DF;
2638 debug |= _CRTDBG_LEAK_CHECK_DF;
2639 (void) _CrtSetDbgFlag(debug);
2646 #if defined(MAGICKCORE_INSTALLED_SUPPORT)
2651 path=NTRegistryKeyLookup(
"LibPath");
2652 if (path != (
unsigned char *) NULL)
2658 lib_path[MagickPathExtent];
2660 length=MultiByteToWideChar(CP_UTF8,0,(
char *) path,-1,lib_path,
2663 SetDllDirectoryW(lib_path);
2664 path=(
unsigned char *) RelinquishMagickMemory(path);
2688 MagickPrivate
void NTWindowsTerminus(
void)
2690 NTGhostscriptUnLoadDLL();
2692 ActivateSemaphoreInfo(&winsock_semaphore);
2693 LockSemaphoreInfo(winsock_semaphore);
2694 if (wsaData != (WSADATA *) NULL)
2697 wsaData=(WSADATA *) RelinquishMagickMemory((
void *) wsaData);
2699 UnlockSemaphoreInfo(winsock_semaphore);
2700 RelinquishSemaphoreInfo(&winsock_semaphore);