torint.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007, 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 __TORINT_H
  11. #define __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. #ifdef MS_WINDOWS
  41. /* Windows likes to capitalize ssize_t. Charming. */
  42. #define ssize_t SSIZE_T
  43. #endif
  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
  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
  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 UINT32_MAX
  113. #define UINT32_MAX 0xffffffffu
  114. #endif
  115. #ifndef INT32_MAX
  116. #define INT32_MAX 0x7fffffffu
  117. #endif
  118. #endif
  119. #if (SIZEOF_LONG == 4)
  120. #ifndef HAVE_INT32_T
  121. typedef signed long int32_t;
  122. #define HAVE_INT32_T
  123. #endif
  124. #ifndef HAVE_UINT32_T
  125. typedef unsigned long uint32_t;
  126. #define HAVE_UINT32_T
  127. #ifndef UINT32_MAX
  128. #define UINT32_MAX 0xfffffffful
  129. #endif
  130. #endif
  131. #elif (SIZEOF_LONG == 8)
  132. #ifndef HAVE_INT64_T
  133. typedef signed long int64_t;
  134. #define HAVE_INT64_T
  135. #endif
  136. #ifndef HAVE_UINT32_T
  137. typedef unsigned long uint64_t;
  138. #define HAVE_UINT32_T
  139. #endif
  140. #ifndef UINT64_MAX
  141. #define UINT64_MAX 0xfffffffffffffffful
  142. #endif
  143. #endif
  144. #if (SIZEOF_LONG_LONG == 8)
  145. #ifndef HAVE_INT64_T
  146. typedef signed long long int64_t;
  147. #define HAVE_INT64_T
  148. #endif
  149. #ifndef HAVE_UINT64_T
  150. typedef unsigned long long uint64_t;
  151. #define HAVE_UINT64_T
  152. #endif
  153. #ifndef UINT64_MAX
  154. #define UINT64_MAX 0xffffffffffffffffull
  155. #endif
  156. #ifndef INT64_MAX
  157. #define INT64_MAX 0x7fffffffffffffffll
  158. #endif
  159. #endif
  160. #if (SIZEOF___INT64 == 8)
  161. #ifndef HAVE_INT64_T
  162. typedef signed __int64 int64_t;
  163. #define HAVE_INT64_T
  164. #endif
  165. #ifndef HAVE_UINT64_T
  166. typedef unsigned __int64 uint64_t;
  167. #define HAVE_UINT64_T
  168. #endif
  169. #ifndef UINT64_MAX
  170. #define UINT64_MAX 0xffffffffffffffffui64
  171. #endif
  172. #ifndef INT64_MAX
  173. #define INT64_MAX 0x7fffffffffffffffi64
  174. #endif
  175. #endif
  176. #if (SIZEOF_VOID_P > 4 && SIZEOF_VOID_P <= 8)
  177. #ifndef HAVE_INTPTR_T
  178. typedef int64_t intptr_t;
  179. #endif
  180. #ifndef HAVE_UINTPTR_T
  181. typedef uint64_t uintptr_t;
  182. #endif
  183. #elif (SIZEOF_VOID_P > 2 && SIZEOF_VOID_P <= 4)
  184. #ifndef HAVE_INTPTR_T
  185. typedef int32_t intptr_t;
  186. #endif
  187. #ifndef HAVE_UINTPTR_T
  188. typedef uint32_t uintptr_t;
  189. #endif
  190. #else
  191. #error "void * is either >8 bytes or <= 2. In either case, I am confused."
  192. #endif
  193. #ifndef HAVE_INT8_T
  194. #error "Missing type int8_t"
  195. #endif
  196. #ifndef HAVE_UINT8_T
  197. #error "Missing type uint8_t"
  198. #endif
  199. #ifndef HAVE_INT16_T
  200. #error "Missing type int16_t"
  201. #endif
  202. #ifndef HAVE_UINT16_T
  203. #error "Missing type uint16_t"
  204. #endif
  205. #ifndef HAVE_INT32_T
  206. #error "Missing type int32_t"
  207. #endif
  208. #ifndef HAVE_UINT32_T
  209. #error "Missing type uint32_t"
  210. #endif
  211. #ifndef HAVE_INT64_T
  212. #error "Missing type int64_t"
  213. #endif
  214. #ifndef HAVE_UINT64_T
  215. #error "Missing type uint64_t"
  216. #endif
  217. /* This assumes a sane (2's-complement) representation. But if you
  218. * aren't 2's complement, and you don't define LONG_MAX, then you're so
  219. * bizarre that I want nothing to do with you. */
  220. #ifndef USING_TWOS_COMPLEMENT
  221. #error "Seems that your platform doesn't use 2's complement arithmetic. Argh."
  222. #endif
  223. #ifndef LONG_MAX
  224. #if (SIZEOF_LONG == 4)
  225. #define LONG_MAX 0x7fffffffL
  226. #elif (SIZEOF_LONG == 8)
  227. #define LONG_MAX 0x7fffffffffffffffL
  228. #else
  229. #error "Can't define LONG_MAX"
  230. #endif
  231. #endif
  232. #ifndef INT_MAX
  233. #if (SIZEOF_INT == 4)
  234. #define INT_MAX 0x7fffffffL
  235. #elif (SIZEOF_INT == 8)
  236. #define INT_MAX 0x7fffffffffffffffL
  237. #else
  238. #error "Can't define INT_MAX"
  239. #endif
  240. #endif
  241. #ifndef UINT_MAX
  242. #if (SIZEOF_INT == 2)
  243. #define UINT_MAX 0xffffu
  244. #elif (SIZEOF_INT == 4)
  245. #define UINT_MAX 0xffffffffu
  246. #elif (SIZEOF_INT == 8)
  247. #define UINT_MAX 0xffffffffffffffffu
  248. #else
  249. #error "Can't define UINT_MAX"
  250. #endif
  251. #endif
  252. #ifndef TIME_MAX
  253. #ifdef TIME_T_IS_SIGNED
  254. #if (SIZEOF_TIME_T == SIZEOF_INT)
  255. #define TIME_MAX ((time_t)INT_MAX)
  256. #elif (SIZEOF_TIME_T == SIZEOF_LONG)
  257. #define TIME_MAX ((time_t)LONG_MAX)
  258. #else
  259. #error "Can't define (signed) TIME_MAX"
  260. #endif
  261. #else
  262. /* Unsigned case */
  263. #if (SIZEOF_TIME_T == 4)
  264. #define TIME_MAX ((time_t)UINT32_MAX)
  265. #elif (SIZEOF_TIME_T == 8)
  266. #define TIME_MAX ((time_t)UINT64_MAX)
  267. #else
  268. #error "Can't define (unsigned) TIME_MAX"
  269. #endif
  270. #endif /* time_t_is_signed */
  271. #endif /* ifndef(TIME_MAX) */
  272. #ifndef SIZE_T_MAX
  273. #if (SIZEOF_SIZE_T == 4)
  274. #define SIZE_T_MAX UINT32_MAX
  275. #elif (SIZEOF_SIZE_T == 8)
  276. #define SIZE_T_MAX UINT64_MAX
  277. #else
  278. #error "Can't define SIZE_T_MAX"
  279. #endif
  280. #endif
  281. /* Any size_t larger than this amount is likely to be an underflow. */
  282. #define SIZE_T_CEILING (sizeof(char)<<(sizeof(size_t)*8 - 1))
  283. #endif /* __TORINT_H */