blob: 166fc6f04b94b1be35a3d641bd4ed3b12cab9eab [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% John Cristy %
17% December 1996 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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% http://www.imagemagick.org/script/license.php %
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 "magick/studio.h"
cristy0157aea2010-04-24 21:12:18 +000042#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000043#include "magick/client.h"
44#include "magick/log.h"
45#include "magick/magick.h"
46#include "magick/memory_.h"
47#include "magick/resource_.h"
48#include "magick/timer.h"
49#include "magick/string_.h"
50#include "magick/utility.h"
51#include "magick/version.h"
52#if defined(MAGICKCORE_LTDL_DELEGATE)
53# include "ltdl.h"
54#endif
55#include "magick/nt-base.h"
56#if defined(MAGICKCORE_CIPHER_SUPPORT)
57#include <ntsecapi.h>
58#include <wincrypt.h>
59#endif
60
61/*
62 Define declarations.
63*/
64#if !defined(MAP_FAILED)
65#define MAP_FAILED ((void *) -1)
66#endif
67
68/*
69 Static declarations.
70*/
71#if !defined(MAGICKCORE_LTDL_DELEGATE)
72static char
73 *lt_slsearchpath = (char *) NULL;
74#endif
75
cristydefb3f02009-09-10 02:18:35 +000076static GhostInfo
77 ghost_info;
cristy3ed852e2009-09-05 21:47:34 +000078
79static void
cristydefb3f02009-09-10 02:18:35 +000080 *ghost_handle = (void *) NULL;
cristy3ed852e2009-09-05 21:47:34 +000081
82/*
83 External declarations.
84*/
cristy0157aea2010-04-24 21:12:18 +000085#if !defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000086extern "C" BOOL WINAPI
87 DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved);
88#endif
89
90/*
91%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92% %
93% %
94% %
95% D l l M a i n %
96% %
97% %
98% %
99%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100%
101% DllMain() is an entry point to the DLL which is called when processes and
102% threads are initialized and terminated, or upon calls to the Windows
103% LoadLibrary and FreeLibrary functions.
104%
105% The function returns TRUE of it succeeds, or FALSE if initialization fails.
106%
107% The format of the DllMain method is:
108%
109% BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
110%
111% A description of each parameter follows:
112%
113% o handle: handle to the DLL module
114%
115% o reason: reason for calling function:
116%
117% DLL_PROCESS_ATTACH - DLL is being loaded into virtual address
118% space of current process.
119% DLL_THREAD_ATTACH - Indicates that the current process is
120% creating a new thread. Called under the
121% context of the new thread.
122% DLL_THREAD_DETACH - Indicates that the thread is exiting.
123% Called under the context of the exiting
124% thread.
125% DLL_PROCESS_DETACH - Indicates that the DLL is being unloaded
126% from the virtual address space of the
127% current process.
128%
129% o lpvReserved: Used for passing additional info during DLL_PROCESS_ATTACH
130% and DLL_PROCESS_DETACH.
131%
132*/
133#if defined(_DLL) && defined( ProvideDllMain )
134BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
135{
136 switch (reason)
137 {
138 case DLL_PROCESS_ATTACH:
139 {
140 char
141 *module_path;
142
143 ssize_t
144 count;
145
146 module_path=(char *) AcquireQuantumMemory(MaxTextExtent,
147 sizeof(*module_path));
148 if (module_path == (char *) NULL)
149 return(FALSE);
150 count=(ssize_t) GetModuleFileName(handle,module_path,MaxTextExtent);
151 if (count != 0)
152 {
153 char
154 *path;
155
156 for ( ; count > 0; count--)
157 if (module_path[count] == '\\')
158 {
159 module_path[count+1]='\0';
160 break;
161 }
162 MagickCoreGenesis(module_path,MagickFalse);
163 path=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,sizeof(*path));
164 if (path == (char *) NULL)
165 {
166 module_path=DestroyString(module_path);
167 return(FALSE);
168 }
169 count=(ssize_t) GetEnvironmentVariable("PATH",path,16*MaxTextExtent);
170 if ((count != 0) && (strstr(path,module_path) == (char *) NULL))
171 {
172 if ((strlen(module_path)+count+1) < (16*MaxTextExtent-1))
173 {
174 char
175 *variable;
176
177 variable=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,
178 sizeof(*variable));
179 if (variable == (char *) NULL)
180 {
181 path=DestroyString(path);
182 module_path=DestroyString(module_path);
183 return(FALSE);
184 }
185 (void) FormatMagickString(variable,16*MaxTextExtent,
186 "%s;%s",module_path,path);
187 SetEnvironmentVariable("PATH",variable);
188 variable=DestroyString(variable);
189 }
190 }
191 path=DestroyString(path);
192 }
193 module_path=DestroyString(module_path);
194 break;
195 }
196 case DLL_PROCESS_DETACH:
197 {
198 MagickCoreTerminus();
199 break;
200 }
201 default:
202 break;
203 }
204 return(TRUE);
205}
206#endif
207
208/*
209%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210% %
211% %
212% %
213% E x i t %
214% %
215% %
216% %
217%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218%
219% Exit() calls TerminateProcess for Win95.
220%
221% The format of the exit method is:
222%
223% int Exit(int status)
224%
225% A description of each parameter follows:
226%
227% o status: an integer value representing the status of the terminating
228% process.
229%
230*/
231MagickExport int Exit(int status)
232{
233 if (IsWindows95())
234 TerminateProcess(GetCurrentProcess(),(unsigned int) status);
235 exit(status);
236 return(0);
237}
238
cristy0157aea2010-04-24 21:12:18 +0000239#if !defined(__MINGW32__)
cristy3ed852e2009-09-05 21:47:34 +0000240/*
241%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242% %
243% %
244% %
cristy6d71f8d2010-02-28 00:46:02 +0000245% g e t t i m e o f d a y %
246% %
247% %
248% %
249%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250%
251% The gettimeofday() method get the time of day.
252%
253% The format of the gettimeofday method is:
254%
255% int gettimeofday(struct timeval *time_value,struct timezone *time_zone)
256%
257% A description of each parameter follows:
258%
259% o time_value: the time value.
260%
261% o time_zone: the time zone.
262%
263*/
264MagickExport int gettimeofday (struct timeval *time_value,
265 struct timezone *time_zone)
266{
cristyc05bf172010-06-04 19:53:53 +0000267#define EpochFiletime MagickLLConstant(116444736000000000)
cristy6d71f8d2010-02-28 00:46:02 +0000268
269 static int
270 is_tz_set;
271
272 if (time_value != (struct timeval *) NULL)
273 {
274 FILETIME
275 file_time;
276
277 __int64
278 time;
279
280 LARGE_INTEGER
281 date_time;
282
283 GetSystemTimeAsFileTime(&file_time);
284 date_time.LowPart=file_time.dwLowDateTime;
285 date_time.HighPart=file_time.dwHighDateTime;
286 time=date_time.QuadPart;
cristy99881662010-03-04 14:25:26 +0000287 time-=EpochFiletime;
cristy6d71f8d2010-02-28 00:46:02 +0000288 time/=10;
cristybb503372010-05-27 20:51:26 +0000289 time_value->tv_sec=(ssize_t) (time / 1000000);
290 time_value->tv_usec=(ssize_t) (time % 1000000);
cristy6d71f8d2010-02-28 00:46:02 +0000291 }
292 if (time_zone != (struct timezone *) NULL)
293 {
294 if (is_tz_set == 0)
295 {
296 _tzset();
297 is_tz_set++;
298 }
299 time_zone->tz_minuteswest=_timezone/60;
300 time_zone->tz_dsttime=_daylight;
301 }
302 return(0);
303}
cristy807fd932010-04-24 03:38:46 +0000304#endif
cristy6d71f8d2010-02-28 00:46:02 +0000305
306/*
307%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308% %
309% %
310% %
cristy3ed852e2009-09-05 21:47:34 +0000311% I s W i n d o w s 9 5 %
312% %
313% %
314% %
315%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316%
317% IsWindows95() returns true if the system is Windows 95.
318%
319% The format of the IsWindows95 method is:
320%
321% int IsWindows95()
322%
323*/
324MagickExport int IsWindows95()
325{
326 OSVERSIONINFO
327 version_info;
328
329 version_info.dwOSVersionInfoSize=sizeof(version_info);
330 if (GetVersionEx(&version_info) &&
331 (version_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS))
332 return(1);
333 return(0);
334}
335
336/*
337%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338% %
339% %
340% %
341% N T C l o s e D i r e c t o r y %
342% %
343% %
344% %
345%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346%
347% NTCloseDirectory() closes the named directory stream and frees the DIR
348% structure.
349%
350% The format of the NTCloseDirectory method is:
351%
352% int NTCloseDirectory(DIR *entry)
353%
354% A description of each parameter follows:
355%
356% o entry: Specifies a pointer to a DIR structure.
357%
358*/
359MagickExport int NTCloseDirectory(DIR *entry)
360{
361 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
362 assert(entry != (DIR *) NULL);
363 FindClose(entry->hSearch);
364 entry=(DIR *) RelinquishMagickMemory(entry);
365 return(0);
366}
367
368/*
369%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370% %
371% %
372% %
373% N T C l o s e L i b r a r y %
374% %
375% %
376% %
377%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378%
379% NTCloseLibrary() unloads the module associated with the passed handle.
380%
381% The format of the NTCloseLibrary method is:
382%
383% void NTCloseLibrary(void *handle)
384%
385% A description of each parameter follows:
386%
387% o handle: Specifies a handle to a previously loaded dynamic module.
388%
389*/
390MagickExport int NTCloseLibrary(void *handle)
391{
392 if (IsWindows95())
393 return(FreeLibrary((HINSTANCE) handle));
394 return(!(FreeLibrary((HINSTANCE) handle)));
395}
396
397/*
398%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399% %
400% %
401% %
402% N T C o n t r o l H a n d l e r %
403% %
404% %
405% %
406%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
407%
408% NTControlHandler() registers a control handler that is activated when, for
409% example, a ctrl-c is received.
410%
411% The format of the NTControlHandler method is:
412%
413% int NTControlHandler(void)
414%
415*/
416
417static BOOL ControlHandler(DWORD type)
418{
cristy3b743bb2009-09-14 16:07:59 +0000419 (void) type;
cristyf34a1452009-10-24 22:29:27 +0000420 AsynchronousResourceComponentTerminus();
cristy3ed852e2009-09-05 21:47:34 +0000421 return(FALSE);
422}
423
424MagickExport int NTControlHandler(void)
425{
426 return(SetConsoleCtrlHandler((PHANDLER_ROUTINE) ControlHandler,TRUE));
427}
428
429/*
430%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431% %
432% %
433% %
434% N T E l a p s e d T i m e %
435% %
436% %
437% %
438%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
439%
440% NTElapsedTime() returns the elapsed time (in seconds) since the last call to
441% StartTimer().
442%
443% The format of the ElapsedTime method is:
444%
445% double NTElapsedTime(void)
446%
447*/
448MagickExport double NTElapsedTime(void)
449{
450 union
451 {
452 FILETIME
453 filetime;
454
455 __int64
456 filetime64;
457 } elapsed_time;
458
459 SYSTEMTIME
460 system_time;
461
462 GetSystemTime(&system_time);
463 SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
464 return((double) 1.0e-7*elapsed_time.filetime64);
465}
466
467/*
468%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
469% %
470% %
471% %
472+ N T E r r o r H a n d l e r %
473% %
474% %
475% %
476%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
477%
478% NTErrorHandler() displays an error reason and then terminates the program.
479%
480% The format of the NTErrorHandler method is:
481%
cristy3b743bb2009-09-14 16:07:59 +0000482% void NTErrorHandler(const ExceptionType severity,const char *reason,
cristy3ed852e2009-09-05 21:47:34 +0000483% const char *description)
484%
485% A description of each parameter follows:
486%
cristy3b743bb2009-09-14 16:07:59 +0000487% o severity: Specifies the numeric error category.
cristy3ed852e2009-09-05 21:47:34 +0000488%
489% o reason: Specifies the reason to display before terminating the
490% program.
491%
492% o description: Specifies any description to the reason.
493%
494*/
cristy3b743bb2009-09-14 16:07:59 +0000495MagickExport void NTErrorHandler(const ExceptionType severity,
496 const char *reason,const char *description)
cristy3ed852e2009-09-05 21:47:34 +0000497{
498 char
499 buffer[3*MaxTextExtent],
500 *message;
501
cristy3b743bb2009-09-14 16:07:59 +0000502 (void) severity;
cristy3ed852e2009-09-05 21:47:34 +0000503 if (reason == (char *) NULL)
504 {
505 MagickCoreTerminus();
506 exit(0);
507 }
508 message=GetExceptionMessage(errno);
509 if ((description != (char *) NULL) && errno)
510 (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s (%s) [%s].\n",
511 GetClientName(),reason,description,message);
512 else
513 if (description != (char *) NULL)
514 (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s (%s).\n",
515 GetClientName(),reason,description);
516 else
517 if (errno != 0)
518 (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s [%s].\n",
519 GetClientName(),reason,message);
520 else
521 (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s.\n",
522 GetClientName(),reason);
523 message=DestroyString(message);
524 (void) MessageBox(NULL,buffer,"ImageMagick Exception",MB_OK | MB_TASKMODAL |
525 MB_SETFOREGROUND | MB_ICONEXCLAMATION);
526 MagickCoreTerminus();
527 exit(0);
528}
529
530/*
531%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
532% %
533% %
534% %
535% N T E x i t L i b r a r y %
536% %
537% %
538% %
539%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
540%
541% NTExitLibrary() exits the dynamic module loading subsystem.
542%
543% The format of the NTExitLibrary method is:
544%
545% int NTExitLibrary(void)
546%
547*/
548MagickExport int NTExitLibrary(void)
549{
550 return(0);
551}
552
553/*
554%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
555% %
556% %
557% %
558% N T G a t h e r R a n d o m D a t a %
559% %
560% %
561% %
562%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563%
564% NTGatherRandomData() gathers random data and returns it.
565%
566% The format of the GatherRandomData method is:
567%
568% MagickBooleanType NTGatherRandomData(const size_t length,
569% unsigned char *random)
570%
571% A description of each parameter follows:
572%
573% length: the length of random data buffer
574%
575% random: the random data is returned here.
576%
577*/
578MagickExport MagickBooleanType NTGatherRandomData(const size_t length,
579 unsigned char *random)
580{
581#if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER) && (_MSC_VER > 1200)
582 HCRYPTPROV
583 handle;
584
585 int
586 status;
587
588 handle=(HCRYPTPROV) NULL;
589 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
590 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
591 if (status == 0)
592 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
593 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
594 if (status == 0)
595 return(MagickFalse);
596 status=CryptGenRandom(handle,(DWORD) length,random);
597 if (status == 0)
598 {
599 status=CryptReleaseContext(handle,0);
600 return(MagickFalse);
601 }
602 status=CryptReleaseContext(handle,0);
603 if (status == 0)
604 return(MagickFalse);
cristy3b743bb2009-09-14 16:07:59 +0000605#else
606 (void) random;
607 (void) length;
cristy3ed852e2009-09-05 21:47:34 +0000608#endif
609 return(MagickTrue);
610}
611
612/*
613%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614% %
615% %
616% %
617% N T G e t E x e c u t i o n P a t h %
618% %
619% %
620% %
621%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
622%
623% NTGetExecutionPath() returns the execution path of a program.
624%
625% The format of the GetExecutionPath method is:
626%
627% MagickBooleanType NTGetExecutionPath(char *path,const size_t extent)
628%
629% A description of each parameter follows:
630%
631% o path: the pathname of the executable that started the process.
632%
633% o extent: the maximum extent of the path.
634%
635*/
636MagickExport MagickBooleanType NTGetExecutionPath(char *path,
637 const size_t extent)
638{
639 GetModuleFileName(0,path,(DWORD) extent);
640 return(MagickTrue);
641}
642
643/*
644%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645% %
646% %
647% %
648% N T G e t L a s t E r r o r %
649% %
650% %
651% %
652%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
653%
654% NTGetLastError() returns the last error that occurred.
655%
656% The format of the NTGetLastError method is:
657%
658% char *NTGetLastError(void)
659%
660*/
661char *NTGetLastError(void)
662{
663 char
664 *reason;
665
666 int
667 status;
668
669 LPVOID
670 buffer;
671
672 status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
673 FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),
674 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
675 if (!status)
676 reason=AcquireString("An unknown error occurred");
677 else
678 {
679 reason=AcquireString((const char *) buffer);
680 LocalFree(buffer);
681 }
682 return(reason);
683}
684
685/*
686%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
687% %
688% %
689% %
690% N T G e t L i b r a r y E r r o r %
691% %
692% %
693% %
694%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
695%
696% Lt_dlerror() returns a pointer to a string describing the last error
697% associated with a lt_dl method. Note that this function is not thread
698% safe so it should only be used under the protection of a lock.
699%
700% The format of the NTGetLibraryError method is:
701%
702% const char *NTGetLibraryError(void)
703%
704*/
705MagickExport const char *NTGetLibraryError(void)
706{
707 static char
708 last_error[MaxTextExtent];
709
710 char
711 *error;
712
713 *last_error='\0';
714 error=NTGetLastError();
715 if (error)
716 (void) CopyMagickString(last_error,error,MaxTextExtent);
717 error=DestroyString(error);
718 return(last_error);
719}
720
721/*
722%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723% %
724% %
725% %
726% N T G e t L i b r a r y S y m b o l %
727% %
728% %
729% %
730%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731%
732% NTGetLibrarySymbol() retrieve the procedure address of the method
733% specified by the passed character string.
734%
735% The format of the NTGetLibrarySymbol method is:
736%
737% void *NTGetLibrarySymbol(void *handle,const char *name)
738%
739% A description of each parameter follows:
740%
741% o handle: Specifies a handle to the previously loaded dynamic module.
742%
743% o name: Specifies the procedure entry point to be returned.
744%
745*/
746void *NTGetLibrarySymbol(void *handle,const char *name)
747{
748 LPFNDLLFUNC1
749 lpfnDllFunc1;
750
751 lpfnDllFunc1=(LPFNDLLFUNC1) GetProcAddress((HINSTANCE) handle,name);
752 if (!lpfnDllFunc1)
753 return((void *) NULL);
754 return((void *) lpfnDllFunc1);
755}
756
757/*
758%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
759% %
760% %
761% %
762% N T G e t M o d u l e P a t h %
763% %
764% %
765% %
766%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
767%
768% NTGetModulePath() returns the path of the specified module.
769%
770% The format of the GetModulePath method is:
771%
772% MagickBooleanType NTGetModulePath(const char *module,char *path)
773%
774% A description of each parameter follows:
775%
776% modith: the module name.
777%
778% path: the module path is returned here.
779%
780*/
781MagickExport MagickBooleanType NTGetModulePath(const char *module,char *path)
782{
783 char
784 module_path[MaxTextExtent];
785
786 HMODULE
787 handle;
788
cristybb503372010-05-27 20:51:26 +0000789 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000790 length;
791
792 *path='\0';
793 handle=GetModuleHandle(module);
794 if (handle == (HMODULE) NULL)
795 return(MagickFalse);
796 length=GetModuleFileName(handle,module_path,MaxTextExtent);
797 if (length != 0)
798 GetPathComponent(module_path,HeadPath,path);
799 return(MagickTrue);
800}
801
802/*
803%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
804% %
805% %
806% %
807% N T G h o s t s c r i p t D L L %
808% %
809% %
810% %
811%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
812%
cristydefb3f02009-09-10 02:18:35 +0000813% NTGhostscriptDLL() returns the path to the most recent Ghostscript version
814% DLL. The method returns TRUE on success otherwise FALSE.
cristy3ed852e2009-09-05 21:47:34 +0000815%
816% The format of the NTGhostscriptDLL method is:
817%
cristyfc0f64b2009-09-09 18:57:08 +0000818% int NTGhostscriptDLL(char *path,int length)
cristy3ed852e2009-09-05 21:47:34 +0000819%
820% A description of each parameter follows:
821%
cristyfc0f64b2009-09-09 18:57:08 +0000822% o path: return the Ghostscript DLL path here.
cristy3ed852e2009-09-05 21:47:34 +0000823%
cristyfc0f64b2009-09-09 18:57:08 +0000824% o length: the buffer length.
cristy3ed852e2009-09-05 21:47:34 +0000825%
826*/
827
cristyfc0f64b2009-09-09 18:57:08 +0000828static int NTGetRegistryValue(HKEY root,const char *key,const char *name,
829 char *value,int *length)
cristy3ed852e2009-09-05 21:47:34 +0000830{
cristyfc0f64b2009-09-09 18:57:08 +0000831 BYTE
832 byte,
cristy3ed852e2009-09-05 21:47:34 +0000833 *p;
834
cristyfc0f64b2009-09-09 18:57:08 +0000835 DWORD
836 extent,
837 type;
cristy3ed852e2009-09-05 21:47:34 +0000838
cristy3ed852e2009-09-05 21:47:34 +0000839 HKEY
840 hkey;
841
cristy3ed852e2009-09-05 21:47:34 +0000842 LONG
cristyfc0f64b2009-09-09 18:57:08 +0000843 status;
cristy3ed852e2009-09-05 21:47:34 +0000844
cristyfc0f64b2009-09-09 18:57:08 +0000845 /*
cristydefb3f02009-09-10 02:18:35 +0000846 Get a registry value: key = root\\key, named value = name.
847 */
cristyfc0f64b2009-09-09 18:57:08 +0000848 if (RegOpenKeyExA(root,key,0,KEY_READ,&hkey) != ERROR_SUCCESS)
849 return(1); /* no match */
850 p=(BYTE *) value;
851 type=REG_SZ;
852 extent=(*length);
853 if (p == (BYTE *) NULL)
cristydefb3f02009-09-10 02:18:35 +0000854 p=(&byte); /* ERROR_MORE_DATA only if value is NULL */
cristyfc0f64b2009-09-09 18:57:08 +0000855 status=RegQueryValueExA(hkey,(char *) name,0,&type,p,&extent);
856 RegCloseKey(hkey);
857 if (status == ERROR_SUCCESS)
cristy3ed852e2009-09-05 21:47:34 +0000858 {
cristyfc0f64b2009-09-09 18:57:08 +0000859 *length=extent;
860 return(0); /* return the match */
861 }
862 if (status == ERROR_MORE_DATA)
863 {
864 *length=extent;
cristydefb3f02009-09-10 02:18:35 +0000865 return(-1); /* buffer not large enough */
cristy3ed852e2009-09-05 21:47:34 +0000866 }
867 return(1); /* not found */
868}
869
cristydefb3f02009-09-10 02:18:35 +0000870static int NTLocateGhostscript(const char **product_family,int *major_version,
cristyfc0f64b2009-09-09 18:57:08 +0000871 int *minor_version)
cristy3ed852e2009-09-05 21:47:34 +0000872{
cristyfc0f64b2009-09-09 18:57:08 +0000873 int
874 i;
cristy3ed852e2009-09-05 21:47:34 +0000875
cristyfc0f64b2009-09-09 18:57:08 +0000876 MagickBooleanType
877 status;
878
879 static const char
880 *products[4] =
881 {
882 "GPL Ghostscript",
883 "GNU Ghostscript",
884 "AFPL Ghostscript",
cristy82b15832009-10-06 19:17:37 +0000885 "Aladdin Ghostscript"
cristyfc0f64b2009-09-09 18:57:08 +0000886 };
887
888 /*
889 Find the most recent version of Ghostscript.
890 */
891 status=FALSE;
892 *product_family=NULL;
893 *major_version=5;
894 *minor_version=49; /* min version of Ghostscript is 5.50 */
cristybb503372010-05-27 20:51:26 +0000895 for (i=0; i < (ssize_t) (sizeof(products)/sizeof(products[0])); i++)
cristyfc0f64b2009-09-09 18:57:08 +0000896 {
897 char
898 key[MaxTextExtent];
899
900 HKEY
901 hkey,
902 root;
903
cristye66bcb42009-09-17 13:31:08 +0000904 REGSAM
905 mode;
906
cristyfc0f64b2009-09-09 18:57:08 +0000907 (void) FormatMagickString(key,MaxTextExtent,"SOFTWARE\\%s",products[i]);
908 root=HKEY_LOCAL_MACHINE;
cristye66bcb42009-09-17 13:31:08 +0000909 mode=KEY_READ;
910#if defined(KEY_WOW64_32KEY)
911 mode|=KEY_WOW64_32KEY;
912#endif
913 if (RegOpenKeyExA(root,key,0,mode,&hkey) == ERROR_SUCCESS)
cristyfc0f64b2009-09-09 18:57:08 +0000914 {
915 DWORD
916 extent;
917
918 int
919 j;
920
921 /*
922 Now enumerate the keys.
923 */
924 extent=sizeof(key)/sizeof(char);
925 for (j=0; RegEnumKeyA(hkey,j,key,extent) == ERROR_SUCCESS; j++)
926 {
927 int
928 major,
929 minor;
930
931 major=0;
932 minor=0;
933 if (sscanf(key,"%d.%d",&major,&minor) != 2)
934 continue;
935 if ((major > *major_version) || ((major == *major_version) &&
936 (minor > *minor_version)))
937 {
938 *product_family=products[i];
939 *major_version=major;
940 *minor_version=minor;
cristy37f63772009-11-16 17:06:36 +0000941 status=TRUE;
cristyfc0f64b2009-09-09 18:57:08 +0000942 }
cristyfc0f64b2009-09-09 18:57:08 +0000943 }
cristye66bcb42009-09-17 13:31:08 +0000944 (void) RegCloseKey(hkey);
945 }
946 }
cristy37f63772009-11-16 17:06:36 +0000947 if (status == FALSE)
cristyfc0f64b2009-09-09 18:57:08 +0000948 {
949 *major_version=0;
950 *minor_version=0;
951 }
952 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"Ghostscript (%s) "
953 "version %d.%02d",*product_family,*major_version,*minor_version);
954 return(status);
955}
956
957static int NTGhostscriptGetString(const char *name,char *value,
958 const size_t length)
959{
cristy3ed852e2009-09-05 21:47:34 +0000960 char
cristy3ed852e2009-09-05 21:47:34 +0000961 key[MaxTextExtent];
962
963 int
cristyfc0f64b2009-09-09 18:57:08 +0000964 i,
965 extent;
cristy82b15832009-10-06 19:17:37 +0000966
cristyfc0f64b2009-09-09 18:57:08 +0000967 static const char
cristydefb3f02009-09-10 02:18:35 +0000968 *product_family = (const char *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000969
cristyfc0f64b2009-09-09 18:57:08 +0000970 static int
971 major_version=0,
972 minor_version=0;
cristy3ed852e2009-09-05 21:47:34 +0000973
cristyfc0f64b2009-09-09 18:57:08 +0000974 struct
975 {
976 const HKEY
977 hkey;
cristy3ed852e2009-09-05 21:47:34 +0000978
cristyfc0f64b2009-09-09 18:57:08 +0000979 const char
980 *name;
981 }
982 hkeys[2] =
983 {
984 { HKEY_CURRENT_USER, "HKEY_CURRENT_USER" },
985 { HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" }
986 };
987
988 /*
989 Get a string from the installed Ghostscript.
990 */
cristydefb3f02009-09-10 02:18:35 +0000991 *value='\0';
cristyfc0f64b2009-09-09 18:57:08 +0000992 if (product_family == NULL)
cristydefb3f02009-09-10 02:18:35 +0000993 (void) NTLocateGhostscript(&product_family,&major_version,&minor_version);
cristyfc0f64b2009-09-09 18:57:08 +0000994 if (product_family == NULL)
995 return(FALSE);
996 (void) FormatMagickString(key,MaxTextExtent,"SOFTWARE\\%s\\%d.%02d",
997 product_family,major_version,minor_version);
cristybb503372010-05-27 20:51:26 +0000998 for (i=0; i < (ssize_t) (sizeof(hkeys)/sizeof(hkeys[0])); i++)
cristyfc0f64b2009-09-09 18:57:08 +0000999 {
cristy76319442009-09-22 02:04:05 +00001000 extent=(int) length;
cristyfc0f64b2009-09-09 18:57:08 +00001001 if (NTGetRegistryValue(hkeys[i].hkey,key,name,value,&extent) == 0)
1002 {
1003 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1004 "registry: \"%s\\%s\\%s\"=\"%s\"",hkeys[i].name,key,name,value);
1005 return(TRUE);
1006 }
1007 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1008 "registry: \"%s\\%s\\%s\" (failed)",hkeys[i].name,key,name);
1009 }
cristy3ed852e2009-09-05 21:47:34 +00001010 return(FALSE);
1011}
1012
cristyfc0f64b2009-09-09 18:57:08 +00001013MagickExport int NTGhostscriptDLL(char *path,int length)
cristy3ed852e2009-09-05 21:47:34 +00001014{
cristy4c11ed82009-09-11 03:36:46 +00001015 static char
1016 dll[MaxTextExtent] = { "" };
cristy3ed852e2009-09-05 21:47:34 +00001017
1018 *path='\0';
cristy4c11ed82009-09-11 03:36:46 +00001019 if ((*dll == '\0') &&
1020 (NTGhostscriptGetString("GS_DLL",dll,sizeof(dll)) == FALSE))
cristy106919c2009-09-11 03:46:56 +00001021 return(FALSE);
cristy4c11ed82009-09-11 03:36:46 +00001022 (void) CopyMagickString(path,dll,length);
cristy3ed852e2009-09-05 21:47:34 +00001023 return(TRUE);
1024}
1025
1026/*
1027%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1028% %
1029% %
1030% %
1031% N T G h o s t s c r i p t D L L V e c t o r s %
1032% %
1033% %
1034% %
1035%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1036%
cristydefb3f02009-09-10 02:18:35 +00001037% NTGhostscriptDLLVectors() returns a GhostInfo structure that includes
1038% function vectors to invoke Ghostscript DLL functions. A null pointer is
1039% returned if there is an error when loading the DLL or retrieving the
1040% function vectors.
cristy3ed852e2009-09-05 21:47:34 +00001041%
1042% The format of the NTGhostscriptDLLVectors method is:
1043%
cristydefb3f02009-09-10 02:18:35 +00001044% const GhostInfo *NTGhostscriptDLLVectors(void)
cristy3ed852e2009-09-05 21:47:34 +00001045%
1046*/
cristydefb3f02009-09-10 02:18:35 +00001047MagickExport const GhostInfo *NTGhostscriptDLLVectors(void)
cristy3ed852e2009-09-05 21:47:34 +00001048{
cristyfc0f64b2009-09-09 18:57:08 +00001049 if (NTGhostscriptLoadDLL() == FALSE)
cristydefb3f02009-09-10 02:18:35 +00001050 return((GhostInfo *) NULL);
1051 return(&ghost_info);
cristy3ed852e2009-09-05 21:47:34 +00001052}
1053
1054/*
1055%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1056% %
1057% %
1058% %
1059% N T G h o s t s c r i p t E X E %
1060% %
1061% %
1062% %
1063%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1064%
1065% NTGhostscriptEXE() obtains the path to the latest Ghostscript executable.
cristyfc0f64b2009-09-09 18:57:08 +00001066% The method returns FALSE if a full path value is not obtained and returns
1067% a default path of gswin32c.exe.
cristy3ed852e2009-09-05 21:47:34 +00001068%
1069% The format of the NTGhostscriptEXE method is:
1070%
cristyfc0f64b2009-09-09 18:57:08 +00001071% int NTGhostscriptEXE(char *path,int length)
cristy3ed852e2009-09-05 21:47:34 +00001072%
1073% A description of each parameter follows:
1074%
cristyfc0f64b2009-09-09 18:57:08 +00001075% o path: return the Ghostscript executable path here.
cristy3ed852e2009-09-05 21:47:34 +00001076%
cristyb32b90a2009-09-07 21:45:48 +00001077% o length: length of buffer.
cristy3ed852e2009-09-05 21:47:34 +00001078%
1079*/
1080MagickExport int NTGhostscriptEXE(char *path,int length)
1081{
cristy4c11ed82009-09-11 03:36:46 +00001082 register char
cristy3ed852e2009-09-05 21:47:34 +00001083 *p;
1084
cristyfc0f64b2009-09-09 18:57:08 +00001085 static char
cristy4c11ed82009-09-11 03:36:46 +00001086 program[MaxTextExtent] = { "" };
cristy3ed852e2009-09-05 21:47:34 +00001087
cristyb32b90a2009-09-07 21:45:48 +00001088 (void) CopyMagickString(path,"gswin32c.exe",length);
cristy4c11ed82009-09-11 03:36:46 +00001089 if ((*program == '\0') &&
1090 (NTGhostscriptGetString("GS_DLL",program,sizeof(program)) == FALSE))
cristyb32b90a2009-09-07 21:45:48 +00001091 return(FALSE);
cristy4c11ed82009-09-11 03:36:46 +00001092 p=strrchr(program,'\\');
cristy7ba1b042011-02-05 19:07:50 +00001093 if (p != (char *) NULL)
cristy4c11ed82009-09-11 03:36:46 +00001094 {
1095 p++;
1096 *p='\0';
1097 (void) ConcatenateMagickString(program,"gswin32c.exe",sizeof(program));
1098 }
1099 (void) CopyMagickString(path,program,length);
cristyb32b90a2009-09-07 21:45:48 +00001100 return(TRUE);
cristy3ed852e2009-09-05 21:47:34 +00001101}
1102
1103/*
1104%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1105% %
1106% %
1107% %
1108% N T G h o s t s c r i p t F o n t s %
1109% %
1110% %
1111% %
1112%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1113%
cristyfc0f64b2009-09-09 18:57:08 +00001114% NTGhostscriptFonts() obtains the path to the Ghostscript fonts. The method
1115% returns FALSE if it cannot determine the font path.
cristy3ed852e2009-09-05 21:47:34 +00001116%
1117% The format of the NTGhostscriptFonts method is:
1118%
cristyfc0f64b2009-09-09 18:57:08 +00001119% int NTGhostscriptFonts(char *path, int length)
cristy3ed852e2009-09-05 21:47:34 +00001120%
1121% A description of each parameter follows:
1122%
cristyfc0f64b2009-09-09 18:57:08 +00001123% o path: return the font path here.
cristy3ed852e2009-09-05 21:47:34 +00001124%
cristyfc0f64b2009-09-09 18:57:08 +00001125% o length: length of the path buffer.
cristy3ed852e2009-09-05 21:47:34 +00001126%
1127*/
1128MagickExport int NTGhostscriptFonts(char *path,int length)
1129{
1130 char
1131 buffer[MaxTextExtent],
1132 filename[MaxTextExtent];
1133
cristy3ed852e2009-09-05 21:47:34 +00001134 register char
1135 *p,
1136 *q;
1137
1138 *path='\0';
cristyfc0f64b2009-09-09 18:57:08 +00001139 if (NTGhostscriptGetString("GS_LIB",buffer,MaxTextExtent) == FALSE)
cristy3ed852e2009-09-05 21:47:34 +00001140 return(FALSE);
1141 for (p=buffer-1; p != (char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1142 {
1143 (void) CopyMagickString(path,p+1,length+1);
1144 q=strchr(path,DirectoryListSeparator);
1145 if (q != (char *) NULL)
1146 *q='\0';
cristyfc0f64b2009-09-09 18:57:08 +00001147 (void) FormatMagickString(filename,MaxTextExtent,"%s%sfonts.dir",path,
cristy3ed852e2009-09-05 21:47:34 +00001148 DirectorySeparator);
1149 if (IsPathAccessible(filename) != MagickFalse)
1150 return(TRUE);
1151 }
1152 return(FALSE);
1153}
1154
1155/*
1156%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1157% %
1158% %
1159% %
1160% N T G h o s t s c r i p t L o a d D L L %
1161% %
1162% %
1163% %
1164%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1165%
1166% NTGhostscriptLoadDLL() attempts to load the Ghostscript DLL and returns
cristyfc0f64b2009-09-09 18:57:08 +00001167% TRUE if it succeeds.
cristy3ed852e2009-09-05 21:47:34 +00001168%
1169% The format of the NTGhostscriptLoadDLL method is:
1170%
1171% int NTGhostscriptLoadDLL(void)
1172%
1173*/
1174MagickExport int NTGhostscriptLoadDLL(void)
1175{
1176 char
cristyfc0f64b2009-09-09 18:57:08 +00001177 path[MaxTextExtent];
cristy3ed852e2009-09-05 21:47:34 +00001178
cristydefb3f02009-09-10 02:18:35 +00001179 if (ghost_handle != (void *) NULL)
cristyfc0f64b2009-09-09 18:57:08 +00001180 return(TRUE);
1181 if (NTGhostscriptDLL(path,sizeof(path)) == FALSE)
1182 return(FALSE);
cristydefb3f02009-09-10 02:18:35 +00001183 ghost_handle=lt_dlopen(path);
1184 if (ghost_handle == (void *) NULL)
cristyfc0f64b2009-09-09 18:57:08 +00001185 return(FALSE);
cristydefb3f02009-09-10 02:18:35 +00001186 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1187 ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
1188 lt_dlsym(ghost_handle,"gsapi_exit");
1189 ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
1190 char **)) (lt_dlsym(ghost_handle,"gsapi_init_with_args"));
1191 ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,void *)) (
1192 lt_dlsym(ghost_handle,"gsapi_new_instance"));
1193 ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,const char *,
1194 int,int *)) (lt_dlsym(ghost_handle,"gsapi_run_string"));
1195 ghost_info.delete_instance=(void (MagickDLLCall *) (gs_main_instance *)) (
1196 lt_dlsym(ghost_handle,"gsapi_delete_instance"));
1197 if ((ghost_info.exit == NULL) || (ghost_info.init_with_args == NULL) ||
1198 (ghost_info.new_instance == NULL) || (ghost_info.run_string == NULL) ||
1199 (ghost_info.delete_instance == NULL))
cristyfc0f64b2009-09-09 18:57:08 +00001200 return(FALSE);
1201 return(TRUE);
cristy3ed852e2009-09-05 21:47:34 +00001202}
1203
1204/*
1205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1206% %
1207% %
1208% %
1209% N T G h o s t s c r i p t U n L o a d D L L %
1210% %
1211% %
1212% %
1213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1214%
cristyfc0f64b2009-09-09 18:57:08 +00001215% NTGhostscriptUnLoadDLL() unloads the Ghostscript DLL and returns TRUE if
1216% it succeeds.
cristy3ed852e2009-09-05 21:47:34 +00001217%
1218% The format of the NTGhostscriptUnLoadDLL method is:
1219%
1220% int NTGhostscriptUnLoadDLL(void)
1221%
1222*/
1223MagickExport int NTGhostscriptUnLoadDLL(void)
1224{
cristyfc0f64b2009-09-09 18:57:08 +00001225 int
1226 status;
1227
cristydefb3f02009-09-10 02:18:35 +00001228 if (ghost_handle == (void *) NULL)
cristyfc0f64b2009-09-09 18:57:08 +00001229 return(FALSE);
cristydefb3f02009-09-10 02:18:35 +00001230 status=lt_dlclose(ghost_handle);
1231 ghost_handle=(void *) NULL;
1232 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
cristyfc0f64b2009-09-09 18:57:08 +00001233 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001234}
1235
1236/*
1237%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1238% %
1239% %
1240% %
1241% N T I n i t i a l i z e L i b r a r y %
1242% %
1243% %
1244% %
1245%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1246%
1247% NTInitializeLibrary() initializes the dynamic module loading subsystem.
1248%
1249% The format of the NTInitializeLibrary method is:
1250%
1251% int NTInitializeLibrary(void)
1252%
1253*/
1254MagickExport int NTInitializeLibrary(void)
1255{
1256 return(0);
1257}
1258
1259/*
1260%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1261% %
1262% %
1263% %
1264+ N T M a p M e m o r y %
1265% %
1266% %
1267% %
1268%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1269%
1270% Mmap() emulates the Unix method of the same name.
1271%
1272% The format of the NTMapMemory method is:
1273%
1274% MagickExport void *NTMapMemory(char *address,size_t length,int protection,
1275% int access,int file,MagickOffsetType offset)
1276%
1277*/
1278MagickExport void *NTMapMemory(char *address,size_t length,int protection,
1279 int flags,int file,MagickOffsetType offset)
1280{
1281 DWORD
1282 access_mode,
1283 high_length,
1284 high_offset,
1285 low_length,
1286 low_offset,
1287 protection_mode;
1288
1289 HANDLE
1290 file_handle,
1291 map_handle;
1292
1293 void
1294 *map;
1295
cristy3b743bb2009-09-14 16:07:59 +00001296 (void) address;
cristy3ed852e2009-09-05 21:47:34 +00001297 access_mode=0;
1298 file_handle=INVALID_HANDLE_VALUE;
1299 low_length=(DWORD) (length & 0xFFFFFFFFUL);
1300 high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1301 map_handle=INVALID_HANDLE_VALUE;
1302 map=(void *) NULL;
1303 low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1304 high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1305 protection_mode=0;
1306 if (protection & PROT_WRITE)
1307 {
1308 access_mode=FILE_MAP_WRITE;
1309 if (!(flags & MAP_PRIVATE))
1310 protection_mode=PAGE_READWRITE;
1311 else
1312 {
1313 access_mode=FILE_MAP_COPY;
1314 protection_mode=PAGE_WRITECOPY;
1315 }
1316 }
1317 else
1318 if (protection & PROT_READ)
1319 {
1320 access_mode=FILE_MAP_READ;
1321 protection_mode=PAGE_READONLY;
1322 }
1323 if ((file == -1) && (flags & MAP_ANONYMOUS))
1324 file_handle=INVALID_HANDLE_VALUE;
1325 else
1326 file_handle=(HANDLE) _get_osfhandle(file);
1327 map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1328 low_length,0);
1329 if (map_handle)
1330 {
1331 map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1332 length);
1333 CloseHandle(map_handle);
1334 }
1335 if (map == (void *) NULL)
1336 return((void *) MAP_FAILED);
1337 return((void *) ((char *) map));
1338}
1339
1340/*
1341%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1342% %
1343% %
1344% %
1345% N T O p e n D i r e c t o r y %
1346% %
1347% %
1348% %
1349%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1350%
1351% NTOpenDirectory() opens the directory named by filename and associates a
1352% directory stream with it.
1353%
1354% The format of the NTOpenDirectory method is:
1355%
1356% DIR *NTOpenDirectory(const char *path)
1357%
1358% A description of each parameter follows:
1359%
1360% o entry: Specifies a pointer to a DIR structure.
1361%
1362*/
1363MagickExport DIR *NTOpenDirectory(const char *path)
1364{
1365 char
1366 file_specification[MaxTextExtent];
1367
1368 DIR
1369 *entry;
1370
1371 size_t
1372 length;
1373
1374 assert(path != (const char *) NULL);
1375 length=CopyMagickString(file_specification,path,MaxTextExtent);
1376 if (length >= MaxTextExtent)
1377 return((DIR *) NULL);
1378 length=ConcatenateMagickString(file_specification,DirectorySeparator,
1379 MaxTextExtent);
1380 if (length >= MaxTextExtent)
1381 return((DIR *) NULL);
cristy73bd4a52010-10-05 11:24:23 +00001382 entry=(DIR *) AcquireMagickMemory(sizeof(DIR));
cristy3ed852e2009-09-05 21:47:34 +00001383 if (entry != (DIR *) NULL)
1384 {
1385 entry->firsttime=TRUE;
1386 entry->hSearch=FindFirstFile(file_specification,&entry->Win32FindData);
1387 }
1388 if (entry->hSearch == INVALID_HANDLE_VALUE)
1389 {
1390 length=ConcatenateMagickString(file_specification,"\\*.*",MaxTextExtent);
1391 if (length >= MaxTextExtent)
1392 {
1393 entry=(DIR *) RelinquishMagickMemory(entry);
1394 return((DIR *) NULL);
1395 }
1396 entry->hSearch=FindFirstFile(file_specification,&entry->Win32FindData);
1397 if (entry->hSearch == INVALID_HANDLE_VALUE)
1398 {
1399 entry=(DIR *) RelinquishMagickMemory(entry);
1400 return((DIR *) NULL);
1401 }
1402 }
1403 return(entry);
1404}
1405
1406/*
1407%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1408% %
1409% %
1410% %
1411% N T O p e n L i b r a r y %
1412% %
1413% %
1414% %
1415%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1416%
1417% NTOpenLibrary() loads a dynamic module into memory and returns a handle that
1418% can be used to access the various procedures in the module.
1419%
1420% The format of the NTOpenLibrary method is:
1421%
1422% void *NTOpenLibrary(const char *filename)
1423%
1424% A description of each parameter follows:
1425%
1426% o path: Specifies a pointer to string representing dynamic module that
1427% is to be loaded.
1428%
1429*/
1430
1431static const char *GetSearchPath( void )
1432{
1433#if defined(MAGICKCORE_LTDL_DELEGATE)
1434 return(lt_dlgetsearchpath());
1435#else
1436 return(lt_slsearchpath);
1437#endif
1438}
1439
1440MagickExport void *NTOpenLibrary(const char *filename)
1441{
1442#define MaxPathElements 31
1443
1444 char
1445 buffer[MaxTextExtent];
1446
1447 int
1448 index;
1449
1450 register const char
1451 *p,
1452 *q;
1453
1454 register int
1455 i;
1456
1457 UINT
1458 mode;
1459
1460 void
1461 *handle;
1462
1463 mode=SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
1464 handle=(void *) LoadLibraryEx(filename,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1465 if ((handle != (void *) NULL) || (GetSearchPath() == (char *) NULL))
1466 {
1467 SetErrorMode(mode);
1468 return(handle);
1469 }
1470 p=(char *) GetSearchPath();
1471 index=0;
1472 while (index < MaxPathElements)
1473 {
1474 q=strchr(p,DirectoryListSeparator);
1475 if (q == (char *) NULL)
1476 {
1477 (void) CopyMagickString(buffer,p,MaxTextExtent);
1478 (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1479 (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1480 handle=(void *) LoadLibraryEx(buffer,NULL,
1481 LOAD_WITH_ALTERED_SEARCH_PATH);
1482 break;
1483 }
1484 i=q-p;
1485 (void) CopyMagickString(buffer,p,i+1);
1486 (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1487 (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1488 handle=(void *) LoadLibraryEx(buffer,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1489 if (handle != (void *) NULL)
1490 break;
1491 p=q+1;
1492 }
1493 SetErrorMode(mode);
1494 return(handle);
1495}
1496
1497/*
1498%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1499% %
1500% %
1501% %
1502% N T R e a d D i r e c t o r y %
1503% %
1504% %
1505% %
1506%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1507%
1508% NTReadDirectory() returns a pointer to a structure representing the
1509% directory entry at the current position in the directory stream to which
1510% entry refers.
1511%
1512% The format of the NTReadDirectory
1513%
1514% NTReadDirectory(entry)
1515%
1516% A description of each parameter follows:
1517%
1518% o entry: Specifies a pointer to a DIR structure.
1519%
1520*/
1521MagickExport struct dirent *NTReadDirectory(DIR *entry)
1522{
1523 int
1524 status;
1525
1526 size_t
1527 length;
1528
1529 if (entry == (DIR *) NULL)
1530 return((struct dirent *) NULL);
1531 if (!entry->firsttime)
1532 {
1533 status=FindNextFile(entry->hSearch,&entry->Win32FindData);
1534 if (status == 0)
1535 return((struct dirent *) NULL);
1536 }
1537 length=CopyMagickString(entry->file_info.d_name,
1538 entry->Win32FindData.cFileName,sizeof(entry->file_info.d_name));
1539 if (length >= sizeof(entry->file_info.d_name))
1540 return((struct dirent *) NULL);
1541 entry->firsttime=FALSE;
1542 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1543 return(&entry->file_info);
1544}
1545
1546/*
1547%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1548% %
1549% %
1550% %
1551% N T R e g i s t r y K e y L o o k u p %
1552% %
1553% %
1554% %
1555%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1556%
1557% NTRegistryKeyLookup() returns ImageMagick installation path settings
1558% stored in the Windows Registry. Path settings are specific to the
1559% installed ImageMagick version so that multiple Image Magick installations
1560% may coexist.
1561%
1562% Values are stored in the registry under a base path path similar to
1563% "HKEY_LOCAL_MACHINE/SOFTWARE\ImageMagick\5.5.7\Q:16". The provided subkey
1564% is appended to this base path to form the full key.
1565%
1566% The format of the NTRegistryKeyLookup method is:
1567%
1568% unsigned char *NTRegistryKeyLookup(const char *subkey)
1569%
1570% A description of each parameter follows:
1571%
1572% o subkey: Specifies a string that identifies the registry object.
1573% Currently supported sub-keys include: "BinPath", "ConfigurePath",
1574% "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
1575%
1576*/
1577MagickExport unsigned char *NTRegistryKeyLookup(const char *subkey)
1578{
1579 char
1580 package_key[MaxTextExtent];
1581
1582 DWORD
1583 size,
1584 type;
1585
1586 HKEY
1587 registry_key;
1588
1589 LONG
1590 status;
1591
1592 unsigned char
1593 *value;
1594
1595 /*
1596 Look-up base key.
1597 */
1598 (void) FormatMagickString(package_key,MaxTextExtent,"SOFTWARE\\%s\\%s\\Q:%d",
1599 MagickPackageName,MagickLibVersionText,MAGICKCORE_QUANTUM_DEPTH);
1600 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",package_key);
1601 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1602 status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,package_key,0,KEY_READ,&registry_key);
1603 if (status != ERROR_SUCCESS)
1604 {
1605 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1606 return((unsigned char *) NULL);
1607 }
1608 /*
1609 Look-up sub key.
1610 */
1611 size=32;
1612 value=(unsigned char *) AcquireQuantumMemory(size,sizeof(*value));
1613 if (value == (unsigned char *) NULL)
1614 {
1615 RegCloseKey(registry_key);
1616 return((unsigned char *) NULL);
1617 }
1618 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",subkey);
1619 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1620 if ((status == ERROR_MORE_DATA) && (type == REG_SZ))
1621 {
1622 value=(unsigned char *) ResizeQuantumMemory(value,size,sizeof(*value));
1623 if (value == (BYTE *) NULL)
1624 {
1625 RegCloseKey(registry_key);
1626 return((unsigned char *) NULL);
1627 }
1628 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1629 }
1630 RegCloseKey(registry_key);
1631 if ((type != REG_SZ) || (status != ERROR_SUCCESS))
1632 value=(unsigned char *) RelinquishMagickMemory(value);
1633 return((unsigned char *) value);
1634}
1635
1636/*
1637%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1638% %
1639% %
1640% %
1641% N T R e p o r t E v e n t %
1642% %
1643% %
1644% %
1645%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1646%
1647% NTReportEvent() reports an event.
1648%
1649% The format of the NTReportEvent method is:
1650%
1651% MagickBooleanType NTReportEvent(const char *event,
1652% const MagickBooleanType error)
1653%
1654% A description of each parameter follows:
1655%
1656% o event: the event.
1657%
1658% o error: MagickTrue the event is an error.
1659%
1660*/
1661MagickExport MagickBooleanType NTReportEvent(const char *event,
1662 const MagickBooleanType error)
1663{
1664 const char
1665 *events[1];
1666
1667 HANDLE
1668 handle;
1669
1670 WORD
1671 type;
1672
1673 handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
1674 if (handle == NULL)
1675 return(MagickFalse);
1676 events[0]=event;
1677 type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
1678 ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
1679 DeregisterEventSource(handle);
1680 return(MagickTrue);
1681}
1682
1683/*
1684%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1685% %
1686% %
1687% %
1688% N T R e s o u r c e T o B l o b %
1689% %
1690% %
1691% %
1692%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1693%
1694% NTResourceToBlob() returns a blob containing the contents of the resource
1695% in the current executable specified by the id parameter. This currently
1696% used to retrieve MGK files tha have been embedded into the various command
1697% line utilities.
1698%
1699% The format of the NTResourceToBlob method is:
1700%
1701% unsigned char *NTResourceToBlob(const char *id)
1702%
1703% A description of each parameter follows:
1704%
1705% o id: Specifies a string that identifies the resource.
1706%
1707*/
1708MagickExport unsigned char *NTResourceToBlob(const char *id)
1709{
1710 char
1711 path[MaxTextExtent];
1712
1713 DWORD
1714 length;
1715
1716 HGLOBAL
1717 global;
1718
1719 HMODULE
1720 handle;
1721
1722 HRSRC
1723 resource;
1724
1725 unsigned char
1726 *blob,
1727 *value;
1728
1729 assert(id != (const char *) NULL);
1730 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1731 (void) FormatMagickString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
1732 DirectorySeparator,GetClientName());
1733 if (IsPathAccessible(path) != MagickFalse)
1734 handle=GetModuleHandle(path);
1735 else
1736 handle=GetModuleHandle(0);
1737 if (!handle)
1738 return((unsigned char *) NULL);
1739 resource=FindResource(handle,id,"IMAGEMAGICK");
1740 if (!resource)
1741 return((unsigned char *) NULL);
1742 global=LoadResource(handle,resource);
1743 if (!global)
1744 return((unsigned char *) NULL);
1745 length=SizeofResource(handle,resource);
1746 value=(unsigned char *) LockResource(global);
1747 if (!value)
1748 {
1749 FreeResource(global);
1750 return((unsigned char *) NULL);
1751 }
1752 blob=(unsigned char *) AcquireQuantumMemory(length+MaxTextExtent,
1753 sizeof(*blob));
1754 if (blob != (unsigned char *) NULL)
1755 {
1756 (void) CopyMagickMemory(blob,value,length);
1757 blob[length]='\0';
1758 }
1759 UnlockResource(global);
1760 FreeResource(global);
1761 return(blob);
1762}
1763
1764/*
1765%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1766% %
1767% %
1768% %
1769% N T S e e k D i r e c t o r y %
1770% %
1771% %
1772% %
1773%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1774%
1775% NTSeekDirectory() sets the position of the next NTReadDirectory() operation
1776% on the directory stream.
1777%
1778% The format of the NTSeekDirectory method is:
1779%
cristybb503372010-05-27 20:51:26 +00001780% void NTSeekDirectory(DIR *entry,ssize_t position)
cristy3ed852e2009-09-05 21:47:34 +00001781%
1782% A description of each parameter follows:
1783%
1784% o entry: Specifies a pointer to a DIR structure.
1785%
1786% o position: specifies the position associated with the directory
1787% stream.
1788%
1789*/
cristybb503372010-05-27 20:51:26 +00001790MagickExport void NTSeekDirectory(DIR *entry,ssize_t position)
cristy3ed852e2009-09-05 21:47:34 +00001791{
cristy3b743bb2009-09-14 16:07:59 +00001792 (void) position;
cristy3ed852e2009-09-05 21:47:34 +00001793 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1794 assert(entry != (DIR *) NULL);
1795}
1796
1797/*
1798%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1799% %
1800% %
1801% %
1802% N T S e t S e a r c h P a t h %
1803% %
1804% %
1805% %
1806%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1807%
1808% NTSetSearchPath() sets the current locations that the subsystem should
1809% look at to find dynamically loadable modules.
1810%
1811% The format of the NTSetSearchPath method is:
1812%
1813% int NTSetSearchPath(const char *path)
1814%
1815% A description of each parameter follows:
1816%
1817% o path: Specifies a pointer to string representing the search path
1818% for DLL's that can be dynamically loaded.
1819%
1820*/
1821MagickExport int NTSetSearchPath(const char *path)
1822{
1823#if defined(MAGICKCORE_LTDL_DELEGATE)
1824 lt_dlsetsearchpath(path);
1825#else
1826 if (lt_slsearchpath != (char *) NULL)
1827 lt_slsearchpath=DestroyString(lt_slsearchpath);
1828 if (path != (char *) NULL)
1829 lt_slsearchpath=AcquireString(path);
1830#endif
1831 return(0);
1832}
1833
1834/*
1835%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1836% %
1837% %
1838% %
1839+ N T S y n c M e m o r y %
1840% %
1841% %
1842% %
1843%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1844%
1845% NTSyncMemory() emulates the Unix method of the same name.
1846%
1847% The format of the NTSyncMemory method is:
1848%
1849% int NTSyncMemory(void *address,size_t length,int flags)
1850%
1851% A description of each parameter follows:
1852%
1853% o address: the address of the binary large object.
1854%
1855% o length: the length of the binary large object.
1856%
1857% o flags: Option flags (ignored for Windows).
1858%
1859*/
1860MagickExport int NTSyncMemory(void *address,size_t length,int flags)
1861{
cristy3b743bb2009-09-14 16:07:59 +00001862 (void) flags;
cristy3ed852e2009-09-05 21:47:34 +00001863 if (FlushViewOfFile(address,length) == MagickFalse)
1864 return(-1);
1865 return(0);
1866}
1867
1868/*
1869%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1870% %
1871% %
1872% %
1873% N T S y s t e m C o m m a n d %
1874% %
1875% %
1876% %
1877%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1878%
1879% NTSystemCommand() executes the specified command and waits until it
1880% terminates. The returned value is the exit status of the command.
1881%
cristyb32b90a2009-09-07 21:45:48 +00001882% The format of the NTSystemCommand method is:
cristy3ed852e2009-09-05 21:47:34 +00001883%
cristy6de4bc22010-01-12 17:10:35 +00001884% int NTSystemCommand(MagickFalse,const char *command)
cristy3ed852e2009-09-05 21:47:34 +00001885%
1886% A description of each parameter follows:
1887%
1888% o command: This string is the command to execute.
1889%
1890*/
1891MagickExport int NTSystemCommand(const char *command)
1892{
1893 char
1894 local_command[MaxTextExtent];
1895
1896 DWORD
1897 child_status;
1898
1899 int
1900 status;
1901
1902 MagickBooleanType
1903 background_process;
1904
1905 PROCESS_INFORMATION
1906 process_info;
1907
1908 STARTUPINFO
1909 startup_info;
1910
1911 if (command == (char *) NULL)
1912 return(-1);
1913 GetStartupInfo(&startup_info);
1914 startup_info.dwFlags=STARTF_USESHOWWINDOW;
1915 startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
1916 (void) CopyMagickString(local_command,command,MaxTextExtent);
1917 background_process=command[strlen(command)-1] == '&' ? MagickTrue :
1918 MagickFalse;
1919 if (background_process)
1920 local_command[strlen(command)-1]='\0';
1921 if (command[strlen(command)-1] == '|')
1922 local_command[strlen(command)-1]='\0';
1923 else
1924 startup_info.wShowWindow=SW_SHOWDEFAULT;
1925 status=CreateProcess((LPCTSTR) NULL,local_command,
1926 (LPSECURITY_ATTRIBUTES) NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) FALSE,
1927 (DWORD) NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
1928 &process_info);
1929 if (status == 0)
1930 return(-1);
1931 if (background_process)
1932 return(status == 0);
1933 status=WaitForSingleObject(process_info.hProcess,INFINITE);
1934 if (status != WAIT_OBJECT_0)
1935 return(status);
1936 status=GetExitCodeProcess(process_info.hProcess,&child_status);
1937 if (status == 0)
1938 return(-1);
1939 CloseHandle(process_info.hProcess);
1940 CloseHandle(process_info.hThread);
1941 return((int) child_status);
1942}
1943
1944/*
1945%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1946% %
1947% %
1948% %
1949% N T S y s t e m C o n i f i g u r a t i o n %
1950% %
1951% %
1952% %
1953%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1954%
1955% NTSystemConfiguration() provides a way for the application to determine
1956% values for system limits or options at runtime.
1957%
1958% The format of the exit method is:
1959%
cristybb503372010-05-27 20:51:26 +00001960% ssize_t NTSystemConfiguration(int name)
cristy3ed852e2009-09-05 21:47:34 +00001961%
1962% A description of each parameter follows:
1963%
1964% o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
1965%
1966*/
cristybb503372010-05-27 20:51:26 +00001967MagickExport ssize_t NTSystemConfiguration(int name)
cristy3ed852e2009-09-05 21:47:34 +00001968{
1969 switch (name)
1970 {
1971 case _SC_PAGESIZE:
1972 {
1973 SYSTEM_INFO
1974 system_info;
1975
1976 GetSystemInfo(&system_info);
1977 return(system_info.dwPageSize);
1978 }
1979 case _SC_PHYS_PAGES:
1980 {
1981 HMODULE
1982 handle;
1983
1984 LPFNDLLFUNC2
1985 module;
1986
1987 NTMEMORYSTATUSEX
1988 status;
1989
1990 SYSTEM_INFO
1991 system_info;
1992
1993 handle=GetModuleHandle("kernel32.dll");
1994 if (handle == (HMODULE) NULL)
1995 return(0L);
1996 GetSystemInfo(&system_info);
1997 module=(LPFNDLLFUNC2) NTGetLibrarySymbol(handle,"GlobalMemoryStatusEx");
1998 if (module == (LPFNDLLFUNC2) NULL)
1999 {
2000 MEMORYSTATUS
2001 status;
2002
2003 GlobalMemoryStatus(&status);
cristybb503372010-05-27 20:51:26 +00002004 return((ssize_t) status.dwTotalPhys/system_info.dwPageSize);
cristy3ed852e2009-09-05 21:47:34 +00002005 }
2006 status.dwLength=sizeof(status);
2007 if (module(&status) == 0)
2008 return(0L);
cristybb503372010-05-27 20:51:26 +00002009 return((ssize_t) status.ullTotalPhys/system_info.dwPageSize);
cristy3ed852e2009-09-05 21:47:34 +00002010 }
2011 case _SC_OPEN_MAX:
2012 return(2048);
2013 default:
2014 break;
2015 }
2016 return(-1);
2017}
2018
2019/*
2020%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2021% %
2022% %
2023% %
2024% N T T e l l D i r e c t o r y %
2025% %
2026% %
2027% %
2028%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2029%
2030% NTTellDirectory() returns the current location associated with the named
2031% directory stream.
2032%
2033% The format of the NTTellDirectory method is:
2034%
cristybb503372010-05-27 20:51:26 +00002035% ssize_t NTTellDirectory(DIR *entry)
cristy3ed852e2009-09-05 21:47:34 +00002036%
2037% A description of each parameter follows:
2038%
2039% o entry: Specifies a pointer to a DIR structure.
2040%
2041*/
cristybb503372010-05-27 20:51:26 +00002042MagickExport ssize_t NTTellDirectory(DIR *entry)
cristy3ed852e2009-09-05 21:47:34 +00002043{
2044 assert(entry != (DIR *) NULL);
2045 return(0);
2046}
2047
2048/*
2049%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2050% %
2051% %
2052% %
2053% N T T r u n c a t e F i l e %
2054% %
2055% %
2056% %
2057%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2058%
2059% NTTruncateFile() truncates a file to a specified length.
2060%
2061% The format of the NTTruncateFile method is:
2062%
2063% int NTTruncateFile(int file,off_t length)
2064%
2065% A description of each parameter follows:
2066%
2067% o file: the file.
2068%
2069% o length: the file length.
2070%
2071*/
2072MagickExport int NTTruncateFile(int file,off_t length)
2073{
2074 DWORD
2075 file_pointer;
2076
cristyfeb262e2010-06-04 22:57:35 +00002077 long
cristy3ed852e2009-09-05 21:47:34 +00002078 file_handle,
2079 high,
2080 low;
2081
2082 file_handle=_get_osfhandle(file);
2083 if (file_handle == -1L)
2084 return(-1);
cristyfeb262e2010-06-04 22:57:35 +00002085 low=(long) (length & 0xffffffffUL);
2086 high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
cristy3ed852e2009-09-05 21:47:34 +00002087 file_pointer=SetFilePointer((HANDLE) file_handle,low,&high,FILE_BEGIN);
2088 if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2089 return(-1);
2090 if (SetEndOfFile((HANDLE) file_handle) == 0)
2091 return(-1);
2092 return(0);
2093}
2094
2095/*
2096%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2097% %
2098% %
2099% %
2100+ N T U n m a p M e m o r y %
2101% %
2102% %
2103% %
2104%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2105%
2106% NTUnmapMemory() emulates the Unix munmap method.
2107%
2108% The format of the NTUnmapMemory method is:
2109%
2110% int NTUnmapMemory(void *map,size_t length)
2111%
2112% A description of each parameter follows:
2113%
2114% o map: the address of the binary large object.
2115%
2116% o length: the length of the binary large object.
2117%
2118*/
2119MagickExport int NTUnmapMemory(void *map,size_t length)
2120{
cristy3b743bb2009-09-14 16:07:59 +00002121 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00002122 if (UnmapViewOfFile(map) == 0)
2123 return(-1);
2124 return(0);
2125}
2126
2127/*
2128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2129% %
2130% %
2131% %
2132% N T U s e r T i m e %
2133% %
2134% %
2135% %
2136%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2137%
2138% NTUserTime() returns the total time the process has been scheduled (e.g.
2139% seconds) since the last call to StartTimer().
2140%
2141% The format of the UserTime method is:
2142%
2143% double NTUserTime(void)
2144%
2145*/
2146MagickExport double NTUserTime(void)
2147{
2148 DWORD
2149 status;
2150
2151 FILETIME
2152 create_time,
2153 exit_time;
2154
2155 OSVERSIONINFO
2156 OsVersionInfo;
2157
2158 union
2159 {
2160 FILETIME
2161 filetime;
2162
2163 __int64
2164 filetime64;
2165 } kernel_time;
2166
2167 union
2168 {
2169 FILETIME
2170 filetime;
2171
2172 __int64
2173 filetime64;
2174 } user_time;
2175
2176 OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
2177 GetVersionEx(&OsVersionInfo);
2178 if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
2179 return(NTElapsedTime());
2180 status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
2181 &kernel_time.filetime,&user_time.filetime);
2182 if (status != TRUE)
2183 return(0.0);
2184 return((double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
2185}
2186
2187/*
2188%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2189% %
2190% %
2191% %
2192% N T W a r n i n g H a n d l e r %
2193% %
2194% %
2195% %
2196%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2197%
2198% NTWarningHandler() displays a warning reason.
2199%
2200% The format of the NTWarningHandler method is:
2201%
cristy3b743bb2009-09-14 16:07:59 +00002202% void NTWarningHandler(const ExceptionType severity,const char *reason,
cristy3ed852e2009-09-05 21:47:34 +00002203% const char *description)
2204%
2205% A description of each parameter follows:
2206%
cristy3b743bb2009-09-14 16:07:59 +00002207% o severity: Specifies the numeric warning category.
cristy3ed852e2009-09-05 21:47:34 +00002208%
2209% o reason: Specifies the reason to display before terminating the
2210% program.
2211%
2212% o description: Specifies any description to the reason.
2213%
2214*/
cristy3b743bb2009-09-14 16:07:59 +00002215MagickExport void NTWarningHandler(const ExceptionType severity,
cristy3ed852e2009-09-05 21:47:34 +00002216 const char *reason,const char *description)
2217{
2218 char
2219 buffer[2*MaxTextExtent];
2220
cristy3b743bb2009-09-14 16:07:59 +00002221 (void) severity;
cristy3ed852e2009-09-05 21:47:34 +00002222 if (reason == (char *) NULL)
2223 return;
2224 if (description == (char *) NULL)
2225 (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s.\n",GetClientName(),
2226 reason);
2227 else
2228 (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s (%s).\n",
2229 GetClientName(),reason,description);
2230 (void) MessageBox(NULL,buffer,"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2231 MB_SETFOREGROUND | MB_ICONINFORMATION);
2232}
2233#endif