blob: 6dbd148005e3087444a7f6ae5177d40642e407c3 [file] [log] [blame]
Lev Walkine2bbbf82017-10-08 18:52:37 -07001/*
2 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>.
Lev Walkin3e86a4e2007-06-29 11:38:57 +00003 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
6/*
7 * Miscellaneous system-dependent types.
8 */
Lev Walkinc6cac8e2016-03-14 02:57:07 -07009#ifndef ASN_SYSTEM_H
10#define ASN_SYSTEM_H
Lev Walkinf15320b2004-06-03 03:38:44 +000011
Lev Walkin0ede18c2004-09-07 10:47:39 +000012#ifdef HAVE_CONFIG_H
13#include "config.h"
14#endif
15
Lev Walkinbce0ce42016-07-02 23:51:32 -070016#ifndef _BSD_SOURCE
17#define _BSD_SOURCE /* for snprintf() on some linux systems */
18#endif
19
Lev Walkina460ba32004-10-20 15:40:04 +000020#include <stdio.h> /* For snprintf(3) */
Lev Walkinf15320b2004-06-03 03:38:44 +000021#include <stdlib.h> /* For *alloc(3) */
22#include <string.h> /* For memcpy(3) */
23#include <sys/types.h> /* For size_t */
Lev Walkin5c879db2007-11-06 06:23:31 +000024#include <limits.h> /* For LONG_MAX */
Lev Walkinf15320b2004-06-03 03:38:44 +000025#include <stdarg.h> /* For va_start */
Lev Walkinf15320b2004-06-03 03:38:44 +000026#include <stddef.h> /* for offsetof and ptrdiff_t */
Lev Walkin0ede18c2004-09-07 10:47:39 +000027
Lev Walkin93659562010-11-20 09:47:13 -080028#ifdef _WIN32
Lev Walkin4efbfb72005-02-25 14:20:30 +000029
Lev Walkinf689f0b2006-05-02 18:55:41 +000030#include <malloc.h>
Lev Walkin4efbfb72005-02-25 14:20:30 +000031#define snprintf _snprintf
32#define vsnprintf _vsnprintf
Lev Walkin4efbfb72005-02-25 14:20:30 +000033
Lev Walkin217f2402006-11-21 10:38:50 +000034/* To avoid linking with ws2_32.lib, here's the definition of ntohl() */
Lev Walkin349e8472006-11-27 13:31:35 +000035#define sys_ntohl(l) ((((l) << 24) & 0xff000000) \
Ilya Basin762747c2012-12-01 14:38:00 +040036 | (((l) << 8) & 0xff0000) \
37 | (((l) >> 8) & 0xff00) \
38 | ((l >> 24) & 0xff))
Lev Walkin217f2402006-11-21 10:38:50 +000039
Lev Walkin4cca5092005-05-27 20:56:08 +000040#ifdef _MSC_VER /* MSVS.Net */
Lev Walkine44ea0b2005-08-17 18:39:36 +000041#ifndef __cplusplus
42#define inline __inline
43#endif
Lev Walkin8e391912007-11-13 23:32:38 +000044#ifndef ASSUMESTDTYPES /* Standard types have been defined elsewhere */
Lev Walkin4cca5092005-05-27 20:56:08 +000045#define ssize_t SSIZE_T
Erika Thorsenafea72f2016-11-14 07:55:19 +010046#if _MSC_VER < 1600
Lev Walkin4cca5092005-05-27 20:56:08 +000047typedef char int8_t;
48typedef short int16_t;
49typedef int int32_t;
50typedef unsigned char uint8_t;
51typedef unsigned short uint16_t;
52typedef unsigned int uint32_t;
Erika Thorsenafea72f2016-11-14 07:55:19 +010053#else /* _MSC_VER >= 1600 */
54#include <stdint.h>
55#endif /* _MSC_VER < 1600 */
Lev Walkin8e391912007-11-13 23:32:38 +000056#endif /* ASSUMESTDTYPES */
Lev Walkin05c81b72010-11-27 17:08:12 -080057#define WIN32_LEAN_AND_MEAN
Lev Walkin4cca5092005-05-27 20:56:08 +000058#include <windows.h>
Lev Walkin2f505022005-07-01 08:28:18 +000059#include <float.h>
60#define isnan _isnan
61#define finite _finite
62#define copysign _copysign
63#define ilogb _logb
Lev Walkin7943c302007-11-13 05:09:35 +000064#else /* !_MSC_VER */
65#include <stdint.h>
Lev Walkin4cca5092005-05-27 20:56:08 +000066#endif /* _MSC_VER */
67
Lev Walkin93659562010-11-20 09:47:13 -080068#else /* !_WIN32 */
Lev Walkin4efbfb72005-02-25 14:20:30 +000069
Lev Walkinf689f0b2006-05-02 18:55:41 +000070#if defined(__vxworks)
71#include <types/vxTypes.h>
72#else /* !defined(__vxworks) */
73
Lev Walkin0ede18c2004-09-07 10:47:39 +000074#include <inttypes.h> /* C99 specifies this file */
Lev Walkin217f2402006-11-21 10:38:50 +000075#include <netinet/in.h> /* for ntohl() */
76#define sys_ntohl(foo) ntohl(foo)
Lev Walkinf689f0b2006-05-02 18:55:41 +000077#endif /* defined(__vxworks) */
78
Lev Walkin93659562010-11-20 09:47:13 -080079#endif /* _WIN32 */
Lev Walkin64399722004-08-11 07:17:22 +000080
Lev Walkin642b92f2017-09-17 22:16:02 -070081#if __GNUC__ >= 3 || defined(__clang__)
Lev Walkin3f995632017-09-26 18:27:32 -070082#define CC_ATTRIBUTE(attr) __attribute__((attr))
Lev Walkin3f6afc12006-09-15 18:52:36 +000083#else
Lev Walkin3f995632017-09-26 18:27:32 -070084#define CC_ATTRIBUTE(attr)
Lev Walkin3f6afc12006-09-15 18:52:36 +000085#endif
Lev Walkina4f68912017-09-27 02:21:38 +000086#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 Walkin642b92f2017-09-17 22:16:02 -070091
Lev Walkin3e86a4e2007-06-29 11:38:57 +000092/* Figure out if thread safety is requested */
Lev Walkin27f53bc2007-06-29 12:10:50 +000093#if !defined(ASN_THREAD_SAFE) && (defined(THREAD_SAFE) || defined(_REENTRANT))
Lev Walkin3e86a4e2007-06-29 11:38:57 +000094#define ASN_THREAD_SAFE
95#endif /* Thread safety */
96
Lev Walkin64399722004-08-11 07:17:22 +000097#ifndef offsetof /* If not defined by <stddef.h> */
Lev Walkinf15320b2004-06-03 03:38:44 +000098#define offsetof(s, m) ((ptrdiff_t)&(((s *)0)->m) - (ptrdiff_t)((s *)0))
99#endif /* offsetof */
100
Lev Walkinf15320b2004-06-03 03:38:44 +0000101#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 Walkind84f6032017-10-03 16:33:59 -0700110#if __STDC_VERSION__ >= 199901L
Lev Walkin39837e62017-07-20 14:51:08 +0300111#ifndef SIZE_MAX
Lev Walkind84f6032017-10-03 16:33:59 -0700112#define SIZE_MAX ((~((size_t)0)) >> 1)
Lev Walkin39837e62017-07-20 14:51:08 +0300113#endif
114
115#ifndef RSIZE_MAX /* C11, Annex K */
116#define RSIZE_MAX (SIZE_MAX >> 1)
117#endif
Lev Walkin93682632017-09-15 23:32:36 -0700118#ifndef RSSIZE_MAX /* Halve signed size even further than unsigned */
119#define RSSIZE_MAX ((ssize_t)(RSIZE_MAX >> 1))
120#endif
Lev Walkind84f6032017-10-03 16:33:59 -0700121#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 Walkin39837e62017-07-20 14:51:08 +0300129
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700130#endif /* ASN_SYSTEM_H */