41#include "MagickCore/studio.h"
42#if defined(MAGICKCORE_WINDOWS_SUPPORT)
43#include "MagickCore/client.h"
44#include "MagickCore/distribute-cache-private.h"
45#include "MagickCore/exception-private.h"
46#include "MagickCore/image-private.h"
47#include "MagickCore/locale_.h"
48#include "MagickCore/log.h"
49#include "MagickCore/magick.h"
50#include "MagickCore/memory_.h"
51#include "MagickCore/memory-private.h"
52#include "MagickCore/nt-base.h"
53#include "MagickCore/nt-base-private.h"
54#include "MagickCore/resource_.h"
55#include "MagickCore/resource-private.h"
56#include "MagickCore/timer.h"
57#include "MagickCore/string_.h"
58#include "MagickCore/string-private.h"
59#include "MagickCore/utility.h"
60#include "MagickCore/utility-private.h"
61#include "MagickCore/version.h"
62#if defined(MAGICKCORE_LTDL_DELEGATE)
65#if defined(MAGICKCORE_CIPHER_SUPPORT)
73#if !defined(MAP_FAILED)
74#define MAP_FAILED ((void *)(LONG_PTR)-1)
76#define MaxWideByteExtent 100
90typedef struct _NTGhostInfo
93 (MagickDLLCall *delete_instance)(gs_main_instance *);
96 (MagickDLLCall *new_instance)(gs_main_instance **,
void *);
112 *ghost_handle = (
void *) NULL;
125const registry_roots[2] =
127 { HKEY_CURRENT_USER,
"HKEY_CURRENT_USER" },
128 { HKEY_LOCAL_MACHINE,
"HKEY_LOCAL_MACHINE" }
134static void MagickDLLCall NTGhostscriptDeleteInstance(
135 gs_main_instance *instance)
137 LockSemaphoreInfo(ghost_semaphore);
138 nt_ghost_info.delete_instance(instance);
139 nt_ghost_info.has_instance=MagickFalse;
140 UnlockSemaphoreInfo(ghost_semaphore);
143static int MagickDLLCall NTGhostscriptNewInstance(gs_main_instance **pinstance,
149 LockSemaphoreInfo(ghost_semaphore);
151 if (nt_ghost_info.has_instance == MagickFalse)
153 status=nt_ghost_info.new_instance(pinstance,caller_handle);
155 nt_ghost_info.has_instance=MagickTrue;
157 UnlockSemaphoreInfo(ghost_semaphore);
161static inline char *create_utf8_string(
const wchar_t *wide)
169 count=WideCharToMultiByte(CP_UTF8,0,wide,-1,NULL,0,NULL,NULL);
171 return((
char *) NULL);
172 utf8=(
char *) NTAcquireQuantumMemory(count+1,
sizeof(*utf8));
173 if (utf8 == (
char *) NULL)
174 return((
char *) NULL);
175 count=WideCharToMultiByte(CP_UTF8,0,wide,-1,utf8,count,NULL,NULL);
178 utf8=(
char *) RelinquishMagickMemory(utf8);
179 return((
char *) NULL);
185static unsigned char *NTGetRegistryValue(HKEY root,
const char *key,DWORD flags,
202 wide_name[MaxWideByteExtent];
204 value=(
unsigned char *) NULL;
205 status=RegOpenKeyExA(root,key,0,(KEY_READ | flags),®istry_key);
206 if (status != ERROR_SUCCESS)
208 if (MultiByteToWideChar(CP_UTF8,0,name,-1,wide_name,MaxWideByteExtent) == 0)
210 RegCloseKey(registry_key);
213 status=RegQueryValueExW(registry_key,wide_name,0,&type,0,&size);
214 if ((status == ERROR_SUCCESS) && (type == REG_SZ))
219 wide=(LPBYTE) NTAcquireQuantumMemory((
const size_t) size,
sizeof(*wide));
220 if (wide != (LPBYTE) NULL)
222 status=RegQueryValueExW(registry_key,wide_name,0,&type,wide,&size);
223 if ((status == ERROR_SUCCESS) && (type == REG_SZ))
224 value=(
unsigned char *) create_utf8_string((
const wchar_t *) wide);
225 wide=(LPBYTE) RelinquishMagickMemory(wide);
228 RegCloseKey(registry_key);
275#if defined(_DLL) && defined(ProvideDllMain)
276BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
280 case DLL_PROCESS_ATTACH:
291 MagickCoreGenesis((
const char *) NULL,MagickFalse);
292 wide_path=(
wchar_t *) NTAcquireQuantumMemory(MagickPathExtent,
294 if (wide_path == (
wchar_t *) NULL)
296 count=(ssize_t) GetModuleFileNameW(handle,wide_path,MagickPathExtent);
302 module_path=create_utf8_string(wide_path);
303 for ( ; count > 0; count--)
304 if (module_path[count] ==
'\\')
306 module_path[count+1]=
'\0';
309 path=(
char *) NTAcquireQuantumMemory(MagickPathExtent,
311 if (path == (
char *) NULL)
313 module_path=DestroyString(module_path);
314 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
317 count=(ssize_t) GetEnvironmentVariable(
"PATH",path,16*
319 if ((count != 0) && (strstr(path,module_path) == (
char *) NULL))
321 if ((strlen(module_path)+count+1) < (16*MagickPathExtent-1))
326 variable=(
char *) NTAcquireQuantumMemory(MagickPathExtent,
327 16*
sizeof(*variable));
328 if (variable == (
char *) NULL)
330 path=DestroyString(path);
331 module_path=DestroyString(module_path);
332 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
335 (void) FormatLocaleString(variable,16*MagickPathExtent,
336 "%s;%s",module_path,path);
337 SetEnvironmentVariable(
"PATH",variable);
338 variable=DestroyString(variable);
341 path=DestroyString(path);
342 module_path=DestroyString(module_path);
344 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
347 case DLL_PROCESS_DETACH:
349 MagickCoreTerminus();
359#if !defined(__MINGW32__)
384MagickPrivate
int gettimeofday (
struct timeval *time_value,
385 struct timezone *time_zone)
387#define EpochFiletime MagickLLConstant(116444736000000000)
392 if (time_value != (
struct timeval *) NULL)
403 GetSystemTimeAsFileTime(&file_time);
404 date_time.LowPart=file_time.dwLowDateTime;
405 date_time.HighPart=file_time.dwHighDateTime;
406 time=date_time.QuadPart;
409 time_value->tv_sec=(long) (time / 1000000);
410 time_value->tv_usec=(long) (time % 1000000);
412 if (time_zone != (
struct timezone *) NULL)
425 _get_timezone(&timezone);
426 time_zone->tz_minuteswest=timezone/60;
427 _get_daylight(&daylight);
428 time_zone->tz_dsttime=daylight;
459MagickPrivate
char **NTArgvToUTF8(
const int argc,
wchar_t **argv)
467 utf8=(
char **) NTAcquireQuantumMemory(argc,
sizeof(*utf8));
468 if (utf8 == (
char **) NULL)
469 ThrowFatalException(ResourceLimitFatalError,
"UnableToConvertStringToARGV");
470 for (i=0; i < (ssize_t) argc; i++)
472 utf8[i]=create_utf8_string(argv[i]);
473 if (utf8[i] == (
char *) NULL)
475 for (i--; i >= 0; i--)
476 utf8[i]=DestroyString(utf8[i]);
477 ThrowFatalException(ResourceLimitFatalError,
478 "UnableToConvertStringToARGV");
507MagickPrivate
int NTCloseDirectory(DIR *entry)
509 assert(entry != (DIR *) NULL);
510 if (IsEventLogging() != MagickFalse)
511 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
512 FindClose(entry->hSearch);
513 entry=(DIR *) RelinquishMagickMemory(entry);
538MagickPrivate
int NTCloseLibrary(
void *handle)
540 return(FreeLibrary((HINSTANCE) handle) ? 0 : 1);
563MagickPrivate
double NTElapsedTime(
void)
583 if (frequency.QuadPart == 0)
585 if (QueryPerformanceFrequency(&frequency) == 0)
586 frequency.QuadPart=1;
588 if (frequency.QuadPart > 1)
590 QueryPerformanceCounter(&performance_count);
591 return((
double) performance_count.QuadPart/frequency.QuadPart);
593 GetSystemTime(&system_time);
594 SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
595 return((
double) 1.0e-7*elapsed_time.filetime64);
620MagickPrivate
double NTErf(
double x)
644 y=1.0-(((((a5*t+a4)*t)+a3)*t+a2)*t+a1)*t*exp(-x*x);
677MagickPrivate
void NTErrorHandler(
const ExceptionType severity,
678 const char *reason,
const char *description)
681 buffer[3*MagickPathExtent],
685 if (reason == (
char *) NULL)
687 MagickCoreTerminus();
690 message=GetExceptionMessage(errno);
691 if ((description != (
char *) NULL) && errno)
692 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s (%s) [%s].\n",
693 GetClientName(),reason,description,message);
695 if (description != (
char *) NULL)
696 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s (%s).\n",
697 GetClientName(),reason,description);
700 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s [%s].\n",
701 GetClientName(),reason,message);
703 (
void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s.\n",
704 GetClientName(),reason);
705 message=DestroyString(message);
706 (void) MessageBox(NULL,buffer,
"ImageMagick Exception",MB_OK | MB_TASKMODAL |
707 MB_SETFOREGROUND | MB_ICONEXCLAMATION);
708 MagickCoreTerminus();
737MagickPrivate MagickBooleanType NTGatherRandomData(
const size_t length,
738 unsigned char *random)
740#if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER)
747 handle=(HCRYPTPROV) NULL;
748 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
749 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
751 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
752 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
755 status=CryptGenRandom(handle,(DWORD) length,random);
758 status=CryptReleaseContext(handle,0);
761 status=CryptReleaseContext(handle,0);
794extern MagickPrivate
char *NTGetEnvironmentValue(
const char *name)
797 *environment = (
char *) NULL;
806 wide_name[MaxWideByteExtent];
808 if (MultiByteToWideChar(CP_UTF8,0,name,-1,wide_name,MaxWideByteExtent) == 0)
810 size=GetEnvironmentVariableW(wide_name,(LPWSTR) NULL,0);
813 wide=(LPWSTR) NTAcquireQuantumMemory((
const size_t) size,
sizeof(*wide));
814 if (wide == (LPWSTR) NULL)
816 if (GetEnvironmentVariableW(wide_name,wide,size) != 0)
817 environment=create_utf8_string(wide);
818 wide=(LPWSTR) RelinquishMagickMemory(wide);
846MagickPrivate MagickBooleanType NTGetExecutionPath(
char *path,
850 wide_path[MagickPathExtent];
852 (void) GetModuleFileNameW((HMODULE) NULL,wide_path,(DWORD) extent);
853 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,
880static char *NTGetLastErrorMessage(DWORD last_error)
889 buffer = (LPVOID) NULL;
891 status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
892 FORMAT_MESSAGE_FROM_SYSTEM,NULL,last_error,
893 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
895 reason=AcquireString(
"An unknown error occurred");
898 reason=AcquireString((
const char *) buffer);
924MagickPrivate
const char *NTGetLibraryError(
void)
927 last_error[MagickPathExtent];
933 error=NTGetLastErrorMessage(GetLastError());
935 (void) CopyMagickString(last_error,error,MagickPathExtent);
936 error=DestroyString(error);
965void *NTGetLibrarySymbol(
void *handle,
const char *name)
970 proc_address=GetProcAddress((HMODULE) handle,(LPCSTR) name);
971 if (proc_address == (FARPROC) NULL)
972 return((
void *) NULL);
973 return((
void *) proc_address);
1001MagickPrivate MagickBooleanType NTGetModulePath(
const char *module,
char *path)
1004 module_path[MagickPathExtent];
1013 handle=GetModuleHandle(module);
1014 if (handle == (HMODULE) NULL)
1015 return(MagickFalse);
1016 length=GetModuleFileName(handle,module_path,MagickPathExtent);
1018 GetPathComponent(module_path,HeadPath,path);
1043static int NTLocateGhostscript(DWORD flags,
int *root_index,
1044 const char **product_family,
int *major_version,
int *minor_version,
1056 "Artifex Ghostscript",
1065 *product_family=NULL;
1068 for (i=0; i < (ssize_t) (
sizeof(products)/
sizeof(products[0])); i++)
1071 key[MagickPathExtent];
1082 (void) FormatLocaleString(key,MagickPathExtent,
"SOFTWARE\\%s",products[i]);
1083 for (j=0; j < (ssize_t) (
sizeof(registry_roots)/
sizeof(registry_roots[0]));
1086 mode=KEY_READ | flags;
1087 if (RegOpenKeyExA(registry_roots[j].hkey,key,0,mode,&hkey) ==
1099 extent=
sizeof(key)/
sizeof(
char);
1100 for (k=0; RegEnumKeyA(hkey,k,key,extent) == ERROR_SUCCESS; k++)
1110 if (MagickSscanf(key,
"%d.%d.%d",&major,&minor,&patch) != 3)
1111 if (MagickSscanf(key,
"%d.%d",&major,&minor) != 2)
1113 if ((major > *major_version) ||
1114 ((major == *major_version) && (minor > *minor_version)) ||
1115 ((minor == *minor_version) && (patch > *patch_version)))
1118 *product_family=products[i];
1119 *major_version=major;
1120 *minor_version=minor;
1121 *patch_version=patch;
1125 (void) RegCloseKey(hkey);
1129 if (status == MagickFalse)
1135 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
"Ghostscript (%s) "
1136 "version %d.%d.%d",*product_family,*major_version,*minor_version,*patch_version);
1140static MagickBooleanType NTGhostscriptGetString(
const char *name,
1141 BOOL *is_64_bit,
char *value,
const size_t length)
1144 buffer[MagickPathExtent],
1148 *product_family = (
const char *) NULL;
1151 is_64_bit_version = FALSE;
1167 directory=(
char *) NULL;
1168 if (LocaleCompare(name,
"GS_DLL") == 0)
1170 directory=GetEnvironmentValue(
"MAGICK_GHOSTSCRIPT_PATH");
1171 if (directory != (
char *) NULL)
1173 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s%sgsdll64.dll",
1174 directory,DirectorySeparator);
1175 if (IsPathAccessible(buffer) != MagickFalse)
1177 directory=DestroyString(directory);
1178 (void) CopyMagickString(value,buffer,length);
1179 if (is_64_bit != NULL)
1183 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s%sgsdll32.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 return(MagickFalse);
1196 if (product_family == (
const char *) NULL)
1199#if defined(KEY_WOW64_32KEY)
1201 flags=KEY_WOW64_64KEY;
1203 flags=KEY_WOW64_32KEY;
1205 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1206 &major_version,&minor_version,&patch_version);
1207 if (product_family == (
const char *) NULL)
1209 flags=KEY_WOW64_32KEY;
1211 is_64_bit_version=TRUE;
1213 flags=KEY_WOW64_64KEY;
1217 if (product_family == (
const char *) NULL)
1219 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1220 &major_version,&minor_version,&patch_version);
1222 is_64_bit_version=TRUE;
1225 if (product_family == (
const char *) NULL)
1226 return(MagickFalse);
1227 if (is_64_bit != NULL)
1228 *is_64_bit=is_64_bit_version;
1229 (void) FormatLocaleString(buffer,MagickPathExtent,
"SOFTWARE\\%s\\%d.%.2d.%d",
1230 product_family,major_version,minor_version,patch_version);
1231 registry_value=NTGetRegistryValue(registry_roots[root_index].hkey,buffer,
1233 if (registry_value == (
unsigned char *) NULL)
1235 (void) FormatLocaleString(buffer,MagickPathExtent,
"SOFTWARE\\%s\\%d.%02d",
1236 product_family,major_version,minor_version);
1237 registry_value=NTGetRegistryValue(registry_roots[root_index].hkey,buffer,
1240 if (registry_value == (
unsigned char *) NULL)
1241 return(MagickFalse);
1242 (void) CopyMagickString(value,(
const char *) registry_value,length);
1243 registry_value=(
unsigned char *) RelinquishMagickMemory(registry_value);
1244 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1245 "registry: \"%s\\%s\\%s\"=\"%s\"",registry_roots[root_index].name,
1250static MagickBooleanType NTGhostscriptDLL(
char *path,
int length)
1253 dll[MagickPathExtent] = {
"" };
1259 if ((*dll ==
'\0') &&
1260 (NTGhostscriptGetString(
"GS_DLL",&is_64_bit,dll,
sizeof(dll)) != MagickTrue))
1261 return(MagickFalse);
1264 return(MagickFalse);
1267 return(MagickFalse);
1269 (void) CopyMagickString(path,dll,length);
1273static inline MagickBooleanType NTGhostscriptHasValidHandle()
1275 if ((nt_ghost_info.delete_instance == NULL) || (ghost_info.exit == NULL) ||
1276 (nt_ghost_info.new_instance == NULL) || (ghost_info.set_stdio == NULL) ||
1277 (ghost_info.init_with_args == NULL) || (ghost_info.revision == NULL))
1278 return(MagickFalse);
1282MagickPrivate
const GhostInfo *NTGhostscriptDLLVectors(
void)
1285 path[MagickPathExtent];
1288 ActivateSemaphoreInfo(&ghost_semaphore);
1289 LockSemaphoreInfo(ghost_semaphore);
1290 if (ghost_handle != (
void *) NULL)
1292 UnlockSemaphoreInfo(ghost_semaphore);
1293 if (NTGhostscriptHasValidHandle() == MagickTrue)
1294 return(&ghost_info);
1295 return((GhostInfo *) NULL);
1297 if (NTGhostscriptDLL(path,
sizeof(path)) != MagickTrue)
1299 UnlockSemaphoreInfo(ghost_semaphore);
1300 return((GhostInfo *) NULL);
1302 ghost_handle=lt_dlopen(path);
1303 if (ghost_handle == (
void *) NULL)
1305 UnlockSemaphoreInfo(ghost_semaphore);
1306 return((GhostInfo *) NULL);
1308 (void) memset((
void *) &nt_ghost_info,0,
sizeof(NTGhostInfo));
1309 nt_ghost_info.delete_instance=(void (MagickDLLCall *)(gs_main_instance *)) (
1310 lt_dlsym(ghost_handle,
"gsapi_delete_instance"));
1311 nt_ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,
1312 void *)) (lt_dlsym(ghost_handle,
"gsapi_new_instance"));
1313 nt_ghost_info.has_instance=MagickFalse;
1314 (void) memset((
void *) &ghost_info,0,
sizeof(GhostInfo));
1315 ghost_info.delete_instance=NTGhostscriptDeleteInstance;
1316 ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
1317 lt_dlsym(ghost_handle,
"gsapi_exit");
1318 ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
1319 char **)) (lt_dlsym(ghost_handle,
"gsapi_init_with_args"));
1320 ghost_info.new_instance=NTGhostscriptNewInstance;
1321 ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,
const char *,
1322 int,
int *)) (lt_dlsym(ghost_handle,
"gsapi_run_string"));
1323 ghost_info.set_arg_encoding=(int (MagickDLLCall*)(gs_main_instance*, int)) (
1324 lt_dlsym(ghost_handle,
"gsapi_set_arg_encoding"));
1325 ghost_info.set_stdio=(int (MagickDLLCall *)(gs_main_instance *,int(
1326 MagickDLLCall *)(
void *,
char *,int),
int(MagickDLLCall *)(
void *,
1327 const char *,int),
int(MagickDLLCall *)(
void *,
const char *,int)))
1328 (lt_dlsym(ghost_handle,
"gsapi_set_stdio"));
1329 ghost_info.revision=(int (MagickDLLCall *)(gsapi_revision_t *,int)) (
1330 lt_dlsym(ghost_handle,
"gsapi_revision"));
1331 UnlockSemaphoreInfo(ghost_semaphore);
1332 if (NTGhostscriptHasValidHandle() == MagickTrue)
1333 return(&ghost_info);
1334 return((GhostInfo *) NULL);
1363MagickPrivate
void NTGhostscriptEXE(
char *path,
int length)
1369 program[MagickPathExtent] = {
"" };
1372 is_64_bit_version = FALSE;
1374 if (*program ==
'\0')
1377 ActivateSemaphoreInfo(&ghost_semaphore);
1378 LockSemaphoreInfo(ghost_semaphore);
1379 if (*program ==
'\0')
1381 if (NTGhostscriptGetString(
"GS_DLL",&is_64_bit_version,program,
1382 sizeof(program)) == MagickFalse)
1384 UnlockSemaphoreInfo(ghost_semaphore);
1386 (void) CopyMagickString(program,
"gswin64c.exe",
sizeof(program));
1388 (void) CopyMagickString(program,
"gswin32c.exe",
sizeof(program));
1390 (void) CopyMagickString(path,program,length);
1393 p=strrchr(program,
'\\');
1394 if (p != (
char *) NULL)
1398 (void) ConcatenateMagickString(program,is_64_bit_version ?
1399 "gswin64c.exe" :
"gswin32c.exe",
sizeof(program));
1402 UnlockSemaphoreInfo(ghost_semaphore);
1404 (void) CopyMagickString(path,program,length);
1432MagickPrivate MagickBooleanType NTGhostscriptFonts(
char *path,
int length)
1435 buffer[MagickPathExtent],
1437 filename[MagickPathExtent];
1444 directory=GetEnvironmentValue(
"MAGICK_GHOSTSCRIPT_FONT_PATH");
1445 if (directory != (
char *) NULL)
1447 (void) CopyMagickString(buffer,directory,MagickPathExtent);
1448 directory=DestroyString(directory);
1452 if (NTGhostscriptGetString(
"GS_LIB",NULL,buffer,
1453 MagickPathExtent) == MagickFalse)
1454 return(MagickFalse);
1456 for (p=buffer-1; p != (
char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1458 (void) CopyMagickString(path,p+1,length+1);
1459 q=strchr(path,DirectoryListSeparator);
1460 if (q != (
char *) NULL)
1462 (void) FormatLocaleString(filename,MagickPathExtent,
"%s%sfonts.dir",path,
1463 DirectorySeparator);
1464 if (IsPathAccessible(filename) != MagickFalse)
1466 (void) FormatLocaleString(filename,MagickPathExtent,
"%s%sn019003l.pfb",path,
1467 DirectorySeparator);
1468 if (IsPathAccessible(filename) != MagickFalse)
1472 return(MagickFalse);
1494MagickPrivate
void NTGhostscriptUnLoadDLL(
void)
1497 ActivateSemaphoreInfo(&ghost_semaphore);
1498 LockSemaphoreInfo(ghost_semaphore);
1499 if (ghost_handle != (
void *) NULL)
1501 (void) lt_dlclose(ghost_handle);
1502 ghost_handle=(
void *) NULL;
1503 (void) memset((
void *) &ghost_info,0,
sizeof(GhostInfo));
1505 UnlockSemaphoreInfo(ghost_semaphore);
1506 RelinquishSemaphoreInfo(&ghost_semaphore);
1528MagickExport MagickBooleanType NTLongPathsEnabled()
1531 long_paths_enabled = 2;
1533 if (long_paths_enabled == 2)
1546 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1547 status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1548 "SYSTEM\\CurrentControlSet\\Control\\FileSystem",0,KEY_READ,
1550 if (status != ERROR_SUCCESS)
1552 RegCloseKey(registry_key);
1553 long_paths_enabled=0;
1554 return(MagickFalse);
1557 status=RegQueryValueExA(registry_key,
"LongPathsEnabled",0,&type,NULL,
1559 if ((status != ERROR_SUCCESS) || (type != REG_DWORD))
1561 RegCloseKey(registry_key);
1562 long_paths_enabled=0;
1563 return(MagickFalse);
1566 status=RegQueryValueExA(registry_key,
"LongPathsEnabled",0,&type,
1567 (LPBYTE) &value,&size);
1568 RegCloseKey(registry_key);
1569 if (status != ERROR_SUCCESS)
1571 long_paths_enabled=0;
1572 return(MagickFalse);
1574 long_paths_enabled=(size_t) value;
1576 return(long_paths_enabled == 1 ? MagickTrue : MagickFalse);
1598MagickPrivate
void *NTMapMemory(
char *address,
size_t length,
int protection,
1599 int flags,
int file,MagickOffsetType offset)
1618 file_handle=INVALID_HANDLE_VALUE;
1619 low_length=(DWORD) (length & 0xFFFFFFFFUL);
1620 high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1621 map_handle=INVALID_HANDLE_VALUE;
1623 low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1624 high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1626 if (protection & PROT_WRITE)
1628 access_mode=FILE_MAP_WRITE;
1629 if (!(flags & MAP_PRIVATE))
1630 protection_mode=PAGE_READWRITE;
1633 access_mode=FILE_MAP_COPY;
1634 protection_mode=PAGE_WRITECOPY;
1638 if (protection & PROT_READ)
1640 access_mode=FILE_MAP_READ;
1641 protection_mode=PAGE_READONLY;
1643 if ((file == -1) && (flags & MAP_ANONYMOUS))
1644 file_handle=INVALID_HANDLE_VALUE;
1646 file_handle=(HANDLE) _get_osfhandle(file);
1647 map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1651 map=(
void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1653 CloseHandle(map_handle);
1655 if (map == (
void *) NULL)
1656 return((
void *) ((
char *) MAP_FAILED));
1657 return((
void *) ((
char *) map));
1683MagickPrivate DIR *NTOpenDirectory(
const char *path)
1692 file_specification[MagickPathExtent];
1694 assert(path != (
const char *) NULL);
1695 length=MultiByteToWideChar(CP_UTF8,0,path,-1,file_specification,
1698 return((DIR *) NULL);
1699 if (wcsncat_s(file_specification,MagickPathExtent,L
"\\*.*",MagickPathExtent-
1700 wcslen(file_specification)-1) != 0)
1701 return((DIR *) NULL);
1702 entry=(DIR *) AcquireCriticalMemory(
sizeof(DIR));
1703 entry->firsttime=TRUE;
1704 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1705 if (entry->hSearch == INVALID_HANDLE_VALUE)
1707 entry=(DIR *) RelinquishMagickMemory(entry);
1708 return((DIR *) NULL);
1738static UINT ChangeErrorMode(
void)
1741 (CALLBACK *GETERRORMODE)(void);
1752 mode=SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1754 handle=GetModuleHandle(
"kernel32.dll");
1755 if (handle == (HMODULE) NULL)
1756 return SetErrorMode(mode);
1758 getErrorMode=(GETERRORMODE) NTGetLibrarySymbol(handle,
"GetErrorMode");
1759 if (getErrorMode != (GETERRORMODE) NULL)
1760 mode=getErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1762 return SetErrorMode(mode);
1765static inline void *NTLoadLibrary(
const char *filename)
1773 library=(
void *) NULL;
1774 path=create_wchar_path(filename);
1775 if (path != (
wchar_t *) NULL)
1777 library=LoadLibraryExW(path,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1778 path=(
wchar_t *) RelinquishMagickMemory(path);
1783MagickPrivate
void *NTOpenLibrary(
const char *filename)
1791 mode=ChangeErrorMode();
1792 handle=NTLoadLibrary(filename);
1793#if defined(MAGICKCORE_LTDL_DELEGATE)
1794 if (handle == (
void *) NULL)
1797 path[MagickPathExtent];
1803 p=lt_dlgetsearchpath();
1804 while (p != (
const char*) NULL)
1806 q=strchr(p,DirectoryListSeparator);
1807 if (q != (
const char*) NULL)
1808 (void) CopyMagickString(path,p,q-p+1);
1810 (
void) CopyMagickString(path,p,MagickPathExtent);
1811 (void) ConcatenateMagickString(path,DirectorySeparator,MagickPathExtent);
1812 (void) ConcatenateMagickString(path,filename,MagickPathExtent);
1813 handle=NTLoadLibrary(path);
1814 if (handle != (
void *) NULL || q == (
const char*) NULL)
1848MagickPrivate
struct dirent *NTReadDirectory(DIR *entry)
1856 if (entry == (DIR *) NULL)
1857 return((
struct dirent *) NULL);
1858 if (!entry->firsttime)
1860 status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
1862 return((
struct dirent *) NULL);
1864 length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
1865 entry->file_info.d_name,
sizeof(entry->file_info.d_name),NULL,NULL);
1867 return((
struct dirent *) NULL);
1868 entry->firsttime=FALSE;
1869 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1870 return(&entry->file_info);
1905MagickPrivate
unsigned char *NTRegistryKeyLookup(
const char *subkey)
1908 package_key[MagickPathExtent] =
"";
1913 (void) FormatLocaleString(package_key,MagickPathExtent,
1914 "SOFTWARE\\%s\\%s\\Q:%d",MagickPackageName,MagickLibVersionText,
1915 MAGICKCORE_QUANTUM_DEPTH);
1916 value=NTGetRegistryValue(HKEY_LOCAL_MACHINE,package_key,0,subkey);
1917 if (value == (
unsigned char *) NULL)
1918 value=NTGetRegistryValue(HKEY_CURRENT_USER,package_key,0,subkey);
1947MagickPrivate MagickBooleanType NTReportEvent(
const char *event,
1948 const MagickBooleanType error)
1959 handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
1961 return(MagickFalse);
1963 type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
1964 ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
1965 DeregisterEventSource(handle);
1994MagickPrivate
unsigned char *NTResourceToBlob(
const char *
id)
1997#ifndef MAGICKCORE_LIBRARY_NAME
1999 path[MagickPathExtent];
2018 assert(
id != (
const char *) NULL);
2019 if (IsEventLogging() != MagickFalse)
2020 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
id);
2021#ifdef MAGICKCORE_LIBRARY_NAME
2022 handle=GetModuleHandle(MAGICKCORE_LIBRARY_NAME);
2024 (void) FormatLocaleString(path,MagickPathExtent,
"%s%s%s",GetClientPath(),
2025 DirectorySeparator,GetClientName());
2026 if (IsPathAccessible(path) != MagickFalse)
2027 handle=GetModuleHandle(path);
2029 handle=GetModuleHandle(0);
2032 return((
unsigned char *) NULL);
2033 resource=FindResource(handle,
id,
"IMAGEMAGICK");
2035 return((
unsigned char *) NULL);
2036 global=LoadResource(handle,resource);
2038 return((
unsigned char *) NULL);
2039 length=SizeofResource(handle,resource);
2040 value=(
unsigned char *) LockResource(global);
2043 FreeResource(global);
2044 return((
unsigned char *) NULL);
2046 blob=(
unsigned char *) AcquireQuantumMemory(length+MagickPathExtent,
2048 if (blob != (
unsigned char *) NULL)
2050 (void) memcpy(blob,value,length);
2053 UnlockResource(global);
2054 FreeResource(global);
2083MagickPrivate
int NTSystemCommand(
const char *command,
char *output)
2085#define CleanupOutputHandles \
2086 if (read_output != (HANDLE) NULL) \
2088 CloseHandle(read_output); \
2089 read_output=(HANDLE) NULL; \
2090 CloseHandle(write_output); \
2091 write_output=(HANDLE) NULL; \
2094#define CopyLastError \
2095 last_error=GetLastError(); \
2096 if (output != (char *) NULL) \
2098 error=NTGetLastErrorMessage(last_error); \
2099 if (error != (char *) NULL) \
2101 (void) CopyMagickString(output,error,MagickPathExtent); \
2102 error=DestroyString(error); \
2108 local_command[MagickPathExtent];
2133 if (command == (
char *) NULL)
2135 read_output=(HANDLE) NULL;
2136 write_output=(HANDLE) NULL;
2137 GetStartupInfo(&startup_info);
2138 startup_info.dwFlags=STARTF_USESHOWWINDOW;
2139 startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
2140 (void) CopyMagickString(local_command,command,MagickPathExtent);
2141 asynchronous=command[strlen(command)-1] ==
'&' ? MagickTrue : MagickFalse;
2142 if (asynchronous != MagickFalse)
2144 local_command[strlen(command)-1]=
'\0';
2145 startup_info.wShowWindow=SW_SHOWDEFAULT;
2149 if (command[strlen(command)-1] ==
'|')
2150 local_command[strlen(command)-1]=
'\0';
2152 startup_info.wShowWindow=SW_HIDE;
2153 read_output=(HANDLE) NULL;
2154 if (output != (
char *) NULL)
2156 if (CreatePipe(&read_output,&write_output,NULL,0))
2158 if (SetHandleInformation(write_output,HANDLE_FLAG_INHERIT,
2159 HANDLE_FLAG_INHERIT))
2161 startup_info.dwFlags|=STARTF_USESTDHANDLES;
2162 startup_info.hStdOutput=write_output;
2163 startup_info.hStdError=write_output;
2166 CleanupOutputHandles;
2169 read_output=(HANDLE) NULL;
2172 status=CreateProcess((LPCTSTR) NULL,local_command,(LPSECURITY_ATTRIBUTES)
2173 NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) TRUE,(DWORD)
2174 NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
2179 CleanupOutputHandles;
2180 return(last_error == ERROR_FILE_NOT_FOUND ? 127 : -1);
2182 if (output != (
char *) NULL)
2184 if (asynchronous != MagickFalse)
2185 return(status == 0);
2187 status=STATUS_TIMEOUT;
2188 while (status == STATUS_TIMEOUT)
2193 status=WaitForSingleObject(process_info.hProcess,1000);
2195 if (read_output != (HANDLE) NULL)
2196 if (!PeekNamedPipe(read_output,NULL,0,NULL,&size,NULL))
2201 buffer[MagickPathExtent];
2206 if (ReadFile(read_output,buffer,MagickPathExtent-1,&bytes_read,NULL))
2211 count=MagickMin(MagickPathExtent-output_offset,
2212 (
size_t) bytes_read+1);
2215 (void) CopyMagickString(output+output_offset,buffer,count);
2216 output_offset+=count-1;
2219 if (!PeekNamedPipe(read_output,NULL,0,NULL,&size,NULL))
2223 if (status != WAIT_OBJECT_0)
2226 CleanupOutputHandles;
2229 status=GetExitCodeProcess(process_info.hProcess,&child_status);
2233 CleanupOutputHandles;
2236 CloseHandle(process_info.hProcess);
2237 CloseHandle(process_info.hThread);
2238 CleanupOutputHandles;
2239 return((
int) child_status);
2265MagickPrivate ssize_t NTSystemConfiguration(
int name)
2274 GetSystemInfo(&system_info);
2275 return(system_info.dwPageSize);
2277 case _SC_PHYS_PAGES:
2285 status.dwLength=
sizeof(status);
2286 if (GlobalMemoryStatusEx(&status) == 0)
2288 GetSystemInfo(&system_info);
2289 return((ssize_t) status.ullTotalPhys/system_info.dwPageSize);
2323MagickPrivate
int NTTruncateFile(
int file,off_t length)
2335 file_handle=(HANDLE) _get_osfhandle(file);
2336 if (file_handle == INVALID_HANDLE_VALUE)
2338 low=(long) (length & 0xffffffffUL);
2339 high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2340 file_pointer=SetFilePointer(file_handle,low,&high,FILE_BEGIN);
2341 if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2343 if (SetEndOfFile(file_handle) == 0)
2372MagickPrivate
int NTUnmapMemory(
void *map,
size_t length)
2375 if (UnmapViewOfFile(map) == 0)
2408MagickPrivate
void NTWarningHandler(
const ExceptionType severity,
2409 const char *reason,
const char *description)
2412 buffer[2*MagickPathExtent];
2415 if (reason == (
char *) NULL)
2417 if (description == (
char *) NULL)
2418 (void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s.\n",GetClientName(),
2421 (
void) FormatLocaleString(buffer,MagickPathExtent,
"%s: %s (%s).\n",
2422 GetClientName(),reason,description);
2423 (void) MessageBox(NULL,buffer,
"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2424 MB_SETFOREGROUND | MB_ICONINFORMATION);
2446MagickPrivate
void NTWindowsGenesis(
void)
2451 mode=GetEnvironmentValue(
"MAGICK_ERRORMODE");
2452 if (mode != (
char *) NULL)
2454 (void) SetErrorMode(StringToInteger(mode));
2455 mode=DestroyString(mode);
2457#if defined(_DEBUG) && !defined(__MINGW32__)
2458 if (IsEventLogging() != MagickFalse)
2463 debug=_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
2465 debug |= _CRTDBG_DELAY_FREE_MEM_DF;
2466 debug |= _CRTDBG_LEAK_CHECK_DF;
2467 (void) _CrtSetDbgFlag(debug);
2474#if defined(MAGICKCORE_INSTALLED_SUPPORT)
2479 path=NTRegistryKeyLookup(
"LibPath");
2480 if (path != (
unsigned char *) NULL)
2485 lib_path=create_wchar_path((
const char *) path);
2486 if (lib_path != (
wchar_t *) NULL)
2488 SetDllDirectoryW(lib_path);
2489 lib_path=(
wchar_t *) RelinquishMagickMemory(lib_path);
2491 path=(
unsigned char *) RelinquishMagickMemory(path);
2516MagickPrivate
void NTWindowsTerminus(
void)
2518 NTGhostscriptUnLoadDLL();
2519 DistributeCacheTerminus();