torint.h 6.2 KB

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