Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 1 | #pragma once |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 2 | |
| 3 | /* bit vector utility routines */ |
| 4 | |
| 5 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 25 | /*! \defgroup bitvec Bit vectors |
| 26 | * @{ |
| 27 | */ |
| 28 | |
| 29 | /*! \file bitvec.h |
| 30 | * \brief Osmocom bit vector abstraction |
| 31 | */ |
| 32 | |
Harald Welte | cd623eb | 2011-05-29 15:37:38 +0200 | [diff] [blame] | 33 | #include <stdint.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 34 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 35 | /*! \brief A single GSM bit |
| 36 | * |
| 37 | * In GSM mac blocks, every bit can be 0 or 1, or L or H. L/H are |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 38 | * defined relative to the 0x2b padding pattern */ |
| 39 | enum bit_value { |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 40 | ZERO = 0, /*!< \brief A zero (0) bit */ |
| 41 | ONE = 1, /*!< \brief A one (1) bit */ |
| 42 | L = 2, /*!< \brief A CSN.1 "L" bit */ |
| 43 | H = 3, /*!< \brief A CSN.1 "H" bit */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 44 | }; |
| 45 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 46 | /*! \brief structure describing a bit vector */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 47 | struct bitvec { |
Holger Hans Peter Freyther | c8c3309 | 2014-12-28 18:22:48 +0100 | [diff] [blame] | 48 | unsigned int cur_bit; /*!< \brief cursor to the next unused bit */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 49 | unsigned int data_len; /*!< \brief length of data array in bytes */ |
| 50 | uint8_t *data; /*!< \brief pointer to data array */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
Harald Welte | d9abf01 | 2010-03-06 11:28:49 +0100 | [diff] [blame] | 53 | enum bit_value bitvec_get_bit_pos(const struct bitvec *bv, unsigned int bitnr); |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 54 | enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv, |
| 55 | unsigned int bitnr); |
Harald Welte | d9abf01 | 2010-03-06 11:28:49 +0100 | [diff] [blame] | 56 | unsigned int bitvec_get_nth_set_bit(const struct bitvec *bv, unsigned int n); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 57 | int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnum, |
| 58 | enum bit_value bit); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 59 | int bitvec_set_bit(struct bitvec *bv, enum bit_value bit); |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 60 | int bitvec_get_bit_high(struct bitvec *bv); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 61 | int bitvec_set_bits(struct bitvec *bv, enum bit_value *bits, int count); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 62 | int bitvec_set_uint(struct bitvec *bv, unsigned int in, int count); |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 63 | int bitvec_get_uint(struct bitvec *bv, int num_bits); |
Pablo Neira Ayuso | 36bdf2c | 2011-03-28 19:24:19 +0200 | [diff] [blame] | 64 | int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n, enum bit_value val); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 65 | int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit); |
| 66 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 67 | /*! @} */ |