blob: d64d69dc2f9a5d5e7b9c293e31ccd9a2bae9e8bd [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +01002
3/* bit vector utility routines */
4
5/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
Maxa15f05f2016-01-26 10:43:15 +01006 * (C) 2012 Ivan Klyuchnikov
Harald Welteec8b4502010-02-20 20:34:29 +01007 *
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
Jacob Erlbeck5f349be2015-12-21 16:04:03 +010032 *
33 * These functions assume a MSB (most significant bit) first layout of the
34 * bits, so that for instance the 5 bit number abcde (a is MSB) can be
35 * embedded into a byte sequence like in xxxxxxab cdexxxxx. The bit count
36 * starts with the MSB, so the bits in a byte are numbered (MSB) 01234567 (LSB).
37 * Note that there are other incompatible encodings, like it is used
38 * for the EGPRS RLC data block headers (there the bits are numbered from LSB
39 * to MSB).
Harald Welteba6988b2011-08-17 12:46:48 +020040 */
41
Harald Weltecd623eb2011-05-29 15:37:38 +020042#include <stdint.h>
Maxa15f05f2016-01-26 10:43:15 +010043#include <talloc.h>
Harald Welteec8b4502010-02-20 20:34:29 +010044
Harald Welteba6988b2011-08-17 12:46:48 +020045/*! \brief A single GSM bit
46 *
47 * 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 +010048 * defined relative to the 0x2b padding pattern */
49enum bit_value {
Harald Welteba6988b2011-08-17 12:46:48 +020050 ZERO = 0, /*!< \brief A zero (0) bit */
51 ONE = 1, /*!< \brief A one (1) bit */
52 L = 2, /*!< \brief A CSN.1 "L" bit */
53 H = 3, /*!< \brief A CSN.1 "H" bit */
Harald Welteec8b4502010-02-20 20:34:29 +010054};
55
Harald Welteba6988b2011-08-17 12:46:48 +020056/*! \brief structure describing a bit vector */
Harald Welteec8b4502010-02-20 20:34:29 +010057struct bitvec {
Holger Hans Peter Freytherc8c33092014-12-28 18:22:48 +010058 unsigned int cur_bit; /*!< \brief cursor to the next unused bit */
Harald Welteba6988b2011-08-17 12:46:48 +020059 unsigned int data_len; /*!< \brief length of data array in bytes */
60 uint8_t *data; /*!< \brief pointer to data array */
Harald Welteec8b4502010-02-20 20:34:29 +010061};
62
Harald Welted9abf012010-03-06 11:28:49 +010063enum bit_value bitvec_get_bit_pos(const struct bitvec *bv, unsigned int bitnr);
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +000064enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv,
65 unsigned int bitnr);
Harald Welted9abf012010-03-06 11:28:49 +010066unsigned int bitvec_get_nth_set_bit(const struct bitvec *bv, unsigned int n);
Harald Welteec8b4502010-02-20 20:34:29 +010067int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnum,
68 enum bit_value bit);
Harald Welteec8b4502010-02-20 20:34:29 +010069int bitvec_set_bit(struct bitvec *bv, enum bit_value bit);
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +000070int bitvec_get_bit_high(struct bitvec *bv);
Maxe49af082016-01-22 16:46:56 +010071int bitvec_set_bits(struct bitvec *bv, enum bit_value *bits, unsigned int count);
72int bitvec_set_uint(struct bitvec *bv, uint32_t in, unsigned int count);
73int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits);
Pablo Neira Ayuso36bdf2c2011-03-28 19:24:19 +020074int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n, enum bit_value val);
Harald Welteec8b4502010-02-20 20:34:29 +010075int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit);
Maxe49af082016-01-22 16:46:56 +010076int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count);
77int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count);
Maxa15f05f2016-01-26 10:43:15 +010078struct bitvec *bitvec_alloc(unsigned int size, TALLOC_CTX *bvctx);
79void bitvec_free(struct bitvec *bv);
80int bitvec_unhex(struct bitvec *bv, const char *src);
81unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer);
82unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer);
83uint64_t bitvec_read_field(struct bitvec *bv, unsigned int read_index, unsigned int len);
84int bitvec_write_field(struct bitvec *bv, unsigned int write_index, uint64_t val, unsigned int len);
Harald Welteec8b4502010-02-20 20:34:29 +010085
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020086/*! @} */