Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | 3e86a4e | 2007-06-29 11:38:57 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004, 2007 Lev Walkin <vlm@lionet.info>. |
| 3 | * All rights reserved. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 4 | * Redistribution and modifications are permitted subject to BSD license. |
| 5 | */ |
| 6 | /* |
| 7 | * Miscellaneous system-dependent types. |
| 8 | */ |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 9 | #ifndef ASN_SYSTEM_H |
| 10 | #define ASN_SYSTEM_H |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 11 | |
Lev Walkin | 0ede18c | 2004-09-07 10:47:39 +0000 | [diff] [blame] | 12 | #ifdef HAVE_CONFIG_H |
| 13 | #include "config.h" |
| 14 | #endif |
| 15 | |
Lev Walkin | bce0ce4 | 2016-07-02 23:51:32 -0700 | [diff] [blame] | 16 | #ifndef _BSD_SOURCE |
| 17 | #define _BSD_SOURCE /* for snprintf() on some linux systems */ |
| 18 | #endif |
| 19 | |
Lev Walkin | a460ba3 | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 20 | #include <stdio.h> /* For snprintf(3) */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 21 | #include <stdlib.h> /* For *alloc(3) */ |
| 22 | #include <string.h> /* For memcpy(3) */ |
| 23 | #include <sys/types.h> /* For size_t */ |
Lev Walkin | 5c879db | 2007-11-06 06:23:31 +0000 | [diff] [blame] | 24 | #include <limits.h> /* For LONG_MAX */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 25 | #include <stdarg.h> /* For va_start */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 26 | #include <stddef.h> /* for offsetof and ptrdiff_t */ |
Lev Walkin | 0ede18c | 2004-09-07 10:47:39 +0000 | [diff] [blame] | 27 | |
Lev Walkin | 619f0cd | 2016-07-02 19:55:23 +0000 | [diff] [blame] | 28 | #ifdef HAVE_ALLOCA_H |
| 29 | #include <alloca.h> /* For alloca(3) */ |
| 30 | #endif |
| 31 | |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 32 | #ifdef _WIN32 |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 33 | |
Lev Walkin | f689f0b | 2006-05-02 18:55:41 +0000 | [diff] [blame] | 34 | #include <malloc.h> |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 35 | #define snprintf _snprintf |
| 36 | #define vsnprintf _vsnprintf |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 37 | |
Lev Walkin | 217f240 | 2006-11-21 10:38:50 +0000 | [diff] [blame] | 38 | /* To avoid linking with ws2_32.lib, here's the definition of ntohl() */ |
Lev Walkin | 349e847 | 2006-11-27 13:31:35 +0000 | [diff] [blame] | 39 | #define sys_ntohl(l) ((((l) << 24) & 0xff000000) \ |
Ilya Basin | 762747c | 2012-12-01 14:38:00 +0400 | [diff] [blame] | 40 | | (((l) << 8) & 0xff0000) \ |
| 41 | | (((l) >> 8) & 0xff00) \ |
| 42 | | ((l >> 24) & 0xff)) |
Lev Walkin | 217f240 | 2006-11-21 10:38:50 +0000 | [diff] [blame] | 43 | |
Lev Walkin | 4cca509 | 2005-05-27 20:56:08 +0000 | [diff] [blame] | 44 | #ifdef _MSC_VER /* MSVS.Net */ |
Lev Walkin | e44ea0b | 2005-08-17 18:39:36 +0000 | [diff] [blame] | 45 | #ifndef __cplusplus |
| 46 | #define inline __inline |
| 47 | #endif |
Lev Walkin | 8e39191 | 2007-11-13 23:32:38 +0000 | [diff] [blame] | 48 | #ifndef ASSUMESTDTYPES /* Standard types have been defined elsewhere */ |
Lev Walkin | 4cca509 | 2005-05-27 20:56:08 +0000 | [diff] [blame] | 49 | #define ssize_t SSIZE_T |
Erika Thorsen | afea72f | 2016-11-14 07:55:19 +0100 | [diff] [blame] | 50 | #if _MSC_VER < 1600 |
Lev Walkin | 4cca509 | 2005-05-27 20:56:08 +0000 | [diff] [blame] | 51 | typedef char int8_t; |
| 52 | typedef short int16_t; |
| 53 | typedef int int32_t; |
| 54 | typedef unsigned char uint8_t; |
| 55 | typedef unsigned short uint16_t; |
| 56 | typedef unsigned int uint32_t; |
Erika Thorsen | afea72f | 2016-11-14 07:55:19 +0100 | [diff] [blame] | 57 | #else /* _MSC_VER >= 1600 */ |
| 58 | #include <stdint.h> |
| 59 | #endif /* _MSC_VER < 1600 */ |
Lev Walkin | 8e39191 | 2007-11-13 23:32:38 +0000 | [diff] [blame] | 60 | #endif /* ASSUMESTDTYPES */ |
Lev Walkin | 05c81b7 | 2010-11-27 17:08:12 -0800 | [diff] [blame] | 61 | #define WIN32_LEAN_AND_MEAN |
Lev Walkin | 4cca509 | 2005-05-27 20:56:08 +0000 | [diff] [blame] | 62 | #include <windows.h> |
Lev Walkin | 2f50502 | 2005-07-01 08:28:18 +0000 | [diff] [blame] | 63 | #include <float.h> |
| 64 | #define isnan _isnan |
| 65 | #define finite _finite |
| 66 | #define copysign _copysign |
| 67 | #define ilogb _logb |
Lev Walkin | 7943c30 | 2007-11-13 05:09:35 +0000 | [diff] [blame] | 68 | #else /* !_MSC_VER */ |
| 69 | #include <stdint.h> |
Lev Walkin | 4cca509 | 2005-05-27 20:56:08 +0000 | [diff] [blame] | 70 | #endif /* _MSC_VER */ |
| 71 | |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 72 | #else /* !_WIN32 */ |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 73 | |
Lev Walkin | f689f0b | 2006-05-02 18:55:41 +0000 | [diff] [blame] | 74 | #if defined(__vxworks) |
| 75 | #include <types/vxTypes.h> |
| 76 | #else /* !defined(__vxworks) */ |
| 77 | |
Lev Walkin | 0ede18c | 2004-09-07 10:47:39 +0000 | [diff] [blame] | 78 | #include <inttypes.h> /* C99 specifies this file */ |
Lev Walkin | 0ede18c | 2004-09-07 10:47:39 +0000 | [diff] [blame] | 79 | /* |
Lev Walkin | a460ba3 | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 80 | * 1. Earlier FreeBSD version didn't have <stdint.h>, |
Lev Walkin | 0ede18c | 2004-09-07 10:47:39 +0000 | [diff] [blame] | 81 | * but <inttypes.h> was present. |
Lev Walkin | a460ba3 | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 82 | * 2. Sun Solaris requires <alloca.h> for alloca(3), |
| 83 | * but does not have <stdint.h>. |
Lev Walkin | 0ede18c | 2004-09-07 10:47:39 +0000 | [diff] [blame] | 84 | */ |
Lev Walkin | eeab25b | 2004-10-25 22:58:35 +0000 | [diff] [blame] | 85 | #if (!defined(__FreeBSD__) || !defined(_SYS_INTTYPES_H_)) |
Lev Walkin | a460ba3 | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 86 | #if defined(sun) |
| 87 | #include <alloca.h> /* For alloca(3) */ |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 88 | #include <ieeefp.h> /* for finite(3) */ |
Lev Walkin | fa95665 | 2006-03-16 22:29:55 +0000 | [diff] [blame] | 89 | #elif defined(__hpux) |
| 90 | #ifdef __GNUC__ |
| 91 | #include <alloca.h> /* For alloca(3) */ |
| 92 | #else /* !__GNUC__ */ |
| 93 | #define inline |
| 94 | #endif /* __GNUC__ */ |
Lev Walkin | a460ba3 | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 95 | #else |
Lev Walkin | 1ff369a | 2004-09-07 07:26:32 +0000 | [diff] [blame] | 96 | #include <stdint.h> /* SUSv2+ and C99 specify this file, for uintXX_t */ |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 97 | #endif /* defined(sun) */ |
Lev Walkin | a460ba3 | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 98 | #endif |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 99 | |
Lev Walkin | 217f240 | 2006-11-21 10:38:50 +0000 | [diff] [blame] | 100 | #include <netinet/in.h> /* for ntohl() */ |
| 101 | #define sys_ntohl(foo) ntohl(foo) |
| 102 | |
Lev Walkin | f689f0b | 2006-05-02 18:55:41 +0000 | [diff] [blame] | 103 | #endif /* defined(__vxworks) */ |
| 104 | |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 105 | #endif /* _WIN32 */ |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 106 | |
Lev Walkin | 3f6afc1 | 2006-09-15 18:52:36 +0000 | [diff] [blame] | 107 | #if __GNUC__ >= 3 |
| 108 | #ifndef GCC_PRINTFLIKE |
| 109 | #define GCC_PRINTFLIKE(fmt,var) __attribute__((format(printf,fmt,var))) |
| 110 | #endif |
Lev Walkin | 50b0f6c | 2006-10-16 12:01:36 +0000 | [diff] [blame] | 111 | #ifndef GCC_NOTUSED |
| 112 | #define GCC_NOTUSED __attribute__((unused)) |
| 113 | #endif |
Lev Walkin | 3f6afc1 | 2006-09-15 18:52:36 +0000 | [diff] [blame] | 114 | #else |
| 115 | #ifndef GCC_PRINTFLIKE |
| 116 | #define GCC_PRINTFLIKE(fmt,var) /* nothing */ |
| 117 | #endif |
Lev Walkin | 50b0f6c | 2006-10-16 12:01:36 +0000 | [diff] [blame] | 118 | #ifndef GCC_NOTUSED |
| 119 | #define GCC_NOTUSED |
| 120 | #endif |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 121 | #endif |
| 122 | |
Lev Walkin | 3e86a4e | 2007-06-29 11:38:57 +0000 | [diff] [blame] | 123 | /* Figure out if thread safety is requested */ |
Lev Walkin | 27f53bc | 2007-06-29 12:10:50 +0000 | [diff] [blame] | 124 | #if !defined(ASN_THREAD_SAFE) && (defined(THREAD_SAFE) || defined(_REENTRANT)) |
Lev Walkin | 3e86a4e | 2007-06-29 11:38:57 +0000 | [diff] [blame] | 125 | #define ASN_THREAD_SAFE |
| 126 | #endif /* Thread safety */ |
| 127 | |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 128 | #ifndef offsetof /* If not defined by <stddef.h> */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 129 | #define offsetof(s, m) ((ptrdiff_t)&(((s *)0)->m) - (ptrdiff_t)((s *)0)) |
| 130 | #endif /* offsetof */ |
| 131 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 132 | #ifndef MIN /* Suitable for comparing primitive types (integers) */ |
| 133 | #if defined(__GNUC__) |
| 134 | #define MIN(a,b) ({ __typeof a _a = a; __typeof b _b = b; \ |
| 135 | ((_a)<(_b)?(_a):(_b)); }) |
| 136 | #else /* !__GNUC__ */ |
| 137 | #define MIN(a,b) ((a)<(b)?(a):(b)) /* Unsafe variant */ |
| 138 | #endif /* __GNUC__ */ |
| 139 | #endif /* MIN */ |
| 140 | |
Lev Walkin | 39837e6 | 2017-07-20 14:51:08 +0300 | [diff] [blame] | 141 | #ifndef SIZE_MAX |
| 142 | #define SIZE_MAX ULONG_MAX |
| 143 | #endif |
| 144 | |
| 145 | #ifndef RSIZE_MAX /* C11, Annex K */ |
| 146 | #define RSIZE_MAX (SIZE_MAX >> 1) |
| 147 | #endif |
| 148 | |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 149 | #endif /* ASN_SYSTEM_H */ |