Back to index

tor  0.2.3.18-rc
torint.h
Go to the documentation of this file.
00001 /* Copyright (c) 2003, Roger Dingledine
00002  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
00003  * Copyright (c) 2007-2012, The Tor Project, Inc. */
00004 /* See LICENSE for licensing information */
00005 
00011 #ifndef _TOR_TORINT_H
00012 #define _TOR_TORINT_H
00013 
00014 #include "orconfig.h"
00015 
00016 #ifdef HAVE_STDINT_H
00017 #include <stdint.h>
00018 #endif
00019 #ifdef HAVE_SYS_TYPES_H
00020 #include <sys/types.h>
00021 #endif
00022 #ifdef HAVE_LIMITS_H
00023 #include <limits.h>
00024 #endif
00025 #ifdef HAVE_SYS_LIMITS_H
00026 #include <sys/limits.h>
00027 #endif
00028 #ifdef HAVE_MACHINE_LIMITS_H
00029 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
00030   /* FreeBSD has a bug where it complains that this file is obsolete,
00031      and I should migrate to using sys/limits. It complains even when
00032      I include both.
00033      __FreeBSD_kernel__ is defined by Debian GNU/kFreeBSD which
00034       does the same thing (but doesn't defined __FreeBSD__).
00035      */
00036 #include <machine/limits.h>
00037 #endif
00038 #endif
00039 #ifdef HAVE_INTTYPES_H
00040 #include <inttypes.h>
00041 #endif
00042 
00043 #if (SIZEOF_INT8_T != 0)
00044 #define HAVE_INT8_T
00045 #endif
00046 #if (SIZEOF_INT16_T != 0)
00047 #define HAVE_INT16_T
00048 #endif
00049 #if (SIZEOF_INT32_T != 0)
00050 #define HAVE_INT32_T
00051 #endif
00052 #if (SIZEOF_INT64_T != 0)
00053 #define HAVE_INT64_T
00054 #endif
00055 #if (SIZEOF_UINT8_T != 0)
00056 #define HAVE_UINT8_T
00057 #endif
00058 #if (SIZEOF_UINT16_T != 0)
00059 #define HAVE_UINT16_T
00060 #endif
00061 #if (SIZEOF_UINT32_T != 0)
00062 #define HAVE_UINT32_T
00063 #endif
00064 #if (SIZEOF_UINT64_T != 0)
00065 #define HAVE_UINT64_T
00066 #endif
00067 #if (SIZEOF_INTPTR_T != 0)
00068 #define HAVE_INTPTR_T
00069 #endif
00070 #if (SIZEOF_UINTPTR_T != 0)
00071 #define HAVE_UINTPTR_T
00072 #endif
00073 
00074 #if (SIZEOF_CHAR == 1)
00075 #ifndef HAVE_INT8_T
00076 typedef signed char int8_t;
00077 #define HAVE_INT8_T
00078 #endif
00079 #ifndef HAVE_UINT8_T
00080 typedef unsigned char uint8_t;
00081 #define HAVE_UINT8_T
00082 #endif
00083 #endif
00084 
00085 #if (SIZEOF_SHORT == 2)
00086 #ifndef HAVE_INT16_T
00087 typedef signed short int16_t;
00088 #define HAVE_INT16_T
00089 #endif
00090 #ifndef HAVE_UINT16_T
00091 typedef unsigned short uint16_t;
00092 #define HAVE_UINT16_T
00093 #endif
00094 #endif
00095 
00096 #if (SIZEOF_INT == 2)
00097 #ifndef HAVE_INT16_T
00098 typedef signed int int16_t;
00099 #define HAVE_INT16_T
00100 #endif
00101 #ifndef HAVE_UINT16_T
00102 typedef unsigned int uint16_t;
00103 #define HAVE_UINT16_T
00104 #endif
00105 #elif (SIZEOF_INT == 4)
00106 #ifndef HAVE_INT32_T
00107 typedef signed int int32_t;
00108 #define HAVE_INT32_T
00109 #endif
00110 #ifndef HAVE_UINT32_T
00111 typedef unsigned int uint32_t;
00112 #define HAVE_UINT32_T
00113 #endif
00114 #ifndef UINT16_MAX
00115 #define UINT16_MAX 0xffffu
00116 #endif
00117 #ifndef INT16_MAX
00118 #define INT16_MAX 0x7fff
00119 #endif
00120 #ifndef INT16_MIN
00121 #define INT16_MIN (-INT16_MAX-1)
00122 #endif
00123 #ifndef UINT32_MAX
00124 #define UINT32_MAX 0xffffffffu
00125 #endif
00126 #ifndef INT32_MAX
00127 #define INT32_MAX 0x7fffffff
00128 #endif
00129 #ifndef INT32_MIN
00130 #define INT32_MIN (-2147483647-1)
00131 #endif
00132 #endif
00133 
00134 #if (SIZEOF_LONG == 4)
00135 #ifndef HAVE_INT32_T
00136 typedef signed long int32_t;
00137 #define HAVE_INT32_T
00138 #endif
00139 #ifndef HAVE_UINT32_T
00140 typedef unsigned long uint32_t;
00141 #define HAVE_UINT32_T
00142 #ifndef UINT32_MAX
00143 #define UINT32_MAX 0xfffffffful
00144 #endif
00145 #endif
00146 #elif (SIZEOF_LONG == 8)
00147 #ifndef HAVE_INT64_T
00148 typedef signed long int64_t;
00149 #define HAVE_INT64_T
00150 #endif
00151 #ifndef HAVE_UINT32_T
00152 typedef unsigned long uint64_t;
00153 #define HAVE_UINT32_T
00154 #endif
00155 #ifndef UINT64_MAX
00156 #define UINT64_MAX 0xfffffffffffffffful
00157 #endif
00158 #endif
00159 
00160 #if (SIZEOF_LONG_LONG == 8)
00161 #ifndef HAVE_INT64_T
00162 typedef signed long long int64_t;
00163 #define HAVE_INT64_T
00164 #endif
00165 #ifndef HAVE_UINT64_T
00166 typedef unsigned long long uint64_t;
00167 #define HAVE_UINT64_T
00168 #endif
00169 #ifndef UINT64_MAX
00170 #define UINT64_MAX 0xffffffffffffffffull
00171 #endif
00172 #ifndef INT64_MAX
00173 #define INT64_MAX 0x7fffffffffffffffll
00174 #endif
00175 #endif
00176 
00177 #if (SIZEOF___INT64 == 8)
00178 #ifndef HAVE_INT64_T
00179 typedef signed __int64 int64_t;
00180 #define HAVE_INT64_T
00181 #endif
00182 #ifndef HAVE_UINT64_T
00183 typedef unsigned __int64 uint64_t;
00184 #define HAVE_UINT64_T
00185 #endif
00186 #ifndef UINT64_MAX
00187 #define UINT64_MAX 0xffffffffffffffffui64
00188 #endif
00189 #ifndef INT64_MAX
00190 #define INT64_MAX 0x7fffffffffffffffi64
00191 #endif
00192 #endif
00193 
00194 #ifndef SIZE_MAX
00195 #if SIZEOF_SIZE_T == 8
00196 #define SIZE_MAX UINT64_MAX
00197 #elif  SIZEOF_SIZE_T == 4
00198 #define SIZE_MAX UINT32_MAX
00199 #else
00200 #error "Can't define SIZE_MAX"
00201 #endif
00202 #endif
00203 
00204 #ifndef HAVE_SSIZE_T
00205 #if SIZEOF_SIZE_T == 8
00206 typedef int64_t ssize_t;
00207 #elif SIZEOF_SIZE_T == 4
00208 typedef int32_t ssize_t;
00209 #else
00210 #error "Can't define ssize_t."
00211 #endif
00212 #endif
00213 
00214 #if (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8)
00215 #ifndef HAVE_INTPTR_T
00216 typedef int64_t intptr_t;
00217 #endif
00218 #ifndef HAVE_UINTPTR_T
00219 typedef uint64_t uintptr_t;
00220 #endif
00221 #elif (SIZEOF_VOID_P > 2 && SIZEOF_VOID_P <= 4)
00222 #ifndef HAVE_INTPTR_T
00223 typedef int32_t intptr_t;
00224 #endif
00225 #ifndef HAVE_UINTPTR_T
00226 typedef uint32_t uintptr_t;
00227 #endif
00228 #else
00229 #error "void * is either >8 bytes or <= 2.  In either case, I am confused."
00230 #endif
00231 
00232 #ifndef HAVE_INT8_T
00233 #error "Missing type int8_t"
00234 #endif
00235 #ifndef HAVE_UINT8_T
00236 #error "Missing type uint8_t"
00237 #endif
00238 #ifndef HAVE_INT16_T
00239 #error "Missing type int16_t"
00240 #endif
00241 #ifndef HAVE_UINT16_T
00242 #error "Missing type uint16_t"
00243 #endif
00244 #ifndef HAVE_INT32_T
00245 #error "Missing type int32_t"
00246 #endif
00247 #ifndef HAVE_UINT32_T
00248 #error "Missing type uint32_t"
00249 #endif
00250 #ifndef HAVE_INT64_T
00251 #error "Missing type int64_t"
00252 #endif
00253 #ifndef HAVE_UINT64_T
00254 #error "Missing type uint64_t"
00255 #endif
00256 
00257 /* This assumes a sane (2's-complement) representation.  But if you
00258  * aren't 2's complement, and you don't define LONG_MAX, then you're so
00259  * bizarre that I want nothing to do with you. */
00260 #ifndef USING_TWOS_COMPLEMENT
00261 #error "Seems that your platform doesn't use 2's complement arithmetic. Argh."
00262 #endif
00263 #ifndef LONG_MAX
00264 #if (SIZEOF_LONG == 4)
00265 #define LONG_MAX 0x7fffffffL
00266 #elif (SIZEOF_LONG == 8)
00267 #define LONG_MAX 0x7fffffffffffffffL
00268 #else
00269 #error "Can't define LONG_MAX"
00270 #endif
00271 #endif
00272 
00273 #ifndef INT_MAX
00274 #if (SIZEOF_INT == 4)
00275 #define INT_MAX 0x7fffffffL
00276 #elif (SIZEOF_INT == 8)
00277 #define INT_MAX 0x7fffffffffffffffL
00278 #else
00279 #error "Can't define INT_MAX"
00280 #endif
00281 #endif
00282 
00283 #ifndef UINT_MAX
00284 #if (SIZEOF_INT == 2)
00285 #define UINT_MAX 0xffffu
00286 #elif (SIZEOF_INT == 4)
00287 #define UINT_MAX 0xffffffffu
00288 #elif (SIZEOF_INT == 8)
00289 #define UINT_MAX 0xffffffffffffffffu
00290 #else
00291 #error "Can't define UINT_MAX"
00292 #endif
00293 #endif
00294 
00295 #ifndef SHORT_MAX
00296 #if (SIZEOF_SHORT == 2)
00297 #define SHORT_MAX 0x7fff
00298 #elif (SIZEOF_SHORT == 4)
00299 #define SHORT_MAX 0x7fffffff
00300 #else
00301 #error "Can't define SHORT_MAX"
00302 #endif
00303 #endif
00304 
00305 #ifndef TIME_MAX
00306 
00307 #ifdef TIME_T_IS_SIGNED
00308 
00309 #if (SIZEOF_TIME_T == SIZEOF_INT)
00310 #define TIME_MAX ((time_t)INT_MAX)
00311 #elif (SIZEOF_TIME_T == SIZEOF_LONG)
00312 #define TIME_MAX ((time_t)LONG_MAX)
00313 #elif (SIZEOF_TIME_T == 8)
00314 #define TIME_MAX ((time_t)INT64_MAX)
00315 #else
00316 #error "Can't define (signed) TIME_MAX"
00317 #endif
00318 
00319 #else
00320 /* Unsigned case */
00321 #if (SIZEOF_TIME_T == 4)
00322 #define TIME_MAX ((time_t)UINT32_MAX)
00323 #elif (SIZEOF_TIME_T == 8)
00324 #define TIME_MAX ((time_t)UINT64_MAX)
00325 #else
00326 #error "Can't define (unsigned) TIME_MAX"
00327 #endif
00328 #endif /* time_t_is_signed */
00329 #endif /* ifndef(TIME_MAX) */
00330 
00331 #ifndef SIZE_T_MAX
00332 #if (SIZEOF_SIZE_T == 4)
00333 #define SIZE_T_MAX UINT32_MAX
00334 #elif (SIZEOF_SIZE_T == 8)
00335 #define SIZE_T_MAX UINT64_MAX
00336 #else
00337 #error "Can't define SIZE_T_MAX"
00338 #endif
00339 #endif
00340 
00341 #ifndef SSIZE_T_MAX
00342 #if (SIZEOF_SIZE_T == 4)
00343 #define SSIZE_T_MAX INT32_MAX
00344 #elif (SIZEOF_SIZE_T == 8)
00345 #define SSIZE_T_MAX INT64_MAX
00346 #else
00347 #error "Can't define SSIZE_T_MAX"
00348 #endif
00349 #endif
00350 
00352 #define SSIZE_T_CEILING ((ssize_t)(SSIZE_T_MAX-16))
00353 
00354 #define SIZE_T_CEILING  ((size_t)(SSIZE_T_MAX-16))
00355 
00356 #endif /* __TORINT_H */
00357