1 /*
2   Copyright (C) 2003 - 2016 GraphicsMagick Group
3   Copyright (C) 2002 ImageMagick Studio
4 
5   This program is covered by multiple licenses, which are described in
6   Copyright.txt. You should have received a copy of Copyright.txt with this
7   package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
8 
9   Windows NT Utility Methods for GraphicsMagick.
10 */
11 #ifndef _MAGICK_NTBASE_H
12 #define _MAGICK_NTBASE_H
13 
14 #if defined(__cplusplus) || defined(c_plusplus)
15 extern "C" {
16 #endif
17 
18 #include "magick/delegate.h"
19 
20 #define WIN32_LEAN_AND_MEAN
21 #define VC_EXTRALEAN
22 #include <windows.h>
23 #include <winuser.h>
24 #include <wingdi.h>
25 #include <io.h>
26 #include <process.h>
27 #include <errno.h>
28 #include <malloc.h>
29 #if defined(_DEBUG)
30 #include <crtdbg.h>
31 #endif
32 
33 /*
34   Define declarations.
35 */
36 
37 #define F_OK 0
38 #define R_OK 4
39 #define W_OK 2
40 #define RW_OK 6
41 #define HAVE_VSNPRINTF 1
42 #define HAVE_RAISE 1
43 #define HAVE_SPAWNVP 1
44 #define HAVE_PROCESS_H 1
45 
46 #if defined(_VISUALC_)
47 #  define HAVE_CRYPTGENRANDOM 1
48 #  define HAVE_WINCRYPT_H 1
49 #endif
50 
51 /*
52   POSIX definitions for standard file numbers for use with _read() or _write()
53 */
54 #if !defined(STDIN_FILENO)
55 #  define STDIN_FILENO 0
56 #endif
57 #if !defined(STDOUT_FILENO)
58 #  define STDOUT_FILENO 1
59 #endif
60 #if !defined(STDERR_FILENO)
61 #  define STDERR_FILENO 2
62 #endif
63 
64 /*
65   Size type passed to read/write
66 */
67 #define MAGICK_POSIX_IO_SIZE_T unsigned int
68 
69 /*
70   libtiff features.
71 */
72 
73 /* Define to 1 if you have the <tiffconf.h> header file. */
74 #define HAVE_TIFFCONF_H 1
75 
76 /* Define to 1 if you have the `TIFFIsCODECConfigured' function. */
77 #define HAVE_TIFFISCODECCONFIGURED 1
78 
79 /* Define to 1 if you have the `TIFFMergeFieldInfo' function. */
80 #define HAVE_TIFFMERGEFIELDINFO 1
81 
82 /* Define to 1 if you have the `TIFFSetErrorHandlerExt' function. */
83 #define HAVE_TIFFSETERRORHANDLEREXT 1
84 
85 /* Define to 1 if you have the `TIFFSetTagExtender' function. */
86 #define HAVE_TIFFSETTAGEXTENDER 1
87 
88 /* Define to 1 if you have the `TIFFSetWarningHandlerExt' function. */
89 #define HAVE_TIFFSETWARNINGHANDLEREXT 1
90 
91 /* Define to 1 if you have the `TIFFSwabArrayOfTriples' function. */
92 #define HAVE_TIFFSWABARRAYOFTRIPLES 1
93 
94 /* Define to 1 if you have the <ft2build.h> header file. */
95 #define HAVE_FT2BUILD_H 1
96 
97 // Define to support memory mapping files for improved performance
98 #define HAVE_MMAP_FILEIO 1
99 
100 // Use Visual C++ C inline method extension to improve performance
101 #if !defined(inline)
102 #  define inline __inline
103 #endif
104 
105 // Visual C++ does not usually seem to support the C'99 restrict keyword
106 // Maybe it will be added in some version.
107 #if defined(_VISUALC_)  /* && (_MSC_VER <= 1500) */
108 #  define restrict /* nothing */
109 #endif
110 
111 #if !defined(chsize)
112 # if defined(__BORLANDC__)
113 #   define chsize(file,length) chsize(file,length)
114 # else
115 #   define chsize(file,length) _chsize(file,length)
116 # endif
117 #endif
118 
119 #if !defined(hypot)
120 #  define hypot  _hypot
121 #endif
122 
123 // We do not yet know what CRT versions have these float functions or if
124 // special-handling (e.g. mapping to underscore version) is needed.
125 // It is known that Visual Studio 2003 does not offer these functions.
126 #if (defined(_MSC_VER) && _MSC_VER >= 1500)
127 
128 /* Define to 1 if you have the `cosf' function. */
129 #define HAVE_COSF 1
130 
131 /* Define to 1 if you have the `fabsf' function. */
132 #define HAVE_FABSF 1
133 
134 /* Define to 1 if you have the `logf' function. */
135 #define HAVE_LOGF 1
136 
137 /* Define to 1 if you have the `sinf' function. */
138 #define HAVE_SINF 1
139 
140 /* Define to 1 if you have the `sqrtf' function. */
141 #define HAVE_SQRTF 1
142 
143 #endif // defined(_VISUALC_)
144 
145 #if !defined(_MSC_VER) || (defined(_MSC_VER) && _MSC_VER < 1500)
146 #define vsnprintf _vsnprintf
147 #endif
148 
149 #if defined(_MT) && defined(MSWINDOWS)
150 #define SAFE_GLOBAL __declspec(thread)
151 #else
152 #define SAFE_GLOBAL
153 #endif
154 
155 /*
156   With Visual C++, Popen and pclose are available via _popen and _pclose.
157   These are documented to work for console applications only.
158 */
159 #define HAVE_POPEN 1
160 #define HAVE__POPEN 1
161 #if !defined(popen)
162 #  define popen(command,mode)  _popen(command,mode)
163 #endif /* !defined(popen) */
164 #define HAVE_PCLOSE 1
165 #define HAVE__PCLOSE 1
166 #if !defined(pclose)
167 #  define pclose(stream)  _pclose(stream)
168 #endif /* !defined(pclose) */
169 
170 /*
171   Visual C++ 7.0 supports GlobalMemoryStatusEx but MinGW headers and
172   Visual C++ 6.0 lack support for it.
173 
174   _MSC_VER values (see https://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B):
175     1100 MSVC 5.0
176     1200 MSVC 6.0
177     1300 MSVC 7.0 Visual C++ .NET 2002
178     1310 Visual C++ .NET 2003
179     1400 Visual C++ 2005
180     1500 Visual C++ 2008
181     1600 Visual C++ 2010
182     1700 Visual C++ 2012
183     1800 Visual C++ 2013
184     1900 Visual C++ 2015
185     1910 Visual C++ 2017
186     1920 Visual C++ 2019
187 
188   Should look at __CLR_VER ("Defines the version of the common language
189   runtime used when the application was compiled.") as well.
190 */
191 #if defined(_VISUALC_) && (_MSC_VER >= 1310)
192 #  define HAVE_GLOBALMEMORYSTATUSEX 1
193 #endif
194 
195 /*
196   _aligned_malloc was introduced in Visual Studio .NET 2003.
197   The feature is is introduced in msvcr71.dll and is capable of
198   working on Windows '98 given that the correct run-time libraries
199   are installed.
200 */
201 #if ((defined(_VISUALC_) && (_MSC_VER >= 1310)) ||              \
202      (defined(__MINGW32__) && (__MSVCRT_VERSION__ >= 0x0700)))
203 #  define HAVE__ALIGNED_MALLOC 1
204 #endif
205 
206 /*
207   Windows provides Unix-style access() via _access()
208  */
209 #define HAVE_ACCESS 1
210 #define access(path,mode) _access(path,mode)
211 
212 /*
213   Windows provides Unix-style mkdir() via _mkdir() but it does not
214   seem to accept a 'mode' argument.
215  */
216 #define mkdir(path,mode) _mkdir(path)
217 
218 /*
219   Windows provides _commit() as a substitute for fsync()
220 */
221 #define fsync(fd) _commit(fd)
222 
223 #if !defined(isatty)
224 #  define isatty(x) _isatty(x)
225 #endif
226 
227 #if !defined(fileno)
228 #  define fileno(x) _fileno(x)
229 #endif
230 
231 #if !defined(unlink)
232 #  define unlink(path) _unlink(path)
233 #endif /* !defined(unlink) */
234 
235 
236 /*
237   I/O defines.
238 */
239 #if !defined(Windows95) && !defined(__BORLANDC__)
240   /* Windows '95 and Borland C do not support _lseeki64 */
241 #  define MagickSeek(fildes,offset,whence)  _lseeki64(fildes,/* __int64 */ offset,whence)
242 #  define MagickTell(fildes) /* __int64 */ _telli64(fildes)
243 #else
244 #  define MagickSeek(fildes,offset,whence)  lseek(fildes,offset,whence)
245 #  define MagickTell(fildes) (MagickSeek(fildes,0,SEEK_CUR))
246 #endif
247 
248 
249 #if !defined(Windows95) && !defined(__BORLANDC__) && \
250   !(defined(_MSC_VER) && _MSC_VER < 1400) && \
251   !(defined(__MSVCRT_VERSION__) && __MSVCRT_VERSION__ < 0x800)
252 
253   /*
254     Windows '95 and Borland C do not support _lseeki64
255     Visual Studio does not support _fseeki64 and _ftelli64 until the 2005 release.
256     Without these interfaces, files over 2GB in size are not supported for Windows.
257   */
258 #  define MagickFseek(stream,offset,whence) _fseeki64(stream,/* __int64 */ offset,whence)
259 #  define MagickFstat(fildes,stat_buff) _fstati64(fildes,/* struct _stati64 */ stat_buff)
260 #  define MagickFtell(stream) /* __int64 */ _ftelli64(stream)
261 #  define MagickStatStruct_t struct _stati64
262 #  define MagickStat(path,stat_buff) _stati64(path,/* struct _stati64 */ stat_buff)
263 #else
264 #  define MagickFseek(stream,offset,whence) fseek(stream,offset,whence)
265 #  define MagickFstat(fildes,stat_buff) fstat(fildes,stat_buff)
266 #  define MagickFtell(stream) ftell(stream)
267 #  define MagickStatStruct_t struct stat
268 #  define MagickStat(path,stat_buff) stat(path,stat_buff)
269 #endif
270 
271 /*
272   The isnan() function was implemented starting in Visual Studio 2003
273   The function prototype for isnan() and _isnanf() comes from <math.h>
274   and the prototype for _isnan() comes from <float.h>.  The _isnanf()
275   function is only available in _WIN64 builds.
276 
277   Some useful documentation was found at
278   https://msdn.microsoft.com/en-us/library/tzthab44.aspx.
279 */
280 #if (defined(_MSC_VER) && _MSC_VER < 1700 && !defined(isnan))
281 #  if defined(_WIN64)
282 #    define isnan(f) _isnanf(f)
283 #  else
284 #    include <float.h> /* Needed for _isnan */
285 #    if !defined(isnan)
286 #      define isnan(f) _isnan(f)
287 #    endif
288 #  endif
289 #endif
290 
291 
292 /*
293   Typedef declarations.
294 */
295 typedef UINT (CALLBACK *LPFNDLLFUNC1)(DWORD,UINT);
296 
297 #if !defined(XS_VERSION) /* Not in Perl extension */
298 
299 /*
300   For POSIX, ssize_t is the type returned by _read and _write.
301   Recent MinGW compilers include this typedef by default.
302 
303   Note that under WIN64 read/write appear to still return 'int' and use
304   'unsigned int' rather than 'size_t' to specify the I/O size.  This really
305   sucks!
306  */
307 #if !defined(ssize_t) && !defined(__MINGW32__) && !defined(__MINGW64__)
308 #  if defined(WIN64)
309   typedef __int64 ssize_t;
310 #  else
311   typedef int ssize_t;
312 #  endif
313 #endif /* !defined(ssize_t) && !defined(__MINGW32__) && !defined(__MINGW64__)*/
314 
315 #endif /* !defined(XS_VERSION) */
316 
317 /*
318   Bzlib is strange in that symbols from bzlib.h are DLL-exported by
319   default rather than imported.  This feels like a bug to me.
320 */
321 #if defined(HasBZLIB)
322 #  if defined(_WIN32)
323 #    define BZ_IMPORT 1
324 #  endif
325 #endif /* defined(HasBZLIB) */
326 
327 
328 /*
329   NT utilities routines.
330 */
331 
332 extern MagickExport char
333   *NTGetLastError(void);
334 
335 /*
336   Ghostscript DLL support.
337 */
338 extern MagickExport int
339   NTGhostscriptDLL(char *path, int path_length),
340   NTGhostscriptEXE(char *path, int path_length),
341   NTGhostscriptFonts(char *path, int path_length),
342   NTGhostscriptLoadDLL(void),
343   NTGhostscriptUnLoadDLL(void);
344 
345 extern MagickExport const GhostscriptVectors
346   *NTGhostscriptDLLVectors( void );
347 
348 /*
349   Directory access functions
350 */
351 #if !defined(HAVE_DIRENT_H) || defined(__MINGW32__) || defined(__MINGW64__)
352 struct dirent
353 {
354   char
355     d_name[2048];
356 
357   int
358     d_namlen;
359 };
360 
361 typedef struct _DIR
362 {
363   HANDLE
364     hSearch;
365 
366   WIN32_FIND_DATA
367     Win32FindData;
368 
369   BOOL
370     firsttime;
371 
372   struct dirent
373     file_info;
374 } DIR;
375 
376 extern MagickExport long
377   NTtelldir(DIR *);
378 
379 extern MagickExport struct dirent
380   *NTreaddir(DIR *);
381 
382 extern MagickExport DIR
383   *NTopendir(const char *);
384 
385 extern MagickExport int
386   NTclosedir(DIR *);
387 
388 extern MagickExport void
389   NTseekdir(DIR *,long);
390 
391 #define closedir(dir) NTclosedir(dir)
392 #define opendir(path) NTopendir(path)
393 #define readdir(entry) NTreaddir(entry)
394 #define seekdir(entry,position) NTseekdir(entry,position)
395 #define telldir(entry) NTtelldir(entry)
396 #endif /* !defined(HAVE_DIRENT_H) */
397 
398 /*
399   System functions.
400 */
401 extern MagickExport int
402   NTSystemComman(const char *);
403 
404 #if !defined(XS_VERSION)  /* Not in Perl extension */
405 
406 /*
407   Precision timing functions.
408 */
409 extern MagickExport double
410   NTElapsedTime(void),
411   NTUserTime(void);
412 
413 /*
414   Memory mapped file support.
415 */
416 #define PROT_NONE       0x0  // pages can not be accessed
417 #define PROT_READ       0x1  // pages can be read
418 #define PROT_WRITE      0x2  // pages can be written
419 #define MAP_SHARED      0x1  // share changes
420 #define MAP_PRIVATE     0x2  // changes are private
421 #define MAP_NORESERVE   0x4  // do not reserve paging space
422 #define MAP_ANON        0x8  // anonymous mapping
423 #if !defined(MAP_FAILED)
424 #  define MAP_FAILED      ((void *) -1) // returned on error by mmap
425 #endif
426 #define MS_ASYNC        0x0  // asynchronous page sync
427 #define MS_SYNC         0x1  // synchronous page sync
428 
429 extern MagickExport void
430   Exit(int) MAGICK_FUNC_NORETURN,
431   *NTmmap(char *address,size_t length,int protection,int access,int file,
432      magick_off_t offset);
433 
434 extern MagickExport int
435   NTftruncate(int filedes, off_t length),
436   NTmsync(void *addr, size_t len, int flags),
437   NTmunmap(void *addr, size_t len);
438 
439 extern MagickExport int
440   MagickGetFileAttributes(const char *filename, MagickStatStruct_t *statbuf);
441 
442 extern MagickExport int
443   MagickSetFileAttributes(const char *filename, const MagickStatStruct_t *statbuf);
444 
445 #define MagickMmap(address,length,protection,access,file,offset) \
446   NTmmap(address,length,protection,access,file,offset)
447 #define MagickMsync(addr,len,flags) NTmsync(addr,len,flags)
448 #define MagickMunmap(addr,len) NTmunmap(addr,len)
449 #define MagickFtruncate(filedes,length) NTftruncate(filedes,length)
450 
451 /*
452   libltdl-like module loader wrappers
453 */
454 #if !defined(HasLTDL)
455 extern MagickExport void
456   *NTdlopen(const char *filename),
457   *NTdlsym(void *handle, const char *name);
458 
459 extern MagickExport int
460   NTdlclose(void *handle),
461   NTdlexit(void),
462   NTdlinit(void),
463   NTdlsetsearchpath(const char *);
464 
465 extern MagickExport const char
466   *NTdlerror(void);
467 
468 #define lt_dlclose(handle) NTdlclose(handle)
469 #define lt_dlerror() NTdlerror()
470 #define lt_dlexit() NTdlexit()
471 #define lt_dlinit() NTdlinit()
472 #define lt_dlopen(filename) NTdlopen(filename)
473 #define lt_dlsetsearchpath(path) NTdlsetsearchpath(path)
474 #define lt_dlsym(handle,name) NTdlsym(handle,name)
475 #endif /* !defined(HasLTDL) */
476 
477 extern MagickExport unsigned char
478   *NTResourceToBlob(const char *);
479 
480 extern MagickExport MagickPassFail
481   NTGetExecutionPath(char *path);
482 
483 extern MagickExport MagickBool
484   IsWindows95(),
485   NTKernelAPISupported(const char *name);
486 
487 extern MagickExport void
488   NTErrorHandler(const ExceptionType,const char *,const char *),
489   NTWarningHandler(const ExceptionType,const char *,const char *);
490 
491 extern MagickExport long
492   MagickGetMMUPageSize();
493 
494 extern MagickExport void
495   NTInitializeExceptionHandlers();
496 
497 #endif /* !XS_VERSION */
498 
499 #if defined(__cplusplus) || defined(c_plusplus)
500 }
501 #endif /* !C++ */
502 
503 #endif /* !_MAGICK_NTBASE_H */
504 
505 /*
506  * Local Variables:
507  * mode: c
508  * c-basic-offset: 2
509  * fill-column: 78
510  * End:
511  */
512