torint.h 8.4 KB

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