util.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright (c) 2003-2004, 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 util.h
  7. * \brief Headers for util.c
  8. **/
  9. #ifndef TOR_UTIL_H
  10. #define TOR_UTIL_H
  11. #include "orconfig.h"
  12. #include "lib/cc/torint.h"
  13. #include "common/compat.h"
  14. #include "lib/ctime/di_ops.h"
  15. #include "lib/testsupport/testsupport.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #ifdef _WIN32
  19. /* for the correct alias to struct stat */
  20. #include <sys/stat.h>
  21. #endif
  22. #include "lib/err/torerr.h"
  23. #include "lib/malloc/util_malloc.h"
  24. #include "lib/wallclock/approx_time.h"
  25. #include "lib/string/util_string.h"
  26. #include "lib/string/parse_int.h"
  27. #include "lib/string/scanf.h"
  28. #include "lib/intmath/bits.h"
  29. #include "lib/intmath/addsub.h"
  30. #include "lib/intmath/muldiv.h"
  31. #include "lib/intmath/cmp.h"
  32. #include "lib/log/ratelim.h"
  33. #include "lib/log/util_bug.h"
  34. #include "lib/log/escape.h"
  35. #include "lib/fs/dir.h"
  36. #include "lib/fs/files.h"
  37. #include "lib/fs/path.h"
  38. #include "lib/encoding/time_fmt.h"
  39. #include "lib/encoding/cstring.h"
  40. void tor_log_mallinfo(int severity);
  41. /** Macro: yield a pointer to an enclosing structure given a pointer to
  42. * a substructure at offset <b>off</b>. Example:
  43. * <pre>
  44. * struct base { ... };
  45. * struct subtype { int x; struct base b; } x;
  46. * struct base *bp = &x.base;
  47. * struct *sp = SUBTYPE_P(bp, struct subtype, b);
  48. * </pre>
  49. */
  50. #define SUBTYPE_P(p, subtype, basemember) \
  51. ((void*) ( ((char*)(p)) - offsetof(subtype, basemember) ))
  52. /* Logic */
  53. /** Macro: true if two values have the same boolean value. */
  54. #define bool_eq(a,b) (!(a)==!(b))
  55. /** Macro: true if two values have different boolean values. */
  56. #define bool_neq(a,b) (!(a)!=!(b))
  57. /* Math functions */
  58. double tor_mathlog(double d) ATTR_CONST;
  59. long tor_lround(double d) ATTR_CONST;
  60. int64_t tor_llround(double d) ATTR_CONST;
  61. int64_t sample_laplace_distribution(double mu, double b, double p);
  62. int64_t add_laplace_noise(int64_t signal, double random, double delta_f,
  63. double epsilon);
  64. int64_t clamp_double_to_int64(double number);
  65. /* String manipulation */
  66. int string_is_key_value(int severity, const char *string);
  67. char *tor_escape_str_for_pt_args(const char *string,
  68. const char *chars_to_escape);
  69. /* Time helpers */
  70. long tv_udiff(const struct timeval *start, const struct timeval *end);
  71. long tv_mdiff(const struct timeval *start, const struct timeval *end);
  72. int64_t tv_to_msec(const struct timeval *tv);
  73. /* File helpers */
  74. #define write_all(fd, buf, count, isSock) \
  75. ((isSock) ? write_all_to_socket((fd), (buf), (count)) \
  76. : write_all_to_fd((int)(fd), (buf), (count)))
  77. #define read_all(fd, buf, count, isSock) \
  78. ((isSock) ? read_all_from_socket((fd), (buf), (count)) \
  79. : read_all_from_fd((int)(fd), (buf), (count)))
  80. #ifdef _WIN32
  81. HANDLE load_windows_system_library(const TCHAR *library_name);
  82. #endif
  83. /* ===== Insecure rng */
  84. typedef struct tor_weak_rng_t {
  85. uint32_t state;
  86. } tor_weak_rng_t;
  87. #define TOR_WEAK_RNG_INIT {383745623}
  88. #define TOR_WEAK_RANDOM_MAX (INT_MAX)
  89. void tor_init_weak_random(tor_weak_rng_t *weak_rng, unsigned seed);
  90. int32_t tor_weak_random(tor_weak_rng_t *weak_rng);
  91. int32_t tor_weak_random_range(tor_weak_rng_t *rng, int32_t top);
  92. /** Randomly return true according to <b>rng</b> with probability 1 in
  93. * <b>n</b> */
  94. #define tor_weak_random_one_in_n(rng, n) (0==tor_weak_random_range((rng),(n)))
  95. #endif /* !defined(TOR_UTIL_H) */