1 #if defined(HAVE_STDINT_H) 2 #include <stdint.h> 3 #endif 4 #if defined(HAVE_INTTYPES_H) 5 #include <inttypes.h> 6 #endif 7 8 #if defined(HAVE_GETTEXT) 9 #include <libintl.h> 10 #define _(STRING) gettext(STRING) 11 #else 12 #define _(STRING) (STRING) 13 #endif 14 15 /* Define some types if not defined by the system */ 16 #if ! defined(HAVE_INTPTR_T) 17 #if defined(__LP64__) 18 typedef long intptr_t; 19 #else 20 #if defined(HAVE_LONG_LONG_INT) 21 typedef long long intptr_t; 22 #else 23 #error long long type is not supported by this implementation. 24 #endif 25 #endif 26 #endif 27 28 #if ! defined(HAVE_UINTPTR_T) 29 #if defined(__LP64__) 30 typedef unsigned long uintptr_t; 31 #else 32 #if defined(HAVE_UNSIGNED_LONG_LONG_INT) 33 typedef unsigned long long uintptr_t; 34 #else 35 #error unsigned long long type is not supported by this implementation. 36 #endif 37 #endif 38 #endif 39 40 #if ! defined(UINT32_C) 41 #define UINT32_C(c) (c ## U) 42 #endif 43 44 #if ! defined(UINT64_C) 45 #if defined(__LP64__) 46 #define UINT64_C(c) (c ## UL) 47 #else 48 #define UINT64_C(c) (c ## ULL) 49 #endif 50 #endif 51 52 /* Define couple of big numbers if not defined by the system */ 53 #if ! defined(LLONG_MAX) 54 #define LLONG_MAX 9223372036854775807LL 55 #endif 56 #if ! defined(LLONG_MIN) 57 #define LLONG_MIN (-LLONG_MAX - 1LL) 58 #endif 59 #if ! defined(ULLONG_MAX) 60 #define ULLONG_MAX 18446744073709551615ULL 61 #endif 62 #if ! defined(ULLONG_MIN) 63 #define ULLONG_MIN 0 64 #endif 65 #if ! defined(SIZE_MAX) 66 #if defined(ULONG_MAX) 67 #define SIZE_MAX ULONG_MAX 68 #else 69 #define SIZE_MAX UINT_MAX 70 #endif 71 #endif 72 73 /* Define some math and other functions if not defined by the system */ 74 #if ! defined(HAVE_FLOORF) 75 #define floorf floor 76 #endif 77 78 #if ! defined(HAVE_ROUND) 79 #define round rint 80 #endif 81 82 #if ! defined(HAVE_ROUNDF) 83 #define roundf round 84 #endif 85 86 #if ! defined(HAVE_SINF) 87 #define sinf sin 88 #endif 89 90 #if ! defined(HAVE_SQRTF) 91 #define sqrtf sqrt 92 #endif 93 94 #if ! defined(HAVE_STRNLEN) 95 size_t strnlen(const char *, size_t); 96 #define strnlen strn_len 97 #endif 98 99 /* Substitute some functions with safer ones if supported and requested */ 100 #if defined(PROG_HAS_SAFEMEM) 101 #if defined(HAVE_TIMINGSAFE_BCMP) 102 #if defined(bcmp) 103 #undef bcmp 104 #endif 105 #define bcmp timingsafe_bcmp 106 #endif 107 108 #if defined(HAVE_TIMINGSAFE_MEMCMP) 109 #if defined(memcmp) 110 #undef memcmp 111 #endif 112 #define memcmp timingsafe_memcmp 113 #endif 114 115 #if defined(HAVE_EXPLICIT_BZERO) 116 #if defined(bzero) 117 #undef bzero 118 #endif 119 #define bzero explicit_bzero 120 #endif 121 122 #if defined(HAVE_EXPLICIT_MEMSET) 123 #if defined(memset) 124 #undef memset 125 #endif 126 #define memset explicit_memset 127 #endif 128 #endif 129 130 /* Define or hide some compiler extensions */ 131 #if defined(HAVE___ATTRIBUTE__) 132 #define __NORETURN__ __attribute__((noreturn)) 133 #define __PACKED__ __attribute__((packed)) 134 #define __UNUSED__ __attribute__((unused)) 135 #define __VISIBILITY_DEFAULT__ __attribute__((visibility("default"))) 136 #else 137 #define __PACKED__ /**/ 138 #define __UNUSED__ /**/ 139 #define __VISIBILITY_DEFAULT__ /**/ 140 #endif 141 142 /* Define some handy shortcuts */ 143 #define LOGINFO(...) log_info(__FILE__, __func__, __LINE__, __VA_ARGS__) 144 #define LOGWARN(...) log_warn(__FILE__, __func__, __LINE__, __VA_ARGS__) 145 146 #define VALUE_MIN2(a, b) ( \ 147 { __typeof__(a) _a = (a); __typeof__(b) _b = (b); _a < _b ? _a : _b; } \ 148 ) 149 150 #define VALUE_MAX2(a, b) ( \ 151 { __typeof__(a) _a = (a); __typeof__(b) _b = (b); _a > _b ? _a : _b; } \ 152 ) 153 154 #define VALUE_MIN3(a, b, c) ( \ 155 { __typeof__(a) _a = (a); __typeof__(b) _b = (b); __typeof__(c) _c = (c); \ 156 (_a < _b) ? ((_a < _c) ? _a : _c) : ((_b < _c) ? _b : _c); } \ 157 ) 158 159 #define VALUE_MAX3(a, b, c) ( \ 160 { __typeof__(a) _a = (a); __typeof__(b) _b = (b); __typeof__(c) _c = (c); \ 161 (_a > _b) ? ((_a > _c) ? _a : _c) : ((_b > _c) ? _b : _c); } \ 162 ) 163 164 /** 165 * Allocate memory buffer returning _r if unsuccesful. 166 * 167 * _s = buffer 168 * _t = buffer size in bytes 169 * _r = value to return in case of failure 170 * 171 */ 172 173 #define APP_MALLOC_RET_VALUE(_s, _t, _r) \ 174 if(_t == 0) { \ 175 (void) flush_error(); \ 176 \ 177 LOGWARN( \ 178 ERROR_SLIGHT, SUBSYSTEM, \ 179 _("Refused to allocate zero bytes of memory") \ 180 ); \ 181 \ 182 return(_r); \ 183 } \ 184 \ 185 if((_s = malloc((size_t) _t)) == NULL) { \ 186 LOGWARN( \ 187 ERROR_SLIGHT, SUBSYSTEM, \ 188 _("Failed to allocate %lu bytes of memory"), \ 189 (unsigned long) _t \ 190 ); \ 191 \ 192 return(_r); \ 193 } 194 195 /** 196 * Allocate memory buffer returning void if unsuccesful. 197 * 198 * _s = buffer 199 * _t = buffer size in bytes 200 * 201 */ 202 203 #define APP_MALLOC_RET_VOID(_s, _t) \ 204 if(_t == 0) { \ 205 (void) flush_error(); \ 206 \ 207 LOGWARN( \ 208 ERROR_SLIGHT, SUBSYSTEM, \ 209 _("Refused to allocate zero bytes of memory") \ 210 ); \ 211 \ 212 return; \ 213 } \ 214 \ 215 if((_s = malloc((size_t) _t)) == NULL) { \ 216 LOGWARN( \ 217 ERROR_SLIGHT, SUBSYSTEM, \ 218 _("Failed to allocate %lu bytes of memory"), \ 219 (unsigned long) _t \ 220 ); \ 221 \ 222 return; \ 223 } 224 225 /** 226 * Reallocate memory buffer returning _r if unsuccesful. 227 * 228 * _p = temporary pointer 229 * _s = buffer to reallocate 230 * _t = buffer new size in bytes 231 * _r = value to return in case of failure 232 * 233 */ 234 235 #define APP_REALLOC_RET_VALUE(_p, _s, _t, _r) \ 236 if(_t == 0) { \ 237 (void) flush_error(); \ 238 \ 239 LOGWARN( \ 240 ERROR_SLIGHT, SUBSYSTEM, \ 241 _("Refused to allocate zero bytes of memory") \ 242 ); \ 243 \ 244 return(_r); \ 245 } \ 246 \ 247 if((_p = realloc(_s, (size_t) _t)) == NULL) { \ 248 LOGWARN( \ 249 ERROR_SLIGHT, SUBSYSTEM, \ 250 _("Failed to allocate %lu bytes of memory"), \ 251 (unsigned long) _t \ 252 ); \ 253 \ 254 return(_r); \ 255 } \ 256 \ 257 _s = _p; 258 259 /** 260 * Reallocate memory buffer returning void if unsuccesful. 261 * 262 * _p = temporary pointer 263 * _s = buffer to reallocate 264 * _t = buffer new size in bytes 265 * 266 */ 267 268 #define APP_REALLOC_RET_VOID(_p, _s, _t) \ 269 if(_t == 0) { \ 270 (void) flush_error(); \ 271 \ 272 LOGWARN( \ 273 ERROR_SLIGHT, SUBSYSTEM, \ 274 _("Refused to allocate zero bytes of memory") \ 275 ); \ 276 \ 277 return; \ 278 } \ 279 \ 280 if((_p = realloc(_s, (size_t) _t)) == NULL) { \ 281 LOGWARN( \ 282 ERROR_SLIGHT, SUBSYSTEM, \ 283 _("Failed to allocate %lu bytes of memory"), \ 284 (unsigned long) _t \ 285 ); \ 286 \ 287 return; \ 288 } \ 289 \ 290 _s = _p; 291