]> granicus.if.org Git - imagemagick/blob - MagickCore/nt-base.c
The command window created by NTSystemCommand will be hidden.
[imagemagick] / MagickCore / 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-2014 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 "MagickCore/studio.h"
42 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
43 #include "MagickCore/client.h"
44 #include "MagickCore/exception-private.h"
45 #include "MagickCore/locale_.h"
46 #include "MagickCore/log.h"
47 #include "MagickCore/magick.h"
48 #include "MagickCore/memory_.h"
49 #include "MagickCore/nt-base.h"
50 #include "MagickCore/nt-base-private.h"
51 #include "MagickCore/resource_.h"
52 #include "MagickCore/resource-private.h"
53 #include "MagickCore/timer.h"
54 #include "MagickCore/string_.h"
55 #include "MagickCore/utility.h"
56 #include "MagickCore/version.h"
57 #if defined(MAGICKCORE_LTDL_DELEGATE)
58 #  include "ltdl.h"
59 #endif
60 #include "MagickCore/nt-base-private.h"
61 #if defined(MAGICKCORE_CIPHER_SUPPORT)
62 #include <ntsecapi.h>
63 #include <wincrypt.h>
64 #endif
65 \f
66 /*
67   Define declarations.
68 */
69 #if !defined(MAP_FAILED)
70 #define MAP_FAILED      ((void *) -1)
71 #endif
72 \f
73 /*
74   Static declarations.
75 */
76 #if !defined(MAGICKCORE_LTDL_DELEGATE)
77 static char
78   *lt_slsearchpath = (char *) NULL;
79 #endif
80
81 static GhostInfo
82   ghost_info;
83
84 static void
85   *ghost_handle = (void *) NULL;
86 \f
87 /*
88   External declarations.
89 */
90 #if !defined(MAGICKCORE_WINDOWS_SUPPORT)
91 extern "C" BOOL WINAPI
92   DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved);
93 #endif
94 \f
95 /*
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 %                                                                             %
98 %                                                                             %
99 %                                                                             %
100 %   D l l M a i n                                                             %
101 %                                                                             %
102 %                                                                             %
103 %                                                                             %
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 %
106 %  DllMain() is an entry point to the DLL which is called when processes and
107 %  threads are initialized and terminated, or upon calls to the Windows
108 %  LoadLibrary and FreeLibrary functions.
109 %
110 %  The function returns TRUE of it succeeds, or FALSE if initialization fails.
111 %
112 %  The format of the DllMain method is:
113 %
114 %    BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
115 %
116 %  A description of each parameter follows:
117 %
118 %    o handle: handle to the DLL module
119 %
120 %    o reason: reason for calling function:
121 %
122 %      DLL_PROCESS_ATTACH - DLL is being loaded into virtual address
123 %                           space of current process.
124 %      DLL_THREAD_ATTACH - Indicates that the current process is
125 %                          creating a new thread.  Called under the
126 %                          context of the new thread.
127 %      DLL_THREAD_DETACH - Indicates that the thread is exiting.
128 %                          Called under the context of the exiting
129 %                          thread.
130 %      DLL_PROCESS_DETACH - Indicates that the DLL is being unloaded
131 %                           from the virtual address space of the
132 %                           current process.
133 %
134 %    o lpvReserved: Used for passing additional info during DLL_PROCESS_ATTACH
135 %                   and DLL_PROCESS_DETACH.
136 %
137 */
138 #if defined(_DLL) && defined( ProvideDllMain )
139 BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
140 {
141   switch (reason)
142   {
143     case DLL_PROCESS_ATTACH:
144     {
145       char
146         *module_path;
147
148       ssize_t
149         count;
150
151       module_path=(char *) AcquireQuantumMemory(MaxTextExtent,
152         sizeof(*module_path));
153       if (module_path == (char *) NULL)
154         return(FALSE);
155       count=(ssize_t) GetModuleFileName(handle,module_path,MaxTextExtent);
156       if (count != 0)
157         {
158           char
159             *path;
160
161           for ( ; count > 0; count--)
162             if (module_path[count] == '\\')
163               {
164                 module_path[count+1]='\0';
165                 break;
166               }
167           MagickCoreGenesis(module_path,MagickFalse);
168           path=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,sizeof(*path));
169           if (path == (char *) NULL)
170             {
171               module_path=DestroyString(module_path);
172               return(FALSE);
173             }
174           count=(ssize_t) GetEnvironmentVariable("PATH",path,16*MaxTextExtent);
175           if ((count != 0) && (strstr(path,module_path) == (char *) NULL))
176             {
177               if ((strlen(module_path)+count+1) < (16*MaxTextExtent-1))
178                 {
179                   char
180                     *variable;
181
182                   variable=(char *) AcquireQuantumMemory(16UL*MaxTextExtent,
183                     sizeof(*variable));
184                   if (variable == (char *) NULL)
185                     {
186                       path=DestroyString(path);
187                       module_path=DestroyString(module_path);
188                       return(FALSE);
189                     }
190                   (void) FormatLocaleString(variable,16*MaxTextExtent,
191                     "%s;%s",module_path,path);
192                   SetEnvironmentVariable("PATH",variable);
193                   variable=DestroyString(variable);
194                 }
195             }
196           path=DestroyString(path);
197         }
198       module_path=DestroyString(module_path);
199       break;
200     }
201     case DLL_PROCESS_DETACH:
202     {
203       MagickCoreTerminus();
204       break;
205     }
206     default:
207       break;
208   }
209   return(TRUE);
210 }
211 #endif
212 \f
213 /*
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 %                                                                             %
216 %                                                                             %
217 %                                                                             %
218 %   E x i t                                                                   %
219 %                                                                             %
220 %                                                                             %
221 %                                                                             %
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 %
224 %  Exit() calls TerminateProcess for Win95.
225 %
226 %  The format of the exit method is:
227 %
228 %      int Exit(int status)
229 %
230 %  A description of each parameter follows:
231 %
232 %    o status: an integer value representing the status of the terminating
233 %      process.
234 %
235 */
236 MagickPrivate int Exit(int status)
237 {
238   if (IsWindows95())
239     {
240       TerminateProcess(GetCurrentProcess(),(unsigned int) status);
241       return(0);
242     }
243   exit(status);
244 }
245 \f
246 #if !defined(__MINGW32__) && !defined(__MINGW64__)
247 /*
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 %                                                                             %
250 %                                                                             %
251 %                                                                             %
252 %   g e t t i m e o f d a y                                                   %
253 %                                                                             %
254 %                                                                             %
255 %                                                                             %
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 %
258 %  The gettimeofday() method get the time of day.
259 %
260 %  The format of the gettimeofday method is:
261 %
262 %      int gettimeofday(struct timeval *time_value,struct timezone *time_zone)
263 %
264 %  A description of each parameter follows:
265 %
266 %    o time_value: the time value.
267 %
268 %    o time_zone: the time zone.
269 %
270 */
271 MagickPrivate int gettimeofday (struct timeval *time_value,
272   struct timezone *time_zone)
273 {
274 #define EpochFiletime  MagickLLConstant(116444736000000000)
275
276   static int
277     is_tz_set;
278
279   if (time_value != (struct timeval *) NULL)
280     {
281       FILETIME
282         file_time;
283
284       __int64
285         time;
286
287       LARGE_INTEGER
288         date_time;
289
290       GetSystemTimeAsFileTime(&file_time);
291       date_time.LowPart=file_time.dwLowDateTime;
292       date_time.HighPart=file_time.dwHighDateTime;
293       time=date_time.QuadPart;
294       time-=EpochFiletime;
295       time/=10;
296       time_value->tv_sec=(ssize_t) (time / 1000000);
297       time_value->tv_usec=(ssize_t) (time % 1000000);
298     }
299   if (time_zone != (struct timezone *) NULL)
300     {
301       if (is_tz_set == 0)
302         {
303           _tzset();
304           is_tz_set++;
305         }
306       time_zone->tz_minuteswest=_timezone/60;
307       time_zone->tz_dsttime=_daylight;
308     }
309   return(0);
310 }
311 #endif
312 \f
313 /*
314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315 %                                                                             %
316 %                                                                             %
317 %                                                                             %
318 %   I s W i n d o w s 9 5                                                     %
319 %                                                                             %
320 %                                                                             %
321 %                                                                             %
322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323 %
324 %  IsWindows95() returns true if the system is Windows 95.
325 %
326 %  The format of the IsWindows95 method is:
327 %
328 %      int IsWindows95()
329 %
330 */
331 MagickPrivate int IsWindows95()
332 {
333   OSVERSIONINFO
334     version_info;
335
336   version_info.dwOSVersionInfoSize=sizeof(version_info);
337   if (GetVersionEx(&version_info) &&
338       (version_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS))
339     return(1);
340   return(0);
341 }
342 \f
343 /*
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
345 %                                                                             %
346 %                                                                             %
347 %                                                                             %
348 %   N T A r g v T o U T F 8                                                   %
349 %                                                                             %
350 %                                                                             %
351 %                                                                             %
352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353 %
354 %  NTArgvToUTF8() converts the wide command line arguments to UTF-8 to ensure
355 %  compatibility with Linux.
356 %
357 %  The format of the NTArgvToUTF8 method is:
358 %
359 %      char **NTArgvToUTF8(const int argc,wchar_t **argv)
360 %
361 %  A description of each parameter follows:
362 %
363 %    o argc: the number of command line arguments.
364 %
365 %    o argv:  the  wide-character command line arguments.
366 %
367 */
368 MagickPrivate char **NTArgvToUTF8(const int argc,wchar_t **argv)
369 {
370   char
371     **utf8;
372
373   ssize_t
374     i;
375
376   utf8=(char **) AcquireQuantumMemory(argc,sizeof(*utf8));
377   if (utf8 == (char **) NULL)
378     ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
379   for (i=0; i < (ssize_t) argc; i++)
380   {
381     ssize_t
382       count;
383
384     count=WideCharToMultiByte(CP_UTF8,0,argv[i],-1,NULL,0,NULL,NULL);
385     if (count < 0)
386       count=0;
387     utf8[i]=(char *) AcquireQuantumMemory(count+1,sizeof(**utf8));
388     if (utf8[i] == (char *) NULL)
389       {
390         for (i--; i >= 0; i--)
391           utf8[i]=DestroyString(utf8[i]);
392         utf8=(char **) RelinquishMagickMemory(utf8);
393         ThrowFatalException(ResourceLimitFatalError,
394           "UnableToConvertStringToARGV");
395       }
396     count=WideCharToMultiByte(CP_UTF8,0,argv[i],-1,utf8[i],count,NULL,NULL);
397     utf8[i][count]=0;
398   }
399   return(utf8);
400 }
401 \f
402 /*
403 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404 %                                                                             %
405 %                                                                             %
406 %                                                                             %
407 %   N T C l o s e D i r e c t o r y                                           %
408 %                                                                             %
409 %                                                                             %
410 %                                                                             %
411 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
412 %
413 %  NTCloseDirectory() closes the named directory stream and frees the DIR
414 %  structure.
415 %
416 %  The format of the NTCloseDirectory method is:
417 %
418 %      int NTCloseDirectory(DIR *entry)
419 %
420 %  A description of each parameter follows:
421 %
422 %    o entry: Specifies a pointer to a DIR structure.
423 %
424 */
425 MagickPrivate int NTCloseDirectory(DIR *entry)
426 {
427   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
428   assert(entry != (DIR *) NULL);
429   FindClose(entry->hSearch);
430   entry=(DIR *) RelinquishMagickMemory(entry);
431   return(0);
432 }
433 \f
434 /*
435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 %                                                                             %
437 %                                                                             %
438 %                                                                             %
439 %   N T C l o s e L i b r a r y                                               %
440 %                                                                             %
441 %                                                                             %
442 %                                                                             %
443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 %
445 %  NTCloseLibrary() unloads the module associated with the passed handle.
446 %
447 %  The format of the NTCloseLibrary method is:
448 %
449 %      void NTCloseLibrary(void *handle)
450 %
451 %  A description of each parameter follows:
452 %
453 %    o handle: Specifies a handle to a previously loaded dynamic module.
454 %
455 */
456 MagickPrivate int NTCloseLibrary(void *handle)
457 {
458   if (IsWindows95())
459     return(FreeLibrary((HINSTANCE) handle));
460   return(!(FreeLibrary((HINSTANCE) handle)));
461 }
462 \f
463 /*
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465 %                                                                             %
466 %                                                                             %
467 %                                                                             %
468 %   N T C o n t r o l H a n d l e r                                           %
469 %                                                                             %
470 %                                                                             %
471 %                                                                             %
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 %
474 %  NTControlHandler() registers a control handler that is activated when, for
475 %  example, a ctrl-c is received.
476 %
477 %  The format of the NTControlHandler method is:
478 %
479 %      int NTControlHandler(void)
480 %
481 */
482
483 static BOOL ControlHandler(DWORD type)
484 {
485   (void) type;
486   AsynchronousResourceComponentTerminus();
487   return(FALSE);
488 }
489
490 MagickPrivate int NTControlHandler(void)
491 {
492   return(SetConsoleCtrlHandler((PHANDLER_ROUTINE) ControlHandler,TRUE));
493 }
494 \f
495 /*
496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
497 %                                                                             %
498 %                                                                             %
499 %                                                                             %
500 %   N T E l a p s e d T i m e                                                 %
501 %                                                                             %
502 %                                                                             %
503 %                                                                             %
504 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
505 %
506 %  NTElapsedTime() returns the elapsed time (in seconds) since the last call to
507 %  StartTimer().
508 %
509 %  The format of the ElapsedTime method is:
510 %
511 %      double NTElapsedTime(void)
512 %
513 */
514 MagickPrivate double NTElapsedTime(void)
515 {
516   union
517   {
518     FILETIME
519       filetime;
520
521     __int64
522       filetime64;
523   } elapsed_time;
524
525   SYSTEMTIME
526     system_time;
527
528   GetSystemTime(&system_time);
529   SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
530   return((double) 1.0e-7*elapsed_time.filetime64);
531 }
532 \f
533 /*
534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
535 %                                                                             %
536 %                                                                             %
537 %                                                                             %
538 +   N T E r r o r H a n d l e r                                               %
539 %                                                                             %
540 %                                                                             %
541 %                                                                             %
542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
543 %
544 %  NTErrorHandler() displays an error reason and then terminates the program.
545 %
546 %  The format of the NTErrorHandler method is:
547 %
548 %      void NTErrorHandler(const ExceptionType severity,const char *reason,
549 %        const char *description)
550 %
551 %  A description of each parameter follows:
552 %
553 %    o severity: Specifies the numeric error category.
554 %
555 %    o reason: Specifies the reason to display before terminating the
556 %      program.
557 %
558 %    o description: Specifies any description to the reason.
559 %
560 */
561 MagickPrivate void NTErrorHandler(const ExceptionType severity,
562   const char *reason,const char *description)
563 {
564   char
565     buffer[3*MaxTextExtent],
566     *message;
567
568   (void) severity;
569   if (reason == (char *) NULL)
570     {
571       MagickCoreTerminus();
572       exit(0);
573     }
574   message=GetExceptionMessage(errno);
575   if ((description != (char *) NULL) && errno)
576     (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s) [%s].\n",
577       GetClientName(),reason,description,message);
578   else
579     if (description != (char *) NULL)
580       (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
581         GetClientName(),reason,description);
582     else
583       if (errno != 0)
584         (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s [%s].\n",
585           GetClientName(),reason,message);
586       else
587         (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s.\n",
588           GetClientName(),reason);
589   message=DestroyString(message);
590   (void) MessageBox(NULL,buffer,"ImageMagick Exception",MB_OK | MB_TASKMODAL |
591     MB_SETFOREGROUND | MB_ICONEXCLAMATION);
592   MagickCoreTerminus();
593   exit(0);
594 }
595 \f
596 /*
597 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
598 %                                                                             %
599 %                                                                             %
600 %                                                                             %
601 %   N T E x i t L i b r a r y                                                 %
602 %                                                                             %
603 %                                                                             %
604 %                                                                             %
605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
606 %
607 %  NTExitLibrary() exits the dynamic module loading subsystem.
608 %
609 %  The format of the NTExitLibrary method is:
610 %
611 %      int NTExitLibrary(void)
612 %
613 */
614 MagickPrivate int NTExitLibrary(void)
615 {
616   return(0);
617 }
618 \f
619 /*
620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
621 %                                                                             %
622 %                                                                             %
623 %                                                                             %
624 %   N T G a t h e r R a n d o m D a t a                                       %
625 %                                                                             %
626 %                                                                             %
627 %                                                                             %
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629 %
630 %  NTGatherRandomData() gathers random data and returns it.
631 %
632 %  The format of the GatherRandomData method is:
633 %
634 %      MagickBooleanType NTGatherRandomData(const size_t length,
635 %        unsigned char *random)
636 %
637 %  A description of each parameter follows:
638 %
639 %    length: the length of random data buffer
640 %
641 %    random: the random data is returned here.
642 %
643 */
644 MagickPrivate MagickBooleanType NTGatherRandomData(const size_t length,
645   unsigned char *random)
646 {
647 #if defined(MAGICKCORE_CIPHER_SUPPORT) && defined(_MSC_VER) && (_MSC_VER > 1200)
648   HCRYPTPROV
649     handle;
650
651   int
652     status;
653
654   handle=(HCRYPTPROV) NULL;
655   status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
656     (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
657   if (status == 0)
658     status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
659       (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
660   if (status == 0)
661     return(MagickFalse);
662   status=CryptGenRandom(handle,(DWORD) length,random);
663   if (status == 0)
664     {
665       status=CryptReleaseContext(handle,0);
666       return(MagickFalse);
667     }
668   status=CryptReleaseContext(handle,0);
669   if (status == 0)
670     return(MagickFalse);
671 #else
672   (void) random;
673   (void) length;
674 #endif
675   return(MagickTrue);
676 }
677 \f
678 /*
679 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680 %                                                                             %
681 %                                                                             %
682 %                                                                             %
683 %   N T G e t E x e c u t i o n P a t h                                       %
684 %                                                                             %
685 %                                                                             %
686 %                                                                             %
687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
688 %
689 %  NTGetExecutionPath() returns the execution path of a program.
690 %
691 %  The format of the GetExecutionPath method is:
692 %
693 %      MagickBooleanType NTGetExecutionPath(char *path,const size_t extent)
694 %
695 %  A description of each parameter follows:
696 %
697 %    o path: the pathname of the executable that started the process.
698 %
699 %    o extent: the maximum extent of the path.
700 %
701 */
702 MagickPrivate MagickBooleanType NTGetExecutionPath(char *path,
703   const size_t extent)
704 {
705   GetModuleFileName(0,path,(DWORD) extent);
706   return(MagickTrue);
707 }
708 \f
709 /*
710 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
711 %                                                                             %
712 %                                                                             %
713 %                                                                             %
714 %   N T G e t L a s t E r r o r                                               %
715 %                                                                             %
716 %                                                                             %
717 %                                                                             %
718 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719 %
720 %  NTGetLastError() returns the last error that occurred.
721 %
722 %  The format of the NTGetLastError method is:
723 %
724 %      char *NTGetLastError(void)
725 %
726 */
727 char *NTGetLastError(void)
728 {
729   char
730     *reason;
731
732   int
733     status;
734
735   LPVOID
736     buffer;
737
738   status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
739     FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),
740     MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
741   if (!status)
742     reason=AcquireString("An unknown error occurred");
743   else
744     {
745       reason=AcquireString((const char *) buffer);
746       LocalFree(buffer);
747     }
748   return(reason);
749 }
750 \f
751 /*
752 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
753 %                                                                             %
754 %                                                                             %
755 %                                                                             %
756 %   N T G e t L i b r a r y E r r o r                                         %
757 %                                                                             %
758 %                                                                             %
759 %                                                                             %
760 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
761 %
762 %  Lt_dlerror() returns a pointer to a string describing the last error
763 %  associated with a lt_dl method.  Note that this function is not thread
764 %  safe so it should only be used under the protection of a lock.
765 %
766 %  The format of the NTGetLibraryError method is:
767 %
768 %      const char *NTGetLibraryError(void)
769 %
770 */
771 MagickPrivate const char *NTGetLibraryError(void)
772 {
773   static char
774     last_error[MaxTextExtent];
775
776   char
777     *error;
778
779   *last_error='\0';
780   error=NTGetLastError();
781   if (error)
782     (void) CopyMagickString(last_error,error,MaxTextExtent);
783   error=DestroyString(error);
784   return(last_error);
785 }
786 \f
787 /*
788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
789 %                                                                             %
790 %                                                                             %
791 %                                                                             %
792 %   N T G e t L i b r a r y S y m b o l                                       %
793 %                                                                             %
794 %                                                                             %
795 %                                                                             %
796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
797 %
798 %  NTGetLibrarySymbol() retrieve the procedure address of the method
799 %  specified by the passed character string.
800 %
801 %  The format of the NTGetLibrarySymbol method is:
802 %
803 %      void *NTGetLibrarySymbol(void *handle,const char *name)
804 %
805 %  A description of each parameter follows:
806 %
807 %    o handle: Specifies a handle to the previously loaded dynamic module.
808 %
809 %    o name: Specifies the procedure entry point to be returned.
810 %
811 */
812 void *NTGetLibrarySymbol(void *handle,const char *name)
813 {
814   LPFNDLLFUNC1
815     lpfnDllFunc1;
816
817   lpfnDllFunc1=(LPFNDLLFUNC1) GetProcAddress((HINSTANCE) handle,name);
818   if (!lpfnDllFunc1)
819     return((void *) NULL);
820   return((void *) lpfnDllFunc1);
821 }
822 \f
823 /*
824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
825 %                                                                             %
826 %                                                                             %
827 %                                                                             %
828 %   N T G e t M o d u l e P a t h                                             %
829 %                                                                             %
830 %                                                                             %
831 %                                                                             %
832 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
833 %
834 %  NTGetModulePath() returns the path of the specified module.
835 %
836 %  The format of the GetModulePath method is:
837 %
838 %      MagickBooleanType NTGetModulePath(const char *module,char *path)
839 %
840 %  A description of each parameter follows:
841 %
842 %    modith: the module name.
843 %
844 %    path: the module path is returned here.
845 %
846 */
847 MagickPrivate MagickBooleanType NTGetModulePath(const char *module,char *path)
848 {
849   char
850     module_path[MaxTextExtent];
851
852   HMODULE
853     handle;
854
855   ssize_t
856     length;
857
858   *path='\0';
859   handle=GetModuleHandle(module);
860   if (handle == (HMODULE) NULL)
861     return(MagickFalse);
862   length=GetModuleFileName(handle,module_path,MaxTextExtent);
863   if (length != 0)
864     GetPathComponent(module_path,HeadPath,path);
865   return(MagickTrue);
866 }
867 \f
868 /*
869 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
870 %                                                                             %
871 %                                                                             %
872 %                                                                             %
873 %   N T G h o s t s c r i p t D L L                                           %
874 %                                                                             %
875 %                                                                             %
876 %                                                                             %
877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
878 %
879 %  NTGhostscriptDLL() returns the path to the most recent Ghostscript version
880 %  DLL.  The method returns TRUE on success otherwise FALSE.
881 %
882 %  The format of the NTGhostscriptDLL method is:
883 %
884 %      int NTGhostscriptDLL(char *path,int length)
885 %
886 %  A description of each parameter follows:
887 %
888 %    o path: return the Ghostscript DLL path here.
889 %
890 %    o length: the buffer length.
891 %
892 */
893
894 static int NTGetRegistryValue(HKEY root,const char *key,DWORD flags,const char *name,
895   char *value,int *length)
896 {
897   BYTE
898     byte,
899     *p;
900
901   DWORD
902     extent,
903     type;
904
905   HKEY
906     hkey;
907
908   LONG
909     status;
910
911   /*
912     Get a registry value: key = root\\key, named value = name.
913   */
914   if (RegOpenKeyExA(root,key,0,KEY_READ | flags,&hkey) != ERROR_SUCCESS)
915     return(1);  /* no match */
916   p=(BYTE *) value;
917   type=REG_SZ;
918   extent=(*length);
919   if (p == (BYTE *) NULL)
920     p=(&byte);  /* ERROR_MORE_DATA only if value is NULL */
921   status=RegQueryValueExA(hkey,(char *) name,0,&type,p,&extent);
922   RegCloseKey(hkey);
923   if (status == ERROR_SUCCESS)
924     {
925       *length=extent;
926       return(0);  /* return the match */
927     }
928   if (status == ERROR_MORE_DATA)
929     {
930       *length=extent;
931       return(-1);  /* buffer not large enough */
932     }
933   return(1);  /* not found */
934 }
935
936 static int NTLocateGhostscript(DWORD flags,const char **product_family,int *major_version,
937   int *minor_version)
938 {
939   int
940     i;
941
942   MagickBooleanType
943     status;
944
945   static const char
946     *products[4] =
947     {
948       "GPL Ghostscript",
949       "GNU Ghostscript",
950       "AFPL Ghostscript",
951       "Aladdin Ghostscript"
952     };
953
954   /*
955     Find the most recent version of Ghostscript.
956   */
957   status=MagickFalse;
958   *product_family=NULL;
959   *major_version=5;
960   *minor_version=49; /* min version of Ghostscript is 5.50 */
961   for (i=0; i < (ssize_t) (sizeof(products)/sizeof(products[0])); i++)
962   {
963     char
964       key[MaxTextExtent];
965
966     HKEY
967       hkey,
968       root;
969
970     REGSAM
971       mode;
972
973     (void) FormatLocaleString(key,MaxTextExtent,"SOFTWARE\\%s",products[i]);
974     root=HKEY_LOCAL_MACHINE;
975     mode=KEY_READ | flags;
976     if (RegOpenKeyExA(root,key,0,mode,&hkey) == ERROR_SUCCESS)
977       {
978         DWORD
979           extent;
980
981         int
982           j;
983
984         /*
985           Now enumerate the keys.
986         */
987         extent=sizeof(key)/sizeof(char);
988         for (j=0; RegEnumKeyA(hkey,j,key,extent) == ERROR_SUCCESS; j++)
989         {
990           int
991             major,
992             minor;
993
994           major=0;
995           minor=0;
996           if (sscanf(key,"%d.%d",&major,&minor) != 2)
997             continue;
998           if ((major > *major_version) || ((major == *major_version) &&
999               (minor > *minor_version)))
1000             {
1001               *product_family=products[i];
1002               *major_version=major;
1003               *minor_version=minor;
1004               status=MagickTrue;
1005             }
1006        }
1007        (void) RegCloseKey(hkey);
1008      }
1009   }
1010   if (status == MagickFalse)
1011     {
1012       *major_version=0;
1013       *minor_version=0;
1014     }
1015   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"Ghostscript (%s) "
1016     "version %d.%02d",*product_family,*major_version,*minor_version);
1017   return(status);
1018 }
1019
1020 static BOOL NTIs64BitPlatform()
1021 {
1022 #if defined(_WIN64) || !defined(KEY_WOW64_32KEY)
1023   return(TRUE);
1024 #else
1025   BOOL is64=FALSE;
1026   return(IsWow64Process(GetCurrentProcess(), &is64) && is64);
1027 #endif
1028 }
1029
1030 static int NTGhostscriptGetString(const char *name,BOOL *is_64_bit,char *value,
1031   const size_t length)
1032 {
1033   char
1034     key[MaxTextExtent];
1035
1036   int
1037     i,
1038     extent;
1039
1040   static const char
1041     *product_family = (const char *) NULL;
1042
1043   static BOOL
1044     is_64_bit_version = FALSE;
1045
1046   static int
1047     flags=0,
1048     major_version=0,
1049     minor_version=0;
1050
1051   struct
1052   {
1053     const HKEY
1054       hkey;
1055
1056     const char
1057       *name;
1058   }
1059   hkeys[2] =
1060   {
1061     { HKEY_CURRENT_USER,  "HKEY_CURRENT_USER"  },
1062     { HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" }
1063   };
1064
1065   /*
1066     Get a string from the installed Ghostscript.
1067   */
1068   if (is_64_bit!=NULL)
1069     *is_64_bit=FALSE;
1070   *value='\0';
1071   if (product_family == NULL)
1072   {
1073     flags=0;
1074 #if defined(KEY_WOW64_32KEY)
1075     flags=NTIs64BitPlatform() ? KEY_WOW64_64KEY : 0;
1076 #endif
1077     (void) NTLocateGhostscript(flags,&product_family,&major_version,
1078       &minor_version);
1079     if (product_family == NULL)
1080     {
1081       if (flags!=0)
1082       {
1083         /*
1084           We are running on a 64 bit platform - check for a 32 bit Ghostscript.
1085         */
1086         flags=0;
1087 #if defined(KEY_WOW64_32KEY)
1088         flags=KEY_WOW64_32KEY;
1089 #endif
1090         (void) NTLocateGhostscript(flags,&product_family,&major_version,
1091           &minor_version);
1092       }
1093     }
1094     else
1095       is_64_bit_version=NTIs64BitPlatform();
1096   }
1097   if (is_64_bit != NULL)
1098     *is_64_bit=is_64_bit_version;
1099   if (product_family == NULL)
1100     return(FALSE);
1101   (void) FormatLocaleString(key,MaxTextExtent,"SOFTWARE\\%s\\%d.%02d",
1102     product_family,major_version,minor_version);
1103   for (i=0; i < (ssize_t) (sizeof(hkeys)/sizeof(hkeys[0])); i++)
1104   {
1105     extent=(int) length;
1106     if (NTGetRegistryValue(hkeys[i].hkey,key,flags,name,value,&extent) == 0)
1107       {
1108         (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1109           "registry: \"%s\\%s\\%s\"=\"%s\"",hkeys[i].name,key,name,value);
1110         return(TRUE);
1111       }
1112     (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1113       "registry: \"%s\\%s\\%s\" (failed)",hkeys[i].name,key,name);
1114   }
1115   return(FALSE);
1116 }
1117
1118 MagickPrivate int NTGhostscriptDLL(char *path,int length)
1119 {
1120   static char
1121     dll[MaxTextExtent] = { "" };
1122
1123   static BOOL
1124     is_64_bit_version;
1125
1126   *path='\0';
1127   if ((*dll == '\0') &&
1128       (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,dll,sizeof(dll)) == FALSE))
1129     return(FALSE);
1130
1131 #if defined(_WIN64)
1132   if (!is_64_bit_version)
1133     return(FALSE);
1134 #else
1135   if (is_64_bit_version)
1136     return(FALSE);
1137 #endif
1138   (void) CopyMagickString(path,dll,length);
1139   return(TRUE);
1140 }
1141 \f
1142 /*
1143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1144 %                                                                             %
1145 %                                                                             %
1146 %                                                                             %
1147 %   N T G h o s t s c r i p t D L L V e c t o r s                             %
1148 %                                                                             %
1149 %                                                                             %
1150 %                                                                             %
1151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1152 %
1153 %  NTGhostscriptDLLVectors() returns a GhostInfo structure that includes
1154 %  function vectors to invoke Ghostscript DLL functions. A null pointer is
1155 %  returned if there is an error when loading the DLL or retrieving the
1156 %  function vectors.
1157 %
1158 %  The format of the NTGhostscriptDLLVectors method is:
1159 %
1160 %      const GhostInfo *NTGhostscriptDLLVectors(void)
1161 %
1162 */
1163 MagickPrivate const GhostInfo *NTGhostscriptDLLVectors(void)
1164 {
1165   if (NTGhostscriptLoadDLL() == FALSE)
1166     return((GhostInfo *) NULL);
1167   return(&ghost_info);
1168 }
1169 \f
1170 /*
1171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1172 %                                                                             %
1173 %                                                                             %
1174 %                                                                             %
1175 %   N T G h o s t s c r i p t E X E                                           %
1176 %                                                                             %
1177 %                                                                             %
1178 %                                                                             %
1179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1180 %
1181 %  NTGhostscriptEXE() obtains the path to the latest Ghostscript executable.
1182 %  The method returns FALSE if a full path value is not obtained and returns
1183 %  a default path of gswin32c.exe.
1184 %
1185 %  The format of the NTGhostscriptEXE method is:
1186 %
1187 %      int NTGhostscriptEXE(char *path,int length)
1188 %
1189 %  A description of each parameter follows:
1190 %
1191 %    o path: return the Ghostscript executable path here.
1192 %
1193 %    o length: length of buffer.
1194 %
1195 */
1196 MagickPrivate int NTGhostscriptEXE(char *path,int length)
1197 {
1198   register char
1199     *p;
1200
1201   static char
1202     program[MaxTextExtent] = { "" };
1203
1204   static BOOL
1205     is_64_bit_version = FALSE;
1206
1207   (void) CopyMagickString(path,"gswin32c.exe",length);
1208   if ((*program == '\0') &&
1209       (NTGhostscriptGetString("GS_DLL",&is_64_bit_version,program,sizeof(program)) == FALSE))
1210     return(FALSE);
1211   p=strrchr(program,'\\');
1212   if (p != (char *) NULL)
1213     {
1214       p++;
1215       *p='\0';
1216       (void) ConcatenateMagickString(program,is_64_bit_version ?
1217         "gswin64c.exe" : "gswin32c.exe",sizeof(program));
1218     }
1219   (void) CopyMagickString(path,program,length);
1220   return(TRUE);
1221 }
1222 \f
1223 /*
1224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1225 %                                                                             %
1226 %                                                                             %
1227 %                                                                             %
1228 %   N T G h o s t s c r i p t F o n t s                                       %
1229 %                                                                             %
1230 %                                                                             %
1231 %                                                                             %
1232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1233 %
1234 %  NTGhostscriptFonts() obtains the path to the Ghostscript fonts.  The method
1235 %  returns FALSE if it cannot determine the font path.
1236 %
1237 %  The format of the NTGhostscriptFonts method is:
1238 %
1239 %      int NTGhostscriptFonts(char *path, int length)
1240 %
1241 %  A description of each parameter follows:
1242 %
1243 %    o path: return the font path here.
1244 %
1245 %    o length: length of the path buffer.
1246 %
1247 */
1248 MagickPrivate int NTGhostscriptFonts(char *path,int length)
1249 {
1250   char
1251     buffer[MaxTextExtent],
1252     filename[MaxTextExtent];
1253
1254   register char
1255     *p,
1256     *q;
1257
1258   *path='\0';
1259   if (NTGhostscriptGetString("GS_LIB",NULL,buffer,MaxTextExtent) == FALSE)
1260     return(FALSE);
1261   for (p=buffer-1; p != (char *) NULL; p=strchr(p+1,DirectoryListSeparator))
1262   {
1263     (void) CopyMagickString(path,p+1,length+1);
1264     q=strchr(path,DirectoryListSeparator);
1265     if (q != (char *) NULL)
1266       *q='\0';
1267     (void) FormatLocaleString(filename,MaxTextExtent,"%s%sfonts.dir",path,
1268       DirectorySeparator);
1269     if (IsPathAccessible(filename) != MagickFalse)
1270       return(TRUE);
1271   }
1272   return(FALSE);
1273 }
1274 \f
1275 /*
1276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1277 %                                                                             %
1278 %                                                                             %
1279 %                                                                             %
1280 %   N T G h o s t s c r i p t L o a d D L L                                   %
1281 %                                                                             %
1282 %                                                                             %
1283 %                                                                             %
1284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1285 %
1286 %  NTGhostscriptLoadDLL() attempts to load the Ghostscript DLL and returns
1287 %  TRUE if it succeeds.
1288 %
1289 %  The format of the NTGhostscriptLoadDLL method is:
1290 %
1291 %      int NTGhostscriptLoadDLL(void)
1292 %
1293 */
1294 MagickPrivate int NTGhostscriptLoadDLL(void)
1295 {
1296   char
1297     path[MaxTextExtent];
1298
1299   if (ghost_handle != (void *) NULL)
1300     return(TRUE);
1301   if (NTGhostscriptDLL(path,sizeof(path)) == FALSE)
1302     return(FALSE);
1303   ghost_handle=lt_dlopen(path);
1304   if (ghost_handle == (void *) NULL)
1305     return(FALSE);
1306   (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1307   ghost_info.exit=(int (MagickDLLCall *)(gs_main_instance*))
1308     lt_dlsym(ghost_handle,"gsapi_exit");
1309   ghost_info.init_with_args=(int (MagickDLLCall *)(gs_main_instance *,int,
1310     char **)) (lt_dlsym(ghost_handle,"gsapi_init_with_args"));
1311   ghost_info.new_instance=(int (MagickDLLCall *)(gs_main_instance **,void *)) (
1312     lt_dlsym(ghost_handle,"gsapi_new_instance"));
1313   ghost_info.run_string=(int (MagickDLLCall *)(gs_main_instance *,const char *,
1314     int,int *)) (lt_dlsym(ghost_handle,"gsapi_run_string"));
1315   ghost_info.delete_instance=(void (MagickDLLCall *) (gs_main_instance *)) (
1316     lt_dlsym(ghost_handle,"gsapi_delete_instance"));
1317   if ((ghost_info.exit == NULL) || (ghost_info.init_with_args == NULL) ||
1318       (ghost_info.new_instance == NULL) || (ghost_info.run_string == NULL) ||
1319       (ghost_info.delete_instance == NULL))
1320     return(FALSE);
1321   return(TRUE);
1322 }
1323 \f
1324 /*
1325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1326 %                                                                             %
1327 %                                                                             %
1328 %                                                                             %
1329 %   N T G h o s t s c r i p t U n L o a d D L L                               %
1330 %                                                                             %
1331 %                                                                             %
1332 %                                                                             %
1333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1334 %
1335 %  NTGhostscriptUnLoadDLL() unloads the Ghostscript DLL and returns TRUE if
1336 %  it succeeds.
1337 %
1338 %  The format of the NTGhostscriptUnLoadDLL method is:
1339 %
1340 %      int NTGhostscriptUnLoadDLL(void)
1341 %
1342 */
1343 MagickPrivate int NTGhostscriptUnLoadDLL(void)
1344 {
1345   int
1346     status;
1347
1348   if (ghost_handle == (void *) NULL)
1349     return(FALSE);
1350   status=lt_dlclose(ghost_handle);
1351   ghost_handle=(void *) NULL;
1352   (void) ResetMagickMemory((void *) &ghost_info,0,sizeof(GhostInfo));
1353   return(status);
1354 }
1355 \f
1356 /*
1357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1358 %                                                                             %
1359 %                                                                             %
1360 %                                                                             %
1361 %   N T I n i t i a l i z e L i b r a r y                                     %
1362 %                                                                             %
1363 %                                                                             %
1364 %                                                                             %
1365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1366 %
1367 %  NTInitializeLibrary() initializes the dynamic module loading subsystem.
1368 %
1369 %  The format of the NTInitializeLibrary method is:
1370 %
1371 %      int NTInitializeLibrary(void)
1372 %
1373 */
1374 MagickPrivate int NTInitializeLibrary(void)
1375 {
1376   return(0);
1377 }
1378 \f
1379 /*
1380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1381 %                                                                             %
1382 %                                                                             %
1383 %                                                                             %
1384 +  N T M a p M e m o r y                                                      %
1385 %                                                                             %
1386 %                                                                             %
1387 %                                                                             %
1388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1389 %
1390 %  Mmap() emulates the Unix method of the same name.
1391 %
1392 %  The format of the NTMapMemory method is:
1393 %
1394 %    MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1395 %      int access,int file,MagickOffsetType offset)
1396 %
1397 */
1398 MagickPrivate void *NTMapMemory(char *address,size_t length,int protection,
1399   int flags,int file,MagickOffsetType offset)
1400 {
1401   DWORD
1402     access_mode,
1403     high_length,
1404     high_offset,
1405     low_length,
1406     low_offset,
1407     protection_mode;
1408
1409   HANDLE
1410     file_handle,
1411     map_handle;
1412
1413   void
1414     *map;
1415
1416   (void) address;
1417   access_mode=0;
1418   file_handle=INVALID_HANDLE_VALUE;
1419   low_length=(DWORD) (length & 0xFFFFFFFFUL);
1420   high_length=(DWORD) ((((MagickOffsetType) length) >> 32) & 0xFFFFFFFFUL);
1421   map_handle=INVALID_HANDLE_VALUE;
1422   map=(void *) NULL;
1423   low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
1424   high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
1425   protection_mode=0;
1426   if (protection & PROT_WRITE)
1427     {
1428       access_mode=FILE_MAP_WRITE;
1429       if (!(flags & MAP_PRIVATE))
1430         protection_mode=PAGE_READWRITE;
1431       else
1432         {
1433           access_mode=FILE_MAP_COPY;
1434           protection_mode=PAGE_WRITECOPY;
1435         }
1436     }
1437   else
1438     if (protection & PROT_READ)
1439       {
1440         access_mode=FILE_MAP_READ;
1441         protection_mode=PAGE_READONLY;
1442       }
1443   if ((file == -1) && (flags & MAP_ANONYMOUS))
1444     file_handle=INVALID_HANDLE_VALUE;
1445   else
1446     file_handle=(HANDLE) _get_osfhandle(file);
1447   map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
1448     low_length,0);
1449   if (map_handle)
1450     {
1451       map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
1452         length);
1453       CloseHandle(map_handle);
1454     }
1455   if (map == (void *) NULL)
1456     return((void *) MAP_FAILED);
1457   return((void *) ((char *) map));
1458 }
1459 \f
1460 /*
1461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1462 %                                                                             %
1463 %                                                                             %
1464 %                                                                             %
1465 %   N T O p e n D i r e c t o r y                                             %
1466 %                                                                             %
1467 %                                                                             %
1468 %                                                                             %
1469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1470 %
1471 %  NTOpenDirectory() opens the directory named by filename and associates a
1472 %  directory stream with it.
1473 %
1474 %  The format of the NTOpenDirectory method is:
1475 %
1476 %      DIR *NTOpenDirectory(const char *path)
1477 %
1478 %  A description of each parameter follows:
1479 %
1480 %    o entry: Specifies a pointer to a DIR structure.
1481 %
1482 */
1483 MagickPrivate DIR *NTOpenDirectory(const char *path)
1484 {
1485   wchar_t
1486     file_specification[MaxTextExtent];
1487
1488   DIR
1489     *entry;
1490
1491   size_t
1492     length;
1493
1494   assert(path != (const char *) NULL);
1495   length=MultiByteToWideChar(CP_UTF8,0,path,-1,file_specification,
1496     MaxTextExtent);
1497   if (length == 0)
1498     return((DIR *) NULL);
1499   if(wcsncat(file_specification,(const wchar_t*) DirectorySeparator,
1500        MaxTextExtent) == (wchar_t*)NULL)
1501     return((DIR *) NULL);
1502   entry=(DIR *) AcquireMagickMemory(sizeof(DIR));
1503   if (entry != (DIR *) NULL)
1504     {
1505       entry->firsttime=TRUE;
1506       entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1507     }
1508   if (entry->hSearch == INVALID_HANDLE_VALUE)
1509     {
1510       if(wcsncat(file_specification,L"*.*",
1511         MaxTextExtent) == (wchar_t*)NULL)
1512         {
1513           entry=(DIR *) RelinquishMagickMemory(entry);
1514           return((DIR *) NULL);
1515         }
1516       entry->hSearch=FindFirstFileW(file_specification,&entry->Win32FindData);
1517       if (entry->hSearch == INVALID_HANDLE_VALUE)
1518         {
1519           entry=(DIR *) RelinquishMagickMemory(entry);
1520           return((DIR *) NULL);
1521         }
1522     }
1523   return(entry);
1524 }
1525 \f
1526 /*
1527 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1528 %                                                                             %
1529 %                                                                             %
1530 %                                                                             %
1531 %   N T O p e n L i b r a r y                                                 %
1532 %                                                                             %
1533 %                                                                             %
1534 %                                                                             %
1535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1536 %
1537 %  NTOpenLibrary() loads a dynamic module into memory and returns a handle that
1538 %  can be used to access the various procedures in the module.
1539 %
1540 %  The format of the NTOpenLibrary method is:
1541 %
1542 %      void *NTOpenLibrary(const char *filename)
1543 %
1544 %  A description of each parameter follows:
1545 %
1546 %    o path: Specifies a pointer to string representing dynamic module that
1547 %      is to be loaded.
1548 %
1549 */
1550
1551 static inline const char *GetSearchPath(void)
1552 {
1553 #if defined(MAGICKCORE_LTDL_DELEGATE)
1554   return(lt_dlgetsearchpath());
1555 #else
1556   return(lt_slsearchpath);
1557 #endif
1558 }
1559
1560 static UINT ChangeErrorMode(void)
1561 {
1562   typedef UINT
1563     (CALLBACK *GETERRORMODE)(void);
1564
1565   GETERRORMODE
1566     getErrorMode;
1567
1568   HMODULE
1569     handle;
1570
1571   UINT
1572     mode;
1573
1574   mode=SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1575
1576   handle=GetModuleHandle("kernel32.dll");
1577   if (handle == (HMODULE) NULL)
1578     return SetErrorMode(mode);
1579
1580   getErrorMode=(GETERRORMODE) NTGetLibrarySymbol(handle,"GetErrorMode");
1581   if (getErrorMode != (GETERRORMODE) NULL)
1582     mode=getErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX;
1583
1584   return SetErrorMode(mode);
1585 }
1586
1587 MagickPrivate void *NTOpenLibrary(const char *filename)
1588 {
1589 #define MaxPathElements 31
1590
1591   char
1592     buffer[MaxTextExtent];
1593
1594   int
1595     index;
1596
1597   register const char
1598     *p,
1599     *q;
1600
1601   register int
1602     i;
1603
1604   UINT
1605     mode;
1606
1607   void
1608     *handle;
1609
1610   mode=ChangeErrorMode();
1611   handle=(void *) LoadLibraryEx(filename,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1612   if ((handle != (void *) NULL) || (GetSearchPath() == (char *) NULL))
1613     {
1614       SetErrorMode(mode);
1615       return(handle);
1616     }
1617   p=(char *) GetSearchPath();
1618   index=0;
1619   while (index < MaxPathElements)
1620   {
1621     q=strchr(p,DirectoryListSeparator);
1622     if (q == (char *) NULL)
1623       {
1624         (void) CopyMagickString(buffer,p,MaxTextExtent);
1625         (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1626         (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1627         handle=(void *) LoadLibraryEx(buffer,NULL,
1628           LOAD_WITH_ALTERED_SEARCH_PATH);
1629         break;
1630       }
1631     i=q-p;
1632     (void) CopyMagickString(buffer,p,i+1);
1633     (void) ConcatenateMagickString(buffer,"\\",MaxTextExtent);
1634     (void) ConcatenateMagickString(buffer,filename,MaxTextExtent);
1635     handle=(void *) LoadLibraryEx(buffer,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1636     if (handle != (void *) NULL)
1637       break;
1638     p=q+1;
1639   }
1640   SetErrorMode(mode);
1641   return(handle);
1642 }
1643 \f
1644 /*
1645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1646 %                                                                             %
1647 %                                                                             %
1648 %                                                                             %
1649 %    N T R e a d D i r e c t o r y                                            %
1650 %                                                                             %
1651 %                                                                             %
1652 %                                                                             %
1653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1654 %
1655 %  NTReadDirectory() returns a pointer to a structure representing the
1656 %  directory entry at the current position in the directory stream to which
1657 %  entry refers.
1658 %
1659 %  The format of the NTReadDirectory
1660 %
1661 %      NTReadDirectory(entry)
1662 %
1663 %  A description of each parameter follows:
1664 %
1665 %    o entry: Specifies a pointer to a DIR structure.
1666 %
1667 */
1668 MagickPrivate struct dirent *NTReadDirectory(DIR *entry)
1669 {
1670   int
1671     status;
1672
1673   size_t
1674     length;
1675
1676   if (entry == (DIR *) NULL)
1677     return((struct dirent *) NULL);
1678   if (!entry->firsttime)
1679     {
1680       status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
1681       if (status == 0)
1682         return((struct dirent *) NULL);
1683     }
1684   length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
1685     entry->file_info.d_name,sizeof(entry->file_info.d_name),NULL,NULL);
1686   if (length == 0)
1687     return((struct dirent *) NULL);
1688   entry->firsttime=FALSE;
1689   entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1690   return(&entry->file_info);
1691 }
1692 \f
1693 /*
1694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1695 %                                                                             %
1696 %                                                                             %
1697 %                                                                             %
1698 %   N T R e g i s t r y K e y L o o k u p                                     %
1699 %                                                                             %
1700 %                                                                             %
1701 %                                                                             %
1702 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1703 %
1704 %  NTRegistryKeyLookup() returns ImageMagick installation path settings
1705 %  stored in the Windows Registry.  Path settings are specific to the
1706 %  installed ImageMagick version so that multiple Image Magick installations
1707 %  may coexist.
1708 %
1709 %  Values are stored in the registry under a base path path similar to
1710 %  "HKEY_LOCAL_MACHINE/SOFTWARE\ImageMagick\6.7.4\Q:16" or
1711 %  "HKEY_CURRENT_USER/SOFTWARE\ImageMagick\6.7.4\Q:16". The provided subkey
1712 %  is appended to this base path to form the full key.
1713 %
1714 %  The format of the NTRegistryKeyLookup method is:
1715 %
1716 %      unsigned char *NTRegistryKeyLookup(const char *subkey)
1717 %
1718 %  A description of each parameter follows:
1719 %
1720 %    o subkey: Specifies a string that identifies the registry object.
1721 %      Currently supported sub-keys include: "BinPath", "ConfigurePath",
1722 %      "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
1723 %
1724 */
1725 MagickPrivate unsigned char *NTRegistryKeyLookup(const char *subkey)
1726 {
1727   char
1728     package_key[MaxTextExtent];
1729
1730   DWORD
1731     size,
1732     type;
1733
1734   HKEY
1735     registry_key;
1736
1737   LONG
1738     status;
1739
1740   unsigned char
1741     *value;
1742
1743   /*
1744     Look-up base key.
1745   */
1746   (void) FormatLocaleString(package_key,MaxTextExtent,"SOFTWARE\\%s\\%s\\Q:%d",
1747     MagickPackageName,MagickLibVersionText,MAGICKCORE_QUANTUM_DEPTH);
1748   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",package_key);
1749   registry_key=(HKEY) INVALID_HANDLE_VALUE;
1750   status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,package_key,0,KEY_READ,&registry_key);
1751   if (status != ERROR_SUCCESS)
1752     status=RegOpenKeyExA(HKEY_CURRENT_USER,package_key,0,KEY_READ,
1753       &registry_key);
1754   if (status != ERROR_SUCCESS)
1755     {
1756       registry_key=(HKEY) INVALID_HANDLE_VALUE;
1757       return((unsigned char *) NULL);
1758     }
1759   /*
1760     Look-up sub key.
1761   */
1762   size=32;
1763   value=(unsigned char *) AcquireQuantumMemory(size,sizeof(*value));
1764   if (value == (unsigned char *) NULL)
1765     {
1766       RegCloseKey(registry_key);
1767       return((unsigned char *) NULL);
1768     }
1769   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),"%s",subkey);
1770   status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1771   if ((status == ERROR_MORE_DATA) && (type == REG_SZ))
1772     {
1773       value=(unsigned char *) ResizeQuantumMemory(value,size,sizeof(*value));
1774       if (value == (BYTE *) NULL)
1775         {
1776           RegCloseKey(registry_key);
1777           return((unsigned char *) NULL);
1778         }
1779       status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1780     }
1781   RegCloseKey(registry_key);
1782   if ((type != REG_SZ) || (status != ERROR_SUCCESS))
1783     value=(unsigned char *) RelinquishMagickMemory(value);
1784   return((unsigned char *) value);
1785 }
1786 \f
1787 /*
1788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1789 %                                                                             %
1790 %                                                                             %
1791 %                                                                             %
1792 %   N T R e p o r t E v e n t                                                 %
1793 %                                                                             %
1794 %                                                                             %
1795 %                                                                             %
1796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1797 %
1798 %  NTReportEvent() reports an event.
1799 %
1800 %  The format of the NTReportEvent method is:
1801 %
1802 %      MagickBooleanType NTReportEvent(const char *event,
1803 %        const MagickBooleanType error)
1804 %
1805 %  A description of each parameter follows:
1806 %
1807 %    o event: the event.
1808 %
1809 %    o error: MagickTrue the event is an error.
1810 %
1811 */
1812 MagickPrivate MagickBooleanType NTReportEvent(const char *event,
1813   const MagickBooleanType error)
1814 {
1815   const char
1816     *events[1];
1817
1818   HANDLE
1819     handle;
1820
1821   WORD
1822     type;
1823
1824   handle=RegisterEventSource(NULL,MAGICKCORE_PACKAGE_NAME);
1825   if (handle == NULL)
1826     return(MagickFalse);
1827   events[0]=event;
1828   type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
1829   ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
1830   DeregisterEventSource(handle);
1831   return(MagickTrue);
1832 }
1833 \f
1834 /*
1835 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1836 %                                                                             %
1837 %                                                                             %
1838 %                                                                             %
1839 %   N T R e s o u r c e T o B l o b                                           %
1840 %                                                                             %
1841 %                                                                             %
1842 %                                                                             %
1843 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1844 %
1845 %  NTResourceToBlob() returns a blob containing the contents of the resource
1846 %  in the current executable specified by the id parameter. This currently
1847 %  used to retrieve MGK files tha have been embedded into the various command
1848 %  line utilities.
1849 %
1850 %  The format of the NTResourceToBlob method is:
1851 %
1852 %      unsigned char *NTResourceToBlob(const char *id)
1853 %
1854 %  A description of each parameter follows:
1855 %
1856 %    o id: Specifies a string that identifies the resource.
1857 %
1858 */
1859 MagickPrivate unsigned char *NTResourceToBlob(const char *id)
1860 {
1861
1862 #ifndef MAGICKCORE_LIBRARY_NAME
1863   char
1864     path[MaxTextExtent];
1865 #endif
1866
1867   DWORD
1868     length;
1869
1870   HGLOBAL
1871     global;
1872
1873   HMODULE
1874     handle;
1875
1876   HRSRC
1877     resource;
1878
1879   unsigned char
1880     *blob,
1881     *value;
1882
1883   assert(id != (const char *) NULL);
1884   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1885 #ifdef MAGICKCORE_LIBRARY_NAME
1886   handle=GetModuleHandle(MAGICKCORE_LIBRARY_NAME);
1887 #else
1888   (void) FormatLocaleString(path,MaxTextExtent,"%s%s%s",GetClientPath(),
1889     DirectorySeparator,GetClientName());
1890   if (IsPathAccessible(path) != MagickFalse)
1891     handle=GetModuleHandle(path);
1892   else
1893     handle=GetModuleHandle(0);
1894 #endif
1895   if (!handle)
1896     return((unsigned char *) NULL);
1897   resource=FindResource(handle,id,"IMAGEMAGICK");
1898   if (!resource)
1899     return((unsigned char *) NULL);
1900   global=LoadResource(handle,resource);
1901   if (!global)
1902     return((unsigned char *) NULL);
1903   length=SizeofResource(handle,resource);
1904   value=(unsigned char *) LockResource(global);
1905   if (!value)
1906     {
1907       FreeResource(global);
1908       return((unsigned char *) NULL);
1909     }
1910   blob=(unsigned char *) AcquireQuantumMemory(length+MaxTextExtent,
1911     sizeof(*blob));
1912   if (blob != (unsigned char *) NULL)
1913     {
1914       (void) CopyMagickMemory(blob,value,length);
1915       blob[length]='\0';
1916     }
1917   UnlockResource(global);
1918   FreeResource(global);
1919   return(blob);
1920 }
1921 \f
1922 /*
1923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1924 %                                                                             %
1925 %                                                                             %
1926 %                                                                             %
1927 %   N T S e e k D i r e c t o r y                                             %
1928 %                                                                             %
1929 %                                                                             %
1930 %                                                                             %
1931 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1932 %
1933 %  NTSeekDirectory() sets the position of the next NTReadDirectory() operation
1934 %  on the directory stream.
1935 %
1936 %  The format of the NTSeekDirectory method is:
1937 %
1938 %      void NTSeekDirectory(DIR *entry,ssize_t position)
1939 %
1940 %  A description of each parameter follows:
1941 %
1942 %    o entry: Specifies a pointer to a DIR structure.
1943 %
1944 %    o position: specifies the position associated with the directory
1945 %      stream.
1946 %
1947 */
1948 MagickPrivate void NTSeekDirectory(DIR *entry,ssize_t position)
1949 {
1950   (void) position;
1951   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1952   assert(entry != (DIR *) NULL);
1953 }
1954 \f
1955 /*
1956 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1957 %                                                                             %
1958 %                                                                             %
1959 %                                                                             %
1960 %   N T S e t S e a r c h P a t h                                             %
1961 %                                                                             %
1962 %                                                                             %
1963 %                                                                             %
1964 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1965 %
1966 %  NTSetSearchPath() sets the current locations that the subsystem should
1967 %  look at to find dynamically loadable modules.
1968 %
1969 %  The format of the NTSetSearchPath method is:
1970 %
1971 %      int NTSetSearchPath(const char *path)
1972 %
1973 %  A description of each parameter follows:
1974 %
1975 %    o path: Specifies a pointer to string representing the search path
1976 %      for DLL's that can be dynamically loaded.
1977 %
1978 */
1979 MagickPrivate int NTSetSearchPath(const char *path)
1980 {
1981 #if defined(MAGICKCORE_LTDL_DELEGATE)
1982   lt_dlsetsearchpath(path);
1983 #else
1984   if (lt_slsearchpath != (char *) NULL)
1985     lt_slsearchpath=DestroyString(lt_slsearchpath);
1986   if (path != (char *) NULL)
1987     lt_slsearchpath=AcquireString(path);
1988 #endif
1989   return(0);
1990 }
1991 \f
1992 /*
1993 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1994 %                                                                             %
1995 %                                                                             %
1996 %                                                                             %
1997 +  N T S y n c M e m o r y                                                    %
1998 %                                                                             %
1999 %                                                                             %
2000 %                                                                             %
2001 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2002 %
2003 %  NTSyncMemory() emulates the Unix method of the same name.
2004 %
2005 %  The format of the NTSyncMemory method is:
2006 %
2007 %      int NTSyncMemory(void *address,size_t length,int flags)
2008 %
2009 %  A description of each parameter follows:
2010 %
2011 %    o address: the address of the binary large object.
2012 %
2013 %    o length: the length of the binary large object.
2014 %
2015 %    o flags: Option flags (ignored for Windows).
2016 %
2017 */
2018 MagickPrivate int NTSyncMemory(void *address,size_t length,int flags)
2019 {
2020   (void) flags;
2021   if (FlushViewOfFile(address,length) == MagickFalse)
2022     return(-1);
2023   return(0);
2024 }
2025 \f
2026 /*
2027 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2028 %                                                                             %
2029 %                                                                             %
2030 %                                                                             %
2031 %   N T S y s t e m C o m m a n d                                             %
2032 %                                                                             %
2033 %                                                                             %
2034 %                                                                             %
2035 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2036 %
2037 %  NTSystemCommand() executes the specified command and waits until it
2038 %  terminates.  The returned value is the exit status of the command.
2039 %
2040 %  The format of the NTSystemCommand method is:
2041 %
2042 %      int NTSystemCommand(MagickFalse,const char *command)
2043 %
2044 %  A description of each parameter follows:
2045 %
2046 %    o command: This string is the command to execute.
2047 %
2048 */
2049 MagickPrivate int NTSystemCommand(const char *command)
2050 {
2051   char
2052     local_command[MaxTextExtent];
2053
2054   DWORD
2055     child_status;
2056
2057   int
2058     status;
2059
2060   MagickBooleanType
2061     background_process;
2062
2063   PROCESS_INFORMATION
2064     process_info;
2065
2066   STARTUPINFO
2067     startup_info;
2068
2069   if (command == (char *) NULL)
2070     return(-1);
2071   GetStartupInfo(&startup_info);
2072   startup_info.dwFlags=STARTF_USESHOWWINDOW;
2073   startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
2074   (void) CopyMagickString(local_command,command,MaxTextExtent);
2075   background_process=command[strlen(command)-1] == '&' ? MagickTrue :
2076     MagickFalse;
2077   if (background_process != MagickFalse)
2078     local_command[strlen(command)-1]='\0';
2079   if (command[strlen(command)-1] == '|')
2080     local_command[strlen(command)-1]='\0';
2081   else
2082     startup_info.wShowWindow=SW_HIDE;
2083   status=CreateProcess((LPCTSTR) NULL,local_command,(LPSECURITY_ATTRIBUTES)
2084     NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) FALSE,(DWORD)
2085     NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
2086     &process_info);
2087   if (status == 0)
2088     return(-1);
2089   if (background_process != MagickFalse)
2090     return(status == 0);
2091   status=WaitForSingleObject(process_info.hProcess,INFINITE);
2092   if (status != WAIT_OBJECT_0)
2093     return(status);
2094   status=GetExitCodeProcess(process_info.hProcess,&child_status);
2095   if (status == 0)
2096     return(-1);
2097   CloseHandle(process_info.hProcess);
2098   CloseHandle(process_info.hThread);
2099   return((int) child_status);
2100 }
2101 \f
2102 /*
2103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2104 %                                                                             %
2105 %                                                                             %
2106 %                                                                             %
2107 %   N T S y s t e m C o n i f i g u r a t i o n                               %
2108 %                                                                             %
2109 %                                                                             %
2110 %                                                                             %
2111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2112 %
2113 %  NTSystemConfiguration() provides a way for the application to determine
2114 %  values for system limits or options at runtime.
2115 %
2116 %  The format of the exit method is:
2117 %
2118 %      ssize_t NTSystemConfiguration(int name)
2119 %
2120 %  A description of each parameter follows:
2121 %
2122 %    o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
2123 %
2124 */
2125 MagickPrivate ssize_t NTSystemConfiguration(int name)
2126 {
2127   switch (name)
2128   {
2129     case _SC_PAGESIZE:
2130     {
2131       SYSTEM_INFO
2132         system_info;
2133
2134       GetSystemInfo(&system_info);
2135       return(system_info.dwPageSize);
2136     }
2137     case _SC_PHYS_PAGES:
2138     {
2139       HMODULE
2140         handle;
2141
2142       LPFNDLLFUNC2
2143         module;
2144
2145       NTMEMORYSTATUSEX
2146         status;
2147
2148       SYSTEM_INFO
2149         system_info;
2150
2151       handle=GetModuleHandle("kernel32.dll");
2152       if (handle == (HMODULE) NULL)
2153         return(0L);
2154       GetSystemInfo(&system_info);
2155       module=(LPFNDLLFUNC2) NTGetLibrarySymbol(handle,"GlobalMemoryStatusEx");
2156       if (module == (LPFNDLLFUNC2) NULL)
2157         {
2158           MEMORYSTATUS
2159             status;
2160
2161           GlobalMemoryStatus(&status);
2162           return((ssize_t) status.dwTotalPhys/system_info.dwPageSize/4);
2163         }
2164       status.dwLength=sizeof(status);
2165       if (module(&status) == 0)
2166         return(0L);
2167       return((ssize_t) status.ullTotalPhys/system_info.dwPageSize/4);
2168     }
2169     case _SC_OPEN_MAX:
2170       return(2048);
2171     default:
2172       break;
2173   }
2174   return(-1);
2175 }
2176 \f
2177 /*
2178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2179 %                                                                             %
2180 %                                                                             %
2181 %                                                                             %
2182 %   N T T e l l D i r e c t o r y                                             %
2183 %                                                                             %
2184 %                                                                             %
2185 %                                                                             %
2186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2187 %
2188 %  NTTellDirectory() returns the current location associated with the named
2189 %  directory stream.
2190 %
2191 %  The format of the NTTellDirectory method is:
2192 %
2193 %      ssize_t NTTellDirectory(DIR *entry)
2194 %
2195 %  A description of each parameter follows:
2196 %
2197 %    o entry: Specifies a pointer to a DIR structure.
2198 %
2199 */
2200 MagickPrivate ssize_t NTTellDirectory(DIR *entry)
2201 {
2202   assert(entry != (DIR *) NULL);
2203   return(0);
2204 }
2205 \f
2206 /*
2207 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2208 %                                                                             %
2209 %                                                                             %
2210 %                                                                             %
2211 %   N T T r u n c a t e F i l e                                               %
2212 %                                                                             %
2213 %                                                                             %
2214 %                                                                             %
2215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2216 %
2217 %  NTTruncateFile() truncates a file to a specified length.
2218 %
2219 %  The format of the NTTruncateFile method is:
2220 %
2221 %      int NTTruncateFile(int file,off_t length)
2222 %
2223 %  A description of each parameter follows:
2224 %
2225 %    o file: the file.
2226 %
2227 %    o length: the file length.
2228 %
2229 */
2230 MagickPrivate int NTTruncateFile(int file,off_t length)
2231 {
2232   DWORD
2233     file_pointer;
2234
2235   long
2236     file_handle,
2237     high,
2238     low;
2239
2240   file_handle=_get_osfhandle(file);
2241   if (file_handle == -1L)
2242     return(-1);
2243   low=(long) (length & 0xffffffffUL);
2244   high=(long) ((((MagickOffsetType) length) >> 32) & 0xffffffffUL);
2245   file_pointer=SetFilePointer((HANDLE) file_handle,low,&high,FILE_BEGIN);
2246   if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
2247     return(-1);
2248   if (SetEndOfFile((HANDLE) file_handle) == 0)
2249     return(-1);
2250   return(0);
2251 }
2252 \f
2253 /*
2254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2255 %                                                                             %
2256 %                                                                             %
2257 %                                                                             %
2258 +  N T U n m a p M e m o r y                                                  %
2259 %                                                                             %
2260 %                                                                             %
2261 %                                                                             %
2262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2263 %
2264 %  NTUnmapMemory() emulates the Unix munmap method.
2265 %
2266 %  The format of the NTUnmapMemory method is:
2267 %
2268 %      int NTUnmapMemory(void *map,size_t length)
2269 %
2270 %  A description of each parameter follows:
2271 %
2272 %    o map: the address of the binary large object.
2273 %
2274 %    o length: the length of the binary large object.
2275 %
2276 */
2277 MagickPrivate int NTUnmapMemory(void *map,size_t length)
2278 {
2279   (void) length;
2280   if (UnmapViewOfFile(map) == 0)
2281     return(-1);
2282   return(0);
2283 }
2284 \f
2285 /*
2286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2287 %                                                                             %
2288 %                                                                             %
2289 %                                                                             %
2290 %   N T U s e r T i m e                                                       %
2291 %                                                                             %
2292 %                                                                             %
2293 %                                                                             %
2294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2295 %
2296 %  NTUserTime() returns the total time the process has been scheduled (e.g.
2297 %  seconds) since the last call to StartTimer().
2298 %
2299 %  The format of the UserTime method is:
2300 %
2301 %      double NTUserTime(void)
2302 %
2303 */
2304 MagickPrivate double NTUserTime(void)
2305 {
2306   DWORD
2307     status;
2308
2309   FILETIME
2310     create_time,
2311     exit_time;
2312
2313   OSVERSIONINFO
2314     OsVersionInfo;
2315
2316   union
2317   {
2318     FILETIME
2319       filetime;
2320
2321     __int64
2322       filetime64;
2323   } kernel_time;
2324
2325   union
2326   {
2327     FILETIME
2328       filetime;
2329
2330     __int64
2331       filetime64;
2332   } user_time;
2333
2334   OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
2335   GetVersionEx(&OsVersionInfo);
2336   if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
2337     return(NTElapsedTime());
2338   status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
2339     &kernel_time.filetime,&user_time.filetime);
2340   if (status != TRUE)
2341     return(0.0);
2342   return((double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
2343 }
2344 \f
2345 /*
2346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2347 %                                                                             %
2348 %                                                                             %
2349 %                                                                             %
2350 %   N T W a r n i n g H a n d l e r                                           %
2351 %                                                                             %
2352 %                                                                             %
2353 %                                                                             %
2354 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2355 %
2356 %  NTWarningHandler() displays a warning reason.
2357 %
2358 %  The format of the NTWarningHandler method is:
2359 %
2360 %      void NTWarningHandler(const ExceptionType severity,const char *reason,
2361 %        const char *description)
2362 %
2363 %  A description of each parameter follows:
2364 %
2365 %    o severity: Specifies the numeric warning category.
2366 %
2367 %    o reason: Specifies the reason to display before terminating the
2368 %      program.
2369 %
2370 %    o description: Specifies any description to the reason.
2371 %
2372 */
2373 MagickPrivate void NTWarningHandler(const ExceptionType severity,
2374   const char *reason,const char *description)
2375 {
2376   char
2377     buffer[2*MaxTextExtent];
2378
2379   (void) severity;
2380   if (reason == (char *) NULL)
2381     return;
2382   if (description == (char *) NULL)
2383     (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s.\n",GetClientName(),
2384       reason);
2385   else
2386     (void) FormatLocaleString(buffer,MaxTextExtent,"%s: %s (%s).\n",
2387       GetClientName(),reason,description);
2388   (void) MessageBox(NULL,buffer,"ImageMagick Warning",MB_OK | MB_TASKMODAL |
2389     MB_SETFOREGROUND | MB_ICONINFORMATION);
2390 }
2391 #endif