1 /*	$NetBSD: bitstring.h,v 1.1.1.1 2009/12/13 16:54:25 kardel Exp $	*/
2 
3 /*
4  * Copyright (C) 2004-2007  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1999-2001  Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 /* Id: bitstring.h,v 1.14 2007/06/19 23:47:18 tbox Exp */
21 
22 #ifndef ISC_BITSTRING_H
23 #define ISC_BITSTRING_H 1
24 
25 /*****
26  ***** Module Info
27  *****/
28 
29 /*! \file isc/bitstring.h
30  *
31  * \brief Bitstring manipulation functions.
32  *
33  * A bitstring is a packed array of bits, stored in a contiguous
34  * sequence of octets.  The "most significant bit" (msb) of a bitstring
35  * is the high bit of the first octet.  The "least significant bit" of a
36  * bitstring is the low bit of the last octet.
37  *
38  * Two bit numbering schemes are supported, "msb0" and "lsb0".
39  *
40  * In the "msb0" scheme, bit number 0 designates the most significant bit,
41  * and any padding bits required to make the bitstring a multiple of 8 bits
42  * long are added to the least significant end of the last octet.
43  *
44  * In the "lsb0" scheme, bit number 0 designates the least significant bit,
45  * and any padding bits required to make the bitstring a multiple of 8 bits
46  * long are added to the most significant end of the first octet.
47  *
48  * E.g., consider the bitstring "11010001111".  This bitstring is 11 bits
49  * long and will take two octets.  Let "p" denote a pad bit.  In the msb0
50  * encoding, it would be
51  *
52  * \verbatim
53  *             Octet 0           Octet 1
54  *                         |
55  *         1 1 0 1 0 0 0 1 | 1 1 1 p p p p p
56  *         ^               |               ^
57  *         |                               |
58  *         bit 0                           bit 15
59  * \endverbatim
60  *
61  * In the lsb0 encoding, it would be
62  *
63  * \verbatim
64  *             Octet 0           Octet 1
65  *                         |
66  *         p p p p p 1 1 0 | 1 0 0 0 1 1 1 1
67  *         ^               |               ^
68  *         |                               |
69  *         bit 15                          bit 0
70  * \endverbatim
71  */
72 
73 /***
74  *** Imports
75  ***/
76 
77 #include <isc/lang.h>
78 #include <isc/types.h>
79 
80 ISC_LANG_BEGINDECLS
81 
82 /***
83  *** Types
84  ***/
85 
86 struct isc_bitstring {
87 	unsigned int		magic;
88 	unsigned char *		data;
89 	unsigned int		length;
90 	unsigned int		size;
91 	isc_boolean_t		lsb0;
92 };
93 
94 /***
95  *** Functions
96  ***/
97 
98 void
99 isc_bitstring_init(isc_bitstring_t *bitstring, unsigned char *data,
100 		   unsigned int length, unsigned int size, isc_boolean_t lsb0);
101 /*!<
102  * \brief Make 'bitstring' refer to the bitstring of 'size' bits starting
103  * at 'data'.  'length' bits of the bitstring are valid.  If 'lsb0'
104  * is set then, bit 0 refers to the least significant bit of the
105  * bitstring.  Otherwise bit 0 is the most significant bit.
106  *
107  * Requires:
108  *
109  *\li	'bitstring' points to a isc_bitstring_t.
110  *
111  *\li	'data' points to an array of unsigned char large enough to hold
112  *	'size' bits.
113  *
114  *\li	'length' <= 'size'.
115  *
116  * Ensures:
117  *
118  *\li	'bitstring' is a valid bitstring.
119  */
120 
121 void
122 isc_bitstring_invalidate(isc_bitstring_t *bitstring);
123 /*!<
124  * \brief Invalidate 'bitstring'.
125  *
126  * Requires:
127  *
128  *\li	'bitstring' is a valid bitstring.
129  *
130  * Ensures:
131  *
132  *\li	'bitstring' is not a valid bitstring.
133  */
134 
135 void
136 isc_bitstring_copy(isc_bitstring_t *source, unsigned int sbitpos,
137 		   isc_bitstring_t *target, unsigned int tbitpos,
138 		   unsigned int n);
139 /*!<
140  * \brief Starting at bit 'sbitpos', copy 'n' bits from 'source' to
141  * the 'n' bits of 'target' starting at 'tbitpos'.
142  *
143  * Requires:
144  *
145  *\li	'source' and target are valid bitstrings with the same lsb0 setting.
146  *
147  *\li	'sbitpos' + 'n' is less than or equal to the length of 'source'.
148  *
149  *\li	'tbitpos' + 'n' is less than or equal to the size of 'target'.
150  *
151  * Ensures:
152  *
153  *\li	The specified bits have been copied, and the length of 'target'
154  *	adjusted (if required).
155  */
156 
157 ISC_LANG_ENDDECLS
158 
159 #endif /* ISC_BITSTRING_H */
160