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