blob: eacacc40a90178d740149667ddd1d0e3e5069cb0 [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 Walkinbce0ce42016-07-02 23:51:32 -070015#ifndef _BSD_SOURCE
16#define _BSD_SOURCE /* for snprintf() on some linux systems */
17#endif
18
Lev Walkina460ba32004-10-20 15:40:04 +000019#include <stdio.h> /* For snprintf(3) */
Lev Walkinf15320b2004-06-03 03:38:44 +000020#include <stdlib.h> /* For *alloc(3) */
21#include <string.h> /* For memcpy(3) */
22#include <sys/types.h> /* For size_t */
Lev Walkin5c879db2007-11-06 06:23:31 +000023#include <limits.h> /* For LONG_MAX */
Lev Walkinf15320b2004-06-03 03:38:44 +000024#include <stdarg.h> /* For va_start */
Lev Walkinf15320b2004-06-03 03:38:44 +000025#include <stddef.h> /* for offsetof and ptrdiff_t */
Lev Walkin0ede18c2004-09-07 10:47:39 +000026
Lev Walkin93659562010-11-20 09:47:13 -080027#ifdef _WIN32
Lev Walkin4efbfb72005-02-25 14:20:30 +000028
Lev Walkinf689f0b2006-05-02 18:55:41 +000029#include <malloc.h>
Lev Walkin4efbfb72005-02-25 14:20:30 +000030#define snprintf _snprintf
31#define vsnprintf _vsnprintf
Lev Walkin4efbfb72005-02-25 14:20:30 +000032
Lev Walkin217f2402006-11-21 10:38:50 +000033/* To avoid linking with ws2_32.lib, here's the definition of ntohl() */
Lev Walkin349e8472006-11-27 13:31:35 +000034#define sys_ntohl(l) ((((l) << 24) & 0xff000000) \
Ilya Basin762747c2012-12-01 14:38:00 +040035 | (((l) << 8) & 0xff0000) \
36 | (((l) >> 8) & 0xff00) \
37 | ((l >> 24) & 0xff))
Lev Walkin217f2402006-11-21 10:38:50 +000038
Lev Walkin4cca5092005-05-27 20:56:08 +000039#ifdef _MSC_VER /* MSVS.Net */
Lev Walkine44ea0b2005-08-17 18:39:36 +000040#ifndef __cplusplus
41#define inline __inline
42#endif
Lev Walkin8e391912007-11-13 23:32:38 +000043#ifndef ASSUMESTDTYPES /* Standard types have been defined elsewhere */
Lev Walkin4cca5092005-05-27 20:56:08 +000044#define ssize_t SSIZE_T
Erika Thorsenafea72f2016-11-14 07:55:19 +010045#if _MSC_VER < 1600
Lev Walkin4cca5092005-05-27 20:56:08 +000046typedef char int8_t;
47typedef short int16_t;
48typedef int int32_t;
49typedef unsigned char uint8_t;
50typedef unsigned short uint16_t;
51typedef unsigned int uint32_t;
Erika Thorsenafea72f2016-11-14 07:55:19 +010052#else /* _MSC_VER >= 1600 */
53#include <stdint.h>
54#endif /* _MSC_VER < 1600 */
Lev Walkin8e391912007-11-13 23:32:38 +000055#endif /* ASSUMESTDTYPES */
Lev Walkin05c81b72010-11-27 17:08:12 -080056#define WIN32_LEAN_AND_MEAN
Lev Walkin4cca5092005-05-27 20:56:08 +000057#include <windows.h>
Lev Walkin2f505022005-07-01 08:28:18 +000058#include <float.h>
59#define isnan _isnan
60#define finite _finite
61#define copysign _copysign
62#define ilogb _logb
Lev Walkin7943c302007-11-13 05:09:35 +000063#else /* !_MSC_VER */
64#include <stdint.h>
Lev Walkin4cca5092005-05-27 20:56:08 +000065#endif /* _MSC_VER */
66
Lev Walkin93659562010-11-20 09:47:13 -080067#else /* !_WIN32 */
Lev Walkin4efbfb72005-02-25 14:20:30 +000068
Lev Walkinf689f0b2006-05-02 18:55:41 +000069#if defined(__vxworks)
70#include <types/vxTypes.h>
71#else /* !defined(__vxworks) */
72
Lev Walkin0ede18c2004-09-07 10:47:39 +000073#include <inttypes.h> /* C99 specifies this file */
Lev Walkin217f2402006-11-21 10:38:50 +000074#include <netinet/in.h> /* for ntohl() */
75#define sys_ntohl(foo) ntohl(foo)
Lev Walkinf689f0b2006-05-02 18:55:41 +000076#endif /* defined(__vxworks) */
77
Lev Walkin93659562010-11-20 09:47:13 -080078#endif /* _WIN32 */
Lev Walkin64399722004-08-11 07:17:22 +000079
Lev Walkin642b92f2017-09-17 22:16:02 -070080#if __GNUC__ >= 3 || defined(__clang__)
Lev Walkin3f995632017-09-26 18:27:32 -070081#define CC_ATTRIBUTE(attr) __attribute__((attr))
Lev Walkin3f6afc12006-09-15 18:52:36 +000082#else
Lev Walkin3f995632017-09-26 18:27:32 -070083#define CC_ATTRIBUTE(attr)
Lev Walkin3f6afc12006-09-15 18:52:36 +000084#endif
Lev Walkina4f68912017-09-27 02:21:38 +000085#define CC_PRINTFLIKE(fmt, var) CC_ATTRIBUTE(format(printf, fmt, var))
86#define CC_NOTUSED CC_ATTRIBUTE(unused)
87#ifndef CC_ATTR_NO_SANITIZE
88#define CC_ATTR_NO_SANITIZE(what) CC_ATTRIBUTE(no_sanitize(what))
89#endif
Lev Walkin642b92f2017-09-17 22:16:02 -070090
Lev Walkin3e86a4e2007-06-29 11:38:57 +000091/* Figure out if thread safety is requested */
Lev Walkin27f53bc2007-06-29 12:10:50 +000092#if !defined(ASN_THREAD_SAFE) && (defined(THREAD_SAFE) || defined(_REENTRANT))
Lev Walkin3e86a4e2007-06-29 11:38:57 +000093#define ASN_THREAD_SAFE
94#endif /* Thread safety */
95
Lev Walkin64399722004-08-11 07:17:22 +000096#ifndef offsetof /* If not defined by <stddef.h> */
Lev Walkinf15320b2004-06-03 03:38:44 +000097#define offsetof(s, m) ((ptrdiff_t)&(((s *)0)->m) - (ptrdiff_t)((s *)0))
98#endif /* offsetof */
99
Lev Walkinf15320b2004-06-03 03:38:44 +0000100#ifndef MIN /* Suitable for comparing primitive types (integers) */
101#if defined(__GNUC__)
102#define MIN(a,b) ({ __typeof a _a = a; __typeof b _b = b; \
103 ((_a)<(_b)?(_a):(_b)); })
104#else /* !__GNUC__ */
105#define MIN(a,b) ((a)<(b)?(a):(b)) /* Unsafe variant */
106#endif /* __GNUC__ */
107#endif /* MIN */
108
Lev Walkind84f6032017-10-03 16:33:59 -0700109#if __STDC_VERSION__ >= 199901L
Lev Walkin39837e62017-07-20 14:51:08 +0300110#ifndef SIZE_MAX
Lev Walkind84f6032017-10-03 16:33:59 -0700111#define SIZE_MAX ((~((size_t)0)) >> 1)
Lev Walkin39837e62017-07-20 14:51:08 +0300112#endif
113
114#ifndef RSIZE_MAX /* C11, Annex K */
115#define RSIZE_MAX (SIZE_MAX >> 1)
116#endif
Lev Walkin93682632017-09-15 23:32:36 -0700117#ifndef RSSIZE_MAX /* Halve signed size even further than unsigned */
118#define RSSIZE_MAX ((ssize_t)(RSIZE_MAX >> 1))
119#endif
Lev Walkind84f6032017-10-03 16:33:59 -0700120#else /* Old compiler */
121#undef SIZE_MAX
122#undef RSIZE_MAX
123#undef RSSIZE_MAX
124#define SIZE_MAX ((~((size_t)0)) >> 1)
125#define RSIZE_MAX (SIZE_MAX >> 1)
126#define RSSIZE_MAX ((ssize_t)(RSIZE_MAX >> 1))
127#endif
Lev Walkin39837e62017-07-20 14:51:08 +0300128
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700129#endif /* ASN_SYSTEM_H */