2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.
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.
18 /* $Id: bitstring.c,v 1.12.2.1 2004/03/09 06:11:45 marka Exp $ */
24 #include <isc/magic.h>
25 #include <isc/bitstring.h>
28 #define DIV8(x) ((x) >> 3)
29 #define MOD8(x) ((x) & 0x00000007U)
30 #define OCTETS(n) (((n) + 7) >> 3)
31 #define PADDED(n) ((((n) + 7) >> 3) << 3)
32 #define BITSET(bs, n) (((bs)->data[DIV8(n)] & \
33 (1 << (7 - MOD8(n)))) != 0)
34 #define SETBIT(bs, n) (bs)->data[DIV8(n)] |= (1 << (7 - MOD8(n)))
35 #define CLEARBIT(bs, n) (bs)->data[DIV8(n)] &= ~(1 << (7 - MOD8(n)))
37 #define BITSTRING_MAGIC ISC_MAGIC('B', 'S', 't', 'r')
38 #define VALID_BITSTRING(b) ISC_MAGIC_VALID(b, BITSTRING_MAGIC)
41 isc_bitstring_init(isc_bitstring_t
*bitstring
, unsigned char *data
,
42 unsigned int length
, unsigned int size
, isc_boolean_t lsb0
)
45 * Make 'bitstring' refer to the bitstring of 'size' bits starting
46 * at 'data'. 'length' bits of the bitstring are valid. If 'lsb0'
47 * is set then, bit 0 refers to the least significant bit of the
48 * bitstring. Otherwise bit 0 is the most significant bit.
51 REQUIRE(bitstring
!= NULL
);
52 REQUIRE(data
!= NULL
);
53 REQUIRE(length
<= size
);
55 bitstring
->magic
= BITSTRING_MAGIC
;
56 bitstring
->data
= data
;
57 bitstring
->length
= length
;
58 bitstring
->size
= size
;
59 bitstring
->lsb0
= lsb0
;
63 isc_bitstring_invalidate(isc_bitstring_t
*bitstring
) {
66 * Invalidate 'bitstring'.
69 REQUIRE(VALID_BITSTRING(bitstring
));
72 bitstring
->data
= NULL
;
73 bitstring
->length
= 0;
75 bitstring
->lsb0
= ISC_FALSE
;
79 isc_bitstring_copy(isc_bitstring_t
*source
, unsigned int sbitpos
,
80 isc_bitstring_t
*target
, unsigned int tbitpos
,
86 * Starting at bit 'sbitpos', copy 'n' bits from 'source' to
87 * the 'n' bits of 'target' starting at 'tbitpos'.
90 REQUIRE(VALID_BITSTRING(source
));
91 REQUIRE(VALID_BITSTRING(target
));
92 REQUIRE(source
->lsb0
== target
->lsb0
);
94 REQUIRE(sbitpos
<= source
->length
);
95 sbitpos
= PADDED(source
->size
) - sbitpos
;
96 REQUIRE(sbitpos
>= n
);
99 REQUIRE(sbitpos
+ n
<= source
->length
);
102 REQUIRE(tbitpos
<= target
->length
);
103 tbitpos
= PADDED(target
->size
) - tbitpos
;
104 REQUIRE(tbitpos
>= n
);
107 REQUIRE(tlast
<= target
->size
);
109 if (tlast
> target
->length
)
110 target
->length
= tlast
;
113 * This is far from optimal...
117 if (BITSET(source
, sbitpos
))
118 SETBIT(target
, tbitpos
);
120 CLEARBIT(target
, tbitpos
);