1 /* Copyright (c) 2003, Roger Dingledine 2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. 3 * Copyright (c) 2007-2021, The Tor Project, Inc. */ 4 /* See LICENSE for licensing information */ 5 6 /** 7 * \file torint.h 8 * 9 * \brief Integer definitions used throughout Tor. 10 **/ 11 12 #ifndef TOR_TORINT_H 13 #define TOR_TORINT_H 14 15 #include "orconfig.h" 16 17 #include <stdint.h> 18 #include <stdbool.h> 19 #include <limits.h> 20 21 #ifdef HAVE_SYS_TYPES_H 22 #include <sys/types.h> 23 #endif 24 #ifdef HAVE_SYS_LIMITS_H 25 #include <sys/limits.h> 26 #endif 27 28 #ifndef SIZE_MAX 29 #if SIZEOF_SIZE_T == 8 30 #define SIZE_MAX UINT64_MAX 31 #elif SIZEOF_SIZE_T == 4 32 #define SIZE_MAX UINT32_MAX 33 #else 34 #error "Can't define SIZE_MAX" 35 #endif /* SIZEOF_SIZE_T == 8 || ... */ 36 #endif /* !defined(SIZE_MAX) */ 37 38 #ifndef HAVE_SSIZE_T 39 #if SIZEOF_SIZE_T == 8 40 typedef int64_t ssize_t; 41 #elif SIZEOF_SIZE_T == 4 42 typedef int32_t ssize_t; 43 #else 44 #error "Can't define ssize_t." 45 #endif /* SIZEOF_SIZE_T == 8 || ... */ 46 #endif /* !defined(HAVE_SSIZE_T) */ 47 48 /* This assumes a sane (2's-complement) representation. But if you 49 * aren't 2's complement, and you don't define LONG_MAX, then you're so 50 * bizarre that I want nothing to do with you. */ 51 #ifndef USING_TWOS_COMPLEMENT 52 #error "Your platform doesn't use 2's complement arithmetic." 53 #endif 54 55 #ifndef TIME_MAX 56 57 #if (SIZEOF_TIME_T == SIZEOF_INT) 58 #define TIME_MAX ((time_t)INT_MAX) 59 #elif (SIZEOF_TIME_T == SIZEOF_LONG) 60 #define TIME_MAX ((time_t)LONG_MAX) 61 #elif (SIZEOF_TIME_T == 8) 62 #define TIME_MAX ((time_t)INT64_MAX) 63 #else 64 #error "Can't define TIME_MAX" 65 #endif /* (SIZEOF_TIME_T == SIZEOF_INT) || ... */ 66 67 #endif /* !defined(TIME_MAX) */ 68 69 #ifndef TIME_MIN 70 71 #if (SIZEOF_TIME_T == SIZEOF_INT) 72 #define TIME_MIN ((time_t)INT_MIN) 73 #elif (SIZEOF_TIME_T == SIZEOF_LONG) 74 #define TIME_MIN ((time_t)LONG_MIN) 75 #elif (SIZEOF_TIME_T == 8) 76 #define TIME_MIN ((time_t)INT64_MIN) 77 #else 78 #error "Can't define TIME_MIN" 79 #endif /* (SIZEOF_TIME_T == SIZEOF_INT) || ... */ 80 81 #endif /* !defined(TIME_MIN) */ 82 83 #ifndef SIZE_MAX 84 #if (SIZEOF_SIZE_T == 4) 85 #define SIZE_MAX UINT32_MAX 86 #elif (SIZEOF_SIZE_T == 8) 87 #define SIZE_MAX UINT64_MAX 88 #else 89 #error "Can't define SIZE_MAX" 90 #endif /* (SIZEOF_SIZE_T == 4) || ... */ 91 #endif /* !defined(SIZE_MAX) */ 92 93 #ifdef _WIN32 94 # ifdef _WIN64 95 # define TOR_PRIuSZ PRIu64 96 # else 97 # define TOR_PRIuSZ PRIu32 98 # endif 99 #else /* !defined(_WIN32) */ 100 # define TOR_PRIuSZ "zu" 101 #endif /* defined(_WIN32) */ 102 103 #ifdef _WIN32 104 # ifdef _WIN64 105 # define TOR_PRIdSZ PRId64 106 # else 107 # define TOR_PRIdSZ PRId32 108 # endif 109 #else /* !defined(_WIN32) */ 110 # define TOR_PRIdSZ "zd" 111 #endif /* defined(_WIN32) */ 112 113 #ifndef SSIZE_MAX 114 #if (SIZEOF_SIZE_T == 4) 115 #define SSIZE_MAX INT32_MAX 116 #elif (SIZEOF_SIZE_T == 8) 117 #define SSIZE_MAX INT64_MAX 118 #else 119 #error "Can't define SSIZE_MAX" 120 #endif /* (SIZEOF_SIZE_T == 4) || ... */ 121 #endif /* !defined(SSIZE_MAX) */ 122 123 /** Any ssize_t larger than this amount is likely to be an underflow. */ 124 #define SSIZE_T_CEILING ((ssize_t)(SSIZE_MAX-16)) 125 /** Any size_t larger than this amount is likely to be an underflow. */ 126 #define SIZE_T_CEILING ((size_t)(SSIZE_MAX-16)) 127 128 #if SIZEOF_INT > SIZEOF_VOID_P 129 #error "sizeof(int) > sizeof(void *) - Can't build Tor here." 130 #endif 131 132 #if SIZEOF_UNSIGNED_INT > SIZEOF_VOID_P 133 #error "sizeof(unsigned int) > sizeof(void *) - Can't build Tor here." 134 #endif 135 136 #endif /* !defined(TOR_TORINT_H) */ 137