blob: 350f7383b889a0ee02b83397bd5ff2951e0e3d65 [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{
Harald Weltec85aaed2022-01-09 11:56:19 +0100200 unsigned int i;
201 int rc;
Harald Welteec8b4502010-02-20 20:34:29 +0100202
203 for (i = 0; i < count; i++) {
204 rc = bitvec_set_bit(bv, bits[i]);
205 if (rc)
206 return rc;
207 }
208
209 return 0;
210}
211
Max0b3db502017-10-18 13:48:10 +0200212/*! set multiple bits (based on numeric value) at current pos.
213 * \param[in] bv bit vector.
214 * \param[in] v mask representing which bits needs to be set.
215 * \param[in] num_bits number of meaningful bits in the mask.
216 * \param[in] use_lh whether to interpret the bits as L/H values or as 0/1.
217 * \return 0 on success; negative in case of error. */
218int bitvec_set_u64(struct bitvec *bv, uint64_t v, uint8_t num_bits, bool use_lh)
Harald Welteec8b4502010-02-20 20:34:29 +0100219{
Max0b3db502017-10-18 13:48:10 +0200220 uint8_t i;
221
222 if (num_bits > 64)
223 return -E2BIG;
224
Harald Welteec8b4502010-02-20 20:34:29 +0100225 for (i = 0; i < num_bits; i++) {
Max0b3db502017-10-18 13:48:10 +0200226 int rc;
227 enum bit_value bit = use_lh ? L : 0;
228
229 if (v & ((uint64_t)1 << (num_bits - i - 1)))
230 bit = use_lh ? H : 1;
231
Harald Welteec8b4502010-02-20 20:34:29 +0100232 rc = bitvec_set_bit(bv, bit);
Max0b3db502017-10-18 13:48:10 +0200233 if (rc != 0)
Harald Welteec8b4502010-02-20 20:34:29 +0100234 return rc;
235 }
236
237 return 0;
238}
239
Max0b3db502017-10-18 13:48:10 +0200240/*! set multiple bits (based on numeric value) at current pos.
241 * \return 0 in case of success; negative in case of error. */
242int bitvec_set_uint(struct bitvec *bv, unsigned int ui, unsigned int num_bits)
243{
244 return bitvec_set_u64(bv, ui, num_bits, false);
245}
246
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200247/*! get multiple bits (num_bits) from beginning of vector (MSB side)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200248 * \return 16bit signed integer retrieved from bit vector */
Max0a59e982016-02-05 13:55:37 +0100249int16_t bitvec_get_int16_msb(const struct bitvec *bv, unsigned int num_bits)
250{
251 if (num_bits > 15 || bv->cur_bit < num_bits)
252 return -EINVAL;
253
254 if (num_bits < 9)
255 return bv->data[0] >> (8 - num_bits);
256
257 return osmo_load16be(bv->data) >> (16 - num_bits);
258}
259
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200260/*! get multiple bits (based on numeric value) from current pos
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200261 * \return integer value retrieved from bit vector */
Maxe49af082016-01-22 16:46:56 +0100262int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits)
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000263{
Harald Weltec85aaed2022-01-09 11:56:19 +0100264 unsigned int i;
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000265 unsigned int ui = 0;
266
267 for (i = 0; i < num_bits; i++) {
268 int bit = bitvec_get_bit_pos(bv, bv->cur_bit);
269 if (bit < 0)
270 return bit;
271 if (bit)
Pau Espin Pedrol4c951ad2020-11-13 12:08:59 +0100272 ui |= ((unsigned)1 << (num_bits - i - 1));
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000273 bv->cur_bit++;
274 }
275
276 return ui;
277}
278
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200279/*! fill num_bits with \fill starting from the current position
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200280 * \return 0 on success; negative otherwise (out of vector boundary)
Max0a59e982016-02-05 13:55:37 +0100281 */
282int bitvec_fill(struct bitvec *bv, unsigned int num_bits, enum bit_value fill)
283{
284 unsigned i, stop = bv->cur_bit + num_bits;
285 for (i = bv->cur_bit; i < stop; i++)
286 if (bitvec_set_bit(bv, fill) < 0)
287 return -EINVAL;
288
289 return 0;
290}
291
Vadim Yanitskiya500bc32020-02-19 04:58:35 +0700292/*! pad all remaining bits up to a given bit number
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200293 * \return 0 on success; negative otherwise */
Harald Welteec8b4502010-02-20 20:34:29 +0100294int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit)
295{
Max0a59e982016-02-05 13:55:37 +0100296 int n = up_to_bit - bv->cur_bit + 1;
297 if (n < 1)
298 return 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100299
Max0a59e982016-02-05 13:55:37 +0100300 return bitvec_fill(bv, n, L);
Harald Welteec8b4502010-02-20 20:34:29 +0100301}
Pablo Neira Ayuso36bdf2c2011-03-28 19:24:19 +0200302
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200303/*! find first bit set in bit vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200304 * \return 0 on success; negative otherwise */
Pablo Neira Ayuso36bdf2c2011-03-28 19:24:19 +0200305int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n,
306 enum bit_value val)
307{
308 unsigned int i;
309
310 for (i = n; i < bv->data_len*8; i++) {
311 if (bitvec_get_bit_pos(bv, i) == val)
312 return i;
313 }
314
315 return -1;
316}
Harald Welteba6988b2011-08-17 12:46:48 +0200317
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200318/*! get multiple bytes from current pos
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100319 * Assumes MSB first encoding.
320 * \param[in] bv bit vector
321 * \param[in] bytes array
322 * \param[in] count number of bytes to copy
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200323 * \return 0 on success; negative otherwise
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100324 */
Maxe49af082016-01-22 16:46:56 +0100325int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count)
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100326{
327 int byte_offs = bytenum_from_bitnum(bv->cur_bit);
328 int bit_offs = bv->cur_bit % 8;
329 uint8_t c, last_c;
330 int i;
331 uint8_t *src;
332
333 if (byte_offs + count + (bit_offs ? 1 : 0) > bv->data_len)
334 return -EINVAL;
335
336 if (bit_offs == 0) {
337 memcpy(bytes, bv->data + byte_offs, count);
338 } else {
339 src = bv->data + byte_offs;
340 last_c = *(src++);
341 for (i = count; i > 0; i--) {
342 c = *(src++);
343 *(bytes++) =
344 (last_c << bit_offs) |
345 (c >> (8 - bit_offs));
346 last_c = c;
347 }
348 }
349
350 bv->cur_bit += count * 8;
351 return 0;
352}
353
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200354/*! set multiple bytes at current pos
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100355 * Assumes MSB first encoding.
356 * \param[in] bv bit vector
357 * \param[in] bytes array
358 * \param[in] count number of bytes to copy
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200359 * \return 0 on success; negative otherwise
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100360 */
Maxe49af082016-01-22 16:46:56 +0100361int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count)
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100362{
363 int byte_offs = bytenum_from_bitnum(bv->cur_bit);
364 int bit_offs = bv->cur_bit % 8;
365 uint8_t c, last_c;
366 int i;
367 uint8_t *dst;
368
369 if (byte_offs + count + (bit_offs ? 1 : 0) > bv->data_len)
370 return -EINVAL;
371
372 if (bit_offs == 0) {
373 memcpy(bv->data + byte_offs, bytes, count);
374 } else if (count > 0) {
375 dst = bv->data + byte_offs;
376 /* Get lower bits of first dst byte */
377 last_c = *dst >> (8 - bit_offs);
378 for (i = count; i > 0; i--) {
379 c = *(bytes++);
380 *(dst++) =
381 (last_c << (8 - bit_offs)) |
382 (c >> bit_offs);
383 last_c = c;
384 }
385 /* Overwrite lower bits of N+1 dst byte */
386 *dst = (*dst & ((1 << (8 - bit_offs)) - 1)) |
387 (last_c << (8 - bit_offs));
388 }
389
390 bv->cur_bit += count * 8;
391 return 0;
392}
Maxa15f05f2016-01-26 10:43:15 +0100393
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200394/*! Allocate a bit vector
Alexander Couzens76e8cbd2019-06-16 22:26:08 +0200395 * \param[in] size Number of bytes in the vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200396 * \param[in] ctx Context from which to allocate
397 * \return pointer to allocated vector; NULL in case of error */
Maxa15f05f2016-01-26 10:43:15 +0100398struct bitvec *bitvec_alloc(unsigned int size, TALLOC_CTX *ctx)
399{
Vadim Yanitskiy4960ee42020-02-19 05:07:40 +0700400 struct bitvec *bv = talloc(ctx, struct bitvec);
Maxa15f05f2016-01-26 10:43:15 +0100401 if (!bv)
402 return NULL;
403
404 bv->data = talloc_zero_array(bv, uint8_t, size);
405 if (!(bv->data)) {
406 talloc_free(bv);
407 return NULL;
408 }
409
410 bv->data_len = size;
411 bv->cur_bit = 0;
412 return bv;
413}
414
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200415/*! Free a bit vector (release its memory)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200416 * \param[in] bit vector to free */
Maxa15f05f2016-01-26 10:43:15 +0100417void bitvec_free(struct bitvec *bv)
418{
Vadim Yanitskiy4c9a36c2020-02-10 14:21:52 +0700419 if (bv == NULL)
420 return;
Maxa15f05f2016-01-26 10:43:15 +0100421 talloc_free(bv->data);
422 talloc_free(bv);
423}
424
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200425/*! Export a bit vector to a buffer
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200426 * \param[in] bitvec (unpacked bits)
427 * \param[out] buffer for the unpacked bits
428 * \return number of bytes (= bits) copied */
Maxa15f05f2016-01-26 10:43:15 +0100429unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer)
430{
Vadim Yanitskiyc866b322020-02-19 05:10:57 +0700431 unsigned int i;
Maxa15f05f2016-01-26 10:43:15 +0100432 for (i = 0; i < bv->data_len; i++)
433 buffer[i] = bv->data[i];
434
435 return i;
436}
437
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200438/*! Copy buffer of unpacked bits into bit vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200439 * \param[in] buffer unpacked input bits
440 * \param[out] bv unpacked bit vector
441 * \return number of bytes (= bits) copied */
Maxa15f05f2016-01-26 10:43:15 +0100442unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer)
443{
Vadim Yanitskiyc866b322020-02-19 05:10:57 +0700444 unsigned int i;
Maxa15f05f2016-01-26 10:43:15 +0100445 for (i = 0; i < bv->data_len; i++)
446 bv->data[i] = buffer[i];
447
448 return i;
449}
450
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200451/*! read hexadecimap string into a bit vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200452 * \param[in] src string containing hex digits
453 * \param[out] bv unpacked bit vector
454 * \return 0 in case of success; 1 in case of error
455 */
Maxa15f05f2016-01-26 10:43:15 +0100456int bitvec_unhex(struct bitvec *bv, const char *src)
457{
Vadim Yanitskiy832d8b82020-02-19 04:20:08 +0700458 int rc;
Holger Hans Peter Freyther2745b482016-01-27 17:08:02 +0100459
Vadim Yanitskiy832d8b82020-02-19 04:20:08 +0700460 rc = osmo_hexparse(src, bv->data, bv->data_len);
461 if (rc < 0) /* turn -1 into 1 in case of error */
462 return 1;
463
464 bv->cur_bit = rc * 8;
Maxa15f05f2016-01-26 10:43:15 +0100465 return 0;
466}
467
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200468/*! read part of the vector
Max912bc6f2016-01-28 12:07:12 +0100469 * \param[in] bv The boolean vector to work on
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100470 * \param[in,out] read_index Where reading supposed to start in the vector
Max912bc6f2016-01-28 12:07:12 +0100471 * \param[in] len How many bits to read from vector
Vadim Yanitskiyde3549a2021-11-17 06:34:48 +0300472 * \returns An integer made up of the bits read.
473 *
474 * In case of an error, errno is set to a non-zero value. Otherwise it holds 0.
Max912bc6f2016-01-28 12:07:12 +0100475 */
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100476uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned int len)
Maxa15f05f2016-01-26 10:43:15 +0100477{
478 unsigned int i;
479 uint64_t ui = 0;
Vadim Yanitskiy8a55a6c2021-11-17 06:36:38 +0300480
481 /* Prevent bitvec overrun due to incorrect index and/or length */
482 if (len && bytenum_from_bitnum(*read_index + len - 1) >= bv->data_len) {
483 errno = EOVERFLOW;
484 return 0;
485 }
486
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100487 bv->cur_bit = *read_index;
Vadim Yanitskiyde3549a2021-11-17 06:34:48 +0300488 errno = 0;
Maxa15f05f2016-01-26 10:43:15 +0100489
490 for (i = 0; i < len; i++) {
Vadim Yanitskiy49b60402021-11-17 06:21:28 +0300491 unsigned int bytenum = bytenum_from_bitnum(bv->cur_bit);
492 unsigned int bitnum = 7 - (bv->cur_bit % 8);
493
494 if (bv->data[bytenum] & (1 << bitnum))
Maxa15f05f2016-01-26 10:43:15 +0100495 ui |= ((uint64_t)1 << (len - i - 1));
496 bv->cur_bit++;
497 }
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100498 *read_index += len;
Maxa15f05f2016-01-26 10:43:15 +0100499 return ui;
500}
501
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200502/*! write into the vector
Max912bc6f2016-01-28 12:07:12 +0100503 * \param[in] bv The boolean vector to work on
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100504 * \param[in,out] write_index Where writing supposed to start in the vector
Max912bc6f2016-01-28 12:07:12 +0100505 * \param[in] len How many bits to write
Pau Espin Pedrol2b98cbe2020-01-03 17:36:11 +0100506 * \returns 0 on success, negative value on error
Max912bc6f2016-01-28 12:07:12 +0100507 */
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100508int bitvec_write_field(struct bitvec *bv, unsigned int *write_index, uint64_t val, unsigned int len)
Maxa15f05f2016-01-26 10:43:15 +0100509{
Maxa15f05f2016-01-26 10:43:15 +0100510 int rc;
Maxf0e392a2017-10-18 13:32:30 +0200511
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100512 bv->cur_bit = *write_index;
Maxf0e392a2017-10-18 13:32:30 +0200513
514 rc = bitvec_set_u64(bv, val, len, false);
515 if (rc != 0)
516 return rc;
517
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100518 *write_index += len;
Maxf0e392a2017-10-18 13:32:30 +0200519
Maxa15f05f2016-01-26 10:43:15 +0100520 return 0;
521}
522
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200523/*! convert enum to corresponding character
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200524 * \param v input value (bit)
525 * \return single character, either 0, 1, L or H */
Max0a59e982016-02-05 13:55:37 +0100526char bit_value_to_char(enum bit_value v)
527{
528 switch (v) {
529 case ZERO: return '0';
530 case ONE: return '1';
531 case L: return 'L';
532 case H: return 'H';
Harald Welte459a1802018-06-28 09:24:17 +0200533 default: osmo_panic("unexpected input in bit_value_to_char"); return 'X';
Max0a59e982016-02-05 13:55:37 +0100534 }
535}
536
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200537/*! prints bit vector to provided string
Max0a59e982016-02-05 13:55:37 +0100538 * It's caller's responsibility to ensure that we won't shoot him in the foot:
539 * the provided buffer should be at lest cur_bit + 1 bytes long
540 */
541void bitvec_to_string_r(const struct bitvec *bv, char *str)
542{
543 unsigned i, pos = 0;
544 char *cur = str;
545 for (i = 0; i < bv->cur_bit; i++) {
546 if (0 == i % 8)
547 *cur++ = ' ';
548 *cur++ = bit_value_to_char(bitvec_get_bit_pos(bv, i));
549 pos++;
550 }
551 *cur = 0;
552}
553
554/* we assume that x have at least 1 non-b bit */
555static inline unsigned leading_bits(uint8_t x, bool b)
556{
557 if (b) {
558 if (x < 0x80) return 0;
559 if (x < 0xC0) return 1;
560 if (x < 0xE0) return 2;
561 if (x < 0xF0) return 3;
562 if (x < 0xF8) return 4;
563 if (x < 0xFC) return 5;
564 if (x < 0xFE) return 6;
565 } else {
566 if (x > 0x7F) return 0;
567 if (x > 0x3F) return 1;
568 if (x > 0x1F) return 2;
569 if (x > 0xF) return 3;
570 if (x > 7) return 4;
571 if (x > 3) return 5;
572 if (x > 1) return 6;
573 }
574 return 7;
575}
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200576/*! force bit vector to all 0 and current bit to the beginnig of the vector */
Max0a59e982016-02-05 13:55:37 +0100577void bitvec_zero(struct bitvec *bv)
578{
579 bv->cur_bit = 0;
580 memset(bv->data, 0, bv->data_len);
581}
582
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200583/*! Return number (bits) of uninterrupted bit run in vector starting from the MSB
Max0a59e982016-02-05 13:55:37 +0100584 * \param[in] bv The boolean vector to work on
585 * \param[in] b The boolean, sequence of which is looked at from the vector start
586 * \returns Number of consecutive bits of \p b in \p bv
587 */
588unsigned bitvec_rl(const struct bitvec *bv, bool b)
589{
590 unsigned i;
591 for (i = 0; i < (bv->cur_bit % 8 ? bv->cur_bit / 8 + 1 : bv->cur_bit / 8); i++) {
592 if ( (b ? 0xFF : 0) != bv->data[i])
593 return i * 8 + leading_bits(bv->data[i], b);
594 }
595
596 return bv->cur_bit;
597}
598
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200599/*! Return number (bits) of uninterrupted bit run in vector
Pravin Kumarvel848de8f2016-12-02 15:13:03 +0530600 * starting from the current bit
601 * \param[in] bv The boolean vector to work on
602 * \param[in] b The boolean, sequence of 1's or 0's to be checked
603 * \param[in] max_bits Total Number of Uncmopresed bits
604 * \returns Number of consecutive bits of \p b in \p bv and cur_bit will
605 * \go to cur_bit + number of consecutive bit
606 */
Harald Weltec85aaed2022-01-09 11:56:19 +0100607unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, unsigned int max_bits)
Pravin Kumarvel848de8f2016-12-02 15:13:03 +0530608{
609 unsigned i = 0;
610 unsigned j = 8;
611 int temp_res = 0;
612 int count = 0;
613 unsigned readIndex = bv->cur_bit;
614 unsigned remaining_bits = max_bits % 8;
615 unsigned remaining_bytes = max_bits / 8;
616 unsigned byte_mask = 0xFF;
617
618 if (readIndex % 8) {
619 for (j -= (readIndex % 8) ; j > 0 ; j--) {
620 if (readIndex < max_bits && bitvec_read_field(bv, &readIndex, 1) == b)
621 temp_res++;
622 else {
623 bv->cur_bit--;
624 return temp_res;
625 }
626 }
627 }
628 for (i = (readIndex / 8);
629 i < (remaining_bits ? remaining_bytes + 1 : remaining_bytes);
630 i++, count++) {
631 if ((b ? byte_mask : 0) != bv->data[i]) {
632 bv->cur_bit = (count * 8 +
633 leading_bits(bv->data[i], b) + readIndex);
634 return count * 8 +
635 leading_bits(bv->data[i], b) + temp_res;
636 }
637 }
638 bv->cur_bit = (temp_res + (count * 8)) + readIndex;
639 if (bv->cur_bit > max_bits)
640 bv->cur_bit = max_bits;
641 return (bv->cur_bit - readIndex + temp_res);
642}
643
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200644/*! Shifts bitvec to the left, n MSB bits lost */
Max0a59e982016-02-05 13:55:37 +0100645void bitvec_shiftl(struct bitvec *bv, unsigned n)
646{
647 if (0 == n)
648 return;
649 if (n >= bv->cur_bit) {
650 bitvec_zero(bv);
651 return;
652 }
653
654 memmove(bv->data, bv->data + n / 8, bv->data_len - n / 8);
655
656 uint8_t tmp[2];
657 unsigned i;
658 for (i = 0; i < bv->data_len - 2; i++) {
659 uint16_t t = osmo_load16be(bv->data + i);
660 osmo_store16be(t << (n % 8), &tmp);
661 bv->data[i] = tmp[0];
662 }
663
664 bv->data[bv->data_len - 1] <<= (n % 8);
665 bv->cur_bit -= n;
666}
667
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200668/*! Add given array to bitvec
Maxd4793212016-03-17 11:51:08 +0100669 * \param[in,out] bv bit vector to work with
670 * \param[in] array elements to be added
671 * \param[in] array_len length of array
672 * \param[in] dry_run indicates whether to return number of bits required
673 * instead of adding anything to bv for real
674 * \param[in] num_bits number of bits to consider in each element of array
675 * \returns number of bits necessary to add array elements if dry_run is true,
676 * 0 otherwise (only in this case bv is actually changed)
677 *
678 * N. B: no length checks are performed on bv - it's caller's job to ensure
679 * enough space is available - for example by calling with dry_run = true first.
680 *
681 * Useful for common pattern in CSN.1 spec which looks like:
682 * { 1 < XXX : bit (num_bits) > } ** 0
683 * which means repeat any times (between 0 and infinity),
684 * start each repetition with 1, mark end of repetitions with 0 bit
685 * see app. note in 3GPP TS 24.007 ยง B.2.1 Rule A2
686 */
687unsigned int bitvec_add_array(struct bitvec *bv, const uint32_t *array,
688 unsigned int array_len, bool dry_run,
689 unsigned int num_bits)
690{
691 unsigned i, bits = 1; /* account for stop bit */
692 for (i = 0; i < array_len; i++) {
693 if (dry_run) {
694 bits += (1 + num_bits);
695 } else {
696 bitvec_set_bit(bv, 1);
697 bitvec_set_uint(bv, array[i], num_bits);
698 }
699 }
700
701 if (dry_run)
702 return bits;
703
704 bitvec_set_bit(bv, 0); /* stop bit - end of the sequence */
705 return 0;
706}
707
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200708/*! @} */