torint.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, 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 UINT16_MAX
  107. #define UINT16_MAX 0xffffu
  108. #endif
  109. #ifndef INT16_MAX
  110. #define INT16_MAX 0x7fff
  111. #endif
  112. #ifndef INT16_MIN
  113. #define INT16_MIN (-INT16_MAX-1)
  114. #endif
  115. #ifndef UINT32_MAX
  116. #define UINT32_MAX 0xffffffffu
  117. #endif
  118. #ifndef INT32_MAX
  119. #define INT32_MAX 0x7fffffff
  120. #endif
  121. #ifndef INT32_MIN
  122. #define INT32_MIN (-2147483647-1)
  123. #endif
  124. #endif
  125. #if (SIZEOF_LONG == 4)
  126. #ifndef HAVE_INT32_T
  127. typedef signed long int32_t;
  128. #define HAVE_INT32_T
  129. #endif
  130. #ifndef HAVE_UINT32_T
  131. typedef unsigned long uint32_t;
  132. #define HAVE_UINT32_T
  133. #ifndef UINT32_MAX
  134. #define UINT32_MAX 0xfffffffful
  135. #endif
  136. #endif
  137. #elif (SIZEOF_LONG == 8)
  138. #ifndef HAVE_INT64_T
  139. typedef signed long int64_t;
  140. #define HAVE_INT64_T
  141. #endif
  142. #ifndef HAVE_UINT32_T
  143. typedef unsigned long uint64_t;
  144. #define HAVE_UINT32_T
  145. #endif
  146. #ifndef UINT64_MAX
  147. #define UINT64_MAX 0xfffffffffffffffful
  148. #endif
  149. #endif
  150. #if (SIZEOF_LONG_LONG == 8)
  151. #ifndef HAVE_INT64_T
  152. typedef signed long long int64_t;
  153. #define HAVE_INT64_T
  154. #endif
  155. #ifndef HAVE_UINT64_T
  156. typedef unsigned long long uint64_t;
  157. #define HAVE_UINT64_T
  158. #endif
  159. #ifndef UINT64_MAX
  160. #define UINT64_MAX 0xffffffffffffffffull
  161. #endif
  162. #ifndef INT64_MAX
  163. #define INT64_MAX 0x7fffffffffffffffll
  164. #endif
  165. #endif
  166. #if (SIZEOF___INT64 == 8)
  167. #ifndef HAVE_INT64_T
  168. typedef signed __int64 int64_t;
  169. #define HAVE_INT64_T
  170. #endif
  171. #ifndef HAVE_UINT64_T
  172. typedef unsigned __int64 uint64_t;
  173. #define HAVE_UINT64_T
  174. #endif
  175. #ifndef UINT64_MAX
  176. #define UINT64_MAX 0xffffffffffffffffui64
  177. #endif
  178. #ifndef INT64_MAX
  179. #define INT64_MAX 0x7fffffffffffffffi64
  180. #endif
  181. #endif
  182. #ifndef INT64_MIN
  183. #define INT64_MIN ((- INT64_MAX) - 1)
  184. #endif
  185. #ifndef SIZE_MAX
  186. #if SIZEOF_SIZE_T == 8
  187. #define SIZE_MAX UINT64_MAX
  188. #elif SIZEOF_SIZE_T == 4
  189. #define SIZE_MAX UINT32_MAX
  190. #else
  191. #error "Can't define SIZE_MAX"
  192. #endif
  193. #endif
  194. #ifndef HAVE_SSIZE_T
  195. #if SIZEOF_SIZE_T == 8
  196. typedef int64_t ssize_t;
  197. #elif SIZEOF_SIZE_T == 4
  198. typedef int32_t ssize_t;
  199. #else
  200. #error "Can't define ssize_t."
  201. #endif
  202. #endif
  203. #if (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8)
  204. #ifndef HAVE_INTPTR_T
  205. typedef int64_t intptr_t;
  206. #define SIZEOF_INTPTR_T 8
  207. #endif
  208. #ifndef HAVE_UINTPTR_T
  209. typedef uint64_t uintptr_t;
  210. #define SIZEOF_UINTPTR_T 8
  211. #endif
  212. #elif (SIZEOF_VOID_P > 2 && SIZEOF_VOID_P <= 4)
  213. #ifndef HAVE_INTPTR_T
  214. typedef int32_t intptr_t;
  215. #define SIZEOF_INTPTR_T 4
  216. #endif
  217. #ifndef HAVE_UINTPTR_T
  218. typedef uint32_t uintptr_t;
  219. #define SIZEOF_UINTPTR_T 4
  220. #endif
  221. #else
  222. #error "void * is either >8 bytes or <= 2. In either case, I am confused."
  223. #endif
  224. #ifndef HAVE_INT8_T
  225. #error "Missing type int8_t"
  226. #endif
  227. #ifndef HAVE_UINT8_T
  228. #error "Missing type uint8_t"
  229. #endif
  230. #ifndef HAVE_INT16_T
  231. #error "Missing type int16_t"
  232. #endif
  233. #ifndef HAVE_UINT16_T
  234. #error "Missing type uint16_t"
  235. #endif
  236. #ifndef HAVE_INT32_T
  237. #error "Missing type int32_t"
  238. #endif
  239. #ifndef HAVE_UINT32_T
  240. #error "Missing type uint32_t"
  241. #endif
  242. #ifndef HAVE_INT64_T
  243. #error "Missing type int64_t"
  244. #endif
  245. #ifndef HAVE_UINT64_T
  246. #error "Missing type uint64_t"
  247. #endif
  248. /* This assumes a sane (2's-complement) representation. But if you
  249. * aren't 2's complement, and you don't define LONG_MAX, then you're so
  250. * bizarre that I want nothing to do with you. */
  251. #ifndef USING_TWOS_COMPLEMENT
  252. #error "Seems that your platform doesn't use 2's complement arithmetic. Argh."
  253. #endif
  254. #ifndef LONG_MAX
  255. #if (SIZEOF_LONG == 4)
  256. #define LONG_MAX 0x7fffffffL
  257. #elif (SIZEOF_LONG == 8)
  258. #define LONG_MAX 0x7fffffffffffffffL
  259. #else
  260. #error "Can't define LONG_MAX"
  261. #endif
  262. #endif
  263. #ifndef INT_MAX
  264. #if (SIZEOF_INT == 4)
  265. #define INT_MAX 0x7fffffffL
  266. #elif (SIZEOF_INT == 8)
  267. #define INT_MAX 0x7fffffffffffffffL
  268. #else
  269. #error "Can't define INT_MAX"
  270. #endif
  271. #endif
  272. #ifndef UINT_MAX
  273. #if (SIZEOF_INT == 2)
  274. #define UINT_MAX 0xffffu
  275. #elif (SIZEOF_INT == 4)
  276. #define UINT_MAX 0xffffffffu
  277. #elif (SIZEOF_INT == 8)
  278. #define UINT_MAX 0xffffffffffffffffu
  279. #else
  280. #error "Can't define UINT_MAX"
  281. #endif
  282. #endif
  283. #ifndef SHORT_MAX
  284. #if (SIZEOF_SHORT == 2)
  285. #define SHORT_MAX 0x7fff
  286. #elif (SIZEOF_SHORT == 4)
  287. #define SHORT_MAX 0x7fffffff
  288. #else
  289. #error "Can't define SHORT_MAX"
  290. #endif
  291. #endif
  292. #ifndef TIME_MAX
  293. #if (SIZEOF_TIME_T == SIZEOF_INT)
  294. #define TIME_MAX ((time_t)INT_MAX)
  295. #elif (SIZEOF_TIME_T == SIZEOF_LONG)
  296. #define TIME_MAX ((time_t)LONG_MAX)
  297. #elif (SIZEOF_TIME_T == 8)
  298. #define TIME_MAX ((time_t)INT64_MAX)
  299. #else
  300. #error "Can't define TIME_MAX"
  301. #endif
  302. #endif /* ifndef(TIME_MAX) */
  303. #ifndef TIME_MIN
  304. #if (SIZEOF_TIME_T == SIZEOF_INT)
  305. #define TIME_MIN ((time_t)INT_MIN)
  306. #elif (SIZEOF_TIME_T == SIZEOF_LONG)
  307. #define TIME_MIN ((time_t)LONG_MIN)
  308. #elif (SIZEOF_TIME_T == 8)
  309. #define TIME_MIN ((time_t)INT64_MIN)
  310. #else
  311. #error "Can't define TIME_MIN"
  312. #endif
  313. #endif /* ifndef(TIME_MIN) */
  314. #ifndef SIZE_MAX
  315. #if (SIZEOF_SIZE_T == 4)
  316. #define SIZE_MAX UINT32_MAX
  317. #elif (SIZEOF_SIZE_T == 8)
  318. #define SIZE_MAX UINT64_MAX
  319. #else
  320. #error "Can't define SIZE_MAX"
  321. #endif
  322. #endif
  323. #ifndef SSIZE_MAX
  324. #if (SIZEOF_SIZE_T == 4)
  325. #define SSIZE_MAX INT32_MAX
  326. #elif (SIZEOF_SIZE_T == 8)
  327. #define SSIZE_MAX INT64_MAX
  328. #else
  329. #error "Can't define SSIZE_MAX"
  330. #endif
  331. #endif
  332. /** Any ssize_t larger than this amount is likely to be an underflow. */
  333. #define SSIZE_T_CEILING ((ssize_t)(SSIZE_MAX-16))
  334. /** Any size_t larger than this amount is likely to be an underflow. */
  335. #define SIZE_T_CEILING ((size_t)(SSIZE_MAX-16))
  336. #endif /* __TORINT_H */