lookup_last_hid_serv_request() could overflow and leak memory
[tor/rransom.git] / src / common / torint.h
blobf8441859a9f453dd7943a765c9bc97b7e72af129
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 */
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 #endif
122 #ifndef INT32_MIN
123 #define INT32_MIN (-2147483647-1)
124 #endif
126 #if (SIZEOF_LONG == 4)
127 #ifndef HAVE_INT32_T
128 typedef signed long int32_t;
129 #define HAVE_INT32_T
130 #endif
131 #ifndef HAVE_UINT32_T
132 typedef unsigned long uint32_t;
133 #define HAVE_UINT32_T
134 #ifndef UINT32_MAX
135 #define UINT32_MAX 0xfffffffful
136 #endif
137 #endif
138 #elif (SIZEOF_LONG == 8)
139 #ifndef HAVE_INT64_T
140 typedef signed long int64_t;
141 #define HAVE_INT64_T
142 #endif
143 #ifndef HAVE_UINT32_T
144 typedef unsigned long uint64_t;
145 #define HAVE_UINT32_T
146 #endif
147 #ifndef UINT64_MAX
148 #define UINT64_MAX 0xfffffffffffffffful
149 #endif
150 #endif
152 #if (SIZEOF_LONG_LONG == 8)
153 #ifndef HAVE_INT64_T
154 typedef signed long long int64_t;
155 #define HAVE_INT64_T
156 #endif
157 #ifndef HAVE_UINT64_T
158 typedef unsigned long long uint64_t;
159 #define HAVE_UINT64_T
160 #endif
161 #ifndef UINT64_MAX
162 #define UINT64_MAX 0xffffffffffffffffull
163 #endif
164 #ifndef INT64_MAX
165 #define INT64_MAX 0x7fffffffffffffffll
166 #endif
167 #endif
169 #if (SIZEOF___INT64 == 8)
170 #ifndef HAVE_INT64_T
171 typedef signed __int64 int64_t;
172 #define HAVE_INT64_T
173 #endif
174 #ifndef HAVE_UINT64_T
175 typedef unsigned __int64 uint64_t;
176 #define HAVE_UINT64_T
177 #endif
178 #ifndef UINT64_MAX
179 #define UINT64_MAX 0xffffffffffffffffui64
180 #endif
181 #ifndef INT64_MAX
182 #define INT64_MAX 0x7fffffffffffffffi64
183 #endif
184 #endif
186 #ifndef HAVE_SSIZE_T
187 #if SIZEOF_SIZE_T == 8
188 typedef int64_t ssize_t;
189 #elif SIZEOF_SIZE_T == 4
190 typedef int32_t ssize_t;
191 #else
192 #error "Can't define ssize_t."
193 #endif
194 #endif
196 #if (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8)
197 #ifndef HAVE_INTPTR_T
198 typedef int64_t intptr_t;
199 #endif
200 #ifndef HAVE_UINTPTR_T
201 typedef uint64_t uintptr_t;
202 #endif
203 #elif (SIZEOF_VOID_P > 2 && SIZEOF_VOID_P <= 4)
204 #ifndef HAVE_INTPTR_T
205 typedef int32_t intptr_t;
206 #endif
207 #ifndef HAVE_UINTPTR_T
208 typedef uint32_t uintptr_t;
209 #endif
210 #else
211 #error "void * is either >8 bytes or <= 2. In either case, I am confused."
212 #endif
214 #ifndef HAVE_INT8_T
215 #error "Missing type int8_t"
216 #endif
217 #ifndef HAVE_UINT8_T
218 #error "Missing type uint8_t"
219 #endif
220 #ifndef HAVE_INT16_T
221 #error "Missing type int16_t"
222 #endif
223 #ifndef HAVE_UINT16_T
224 #error "Missing type uint16_t"
225 #endif
226 #ifndef HAVE_INT32_T
227 #error "Missing type int32_t"
228 #endif
229 #ifndef HAVE_UINT32_T
230 #error "Missing type uint32_t"
231 #endif
232 #ifndef HAVE_INT64_T
233 #error "Missing type int64_t"
234 #endif
235 #ifndef HAVE_UINT64_T
236 #error "Missing type uint64_t"
237 #endif
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."
244 #endif
245 #ifndef LONG_MAX
246 #if (SIZEOF_LONG == 4)
247 #define LONG_MAX 0x7fffffffL
248 #elif (SIZEOF_LONG == 8)
249 #define LONG_MAX 0x7fffffffffffffffL
250 #else
251 #error "Can't define LONG_MAX"
252 #endif
253 #endif
255 #ifndef INT_MAX
256 #if (SIZEOF_INT == 4)
257 #define INT_MAX 0x7fffffffL
258 #elif (SIZEOF_INT == 8)
259 #define INT_MAX 0x7fffffffffffffffL
260 #else
261 #error "Can't define INT_MAX"
262 #endif
263 #endif
265 #ifndef UINT_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
272 #else
273 #error "Can't define UINT_MAX"
274 #endif
275 #endif
277 #ifndef SHORT_MAX
278 #if (SIZEOF_SHORT == 2)
279 #define SHORT_MAX 0x7fff
280 #elif (SIZEOF_SHORT == 4)
281 #define SHORT_MAX 0x7fffffff
282 #else
283 #error "Can't define SHORT_MAX"
284 #endif
285 #endif
287 #ifndef TIME_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)
297 #else
298 #error "Can't define (signed) TIME_MAX"
299 #endif
301 #else
302 /* Unsigned case */
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)
307 #else
308 #error "Can't define (unsigned) TIME_MAX"
309 #endif
310 #endif /* time_t_is_signed */
311 #endif /* ifndef(TIME_MAX) */
313 #ifndef SIZE_T_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
318 #else
319 #error "Can't define SIZE_T_MAX"
320 #endif
321 #endif
323 #ifndef SSIZE_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
328 #else
329 #error "Can't define SSIZE_T_MAX"
330 #endif
331 #endif
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 */