blob: 9c000d02191f2dc03319f5967c84e2ab2782b297 [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001#ifndef _BITVEC_H
2#define _BITVEC_H
3
4/* bit vector utility routines */
5
6/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
7 *
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
Harald Welteba6988b2011-08-17 12:46:48 +020026/*! \defgroup bitvec Bit vectors
27 * @{
28 */
29
30/*! \file bitvec.h
31 * \brief Osmocom bit vector abstraction
32 */
33
Harald Weltecd623eb2011-05-29 15:37:38 +020034#include <stdint.h>
Harald Welteec8b4502010-02-20 20:34:29 +010035
Harald Welteba6988b2011-08-17 12:46:48 +020036/*! \brief A single GSM bit
37 *
38 * In GSM mac blocks, every bit can be 0 or 1, or L or H. L/H are
Harald Welteec8b4502010-02-20 20:34:29 +010039 * defined relative to the 0x2b padding pattern */
40enum bit_value {
Harald Welteba6988b2011-08-17 12:46:48 +020041 ZERO = 0, /*!< \brief A zero (0) bit */
42 ONE = 1, /*!< \brief A one (1) bit */
43 L = 2, /*!< \brief A CSN.1 "L" bit */
44 H = 3, /*!< \brief A CSN.1 "H" bit */
Harald Welteec8b4502010-02-20 20:34:29 +010045};
46
Harald Welteba6988b2011-08-17 12:46:48 +020047/*! \brief structure describing a bit vector */
Harald Welteec8b4502010-02-20 20:34:29 +010048struct bitvec {
Harald Welteba6988b2011-08-17 12:46:48 +020049 unsigned int cur_bit; /*!< \brief curser to the next unused bit */
50 unsigned int data_len; /*!< \brief length of data array in bytes */
51 uint8_t *data; /*!< \brief pointer to data array */
Harald Welteec8b4502010-02-20 20:34:29 +010052};
53
Harald Welted9abf012010-03-06 11:28:49 +010054enum bit_value bitvec_get_bit_pos(const struct bitvec *bv, unsigned int bitnr);
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +000055enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv,
56 unsigned int bitnr);
Harald Welted9abf012010-03-06 11:28:49 +010057unsigned int bitvec_get_nth_set_bit(const struct bitvec *bv, unsigned int n);
Harald Welteec8b4502010-02-20 20:34:29 +010058int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnum,
59 enum bit_value bit);
Harald Welteec8b4502010-02-20 20:34:29 +010060int bitvec_set_bit(struct bitvec *bv, enum bit_value bit);
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +000061int bitvec_get_bit_high(struct bitvec *bv);
Harald Welteec8b4502010-02-20 20:34:29 +010062int bitvec_set_bits(struct bitvec *bv, enum bit_value *bits, int count);
Harald Welteec8b4502010-02-20 20:34:29 +010063int bitvec_set_uint(struct bitvec *bv, unsigned int in, int count);
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +000064int bitvec_get_uint(struct bitvec *bv, int num_bits);
Pablo Neira Ayuso36bdf2c2011-03-28 19:24:19 +020065int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n, enum bit_value val);
Harald Welteec8b4502010-02-20 20:34:29 +010066int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit);
67
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020068/*! @} */
Harald Welteba6988b2011-08-17 12:46:48 +020069
Harald Welteec8b4502010-02-20 20:34:29 +010070#endif /* _BITVEC_H */