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