blob: 8c82b87765268f7ca0ab93fd9e755539a79b5f01 [file] [log] [blame]
Harald Weltea43f7892009-12-01 18:04:30 +05301/* bit vector utility routines */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23
24#include <errno.h>
25#include <sys/types.h>
26
27#include <openbsc/bitvec.h>
28
29#define BITNUM_FROM_COMP(byte, bit) ((byte*8)+bit)
30
31static inline unsigned int bytenum_from_bitnum(unsigned int bitnum)
32{
33 unsigned int bytenum = bitnum / 8;
34
35 return bytenum;
36}
37
Harald Welted57f1632009-12-14 22:04:31 +010038/* convert ZERO/ONE/L/H to a bitmask at given pos in a byte */
39static u_int8_t bitval2mask(enum bit_value bit, u_int8_t bitnum)
Harald Weltea43f7892009-12-01 18:04:30 +053040{
Harald Welted57f1632009-12-14 22:04:31 +010041 int bitval;
Harald Weltea43f7892009-12-01 18:04:30 +053042
43 switch (bit) {
44 case ZERO:
45 bitval = (0 << bitnum);
46 break;
47 case ONE:
48 bitval = (1 << bitnum);
49 break;
50 case L:
51 bitval = ((0x2b ^ (0 << bitnum)) & (1 << bitnum));
52 break;
53 case H:
54 bitval = ((0x2b ^ (1 << bitnum)) & (1 << bitnum));
55 break;
56 default:
Harald Welted57f1632009-12-14 22:04:31 +010057 return 0;
Harald Weltea43f7892009-12-01 18:04:30 +053058 }
Harald Welted57f1632009-12-14 22:04:31 +010059 return bitval;
60}
61
62/* check if the bit is 0 or 1 for a given position inside a bitvec */
63enum bit_value bitvec_get_bit_pos(struct bitvec *bv, unsigned int bitnr)
64{
65 unsigned int bytenum = bytenum_from_bitnum(bitnr);
66 unsigned int bitnum = 7 - (bitnr % 8);
67 u_int8_t bitval;
68
69 if (bytenum >= bv->data_len)
70 return -EINVAL;
71
72 bitval = bitval2mask(ONE, bitnum);
73
74 if (bv->data[bytenum] & bitval)
75 return ONE;
76
77 return ZERO;
78}
79
80/* set the bit at a given position inside a bitvec */
81int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnr,
82 enum bit_value bit)
83{
84 unsigned int bytenum = bytenum_from_bitnum(bitnr);
85 unsigned int bitnum = 7 - (bitnr % 8);
86 u_int8_t bitval;
87
88 if (bytenum >= bv->data_len)
89 return -EINVAL;
90
91 bitval = bitval2mask(ONE, bitnum);
Harald Weltea43f7892009-12-01 18:04:30 +053092
93 /* first clear the bit */
94 bv->data[bytenum] &= ~(1 << bitnum);
95
96 /* then set it to desired value */
97 bv->data[bytenum] |= bitval;
98
99 return 0;
100}
101
Harald Welted57f1632009-12-14 22:04:31 +0100102/* set the next bit inside a bitvec */
Harald Weltea43f7892009-12-01 18:04:30 +0530103int bitvec_set_bit(struct bitvec *bv, enum bit_value bit)
104{
105 int rc;
106
107 rc = bitvec_set_bit_pos(bv, bv->cur_bit, bit);
108 if (!rc)
109 bv->cur_bit++;
110
111 return rc;
112}
113
Harald Welted57f1632009-12-14 22:04:31 +0100114/* set multiple bits (based on array of bitvals) at current pos */
Harald Weltea43f7892009-12-01 18:04:30 +0530115int bitvec_set_bits(struct bitvec *bv, enum bit_value *bits, int count)
116{
117 int i, rc;
118
119 for (i = 0; i < count; i++) {
120 rc = bitvec_set_bit(bv, bits[i]);
121 if (rc)
122 return rc;
123 }
124
125 return 0;
126}
127
Harald Welted57f1632009-12-14 22:04:31 +0100128/* set multiple bits (based on numeric value) at current pos */
Harald Weltea43f7892009-12-01 18:04:30 +0530129int bitvec_set_uint(struct bitvec *bv, unsigned int ui, int num_bits)
130{
131 int i, rc;
132
133 for (i = 0; i < num_bits; i++) {
134 int bit = 0;
135 if (ui & (1 << (num_bits - i - 1)))
136 bit = 1;
137 rc = bitvec_set_bit(bv, bit);
138 if (rc)
139 return rc;
140 }
141
142 return 0;
143}
144
145/* pad all remaining bits up to num_bits */
146int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit)
147{
148 unsigned int i;
149
150 for (i = bv->cur_bit; i <= up_to_bit; i++)
151 bitvec_set_bit(bv, L);
152
153 return 0;
154}