]> granicus.if.org Git - imagemagick/blob - magick/nt-base.c
5b6cbb2ae4cb870f9c4b93cd2a214e48a5fc4ca0
[imagemagick] / magick / nt-base.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                                 N   N  TTTTT                                %
7 %                                 NN  N    T                                  %
8 %                                 N N N    T                                  %
9 %                                 N  NN    T                                  %
10 %                                 N   N    T                                  %
11 %                                                                             %
12 %                                                                             %
13 %                   Windows NT Utility Methods for MagickCore                 %
14 %                                                                             %
15 %                               Software Design                               %
16 %                                 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 \f
61 /*
62   Define declarations.
63 */
64 #if !defined(MAP_FAILED)
65 #define MAP_FAILED      ((void *) -1)
66 #endif
67 \f
68 /*
69   Static declarations.
70 */
71 #if !defined(MAGICKCORE_LTDL_DELEGATE)
72 static char
73   *lt_slsearchpath = (char *) NULL;
74 #endif
75
76 static GhostscriptVectors
77   gs_vectors;
78
79 static void
80   *gs_dll_handle = (void *) NULL;
81 \f
82 /*
83   External declarations.
84 */
85 #if !defined(__WINDOWS__)
86 extern "C" BOOL WINAPI
87   DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved);
88 #endif
89 \f
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 )
134 BOOL 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 \f
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 */
231 MagickExport int Exit(int status)
232 {
233   if (IsWindows95())
234     TerminateProcess(GetCurrentProcess(),(unsigned int) status);
235   exit(status);
236   return(0);
237 }
238 \f
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 */
257 MagickExport 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 \f
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 */
292 MagickExport 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 \f
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 */
323 MagickExport int NTCloseLibrary(void *handle)
324 {
325   if (IsWindows95())
326     return(FreeLibrary((HINSTANCE) handle));
327   return(!(FreeLibrary((HINSTANCE) handle)));
328 }
329 \f
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
350 static BOOL ControlHandler(DWORD type)
351 {
352   AsynchronousDestroyMagickResources();
353   return(FALSE);
354 }
355
356 MagickExport int NTControlHandler(void)
357 {
358   return(SetConsoleCtrlHandler((PHANDLER_ROUTINE) ControlHandler,TRUE));
359 }
360 \f
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 */
380 MagickExport 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 \f
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 */
427 MagickExport 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 \f
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 */
479 MagickExport int NTExitLibrary(void)
480 {
481   return(0);
482 }
483 \f
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 */
509 MagickExport 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 \f
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 */
564 MagickExport MagickBooleanType NTGetExecutionPath(char *path,
565   const size_t extent)
566 {
567   GetModuleFileName(0,path,(DWORD) extent);
568   return(MagickTrue);
569 }
570 \f
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 */
589 char *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 \f
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 */
633 MagickExport 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 \f
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 */
674 void *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 \f
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 */
709 MagickExport 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 \f
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 %
741 %  NTGhostscriptDLL() obtains the path to the latest Ghostscript DLL.  The
742 %  method returns MagickFalse if a value is not obtained.
743 %
744 %  The format of the NTGhostscriptDLL method is:
745 %
746 %      int NTGhostscriptDLL(char *path, int length)
747 %
748 %  A description of each parameter follows:
749 %
750 %    o path: Pointer to buffer in which to return result.
751 %
752 %    o length: Length of buffer
753 %
754 */
755
756 #define GS_PRODUCT_AFPL "AFPL Ghostscript"
757 #define GS_PRODUCT_ALADDIN "Aladdin Ghostscript"
758 #define GS_PRODUCT_GNU "GNU Ghostscript"
759 #define GS_PRODUCT_GPL "GPL Ghostscript"
760 #define GS_MINIMUM_VERSION 550
761
762 /*
763   Get Ghostscript versions for given product.
764   Store results starting at pver + 1 + offset.
765   Returns total number of versions in pver.
766 */
767 static int NTGhostscriptProductVersions(int *pver,int offset,
768   const char *gs_productfamily)
769 {
770   HKEY
771     hkey,
772     hkeyroot;
773
774   DWORD
775     cbData;
776
777   char
778     key[MaxTextExtent],
779     *p;
780
781   int
782     n = 0,
783     ver;
784
785   (void) FormatMagickString(key,MaxTextExtent,"Software\\%s",gs_productfamily);
786   hkeyroot = HKEY_LOCAL_MACHINE;
787   if (RegOpenKeyExA(hkeyroot,key,0,KEY_READ,&hkey) == ERROR_SUCCESS)
788     {
789       /*
790         Now enumerate the keys.
791       */
792       cbData = sizeof(key) / sizeof(char);
793       while (RegEnumKeyA(hkey,n,key,cbData) == ERROR_SUCCESS)
794       {
795         n++;
796         ver = 0;
797         p = key;
798         while (*p && (*p!='.')) {
799           ver = (ver * 10) + (*p - '0')*100;
800           p++;
801         }
802         if (*p == '.')
803           p++;
804         if (*p)
805           {
806             ver+=10*(*p-'0');
807             p++;
808           }
809         if (*p)
810           ver+=(*p - '0');
811         if ((n+offset) < pver[0])
812           pver[n+offset]=ver;
813       }
814       RegCloseKey(hkey);
815     }
816   return(n+offset);
817 }
818
819 /* Query registry to find which versions of Ghostscript are installed.
820  * Return version numbers in an integer array.
821  * On entry, the first element in the array must be the array size
822  * in elements.
823  * If all is well, TRUE is returned.
824  * On exit, the first element is set to the number of Ghostscript
825  * versions installed, and subsequent elements to the version
826  * numbers of Ghostscript.
827  * e.g. on entry {5, 0, 0, 0, 0}, on exit {3, 550, 600, 596, 0}
828  * Returned version numbers may not be sorted.
829  *
830  * If Ghostscript is not installed at all, return FALSE
831  * and set pver[0] to 0.
832  * If the array is not large enough, return FALSE
833  * and set pver[0] to the number of Ghostscript versions installed.
834  */
835
836 static int NTGhostscriptEnumerateVersions(int *pver)
837 {
838   int
839     n;
840
841   assert(pver != (int *) NULL);
842   n=NTGhostscriptProductVersions(pver,0,GS_PRODUCT_AFPL);
843   n=NTGhostscriptProductVersions(pver,n,GS_PRODUCT_ALADDIN);
844   n=NTGhostscriptProductVersions(pver,n,GS_PRODUCT_GNU);
845   n=NTGhostscriptProductVersions(pver,n,GS_PRODUCT_GPL);
846   if (n >= pver[0])
847     {
848       pver[0]=n;
849       return(FALSE);  /* too small */
850     }
851   if (n == 0)
852     {
853       pver[0] = 0;
854       return(FALSE);  /* not installed */
855     }
856   pver[0]=n;
857   return(TRUE);
858 }
859
860 /*
861  Get a named registry value.
862  Key = hkeyroot\\key, named value = name.
863  name, ptr, plen and return values are the same as in gp_getenv();
864 */
865 static int NTGetRegistryValue(HKEY hkeyroot,const char *key,const char *name,
866   char *ptr,int *plen)
867 {
868   HKEY
869     hkey;
870
871   DWORD
872     cbData,
873     keytype;
874
875   BYTE
876     b,
877     *bptr = (BYTE *)ptr;
878
879   LONG
880     rc;
881
882   if (RegOpenKeyExA(hkeyroot,key,0,KEY_READ,&hkey) == ERROR_SUCCESS)
883     {
884       keytype = REG_SZ;
885       cbData = *plen;
886       if (bptr == (BYTE *)NULL)
887         bptr=(&b);  /* Registry API won't return ERROR_MORE_DATA */
888       /* if ptr is NULL */
889       rc=RegQueryValueExA(hkey,(char *) name,0,&keytype,bptr,&cbData);
890       RegCloseKey(hkey);
891       if (rc == ERROR_SUCCESS)
892         {
893           *plen = cbData;
894           return 0;  /* found environment variable and copied it */
895         } else if (rc == ERROR_MORE_DATA) {
896         /* buffer wasn't large enough */
897           *plen = cbData;
898           return -1;
899         }
900     }
901   return(1);  /* not found */
902 }
903
904 static int NTGhostscriptGetProductString(int gs_revision,const char *name,
905   char *ptr,int len,const char *gs_productfamily)
906 {
907   /* If using Win32, look in the registry for a value with
908    * the given name.  The registry value will be under the key
909    * HKEY_CURRENT_USER\Software\AFPL Ghostscript\N.NN
910    * or if that fails under the key
911    * HKEY_LOCAL_MACHINE\Software\AFPL Ghostscript\N.NN
912    * where "AFPL Ghostscript" is actually gs_productfamily
913    * and N.NN is obtained from gs_revision.
914    */
915
916   char
917     dotversion[MaxTextExtent],
918     key[MaxTextExtent];
919
920   int
921     code,
922     length;
923
924   DWORD version = GetVersion();
925
926   if (((HIWORD(version) & 0x8000) != 0) && ((HIWORD(version) & 0x4000) == 0))
927     {
928       /* Win32s */
929       return FALSE;
930     }
931   (void) FormatMagickString(dotversion,MaxTextExtent,"%d.%02d",(int)
932     (gs_revision/100),(int) (gs_revision % 100));
933   (void) FormatMagickString(key,MaxTextExtent,"Software\\%s\\%s",
934     gs_productfamily,dotversion);
935   length = len;
936   code = NTGetRegistryValue(HKEY_CURRENT_USER, key, name, ptr, &length);
937   if ( code == 0 )
938     return TRUE;  /* found it */
939   length = len;
940   code = NTGetRegistryValue(HKEY_LOCAL_MACHINE, key, name, ptr, &length);
941   if ( code == 0 )
942     return TRUE;  /* found it */
943   return FALSE;
944 }
945
946 static int NTGhostscriptGetString(int gs_revision,const char *name,char *ptr,
947   int len)
948 {
949   if (NTGhostscriptGetProductString(gs_revision,name,ptr,len,GS_PRODUCT_AFPL))
950     return(TRUE);
951   if (NTGhostscriptGetProductString(gs_revision,name,ptr,len,GS_PRODUCT_ALADDIN))
952     return(TRUE);
953   if (NTGhostscriptGetProductString(gs_revision,name,ptr,len,GS_PRODUCT_GNU))
954     return(TRUE);
955   if (NTGhostscriptGetProductString(gs_revision,name,ptr,len,GS_PRODUCT_GPL))
956     return(TRUE);
957   return(FALSE);
958 }
959
960 static int NTGetLatestGhostscript( void )
961 {
962   int
963     count,
964     i,
965     version,
966     *ver;
967
968   DWORD version = GetVersion();
969   if ( ((HIWORD(version) & 0x8000)!=0) && ((HIWORD(version) & 0x4000)==0) )
970     return FALSE;  /* win32s */
971
972   count = 1;
973   NTGhostscriptEnumerateVersions(&count);
974   if (count < 1)
975     return FALSE;
976   ver=(int *) AcquireQuantumMemory(count+1UL,sizeof(*ver));
977   if (ver == (int *)NULL)
978     return(FALSE);
979   ver[0]=count+1;
980   if (!NTGhostscriptEnumerateVersions(ver))
981     {
982       ver=(int *) RelinquishMagickMemory(ver);
983       return FALSE;
984     }
985   version = 0;
986   for (i=1; i<=ver[0]; i++) {
987     if (ver[i] > version)
988       version = ver[i];
989   }
990   ver=(int *) RelinquishMagickMemory(ver);
991   return(version);
992 }
993
994 \f
995 /*
996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
997 %                                                                             %
998 %                                                                             %
999 %                                                                             %
1000 %   N T G h o s t s c r i p t D L L                                           %
1001 %                                                                             %
1002 %                                                                             %
1003 %                                                                             %
1004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1005 %
1006 %  NTGhostscriptDLL() obtains the path to the latest Ghostscript DLL.  The
1007 %  method returns MagickFalse if a value is not obtained.
1008 %
1009 %  The format of the NTGhostscriptDLL method is:
1010 %
1011 %      int NTGhostscriptDLL( char *path, int length)
1012 %
1013 %  A description of each parameter follows:
1014 %
1015 %    o path: Pointer to path buffer to update
1016 %
1017 %    o length: Allocation size of path buffer.
1018 %
1019 */
1020 MagickExport int NTGhostscriptDLL(char *path, int length)
1021 {
1022   int
1023     version;
1024
1025   char
1026     buf[256];
1027
1028   *path='\0';
1029   version = NTGetLatestGhostscript();
1030   if ((version == FALSE) || (version < GS_MINIMUM_VERSION))
1031     return FALSE;
1032
1033   if (!NTGhostscriptGetString(version, "GS_DLL", buf, sizeof(buf)))
1034     return FALSE;
1035
1036   (void) CopyMagickString(path,buf,length+1);
1037   return(TRUE);
1038 }
1039 \f
1040 /*
1041 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1042 %                                                                             %
1043 %                                                                             %
1044 %                                                                             %
1045 %   N T G h o s t s c r i p t D L L V e c t o r s                             %
1046 %                                                                             %
1047 %                                                                             %
1048 %                                                                             %
1049 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1050 %
1051 %  NTGhostscriptDLLVectors() returns a GhostscriptVectors structure containing
1052 %  function vectors to invoke Ghostscript DLL functions. A null pointer is
1053 %  returned if there is an error with loading the DLL or retrieving the
1054 %  function vectors.
1055 %
1056 %  The format of the NTGhostscriptDLLVectors method is:
1057 %
1058 %      const GhostscriptVectors *NTGhostscriptDLLVectors(void)
1059 %
1060 */
1061 MagickExport const GhostscriptVectors *NTGhostscriptDLLVectors( void )
1062 {
1063   if (NTGhostscriptLoadDLL())
1064     return(&gs_vectors);
1065   return((GhostscriptVectors*) NULL);
1066 }
1067 \f
1068 /*
1069 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1070 %                                                                             %
1071 %                                                                             %
1072 %                                                                             %
1073 %   N T G h o s t s c r i p t E X E                                           %
1074 %                                                                             %
1075 %                                                                             %
1076 %                                                                             %
1077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1078 %
1079 %  NTGhostscriptEXE() obtains the path to the latest Ghostscript executable.
1080 %  The method returns MagickFalse if a value is not obtained.
1081 %
1082 %  The format of the NTGhostscriptEXE method is:
1083 %
1084 %      int NTGhostscriptEXE(char *path, int length)
1085 %
1086 %  A description of each parameter follows:
1087 %
1088 %    o path: pointer to buffer in which to return result.
1089 %
1090 %    o length: length of buffer.
1091 %
1092 */
1093 MagickExport int NTGhostscriptEXE(char *path,int length)
1094 {
1095   char
1096     buffer[MaxTexytExtent],
1097     *p;
1098
1099   int
1100     version;
1101
1102   (void) CopyMagickString(path,"gswin32c.exe",length);
1103   version=NTGetLatestGhostscript();
1104   if ((version == FALSE) || (version < GS_MINIMUM_VERSION))
1105     return(FALSE);
1106   if (NTGhostscriptGetString(version,"GS_DLL",buffer,sizeof(buffer)) == 0)
1107     return(FALSE);
1108   p=strrchr(buffer, '\\');
1109   if (p == (char *) NULL)
1110     return(FALSE);
1111   p++;
1112   *p='\0';
1113   (void) CopyMagickString(p,path,sizeof(buffer)-strlen(buffer));
1114   (void) CopyMagickString(path,buffer,length);
1115   return(TRUE);
1116 }
1117 \f
1118 /*
1119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1120 %                                                                             %
1121 %                                                                             %
1122 %                                                                             %
1123 %   N T G h o s t s c r i p t F o n t s                                       %
1124 %                                                                             %
1125 %                                                                             %
1126 %                                                                             %
1127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1128 %
1129 %  NTGhostscriptFonts() gets the path to the Ghostscript fonts.  The method
1130 %  returns MagickFalse if an error occurs otherwise MagickTrue.
1131 %
1132 %  The format of the NTGhostscriptFonts method is:
1133 %
1134 %      int NTGhostscriptFonts(char *path,int length)
1135 %
1136 %  A description of each parameter follows:
1137 %
1138 %    o path: Pointer to buffer in which to return result.
1139 %
1140 %    o length: Length of buffer.
1141 %
1142 */
1143 MagickExport int NTGhostscriptFonts(char *path,int length)
1144 {
1145   char
1146     buffer[MaxTextExtent],
1147     filename[MaxTextExtent];
1148
1149   int
1150     version;
1151
1152   register char
1153     *p,
1154     *q;
1155
1156   *path='\0';
1157   version=NTGetLatestGhostscript();
1158   if ((version == FALSE) || (version < GS_MINIMUM_VERSION))
1159     return(FALSE);
1160   if (!NTGhostscriptGetString(version,"GS_LIB",buffer,MaxTextExtent))
1161     return(FALSE);
1162   for (p=buffer-1; p != (char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1163   {
1164     (void) CopyMagickString(path,p+1,length+1);
1165     q=strchr(path,DirectoryListSeparator);
1166     if (q != (char *) NULL)
1167       *q='\0';
1168     FormatMagickString(filename,MaxTextExtent,"%s%sfonts.dir",path,
1169       DirectorySeparator);
1170     if (IsPathAccessible(filename) != MagickFalse)
1171       return(TRUE);
1172   }
1173   return(FALSE);
1174 }
1175 \f
1176 /*
1177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1178 %                                                                             %
1179 %                                                                             %
1180 %                                                                             %
1181 %   N T G h o s t s c r i p t L o a d D L L                                   %
1182 %                                                                             %
1183 %                                                                             %
1184 %                                                                             %
1185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1186 %
1187 %  NTGhostscriptLoadDLL() attempts to load the Ghostscript DLL and returns
1188 %  MagickTrue if it succeeds.
1189 %
1190 %  The format of the NTGhostscriptLoadDLL method is:
1191 %
1192 %      int NTGhostscriptLoadDLL(void)
1193 %
1194 */
1195 MagickExport int NTGhostscriptLoadDLL(void)
1196 {
1197   char
1198     module_path[MaxTextExtent];
1199
1200   if (gs_dll_handle != (void *) NULL)
1201     return(MagickTrue);
1202   if (NTGhostscriptDLL(module_path,sizeof(module_path)) == MagickFalse)
1203     return(MagickFalse);
1204   gs_dll_handle=NTOpenLibrary(module_path);
1205   if (gs_dll_handle == (void *) NULL)
1206     return(MagickFalse);
1207   (void) ResetMagickMemory((void *) &gs_vectors,0,sizeof(GhostscriptVectors));
1208   gs_vectors.exit=(int (MagickDLLCall *)
1209     (gs_main_instance *)) NTGetLibrarySymbol(gs_dll_handle,"gsapi_exit");
1210   gs_vectors.init_with_args=(int (MagickDLLCall *)
1211     (gs_main_instance *,int,char **))
1212     (NTGetLibrarySymbol(gs_dll_handle,"gsapi_init_with_args"));
1213   gs_vectors.new_instance=(int (MagickDLLCall *) (gs_main_instance **,void *))
1214     (NTGetLibrarySymbol(gs_dll_handle,"gsapi_new_instance"));
1215   gs_vectors.run_string=(int (MagickDLLCall *)
1216     (gs_main_instance *,const char *,int,int *))
1217     (NTGetLibrarySymbol(gs_dll_handle,"gsapi_run_string"));
1218   gs_vectors.delete_instance=(void (MagickDLLCall *)(gs_main_instance *))
1219     (NTGetLibrarySymbol(gs_dll_handle,"gsapi_delete_instance"));
1220   if ((gs_vectors.exit == NULL) ||
1221       (gs_vectors.init_with_args == NULL) ||
1222       (gs_vectors.new_instance == NULL) ||
1223       (gs_vectors.run_string == NULL) ||
1224       (gs_vectors.delete_instance == NULL))
1225     return(MagickFalse);
1226   return(MagickTrue);
1227 }
1228 \f
1229 /*
1230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1231 %                                                                             %
1232 %                                                                             %
1233 %                                                                             %
1234 %   N T G h o s t s c r i p t U n L o a d D L L                               %
1235 %                                                                             %
1236 %                                                                             %
1237 %                                                                             %
1238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1239 %
1240 %  NTGhostscriptUnLoadDLL() unloads the Ghostscript DLL if it is loaded.
1241 %
1242 %  The format of the NTGhostscriptUnLoadDLL method is:
1243 %
1244 %      int NTGhostscriptUnLoadDLL(void)
1245 %
1246 */
1247 MagickExport int NTGhostscriptUnLoadDLL(void)
1248 {
1249   if (gs_dll_handle == (void *) NULL)
1250     return(MagickFalse);
1251   NTCloseLibrary(gs_dll_handle);
1252   gs_dll_handle=(void *) NULL;
1253   (void) ResetMagickMemory((void *) &gs_vectors,0,sizeof(GhostscriptVectors));
1254   return(MagickTrue);
1255 }
1256 \f
1257 /*
1258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1259 %                                                                             %
1260 %                                                                             %
1261 %                                                                             %
1262 %   N T I n i t i a l i z e L i b r a r y                                     %
1263 %                                                                             %
1264 %                                                                             %
1265 %                                                                             %
1266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1267 %
1268 %  NTInitializeLibrary() initializes the dynamic module loading subsystem.
1269 %
1270 %  The format of the NTInitializeLibrary method is:
1271 %
1272 %      int NTInitializeLibrary(void)
1273 %
1274 */
1275 MagickExport int NTInitializeLibrary(void)
1276 {
1277   return(0);
1278 }
1279 \f
1280 /*
1281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1282 %                                                                             %
1283 %                                                                             %
1284 %                                                                             %
1285 +  N T M a p M e m o r y                                                      %
1286 %                                                                             %
1287 %                                                                             %
1288 %                                                                             %
1289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1290 %
1291 %  Mmap() emulates the Unix method of the same name.
1292 %
1293 %  The format of the NTMapMemory method is:
1294 %
1295 %    MagickExport void *NTMapMemory(char *address,size_t length,int protection,
1296 %      int access,int file,MagickOffsetType offset)
1297 %
1298 */
1299 MagickExport void *NTMapMemory(char *address,size_t length,int protection,
1300   int flags,int file,MagickOffsetType offset)
1301 {
1302   DWORD
1303     access_mode,
1304     high_length,
1305     high_offset,
1306     low_length,
1307     low_offset,
1308     protection_mode;
1309
1310   HANDLE
1311     file_handle,
1312     map_handle;
1313
1314   void
1315     *map;
1316
1317   access_mode=0;
1318   file_handle=INVALID_HANDLE_VALUE;
1319   low_length=(DWORD) (length & 0xFFFFFFFFUL);
1320   high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1321   map_handle=INVALID_HANDLE_VALUE;
1322   map=(void *) NULL;
1323   low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1324   high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1325   protection_mode=0;
1326   if (protection & PROT_WRITE)
1327     {
1328       access_mode=FILE_MAP_WRITE;
1329       if (!(flags & MAP_PRIVATE))
1330         protection_mode=PAGE_READWRITE;
1331       else
1332         {
1333           access_mode=FILE_MAP_COPY;
1334           protection_mode=PAGE_WRITECOPY;
1335         }
1336     }
1337   else
1338     if (protection & PROT_READ)
1339       {
1340         access_mode=FILE_MAP_READ;
1341         protection_mode=PAGE_READONLY;
1342       }
1343   if ((file == -1) && (flags & MAP_ANONYMOUS))
1344     file_handle=INVALID_HANDLE_VALUE;
1345   else
1346     file_handle=(HANDLE) _get_osfhandle(file);
1347   map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1348     low_length,0);
1349   if (map_handle)
1350     {
1351       map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1352         length);
1353       CloseHandle(map_handle);
1354     }
1355   if (map == (void *) NULL)
1356     return((void *) MAP_FAILED);
1357   return((void *) ((char *) map));
1358 }
1359 \f
1360 /*
1361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1362 %                                                                             %
1363 %                                                                             %
1364 %                                                                             %
1365 %   N T O p e n D i r e c t o r y                                             %
1366 %                                                                             %
1367 %                                                                             %
1368 %                                                                             %
1369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1370 %
1371 %  NTOpenDirectory() opens the directory named by filename and associates a
1372 %  directory stream with it.
1373 %
1374 %  The format of the NTOpenDirectory method is:
1375 %
1376 %      DIR *NTOpenDirectory(const char *path)
1377 %
1378 %  A description of each parameter follows:
1379 %
1380 %    o entry: Specifies a pointer to a DIR structure.
1381 %
1382 */
1383 MagickExport DIR *NTOpenDirectory(const char *path)
1384 {
1385   char
1386     file_specification[MaxTextExtent];
1387
1388   DIR
1389     *entry;
1390
1391   size_t
1392     length;
1393
1394   assert(path != (const char *) NULL);
1395   length=CopyMagickString(file_specification,path,MaxTextExtent);
1396   if (length >= MaxTextExtent)
1397     return((DIR *) NULL);
1398   length=ConcatenateMagickString(file_specification,DirectorySeparator,
1399     MaxTextExtent);
1400   if (length >= MaxTextExtent)
1401     return((DIR *) NULL);
1402   entry=(DIR *) AcquireMagickMemory(sizeof(DIR));
1403   if (entry != (DIR *) NULL)
1404     {
1405       entry->firsttime=TRUE;
1406       entry->hSearch=FindFirstFile(file_specification,&entry->Win32FindData);
1407     }
1408   if (entry->hSearch == INVALID_HANDLE_VALUE)
1409     {
1410       length=ConcatenateMagickString(file_specification,"\\*.*",MaxTextExtent);
1411       if (length >= MaxTextExtent)
1412         {
1413           entry=(DIR *) RelinquishMagickMemory(entry);
1414           return((DIR *) NULL);
1415         }
1416       entry->hSearch=FindFirstFile(file_specification,&entry->Win32FindData);
1417       if (entry->hSearch == INVALID_HANDLE_VALUE)
1418         {
1419           entry=(DIR *) RelinquishMagickMemory(entry);
1420           return((DIR *) NULL);
1421         }
1422     }
1423   return(entry);
1424 }
1425 \f
1426 /*
1427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1428 %                                                                             %
1429 %                                                                             %
1430 %                                                                             %
1431 %   N T O p e n L i b r a r y                                                 %
1432 %                                                                             %
1433 %                                                                             %
1434 %                                                                             %
1435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1436 %
1437 %  NTOpenLibrary() loads a dynamic module into memory and returns a handle that
1438 %  can be used to access the various procedures in the module.
1439 %
1440 %  The format of the NTOpenLibrary method is:
1441 %
1442 %      void *NTOpenLibrary(const char *filename)
1443 %
1444 %  A description of each parameter follows:
1445 %
1446 %    o path: Specifies a pointer to string representing dynamic module that
1447 %      is to be loaded.
1448 %
1449 */
1450
1451 static const char *GetSearchPath( void )
1452 {
1453 #if defined(MAGICKCORE_LTDL_DELEGATE)
1454   return(lt_dlgetsearchpath());
1455 #else
1456   return(lt_slsearchpath);
1457 #endif
1458 }
1459
1460 MagickExport void *NTOpenLibrary(const char *filename)
1461 {
1462 #define MaxPathElements  31
1463
1464   char
1465     buffer[MaxTextExtent];
1466
1467   int
1468     index;
1469
1470   register const char
1471     *p,
1472     *q;
1473
1474   register int
1475     i;
1476
1477   UINT
1478     mode;
1479
1480   void
1481     *handle;
1482
1483   mode=SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
1484   handle=(void *) LoadLibraryEx(filename,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1485   if ((handle != (void *) NULL) || (GetSearchPath() == (char *) NULL))
1486     {
1487       SetErrorMode(mode);
1488       return(handle);
1489     }
1490   p=(char *) GetSearchPath();
1491   index=0;
1492   while (index < MaxPathElements)
1493   {
1494     q=strchr(p,DirectoryListSeparator);
1495     if (q == (char *) NULL)
1496       {
1497         (void) CopyMagickString(buffer,p,MaxTextExtent);
1498         (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1499         (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1500         handle=(void *) LoadLibraryEx(buffer,NULL,
1501           LOAD_WITH_ALTERED_SEARCH_PATH);
1502         break;
1503       }
1504     i=q-p;
1505     (void) CopyMagickString(buffer,p,i+1);
1506     (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1507     (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1508     handle=(void *) LoadLibraryEx(buffer,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1509     if (handle != (void *) NULL)
1510       break;
1511     p=q+1;
1512   }
1513   SetErrorMode(mode);
1514   return(handle);
1515 }
1516 \f
1517 /*
1518 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1519 %                                                                             %
1520 %                                                                             %
1521 %                                                                             %
1522 %    N T R e a d D i r e c t o r y                                            %
1523 %                                                                             %
1524 %                                                                             %
1525 %                                                                             %
1526 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1527 %
1528 %  NTReadDirectory() returns a pointer to a structure representing the
1529 %  directory entry at the current position in the directory stream to which
1530 %  entry refers.
1531 %
1532 %  The format of the NTReadDirectory
1533 %
1534 %      NTReadDirectory(entry)
1535 %
1536 %  A description of each parameter follows:
1537 %
1538 %    o entry: Specifies a pointer to a DIR structure.
1539 %
1540 */
1541 MagickExport struct dirent *NTReadDirectory(DIR *entry)
1542 {
1543   int
1544     status;
1545
1546   size_t
1547     length;
1548
1549   if (entry == (DIR *) NULL)
1550     return((struct dirent *) NULL);
1551   if (!entry->firsttime)
1552     {
1553       status=FindNextFile(entry->hSearch,&entry->Win32FindData);
1554       if (status == 0)
1555         return((struct dirent *) NULL);
1556     }
1557   length=CopyMagickString(entry->file_info.d_name,
1558     entry->Win32FindData.cFileName,sizeof(entry->file_info.d_name));
1559   if (length >= sizeof(entry->file_info.d_name))
1560     return((struct dirent *) NULL);
1561   entry->firsttime=FALSE;
1562   entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1563   return(&entry->file_info);
1564 }
1565 \f
1566 /*
1567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1568 %                                                                             %
1569 %                                                                             %
1570 %                                                                             %
1571 %   N T R e g i s t r y K e y L o o k u p                                     %
1572 %                                                                             %
1573 %                                                                             %
1574 %                                                                             %
1575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1576 %
1577 %  NTRegistryKeyLookup() returns ImageMagick installation path settings
1578 %  stored in the Windows Registry.  Path settings are specific to the
1579 %  installed ImageMagick version so that multiple Image Magick installations
1580 %  may coexist.
1581 %
1582 %  Values are stored in the registry under a base path path similar to
1583 %  "HKEY_LOCAL_MACHINE/SOFTWARE\ImageMagick\5.5.7\Q:16". The provided subkey
1584 %  is appended to this base path to form the full key.
1585 %
1586 %  The format of the NTRegistryKeyLookup method is:
1587 %
1588 %      unsigned char *NTRegistryKeyLookup(const char *subkey)
1589 %
1590 %  A description of each parameter follows:
1591 %
1592 %    o subkey: Specifies a string that identifies the registry object.
1593 %      Currently supported sub-keys include: "BinPath", "ConfigurePath",
1594 %      "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
1595 %
1596 */
1597 MagickExport unsigned char *NTRegistryKeyLookup(const char *subkey)
1598 {
1599   char
1600     package_key[MaxTextExtent];
1601
1602   DWORD
1603     size,
1604     type;
1605
1606   HKEY
1607     registry_key;
1608
1609   LONG
1610     status;
1611
1612   unsigned char
1613     *value;
1614
1615   /*
1616     Look-up base key.
1617   */
1618   (void) FormatMagickString(package_key,MaxTextExtent,"SOFTWARE\\%s\\%s\\Q:%d",
1619     MagickPackageName,MagickLibVersionText,MAGICKCORE_QUANTUM_DEPTH);
1620   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",package_key);
1621   registry_key=(HKEY) INVALID_HANDLE_VALUE;
1622   status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,package_key,0,KEY_READ,&registry_key);
1623   if (status != ERROR_SUCCESS)
1624     {
1625       registry_key=(HKEY) INVALID_HANDLE_VALUE;
1626       return((unsigned char *) NULL);
1627     }
1628   /*
1629     Look-up sub key.
1630   */
1631   size=32;
1632   value=(unsigned char *) AcquireQuantumMemory(size,sizeof(*value));
1633   if (value == (unsigned char *) NULL)
1634     {
1635       RegCloseKey(registry_key);
1636       return((unsigned char *) NULL);
1637     }
1638   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",subkey);
1639   status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1640   if ((status == ERROR_MORE_DATA) && (type == REG_SZ))
1641     {
1642       value=(unsigned char *) ResizeQuantumMemory(value,size,sizeof(*value));
1643       if (value == (BYTE *) NULL)
1644         {
1645           RegCloseKey(registry_key);
1646           return((unsigned char *) NULL);
1647         }
1648       status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1649     }
1650   RegCloseKey(registry_key);
1651   if ((type != REG_SZ) || (status != ERROR_SUCCESS))
1652     value=(unsigned char *) RelinquishMagickMemory(value);
1653   return((unsigned char *) value);
1654 }
1655 \f
1656 /*
1657 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1658 %                                                                             %
1659 %                                                                             %
1660 %                                                                             %
1661 %   N T R e p o r t E v e n t                                                 %
1662 %                                                                             %
1663 %                                                                             %
1664 %                                                                             %
1665 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1666 %
1667 %  NTReportEvent() reports an event.
1668 %
1669 %  The format of the NTReportEvent method is:
1670 %
1671 %      MagickBooleanType NTReportEvent(const char *event,
1672 %        const MagickBooleanType error)
1673 %
1674 %  A description of each parameter follows:
1675 %
1676 %    o event: the event.
1677 %
1678 %    o error: MagickTrue the event is an error.
1679 %
1680 */
1681 MagickExport MagickBooleanType NTReportEvent(const char *event,
1682   const MagickBooleanType error)
1683 {
1684   const char
1685     *events[1];
1686
1687   HANDLE
1688     handle;
1689
1690   WORD
1691     type;
1692
1693   handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
1694   if (handle == NULL)
1695     return(MagickFalse);
1696   events[0]=event;
1697   type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
1698   ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
1699   DeregisterEventSource(handle);
1700   return(MagickTrue);
1701 }
1702 \f
1703 /*
1704 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1705 %                                                                             %
1706 %                                                                             %
1707 %                                                                             %
1708 %   N T R e s o u r c e T o B l o b                                           %
1709 %                                                                             %
1710 %                                                                             %
1711 %                                                                             %
1712 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1713 %
1714 %  NTResourceToBlob() returns a blob containing the contents of the resource
1715 %  in the current executable specified by the id parameter. This currently
1716 %  used to retrieve MGK files tha have been embedded into the various command
1717 %  line utilities.
1718 %
1719 %  The format of the NTResourceToBlob method is:
1720 %
1721 %      unsigned char *NTResourceToBlob(const char *id)
1722 %
1723 %  A description of each parameter follows:
1724 %
1725 %    o id: Specifies a string that identifies the resource.
1726 %
1727 */
1728 MagickExport unsigned char *NTResourceToBlob(const char *id)
1729 {
1730   char
1731     path[MaxTextExtent];
1732
1733   DWORD
1734     length;
1735
1736   HGLOBAL
1737     global;
1738
1739   HMODULE
1740     handle;
1741
1742   HRSRC
1743     resource;
1744
1745   unsigned char
1746     *blob,
1747     *value;
1748
1749   assert(id != (const char *) NULL);
1750   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1751   (void) FormatMagickString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
1752     DirectorySeparator,GetClientName());
1753   if (IsPathAccessible(path) != MagickFalse)
1754     handle=GetModuleHandle(path);
1755   else
1756     handle=GetModuleHandle(0);
1757   if (!handle)
1758     return((unsigned char *) NULL);
1759   resource=FindResource(handle,id,"IMAGEMAGICK");
1760   if (!resource)
1761     return((unsigned char *) NULL);
1762   global=LoadResource(handle,resource);
1763   if (!global)
1764     return((unsigned char *) NULL);
1765   length=SizeofResource(handle,resource);
1766   value=(unsigned char *) LockResource(global);
1767   if (!value)
1768     {
1769       FreeResource(global);
1770       return((unsigned char *) NULL);
1771     }
1772   blob=(unsigned char *) AcquireQuantumMemory(length+MaxTextExtent,
1773     sizeof(*blob));
1774   if (blob != (unsigned char *) NULL)
1775     {
1776       (void) CopyMagickMemory(blob,value,length);
1777       blob[length]='\0';
1778     }
1779   UnlockResource(global);
1780   FreeResource(global);
1781   return(blob);
1782 }
1783 \f
1784 /*
1785 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1786 %                                                                             %
1787 %                                                                             %
1788 %                                                                             %
1789 %   N T S e e k D i r e c t o r y                                             %
1790 %                                                                             %
1791 %                                                                             %
1792 %                                                                             %
1793 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1794 %
1795 %  NTSeekDirectory() sets the position of the next NTReadDirectory() operation
1796 %  on the directory stream.
1797 %
1798 %  The format of the NTSeekDirectory method is:
1799 %
1800 %      void NTSeekDirectory(DIR *entry,long position)
1801 %
1802 %  A description of each parameter follows:
1803 %
1804 %    o entry: Specifies a pointer to a DIR structure.
1805 %
1806 %    o position: specifies the position associated with the directory
1807 %      stream.
1808 %
1809 */
1810 MagickExport void NTSeekDirectory(DIR *entry,long position)
1811 {
1812   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1813   assert(entry != (DIR *) NULL);
1814 }
1815 \f
1816 /*
1817 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1818 %                                                                             %
1819 %                                                                             %
1820 %                                                                             %
1821 %   N T S e t S e a r c h P a t h                                             %
1822 %                                                                             %
1823 %                                                                             %
1824 %                                                                             %
1825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1826 %
1827 %  NTSetSearchPath() sets the current locations that the subsystem should
1828 %  look at to find dynamically loadable modules.
1829 %
1830 %  The format of the NTSetSearchPath method is:
1831 %
1832 %      int NTSetSearchPath(const char *path)
1833 %
1834 %  A description of each parameter follows:
1835 %
1836 %    o path: Specifies a pointer to string representing the search path
1837 %      for DLL's that can be dynamically loaded.
1838 %
1839 */
1840 MagickExport int NTSetSearchPath(const char *path)
1841 {
1842 #if defined(MAGICKCORE_LTDL_DELEGATE)
1843   lt_dlsetsearchpath(path);
1844 #else
1845   if (lt_slsearchpath != (char *) NULL)
1846     lt_slsearchpath=DestroyString(lt_slsearchpath);
1847   if (path != (char *) NULL)
1848     lt_slsearchpath=AcquireString(path);
1849 #endif
1850   return(0);
1851 }
1852 \f
1853 /*
1854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1855 %                                                                             %
1856 %                                                                             %
1857 %                                                                             %
1858 +  N T S y n c M e m o r y                                                    %
1859 %                                                                             %
1860 %                                                                             %
1861 %                                                                             %
1862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1863 %
1864 %  NTSyncMemory() emulates the Unix method of the same name.
1865 %
1866 %  The format of the NTSyncMemory method is:
1867 %
1868 %      int NTSyncMemory(void *address,size_t length,int flags)
1869 %
1870 %  A description of each parameter follows:
1871 %
1872 %    o address: the address of the binary large object.
1873 %
1874 %    o length: the length of the binary large object.
1875 %
1876 %    o flags: Option flags (ignored for Windows).
1877 %
1878 */
1879 MagickExport int NTSyncMemory(void *address,size_t length,int flags)
1880 {
1881   if (FlushViewOfFile(address,length) == MagickFalse)
1882     return(-1);
1883   return(0);
1884 }
1885 \f
1886 /*
1887 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1888 %                                                                             %
1889 %                                                                             %
1890 %                                                                             %
1891 %   N T S y s t e m C o m m a n d                                             %
1892 %                                                                             %
1893 %                                                                             %
1894 %                                                                             %
1895 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1896 %
1897 %  NTSystemCommand() executes the specified command and waits until it
1898 %  terminates.  The returned value is the exit status of the command.
1899 %
1900 %  The format of the NTSystemCommand method is:
1901 %
1902 %      int NTSystemCommand(const char *command)
1903 %
1904 %  A description of each parameter follows:
1905 %
1906 %    o command: This string is the command to execute.
1907 %
1908 */
1909 MagickExport int NTSystemCommand(const char *command)
1910 {
1911   char
1912     local_command[MaxTextExtent];
1913
1914   DWORD
1915     child_status;
1916
1917   int
1918     status;
1919
1920   MagickBooleanType
1921     background_process;
1922
1923   PROCESS_INFORMATION
1924     process_info;
1925
1926   STARTUPINFO
1927     startup_info;
1928
1929   if (command == (char *) NULL)
1930     return(-1);
1931   GetStartupInfo(&startup_info);
1932   startup_info.dwFlags=STARTF_USESHOWWINDOW;
1933   startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
1934   (void) CopyMagickString(local_command,command,MaxTextExtent);
1935   background_process=command[strlen(command)-1] == '&' ? MagickTrue :
1936     MagickFalse;
1937   if (background_process)
1938     local_command[strlen(command)-1]='\0';
1939   if (command[strlen(command)-1] == '|')
1940      local_command[strlen(command)-1]='\0';
1941    else
1942      startup_info.wShowWindow=SW_SHOWDEFAULT;
1943   status=CreateProcess((LPCTSTR) NULL,local_command,
1944     (LPSECURITY_ATTRIBUTES) NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) FALSE,
1945     (DWORD) NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
1946     &process_info);
1947   if (status == 0)
1948     return(-1);
1949   if (background_process)
1950     return(status == 0);
1951   status=WaitForSingleObject(process_info.hProcess,INFINITE);
1952   if (status != WAIT_OBJECT_0)
1953     return(status);
1954   status=GetExitCodeProcess(process_info.hProcess,&child_status);
1955   if (status == 0)
1956     return(-1);
1957   CloseHandle(process_info.hProcess);
1958   CloseHandle(process_info.hThread);
1959   return((int) child_status);
1960 }
1961 \f
1962 /*
1963 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1964 %                                                                             %
1965 %                                                                             %
1966 %                                                                             %
1967 %   N T S y s t e m C o n i f i g u r a t i o n                               %
1968 %                                                                             %
1969 %                                                                             %
1970 %                                                                             %
1971 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1972 %
1973 %  NTSystemConfiguration() provides a way for the application to determine
1974 %  values for system limits or options at runtime.
1975 %
1976 %  The format of the exit method is:
1977 %
1978 %      long NTSystemConfiguration(int name)
1979 %
1980 %  A description of each parameter follows:
1981 %
1982 %    o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
1983 %
1984 */
1985 MagickExport long NTSystemConfiguration(int name)
1986 {
1987   switch (name)
1988   {
1989     case _SC_PAGESIZE:
1990     {
1991       SYSTEM_INFO
1992         system_info;
1993
1994       GetSystemInfo(&system_info);
1995       return(system_info.dwPageSize);
1996     }
1997     case _SC_PHYS_PAGES:
1998     {
1999       HMODULE
2000         handle;
2001
2002       LPFNDLLFUNC2
2003         module;
2004
2005       NTMEMORYSTATUSEX
2006         status;
2007
2008       SYSTEM_INFO
2009         system_info;
2010
2011       handle=GetModuleHandle("kernel32.dll");
2012       if (handle == (HMODULE) NULL)
2013         return(0L);
2014       GetSystemInfo(&system_info);
2015       module=(LPFNDLLFUNC2) NTGetLibrarySymbol(handle,"GlobalMemoryStatusEx");
2016       if (module == (LPFNDLLFUNC2) NULL)
2017         {
2018           MEMORYSTATUS
2019             status;
2020
2021           GlobalMemoryStatus(&status);
2022           return((long) status.dwTotalPhys/system_info.dwPageSize);
2023         }
2024       status.dwLength=sizeof(status);
2025       if (module(&status) == 0)
2026         return(0L);
2027       return((long) status.ullTotalPhys/system_info.dwPageSize);
2028     }
2029     case _SC_OPEN_MAX:
2030       return(2048);
2031     default:
2032       break;
2033   }
2034   return(-1);
2035 }
2036 \f
2037 /*
2038 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2039 %                                                                             %
2040 %                                                                             %
2041 %                                                                             %
2042 %   N T T e l l D i r e c t o r y                                             %
2043 %                                                                             %
2044 %                                                                             %
2045 %                                                                             %
2046 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2047 %
2048 %  NTTellDirectory() returns the current location associated with the named
2049 %  directory stream.
2050 %
2051 %  The format of the NTTellDirectory method is:
2052 %
2053 %      long NTTellDirectory(DIR *entry)
2054 %
2055 %  A description of each parameter follows:
2056 %
2057 %    o entry: Specifies a pointer to a DIR structure.
2058 %
2059 */
2060 MagickExport long NTTellDirectory(DIR *entry)
2061 {
2062   assert(entry != (DIR *) NULL);
2063   return(0);
2064 }
2065 \f
2066 /*
2067 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2068 %                                                                             %
2069 %                                                                             %
2070 %                                                                             %
2071 %   N T T r u n c a t e F i l e                                               %
2072 %                                                                             %
2073 %                                                                             %
2074 %                                                                             %
2075 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2076 %
2077 %  NTTruncateFile() truncates a file to a specified length.
2078 %
2079 %  The format of the NTTruncateFile method is:
2080 %
2081 %      int NTTruncateFile(int file,off_t length)
2082 %
2083 %  A description of each parameter follows:
2084 %
2085 %    o file: the file.
2086 %
2087 %    o length: the file length.
2088 %
2089 */
2090 MagickExport int NTTruncateFile(int file,off_t length)
2091 {
2092   DWORD
2093     file_pointer;
2094
2095   long
2096     file_handle,
2097     high,
2098     low;
2099
2100   file_handle=_get_osfhandle(file);
2101   if (file_handle == -1L)
2102     return(-1);
2103   low=(long) (length & 0xffffffffUL);
2104   high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2105   file_pointer=SetFilePointer((HANDLE) file_handle,low,&high,FILE_BEGIN);
2106   if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2107     return(-1);
2108   if (SetEndOfFile((HANDLE) file_handle) == 0)
2109     return(-1);
2110   return(0);
2111 }
2112 \f
2113 /*
2114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2115 %                                                                             %
2116 %                                                                             %
2117 %                                                                             %
2118 +  N T U n m a p M e m o r y                                                  %
2119 %                                                                             %
2120 %                                                                             %
2121 %                                                                             %
2122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2123 %
2124 %  NTUnmapMemory() emulates the Unix munmap method.
2125 %
2126 %  The format of the NTUnmapMemory method is:
2127 %
2128 %      int NTUnmapMemory(void *map,size_t length)
2129 %
2130 %  A description of each parameter follows:
2131 %
2132 %    o map: the address of the binary large object.
2133 %
2134 %    o length: the length of the binary large object.
2135 %
2136 */
2137 MagickExport int NTUnmapMemory(void *map,size_t length)
2138 {
2139   if (UnmapViewOfFile(map) == 0)
2140     return(-1);
2141   return(0);
2142 }
2143 \f
2144 /*
2145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2146 %                                                                             %
2147 %                                                                             %
2148 %                                                                             %
2149 %   N T U s e r T i m e                                                       %
2150 %                                                                             %
2151 %                                                                             %
2152 %                                                                             %
2153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2154 %
2155 %  NTUserTime() returns the total time the process has been scheduled (e.g.
2156 %  seconds) since the last call to StartTimer().
2157 %
2158 %  The format of the UserTime method is:
2159 %
2160 %      double NTUserTime(void)
2161 %
2162 */
2163 MagickExport double NTUserTime(void)
2164 {
2165   DWORD
2166     status;
2167
2168   FILETIME
2169     create_time,
2170     exit_time;
2171
2172   OSVERSIONINFO
2173     OsVersionInfo;
2174
2175   union
2176   {
2177     FILETIME
2178       filetime;
2179
2180     __int64
2181       filetime64;
2182   } kernel_time;
2183
2184   union
2185   {
2186     FILETIME
2187       filetime;
2188
2189     __int64
2190       filetime64;
2191   } user_time;
2192
2193   OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
2194   GetVersionEx(&OsVersionInfo);
2195   if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
2196     return(NTElapsedTime());
2197   status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
2198     &kernel_time.filetime,&user_time.filetime);
2199   if (status != TRUE)
2200     return(0.0);
2201   return((double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
2202 }
2203 \f
2204 /*
2205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2206 %                                                                             %
2207 %                                                                             %
2208 %                                                                             %
2209 %   N T W a r n i n g H a n d l e r                                           %
2210 %                                                                             %
2211 %                                                                             %
2212 %                                                                             %
2213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2214 %
2215 %  NTWarningHandler() displays a warning reason.
2216 %
2217 %  The format of the NTWarningHandler method is:
2218 %
2219 %      void NTWarningHandler(const ExceptionType warning,const char *reason,
2220 %        const char *description)
2221 %
2222 %  A description of each parameter follows:
2223 %
2224 %    o warning: Specifies the numeric warning category.
2225 %
2226 %    o reason: Specifies the reason to display before terminating the
2227 %      program.
2228 %
2229 %    o description: Specifies any description to the reason.
2230 %
2231 */
2232 MagickExport void NTWarningHandler(const ExceptionType warning,
2233   const char *reason,const char *description)
2234 {
2235   char
2236     buffer[2*MaxTextExtent];
2237
2238   if (reason == (char *) NULL)
2239     return;
2240   if (description == (char *) NULL)
2241     (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s.\n",GetClientName(),
2242       reason);
2243   else
2244     (void) FormatMagickString(buffer,MaxTextExtent,"%s: %s (%s).\n",
2245       GetClientName(),reason,description);
2246   (void) MessageBox(NULL,buffer,"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2247     MB_SETFOREGROUND | MB_ICONINFORMATION);
2248 }
2249 #endif