util.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include "lib/fs/winlib.h"
  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. /* String manipulation */
  59. /* Time helpers */
  60. /* File helpers */
  61. #define write_all(fd, buf, count, isSock) \
  62. ((isSock) ? write_all_to_socket((fd), (buf), (count)) \
  63. : write_all_to_fd((int)(fd), (buf), (count)))
  64. #define read_all(fd, buf, count, isSock) \
  65. ((isSock) ? read_all_from_socket((fd), (buf), (count)) \
  66. : read_all_from_fd((int)(fd), (buf), (count)))
  67. #endif /* !defined(TOR_UTIL_H) */