blob: fa8cf116576e2b620cb85d35099f8bf130997721 [file] [log] [blame]
Lev Walkine2bbbf82017-10-08 18:52:37 -07001/*
Lev Walkin20696a42017-10-17 21:27:33 -07002 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
5/*
6 * Miscellaneous system-dependent types.
7 */
Lev Walkinc6cac8e2016-03-14 02:57:07 -07008#ifndef ASN_SYSTEM_H
9#define ASN_SYSTEM_H
Lev Walkinf15320b2004-06-03 03:38:44 +000010
Lev Walkin0ede18c2004-09-07 10:47:39 +000011#ifdef HAVE_CONFIG_H
12#include "config.h"
13#endif
14
Lev Walkinee8482a2017-10-31 18:26:15 -070015#ifndef _DEFAULT_SOURCE
16#define _DEFAULT_SOURCE 1
17#endif
18
Lev Walkinbce0ce42016-07-02 23:51:32 -070019#ifndef _BSD_SOURCE
20#define _BSD_SOURCE /* for snprintf() on some linux systems */
21#endif
22
Lev Walkina460ba32004-10-20 15:40:04 +000023#include <stdio.h> /* For snprintf(3) */
Lev Walkinf15320b2004-06-03 03:38:44 +000024#include <stdlib.h> /* For *alloc(3) */
25#include <string.h> /* For memcpy(3) */
26#include <sys/types.h> /* For size_t */
Lev Walkin5c879db2007-11-06 06:23:31 +000027#include <limits.h> /* For LONG_MAX */
Lev Walkinf15320b2004-06-03 03:38:44 +000028#include <stdarg.h> /* For va_start */
Lev Walkinf15320b2004-06-03 03:38:44 +000029#include <stddef.h> /* for offsetof and ptrdiff_t */
Lev Walkin0ede18c2004-09-07 10:47:39 +000030
Lev Walkin93659562010-11-20 09:47:13 -080031#ifdef _WIN32
Lev Walkin4efbfb72005-02-25 14:20:30 +000032
Lev Walkinf689f0b2006-05-02 18:55:41 +000033#include <malloc.h>
Lev Walkin4efbfb72005-02-25 14:20:30 +000034#define snprintf _snprintf
35#define vsnprintf _vsnprintf
Lev Walkin4efbfb72005-02-25 14:20:30 +000036
Lev Walkin217f2402006-11-21 10:38:50 +000037/* To avoid linking with ws2_32.lib, here's the definition of ntohl() */
Lev Walkin349e8472006-11-27 13:31:35 +000038#define sys_ntohl(l) ((((l) << 24) & 0xff000000) \
Ilya Basin762747c2012-12-01 14:38:00 +040039 | (((l) << 8) & 0xff0000) \
40 | (((l) >> 8) & 0xff00) \
41 | ((l >> 24) & 0xff))
Lev Walkin217f2402006-11-21 10:38:50 +000042
Lev Walkin4cca5092005-05-27 20:56:08 +000043#ifdef _MSC_VER /* MSVS.Net */
Lev Walkine44ea0b2005-08-17 18:39:36 +000044#ifndef __cplusplus
45#define inline __inline
46#endif
Lev Walkin8e391912007-11-13 23:32:38 +000047#ifndef ASSUMESTDTYPES /* Standard types have been defined elsewhere */
Lev Walkin4cca5092005-05-27 20:56:08 +000048#define ssize_t SSIZE_T
Erika Thorsenafea72f2016-11-14 07:55:19 +010049#if _MSC_VER < 1600
Lev Walkin4cca5092005-05-27 20:56:08 +000050typedef char int8_t;
51typedef short int16_t;
52typedef int int32_t;
53typedef unsigned char uint8_t;
54typedef unsigned short uint16_t;
55typedef unsigned int uint32_t;
Erika Thorsenafea72f2016-11-14 07:55:19 +010056#else /* _MSC_VER >= 1600 */
57#include <stdint.h>
58#endif /* _MSC_VER < 1600 */
Lev Walkin8e391912007-11-13 23:32:38 +000059#endif /* ASSUMESTDTYPES */
Lev Walkin05c81b72010-11-27 17:08:12 -080060#define WIN32_LEAN_AND_MEAN
Lev Walkin4cca5092005-05-27 20:56:08 +000061#include <windows.h>
Lev Walkin2f505022005-07-01 08:28:18 +000062#include <float.h>
63#define isnan _isnan
64#define finite _finite
65#define copysign _copysign
66#define ilogb _logb
Lev Walkin7943c302007-11-13 05:09:35 +000067#else /* !_MSC_VER */
68#include <stdint.h>
Lev Walkin4cca5092005-05-27 20:56:08 +000069#endif /* _MSC_VER */
70
Lev Walkin93659562010-11-20 09:47:13 -080071#else /* !_WIN32 */
Lev Walkin4efbfb72005-02-25 14:20:30 +000072
Lev Walkinf689f0b2006-05-02 18:55:41 +000073#if defined(__vxworks)
74#include <types/vxTypes.h>
75#else /* !defined(__vxworks) */
76
Lev Walkin0ede18c2004-09-07 10:47:39 +000077#include <inttypes.h> /* C99 specifies this file */
Lev Walkin217f2402006-11-21 10:38:50 +000078#include <netinet/in.h> /* for ntohl() */
79#define sys_ntohl(foo) ntohl(foo)
Lev Walkinf689f0b2006-05-02 18:55:41 +000080#endif /* defined(__vxworks) */
81
Lev Walkin93659562010-11-20 09:47:13 -080082#endif /* _WIN32 */
Lev Walkin64399722004-08-11 07:17:22 +000083
Lev Walkin642b92f2017-09-17 22:16:02 -070084#if __GNUC__ >= 3 || defined(__clang__)
Lev Walkin3f995632017-09-26 18:27:32 -070085#define CC_ATTRIBUTE(attr) __attribute__((attr))
Lev Walkin3f6afc12006-09-15 18:52:36 +000086#else
Lev Walkin3f995632017-09-26 18:27:32 -070087#define CC_ATTRIBUTE(attr)
Lev Walkin3f6afc12006-09-15 18:52:36 +000088#endif
Lev Walkina4f68912017-09-27 02:21:38 +000089#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
Lev Walkin642b92f2017-09-17 22:16:02 -070094
Lev Walkin3e86a4e2007-06-29 11:38:57 +000095/* Figure out if thread safety is requested */
Lev Walkin27f53bc2007-06-29 12:10:50 +000096#if !defined(ASN_THREAD_SAFE) && (defined(THREAD_SAFE) || defined(_REENTRANT))
Lev Walkin3e86a4e2007-06-29 11:38:57 +000097#define ASN_THREAD_SAFE
98#endif /* Thread safety */
99
Lev Walkin64399722004-08-11 07:17:22 +0000100#ifndef offsetof /* If not defined by <stddef.h> */
Lev Walkinf15320b2004-06-03 03:38:44 +0000101#define offsetof(s, m) ((ptrdiff_t)&(((s *)0)->m) - (ptrdiff_t)((s *)0))
102#endif /* offsetof */
103
Lev Walkinf15320b2004-06-03 03:38:44 +0000104#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
Lev Walkind84f6032017-10-03 16:33:59 -0700113#if __STDC_VERSION__ >= 199901L
Lev Walkin39837e62017-07-20 14:51:08 +0300114#ifndef SIZE_MAX
Lev Walkind84f6032017-10-03 16:33:59 -0700115#define SIZE_MAX ((~((size_t)0)) >> 1)
Lev Walkin39837e62017-07-20 14:51:08 +0300116#endif
117
118#ifndef RSIZE_MAX /* C11, Annex K */
119#define RSIZE_MAX (SIZE_MAX >> 1)
120#endif
Lev Walkin93682632017-09-15 23:32:36 -0700121#ifndef RSSIZE_MAX /* Halve signed size even further than unsigned */
122#define RSSIZE_MAX ((ssize_t)(RSIZE_MAX >> 1))
123#endif
Lev Walkind84f6032017-10-03 16:33:59 -0700124#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
Lev Walkin39837e62017-07-20 14:51:08 +0300132
Lev Walkin9f470d62017-10-21 16:27:08 -0700133#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
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700150#endif /* ASN_SYSTEM_H */