bump to 0.2.2.14-alpha-dev
[tor/rransom.git] / src / common / torint.h
blob57f18212adb774bcf32e2ca9b5bb38d5a739cb54
1 /* Copyright (c) 2003, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2010, 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 #ifndef INT32_MIN
121 #define INT32_MIN (-2147483647-1)
122 #endif
123 #endif
125 #if (SIZEOF_LONG == 4)
126 #ifndef HAVE_INT32_T
127 typedef signed long int32_t;
128 #define HAVE_INT32_T
129 #endif
130 #ifndef HAVE_UINT32_T
131 typedef unsigned long uint32_t;
132 #define HAVE_UINT32_T
133 #ifndef UINT32_MAX
134 #define UINT32_MAX 0xfffffffful
135 #endif
136 #endif
137 #elif (SIZEOF_LONG == 8)
138 #ifndef HAVE_INT64_T
139 typedef signed long int64_t;
140 #define HAVE_INT64_T
141 #endif
142 #ifndef HAVE_UINT32_T
143 typedef unsigned long uint64_t;
144 #define HAVE_UINT32_T
145 #endif
146 #ifndef UINT64_MAX
147 #define UINT64_MAX 0xfffffffffffffffful
148 #endif
149 #endif
151 #if (SIZEOF_LONG_LONG == 8)
152 #ifndef HAVE_INT64_T
153 typedef signed long long int64_t;
154 #define HAVE_INT64_T
155 #endif
156 #ifndef HAVE_UINT64_T
157 typedef unsigned long long uint64_t;
158 #define HAVE_UINT64_T
159 #endif
160 #ifndef UINT64_MAX
161 #define UINT64_MAX 0xffffffffffffffffull
162 #endif
163 #ifndef INT64_MAX
164 #define INT64_MAX 0x7fffffffffffffffll
165 #endif
166 #endif
168 #if (SIZEOF___INT64 == 8)
169 #ifndef HAVE_INT64_T
170 typedef signed __int64 int64_t;
171 #define HAVE_INT64_T
172 #endif
173 #ifndef HAVE_UINT64_T
174 typedef unsigned __int64 uint64_t;
175 #define HAVE_UINT64_T
176 #endif
177 #ifndef UINT64_MAX
178 #define UINT64_MAX 0xffffffffffffffffui64
179 #endif
180 #ifndef INT64_MAX
181 #define INT64_MAX 0x7fffffffffffffffi64
182 #endif
183 #endif
185 #ifndef HAVE_SSIZE_T
186 #if SIZEOF_SIZE_T == 8
187 typedef int64_t ssize_t;
188 #elif SIZEOF_SIZE_T == 4
189 typedef int32_t ssize_t;
190 #else
191 #error "Can't define ssize_t."
192 #endif
193 #endif
195 #if (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8)
196 #ifndef HAVE_INTPTR_T
197 typedef int64_t intptr_t;
198 #endif
199 #ifndef HAVE_UINTPTR_T
200 typedef uint64_t uintptr_t;
201 #endif
202 #elif (SIZEOF_VOID_P > 2 && SIZEOF_VOID_P <= 4)
203 #ifndef HAVE_INTPTR_T
204 typedef int32_t intptr_t;
205 #endif
206 #ifndef HAVE_UINTPTR_T
207 typedef uint32_t uintptr_t;
208 #endif
209 #else
210 #error "void * is either >8 bytes or <= 2. In either case, I am confused."
211 #endif
213 #ifndef HAVE_INT8_T
214 #error "Missing type int8_t"
215 #endif
216 #ifndef HAVE_UINT8_T
217 #error "Missing type uint8_t"
218 #endif
219 #ifndef HAVE_INT16_T
220 #error "Missing type int16_t"
221 #endif
222 #ifndef HAVE_UINT16_T
223 #error "Missing type uint16_t"
224 #endif
225 #ifndef HAVE_INT32_T
226 #error "Missing type int32_t"
227 #endif
228 #ifndef HAVE_UINT32_T
229 #error "Missing type uint32_t"
230 #endif
231 #ifndef HAVE_INT64_T
232 #error "Missing type int64_t"
233 #endif
234 #ifndef HAVE_UINT64_T
235 #error "Missing type uint64_t"
236 #endif
238 /* This assumes a sane (2's-complement) representation. But if you
239 * aren't 2's complement, and you don't define LONG_MAX, then you're so
240 * bizarre that I want nothing to do with you. */
241 #ifndef USING_TWOS_COMPLEMENT
242 #error "Seems that your platform doesn't use 2's complement arithmetic. Argh."
243 #endif
244 #ifndef LONG_MAX
245 #if (SIZEOF_LONG == 4)
246 #define LONG_MAX 0x7fffffffL
247 #elif (SIZEOF_LONG == 8)
248 #define LONG_MAX 0x7fffffffffffffffL
249 #else
250 #error "Can't define LONG_MAX"
251 #endif
252 #endif
254 #ifndef INT_MAX
255 #if (SIZEOF_INT == 4)
256 #define INT_MAX 0x7fffffffL
257 #elif (SIZEOF_INT == 8)
258 #define INT_MAX 0x7fffffffffffffffL
259 #else
260 #error "Can't define INT_MAX"
261 #endif
262 #endif
264 #ifndef UINT_MAX
265 #if (SIZEOF_INT == 2)
266 #define UINT_MAX 0xffffu
267 #elif (SIZEOF_INT == 4)
268 #define UINT_MAX 0xffffffffu
269 #elif (SIZEOF_INT == 8)
270 #define UINT_MAX 0xffffffffffffffffu
271 #else
272 #error "Can't define UINT_MAX"
273 #endif
274 #endif
276 #ifndef SHORT_MAX
277 #if (SIZEOF_SHORT == 2)
278 #define SHORT_MAX 0x7fff
279 #elif (SIZEOF_SHORT == 4)
280 #define SHORT_MAX 0x7fffffff
281 #else
282 #error "Can't define SHORT_MAX"
283 #endif
284 #endif
286 #ifndef TIME_MAX
288 #ifdef TIME_T_IS_SIGNED
290 #if (SIZEOF_TIME_T == SIZEOF_INT)
291 #define TIME_MAX ((time_t)INT_MAX)
292 #elif (SIZEOF_TIME_T == SIZEOF_LONG)
293 #define TIME_MAX ((time_t)LONG_MAX)
294 #elif (SIZEOF_TIME_T == 8)
295 #define TIME_MAX ((time_t)INT64_MAX)
296 #else
297 #error "Can't define (signed) TIME_MAX"
298 #endif
300 #else
301 /* Unsigned case */
302 #if (SIZEOF_TIME_T == 4)
303 #define TIME_MAX ((time_t)UINT32_MAX)
304 #elif (SIZEOF_TIME_T == 8)
305 #define TIME_MAX ((time_t)UINT64_MAX)
306 #else
307 #error "Can't define (unsigned) TIME_MAX"
308 #endif
309 #endif /* time_t_is_signed */
310 #endif /* ifndef(TIME_MAX) */
312 #ifndef SIZE_T_MAX
313 #if (SIZEOF_SIZE_T == 4)
314 #define SIZE_T_MAX UINT32_MAX
315 #elif (SIZEOF_SIZE_T == 8)
316 #define SIZE_T_MAX UINT64_MAX
317 #else
318 #error "Can't define SIZE_T_MAX"
319 #endif
320 #endif
322 #ifndef SSIZE_T_MAX
323 #if (SIZEOF_SIZE_T == 4)
324 #define SSIZE_T_MAX INT32_MAX
325 #elif (SIZEOF_SIZE_T == 8)
326 #define SSIZE_T_MAX INT64_MAX
327 #else
328 #error "Can't define SSIZE_T_MAX"
329 #endif
330 #endif
332 /* Any size_t larger than this amount is likely to be an underflow. */
333 #define SIZE_T_CEILING (sizeof(char)<<(sizeof(size_t)*8 - 1))
335 #endif /* __TORINT_H */