1 /*
2   Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization
3   dedicated to making software imaging solutions freely available.
4 
5   You may not use this file except in compliance with the License.  You may
6   obtain a copy of the License at
7 
8     https://imagemagick.org/script/license.php
9 
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 
16   MagickCore Windows NT utility methods.
17 */
18 #ifndef MAGICKCORE_NT_BASE_H
19 #define MAGICKCORE_NT_BASE_H
20 
21 #include "MagickCore/exception.h"
22 #include "MagickCore/geometry.h"
23 
24 #if defined(__cplusplus) || defined(c_plusplus)
25 extern "C" {
26 #endif
27 
28 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
29 
30 #define WIN32_LEAN_AND_MEAN
31 #define VC_EXTRALEAN
32 #define _CRT_SECURE_NO_DEPRECATE  1
33 #include <windows.h>
34 #include <wchar.h>
35 #include <winuser.h>
36 #include <wingdi.h>
37 #include <io.h>
38 #include <process.h>
39 #include <errno.h>
40 #include <malloc.h>
41 #include <sys/utime.h>
42 #if defined(_DEBUG) && !defined(__MINGW32__)
43 #include <crtdbg.h>
44 #endif
45 
46 #define PROT_READ  0x01
47 #define PROT_WRITE  0x02
48 #define MAP_SHARED  0x01
49 #define MAP_PRIVATE  0x02
50 #define MAP_ANONYMOUS  0x20
51 #define F_OK 0
52 #define R_OK 4
53 #define W_OK 2
54 #define RW_OK 6
55 #define _SC_PAGESIZE 1
56 #define _SC_PHYS_PAGES 2
57 #define _SC_OPEN_MAX 3
58 #if !defined(SSIZE_MAX)
59 #define SSIZE_MAX  0x7fffffffL
60 #endif
61 
62 /*
63   _MSC_VER values:
64     1100 MSVC 5.0
65     1200 MSVC 6.0
66     1300 MSVC 7.0 Visual C++ .NET 2002
67     1310 Visual c++ .NET 2003
68     1400 Visual C++ 2005
69     1500 Visual C++ 2008
70     1600 Visual C++ 2010
71     1700 Visual C++ 2012
72     1800 Visual C++ 2013
73     1900 Visual C++ 2015
74 */
75 
76 #if !defined(chsize)
77 # if defined(__BORLANDC__)
78 #   define chsize(file,length)  chsize(file,length)
79 # else
80 #   define chsize(file,length)  _chsize(file,length)
81 # endif
82 #endif
83 
84 #if !defined(access)
85 #if defined(_VISUALC_) && (_MSC_VER >= 1400)
86 #  define access(path,mode)  _access_s(path,mode)
87 #endif
88 #endif
89 #if !defined(chdir)
90 #  define chdir  _chdir
91 #endif
92 #if !defined(close)
93 #  define close  _close
94 #endif
95 #if !defined(closedir)
96 #  define closedir(directory)  NTCloseDirectory(directory)
97 #endif
98 #define MAGICKCORE_HAVE_ERF
99 #if defined(_VISUALC_) && (_MSC_VER < 1700)
100 #  define erf(x)  NTErf(x)
101 #endif
102 #if !defined(fdopen)
103 #  define fdopen  _fdopen
104 #endif
105 #if !defined(fileno)
106 #  define fileno  _fileno
107 #endif
108 #if !defined(freelocale)
109 #  define freelocale  _free_locale
110 #endif
111 #if !defined(fseek) && !defined(__MINGW32__)
112 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(Windows95) && \
113   !(defined(_MSC_VER) && (_MSC_VER < 1400)) && \
114   !(defined(__MSVCRT_VERSION__) && (__MSVCRT_VERSION__ < 0x800))
115 #  define fseek  _fseeki64
116 #endif
117 #endif
118 #if !defined(fstat) && !defined(__BORLANDC__)
119 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(Windows95) && \
120   !(defined(_MSC_VER) && (_MSC_VER < 1400)) && \
121   !(defined(__MSVCRT_VERSION__) && (__MSVCRT_VERSION__ < 0x800))
122 #  define fstat  _fstati64
123 #else
124 #  define fstat  _fstat
125 #endif
126 #endif
127 #if !defined(fsync)
128 #  define fsync  _commit
129 #endif
130 #if !defined(ftell) && !defined(__MINGW32__)
131 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(Windows95) && \
132   !(defined(_MSC_VER) && (_MSC_VER < 1400)) && \
133   !(defined(__MSVCRT_VERSION__) && (__MSVCRT_VERSION__ < 0x800))
134 #  define ftell  _ftelli64
135 #endif
136 #endif
137 #if !defined(ftruncate)
138 #  define ftruncate(file,length)  NTTruncateFile(file,length)
139 #endif
140 #if !defined(getcwd)
141 #  define getcwd  _getcwd
142 #endif
143 #if !defined(getpid)
144 #  define getpid  _getpid
145 #endif
146 #if !defined(hypot)
147 #  define hypot  _hypot
148 #endif
149 #if !defined(isatty)
150 #  define isatty  _isatty
151 #endif
152 #if !defined(locale_t)
153 #define locale_t _locale_t
154 #endif
155 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(Windows95) && \
156   !(defined(_MSC_VER) && (_MSC_VER < 1400)) && \
157   !(defined(__MSVCRT_VERSION__) && (__MSVCRT_VERSION__ < 0x800))
158 #if !defined(lseek)
159 #  define lseek  _lseeki64
160 #endif
161 #else
162 #if !defined(lseek)
163 #  define lseek  _lseek
164 #endif
165 #endif
166 #if !defined(MAGICKCORE_LTDL_DELEGATE)
167 #if !defined(lt_dlclose)
168 #  define lt_dlclose(handle)  NTCloseLibrary(handle)
169 #endif
170 #if !defined(lt_dlerror)
171 #  define lt_dlerror()  NTGetLibraryError()
172 #endif
173 #if !defined(lt_dlexit)
174 #  define lt_dlexit()  NTExitLibrary()
175 #endif
176 #if !defined(lt_dlinit)
177 #  define lt_dlinit()  NTInitializeLibrary()
178 #endif
179 #if !defined(lt_dlopen)
180 #  define lt_dlopen(filename)  NTOpenLibrary(filename)
181 #endif
182 #if !defined(lt_dlsetsearchpath)
183 #  define lt_dlsetsearchpath(path)  NTSetSearchPath(path)
184 #endif
185 #if !defined(lt_dlsym)
186 #  define lt_dlsym(handle,name)  NTGetLibrarySymbol(handle,name)
187 #endif
188 #endif
189 #if !defined(mkdir)
190 #  define mkdir  _mkdir
191 #endif
192 #if !defined(mmap)
193 #  define mmap(address,length,protection,access,file,offset) \
194   NTMapMemory(address,length,protection,access,file,offset)
195 #endif
196 #if !defined(msync)
197 #  define msync(address,length,flags)  NTSyncMemory(address,length,flags)
198 #endif
199 #if !defined(munmap)
200 #  define munmap(address,length)  NTUnmapMemory(address,length)
201 #endif
202 #if !defined(opendir)
203 #  define opendir(directory)  NTOpenDirectory(directory)
204 #endif
205 #if !defined(open)
206 #  define open  _open
207 #endif
208 #if !defined(pclose)
209 #  define pclose  _pclose
210 #endif
211 #if !defined(popen)
212 #  define popen  _popen
213 #endif
214 #if !defined(fprintf_l)
215 #define fprintf_l  _fprintf_s_l
216 #endif
217 #if !defined(read)
218 #  define read(fd,buffer,count)  _read(fd,buffer,(unsigned int) count)
219 #endif
220 #if !defined(readdir)
221 #  define readdir(directory)  NTReadDirectory(directory)
222 #endif
223 #if !defined(seekdir)
224 #  define seekdir(directory,offset)  NTSeekDirectory(directory,offset)
225 #endif
226 #if !defined(setmode)
227 #  define setmode  _setmode
228 #endif
229 #if !defined(spawnvp)
230 #  define spawnvp  _spawnvp
231 #endif
232 #if !defined(strtod_l)
233 #define strtod_l  _strtod_l
234 #endif
235 #if !defined(stat) && !defined(__BORLANDC__)
236 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(Windows95) && \
237   !(defined(_MSC_VER) && (_MSC_VER < 1400)) && \
238   !(defined(__MSVCRT_VERSION__) && (__MSVCRT_VERSION__ < 0x800))
239 #  define stat  _stati64
240 #else
241 #  define stat  _stat
242 #endif
243 #endif
244 #if !defined(strcasecmp)
245 #  define strcasecmp  _stricmp
246 #endif
247 #if !defined(strncasecmp)
248 #  define strncasecmp  _strnicmp
249 #endif
250 #if !defined(sysconf)
251 #  define sysconf(name)  NTSystemConfiguration(name)
252 #endif
253 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(Windows95) && \
254   !(defined(_MSC_VER) && (_MSC_VER < 1400)) && \
255   !(defined(__MSVCRT_VERSION__) && (__MSVCRT_VERSION__ < 0x800))
256 #  define tell  _telli64
257 #else
258 #  define tell  _tell
259 #endif
260 #if !defined(telldir)
261 #  define telldir(directory)  NTTellDirectory(directory)
262 #endif
263 #if !defined(tempnam)
264 #  define tempnam  _tempnam_s
265 #endif
266 #if !defined(tolower_l)
267 #define tolower_l  _tolower_l
268 #endif
269 #if !defined(toupper_l)
270 #define toupper_l  _toupper_l
271 #endif
272 #if !defined(umask)
273 #  define umask  _umask
274 #endif
275 #if !defined(unlink)
276 #  define unlink  _unlink
277 #endif
278 #if !defined(utime)
279 #  define utime(filename,time)  _utime(filename,(struct _utimbuf*) time)
280 #endif
281 #if !defined(vfprintf_l)
282 #define vfprintf_l  _vfprintf_l
283 #endif
284 #if !defined(vsnprintf)
285 #if !defined(_MSC_VER) || (defined(_MSC_VER) && _MSC_VER < 1500)
286 #define vsnprintf _vsnprintf
287 #endif
288 #endif
289 #if !defined(vsnprintf_l)
290 #define vsnprintf_l  _vsnprintf_l
291 #endif
292 #if !defined(write)
293 #  define write(fd,buffer,count)  _write(fd,buffer,(unsigned int) count)
294 #endif
295 #if !defined(wstat) && !defined(__BORLANDC__)
296 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(Windows95) && \
297   !(defined(_MSC_VER) && (_MSC_VER < 1400)) && \
298   !(defined(__MSVCRT_VERSION__) && (__MSVCRT_VERSION__ < 0x800))
299 #  define wstat  _wstati64
300 #else
301 #  define wstat  _wstat
302 #endif
303 #endif
304 
305 #if defined(__BORLANDC__)
306 #undef _O_RANDOM
307 #define _O_RANDOM 0
308 #undef _O_SEQUENTIAL
309 #define _O_SEQUENTIAL 0
310 #undef _O_SHORT_LIVED
311 #define _O_SHORT_LIVED 0
312 #undef _O_TEMPORARY
313 #define _O_TEMPORARY 0
314 #endif
315 
316 #undef gettimeofday
317 
318 typedef struct _GhostInfo
319   GhostInfo_;
320 
321 extern MagickExport char
322   **NTArgvToUTF8(const int argc,wchar_t **);
323 
324 extern MagickExport const GhostInfo_
325   *NTGhostscriptDLLVectors(void);
326 
327 extern MagickExport void
328   NTErrorHandler(const ExceptionType,const char *,const char *),
329   NTGhostscriptUnLoadDLL(void),
330   NTWarningHandler(const ExceptionType,const char *,const char *);
331 #endif
332 
333 #if defined(__cplusplus) || defined(c_plusplus)
334 }
335 #endif
336 
337 #endif
338