Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | #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 Welte | cd623eb | 2011-05-29 15:37:38 +0200 | [diff] [blame] | 26 | #include <stdint.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 27 | |
| 28 | /* In GSM mac blocks, every bit can be 0 or 1, or L or H. L/H are |
| 29 | * defined relative to the 0x2b padding pattern */ |
| 30 | enum bit_value { |
| 31 | ZERO = 0, |
| 32 | ONE = 1, |
| 33 | L = 2, |
| 34 | H = 3, |
| 35 | }; |
| 36 | |
| 37 | struct bitvec { |
| 38 | unsigned int cur_bit; /* curser to the next unused bit */ |
| 39 | unsigned int data_len; /* length of data array in bytes */ |
| 40 | uint8_t *data; /* pointer to data array */ |
| 41 | }; |
| 42 | |
| 43 | /* check if the bit is 0 or 1 for a given position inside a bitvec */ |
Harald Welte | d9abf01 | 2010-03-06 11:28:49 +0100 | [diff] [blame] | 44 | enum bit_value bitvec_get_bit_pos(const struct bitvec *bv, unsigned int bitnr); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 45 | |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 46 | /* check if the bit is L or H for a given position inside a bitvec */ |
| 47 | enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv, |
| 48 | unsigned int bitnr); |
| 49 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 50 | /* get the Nth set bit inside the bit vector */ |
Harald Welte | d9abf01 | 2010-03-06 11:28:49 +0100 | [diff] [blame] | 51 | 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] | 52 | |
| 53 | /* Set a bit at given position */ |
| 54 | int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnum, |
| 55 | enum bit_value bit); |
| 56 | |
| 57 | /* Set the next bit in the vector */ |
| 58 | int bitvec_set_bit(struct bitvec *bv, enum bit_value bit); |
| 59 | |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 60 | /* get the next bit (low/high) inside a bitvec */ |
| 61 | int bitvec_get_bit_high(struct bitvec *bv); |
| 62 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 63 | /* Set multiple bits at the current position */ |
| 64 | int bitvec_set_bits(struct bitvec *bv, enum bit_value *bits, int count); |
| 65 | |
| 66 | /* Add an unsigned integer (of length count bits) to current position */ |
| 67 | int bitvec_set_uint(struct bitvec *bv, unsigned int in, int count); |
| 68 | |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 69 | /* get multiple bits (based on numeric value) from current pos */ |
| 70 | int bitvec_get_uint(struct bitvec *bv, int num_bits); |
| 71 | |
Pablo Neira Ayuso | 36bdf2c | 2011-03-28 19:24:19 +0200 | [diff] [blame] | 72 | /* find the first bit set in bit vector */ |
| 73 | 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] | 74 | |
| 75 | /* Pad the bit vector up to a certain bit position */ |
| 76 | int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit); |
| 77 | |
| 78 | #endif /* _BITVEC_H */ |