Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | /* |
| 6 | * Miscellaneous system-dependent types. |
| 7 | */ |
| 8 | #ifndef ASN_SYSTEM_H |
| 9 | #define ASN_SYSTEM_H |
| 10 | |
| 11 | #ifdef HAVE_CONFIG_H |
| 12 | #include "config.h" |
| 13 | #endif |
| 14 | |
| 15 | #ifndef _DEFAULT_SOURCE |
| 16 | #define _DEFAULT_SOURCE 1 |
| 17 | #endif |
| 18 | |
| 19 | #ifndef _BSD_SOURCE |
| 20 | #define _BSD_SOURCE /* for snprintf() on some linux systems */ |
| 21 | #endif |
| 22 | |
| 23 | #include <stdio.h> /* For snprintf(3) */ |
| 24 | #include <stdlib.h> /* For *alloc(3) */ |
| 25 | #include <string.h> /* For memcpy(3) */ |
| 26 | #include <sys/types.h> /* For size_t */ |
| 27 | #include <limits.h> /* For LONG_MAX */ |
| 28 | #include <stdarg.h> /* For va_start */ |
| 29 | #include <stddef.h> /* for offsetof and ptrdiff_t */ |
| 30 | |
| 31 | #ifdef _WIN32 |
| 32 | |
| 33 | #include <malloc.h> |
| 34 | #define snprintf _snprintf |
| 35 | #define vsnprintf _vsnprintf |
| 36 | |
| 37 | /* To avoid linking with ws2_32.lib, here's the definition of ntohl() */ |
| 38 | #define sys_ntohl(l) ((((l) << 24) & 0xff000000) \ |
| 39 | | (((l) << 8) & 0xff0000) \ |
| 40 | | (((l) >> 8) & 0xff00) \ |
| 41 | | ((l >> 24) & 0xff)) |
| 42 | |
| 43 | #ifdef _MSC_VER /* MSVS.Net */ |
| 44 | #ifndef __cplusplus |
| 45 | #define inline __inline |
| 46 | #endif |
| 47 | #ifndef ASSUMESTDTYPES /* Standard types have been defined elsewhere */ |
| 48 | #define ssize_t SSIZE_T |
| 49 | #if _MSC_VER < 1600 |
| 50 | typedef char int8_t; |
| 51 | typedef short int16_t; |
| 52 | typedef int int32_t; |
| 53 | typedef unsigned char uint8_t; |
| 54 | typedef unsigned short uint16_t; |
| 55 | typedef unsigned int uint32_t; |
| 56 | #else /* _MSC_VER >= 1600 */ |
| 57 | #include <stdint.h> |
| 58 | #endif /* _MSC_VER < 1600 */ |
| 59 | #endif /* ASSUMESTDTYPES */ |
| 60 | #define WIN32_LEAN_AND_MEAN |
| 61 | #include <windows.h> |
| 62 | #include <float.h> |
| 63 | #define isnan _isnan |
| 64 | #define finite _finite |
| 65 | #define copysign _copysign |
| 66 | #define ilogb _logb |
| 67 | #else /* !_MSC_VER */ |
| 68 | #include <stdint.h> |
| 69 | #endif /* _MSC_VER */ |
| 70 | |
| 71 | #else /* !_WIN32 */ |
| 72 | |
| 73 | #if defined(__vxworks) |
| 74 | #include <types/vxTypes.h> |
| 75 | #else /* !defined(__vxworks) */ |
| 76 | |
| 77 | #include <inttypes.h> /* C99 specifies this file */ |
| 78 | #include <netinet/in.h> /* for ntohl() */ |
| 79 | #define sys_ntohl(foo) ntohl(foo) |
| 80 | #endif /* defined(__vxworks) */ |
| 81 | |
| 82 | #endif /* _WIN32 */ |
| 83 | |
| 84 | #if __GNUC__ >= 3 || defined(__clang__) |
| 85 | #define CC_ATTRIBUTE(attr) __attribute__((attr)) |
| 86 | #else |
| 87 | #define CC_ATTRIBUTE(attr) |
| 88 | #endif |
| 89 | #define CC_PRINTFLIKE(fmt, var) CC_ATTRIBUTE(format(printf, fmt, var)) |
| 90 | #define CC_NOTUSED CC_ATTRIBUTE(unused) |
| 91 | #ifndef CC_ATTR_NO_SANITIZE |
| 92 | #define CC_ATTR_NO_SANITIZE(what) CC_ATTRIBUTE(no_sanitize(what)) |
| 93 | #endif |
| 94 | |
| 95 | /* Figure out if thread safety is requested */ |
| 96 | #if !defined(ASN_THREAD_SAFE) && (defined(THREAD_SAFE) || defined(_REENTRANT)) |
| 97 | #define ASN_THREAD_SAFE |
| 98 | #endif /* Thread safety */ |
| 99 | |
| 100 | #ifndef offsetof /* If not defined by <stddef.h> */ |
| 101 | #define offsetof(s, m) ((ptrdiff_t)&(((s *)0)->m) - (ptrdiff_t)((s *)0)) |
| 102 | #endif /* offsetof */ |
| 103 | |
| 104 | #ifndef MIN /* Suitable for comparing primitive types (integers) */ |
| 105 | #if defined(__GNUC__) |
| 106 | #define MIN(a,b) ({ __typeof a _a = a; __typeof b _b = b; \ |
| 107 | ((_a)<(_b)?(_a):(_b)); }) |
| 108 | #else /* !__GNUC__ */ |
| 109 | #define MIN(a,b) ((a)<(b)?(a):(b)) /* Unsafe variant */ |
| 110 | #endif /* __GNUC__ */ |
| 111 | #endif /* MIN */ |
| 112 | |
| 113 | #if __STDC_VERSION__ >= 199901L |
| 114 | #ifndef SIZE_MAX |
| 115 | #define SIZE_MAX ((~((size_t)0)) >> 1) |
| 116 | #endif |
| 117 | |
| 118 | #ifndef RSIZE_MAX /* C11, Annex K */ |
| 119 | #define RSIZE_MAX (SIZE_MAX >> 1) |
| 120 | #endif |
| 121 | #ifndef RSSIZE_MAX /* Halve signed size even further than unsigned */ |
| 122 | #define RSSIZE_MAX ((ssize_t)(RSIZE_MAX >> 1)) |
| 123 | #endif |
| 124 | #else /* Old compiler */ |
| 125 | #undef SIZE_MAX |
| 126 | #undef RSIZE_MAX |
| 127 | #undef RSSIZE_MAX |
| 128 | #define SIZE_MAX ((~((size_t)0)) >> 1) |
| 129 | #define RSIZE_MAX (SIZE_MAX >> 1) |
| 130 | #define RSSIZE_MAX ((ssize_t)(RSIZE_MAX >> 1)) |
| 131 | #endif |
| 132 | |
| 133 | #if __STDC_VERSION__ >= 199901L |
| 134 | #define ASN_PRI_SIZE "zu" |
| 135 | #define ASN_PRI_SSIZE "zd" |
| 136 | #define ASN_PRIuMAX PRIuMAX |
| 137 | #define ASN_PRIdMAX PRIdMAX |
| 138 | #else |
| 139 | #define ASN_PRI_SIZE "lu" |
| 140 | #define ASN_PRI_SSIZE "ld" |
| 141 | #if LLONG_MAX > LONG_MAX |
| 142 | #define ASN_PRIuMAX "llu" |
| 143 | #define ASN_PRIdMAX "lld" |
| 144 | #else |
| 145 | #define ASN_PRIuMAX "lu" |
| 146 | #define ASN_PRIdMAX "ld" |
| 147 | #endif |
| 148 | #endif |
| 149 | |
| 150 | #endif /* ASN_SYSTEM_H */ |