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> |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 6 | * (C) 2012 Ivan Klyuchnikov |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 7 | * (C) 2015 Sysmocom s.f.m.c. GmbH |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 8 | * |
| 9 | * All Rights Reserved |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | * |
| 25 | */ |
| 26 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 27 | /*! \defgroup bitvec Bit vectors |
| 28 | * @{ |
| 29 | */ |
| 30 | |
| 31 | /*! \file bitvec.h |
| 32 | * \brief Osmocom bit vector abstraction |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 33 | * |
| 34 | * These functions assume a MSB (most significant bit) first layout of the |
| 35 | * bits, so that for instance the 5 bit number abcde (a is MSB) can be |
| 36 | * embedded into a byte sequence like in xxxxxxab cdexxxxx. The bit count |
| 37 | * starts with the MSB, so the bits in a byte are numbered (MSB) 01234567 (LSB). |
| 38 | * Note that there are other incompatible encodings, like it is used |
| 39 | * for the EGPRS RLC data block headers (there the bits are numbered from LSB |
| 40 | * to MSB). |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 41 | */ |
| 42 | |
Harald Welte | cd623eb | 2011-05-29 15:37:38 +0200 | [diff] [blame] | 43 | #include <stdint.h> |
Harald Welte | f2899c6 | 2017-01-15 17:54:11 +0100 | [diff] [blame] | 44 | #include <osmocom/core/talloc.h> |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 45 | #include <stdbool.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 46 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 47 | /*! \brief A single GSM bit |
| 48 | * |
| 49 | * 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] | 50 | * defined relative to the 0x2b padding pattern */ |
| 51 | enum bit_value { |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 52 | ZERO = 0, /*!< \brief A zero (0) bit */ |
| 53 | ONE = 1, /*!< \brief A one (1) bit */ |
| 54 | L = 2, /*!< \brief A CSN.1 "L" bit */ |
| 55 | H = 3, /*!< \brief A CSN.1 "H" bit */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 58 | /*! \brief structure describing a bit vector */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 59 | struct bitvec { |
Holger Hans Peter Freyther | c8c3309 | 2014-12-28 18:22:48 +0100 | [diff] [blame] | 60 | unsigned int cur_bit; /*!< \brief cursor to the next unused bit */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 61 | unsigned int data_len; /*!< \brief length of data array in bytes */ |
| 62 | uint8_t *data; /*!< \brief pointer to data array */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
Harald Welte | d9abf01 | 2010-03-06 11:28:49 +0100 | [diff] [blame] | 65 | 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] | 66 | enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv, |
| 67 | unsigned int bitnr); |
Harald Welte | d9abf01 | 2010-03-06 11:28:49 +0100 | [diff] [blame] | 68 | 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] | 69 | int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnum, |
| 70 | enum bit_value bit); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 71 | int bitvec_set_bit(struct bitvec *bv, enum bit_value bit); |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 72 | int bitvec_get_bit_high(struct bitvec *bv); |
Harald Welte | 14bf28a | 2016-06-27 15:19:10 +0200 | [diff] [blame] | 73 | int bitvec_set_bits(struct bitvec *bv, const enum bit_value *bits, unsigned int count); |
Harald Welte | 449324b | 2017-01-15 17:55:32 +0100 | [diff] [blame] | 74 | int bitvec_set_uint(struct bitvec *bv, unsigned int in, unsigned int count); |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 75 | int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits); |
Pablo Neira Ayuso | 36bdf2c | 2011-03-28 19:24:19 +0200 | [diff] [blame] | 76 | 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] | 77 | int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit); |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 78 | int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count); |
| 79 | int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count); |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 80 | struct bitvec *bitvec_alloc(unsigned int size, TALLOC_CTX *bvctx); |
| 81 | void bitvec_free(struct bitvec *bv); |
| 82 | int bitvec_unhex(struct bitvec *bv, const char *src); |
| 83 | unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer); |
| 84 | unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer); |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 85 | uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned int len); |
| 86 | int bitvec_write_field(struct bitvec *bv, unsigned int *write_index, uint64_t val, unsigned int len); |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 87 | int bitvec_fill(struct bitvec *bv, unsigned int num_bits, enum bit_value fill); |
| 88 | char bit_value_to_char(enum bit_value v); |
| 89 | void bitvec_to_string_r(const struct bitvec *bv, char *str); |
| 90 | void bitvec_zero(struct bitvec *bv); |
| 91 | unsigned bitvec_rl(const struct bitvec *bv, bool b); |
Pravin Kumarvel | 848de8f | 2016-12-02 15:13:03 +0530 | [diff] [blame] | 92 | unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, int max_bits); |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 93 | void bitvec_shiftl(struct bitvec *bv, unsigned int n); |
| 94 | int16_t bitvec_get_int16_msb(const struct bitvec *bv, unsigned int num_bits); |
Max | d479321 | 2016-03-17 11:51:08 +0100 | [diff] [blame] | 95 | unsigned int bitvec_add_array(struct bitvec *bv, const uint32_t *array, |
| 96 | unsigned int array_len, bool dry_run, |
| 97 | unsigned int num_bits); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 98 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 99 | /*! @} */ |