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