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