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 */
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
123 #define INT32_MIN (-2147483647-1)
126 #if (SIZEOF_LONG == 4)
128 typedef signed long int32_t;
131 #ifndef HAVE_UINT32_T
132 typedef unsigned long uint32_t;
133 #define HAVE_UINT32_T
135 #define UINT32_MAX 0xfffffffful
138 #elif (SIZEOF_LONG == 8)
140 typedef signed long int64_t;
143 #ifndef HAVE_UINT32_T
144 typedef unsigned long uint64_t;
145 #define HAVE_UINT32_T
148 #define UINT64_MAX 0xfffffffffffffffful
152 #if (SIZEOF_LONG_LONG == 8)
154 typedef signed long long int64_t;
157 #ifndef HAVE_UINT64_T
158 typedef unsigned long long uint64_t;
159 #define HAVE_UINT64_T
162 #define UINT64_MAX 0xffffffffffffffffull
165 #define INT64_MAX 0x7fffffffffffffffll
169 #if (SIZEOF___INT64 == 8)
171 typedef signed __int64
int64_t;
174 #ifndef HAVE_UINT64_T
175 typedef unsigned __int64
uint64_t;
176 #define HAVE_UINT64_T
179 #define UINT64_MAX 0xffffffffffffffffui64
182 #define INT64_MAX 0x7fffffffffffffffi64
187 #if SIZEOF_SIZE_T == 8
188 typedef int64_t ssize_t
;
189 #elif SIZEOF_SIZE_T == 4
190 typedef int32_t ssize_t
;
192 #error "Can't define ssize_t."
196 #if (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8)
197 #ifndef HAVE_INTPTR_T
198 typedef int64_t intptr_t;
200 #ifndef HAVE_UINTPTR_T
201 typedef uint64_t uintptr_t;
203 #elif (SIZEOF_VOID_P > 2 && SIZEOF_VOID_P <= 4)
204 #ifndef HAVE_INTPTR_T
205 typedef int32_t intptr_t;
207 #ifndef HAVE_UINTPTR_T
208 typedef uint32_t uintptr_t;
211 #error "void * is either >8 bytes or <= 2. In either case, I am confused."
215 #error "Missing type int8_t"
218 #error "Missing type uint8_t"
221 #error "Missing type int16_t"
223 #ifndef HAVE_UINT16_T
224 #error "Missing type uint16_t"
227 #error "Missing type int32_t"
229 #ifndef HAVE_UINT32_T
230 #error "Missing type uint32_t"
233 #error "Missing type int64_t"
235 #ifndef HAVE_UINT64_T
236 #error "Missing type uint64_t"
239 /* This assumes a sane (2's-complement) representation. But if you
240 * aren't 2's complement, and you don't define LONG_MAX, then you're so
241 * bizarre that I want nothing to do with you. */
242 #ifndef USING_TWOS_COMPLEMENT
243 #error "Seems that your platform doesn't use 2's complement arithmetic. Argh."
246 #if (SIZEOF_LONG == 4)
247 #define LONG_MAX 0x7fffffffL
248 #elif (SIZEOF_LONG == 8)
249 #define LONG_MAX 0x7fffffffffffffffL
251 #error "Can't define LONG_MAX"
256 #if (SIZEOF_INT == 4)
257 #define INT_MAX 0x7fffffffL
258 #elif (SIZEOF_INT == 8)
259 #define INT_MAX 0x7fffffffffffffffL
261 #error "Can't define INT_MAX"
266 #if (SIZEOF_INT == 2)
267 #define UINT_MAX 0xffffu
268 #elif (SIZEOF_INT == 4)
269 #define UINT_MAX 0xffffffffu
270 #elif (SIZEOF_INT == 8)
271 #define UINT_MAX 0xffffffffffffffffu
273 #error "Can't define UINT_MAX"
278 #if (SIZEOF_SHORT == 2)
279 #define SHORT_MAX 0x7fff
280 #elif (SIZEOF_SHORT == 4)
281 #define SHORT_MAX 0x7fffffff
283 #error "Can't define SHORT_MAX"
289 #ifdef TIME_T_IS_SIGNED
291 #if (SIZEOF_TIME_T == SIZEOF_INT)
292 #define TIME_MAX ((time_t)INT_MAX)
293 #elif (SIZEOF_TIME_T == SIZEOF_LONG)
294 #define TIME_MAX ((time_t)LONG_MAX)
295 #elif (SIZEOF_TIME_T == 8)
296 #define TIME_MAX ((time_t)INT64_MAX)
298 #error "Can't define (signed) TIME_MAX"
303 #if (SIZEOF_TIME_T == 4)
304 #define TIME_MAX ((time_t)UINT32_MAX)
305 #elif (SIZEOF_TIME_T == 8)
306 #define TIME_MAX ((time_t)UINT64_MAX)
308 #error "Can't define (unsigned) TIME_MAX"
310 #endif /* time_t_is_signed */
311 #endif /* ifndef(TIME_MAX) */
314 #if (SIZEOF_SIZE_T == 4)
315 #define SIZE_T_MAX UINT32_MAX
316 #elif (SIZEOF_SIZE_T == 8)
317 #define SIZE_T_MAX UINT64_MAX
319 #error "Can't define SIZE_T_MAX"
324 #if (SIZEOF_SIZE_T == 4)
325 #define SSIZE_T_MAX INT32_MAX
326 #elif (SIZEOF_SIZE_T == 8)
327 #define SSIZE_T_MAX INT64_MAX
329 #error "Can't define SSIZE_T_MAX"
333 /* Any size_t larger than this amount is likely to be an underflow. */
334 #define SIZE_T_CEILING (sizeof(char)<<(sizeof(size_t)*8 - 1))
336 #endif /* __TORINT_H */