blob: ac25544754934bdeb88c63a742092b37e007d1e6 [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
38int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnr,
39 enum bit_value bit)
40{
41 unsigned int bytenum = bytenum_from_bitnum(bitnr);
42 unsigned int bitnum = 7 - (bitnr % 8);
43 u_int8_t bitval;
44
45 if (bytenum >= bv->data_len)
46 return -EINVAL;
47
48 switch (bit) {
49 case ZERO:
50 bitval = (0 << bitnum);
51 break;
52 case ONE:
53 bitval = (1 << bitnum);
54 break;
55 case L:
56 bitval = ((0x2b ^ (0 << bitnum)) & (1 << bitnum));
57 break;
58 case H:
59 bitval = ((0x2b ^ (1 << bitnum)) & (1 << bitnum));
60 break;
61 default:
62 return -EINVAL;
63 }
64
65 /* first clear the bit */
66 bv->data[bytenum] &= ~(1 << bitnum);
67
68 /* then set it to desired value */
69 bv->data[bytenum] |= bitval;
70
71 return 0;
72}
73
74int bitvec_set_bit(struct bitvec *bv, enum bit_value bit)
75{
76 int rc;
77
78 rc = bitvec_set_bit_pos(bv, bv->cur_bit, bit);
79 if (!rc)
80 bv->cur_bit++;
81
82 return rc;
83}
84
85int bitvec_set_bits(struct bitvec *bv, enum bit_value *bits, int count)
86{
87 int i, rc;
88
89 for (i = 0; i < count; i++) {
90 rc = bitvec_set_bit(bv, bits[i]);
91 if (rc)
92 return rc;
93 }
94
95 return 0;
96}
97
98int bitvec_set_uint(struct bitvec *bv, unsigned int ui, int num_bits)
99{
100 int i, rc;
101
102 for (i = 0; i < num_bits; i++) {
103 int bit = 0;
104 if (ui & (1 << (num_bits - i - 1)))
105 bit = 1;
106 rc = bitvec_set_bit(bv, bit);
107 if (rc)
108 return rc;
109 }
110
111 return 0;
112}
113
114/* pad all remaining bits up to num_bits */
115int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit)
116{
117 unsigned int i;
118
119 for (i = bv->cur_bit; i <= up_to_bit; i++)
120 bitvec_set_bit(bv, L);
121
122 return 0;
123}