Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 1 | /* bit vector utility routines */ |
| 2 | |
| 3 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 4 | * (C) 2015 by Sysmocom s.f.m.c. GmbH |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 24 | /*! \addtogroup bitvec |
| 25 | * @{ |
| 26 | */ |
| 27 | |
| 28 | /*! \file bitvec.c |
| 29 | * \brief Osmocom bit vector abstraction |
| 30 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 31 | |
| 32 | #include <errno.h> |
| 33 | #include <stdint.h> |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 34 | #include <string.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 35 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 36 | #include <osmocom/core/bitvec.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 37 | |
| 38 | #define BITNUM_FROM_COMP(byte, bit) ((byte*8)+bit) |
| 39 | |
| 40 | static inline unsigned int bytenum_from_bitnum(unsigned int bitnum) |
| 41 | { |
| 42 | unsigned int bytenum = bitnum / 8; |
| 43 | |
| 44 | return bytenum; |
| 45 | } |
| 46 | |
| 47 | /* convert ZERO/ONE/L/H to a bitmask at given pos in a byte */ |
| 48 | static uint8_t bitval2mask(enum bit_value bit, uint8_t bitnum) |
| 49 | { |
| 50 | int bitval; |
| 51 | |
| 52 | switch (bit) { |
| 53 | case ZERO: |
| 54 | bitval = (0 << bitnum); |
| 55 | break; |
| 56 | case ONE: |
| 57 | bitval = (1 << bitnum); |
| 58 | break; |
| 59 | case L: |
| 60 | bitval = ((0x2b ^ (0 << bitnum)) & (1 << bitnum)); |
| 61 | break; |
| 62 | case H: |
| 63 | bitval = ((0x2b ^ (1 << bitnum)) & (1 << bitnum)); |
| 64 | break; |
| 65 | default: |
| 66 | return 0; |
| 67 | } |
| 68 | return bitval; |
| 69 | } |
| 70 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 71 | /*! \brief check if the bit is 0 or 1 for a given position inside a bitvec |
| 72 | * \param[in] bv the bit vector on which to check |
| 73 | * \param[in] bitnr the bit number inside the bit vector to check |
| 74 | * \returns |
| 75 | */ |
Harald Welte | d9abf01 | 2010-03-06 11:28:49 +0100 | [diff] [blame] | 76 | 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] | 77 | { |
| 78 | unsigned int bytenum = bytenum_from_bitnum(bitnr); |
| 79 | unsigned int bitnum = 7 - (bitnr % 8); |
| 80 | uint8_t bitval; |
| 81 | |
| 82 | if (bytenum >= bv->data_len) |
| 83 | return -EINVAL; |
| 84 | |
| 85 | bitval = bitval2mask(ONE, bitnum); |
| 86 | |
| 87 | if (bv->data[bytenum] & bitval) |
| 88 | return ONE; |
| 89 | |
| 90 | return ZERO; |
| 91 | } |
| 92 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 93 | /*! \brief check if the bit is L or H for a given position inside a bitvec |
| 94 | * \param[in] bv the bit vector on which to check |
| 95 | * \param[in] bitnr the bit number inside the bit vector to check |
| 96 | */ |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 97 | enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv, |
| 98 | unsigned int bitnr) |
| 99 | { |
| 100 | unsigned int bytenum = bytenum_from_bitnum(bitnr); |
| 101 | unsigned int bitnum = 7 - (bitnr % 8); |
| 102 | uint8_t bitval; |
| 103 | |
| 104 | if (bytenum >= bv->data_len) |
| 105 | return -EINVAL; |
| 106 | |
| 107 | bitval = bitval2mask(H, bitnum); |
| 108 | |
Andreas.Eversberg | dc0ebdf | 2010-10-24 11:59:33 +0200 | [diff] [blame] | 109 | if ((bv->data[bytenum] & (1 << bitnum)) == bitval) |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 110 | return H; |
| 111 | |
| 112 | return L; |
| 113 | } |
| 114 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 115 | /*! \brief get the Nth set bit inside the bit vector |
| 116 | * \param[in] bv the bit vector to use |
| 117 | * \param[in] n the bit number to get |
| 118 | * \returns the bit number (offset) of the Nth set bit in \a bv |
| 119 | */ |
Harald Welte | d9abf01 | 2010-03-06 11:28:49 +0100 | [diff] [blame] | 120 | 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] | 121 | { |
| 122 | unsigned int i, k = 0; |
| 123 | |
| 124 | for (i = 0; i < bv->data_len*8; i++) { |
| 125 | if (bitvec_get_bit_pos(bv, i) == ONE) { |
| 126 | k++; |
| 127 | if (k == n) |
| 128 | return i; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 135 | /*! \brief set a bit at given position in a bit vector |
| 136 | * \param[in] bv bit vector on which to operate |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 137 | * \param[in] bitnr number of bit to be set |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 138 | * \param[in] bit value to which the bit is to be set |
| 139 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 140 | int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnr, |
| 141 | enum bit_value bit) |
| 142 | { |
| 143 | unsigned int bytenum = bytenum_from_bitnum(bitnr); |
| 144 | unsigned int bitnum = 7 - (bitnr % 8); |
| 145 | uint8_t bitval; |
| 146 | |
| 147 | if (bytenum >= bv->data_len) |
| 148 | return -EINVAL; |
| 149 | |
| 150 | /* first clear the bit */ |
| 151 | bitval = bitval2mask(ONE, bitnum); |
| 152 | bv->data[bytenum] &= ~bitval; |
| 153 | |
| 154 | /* then set it to desired value */ |
| 155 | bitval = bitval2mask(bit, bitnum); |
| 156 | bv->data[bytenum] |= bitval; |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 161 | /*! \brief set the next bit inside a bitvec |
| 162 | * \param[in] bv bit vector to be used |
| 163 | * \param[in] bit value of the bit to be set |
| 164 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 165 | int bitvec_set_bit(struct bitvec *bv, enum bit_value bit) |
| 166 | { |
| 167 | int rc; |
| 168 | |
| 169 | rc = bitvec_set_bit_pos(bv, bv->cur_bit, bit); |
| 170 | if (!rc) |
| 171 | bv->cur_bit++; |
| 172 | |
| 173 | return rc; |
| 174 | } |
| 175 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 176 | /*! \brief get the next bit (low/high) inside a bitvec */ |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 177 | int bitvec_get_bit_high(struct bitvec *bv) |
| 178 | { |
| 179 | int rc; |
| 180 | |
| 181 | rc = bitvec_get_bit_pos_high(bv, bv->cur_bit); |
| 182 | if (rc >= 0) |
| 183 | bv->cur_bit++; |
| 184 | |
| 185 | return rc; |
| 186 | } |
| 187 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 188 | /*! \brief set multiple bits (based on array of bitvals) at current pos |
| 189 | * \param[in] bv bit vector |
| 190 | * \param[in] bits array of \ref bit_value |
| 191 | * \param[in] count number of bits to set |
| 192 | */ |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 193 | int bitvec_set_bits(struct bitvec *bv, enum bit_value *bits, unsigned int count) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 194 | { |
| 195 | int i, rc; |
| 196 | |
| 197 | for (i = 0; i < count; i++) { |
| 198 | rc = bitvec_set_bit(bv, bits[i]); |
| 199 | if (rc) |
| 200 | return rc; |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 206 | /*! \brief set multiple bits (based on numeric value) at current pos */ |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 207 | int bitvec_set_uint(struct bitvec *bv, unsigned int ui, unsigned int num_bits) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 208 | { |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 209 | int rc; |
| 210 | unsigned i; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 211 | for (i = 0; i < num_bits; i++) { |
| 212 | int bit = 0; |
| 213 | if (ui & (1 << (num_bits - i - 1))) |
| 214 | bit = 1; |
| 215 | rc = bitvec_set_bit(bv, bit); |
| 216 | if (rc) |
| 217 | return rc; |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 223 | /*! \brief get multiple bits (based on numeric value) from current pos */ |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 224 | int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits) |
Andreas.Eversberg | 0ebd688 | 2010-05-09 09:36:54 +0000 | [diff] [blame] | 225 | { |
| 226 | int i; |
| 227 | unsigned int ui = 0; |
| 228 | |
| 229 | for (i = 0; i < num_bits; i++) { |
| 230 | int bit = bitvec_get_bit_pos(bv, bv->cur_bit); |
| 231 | if (bit < 0) |
| 232 | return bit; |
| 233 | if (bit) |
| 234 | ui |= (1 << (num_bits - i - 1)); |
| 235 | bv->cur_bit++; |
| 236 | } |
| 237 | |
| 238 | return ui; |
| 239 | } |
| 240 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 241 | /*! \brief pad all remaining bits up to num_bits */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 242 | int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit) |
| 243 | { |
| 244 | unsigned int i; |
| 245 | |
| 246 | for (i = bv->cur_bit; i <= up_to_bit; i++) |
| 247 | bitvec_set_bit(bv, L); |
| 248 | |
| 249 | return 0; |
| 250 | } |
Pablo Neira Ayuso | 36bdf2c | 2011-03-28 19:24:19 +0200 | [diff] [blame] | 251 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 252 | /*! \brief find first bit set in bit vector */ |
Pablo Neira Ayuso | 36bdf2c | 2011-03-28 19:24:19 +0200 | [diff] [blame] | 253 | int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n, |
| 254 | enum bit_value val) |
| 255 | { |
| 256 | unsigned int i; |
| 257 | |
| 258 | for (i = n; i < bv->data_len*8; i++) { |
| 259 | if (bitvec_get_bit_pos(bv, i) == val) |
| 260 | return i; |
| 261 | } |
| 262 | |
| 263 | return -1; |
| 264 | } |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 265 | |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 266 | /*! \brief get multiple bytes from current pos |
| 267 | * Assumes MSB first encoding. |
| 268 | * \param[in] bv bit vector |
| 269 | * \param[in] bytes array |
| 270 | * \param[in] count number of bytes to copy |
| 271 | */ |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 272 | 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] | 273 | { |
| 274 | int byte_offs = bytenum_from_bitnum(bv->cur_bit); |
| 275 | int bit_offs = bv->cur_bit % 8; |
| 276 | uint8_t c, last_c; |
| 277 | int i; |
| 278 | uint8_t *src; |
| 279 | |
| 280 | if (byte_offs + count + (bit_offs ? 1 : 0) > bv->data_len) |
| 281 | return -EINVAL; |
| 282 | |
| 283 | if (bit_offs == 0) { |
| 284 | memcpy(bytes, bv->data + byte_offs, count); |
| 285 | } else { |
| 286 | src = bv->data + byte_offs; |
| 287 | last_c = *(src++); |
| 288 | for (i = count; i > 0; i--) { |
| 289 | c = *(src++); |
| 290 | *(bytes++) = |
| 291 | (last_c << bit_offs) | |
| 292 | (c >> (8 - bit_offs)); |
| 293 | last_c = c; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | bv->cur_bit += count * 8; |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | /*! \brief set multiple bytes at current pos |
| 302 | * Assumes MSB first encoding. |
| 303 | * \param[in] bv bit vector |
| 304 | * \param[in] bytes array |
| 305 | * \param[in] count number of bytes to copy |
| 306 | */ |
Max | e49af08 | 2016-01-22 16:46:56 +0100 | [diff] [blame] | 307 | 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] | 308 | { |
| 309 | int byte_offs = bytenum_from_bitnum(bv->cur_bit); |
| 310 | int bit_offs = bv->cur_bit % 8; |
| 311 | uint8_t c, last_c; |
| 312 | int i; |
| 313 | uint8_t *dst; |
| 314 | |
| 315 | if (byte_offs + count + (bit_offs ? 1 : 0) > bv->data_len) |
| 316 | return -EINVAL; |
| 317 | |
| 318 | if (bit_offs == 0) { |
| 319 | memcpy(bv->data + byte_offs, bytes, count); |
| 320 | } else if (count > 0) { |
| 321 | dst = bv->data + byte_offs; |
| 322 | /* Get lower bits of first dst byte */ |
| 323 | last_c = *dst >> (8 - bit_offs); |
| 324 | for (i = count; i > 0; i--) { |
| 325 | c = *(bytes++); |
| 326 | *(dst++) = |
| 327 | (last_c << (8 - bit_offs)) | |
| 328 | (c >> bit_offs); |
| 329 | last_c = c; |
| 330 | } |
| 331 | /* Overwrite lower bits of N+1 dst byte */ |
| 332 | *dst = (*dst & ((1 << (8 - bit_offs)) - 1)) | |
| 333 | (last_c << (8 - bit_offs)); |
| 334 | } |
| 335 | |
| 336 | bv->cur_bit += count * 8; |
| 337 | return 0; |
| 338 | } |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 339 | /*! @} */ |