/contrib/bind9/lib/isc/bitstring.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 127 lines · 73 code · 20 blank · 34 comment · 9 complexity · 5eaf9ab41ba86c7f357f7a203d0cb1b3 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2001 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: bitstring.c,v 1.17 2007/06/19 23:47:17 tbox Exp $ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <stddef.h>
  21. #include <isc/magic.h>
  22. #include <isc/bitstring.h>
  23. #include <isc/util.h>
  24. #define DIV8(x) ((x) >> 3)
  25. #define MOD8(x) ((x) & 0x00000007U)
  26. #define OCTETS(n) (((n) + 7) >> 3)
  27. #define PADDED(n) ((((n) + 7) >> 3) << 3)
  28. #define BITSET(bs, n) (((bs)->data[DIV8(n)] & \
  29. (1 << (7 - MOD8(n)))) != 0)
  30. #define SETBIT(bs, n) (bs)->data[DIV8(n)] |= (1 << (7 - MOD8(n)))
  31. #define CLEARBIT(bs, n) (bs)->data[DIV8(n)] &= ~(1 << (7 - MOD8(n)))
  32. #define BITSTRING_MAGIC ISC_MAGIC('B', 'S', 't', 'r')
  33. #define VALID_BITSTRING(b) ISC_MAGIC_VALID(b, BITSTRING_MAGIC)
  34. void
  35. isc_bitstring_init(isc_bitstring_t *bitstring, unsigned char *data,
  36. unsigned int length, unsigned int size, isc_boolean_t lsb0)
  37. {
  38. /*
  39. * Make 'bitstring' refer to the bitstring of 'size' bits starting
  40. * at 'data'. 'length' bits of the bitstring are valid. If 'lsb0'
  41. * is set then, bit 0 refers to the least significant bit of the
  42. * bitstring. Otherwise bit 0 is the most significant bit.
  43. */
  44. REQUIRE(bitstring != NULL);
  45. REQUIRE(data != NULL);
  46. REQUIRE(length <= size);
  47. bitstring->magic = BITSTRING_MAGIC;
  48. bitstring->data = data;
  49. bitstring->length = length;
  50. bitstring->size = size;
  51. bitstring->lsb0 = lsb0;
  52. }
  53. void
  54. isc_bitstring_invalidate(isc_bitstring_t *bitstring) {
  55. /*
  56. * Invalidate 'bitstring'.
  57. */
  58. REQUIRE(VALID_BITSTRING(bitstring));
  59. bitstring->magic = 0;
  60. bitstring->data = NULL;
  61. bitstring->length = 0;
  62. bitstring->size = 0;
  63. bitstring->lsb0 = ISC_FALSE;
  64. }
  65. void
  66. isc_bitstring_copy(isc_bitstring_t *source, unsigned int sbitpos,
  67. isc_bitstring_t *target, unsigned int tbitpos,
  68. unsigned int n)
  69. {
  70. unsigned int tlast;
  71. /*
  72. * Starting at bit 'sbitpos', copy 'n' bits from 'source' to
  73. * the 'n' bits of 'target' starting at 'tbitpos'.
  74. */
  75. REQUIRE(VALID_BITSTRING(source));
  76. REQUIRE(VALID_BITSTRING(target));
  77. REQUIRE(source->lsb0 == target->lsb0);
  78. if (source->lsb0) {
  79. REQUIRE(sbitpos <= source->length);
  80. sbitpos = PADDED(source->size) - sbitpos;
  81. REQUIRE(sbitpos >= n);
  82. sbitpos -= n;
  83. } else
  84. REQUIRE(sbitpos + n <= source->length);
  85. tlast = tbitpos + n;
  86. if (target->lsb0) {
  87. REQUIRE(tbitpos <= target->length);
  88. tbitpos = PADDED(target->size) - tbitpos;
  89. REQUIRE(tbitpos >= n);
  90. tbitpos -= n;
  91. } else
  92. REQUIRE(tlast <= target->size);
  93. if (tlast > target->length)
  94. target->length = tlast;
  95. /*
  96. * This is far from optimal...
  97. */
  98. while (n > 0) {
  99. if (BITSET(source, sbitpos))
  100. SETBIT(target, tbitpos);
  101. else
  102. CLEARBIT(target, tbitpos);
  103. sbitpos++;
  104. tbitpos++;
  105. n--;
  106. }
  107. }