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 |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [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 | * |
| 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 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 23 | /*! \addtogroup bitvec |
| 24 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 25 | * Osmocom bit vector abstraction utility routines. |
| 26 | * |
| 27 | * These functions assume a MSB (most significant bit) first layout of the |
| 28 | * bits, so that for instance the 5 bit number abcde (a is MSB) can be |
| 29 | * embedded into a byte sequence like in xxxxxxab cdexxxxx. The bit count |
| 30 | * starts with the MSB, so the bits in a byte are numbered (MSB) 01234567 (LSB). |
| 31 | * Note that there are other incompatible encodings, like it is used |
| 32 | * for the EGPRS RLC data block headers (there the bits are numbered from LSB |
| 33 | * to MSB). |
| 34 | * |
| 35 | * \file bitvec.c */ |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 36 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 37 | #include <errno.h> |
| 38 | #include <stdint.h> |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 39 | #include <string.h> |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 40 | #include <stdio.h> |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 41 | #include <stdbool.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 42 | |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 43 | #include <osmocom/core/bits.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 44 | #include <osmocom/core/bitvec.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 | { |
| 58 | int bitval; |
| 59 | |
| 60 | switch (bit) { |
| 61 | case ZERO: |
| 62 | bitval = (0 << bitnum); |
| 63 | break; |
| 64 | case ONE: |
| 65 | bitval = (1 << bitnum); |
| 66 | break; |
| 67 | case L: |
| 68 | bitval = ((0x2b ^ (0 << bitnum)) & (1 << bitnum)); |
| 69 | break; |
| 70 | case H: |
| 71 | bitval = ((0x2b ^ (1 << bitnum)) & (1 << bitnum)); |
| 72 | break; |
| 73 | default: |
| 74 | return 0; |
| 75 | } |
| 76 | return bitval; |
| 77 | } |
| 78 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 79 | /*! 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] | 80 | * \param[in] bv the bit vector on which to check |
| 81 | * \param[in] bitnr the bit number inside the bit vector to check |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 82 | * \return value of the requested bit |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 83 | */ |
Harald Welte | d9abf01 | 2010-03-06 11:28:49 +0100 | [diff] [blame] | 84 | 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] | 85 | { |
| 86 | unsigned int bytenum = bytenum_from_bitnum(bitnr); |
| 87 | unsigned int bitnum = 7 - (bitnr % 8); |
| 88 | uint8_t bitval; |
| 89 | |
| 90 | if (bytenum >= bv->data_len) |
| 91 | return -EINVAL; |
| 92 | |
| 93 | bitval = bitval2mask(ONE, bitnum); |
| 94 | |
| 95 | if (bv->data[bytenum] & bitval) |
| 96 | return ONE; |
| 97 | |
| 98 | return ZERO; |
| 99 | } |
| 100 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 101 | /*! 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] | 102 | * \param[in] bv the bit vector on which to check |
| 103 | * \param[in] bitnr the bit number inside the bit vector to check |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 104 | * \return value of the requested bit |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 105 | */ |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 106 | enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv, |
| 107 | unsigned int bitnr) |
| 108 | { |
| 109 | unsigned int bytenum = bytenum_from_bitnum(bitnr); |
| 110 | unsigned int bitnum = 7 - (bitnr % 8); |
| 111 | uint8_t bitval; |
| 112 | |
| 113 | if (bytenum >= bv->data_len) |
| 114 | return -EINVAL; |
| 115 | |
| 116 | bitval = bitval2mask(H, bitnum); |
| 117 | |
Andreas.Eversberg | dc0ebdf | 2010-10-24 11:59:33 +0200 | [diff] [blame] | 118 | if ((bv->data[bytenum] & (1 << bitnum)) == bitval) |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 119 | return H; |
| 120 | |
| 121 | return L; |
| 122 | } |
| 123 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 124 | /*! get the Nth set bit inside the bit vector |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 125 | * \param[in] bv the bit vector to use |
| 126 | * \param[in] n the bit number to get |
| 127 | * \returns the bit number (offset) of the Nth set bit in \a bv |
| 128 | */ |
Harald Welte | d9abf01 | 2010-03-06 11:28:49 +0100 | [diff] [blame] | 129 | 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] | 130 | { |
| 131 | unsigned int i, k = 0; |
| 132 | |
| 133 | for (i = 0; i < bv->data_len*8; i++) { |
| 134 | if (bitvec_get_bit_pos(bv, i) == ONE) { |
| 135 | k++; |
| 136 | if (k == n) |
| 137 | return i; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 144 | /*! set a bit at given position in a bit vector |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 145 | * \param[in] bv bit vector on which to operate |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 146 | * \param[in] bitnr number of bit to be set |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 147 | * \param[in] bit value to which the bit is to be set |
Max | 912bc6f | 2016-01-28 12:07:12 +0100 | [diff] [blame] | 148 | * \returns 0 on success, negative value on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 149 | */ |
Holger Hans Peter Freyther | 511b448 | 2016-07-13 12:26:10 +0200 | [diff] [blame] | 150 | inline int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnr, |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 151 | enum bit_value bit) |
| 152 | { |
| 153 | unsigned int bytenum = bytenum_from_bitnum(bitnr); |
| 154 | unsigned int bitnum = 7 - (bitnr % 8); |
| 155 | uint8_t bitval; |
| 156 | |
| 157 | if (bytenum >= bv->data_len) |
| 158 | return -EINVAL; |
| 159 | |
| 160 | /* first clear the bit */ |
| 161 | bitval = bitval2mask(ONE, bitnum); |
| 162 | bv->data[bytenum] &= ~bitval; |
| 163 | |
| 164 | /* then set it to desired value */ |
| 165 | bitval = bitval2mask(bit, bitnum); |
| 166 | bv->data[bytenum] |= bitval; |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 171 | /*! set the next bit inside a bitvec |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 172 | * \param[in] bv bit vector to be used |
| 173 | * \param[in] bit value of the bit to be set |
Max | 912bc6f | 2016-01-28 12:07:12 +0100 | [diff] [blame] | 174 | * \returns 0 on success, negative value on error |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 175 | */ |
Holger Hans Peter Freyther | 511b448 | 2016-07-13 12:26:10 +0200 | [diff] [blame] | 176 | inline int bitvec_set_bit(struct bitvec *bv, enum bit_value bit) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 177 | { |
| 178 | int rc; |
| 179 | |
| 180 | rc = bitvec_set_bit_pos(bv, bv->cur_bit, bit); |
| 181 | if (!rc) |
| 182 | bv->cur_bit++; |
| 183 | |
| 184 | return rc; |
| 185 | } |
| 186 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 187 | /*! get the next bit (low/high) inside a bitvec |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 188 | * \return value of th next bit in the vector */ |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 189 | int bitvec_get_bit_high(struct bitvec *bv) |
| 190 | { |
| 191 | int rc; |
| 192 | |
| 193 | rc = bitvec_get_bit_pos_high(bv, bv->cur_bit); |
| 194 | if (rc >= 0) |
| 195 | bv->cur_bit++; |
| 196 | |
| 197 | return rc; |
| 198 | } |
| 199 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 200 | /*! set multiple bits (based on array of bitvals) at current pos |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 201 | * \param[in] bv bit vector |
| 202 | * \param[in] bits array of \ref bit_value |
| 203 | * \param[in] count number of bits to set |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 204 | * \return 0 on success; negative in case of error */ |
Harald Welte | 14bf28a | 2016-06-27 15:19:10 +0200 | [diff] [blame] | 205 | 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] | 206 | { |
| 207 | int i, rc; |
| 208 | |
| 209 | for (i = 0; i < count; i++) { |
| 210 | rc = bitvec_set_bit(bv, bits[i]); |
| 211 | if (rc) |
| 212 | return rc; |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
Max | 0b3db50 | 2017-10-18 13:48:10 +0200 | [diff] [blame] | 218 | /*! set multiple bits (based on numeric value) at current pos. |
| 219 | * \param[in] bv bit vector. |
| 220 | * \param[in] v mask representing which bits needs to be set. |
| 221 | * \param[in] num_bits number of meaningful bits in the mask. |
| 222 | * \param[in] use_lh whether to interpret the bits as L/H values or as 0/1. |
| 223 | * \return 0 on success; negative in case of error. */ |
| 224 | 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] | 225 | { |
Max | 0b3db50 | 2017-10-18 13:48:10 +0200 | [diff] [blame] | 226 | uint8_t i; |
| 227 | |
| 228 | if (num_bits > 64) |
| 229 | return -E2BIG; |
| 230 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 231 | for (i = 0; i < num_bits; i++) { |
Max | 0b3db50 | 2017-10-18 13:48:10 +0200 | [diff] [blame] | 232 | int rc; |
| 233 | enum bit_value bit = use_lh ? L : 0; |
| 234 | |
| 235 | if (v & ((uint64_t)1 << (num_bits - i - 1))) |
| 236 | bit = use_lh ? H : 1; |
| 237 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 238 | rc = bitvec_set_bit(bv, bit); |
Max | 0b3db50 | 2017-10-18 13:48:10 +0200 | [diff] [blame] | 239 | if (rc != 0) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 240 | return rc; |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Max | 0b3db50 | 2017-10-18 13:48:10 +0200 | [diff] [blame] | 246 | /*! set multiple bits (based on numeric value) at current pos. |
| 247 | * \return 0 in case of success; negative in case of error. */ |
| 248 | int bitvec_set_uint(struct bitvec *bv, unsigned int ui, unsigned int num_bits) |
| 249 | { |
| 250 | return bitvec_set_u64(bv, ui, num_bits, false); |
| 251 | } |
| 252 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 253 | /*! get multiple bits (num_bits) from beginning of vector (MSB side) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 254 | * \return 16bit signed integer retrieved from bit vector */ |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 255 | int16_t bitvec_get_int16_msb(const struct bitvec *bv, unsigned int num_bits) |
| 256 | { |
| 257 | if (num_bits > 15 || bv->cur_bit < num_bits) |
| 258 | return -EINVAL; |
| 259 | |
| 260 | if (num_bits < 9) |
| 261 | return bv->data[0] >> (8 - num_bits); |
| 262 | |
| 263 | return osmo_load16be(bv->data) >> (16 - num_bits); |
| 264 | } |
| 265 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 266 | /*! get multiple bits (based on numeric value) from current pos |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 267 | * \return integer value retrieved from bit vector */ |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 268 | int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits) |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 269 | { |
| 270 | int i; |
| 271 | unsigned int ui = 0; |
| 272 | |
| 273 | for (i = 0; i < num_bits; i++) { |
| 274 | int bit = bitvec_get_bit_pos(bv, bv->cur_bit); |
| 275 | if (bit < 0) |
| 276 | return bit; |
| 277 | if (bit) |
| 278 | ui |= (1 << (num_bits - i - 1)); |
| 279 | bv->cur_bit++; |
| 280 | } |
| 281 | |
| 282 | return ui; |
| 283 | } |
| 284 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 285 | /*! fill num_bits with \fill starting from the current position |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 286 | * \return 0 on success; negative otherwise (out of vector boundary) |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 287 | */ |
| 288 | int bitvec_fill(struct bitvec *bv, unsigned int num_bits, enum bit_value fill) |
| 289 | { |
| 290 | unsigned i, stop = bv->cur_bit + num_bits; |
| 291 | for (i = bv->cur_bit; i < stop; i++) |
| 292 | if (bitvec_set_bit(bv, fill) < 0) |
| 293 | return -EINVAL; |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 298 | /*! pad all remaining bits up to num_bits |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 299 | * \return 0 on success; negative otherwise */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 300 | int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit) |
| 301 | { |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 302 | int n = up_to_bit - bv->cur_bit + 1; |
| 303 | if (n < 1) |
| 304 | return 0; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 305 | |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 306 | return bitvec_fill(bv, n, L); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 307 | } |
Pablo Neira Ayuso | 36bdf2c | 2011-03-28 19:24:19 +0200 | [diff] [blame] | 308 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 309 | /*! find first bit set in bit vector |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 310 | * \return 0 on success; negative otherwise */ |
Pablo Neira Ayuso | 36bdf2c | 2011-03-28 19:24:19 +0200 | [diff] [blame] | 311 | int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n, |
| 312 | enum bit_value val) |
| 313 | { |
| 314 | unsigned int i; |
| 315 | |
| 316 | for (i = n; i < bv->data_len*8; i++) { |
| 317 | if (bitvec_get_bit_pos(bv, i) == val) |
| 318 | return i; |
| 319 | } |
| 320 | |
| 321 | return -1; |
| 322 | } |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 323 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 324 | /*! get multiple bytes from current pos |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 325 | * Assumes MSB first encoding. |
| 326 | * \param[in] bv bit vector |
| 327 | * \param[in] bytes array |
| 328 | * \param[in] count number of bytes to copy |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 329 | * \return 0 on success; negative otherwise |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 330 | */ |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 331 | 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] | 332 | { |
| 333 | int byte_offs = bytenum_from_bitnum(bv->cur_bit); |
| 334 | int bit_offs = bv->cur_bit % 8; |
| 335 | uint8_t c, last_c; |
| 336 | int i; |
| 337 | uint8_t *src; |
| 338 | |
| 339 | if (byte_offs + count + (bit_offs ? 1 : 0) > bv->data_len) |
| 340 | return -EINVAL; |
| 341 | |
| 342 | if (bit_offs == 0) { |
| 343 | memcpy(bytes, bv->data + byte_offs, count); |
| 344 | } else { |
| 345 | src = bv->data + byte_offs; |
| 346 | last_c = *(src++); |
| 347 | for (i = count; i > 0; i--) { |
| 348 | c = *(src++); |
| 349 | *(bytes++) = |
| 350 | (last_c << bit_offs) | |
| 351 | (c >> (8 - bit_offs)); |
| 352 | last_c = c; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | bv->cur_bit += count * 8; |
| 357 | return 0; |
| 358 | } |
| 359 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 360 | /*! set multiple bytes at current pos |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 361 | * Assumes MSB first encoding. |
| 362 | * \param[in] bv bit vector |
| 363 | * \param[in] bytes array |
| 364 | * \param[in] count number of bytes to copy |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 365 | * \return 0 on success; negative otherwise |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 366 | */ |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 367 | 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] | 368 | { |
| 369 | int byte_offs = bytenum_from_bitnum(bv->cur_bit); |
| 370 | int bit_offs = bv->cur_bit % 8; |
| 371 | uint8_t c, last_c; |
| 372 | int i; |
| 373 | uint8_t *dst; |
| 374 | |
| 375 | if (byte_offs + count + (bit_offs ? 1 : 0) > bv->data_len) |
| 376 | return -EINVAL; |
| 377 | |
| 378 | if (bit_offs == 0) { |
| 379 | memcpy(bv->data + byte_offs, bytes, count); |
| 380 | } else if (count > 0) { |
| 381 | dst = bv->data + byte_offs; |
| 382 | /* Get lower bits of first dst byte */ |
| 383 | last_c = *dst >> (8 - bit_offs); |
| 384 | for (i = count; i > 0; i--) { |
| 385 | c = *(bytes++); |
| 386 | *(dst++) = |
| 387 | (last_c << (8 - bit_offs)) | |
| 388 | (c >> bit_offs); |
| 389 | last_c = c; |
| 390 | } |
| 391 | /* Overwrite lower bits of N+1 dst byte */ |
| 392 | *dst = (*dst & ((1 << (8 - bit_offs)) - 1)) | |
| 393 | (last_c << (8 - bit_offs)); |
| 394 | } |
| 395 | |
| 396 | bv->cur_bit += count * 8; |
| 397 | return 0; |
| 398 | } |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 399 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 400 | /*! Allocate a bit vector |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 401 | * \param[in] size Number of bits in the vector |
| 402 | * \param[in] ctx Context from which to allocate |
| 403 | * \return pointer to allocated vector; NULL in case of error */ |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 404 | struct bitvec *bitvec_alloc(unsigned int size, TALLOC_CTX *ctx) |
| 405 | { |
| 406 | struct bitvec *bv = talloc_zero(ctx, struct bitvec); |
| 407 | if (!bv) |
| 408 | return NULL; |
| 409 | |
| 410 | bv->data = talloc_zero_array(bv, uint8_t, size); |
| 411 | if (!(bv->data)) { |
| 412 | talloc_free(bv); |
| 413 | return NULL; |
| 414 | } |
| 415 | |
| 416 | bv->data_len = size; |
| 417 | bv->cur_bit = 0; |
| 418 | return bv; |
| 419 | } |
| 420 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 421 | /*! Free a bit vector (release its memory) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 422 | * \param[in] bit vector to free */ |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 423 | void bitvec_free(struct bitvec *bv) |
| 424 | { |
| 425 | talloc_free(bv->data); |
| 426 | talloc_free(bv); |
| 427 | } |
| 428 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 429 | /*! Export a bit vector to a buffer |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 430 | * \param[in] bitvec (unpacked bits) |
| 431 | * \param[out] buffer for the unpacked bits |
| 432 | * \return number of bytes (= bits) copied */ |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 433 | unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer) |
| 434 | { |
| 435 | unsigned int i = 0; |
| 436 | for (i = 0; i < bv->data_len; i++) |
| 437 | buffer[i] = bv->data[i]; |
| 438 | |
| 439 | return i; |
| 440 | } |
| 441 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 442 | /*! Copy buffer of unpacked bits into bit vector |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 443 | * \param[in] buffer unpacked input bits |
| 444 | * \param[out] bv unpacked bit vector |
| 445 | * \return number of bytes (= bits) copied */ |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 446 | unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer) |
| 447 | { |
| 448 | unsigned int i = 0; |
| 449 | for (i = 0; i < bv->data_len; i++) |
| 450 | bv->data[i] = buffer[i]; |
| 451 | |
| 452 | return i; |
| 453 | } |
| 454 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 455 | /*! read hexadecimap string into a bit vector |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 456 | * \param[in] src string containing hex digits |
| 457 | * \param[out] bv unpacked bit vector |
| 458 | * \return 0 in case of success; 1 in case of error |
| 459 | */ |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 460 | int bitvec_unhex(struct bitvec *bv, const char *src) |
| 461 | { |
Holger Hans Peter Freyther | 2745b48 | 2016-01-27 17:08:02 +0100 | [diff] [blame] | 462 | unsigned i; |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 463 | unsigned val; |
| 464 | unsigned write_index = 0; |
| 465 | unsigned digits = bv->data_len * 2; |
Holger Hans Peter Freyther | 2745b48 | 2016-01-27 17:08:02 +0100 | [diff] [blame] | 466 | |
| 467 | for (i = 0; i < digits; i++) { |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 468 | if (sscanf(src + i, "%1x", &val) < 1) { |
| 469 | return 1; |
| 470 | } |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 471 | bitvec_write_field(bv, &write_index, val, 4); |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 472 | } |
| 473 | return 0; |
| 474 | } |
| 475 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 476 | /*! read part of the vector |
Max | 912bc6f | 2016-01-28 12:07:12 +0100 | [diff] [blame] | 477 | * \param[in] bv The boolean vector to work on |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 478 | * \param[in,out] read_index Where reading supposed to start in the vector |
Max | 912bc6f | 2016-01-28 12:07:12 +0100 | [diff] [blame] | 479 | * \param[in] len How many bits to read from vector |
| 480 | * \returns read bits or negative value on error |
| 481 | */ |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 482 | 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] | 483 | { |
| 484 | unsigned int i; |
| 485 | uint64_t ui = 0; |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 486 | bv->cur_bit = *read_index; |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 487 | |
| 488 | for (i = 0; i < len; i++) { |
| 489 | int bit = bitvec_get_bit_pos((const struct bitvec *)bv, bv->cur_bit); |
| 490 | if (bit < 0) |
| 491 | return bit; |
| 492 | if (bit) |
| 493 | ui |= ((uint64_t)1 << (len - i - 1)); |
| 494 | bv->cur_bit++; |
| 495 | } |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 496 | *read_index += len; |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 497 | return ui; |
| 498 | } |
| 499 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 500 | /*! write into the vector |
Max | 912bc6f | 2016-01-28 12:07:12 +0100 | [diff] [blame] | 501 | * \param[in] bv The boolean vector to work on |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 502 | * \param[in,out] write_index Where writing supposed to start in the vector |
Max | 912bc6f | 2016-01-28 12:07:12 +0100 | [diff] [blame] | 503 | * \param[in] len How many bits to write |
| 504 | * \returns next write index or negative value on error |
| 505 | */ |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 506 | 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] | 507 | { |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 508 | int rc; |
Max | f0e392a | 2017-10-18 13:32:30 +0200 | [diff] [blame] | 509 | |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 510 | bv->cur_bit = *write_index; |
Max | f0e392a | 2017-10-18 13:32:30 +0200 | [diff] [blame] | 511 | |
| 512 | rc = bitvec_set_u64(bv, val, len, false); |
| 513 | if (rc != 0) |
| 514 | return rc; |
| 515 | |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 516 | *write_index += len; |
Max | f0e392a | 2017-10-18 13:32:30 +0200 | [diff] [blame] | 517 | |
Max | a15f05f | 2016-01-26 10:43:15 +0100 | [diff] [blame] | 518 | return 0; |
| 519 | } |
| 520 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 521 | /*! convert enum to corresponding character |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 522 | * \param v input value (bit) |
| 523 | * \return single character, either 0, 1, L or H */ |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 524 | char bit_value_to_char(enum bit_value v) |
| 525 | { |
| 526 | switch (v) { |
| 527 | case ZERO: return '0'; |
| 528 | case ONE: return '1'; |
| 529 | case L: return 'L'; |
| 530 | case H: return 'H'; |
| 531 | default: abort(); |
| 532 | } |
| 533 | } |
| 534 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 535 | /*! prints bit vector to provided string |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 536 | * It's caller's responsibility to ensure that we won't shoot him in the foot: |
| 537 | * the provided buffer should be at lest cur_bit + 1 bytes long |
| 538 | */ |
| 539 | void bitvec_to_string_r(const struct bitvec *bv, char *str) |
| 540 | { |
| 541 | unsigned i, pos = 0; |
| 542 | char *cur = str; |
| 543 | for (i = 0; i < bv->cur_bit; i++) { |
| 544 | if (0 == i % 8) |
| 545 | *cur++ = ' '; |
| 546 | *cur++ = bit_value_to_char(bitvec_get_bit_pos(bv, i)); |
| 547 | pos++; |
| 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 | */ |
| 605 | unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, int max_bits) |
| 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 | /*! @} */ |