You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.6 KiB

10 years ago
  1. /* -*- coding: utf-8 -*-
  2. * ----------------------------------------------------------------------
  3. * Copyright © 2012, RedJack, LLC.
  4. * All rights reserved.
  5. *
  6. * Please see the LICENSE.txt file in this distribution for license
  7. * details.
  8. * ----------------------------------------------------------------------
  9. */
  10. #ifndef IPSET_BITS_H
  11. #define IPSET_BITS_H
  12. #include <libcork/core.h>
  13. /*-----------------------------------------------------------------------
  14. * Bit arrays
  15. */
  16. /**
  17. * Extract the byte that contains a particular bit in an array.
  18. */
  19. #define IPSET_BIT_GET_BYTE(array, i) \
  20. (((uint8_t *) (array))[(i) / 8])
  21. /**
  22. * Create a bit mask that extracts a particular bit from the byte that
  23. * it lives in.
  24. */
  25. #define IPSET_BIT_ON_MASK(i) \
  26. (0x80 >> ((i) % 8))
  27. /**
  28. * Create a bit mask that extracts everything except for a particular
  29. * bit from the byte that it lives in.
  30. */
  31. #define IPSET_BIT_NEG_MASK(i) \
  32. (~IPSET_BIT_ON_MASK(i))
  33. /**
  34. * Return whether a particular bit is set in a byte array. Bits are
  35. * numbered from 0, in a big-endian order.
  36. */
  37. #define IPSET_BIT_GET(array, i) \
  38. ((IPSET_BIT_GET_BYTE(array, i) & \
  39. IPSET_BIT_ON_MASK(i)) != 0)
  40. /**
  41. * Set (or unset) a particular bit is set in a byte array. Bits are
  42. * numbered from 0, in a big-endian order.
  43. */
  44. #define IPSET_BIT_SET(array, i, val) \
  45. (IPSET_BIT_GET_BYTE(array, i) = \
  46. (IPSET_BIT_GET_BYTE(array, i) & IPSET_BIT_NEG_MASK(i)) \
  47. | ((val)? IPSET_BIT_ON_MASK(i): 0))
  48. #endif /* IPSET_BITS_H */