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 by sysmocom - s.f.m.c. GmbH |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | * |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 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 | * |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 21 | /*! \addtogroup bitvec |
| 22 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 23 | * Osmocom bit vector abstraction utility routines. |
| 24 | * |
| 25 | * These functions assume a MSB (most significant bit) first layout of the |
| 26 | * bits, so that for instance the 5 bit number abcde (a is MSB) can be |
| 27 | * embedded into a byte sequence like in xxxxxxab cdexxxxx. The bit count |
| 28 | * starts with the MSB, so the bits in a byte are numbered (MSB) 01234567 (LSB). |
| 29 | * Note that there are other incompatible encodings, like it is used |
| 30 | * for the EGPRS RLC data block headers (there the bits are numbered from LSB |
| 31 | * to MSB). |
| 32 | * |
| 33 | * \file bitvec.c */ |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 34 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 35 | #include <errno.h> |
| 36 | #include <stdint.h> |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 37 | #include <string.h> |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 38 | #include <stdio.h> |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 39 | #include <stdbool.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 40 | |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 41 | #include <osmocom/core/bits.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 42 | #include <osmocom/core/bitvec.h> |
Harald Welte | 459a180 | 2018-06-28 09:24:17 +0200 | [diff] [blame] | 43 | #include <osmocom/core/panic.h> |
Vadim Yanitskiy | 832d8b8 | 2020-02-19 04:20:08 +0700 | [diff] [blame] | 44 | #include <osmocom/core/utils.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 45 | |
| 46 | #define BITNUM_FROM_COMP(byte, bit) ((byte*8)+bit) |
| 47 | |
| 48 | static inline unsigned int bytenum_from_bitnum(unsigned int bitnum) |
| 49 | { |
| 50 | unsigned int bytenum = bitnum / 8; |
| 51 | |
| 52 | return bytenum; |
| 53 | } |
| 54 | |
| 55 | /* convert ZERO/ONE/L/H to a bitmask at given pos in a byte */ |
| 56 | static uint8_t bitval2mask(enum bit_value bit, uint8_t bitnum) |
| 57 | { |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 58 | switch (bit) { |
| 59 | case ZERO: |
Vadim Yanitskiy | 00a55ae | 2019-07-17 16:41:17 +0700 | [diff] [blame] | 60 | return (0 << bitnum); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 61 | case ONE: |
Vadim Yanitskiy | 00a55ae | 2019-07-17 16:41:17 +0700 | [diff] [blame] | 62 | return (1 << bitnum); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 63 | case L: |
Vadim Yanitskiy | 00a55ae | 2019-07-17 16:41:17 +0700 | [diff] [blame] | 64 | return ((0x2b ^ (0 << bitnum)) & (1 << bitnum)); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 65 | case H: |
Vadim Yanitskiy | 00a55ae | 2019-07-17 16:41:17 +0700 | [diff] [blame] | 66 | return ((0x2b ^ (1 << bitnum)) & (1 << bitnum)); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 67 | default: |
| 68 | return 0; |
| 69 | } |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 72 | /*! check if the bit is 0 or 1 for a given position inside a bitvec |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 73 | * \param[in] bv the bit vector on which to check |
| 74 | * \param[in] bitnr the bit number inside the bit vector to check |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 75 | * \return value of the requested bit |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 76 | */ |
Harald Welte | d9abf01 | 2010-03-06 11:28:49 +0100 | [diff] [blame] | 77 | 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] | 78 | { |
| 79 | unsigned int bytenum = bytenum_from_bitnum(bitnr); |
| 80 | unsigned int bitnum = 7 - (bitnr % 8); |
| 81 | uint8_t bitval; |
| 82 | |
| 83 | if (bytenum >= bv->data_len) |
| 84 | return -EINVAL; |
| 85 | |
| 86 | bitval = bitval2mask(ONE, bitnum); |
| 87 | |
| 88 | if (bv->data[bytenum] & bitval) |
| 89 | return ONE; |
| 90 | |
| 91 | return ZERO; |
| 92 | } |
| 93 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 94 | /*! check if the bit is L or H for a given position inside a bitvec |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 95 | * \param[in] bv the bit vector on which to check |
| 96 | * \param[in] bitnr the bit number inside the bit vector to check |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 97 | * \return value of the requested bit |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 98 | */ |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 99 | enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv, |
| 100 | unsigned int bitnr) |
| 101 | { |
| 102 | unsigned int bytenum = bytenum_from_bitnum(bitnr); |
| 103 | unsigned int bitnum = 7 - (bitnr % 8); |
| 104 | uint8_t bitval; |
| 105 | |
| 106 | if (bytenum >= bv->data_len) |
| 107 | return -EINVAL; |
| 108 | |
| 109 | bitval = bitval2mask(H, bitnum); |
| 110 | |
Andreas.Eversberg | dc0ebdf | 2010-10-24 11:59:33 +0200 | [diff] [blame] | 111 | if ((bv->data[bytenum] & (1 << bitnum)) == bitval) |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 112 | return H; |
| 113 | |
| 114 | return L; |
| 115 | } |
| 116 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 117 | /*! get the Nth set bit inside the bit vector |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 118 | * \param[in] bv the bit vector to use |
| 119 | * \param[in] n the bit number to get |
| 120 | * \returns the bit number (offset) of the Nth set bit in \a bv |
| 121 | */ |
Harald Welte | d9abf01 | 2010-03-06 11:28:49 +0100 | [diff] [blame] | 122 | 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] | 123 | { |
| 124 | unsigned int i, k = 0; |
| 125 | |
| 126 | for (i = 0; i < bv->data_len*8; i++) { |
| 127 | if (bitvec_get_bit_pos(bv, i) == ONE) { |
| 128 | k++; |
| 129 | if (k == n) |
| 130 | return i; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 137 | /*! set a bit at given position in a bit vector |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 138 | * \param[in] bv bit vector on which to operate |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 139 | * \param[in] bitnr number of bit to be set |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 140 | * \param[in] bit value to which the bit is to be set |
Max | 912bc6f | 2016-01-28 12:07:12 +0100 | [diff] [blame] | 141 | * \returns 0 on success, negative value on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 142 | */ |
Holger Hans Peter Freyther | 511b448 | 2016-07-13 12:26:10 +0200 | [diff] [blame] | 143 | inline int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnr, |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 144 | enum bit_value bit) |
| 145 | { |
| 146 | unsigned int bytenum = bytenum_from_bitnum(bitnr); |
| 147 | unsigned int bitnum = 7 - (bitnr % 8); |
| 148 | uint8_t bitval; |
| 149 | |
| 150 | if (bytenum >= bv->data_len) |
| 151 | return -EINVAL; |
| 152 | |
| 153 | /* first clear the bit */ |
| 154 | bitval = bitval2mask(ONE, bitnum); |
| 155 | bv->data[bytenum] &= ~bitval; |
| 156 | |
| 157 | /* then set it to desired value */ |
| 158 | bitval = bitval2mask(bit, bitnum); |
| 159 | bv->data[bytenum] |= bitval; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 164 | /*! set the next bit inside a bitvec |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 165 | * \param[in] bv bit vector to be used |
| 166 | * \param[in] bit value of the bit to be set |
Max | 912bc6f | 2016-01-28 12:07:12 +0100 | [diff] [blame] | 167 | * \returns 0 on success, negative value on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 168 | */ |
Holger Hans Peter Freyther | 511b448 | 2016-07-13 12:26:10 +0200 | [diff] [blame] | 169 | inline int bitvec_set_bit(struct bitvec *bv, enum bit_value bit) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 170 | { |
| 171 | int rc; |
| 172 | |
| 173 | rc = bitvec_set_bit_pos(bv, bv->cur_bit, bit); |
| 174 | if (!rc) |
| 175 | bv->cur_bit++; |
| 176 | |
| 177 | return rc; |
| 178 | } |
| 179 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 180 | /*! get the next bit (low/high) inside a bitvec |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 181 | * \return value of th next bit in the vector */ |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 182 | int bitvec_get_bit_high(struct bitvec *bv) |
| 183 | { |
| 184 | int rc; |
| 185 | |
| 186 | rc = bitvec_get_bit_pos_high(bv, bv->cur_bit); |
| 187 | if (rc >= 0) |
| 188 | bv->cur_bit++; |
| 189 | |
| 190 | return rc; |
| 191 | } |
| 192 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 193 | /*! set multiple bits (based on array of bitvals) at current pos |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 194 | * \param[in] bv bit vector |
| 195 | * \param[in] bits array of \ref bit_value |
| 196 | * \param[in] count number of bits to set |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 197 | * \return 0 on success; negative in case of error */ |
Harald Welte | 14bf28a | 2016-06-27 15:19:10 +0200 | [diff] [blame] | 198 | int bitvec_set_bits(struct bitvec *bv, const enum bit_value *bits, unsigned int count) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 199 | { |
Harald Welte | c85aaed | 2022-01-09 11:56:19 +0100 | [diff] [blame] | 200 | unsigned int i; |
| 201 | int rc; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 202 | |
| 203 | for (i = 0; i < count; i++) { |
| 204 | rc = bitvec_set_bit(bv, bits[i]); |
| 205 | if (rc) |
| 206 | return rc; |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Max | 0b3db50 | 2017-10-18 13:48:10 +0200 | [diff] [blame] | 212 | /*! set multiple bits (based on numeric value) at current pos. |
| 213 | * \param[in] bv bit vector. |
| 214 | * \param[in] v mask representing which bits needs to be set. |
| 215 | * \param[in] num_bits number of meaningful bits in the mask. |
| 216 | * \param[in] use_lh whether to interpret the bits as L/H values or as 0/1. |
| 217 | * \return 0 on success; negative in case of error. */ |
| 218 | int bitvec_set_u64(struct bitvec *bv, uint64_t v, uint8_t num_bits, bool use_lh) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 219 | { |
Max | 0b3db50 | 2017-10-18 13:48:10 +0200 | [diff] [blame] | 220 | uint8_t i; |
| 221 | |
| 222 | if (num_bits > 64) |
| 223 | return -E2BIG; |
| 224 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 225 | for (i = 0; i < num_bits; i++) { |
Max | 0b3db50 | 2017-10-18 13:48:10 +0200 | [diff] [blame] | 226 | int rc; |
| 227 | enum bit_value bit = use_lh ? L : 0; |
| 228 | |
| 229 | if (v & ((uint64_t)1 << (num_bits - i - 1))) |
| 230 | bit = use_lh ? H : 1; |
| 231 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 232 | rc = bitvec_set_bit(bv, bit); |
Max | 0b3db50 | 2017-10-18 13:48:10 +0200 | [diff] [blame] | 233 | if (rc != 0) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 234 | return rc; |
| 235 | } |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
Max | 0b3db50 | 2017-10-18 13:48:10 +0200 | [diff] [blame] | 240 | /*! set multiple bits (based on numeric value) at current pos. |
| 241 | * \return 0 in case of success; negative in case of error. */ |
| 242 | int bitvec_set_uint(struct bitvec *bv, unsigned int ui, unsigned int num_bits) |
| 243 | { |
| 244 | return bitvec_set_u64(bv, ui, num_bits, false); |
| 245 | } |
| 246 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 247 | /*! get multiple bits (num_bits) from beginning of vector (MSB side) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 248 | * \return 16bit signed integer retrieved from bit vector */ |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 249 | int16_t bitvec_get_int16_msb(const struct bitvec *bv, unsigned int num_bits) |
| 250 | { |
| 251 | if (num_bits > 15 || bv->cur_bit < num_bits) |
| 252 | return -EINVAL; |
| 253 | |
| 254 | if (num_bits < 9) |
| 255 | return bv->data[0] >> (8 - num_bits); |
| 256 | |
| 257 | return osmo_load16be(bv->data) >> (16 - num_bits); |
| 258 | } |
| 259 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 260 | /*! get multiple bits (based on numeric value) from current pos |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 261 | * \return integer value retrieved from bit vector */ |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 262 | int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits) |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 263 | { |
Harald Welte | c85aaed | 2022-01-09 11:56:19 +0100 | [diff] [blame] | 264 | unsigned int i; |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 265 | unsigned int ui = 0; |
| 266 | |
| 267 | for (i = 0; i < num_bits; i++) { |
| 268 | int bit = bitvec_get_bit_pos(bv, bv->cur_bit); |
| 269 | if (bit < 0) |
| 270 | return bit; |
| 271 | if (bit) |
Pau Espin Pedrol | 4c951ad | 2020-11-13 12:08:59 +0100 | [diff] [blame] | 272 | ui |= ((unsigned)1 << (num_bits - i - 1)); |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 273 | bv->cur_bit++; |
| 274 | } |
| 275 | |
| 276 | return ui; |
| 277 | } |
| 278 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 279 | /*! fill num_bits with \fill starting from the current position |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 280 | * \return 0 on success; negative otherwise (out of vector boundary) |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 281 | */ |
| 282 | int bitvec_fill(struct bitvec *bv, unsigned int num_bits, enum bit_value fill) |
| 283 | { |
| 284 | unsigned i, stop = bv->cur_bit + num_bits; |
| 285 | for (i = bv->cur_bit; i < stop; i++) |
| 286 | if (bitvec_set_bit(bv, fill) < 0) |
| 287 | return -EINVAL; |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
Vadim Yanitskiy | a500bc3 | 2020-02-19 04:58:35 +0700 | [diff] [blame] | 292 | /*! pad all remaining bits up to a given bit number |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 293 | * \return 0 on success; negative otherwise */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 294 | int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit) |
| 295 | { |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 296 | int n = up_to_bit - bv->cur_bit + 1; |
| 297 | if (n < 1) |
| 298 | return 0; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 299 | |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 300 | return bitvec_fill(bv, n, L); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 301 | } |
Pablo Neira Ayuso | 36bdf2c | 2011-03-28 19:24:19 +0200 | [diff] [blame] | 302 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 303 | /*! find first bit set in bit vector |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 304 | * \return 0 on success; negative otherwise */ |
Pablo Neira Ayuso | 36bdf2c | 2011-03-28 19:24:19 +0200 | [diff] [blame] | 305 | int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n, |
| 306 | enum bit_value val) |
| 307 | { |
| 308 | unsigned int i; |
| 309 | |
| 310 | for (i = n; i < bv->data_len*8; i++) { |
| 311 | if (bitvec_get_bit_pos(bv, i) == val) |
| 312 | return i; |
| 313 | } |
| 314 | |
| 315 | return -1; |
| 316 | } |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 317 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 318 | /*! get multiple bytes from current pos |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 319 | * Assumes MSB first encoding. |
| 320 | * \param[in] bv bit vector |
| 321 | * \param[in] bytes array |
| 322 | * \param[in] count number of bytes to copy |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 323 | * \return 0 on success; negative otherwise |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 324 | */ |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 325 | int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count) |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 326 | { |
| 327 | int byte_offs = bytenum_from_bitnum(bv->cur_bit); |
| 328 | int bit_offs = bv->cur_bit % 8; |
| 329 | uint8_t c, last_c; |
| 330 | int i; |
| 331 | uint8_t *src; |
| 332 | |
| 333 | if (byte_offs + count + (bit_offs ? 1 : 0) > bv->data_len) |
| 334 | return -EINVAL; |
| 335 | |
| 336 | if (bit_offs == 0) { |
| 337 | memcpy(bytes, bv->data + byte_offs, count); |
| 338 | } else { |
| 339 | src = bv->data + byte_offs; |
| 340 | last_c = *(src++); |
| 341 | for (i = count; i > 0; i--) { |
| 342 | c = *(src++); |
| 343 | *(bytes++) = |
| 344 | (last_c << bit_offs) | |
| 345 | (c >> (8 - bit_offs)); |
| 346 | last_c = c; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | bv->cur_bit += count * 8; |
| 351 | return 0; |
| 352 | } |
| 353 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 354 | /*! set multiple bytes at current pos |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 355 | * Assumes MSB first encoding. |
| 356 | * \param[in] bv bit vector |
| 357 | * \param[in] bytes array |
| 358 | * \param[in] count number of bytes to copy |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 359 | * \return 0 on success; negative otherwise |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 360 | */ |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 361 | int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count) |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 362 | { |
| 363 | int byte_offs = bytenum_from_bitnum(bv->cur_bit); |
| 364 | int bit_offs = bv->cur_bit % 8; |
| 365 | uint8_t c, last_c; |
| 366 | int i; |
| 367 | uint8_t *dst; |
| 368 | |
| 369 | if (byte_offs + count + (bit_offs ? 1 : 0) > bv->data_len) |
| 370 | return -EINVAL; |
| 371 | |
| 372 | if (bit_offs == 0) { |
| 373 | memcpy(bv->data + byte_offs, bytes, count); |
| 374 | } else if (count > 0) { |
| 375 | dst = bv->data + byte_offs; |
| 376 | /* Get lower bits of first dst byte */ |
| 377 | last_c = *dst >> (8 - bit_offs); |
| 378 | for (i = count; i > 0; i--) { |
| 379 | c = *(bytes++); |
| 380 | *(dst++) = |
| 381 | (last_c << (8 - bit_offs)) | |
| 382 | (c >> bit_offs); |
| 383 | last_c = c; |
| 384 | } |
| 385 | /* Overwrite lower bits of N+1 dst byte */ |
| 386 | *dst = (*dst & ((1 << (8 - bit_offs)) - 1)) | |
| 387 | (last_c << (8 - bit_offs)); |
| 388 | } |
| 389 | |
| 390 | bv->cur_bit += count * 8; |
| 391 | return 0; |
| 392 | } |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 393 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 394 | /*! Allocate a bit vector |
Alexander Couzens | 76e8cbd | 2019-06-16 22:26:08 +0200 | [diff] [blame] | 395 | * \param[in] size Number of bytes in the vector |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 396 | * \param[in] ctx Context from which to allocate |
| 397 | * \return pointer to allocated vector; NULL in case of error */ |
Vadim Yanitskiy | 37dc995 | 2023-03-31 05:39:04 +0700 | [diff] [blame] | 398 | struct bitvec *bitvec_alloc(unsigned int size, void *ctx) |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 399 | { |
Vadim Yanitskiy | 4960ee4 | 2020-02-19 05:07:40 +0700 | [diff] [blame] | 400 | struct bitvec *bv = talloc(ctx, struct bitvec); |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 401 | if (!bv) |
| 402 | return NULL; |
| 403 | |
| 404 | bv->data = talloc_zero_array(bv, uint8_t, size); |
| 405 | if (!(bv->data)) { |
| 406 | talloc_free(bv); |
| 407 | return NULL; |
| 408 | } |
| 409 | |
| 410 | bv->data_len = size; |
| 411 | bv->cur_bit = 0; |
| 412 | return bv; |
| 413 | } |
| 414 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 415 | /*! Free a bit vector (release its memory) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 416 | * \param[in] bit vector to free */ |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 417 | void bitvec_free(struct bitvec *bv) |
| 418 | { |
Vadim Yanitskiy | 4c9a36c | 2020-02-10 14:21:52 +0700 | [diff] [blame] | 419 | if (bv == NULL) |
| 420 | return; |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 421 | talloc_free(bv->data); |
| 422 | talloc_free(bv); |
| 423 | } |
| 424 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 425 | /*! Export a bit vector to a buffer |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 426 | * \param[in] bitvec (unpacked bits) |
| 427 | * \param[out] buffer for the unpacked bits |
| 428 | * \return number of bytes (= bits) copied */ |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 429 | unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer) |
| 430 | { |
Vadim Yanitskiy | c866b32 | 2020-02-19 05:10:57 +0700 | [diff] [blame] | 431 | unsigned int i; |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 432 | for (i = 0; i < bv->data_len; i++) |
| 433 | buffer[i] = bv->data[i]; |
| 434 | |
| 435 | return i; |
| 436 | } |
| 437 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 438 | /*! Copy buffer of unpacked bits into bit vector |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 439 | * \param[in] buffer unpacked input bits |
| 440 | * \param[out] bv unpacked bit vector |
| 441 | * \return number of bytes (= bits) copied */ |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 442 | unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer) |
| 443 | { |
Vadim Yanitskiy | c866b32 | 2020-02-19 05:10:57 +0700 | [diff] [blame] | 444 | unsigned int i; |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 445 | for (i = 0; i < bv->data_len; i++) |
| 446 | bv->data[i] = buffer[i]; |
| 447 | |
| 448 | return i; |
| 449 | } |
| 450 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 451 | /*! read hexadecimap string into a bit vector |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 452 | * \param[in] src string containing hex digits |
| 453 | * \param[out] bv unpacked bit vector |
| 454 | * \return 0 in case of success; 1 in case of error |
| 455 | */ |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 456 | int bitvec_unhex(struct bitvec *bv, const char *src) |
| 457 | { |
Vadim Yanitskiy | 832d8b8 | 2020-02-19 04:20:08 +0700 | [diff] [blame] | 458 | int rc; |
Holger Hans Peter Freyther | 2745b48 | 2016-01-27 17:08:02 +0100 | [diff] [blame] | 459 | |
Vadim Yanitskiy | 832d8b8 | 2020-02-19 04:20:08 +0700 | [diff] [blame] | 460 | rc = osmo_hexparse(src, bv->data, bv->data_len); |
| 461 | if (rc < 0) /* turn -1 into 1 in case of error */ |
| 462 | return 1; |
| 463 | |
| 464 | bv->cur_bit = rc * 8; |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 465 | return 0; |
| 466 | } |
| 467 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 468 | /*! read part of the vector |
Max | 912bc6f | 2016-01-28 12:07:12 +0100 | [diff] [blame] | 469 | * \param[in] bv The boolean vector to work on |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 470 | * \param[in,out] read_index Where reading supposed to start in the vector |
Max | 912bc6f | 2016-01-28 12:07:12 +0100 | [diff] [blame] | 471 | * \param[in] len How many bits to read from vector |
Vadim Yanitskiy | de3549a | 2021-11-17 06:34:48 +0300 | [diff] [blame] | 472 | * \returns An integer made up of the bits read. |
| 473 | * |
| 474 | * In case of an error, errno is set to a non-zero value. Otherwise it holds 0. |
Max | 912bc6f | 2016-01-28 12:07:12 +0100 | [diff] [blame] | 475 | */ |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 476 | uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned int len) |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 477 | { |
| 478 | unsigned int i; |
| 479 | uint64_t ui = 0; |
Vadim Yanitskiy | 8a55a6c | 2021-11-17 06:36:38 +0300 | [diff] [blame] | 480 | |
| 481 | /* Prevent bitvec overrun due to incorrect index and/or length */ |
| 482 | if (len && bytenum_from_bitnum(*read_index + len - 1) >= bv->data_len) { |
| 483 | errno = EOVERFLOW; |
| 484 | return 0; |
| 485 | } |
| 486 | |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 487 | bv->cur_bit = *read_index; |
Vadim Yanitskiy | de3549a | 2021-11-17 06:34:48 +0300 | [diff] [blame] | 488 | errno = 0; |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 489 | |
| 490 | for (i = 0; i < len; i++) { |
Vadim Yanitskiy | 49b6040 | 2021-11-17 06:21:28 +0300 | [diff] [blame] | 491 | unsigned int bytenum = bytenum_from_bitnum(bv->cur_bit); |
| 492 | unsigned int bitnum = 7 - (bv->cur_bit % 8); |
| 493 | |
| 494 | if (bv->data[bytenum] & (1 << bitnum)) |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 495 | ui |= ((uint64_t)1 << (len - i - 1)); |
| 496 | bv->cur_bit++; |
| 497 | } |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 498 | *read_index += len; |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 499 | return ui; |
| 500 | } |
| 501 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 502 | /*! write into the vector |
Max | 912bc6f | 2016-01-28 12:07:12 +0100 | [diff] [blame] | 503 | * \param[in] bv The boolean vector to work on |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 504 | * \param[in,out] write_index Where writing supposed to start in the vector |
Max | 912bc6f | 2016-01-28 12:07:12 +0100 | [diff] [blame] | 505 | * \param[in] len How many bits to write |
Pau Espin Pedrol | 2b98cbe | 2020-01-03 17:36:11 +0100 | [diff] [blame] | 506 | * \returns 0 on success, negative value on error |
Max | 912bc6f | 2016-01-28 12:07:12 +0100 | [diff] [blame] | 507 | */ |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 508 | int bitvec_write_field(struct bitvec *bv, unsigned int *write_index, uint64_t val, unsigned int len) |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 509 | { |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 510 | int rc; |
Max | f0e392a | 2017-10-18 13:32:30 +0200 | [diff] [blame] | 511 | |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 512 | bv->cur_bit = *write_index; |
Max | f0e392a | 2017-10-18 13:32:30 +0200 | [diff] [blame] | 513 | |
| 514 | rc = bitvec_set_u64(bv, val, len, false); |
| 515 | if (rc != 0) |
| 516 | return rc; |
| 517 | |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 518 | *write_index += len; |
Max | f0e392a | 2017-10-18 13:32:30 +0200 | [diff] [blame] | 519 | |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 520 | return 0; |
| 521 | } |
| 522 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 523 | /*! convert enum to corresponding character |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 524 | * \param v input value (bit) |
| 525 | * \return single character, either 0, 1, L or H */ |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 526 | char bit_value_to_char(enum bit_value v) |
| 527 | { |
| 528 | switch (v) { |
| 529 | case ZERO: return '0'; |
| 530 | case ONE: return '1'; |
| 531 | case L: return 'L'; |
| 532 | case H: return 'H'; |
Harald Welte | 459a180 | 2018-06-28 09:24:17 +0200 | [diff] [blame] | 533 | default: osmo_panic("unexpected input in bit_value_to_char"); return 'X'; |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 537 | /*! prints bit vector to provided string |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 538 | * It's caller's responsibility to ensure that we won't shoot him in the foot: |
| 539 | * the provided buffer should be at lest cur_bit + 1 bytes long |
| 540 | */ |
| 541 | void bitvec_to_string_r(const struct bitvec *bv, char *str) |
| 542 | { |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 543 | char *cur = str; |
Vadim Yanitskiy | 4728f8c | 2023-12-31 16:12:27 +0700 | [diff] [blame] | 544 | for (unsigned int i = 0; i < bv->cur_bit; i++) { |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 545 | if (0 == i % 8) |
| 546 | *cur++ = ' '; |
| 547 | *cur++ = bit_value_to_char(bitvec_get_bit_pos(bv, i)); |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 548 | } |
| 549 | *cur = 0; |
| 550 | } |
| 551 | |
| 552 | /* we assume that x have at least 1 non-b bit */ |
| 553 | static inline unsigned leading_bits(uint8_t x, bool b) |
| 554 | { |
| 555 | if (b) { |
| 556 | if (x < 0x80) return 0; |
| 557 | if (x < 0xC0) return 1; |
| 558 | if (x < 0xE0) return 2; |
| 559 | if (x < 0xF0) return 3; |
| 560 | if (x < 0xF8) return 4; |
| 561 | if (x < 0xFC) return 5; |
| 562 | if (x < 0xFE) return 6; |
| 563 | } else { |
| 564 | if (x > 0x7F) return 0; |
| 565 | if (x > 0x3F) return 1; |
| 566 | if (x > 0x1F) return 2; |
| 567 | if (x > 0xF) return 3; |
| 568 | if (x > 7) return 4; |
| 569 | if (x > 3) return 5; |
| 570 | if (x > 1) return 6; |
| 571 | } |
| 572 | return 7; |
| 573 | } |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 574 | /*! force bit vector to all 0 and current bit to the beginnig of the vector */ |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 575 | void bitvec_zero(struct bitvec *bv) |
| 576 | { |
| 577 | bv->cur_bit = 0; |
| 578 | memset(bv->data, 0, bv->data_len); |
| 579 | } |
| 580 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 581 | /*! Return number (bits) of uninterrupted bit run in vector starting from the MSB |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 582 | * \param[in] bv The boolean vector to work on |
| 583 | * \param[in] b The boolean, sequence of which is looked at from the vector start |
| 584 | * \returns Number of consecutive bits of \p b in \p bv |
| 585 | */ |
| 586 | unsigned bitvec_rl(const struct bitvec *bv, bool b) |
| 587 | { |
| 588 | unsigned i; |
| 589 | for (i = 0; i < (bv->cur_bit % 8 ? bv->cur_bit / 8 + 1 : bv->cur_bit / 8); i++) { |
| 590 | if ( (b ? 0xFF : 0) != bv->data[i]) |
| 591 | return i * 8 + leading_bits(bv->data[i], b); |
| 592 | } |
| 593 | |
| 594 | return bv->cur_bit; |
| 595 | } |
| 596 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 597 | /*! Return number (bits) of uninterrupted bit run in vector |
Pravin Kumarvel | 848de8f | 2016-12-02 15:13:03 +0530 | [diff] [blame] | 598 | * starting from the current bit |
| 599 | * \param[in] bv The boolean vector to work on |
| 600 | * \param[in] b The boolean, sequence of 1's or 0's to be checked |
| 601 | * \param[in] max_bits Total Number of Uncmopresed bits |
| 602 | * \returns Number of consecutive bits of \p b in \p bv and cur_bit will |
| 603 | * \go to cur_bit + number of consecutive bit |
| 604 | */ |
Harald Welte | c85aaed | 2022-01-09 11:56:19 +0100 | [diff] [blame] | 605 | unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, unsigned int max_bits) |
Pravin Kumarvel | 848de8f | 2016-12-02 15:13:03 +0530 | [diff] [blame] | 606 | { |
| 607 | unsigned i = 0; |
| 608 | unsigned j = 8; |
| 609 | int temp_res = 0; |
| 610 | int count = 0; |
| 611 | unsigned readIndex = bv->cur_bit; |
| 612 | unsigned remaining_bits = max_bits % 8; |
| 613 | unsigned remaining_bytes = max_bits / 8; |
| 614 | unsigned byte_mask = 0xFF; |
| 615 | |
| 616 | if (readIndex % 8) { |
| 617 | for (j -= (readIndex % 8) ; j > 0 ; j--) { |
| 618 | if (readIndex < max_bits && bitvec_read_field(bv, &readIndex, 1) == b) |
| 619 | temp_res++; |
| 620 | else { |
| 621 | bv->cur_bit--; |
| 622 | return temp_res; |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | for (i = (readIndex / 8); |
| 627 | i < (remaining_bits ? remaining_bytes + 1 : remaining_bytes); |
| 628 | i++, count++) { |
| 629 | if ((b ? byte_mask : 0) != bv->data[i]) { |
| 630 | bv->cur_bit = (count * 8 + |
| 631 | leading_bits(bv->data[i], b) + readIndex); |
| 632 | return count * 8 + |
| 633 | leading_bits(bv->data[i], b) + temp_res; |
| 634 | } |
| 635 | } |
| 636 | bv->cur_bit = (temp_res + (count * 8)) + readIndex; |
| 637 | if (bv->cur_bit > max_bits) |
| 638 | bv->cur_bit = max_bits; |
| 639 | return (bv->cur_bit - readIndex + temp_res); |
| 640 | } |
| 641 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 642 | /*! Shifts bitvec to the left, n MSB bits lost */ |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 643 | void bitvec_shiftl(struct bitvec *bv, unsigned n) |
| 644 | { |
| 645 | if (0 == n) |
| 646 | return; |
| 647 | if (n >= bv->cur_bit) { |
| 648 | bitvec_zero(bv); |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | memmove(bv->data, bv->data + n / 8, bv->data_len - n / 8); |
| 653 | |
| 654 | uint8_t tmp[2]; |
| 655 | unsigned i; |
| 656 | for (i = 0; i < bv->data_len - 2; i++) { |
| 657 | uint16_t t = osmo_load16be(bv->data + i); |
| 658 | osmo_store16be(t << (n % 8), &tmp); |
| 659 | bv->data[i] = tmp[0]; |
| 660 | } |
| 661 | |
| 662 | bv->data[bv->data_len - 1] <<= (n % 8); |
| 663 | bv->cur_bit -= n; |
| 664 | } |
| 665 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 666 | /*! Add given array to bitvec |
Max | d479321 | 2016-03-17 11:51:08 +0100 | [diff] [blame] | 667 | * \param[in,out] bv bit vector to work with |
| 668 | * \param[in] array elements to be added |
| 669 | * \param[in] array_len length of array |
| 670 | * \param[in] dry_run indicates whether to return number of bits required |
| 671 | * instead of adding anything to bv for real |
| 672 | * \param[in] num_bits number of bits to consider in each element of array |
| 673 | * \returns number of bits necessary to add array elements if dry_run is true, |
| 674 | * 0 otherwise (only in this case bv is actually changed) |
| 675 | * |
| 676 | * N. B: no length checks are performed on bv - it's caller's job to ensure |
| 677 | * enough space is available - for example by calling with dry_run = true first. |
| 678 | * |
| 679 | * Useful for common pattern in CSN.1 spec which looks like: |
| 680 | * { 1 < XXX : bit (num_bits) > } ** 0 |
| 681 | * which means repeat any times (between 0 and infinity), |
| 682 | * start each repetition with 1, mark end of repetitions with 0 bit |
| 683 | * see app. note in 3GPP TS 24.007 ยง B.2.1 Rule A2 |
| 684 | */ |
| 685 | unsigned int bitvec_add_array(struct bitvec *bv, const uint32_t *array, |
| 686 | unsigned int array_len, bool dry_run, |
| 687 | unsigned int num_bits) |
| 688 | { |
| 689 | unsigned i, bits = 1; /* account for stop bit */ |
| 690 | for (i = 0; i < array_len; i++) { |
| 691 | if (dry_run) { |
| 692 | bits += (1 + num_bits); |
| 693 | } else { |
| 694 | bitvec_set_bit(bv, 1); |
| 695 | bitvec_set_uint(bv, array[i], num_bits); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | if (dry_run) |
| 700 | return bits; |
| 701 | |
| 702 | bitvec_set_bit(bv, 0); /* stop bit - end of the sequence */ |
| 703 | return 0; |
| 704 | } |
| 705 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 706 | /*! @} */ |