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