Move PRIu64 to e_os.h
[openssl.git] / e_os.h
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #ifndef HEADER_E_OS_H
11 # define HEADER_E_OS_H
12
13 # include <openssl/opensslconf.h>
14
15 # include <openssl/e_os2.h>
16 /*
17  * <openssl/e_os2.h> contains what we can justify to make visible to the
18  * outside; this file e_os.h is not part of the exported interface.
19  */
20
21 #ifdef  __cplusplus
22 extern "C" {
23 #endif
24
25 /* Used to checking reference counts, most while doing perl5 stuff :-) */
26 # if defined(OPENSSL_NO_STDIO)
27 #  if defined(REF_PRINT)
28 #   error "REF_PRINT requires stdio"
29 #  endif
30 # endif
31
32 /*
33  * We need a format operator for some client tools for uint64_t.  If inttypes.h
34  * isn't available or did not define it, just go with hard-coded.
35  */
36 # if defined(OPENSSL_SYS_UEFI)
37 #  define PRIu64 "Lu"
38 # endif
39 # ifndef PRIu64
40 #  ifdef SIXTY_FOUR_BIT_LONG
41 #   define PRIu64 "lu"
42 #  else
43 #   define PRIu64 "llu"
44 #  endif
45 # endif
46
47 # if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO)
48 #  define REF_ASSERT_ISNT(test) \
49     (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0)
50 # else
51 #  define REF_ASSERT_ISNT(i)
52 # endif
53 # ifdef REF_PRINT
54 #  define REF_PRINT_COUNT(a, b) \
55         fprintf(stderr, "%p:%4d:%s\n", b, b->references, a)
56 # else
57 #  define REF_PRINT_COUNT(a, b)
58 # endif
59
60 # define osslargused(x)      (void)x
61 # define OPENSSL_CONF        "openssl.cnf"
62
63 # ifndef DEVRANDOM
64 /*
65  * set this to a comma-separated list of 'random' device files to try out. My
66  * default, we will try to read at least one of these files
67  */
68 #  define DEVRANDOM "/dev/urandom","/dev/random","/dev/srandom"
69 # endif
70 # if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
71 /*
72  * set this to a comma-separated list of 'egd' sockets to try out. These
73  * sockets will be tried in the order listed in case accessing the device
74  * files listed in DEVRANDOM did not return enough entropy.
75  */
76 #  define DEVRANDOM_EGD "/var/run/egd-pool","/dev/egd-pool","/etc/egd-pool","/etc/entropy"
77 # endif
78
79 # if defined(OPENSSL_SYS_VXWORKS)
80 #  define NO_SYS_PARAM_H
81 #  define NO_CHMOD
82 #  define NO_SYSLOG
83 # endif
84
85 /********************************************************************
86  The Microsoft section
87  ********************************************************************/
88 # if defined(OPENSSL_SYS_WIN32) && !defined(WIN32)
89 #  define WIN32
90 # endif
91 # if defined(OPENSSL_SYS_WINDOWS) && !defined(WINDOWS)
92 #  define WINDOWS
93 # endif
94 # if defined(OPENSSL_SYS_MSDOS) && !defined(MSDOS)
95 #  define MSDOS
96 # endif
97
98 # if (defined(MSDOS) || defined(OPENSSL_SYS_UEFI)) && !defined(GETPID_IS_MEANINGLESS)
99 #  define GETPID_IS_MEANINGLESS
100 # endif
101
102 # ifdef WIN32
103 #  define NO_SYS_UN_H
104 #  define get_last_sys_error()    GetLastError()
105 #  define clear_sys_error()       SetLastError(0)
106 #  if !defined(WINNT)
107 #   define WIN_CONSOLE_BUG
108 #  endif
109 # else
110 #  define get_last_sys_error()    errno
111 #  define clear_sys_error()       errno=0
112 # endif
113
114 # if defined(WINDOWS)
115 #  define get_last_socket_error() WSAGetLastError()
116 #  define clear_socket_error()    WSASetLastError(0)
117 #  define readsocket(s,b,n)       recv((s),(b),(n),0)
118 #  define writesocket(s,b,n)      send((s),(b),(n),0)
119 # elif defined(__DJGPP__)
120 #  define WATT32
121 #  define WATT32_NO_OLDIES
122 #  define get_last_socket_error() errno
123 #  define clear_socket_error()    errno=0
124 #  define closesocket(s)          close_s(s)
125 #  define readsocket(s,b,n)       read_s(s,b,n)
126 #  define writesocket(s,b,n)      send(s,b,n,0)
127 # elif defined(OPENSSL_SYS_VMS)
128 #  define get_last_socket_error() errno
129 #  define clear_socket_error()    errno=0
130 #  define ioctlsocket(a,b,c)      ioctl(a,b,c)
131 #  define closesocket(s)          close(s)
132 #  define readsocket(s,b,n)       recv((s),(b),(n),0)
133 #  define writesocket(s,b,n)      send((s),(b),(n),0)
134 # elif defined(OPENSSL_SYS_VXWORKS)
135 #  define get_last_socket_error() errno
136 #  define clear_socket_error()    errno=0
137 #  define ioctlsocket(a,b,c)          ioctl((a),(b),(int)(c))
138 #  define closesocket(s)              close(s)
139 #  define readsocket(s,b,n)           read((s),(b),(n))
140 #  define writesocket(s,b,n)          write((s),(char *)(b),(n))
141 # else
142 #  define get_last_socket_error() errno
143 #  define clear_socket_error()    errno=0
144 #  define ioctlsocket(a,b,c)      ioctl(a,b,c)
145 #  define closesocket(s)          close(s)
146 #  define readsocket(s,b,n)       read((s),(b),(n))
147 #  define writesocket(s,b,n)      write((s),(b),(n))
148 # endif
149
150 # if (defined(WINDOWS) || defined(MSDOS))
151
152 #  ifdef __DJGPP__
153 #   include <unistd.h>
154 #   include <sys/stat.h>
155 #   include <sys/socket.h>
156 #   include <sys/un.h>
157 #   include <tcp.h>
158 #   include <netdb.h>
159 #   define _setmode setmode
160 #   define _O_TEXT O_TEXT
161 #   define _O_BINARY O_BINARY
162 #   define HAS_LFN_SUPPORT(name)  (pathconf((name), _PC_NAME_MAX) > 12)
163 #   undef DEVRANDOM_EGD  /*  Neither MS-DOS nor FreeDOS provide 'egd' sockets.  */
164 #   undef DEVRANDOM
165 #   define DEVRANDOM "/dev/urandom\x24"
166 #  endif                        /* __DJGPP__ */
167
168 #  ifndef S_IFDIR
169 #   define S_IFDIR     _S_IFDIR
170 #  endif
171
172 #  ifndef S_IFMT
173 #   define S_IFMT      _S_IFMT
174 #  endif
175
176 #  if !defined(WINNT) && !defined(__DJGPP__)
177 #   define NO_SYSLOG
178 #  endif
179 #  define NO_DIRENT
180
181 #  ifdef WINDOWS
182 #   if !defined(_WIN32_WCE) && !defined(_WIN32_WINNT)
183        /*
184         * Defining _WIN32_WINNT here in e_os.h implies certain "discipline."
185         * Most notably we ought to check for availability of each specific
186         * routine that was introduced after denoted _WIN32_WINNT with
187         * GetProcAddress(). Normally newer functions are masked with higher
188         * _WIN32_WINNT in SDK headers. So that if you wish to use them in
189         * some module, you'd need to override _WIN32_WINNT definition in
190         * the target module in order to "reach for" prototypes, but replace
191         * calls to new functions with indirect calls. Alternatively it
192         * might be possible to achieve the goal by /DELAYLOAD-ing .DLLs
193         * and check for current OS version instead.
194         */
195 #    define _WIN32_WINNT 0x0501
196 #   endif
197 #   if defined(_WIN32_WINNT) || defined(_WIN32_WCE)
198        /*
199         * Just like defining _WIN32_WINNT including winsock2.h implies
200         * certain "discipline" for maintaining [broad] binary compatibility.
201         * As long as structures are invariant among Winsock versions,
202         * it's sufficient to check for specific Winsock2 API availability
203         * at run-time [DSO_global_lookup is recommended]...
204         */
205 #    include <winsock2.h>
206 #    include <ws2tcpip.h>
207        /* yes, they have to be #included prior to <windows.h> */
208 #   endif
209 #   include <windows.h>
210 #   include <stdio.h>
211 #   include <stddef.h>
212 #   include <errno.h>
213 #   if defined(_WIN32_WCE) && !defined(EACCES)
214 #    define EACCES   13
215 #   endif
216 #   include <string.h>
217 #   ifdef _WIN64
218 #    define strlen(s) _strlen31(s)
219 /* cut strings to 2GB */
220 static __inline unsigned int _strlen31(const char *str)
221 {
222     unsigned int len = 0;
223     while (*str && len < 0x80000000U)
224         str++, len++;
225     return len & 0x7FFFFFFF;
226 }
227 #   endif
228 #   include <malloc.h>
229 #   if defined(_MSC_VER) && _MSC_VER<=1200 && defined(_MT) && defined(isspace)
230        /* compensate for bug in VC6 ctype.h */
231 #    undef isspace
232 #    undef isdigit
233 #    undef isalnum
234 #    undef isupper
235 #    undef isxdigit
236 #   endif
237 #   if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
238 #    if _MSC_VER>=1300 && _MSC_VER<1600
239 #     undef stdin
240 #     undef stdout
241 #     undef stderr
242 FILE *__iob_func();
243 #     define stdin  (&__iob_func()[0])
244 #     define stdout (&__iob_func()[1])
245 #     define stderr (&__iob_func()[2])
246 #    elif _MSC_VER<1300 && defined(I_CAN_LIVE_WITH_LNK4049)
247 #     undef stdin
248 #     undef stdout
249 #     undef stderr
250          /*
251           * pre-1300 has __p__iob(), but it's available only in msvcrt.lib,
252           * or in other words with /MD. Declaring implicit import, i.e. with
253           * _imp_ prefix, works correctly with all compiler options, but
254           * without /MD results in LINK warning LNK4049: 'locally defined
255           * symbol "__iob" imported'.
256           */
257 extern FILE *_imp___iob;
258 #     define stdin  (&_imp___iob[0])
259 #     define stdout (&_imp___iob[1])
260 #     define stderr (&_imp___iob[2])
261 #    endif
262 #   endif
263 #  endif
264 #  include <io.h>
265 #  include <fcntl.h>
266
267 #  ifdef OPENSSL_SYS_WINCE
268 #   define OPENSSL_NO_POSIX_IO
269 #  endif
270
271 #  define EXIT(n) exit(n)
272 #  define LIST_SEPARATOR_CHAR ';'
273 #  ifndef X_OK
274 #   define X_OK        0
275 #  endif
276 #  ifndef W_OK
277 #   define W_OK        2
278 #  endif
279 #  ifndef R_OK
280 #   define R_OK        4
281 #  endif
282 #  ifdef OPENSSL_SYS_WINCE
283 #   define DEFAULT_HOME  ""
284 #  else
285 #   define DEFAULT_HOME  "C:"
286 #  endif
287
288 /* Avoid Visual Studio 13 GetVersion deprecated problems */
289 #  if defined(_MSC_VER) && _MSC_VER>=1800
290 #   define check_winnt() (1)
291 #   define check_win_minplat(x) (1)
292 #  else
293 #   define check_winnt() (GetVersion() < 0x80000000)
294 #   define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
295 #  endif
296
297 # else                          /* The non-microsoft world */
298
299 #  ifdef OPENSSL_SYS_VMS
300 #   define VMS 1
301   /*
302    * some programs don't include stdlib, so exit() and others give implicit
303    * function warnings
304    */
305 #   include <stdlib.h>
306 #   if defined(__DECC)
307 #    include <unistd.h>
308 #   else
309 #    include <unixlib.h>
310 #   endif
311 #   define LIST_SEPARATOR_CHAR ','
312   /* We don't have any well-defined random devices on VMS, yet... */
313 #   undef DEVRANDOM
314   /*-
315      We need to do this since VMS has the following coding on status codes:
316
317      Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
318                The important thing to know is that odd numbers are considered
319                good, while even ones are considered errors.
320      Bits 3-15: actual status number
321      Bits 16-27: facility number.  0 is considered "unknown"
322      Bits 28-31: control bits.  If bit 28 is set, the shell won't try to
323                  output the message (which, for random codes, just looks ugly)
324
325      So, what we do here is to change 0 to 1 to get the default success status,
326      and everything else is shifted up to fit into the status number field, and
327      the status is tagged as an error, which is what is wanted here.
328
329      Finally, we add the VMS C facility code 0x35a000, because there are some
330      programs, such as Perl, that will reinterpret the code back to something
331      POSIXly.  'man perlvms' explains it further.
332
333      NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
334      codes (status type = 1).  I couldn't disagree more.  Fortunately, the
335      status type doesn't seem to bother Perl.
336      -- Richard Levitte
337   */
338 #   define EXIT(n)  exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
339
340 #   define NO_SYS_PARAM_H
341 #   define NO_SYS_UN_H
342
343 #   define DEFAULT_HOME "SYS$LOGIN:"
344
345 #  else
346      /* !defined VMS */
347 #   ifdef OPENSSL_UNISTD
348 #    include OPENSSL_UNISTD
349 #   else
350 #    include <unistd.h>
351 #   endif
352 #   ifndef NO_SYS_TYPES_H
353 #    include <sys/types.h>
354 #   endif
355 #   ifdef OPENSSL_SYS_WIN32_CYGWIN
356 #    include <io.h>
357 #    include <fcntl.h>
358 #   endif
359
360 #   define LIST_SEPARATOR_CHAR ':'
361 #   define EXIT(n)             exit(n)
362 #  endif
363
364 # endif
365
366 /*************/
367
368 # ifdef USE_SOCKETS
369 #  ifdef OPENSSL_NO_SOCK
370 #  elif defined(WINDOWS) || defined(MSDOS)
371       /* windows world */
372 #   if !defined(__DJGPP__)
373 #    if defined(_WIN32_WCE) && _WIN32_WCE<410
374 #     define getservbyname _masked_declaration_getservbyname
375 #    endif
376 #    if !defined(IPPROTO_IP)
377          /* winsock[2].h was included already? */
378 #     include <winsock.h>
379 #    endif
380 #    ifdef getservbyname
381 #     undef getservbyname
382          /* this is used to be wcecompat/include/winsock_extras.h */
383 struct servent *PASCAL getservbyname(const char *, const char *);
384 #    endif
385
386 #    ifdef _WIN64
387 /*
388  * Even though sizeof(SOCKET) is 8, it's safe to cast it to int, because
389  * the value constitutes an index in per-process table of limited size
390  * and not a real pointer. And we also depend on fact that all processors
391  * Windows run on happen to be two's-complement, which allows to
392  * interchange INVALID_SOCKET and -1.
393  */
394 #     define socket(d,t,p)   ((int)socket(d,t,p))
395 #     define accept(s,f,l)   ((int)accept(s,f,l))
396 #    endif
397 #   else
398 #   endif
399
400 #  else
401
402 #   ifndef NO_SYS_PARAM_H
403 #    include <sys/param.h>
404 #   endif
405 #   ifdef OPENSSL_SYS_VXWORKS
406 #    include <time.h>
407 #   endif
408
409 #   include <netdb.h>
410 #   if defined(OPENSSL_SYS_VMS_NODECC)
411 #    include <socket.h>
412 #    include <in.h>
413 #    include <inet.h>
414 #   else
415 #    include <sys/socket.h>
416 #    ifndef NO_SYS_UN_H
417 #     ifdef OPENSSL_SYS_VXWORKS
418 #      include <streams/un.h>
419 #     else
420 #      include <sys/un.h>
421 #     endif
422 #     ifndef UNIX_PATH_MAX
423 #      define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)NULL)->sun_path)
424 #     endif
425 #    endif
426 #    ifdef FILIO_H
427 #     include <sys/filio.h> /* FIONBIO in some SVR4, e.g. unixware, solaris */
428 #    endif
429 #    include <netinet/in.h>
430 #    include <arpa/inet.h>
431 #    include <netinet/tcp.h>
432 #   endif
433
434 #   ifdef OPENSSL_SYS_AIX
435 #    include <sys/select.h>
436 #   endif
437
438 #   ifdef __QNX__
439 #    include <sys/select.h>
440 #   endif
441
442 #   ifndef VMS
443 #    include <sys/ioctl.h>
444 #   else
445         /* ioctl is only in VMS > 7.0 and when socketshr is not used */
446 #    if !defined(TCPIP_TYPE_SOCKETSHR) && defined(__VMS_VER) && (__VMS_VER > 70000000)
447 #     include <sys/ioctl.h>
448 #    endif
449 #   endif
450
451 #   ifdef VMS
452 #    include <unixio.h>
453 #    if defined(TCPIP_TYPE_SOCKETSHR)
454 #     include <socketshr.h>
455 #    endif
456 #   endif
457
458 #   ifndef INVALID_SOCKET
459 #    define INVALID_SOCKET      (-1)
460 #   endif                       /* INVALID_SOCKET */
461 #  endif
462
463 /*
464  * Some IPv6 implementations are broken, disable them in known bad versions.
465  */
466 #  if !defined(OPENSSL_USE_IPV6)
467 #   if defined(AF_INET6) && !defined(NETWARE_CLIB)
468 #    define OPENSSL_USE_IPV6 1
469 #   else
470 #    define OPENSSL_USE_IPV6 0
471 #   endif
472 #  endif
473
474 # endif
475
476 # ifndef OPENSSL_EXIT
477 #  if defined(MONOLITH) && !defined(OPENSSL_C)
478 #   define OPENSSL_EXIT(n) return(n)
479 #  else
480 #   define OPENSSL_EXIT(n) do { EXIT(n); return(n); } while(0)
481 #  endif
482 # endif
483
484 /***********************************************/
485
486 # if defined(OPENSSL_SYS_WINDOWS)
487 #  define strcasecmp _stricmp
488 #  define strncasecmp _strnicmp
489 #  if (_MSC_VER >= 1310)
490 #   define open _open
491 #   define fdopen _fdopen
492 #   define close _close
493 #   ifndef strdup
494 #    define strdup _strdup
495 #   endif
496 #   define unlink _unlink
497 #  endif
498 # else
499 #  include <strings.h>
500 # endif
501
502 /* vxworks */
503 # if defined(OPENSSL_SYS_VXWORKS)
504 #  include <ioLib.h>
505 #  include <tickLib.h>
506 #  include <sysLib.h>
507
508 #  define TTY_STRUCT int
509
510 #  define sleep(a) taskDelay((a) * sysClkRateGet())
511
512 #  include <vxWorks.h>
513 #  include <sockLib.h>
514 #  include <taskLib.h>
515
516 #  define getpid taskIdSelf
517
518 /*
519  * NOTE: these are implemented by helpers in database app! if the database is
520  * not linked, we need to implement them elswhere
521  */
522 struct hostent *gethostbyname(const char *name);
523 struct hostent *gethostbyaddr(const char *addr, int length, int type);
524 struct servent *getservbyname(const char *name, const char *proto);
525
526 # endif
527 /* end vxworks */
528
529 #define OSSL_NELEM(x)    (sizeof(x)/sizeof(x[0]))
530
531 #ifdef  __cplusplus
532 }
533 #endif
534
535 #endif