blob: dd40d543638d9345cb68c98294bae7587d5fe577 [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{
cristy4c11ed82009-09-11 03:36:46 +0000935 static char
936 dll[MaxTextExtent] = { "" };
cristy3ed852e2009-09-05 21:47:34 +0000937
938 *path='\0';
cristy4c11ed82009-09-11 03:36:46 +0000939 if ((*dll == '\0') &&
940 (NTGhostscriptGetString("GS_DLL",dll,sizeof(dll)) == FALSE))
941 return(FALSE);
942 (void) CopyMagickString(path,dll,length);
cristy3ed852e2009-09-05 21:47:34 +0000943 return(TRUE);
944}
945
946/*
947%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
948% %
949% %
950% %
951% N T G h o s t s c r i p t D L L V e c t o r s %
952% %
953% %
954% %
955%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
956%
cristydefb3f02009-09-10 02:18:35 +0000957% NTGhostscriptDLLVectors() returns a GhostInfo structure that includes
958% function vectors to invoke Ghostscript DLL functions. A null pointer is
959% returned if there is an error when loading the DLL or retrieving the
960% function vectors.
cristy3ed852e2009-09-05 21:47:34 +0000961%
962% The format of the NTGhostscriptDLLVectors method is:
963%
cristydefb3f02009-09-10 02:18:35 +0000964% const GhostInfo *NTGhostscriptDLLVectors(void)
cristy3ed852e2009-09-05 21:47:34 +0000965%
966*/
cristydefb3f02009-09-10 02:18:35 +0000967MagickExport const GhostInfo *NTGhostscriptDLLVectors(void)
cristy3ed852e2009-09-05 21:47:34 +0000968{
cristyfc0f64b2009-09-09 18:57:08 +0000969 if (NTGhostscriptLoadDLL() == FALSE)
cristydefb3f02009-09-10 02:18:35 +0000970 return((GhostInfo *) NULL);
971 return(&ghost_info);
cristy3ed852e2009-09-05 21:47:34 +0000972}
973
974/*
975%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
976% %
977% %
978% %
979% N T G h o s t s c r i p t E X E %
980% %
981% %
982% %
983%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
984%
985% NTGhostscriptEXE() obtains the path to the latest Ghostscript executable.
cristyfc0f64b2009-09-09 18:57:08 +0000986% The method returns FALSE if a full path value is not obtained and returns
987% a default path of gswin32c.exe.
cristy3ed852e2009-09-05 21:47:34 +0000988%
989% The format of the NTGhostscriptEXE method is:
990%
cristyfc0f64b2009-09-09 18:57:08 +0000991% int NTGhostscriptEXE(char *path,int length)
cristy3ed852e2009-09-05 21:47:34 +0000992%
993% A description of each parameter follows:
994%
cristyfc0f64b2009-09-09 18:57:08 +0000995% o path: return the Ghostscript executable path here.
cristy3ed852e2009-09-05 21:47:34 +0000996%
cristyb32b90a2009-09-07 21:45:48 +0000997% o length: length of buffer.
cristy3ed852e2009-09-05 21:47:34 +0000998%
999*/
1000MagickExport int NTGhostscriptEXE(char *path,int length)
1001{
cristy4c11ed82009-09-11 03:36:46 +00001002 register char
cristy3ed852e2009-09-05 21:47:34 +00001003 *p;
1004
cristyfc0f64b2009-09-09 18:57:08 +00001005 static char
cristy4c11ed82009-09-11 03:36:46 +00001006 program[MaxTextExtent] = { "" };
cristy3ed852e2009-09-05 21:47:34 +00001007
cristyb32b90a2009-09-07 21:45:48 +00001008 (void) CopyMagickString(path,"gswin32c.exe",length);
cristy4c11ed82009-09-11 03:36:46 +00001009 if ((*program == '\0') &&
1010 (NTGhostscriptGetString("GS_DLL",program,sizeof(program)) == FALSE))
cristyb32b90a2009-09-07 21:45:48 +00001011 return(FALSE);
cristy4c11ed82009-09-11 03:36:46 +00001012 p=strrchr(program,'\\');
1013 if (p != (char *) NULL)
1014 {
1015 p++;
1016 *p='\0';
1017 (void) ConcatenateMagickString(program,"gswin32c.exe",sizeof(program));
1018 }
1019 (void) CopyMagickString(path,program,length);
cristyb32b90a2009-09-07 21:45:48 +00001020 return(TRUE);
cristy3ed852e2009-09-05 21:47:34 +00001021}
1022
1023/*
1024%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1025% %
1026% %
1027% %
1028% N T G h o s t s c r i p t F o n t s %
1029% %
1030% %
1031% %
1032%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1033%
cristyfc0f64b2009-09-09 18:57:08 +00001034% NTGhostscriptFonts() obtains the path to the Ghostscript fonts. The method
1035% returns FALSE if it cannot determine the font path.
cristy3ed852e2009-09-05 21:47:34 +00001036%
1037% The format of the NTGhostscriptFonts method is:
1038%
cristyfc0f64b2009-09-09 18:57:08 +00001039% int NTGhostscriptFonts(char *path, int length)
cristy3ed852e2009-09-05 21:47:34 +00001040%
1041% A description of each parameter follows:
1042%
cristyfc0f64b2009-09-09 18:57:08 +00001043% o path: return the font path here.
cristy3ed852e2009-09-05 21:47:34 +00001044%
cristyfc0f64b2009-09-09 18:57:08 +00001045% o length: length of the path buffer.
cristy3ed852e2009-09-05 21:47:34 +00001046%
1047*/
1048MagickExport int NTGhostscriptFonts(char *path,int length)
1049{
1050 char
1051 buffer[MaxTextExtent],
1052 filename[MaxTextExtent];
1053
cristy3ed852e2009-09-05 21:47:34 +00001054 register char
1055 *p,
1056 *q;
1057
1058 *path='\0';
cristyfc0f64b2009-09-09 18:57:08 +00001059 if (NTGhostscriptGetString("GS_LIB",buffer,MaxTextExtent) == FALSE)
cristy3ed852e2009-09-05 21:47:34 +00001060 return(FALSE);
1061 for (p=buffer-1; p != (char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1062 {
1063 (void) CopyMagickString(path,p+1,length+1);
1064 q=strchr(path,DirectoryListSeparator);
1065 if (q != (char *) NULL)
1066 *q='\0';
cristyfc0f64b2009-09-09 18:57:08 +00001067 (void) FormatMagickString(filename,MaxTextExtent,"%s%sfonts.dir",path,
cristy3ed852e2009-09-05 21:47:34 +00001068 DirectorySeparator);
1069 if (IsPathAccessible(filename) != MagickFalse)
1070 return(TRUE);
1071 }
1072 return(FALSE);
1073}
1074
1075/*
1076%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1077% %
1078% %
1079% %
1080% N T G h o s t s c r i p t L o a d D L L %
1081% %
1082% %
1083% %
1084%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1085%
1086% NTGhostscriptLoadDLL() attempts to load the Ghostscript DLL and returns
cristyfc0f64b2009-09-09 18:57:08 +00001087% TRUE if it succeeds.
cristy3ed852e2009-09-05 21:47:34 +00001088%
1089% The format of the NTGhostscriptLoadDLL method is:
1090%
1091% int NTGhostscriptLoadDLL(void)
1092%
1093*/
1094MagickExport int NTGhostscriptLoadDLL(void)
1095{
1096 char
cristyfc0f64b2009-09-09 18:57:08 +00001097 path[MaxTextExtent];
cristy3ed852e2009-09-05 21:47:34 +00001098
cristydefb3f02009-09-10 02:18:35 +00001099 if (ghost_handle != (void *) NULL)
cristyfc0f64b2009-09-09 18:57:08 +00001100 return(TRUE);
1101 if (NTGhostscriptDLL(path,sizeof(path)) == FALSE)
1102 return(FALSE);
cristydefb3f02009-09-10 02:18:35 +00001103 ghost_handle=lt_dlopen(path);
1104 if (ghost_handle == (void *) NULL)
cristyfc0f64b2009-09-09 18:57:08 +00001105 return(FALSE);
cristydefb3f02009-09-10 02:18:35 +00001106 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1107 ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
1108 lt_dlsym(ghost_handle,"gsapi_exit");
1109 ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
1110 char **)) (lt_dlsym(ghost_handle,"gsapi_init_with_args"));
1111 ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,void *)) (
1112 lt_dlsym(ghost_handle,"gsapi_new_instance"));
1113 ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,const char *,
1114 int,int *)) (lt_dlsym(ghost_handle,"gsapi_run_string"));
1115 ghost_info.delete_instance=(void (MagickDLLCall *) (gs_main_instance *)) (
1116 lt_dlsym(ghost_handle,"gsapi_delete_instance"));
1117 if ((ghost_info.exit == NULL) || (ghost_info.init_with_args == NULL) ||
1118 (ghost_info.new_instance == NULL) || (ghost_info.run_string == NULL) ||
1119 (ghost_info.delete_instance == NULL))
cristyfc0f64b2009-09-09 18:57:08 +00001120 return(FALSE);
1121 return(TRUE);
cristy3ed852e2009-09-05 21:47:34 +00001122}
1123
1124/*
1125%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1126% %
1127% %
1128% %
1129% N T G h o s t s c r i p t U n L o a d D L L %
1130% %
1131% %
1132% %
1133%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1134%
cristyfc0f64b2009-09-09 18:57:08 +00001135% NTGhostscriptUnLoadDLL() unloads the Ghostscript DLL and returns TRUE if
1136% it succeeds.
cristy3ed852e2009-09-05 21:47:34 +00001137%
1138% The format of the NTGhostscriptUnLoadDLL method is:
1139%
1140% int NTGhostscriptUnLoadDLL(void)
1141%
1142*/
1143MagickExport int NTGhostscriptUnLoadDLL(void)
1144{
cristyfc0f64b2009-09-09 18:57:08 +00001145 int
1146 status;
1147
cristydefb3f02009-09-10 02:18:35 +00001148 if (ghost_handle == (void *) NULL)
cristyfc0f64b2009-09-09 18:57:08 +00001149 return(FALSE);
cristydefb3f02009-09-10 02:18:35 +00001150 status=lt_dlclose(ghost_handle);
1151 ghost_handle=(void *) NULL;
1152 (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
cristyfc0f64b2009-09-09 18:57:08 +00001153 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001154}
1155
1156/*
1157%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1158% %
1159% %
1160% %
1161% N T I n i t i a l i z e L i b r a r y %
1162% %
1163% %
1164% %
1165%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1166%
1167% NTInitializeLibrary() initializes the dynamic module loading subsystem.
1168%
1169% The format of the NTInitializeLibrary method is:
1170%
1171% int NTInitializeLibrary(void)
1172%
1173*/
1174MagickExport int NTInitializeLibrary(void)
1175{
1176 return(0);
1177}
1178
1179/*
1180%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1181% %
1182% %
1183% %
1184+ N T M a p M e m o r y %
1185% %
1186% %
1187% %
1188%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1189%
1190% Mmap() emulates the Unix method of the same name.
1191%
1192% The format of the NTMapMemory method is:
1193%
1194% MagickExport void *NTMapMemory(char *address,size_t length,int protection,
1195% int access,int file,MagickOffsetType offset)
1196%
1197*/
1198MagickExport void *NTMapMemory(char *address,size_t length,int protection,
1199 int flags,int file,MagickOffsetType offset)
1200{
1201 DWORD
1202 access_mode,
1203 high_length,
1204 high_offset,
1205 low_length,
1206 low_offset,
1207 protection_mode;
1208
1209 HANDLE
1210 file_handle,
1211 map_handle;
1212
1213 void
1214 *map;
1215
1216 access_mode=0;
1217 file_handle=INVALID_HANDLE_VALUE;
1218 low_length=(DWORD) (length & 0xFFFFFFFFUL);
1219 high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1220 map_handle=INVALID_HANDLE_VALUE;
1221 map=(void *) NULL;
1222 low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1223 high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1224 protection_mode=0;
1225 if (protection & PROT_WRITE)
1226 {
1227 access_mode=FILE_MAP_WRITE;
1228 if (!(flags & MAP_PRIVATE))
1229 protection_mode=PAGE_READWRITE;
1230 else
1231 {
1232 access_mode=FILE_MAP_COPY;
1233 protection_mode=PAGE_WRITECOPY;
1234 }
1235 }
1236 else
1237 if (protection & PROT_READ)
1238 {
1239 access_mode=FILE_MAP_READ;
1240 protection_mode=PAGE_READONLY;
1241 }
1242 if ((file == -1) && (flags & MAP_ANONYMOUS))
1243 file_handle=INVALID_HANDLE_VALUE;
1244 else
1245 file_handle=(HANDLE) _get_osfhandle(file);
1246 map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1247 low_length,0);
1248 if (map_handle)
1249 {
1250 map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1251 length);
1252 CloseHandle(map_handle);
1253 }
1254 if (map == (void *) NULL)
1255 return((void *) MAP_FAILED);
1256 return((void *) ((char *) map));
1257}
1258
1259/*
1260%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1261% %
1262% %
1263% %
1264% N T O p e n D i r e c t o r y %
1265% %
1266% %
1267% %
1268%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1269%
1270% NTOpenDirectory() opens the directory named by filename and associates a
1271% directory stream with it.
1272%
1273% The format of the NTOpenDirectory method is:
1274%
1275% DIR *NTOpenDirectory(const char *path)
1276%
1277% A description of each parameter follows:
1278%
1279% o entry: Specifies a pointer to a DIR structure.
1280%
1281*/
1282MagickExport DIR *NTOpenDirectory(const char *path)
1283{
1284 char
1285 file_specification[MaxTextExtent];
1286
1287 DIR
1288 *entry;
1289
1290 size_t
1291 length;
1292
1293 assert(path != (const char *) NULL);
1294 length=CopyMagickString(file_specification,path,MaxTextExtent);
1295 if (length >= MaxTextExtent)
1296 return((DIR *) NULL);
1297 length=ConcatenateMagickString(file_specification,DirectorySeparator,
1298 MaxTextExtent);
1299 if (length >= MaxTextExtent)
1300 return((DIR *) NULL);
1301 entry=(DIR *) AcquireMagickMemory(sizeof(DIR));
1302 if (entry != (DIR *) NULL)
1303 {
1304 entry->firsttime=TRUE;
1305 entry->hSearch=FindFirstFile(file_specification,&entry->Win32FindData);
1306 }
1307 if (entry->hSearch == INVALID_HANDLE_VALUE)
1308 {
1309 length=ConcatenateMagickString(file_specification,"\\*.*",MaxTextExtent);
1310 if (length >= MaxTextExtent)
1311 {
1312 entry=(DIR *) RelinquishMagickMemory(entry);
1313 return((DIR *) NULL);
1314 }
1315 entry->hSearch=FindFirstFile(file_specification,&entry->Win32FindData);
1316 if (entry->hSearch == INVALID_HANDLE_VALUE)
1317 {
1318 entry=(DIR *) RelinquishMagickMemory(entry);
1319 return((DIR *) NULL);
1320 }
1321 }
1322 return(entry);
1323}
1324
1325/*
1326%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1327% %
1328% %
1329% %
1330% N T O p e n L i b r a r y %
1331% %
1332% %
1333% %
1334%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1335%
1336% NTOpenLibrary() loads a dynamic module into memory and returns a handle that
1337% can be used to access the various procedures in the module.
1338%
1339% The format of the NTOpenLibrary method is:
1340%
1341% void *NTOpenLibrary(const char *filename)
1342%
1343% A description of each parameter follows:
1344%
1345% o path: Specifies a pointer to string representing dynamic module that
1346% is to be loaded.
1347%
1348*/
1349
1350static const char *GetSearchPath( void )
1351{
1352#if defined(MAGICKCORE_LTDL_DELEGATE)
1353 return(lt_dlgetsearchpath());
1354#else
1355 return(lt_slsearchpath);
1356#endif
1357}
1358
1359MagickExport void *NTOpenLibrary(const char *filename)
1360{
1361#define MaxPathElements 31
1362
1363 char
1364 buffer[MaxTextExtent];
1365
1366 int
1367 index;
1368
1369 register const char
1370 *p,
1371 *q;
1372
1373 register int
1374 i;
1375
1376 UINT
1377 mode;
1378
1379 void
1380 *handle;
1381
1382 mode=SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
1383 handle=(void *) LoadLibraryEx(filename,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1384 if ((handle != (void *) NULL) || (GetSearchPath() == (char *) NULL))
1385 {
1386 SetErrorMode(mode);
1387 return(handle);
1388 }
1389 p=(char *) GetSearchPath();
1390 index=0;
1391 while (index < MaxPathElements)
1392 {
1393 q=strchr(p,DirectoryListSeparator);
1394 if (q == (char *) NULL)
1395 {
1396 (void) CopyMagickString(buffer,p,MaxTextExtent);
1397 (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1398 (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1399 handle=(void *) LoadLibraryEx(buffer,NULL,
1400 LOAD_WITH_ALTERED_SEARCH_PATH);
1401 break;
1402 }
1403 i=q-p;
1404 (void) CopyMagickString(buffer,p,i+1);
1405 (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1406 (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1407 handle=(void *) LoadLibraryEx(buffer,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1408 if (handle != (void *) NULL)
1409 break;
1410 p=q+1;
1411 }
1412 SetErrorMode(mode);
1413 return(handle);
1414}
1415
1416/*
1417%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1418% %
1419% %
1420% %
1421% N T R e a d D i r e c t o r y %
1422% %
1423% %
1424% %
1425%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1426%
1427% NTReadDirectory() returns a pointer to a structure representing the
1428% directory entry at the current position in the directory stream to which
1429% entry refers.
1430%
1431% The format of the NTReadDirectory
1432%
1433% NTReadDirectory(entry)
1434%
1435% A description of each parameter follows:
1436%
1437% o entry: Specifies a pointer to a DIR structure.
1438%
1439*/
1440MagickExport struct dirent *NTReadDirectory(DIR *entry)
1441{
1442 int
1443 status;
1444
1445 size_t
1446 length;
1447
1448 if (entry == (DIR *) NULL)
1449 return((struct dirent *) NULL);
1450 if (!entry->firsttime)
1451 {
1452 status=FindNextFile(entry->hSearch,&entry->Win32FindData);
1453 if (status == 0)
1454 return((struct dirent *) NULL);
1455 }
1456 length=CopyMagickString(entry->file_info.d_name,
1457 entry->Win32FindData.cFileName,sizeof(entry->file_info.d_name));
1458 if (length >= sizeof(entry->file_info.d_name))
1459 return((struct dirent *) NULL);
1460 entry->firsttime=FALSE;
1461 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1462 return(&entry->file_info);
1463}
1464
1465/*
1466%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1467% %
1468% %
1469% %
1470% N T R e g i s t r y K e y L o o k u p %
1471% %
1472% %
1473% %
1474%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1475%
1476% NTRegistryKeyLookup() returns ImageMagick installation path settings
1477% stored in the Windows Registry. Path settings are specific to the
1478% installed ImageMagick version so that multiple Image Magick installations
1479% may coexist.
1480%
1481% Values are stored in the registry under a base path path similar to
1482% "HKEY_LOCAL_MACHINE/SOFTWARE\ImageMagick\5.5.7\Q:16". The provided subkey
1483% is appended to this base path to form the full key.
1484%
1485% The format of the NTRegistryKeyLookup method is:
1486%
1487% unsigned char *NTRegistryKeyLookup(const char *subkey)
1488%
1489% A description of each parameter follows:
1490%
1491% o subkey: Specifies a string that identifies the registry object.
1492% Currently supported sub-keys include: "BinPath", "ConfigurePath",
1493% "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
1494%
1495*/
1496MagickExport unsigned char *NTRegistryKeyLookup(const char *subkey)
1497{
1498 char
1499 package_key[MaxTextExtent];
1500
1501 DWORD
1502 size,
1503 type;
1504
1505 HKEY
1506 registry_key;
1507
1508 LONG
1509 status;
1510
1511 unsigned char
1512 *value;
1513
1514 /*
1515 Look-up base key.
1516 */
1517 (void) FormatMagickString(package_key,MaxTextExtent,"SOFTWARE\\%s\\%s\\Q:%d",
1518 MagickPackageName,MagickLibVersionText,MAGICKCORE_QUANTUM_DEPTH);
1519 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",package_key);
1520 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1521 status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,package_key,0,KEY_READ,&registry_key);
1522 if (status != ERROR_SUCCESS)
1523 {
1524 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1525 return((unsigned char *) NULL);
1526 }
1527 /*
1528 Look-up sub key.
1529 */
1530 size=32;
1531 value=(unsigned char *) AcquireQuantumMemory(size,sizeof(*value));
1532 if (value == (unsigned char *) NULL)
1533 {
1534 RegCloseKey(registry_key);
1535 return((unsigned char *) NULL);
1536 }
1537 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",subkey);
1538 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1539 if ((status == ERROR_MORE_DATA) && (type == REG_SZ))
1540 {
1541 value=(unsigned char *) ResizeQuantumMemory(value,size,sizeof(*value));
1542 if (value == (BYTE *) NULL)
1543 {
1544 RegCloseKey(registry_key);
1545 return((unsigned char *) NULL);
1546 }
1547 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1548 }
1549 RegCloseKey(registry_key);
1550 if ((type != REG_SZ) || (status != ERROR_SUCCESS))
1551 value=(unsigned char *) RelinquishMagickMemory(value);
1552 return((unsigned char *) value);
1553}
1554
1555/*
1556%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1557% %
1558% %
1559% %
1560% N T R e p o r t E v e n t %
1561% %
1562% %
1563% %
1564%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1565%
1566% NTReportEvent() reports an event.
1567%
1568% The format of the NTReportEvent method is:
1569%
1570% MagickBooleanType NTReportEvent(const char *event,
1571% const MagickBooleanType error)
1572%
1573% A description of each parameter follows:
1574%
1575% o event: the event.
1576%
1577% o error: MagickTrue the event is an error.
1578%
1579*/
1580MagickExport MagickBooleanType NTReportEvent(const char *event,
1581 const MagickBooleanType error)
1582{
1583 const char
1584 *events[1];
1585
1586 HANDLE
1587 handle;
1588
1589 WORD
1590 type;
1591
1592 handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
1593 if (handle == NULL)
1594 return(MagickFalse);
1595 events[0]=event;
1596 type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
1597 ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
1598 DeregisterEventSource(handle);
1599 return(MagickTrue);
1600}
1601
1602/*
1603%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1604% %
1605% %
1606% %
1607% N T R e s o u r c e T o B l o b %
1608% %
1609% %
1610% %
1611%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1612%
1613% NTResourceToBlob() returns a blob containing the contents of the resource
1614% in the current executable specified by the id parameter. This currently
1615% used to retrieve MGK files tha have been embedded into the various command
1616% line utilities.
1617%
1618% The format of the NTResourceToBlob method is:
1619%
1620% unsigned char *NTResourceToBlob(const char *id)
1621%
1622% A description of each parameter follows:
1623%
1624% o id: Specifies a string that identifies the resource.
1625%
1626*/
1627MagickExport unsigned char *NTResourceToBlob(const char *id)
1628{
1629 char
1630 path[MaxTextExtent];
1631
1632 DWORD
1633 length;
1634
1635 HGLOBAL
1636 global;
1637
1638 HMODULE
1639 handle;
1640
1641 HRSRC
1642 resource;
1643
1644 unsigned char
1645 *blob,
1646 *value;
1647
1648 assert(id != (const char *) NULL);
1649 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1650 (void) FormatMagickString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
1651 DirectorySeparator,GetClientName());
1652 if (IsPathAccessible(path) != MagickFalse)
1653 handle=GetModuleHandle(path);
1654 else
1655 handle=GetModuleHandle(0);
1656 if (!handle)
1657 return((unsigned char *) NULL);
1658 resource=FindResource(handle,id,"IMAGEMAGICK");
1659 if (!resource)
1660 return((unsigned char *) NULL);
1661 global=LoadResource(handle,resource);
1662 if (!global)
1663 return((unsigned char *) NULL);
1664 length=SizeofResource(handle,resource);
1665 value=(unsigned char *) LockResource(global);
1666 if (!value)
1667 {
1668 FreeResource(global);
1669 return((unsigned char *) NULL);
1670 }
1671 blob=(unsigned char *) AcquireQuantumMemory(length+MaxTextExtent,
1672 sizeof(*blob));
1673 if (blob != (unsigned char *) NULL)
1674 {
1675 (void) CopyMagickMemory(blob,value,length);
1676 blob[length]='\0';
1677 }
1678 UnlockResource(global);
1679 FreeResource(global);
1680 return(blob);
1681}
1682
1683/*
1684%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1685% %
1686% %
1687% %
1688% N T S e e k D i r e c t o r y %
1689% %
1690% %
1691% %
1692%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1693%
1694% NTSeekDirectory() sets the position of the next NTReadDirectory() operation
1695% on the directory stream.
1696%
1697% The format of the NTSeekDirectory method is:
1698%
1699% void NTSeekDirectory(DIR *entry,long position)
1700%
1701% A description of each parameter follows:
1702%
1703% o entry: Specifies a pointer to a DIR structure.
1704%
1705% o position: specifies the position associated with the directory
1706% stream.
1707%
1708*/
1709MagickExport void NTSeekDirectory(DIR *entry,long position)
1710{
1711 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1712 assert(entry != (DIR *) NULL);
1713}
1714
1715/*
1716%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1717% %
1718% %
1719% %
1720% N T S e t S e a r c h P a t h %
1721% %
1722% %
1723% %
1724%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1725%
1726% NTSetSearchPath() sets the current locations that the subsystem should
1727% look at to find dynamically loadable modules.
1728%
1729% The format of the NTSetSearchPath method is:
1730%
1731% int NTSetSearchPath(const char *path)
1732%
1733% A description of each parameter follows:
1734%
1735% o path: Specifies a pointer to string representing the search path
1736% for DLL's that can be dynamically loaded.
1737%
1738*/
1739MagickExport int NTSetSearchPath(const char *path)
1740{
1741#if defined(MAGICKCORE_LTDL_DELEGATE)
1742 lt_dlsetsearchpath(path);
1743#else
1744 if (lt_slsearchpath != (char *) NULL)
1745 lt_slsearchpath=DestroyString(lt_slsearchpath);
1746 if (path != (char *) NULL)
1747 lt_slsearchpath=AcquireString(path);
1748#endif
1749 return(0);
1750}
1751
1752/*
1753%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1754% %
1755% %
1756% %
1757+ N T S y n c M e m o r y %
1758% %
1759% %
1760% %
1761%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1762%
1763% NTSyncMemory() emulates the Unix method of the same name.
1764%
1765% The format of the NTSyncMemory method is:
1766%
1767% int NTSyncMemory(void *address,size_t length,int flags)
1768%
1769% A description of each parameter follows:
1770%
1771% o address: the address of the binary large object.
1772%
1773% o length: the length of the binary large object.
1774%
1775% o flags: Option flags (ignored for Windows).
1776%
1777*/
1778MagickExport int NTSyncMemory(void *address,size_t length,int flags)
1779{
1780 if (FlushViewOfFile(address,length) == MagickFalse)
1781 return(-1);
1782 return(0);
1783}
1784
1785/*
1786%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1787% %
1788% %
1789% %
1790% N T S y s t e m C o m m a n d %
1791% %
1792% %
1793% %
1794%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1795%
1796% NTSystemCommand() executes the specified command and waits until it
1797% terminates. The returned value is the exit status of the command.
1798%
cristyb32b90a2009-09-07 21:45:48 +00001799% The format of the NTSystemCommand method is:
cristy3ed852e2009-09-05 21:47:34 +00001800%
cristyb32b90a2009-09-07 21:45:48 +00001801% int NTSystemCommand(const char *command)
cristy3ed852e2009-09-05 21:47:34 +00001802%
1803% A description of each parameter follows:
1804%
1805% o command: This string is the command to execute.
1806%
1807*/
1808MagickExport int NTSystemCommand(const char *command)
1809{
1810 char
1811 local_command[MaxTextExtent];
1812
1813 DWORD
1814 child_status;
1815
1816 int
1817 status;
1818
1819 MagickBooleanType
1820 background_process;
1821
1822 PROCESS_INFORMATION
1823 process_info;
1824
1825 STARTUPINFO
1826 startup_info;
1827
1828 if (command == (char *) NULL)
1829 return(-1);
1830 GetStartupInfo(&startup_info);
1831 startup_info.dwFlags=STARTF_USESHOWWINDOW;
1832 startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
1833 (void) CopyMagickString(local_command,command,MaxTextExtent);
1834 background_process=command[strlen(command)-1] == '&' ? MagickTrue :
1835 MagickFalse;
1836 if (background_process)
1837 local_command[strlen(command)-1]='\0';
1838 if (command[strlen(command)-1] == '|')
1839 local_command[strlen(command)-1]='\0';
1840 else
1841 startup_info.wShowWindow=SW_SHOWDEFAULT;
1842 status=CreateProcess((LPCTSTR) NULL,local_command,
1843 (LPSECURITY_ATTRIBUTES) NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) FALSE,
1844 (DWORD) NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
1845 &process_info);
1846 if (status == 0)
1847 return(-1);
1848 if (background_process)
1849 return(status == 0);
1850 status=WaitForSingleObject(process_info.hProcess,INFINITE);
1851 if (status != WAIT_OBJECT_0)
1852 return(status);
1853 status=GetExitCodeProcess(process_info.hProcess,&child_status);
1854 if (status == 0)
1855 return(-1);
1856 CloseHandle(process_info.hProcess);
1857 CloseHandle(process_info.hThread);
1858 return((int) child_status);
1859}
1860
1861/*
1862%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1863% %
1864% %
1865% %
1866% N T S y s t e m C o n i f i g u r a t i o n %
1867% %
1868% %
1869% %
1870%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1871%
1872% NTSystemConfiguration() provides a way for the application to determine
1873% values for system limits or options at runtime.
1874%
1875% The format of the exit method is:
1876%
1877% long NTSystemConfiguration(int name)
1878%
1879% A description of each parameter follows:
1880%
1881% o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
1882%
1883*/
1884MagickExport long NTSystemConfiguration(int name)
1885{
1886 switch (name)
1887 {
1888 case _SC_PAGESIZE:
1889 {
1890 SYSTEM_INFO
1891 system_info;
1892
1893 GetSystemInfo(&system_info);
1894 return(system_info.dwPageSize);
1895 }
1896 case _SC_PHYS_PAGES:
1897 {
1898 HMODULE
1899 handle;
1900
1901 LPFNDLLFUNC2
1902 module;
1903
1904 NTMEMORYSTATUSEX
1905 status;
1906
1907 SYSTEM_INFO
1908 system_info;
1909
1910 handle=GetModuleHandle("kernel32.dll");
1911 if (handle == (HMODULE) NULL)
1912 return(0L);
1913 GetSystemInfo(&system_info);
1914 module=(LPFNDLLFUNC2) NTGetLibrarySymbol(handle,"GlobalMemoryStatusEx");
1915 if (module == (LPFNDLLFUNC2) NULL)
1916 {
1917 MEMORYSTATUS
1918 status;
1919
1920 GlobalMemoryStatus(&status);
1921 return((long) status.dwTotalPhys/system_info.dwPageSize);
1922 }
1923 status.dwLength=sizeof(status);
1924 if (module(&status) == 0)
1925 return(0L);
1926 return((long) status.ullTotalPhys/system_info.dwPageSize);
1927 }
1928 case _SC_OPEN_MAX:
1929 return(2048);
1930 default:
1931 break;
1932 }
1933 return(-1);
1934}
1935
1936/*
1937%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1938% %
1939% %
1940% %
1941% N T T e l l D i r e c t o r y %
1942% %
1943% %
1944% %
1945%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1946%
1947% NTTellDirectory() returns the current location associated with the named
1948% directory stream.
1949%
1950% The format of the NTTellDirectory method is:
1951%
1952% long NTTellDirectory(DIR *entry)
1953%
1954% A description of each parameter follows:
1955%
1956% o entry: Specifies a pointer to a DIR structure.
1957%
1958*/
1959MagickExport long NTTellDirectory(DIR *entry)
1960{
1961 assert(entry != (DIR *) NULL);
1962 return(0);
1963}
1964
1965/*
1966%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1967% %
1968% %
1969% %
1970% N T T r u n c a t e F i l e %
1971% %
1972% %
1973% %
1974%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1975%
1976% NTTruncateFile() truncates a file to a specified length.
1977%
1978% The format of the NTTruncateFile method is:
1979%
1980% int NTTruncateFile(int file,off_t length)
1981%
1982% A description of each parameter follows:
1983%
1984% o file: the file.
1985%
1986% o length: the file length.
1987%
1988*/
1989MagickExport int NTTruncateFile(int file,off_t length)
1990{
1991 DWORD
1992 file_pointer;
1993
1994 long
1995 file_handle,
1996 high,
1997 low;
1998
1999 file_handle=_get_osfhandle(file);
2000 if (file_handle == -1L)
2001 return(-1);
2002 low=(long) (length & 0xffffffffUL);
2003 high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2004 file_pointer=SetFilePointer((HANDLE) file_handle,low,&high,FILE_BEGIN);
2005 if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2006 return(-1);
2007 if (SetEndOfFile((HANDLE) file_handle) == 0)
2008 return(-1);
2009 return(0);
2010}
2011
2012/*
2013%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2014% %
2015% %
2016% %
2017+ N T U n m a p M e m o r y %
2018% %
2019% %
2020% %
2021%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2022%
2023% NTUnmapMemory() emulates the Unix munmap method.
2024%
2025% The format of the NTUnmapMemory method is:
2026%
2027% int NTUnmapMemory(void *map,size_t length)
2028%
2029% A description of each parameter follows:
2030%
2031% o map: the address of the binary large object.
2032%
2033% o length: the length of the binary large object.
2034%
2035*/
2036MagickExport int NTUnmapMemory(void *map,size_t length)
2037{
2038 if (UnmapViewOfFile(map) == 0)
2039 return(-1);
2040 return(0);
2041}
2042
2043/*
2044%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2045% %
2046% %
2047% %
2048% N T U s e r T i m e %
2049% %
2050% %
2051% %
2052%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2053%
2054% NTUserTime() returns the total time the process has been scheduled (e.g.
2055% seconds) since the last call to StartTimer().
2056%
2057% The format of the UserTime method is:
2058%
2059% double NTUserTime(void)
2060%
2061*/
2062MagickExport double NTUserTime(void)
2063{
2064 DWORD
2065 status;
2066
2067 FILETIME
2068 create_time,
2069 exit_time;
2070
2071 OSVERSIONINFO
2072 OsVersionInfo;
2073
2074 union
2075 {
2076 FILETIME
2077 filetime;
2078
2079 __int64
2080 filetime64;
2081 } kernel_time;
2082
2083 union
2084 {
2085 FILETIME
2086 filetime;
2087
2088 __int64
2089 filetime64;
2090 } user_time;
2091
2092 OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
2093 GetVersionEx(&OsVersionInfo);
2094 if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
2095 return(NTElapsedTime());
2096 status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
2097 &kernel_time.filetime,&user_time.filetime);
2098 if (status != TRUE)
2099 return(0.0);
2100 return((double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
2101}
2102
2103/*
2104%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2105% %
2106% %
2107% %
2108% N T W a r n i n g H a n d l e r %
2109% %
2110% %
2111% %
2112%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2113%
2114% NTWarningHandler() displays a warning reason.
2115%
2116% The format of the NTWarningHandler method is:
2117%
2118% void NTWarningHandler(const ExceptionType warning,const char *reason,
2119% const char *description)
2120%
2121% A description of each parameter follows:
2122%
2123% o warning: Specifies the numeric warning category.
2124%
2125% o reason: Specifies the reason to display before terminating the
2126% program.
2127%
2128% o description: Specifies any description to the reason.
2129%
2130*/
2131MagickExport void NTWarningHandler(const ExceptionType warning,
2132 const char *reason,const char *description)
2133{
2134 char
2135 buffer[2*MaxTextExtent];
2136
2137 if (reason == (char *) NULL)
2138 return;
2139 if (description == (char *) NULL)
2140 (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s.\n",GetClientName(),
2141 reason);
2142 else
2143 (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s (%s).\n",
2144 GetClientName(),reason,description);
2145 (void) MessageBox(NULL,buffer,"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2146 MB_SETFOREGROUND | MB_ICONINFORMATION);
2147}
2148#endif