blob: 54849f03ba5b392cd019b76f5e8cf7c28bf588c5 [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% %
20% Copyright 1999-2009 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% 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"
42#if defined(__WINDOWS__)
43#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*/
85#if !defined(__WINDOWS__)
86extern "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
239/*
240%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241% %
242% %
243% %
244% I s W i n d o w s 9 5 %
245% %
246% %
247% %
248%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249%
250% IsWindows95() returns true if the system is Windows 95.
251%
252% The format of the IsWindows95 method is:
253%
254% int IsWindows95()
255%
256*/
257MagickExport int IsWindows95()
258{
259 OSVERSIONINFO
260 version_info;
261
262 version_info.dwOSVersionInfoSize=sizeof(version_info);
263 if (GetVersionEx(&version_info) &&
264 (version_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS))
265 return(1);
266 return(0);
267}
268
269/*
270%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271% %
272% %
273% %
274% N T C l o s e D i r e c t o r y %
275% %
276% %
277% %
278%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279%
280% NTCloseDirectory() closes the named directory stream and frees the DIR
281% structure.
282%
283% The format of the NTCloseDirectory method is:
284%
285% int NTCloseDirectory(DIR *entry)
286%
287% A description of each parameter follows:
288%
289% o entry: Specifies a pointer to a DIR structure.
290%
291*/
292MagickExport int NTCloseDirectory(DIR *entry)
293{
294 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
295 assert(entry != (DIR *) NULL);
296 FindClose(entry->hSearch);
297 entry=(DIR *) RelinquishMagickMemory(entry);
298 return(0);
299}
300
301/*
302%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303% %
304% %
305% %
306% N T C l o s e L i b r a r y %
307% %
308% %
309% %
310%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311%
312% NTCloseLibrary() unloads the module associated with the passed handle.
313%
314% The format of the NTCloseLibrary method is:
315%
316% void NTCloseLibrary(void *handle)
317%
318% A description of each parameter follows:
319%
320% o handle: Specifies a handle to a previously loaded dynamic module.
321%
322*/
323MagickExport int NTCloseLibrary(void *handle)
324{
325 if (IsWindows95())
326 return(FreeLibrary((HINSTANCE) handle));
327 return(!(FreeLibrary((HINSTANCE) handle)));
328}
329
330/*
331%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332% %
333% %
334% %
335% N T C o n t r o l H a n d l e r %
336% %
337% %
338% %
339%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
340%
341% NTControlHandler() registers a control handler that is activated when, for
342% example, a ctrl-c is received.
343%
344% The format of the NTControlHandler method is:
345%
346% int NTControlHandler(void)
347%
348*/
349
350static BOOL ControlHandler(DWORD type)
351{
352 AsynchronousDestroyMagickResources();
353 return(FALSE);
354}
355
356MagickExport int NTControlHandler(void)
357{
358 return(SetConsoleCtrlHandler((PHANDLER_ROUTINE) ControlHandler,TRUE));
359}
360
361/*
362%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
363% %
364% %
365% %
366% N T E l a p s e d T i m e %
367% %
368% %
369% %
370%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
371%
372% NTElapsedTime() returns the elapsed time (in seconds) since the last call to
373% StartTimer().
374%
375% The format of the ElapsedTime method is:
376%
377% double NTElapsedTime(void)
378%
379*/
380MagickExport double NTElapsedTime(void)
381{
382 union
383 {
384 FILETIME
385 filetime;
386
387 __int64
388 filetime64;
389 } elapsed_time;
390
391 SYSTEMTIME
392 system_time;
393
394 GetSystemTime(&system_time);
395 SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
396 return((double) 1.0e-7*elapsed_time.filetime64);
397}
398
399/*
400%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401% %
402% %
403% %
404+ N T E r r o r H a n d l e r %
405% %
406% %
407% %
408%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409%
410% NTErrorHandler() displays an error reason and then terminates the program.
411%
412% The format of the NTErrorHandler method is:
413%
414% void NTErrorHandler(const ExceptionType error,const char *reason,
415% const char *description)
416%
417% A description of each parameter follows:
418%
419% o error: Specifies the numeric error category.
420%
421% o reason: Specifies the reason to display before terminating the
422% program.
423%
424% o description: Specifies any description to the reason.
425%
426*/
427MagickExport void NTErrorHandler(const ExceptionType error,const char *reason,
428 const char *description)
429{
430 char
431 buffer[3*MaxTextExtent],
432 *message;
433
434 if (reason == (char *) NULL)
435 {
436 MagickCoreTerminus();
437 exit(0);
438 }
439 message=GetExceptionMessage(errno);
440 if ((description != (char *) NULL) && errno)
441 (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s (%s) [%s].\n",
442 GetClientName(),reason,description,message);
443 else
444 if (description != (char *) NULL)
445 (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s (%s).\n",
446 GetClientName(),reason,description);
447 else
448 if (errno != 0)
449 (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s [%s].\n",
450 GetClientName(),reason,message);
451 else
452 (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s.\n",
453 GetClientName(),reason);
454 message=DestroyString(message);
455 (void) MessageBox(NULL,buffer,"ImageMagick Exception",MB_OK | MB_TASKMODAL |
456 MB_SETFOREGROUND | MB_ICONEXCLAMATION);
457 MagickCoreTerminus();
458 exit(0);
459}
460
461/*
462%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463% %
464% %
465% %
466% N T E x i t L i b r a r y %
467% %
468% %
469% %
470%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
471%
472% NTExitLibrary() exits the dynamic module loading subsystem.
473%
474% The format of the NTExitLibrary method is:
475%
476% int NTExitLibrary(void)
477%
478*/
479MagickExport int NTExitLibrary(void)
480{
481 return(0);
482}
483
484/*
485%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486% %
487% %
488% %
489% N T G a t h e r R a n d o m D a t a %
490% %
491% %
492% %
493%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
494%
495% NTGatherRandomData() gathers random data and returns it.
496%
497% The format of the GatherRandomData method is:
498%
499% MagickBooleanType NTGatherRandomData(const size_t length,
500% unsigned char *random)
501%
502% A description of each parameter follows:
503%
504% length: the length of random data buffer
505%
506% random: the random data is returned here.
507%
508*/
509MagickExport MagickBooleanType NTGatherRandomData(const size_t length,
510 unsigned char *random)
511{
512#if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER) && (_MSC_VER > 1200)
513 HCRYPTPROV
514 handle;
515
516 int
517 status;
518
519 handle=(HCRYPTPROV) NULL;
520 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
521 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
522 if (status == 0)
523 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
524 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
525 if (status == 0)
526 return(MagickFalse);
527 status=CryptGenRandom(handle,(DWORD) length,random);
528 if (status == 0)
529 {
530 status=CryptReleaseContext(handle,0);
531 return(MagickFalse);
532 }
533 status=CryptReleaseContext(handle,0);
534 if (status == 0)
535 return(MagickFalse);
536#endif
537 return(MagickTrue);
538}
539
540/*
541%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
542% %
543% %
544% %
545% N T G e t E x e c u t i o n P a t h %
546% %
547% %
548% %
549%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
550%
551% NTGetExecutionPath() returns the execution path of a program.
552%
553% The format of the GetExecutionPath method is:
554%
555% MagickBooleanType NTGetExecutionPath(char *path,const size_t extent)
556%
557% A description of each parameter follows:
558%
559% o path: the pathname of the executable that started the process.
560%
561% o extent: the maximum extent of the path.
562%
563*/
564MagickExport MagickBooleanType NTGetExecutionPath(char *path,
565 const size_t extent)
566{
567 GetModuleFileName(0,path,(DWORD) extent);
568 return(MagickTrue);
569}
570
571/*
572%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
573% %
574% %
575% %
576% N T G e t L a s t E r r o r %
577% %
578% %
579% %
580%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
581%
582% NTGetLastError() returns the last error that occurred.
583%
584% The format of the NTGetLastError method is:
585%
586% char *NTGetLastError(void)
587%
588*/
589char *NTGetLastError(void)
590{
591 char
592 *reason;
593
594 int
595 status;
596
597 LPVOID
598 buffer;
599
600 status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
601 FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),
602 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
603 if (!status)
604 reason=AcquireString("An unknown error occurred");
605 else
606 {
607 reason=AcquireString((const char *) buffer);
608 LocalFree(buffer);
609 }
610 return(reason);
611}
612
613/*
614%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
615% %
616% %
617% %
618% N T G e t L i b r a r y E r r o r %
619% %
620% %
621% %
622%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
623%
624% Lt_dlerror() returns a pointer to a string describing the last error
625% associated with a lt_dl method. Note that this function is not thread
626% safe so it should only be used under the protection of a lock.
627%
628% The format of the NTGetLibraryError method is:
629%
630% const char *NTGetLibraryError(void)
631%
632*/
633MagickExport const char *NTGetLibraryError(void)
634{
635 static char
636 last_error[MaxTextExtent];
637
638 char
639 *error;
640
641 *last_error='\0';
642 error=NTGetLastError();
643 if (error)
644 (void) CopyMagickString(last_error,error,MaxTextExtent);
645 error=DestroyString(error);
646 return(last_error);
647}
648
649/*
650%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651% %
652% %
653% %
654% N T G e t L i b r a r y S y m b o l %
655% %
656% %
657% %
658%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
659%
660% NTGetLibrarySymbol() retrieve the procedure address of the method
661% specified by the passed character string.
662%
663% The format of the NTGetLibrarySymbol method is:
664%
665% void *NTGetLibrarySymbol(void *handle,const char *name)
666%
667% A description of each parameter follows:
668%
669% o handle: Specifies a handle to the previously loaded dynamic module.
670%
671% o name: Specifies the procedure entry point to be returned.
672%
673*/
674void *NTGetLibrarySymbol(void *handle,const char *name)
675{
676 LPFNDLLFUNC1
677 lpfnDllFunc1;
678
679 lpfnDllFunc1=(LPFNDLLFUNC1) GetProcAddress((HINSTANCE) handle,name);
680 if (!lpfnDllFunc1)
681 return((void *) NULL);
682 return((void *) lpfnDllFunc1);
683}
684
685/*
686%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
687% %
688% %
689% %
690% N T G e t M o d u l e P a t h %
691% %
692% %
693% %
694%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
695%
696% NTGetModulePath() returns the path of the specified module.
697%
698% The format of the GetModulePath method is:
699%
700% MagickBooleanType NTGetModulePath(const char *module,char *path)
701%
702% A description of each parameter follows:
703%
704% modith: the module name.
705%
706% path: the module path is returned here.
707%
708*/
709MagickExport MagickBooleanType NTGetModulePath(const char *module,char *path)
710{
711 char
712 module_path[MaxTextExtent];
713
714 HMODULE
715 handle;
716
717 long
718 length;
719
720 *path='\0';
721 handle=GetModuleHandle(module);
722 if (handle == (HMODULE) NULL)
723 return(MagickFalse);
724 length=GetModuleFileName(handle,module_path,MaxTextExtent);
725 if (length != 0)
726 GetPathComponent(module_path,HeadPath,path);
727 return(MagickTrue);
728}
729
730/*
731%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732% %
733% %
734% %
735% N T G h o s t s c r i p t D L L %
736% %
737% %
738% %
739%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
740%
cristydefb3f02009-09-10 02:18:35 +0000741% NTGhostscriptDLL() returns the path to the most recent Ghostscript version
742% DLL. The method returns TRUE on success otherwise FALSE.
cristy3ed852e2009-09-05 21:47:34 +0000743%
744% The format of the NTGhostscriptDLL method is:
745%
cristyfc0f64b2009-09-09 18:57:08 +0000746% int NTGhostscriptDLL(char *path,int length)
cristy3ed852e2009-09-05 21:47:34 +0000747%
748% A description of each parameter follows:
749%
cristyfc0f64b2009-09-09 18:57:08 +0000750% o path: return the Ghostscript DLL path here.
cristy3ed852e2009-09-05 21:47:34 +0000751%
cristyfc0f64b2009-09-09 18:57:08 +0000752% o length: the buffer length.
cristy3ed852e2009-09-05 21:47:34 +0000753%
754*/
755
cristyfc0f64b2009-09-09 18:57:08 +0000756static int NTGetRegistryValue(HKEY root,const char *key,const char *name,
757 char *value,int *length)
cristy3ed852e2009-09-05 21:47:34 +0000758{
cristyfc0f64b2009-09-09 18:57:08 +0000759 BYTE
760 byte,
cristy3ed852e2009-09-05 21:47:34 +0000761 *p;
762
cristyfc0f64b2009-09-09 18:57:08 +0000763 DWORD
764 extent,
765 type;
cristy3ed852e2009-09-05 21:47:34 +0000766
cristy3ed852e2009-09-05 21:47:34 +0000767 HKEY
768 hkey;
769
cristy3ed852e2009-09-05 21:47:34 +0000770 LONG
cristyfc0f64b2009-09-09 18:57:08 +0000771 status;
cristy3ed852e2009-09-05 21:47:34 +0000772
cristyfc0f64b2009-09-09 18:57:08 +0000773 /*
cristydefb3f02009-09-10 02:18:35 +0000774 Get a registry value: key = root\\key, named value = name.
775 */
cristyfc0f64b2009-09-09 18:57:08 +0000776 if (RegOpenKeyExA(root,key,0,KEY_READ,&hkey) != ERROR_SUCCESS)
777 return(1); /* no match */
778 p=(BYTE *) value;
779 type=REG_SZ;
780 extent=(*length);
781 if (p == (BYTE *) NULL)
cristydefb3f02009-09-10 02:18:35 +0000782 p=(&byte); /* ERROR_MORE_DATA only if value is NULL */
cristyfc0f64b2009-09-09 18:57:08 +0000783 status=RegQueryValueExA(hkey,(char *) name,0,&type,p,&extent);
784 RegCloseKey(hkey);
785 if (status == ERROR_SUCCESS)
cristy3ed852e2009-09-05 21:47:34 +0000786 {
cristyfc0f64b2009-09-09 18:57:08 +0000787 *length=extent;
788 return(0); /* return the match */
789 }
790 if (status == ERROR_MORE_DATA)
791 {
792 *length=extent;
cristydefb3f02009-09-10 02:18:35 +0000793 return(-1); /* buffer not large enough */
cristy3ed852e2009-09-05 21:47:34 +0000794 }
795 return(1); /* not found */
796}
797
cristydefb3f02009-09-10 02:18:35 +0000798static int NTLocateGhostscript(const char **product_family,int *major_version,
cristyfc0f64b2009-09-09 18:57:08 +0000799 int *minor_version)
cristy3ed852e2009-09-05 21:47:34 +0000800{
cristyfc0f64b2009-09-09 18:57:08 +0000801 int
802 i;
cristy3ed852e2009-09-05 21:47:34 +0000803
cristyfc0f64b2009-09-09 18:57:08 +0000804 MagickBooleanType
805 status;
806
807 static const char
808 *products[4] =
809 {
810 "GPL Ghostscript",
811 "GNU Ghostscript",
812 "AFPL Ghostscript",
cristydefb3f02009-09-10 02:18:35 +0000813 "Aladdin Ghostscript"
cristyfc0f64b2009-09-09 18:57:08 +0000814 };
815
816 /*
817 Find the most recent version of Ghostscript.
818 */
819 status=FALSE;
820 *product_family=NULL;
821 *major_version=5;
822 *minor_version=49; /* min version of Ghostscript is 5.50 */
823 for (i=0; i < (sizeof(products)/sizeof(products[0])); i++)
824 {
825 char
826 key[MaxTextExtent];
827
828 HKEY
829 hkey,
830 root;
831
832 (void) FormatMagickString(key,MaxTextExtent,"SOFTWARE\\%s",products[i]);
833 root=HKEY_LOCAL_MACHINE;
834 if (RegOpenKeyExA(root,key,0,KEY_READ,&hkey) == ERROR_SUCCESS)
835 {
836 DWORD
837 extent;
838
839 int
840 j;
841
842 /*
843 Now enumerate the keys.
844 */
845 extent=sizeof(key)/sizeof(char);
846 for (j=0; RegEnumKeyA(hkey,j,key,extent) == ERROR_SUCCESS; j++)
847 {
848 int
849 major,
850 minor;
851
852 major=0;
853 minor=0;
854 if (sscanf(key,"%d.%d",&major,&minor) != 2)
855 continue;
856 if ((major > *major_version) || ((major == *major_version) &&
857 (minor > *minor_version)))
858 {
859 *product_family=products[i];
860 *major_version=major;
861 *minor_version=minor;
862 status=MagickTrue;
863 }
864 }
865 }
866 }
867 if (status == MagickFalse)
868 {
869 *major_version=0;
870 *minor_version=0;
871 }
872 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"Ghostscript (%s) "
873 "version %d.%02d",*product_family,*major_version,*minor_version);
874 return(status);
875}
876
877static int NTGhostscriptGetString(const char *name,char *value,
878 const size_t length)
879{
cristy3ed852e2009-09-05 21:47:34 +0000880 char
cristy3ed852e2009-09-05 21:47:34 +0000881 key[MaxTextExtent];
882
883 int
cristyfc0f64b2009-09-09 18:57:08 +0000884 i,
885 extent;
cristydefb3f02009-09-10 02:18:35 +0000886
cristyfc0f64b2009-09-09 18:57:08 +0000887 static const char
cristydefb3f02009-09-10 02:18:35 +0000888 *product_family = (const char *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000889
cristyfc0f64b2009-09-09 18:57:08 +0000890 static int
891 major_version=0,
892 minor_version=0;
cristy3ed852e2009-09-05 21:47:34 +0000893
cristyfc0f64b2009-09-09 18:57:08 +0000894 struct
895 {
896 const HKEY
897 hkey;
cristy3ed852e2009-09-05 21:47:34 +0000898
cristyfc0f64b2009-09-09 18:57:08 +0000899 const char
900 *name;
901 }
902 hkeys[2] =
903 {
904 { HKEY_CURRENT_USER, "HKEY_CURRENT_USER" },
905 { HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" }
906 };
907
908 /*
909 Get a string from the installed Ghostscript.
910 */
cristydefb3f02009-09-10 02:18:35 +0000911 *value='\0';
cristyfc0f64b2009-09-09 18:57:08 +0000912 if (product_family == NULL)
cristydefb3f02009-09-10 02:18:35 +0000913 (void) NTLocateGhostscript(&product_family,&major_version,&minor_version);
cristyfc0f64b2009-09-09 18:57:08 +0000914 if (product_family == NULL)
915 return(FALSE);
916 (void) FormatMagickString(key,MaxTextExtent,"SOFTWARE\\%s\\%d.%02d",
917 product_family,major_version,minor_version);
918 for (i=0; i < sizeof(hkeys)/sizeof(hkeys[0]); i++)
919 {
920 extent=length;
921 if (NTGetRegistryValue(hkeys[i].hkey,key,name,value,&extent) == 0)
922 {
923 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
924 "registry: \"%s\\%s\\%s\"=\"%s\"",hkeys[i].name,key,name,value);
925 return(TRUE);
926 }
927 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
928 "registry: \"%s\\%s\\%s\" (failed)",hkeys[i].name,key,name);
929 }
cristy3ed852e2009-09-05 21:47:34 +0000930 return(FALSE);
931}
932
cristyfc0f64b2009-09-09 18:57:08 +0000933MagickExport int NTGhostscriptDLL(char *path,int length)
cristy3ed852e2009-09-05 21:47:34 +0000934{
cristy3ed852e2009-09-05 21:47:34 +0000935 char
cristyfc0f64b2009-09-09 18:57:08 +0000936 dll_path[MaxTextExtent];
cristy3ed852e2009-09-05 21:47:34 +0000937
938 *path='\0';
cristyfc0f64b2009-09-09 18:57:08 +0000939 if (NTGhostscriptGetString("GS_DLL",dll_path,sizeof(dll_path)) == 0)
940 return(FALSE);
941 (void) CopyMagickString(path,dll_path,length);
cristy3ed852e2009-09-05 21:47:34 +0000942 return(TRUE);
943}
944
945/*
946%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
947% %
948% %
949% %
950% N T G h o s t s c r i p t D L L V e c t o r s %
951% %
952% %
953% %
954%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
955%
cristydefb3f02009-09-10 02:18:35 +0000956% NTGhostscriptDLLVectors() returns a GhostInfo structure that includes
957% function vectors to invoke Ghostscript DLL functions. A null pointer is
958% returned if there is an error when loading the DLL or retrieving the
959% function vectors.
cristy3ed852e2009-09-05 21:47:34 +0000960%
961% The format of the NTGhostscriptDLLVectors method is:
962%
cristydefb3f02009-09-10 02:18:35 +0000963% const GhostInfo *NTGhostscriptDLLVectors(void)
cristy3ed852e2009-09-05 21:47:34 +0000964%
965*/
cristydefb3f02009-09-10 02:18:35 +0000966MagickExport const GhostInfo *NTGhostscriptDLLVectors(void)
cristy3ed852e2009-09-05 21:47:34 +0000967{
cristyfc0f64b2009-09-09 18:57:08 +0000968 if (NTGhostscriptLoadDLL() == FALSE)
cristydefb3f02009-09-10 02:18:35 +0000969 return((GhostInfo *) NULL);
970 return(&ghost_info);
cristy3ed852e2009-09-05 21:47:34 +0000971}
972
973/*
974%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
975% %
976% %
977% %
978% N T G h o s t s c r i p t E X E %
979% %
980% %
981% %
982%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
983%
984% NTGhostscriptEXE() obtains the path to the latest Ghostscript executable.
cristyfc0f64b2009-09-09 18:57:08 +0000985% The method returns FALSE if a full path value is not obtained and returns
986% a default path of gswin32c.exe.
cristy3ed852e2009-09-05 21:47:34 +0000987%
988% The format of the NTGhostscriptEXE method is:
989%
cristyfc0f64b2009-09-09 18:57:08 +0000990% int NTGhostscriptEXE(char *path,int length)
cristy3ed852e2009-09-05 21:47:34 +0000991%
992% A description of each parameter follows:
993%
cristyfc0f64b2009-09-09 18:57:08 +0000994% o path: return the Ghostscript executable path here.
cristy3ed852e2009-09-05 21:47:34 +0000995%
cristyb32b90a2009-09-07 21:45:48 +0000996% o length: length of buffer.
cristy3ed852e2009-09-05 21:47:34 +0000997%
998*/
999MagickExport int NTGhostscriptEXE(char *path,int length)
1000{
cristy3ed852e2009-09-05 21:47:34 +00001001 char
cristy3ed852e2009-09-05 21:47:34 +00001002 *p;
1003
cristyfc0f64b2009-09-09 18:57:08 +00001004 static char
1005 executable[MaxTextExtent] = { "" };
cristy3ed852e2009-09-05 21:47:34 +00001006
cristyfc0f64b2009-09-09 18:57:08 +00001007 if (*executable != '\0')
1008 {
1009 (void) CopyMagickString(path,executable,length);
1010 return(TRUE);
1011 }
cristyb32b90a2009-09-07 21:45:48 +00001012 (void) CopyMagickString(path,"gswin32c.exe",length);
cristyfc0f64b2009-09-09 18:57:08 +00001013 if (NTGhostscriptGetString("GS_DLL",executable,sizeof(executable)) == FALSE)
cristyb32b90a2009-09-07 21:45:48 +00001014 return(FALSE);
cristyfc0f64b2009-09-09 18:57:08 +00001015 p=strrchr(executable, '\\');
cristyb32b90a2009-09-07 21:45:48 +00001016 if (p == (char *) NULL)
1017 return(FALSE);
1018 p++;
1019 *p='\0';
cristyfc0f64b2009-09-09 18:57:08 +00001020 (void) CopyMagickString(p,path,sizeof(executable)-strlen(executable));
1021 (void) CopyMagickString(path,executable,length);
cristyb32b90a2009-09-07 21:45:48 +00001022 return(TRUE);
cristy3ed852e2009-09-05 21:47:34 +00001023}
1024
1025/*
1026%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1027% %
1028% %
1029% %
1030% N T G h o s t s c r i p t F o n t s %
1031% %
1032% %
1033% %
1034%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1035%
cristyfc0f64b2009-09-09 18:57:08 +00001036% NTGhostscriptFonts() obtains the path to the Ghostscript fonts. The method
1037% returns FALSE if it cannot determine the font path.
cristy3ed852e2009-09-05 21:47:34 +00001038%
1039% The format of the NTGhostscriptFonts method is:
1040%
cristyfc0f64b2009-09-09 18:57:08 +00001041% int NTGhostscriptFonts(char *path, int length)
cristy3ed852e2009-09-05 21:47:34 +00001042%
1043% A description of each parameter follows:
1044%
cristyfc0f64b2009-09-09 18:57:08 +00001045% o path: return the font path here.
cristy3ed852e2009-09-05 21:47:34 +00001046%
cristyfc0f64b2009-09-09 18:57:08 +00001047% o length: length of the path buffer.
cristy3ed852e2009-09-05 21:47:34 +00001048%
1049*/
1050MagickExport int NTGhostscriptFonts(char *path,int length)
1051{
1052 char
1053 buffer[MaxTextExtent],
1054 filename[MaxTextExtent];
1055
cristy3ed852e2009-09-05 21:47:34 +00001056 register char
1057 *p,
1058 *q;
1059
1060 *path='\0';
cristyfc0f64b2009-09-09 18:57:08 +00001061 if (NTGhostscriptGetString("GS_LIB",buffer,MaxTextExtent) == FALSE)
cristy3ed852e2009-09-05 21:47:34 +00001062 return(FALSE);
1063 for (p=buffer-1; p != (char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1064 {
1065 (void) CopyMagickString(path,p+1,length+1);
1066 q=strchr(path,DirectoryListSeparator);
1067 if (q != (char *) NULL)
1068 *q='\0';
cristyfc0f64b2009-09-09 18:57:08 +00001069 (void) FormatMagickString(filename,MaxTextExtent,"%s%sfonts.dir",path,
cristy3ed852e2009-09-05 21:47:34 +00001070 DirectorySeparator);
1071 if (IsPathAccessible(filename) != MagickFalse)
1072 return(TRUE);
1073 }
1074 return(FALSE);
1075}
1076
1077/*
1078%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1079% %
1080% %
1081% %
1082% N T G h o s t s c r i p t L o a d D L L %
1083% %
1084% %
1085% %
1086%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1087%
1088% NTGhostscriptLoadDLL() attempts to load the Ghostscript DLL and returns
cristyfc0f64b2009-09-09 18:57:08 +00001089% TRUE if it succeeds.
cristy3ed852e2009-09-05 21:47:34 +00001090%
1091% The format of the NTGhostscriptLoadDLL method is:
1092%
1093% int NTGhostscriptLoadDLL(void)
1094%
1095*/
1096MagickExport int NTGhostscriptLoadDLL(void)
1097{
1098 char
cristyfc0f64b2009-09-09 18:57:08 +00001099 path[MaxTextExtent];
cristy3ed852e2009-09-05 21:47:34 +00001100
cristydefb3f02009-09-10 02:18:35 +00001101 if (ghost_handle != (void *) NULL)
cristyfc0f64b2009-09-09 18:57:08 +00001102 return(TRUE);
1103 if (NTGhostscriptDLL(path,sizeof(path)) == FALSE)
1104 return(FALSE);
cristydefb3f02009-09-10 02:18:35 +00001105 ghost_handle=lt_dlopen(path);
1106 if (ghost_handle == (void *) NULL)
cristyfc0f64b2009-09-09 18:57:08 +00001107 return(FALSE);
cristydefb3f02009-09-10 02:18:35 +00001108 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1109 ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
1110 lt_dlsym(ghost_handle,"gsapi_exit");
1111 ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
1112 char **)) (lt_dlsym(ghost_handle,"gsapi_init_with_args"));
1113 ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,void *)) (
1114 lt_dlsym(ghost_handle,"gsapi_new_instance"));
1115 ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,const char *,
1116 int,int *)) (lt_dlsym(ghost_handle,"gsapi_run_string"));
1117 ghost_info.delete_instance=(void (MagickDLLCall *) (gs_main_instance *)) (
1118 lt_dlsym(ghost_handle,"gsapi_delete_instance"));
1119 if ((ghost_info.exit == NULL) || (ghost_info.init_with_args == NULL) ||
1120 (ghost_info.new_instance == NULL) || (ghost_info.run_string == NULL) ||
1121 (ghost_info.delete_instance == NULL))
cristyfc0f64b2009-09-09 18:57:08 +00001122 return(FALSE);
1123 return(TRUE);
cristy3ed852e2009-09-05 21:47:34 +00001124}
1125
1126/*
1127%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1128% %
1129% %
1130% %
1131% N T G h o s t s c r i p t U n L o a d D L L %
1132% %
1133% %
1134% %
1135%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1136%
cristyfc0f64b2009-09-09 18:57:08 +00001137% NTGhostscriptUnLoadDLL() unloads the Ghostscript DLL and returns TRUE if
1138% it succeeds.
cristy3ed852e2009-09-05 21:47:34 +00001139%
1140% The format of the NTGhostscriptUnLoadDLL method is:
1141%
1142% int NTGhostscriptUnLoadDLL(void)
1143%
1144*/
1145MagickExport int NTGhostscriptUnLoadDLL(void)
1146{
cristyfc0f64b2009-09-09 18:57:08 +00001147 int
1148 status;
1149
cristydefb3f02009-09-10 02:18:35 +00001150 if (ghost_handle == (void *) NULL)
cristyfc0f64b2009-09-09 18:57:08 +00001151 return(FALSE);
cristydefb3f02009-09-10 02:18:35 +00001152 status=lt_dlclose(ghost_handle);
1153 ghost_handle=(void *) NULL;
1154 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
cristyfc0f64b2009-09-09 18:57:08 +00001155 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001156}
1157
1158/*
1159%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1160% %
1161% %
1162% %
1163% N T I n i t i a l i z e L i b r a r y %
1164% %
1165% %
1166% %
1167%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1168%
1169% NTInitializeLibrary() initializes the dynamic module loading subsystem.
1170%
1171% The format of the NTInitializeLibrary method is:
1172%
1173% int NTInitializeLibrary(void)
1174%
1175*/
1176MagickExport int NTInitializeLibrary(void)
1177{
1178 return(0);
1179}
1180
1181/*
1182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1183% %
1184% %
1185% %
1186+ N T M a p M e m o r y %
1187% %
1188% %
1189% %
1190%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1191%
1192% Mmap() emulates the Unix method of the same name.
1193%
1194% The format of the NTMapMemory method is:
1195%
1196% MagickExport void *NTMapMemory(char *address,size_t length,int protection,
1197% int access,int file,MagickOffsetType offset)
1198%
1199*/
1200MagickExport void *NTMapMemory(char *address,size_t length,int protection,
1201 int flags,int file,MagickOffsetType offset)
1202{
1203 DWORD
1204 access_mode,
1205 high_length,
1206 high_offset,
1207 low_length,
1208 low_offset,
1209 protection_mode;
1210
1211 HANDLE
1212 file_handle,
1213 map_handle;
1214
1215 void
1216 *map;
1217
1218 access_mode=0;
1219 file_handle=INVALID_HANDLE_VALUE;
1220 low_length=(DWORD) (length & 0xFFFFFFFFUL);
1221 high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1222 map_handle=INVALID_HANDLE_VALUE;
1223 map=(void *) NULL;
1224 low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1225 high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1226 protection_mode=0;
1227 if (protection & PROT_WRITE)
1228 {
1229 access_mode=FILE_MAP_WRITE;
1230 if (!(flags & MAP_PRIVATE))
1231 protection_mode=PAGE_READWRITE;
1232 else
1233 {
1234 access_mode=FILE_MAP_COPY;
1235 protection_mode=PAGE_WRITECOPY;
1236 }
1237 }
1238 else
1239 if (protection & PROT_READ)
1240 {
1241 access_mode=FILE_MAP_READ;
1242 protection_mode=PAGE_READONLY;
1243 }
1244 if ((file == -1) && (flags & MAP_ANONYMOUS))
1245 file_handle=INVALID_HANDLE_VALUE;
1246 else
1247 file_handle=(HANDLE) _get_osfhandle(file);
1248 map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1249 low_length,0);
1250 if (map_handle)
1251 {
1252 map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1253 length);
1254 CloseHandle(map_handle);
1255 }
1256 if (map == (void *) NULL)
1257 return((void *) MAP_FAILED);
1258 return((void *) ((char *) map));
1259}
1260
1261/*
1262%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1263% %
1264% %
1265% %
1266% N T O p e n D i r e c t o r y %
1267% %
1268% %
1269% %
1270%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1271%
1272% NTOpenDirectory() opens the directory named by filename and associates a
1273% directory stream with it.
1274%
1275% The format of the NTOpenDirectory method is:
1276%
1277% DIR *NTOpenDirectory(const char *path)
1278%
1279% A description of each parameter follows:
1280%
1281% o entry: Specifies a pointer to a DIR structure.
1282%
1283*/
1284MagickExport DIR *NTOpenDirectory(const char *path)
1285{
1286 char
1287 file_specification[MaxTextExtent];
1288
1289 DIR
1290 *entry;
1291
1292 size_t
1293 length;
1294
1295 assert(path != (const char *) NULL);
1296 length=CopyMagickString(file_specification,path,MaxTextExtent);
1297 if (length >= MaxTextExtent)
1298 return((DIR *) NULL);
1299 length=ConcatenateMagickString(file_specification,DirectorySeparator,
1300 MaxTextExtent);
1301 if (length >= MaxTextExtent)
1302 return((DIR *) NULL);
1303 entry=(DIR *) AcquireMagickMemory(sizeof(DIR));
1304 if (entry != (DIR *) NULL)
1305 {
1306 entry->firsttime=TRUE;
1307 entry->hSearch=FindFirstFile(file_specification,&entry->Win32FindData);
1308 }
1309 if (entry->hSearch == INVALID_HANDLE_VALUE)
1310 {
1311 length=ConcatenateMagickString(file_specification,"\\*.*",MaxTextExtent);
1312 if (length >= MaxTextExtent)
1313 {
1314 entry=(DIR *) RelinquishMagickMemory(entry);
1315 return((DIR *) NULL);
1316 }
1317 entry->hSearch=FindFirstFile(file_specification,&entry->Win32FindData);
1318 if (entry->hSearch == INVALID_HANDLE_VALUE)
1319 {
1320 entry=(DIR *) RelinquishMagickMemory(entry);
1321 return((DIR *) NULL);
1322 }
1323 }
1324 return(entry);
1325}
1326
1327/*
1328%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1329% %
1330% %
1331% %
1332% N T O p e n L i b r a r y %
1333% %
1334% %
1335% %
1336%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1337%
1338% NTOpenLibrary() loads a dynamic module into memory and returns a handle that
1339% can be used to access the various procedures in the module.
1340%
1341% The format of the NTOpenLibrary method is:
1342%
1343% void *NTOpenLibrary(const char *filename)
1344%
1345% A description of each parameter follows:
1346%
1347% o path: Specifies a pointer to string representing dynamic module that
1348% is to be loaded.
1349%
1350*/
1351
1352static const char *GetSearchPath( void )
1353{
1354#if defined(MAGICKCORE_LTDL_DELEGATE)
1355 return(lt_dlgetsearchpath());
1356#else
1357 return(lt_slsearchpath);
1358#endif
1359}
1360
1361MagickExport void *NTOpenLibrary(const char *filename)
1362{
1363#define MaxPathElements 31
1364
1365 char
1366 buffer[MaxTextExtent];
1367
1368 int
1369 index;
1370
1371 register const char
1372 *p,
1373 *q;
1374
1375 register int
1376 i;
1377
1378 UINT
1379 mode;
1380
1381 void
1382 *handle;
1383
1384 mode=SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
1385 handle=(void *) LoadLibraryEx(filename,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1386 if ((handle != (void *) NULL) || (GetSearchPath() == (char *) NULL))
1387 {
1388 SetErrorMode(mode);
1389 return(handle);
1390 }
1391 p=(char *) GetSearchPath();
1392 index=0;
1393 while (index < MaxPathElements)
1394 {
1395 q=strchr(p,DirectoryListSeparator);
1396 if (q == (char *) NULL)
1397 {
1398 (void) CopyMagickString(buffer,p,MaxTextExtent);
1399 (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1400 (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1401 handle=(void *) LoadLibraryEx(buffer,NULL,
1402 LOAD_WITH_ALTERED_SEARCH_PATH);
1403 break;
1404 }
1405 i=q-p;
1406 (void) CopyMagickString(buffer,p,i+1);
1407 (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1408 (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1409 handle=(void *) LoadLibraryEx(buffer,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1410 if (handle != (void *) NULL)
1411 break;
1412 p=q+1;
1413 }
1414 SetErrorMode(mode);
1415 return(handle);
1416}
1417
1418/*
1419%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1420% %
1421% %
1422% %
1423% N T R e a d D i r e c t o r y %
1424% %
1425% %
1426% %
1427%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1428%
1429% NTReadDirectory() returns a pointer to a structure representing the
1430% directory entry at the current position in the directory stream to which
1431% entry refers.
1432%
1433% The format of the NTReadDirectory
1434%
1435% NTReadDirectory(entry)
1436%
1437% A description of each parameter follows:
1438%
1439% o entry: Specifies a pointer to a DIR structure.
1440%
1441*/
1442MagickExport struct dirent *NTReadDirectory(DIR *entry)
1443{
1444 int
1445 status;
1446
1447 size_t
1448 length;
1449
1450 if (entry == (DIR *) NULL)
1451 return((struct dirent *) NULL);
1452 if (!entry->firsttime)
1453 {
1454 status=FindNextFile(entry->hSearch,&entry->Win32FindData);
1455 if (status == 0)
1456 return((struct dirent *) NULL);
1457 }
1458 length=CopyMagickString(entry->file_info.d_name,
1459 entry->Win32FindData.cFileName,sizeof(entry->file_info.d_name));
1460 if (length >= sizeof(entry->file_info.d_name))
1461 return((struct dirent *) NULL);
1462 entry->firsttime=FALSE;
1463 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1464 return(&entry->file_info);
1465}
1466
1467/*
1468%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1469% %
1470% %
1471% %
1472% N T R e g i s t r y K e y L o o k u p %
1473% %
1474% %
1475% %
1476%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1477%
1478% NTRegistryKeyLookup() returns ImageMagick installation path settings
1479% stored in the Windows Registry. Path settings are specific to the
1480% installed ImageMagick version so that multiple Image Magick installations
1481% may coexist.
1482%
1483% Values are stored in the registry under a base path path similar to
1484% "HKEY_LOCAL_MACHINE/SOFTWARE\ImageMagick\5.5.7\Q:16". The provided subkey
1485% is appended to this base path to form the full key.
1486%
1487% The format of the NTRegistryKeyLookup method is:
1488%
1489% unsigned char *NTRegistryKeyLookup(const char *subkey)
1490%
1491% A description of each parameter follows:
1492%
1493% o subkey: Specifies a string that identifies the registry object.
1494% Currently supported sub-keys include: "BinPath", "ConfigurePath",
1495% "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
1496%
1497*/
1498MagickExport unsigned char *NTRegistryKeyLookup(const char *subkey)
1499{
1500 char
1501 package_key[MaxTextExtent];
1502
1503 DWORD
1504 size,
1505 type;
1506
1507 HKEY
1508 registry_key;
1509
1510 LONG
1511 status;
1512
1513 unsigned char
1514 *value;
1515
1516 /*
1517 Look-up base key.
1518 */
1519 (void) FormatMagickString(package_key,MaxTextExtent,"SOFTWARE\\%s\\%s\\Q:%d",
1520 MagickPackageName,MagickLibVersionText,MAGICKCORE_QUANTUM_DEPTH);
1521 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",package_key);
1522 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1523 status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,package_key,0,KEY_READ,&registry_key);
1524 if (status != ERROR_SUCCESS)
1525 {
1526 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1527 return((unsigned char *) NULL);
1528 }
1529 /*
1530 Look-up sub key.
1531 */
1532 size=32;
1533 value=(unsigned char *) AcquireQuantumMemory(size,sizeof(*value));
1534 if (value == (unsigned char *) NULL)
1535 {
1536 RegCloseKey(registry_key);
1537 return((unsigned char *) NULL);
1538 }
1539 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",subkey);
1540 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1541 if ((status == ERROR_MORE_DATA) && (type == REG_SZ))
1542 {
1543 value=(unsigned char *) ResizeQuantumMemory(value,size,sizeof(*value));
1544 if (value == (BYTE *) NULL)
1545 {
1546 RegCloseKey(registry_key);
1547 return((unsigned char *) NULL);
1548 }
1549 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1550 }
1551 RegCloseKey(registry_key);
1552 if ((type != REG_SZ) || (status != ERROR_SUCCESS))
1553 value=(unsigned char *) RelinquishMagickMemory(value);
1554 return((unsigned char *) value);
1555}
1556
1557/*
1558%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1559% %
1560% %
1561% %
1562% N T R e p o r t E v e n t %
1563% %
1564% %
1565% %
1566%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1567%
1568% NTReportEvent() reports an event.
1569%
1570% The format of the NTReportEvent method is:
1571%
1572% MagickBooleanType NTReportEvent(const char *event,
1573% const MagickBooleanType error)
1574%
1575% A description of each parameter follows:
1576%
1577% o event: the event.
1578%
1579% o error: MagickTrue the event is an error.
1580%
1581*/
1582MagickExport MagickBooleanType NTReportEvent(const char *event,
1583 const MagickBooleanType error)
1584{
1585 const char
1586 *events[1];
1587
1588 HANDLE
1589 handle;
1590
1591 WORD
1592 type;
1593
1594 handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
1595 if (handle == NULL)
1596 return(MagickFalse);
1597 events[0]=event;
1598 type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
1599 ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
1600 DeregisterEventSource(handle);
1601 return(MagickTrue);
1602}
1603
1604/*
1605%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1606% %
1607% %
1608% %
1609% N T R e s o u r c e T o B l o b %
1610% %
1611% %
1612% %
1613%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1614%
1615% NTResourceToBlob() returns a blob containing the contents of the resource
1616% in the current executable specified by the id parameter. This currently
1617% used to retrieve MGK files tha have been embedded into the various command
1618% line utilities.
1619%
1620% The format of the NTResourceToBlob method is:
1621%
1622% unsigned char *NTResourceToBlob(const char *id)
1623%
1624% A description of each parameter follows:
1625%
1626% o id: Specifies a string that identifies the resource.
1627%
1628*/
1629MagickExport unsigned char *NTResourceToBlob(const char *id)
1630{
1631 char
1632 path[MaxTextExtent];
1633
1634 DWORD
1635 length;
1636
1637 HGLOBAL
1638 global;
1639
1640 HMODULE
1641 handle;
1642
1643 HRSRC
1644 resource;
1645
1646 unsigned char
1647 *blob,
1648 *value;
1649
1650 assert(id != (const char *) NULL);
1651 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1652 (void) FormatMagickString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
1653 DirectorySeparator,GetClientName());
1654 if (IsPathAccessible(path) != MagickFalse)
1655 handle=GetModuleHandle(path);
1656 else
1657 handle=GetModuleHandle(0);
1658 if (!handle)
1659 return((unsigned char *) NULL);
1660 resource=FindResource(handle,id,"IMAGEMAGICK");
1661 if (!resource)
1662 return((unsigned char *) NULL);
1663 global=LoadResource(handle,resource);
1664 if (!global)
1665 return((unsigned char *) NULL);
1666 length=SizeofResource(handle,resource);
1667 value=(unsigned char *) LockResource(global);
1668 if (!value)
1669 {
1670 FreeResource(global);
1671 return((unsigned char *) NULL);
1672 }
1673 blob=(unsigned char *) AcquireQuantumMemory(length+MaxTextExtent,
1674 sizeof(*blob));
1675 if (blob != (unsigned char *) NULL)
1676 {
1677 (void) CopyMagickMemory(blob,value,length);
1678 blob[length]='\0';
1679 }
1680 UnlockResource(global);
1681 FreeResource(global);
1682 return(blob);
1683}
1684
1685/*
1686%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1687% %
1688% %
1689% %
1690% N T S e e k D i r e c t o r y %
1691% %
1692% %
1693% %
1694%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1695%
1696% NTSeekDirectory() sets the position of the next NTReadDirectory() operation
1697% on the directory stream.
1698%
1699% The format of the NTSeekDirectory method is:
1700%
1701% void NTSeekDirectory(DIR *entry,long position)
1702%
1703% A description of each parameter follows:
1704%
1705% o entry: Specifies a pointer to a DIR structure.
1706%
1707% o position: specifies the position associated with the directory
1708% stream.
1709%
1710*/
1711MagickExport void NTSeekDirectory(DIR *entry,long position)
1712{
1713 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1714 assert(entry != (DIR *) NULL);
1715}
1716
1717/*
1718%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1719% %
1720% %
1721% %
1722% N T S e t S e a r c h P a t h %
1723% %
1724% %
1725% %
1726%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1727%
1728% NTSetSearchPath() sets the current locations that the subsystem should
1729% look at to find dynamically loadable modules.
1730%
1731% The format of the NTSetSearchPath method is:
1732%
1733% int NTSetSearchPath(const char *path)
1734%
1735% A description of each parameter follows:
1736%
1737% o path: Specifies a pointer to string representing the search path
1738% for DLL's that can be dynamically loaded.
1739%
1740*/
1741MagickExport int NTSetSearchPath(const char *path)
1742{
1743#if defined(MAGICKCORE_LTDL_DELEGATE)
1744 lt_dlsetsearchpath(path);
1745#else
1746 if (lt_slsearchpath != (char *) NULL)
1747 lt_slsearchpath=DestroyString(lt_slsearchpath);
1748 if (path != (char *) NULL)
1749 lt_slsearchpath=AcquireString(path);
1750#endif
1751 return(0);
1752}
1753
1754/*
1755%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1756% %
1757% %
1758% %
1759+ N T S y n c M e m o r y %
1760% %
1761% %
1762% %
1763%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1764%
1765% NTSyncMemory() emulates the Unix method of the same name.
1766%
1767% The format of the NTSyncMemory method is:
1768%
1769% int NTSyncMemory(void *address,size_t length,int flags)
1770%
1771% A description of each parameter follows:
1772%
1773% o address: the address of the binary large object.
1774%
1775% o length: the length of the binary large object.
1776%
1777% o flags: Option flags (ignored for Windows).
1778%
1779*/
1780MagickExport int NTSyncMemory(void *address,size_t length,int flags)
1781{
1782 if (FlushViewOfFile(address,length) == MagickFalse)
1783 return(-1);
1784 return(0);
1785}
1786
1787/*
1788%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1789% %
1790% %
1791% %
1792% N T S y s t e m C o m m a n d %
1793% %
1794% %
1795% %
1796%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1797%
1798% NTSystemCommand() executes the specified command and waits until it
1799% terminates. The returned value is the exit status of the command.
1800%
cristyb32b90a2009-09-07 21:45:48 +00001801% The format of the NTSystemCommand method is:
cristy3ed852e2009-09-05 21:47:34 +00001802%
cristyb32b90a2009-09-07 21:45:48 +00001803% int NTSystemCommand(const char *command)
cristy3ed852e2009-09-05 21:47:34 +00001804%
1805% A description of each parameter follows:
1806%
1807% o command: This string is the command to execute.
1808%
1809*/
1810MagickExport int NTSystemCommand(const char *command)
1811{
1812 char
1813 local_command[MaxTextExtent];
1814
1815 DWORD
1816 child_status;
1817
1818 int
1819 status;
1820
1821 MagickBooleanType
1822 background_process;
1823
1824 PROCESS_INFORMATION
1825 process_info;
1826
1827 STARTUPINFO
1828 startup_info;
1829
1830 if (command == (char *) NULL)
1831 return(-1);
1832 GetStartupInfo(&startup_info);
1833 startup_info.dwFlags=STARTF_USESHOWWINDOW;
1834 startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
1835 (void) CopyMagickString(local_command,command,MaxTextExtent);
1836 background_process=command[strlen(command)-1] == '&' ? MagickTrue :
1837 MagickFalse;
1838 if (background_process)
1839 local_command[strlen(command)-1]='\0';
1840 if (command[strlen(command)-1] == '|')
1841 local_command[strlen(command)-1]='\0';
1842 else
1843 startup_info.wShowWindow=SW_SHOWDEFAULT;
1844 status=CreateProcess((LPCTSTR) NULL,local_command,
1845 (LPSECURITY_ATTRIBUTES) NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) FALSE,
1846 (DWORD) NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
1847 &process_info);
1848 if (status == 0)
1849 return(-1);
1850 if (background_process)
1851 return(status == 0);
1852 status=WaitForSingleObject(process_info.hProcess,INFINITE);
1853 if (status != WAIT_OBJECT_0)
1854 return(status);
1855 status=GetExitCodeProcess(process_info.hProcess,&child_status);
1856 if (status == 0)
1857 return(-1);
1858 CloseHandle(process_info.hProcess);
1859 CloseHandle(process_info.hThread);
1860 return((int) child_status);
1861}
1862
1863/*
1864%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1865% %
1866% %
1867% %
1868% N T S y s t e m C o n i f i g u r a t i o n %
1869% %
1870% %
1871% %
1872%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1873%
1874% NTSystemConfiguration() provides a way for the application to determine
1875% values for system limits or options at runtime.
1876%
1877% The format of the exit method is:
1878%
1879% long NTSystemConfiguration(int name)
1880%
1881% A description of each parameter follows:
1882%
1883% o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
1884%
1885*/
1886MagickExport long NTSystemConfiguration(int name)
1887{
1888 switch (name)
1889 {
1890 case _SC_PAGESIZE:
1891 {
1892 SYSTEM_INFO
1893 system_info;
1894
1895 GetSystemInfo(&system_info);
1896 return(system_info.dwPageSize);
1897 }
1898 case _SC_PHYS_PAGES:
1899 {
1900 HMODULE
1901 handle;
1902
1903 LPFNDLLFUNC2
1904 module;
1905
1906 NTMEMORYSTATUSEX
1907 status;
1908
1909 SYSTEM_INFO
1910 system_info;
1911
1912 handle=GetModuleHandle("kernel32.dll");
1913 if (handle == (HMODULE) NULL)
1914 return(0L);
1915 GetSystemInfo(&system_info);
1916 module=(LPFNDLLFUNC2) NTGetLibrarySymbol(handle,"GlobalMemoryStatusEx");
1917 if (module == (LPFNDLLFUNC2) NULL)
1918 {
1919 MEMORYSTATUS
1920 status;
1921
1922 GlobalMemoryStatus(&status);
1923 return((long) status.dwTotalPhys/system_info.dwPageSize);
1924 }
1925 status.dwLength=sizeof(status);
1926 if (module(&status) == 0)
1927 return(0L);
1928 return((long) status.ullTotalPhys/system_info.dwPageSize);
1929 }
1930 case _SC_OPEN_MAX:
1931 return(2048);
1932 default:
1933 break;
1934 }
1935 return(-1);
1936}
1937
1938/*
1939%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1940% %
1941% %
1942% %
1943% N T T e l l D i r e c t o r y %
1944% %
1945% %
1946% %
1947%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1948%
1949% NTTellDirectory() returns the current location associated with the named
1950% directory stream.
1951%
1952% The format of the NTTellDirectory method is:
1953%
1954% long NTTellDirectory(DIR *entry)
1955%
1956% A description of each parameter follows:
1957%
1958% o entry: Specifies a pointer to a DIR structure.
1959%
1960*/
1961MagickExport long NTTellDirectory(DIR *entry)
1962{
1963 assert(entry != (DIR *) NULL);
1964 return(0);
1965}
1966
1967/*
1968%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1969% %
1970% %
1971% %
1972% N T T r u n c a t e F i l e %
1973% %
1974% %
1975% %
1976%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1977%
1978% NTTruncateFile() truncates a file to a specified length.
1979%
1980% The format of the NTTruncateFile method is:
1981%
1982% int NTTruncateFile(int file,off_t length)
1983%
1984% A description of each parameter follows:
1985%
1986% o file: the file.
1987%
1988% o length: the file length.
1989%
1990*/
1991MagickExport int NTTruncateFile(int file,off_t length)
1992{
1993 DWORD
1994 file_pointer;
1995
1996 long
1997 file_handle,
1998 high,
1999 low;
2000
2001 file_handle=_get_osfhandle(file);
2002 if (file_handle == -1L)
2003 return(-1);
2004 low=(long) (length & 0xffffffffUL);
2005 high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2006 file_pointer=SetFilePointer((HANDLE) file_handle,low,&high,FILE_BEGIN);
2007 if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2008 return(-1);
2009 if (SetEndOfFile((HANDLE) file_handle) == 0)
2010 return(-1);
2011 return(0);
2012}
2013
2014/*
2015%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2016% %
2017% %
2018% %
2019+ N T U n m a p M e m o r y %
2020% %
2021% %
2022% %
2023%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2024%
2025% NTUnmapMemory() emulates the Unix munmap method.
2026%
2027% The format of the NTUnmapMemory method is:
2028%
2029% int NTUnmapMemory(void *map,size_t length)
2030%
2031% A description of each parameter follows:
2032%
2033% o map: the address of the binary large object.
2034%
2035% o length: the length of the binary large object.
2036%
2037*/
2038MagickExport int NTUnmapMemory(void *map,size_t length)
2039{
2040 if (UnmapViewOfFile(map) == 0)
2041 return(-1);
2042 return(0);
2043}
2044
2045/*
2046%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2047% %
2048% %
2049% %
2050% N T U s e r T i m e %
2051% %
2052% %
2053% %
2054%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2055%
2056% NTUserTime() returns the total time the process has been scheduled (e.g.
2057% seconds) since the last call to StartTimer().
2058%
2059% The format of the UserTime method is:
2060%
2061% double NTUserTime(void)
2062%
2063*/
2064MagickExport double NTUserTime(void)
2065{
2066 DWORD
2067 status;
2068
2069 FILETIME
2070 create_time,
2071 exit_time;
2072
2073 OSVERSIONINFO
2074 OsVersionInfo;
2075
2076 union
2077 {
2078 FILETIME
2079 filetime;
2080
2081 __int64
2082 filetime64;
2083 } kernel_time;
2084
2085 union
2086 {
2087 FILETIME
2088 filetime;
2089
2090 __int64
2091 filetime64;
2092 } user_time;
2093
2094 OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
2095 GetVersionEx(&OsVersionInfo);
2096 if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
2097 return(NTElapsedTime());
2098 status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
2099 &kernel_time.filetime,&user_time.filetime);
2100 if (status != TRUE)
2101 return(0.0);
2102 return((double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
2103}
2104
2105/*
2106%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2107% %
2108% %
2109% %
2110% N T W a r n i n g H a n d l e r %
2111% %
2112% %
2113% %
2114%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2115%
2116% NTWarningHandler() displays a warning reason.
2117%
2118% The format of the NTWarningHandler method is:
2119%
2120% void NTWarningHandler(const ExceptionType warning,const char *reason,
2121% const char *description)
2122%
2123% A description of each parameter follows:
2124%
2125% o warning: Specifies the numeric warning category.
2126%
2127% o reason: Specifies the reason to display before terminating the
2128% program.
2129%
2130% o description: Specifies any description to the reason.
2131%
2132*/
2133MagickExport void NTWarningHandler(const ExceptionType warning,
2134 const char *reason,const char *description)
2135{
2136 char
2137 buffer[2*MaxTextExtent];
2138
2139 if (reason == (char *) NULL)
2140 return;
2141 if (description == (char *) NULL)
2142 (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s.\n",GetClientName(),
2143 reason);
2144 else
2145 (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s (%s).\n",
2146 GetClientName(),reason,description);
2147 (void) MessageBox(NULL,buffer,"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2148 MB_SETFOREGROUND | MB_ICONINFORMATION);
2149}
2150#endif