blob: 6a54344c4e320b68487dbda6d82d34858898c0bc [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 *
Harald Welteec8b4502010-02-20 20:34:29 +010019 */
20
Harald Welteba6988b2011-08-17 12:46:48 +020021/*! \addtogroup bitvec
22 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020023 * Osmocom bit vector abstraction utility routines.
24 *
25 * These functions assume a MSB (most significant bit) first layout of the
26 * bits, so that for instance the 5 bit number abcde (a is MSB) can be
27 * embedded into a byte sequence like in xxxxxxab cdexxxxx. The bit count
28 * starts with the MSB, so the bits in a byte are numbered (MSB) 01234567 (LSB).
29 * Note that there are other incompatible encodings, like it is used
30 * for the EGPRS RLC data block headers (there the bits are numbered from LSB
31 * to MSB).
32 *
33 * \file bitvec.c */
Harald Welte96e2a002017-06-12 21:44:18 +020034
Harald Welteec8b4502010-02-20 20:34:29 +010035#include <errno.h>
36#include <stdint.h>
Jacob Erlbeck5f349be2015-12-21 16:04:03 +010037#include <string.h>
Maxa15f05f2016-01-26 10:43:15 +010038#include <stdio.h>
Max0a59e982016-02-05 13:55:37 +010039#include <stdbool.h>
Harald Welteec8b4502010-02-20 20:34:29 +010040
Max0a59e982016-02-05 13:55:37 +010041#include <osmocom/core/bits.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010042#include <osmocom/core/bitvec.h>
Harald Welte459a1802018-06-28 09:24:17 +020043#include <osmocom/core/panic.h>
Vadim Yanitskiy832d8b82020-02-19 04:20:08 +070044#include <osmocom/core/utils.h>
Harald Welteec8b4502010-02-20 20:34:29 +010045
46#define BITNUM_FROM_COMP(byte, bit) ((byte*8)+bit)
47
48static 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 */
56static uint8_t bitval2mask(enum bit_value bit, uint8_t bitnum)
57{
Harald Welteec8b4502010-02-20 20:34:29 +010058 switch (bit) {
59 case ZERO:
Vadim Yanitskiy00a55ae2019-07-17 16:41:17 +070060 return (0 << bitnum);
Harald Welteec8b4502010-02-20 20:34:29 +010061 case ONE:
Vadim Yanitskiy00a55ae2019-07-17 16:41:17 +070062 return (1 << bitnum);
Harald Welteec8b4502010-02-20 20:34:29 +010063 case L:
Vadim Yanitskiy00a55ae2019-07-17 16:41:17 +070064 return ((0x2b ^ (0 << bitnum)) & (1 << bitnum));
Harald Welteec8b4502010-02-20 20:34:29 +010065 case H:
Vadim Yanitskiy00a55ae2019-07-17 16:41:17 +070066 return ((0x2b ^ (1 << bitnum)) & (1 << bitnum));
Harald Welteec8b4502010-02-20 20:34:29 +010067 default:
68 return 0;
69 }
Harald Welteec8b4502010-02-20 20:34:29 +010070}
71
Neels Hofmeyr87e45502017-06-20 00:17:59 +020072/*! check if the bit is 0 or 1 for a given position inside a bitvec
Harald Welteba6988b2011-08-17 12:46:48 +020073 * \param[in] bv the bit vector on which to check
74 * \param[in] bitnr the bit number inside the bit vector to check
Harald Welte2d2e2cc2016-04-25 12:11:20 +020075 * \return value of the requested bit
Harald Welteba6988b2011-08-17 12:46:48 +020076 */
Harald Welted9abf012010-03-06 11:28:49 +010077enum bit_value bitvec_get_bit_pos(const struct bitvec *bv, unsigned int bitnr)
Harald Welteec8b4502010-02-20 20:34:29 +010078{
79 unsigned int bytenum = bytenum_from_bitnum(bitnr);
80 unsigned int bitnum = 7 - (bitnr % 8);
81 uint8_t bitval;
82
83 if (bytenum >= bv->data_len)
84 return -EINVAL;
85
86 bitval = bitval2mask(ONE, bitnum);
87
88 if (bv->data[bytenum] & bitval)
89 return ONE;
90
91 return ZERO;
92}
93
Neels Hofmeyr87e45502017-06-20 00:17:59 +020094/*! check if the bit is L or H for a given position inside a bitvec
Harald Welteba6988b2011-08-17 12:46:48 +020095 * \param[in] bv the bit vector on which to check
96 * \param[in] bitnr the bit number inside the bit vector to check
Harald Welte2d2e2cc2016-04-25 12:11:20 +020097 * \return value of the requested bit
Harald Welteba6988b2011-08-17 12:46:48 +020098 */
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +000099enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv,
100 unsigned int bitnr)
101{
102 unsigned int bytenum = bytenum_from_bitnum(bitnr);
103 unsigned int bitnum = 7 - (bitnr % 8);
104 uint8_t bitval;
105
106 if (bytenum >= bv->data_len)
107 return -EINVAL;
108
109 bitval = bitval2mask(H, bitnum);
110
Andreas.Eversbergdc0ebdf2010-10-24 11:59:33 +0200111 if ((bv->data[bytenum] & (1 << bitnum)) == bitval)
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000112 return H;
113
114 return L;
115}
116
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200117/*! get the Nth set bit inside the bit vector
Harald Welteba6988b2011-08-17 12:46:48 +0200118 * \param[in] bv the bit vector to use
119 * \param[in] n the bit number to get
120 * \returns the bit number (offset) of the Nth set bit in \a bv
121 */
Harald Welted9abf012010-03-06 11:28:49 +0100122unsigned int bitvec_get_nth_set_bit(const struct bitvec *bv, unsigned int n)
Harald Welteec8b4502010-02-20 20:34:29 +0100123{
124 unsigned int i, k = 0;
125
126 for (i = 0; i < bv->data_len*8; i++) {
127 if (bitvec_get_bit_pos(bv, i) == ONE) {
128 k++;
129 if (k == n)
130 return i;
131 }
132 }
133
134 return 0;
135}
136
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200137/*! set a bit at given position in a bit vector
Harald Welteba6988b2011-08-17 12:46:48 +0200138 * \param[in] bv bit vector on which to operate
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100139 * \param[in] bitnr number of bit to be set
Harald Welteba6988b2011-08-17 12:46:48 +0200140 * \param[in] bit value to which the bit is to be set
Max912bc6f2016-01-28 12:07:12 +0100141 * \returns 0 on success, negative value on error
Harald Welteba6988b2011-08-17 12:46:48 +0200142 */
Holger Hans Peter Freyther511b4482016-07-13 12:26:10 +0200143inline int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnr,
Harald Welteec8b4502010-02-20 20:34:29 +0100144 enum bit_value bit)
145{
146 unsigned int bytenum = bytenum_from_bitnum(bitnr);
147 unsigned int bitnum = 7 - (bitnr % 8);
148 uint8_t bitval;
149
150 if (bytenum >= bv->data_len)
151 return -EINVAL;
152
153 /* first clear the bit */
154 bitval = bitval2mask(ONE, bitnum);
155 bv->data[bytenum] &= ~bitval;
156
157 /* then set it to desired value */
158 bitval = bitval2mask(bit, bitnum);
159 bv->data[bytenum] |= bitval;
160
161 return 0;
162}
163
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200164/*! set the next bit inside a bitvec
Harald Welteba6988b2011-08-17 12:46:48 +0200165 * \param[in] bv bit vector to be used
166 * \param[in] bit value of the bit to be set
Max912bc6f2016-01-28 12:07:12 +0100167 * \returns 0 on success, negative value on error
Harald Welteba6988b2011-08-17 12:46:48 +0200168 */
Holger Hans Peter Freyther511b4482016-07-13 12:26:10 +0200169inline int bitvec_set_bit(struct bitvec *bv, enum bit_value bit)
Harald Welteec8b4502010-02-20 20:34:29 +0100170{
171 int rc;
172
173 rc = bitvec_set_bit_pos(bv, bv->cur_bit, bit);
174 if (!rc)
175 bv->cur_bit++;
176
177 return rc;
178}
179
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200180/*! get the next bit (low/high) inside a bitvec
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200181 * \return value of th next bit in the vector */
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000182int bitvec_get_bit_high(struct bitvec *bv)
183{
184 int rc;
185
186 rc = bitvec_get_bit_pos_high(bv, bv->cur_bit);
187 if (rc >= 0)
188 bv->cur_bit++;
189
190 return rc;
191}
192
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200193/*! set multiple bits (based on array of bitvals) at current pos
Harald Welteba6988b2011-08-17 12:46:48 +0200194 * \param[in] bv bit vector
195 * \param[in] bits array of \ref bit_value
196 * \param[in] count number of bits to set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200197 * \return 0 on success; negative in case of error */
Harald Welte14bf28a2016-06-27 15:19:10 +0200198int bitvec_set_bits(struct bitvec *bv, const enum bit_value *bits, unsigned int count)
Harald Welteec8b4502010-02-20 20:34:29 +0100199{
200 int i, rc;
201
202 for (i = 0; i < count; i++) {
203 rc = bitvec_set_bit(bv, bits[i]);
204 if (rc)
205 return rc;
206 }
207
208 return 0;
209}
210
Max0b3db502017-10-18 13:48:10 +0200211/*! set multiple bits (based on numeric value) at current pos.
212 * \param[in] bv bit vector.
213 * \param[in] v mask representing which bits needs to be set.
214 * \param[in] num_bits number of meaningful bits in the mask.
215 * \param[in] use_lh whether to interpret the bits as L/H values or as 0/1.
216 * \return 0 on success; negative in case of error. */
217int bitvec_set_u64(struct bitvec *bv, uint64_t v, uint8_t num_bits, bool use_lh)
Harald Welteec8b4502010-02-20 20:34:29 +0100218{
Max0b3db502017-10-18 13:48:10 +0200219 uint8_t i;
220
221 if (num_bits > 64)
222 return -E2BIG;
223
Harald Welteec8b4502010-02-20 20:34:29 +0100224 for (i = 0; i < num_bits; i++) {
Max0b3db502017-10-18 13:48:10 +0200225 int rc;
226 enum bit_value bit = use_lh ? L : 0;
227
228 if (v & ((uint64_t)1 << (num_bits - i - 1)))
229 bit = use_lh ? H : 1;
230
Harald Welteec8b4502010-02-20 20:34:29 +0100231 rc = bitvec_set_bit(bv, bit);
Max0b3db502017-10-18 13:48:10 +0200232 if (rc != 0)
Harald Welteec8b4502010-02-20 20:34:29 +0100233 return rc;
234 }
235
236 return 0;
237}
238
Max0b3db502017-10-18 13:48:10 +0200239/*! set multiple bits (based on numeric value) at current pos.
240 * \return 0 in case of success; negative in case of error. */
241int bitvec_set_uint(struct bitvec *bv, unsigned int ui, unsigned int num_bits)
242{
243 return bitvec_set_u64(bv, ui, num_bits, false);
244}
245
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200246/*! get multiple bits (num_bits) from beginning of vector (MSB side)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200247 * \return 16bit signed integer retrieved from bit vector */
Max0a59e982016-02-05 13:55:37 +0100248int16_t bitvec_get_int16_msb(const struct bitvec *bv, unsigned int num_bits)
249{
250 if (num_bits > 15 || bv->cur_bit < num_bits)
251 return -EINVAL;
252
253 if (num_bits < 9)
254 return bv->data[0] >> (8 - num_bits);
255
256 return osmo_load16be(bv->data) >> (16 - num_bits);
257}
258
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200259/*! get multiple bits (based on numeric value) from current pos
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200260 * \return integer value retrieved from bit vector */
Maxe49af082016-01-22 16:46:56 +0100261int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits)
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000262{
263 int i;
264 unsigned int ui = 0;
265
266 for (i = 0; i < num_bits; i++) {
267 int bit = bitvec_get_bit_pos(bv, bv->cur_bit);
268 if (bit < 0)
269 return bit;
270 if (bit)
Pau Espin Pedrol4c951ad2020-11-13 12:08:59 +0100271 ui |= ((unsigned)1 << (num_bits - i - 1));
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000272 bv->cur_bit++;
273 }
274
275 return ui;
276}
277
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200278/*! fill num_bits with \fill starting from the current position
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200279 * \return 0 on success; negative otherwise (out of vector boundary)
Max0a59e982016-02-05 13:55:37 +0100280 */
281int bitvec_fill(struct bitvec *bv, unsigned int num_bits, enum bit_value fill)
282{
283 unsigned i, stop = bv->cur_bit + num_bits;
284 for (i = bv->cur_bit; i < stop; i++)
285 if (bitvec_set_bit(bv, fill) < 0)
286 return -EINVAL;
287
288 return 0;
289}
290
Vadim Yanitskiya500bc32020-02-19 04:58:35 +0700291/*! pad all remaining bits up to a given bit number
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200292 * \return 0 on success; negative otherwise */
Harald Welteec8b4502010-02-20 20:34:29 +0100293int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit)
294{
Max0a59e982016-02-05 13:55:37 +0100295 int n = up_to_bit - bv->cur_bit + 1;
296 if (n < 1)
297 return 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100298
Max0a59e982016-02-05 13:55:37 +0100299 return bitvec_fill(bv, n, L);
Harald Welteec8b4502010-02-20 20:34:29 +0100300}
Pablo Neira Ayuso36bdf2c2011-03-28 19:24:19 +0200301
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200302/*! find first bit set in bit vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200303 * \return 0 on success; negative otherwise */
Pablo Neira Ayuso36bdf2c2011-03-28 19:24:19 +0200304int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n,
305 enum bit_value val)
306{
307 unsigned int i;
308
309 for (i = n; i < bv->data_len*8; i++) {
310 if (bitvec_get_bit_pos(bv, i) == val)
311 return i;
312 }
313
314 return -1;
315}
Harald Welteba6988b2011-08-17 12:46:48 +0200316
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200317/*! get multiple bytes from current pos
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100318 * Assumes MSB first encoding.
319 * \param[in] bv bit vector
320 * \param[in] bytes array
321 * \param[in] count number of bytes to copy
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200322 * \return 0 on success; negative otherwise
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100323 */
Maxe49af082016-01-22 16:46:56 +0100324int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count)
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100325{
326 int byte_offs = bytenum_from_bitnum(bv->cur_bit);
327 int bit_offs = bv->cur_bit % 8;
328 uint8_t c, last_c;
329 int i;
330 uint8_t *src;
331
332 if (byte_offs + count + (bit_offs ? 1 : 0) > bv->data_len)
333 return -EINVAL;
334
335 if (bit_offs == 0) {
336 memcpy(bytes, bv->data + byte_offs, count);
337 } else {
338 src = bv->data + byte_offs;
339 last_c = *(src++);
340 for (i = count; i > 0; i--) {
341 c = *(src++);
342 *(bytes++) =
343 (last_c << bit_offs) |
344 (c >> (8 - bit_offs));
345 last_c = c;
346 }
347 }
348
349 bv->cur_bit += count * 8;
350 return 0;
351}
352
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200353/*! set multiple bytes at current pos
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100354 * Assumes MSB first encoding.
355 * \param[in] bv bit vector
356 * \param[in] bytes array
357 * \param[in] count number of bytes to copy
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200358 * \return 0 on success; negative otherwise
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100359 */
Maxe49af082016-01-22 16:46:56 +0100360int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count)
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100361{
362 int byte_offs = bytenum_from_bitnum(bv->cur_bit);
363 int bit_offs = bv->cur_bit % 8;
364 uint8_t c, last_c;
365 int i;
366 uint8_t *dst;
367
368 if (byte_offs + count + (bit_offs ? 1 : 0) > bv->data_len)
369 return -EINVAL;
370
371 if (bit_offs == 0) {
372 memcpy(bv->data + byte_offs, bytes, count);
373 } else if (count > 0) {
374 dst = bv->data + byte_offs;
375 /* Get lower bits of first dst byte */
376 last_c = *dst >> (8 - bit_offs);
377 for (i = count; i > 0; i--) {
378 c = *(bytes++);
379 *(dst++) =
380 (last_c << (8 - bit_offs)) |
381 (c >> bit_offs);
382 last_c = c;
383 }
384 /* Overwrite lower bits of N+1 dst byte */
385 *dst = (*dst & ((1 << (8 - bit_offs)) - 1)) |
386 (last_c << (8 - bit_offs));
387 }
388
389 bv->cur_bit += count * 8;
390 return 0;
391}
Maxa15f05f2016-01-26 10:43:15 +0100392
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200393/*! Allocate a bit vector
Alexander Couzens76e8cbd2019-06-16 22:26:08 +0200394 * \param[in] size Number of bytes in the vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200395 * \param[in] ctx Context from which to allocate
396 * \return pointer to allocated vector; NULL in case of error */
Maxa15f05f2016-01-26 10:43:15 +0100397struct bitvec *bitvec_alloc(unsigned int size, TALLOC_CTX *ctx)
398{
Vadim Yanitskiy4960ee42020-02-19 05:07:40 +0700399 struct bitvec *bv = talloc(ctx, struct bitvec);
Maxa15f05f2016-01-26 10:43:15 +0100400 if (!bv)
401 return NULL;
402
403 bv->data = talloc_zero_array(bv, uint8_t, size);
404 if (!(bv->data)) {
405 talloc_free(bv);
406 return NULL;
407 }
408
409 bv->data_len = size;
410 bv->cur_bit = 0;
411 return bv;
412}
413
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200414/*! Free a bit vector (release its memory)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200415 * \param[in] bit vector to free */
Maxa15f05f2016-01-26 10:43:15 +0100416void bitvec_free(struct bitvec *bv)
417{
Vadim Yanitskiy4c9a36c2020-02-10 14:21:52 +0700418 if (bv == NULL)
419 return;
Maxa15f05f2016-01-26 10:43:15 +0100420 talloc_free(bv->data);
421 talloc_free(bv);
422}
423
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200424/*! Export a bit vector to a buffer
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200425 * \param[in] bitvec (unpacked bits)
426 * \param[out] buffer for the unpacked bits
427 * \return number of bytes (= bits) copied */
Maxa15f05f2016-01-26 10:43:15 +0100428unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer)
429{
Vadim Yanitskiyc866b322020-02-19 05:10:57 +0700430 unsigned int i;
Maxa15f05f2016-01-26 10:43:15 +0100431 for (i = 0; i < bv->data_len; i++)
432 buffer[i] = bv->data[i];
433
434 return i;
435}
436
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200437/*! Copy buffer of unpacked bits into bit vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200438 * \param[in] buffer unpacked input bits
439 * \param[out] bv unpacked bit vector
440 * \return number of bytes (= bits) copied */
Maxa15f05f2016-01-26 10:43:15 +0100441unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer)
442{
Vadim Yanitskiyc866b322020-02-19 05:10:57 +0700443 unsigned int i;
Maxa15f05f2016-01-26 10:43:15 +0100444 for (i = 0; i < bv->data_len; i++)
445 bv->data[i] = buffer[i];
446
447 return i;
448}
449
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200450/*! read hexadecimap string into a bit vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200451 * \param[in] src string containing hex digits
452 * \param[out] bv unpacked bit vector
453 * \return 0 in case of success; 1 in case of error
454 */
Maxa15f05f2016-01-26 10:43:15 +0100455int bitvec_unhex(struct bitvec *bv, const char *src)
456{
Vadim Yanitskiy832d8b82020-02-19 04:20:08 +0700457 int rc;
Holger Hans Peter Freyther2745b482016-01-27 17:08:02 +0100458
Vadim Yanitskiy832d8b82020-02-19 04:20:08 +0700459 rc = osmo_hexparse(src, bv->data, bv->data_len);
460 if (rc < 0) /* turn -1 into 1 in case of error */
461 return 1;
462
463 bv->cur_bit = rc * 8;
Maxa15f05f2016-01-26 10:43:15 +0100464 return 0;
465}
466
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200467/*! read part of the vector
Max912bc6f2016-01-28 12:07:12 +0100468 * \param[in] bv The boolean vector to work on
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100469 * \param[in,out] read_index Where reading supposed to start in the vector
Max912bc6f2016-01-28 12:07:12 +0100470 * \param[in] len How many bits to read from vector
Vadim Yanitskiyde3549a2021-11-17 06:34:48 +0300471 * \returns An integer made up of the bits read.
472 *
473 * In case of an error, errno is set to a non-zero value. Otherwise it holds 0.
Max912bc6f2016-01-28 12:07:12 +0100474 */
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100475uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned int len)
Maxa15f05f2016-01-26 10:43:15 +0100476{
477 unsigned int i;
478 uint64_t ui = 0;
Vadim Yanitskiy8a55a6c2021-11-17 06:36:38 +0300479
480 /* Prevent bitvec overrun due to incorrect index and/or length */
481 if (len && bytenum_from_bitnum(*read_index + len - 1) >= bv->data_len) {
482 errno = EOVERFLOW;
483 return 0;
484 }
485
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100486 bv->cur_bit = *read_index;
Vadim Yanitskiyde3549a2021-11-17 06:34:48 +0300487 errno = 0;
Maxa15f05f2016-01-26 10:43:15 +0100488
489 for (i = 0; i < len; i++) {
Vadim Yanitskiy49b60402021-11-17 06:21:28 +0300490 unsigned int bytenum = bytenum_from_bitnum(bv->cur_bit);
491 unsigned int bitnum = 7 - (bv->cur_bit % 8);
492
493 if (bv->data[bytenum] & (1 << bitnum))
Maxa15f05f2016-01-26 10:43:15 +0100494 ui |= ((uint64_t)1 << (len - i - 1));
495 bv->cur_bit++;
496 }
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100497 *read_index += len;
Maxa15f05f2016-01-26 10:43:15 +0100498 return ui;
499}
500
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200501/*! write into the vector
Max912bc6f2016-01-28 12:07:12 +0100502 * \param[in] bv The boolean vector to work on
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100503 * \param[in,out] write_index Where writing supposed to start in the vector
Max912bc6f2016-01-28 12:07:12 +0100504 * \param[in] len How many bits to write
Pau Espin Pedrol2b98cbe2020-01-03 17:36:11 +0100505 * \returns 0 on success, negative value on error
Max912bc6f2016-01-28 12:07:12 +0100506 */
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100507int bitvec_write_field(struct bitvec *bv, unsigned int *write_index, uint64_t val, unsigned int len)
Maxa15f05f2016-01-26 10:43:15 +0100508{
Maxa15f05f2016-01-26 10:43:15 +0100509 int rc;
Maxf0e392a2017-10-18 13:32:30 +0200510
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100511 bv->cur_bit = *write_index;
Maxf0e392a2017-10-18 13:32:30 +0200512
513 rc = bitvec_set_u64(bv, val, len, false);
514 if (rc != 0)
515 return rc;
516
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100517 *write_index += len;
Maxf0e392a2017-10-18 13:32:30 +0200518
Maxa15f05f2016-01-26 10:43:15 +0100519 return 0;
520}
521
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200522/*! convert enum to corresponding character
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200523 * \param v input value (bit)
524 * \return single character, either 0, 1, L or H */
Max0a59e982016-02-05 13:55:37 +0100525char bit_value_to_char(enum bit_value v)
526{
527 switch (v) {
528 case ZERO: return '0';
529 case ONE: return '1';
530 case L: return 'L';
531 case H: return 'H';
Harald Welte459a1802018-06-28 09:24:17 +0200532 default: osmo_panic("unexpected input in bit_value_to_char"); return 'X';
Max0a59e982016-02-05 13:55:37 +0100533 }
534}
535
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200536/*! prints bit vector to provided string
Max0a59e982016-02-05 13:55:37 +0100537 * It's caller's responsibility to ensure that we won't shoot him in the foot:
538 * the provided buffer should be at lest cur_bit + 1 bytes long
539 */
540void bitvec_to_string_r(const struct bitvec *bv, char *str)
541{
542 unsigned i, pos = 0;
543 char *cur = str;
544 for (i = 0; i < bv->cur_bit; i++) {
545 if (0 == i % 8)
546 *cur++ = ' ';
547 *cur++ = bit_value_to_char(bitvec_get_bit_pos(bv, i));
548 pos++;
549 }
550 *cur = 0;
551}
552
553/* we assume that x have at least 1 non-b bit */
554static inline unsigned leading_bits(uint8_t x, bool b)
555{
556 if (b) {
557 if (x < 0x80) return 0;
558 if (x < 0xC0) return 1;
559 if (x < 0xE0) return 2;
560 if (x < 0xF0) return 3;
561 if (x < 0xF8) return 4;
562 if (x < 0xFC) return 5;
563 if (x < 0xFE) return 6;
564 } else {
565 if (x > 0x7F) return 0;
566 if (x > 0x3F) return 1;
567 if (x > 0x1F) return 2;
568 if (x > 0xF) return 3;
569 if (x > 7) return 4;
570 if (x > 3) return 5;
571 if (x > 1) return 6;
572 }
573 return 7;
574}
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200575/*! force bit vector to all 0 and current bit to the beginnig of the vector */
Max0a59e982016-02-05 13:55:37 +0100576void bitvec_zero(struct bitvec *bv)
577{
578 bv->cur_bit = 0;
579 memset(bv->data, 0, bv->data_len);
580}
581
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200582/*! Return number (bits) of uninterrupted bit run in vector starting from the MSB
Max0a59e982016-02-05 13:55:37 +0100583 * \param[in] bv The boolean vector to work on
584 * \param[in] b The boolean, sequence of which is looked at from the vector start
585 * \returns Number of consecutive bits of \p b in \p bv
586 */
587unsigned bitvec_rl(const struct bitvec *bv, bool b)
588{
589 unsigned i;
590 for (i = 0; i < (bv->cur_bit % 8 ? bv->cur_bit / 8 + 1 : bv->cur_bit / 8); i++) {
591 if ( (b ? 0xFF : 0) != bv->data[i])
592 return i * 8 + leading_bits(bv->data[i], b);
593 }
594
595 return bv->cur_bit;
596}
597
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200598/*! Return number (bits) of uninterrupted bit run in vector
Pravin Kumarvel848de8f2016-12-02 15:13:03 +0530599 * starting from the current bit
600 * \param[in] bv The boolean vector to work on
601 * \param[in] b The boolean, sequence of 1's or 0's to be checked
602 * \param[in] max_bits Total Number of Uncmopresed bits
603 * \returns Number of consecutive bits of \p b in \p bv and cur_bit will
604 * \go to cur_bit + number of consecutive bit
605 */
606unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, int max_bits)
607{
608 unsigned i = 0;
609 unsigned j = 8;
610 int temp_res = 0;
611 int count = 0;
612 unsigned readIndex = bv->cur_bit;
613 unsigned remaining_bits = max_bits % 8;
614 unsigned remaining_bytes = max_bits / 8;
615 unsigned byte_mask = 0xFF;
616
617 if (readIndex % 8) {
618 for (j -= (readIndex % 8) ; j > 0 ; j--) {
619 if (readIndex < max_bits && bitvec_read_field(bv, &readIndex, 1) == b)
620 temp_res++;
621 else {
622 bv->cur_bit--;
623 return temp_res;
624 }
625 }
626 }
627 for (i = (readIndex / 8);
628 i < (remaining_bits ? remaining_bytes + 1 : remaining_bytes);
629 i++, count++) {
630 if ((b ? byte_mask : 0) != bv->data[i]) {
631 bv->cur_bit = (count * 8 +
632 leading_bits(bv->data[i], b) + readIndex);
633 return count * 8 +
634 leading_bits(bv->data[i], b) + temp_res;
635 }
636 }
637 bv->cur_bit = (temp_res + (count * 8)) + readIndex;
638 if (bv->cur_bit > max_bits)
639 bv->cur_bit = max_bits;
640 return (bv->cur_bit - readIndex + temp_res);
641}
642
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200643/*! Shifts bitvec to the left, n MSB bits lost */
Max0a59e982016-02-05 13:55:37 +0100644void bitvec_shiftl(struct bitvec *bv, unsigned n)
645{
646 if (0 == n)
647 return;
648 if (n >= bv->cur_bit) {
649 bitvec_zero(bv);
650 return;
651 }
652
653 memmove(bv->data, bv->data + n / 8, bv->data_len - n / 8);
654
655 uint8_t tmp[2];
656 unsigned i;
657 for (i = 0; i < bv->data_len - 2; i++) {
658 uint16_t t = osmo_load16be(bv->data + i);
659 osmo_store16be(t << (n % 8), &tmp);
660 bv->data[i] = tmp[0];
661 }
662
663 bv->data[bv->data_len - 1] <<= (n % 8);
664 bv->cur_bit -= n;
665}
666
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200667/*! Add given array to bitvec
Maxd4793212016-03-17 11:51:08 +0100668 * \param[in,out] bv bit vector to work with
669 * \param[in] array elements to be added
670 * \param[in] array_len length of array
671 * \param[in] dry_run indicates whether to return number of bits required
672 * instead of adding anything to bv for real
673 * \param[in] num_bits number of bits to consider in each element of array
674 * \returns number of bits necessary to add array elements if dry_run is true,
675 * 0 otherwise (only in this case bv is actually changed)
676 *
677 * N. B: no length checks are performed on bv - it's caller's job to ensure
678 * enough space is available - for example by calling with dry_run = true first.
679 *
680 * Useful for common pattern in CSN.1 spec which looks like:
681 * { 1 < XXX : bit (num_bits) > } ** 0
682 * which means repeat any times (between 0 and infinity),
683 * start each repetition with 1, mark end of repetitions with 0 bit
684 * see app. note in 3GPP TS 24.007 ยง B.2.1 Rule A2
685 */
686unsigned int bitvec_add_array(struct bitvec *bv, const uint32_t *array,
687 unsigned int array_len, bool dry_run,
688 unsigned int num_bits)
689{
690 unsigned i, bits = 1; /* account for stop bit */
691 for (i = 0; i < array_len; i++) {
692 if (dry_run) {
693 bits += (1 + num_bits);
694 } else {
695 bitvec_set_bit(bv, 1);
696 bitvec_set_uint(bv, array[i], num_bits);
697 }
698 }
699
700 if (dry_run)
701 return bits;
702
703 bitvec_set_bit(bv, 0); /* stop bit - end of the sequence */
704 return 0;
705}
706
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200707/*! @} */