torint.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <limits.h>
  20. #ifdef HAVE_SYS_TYPES_H
  21. #include <sys/types.h>
  22. #endif
  23. #ifdef HAVE_SYS_LIMITS_H
  24. #include <sys/limits.h>
  25. #endif
  26. #ifndef SIZE_MAX
  27. #if SIZEOF_SIZE_T == 8
  28. #define SIZE_MAX UINT64_MAX
  29. #elif SIZEOF_SIZE_T == 4
  30. #define SIZE_MAX UINT32_MAX
  31. #else
  32. #error "Can't define SIZE_MAX"
  33. #endif /* SIZEOF_SIZE_T == 8 || ... */
  34. #endif /* !defined(SIZE_MAX) */
  35. #ifndef HAVE_SSIZE_T
  36. #if SIZEOF_SIZE_T == 8
  37. typedef int64_t ssize_t;
  38. #elif SIZEOF_SIZE_T == 4
  39. typedef int32_t ssize_t;
  40. #else
  41. #error "Can't define ssize_t."
  42. #endif /* SIZEOF_SIZE_T == 8 || ... */
  43. #endif /* !defined(HAVE_SSIZE_T) */
  44. /* This assumes a sane (2's-complement) representation. But if you
  45. * aren't 2's complement, and you don't define LONG_MAX, then you're so
  46. * bizarre that I want nothing to do with you. */
  47. #ifndef USING_TWOS_COMPLEMENT
  48. #error "Seems that your platform doesn't use 2's complement arithmetic. Argh."
  49. #endif
  50. #ifndef TIME_MAX
  51. #if (SIZEOF_TIME_T == SIZEOF_INT)
  52. #define TIME_MAX ((time_t)INT_MAX)
  53. #elif (SIZEOF_TIME_T == SIZEOF_LONG)
  54. #define TIME_MAX ((time_t)LONG_MAX)
  55. #elif (SIZEOF_TIME_T == 8)
  56. #define TIME_MAX ((time_t)INT64_MAX)
  57. #else
  58. #error "Can't define TIME_MAX"
  59. #endif /* (SIZEOF_TIME_T == SIZEOF_INT) || ... */
  60. #endif /* !defined(TIME_MAX) */
  61. #ifndef TIME_MIN
  62. #if (SIZEOF_TIME_T == SIZEOF_INT)
  63. #define TIME_MIN ((time_t)INT_MIN)
  64. #elif (SIZEOF_TIME_T == SIZEOF_LONG)
  65. #define TIME_MIN ((time_t)LONG_MIN)
  66. #elif (SIZEOF_TIME_T == 8)
  67. #define TIME_MIN ((time_t)INT64_MIN)
  68. #else
  69. #error "Can't define TIME_MIN"
  70. #endif /* (SIZEOF_TIME_T == SIZEOF_INT) || ... */
  71. #endif /* !defined(TIME_MIN) */
  72. #ifndef SIZE_MAX
  73. #if (SIZEOF_SIZE_T == 4)
  74. #define SIZE_MAX UINT32_MAX
  75. #elif (SIZEOF_SIZE_T == 8)
  76. #define SIZE_MAX UINT64_MAX
  77. #else
  78. #error "Can't define SIZE_MAX"
  79. #endif /* (SIZEOF_SIZE_T == 4) || ... */
  80. #endif /* !defined(SIZE_MAX) */
  81. #ifdef _WIN32
  82. # ifdef _WIN64
  83. # define TOR_PRIuSZ PRIu64
  84. # else
  85. # define TOR_PRIuSZ PRIu32
  86. # endif
  87. #else
  88. # define TOR_PRIuSZ "zu"
  89. #endif
  90. #ifndef SSIZE_MAX
  91. #if (SIZEOF_SIZE_T == 4)
  92. #define SSIZE_MAX INT32_MAX
  93. #elif (SIZEOF_SIZE_T == 8)
  94. #define SSIZE_MAX INT64_MAX
  95. #else
  96. #error "Can't define SSIZE_MAX"
  97. #endif /* (SIZEOF_SIZE_T == 4) || ... */
  98. #endif /* !defined(SSIZE_MAX) */
  99. /** Any ssize_t larger than this amount is likely to be an underflow. */
  100. #define SSIZE_T_CEILING ((ssize_t)(SSIZE_MAX-16))
  101. /** Any size_t larger than this amount is likely to be an underflow. */
  102. #define SIZE_T_CEILING ((size_t)(SSIZE_MAX-16))
  103. #endif /* !defined(TOR_TORINT_H) */