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