MagickCore 7.1.2
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
nt-base.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% N N TTTTT %
7% NN N T %
8% N N N T %
9% N NN T %
10% N N T %
11% %
12% %
13% Windows NT Utility Methods for MagickCore %
14% %
15% Software Design %
16% Cristy %
17% December 1996 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/license/ %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38/*
39 Include declarations.
40*/
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)
63# include "ltdl.h"
64#endif
65#if defined(MAGICKCORE_CIPHER_SUPPORT)
66#include <ntsecapi.h>
67#include <wincrypt.h>
68#endif
69
70/*
71 Define declarations.
72*/
73#if !defined(MAP_FAILED)
74#define MAP_FAILED ((void *)(LONG_PTR)-1)
75#endif
76#define MaxWideByteExtent 100
77
78/*
79 Typedef declarations.
80*/
81
82/*
83 We need to make sure only one instance is created for each process and that
84 is why we wrap the new/delete instance methods.
85
86 From: http://www.ghostscript.com/doc/current/API.htm
87 "The Win32 DLL gsdll32.dll can be used by multiple programs simultaneously,
88 but only once within each process"
89*/
90typedef struct _NTGhostInfo
91{
92 void
93 (MagickDLLCall *delete_instance)(gs_main_instance *);
94
95 int
96 (MagickDLLCall *new_instance)(gs_main_instance **,void *);
97
98 MagickBooleanType
99 has_instance;
100} NTGhostInfo;
101
102/*
103 Static declarations.
104*/
105static NTGhostInfo
106 nt_ghost_info;
107
108static GhostInfo
109 ghost_info;
110
111static void
112 *ghost_handle = (void *) NULL;
113
114static SemaphoreInfo
115 *ghost_semaphore = (SemaphoreInfo *) NULL;
116
117struct
118{
119 const HKEY
120 hkey;
121
122 const char
123 *name;
124}
125const registry_roots[2] =
126{
127 { HKEY_CURRENT_USER, "HKEY_CURRENT_USER" },
128 { HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" }
129};
130
131/*
132 External declarations.
133*/
134static void MagickDLLCall NTGhostscriptDeleteInstance(
135 gs_main_instance *instance)
136{
137 LockSemaphoreInfo(ghost_semaphore);
138 nt_ghost_info.delete_instance(instance);
139 nt_ghost_info.has_instance=MagickFalse;
140 UnlockSemaphoreInfo(ghost_semaphore);
141}
142
143static int MagickDLLCall NTGhostscriptNewInstance(gs_main_instance **pinstance,
144 void *caller_handle)
145{
146 int
147 status;
148
149 LockSemaphoreInfo(ghost_semaphore);
150 status=-1;
151 if (nt_ghost_info.has_instance == MagickFalse)
152 {
153 status=nt_ghost_info.new_instance(pinstance,caller_handle);
154 if (status >= 0)
155 nt_ghost_info.has_instance=MagickTrue;
156 }
157 UnlockSemaphoreInfo(ghost_semaphore);
158 return(status);
159}
160
161static inline char *create_utf8_string(const wchar_t *wide)
162{
163 char
164 *utf8;
165
166 int
167 count;
168
169 count=WideCharToMultiByte(CP_UTF8,0,wide,-1,NULL,0,NULL,NULL);
170 if (count == 0)
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);
176 if (count == 0)
177 {
178 utf8=(char *) RelinquishMagickMemory(utf8);
179 return((char *) NULL);
180 }
181 utf8[count]=0;
182 return(utf8);
183}
184
185static unsigned char *NTGetRegistryValue(HKEY root,const char *key,DWORD flags,
186 const char *name)
187{
188 unsigned char
189 *value;
190
191 HKEY
192 registry_key;
193
194 DWORD
195 size,
196 type;
197
198 LSTATUS
199 status;
200
201 wchar_t
202 wide_name[MaxWideByteExtent];
203
204 value=(unsigned char *) NULL;
205 status=RegOpenKeyExA(root,key,0,(KEY_READ | flags),&registry_key);
206 if (status != ERROR_SUCCESS)
207 return(value);
208 if (MultiByteToWideChar(CP_UTF8,0,name,-1,wide_name,MaxWideByteExtent) == 0)
209 {
210 RegCloseKey(registry_key);
211 return(value);
212 }
213 status=RegQueryValueExW(registry_key,wide_name,0,&type,0,&size);
214 if ((status == ERROR_SUCCESS) && (type == REG_SZ))
215 {
216 LPBYTE
217 wide;
218
219 wide=(LPBYTE) NTAcquireQuantumMemory((const size_t) size,sizeof(*wide));
220 if (wide != (LPBYTE) NULL)
221 {
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);
226 }
227 }
228 RegCloseKey(registry_key);
229 return(value);
230}
231
232/*
233%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234% %
235% %
236% %
237% D l l M a i n %
238% %
239% %
240% %
241%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242%
243% DllMain() is an entry point to the DLL which is called when processes and
244% threads are initialized and terminated, or upon calls to the Windows
245% LoadLibrary and FreeLibrary functions.
246%
247% The function returns TRUE of it succeeds, or FALSE if initialization fails.
248%
249% The format of the DllMain method is:
250%
251% BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
252%
253% A description of each parameter follows:
254%
255% o handle: handle to the DLL module
256%
257% o reason: reason for calling function:
258%
259% DLL_PROCESS_ATTACH - DLL is being loaded into virtual address
260% space of current process.
261% DLL_THREAD_ATTACH - Indicates that the current process is
262% creating a new thread. Called under the
263% context of the new thread.
264% DLL_THREAD_DETACH - Indicates that the thread is exiting.
265% Called under the context of the exiting
266% thread.
267% DLL_PROCESS_DETACH - Indicates that the DLL is being unloaded
268% from the virtual address space of the
269% current process.
270%
271% o lpvReserved: Used for passing additional info during DLL_PROCESS_ATTACH
272% and DLL_PROCESS_DETACH.
273%
274*/
275#if defined(_DLL) && defined(ProvideDllMain)
276BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
277{
278 switch (reason)
279 {
280 case DLL_PROCESS_ATTACH:
281 {
282 char
283 *module_path;
284
285 ssize_t
286 count;
287
288 wchar_t
289 *wide_path;
290
291 MagickCoreGenesis((const char *) NULL,MagickFalse);
292 wide_path=(wchar_t *) NTAcquireQuantumMemory(MagickPathExtent,
293 sizeof(*wide_path));
294 if (wide_path == (wchar_t *) NULL)
295 return(FALSE);
296 count=(ssize_t) GetModuleFileNameW(handle,wide_path,MagickPathExtent);
297 if (count != 0)
298 {
299 char
300 *path;
301
302 module_path=create_utf8_string(wide_path);
303 for ( ; count > 0; count--)
304 if (module_path[count] == '\\')
305 {
306 module_path[count+1]='\0';
307 break;
308 }
309 path=(char *) NTAcquireQuantumMemory(MagickPathExtent,
310 16*sizeof(*path));
311 if (path == (char *) NULL)
312 {
313 module_path=DestroyString(module_path);
314 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
315 return(FALSE);
316 }
317 count=(ssize_t) GetEnvironmentVariable("PATH",path,16*
318 MagickPathExtent);
319 if ((count != 0) && (strstr(path,module_path) == (char *) NULL))
320 {
321 if ((strlen(module_path)+count+1) < (16*MagickPathExtent-1))
322 {
323 char
324 *variable;
325
326 variable=(char *) NTAcquireQuantumMemory(MagickPathExtent,
327 16*sizeof(*variable));
328 if (variable == (char *) NULL)
329 {
330 path=DestroyString(path);
331 module_path=DestroyString(module_path);
332 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
333 return(FALSE);
334 }
335 (void) FormatLocaleString(variable,16*MagickPathExtent,
336 "%s;%s",module_path,path);
337 SetEnvironmentVariable("PATH",variable);
338 variable=DestroyString(variable);
339 }
340 }
341 path=DestroyString(path);
342 module_path=DestroyString(module_path);
343 }
344 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
345 break;
346 }
347 case DLL_PROCESS_DETACH:
348 {
349 MagickCoreTerminus();
350 break;
351 }
352 default:
353 break;
354 }
355 return(TRUE);
356}
357#endif
358
359#if !defined(__MINGW32__)
360/*
361%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362% %
363% %
364% %
365% g e t t i m e o f d a y %
366% %
367% %
368% %
369%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370%
371% The gettimeofday() method get the time of day.
372%
373% The format of the gettimeofday method is:
374%
375% int gettimeofday(struct timeval *time_value,struct timezone *time_zone)
376%
377% A description of each parameter follows:
378%
379% o time_value: the time value.
380%
381% o time_zone: the time zone.
382%
383*/
384MagickPrivate int gettimeofday (struct timeval *time_value,
385 struct timezone *time_zone)
386{
387#define EpochFiletime MagickLLConstant(116444736000000000)
388
389 static int
390 is_tz_set;
391
392 if (time_value != (struct timeval *) NULL)
393 {
394 FILETIME
395 file_time;
396
397 __int64
398 time;
399
400 LARGE_INTEGER
401 date_time;
402
403 GetSystemTimeAsFileTime(&file_time);
404 date_time.LowPart=file_time.dwLowDateTime;
405 date_time.HighPart=file_time.dwHighDateTime;
406 time=date_time.QuadPart;
407 time-=EpochFiletime;
408 time/=10;
409 time_value->tv_sec=(long) (time / 1000000);
410 time_value->tv_usec=(long) (time % 1000000);
411 }
412 if (time_zone != (struct timezone *) NULL)
413 {
414 int
415 daylight;
416
417 long
418 timezone=0;
419
420 if (is_tz_set == 0)
421 {
422 _tzset();
423 is_tz_set++;
424 }
425 _get_timezone(&timezone);
426 time_zone->tz_minuteswest=timezone/60;
427 _get_daylight(&daylight);
428 time_zone->tz_dsttime=daylight;
429 }
430 return(0);
431}
432#endif
433
434/*
435%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436% %
437% %
438% %
439% N T A c c e s s W i d e %
440% %
441% %
442% %
443%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444%
445% NTAccessWide() checks the file accessibility of a file path.
446#
447# The format of the NTAccessWide method is:
448%
449% int NTAccessWide(const char *path, int mode)
450%
451% A description of each parameter follows:
452%
453% o path: the file path.
454%
455% o mode: the accessibility mode.
456%
457*/
458MagickExport int NTAccessWide(const char *path, int mode)
459{
460 int
461 status;
462
463 wchar_t
464 *path_wide;
465
466 path_wide=NTCreateWidePath(path);
467 if (path_wide == (wchar_t *) NULL)
468 return(-1);
469 status=_waccess(path_wide,mode);
470 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
471 return(status);
472}
473
474/*
475%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
476% %
477% %
478% %
479% N T A r g v T o U T F 8 %
480% %
481% %
482% %
483%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484%
485% NTArgvToUTF8() converts the wide command line arguments to UTF-8 to ensure
486% compatibility with Linux.
487%
488% The format of the NTArgvToUTF8 method is:
489%
490% char **NTArgvToUTF8(const int argc,wchar_t **argv)
491%
492% A description of each parameter follows:
493%
494% o argc: the number of command line arguments.
495%
496% o argv: the wide-character command line arguments.
497%
498*/
499MagickPrivate char **NTArgvToUTF8(const int argc,wchar_t **argv)
500{
501 char
502 **utf8;
503
504 ssize_t
505 i;
506
507 utf8=(char **) NTAcquireQuantumMemory(argc,sizeof(*utf8));
508 if (utf8 == (char **) NULL)
509 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
510 for (i=0; i < (ssize_t) argc; i++)
511 {
512 utf8[i]=create_utf8_string(argv[i]);
513 if (utf8[i] == (char *) NULL)
514 {
515 for (i--; i >= 0; i--)
516 utf8[i]=DestroyString(utf8[i]);
517 ThrowFatalException(ResourceLimitFatalError,
518 "UnableToConvertStringToARGV");
519 }
520 }
521 return(utf8);
522}
523
524/*
525%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
526% %
527% %
528% %
529% N T C l o s e D i r e c t o r y %
530% %
531% %
532% %
533%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
534%
535% NTCloseDirectory() closes the named directory stream and frees the DIR
536% structure.
537%
538% The format of the NTCloseDirectory method is:
539%
540% int NTCloseDirectory(DIR *entry)
541%
542% A description of each parameter follows:
543%
544% o entry: Specifies a pointer to a DIR structure.
545%
546*/
547MagickPrivate int NTCloseDirectory(DIR *entry)
548{
549 assert(entry != (DIR *) NULL);
550 if (IsEventLogging() != MagickFalse)
551 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
552 FindClose(entry->hSearch);
553 entry=(DIR *) RelinquishMagickMemory(entry);
554 return(0);
555}
556/*
557%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558% %
559% %
560% %
561% N T C l o s e L i b r a r y %
562% %
563% %
564% %
565%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566%
567% NTCloseLibrary() unloads the module associated with the passed handle.
568%
569% The format of the NTCloseLibrary method is:
570%
571% void NTCloseLibrary(void *handle)
572%
573% A description of each parameter follows:
574%
575% o handle: Specifies a handle to a previously loaded dynamic module.
576%
577*/
578MagickPrivate int NTCloseLibrary(void *handle)
579{
580 return(FreeLibrary((HINSTANCE) handle) ? 0 : 1);
581}
582
583/*
584%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
585% %
586% %
587% %
588% N T C r e a t e W i d e P a t h %
589% %
590% %
591% %
592%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
593%
594% NTCreateWidePath() returns the wide-character version of the specified
595% UTF-8 path.
596%
597% The format of the NTCreateWidePath method is:
598%
599% void NTCreateWidePath(void *handle)
600%
601% A description of each parameter follows:
602%
603% o utf8: Specifies a handle to a previously loaded dynamic module.
604%
605*/
606MagickExport wchar_t* NTCreateWidePath(const char *utf8)
607{
608 int
609 count;
610
611 wchar_t
612 *wide;
613
614 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
615 if ((count > MAX_PATH) && (strncmp(utf8,"\\\\?\\",4) != 0) &&
616 (NTLongPathsEnabled() == MagickFalse))
617 {
618 char
619 buffer[MagickPathExtent];
620
621 wchar_t
622 shortPath[MAX_PATH],
623 *longPath;
624
625 size_t
626 length;
627
628 (void) FormatLocaleString(buffer,MagickPathExtent,"\\\\?\\%s",utf8);
629 count+=4;
630 longPath=(wchar_t *) NTAcquireQuantumMemory((size_t) count,
631 sizeof(*longPath));
632 if (longPath == (wchar_t *) NULL)
633 return((wchar_t *) NULL);
634 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
635 if (count != 0)
636 count=(int) GetShortPathNameW(longPath,shortPath,MAX_PATH);
637 longPath=(wchar_t *) RelinquishMagickMemory(longPath);
638 if ((count < 5) || (count >= MAX_PATH))
639 return((wchar_t *) NULL);
640 length=(size_t) count-3;
641 wide=(wchar_t *) NTAcquireQuantumMemory(length,sizeof(*wide));
642 wcscpy_s(wide,length,shortPath+4);
643 return(wide);
644 }
645 wide=(wchar_t *) NTAcquireQuantumMemory((size_t) count,sizeof(*wide));
646 if ((wide != (wchar_t *) NULL) &&
647 (MultiByteToWideChar(CP_UTF8,0,utf8,-1,wide,count) == 0))
648 wide=(wchar_t *) RelinquishMagickMemory(wide);
649 return(wide);
650}
651
652/*
653%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
654% %
655% %
656% %
657% N T E l a p s e d T i m e %
658% %
659% %
660% %
661%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
662%
663% NTElapsedTime() returns the elapsed time (in seconds) since the last call to
664% StartTimer().
665%
666% The format of the ElapsedTime method is:
667%
668% double NTElapsedTime(void)
669%
670*/
671MagickPrivate double NTElapsedTime(void)
672{
673 union
674 {
675 FILETIME
676 filetime;
677
678 __int64
679 filetime64;
680 } elapsed_time;
681
682 LARGE_INTEGER
683 performance_count;
684
685 static LARGE_INTEGER
686 frequency = { 0 };
687
688 SYSTEMTIME
689 system_time;
690
691 if (frequency.QuadPart == 0)
692 {
693 if (QueryPerformanceFrequency(&frequency) == 0)
694 frequency.QuadPart=1;
695 }
696 if (frequency.QuadPart > 1)
697 {
698 QueryPerformanceCounter(&performance_count);
699 return((double) performance_count.QuadPart/frequency.QuadPart);
700 }
701 GetSystemTime(&system_time);
702 SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
703 return((double) 1.0e-7*elapsed_time.filetime64);
704}
705
706/*
707%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
708% %
709% %
710% %
711% N T E r f %
712% %
713% %
714% %
715%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
716%
717% NTErf() computes the error function of x.
718%
719% The format of the NTErf method is:
720%
721% double NTErf(double x)
722%
723% A description of each parameter follows:
724%
725% o x: The value to compute the error function for.
726%
727*/
728MagickPrivate double NTErf(double x)
729{
730 double
731 a1,
732 a2,
733 a3,
734 a4,
735 a5,
736 p,
737 t,
738 y;
739
740 int
741 sign;
742
743 a1=0.254829592;
744 a2=-0.284496736;
745 a3=1.421413741;
746 a4=-1.453152027;
747 a5=1.061405429;
748 p=0.3275911;
749 sign=x < 0 ? -1 : 1;
750 x=fabs(x);
751 t=1.0/(1.0+p*x);
752 y=1.0-(((((a5*t+a4)*t)+a3)*t+a2)*t+a1)*t*exp(-x*x);
753 return(sign*y);
754}
755
756/*
757%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
758% %
759% %
760% %
761+ N T E r r o r H a n d l e r %
762% %
763% %
764% %
765%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766%
767% NTErrorHandler() displays an error reason and then terminates the program.
768%
769% The format of the NTErrorHandler method is:
770%
771% void NTErrorHandler(const ExceptionType severity,const char *reason,
772% const char *description)
773%
774% A description of each parameter follows:
775%
776% o severity: Specifies the numeric error category.
777%
778% o reason: Specifies the reason to display before terminating the
779% program.
780%
781% o description: Specifies any description to the reason.
782%
783*/
784MagickPrivate void NTErrorHandler(const ExceptionType severity,
785 const char *reason,const char *description)
786{
787 char
788 buffer[3*MagickPathExtent],
789 *message;
790
791 (void) severity;
792 if (reason == (char *) NULL)
793 {
794 MagickCoreTerminus();
795 exit(0);
796 }
797 message=GetExceptionMessage(errno);
798 if ((description != (char *) NULL) && errno)
799 (void) FormatLocaleString(buffer,MagickPathExtent,"%s: %s (%s) [%s].\n",
800 GetClientName(),reason,description,message);
801 else
802 if (description != (char *) NULL)
803 (void) FormatLocaleString(buffer,MagickPathExtent,"%s: %s (%s).\n",
804 GetClientName(),reason,description);
805 else
806 if (errno != 0)
807 (void) FormatLocaleString(buffer,MagickPathExtent,"%s: %s [%s].\n",
808 GetClientName(),reason,message);
809 else
810 (void) FormatLocaleString(buffer,MagickPathExtent,"%s: %s.\n",
811 GetClientName(),reason);
812 message=DestroyString(message);
813 (void) MessageBox(NULL,buffer,"ImageMagick Exception",MB_OK | MB_TASKMODAL |
814 MB_SETFOREGROUND | MB_ICONEXCLAMATION);
815 MagickCoreTerminus();
816 exit(0);
817}
818
819/*
820%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
821% %
822% %
823% %
824% N T G a t h e r R a n d o m D a t a %
825% %
826% %
827% %
828%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
829%
830% NTGatherRandomData() gathers random data and returns it.
831%
832% The format of the GatherRandomData method is:
833%
834% MagickBooleanType NTGatherRandomData(const size_t length,
835% unsigned char *random)
836%
837% A description of each parameter follows:
838%
839% length: the length of random data buffer
840%
841% random: the random data is returned here.
842%
843*/
844MagickPrivate MagickBooleanType NTGatherRandomData(const size_t length,
845 unsigned char *random)
846{
847#if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER)
848 HCRYPTPROV
849 handle;
850
851 int
852 status;
853
854 handle=(HCRYPTPROV) NULL;
855 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
856 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
857 if (status == 0)
858 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
859 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
860 if (status == 0)
861 return(MagickFalse);
862 status=CryptGenRandom(handle,(DWORD) length,random);
863 if (status == 0)
864 {
865 status=CryptReleaseContext(handle,0);
866 return(MagickFalse);
867 }
868 status=CryptReleaseContext(handle,0);
869 if (status == 0)
870 return(MagickFalse);
871#else
872 (void) random;
873 (void) length;
874#endif
875 return(MagickTrue);
876}
877
878/*
879%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
880% %
881% %
882% %
883% N T G e t E n v i r o n m e n t V a l u e %
884% %
885% %
886% %
887%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
888%
889% NTGetEnvironmentValue() returns the environment string that matches the
890% specified name.
891%
892% The format of the NTGetEnvironmentValue method is:
893%
894% char *GetEnvironmentValue(const char *name)
895%
896% A description of each parameter follows:
897%
898% o name: the environment name.
899%
900*/
901extern MagickPrivate char *NTGetEnvironmentValue(const char *name)
902{
903 char
904 *environment = (char *) NULL;
905
906 DWORD
907 size;
908
909 LPWSTR
910 wide;
911
912 wchar_t
913 wide_name[MaxWideByteExtent];
914
915 if (MultiByteToWideChar(CP_UTF8,0,name,-1,wide_name,MaxWideByteExtent) == 0)
916 return(environment);
917 size=GetEnvironmentVariableW(wide_name,(LPWSTR) NULL,0);
918 if (size == 0)
919 return(environment);
920 wide=(LPWSTR) NTAcquireQuantumMemory((const size_t) size,sizeof(*wide));
921 if (wide == (LPWSTR) NULL)
922 return(environment);
923 if (GetEnvironmentVariableW(wide_name,wide,size) != 0)
924 environment=create_utf8_string(wide);
925 wide=(LPWSTR) RelinquishMagickMemory(wide);
926 return(environment);
927}
928
929/*
930%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
931% %
932% %
933% %
934% N T G e t E x e c u t i o n P a t h %
935% %
936% %
937% %
938%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
939%
940% NTGetExecutionPath() returns the execution path of a program.
941%
942% The format of the GetExecutionPath method is:
943%
944% MagickBooleanType NTGetExecutionPath(char *path,const size_t extent)
945%
946% A description of each parameter follows:
947%
948% o path: the pathname of the executable that started the process.
949%
950% o extent: the maximum extent of the path.
951%
952*/
953MagickPrivate MagickBooleanType NTGetExecutionPath(char *path,
954 const size_t extent)
955{
956 wchar_t
957 wide_path[MagickPathExtent];
958
959 (void) GetModuleFileNameW((HMODULE) NULL,wide_path,(DWORD) extent);
960 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(int) extent,NULL,
961 NULL);
962 return(MagickTrue);
963}
964
965/*
966%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
967% %
968% %
969% %
970% N T G e t L a s t E r r o r M e s s a g e %
971% %
972% %
973% %
974%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
975%
976% NTGetLastErrorMessage() returns the last error that occurred.
977%
978% The format of the NTGetLastErrorMessage method is:
979%
980% char *NTGetLastErrorMessage(DWORD last_error)
981%
982% A description of each parameter follows:
983%
984% o last_error: The value of GetLastError.
985%
986*/
987static char *NTGetLastErrorMessage(DWORD last_error)
988{
989 char
990 *reason;
991
992 int
993 status;
994
995 LPVOID
996 buffer = (LPVOID) NULL;
997
998 status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
999 FORMAT_MESSAGE_FROM_SYSTEM,NULL,last_error,
1000 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
1001 if (!status)
1002 reason=AcquireString("An unknown error occurred");
1003 else
1004 {
1005 reason=AcquireString((const char *) buffer);
1006 LocalFree(buffer);
1007 }
1008 return(reason);
1009}
1010
1011/*
1012%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1013% %
1014% %
1015% %
1016% N T G e t L i b r a r y E r r o r %
1017% %
1018% %
1019% %
1020%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1021%
1022% Lt_dlerror() returns a pointer to a string describing the last error
1023% associated with a lt_dl method. Note that this function is not thread
1024% safe so it should only be used under the protection of a lock.
1025%
1026% The format of the NTGetLibraryError method is:
1027%
1028% const char *NTGetLibraryError(void)
1029%
1030*/
1031MagickPrivate const char *NTGetLibraryError(void)
1032{
1033 static char
1034 last_error[MagickPathExtent];
1035
1036 char
1037 *error;
1038
1039 *last_error='\0';
1040 error=NTGetLastErrorMessage(GetLastError());
1041 if (error)
1042 (void) CopyMagickString(last_error,error,MagickPathExtent);
1043 error=DestroyString(error);
1044 return(last_error);
1045}
1046
1047/*
1048%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1049% %
1050% %
1051% %
1052% N T G e t L i b r a r y S y m b o l %
1053% %
1054% %
1055% %
1056%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1057%
1058% NTGetLibrarySymbol() retrieve the procedure address of the method
1059% specified by the passed character string.
1060%
1061% The format of the NTGetLibrarySymbol method is:
1062%
1063% void *NTGetLibrarySymbol(void *handle,const char *name)
1064%
1065% A description of each parameter follows:
1066%
1067% o handle: Specifies a handle to the previously loaded dynamic module.
1068%
1069% o name: Specifies the procedure entry point to be returned.
1070%
1071*/
1072void *NTGetLibrarySymbol(void *handle,const char *name)
1073{
1074 FARPROC
1075 proc_address;
1076
1077 proc_address=GetProcAddress((HMODULE) handle,(LPCSTR) name);
1078 if (proc_address == (FARPROC) NULL)
1079 return((void *) NULL);
1080 return((void *) proc_address);
1081}
1082
1083/*
1084%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1085% %
1086% %
1087% %
1088% N T G e t M o d u l e P a t h %
1089% %
1090% %
1091% %
1092%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1093%
1094% NTGetModulePath() returns the path of the specified module.
1095%
1096% The format of the GetModulePath method is:
1097%
1098% MagickBooleanType NTGetModulePath(const char *module,char *path)
1099%
1100% A description of each parameter follows:
1101%
1102% modith: the module name.
1103%
1104% path: the module path is returned here.
1105%
1106*/
1107MagickPrivate MagickBooleanType NTGetModulePath(const char *module,char *path)
1108{
1109 char
1110 module_path[MagickPathExtent];
1111
1112 HMODULE
1113 handle;
1114
1115 ssize_t
1116 length;
1117
1118 *path='\0';
1119 handle=GetModuleHandle(module);
1120 if (handle == (HMODULE) NULL)
1121 return(MagickFalse);
1122 length=GetModuleFileName(handle,module_path,MagickPathExtent);
1123 if (length != 0)
1124 GetPathComponent(module_path,HeadPath,path);
1125 return(MagickTrue);
1126}
1127
1128/*
1129%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1130% %
1131% %
1132% %
1133% N T G h o s t s c r i p t D L L V e c t o r s %
1134% %
1135% %
1136% %
1137%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1138%
1139% NTGhostscriptDLLVectors() returns a GhostInfo structure that includes
1140% function vectors to invoke Ghostscript DLL functions. A null pointer is
1141% returned if there is an error when loading the DLL or retrieving the
1142% function vectors.
1143%
1144% The format of the NTGhostscriptDLLVectors method is:
1145%
1146% const GhostInfo *NTGhostscriptDLLVectors(void)
1147%
1148*/
1149static int NTLocateGhostscript(DWORD flags,int *root_index,
1150 const char **product_family,int *major_version,int *minor_version,
1151 int *patch_version)
1152{
1153 int
1154 i;
1155
1156 MagickBooleanType
1157 status;
1158
1159 static const char
1160 *products[2] =
1161 {
1162 "Artifex Ghostscript",
1163 "GPL Ghostscript"
1164 };
1165
1166 /*
1167 Find the most recent version of Ghostscript.
1168 */
1169 status=MagickFalse;
1170 *root_index=0;
1171 *product_family=NULL;
1172 *major_version=5;
1173 *minor_version=49; /* min version of Ghostscript is 5.50 */
1174 for (i=0; i < (ssize_t) (sizeof(products)/sizeof(products[0])); i++)
1175 {
1176 char
1177 key[MagickPathExtent];
1178
1179 HKEY
1180 hkey;
1181
1182 int
1183 j;
1184
1185 REGSAM
1186 mode;
1187
1188 (void) FormatLocaleString(key,MagickPathExtent,"SOFTWARE\\%s",products[i]);
1189 for (j=0; j < (ssize_t) (sizeof(registry_roots)/sizeof(registry_roots[0]));
1190 j++)
1191 {
1192 mode=KEY_READ | flags;
1193 if (RegOpenKeyExA(registry_roots[j].hkey,key,0,mode,&hkey) ==
1194 ERROR_SUCCESS)
1195 {
1196 DWORD
1197 extent;
1198
1199 int
1200 k;
1201
1202 /*
1203 Now enumerate the keys.
1204 */
1205 extent=sizeof(key)/sizeof(char);
1206 for (k=0; RegEnumKeyA(hkey,k,key,extent) == ERROR_SUCCESS; k++)
1207 {
1208 int
1209 major,
1210 minor,
1211 patch;
1212
1213 major=0;
1214 minor=0;
1215 patch=0;
1216 if (MagickSscanf(key,"%d.%d.%d",&major,&minor,&patch) != 3)
1217 if (MagickSscanf(key,"%d.%d",&major,&minor) != 2)
1218 continue;
1219 if ((major > *major_version) ||
1220 ((major == *major_version) && (minor > *minor_version)) ||
1221 ((minor == *minor_version) && (patch > *patch_version)))
1222 {
1223 *root_index=j;
1224 *product_family=products[i];
1225 *major_version=major;
1226 *minor_version=minor;
1227 *patch_version=patch;
1228 status=MagickTrue;
1229 }
1230 }
1231 (void) RegCloseKey(hkey);
1232 }
1233 }
1234 }
1235 if (status == MagickFalse)
1236 {
1237 *major_version=0;
1238 *minor_version=0;
1239 *patch_version=0;
1240 }
1241 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"Ghostscript (%s) "
1242 "version %d.%d.%d",*product_family,*major_version,*minor_version,*patch_version);
1243 return(status);
1244}
1245
1246static MagickBooleanType NTGhostscriptGetString(const char *name,
1247 BOOL *is_64_bit,char *value,const size_t length)
1248{
1249 char
1250 buffer[MagickPathExtent],
1251 *directory;
1252
1253 static const char
1254 *product_family = (const char *) NULL;
1255
1256 static BOOL
1257 is_64_bit_version = FALSE;
1258
1259 static int
1260 flags = 0,
1261 major_version = 0,
1262 minor_version = 0,
1263 patch_version = 0,
1264 root_index = 0;
1265
1266 unsigned char
1267 *registry_value;
1268
1269 /*
1270 Get a string from the installed Ghostscript.
1271 */
1272 *value='\0';
1273 directory=(char *) NULL;
1274 if (LocaleCompare(name,"GS_DLL") == 0)
1275 {
1276 directory=GetEnvironmentValue("MAGICK_GHOSTSCRIPT_PATH");
1277 if (directory != (char *) NULL)
1278 {
1279 (void) FormatLocaleString(buffer,MagickPathExtent,"%s%sgsdll64.dll",
1280 directory,DirectorySeparator);
1281 if (IsPathAccessible(buffer) != MagickFalse)
1282 {
1283 directory=DestroyString(directory);
1284 (void) CopyMagickString(value,buffer,length);
1285 if (is_64_bit != NULL)
1286 *is_64_bit=TRUE;
1287 return(MagickTrue);
1288 }
1289 (void) FormatLocaleString(buffer,MagickPathExtent,"%s%sgsdll32.dll",
1290 directory,DirectorySeparator);
1291 if (IsPathAccessible(buffer) != MagickFalse)
1292 {
1293 directory=DestroyString(directory);
1294 (void) CopyMagickString(value,buffer,length);
1295 if (is_64_bit != NULL)
1296 *is_64_bit=FALSE;
1297 return(MagickTrue);
1298 }
1299 return(MagickFalse);
1300 }
1301 }
1302 if (product_family == (const char *) NULL)
1303 {
1304 flags=0;
1305#if defined(KEY_WOW64_32KEY)
1306#if defined(_WIN64)
1307 flags=KEY_WOW64_64KEY;
1308#else
1309 flags=KEY_WOW64_32KEY;
1310#endif
1311 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1312 &major_version,&minor_version,&patch_version);
1313 if (product_family == (const char *) NULL)
1314#if defined(_WIN64)
1315 flags=KEY_WOW64_32KEY;
1316 else
1317 is_64_bit_version=TRUE;
1318#else
1319 flags=KEY_WOW64_64KEY;
1320#endif
1321#endif
1322 }
1323 if (product_family == (const char *) NULL)
1324 {
1325 (void) NTLocateGhostscript(flags,&root_index,&product_family,
1326 &major_version,&minor_version,&patch_version);
1327#if !defined(_WIN64)
1328 is_64_bit_version=TRUE;
1329#endif
1330 }
1331 if (product_family == (const char *) NULL)
1332 return(MagickFalse);
1333 if (is_64_bit != NULL)
1334 *is_64_bit=is_64_bit_version;
1335 (void) FormatLocaleString(buffer,MagickPathExtent,"SOFTWARE\\%s\\%d.%.2d.%d",
1336 product_family,major_version,minor_version,patch_version);
1337 registry_value=NTGetRegistryValue(registry_roots[root_index].hkey,buffer,
1338 flags,name);
1339 if (registry_value == (unsigned char *) NULL)
1340 {
1341 (void) FormatLocaleString(buffer,MagickPathExtent,"SOFTWARE\\%s\\%d.%02d",
1342 product_family,major_version,minor_version);
1343 registry_value=NTGetRegistryValue(registry_roots[root_index].hkey,buffer,
1344 flags,name);
1345 }
1346 if (registry_value == (unsigned char *) NULL)
1347 return(MagickFalse);
1348 (void) CopyMagickString(value,(const char *) registry_value,length);
1349 registry_value=(unsigned char *) RelinquishMagickMemory(registry_value);
1350 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1351 "registry: \"%s\\%s\\%s\"=\"%s\"",registry_roots[root_index].name,
1352 buffer,name,value);
1353 return(MagickTrue);
1354}
1355
1356static MagickBooleanType NTGhostscriptDLL(char *path,int length)
1357{
1358 static char
1359 dll[MagickPathExtent] = { "" };
1360
1361 static BOOL
1362 is_64_bit;
1363
1364 *path='\0';
1365 if ((*dll == '\0') &&
1366 (NTGhostscriptGetString("GS_DLL",&is_64_bit,dll,sizeof(dll)) != MagickTrue))
1367 return(MagickFalse);
1368#if defined(_WIN64)
1369 if (!is_64_bit)
1370 return(MagickFalse);
1371#else
1372 if (is_64_bit)
1373 return(MagickFalse);
1374#endif
1375 (void) CopyMagickString(path,dll,length);
1376 return(MagickTrue);
1377}
1378
1379static inline MagickBooleanType NTGhostscriptHasValidHandle()
1380{
1381 if ((nt_ghost_info.delete_instance == NULL) || (ghost_info.exit == NULL) ||
1382 (nt_ghost_info.new_instance == NULL) || (ghost_info.set_stdio == NULL) ||
1383 (ghost_info.init_with_args == NULL) || (ghost_info.revision == NULL))
1384 return(MagickFalse);
1385 return(MagickTrue);
1386}
1387
1388MagickPrivate const GhostInfo *NTGhostscriptDLLVectors(void)
1389{
1390 char
1391 path[MagickPathExtent];
1392
1393 if (ghost_semaphore == (SemaphoreInfo *) NULL)
1394 ActivateSemaphoreInfo(&ghost_semaphore);
1395 LockSemaphoreInfo(ghost_semaphore);
1396 if (ghost_handle != (void *) NULL)
1397 {
1398 UnlockSemaphoreInfo(ghost_semaphore);
1399 if (NTGhostscriptHasValidHandle() == MagickTrue)
1400 return(&ghost_info);
1401 return((GhostInfo *) NULL);
1402 }
1403 if (NTGhostscriptDLL(path,sizeof(path)) != MagickTrue)
1404 {
1405 UnlockSemaphoreInfo(ghost_semaphore);
1406 return((GhostInfo *) NULL);
1407 }
1408 ghost_handle=lt_dlopen(path);
1409 if (ghost_handle == (void *) NULL)
1410 {
1411 UnlockSemaphoreInfo(ghost_semaphore);
1412 return((GhostInfo *) NULL);
1413 }
1414 (void) memset((void *) &nt_ghost_info,0,sizeof(NTGhostInfo));
1415 nt_ghost_info.delete_instance=(void (MagickDLLCall *)(gs_main_instance *)) (
1416 lt_dlsym(ghost_handle,"gsapi_delete_instance"));
1417 nt_ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,
1418 void *)) (lt_dlsym(ghost_handle,"gsapi_new_instance"));
1419 nt_ghost_info.has_instance=MagickFalse;
1420 (void) memset((void *) &ghost_info,0,sizeof(GhostInfo));
1421 ghost_info.delete_instance=NTGhostscriptDeleteInstance;
1422 ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
1423 lt_dlsym(ghost_handle,"gsapi_exit");
1424 ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
1425 char **)) (lt_dlsym(ghost_handle,"gsapi_init_with_args"));
1426 ghost_info.new_instance=NTGhostscriptNewInstance;
1427 ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,const char *,
1428 int,int *)) (lt_dlsym(ghost_handle,"gsapi_run_string"));
1429 ghost_info.set_arg_encoding=(int (MagickDLLCall*)(gs_main_instance*, int)) (
1430 lt_dlsym(ghost_handle, "gsapi_set_arg_encoding"));
1431 ghost_info.set_stdio=(int (MagickDLLCall *)(gs_main_instance *,int(
1432 MagickDLLCall *)(void *,char *,int),int(MagickDLLCall *)(void *,
1433 const char *,int),int(MagickDLLCall *)(void *,const char *,int)))
1434 (lt_dlsym(ghost_handle,"gsapi_set_stdio"));
1435 ghost_info.revision=(int (MagickDLLCall *)(gsapi_revision_t *,int)) (
1436 lt_dlsym(ghost_handle,"gsapi_revision"));
1437 UnlockSemaphoreInfo(ghost_semaphore);
1438 if (NTGhostscriptHasValidHandle() == MagickTrue)
1439 return(&ghost_info);
1440 return((GhostInfo *) NULL);
1441}
1442
1443/*
1444%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1445% %
1446% %
1447% %
1448% N T G h o s t s c r i p t E X E %
1449% %
1450% %
1451% %
1452%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1453%
1454% NTGhostscriptEXE() obtains the path to the latest Ghostscript executable.
1455% The method returns FALSE if a full path value is not obtained and returns
1456% a default path of gswin32c.exe.
1457%
1458% The format of the NTGhostscriptEXE method is:
1459%
1460% void NTGhostscriptEXE(char *path,int length)
1461%
1462% A description of each parameter follows:
1463%
1464% o path: return the Ghostscript executable path here.
1465%
1466% o length: length of buffer.
1467%
1468*/
1469MagickPrivate void NTGhostscriptEXE(char *path,int length)
1470{
1471 char
1472 *p;
1473
1474 static char
1475 program[MagickPathExtent] = { "" };
1476
1477 static BOOL
1478 is_64_bit_version = FALSE;
1479
1480 if (*program == '\0')
1481 {
1482 if (ghost_semaphore == (SemaphoreInfo *) NULL)
1483 ActivateSemaphoreInfo(&ghost_semaphore);
1484 LockSemaphoreInfo(ghost_semaphore);
1485 if (*program == '\0')
1486 {
1487 if (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,program,
1488 sizeof(program)) == MagickFalse)
1489 {
1490 UnlockSemaphoreInfo(ghost_semaphore);
1491#if defined(_WIN64)
1492 (void) CopyMagickString(program,"gswin64c.exe",sizeof(program));
1493#else
1494 (void) CopyMagickString(program,"gswin32c.exe",sizeof(program));
1495#endif
1496 (void) CopyMagickString(path,program,length);
1497 return;
1498 }
1499 p=strrchr(program,'\\');
1500 if (p != (char *) NULL)
1501 {
1502 p++;
1503 *p='\0';
1504 (void) ConcatenateMagickString(program,is_64_bit_version ?
1505 "gswin64c.exe" : "gswin32c.exe",sizeof(program));
1506 }
1507 }
1508 UnlockSemaphoreInfo(ghost_semaphore);
1509 }
1510 (void) CopyMagickString(path,program,length);
1511}
1512
1513/*
1514%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1515% %
1516% %
1517% %
1518% N T G h o s t s c r i p t F o n t s %
1519% %
1520% %
1521% %
1522%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1523%
1524% NTGhostscriptFonts() obtains the path to the Ghostscript fonts. The method
1525% returns false if it cannot determine the font path.
1526%
1527% The format of the NTGhostscriptFonts method is:
1528%
1529% MagickBooleanType NTGhostscriptFonts(char *path,int length)
1530%
1531% A description of each parameter follows:
1532%
1533% o path: return the font path here.
1534%
1535% o length: length of the path buffer.
1536%
1537*/
1538MagickPrivate MagickBooleanType NTGhostscriptFonts(char *path,int length)
1539{
1540 char
1541 buffer[MagickPathExtent],
1542 *directory,
1543 filename[MagickPathExtent];
1544
1545 char
1546 *p,
1547 *q;
1548
1549 *path='\0';
1550 directory=GetEnvironmentValue("MAGICK_GHOSTSCRIPT_FONT_PATH");
1551 if (directory != (char *) NULL)
1552 {
1553 (void) CopyMagickString(buffer,directory,MagickPathExtent);
1554 directory=DestroyString(directory);
1555 }
1556 else
1557 {
1558 if (NTGhostscriptGetString("GS_LIB",NULL,buffer,
1559 MagickPathExtent) == MagickFalse)
1560 return(MagickFalse);
1561 }
1562 for (p=buffer-1; p != (char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1563 {
1564 (void) CopyMagickString(path,p+1,length+1);
1565 q=strchr(path,DirectoryListSeparator);
1566 if (q != (char *) NULL)
1567 *q='\0';
1568 (void) FormatLocaleString(filename,MagickPathExtent,"%s%sfonts.dir",path,
1569 DirectorySeparator);
1570 if (IsPathAccessible(filename) != MagickFalse)
1571 return(MagickTrue);
1572 (void) FormatLocaleString(filename,MagickPathExtent,"%s%sn019003l.pfb",path,
1573 DirectorySeparator);
1574 if (IsPathAccessible(filename) != MagickFalse)
1575 return(MagickTrue);
1576 }
1577 *path='\0';
1578 return(MagickFalse);
1579}
1580
1581/*
1582%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1583% %
1584% %
1585% %
1586% N T G h o s t s c r i p t U n L o a d D L L %
1587% %
1588% %
1589% %
1590%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1591%
1592% NTGhostscriptUnLoadDLL() unloads the Ghostscript DLL and returns TRUE if
1593% it succeeds.
1594%
1595% The format of the NTGhostscriptUnLoadDLL method is:
1596%
1597% int NTGhostscriptUnLoadDLL(void)
1598%
1599*/
1600MagickPrivate void NTGhostscriptUnLoadDLL(void)
1601{
1602 if (ghost_semaphore == (SemaphoreInfo *) NULL)
1603 ActivateSemaphoreInfo(&ghost_semaphore);
1604 LockSemaphoreInfo(ghost_semaphore);
1605 if (ghost_handle != (void *) NULL)
1606 {
1607 (void) lt_dlclose(ghost_handle);
1608 ghost_handle=(void *) NULL;
1609 (void) memset((void *) &ghost_info,0,sizeof(GhostInfo));
1610 }
1611 UnlockSemaphoreInfo(ghost_semaphore);
1612 RelinquishSemaphoreInfo(&ghost_semaphore);
1613}
1614
1615/*
1616%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1617% %
1618% %
1619% %
1620% N T L o n g P a t h s E n a b l e d %
1621% %
1622% %
1623% %
1624%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1625%
1626% NTLongPathsEnabled() returns a boolean indicating whether long paths are
1627$ enabled.
1628%
1629% The format of the NTLongPathsEnabled method is:
1630%
1631% MagickBooleanType NTLongPathsEnabled()
1632%
1633*/
1634MagickExport MagickBooleanType NTLongPathsEnabled()
1635{
1636 static size_t
1637 long_paths_enabled = 2;
1638
1639 if (long_paths_enabled == 2)
1640 {
1641 DWORD
1642 size,
1643 type,
1644 value;
1645
1646 HKEY
1647 registry_key;
1648
1649 LONG
1650 status;
1651
1652 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1653 status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,
1654 "SYSTEM\\CurrentControlSet\\Control\\FileSystem",0,KEY_READ,
1655 &registry_key);
1656 if (status != ERROR_SUCCESS)
1657 {
1658 RegCloseKey(registry_key);
1659 long_paths_enabled=0;
1660 return(MagickFalse);
1661 }
1662 value=0;
1663 status=RegQueryValueExA(registry_key,"LongPathsEnabled",0,&type,NULL,
1664 NULL);
1665 if ((status != ERROR_SUCCESS) || (type != REG_DWORD))
1666 {
1667 RegCloseKey(registry_key);
1668 long_paths_enabled=0;
1669 return(MagickFalse);
1670 }
1671 size=0;
1672 status=RegQueryValueExA(registry_key,"LongPathsEnabled",0,&type,
1673 (LPBYTE) &value,&size);
1674 RegCloseKey(registry_key);
1675 if (status != ERROR_SUCCESS)
1676 {
1677 long_paths_enabled=0;
1678 return(MagickFalse);
1679 }
1680 long_paths_enabled=(size_t) value;
1681 }
1682 return(long_paths_enabled == 1 ? MagickTrue : MagickFalse);
1683}
1684
1685/*
1686%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1687% %
1688% %
1689% %
1690+ N T M a p M e m o r y %
1691% %
1692% %
1693% %
1694%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1695%
1696% Mmap() emulates the Unix method of the same name.
1697%
1698% The format of the NTMapMemory method is:
1699%
1700% MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1701% int access,int file,MagickOffsetType offset)
1702%
1703*/
1704MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1705 int flags,int file,MagickOffsetType offset)
1706{
1707 DWORD
1708 access_mode,
1709 high_length,
1710 high_offset,
1711 low_length,
1712 low_offset,
1713 protection_mode;
1714
1715 HANDLE
1716 file_handle,
1717 map_handle;
1718
1719 void
1720 *map;
1721
1722 (void) address;
1723 access_mode=0;
1724 file_handle=INVALID_HANDLE_VALUE;
1725 low_length=(DWORD) (length & 0xFFFFFFFFUL);
1726 high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1727 map_handle=INVALID_HANDLE_VALUE;
1728 map=(void *) NULL;
1729 low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1730 high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1731 protection_mode=0;
1732 if (protection & PROT_WRITE)
1733 {
1734 access_mode=FILE_MAP_WRITE;
1735 if (!(flags & MAP_PRIVATE))
1736 protection_mode=PAGE_READWRITE;
1737 else
1738 {
1739 access_mode=FILE_MAP_COPY;
1740 protection_mode=PAGE_WRITECOPY;
1741 }
1742 }
1743 else
1744 if (protection & PROT_READ)
1745 {
1746 access_mode=FILE_MAP_READ;
1747 protection_mode=PAGE_READONLY;
1748 }
1749 if ((file == -1) && (flags & MAP_ANONYMOUS))
1750 file_handle=INVALID_HANDLE_VALUE;
1751 else
1752 file_handle=(HANDLE) _get_osfhandle(file);
1753 map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1754 low_length,0);
1755 if (map_handle)
1756 {
1757 map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1758 length);
1759 CloseHandle(map_handle);
1760 }
1761 if (map == (void *) NULL)
1762 return((void *) ((char *) MAP_FAILED));
1763 return((void *) ((char *) map));
1764}
1765
1766/*
1767%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1768% %
1769% %
1770% %
1771% N T O p e n D i r e c t o r y %
1772% %
1773% %
1774% %
1775%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1776%
1777% NTOpenDirectory() opens the directory named by filename and associates a
1778% directory stream with it.
1779%
1780% The format of the NTOpenDirectory method is:
1781%
1782% DIR *NTOpenDirectory(const char *path)
1783%
1784% A description of each parameter follows:
1785%
1786% o entry: Specifies a pointer to a DIR structure.
1787%
1788*/
1789MagickPrivate DIR *NTOpenDirectory(const char *path)
1790{
1791 DIR
1792 *entry;
1793
1794 size_t
1795 length;
1796
1797 wchar_t
1798 file_specification[MagickPathExtent];
1799
1800 assert(path != (const char *) NULL);
1801 length=MultiByteToWideChar(CP_UTF8,0,path,-1,file_specification,
1802 MagickPathExtent);
1803 if (length == 0)
1804 return((DIR *) NULL);
1805 if (wcsncat_s(file_specification,MagickPathExtent,L"\\*.*",MagickPathExtent-
1806 wcslen(file_specification)-1) != 0)
1807 return((DIR *) NULL);
1808 entry=(DIR *) AcquireCriticalMemory(sizeof(DIR));
1809 entry->firsttime=TRUE;
1810 entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1811 if (entry->hSearch == INVALID_HANDLE_VALUE)
1812 {
1813 entry=(DIR *) RelinquishMagickMemory(entry);
1814 return((DIR *) NULL);
1815 }
1816 return(entry);
1817}
1818
1819/*
1820%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1821% %
1822% %
1823% %
1824% N T O p e n L i b r a r y %
1825% %
1826% %
1827% %
1828%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1829%
1830% NTOpenLibrary() loads a dynamic module into memory and returns a handle that
1831% can be used to access the various procedures in the module.
1832%
1833% The format of the NTOpenLibrary method is:
1834%
1835% void *NTOpenLibrary(const char *filename)
1836%
1837% A description of each parameter follows:
1838%
1839% o path: Specifies a pointer to string representing dynamic module that
1840% is to be loaded.
1841%
1842*/
1843static UINT ChangeErrorMode(void)
1844{
1845 typedef UINT
1846 (CALLBACK *GETERRORMODE)(void);
1847
1848 GETERRORMODE
1849 getErrorMode;
1850
1851 HMODULE
1852 handle;
1853
1854 UINT
1855 mode;
1856
1857 mode=SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1858
1859 handle=GetModuleHandle("kernel32.dll");
1860 if (handle == (HMODULE) NULL)
1861 return SetErrorMode(mode);
1862
1863 getErrorMode=(GETERRORMODE) NTGetLibrarySymbol(handle,"GetErrorMode");
1864 if (getErrorMode != (GETERRORMODE) NULL)
1865 mode=getErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1866
1867 return SetErrorMode(mode);
1868}
1869
1870static inline void *NTLoadLibrary(const char *filename)
1871{
1872 void
1873 *library;
1874
1875 wchar_t
1876 *path;
1877
1878 library=(void *) NULL;
1879 path=NTCreateWidePath(filename);
1880 if (path != (wchar_t *) NULL)
1881 {
1882 library=LoadLibraryExW(path,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1883 path=(wchar_t *) RelinquishMagickMemory(path);
1884 }
1885 return(library);
1886}
1887
1888MagickPrivate void *NTOpenLibrary(const char *filename)
1889{
1890 UINT
1891 mode;
1892
1893 void
1894 *handle;
1895
1896 mode=ChangeErrorMode();
1897 handle=NTLoadLibrary(filename);
1898#if defined(MAGICKCORE_LTDL_DELEGATE)
1899 if (handle == (void *) NULL)
1900 {
1901 char
1902 path[MagickPathExtent];
1903
1904 const char
1905 *p,
1906 *q;
1907
1908 p=lt_dlgetsearchpath();
1909 while (p != (const char*) NULL)
1910 {
1911 q=strchr(p,DirectoryListSeparator);
1912 if (q != (const char*) NULL)
1913 (void) CopyMagickString(path,p,q-p+1);
1914 else
1915 (void) CopyMagickString(path,p,MagickPathExtent);
1916 (void) ConcatenateMagickString(path,DirectorySeparator,MagickPathExtent);
1917 (void) ConcatenateMagickString(path,filename,MagickPathExtent);
1918 handle=NTLoadLibrary(path);
1919 if (handle != (void *) NULL || q == (const char*) NULL)
1920 break;
1921 p=q+1;
1922 }
1923 }
1924#endif
1925 SetErrorMode(mode);
1926 return(handle);
1927}
1928
1929/*
1930%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1931% %
1932% %
1933% %
1934% N T O p e n F i l e W i d e %
1935% %
1936% %
1937% %
1938%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1939%
1940% NTOpenFileWide() opens a file and returns a file pointer.
1941%
1942% The format of the NTOpenFileWide method is:
1943%
1944% FILE *NTOpenFileWide(const char* path, const char* mode)
1945%
1946% A description of each parameter follows:
1947%
1948% o path: the file path.
1949%
1950% o mode: the file open mode.
1951%
1952*/
1953static inline wchar_t *create_wchar_mode(const char *mode)
1954{
1955 int
1956 count;
1957
1958 wchar_t
1959 *wide;
1960
1961 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
1962 wide=(wchar_t *) AcquireQuantumMemory((size_t) count+1,
1963 sizeof(*wide));
1964 if (wide == (wchar_t *) NULL)
1965 return((wchar_t *) NULL);
1966 if (MultiByteToWideChar(CP_UTF8,0,mode,-1,wide,count) == 0)
1967 {
1968 wide=(wchar_t *) RelinquishMagickMemory(wide);
1969 return((wchar_t *) NULL);
1970 }
1971 /* Specifies that the file is not inherited by child processes */
1972 wide[count] = L'\0';
1973 wide[count-1] = L'N';
1974 return(wide);
1975}
1976
1977MagickExport FILE *NTOpenFileWide(const char* path, const char* mode)
1978{
1979 FILE
1980 *file;
1981
1982 wchar_t
1983 *mode_wide,
1984 *path_wide;
1985
1986 path_wide=NTCreateWidePath(path);
1987 if (path_wide == (wchar_t *) NULL)
1988 return((FILE *) NULL);
1989 mode_wide=create_wchar_mode(mode);
1990 if (mode_wide == (wchar_t *) NULL)
1991 {
1992 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
1993 return((FILE *) NULL);
1994 }
1995 if (_wfopen_s(&file,path_wide,mode_wide) != 0)
1996 file=(FILE *) NULL;
1997 mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide);
1998 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
1999 return(file);
2000}
2001
2002/*
2003%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2004% %
2005% %
2006% %
2007% N T O p e n P i p e W i d e %
2008% %
2009% %
2010% %
2011%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2012%
2013% NTOpenPipeWide() opens a pipe and returns a file pointer.
2014%
2015% The format of the NTOpenPipeWide method is:
2016%
2017% FILE *NTOpenPipeWide(const char* command, const char* type)
2018%
2019% A description of each parameter follows:
2020%
2021% o command: the command to execute.
2022%
2023% o type: the file open mode.
2024%
2025*/
2026MagickExport FILE *NTOpenPipeWide(const char *command,const char *type)
2027{
2028 FILE
2029 *file;
2030
2031 int
2032 length;
2033
2034 wchar_t
2035 *command_wide,
2036 type_wide[5];
2037
2038 file=(FILE *) NULL;
2039 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
2040 if (length == 0)
2041 return(file);
2042 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
2043 if (length == 0)
2044 return(file);
2045 command_wide=(wchar_t *) AcquireQuantumMemory((size_t) length,
2046 sizeof(*command_wide));
2047 if (command_wide == (wchar_t *) NULL)
2048 return(file);
2049 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
2050 if (length != 0)
2051 file=_wpopen(command_wide,type_wide);
2052 command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
2053 return(file);
2054}
2055
2056/*
2057%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2058% %
2059% %
2060% %
2061% N T O p e n W i d e %
2062% %
2063% %
2064% %
2065%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2066%
2067% NTOpenWide() opens the file specified by path and mode.
2068%
2069% The format of the NTOpenWide method is:
2070%
2071% FILE *NTOpenWide(const char* path, const char* mode)
2072%
2073% A description of each parameter follows:
2074%
2075% o path: the file path.
2076%
2077% o flags: the file open flags.
2078%
2079% o mode: the file open mode.
2080%
2081*/
2082MagickExport int NTOpenWide(const char* path,int flags,mode_t mode)
2083{
2084 int
2085 file_handle,
2086 status;
2087
2088 wchar_t
2089 *path_wide;
2090
2091 path_wide=NTCreateWidePath(path);
2092 if (path_wide == (wchar_t *) NULL)
2093 return(-1);
2094 /* O_NOINHERIT specifies that the file is not inherited by child processes */
2095 status=_wsopen_s(&file_handle,path_wide,flags | O_NOINHERIT,_SH_DENYNO,mode);
2096 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
2097 return(status == 0 ? file_handle : -1);
2098}
2099
2100/*
2101%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2102% %
2103% %
2104% %
2105% N T R e a d D i r e c t o r y %
2106% %
2107% %
2108% %
2109%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2110%
2111% NTReadDirectory() returns a pointer to a structure representing the
2112% directory entry at the current position in the directory stream to which
2113% entry refers.
2114%
2115% The format of the NTReadDirectory
2116%
2117% NTReadDirectory(entry)
2118%
2119% A description of each parameter follows:
2120%
2121% o entry: Specifies a pointer to a DIR structure.
2122%
2123*/
2124MagickPrivate struct dirent *NTReadDirectory(DIR *entry)
2125{
2126 int
2127 status;
2128
2129 size_t
2130 length;
2131
2132 if (entry == (DIR *) NULL)
2133 return((struct dirent *) NULL);
2134 if (!entry->firsttime)
2135 {
2136 status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
2137 if (status == 0)
2138 return((struct dirent *) NULL);
2139 }
2140 length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
2141 entry->file_info.d_name,sizeof(entry->file_info.d_name),NULL,NULL);
2142 if (length == 0)
2143 return((struct dirent *) NULL);
2144 entry->firsttime=FALSE;
2145 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
2146 return(&entry->file_info);
2147}
2148
2149/*
2150%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2151% %
2152% %
2153% %
2154% N T R e a l P a t h W i d e %
2155% %
2156% %
2157% %
2158%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2159%
2160% NTRealPathWide returns the absolute path of the specified path.
2161%
2162% The format of the NTRealPathWide method is:
2163%
2164% char *NTRealPathWide(const char *path)
2165%
2166% A description of each parameter follows:
2167%
2168% o path: the file path.
2169%
2170*/
2171static inline wchar_t* resolve_symlink(const wchar_t* path)
2172{
2173 DWORD
2174 link_length;
2175
2176 HANDLE
2177 file_handle;
2178
2179 wchar_t
2180 *link;
2181
2182 file_handle=CreateFileW(path,GENERIC_READ,FILE_SHARE_READ |FILE_SHARE_WRITE |
2183 FILE_SHARE_DELETE,NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,NULL);
2184 if (file_handle == INVALID_HANDLE_VALUE)
2185 return((wchar_t *) NULL);
2186 link_length=GetFinalPathNameByHandleW(file_handle,NULL,0,
2187 FILE_NAME_NORMALIZED);
2188 link=(wchar_t *) AcquireQuantumMemory(link_length,sizeof(wchar_t));
2189 if (link == (wchar_t *) NULL)
2190 {
2191 CloseHandle(file_handle);
2192 return((wchar_t *) NULL);
2193 }
2194 GetFinalPathNameByHandleW(file_handle,link,link_length,FILE_NAME_NORMALIZED);
2195 CloseHandle(file_handle);
2196 return(link);
2197}
2198
2199MagickExport char *NTRealPathWide(const char *path)
2200{
2201 char
2202 *real_path;
2203
2204 wchar_t
2205 *wide_real_path,
2206 *wide_path;
2207
2208 wide_path=NTCreateWidePath(path);
2209 wide_real_path=resolve_symlink(wide_path);
2210 if (wide_real_path == (wchar_t*) NULL)
2211 {
2212 DWORD
2213 full_path_length;
2214
2215 full_path_length=GetFullPathNameW(wide_path,0,NULL,NULL);
2216 wide_real_path=(wchar_t *) AcquireQuantumMemory(full_path_length,
2217 sizeof(wchar_t));
2218 if (wide_real_path == (wchar_t*) NULL)
2219 {
2220 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
2221 return((char*) NULL);
2222 }
2223 GetFullPathNameW(wide_path,full_path_length,wide_real_path,NULL);
2224 }
2225 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
2226 /*
2227 Remove \\?\ prefix for POSIX-like behavior.
2228 */
2229 if (wcsncmp(wide_real_path,L"\\\\?\\",4) == 0)
2230 real_path=create_utf8_string(wide_real_path+4);
2231 else
2232 real_path=create_utf8_string(wide_real_path);
2233 wide_real_path=(wchar_t *) RelinquishMagickMemory(wide_real_path);
2234 return(real_path);
2235}
2236
2237/*
2238%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2239% %
2240% %
2241% %
2242% N T R e g i s t r y K e y L o o k u p %
2243% %
2244% %
2245% %
2246%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2247%
2248% NTRegistryKeyLookup() returns ImageMagick installation path settings
2249% stored in the Windows Registry. Path settings are specific to the
2250% installed ImageMagick version so that multiple Image Magick installations
2251% may coexist.
2252%
2253% Values are stored in the registry under a base path similar to
2254% "HKEY_LOCAL_MACHINE/SOFTWARE\ImageMagick\6.7.4\Q:16" or
2255% "HKEY_CURRENT_USER/SOFTWARE\ImageMagick\6.7.4\Q:16". The provided subkey
2256% is appended to this base path to form the full key.
2257%
2258% The format of the NTRegistryKeyLookup method is:
2259%
2260% unsigned char *NTRegistryKeyLookup(const char *subkey)
2261%
2262% A description of each parameter follows:
2263%
2264% o subkey: Specifies a string that identifies the registry object.
2265% Currently supported sub-keys include: "BinPath", "ConfigurePath",
2266% "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
2267%
2268*/
2269MagickPrivate unsigned char *NTRegistryKeyLookup(const char *subkey)
2270{
2271 char
2272 package_key[MagickPathExtent] = "";
2273
2274 unsigned char
2275 *value;
2276
2277 (void) FormatLocaleString(package_key,MagickPathExtent,
2278 "SOFTWARE\\%s\\%s\\Q:%d",MagickPackageName,MagickLibVersionText,
2279 MAGICKCORE_QUANTUM_DEPTH);
2280 value=NTGetRegistryValue(HKEY_LOCAL_MACHINE,package_key,0,subkey);
2281 if (value == (unsigned char *) NULL)
2282 value=NTGetRegistryValue(HKEY_CURRENT_USER,package_key,0,subkey);
2283 return(value);
2284}
2285
2286/*
2287%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2288% %
2289% %
2290% %
2291% N T R e m o v e W i d e %
2292% %
2293% %
2294% %
2295%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2296%
2297% NTRemoveWide() removes the specified file.
2298%
2299% The format of the NTRemoveWide method is:
2300%
2301% int NTRemoveWide(const char *path)
2302%
2303% A description of each parameter follows:
2304%
2305% o path: the file path.
2306%
2307*/
2308MagickExport int NTRemoveWide(const char *path)
2309{
2310 int
2311 status;
2312
2313 wchar_t
2314 *path_wide;
2315
2316 path_wide=NTCreateWidePath(path);
2317 if (path_wide == (wchar_t *) NULL)
2318 return(-1);
2319 status=_wremove(path_wide);
2320 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
2321 return(status);
2322}
2323
2324/*
2325%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2326% %
2327% %
2328% %
2329% N T R e n a m e W i d e %
2330% %
2331% %
2332% %
2333%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2334%
2335% NTRenameWide() renames a file.
2336%
2337% The format of the NTRenameWide method is:
2338%
2339% int NTRenameWide(const char *source, const char *destination)
2340%
2341% A description of each parameter follows:
2342%
2343% o source: the source file path.
2344%
2345% o destination: the destination file path.
2346%
2347*/
2348MagickExport int NTRenameWide(const char* source, const char* destination)
2349{
2350 int
2351 status;
2352
2353 wchar_t
2354 *destination_wide,
2355 *source_wide;
2356
2357 source_wide=NTCreateWidePath(source);
2358 if (source_wide == (wchar_t *) NULL)
2359 return(-1);
2360 destination_wide=NTCreateWidePath(destination);
2361 if (destination_wide == (wchar_t *) NULL)
2362 {
2363 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
2364 return(-1);
2365 }
2366 status=_wrename(source_wide,destination_wide);
2367 destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
2368 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
2369 return(status);
2370}
2371
2372/*
2373%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2374% %
2375% %
2376% %
2377% N T R e p o r t E v e n t %
2378% %
2379% %
2380% %
2381%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2382%
2383% NTReportEvent() reports an event.
2384%
2385% The format of the NTReportEvent method is:
2386%
2387% MagickBooleanType NTReportEvent(const char *event,
2388% const MagickBooleanType error)
2389%
2390% A description of each parameter follows:
2391%
2392% o event: the event.
2393%
2394% o error: MagickTrue the event is an error.
2395%
2396*/
2397MagickPrivate MagickBooleanType NTReportEvent(const char *event,
2398 const MagickBooleanType error)
2399{
2400 const char
2401 *events[1];
2402
2403 HANDLE
2404 handle;
2405
2406 WORD
2407 type;
2408
2409 handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
2410 if (handle == NULL)
2411 return(MagickFalse);
2412 events[0]=event;
2413 type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
2414 ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
2415 DeregisterEventSource(handle);
2416 return(MagickTrue);
2417}
2418
2419/*
2420%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2421% %
2422% %
2423% %
2424% N T R e s o u r c e T o B l o b %
2425% %
2426% %
2427% %
2428%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2429%
2430% NTResourceToBlob() returns a blob containing the contents of the resource
2431% in the current executable specified by the id parameter. This currently
2432% used to retrieve MGK files tha have been embedded into the various command
2433% line utilities.
2434%
2435% The format of the NTResourceToBlob method is:
2436%
2437% unsigned char *NTResourceToBlob(const char *id)
2438%
2439% A description of each parameter follows:
2440%
2441% o id: Specifies a string that identifies the resource.
2442%
2443*/
2444MagickPrivate unsigned char *NTResourceToBlob(const char *id)
2445{
2446#ifndef MAGICKCORE_LIBRARY_NAME
2447 char
2448 path[MagickPathExtent];
2449#endif
2450
2451 DWORD
2452 length;
2453
2454 HGLOBAL
2455 global;
2456
2457 HMODULE
2458 handle;
2459
2460 HRSRC
2461 resource;
2462
2463 unsigned char
2464 *blob,
2465 *value;
2466
2467 assert(id != (const char *) NULL);
2468 if (IsEventLogging() != MagickFalse)
2469 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
2470#ifdef MAGICKCORE_LIBRARY_NAME
2471 handle=GetModuleHandle(MAGICKCORE_LIBRARY_NAME);
2472#else
2473 (void) FormatLocaleString(path,MagickPathExtent,"%s%s%s",GetClientPath(),
2474 DirectorySeparator,GetClientName());
2475 if (IsPathAccessible(path) != MagickFalse)
2476 handle=GetModuleHandle(path);
2477 else
2478 handle=GetModuleHandle(0);
2479#endif
2480 if (!handle)
2481 return((unsigned char *) NULL);
2482 resource=FindResource(handle,id,"IMAGEMAGICK");
2483 if (!resource)
2484 return((unsigned char *) NULL);
2485 global=LoadResource(handle,resource);
2486 if (!global)
2487 return((unsigned char *) NULL);
2488 length=SizeofResource(handle,resource);
2489 value=(unsigned char *) LockResource(global);
2490 if (!value)
2491 {
2492 FreeResource(global);
2493 return((unsigned char *) NULL);
2494 }
2495 blob=(unsigned char *) AcquireQuantumMemory(length+MagickPathExtent,
2496 sizeof(*blob));
2497 if (blob != (unsigned char *) NULL)
2498 {
2499 (void) memcpy(blob,value,length);
2500 blob[length]='\0';
2501 }
2502 UnlockResource(global);
2503 FreeResource(global);
2504 return(blob);
2505}
2506
2507/*
2508%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2509% %
2510% %
2511% %
2512% NT S e t F i l e T i m e s t a m p %
2513% %
2514% %
2515% %
2516%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2517%
2518% NTSetFileTimestamp() sets the file timestamps for a specified file.
2519%
2520% The format of the NTSetFileTimestamp method is:
2521%
2522% int NTSetFileTimestamp(const char *path, struct stat *attributes)
2523%
2524% A description of each parameter follows:
2525%
2526% o path: the file path.
2527%
2528% o attributes: the file attributes.
2529%
2530*/
2531MagickExport int NTSetFileTimestamp(const char *path, struct stat *attributes)
2532{
2533 HANDLE
2534 handle;
2535
2536 int
2537 status;
2538
2539 wchar_t
2540 *path_wide;
2541
2542 status=(-1);
2543 path_wide=NTCreateWidePath(path);
2544 if (path_wide == (WCHAR *) NULL)
2545 return(status);
2546 handle=CreateFileW(path_wide,FILE_WRITE_ATTRIBUTES,FILE_SHARE_WRITE |
2547 FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
2548 if (handle != (HANDLE) NULL)
2549 {
2550 FILETIME
2551 creation_time,
2552 last_access_time,
2553 last_write_time;
2554
2555 ULARGE_INTEGER
2556 date_time;
2557
2558 date_time.QuadPart=(ULONGLONG) (attributes->st_ctime*10000000LL)+
2559 116444736000000000LL;
2560 creation_time.dwLowDateTime=date_time.LowPart;
2561 creation_time.dwHighDateTime=date_time.HighPart;
2562 date_time.QuadPart=(ULONGLONG) (attributes->st_atime*10000000LL)+
2563 116444736000000000LL;
2564 last_access_time.dwLowDateTime=date_time.LowPart;
2565 last_access_time.dwHighDateTime=date_time.HighPart;
2566 date_time.QuadPart=(ULONGLONG) (attributes->st_mtime*10000000LL)+
2567 116444736000000000LL;
2568 last_write_time.dwLowDateTime=date_time.LowPart;
2569 last_write_time.dwHighDateTime=date_time.HighPart;
2570 status=SetFileTime(handle,&creation_time,&last_access_time,&last_write_time);
2571 CloseHandle(handle);
2572 status=0;
2573 }
2574 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
2575 return(status);
2576}
2577
2578/*
2579%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2580% %
2581% %
2582% %
2583% N T S t a t W i d e %
2584% %
2585% %
2586% %
2587%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2588%
2589% NTStatWide() gets the file attributes for a specified file.
2590%
2591% The format of the NTStatWide method is:
2592%
2593% int NTStatWide(const char *path,struct stat *attributes)
2594%
2595% A description of each parameter follows:
2596%
2597% o path: the file path.
2598%
2599% o attributes: the file attributes.
2600%
2601*/
2602MagickExport int NTStatWide(const char *path,struct stat *attributes)
2603{
2604 int
2605 status;
2606
2607 wchar_t
2608 *path_wide;
2609
2610 path_wide=NTCreateWidePath(path);
2611 if (path_wide == (WCHAR *) NULL)
2612 return(-1);
2613 status=wstat(path_wide,attributes);
2614 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
2615 return(status);
2616}
2617
2618/*
2619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2620% %
2621% %
2622% %
2623% N T S y s t e m C o m m a n d %
2624% %
2625% %
2626% %
2627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2628%
2629% NTSystemCommand() executes the specified command and waits until it
2630% terminates. The returned value is the exit status of the command.
2631%
2632% The format of the NTSystemCommand method is:
2633%
2634% int NTSystemCommand(MagickFalse,const char *command)
2635%
2636% A description of each parameter follows:
2637%
2638% o command: This string is the command to execute.
2639%
2640% o output: an optional buffer to store the output from stderr/stdout.
2641%
2642*/
2643MagickPrivate int NTSystemCommand(const char *command,char *output)
2644{
2645#define CleanupOutputHandles \
2646 if (read_output != (HANDLE) NULL) \
2647 { \
2648 CloseHandle(read_output); \
2649 read_output=(HANDLE) NULL; \
2650 CloseHandle(write_output); \
2651 write_output=(HANDLE) NULL; \
2652 }
2653
2654#define CopyLastError \
2655 last_error=GetLastError(); \
2656 if (output != (char *) NULL) \
2657 { \
2658 error=NTGetLastErrorMessage(last_error); \
2659 if (error != (char *) NULL) \
2660 { \
2661 (void) CopyMagickString(output,error,MagickPathExtent); \
2662 error=DestroyString(error); \
2663 } \
2664 }
2665
2666 char
2667 *error,
2668 local_command[MagickPathExtent];
2669
2670 DWORD
2671 child_status,
2672 last_error;
2673
2674 int
2675 status;
2676
2677 MagickBooleanType
2678 asynchronous;
2679
2680 HANDLE
2681 read_output,
2682 write_output;
2683
2684 PROCESS_INFORMATION
2685 process_info;
2686
2687 size_t
2688 output_offset;
2689
2690 STARTUPINFO
2691 startup_info;
2692
2693 if (command == (char *) NULL)
2694 return(-1);
2695 read_output=(HANDLE) NULL;
2696 write_output=(HANDLE) NULL;
2697 GetStartupInfo(&startup_info);
2698 startup_info.dwFlags=STARTF_USESHOWWINDOW;
2699 startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
2700 (void) CopyMagickString(local_command,command,MagickPathExtent);
2701 asynchronous=command[strlen(command)-1] == '&' ? MagickTrue : MagickFalse;
2702 if (asynchronous != MagickFalse)
2703 {
2704 local_command[strlen(command)-1]='\0';
2705 startup_info.wShowWindow=SW_SHOWDEFAULT;
2706 }
2707 else
2708 {
2709 if (command[strlen(command)-1] == '|')
2710 local_command[strlen(command)-1]='\0';
2711 else
2712 startup_info.wShowWindow=SW_HIDE;
2713 read_output=(HANDLE) NULL;
2714 if (output != (char *) NULL)
2715 {
2716 if (CreatePipe(&read_output,&write_output,NULL,0))
2717 {
2718 if (SetHandleInformation(write_output,HANDLE_FLAG_INHERIT,
2719 HANDLE_FLAG_INHERIT))
2720 {
2721 startup_info.dwFlags|=STARTF_USESTDHANDLES;
2722 startup_info.hStdOutput=write_output;
2723 startup_info.hStdError=write_output;
2724 }
2725 else
2726 CleanupOutputHandles;
2727 }
2728 else
2729 read_output=(HANDLE) NULL;
2730 }
2731 }
2732 status=CreateProcess((LPCTSTR) NULL,local_command,(LPSECURITY_ATTRIBUTES)
2733 NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) TRUE,(DWORD)
2734 NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
2735 &process_info);
2736 if (status == 0)
2737 {
2738 CopyLastError;
2739 CleanupOutputHandles;
2740 return(last_error == ERROR_FILE_NOT_FOUND ? 127 : -1);
2741 }
2742 if (output != (char *) NULL)
2743 *output='\0';
2744 if (asynchronous != MagickFalse)
2745 return(status == 0);
2746 output_offset=0;
2747 status=STATUS_TIMEOUT;
2748 while (status == STATUS_TIMEOUT)
2749 {
2750 DWORD
2751 size;
2752
2753 status=WaitForSingleObject(process_info.hProcess,1000);
2754 size=0;
2755 if (read_output != (HANDLE) NULL)
2756 if (!PeekNamedPipe(read_output,NULL,0,NULL,&size,NULL))
2757 break;
2758 while (size > 0)
2759 {
2760 char
2761 buffer[MagickPathExtent];
2762
2763 DWORD
2764 bytes_read;
2765
2766 if (ReadFile(read_output,buffer,MagickPathExtent-1,&bytes_read,NULL))
2767 {
2768 size_t
2769 count;
2770
2771 count=MagickMin(MagickPathExtent-output_offset,
2772 (size_t) bytes_read+1);
2773 if (count > 0)
2774 {
2775 (void) CopyMagickString(output+output_offset,buffer,count);
2776 output_offset+=count-1;
2777 }
2778 }
2779 if (!PeekNamedPipe(read_output,NULL,0,NULL,&size,NULL))
2780 break;
2781 }
2782 }
2783 if (status != WAIT_OBJECT_0)
2784 {
2785 CopyLastError;
2786 CleanupOutputHandles;
2787 return(status);
2788 }
2789 status=GetExitCodeProcess(process_info.hProcess,&child_status);
2790 if (status == 0)
2791 {
2792 CopyLastError;
2793 CleanupOutputHandles;
2794 return(-1);
2795 }
2796 CloseHandle(process_info.hProcess);
2797 CloseHandle(process_info.hThread);
2798 CleanupOutputHandles;
2799 return((int) child_status);
2800}
2801
2802/*
2803%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2804% %
2805% %
2806% %
2807% N T S y s t e m C o n i f i g u r a t i o n %
2808% %
2809% %
2810% %
2811%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2812%
2813% NTSystemConfiguration() provides a way for the application to determine
2814% values for system limits or options at runtime.
2815%
2816% The format of the exit method is:
2817%
2818% ssize_t NTSystemConfiguration(int name)
2819%
2820% A description of each parameter follows:
2821%
2822% o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
2823%
2824*/
2825MagickPrivate ssize_t NTSystemConfiguration(int name)
2826{
2827 switch (name)
2828 {
2829 case _SC_PAGE_SIZE:
2830 {
2831 SYSTEM_INFO
2832 system_info;
2833
2834 GetSystemInfo(&system_info);
2835 return(system_info.dwPageSize);
2836 }
2837 case _SC_PHYS_PAGES:
2838 {
2839 MEMORYSTATUSEX
2840 status;
2841
2842 SYSTEM_INFO
2843 system_info;
2844
2845 status.dwLength=sizeof(status);
2846 if (GlobalMemoryStatusEx(&status) == 0)
2847 return(0L);
2848 GetSystemInfo(&system_info);
2849 return((ssize_t) status.ullTotalPhys/system_info.dwPageSize);
2850 }
2851 case _SC_OPEN_MAX:
2852 return(2048);
2853 default:
2854 break;
2855 }
2856 return(-1);
2857}
2858
2859/*
2860%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2861% %
2862% %
2863% %
2864% N T T r u n c a t e F i l e %
2865% %
2866% %
2867% %
2868%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2869%
2870% NTTruncateFile() truncates a file to a specified length.
2871%
2872% The format of the NTTruncateFile method is:
2873%
2874% int NTTruncateFile(int file,off_t length)
2875%
2876% A description of each parameter follows:
2877%
2878% o file: the file.
2879%
2880% o length: the file length.
2881%
2882*/
2883MagickPrivate int NTTruncateFile(int file,off_t length)
2884{
2885 DWORD
2886 file_pointer;
2887
2888 HANDLE
2889 file_handle;
2890
2891 long
2892 high,
2893 low;
2894
2895 file_handle=(HANDLE) _get_osfhandle(file);
2896 if (file_handle == INVALID_HANDLE_VALUE)
2897 return(-1);
2898 low=(long) (length & 0xffffffffUL);
2899 high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2900 file_pointer=SetFilePointer(file_handle,low,&high,FILE_BEGIN);
2901 if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2902 return(-1);
2903 if (SetEndOfFile(file_handle) == 0)
2904 return(-1);
2905 return(0);
2906}
2907
2908/*
2909%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2910% %
2911% %
2912% %
2913+ N T U n m a p M e m o r y %
2914% %
2915% %
2916% %
2917%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2918%
2919% NTUnmapMemory() emulates the Unix munmap method.
2920%
2921% The format of the NTUnmapMemory method is:
2922%
2923% int NTUnmapMemory(void *map,size_t length)
2924%
2925% A description of each parameter follows:
2926%
2927% o map: the address of the binary large object.
2928%
2929% o length: the length of the binary large object.
2930%
2931*/
2932MagickPrivate int NTUnmapMemory(void *map,size_t length)
2933{
2934 (void) length;
2935 if (UnmapViewOfFile(map) == 0)
2936 return(-1);
2937 return(0);
2938}
2939
2940/*
2941%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2942% %
2943% %
2944% %
2945% N T W a r n i n g H a n d l e r %
2946% %
2947% %
2948% %
2949%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2950%
2951% NTWarningHandler() displays a warning reason.
2952%
2953% The format of the NTWarningHandler method is:
2954%
2955% void NTWarningHandler(const ExceptionType severity,const char *reason,
2956% const char *description)
2957%
2958% A description of each parameter follows:
2959%
2960% o severity: Specifies the numeric warning category.
2961%
2962% o reason: Specifies the reason to display before terminating the
2963% program.
2964%
2965% o description: Specifies any description to the reason.
2966%
2967*/
2968MagickPrivate void NTWarningHandler(const ExceptionType severity,
2969 const char *reason,const char *description)
2970{
2971 char
2972 buffer[2*MagickPathExtent];
2973
2974 (void) severity;
2975 if (reason == (char *) NULL)
2976 return;
2977 if (description == (char *) NULL)
2978 (void) FormatLocaleString(buffer,MagickPathExtent,"%s: %s.\n",GetClientName(),
2979 reason);
2980 else
2981 (void) FormatLocaleString(buffer,MagickPathExtent,"%s: %s (%s).\n",
2982 GetClientName(),reason,description);
2983 (void) MessageBox(NULL,buffer,"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2984 MB_SETFOREGROUND | MB_ICONINFORMATION);
2985}
2986
2987/*
2988%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2989% %
2990% %
2991% %
2992% N T W i n d o w s G e n e s i s %
2993% %
2994% %
2995% %
2996%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2997%
2998% NTWindowsGenesis() initializes the MagickCore Windows environment.
2999%
3000% The format of the NTWindowsGenesis method is:
3001%
3002% void NTWindowsGenesis(void)
3003%
3004*/
3005MagickPrivate void NTWindowsGenesis(void)
3006{
3007 char
3008 *mode;
3009
3010 mode=GetEnvironmentValue("MAGICK_ERRORMODE");
3011 if (mode != (char *) NULL)
3012 {
3013 (void) SetErrorMode(StringToInteger(mode));
3014 mode=DestroyString(mode);
3015 }
3016#if defined(_DEBUG) && !defined(__MINGW32__)
3017 if (IsEventLogging() != MagickFalse)
3018 {
3019 int
3020 debug;
3021
3022 debug=_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
3023 //debug |= _CRTDBG_CHECK_ALWAYS_DF;
3024 debug |= _CRTDBG_DELAY_FREE_MEM_DF;
3025 debug |= _CRTDBG_LEAK_CHECK_DF;
3026 (void) _CrtSetDbgFlag(debug);
3027
3028 //_ASSERTE(_CrtCheckMemory());
3029
3030 //_CrtSetBreakAlloc(42);
3031 }
3032#endif
3033#if defined(MAGICKCORE_INSTALLED_SUPPORT)
3034 {
3035 unsigned char
3036 *path;
3037
3038 path=NTRegistryKeyLookup("LibPath");
3039 if (path != (unsigned char *) NULL)
3040 {
3041 wchar_t
3042 *lib_path;
3043
3044 lib_path=NTCreateWidePath((const char *) path);
3045 if (lib_path != (wchar_t *) NULL)
3046 {
3047 SetDllDirectoryW(lib_path);
3048 lib_path=(wchar_t *) RelinquishMagickMemory(lib_path);
3049 }
3050 path=(unsigned char *) RelinquishMagickMemory(path);
3051 }
3052 }
3053#endif
3054}
3055
3056/*
3057%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3058% %
3059% %
3060% %
3061% N T W i n d o w s T e r m i n u s %
3062% %
3063% %
3064% %
3065%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3066%
3067% NTWindowsTerminus() terminates the MagickCore Windows environment.
3068%
3069% The format of the NTWindowsTerminus method is:
3070%
3071% void NTWindowsTerminus(void)
3072%
3073*/
3074MagickPrivate void NTWindowsTerminus(void)
3075{
3076 NTGhostscriptUnLoadDLL();
3077 DistributeCacheTerminus();
3078}
3079#endif
Definition vms.h:942