Use a simpler fix for the byte-reversing warning
[tor/rransom.git] / src / common / torint.h
blob1f7421174a51caff548bd73a079785f3f42deb95
1 /* Copyright (c) 2003, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2009, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 /**
7 * \file torint.h
8 * \brief Header file to define uint32_t and friends
9 **/
11 #ifndef _TOR_TORINT_H
12 #define _TOR_TORINT_H
14 #include "orconfig.h"
16 #ifdef HAVE_STDINT_H
17 #include <stdint.h>
18 #endif
19 #ifdef HAVE_SYS_TYPES_H
20 #include <sys/types.h>
21 #endif
22 #ifdef HAVE_LIMITS_H
23 #include <limits.h>
24 #endif
25 #ifdef HAVE_SYS_LIMITS_H
26 #include <sys/limits.h>
27 #endif
28 #ifdef HAVE_MACHINE_LIMITS_H
29 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
30 /* FreeBSD has a bug where it complains that this file is obsolete,
31 and I should migrate to using sys/limits. It complains even when
32 I include both.
33 __FreeBSD_kernel__ is defined by Debian GNU/kFreeBSD which
34 does the same thing (but doesn't defined __FreeBSD__).
36 #include <machine/limits.h>
37 #endif
38 #endif
39 #ifdef HAVE_INTTYPES_H
40 #include <inttypes.h>
41 #endif
43 #if (SIZEOF_INT8_T != 0)
44 #define HAVE_INT8_T
45 #endif
46 #if (SIZEOF_INT16_T != 0)
47 #define HAVE_INT16_T
48 #endif
49 #if (SIZEOF_INT32_T != 0)
50 #define HAVE_INT32_T
51 #endif
52 #if (SIZEOF_INT64_T != 0)
53 #define HAVE_INT64_T
54 #endif
55 #if (SIZEOF_UINT8_T != 0)
56 #define HAVE_UINT8_T
57 #endif
58 #if (SIZEOF_UINT16_T != 0)
59 #define HAVE_UINT16_T
60 #endif
61 #if (SIZEOF_UINT32_T != 0)
62 #define HAVE_UINT32_T
63 #endif
64 #if (SIZEOF_UINT64_T != 0)
65 #define HAVE_UINT64_T
66 #endif
67 #if (SIZEOF_INTPTR_T != 0)
68 #define HAVE_INTPTR_T
69 #endif
70 #if (SIZEOF_UINTPTR_T != 0)
71 #define HAVE_UINTPTR_T
72 #endif
74 #if (SIZEOF_CHAR == 1)
75 #ifndef HAVE_INT8_T
76 typedef signed char int8_t;
77 #define HAVE_INT8_T
78 #endif
79 #ifndef HAVE_UINT8_T
80 typedef unsigned char uint8_t;
81 #define HAVE_UINT8_T
82 #endif
83 #endif
85 #if (SIZEOF_SHORT == 2)
86 #ifndef HAVE_INT16_T
87 typedef signed short int16_t;
88 #define HAVE_INT16_T
89 #endif
90 #ifndef HAVE_UINT16_T
91 typedef unsigned short uint16_t;
92 #define HAVE_UINT16_T
93 #endif
94 #endif
96 #if (SIZEOF_INT == 2)
97 #ifndef HAVE_INT16_T
98 typedef signed int int16_t;
99 #define HAVE_INT16_T
100 #endif
101 #ifndef HAVE_UINT16_T
102 typedef unsigned int uint16_t;
103 #define HAVE_UINT16_T
104 #endif
105 #elif (SIZEOF_INT == 4)
106 #ifndef HAVE_INT32_T
107 typedef signed int int32_t;
108 #define HAVE_INT32_T
109 #endif
110 #ifndef HAVE_UINT32_T
111 typedef unsigned int uint32_t;
112 #define HAVE_UINT32_T
113 #endif
114 #ifndef UINT32_MAX
115 #define UINT32_MAX 0xffffffffu
116 #endif
117 #ifndef INT32_MAX
118 #define INT32_MAX 0x7fffffffu
119 #endif
120 #endif
122 #if (SIZEOF_LONG == 4)
123 #ifndef HAVE_INT32_T
124 typedef signed long int32_t;
125 #define HAVE_INT32_T
126 #endif
127 #ifndef HAVE_UINT32_T
128 typedef unsigned long uint32_t;
129 #define HAVE_UINT32_T
130 #ifndef UINT32_MAX
131 #define UINT32_MAX 0xfffffffful
132 #endif
133 #endif
134 #elif (SIZEOF_LONG == 8)
135 #ifndef HAVE_INT64_T
136 typedef signed long int64_t;
137 #define HAVE_INT64_T
138 #endif
139 #ifndef HAVE_UINT32_T
140 typedef unsigned long uint64_t;
141 #define HAVE_UINT32_T
142 #endif
143 #ifndef UINT64_MAX
144 #define UINT64_MAX 0xfffffffffffffffful
145 #endif
146 #endif
148 #if (SIZEOF_LONG_LONG == 8)
149 #ifndef HAVE_INT64_T
150 typedef signed long long int64_t;
151 #define HAVE_INT64_T
152 #endif
153 #ifndef HAVE_UINT64_T
154 typedef unsigned long long uint64_t;
155 #define HAVE_UINT64_T
156 #endif
157 #ifndef UINT64_MAX
158 #define UINT64_MAX 0xffffffffffffffffull
159 #endif
160 #ifndef INT64_MAX
161 #define INT64_MAX 0x7fffffffffffffffll
162 #endif
163 #endif
165 #if (SIZEOF___INT64 == 8)
166 #ifndef HAVE_INT64_T
167 typedef signed __int64 int64_t;
168 #define HAVE_INT64_T
169 #endif
170 #ifndef HAVE_UINT64_T
171 typedef unsigned __int64 uint64_t;
172 #define HAVE_UINT64_T
173 #endif
174 #ifndef UINT64_MAX
175 #define UINT64_MAX 0xffffffffffffffffui64
176 #endif
177 #ifndef INT64_MAX
178 #define INT64_MAX 0x7fffffffffffffffi64
179 #endif
180 #endif
182 #ifndef HAVE_SSIZE_T
183 #if SIZEOF_SIZE_T == 8
184 typedef int64_t ssize_t;
185 #elif SIZEOF_SIZE_T == 4
186 typedef int32_t ssize_t;
187 #else
188 #error "Can't define ssize_t."
189 #endif
190 #endif
192 #if (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8)
193 #ifndef HAVE_INTPTR_T
194 typedef int64_t intptr_t;
195 #endif
196 #ifndef HAVE_UINTPTR_T
197 typedef uint64_t uintptr_t;
198 #endif
199 #elif (SIZEOF_VOID_P > 2 && SIZEOF_VOID_P <= 4)
200 #ifndef HAVE_INTPTR_T
201 typedef int32_t intptr_t;
202 #endif
203 #ifndef HAVE_UINTPTR_T
204 typedef uint32_t uintptr_t;
205 #endif
206 #else
207 #error "void * is either >8 bytes or <= 2. In either case, I am confused."
208 #endif
210 #ifndef HAVE_INT8_T
211 #error "Missing type int8_t"
212 #endif
213 #ifndef HAVE_UINT8_T
214 #error "Missing type uint8_t"
215 #endif
216 #ifndef HAVE_INT16_T
217 #error "Missing type int16_t"
218 #endif
219 #ifndef HAVE_UINT16_T
220 #error "Missing type uint16_t"
221 #endif
222 #ifndef HAVE_INT32_T
223 #error "Missing type int32_t"
224 #endif
225 #ifndef HAVE_UINT32_T
226 #error "Missing type uint32_t"
227 #endif
228 #ifndef HAVE_INT64_T
229 #error "Missing type int64_t"
230 #endif
231 #ifndef HAVE_UINT64_T
232 #error "Missing type uint64_t"
233 #endif
235 /* This assumes a sane (2's-complement) representation. But if you
236 * aren't 2's complement, and you don't define LONG_MAX, then you're so
237 * bizarre that I want nothing to do with you. */
238 #ifndef USING_TWOS_COMPLEMENT
239 #error "Seems that your platform doesn't use 2's complement arithmetic. Argh."
240 #endif
241 #ifndef LONG_MAX
242 #if (SIZEOF_LONG == 4)
243 #define LONG_MAX 0x7fffffffL
244 #elif (SIZEOF_LONG == 8)
245 #define LONG_MAX 0x7fffffffffffffffL
246 #else
247 #error "Can't define LONG_MAX"
248 #endif
249 #endif
251 #ifndef INT_MAX
252 #if (SIZEOF_INT == 4)
253 #define INT_MAX 0x7fffffffL
254 #elif (SIZEOF_INT == 8)
255 #define INT_MAX 0x7fffffffffffffffL
256 #else
257 #error "Can't define INT_MAX"
258 #endif
259 #endif
261 #ifndef UINT_MAX
262 #if (SIZEOF_INT == 2)
263 #define UINT_MAX 0xffffu
264 #elif (SIZEOF_INT == 4)
265 #define UINT_MAX 0xffffffffu
266 #elif (SIZEOF_INT == 8)
267 #define UINT_MAX 0xffffffffffffffffu
268 #else
269 #error "Can't define UINT_MAX"
270 #endif
271 #endif
273 #ifndef SHORT_MAX
274 #if (SIZEOF_SHORT == 2)
275 #define SHORT_MAX 0x7fff
276 #elif (SIZEOF_SHORT == 4)
277 #define SHORT_MAX 0x7fffffff
278 #else
279 #error "Can't define SHORT_MAX"
280 #endif
281 #endif
283 #ifndef TIME_MAX
285 #ifdef TIME_T_IS_SIGNED
287 #if (SIZEOF_TIME_T == SIZEOF_INT)
288 #define TIME_MAX ((time_t)INT_MAX)
289 #elif (SIZEOF_TIME_T == SIZEOF_LONG)
290 #define TIME_MAX ((time_t)LONG_MAX)
291 #elif (SIZEOF_TIME_T == 8)
292 #define TIME_MAX ((time_t)INT64_MAX)
293 #else
294 #error "Can't define (signed) TIME_MAX"
295 #endif
297 #else
298 /* Unsigned case */
299 #if (SIZEOF_TIME_T == 4)
300 #define TIME_MAX ((time_t)UINT32_MAX)
301 #elif (SIZEOF_TIME_T == 8)
302 #define TIME_MAX ((time_t)UINT64_MAX)
303 #else
304 #error "Can't define (unsigned) TIME_MAX"
305 #endif
306 #endif /* time_t_is_signed */
307 #endif /* ifndef(TIME_MAX) */
309 #ifndef SIZE_T_MAX
310 #if (SIZEOF_SIZE_T == 4)
311 #define SIZE_T_MAX UINT32_MAX
312 #elif (SIZEOF_SIZE_T == 8)
313 #define SIZE_T_MAX UINT64_MAX
314 #else
315 #error "Can't define SIZE_T_MAX"
316 #endif
317 #endif
319 #ifndef SSIZE_T_MAX
320 #if (SIZEOF_SIZE_T == 4)
321 #define SSIZE_T_MAX INT32_MAX
322 #elif (SIZEOF_SIZE_T == 8)
323 #define SSIZE_T_MAX INT64_MAX
324 #else
325 #error "Can't define SSIZE_T_MAX"
326 #endif
327 #endif
329 /* Any size_t larger than this amount is likely to be an underflow. */
330 #define SIZE_T_CEILING (sizeof(char)<<(sizeof(size_t)*8 - 1))
332 #endif /* __TORINT_H */