torint.h 8.5 KB

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