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 */
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 UINT32_MAX 0xffffffffu
118 #define INT32_MAX 0x7fffffffu
121 #define INT32_MIN (-2147483647-1)
125 #if (SIZEOF_LONG == 4)
127 typedef signed long int32_t;
130 #ifndef HAVE_UINT32_T
131 typedef unsigned long uint32_t;
132 #define HAVE_UINT32_T
134 #define UINT32_MAX 0xfffffffful
137 #elif (SIZEOF_LONG == 8)
139 typedef signed long int64_t;
142 #ifndef HAVE_UINT32_T
143 typedef unsigned long uint64_t;
144 #define HAVE_UINT32_T
147 #define UINT64_MAX 0xfffffffffffffffful
151 #if (SIZEOF_LONG_LONG == 8)
153 typedef signed long long int64_t;
156 #ifndef HAVE_UINT64_T
157 typedef unsigned long long uint64_t;
158 #define HAVE_UINT64_T
161 #define UINT64_MAX 0xffffffffffffffffull
164 #define INT64_MAX 0x7fffffffffffffffll
168 #if (SIZEOF___INT64 == 8)
170 typedef signed __int64
int64_t;
173 #ifndef HAVE_UINT64_T
174 typedef unsigned __int64
uint64_t;
175 #define HAVE_UINT64_T
178 #define UINT64_MAX 0xffffffffffffffffui64
181 #define INT64_MAX 0x7fffffffffffffffi64
186 #if SIZEOF_SIZE_T == 8
187 typedef int64_t ssize_t
;
188 #elif SIZEOF_SIZE_T == 4
189 typedef int32_t ssize_t
;
191 #error "Can't define ssize_t."
195 #if (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8)
196 #ifndef HAVE_INTPTR_T
197 typedef int64_t intptr_t;
199 #ifndef HAVE_UINTPTR_T
200 typedef uint64_t uintptr_t;
202 #elif (SIZEOF_VOID_P > 2 && SIZEOF_VOID_P <= 4)
203 #ifndef HAVE_INTPTR_T
204 typedef int32_t intptr_t;
206 #ifndef HAVE_UINTPTR_T
207 typedef uint32_t uintptr_t;
210 #error "void * is either >8 bytes or <= 2. In either case, I am confused."
214 #error "Missing type int8_t"
217 #error "Missing type uint8_t"
220 #error "Missing type int16_t"
222 #ifndef HAVE_UINT16_T
223 #error "Missing type uint16_t"
226 #error "Missing type int32_t"
228 #ifndef HAVE_UINT32_T
229 #error "Missing type uint32_t"
232 #error "Missing type int64_t"
234 #ifndef HAVE_UINT64_T
235 #error "Missing type uint64_t"
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."
245 #if (SIZEOF_LONG == 4)
246 #define LONG_MAX 0x7fffffffL
247 #elif (SIZEOF_LONG == 8)
248 #define LONG_MAX 0x7fffffffffffffffL
250 #error "Can't define LONG_MAX"
255 #if (SIZEOF_INT == 4)
256 #define INT_MAX 0x7fffffffL
257 #elif (SIZEOF_INT == 8)
258 #define INT_MAX 0x7fffffffffffffffL
260 #error "Can't define INT_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
272 #error "Can't define UINT_MAX"
277 #if (SIZEOF_SHORT == 2)
278 #define SHORT_MAX 0x7fff
279 #elif (SIZEOF_SHORT == 4)
280 #define SHORT_MAX 0x7fffffff
282 #error "Can't define SHORT_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)
297 #error "Can't define (signed) TIME_MAX"
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)
307 #error "Can't define (unsigned) TIME_MAX"
309 #endif /* time_t_is_signed */
310 #endif /* ifndef(TIME_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
318 #error "Can't define SIZE_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
328 #error "Can't define SSIZE_T_MAX"
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 */