torint.h 6.7 KB

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