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
122 #if (SIZEOF_LONG == 4)
124 typedef signed long int32_t;
127 #ifndef HAVE_UINT32_T
128 typedef unsigned long uint32_t;
129 #define HAVE_UINT32_T
131 #define UINT32_MAX 0xfffffffful
134 #elif (SIZEOF_LONG == 8)
136 typedef signed long int64_t;
139 #ifndef HAVE_UINT32_T
140 typedef unsigned long uint64_t;
141 #define HAVE_UINT32_T
144 #define UINT64_MAX 0xfffffffffffffffful
148 #if (SIZEOF_LONG_LONG == 8)
150 typedef signed long long int64_t;
153 #ifndef HAVE_UINT64_T
154 typedef unsigned long long uint64_t;
155 #define HAVE_UINT64_T
158 #define UINT64_MAX 0xffffffffffffffffull
161 #define INT64_MAX 0x7fffffffffffffffll
165 #if (SIZEOF___INT64 == 8)
167 typedef signed __int64
int64_t;
170 #ifndef HAVE_UINT64_T
171 typedef unsigned __int64
uint64_t;
172 #define HAVE_UINT64_T
175 #define UINT64_MAX 0xffffffffffffffffui64
178 #define INT64_MAX 0x7fffffffffffffffi64
183 #if SIZEOF_SIZE_T == 8
184 typedef int64_t ssize_t
;
185 #elif SIZEOF_SIZE_T == 4
186 typedef int32_t ssize_t
;
188 #error "Can't define ssize_t."
192 #if (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8)
193 #ifndef HAVE_INTPTR_T
194 typedef int64_t intptr_t;
196 #ifndef HAVE_UINTPTR_T
197 typedef uint64_t uintptr_t;
199 #elif (SIZEOF_VOID_P > 2 && SIZEOF_VOID_P <= 4)
200 #ifndef HAVE_INTPTR_T
201 typedef int32_t intptr_t;
203 #ifndef HAVE_UINTPTR_T
204 typedef uint32_t uintptr_t;
207 #error "void * is either >8 bytes or <= 2. In either case, I am confused."
211 #error "Missing type int8_t"
214 #error "Missing type uint8_t"
217 #error "Missing type int16_t"
219 #ifndef HAVE_UINT16_T
220 #error "Missing type uint16_t"
223 #error "Missing type int32_t"
225 #ifndef HAVE_UINT32_T
226 #error "Missing type uint32_t"
229 #error "Missing type int64_t"
231 #ifndef HAVE_UINT64_T
232 #error "Missing type uint64_t"
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."
242 #if (SIZEOF_LONG == 4)
243 #define LONG_MAX 0x7fffffffL
244 #elif (SIZEOF_LONG == 8)
245 #define LONG_MAX 0x7fffffffffffffffL
247 #error "Can't define LONG_MAX"
252 #if (SIZEOF_INT == 4)
253 #define INT_MAX 0x7fffffffL
254 #elif (SIZEOF_INT == 8)
255 #define INT_MAX 0x7fffffffffffffffL
257 #error "Can't define INT_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
269 #error "Can't define UINT_MAX"
274 #if (SIZEOF_SHORT == 2)
275 #define SHORT_MAX 0x7fff
276 #elif (SIZEOF_SHORT == 4)
277 #define SHORT_MAX 0x7fffffff
279 #error "Can't define SHORT_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)
294 #error "Can't define (signed) TIME_MAX"
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)
304 #error "Can't define (unsigned) TIME_MAX"
306 #endif /* time_t_is_signed */
307 #endif /* ifndef(TIME_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
315 #error "Can't define SIZE_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
325 #error "Can't define SSIZE_T_MAX"
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 */