blob: 2b4e8c983de74bf0823469171c1551e8f30b09ac [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
Maxa15f05f2016-01-26 10:43:15 +01002 * (C) 2012 Ivan Klyuchnikov
Harald Weltee08da972017-11-13 01:00:26 +09003 * (C) 2015 by sysmocom - s.f.m.c. GmbH
Harald Welteec8b4502010-02-20 20:34:29 +01004 *
5 * All Rights Reserved
6 *
Harald Weltee08da972017-11-13 01:00:26 +09007 * SPDX-License-Identifier: GPL-2.0+
8 *
Harald Welteec8b4502010-02-20 20:34:29 +01009 * 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 Welteba6988b2011-08-17 12:46:48 +020025/*! \addtogroup bitvec
26 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020027 * 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 Welte96e2a002017-06-12 21:44:18 +020038
Harald Welteec8b4502010-02-20 20:34:29 +010039#include <errno.h>
40#include <stdint.h>
Jacob Erlbeck5f349be2015-12-21 16:04:03 +010041#include <string.h>
Maxa15f05f2016-01-26 10:43:15 +010042#include <stdio.h>
Max0a59e982016-02-05 13:55:37 +010043#include <stdbool.h>
Harald Welteec8b4502010-02-20 20:34:29 +010044
Max0a59e982016-02-05 13:55:37 +010045#include <osmocom/core/bits.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010046#include <osmocom/core/bitvec.h>
Harald Welte459a1802018-06-28 09:24:17 +020047#include <osmocom/core/panic.h>
Vadim Yanitskiy832d8b82020-02-19 04:20:08 +070048#include <osmocom/core/utils.h>
Harald Welteec8b4502010-02-20 20:34:29 +010049
50#define BITNUM_FROM_COMP(byte, bit) ((byte*8)+bit)
51
52static 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 */
60static uint8_t bitval2mask(enum bit_value bit, uint8_t bitnum)
61{
Harald Welteec8b4502010-02-20 20:34:29 +010062 switch (bit) {
63 case ZERO:
Vadim Yanitskiy00a55ae2019-07-17 16:41:17 +070064 return (0 << bitnum);
Harald Welteec8b4502010-02-20 20:34:29 +010065 case ONE:
Vadim Yanitskiy00a55ae2019-07-17 16:41:17 +070066 return (1 << bitnum);
Harald Welteec8b4502010-02-20 20:34:29 +010067 case L:
Vadim Yanitskiy00a55ae2019-07-17 16:41:17 +070068 return ((0x2b ^ (0 << bitnum)) & (1 << bitnum));
Harald Welteec8b4502010-02-20 20:34:29 +010069 case H:
Vadim Yanitskiy00a55ae2019-07-17 16:41:17 +070070 return ((0x2b ^ (1 << bitnum)) & (1 << bitnum));
Harald Welteec8b4502010-02-20 20:34:29 +010071 default:
72 return 0;
73 }
Harald Welteec8b4502010-02-20 20:34:29 +010074}
75
Neels Hofmeyr87e45502017-06-20 00:17:59 +020076/*! check if the bit is 0 or 1 for a given position inside a bitvec
Harald Welteba6988b2011-08-17 12:46:48 +020077 * \param[in] bv the bit vector on which to check
78 * \param[in] bitnr the bit number inside the bit vector to check
Harald Welte2d2e2cc2016-04-25 12:11:20 +020079 * \return value of the requested bit
Harald Welteba6988b2011-08-17 12:46:48 +020080 */
Harald Welted9abf012010-03-06 11:28:49 +010081enum bit_value bitvec_get_bit_pos(const struct bitvec *bv, unsigned int bitnr)
Harald Welteec8b4502010-02-20 20:34:29 +010082{
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 Hofmeyr87e45502017-06-20 00:17:59 +020098/*! check if the bit is L or H for a given position inside a bitvec
Harald Welteba6988b2011-08-17 12:46:48 +020099 * \param[in] bv the bit vector on which to check
100 * \param[in] bitnr the bit number inside the bit vector to check
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200101 * \return value of the requested bit
Harald Welteba6988b2011-08-17 12:46:48 +0200102 */
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000103enum 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.Eversbergdc0ebdf2010-10-24 11:59:33 +0200115 if ((bv->data[bytenum] & (1 << bitnum)) == bitval)
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000116 return H;
117
118 return L;
119}
120
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200121/*! get the Nth set bit inside the bit vector
Harald Welteba6988b2011-08-17 12:46:48 +0200122 * \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 Welted9abf012010-03-06 11:28:49 +0100126unsigned int bitvec_get_nth_set_bit(const struct bitvec *bv, unsigned int n)
Harald Welteec8b4502010-02-20 20:34:29 +0100127{
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 Hofmeyr87e45502017-06-20 00:17:59 +0200141/*! set a bit at given position in a bit vector
Harald Welteba6988b2011-08-17 12:46:48 +0200142 * \param[in] bv bit vector on which to operate
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100143 * \param[in] bitnr number of bit to be set
Harald Welteba6988b2011-08-17 12:46:48 +0200144 * \param[in] bit value to which the bit is to be set
Max912bc6f2016-01-28 12:07:12 +0100145 * \returns 0 on success, negative value on error
Harald Welteba6988b2011-08-17 12:46:48 +0200146 */
Holger Hans Peter Freyther511b4482016-07-13 12:26:10 +0200147inline int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnr,
Harald Welteec8b4502010-02-20 20:34:29 +0100148 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 Hofmeyr87e45502017-06-20 00:17:59 +0200168/*! set the next bit inside a bitvec
Harald Welteba6988b2011-08-17 12:46:48 +0200169 * \param[in] bv bit vector to be used
170 * \param[in] bit value of the bit to be set
Max912bc6f2016-01-28 12:07:12 +0100171 * \returns 0 on success, negative value on error
Harald Welteba6988b2011-08-17 12:46:48 +0200172 */
Holger Hans Peter Freyther511b4482016-07-13 12:26:10 +0200173inline int bitvec_set_bit(struct bitvec *bv, enum bit_value bit)
Harald Welteec8b4502010-02-20 20:34:29 +0100174{
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 Hofmeyr87e45502017-06-20 00:17:59 +0200184/*! get the next bit (low/high) inside a bitvec
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200185 * \return value of th next bit in the vector */
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000186int 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 Hofmeyr87e45502017-06-20 00:17:59 +0200197/*! set multiple bits (based on array of bitvals) at current pos
Harald Welteba6988b2011-08-17 12:46:48 +0200198 * \param[in] bv bit vector
199 * \param[in] bits array of \ref bit_value
200 * \param[in] count number of bits to set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200201 * \return 0 on success; negative in case of error */
Harald Welte14bf28a2016-06-27 15:19:10 +0200202int bitvec_set_bits(struct bitvec *bv, const enum bit_value *bits, unsigned int count)
Harald Welteec8b4502010-02-20 20:34:29 +0100203{
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
Max0b3db502017-10-18 13:48:10 +0200215/*! 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. */
221int bitvec_set_u64(struct bitvec *bv, uint64_t v, uint8_t num_bits, bool use_lh)
Harald Welteec8b4502010-02-20 20:34:29 +0100222{
Max0b3db502017-10-18 13:48:10 +0200223 uint8_t i;
224
225 if (num_bits > 64)
226 return -E2BIG;
227
Harald Welteec8b4502010-02-20 20:34:29 +0100228 for (i = 0; i < num_bits; i++) {
Max0b3db502017-10-18 13:48:10 +0200229 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 Welteec8b4502010-02-20 20:34:29 +0100235 rc = bitvec_set_bit(bv, bit);
Max0b3db502017-10-18 13:48:10 +0200236 if (rc != 0)
Harald Welteec8b4502010-02-20 20:34:29 +0100237 return rc;
238 }
239
240 return 0;
241}
242
Max0b3db502017-10-18 13:48:10 +0200243/*! set multiple bits (based on numeric value) at current pos.
244 * \return 0 in case of success; negative in case of error. */
245int 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 Hofmeyr87e45502017-06-20 00:17:59 +0200250/*! get multiple bits (num_bits) from beginning of vector (MSB side)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200251 * \return 16bit signed integer retrieved from bit vector */
Max0a59e982016-02-05 13:55:37 +0100252int16_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 Hofmeyr87e45502017-06-20 00:17:59 +0200263/*! get multiple bits (based on numeric value) from current pos
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200264 * \return integer value retrieved from bit vector */
Maxe49af082016-01-22 16:46:56 +0100265int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits)
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000266{
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 Pedrol4c951ad2020-11-13 12:08:59 +0100275 ui |= ((unsigned)1 << (num_bits - i - 1));
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000276 bv->cur_bit++;
277 }
278
279 return ui;
280}
281
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200282/*! fill num_bits with \fill starting from the current position
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200283 * \return 0 on success; negative otherwise (out of vector boundary)
Max0a59e982016-02-05 13:55:37 +0100284 */
285int 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 Yanitskiya500bc32020-02-19 04:58:35 +0700295/*! pad all remaining bits up to a given bit number
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200296 * \return 0 on success; negative otherwise */
Harald Welteec8b4502010-02-20 20:34:29 +0100297int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit)
298{
Max0a59e982016-02-05 13:55:37 +0100299 int n = up_to_bit - bv->cur_bit + 1;
300 if (n < 1)
301 return 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100302
Max0a59e982016-02-05 13:55:37 +0100303 return bitvec_fill(bv, n, L);
Harald Welteec8b4502010-02-20 20:34:29 +0100304}
Pablo Neira Ayuso36bdf2c2011-03-28 19:24:19 +0200305
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200306/*! find first bit set in bit vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200307 * \return 0 on success; negative otherwise */
Pablo Neira Ayuso36bdf2c2011-03-28 19:24:19 +0200308int 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 Welteba6988b2011-08-17 12:46:48 +0200320
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200321/*! get multiple bytes from current pos
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100322 * 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 Welte2d2e2cc2016-04-25 12:11:20 +0200326 * \return 0 on success; negative otherwise
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100327 */
Maxe49af082016-01-22 16:46:56 +0100328int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count)
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100329{
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 Hofmeyr87e45502017-06-20 00:17:59 +0200357/*! set multiple bytes at current pos
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100358 * 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 Welte2d2e2cc2016-04-25 12:11:20 +0200362 * \return 0 on success; negative otherwise
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100363 */
Maxe49af082016-01-22 16:46:56 +0100364int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count)
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100365{
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}
Maxa15f05f2016-01-26 10:43:15 +0100396
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200397/*! Allocate a bit vector
Alexander Couzens76e8cbd2019-06-16 22:26:08 +0200398 * \param[in] size Number of bytes in the vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200399 * \param[in] ctx Context from which to allocate
400 * \return pointer to allocated vector; NULL in case of error */
Maxa15f05f2016-01-26 10:43:15 +0100401struct bitvec *bitvec_alloc(unsigned int size, TALLOC_CTX *ctx)
402{
Vadim Yanitskiy4960ee42020-02-19 05:07:40 +0700403 struct bitvec *bv = talloc(ctx, struct bitvec);
Maxa15f05f2016-01-26 10:43:15 +0100404 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 Hofmeyr87e45502017-06-20 00:17:59 +0200418/*! Free a bit vector (release its memory)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200419 * \param[in] bit vector to free */
Maxa15f05f2016-01-26 10:43:15 +0100420void bitvec_free(struct bitvec *bv)
421{
Vadim Yanitskiy4c9a36c2020-02-10 14:21:52 +0700422 if (bv == NULL)
423 return;
Maxa15f05f2016-01-26 10:43:15 +0100424 talloc_free(bv->data);
425 talloc_free(bv);
426}
427
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200428/*! Export a bit vector to a buffer
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200429 * \param[in] bitvec (unpacked bits)
430 * \param[out] buffer for the unpacked bits
431 * \return number of bytes (= bits) copied */
Maxa15f05f2016-01-26 10:43:15 +0100432unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer)
433{
Vadim Yanitskiyc866b322020-02-19 05:10:57 +0700434 unsigned int i;
Maxa15f05f2016-01-26 10:43:15 +0100435 for (i = 0; i < bv->data_len; i++)
436 buffer[i] = bv->data[i];
437
438 return i;
439}
440
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200441/*! Copy buffer of unpacked bits into bit vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200442 * \param[in] buffer unpacked input bits
443 * \param[out] bv unpacked bit vector
444 * \return number of bytes (= bits) copied */
Maxa15f05f2016-01-26 10:43:15 +0100445unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer)
446{
Vadim Yanitskiyc866b322020-02-19 05:10:57 +0700447 unsigned int i;
Maxa15f05f2016-01-26 10:43:15 +0100448 for (i = 0; i < bv->data_len; i++)
449 bv->data[i] = buffer[i];
450
451 return i;
452}
453
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200454/*! read hexadecimap string into a bit vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200455 * \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 */
Maxa15f05f2016-01-26 10:43:15 +0100459int bitvec_unhex(struct bitvec *bv, const char *src)
460{
Vadim Yanitskiy832d8b82020-02-19 04:20:08 +0700461 int rc;
Holger Hans Peter Freyther2745b482016-01-27 17:08:02 +0100462
Vadim Yanitskiy832d8b82020-02-19 04:20:08 +0700463 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;
Maxa15f05f2016-01-26 10:43:15 +0100468 return 0;
469}
470
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200471/*! read part of the vector
Max912bc6f2016-01-28 12:07:12 +0100472 * \param[in] bv The boolean vector to work on
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100473 * \param[in,out] read_index Where reading supposed to start in the vector
Max912bc6f2016-01-28 12:07:12 +0100474 * \param[in] len How many bits to read from vector
475 * \returns read bits or negative value on error
476 */
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100477uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned int len)
Maxa15f05f2016-01-26 10:43:15 +0100478{
479 unsigned int i;
480 uint64_t ui = 0;
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100481 bv->cur_bit = *read_index;
Maxa15f05f2016-01-26 10:43:15 +0100482
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 Freythera9301a12016-01-30 10:54:43 +0100491 *read_index += len;
Maxa15f05f2016-01-26 10:43:15 +0100492 return ui;
493}
494
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200495/*! write into the vector
Max912bc6f2016-01-28 12:07:12 +0100496 * \param[in] bv The boolean vector to work on
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100497 * \param[in,out] write_index Where writing supposed to start in the vector
Max912bc6f2016-01-28 12:07:12 +0100498 * \param[in] len How many bits to write
Pau Espin Pedrol2b98cbe2020-01-03 17:36:11 +0100499 * \returns 0 on success, negative value on error
Max912bc6f2016-01-28 12:07:12 +0100500 */
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100501int bitvec_write_field(struct bitvec *bv, unsigned int *write_index, uint64_t val, unsigned int len)
Maxa15f05f2016-01-26 10:43:15 +0100502{
Maxa15f05f2016-01-26 10:43:15 +0100503 int rc;
Maxf0e392a2017-10-18 13:32:30 +0200504
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100505 bv->cur_bit = *write_index;
Maxf0e392a2017-10-18 13:32:30 +0200506
507 rc = bitvec_set_u64(bv, val, len, false);
508 if (rc != 0)
509 return rc;
510
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100511 *write_index += len;
Maxf0e392a2017-10-18 13:32:30 +0200512
Maxa15f05f2016-01-26 10:43:15 +0100513 return 0;
514}
515
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200516/*! convert enum to corresponding character
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200517 * \param v input value (bit)
518 * \return single character, either 0, 1, L or H */
Max0a59e982016-02-05 13:55:37 +0100519char 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 Welte459a1802018-06-28 09:24:17 +0200526 default: osmo_panic("unexpected input in bit_value_to_char"); return 'X';
Max0a59e982016-02-05 13:55:37 +0100527 }
528}
529
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200530/*! prints bit vector to provided string
Max0a59e982016-02-05 13:55:37 +0100531 * 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 */
534void 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 */
548static 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 Hofmeyr87e45502017-06-20 00:17:59 +0200569/*! force bit vector to all 0 and current bit to the beginnig of the vector */
Max0a59e982016-02-05 13:55:37 +0100570void bitvec_zero(struct bitvec *bv)
571{
572 bv->cur_bit = 0;
573 memset(bv->data, 0, bv->data_len);
574}
575
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200576/*! Return number (bits) of uninterrupted bit run in vector starting from the MSB
Max0a59e982016-02-05 13:55:37 +0100577 * \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 */
581unsigned 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 Hofmeyr87e45502017-06-20 00:17:59 +0200592/*! Return number (bits) of uninterrupted bit run in vector
Pravin Kumarvel848de8f2016-12-02 15:13:03 +0530593 * 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 */
600unsigned 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 Hofmeyr87e45502017-06-20 00:17:59 +0200637/*! Shifts bitvec to the left, n MSB bits lost */
Max0a59e982016-02-05 13:55:37 +0100638void 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 Hofmeyr87e45502017-06-20 00:17:59 +0200661/*! Add given array to bitvec
Maxd4793212016-03-17 11:51:08 +0100662 * \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 */
680unsigned 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 Munautdca7d2c2012-04-18 21:53:23 +0200701/*! @} */