blob: 0c263ad62f9112af482142a0c19277f10af8a71d [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>
Harald Welteec8b4502010-02-20 20:34:29 +010048
49#define BITNUM_FROM_COMP(byte, bit) ((byte*8)+bit)
50
51static inline unsigned int bytenum_from_bitnum(unsigned int bitnum)
52{
53 unsigned int bytenum = bitnum / 8;
54
55 return bytenum;
56}
57
58/* convert ZERO/ONE/L/H to a bitmask at given pos in a byte */
59static uint8_t bitval2mask(enum bit_value bit, uint8_t bitnum)
60{
Harald Welteec8b4502010-02-20 20:34:29 +010061 switch (bit) {
62 case ZERO:
Vadim Yanitskiy00a55ae2019-07-17 16:41:17 +070063 return (0 << bitnum);
Harald Welteec8b4502010-02-20 20:34:29 +010064 case ONE:
Vadim Yanitskiy00a55ae2019-07-17 16:41:17 +070065 return (1 << bitnum);
Harald Welteec8b4502010-02-20 20:34:29 +010066 case L:
Vadim Yanitskiy00a55ae2019-07-17 16:41:17 +070067 return ((0x2b ^ (0 << bitnum)) & (1 << bitnum));
Harald Welteec8b4502010-02-20 20:34:29 +010068 case H:
Vadim Yanitskiy00a55ae2019-07-17 16:41:17 +070069 return ((0x2b ^ (1 << bitnum)) & (1 << bitnum));
Harald Welteec8b4502010-02-20 20:34:29 +010070 default:
71 return 0;
72 }
Harald Welteec8b4502010-02-20 20:34:29 +010073}
74
Neels Hofmeyr87e45502017-06-20 00:17:59 +020075/*! check if the bit is 0 or 1 for a given position inside a bitvec
Harald Welteba6988b2011-08-17 12:46:48 +020076 * \param[in] bv the bit vector on which to check
77 * \param[in] bitnr the bit number inside the bit vector to check
Harald Welte2d2e2cc2016-04-25 12:11:20 +020078 * \return value of the requested bit
Harald Welteba6988b2011-08-17 12:46:48 +020079 */
Harald Welted9abf012010-03-06 11:28:49 +010080enum bit_value bitvec_get_bit_pos(const struct bitvec *bv, unsigned int bitnr)
Harald Welteec8b4502010-02-20 20:34:29 +010081{
82 unsigned int bytenum = bytenum_from_bitnum(bitnr);
83 unsigned int bitnum = 7 - (bitnr % 8);
84 uint8_t bitval;
85
86 if (bytenum >= bv->data_len)
87 return -EINVAL;
88
89 bitval = bitval2mask(ONE, bitnum);
90
91 if (bv->data[bytenum] & bitval)
92 return ONE;
93
94 return ZERO;
95}
96
Neels Hofmeyr87e45502017-06-20 00:17:59 +020097/*! check if the bit is L or H for a given position inside a bitvec
Harald Welteba6988b2011-08-17 12:46:48 +020098 * \param[in] bv the bit vector on which to check
99 * \param[in] bitnr the bit number inside the bit vector to check
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200100 * \return value of the requested bit
Harald Welteba6988b2011-08-17 12:46:48 +0200101 */
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000102enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv,
103 unsigned int bitnr)
104{
105 unsigned int bytenum = bytenum_from_bitnum(bitnr);
106 unsigned int bitnum = 7 - (bitnr % 8);
107 uint8_t bitval;
108
109 if (bytenum >= bv->data_len)
110 return -EINVAL;
111
112 bitval = bitval2mask(H, bitnum);
113
Andreas.Eversbergdc0ebdf2010-10-24 11:59:33 +0200114 if ((bv->data[bytenum] & (1 << bitnum)) == bitval)
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000115 return H;
116
117 return L;
118}
119
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200120/*! get the Nth set bit inside the bit vector
Harald Welteba6988b2011-08-17 12:46:48 +0200121 * \param[in] bv the bit vector to use
122 * \param[in] n the bit number to get
123 * \returns the bit number (offset) of the Nth set bit in \a bv
124 */
Harald Welted9abf012010-03-06 11:28:49 +0100125unsigned int bitvec_get_nth_set_bit(const struct bitvec *bv, unsigned int n)
Harald Welteec8b4502010-02-20 20:34:29 +0100126{
127 unsigned int i, k = 0;
128
129 for (i = 0; i < bv->data_len*8; i++) {
130 if (bitvec_get_bit_pos(bv, i) == ONE) {
131 k++;
132 if (k == n)
133 return i;
134 }
135 }
136
137 return 0;
138}
139
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200140/*! set a bit at given position in a bit vector
Harald Welteba6988b2011-08-17 12:46:48 +0200141 * \param[in] bv bit vector on which to operate
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100142 * \param[in] bitnr number of bit to be set
Harald Welteba6988b2011-08-17 12:46:48 +0200143 * \param[in] bit value to which the bit is to be set
Max912bc6f2016-01-28 12:07:12 +0100144 * \returns 0 on success, negative value on error
Harald Welteba6988b2011-08-17 12:46:48 +0200145 */
Holger Hans Peter Freyther511b4482016-07-13 12:26:10 +0200146inline int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnr,
Harald Welteec8b4502010-02-20 20:34:29 +0100147 enum bit_value bit)
148{
149 unsigned int bytenum = bytenum_from_bitnum(bitnr);
150 unsigned int bitnum = 7 - (bitnr % 8);
151 uint8_t bitval;
152
153 if (bytenum >= bv->data_len)
154 return -EINVAL;
155
156 /* first clear the bit */
157 bitval = bitval2mask(ONE, bitnum);
158 bv->data[bytenum] &= ~bitval;
159
160 /* then set it to desired value */
161 bitval = bitval2mask(bit, bitnum);
162 bv->data[bytenum] |= bitval;
163
164 return 0;
165}
166
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200167/*! set the next bit inside a bitvec
Harald Welteba6988b2011-08-17 12:46:48 +0200168 * \param[in] bv bit vector to be used
169 * \param[in] bit value of the bit to be set
Max912bc6f2016-01-28 12:07:12 +0100170 * \returns 0 on success, negative value on error
Harald Welteba6988b2011-08-17 12:46:48 +0200171 */
Holger Hans Peter Freyther511b4482016-07-13 12:26:10 +0200172inline int bitvec_set_bit(struct bitvec *bv, enum bit_value bit)
Harald Welteec8b4502010-02-20 20:34:29 +0100173{
174 int rc;
175
176 rc = bitvec_set_bit_pos(bv, bv->cur_bit, bit);
177 if (!rc)
178 bv->cur_bit++;
179
180 return rc;
181}
182
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200183/*! get the next bit (low/high) inside a bitvec
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200184 * \return value of th next bit in the vector */
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000185int bitvec_get_bit_high(struct bitvec *bv)
186{
187 int rc;
188
189 rc = bitvec_get_bit_pos_high(bv, bv->cur_bit);
190 if (rc >= 0)
191 bv->cur_bit++;
192
193 return rc;
194}
195
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200196/*! set multiple bits (based on array of bitvals) at current pos
Harald Welteba6988b2011-08-17 12:46:48 +0200197 * \param[in] bv bit vector
198 * \param[in] bits array of \ref bit_value
199 * \param[in] count number of bits to set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200200 * \return 0 on success; negative in case of error */
Harald Welte14bf28a2016-06-27 15:19:10 +0200201int bitvec_set_bits(struct bitvec *bv, const enum bit_value *bits, unsigned int count)
Harald Welteec8b4502010-02-20 20:34:29 +0100202{
203 int i, rc;
204
205 for (i = 0; i < count; i++) {
206 rc = bitvec_set_bit(bv, bits[i]);
207 if (rc)
208 return rc;
209 }
210
211 return 0;
212}
213
Max0b3db502017-10-18 13:48:10 +0200214/*! set multiple bits (based on numeric value) at current pos.
215 * \param[in] bv bit vector.
216 * \param[in] v mask representing which bits needs to be set.
217 * \param[in] num_bits number of meaningful bits in the mask.
218 * \param[in] use_lh whether to interpret the bits as L/H values or as 0/1.
219 * \return 0 on success; negative in case of error. */
220int bitvec_set_u64(struct bitvec *bv, uint64_t v, uint8_t num_bits, bool use_lh)
Harald Welteec8b4502010-02-20 20:34:29 +0100221{
Max0b3db502017-10-18 13:48:10 +0200222 uint8_t i;
223
224 if (num_bits > 64)
225 return -E2BIG;
226
Harald Welteec8b4502010-02-20 20:34:29 +0100227 for (i = 0; i < num_bits; i++) {
Max0b3db502017-10-18 13:48:10 +0200228 int rc;
229 enum bit_value bit = use_lh ? L : 0;
230
231 if (v & ((uint64_t)1 << (num_bits - i - 1)))
232 bit = use_lh ? H : 1;
233
Harald Welteec8b4502010-02-20 20:34:29 +0100234 rc = bitvec_set_bit(bv, bit);
Max0b3db502017-10-18 13:48:10 +0200235 if (rc != 0)
Harald Welteec8b4502010-02-20 20:34:29 +0100236 return rc;
237 }
238
239 return 0;
240}
241
Max0b3db502017-10-18 13:48:10 +0200242/*! set multiple bits (based on numeric value) at current pos.
243 * \return 0 in case of success; negative in case of error. */
244int bitvec_set_uint(struct bitvec *bv, unsigned int ui, unsigned int num_bits)
245{
246 return bitvec_set_u64(bv, ui, num_bits, false);
247}
248
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200249/*! get multiple bits (num_bits) from beginning of vector (MSB side)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200250 * \return 16bit signed integer retrieved from bit vector */
Max0a59e982016-02-05 13:55:37 +0100251int16_t bitvec_get_int16_msb(const struct bitvec *bv, unsigned int num_bits)
252{
253 if (num_bits > 15 || bv->cur_bit < num_bits)
254 return -EINVAL;
255
256 if (num_bits < 9)
257 return bv->data[0] >> (8 - num_bits);
258
259 return osmo_load16be(bv->data) >> (16 - num_bits);
260}
261
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200262/*! get multiple bits (based on numeric value) from current pos
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200263 * \return integer value retrieved from bit vector */
Maxe49af082016-01-22 16:46:56 +0100264int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits)
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000265{
266 int i;
267 unsigned int ui = 0;
268
269 for (i = 0; i < num_bits; i++) {
270 int bit = bitvec_get_bit_pos(bv, bv->cur_bit);
271 if (bit < 0)
272 return bit;
273 if (bit)
274 ui |= (1 << (num_bits - i - 1));
275 bv->cur_bit++;
276 }
277
278 return ui;
279}
280
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200281/*! fill num_bits with \fill starting from the current position
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200282 * \return 0 on success; negative otherwise (out of vector boundary)
Max0a59e982016-02-05 13:55:37 +0100283 */
284int bitvec_fill(struct bitvec *bv, unsigned int num_bits, enum bit_value fill)
285{
286 unsigned i, stop = bv->cur_bit + num_bits;
287 for (i = bv->cur_bit; i < stop; i++)
288 if (bitvec_set_bit(bv, fill) < 0)
289 return -EINVAL;
290
291 return 0;
292}
293
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200294/*! pad all remaining bits up to num_bits
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200295 * \return 0 on success; negative otherwise */
Harald Welteec8b4502010-02-20 20:34:29 +0100296int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit)
297{
Max0a59e982016-02-05 13:55:37 +0100298 int n = up_to_bit - bv->cur_bit + 1;
299 if (n < 1)
300 return 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100301
Max0a59e982016-02-05 13:55:37 +0100302 return bitvec_fill(bv, n, L);
Harald Welteec8b4502010-02-20 20:34:29 +0100303}
Pablo Neira Ayuso36bdf2c2011-03-28 19:24:19 +0200304
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200305/*! find first bit set in bit vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200306 * \return 0 on success; negative otherwise */
Pablo Neira Ayuso36bdf2c2011-03-28 19:24:19 +0200307int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n,
308 enum bit_value val)
309{
310 unsigned int i;
311
312 for (i = n; i < bv->data_len*8; i++) {
313 if (bitvec_get_bit_pos(bv, i) == val)
314 return i;
315 }
316
317 return -1;
318}
Harald Welteba6988b2011-08-17 12:46:48 +0200319
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200320/*! get multiple bytes from current pos
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100321 * Assumes MSB first encoding.
322 * \param[in] bv bit vector
323 * \param[in] bytes array
324 * \param[in] count number of bytes to copy
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200325 * \return 0 on success; negative otherwise
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100326 */
Maxe49af082016-01-22 16:46:56 +0100327int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count)
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100328{
329 int byte_offs = bytenum_from_bitnum(bv->cur_bit);
330 int bit_offs = bv->cur_bit % 8;
331 uint8_t c, last_c;
332 int i;
333 uint8_t *src;
334
335 if (byte_offs + count + (bit_offs ? 1 : 0) > bv->data_len)
336 return -EINVAL;
337
338 if (bit_offs == 0) {
339 memcpy(bytes, bv->data + byte_offs, count);
340 } else {
341 src = bv->data + byte_offs;
342 last_c = *(src++);
343 for (i = count; i > 0; i--) {
344 c = *(src++);
345 *(bytes++) =
346 (last_c << bit_offs) |
347 (c >> (8 - bit_offs));
348 last_c = c;
349 }
350 }
351
352 bv->cur_bit += count * 8;
353 return 0;
354}
355
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200356/*! set multiple bytes at current pos
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100357 * Assumes MSB first encoding.
358 * \param[in] bv bit vector
359 * \param[in] bytes array
360 * \param[in] count number of bytes to copy
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200361 * \return 0 on success; negative otherwise
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100362 */
Maxe49af082016-01-22 16:46:56 +0100363int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count)
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100364{
365 int byte_offs = bytenum_from_bitnum(bv->cur_bit);
366 int bit_offs = bv->cur_bit % 8;
367 uint8_t c, last_c;
368 int i;
369 uint8_t *dst;
370
371 if (byte_offs + count + (bit_offs ? 1 : 0) > bv->data_len)
372 return -EINVAL;
373
374 if (bit_offs == 0) {
375 memcpy(bv->data + byte_offs, bytes, count);
376 } else if (count > 0) {
377 dst = bv->data + byte_offs;
378 /* Get lower bits of first dst byte */
379 last_c = *dst >> (8 - bit_offs);
380 for (i = count; i > 0; i--) {
381 c = *(bytes++);
382 *(dst++) =
383 (last_c << (8 - bit_offs)) |
384 (c >> bit_offs);
385 last_c = c;
386 }
387 /* Overwrite lower bits of N+1 dst byte */
388 *dst = (*dst & ((1 << (8 - bit_offs)) - 1)) |
389 (last_c << (8 - bit_offs));
390 }
391
392 bv->cur_bit += count * 8;
393 return 0;
394}
Maxa15f05f2016-01-26 10:43:15 +0100395
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200396/*! Allocate a bit vector
Alexander Couzens76e8cbd2019-06-16 22:26:08 +0200397 * \param[in] size Number of bytes in the vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200398 * \param[in] ctx Context from which to allocate
399 * \return pointer to allocated vector; NULL in case of error */
Maxa15f05f2016-01-26 10:43:15 +0100400struct bitvec *bitvec_alloc(unsigned int size, TALLOC_CTX *ctx)
401{
402 struct bitvec *bv = talloc_zero(ctx, struct bitvec);
403 if (!bv)
404 return NULL;
405
406 bv->data = talloc_zero_array(bv, uint8_t, size);
407 if (!(bv->data)) {
408 talloc_free(bv);
409 return NULL;
410 }
411
412 bv->data_len = size;
413 bv->cur_bit = 0;
414 return bv;
415}
416
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200417/*! Free a bit vector (release its memory)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200418 * \param[in] bit vector to free */
Maxa15f05f2016-01-26 10:43:15 +0100419void bitvec_free(struct bitvec *bv)
420{
421 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{
431 unsigned int i = 0;
432 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{
444 unsigned int i = 0;
445 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{
Holger Hans Peter Freyther2745b482016-01-27 17:08:02 +0100458 unsigned i;
Maxa15f05f2016-01-26 10:43:15 +0100459 unsigned val;
460 unsigned write_index = 0;
461 unsigned digits = bv->data_len * 2;
Holger Hans Peter Freyther2745b482016-01-27 17:08:02 +0100462
463 for (i = 0; i < digits; i++) {
Maxa15f05f2016-01-26 10:43:15 +0100464 if (sscanf(src + i, "%1x", &val) < 1) {
465 return 1;
466 }
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100467 bitvec_write_field(bv, &write_index, val, 4);
Maxa15f05f2016-01-26 10:43:15 +0100468 }
469 return 0;
470}
471
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200472/*! read part of the vector
Max912bc6f2016-01-28 12:07:12 +0100473 * \param[in] bv The boolean vector to work on
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100474 * \param[in,out] read_index Where reading supposed to start in the vector
Max912bc6f2016-01-28 12:07:12 +0100475 * \param[in] len How many bits to read from vector
476 * \returns read bits or negative value on error
477 */
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100478uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned int len)
Maxa15f05f2016-01-26 10:43:15 +0100479{
480 unsigned int i;
481 uint64_t ui = 0;
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100482 bv->cur_bit = *read_index;
Maxa15f05f2016-01-26 10:43:15 +0100483
484 for (i = 0; i < len; i++) {
485 int bit = bitvec_get_bit_pos((const struct bitvec *)bv, bv->cur_bit);
486 if (bit < 0)
487 return bit;
488 if (bit)
489 ui |= ((uint64_t)1 << (len - i - 1));
490 bv->cur_bit++;
491 }
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100492 *read_index += len;
Maxa15f05f2016-01-26 10:43:15 +0100493 return ui;
494}
495
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200496/*! write into the vector
Max912bc6f2016-01-28 12:07:12 +0100497 * \param[in] bv The boolean vector to work on
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100498 * \param[in,out] write_index Where writing supposed to start in the vector
Max912bc6f2016-01-28 12:07:12 +0100499 * \param[in] len How many bits to write
500 * \returns next write index or negative value on error
501 */
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100502int bitvec_write_field(struct bitvec *bv, unsigned int *write_index, uint64_t val, unsigned int len)
Maxa15f05f2016-01-26 10:43:15 +0100503{
Maxa15f05f2016-01-26 10:43:15 +0100504 int rc;
Maxf0e392a2017-10-18 13:32:30 +0200505
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100506 bv->cur_bit = *write_index;
Maxf0e392a2017-10-18 13:32:30 +0200507
508 rc = bitvec_set_u64(bv, val, len, false);
509 if (rc != 0)
510 return rc;
511
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100512 *write_index += len;
Maxf0e392a2017-10-18 13:32:30 +0200513
Maxa15f05f2016-01-26 10:43:15 +0100514 return 0;
515}
516
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200517/*! convert enum to corresponding character
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200518 * \param v input value (bit)
519 * \return single character, either 0, 1, L or H */
Max0a59e982016-02-05 13:55:37 +0100520char bit_value_to_char(enum bit_value v)
521{
522 switch (v) {
523 case ZERO: return '0';
524 case ONE: return '1';
525 case L: return 'L';
526 case H: return 'H';
Harald Welte459a1802018-06-28 09:24:17 +0200527 default: osmo_panic("unexpected input in bit_value_to_char"); return 'X';
Max0a59e982016-02-05 13:55:37 +0100528 }
529}
530
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200531/*! prints bit vector to provided string
Max0a59e982016-02-05 13:55:37 +0100532 * It's caller's responsibility to ensure that we won't shoot him in the foot:
533 * the provided buffer should be at lest cur_bit + 1 bytes long
534 */
535void bitvec_to_string_r(const struct bitvec *bv, char *str)
536{
537 unsigned i, pos = 0;
538 char *cur = str;
539 for (i = 0; i < bv->cur_bit; i++) {
540 if (0 == i % 8)
541 *cur++ = ' ';
542 *cur++ = bit_value_to_char(bitvec_get_bit_pos(bv, i));
543 pos++;
544 }
545 *cur = 0;
546}
547
548/* we assume that x have at least 1 non-b bit */
549static inline unsigned leading_bits(uint8_t x, bool b)
550{
551 if (b) {
552 if (x < 0x80) return 0;
553 if (x < 0xC0) return 1;
554 if (x < 0xE0) return 2;
555 if (x < 0xF0) return 3;
556 if (x < 0xF8) return 4;
557 if (x < 0xFC) return 5;
558 if (x < 0xFE) return 6;
559 } else {
560 if (x > 0x7F) return 0;
561 if (x > 0x3F) return 1;
562 if (x > 0x1F) return 2;
563 if (x > 0xF) return 3;
564 if (x > 7) return 4;
565 if (x > 3) return 5;
566 if (x > 1) return 6;
567 }
568 return 7;
569}
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200570/*! force bit vector to all 0 and current bit to the beginnig of the vector */
Max0a59e982016-02-05 13:55:37 +0100571void bitvec_zero(struct bitvec *bv)
572{
573 bv->cur_bit = 0;
574 memset(bv->data, 0, bv->data_len);
575}
576
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200577/*! Return number (bits) of uninterrupted bit run in vector starting from the MSB
Max0a59e982016-02-05 13:55:37 +0100578 * \param[in] bv The boolean vector to work on
579 * \param[in] b The boolean, sequence of which is looked at from the vector start
580 * \returns Number of consecutive bits of \p b in \p bv
581 */
582unsigned bitvec_rl(const struct bitvec *bv, bool b)
583{
584 unsigned i;
585 for (i = 0; i < (bv->cur_bit % 8 ? bv->cur_bit / 8 + 1 : bv->cur_bit / 8); i++) {
586 if ( (b ? 0xFF : 0) != bv->data[i])
587 return i * 8 + leading_bits(bv->data[i], b);
588 }
589
590 return bv->cur_bit;
591}
592
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200593/*! Return number (bits) of uninterrupted bit run in vector
Pravin Kumarvel848de8f2016-12-02 15:13:03 +0530594 * starting from the current bit
595 * \param[in] bv The boolean vector to work on
596 * \param[in] b The boolean, sequence of 1's or 0's to be checked
597 * \param[in] max_bits Total Number of Uncmopresed bits
598 * \returns Number of consecutive bits of \p b in \p bv and cur_bit will
599 * \go to cur_bit + number of consecutive bit
600 */
601unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, int max_bits)
602{
603 unsigned i = 0;
604 unsigned j = 8;
605 int temp_res = 0;
606 int count = 0;
607 unsigned readIndex = bv->cur_bit;
608 unsigned remaining_bits = max_bits % 8;
609 unsigned remaining_bytes = max_bits / 8;
610 unsigned byte_mask = 0xFF;
611
612 if (readIndex % 8) {
613 for (j -= (readIndex % 8) ; j > 0 ; j--) {
614 if (readIndex < max_bits && bitvec_read_field(bv, &readIndex, 1) == b)
615 temp_res++;
616 else {
617 bv->cur_bit--;
618 return temp_res;
619 }
620 }
621 }
622 for (i = (readIndex / 8);
623 i < (remaining_bits ? remaining_bytes + 1 : remaining_bytes);
624 i++, count++) {
625 if ((b ? byte_mask : 0) != bv->data[i]) {
626 bv->cur_bit = (count * 8 +
627 leading_bits(bv->data[i], b) + readIndex);
628 return count * 8 +
629 leading_bits(bv->data[i], b) + temp_res;
630 }
631 }
632 bv->cur_bit = (temp_res + (count * 8)) + readIndex;
633 if (bv->cur_bit > max_bits)
634 bv->cur_bit = max_bits;
635 return (bv->cur_bit - readIndex + temp_res);
636}
637
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200638/*! Shifts bitvec to the left, n MSB bits lost */
Max0a59e982016-02-05 13:55:37 +0100639void bitvec_shiftl(struct bitvec *bv, unsigned n)
640{
641 if (0 == n)
642 return;
643 if (n >= bv->cur_bit) {
644 bitvec_zero(bv);
645 return;
646 }
647
648 memmove(bv->data, bv->data + n / 8, bv->data_len - n / 8);
649
650 uint8_t tmp[2];
651 unsigned i;
652 for (i = 0; i < bv->data_len - 2; i++) {
653 uint16_t t = osmo_load16be(bv->data + i);
654 osmo_store16be(t << (n % 8), &tmp);
655 bv->data[i] = tmp[0];
656 }
657
658 bv->data[bv->data_len - 1] <<= (n % 8);
659 bv->cur_bit -= n;
660}
661
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200662/*! Add given array to bitvec
Maxd4793212016-03-17 11:51:08 +0100663 * \param[in,out] bv bit vector to work with
664 * \param[in] array elements to be added
665 * \param[in] array_len length of array
666 * \param[in] dry_run indicates whether to return number of bits required
667 * instead of adding anything to bv for real
668 * \param[in] num_bits number of bits to consider in each element of array
669 * \returns number of bits necessary to add array elements if dry_run is true,
670 * 0 otherwise (only in this case bv is actually changed)
671 *
672 * N. B: no length checks are performed on bv - it's caller's job to ensure
673 * enough space is available - for example by calling with dry_run = true first.
674 *
675 * Useful for common pattern in CSN.1 spec which looks like:
676 * { 1 < XXX : bit (num_bits) > } ** 0
677 * which means repeat any times (between 0 and infinity),
678 * start each repetition with 1, mark end of repetitions with 0 bit
679 * see app. note in 3GPP TS 24.007 ยง B.2.1 Rule A2
680 */
681unsigned int bitvec_add_array(struct bitvec *bv, const uint32_t *array,
682 unsigned int array_len, bool dry_run,
683 unsigned int num_bits)
684{
685 unsigned i, bits = 1; /* account for stop bit */
686 for (i = 0; i < array_len; i++) {
687 if (dry_run) {
688 bits += (1 + num_bits);
689 } else {
690 bitvec_set_bit(bv, 1);
691 bitvec_set_uint(bv, array[i], num_bits);
692 }
693 }
694
695 if (dry_run)
696 return bits;
697
698 bitvec_set_bit(bv, 0); /* stop bit - end of the sequence */
699 return 0;
700}
701
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200702/*! @} */