Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 2 | * (C) 2012 Ivan Klyuchnikov |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 3 | * (C) 2015 sysmocom - s.f.m.c. GmbH |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 17 | */ |
| 18 | |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 19 | #pragma once |
| 20 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 21 | /*! \defgroup bitvec Bit vectors |
| 22 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 23 | * \file bitvec.h */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 24 | |
Harald Welte | cd623eb | 2011-05-29 15:37:38 +0200 | [diff] [blame] | 25 | #include <stdint.h> |
Max | 0b3db50 | 2017-10-18 13:48:10 +0200 | [diff] [blame] | 26 | #include <osmocom/core/defs.h> |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 27 | #include <stdbool.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 28 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 29 | /*! A single GSM bit |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 30 | * |
| 31 | * 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] | 32 | * defined relative to the 0x2b padding pattern */ |
| 33 | enum bit_value { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 34 | ZERO = 0, /*!< A zero (0) bit */ |
| 35 | ONE = 1, /*!< A one (1) bit */ |
| 36 | L = 2, /*!< A CSN.1 "L" bit */ |
| 37 | H = 3, /*!< A CSN.1 "H" bit */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 38 | }; |
| 39 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 40 | /*! structure describing a bit vector */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 41 | struct bitvec { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 42 | unsigned int cur_bit; /*!< cursor to the next unused bit */ |
| 43 | unsigned int data_len; /*!< length of data array in bytes */ |
| 44 | uint8_t *data; /*!< pointer to data array */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
Harald Welte | d9abf01 | 2010-03-06 11:28:49 +0100 | [diff] [blame] | 47 | 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] | 48 | enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv, |
| 49 | unsigned int bitnr); |
Harald Welte | d9abf01 | 2010-03-06 11:28:49 +0100 | [diff] [blame] | 50 | 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] | 51 | int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnum, |
| 52 | enum bit_value bit); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 53 | int bitvec_set_bit(struct bitvec *bv, enum bit_value bit); |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 54 | int bitvec_get_bit_high(struct bitvec *bv); |
Harald Welte | 14bf28a | 2016-06-27 15:19:10 +0200 | [diff] [blame] | 55 | int bitvec_set_bits(struct bitvec *bv, const enum bit_value *bits, unsigned int count); |
Max | 0b3db50 | 2017-10-18 13:48:10 +0200 | [diff] [blame] | 56 | int bitvec_set_u64(struct bitvec *bv, uint64_t v, uint8_t num_bits, bool use_lh); |
Harald Welte | 449324b | 2017-01-15 17:55:32 +0100 | [diff] [blame] | 57 | int bitvec_set_uint(struct bitvec *bv, unsigned int in, unsigned int count); |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 58 | int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits); |
Pablo Neira Ayuso | 36bdf2c | 2011-03-28 19:24:19 +0200 | [diff] [blame] | 59 | 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] | 60 | int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit); |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 61 | int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count); |
| 62 | int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count); |
Vadim Yanitskiy | 37dc995 | 2023-03-31 05:39:04 +0700 | [diff] [blame] | 63 | struct bitvec *bitvec_alloc(unsigned int size, void *bvctx); |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 64 | void bitvec_free(struct bitvec *bv); |
| 65 | int bitvec_unhex(struct bitvec *bv, const char *src); |
| 66 | unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer); |
| 67 | 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] | 68 | uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned int len); |
| 69 | 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] | 70 | int bitvec_fill(struct bitvec *bv, unsigned int num_bits, enum bit_value fill); |
| 71 | char bit_value_to_char(enum bit_value v); |
| 72 | void bitvec_to_string_r(const struct bitvec *bv, char *str); |
| 73 | void bitvec_zero(struct bitvec *bv); |
| 74 | unsigned bitvec_rl(const struct bitvec *bv, bool b); |
Harald Welte | c85aaed | 2022-01-09 11:56:19 +0100 | [diff] [blame] | 75 | unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, unsigned int max_bits); |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 76 | void bitvec_shiftl(struct bitvec *bv, unsigned int n); |
| 77 | 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] | 78 | unsigned int bitvec_add_array(struct bitvec *bv, const uint32_t *array, |
| 79 | unsigned int array_len, bool dry_run, |
| 80 | unsigned int num_bits); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 81 | |
Harald Welte | ae7966d | 2019-02-03 12:04:46 +0100 | [diff] [blame] | 82 | /*! Return the number of bytes used within the bit vector */ |
| 83 | static inline unsigned int bitvec_used_bytes(const struct bitvec *bv) |
| 84 | { |
| 85 | unsigned int bytes = bv->cur_bit/8; |
| 86 | if (bv->cur_bit%8) |
| 87 | bytes++; |
| 88 | return bytes; |
| 89 | } |
| 90 | |
Harald Welte | b0708d3 | 2019-02-04 12:09:05 +0100 | [diff] [blame] | 91 | /*! Return the tailroom in number of unused bits remaining in the bit-vector */ |
| 92 | static inline unsigned int bitvec_tailroom_bits(const struct bitvec *bv) |
| 93 | { |
| 94 | return bv->data_len*8 - bv->cur_bit; |
| 95 | } |
| 96 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 97 | /*! @} */ |