Lev Walkin | e2bbbf8 | 2017-10-08 18:52:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. |
Lev Walkin | 3e86a4e | 2007-06-29 11:38:57 +0000 | [diff] [blame] | 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 | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 28 | #ifdef _WIN32 |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 29 | |
Lev Walkin | f689f0b | 2006-05-02 18:55:41 +0000 | [diff] [blame] | 30 | #include <malloc.h> |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 31 | #define snprintf _snprintf |
| 32 | #define vsnprintf _vsnprintf |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 33 | |
Lev Walkin | 217f240 | 2006-11-21 10:38:50 +0000 | [diff] [blame] | 34 | /* 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] | 35 | #define sys_ntohl(l) ((((l) << 24) & 0xff000000) \ |
Ilya Basin | 762747c | 2012-12-01 14:38:00 +0400 | [diff] [blame] | 36 | | (((l) << 8) & 0xff0000) \ |
| 37 | | (((l) >> 8) & 0xff00) \ |
| 38 | | ((l >> 24) & 0xff)) |
Lev Walkin | 217f240 | 2006-11-21 10:38:50 +0000 | [diff] [blame] | 39 | |
Lev Walkin | 4cca509 | 2005-05-27 20:56:08 +0000 | [diff] [blame] | 40 | #ifdef _MSC_VER /* MSVS.Net */ |
Lev Walkin | e44ea0b | 2005-08-17 18:39:36 +0000 | [diff] [blame] | 41 | #ifndef __cplusplus |
| 42 | #define inline __inline |
| 43 | #endif |
Lev Walkin | 8e39191 | 2007-11-13 23:32:38 +0000 | [diff] [blame] | 44 | #ifndef ASSUMESTDTYPES /* Standard types have been defined elsewhere */ |
Lev Walkin | 4cca509 | 2005-05-27 20:56:08 +0000 | [diff] [blame] | 45 | #define ssize_t SSIZE_T |
Erika Thorsen | afea72f | 2016-11-14 07:55:19 +0100 | [diff] [blame] | 46 | #if _MSC_VER < 1600 |
Lev Walkin | 4cca509 | 2005-05-27 20:56:08 +0000 | [diff] [blame] | 47 | typedef char int8_t; |
| 48 | typedef short int16_t; |
| 49 | typedef int int32_t; |
| 50 | typedef unsigned char uint8_t; |
| 51 | typedef unsigned short uint16_t; |
| 52 | typedef unsigned int uint32_t; |
Erika Thorsen | afea72f | 2016-11-14 07:55:19 +0100 | [diff] [blame] | 53 | #else /* _MSC_VER >= 1600 */ |
| 54 | #include <stdint.h> |
| 55 | #endif /* _MSC_VER < 1600 */ |
Lev Walkin | 8e39191 | 2007-11-13 23:32:38 +0000 | [diff] [blame] | 56 | #endif /* ASSUMESTDTYPES */ |
Lev Walkin | 05c81b7 | 2010-11-27 17:08:12 -0800 | [diff] [blame] | 57 | #define WIN32_LEAN_AND_MEAN |
Lev Walkin | 4cca509 | 2005-05-27 20:56:08 +0000 | [diff] [blame] | 58 | #include <windows.h> |
Lev Walkin | 2f50502 | 2005-07-01 08:28:18 +0000 | [diff] [blame] | 59 | #include <float.h> |
| 60 | #define isnan _isnan |
| 61 | #define finite _finite |
| 62 | #define copysign _copysign |
| 63 | #define ilogb _logb |
Lev Walkin | 7943c30 | 2007-11-13 05:09:35 +0000 | [diff] [blame] | 64 | #else /* !_MSC_VER */ |
| 65 | #include <stdint.h> |
Lev Walkin | 4cca509 | 2005-05-27 20:56:08 +0000 | [diff] [blame] | 66 | #endif /* _MSC_VER */ |
| 67 | |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 68 | #else /* !_WIN32 */ |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 69 | |
Lev Walkin | f689f0b | 2006-05-02 18:55:41 +0000 | [diff] [blame] | 70 | #if defined(__vxworks) |
| 71 | #include <types/vxTypes.h> |
| 72 | #else /* !defined(__vxworks) */ |
| 73 | |
Lev Walkin | 0ede18c | 2004-09-07 10:47:39 +0000 | [diff] [blame] | 74 | #include <inttypes.h> /* C99 specifies this file */ |
Lev Walkin | 217f240 | 2006-11-21 10:38:50 +0000 | [diff] [blame] | 75 | #include <netinet/in.h> /* for ntohl() */ |
| 76 | #define sys_ntohl(foo) ntohl(foo) |
Lev Walkin | f689f0b | 2006-05-02 18:55:41 +0000 | [diff] [blame] | 77 | #endif /* defined(__vxworks) */ |
| 78 | |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 79 | #endif /* _WIN32 */ |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 80 | |
Lev Walkin | 642b92f | 2017-09-17 22:16:02 -0700 | [diff] [blame] | 81 | #if __GNUC__ >= 3 || defined(__clang__) |
Lev Walkin | 3f99563 | 2017-09-26 18:27:32 -0700 | [diff] [blame] | 82 | #define CC_ATTRIBUTE(attr) __attribute__((attr)) |
Lev Walkin | 3f6afc1 | 2006-09-15 18:52:36 +0000 | [diff] [blame] | 83 | #else |
Lev Walkin | 3f99563 | 2017-09-26 18:27:32 -0700 | [diff] [blame] | 84 | #define CC_ATTRIBUTE(attr) |
Lev Walkin | 3f6afc1 | 2006-09-15 18:52:36 +0000 | [diff] [blame] | 85 | #endif |
Lev Walkin | a4f6891 | 2017-09-27 02:21:38 +0000 | [diff] [blame] | 86 | #define CC_PRINTFLIKE(fmt, var) CC_ATTRIBUTE(format(printf, fmt, var)) |
| 87 | #define CC_NOTUSED CC_ATTRIBUTE(unused) |
| 88 | #ifndef CC_ATTR_NO_SANITIZE |
| 89 | #define CC_ATTR_NO_SANITIZE(what) CC_ATTRIBUTE(no_sanitize(what)) |
| 90 | #endif |
Lev Walkin | 642b92f | 2017-09-17 22:16:02 -0700 | [diff] [blame] | 91 | |
Lev Walkin | 3e86a4e | 2007-06-29 11:38:57 +0000 | [diff] [blame] | 92 | /* Figure out if thread safety is requested */ |
Lev Walkin | 27f53bc | 2007-06-29 12:10:50 +0000 | [diff] [blame] | 93 | #if !defined(ASN_THREAD_SAFE) && (defined(THREAD_SAFE) || defined(_REENTRANT)) |
Lev Walkin | 3e86a4e | 2007-06-29 11:38:57 +0000 | [diff] [blame] | 94 | #define ASN_THREAD_SAFE |
| 95 | #endif /* Thread safety */ |
| 96 | |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 97 | #ifndef offsetof /* If not defined by <stddef.h> */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 98 | #define offsetof(s, m) ((ptrdiff_t)&(((s *)0)->m) - (ptrdiff_t)((s *)0)) |
| 99 | #endif /* offsetof */ |
| 100 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 101 | #ifndef MIN /* Suitable for comparing primitive types (integers) */ |
| 102 | #if defined(__GNUC__) |
| 103 | #define MIN(a,b) ({ __typeof a _a = a; __typeof b _b = b; \ |
| 104 | ((_a)<(_b)?(_a):(_b)); }) |
| 105 | #else /* !__GNUC__ */ |
| 106 | #define MIN(a,b) ((a)<(b)?(a):(b)) /* Unsafe variant */ |
| 107 | #endif /* __GNUC__ */ |
| 108 | #endif /* MIN */ |
| 109 | |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 110 | #if __STDC_VERSION__ >= 199901L |
Lev Walkin | 39837e6 | 2017-07-20 14:51:08 +0300 | [diff] [blame] | 111 | #ifndef SIZE_MAX |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 112 | #define SIZE_MAX ((~((size_t)0)) >> 1) |
Lev Walkin | 39837e6 | 2017-07-20 14:51:08 +0300 | [diff] [blame] | 113 | #endif |
| 114 | |
| 115 | #ifndef RSIZE_MAX /* C11, Annex K */ |
| 116 | #define RSIZE_MAX (SIZE_MAX >> 1) |
| 117 | #endif |
Lev Walkin | 9368263 | 2017-09-15 23:32:36 -0700 | [diff] [blame] | 118 | #ifndef RSSIZE_MAX /* Halve signed size even further than unsigned */ |
| 119 | #define RSSIZE_MAX ((ssize_t)(RSIZE_MAX >> 1)) |
| 120 | #endif |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 121 | #else /* Old compiler */ |
| 122 | #undef SIZE_MAX |
| 123 | #undef RSIZE_MAX |
| 124 | #undef RSSIZE_MAX |
| 125 | #define SIZE_MAX ((~((size_t)0)) >> 1) |
| 126 | #define RSIZE_MAX (SIZE_MAX >> 1) |
| 127 | #define RSSIZE_MAX ((ssize_t)(RSIZE_MAX >> 1)) |
| 128 | #endif |
Lev Walkin | 39837e6 | 2017-07-20 14:51:08 +0300 | [diff] [blame] | 129 | |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 130 | #endif /* ASN_SYSTEM_H */ |