torint.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2011, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file torint.h
  7. * \brief Header file to define uint32_t and friends
  8. **/
  9. #ifndef _TOR_TORINT_H
  10. #define _TOR_TORINT_H
  11. #include "orconfig.h"
  12. #ifdef HAVE_STDINT_H
  13. #include <stdint.h>
  14. #endif
  15. #ifdef HAVE_SYS_TYPES_H
  16. #include <sys/types.h>
  17. #endif
  18. #ifdef HAVE_LIMITS_H
  19. #include <limits.h>
  20. #endif
  21. #ifdef HAVE_SYS_LIMITS_H
  22. #include <sys/limits.h>
  23. #endif
  24. #ifdef HAVE_MACHINE_LIMITS_H
  25. #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
  26. /* FreeBSD has a bug where it complains that this file is obsolete,
  27. and I should migrate to using sys/limits. It complains even when
  28. I include both.
  29. __FreeBSD_kernel__ is defined by Debian GNU/kFreeBSD which
  30. does the same thing (but doesn't defined __FreeBSD__).
  31. */
  32. #include <machine/limits.h>
  33. #endif
  34. #endif
  35. #ifdef HAVE_INTTYPES_H
  36. #include <inttypes.h>
  37. #endif
  38. #if (SIZEOF_INT8_T != 0)
  39. #define HAVE_INT8_T
  40. #endif
  41. #if (SIZEOF_INT16_T != 0)
  42. #define HAVE_INT16_T
  43. #endif
  44. #if (SIZEOF_INT32_T != 0)
  45. #define HAVE_INT32_T
  46. #endif
  47. #if (SIZEOF_INT64_T != 0)
  48. #define HAVE_INT64_T
  49. #endif
  50. #if (SIZEOF_UINT8_T != 0)
  51. #define HAVE_UINT8_T
  52. #endif
  53. #if (SIZEOF_UINT16_T != 0)
  54. #define HAVE_UINT16_T
  55. #endif
  56. #if (SIZEOF_UINT32_T != 0)
  57. #define HAVE_UINT32_T
  58. #endif
  59. #if (SIZEOF_UINT64_T != 0)
  60. #define HAVE_UINT64_T
  61. #endif
  62. #if (SIZEOF_INTPTR_T != 0)
  63. #define HAVE_INTPTR_T
  64. #endif
  65. #if (SIZEOF_UINTPTR_T != 0)
  66. #define HAVE_UINTPTR_T
  67. #endif
  68. #if (SIZEOF_CHAR == 1)
  69. #ifndef HAVE_INT8_T
  70. typedef signed char int8_t;
  71. #define HAVE_INT8_T
  72. #endif
  73. #ifndef HAVE_UINT8_T
  74. typedef unsigned char uint8_t;
  75. #define HAVE_UINT8_T
  76. #endif
  77. #endif
  78. #if (SIZEOF_SHORT == 2)
  79. #ifndef HAVE_INT16_T
  80. typedef signed short int16_t;
  81. #define HAVE_INT16_T
  82. #endif
  83. #ifndef HAVE_UINT16_T
  84. typedef unsigned short uint16_t;
  85. #define HAVE_UINT16_T
  86. #endif
  87. #endif
  88. #if (SIZEOF_INT == 2)
  89. #ifndef HAVE_INT16_T
  90. typedef signed int int16_t;
  91. #define HAVE_INT16_T
  92. #endif
  93. #ifndef HAVE_UINT16_T
  94. typedef unsigned int uint16_t;
  95. #define HAVE_UINT16_T
  96. #endif
  97. #elif (SIZEOF_INT == 4)
  98. #ifndef HAVE_INT32_T
  99. typedef signed int int32_t;
  100. #define HAVE_INT32_T
  101. #endif
  102. #ifndef HAVE_UINT32_T
  103. typedef unsigned int uint32_t;
  104. #define HAVE_UINT32_T
  105. #endif
  106. #ifndef UINT32_MAX
  107. #define UINT32_MAX 0xffffffffu
  108. #endif
  109. #ifndef INT32_MAX
  110. #define INT32_MAX 0x7fffffff
  111. #endif
  112. #ifndef INT32_MIN
  113. #define INT32_MIN (-2147483647-1)
  114. #endif
  115. #endif
  116. #if (SIZEOF_LONG == 4)
  117. #ifndef HAVE_INT32_T
  118. typedef signed long int32_t;
  119. #define HAVE_INT32_T
  120. #endif
  121. #ifndef HAVE_UINT32_T
  122. typedef unsigned long uint32_t;
  123. #define HAVE_UINT32_T
  124. #ifndef UINT32_MAX
  125. #define UINT32_MAX 0xfffffffful
  126. #endif
  127. #endif
  128. #elif (SIZEOF_LONG == 8)
  129. #ifndef HAVE_INT64_T
  130. typedef signed long int64_t;
  131. #define HAVE_INT64_T
  132. #endif
  133. #ifndef HAVE_UINT32_T
  134. typedef unsigned long uint64_t;
  135. #define HAVE_UINT32_T
  136. #endif
  137. #ifndef UINT64_MAX
  138. #define UINT64_MAX 0xfffffffffffffffful
  139. #endif
  140. #endif
  141. #if (SIZEOF_LONG_LONG == 8)
  142. #ifndef HAVE_INT64_T
  143. typedef signed long long int64_t;
  144. #define HAVE_INT64_T
  145. #endif
  146. #ifndef HAVE_UINT64_T
  147. typedef unsigned long long uint64_t;
  148. #define HAVE_UINT64_T
  149. #endif
  150. #ifndef UINT64_MAX
  151. #define UINT64_MAX 0xffffffffffffffffull
  152. #endif
  153. #ifndef INT64_MAX
  154. #define INT64_MAX 0x7fffffffffffffffll
  155. #endif
  156. #endif
  157. #if (SIZEOF___INT64 == 8)
  158. #ifndef HAVE_INT64_T
  159. typedef signed __int64 int64_t;
  160. #define HAVE_INT64_T
  161. #endif
  162. #ifndef HAVE_UINT64_T
  163. typedef unsigned __int64 uint64_t;
  164. #define HAVE_UINT64_T
  165. #endif
  166. #ifndef UINT64_MAX
  167. #define UINT64_MAX 0xffffffffffffffffui64
  168. #endif
  169. #ifndef INT64_MAX
  170. #define INT64_MAX 0x7fffffffffffffffi64
  171. #endif
  172. #endif
  173. #ifndef HAVE_SSIZE_T
  174. #if SIZEOF_SIZE_T == 8
  175. typedef int64_t ssize_t;
  176. #elif SIZEOF_SIZE_T == 4
  177. typedef int32_t ssize_t;
  178. #else
  179. #error "Can't define ssize_t."
  180. #endif
  181. #endif
  182. #if (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8)
  183. #ifndef HAVE_INTPTR_T
  184. typedef int64_t intptr_t;
  185. #endif
  186. #ifndef HAVE_UINTPTR_T
  187. typedef uint64_t uintptr_t;
  188. #endif
  189. #elif (SIZEOF_VOID_P > 2 && SIZEOF_VOID_P <= 4)
  190. #ifndef HAVE_INTPTR_T
  191. typedef int32_t intptr_t;
  192. #endif
  193. #ifndef HAVE_UINTPTR_T
  194. typedef uint32_t uintptr_t;
  195. #endif
  196. #else
  197. #error "void * is either >8 bytes or <= 2. In either case, I am confused."
  198. #endif
  199. #ifndef HAVE_INT8_T
  200. #error "Missing type int8_t"
  201. #endif
  202. #ifndef HAVE_UINT8_T
  203. #error "Missing type uint8_t"
  204. #endif
  205. #ifndef HAVE_INT16_T
  206. #error "Missing type int16_t"
  207. #endif
  208. #ifndef HAVE_UINT16_T
  209. #error "Missing type uint16_t"
  210. #endif
  211. #ifndef HAVE_INT32_T
  212. #error "Missing type int32_t"
  213. #endif
  214. #ifndef HAVE_UINT32_T
  215. #error "Missing type uint32_t"
  216. #endif
  217. #ifndef HAVE_INT64_T
  218. #error "Missing type int64_t"
  219. #endif
  220. #ifndef HAVE_UINT64_T
  221. #error "Missing type uint64_t"
  222. #endif
  223. /* This assumes a sane (2's-complement) representation. But if you
  224. * aren't 2's complement, and you don't define LONG_MAX, then you're so
  225. * bizarre that I want nothing to do with you. */
  226. #ifndef USING_TWOS_COMPLEMENT
  227. #error "Seems that your platform doesn't use 2's complement arithmetic. Argh."
  228. #endif
  229. #ifndef LONG_MAX
  230. #if (SIZEOF_LONG == 4)
  231. #define LONG_MAX 0x7fffffffL
  232. #elif (SIZEOF_LONG == 8)
  233. #define LONG_MAX 0x7fffffffffffffffL
  234. #else
  235. #error "Can't define LONG_MAX"
  236. #endif
  237. #endif
  238. #ifndef INT_MAX
  239. #if (SIZEOF_INT == 4)
  240. #define INT_MAX 0x7fffffffL
  241. #elif (SIZEOF_INT == 8)
  242. #define INT_MAX 0x7fffffffffffffffL
  243. #else
  244. #error "Can't define INT_MAX"
  245. #endif
  246. #endif
  247. #ifndef UINT_MAX
  248. #if (SIZEOF_INT == 2)
  249. #define UINT_MAX 0xffffu
  250. #elif (SIZEOF_INT == 4)
  251. #define UINT_MAX 0xffffffffu
  252. #elif (SIZEOF_INT == 8)
  253. #define UINT_MAX 0xffffffffffffffffu
  254. #else
  255. #error "Can't define UINT_MAX"
  256. #endif
  257. #endif
  258. #ifndef SHORT_MAX
  259. #if (SIZEOF_SHORT == 2)
  260. #define SHORT_MAX 0x7fff
  261. #elif (SIZEOF_SHORT == 4)
  262. #define SHORT_MAX 0x7fffffff
  263. #else
  264. #error "Can't define SHORT_MAX"
  265. #endif
  266. #endif
  267. #ifndef TIME_MAX
  268. #ifdef TIME_T_IS_SIGNED
  269. #if (SIZEOF_TIME_T == SIZEOF_INT)
  270. #define TIME_MAX ((time_t)INT_MAX)
  271. #elif (SIZEOF_TIME_T == SIZEOF_LONG)
  272. #define TIME_MAX ((time_t)LONG_MAX)
  273. #elif (SIZEOF_TIME_T == 8)
  274. #define TIME_MAX ((time_t)INT64_MAX)
  275. #else
  276. #error "Can't define (signed) TIME_MAX"
  277. #endif
  278. #else
  279. /* Unsigned case */
  280. #if (SIZEOF_TIME_T == 4)
  281. #define TIME_MAX ((time_t)UINT32_MAX)
  282. #elif (SIZEOF_TIME_T == 8)
  283. #define TIME_MAX ((time_t)UINT64_MAX)
  284. #else
  285. #error "Can't define (unsigned) TIME_MAX"
  286. #endif
  287. #endif /* time_t_is_signed */
  288. #endif /* ifndef(TIME_MAX) */
  289. #ifndef SIZE_T_MAX
  290. #if (SIZEOF_SIZE_T == 4)
  291. #define SIZE_T_MAX UINT32_MAX
  292. #elif (SIZEOF_SIZE_T == 8)
  293. #define SIZE_T_MAX UINT64_MAX
  294. #else
  295. #error "Can't define SIZE_T_MAX"
  296. #endif
  297. #endif
  298. #ifndef SSIZE_T_MAX
  299. #if (SIZEOF_SIZE_T == 4)
  300. #define SSIZE_T_MAX INT32_MAX
  301. #elif (SIZEOF_SIZE_T == 8)
  302. #define SSIZE_T_MAX INT64_MAX
  303. #else
  304. #error "Can't define SSIZE_T_MAX"
  305. #endif
  306. #endif
  307. /* Any size_t larger than this amount is likely to be an underflow. */
  308. #define SIZE_T_CEILING (SSIZE_T_MAX-16)
  309. #endif /* __TORINT_H */