]> git.zerfleddert.de Git - proxmark3-svn/blob - client/tinycbor/compilersupport_p.h
Ndef and MAD (#801)
[proxmark3-svn] / client / tinycbor / compilersupport_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 Intel Corporation
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 **
12 ** The above copyright notice and this permission notice shall be included in
13 ** all copies or substantial portions of the Software.
14 **
15 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 ** THE SOFTWARE.
22 **
23 ****************************************************************************/
24
25 #ifndef COMPILERSUPPORT_H
26 #define COMPILERSUPPORT_H
27
28 #include "cbor.h"
29
30 #ifndef _BSD_SOURCE
31 # define _BSD_SOURCE
32 #endif
33 #ifndef _DEFAULT_SOURCE
34 # define _DEFAULT_SOURCE
35 #endif
36 #ifndef assert
37 # include <assert.h>
38 #endif
39 #include <stddef.h>
40 #include <stdint.h>
41 #include <string.h>
42
43 #ifndef __cplusplus
44 # include <stdbool.h>
45 #endif
46
47 #if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L || __cpp_static_assert >= 200410
48 # define cbor_static_assert(x) static_assert(x, #x)
49 #elif !defined(__cplusplus) && defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406) && (__STDC_VERSION__ > 199901L)
50 # define cbor_static_assert(x) _Static_assert(x, #x)
51 #else
52 # define cbor_static_assert(x) ((void)sizeof(char[2*!!(x) - 1]))
53 #endif
54 #if __STDC_VERSION__ >= 199901L || defined(__cplusplus)
55 /* inline is a keyword */
56 #else
57 /* use the definition from cbor.h */
58 # define inline CBOR_INLINE
59 #endif
60
61 #ifdef NDEBUG
62 # define cbor_assert(cond) do { if (!(cond)) unreachable(); } while (0)
63 #else
64 # define cbor_assert(cond) assert(cond)
65 #endif
66
67 #ifndef STRINGIFY
68 #define STRINGIFY(x) STRINGIFY2(x)
69 #endif
70 #define STRINGIFY2(x) #x
71
72 #if !defined(UINT32_MAX) || !defined(INT64_MAX)
73 /* C89? We can define UINT32_MAX portably, but not INT64_MAX */
74 # error "Your system has stdint.h but that doesn't define UINT32_MAX or INT64_MAX"
75 #endif
76
77 #ifndef DBL_DECIMAL_DIG
78 /* DBL_DECIMAL_DIG is C11 */
79 # define DBL_DECIMAL_DIG 17
80 #endif
81 #define DBL_DECIMAL_DIG_STR STRINGIFY(DBL_DECIMAL_DIG)
82
83 #if defined(__GNUC__) && defined(__i386__) && !defined(__iamcu__)
84 # define CBOR_INTERNAL_API_CC __attribute__((regparm(3)))
85 #elif defined(_MSC_VER) && defined(_M_IX86)
86 # define CBOR_INTERNAL_API_CC __fastcall
87 #else
88 # define CBOR_INTERNAL_API_CC
89 #endif
90
91 #ifndef __has_builtin
92 # define __has_builtin(x) 0
93 #endif
94
95 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) || \
96 (__has_builtin(__builtin_bswap64) && __has_builtin(__builtin_bswap32))
97 # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
98 # define cbor_ntohll __builtin_bswap64
99 # define cbor_htonll __builtin_bswap64
100 # define cbor_ntohl __builtin_bswap32
101 # define cbor_htonl __builtin_bswap32
102 # ifdef __INTEL_COMPILER
103 # define cbor_ntohs _bswap16
104 # define cbor_htons _bswap16
105 # elif (__GNUC__ * 100 + __GNUC_MINOR__ >= 608) || __has_builtin(__builtin_bswap16)
106 # define cbor_ntohs __builtin_bswap16
107 # define cbor_htons __builtin_bswap16
108 # else
109 # define cbor_ntohs(x) (((uint16_t)(x) >> 8) | ((uint16_t)(x) << 8))
110 # define cbor_htons cbor_ntohs
111 # endif
112 # else
113 # define cbor_ntohll
114 # define cbor_htonll
115 # define cbor_ntohl
116 # define cbor_htonl
117 # define cbor_ntohs
118 # define cbor_htons
119 # endif
120 #elif defined(__sun)
121 # include <sys/byteorder.h>
122 #elif defined(_MSC_VER)
123 /* MSVC, which implies Windows, which implies little-endian and sizeof(long) == 4 */
124 # include <stdlib.h>
125 # define cbor_ntohll _byteswap_uint64
126 # define cbor_htonll _byteswap_uint64
127 # define cbor_ntohl _byteswap_ulong
128 # define cbor_htonl _byteswap_ulong
129 # define cbor_ntohs _byteswap_ushort
130 # define cbor_htons _byteswap_ushort
131 #endif
132 #ifndef cbor_ntohs
133 # include <arpa/inet.h>
134 # define cbor_ntohs ntohs
135 # define cbor_htons htons
136 #endif
137 #ifndef cbor_ntohl
138 # include <arpa/inet.h>
139 # define cbor_ntohl ntohl
140 # define cbor_htonl htonl
141 #endif
142 #ifndef cbor_ntohll
143 # define cbor_ntohll ntohll
144 # define cbor_htonll htonll
145 /* ntohll isn't usually defined */
146 # ifndef ntohll
147 # if (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
148 (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN) || \
149 (defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN) || \
150 (defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)) || (defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)) || \
151 defined(__ARMEB__) || defined(__MIPSEB__) || defined(__s390__) || defined(__sparc__)
152 # define ntohll
153 # define htonll
154 # elif (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || \
155 (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
156 (defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && BYTE_ORDER == LITTLE_ENDIAN) || \
157 defined(_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__MIPSEL__) || \
158 defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) || defined(__amd64)
159 # define ntohll(x) ((ntohl((uint32_t)(x)) * UINT64_C(0x100000000)) + (ntohl((x) >> 32)))
160 # define htonll ntohll
161 # else
162 # error "Unable to determine byte order!"
163 # endif
164 # endif
165 #endif
166
167
168 #ifdef __cplusplus
169 # define CONST_CAST(t, v) const_cast<t>(v)
170 #else
171 /* C-style const_cast without triggering a warning with -Wcast-qual */
172 # define CONST_CAST(t, v) (t)(uintptr_t)(v)
173 #endif
174
175 #ifdef __GNUC__
176 #ifndef likely
177 # define likely(x) __builtin_expect(!!(x), 1)
178 #endif
179 #ifndef unlikely
180 # define unlikely(x) __builtin_expect(!!(x), 0)
181 #endif
182 # define unreachable() __builtin_unreachable()
183 #elif defined(_MSC_VER)
184 # define likely(x) (x)
185 # define unlikely(x) (x)
186 # define unreachable() __assume(0)
187 #else
188 # define likely(x) (x)
189 # define unlikely(x) (x)
190 # define unreachable() do {} while (0)
191 #endif
192
193 static inline bool add_check_overflow(size_t v1, size_t v2, size_t *r)
194 {
195 #if ((defined(__GNUC__) && (__GNUC__ >= 5)) && !defined(__INTEL_COMPILER)) || __has_builtin(__builtin_add_overflow)
196 return __builtin_add_overflow(v1, v2, r);
197 #else
198 /* unsigned additions are well-defined */
199 *r = v1 + v2;
200 return v1 > v1 + v2;
201 #endif
202 }
203
204 #endif /* COMPILERSUPPORT_H */
205
Impressum, Datenschutz