MagickCore 7.0.10
nt-base.c
Go to the documentation of this file.
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 the Wizard's Toolkit %
14% %
15% Software Design %
16% Cristy %
17% December 1996 %
18% %
19% %
20% Copyright @ 1999 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% https://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 "wizard/studio.h"
42#if defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
43#include "wizard/client.h"
45#include "wizard/locale_.h"
46#include "wizard/log.h"
47#include "wizard/memory_.h"
48#include "wizard/resource_.h"
49#include "wizard/string_.h"
50#include "wizard/timer.h"
51#include "wizard/utility.h"
52#include "wizard/wizard.h"
53#include "wizard/version.h"
54#if defined(WIZARDSTOOLKIT_LTDL_DELEGATE)
55# include "ltdl.h"
56#endif /* defined(WIZARDSTOOLKIT_LTDL_DELEGATE) */
57#include "wizard/nt-base.h"
58#include <NTSecAPI.h>
59#include <wincrypt.h>
60
61/*
62 Define declarations.
63*/
64#if !defined(MAP_FAILED)
65#define MAP_FAILED ((void *) -1)
66#endif
67
68/*
69 Static declarations.
70*/
71#if !defined(WIZARDSTOOLKIT_LTDL_DELEGATE)
72static char
73 *lt_slsearchpath = (char *) NULL;
74#endif
75
76/*
77 External declarations.
78*/
79#if !defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
80extern "C" BOOL WINAPI
81 DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved);
82#endif
83
84/*
85%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86% %
87% %
88% %
89% D l l M a i n %
90% %
91% %
92% %
93%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94%
95% DllMain() is an entry point to the DLL which is called when processes and
96% threads are initialized and terminated, or upon calls to the Windows
97% LoadLibrary and FreeLibrary functions.
98%
99% The function returns TRUE of it succeeds, or FALSE if initialization fails.
100%
101% The format of the DllMain method is:
102%
103% BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
104%
105% A description of each parameter follows:
106%
107% o handle: handle to the DLL module
108%
109% o reason: reason for calling function:
110%
111% DLL_PROCESS_ATTACH - DLL is being loaded into virtual address
112% space of current process.
113% DLL_THREAD_ATTACH - Indicates that the current process is
114% creating a new thread. Called under the
115% context of the new thread.
116% DLL_THREAD_DETACH - Indicates that the thread is exiting.
117% Called under the context of the exiting
118% thread.
119% DLL_PROCESS_DETACH - Indicates that the DLL is being unloaded
120% from the virtual address space of the
121% current process.
122%
123% o lpvReserved: Used for passing additional info during DLL_PROCESS_ATTACH
124% and DLL_PROCESS_DETACH.
125%
126*/
127#if defined(_DLL) && defined( ProvideDllMain )
128BOOL WINAPI DllMain(HINSTANCE handle,DWORD reason,LPVOID lpvReserved)
129{
130 switch (reason)
131 {
132 case DLL_PROCESS_ATTACH:
133 {
134 char
135 *module_path,
136 *new_path,
137 *path;
138
139 ssize_t
140 count;
141
142 module_path=(char *) AcquireQuantumMemory(WizardPathExtent,
143 sizeof(*module_path));
144 new_path=(char *) AcquireQuantumMemory(16UL*WizardPathExtent,
145 sizeof(*new_path));
146 path=(char *) AcquireQuantumMemory(16UL*WizardPathExtent,sizeof(*path));
147 if ((module_path == (char *) NULL) || (new_path == (char *) NULL) ||
148 (path == (char *) NULL))
149 return(FALSE);
150 count=(ssize_t) GetModuleFileName(handle,module_path,WizardPathExtent);
151 if (count != 0)
152 {
153 for ( ; count > 0; count--)
154 if (module_path[count] == '\\')
155 {
156 module_path[count+1]='\0';
157 break;
158 }
159 WizardsToolkitGenesis(module_path);
160 count=GetEnvironmentVariable("PATH",path,16*WizardPathExtent);
161 if ((count != 0) && (strstr(path,module_path) == (char *) NULL))
162 {
163 if ((strlen(module_path)+count+1) < (16*WizardPathExtent-1))
164 {
165 (void) FormatLocaleString(new_path,16*WizardPathExtent,
166 "%s;%s",module_path,path);
167 SetEnvironmentVariable("PATH",new_path);
168 }
169 }
170 }
171 path=(char *) RelinquishWizardMemory(path);
172 new_path=(char *) RelinquishWizardMemory(new_path);
173 module_path=(char *) RelinquishWizardMemory(module_path);
174 break;
175 }
176 case DLL_PROCESS_DETACH:
177 {
179 break;
180 }
181 default:
182 break;
183 }
184 return(TRUE);
185}
186#endif
187
188/*
189%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190% %
191% %
192% %
193% E x i t %
194% %
195% %
196% %
197%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198%
199% Exit() calls TerminateProcess for Win95.
200%
201% The format of the exit method is:
202%
203% int Exit(int status)
204%
205% A description of each parameter follows:
206%
207% o status: an integer value representing the status of the terminating
208% process.
209%
210%
211*/
212WizardExport int Exit(int status)
213{
214 if (IsWindows95())
215 TerminateProcess(GetCurrentProcess(),(unsigned int) status);
216 exit(status);
217 return(0);
218}
219
220#if !defined(__MINGW32__) && !defined(__MINGW64__)
221/*
222%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223% %
224% %
225% %
226% g e t t i m e o f d a y %
227% %
228% %
229% %
230%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231%
232% The gettimeofday() method get the time of day.
233%
234% The format of the gettimeofday method is:
235%
236% int gettimeofday(struct timeval *time_value,struct timezone *time_zone)
237%
238% A description of each parameter follows:
239%
240% o time_value: time value.
241%
242% o time_zone: time zone.
243%
244*/
245WizardExport int gettimeofday (struct timeval *time_value,
246 struct timezone *time_zone)
247{
248#define EpochFiletime WizardLLConstant(116444736000000000)
249
250 static int
251 is_tz_set;
252
253 if (time_value != (struct timeval *) NULL)
254 {
255 FILETIME
256 file_time;
257
258 __int64
259 time;
260
261 LARGE_INTEGER
262 date_time;
263
264 GetSystemTimeAsFileTime(&file_time);
265 date_time.LowPart=file_time.dwLowDateTime;
266 date_time.HighPart=file_time.dwHighDateTime;
267 time=date_time.QuadPart;
268 time-=EpochFiletime;
269 time/=10;
270 time_value->tv_sec=(ssize_t) (time / 1000000);
271 time_value->tv_usec=(ssize_t) (time % 1000000);
272 }
273 if (time_zone != (struct timezone *) NULL)
274 {
275 if (is_tz_set == 0)
276 {
277 _tzset();
278 is_tz_set++;
279 }
280 time_zone->tz_minuteswest=_timezone/60;
281 time_zone->tz_dsttime=_daylight;
282 }
283 return(0);
284}
285#endif
286
287/*
288%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289% %
290% %
291% %
292% I s W i n d o w s 9 5 %
293% %
294% %
295% %
296%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297%
298% IsWindows95() returns true if the system is Windows 95.
299%
300% The format of the IsWindows95 method is:
301%
302% int IsWindows95()
303%
304%
305*/
306WizardExport int IsWindows95()
307{
308 OSVERSIONINFO
309 version_info;
310
311 version_info.dwOSVersionInfoSize=sizeof(version_info);
312 if (GetVersionEx(&version_info) &&
313 (version_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS))
314 return(1);
315 return(0);
316}
317
318/*
319%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320% %
321% %
322% %
323% N T A r g v T o U T F 8 %
324% %
325% %
326% %
327%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328%
329% NTArgvToUTF8() converts the wide command line arguments to UTF-8 to ensure
330% compatibility with Linux.
331%
332% The format of the NTArgvToUTF8 method is:
333%
334% char **NTArgvToUTF8(const int argc,wchar_t **argv)
335%
336% A description of each parameter follows:
337%
338% o argc: the number of command line arguments.
339%
340% o argv: the wide-character command line arguments.
341%
342*/
343WizardExport char **NTArgvToUTF8(const int argc,wchar_t **argv)
344{
345 char
346 **utf8;
347
348 ssize_t
349 i;
350
351 utf8=(char **) AcquireQuantumMemory(argc,sizeof(*utf8));
352 if (utf8 == (char **) NULL)
353 ThrowFatalException(ResourceFatalError,"UnableToConvertStringToARGV");
354 for (i=0; i < (ssize_t) argc; i++)
355 {
356 ssize_t
357 count;
358
359 count=WideCharToMultiByte(CP_UTF8,0,argv[i],-1,NULL,0,NULL,NULL);
360 if (count < 0)
361 count=0;
362 utf8[i]=(char *) AcquireQuantumMemory(count+1,sizeof(**utf8));
363 if (utf8[i] == (char *) NULL)
364 {
365 for (i--; i >= 0; i--)
366 utf8[i]=DestroyString(utf8[i]);
367 utf8=(char **) RelinquishWizardMemory(utf8);
368 ThrowFatalException(ResourceFatalError,"UnableToConvertStringToARGV");
369 }
370 count=WideCharToMultiByte(CP_UTF8,0,argv[i],-1,utf8[i],count,NULL,NULL);
371 utf8[i][count]=0;
372 }
373 return(utf8);
374}
375
376/*
377%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378% %
379% %
380% %
381% N T C l o s e D i r e c t o r y %
382% %
383% %
384% %
385%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
386%
387% NTCloseDirectory() closes the named directory stream and frees the DIR
388% structure.
389%
390% The format of the NTCloseDirectory method is:
391%
392% int NTCloseDirectory(DIR *entry)
393%
394% A description of each parameter follows:
395%
396% o entry: Specifies a pointer to a DIR structure.
397%
398*/
399WizardExport int NTCloseDirectory(DIR *entry)
400{
402 assert(entry != (DIR *) NULL);
403 FindClose(entry->hSearch);
404 entry=(DIR *) RelinquishWizardMemory(entry);
405 return(0);
406}
407
408/*
409%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410% %
411% %
412% %
413% N T C l o s e L i b r a r y %
414% %
415% %
416% %
417%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418%
419% NTCloseLibrary() unloads the module associated with the passed handle.
420%
421% The format of the NTCloseLibrary method is:
422%
423% void NTCloseLibrary(void *handle)
424%
425% A description of each parameter follows:
426%
427% o handle: Specifies a handle to a previously loaded dynamic module.
428%
429*/
430WizardExport int NTCloseLibrary(void *handle)
431{
432 if (IsWindows95())
433 return(FreeLibrary(handle));
434 return(!(FreeLibrary(handle)));
435}
436
437/*
438%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
439% %
440% %
441% %
442% N T C o n t r o l H a n d l e r %
443% %
444% %
445% %
446%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
447%
448% NTControlHandler() registers a control handler that is activated when, for
449% example, a ctrl-c is received.
450%
451% The format of the NTControlHandler method is:
452%
453% int NTControlHandler(void)
454%
455*/
456
457static BOOL ControlHandler(DWORD type)
458{
460 return(FALSE);
461}
462
463WizardExport int NTControlHandler(void)
464{
465 return(SetConsoleCtrlHandler((PHANDLER_ROUTINE) ControlHandler,TRUE));
466}
467
468/*
469%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
470% %
471% %
472% %
473% N T E l a p s e d T i m e %
474% %
475% %
476% %
477%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478%
479% NTElapsedTime() returns the elapsed time (in seconds) since the last call to
480% StartTimer().
481%
482% The format of the ElapsedTime method is:
483%
484% double NTElapsedTime(void)
485%
486*/
487WizardExport double NTElapsedTime(void)
488{
489 union
490 {
491 FILETIME
492 filetime;
493
494 __int64
495 filetime64;
496 } elapsed_time;
497
498 SYSTEMTIME
499 system_time;
500
501 GetSystemTime(&system_time);
502 SystemTimeToFileTime(&system_time,&elapsed_time.filetime);
503 return((double) 1.0e-7*elapsed_time.filetime64);
504}
505
506/*
507%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
508% %
509% %
510% %
511+ N T E r r o r H a n d l e r %
512% %
513% %
514% %
515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516%
517% NTErrorHandler() displays an error reason and then terminates the program.
518%
519% The format of the NTErrorHandler method is:
520%
521% void NTErrorHandler(const ExceptionType error,const char *reason,
522% const char *description)
523%
524% A description of each parameter follows:
525%
526% o error: Specifies the numeric error category.
527%
528% o reason: Specifies the reason to display before terminating the
529% program.
530%
531% o description: Specifies any description to the reason.
532%
533*/
534WizardExport void NTErrorHandler(const ExceptionType error,const char *reason,
535 const char *description)
536{
537 char
538 buffer[3*WizardPathExtent],
539 *message;
540
541 if (reason == (char *) NULL)
542 {
544 exit(0);
545 }
546 message=GetExceptionMessage(errno);
547 if ((description != (char *) NULL) && errno)
548 (void) FormatLocaleString(buffer,WizardPathExtent,"%s: %s (%s) [%s].\n",
549 GetClientName(),reason,description,message);
550 else
551 if (description != (char *) NULL)
552 (void) FormatLocaleString(buffer,WizardPathExtent,"%s: %s (%s).\n",
553 GetClientName(),reason,description);
554 else
555 if (errno)
556 (void) FormatLocaleString(buffer,WizardPathExtent,"%s: %s [%s].\n",
557 GetClientName(),reason,message);
558 else
559 (void) FormatLocaleString(buffer,WizardPathExtent,"%s: %s.\n",
560 GetClientName(),reason);
561 message=(char *) RelinquishWizardMemory(message);
562 (void) MessageBox(NULL,buffer,"Wizard's Toolkit Exception",MB_OK |
563 MB_TASKMODAL | MB_SETFOREGROUND | MB_ICONEXCLAMATION);
565 exit(0);
566}
567
568/*
569%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
570% %
571% %
572% %
573% N T E x i t L i b r a r y %
574% %
575% %
576% %
577%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
578%
579% NTExitLibrary() exits the dynamic module loading subsystem.
580%
581% The format of the NTExitLibrary method is:
582%
583% int NTExitLibrary(void)
584%
585*/
586WizardExport int NTExitLibrary(void)
587{
588 return(0);
589}
590
591/*
592%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
593% %
594% %
595% %
596% N T F i l e T r u n c a t e %
597% %
598% %
599% %
600%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
601%
602% NTFileTruncate() truncates a file to a specified length.
603%
604% The format of the NTFileTruncate method is:
605%
606% int NTFileTruncate(int file,off_t length)
607%
608% A description of each parameter follows:
609%
610% o file: The file.
611%
612% o length: The file length.
613%
614*/
615WizardExport int NTFileTruncate(int file,off_t length)
616{
617 DWORD
618 file_pointer;
619
620 long
621 file_handle,
622 high,
623 low;
624
625 file_handle=_get_osfhandle(file);
626 if (file_handle == -1L)
627 return(-1);
628 low=(long) (length & 0xffffffffUL);
629 high=(long) ((((WizardOffsetType) length) >> 32) & 0xffffffffUL);
630 file_pointer=SetFilePointer((HANDLE) file_handle,low,&high,FILE_BEGIN);
631 if ((file_pointer == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
632 return(-1);
633 if (SetEndOfFile((HANDLE) file_handle) == 0)
634 return(-1);
635 return(0);
636}
637
638/*
639%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
640% %
641% %
642% %
643% N T G e t E x e c u t i o n P a t h %
644% %
645% %
646% %
647%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
648%
649% NTGetExecutionPath() returns the execution path of a program.
650%
651% The format of the GetExecutionPath method is:
652%
653% WizardBooleanType NTGetExecutionPath(char *path,const size_t extent)
654%
655% A description of each parameter follows:
656%
657% o path: the pathname of the executable that started the process.
658%
659% o extent: the maximum extent of the path.
660%
661*/
662WizardExport WizardBooleanType NTGetExecutionPath(char *path,
663 const size_t extent)
664{
665 GetModuleFileName(0,path,(DWORD) extent);
666 return(WizardTrue);
667}
668
669/*
670%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
671% %
672% %
673% %
674% N T G e t L a s t E r r o r %
675% %
676% %
677% %
678%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679%
680% NTGetLastError() returns the last error that occurred.
681%
682% The format of the NTGetLastError method is:
683%
684% char *NTGetLastError(void)
685%
686*/
687char *NTGetLastError(void)
688{
689 char
690 *reason;
691
692 int
693 status;
694
695 LPVOID
696 buffer = (LPVOID) NULL;
697
698 status=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
699 FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),
700 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &buffer,0,NULL);
701 if (!status)
702 reason=AcquireString("An unknown error occurred");
703 else
704 {
705 reason=AcquireString((const char *) buffer);
706 LocalFree(buffer);
707 }
708 return(reason);
709}
710
711/*
712%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
713% %
714% %
715% %
716% N T G e t L i b r a r y E r r o r %
717% %
718% %
719% %
720%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
721%
722% Lt_dlerror() returns a pointer to a string describing the last error
723% associated with a lt_dl method. Note that this function is not thread
724% safe so it should only be used under the protection of a lock.
725%
726% The format of the NTGetLibraryError method is:
727%
728% const char *NTGetLibraryError(void)
729%
730*/
731WizardExport const char *NTGetLibraryError(void)
732{
733 static char
734 last_error[WizardPathExtent];
735
736 char
737 *error;
738
739 *last_error='\0';
740 error=NTGetLastError();
741 if (error)
742 (void) CopyWizardString(last_error,error,WizardPathExtent);
743 error=(char *) RelinquishWizardMemory(error);
744 return(last_error);
745}
746
747/*
748%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
749% %
750% %
751% %
752% N T G e t L i b r a r y S y m b o l %
753% %
754% %
755% %
756%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757%
758% NTGetLibrarySymbol() retrieve the procedure address of the method
759% specified by the passed character string.
760%
761% The format of the NTGetLibrarySymbol method is:
762%
763% void *NTGetLibrarySymbol(void *handle,const char *name)
764%
765% A description of each parameter follows:
766%
767% o handle: Specifies a handle to the previously loaded dynamic module.
768%
769% o name: Specifies the procedure entry point to be returned.
770%
771*/
772void *NTGetLibrarySymbol(void *handle,const char *name)
773{
774 LPFNDLLFUNC1
775 lpfnDllFunc1;
776
777 lpfnDllFunc1=(LPFNDLLFUNC1) GetProcAddress(handle,name);
778 if (!lpfnDllFunc1)
779 return((void *) NULL);
780 return((void *) lpfnDllFunc1);
781}
782
783/*
784%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
785% %
786% %
787% %
788% N T G e t M o d u l e P a t h %
789% %
790% %
791% %
792%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793%
794% NTGetModulePath() returns the path of the specified module.
795%
796% The format of the GetModulePath method is:
797%
798% WizardBooleanType NTGetModulePath(const char *module,char *path)
799%
800% A description of each parameter follows:
801%
802% module: The module name.
803%
804% path: The module path is returned here.
805%
806*/
807WizardExport WizardBooleanType NTGetModulePath(const char *module,char *path)
808{
809 char
810 module_path[WizardPathExtent];
811
812 HMODULE
813 handle;
814
815 ssize_t
816 length;
817
818 *path='\0';
819 handle=GetModuleHandle(module);
820 if (handle == (HMODULE) NULL)
821 return(WizardFalse);
822 length=GetModuleFileName(handle,module_path,WizardPathExtent);
823 if (length != 0)
824 GetPathComponent(module_path,HeadPath,path);
825 return(WizardTrue);
826}
827
828/*
829%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
830% %
831% %
832% %
833% N T G a t h e r R a n d o m D a t a %
834% %
835% %
836% %
837%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
838%
839% NTGatherRandomData() gathers random data and returns it.
840%
841% The format of the GatherRandomData method is:
842%
843% WizardBooleanType NTGatherRandomData(const size_t length,
844% unsigned char *random)
845%
846% A description of each parameter follows:
847%
848% length: the length of random data buffer
849%
850% random: the random data is returned here.
851%
852*/
853WizardExport WizardBooleanType NTGatherRandomData(const size_t length,
854 unsigned char *random)
855{
856 HCRYPTPROV
857 handle;
858
859 int
860 status;
861
862 handle=(HCRYPTPROV) NULL;
863 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
864 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
865 if (status == 0)
866 status=CryptAcquireContext(&handle,NULL,MS_DEF_PROV,PROV_RSA_FULL,
867 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET));
868 if (status == 0)
869 return(WizardFalse);
870 status=CryptGenRandom(handle,(DWORD) length,random);
871 if (status == 0)
872 {
873 status=CryptReleaseContext(handle,0);
874 return(WizardFalse);
875 }
876 status=CryptReleaseContext(handle,0);
877 if (status == 0)
878 return(WizardFalse);
879 return(WizardTrue);
880}
881
882/*
883%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
884% %
885% %
886% %
887% N T I n i t i a l i z e L i b r a r y %
888% %
889% %
890% %
891%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
892%
893% NTInitializeLibrary() initializes the dynamic module loading subsystem.
894%
895% The format of the NTInitializeLibrary method is:
896%
897% int NTInitializeLibrary(void)
898%
899*/
900WizardExport int NTInitializeLibrary(void)
901{
902 return(0);
903}
904
905/*
906%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
907% %
908% %
909% %
910+ N T M a p M e m o r y %
911% %
912% %
913% %
914%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
915%
916% Mmap() emulates the Unix method of the same name.
917%
918% The format of the NTMapMemory method is:
919%
920% WizardExport void *NTMapMemory(char *address,size_t length,int protection,
921% int access,int file,WizardOffsetType offset)
922%
923*/
924WizardExport void *NTMapMemory(char *address,size_t length,int protection,
925 int flags,int file,WizardOffsetType offset)
926{
927 DWORD
928 access_mode,
929 high_length,
930 high_offset,
931 low_length,
932 low_offset,
933 protection_mode;
934
935 HANDLE
936 file_handle,
937 map_handle;
938
939 void
940 *map;
941
942 access_mode=0;
943 file_handle=INVALID_HANDLE_VALUE;
944 low_length=(DWORD) (length & 0xFFFFFFFFUL);
945 high_length=(DWORD) ((((WizardOffsetType) length) >> 32) & 0xFFFFFFFFUL);
946 map_handle=INVALID_HANDLE_VALUE;
947 map=(void *) NULL;
948 low_offset=(DWORD) (offset & 0xFFFFFFFFUL);
949 high_offset=(DWORD) ((offset >> 32) & 0xFFFFFFFFUL);
950 protection_mode=0;
951 if (protection & PROT_WRITE)
952 {
953 access_mode=FILE_MAP_WRITE;
954 if (!(flags & MAP_PRIVATE))
955 protection_mode=PAGE_READWRITE;
956 else
957 {
958 access_mode=FILE_MAP_COPY;
959 protection_mode=PAGE_WRITECOPY;
960 }
961 }
962 else
963 if (protection & PROT_READ)
964 {
965 access_mode=FILE_MAP_READ;
966 protection_mode=PAGE_READONLY;
967 }
968 if ((file == -1) && (flags & MAP_ANONYMOUS))
969 file_handle=INVALID_HANDLE_VALUE;
970 else
971 file_handle=(HANDLE) _get_osfhandle(file);
972 map_handle=CreateFileMapping(file_handle,0,protection_mode,high_length,
973 low_length,0);
974 if (map_handle)
975 {
976 map=(void *) MapViewOfFile(map_handle,access_mode,high_offset,low_offset,
977 length);
978 CloseHandle(map_handle);
979 }
980 if (map == (void *) NULL)
981 return((void *) MAP_FAILED);
982 return((void *) ((char *) map));
983}
984
985/*
986%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
987% %
988% %
989% %
990% N T O p e n D i r e c t o r y %
991% %
992% %
993% %
994%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995%
996% NTOpenDirectory() opens the directory named by filename and associates a
997% directory stream with it.
998%
999% The format of the NTOpenDirectory method is:
1000%
1001% DIR *NTOpenDirectory(const char *path)
1002%
1003% A description of each parameter follows:
1004%
1005% o entry: Specifies a pointer to a DIR structure.
1006%
1007*/
1008WizardExport DIR *NTOpenDirectory(const char *path)
1009{
1010 char
1011 file_specification[WizardPathExtent];
1012
1013 DIR
1014 *entry;
1015
1016 size_t
1017 length;
1018
1019 assert(path != (const char *) NULL);
1020 length=CopyWizardString(file_specification,path,WizardPathExtent);
1021 if (length >= (WizardPathExtent-1))
1022 return((DIR *) NULL);
1023 length=ConcatenateWizardString(file_specification,DirectorySeparator,
1025 if (length >= (WizardPathExtent-1))
1026 return((DIR *) NULL);
1027 entry=(DIR *) AcquireWizardMemory(sizeof(DIR));
1028 if (entry != (DIR *) NULL)
1029 {
1030 entry->firsttime=TRUE;
1031 entry->hSearch=FindFirstFile(file_specification,&entry->Win32FindData);
1032 }
1033 if (entry->hSearch == INVALID_HANDLE_VALUE)
1034 {
1035 length=ConcatenateWizardString(file_specification,"\\*.*",WizardPathExtent);
1036 if (length >= (WizardPathExtent-1))
1037 {
1038 entry=(DIR *) RelinquishWizardMemory(entry);
1039 return((DIR *) NULL);
1040 }
1041 entry->hSearch=FindFirstFile(file_specification,&entry->Win32FindData);
1042 if (entry->hSearch == INVALID_HANDLE_VALUE)
1043 {
1044 entry=(DIR *) RelinquishWizardMemory(entry);
1045 return((DIR *) NULL);
1046 }
1047 }
1048 return(entry);
1049}
1050
1051/*
1052%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1053% %
1054% %
1055% %
1056% N T O p e n L i b r a r y %
1057% %
1058% %
1059% %
1060%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1061%
1062% NTOpenLibrary() loads a dynamic module into memory and returns a handle that
1063% can be used to access the various procedures in the module.
1064%
1065% The format of the NTOpenLibrary method is:
1066%
1067% void *NTOpenLibrary(const char *filename)
1068%
1069% A description of each parameter follows:
1070%
1071% o path: Specifies a pointer to string representing dynamic module that
1072% is to be loaded.
1073%
1074*/
1075
1076static const char *GetSearchPath( void )
1077{
1078#if defined(WIZARDSTOOLKIT_LTDL_DELEGATE)
1079 return(lt_dlgetsearchpath());
1080#else
1081 return(lt_slsearchpath);
1082#endif
1083}
1084
1085WizardExport void *NTOpenLibrary(const char *filename)
1086{
1087#define MaxPathElements 31
1088
1089 char
1090 buffer[WizardPathExtent];
1091
1092 int
1093 index;
1094
1095 const char
1096 *p,
1097 *q;
1098
1099 int
1100 i;
1101
1102 UINT
1103 mode;
1104
1105 void
1106 *handle;
1107
1108 mode=SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
1109 handle=(void *) LoadLibraryEx(filename,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1110 if ((handle != (void *) NULL) || (GetSearchPath() == (char *) NULL))
1111 {
1112 SetErrorMode(mode);
1113 return(handle);
1114 }
1115 p=(char *) GetSearchPath();
1116 index=0;
1117 while (index < MaxPathElements)
1118 {
1119 q=strchr(p,DirectoryListSeparator);
1120 if (q == (char *) NULL)
1121 {
1122 (void) CopyWizardString(buffer,p,WizardPathExtent);
1123 (void) ConcatenateWizardString(buffer,"\\",WizardPathExtent);
1124 (void) ConcatenateWizardString(buffer,filename,WizardPathExtent);
1125 handle=(void *) LoadLibraryEx(buffer,NULL,
1126 LOAD_WITH_ALTERED_SEARCH_PATH);
1127 break;
1128 }
1129 i=q-p;
1130 (void) CopyWizardString(buffer,p,i+1);
1131 (void) ConcatenateWizardString(buffer,"\\",WizardPathExtent);
1132 (void) ConcatenateWizardString(buffer,filename,WizardPathExtent);
1133 handle=(void *) LoadLibraryEx(buffer,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
1134 if (handle != (void *) NULL)
1135 break;
1136 p=q+1;
1137 }
1138 SetErrorMode(mode);
1139 return(handle);
1140}
1141
1142/*
1143%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1144% %
1145% %
1146% %
1147% N T R e a d D i r e c t o r y %
1148% %
1149% %
1150% %
1151%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1152%
1153% NTReadDirectory() returns a pointer to a structure representing the
1154% directory entry at the current position in the directory stream to which
1155% entry refers.
1156%
1157% The format of the NTReadDirectory
1158%
1159% NTReadDirectory(entry)
1160%
1161% A description of each parameter follows:
1162%
1163% o entry: Specifies a pointer to a DIR structure.
1164%
1165*/
1166WizardExport struct dirent *NTReadDirectory(DIR *entry)
1167{
1168 int
1169 status;
1170
1171 size_t
1172 length;
1173
1174 if (entry == (DIR *) NULL)
1175 return((struct dirent *) NULL);
1176 if (!entry->firsttime)
1177 {
1178 status=FindNextFile(entry->hSearch,&entry->Win32FindData);
1179 if (status == 0)
1180 return((struct dirent *) NULL);
1181 }
1182 length=CopyWizardString(entry->file_info.d_name,
1183 entry->Win32FindData.cFileName,sizeof(entry->file_info.d_name));
1184 if (length >= sizeof(entry->file_info.d_name))
1185 return((struct dirent *) NULL);
1186 entry->firsttime=FALSE;
1187 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
1188 return(&entry->file_info);
1189}
1190
1191/*
1192%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1193% %
1194% %
1195% %
1196% N T R e g i s t r y K e y L o o k u p %
1197% %
1198% %
1199% %
1200%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1201%
1202% NTRegistryKeyLookup() returns Wizard's Toolkit installation path settings
1203% stored in the Windows Registry. Path settings are specific to the
1204% installed Wizard's Toolkit version so that multiple Image Wizard
1205% installations may coexist.
1206%
1207% Values are stored in the registry under a base path path similar to
1208% "HKEY_LOCAL_MACHINE/SOFTWARE\Wizard's Toolkit\1.0.1". The provided subkey
1209% is appended to this base path to form the full key.
1210%
1211% The format of the NTRegistryKeyLookup method is:
1212%
1213% char *NTRegistryKeyLookup(const char *subkey)
1214%
1215% A description of each parameter follows:
1216%
1217% o subkey: Specifies a string that identifies the registry object.
1218% Currently supported sub-keys include: "BinPath", "ConfigurePath",
1219% "LibPath", "CoderModulesPath", "FilterModulesPath", "SharePath".
1220%
1221*/
1222WizardExport unsigned char *NTRegistryKeyLookup(const char *subkey)
1223{
1224 char
1225 package_key[WizardPathExtent];
1226
1227 DWORD
1228 size,
1229 type;
1230
1231 HKEY
1232 registry_key;
1233
1234 LONG
1235 status;
1236
1237 unsigned char
1238 *value;
1239
1240 /*
1241 Look-up base key.
1242 */
1243 (void) FormatLocaleString(package_key,WizardPathExtent,"SOFTWARE\\%s\\%s",
1245 (void) LogWizardEvent(ConfigureEvent,GetWizardModule(),"%s",package_key);
1246 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1247 status=RegOpenKeyExA(HKEY_LOCAL_MACHINE,package_key,0,KEY_READ,&registry_key);
1248 if (status != ERROR_SUCCESS)
1249 {
1250 registry_key=(HKEY) INVALID_HANDLE_VALUE;
1251 return((unsigned char *) NULL);
1252 }
1253 /*
1254 Look-up sub key.
1255 */
1256 size=32;
1257 value=(unsigned char *) AcquireQuantumMemory(size,sizeof(*value));
1258 if (value == (unsigned char *) NULL)
1259 {
1260 RegCloseKey(registry_key);
1261 return((unsigned char *) NULL);
1262 }
1263 (void) LogWizardEvent(ConfigureEvent,GetWizardModule(),"%s",subkey);
1264 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1265 if ((status == ERROR_MORE_DATA) && (type == REG_SZ))
1266 {
1267 value=(unsigned char *) ResizeQuantumMemory(value,size,sizeof(*value));
1268 if (value == (BYTE *) NULL)
1269 {
1270 RegCloseKey(registry_key);
1271 return((unsigned char *) NULL);
1272 }
1273 status=RegQueryValueExA(registry_key,subkey,0,&type,value,&size);
1274 }
1275 RegCloseKey(registry_key);
1276 if ((type != REG_SZ) || (status != ERROR_SUCCESS))
1277 value=(unsigned char *) RelinquishWizardMemory(value);
1278 return((unsigned char *) value);
1279}
1280
1281/*
1282%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1283% %
1284% %
1285% %
1286% N T R e p o r t E v e n t %
1287% %
1288% %
1289% %
1290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1291%
1292% NTReportEvent() reports an event.
1293%
1294% The format of the NTReportEvent method is:
1295%
1296% WizardBooleanType NTReportEvent(const char *event,
1297% const WizardBooleanType error)
1298%
1299% A description of each parameter follows:
1300%
1301% o event: The event.
1302%
1303% o error: WizardTrue the event is an error.
1304%
1305*/
1306WizardExport WizardBooleanType NTReportEvent(const char *event,
1307 const WizardBooleanType error)
1308{
1309 const char
1310 *events[1];
1311
1312 HANDLE
1313 handle;
1314
1315 WORD
1316 type;
1317
1318 handle=RegisterEventSource(NULL,"Wizard's Toolkit");
1319 if (handle == NULL)
1320 return(WizardFalse);
1321 events[0]=event;
1322 type=error ? EVENTLOG_ERROR_TYPE : EVENTLOG_WARNING_TYPE;
1323 ReportEvent(handle,type,0,0,NULL,1,0,events,NULL);
1324 DeregisterEventSource(handle);
1325 return(WizardTrue);
1326}
1327
1328/*
1329%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1330% %
1331% %
1332% %
1333% N T R e s o u r c e T o B l o b %
1334% %
1335% %
1336% %
1337%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1338%
1339% NTResourceToBlob() returns a blob containing the contents of the resource
1340% in the current executable specified by the id parameter. This is currently
1341% used to retrieve MGK files tha have been embedded into the various command
1342% line utilities.
1343%
1344% The format of the NTResourceToBlob method is:
1345%
1346% unsigned char *NTResourceToBlob(const char *id)
1347%
1348% A description of each parameter follows:
1349%
1350% o id: Specifies a string that identifies the resource.
1351%
1352*/
1353WizardExport unsigned char *NTResourceToBlob(const char *id)
1354{
1355 char
1356 path[WizardPathExtent];
1357
1358 DWORD
1359 length;
1360
1361 HGLOBAL
1362 global;
1363
1364 HMODULE
1365 handle;
1366
1367 HRSRC
1368 resource;
1369
1370 unsigned char
1371 *blob,
1372 *value;
1373
1374 assert(id != (const char *) NULL);
1375 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"%s",id);
1376 (void) FormatLocaleString(path,WizardPathExtent,"%s%s%s",GetClientPath(),
1378 if (IsPathAcessible(path) != WizardFalse)
1379 handle=GetModuleHandle(path);
1380 else
1381 handle=GetModuleHandle(0);
1382 if (!handle)
1383 return((char *) NULL);
1384 resource=FindResource(handle,id,"WizardsToolkit");
1385 if (!resource)
1386 return((char *) NULL);
1387 global=LoadResource(handle,resource);
1388 if (!global)
1389 return((char *) NULL);
1390 length=SizeofResource(handle,resource);
1391 value=(unsigned char *) LockResource(global);
1392 if (!value)
1393 {
1394 FreeResource(global);
1395 return((char *) NULL);
1396 }
1397 blob=(unsigned char *) AcquireQuantumMemory(length+WizardPathExtent,
1398 sizeof(*blob));
1399 if (blob != (unsigned char *) NULL)
1400 {
1401 (void) CopyWizardMemory(blob,value,length);
1402 blob[length]='\0';
1403 }
1404 UnlockResource(global);
1405 FreeResource(global);
1406 return(blob);
1407}
1408
1409/*
1410%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1411% %
1412% %
1413% %
1414% N T S e e k D i r e c t o r y %
1415% %
1416% %
1417% %
1418%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1419%
1420% NTSeekDirectory() sets the position of the next NTReadDirectory() operation
1421% on the directory stream.
1422%
1423% The format of the NTSeekDirectory method is:
1424%
1425% void NTSeekDirectory(DIR *entry,ssize_t position)
1426%
1427% A description of each parameter follows:
1428%
1429% o entry: Specifies a pointer to a DIR structure.
1430%
1431% o position: specifies the position associated with the directory
1432% stream.
1433%
1434*/
1435WizardExport void NTSeekDirectory(DIR *entry,ssize_t position)
1436{
1438 assert(entry != (DIR *) NULL);
1439}
1440
1441/*
1442%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1443% %
1444% %
1445% %
1446% N T S e t S e a r c h P a t h %
1447% %
1448% %
1449% %
1450%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1451%
1452% NTSetSearchPath() sets the current locations that the subsystem should
1453% look at to find dynamically loadable modules.
1454%
1455% The format of the NTSetSearchPath method is:
1456%
1457% int NTSetSearchPath(const char *path)
1458%
1459% A description of each parameter follows:
1460%
1461% o path: Specifies a pointer to string representing the search path
1462% for DLL's that can be dynamically loaded.
1463%
1464*/
1465WizardExport int NTSetSearchPath(const char *path)
1466{
1467#if defined(WIZARDSTOOLKIT_LTDL_DELEGATE)
1468 lt_dlsetsearchpath(path);
1469#else
1470 if (lt_slsearchpath != (char *) NULL)
1471 lt_slsearchpath=(char *) RelinquishWizardMemory(lt_slsearchpath);
1472 if (path != (char *) NULL)
1473 lt_slsearchpath=AcquireString(path);
1474#endif
1475 return(0);
1476}
1477
1478/*
1479%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1480% %
1481% %
1482% %
1483+ N T S y n c M e m o r y %
1484% %
1485% %
1486% %
1487%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1488%
1489% NTSyncMemory() emulates the Unix method of the same name.
1490%
1491% The format of the NTSyncMemory method is:
1492%
1493% int NTSyncMemory(void *address,size_t length,int flags)
1494%
1495% A description of each parameter follows:
1496%
1497% o address: The address of the binary large object.
1498%
1499% o length: The length of the binary large object.
1500%
1501% o flags: Option flags (ignored for Windows).
1502%
1503*/
1504WizardExport int NTSyncMemory(void *address,size_t length,int flags)
1505{
1506 if (FlushViewOfFile(address,length) == WizardFalse)
1507 return(-1);
1508 return(0);
1509}
1510
1511/*
1512%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1513% %
1514% %
1515% %
1516% N T S y s t e m C o m m a n d %
1517% %
1518% %
1519% %
1520%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1521%
1522% NTSystemCommand() executes the specified command and waits until it
1523% terminates. The returned value is the exit status of the command.
1524%
1525% The format of the NTSystemComman method is:
1526%
1527% int NTSystemComman(const char *command)
1528%
1529% A description of each parameter follows:
1530%
1531% o command: This string is the command to execute.
1532%
1533*/
1534WizardExport int NTSystemCommand(const char *command)
1535{
1536 char
1537 local_command[WizardPathExtent];
1538
1539 DWORD
1540 child_status;
1541
1542 int
1543 status;
1544
1546 background_process;
1547
1548 PROCESS_INFORMATION
1549 process_info;
1550
1551 STARTUPINFO
1552 startup_info;
1553
1554 if (command == (char *) NULL)
1555 return(-1);
1556 GetStartupInfo(&startup_info);
1557 startup_info.dwFlags=STARTF_USESHOWWINDOW;
1558 startup_info.wShowWindow=SW_SHOWMINNOACTIVE;
1559 (void) CopyWizardString(local_command,command,WizardPathExtent);
1560 background_process=command[strlen(command)-1] == '&';
1561 if (background_process)
1562 local_command[strlen(command)-1]='\0';
1563 if (command[strlen(command)-1] == '|')
1564 local_command[strlen(command)-1]='\0';
1565 else
1566 startup_info.wShowWindow=SW_SHOWDEFAULT;
1567 status=CreateProcess((LPCTSTR) NULL,local_command,
1568 (LPSECURITY_ATTRIBUTES) NULL,(LPSECURITY_ATTRIBUTES) NULL,(BOOL) FALSE,
1569 (DWORD) NORMAL_PRIORITY_CLASS,(LPVOID) NULL,(LPCSTR) NULL,&startup_info,
1570 &process_info);
1571 if (status == 0)
1572 return(-1);
1573 if (background_process)
1574 return(status == 0);
1575 status=WaitForSingleObject(process_info.hProcess,INFINITE);
1576 if (status != WAIT_OBJECT_0)
1577 return (status);
1578 status=GetExitCodeProcess(process_info.hProcess,&child_status);
1579 if (status == 0)
1580 return(-1);
1581 CloseHandle(process_info.hProcess);
1582 CloseHandle(process_info.hThread);
1583 return((int) child_status);
1584}
1585
1586/*
1587%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1588% %
1589% %
1590% %
1591% N T S y s t e m C o n i f i g u r a t i o n %
1592% %
1593% %
1594% %
1595%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1596%
1597% Sysconf() provides a way for the application to determine values for
1598% system limits or options at runtime.
1599%
1600% The format of the exit method is:
1601%
1602% ssize_t NTSystemConfiguration(int name)
1603%
1604% A description of each parameter follows:
1605%
1606% o name: _SC_PAGE_SIZE or _SC_PHYS_PAGES.
1607%
1608*/
1609WizardExport ssize_t NTSystemConfiguration(int name)
1610{
1611 switch (name)
1612 {
1613 case _SC_PAGESIZE:
1614 {
1615 SYSTEM_INFO
1616 system_info;
1617
1618 GetSystemInfo(&system_info);
1619 return(system_info.dwPageSize);
1620 }
1621 case _SC_PHYS_PAGES:
1622 {
1623 HMODULE
1624 handle;
1625
1626 LPFNDLLFUNC2
1627 module;
1628
1629 NTMEMORYSTATUSEX
1630 status;
1631
1632 SYSTEM_INFO
1633 system_info;
1634
1635 handle=GetModuleHandle("kernel32.dll");
1636 if (handle == (HMODULE) NULL)
1637 return(0L);
1638 GetSystemInfo(&system_info);
1639 module=(LPFNDLLFUNC2) NTGetLibrarySymbol(handle,"GlobalMemoryStatusEx");
1640 if (module == (LPFNDLLFUNC2) NULL)
1641 {
1642 MEMORYSTATUS
1643 status;
1644
1645 GlobalMemoryStatus(&status);
1646 return((ssize_t) status.dwAvailPhys/system_info.dwPageSize/4);
1647 }
1648 status.dwLength=sizeof(status);
1649 if (module(&status) == 0)
1650 return(0L);
1651 return((ssize_t) status.ullAvailPhys/system_info.dwPageSize/4);
1652 }
1653 case _SC_OPEN_MAX:
1654 return(2048);
1655 default:
1656 break;
1657 }
1658 return(-1);
1659}
1660
1661/*
1662%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1663% %
1664% %
1665% %
1666% N T T e l l D i r e c t o r y %
1667% %
1668% %
1669% %
1670%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1671%
1672% NTTellDirectory() returns the current location associated with the named
1673% directory stream.
1674%
1675% The format of the NTTellDirectory method is:
1676%
1677% ssize_t NTTellDirectory(DIR *entry)
1678%
1679% A description of each parameter follows:
1680%
1681% o entry: Specifies a pointer to a DIR structure.
1682%
1683*/
1684WizardExport ssize_t NTTellDirectory(DIR *entry)
1685{
1686 assert(entry != (DIR *) NULL);
1687 return(0);
1688}
1689
1690/*
1691%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1692% %
1693% %
1694% %
1695+ N T U n m a p M e m o r y %
1696% %
1697% %
1698% %
1699%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1700%
1701% NTUnmapMemory() emulates the Unix munmap method.
1702%
1703% The format of the NTUnmapMemory method is:
1704%
1705% int NTUnmapMemory(void *map,size_t length)
1706%
1707% A description of each parameter follows:
1708%
1709% o map: The address of the binary large object.
1710%
1711% o length: The length of the binary large object.
1712%
1713*/
1714WizardExport int NTUnmapMemory(void *map,size_t length)
1715{
1716 if (!UnmapViewOfFile(map))
1717 return(-1);
1718 return(0);
1719}
1720
1721/*
1722%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1723% %
1724% %
1725% %
1726% N T U s e r T i m e %
1727% %
1728% %
1729% %
1730%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1731%
1732% NTUserTime() returns the total time the process has been scheduled (e.g.
1733% seconds) since the last call to StartTimer().
1734%
1735% The format of the UserTime method is:
1736%
1737% double NTUserTime(void)
1738%
1739*/
1740WizardExport double NTUserTime(void)
1741{
1742 DWORD
1743 status;
1744
1745 FILETIME
1746 create_time,
1747 exit_time;
1748
1749 OSVERSIONINFO
1750 OsVersionInfo;
1751
1752 union
1753 {
1754 FILETIME
1755 filetime;
1756
1757 __int64
1758 filetime64;
1759 } kernel_time;
1760
1761 union
1762 {
1763 FILETIME
1764 filetime;
1765
1766 __int64
1767 filetime64;
1768 } user_time;
1769
1770 OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
1771 GetVersionEx(&OsVersionInfo);
1772 if (OsVersionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
1773 return(NTElapsedTime());
1774 status=GetProcessTimes(GetCurrentProcess(),&create_time,&exit_time,
1775 &kernel_time.filetime,&user_time.filetime);
1776 if (status != TRUE)
1777 return(0.0);
1778 return((double) 1.0e-7*(kernel_time.filetime64+user_time.filetime64));
1779}
1780
1781/*
1782%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1783% %
1784% %
1785% %
1786% N T W a r n i n g H a n d l e r %
1787% %
1788% %
1789% %
1790%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1791%
1792% NTWarningHandler() displays a warning reason.
1793%
1794% The format of the NTWarningHandler method is:
1795%
1796% void NTWarningHandler(const ExceptionType warning,const char *reason,
1797% const char *description)
1798%
1799% A description of each parameter follows:
1800%
1801% o warning: Specifies the numeric warning category.
1802%
1803% o reason: Specifies the reason to display before terminating the
1804% program.
1805%
1806% o description: Specifies any description to the reason.
1807%
1808*/
1809WizardExport void NTWarningHandler(const ExceptionType warning,
1810 const char *reason,const char *description)
1811{
1812 char
1813 buffer[2*WizardPathExtent];
1814
1815 if (reason == (char *) NULL)
1816 return;
1817 if (description == (char *) NULL)
1818 (void) FormatLocaleString(buffer,WizardPathExtent,"%s: %s.\n",GetClientName(),
1819 reason);
1820 else
1821 (void) FormatLocaleString(buffer,WizardPathExtent,"%s: %s (%s).\n",
1822 GetClientName(),reason,description);
1823 (void) MessageBox(NULL,buffer,"Wizard's Toolkit Warning",MB_OK |
1824 MB_TASKMODAL | MB_SETFOREGROUND | MB_ICONINFORMATION);
1825}
1826#endif
#define MAP_FAILED
Definition blob.c:67
WizardExport const char * GetClientPath(void)
Definition client.c:87
WizardExport const char * GetClientName(void)
Definition client.c:64
#define ThrowFatalException(severity, tag)
WizardExport char * GetExceptionMessage(const int error)
Definition exception.c:471
ExceptionType
Definition exception.h:60
@ ResourceFatalError
Definition exception.h:121
WizardExport ssize_t FormatLocaleString(char *string, const size_t length, const char *format,...)
Definition locale.c:465
WizardBooleanType LogWizardEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition log.c:1390
@ TraceEvent
Definition log.h:39
@ ConfigureEvent
Definition log.h:44
#define GetWizardModule()
Definition log.h:30
WizardExport void * AcquireWizardMemory(const size_t size)
Definition memory.c:586
WizardExport void * CopyWizardMemory(void *destination, const void *source, const size_t size)
Definition memory.c:700
WizardExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition memory.c:657
WizardExport void * RelinquishWizardMemory(void *memory)
Definition memory.c:1039
WizardExport void * ResizeQuantumMemory(void *memory, const size_t count, const size_t quantum)
Definition memory.c:1236
#define WizardExport
#define WizardPathExtent
WizardExport void AsynchronousResourceComponentTerminus(void)
Definition resource.c:511
WizardExport char * DestroyString(char *string)
Definition string.c:830
WizardExport size_t ConcatenateWizardString(char *destination, const char *source, const size_t length)
Definition string.c:478
WizardExport char * AcquireString(const char *source)
Definition string.c:133
WizardExport size_t CopyWizardString(char *destination, const char *source, const size_t length)
Definition string.c:762
#define DirectorySeparator
Definition studio.h:239
#define Exit
Definition studio.h:241
#define DirectoryListSeparator
Definition studio.h:240
WizardExport void GetPathComponent(const char *path, PathType type, char *component)
Definition utility.c:415
WizardExport WizardBooleanType IsPathAcessible(const char *path)
Definition utility.c:779
@ HeadPath
Definition utility.h:30
#define WizardLibVersionText
Definition version.h:31
#define WizardPackageName
Definition version.h:28
ssize_t WizardOffsetType
Definition wizard-type.h:50
WizardBooleanType
Definition wizard-type.h:26
@ WizardTrue
Definition wizard-type.h:28
@ WizardFalse
Definition wizard-type.h:27
WizardExport void WizardsToolkitGenesis(const char *path)
Definition wizard.c:207
WizardExport void WizardsToolkitTerminus(void)
Definition wizard.c:334