1 /* Copyright (c) 2003, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2011, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
8 * \brief Header file to define uint32_t and friends
19 #ifdef HAVE_SYS_TYPES_H
20 #include <sys/types.h>
25 #ifdef HAVE_SYS_LIMITS_H
26 #include <sys/limits.h>
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
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>
39 #ifdef HAVE_INTTYPES_H
43 #if (SIZEOF_INT8_T != 0)
46 #if (SIZEOF_INT16_T != 0)
49 #if (SIZEOF_INT32_T != 0)
52 #if (SIZEOF_INT64_T != 0)
55 #if (SIZEOF_UINT8_T != 0)
58 #if (SIZEOF_UINT16_T != 0)
61 #if (SIZEOF_UINT32_T != 0)
64 #if (SIZEOF_UINT64_T != 0)
67 #if (SIZEOF_INTPTR_T != 0)
70 #if (SIZEOF_UINTPTR_T != 0)
71 #define HAVE_UINTPTR_T
74 #if (SIZEOF_CHAR == 1)
76 typedef signed char int8_t;
80 typedef unsigned char uint8_t;
85 #if (SIZEOF_SHORT == 2)
87 typedef signed short int16_t;
91 typedef unsigned short uint16_t;
98 typedef signed int int16_t;
101 #ifndef HAVE_UINT16_T
102 typedef unsigned int uint16_t;
103 #define HAVE_UINT16_T
105 #elif (SIZEOF_INT == 4)
107 typedef signed int int32_t;
110 #ifndef HAVE_UINT32_T
111 typedef unsigned int uint32_t;
112 #define HAVE_UINT32_T
115 #define UINT16_MAX 0xffffu
118 #define INT16_MAX 0x7fff
121 #define INT16_MIN (-INT16_MAX-1)
124 #define UINT32_MAX 0xffffffffu
127 #define INT32_MAX 0x7fffffff
130 #define INT32_MIN (-2147483647-1)
134 #if (SIZEOF_LONG == 4)
136 typedef signed long int32_t;
139 #ifndef HAVE_UINT32_T
140 typedef unsigned long uint32_t;
141 #define HAVE_UINT32_T
143 #define UINT32_MAX 0xfffffffful
146 #elif (SIZEOF_LONG == 8)
148 typedef signed long int64_t;
151 #ifndef HAVE_UINT32_T
152 typedef unsigned long uint64_t;
153 #define HAVE_UINT32_T
156 #define UINT64_MAX 0xfffffffffffffffful
160 #if (SIZEOF_LONG_LONG == 8)
162 typedef signed long long int64_t;
165 #ifndef HAVE_UINT64_T
166 typedef unsigned long long uint64_t;
167 #define HAVE_UINT64_T
170 #define UINT64_MAX 0xffffffffffffffffull
173 #define INT64_MAX 0x7fffffffffffffffll
177 #if (SIZEOF___INT64 == 8)
179 typedef signed __int64
int64_t;
182 #ifndef HAVE_UINT64_T
183 typedef unsigned __int64
uint64_t;
184 #define HAVE_UINT64_T
187 #define UINT64_MAX 0xffffffffffffffffui64
190 #define INT64_MAX 0x7fffffffffffffffi64
195 #if SIZEOF_SIZE_T == 8
196 typedef int64_t ssize_t
;
197 #elif SIZEOF_SIZE_T == 4
198 typedef int32_t ssize_t
;
200 #error "Can't define ssize_t."
204 #if (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8)
205 #ifndef HAVE_INTPTR_T
206 typedef int64_t intptr_t;
208 #ifndef HAVE_UINTPTR_T
209 typedef uint64_t uintptr_t;
211 #elif (SIZEOF_VOID_P > 2 && SIZEOF_VOID_P <= 4)
212 #ifndef HAVE_INTPTR_T
213 typedef int32_t intptr_t;
215 #ifndef HAVE_UINTPTR_T
216 typedef uint32_t uintptr_t;
219 #error "void * is either >8 bytes or <= 2. In either case, I am confused."
223 #error "Missing type int8_t"
226 #error "Missing type uint8_t"
229 #error "Missing type int16_t"
231 #ifndef HAVE_UINT16_T
232 #error "Missing type uint16_t"
235 #error "Missing type int32_t"
237 #ifndef HAVE_UINT32_T
238 #error "Missing type uint32_t"
241 #error "Missing type int64_t"
243 #ifndef HAVE_UINT64_T
244 #error "Missing type uint64_t"
247 /* This assumes a sane (2's-complement) representation. But if you
248 * aren't 2's complement, and you don't define LONG_MAX, then you're so
249 * bizarre that I want nothing to do with you. */
250 #ifndef USING_TWOS_COMPLEMENT
251 #error "Seems that your platform doesn't use 2's complement arithmetic. Argh."
254 #if (SIZEOF_LONG == 4)
255 #define LONG_MAX 0x7fffffffL
256 #elif (SIZEOF_LONG == 8)
257 #define LONG_MAX 0x7fffffffffffffffL
259 #error "Can't define LONG_MAX"
264 #if (SIZEOF_INT == 4)
265 #define INT_MAX 0x7fffffffL
266 #elif (SIZEOF_INT == 8)
267 #define INT_MAX 0x7fffffffffffffffL
269 #error "Can't define INT_MAX"
274 #if (SIZEOF_INT == 2)
275 #define UINT_MAX 0xffffu
276 #elif (SIZEOF_INT == 4)
277 #define UINT_MAX 0xffffffffu
278 #elif (SIZEOF_INT == 8)
279 #define UINT_MAX 0xffffffffffffffffu
281 #error "Can't define UINT_MAX"
286 #if (SIZEOF_SHORT == 2)
287 #define SHORT_MAX 0x7fff
288 #elif (SIZEOF_SHORT == 4)
289 #define SHORT_MAX 0x7fffffff
291 #error "Can't define SHORT_MAX"
297 #ifdef TIME_T_IS_SIGNED
299 #if (SIZEOF_TIME_T == SIZEOF_INT)
300 #define TIME_MAX ((time_t)INT_MAX)
301 #elif (SIZEOF_TIME_T == SIZEOF_LONG)
302 #define TIME_MAX ((time_t)LONG_MAX)
303 #elif (SIZEOF_TIME_T == 8)
304 #define TIME_MAX ((time_t)INT64_MAX)
306 #error "Can't define (signed) TIME_MAX"
311 #if (SIZEOF_TIME_T == 4)
312 #define TIME_MAX ((time_t)UINT32_MAX)
313 #elif (SIZEOF_TIME_T == 8)
314 #define TIME_MAX ((time_t)UINT64_MAX)
316 #error "Can't define (unsigned) TIME_MAX"
318 #endif /* time_t_is_signed */
319 #endif /* ifndef(TIME_MAX) */
322 #if (SIZEOF_SIZE_T == 4)
323 #define SIZE_T_MAX UINT32_MAX
324 #elif (SIZEOF_SIZE_T == 8)
325 #define SIZE_T_MAX UINT64_MAX
327 #error "Can't define SIZE_T_MAX"
332 #if (SIZEOF_SIZE_T == 4)
333 #define SSIZE_T_MAX INT32_MAX
334 #elif (SIZEOF_SIZE_T == 8)
335 #define SSIZE_T_MAX INT64_MAX
337 #error "Can't define SSIZE_T_MAX"
341 /** Any ssize_t larger than this amount is likely to be an underflow. */
342 #define SSIZE_T_CEILING ((ssize_t)(SSIZE_T_MAX-16))
343 /** Any size_t larger than this amount is likely to be an underflow. */
344 #define SIZE_T_CEILING ((size_t)(SSIZE_T_MAX-16))
346 #endif /* __TORINT_H */