tor  master
torint.h
Go to the documentation of this file.
1 /* Copyright (c) 2003, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2017, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
11 #ifndef TOR_TORINT_H
12 #define TOR_TORINT_H
13 
14 #include "orconfig.h"
15 
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__).
35  */
36 #include <machine/limits.h>
37 #endif /* !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) */
38 #endif /* defined(HAVE_MACHINE_LIMITS_H) */
39 #ifdef HAVE_INTTYPES_H
40 #include <inttypes.h>
41 #endif
42 
43 #include <stdbool.h>
44 
45 #if (SIZEOF_INT8_T != 0)
46 #define HAVE_INT8_T
47 #endif
48 #if (SIZEOF_INT16_T != 0)
49 #define HAVE_INT16_T
50 #endif
51 #if (SIZEOF_INT32_T != 0)
52 #define HAVE_INT32_T
53 #endif
54 #if (SIZEOF_INT64_T != 0)
55 #define HAVE_INT64_T
56 #endif
57 #if (SIZEOF_UINT8_T != 0)
58 #define HAVE_UINT8_T
59 #endif
60 #if (SIZEOF_UINT16_T != 0)
61 #define HAVE_UINT16_T
62 #endif
63 #if (SIZEOF_UINT32_T != 0)
64 #define HAVE_UINT32_T
65 #endif
66 #if (SIZEOF_UINT64_T != 0)
67 #define HAVE_UINT64_T
68 #endif
69 #if (SIZEOF_INTPTR_T != 0)
70 #define HAVE_INTPTR_T
71 #endif
72 #if (SIZEOF_UINTPTR_T != 0)
73 #define HAVE_UINTPTR_T
74 #endif
75 
76 #if (SIZEOF_CHAR == 1)
77 #ifndef HAVE_INT8_T
78 typedef signed char int8_t;
79 #define HAVE_INT8_T
80 #endif
81 #ifndef HAVE_UINT8_T
82 typedef unsigned char uint8_t;
83 #define HAVE_UINT8_T
84 #endif
85 #endif /* (SIZEOF_CHAR == 1) */
86 
87 #if (SIZEOF_SHORT == 2)
88 #ifndef HAVE_INT16_T
89 typedef signed short int16_t;
90 #define HAVE_INT16_T
91 #endif
92 #ifndef HAVE_UINT16_T
93 typedef unsigned short uint16_t;
94 #define HAVE_UINT16_T
95 #endif
96 #endif /* (SIZEOF_SHORT == 2) */
97 
98 #if (SIZEOF_INT == 2)
99 #ifndef HAVE_INT16_T
100 typedef signed int int16_t;
101 #define HAVE_INT16_T
102 #endif
103 #ifndef HAVE_UINT16_T
104 typedef unsigned int uint16_t;
105 #define HAVE_UINT16_T
106 #endif
107 #elif (SIZEOF_INT == 4)
108 #ifndef HAVE_INT32_T
109 typedef signed int int32_t;
110 #define HAVE_INT32_T
111 #endif
112 #ifndef HAVE_UINT32_T
113 typedef unsigned int uint32_t;
114 #define HAVE_UINT32_T
115 #endif
116 #ifndef UINT16_MAX
117 #define UINT16_MAX 0xffffu
118 #endif
119 #ifndef INT16_MAX
120 #define INT16_MAX 0x7fff
121 #endif
122 #ifndef INT16_MIN
123 #define INT16_MIN (-INT16_MAX-1)
124 #endif
125 #ifndef UINT32_MAX
126 #define UINT32_MAX 0xffffffffu
127 #endif
128 #ifndef INT32_MAX
129 #define INT32_MAX 0x7fffffff
130 #endif
131 #ifndef INT32_MIN
132 #define INT32_MIN (-2147483647-1)
133 #endif
134 #endif /* (SIZEOF_INT == 2) || ... */
135 
136 #if (SIZEOF_LONG == 4)
137 #ifndef HAVE_INT32_T
138 typedef signed long int32_t;
139 #define HAVE_INT32_T
140 #endif
141 #ifndef HAVE_UINT32_T
142 typedef unsigned long uint32_t;
143 #define HAVE_UINT32_T
144 #ifndef UINT32_MAX
145 #define UINT32_MAX 0xfffffffful
146 #endif
147 #endif /* !defined(HAVE_UINT32_T) */
148 #elif (SIZEOF_LONG == 8)
149 #ifndef HAVE_INT64_T
150 typedef signed long int64_t;
151 #define HAVE_INT64_T
152 #endif
153 #ifndef HAVE_UINT32_T
154 typedef unsigned long uint64_t;
155 #define HAVE_UINT32_T
156 #endif
157 #ifndef UINT64_MAX
158 #define UINT64_MAX 0xfffffffffffffffful
159 #endif
160 #endif /* (SIZEOF_LONG == 4) || ... */
161 
162 #if (SIZEOF_LONG_LONG == 8)
163 #ifndef HAVE_INT64_T
164 typedef signed long long int64_t;
165 #define HAVE_INT64_T
166 #endif
167 #ifndef HAVE_UINT64_T
168 typedef unsigned long long uint64_t;
169 #define HAVE_UINT64_T
170 #endif
171 #ifndef UINT64_MAX
172 #define UINT64_MAX 0xffffffffffffffffull
173 #endif
174 #ifndef INT64_MAX
175 #define INT64_MAX 0x7fffffffffffffffll
176 #endif
177 #endif /* (SIZEOF_LONG_LONG == 8) */
178 
179 #if (SIZEOF___INT64 == 8)
180 #ifndef HAVE_INT64_T
181 typedef signed __int64 int64_t;
182 #define HAVE_INT64_T
183 #endif
184 #ifndef HAVE_UINT64_T
185 typedef unsigned __int64 uint64_t;
186 #define HAVE_UINT64_T
187 #endif
188 #ifndef UINT64_MAX
189 #define UINT64_MAX 0xffffffffffffffffui64
190 #endif
191 #ifndef INT64_MAX
192 #define INT64_MAX 0x7fffffffffffffffi64
193 #endif
194 #endif /* (SIZEOF___INT64 == 8) */
195 
196 #ifndef INT64_MIN
197 #define INT64_MIN ((- INT64_MAX) - 1)
198 #endif
199 
200 #ifndef SIZE_MAX
201 #if SIZEOF_SIZE_T == 8
202 #define SIZE_MAX UINT64_MAX
203 #elif SIZEOF_SIZE_T == 4
204 #define SIZE_MAX UINT32_MAX
205 #else
206 #error "Can't define SIZE_MAX"
207 #endif /* SIZEOF_SIZE_T == 8 || ... */
208 #endif /* !defined(SIZE_MAX) */
209 
210 #ifndef HAVE_SSIZE_T
211 #if SIZEOF_SIZE_T == 8
212 typedef int64_t ssize_t;
213 #elif SIZEOF_SIZE_T == 4
214 typedef int32_t ssize_t;
215 #else
216 #error "Can't define ssize_t."
217 #endif /* SIZEOF_SIZE_T == 8 || ... */
218 #endif /* !defined(HAVE_SSIZE_T) */
219 
220 #if (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8)
221 #ifndef HAVE_INTPTR_T
222 typedef int64_t intptr_t;
223 #define SIZEOF_INTPTR_T 8
224 #endif
225 #ifndef HAVE_UINTPTR_T
226 typedef uint64_t uintptr_t;
227 #define SIZEOF_UINTPTR_T 8
228 #endif
229 #elif (SIZEOF_VOID_P > 2 && SIZEOF_VOID_P <= 4)
230 #ifndef HAVE_INTPTR_T
231 typedef int32_t intptr_t;
232 #define SIZEOF_INTPTR_T 4
233 #endif
234 #ifndef HAVE_UINTPTR_T
235 typedef uint32_t uintptr_t;
236 #define SIZEOF_UINTPTR_T 4
237 #endif
238 #else
239 #error "void * is either >8 bytes or <= 2. In either case, I am confused."
240 #endif /* (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8) || ... */
241 
242 #ifndef HAVE_INT8_T
243 #error "Missing type int8_t"
244 #endif
245 #ifndef HAVE_UINT8_T
246 #error "Missing type uint8_t"
247 #endif
248 #ifndef HAVE_INT16_T
249 #error "Missing type int16_t"
250 #endif
251 #ifndef HAVE_UINT16_T
252 #error "Missing type uint16_t"
253 #endif
254 #ifndef HAVE_INT32_T
255 #error "Missing type int32_t"
256 #endif
257 #ifndef HAVE_UINT32_T
258 #error "Missing type uint32_t"
259 #endif
260 #ifndef HAVE_INT64_T
261 #error "Missing type int64_t"
262 #endif
263 #ifndef HAVE_UINT64_T
264 #error "Missing type uint64_t"
265 #endif
266 
267 /* This assumes a sane (2's-complement) representation. But if you
268  * aren't 2's complement, and you don't define LONG_MAX, then you're so
269  * bizarre that I want nothing to do with you. */
270 #ifndef USING_TWOS_COMPLEMENT
271 #error "Seems that your platform doesn't use 2's complement arithmetic. Argh."
272 #endif
273 #ifndef LONG_MAX
274 #if (SIZEOF_LONG == 4)
275 #define LONG_MAX 0x7fffffffL
276 #elif (SIZEOF_LONG == 8)
277 #define LONG_MAX 0x7fffffffffffffffL
278 #else
279 #error "Can't define LONG_MAX"
280 #endif /* (SIZEOF_LONG == 4) || ... */
281 #endif /* !defined(LONG_MAX) */
282 
283 #ifndef INT_MAX
284 #if (SIZEOF_INT == 4)
285 #define INT_MAX 0x7fffffffL
286 #elif (SIZEOF_INT == 8)
287 #define INT_MAX 0x7fffffffffffffffL
288 #else
289 #error "Can't define INT_MAX"
290 #endif /* (SIZEOF_INT == 4) || ... */
291 #endif /* !defined(INT_MAX) */
292 
293 #ifndef UINT_MAX
294 #if (SIZEOF_INT == 2)
295 #define UINT_MAX 0xffffu
296 #elif (SIZEOF_INT == 4)
297 #define UINT_MAX 0xffffffffu
298 #elif (SIZEOF_INT == 8)
299 #define UINT_MAX 0xffffffffffffffffu
300 #else
301 #error "Can't define UINT_MAX"
302 #endif /* (SIZEOF_INT == 2) || ... */
303 #endif /* !defined(UINT_MAX) */
304 
305 #ifndef SHORT_MAX
306 #if (SIZEOF_SHORT == 2)
307 #define SHORT_MAX 0x7fff
308 #elif (SIZEOF_SHORT == 4)
309 #define SHORT_MAX 0x7fffffff
310 #else
311 #error "Can't define SHORT_MAX"
312 #endif /* (SIZEOF_SHORT == 2) || ... */
313 #endif /* !defined(SHORT_MAX) */
314 
315 #ifndef TIME_MAX
316 
317 #if (SIZEOF_TIME_T == SIZEOF_INT)
318 #define TIME_MAX ((time_t)INT_MAX)
319 #elif (SIZEOF_TIME_T == SIZEOF_LONG)
320 #define TIME_MAX ((time_t)LONG_MAX)
321 #elif (SIZEOF_TIME_T == 8)
322 #define TIME_MAX ((time_t)INT64_MAX)
323 #else
324 #error "Can't define TIME_MAX"
325 #endif /* (SIZEOF_TIME_T == SIZEOF_INT) || ... */
326 
327 #endif /* !defined(TIME_MAX) */
328 
329 #ifndef TIME_MIN
330 
331 #if (SIZEOF_TIME_T == SIZEOF_INT)
332 #define TIME_MIN ((time_t)INT_MIN)
333 #elif (SIZEOF_TIME_T == SIZEOF_LONG)
334 #define TIME_MIN ((time_t)LONG_MIN)
335 #elif (SIZEOF_TIME_T == 8)
336 #define TIME_MIN ((time_t)INT64_MIN)
337 #else
338 #error "Can't define TIME_MIN"
339 #endif /* (SIZEOF_TIME_T == SIZEOF_INT) || ... */
340 
341 #endif /* !defined(TIME_MIN) */
342 
343 #ifndef SIZE_MAX
344 #if (SIZEOF_SIZE_T == 4)
345 #define SIZE_MAX UINT32_MAX
346 #elif (SIZEOF_SIZE_T == 8)
347 #define SIZE_MAX UINT64_MAX
348 #else
349 #error "Can't define SIZE_MAX"
350 #endif /* (SIZEOF_SIZE_T == 4) || ... */
351 #endif /* !defined(SIZE_MAX) */
352 
353 #ifdef _WIN32
354 # ifdef _WIN64
355 # define TOR_PRIuSZ PRIu64
356 # else
357 # define TOR_PRIuSZ PRIu32
358 # endif
359 #else
360 # define TOR_PRIuSZ "zu"
361 #endif
362 
363 #ifndef SSIZE_MAX
364 #if (SIZEOF_SIZE_T == 4)
365 #define SSIZE_MAX INT32_MAX
366 #elif (SIZEOF_SIZE_T == 8)
367 #define SSIZE_MAX INT64_MAX
368 #else
369 #error "Can't define SSIZE_MAX"
370 #endif /* (SIZEOF_SIZE_T == 4) || ... */
371 #endif /* !defined(SSIZE_MAX) */
372 
374 #define SSIZE_T_CEILING ((ssize_t)(SSIZE_MAX-16))
375 
376 #define SIZE_T_CEILING ((size_t)(SSIZE_MAX-16))
377 
378 #endif /* !defined(TOR_TORINT_H) */
379