blob: 884eb025ada6269c0324cc00063bf581ae945024 [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 Welteec8b4502010-02-20 20:34:29 +010047
48#define BITNUM_FROM_COMP(byte, bit) ((byte*8)+bit)
49
50static inline unsigned int bytenum_from_bitnum(unsigned int bitnum)
51{
52 unsigned int bytenum = bitnum / 8;
53
54 return bytenum;
55}
56
57/* convert ZERO/ONE/L/H to a bitmask at given pos in a byte */
58static uint8_t bitval2mask(enum bit_value bit, uint8_t bitnum)
59{
60 int bitval;
61
62 switch (bit) {
63 case ZERO:
64 bitval = (0 << bitnum);
65 break;
66 case ONE:
67 bitval = (1 << bitnum);
68 break;
69 case L:
70 bitval = ((0x2b ^ (0 << bitnum)) & (1 << bitnum));
71 break;
72 case H:
73 bitval = ((0x2b ^ (1 << bitnum)) & (1 << bitnum));
74 break;
75 default:
76 return 0;
77 }
78 return bitval;
79}
80
Neels Hofmeyr87e45502017-06-20 00:17:59 +020081/*! check if the bit is 0 or 1 for a given position inside a bitvec
Harald Welteba6988b2011-08-17 12:46:48 +020082 * \param[in] bv the bit vector on which to check
83 * \param[in] bitnr the bit number inside the bit vector to check
Harald Welte2d2e2cc2016-04-25 12:11:20 +020084 * \return value of the requested bit
Harald Welteba6988b2011-08-17 12:46:48 +020085 */
Harald Welted9abf012010-03-06 11:28:49 +010086enum bit_value bitvec_get_bit_pos(const struct bitvec *bv, unsigned int bitnr)
Harald Welteec8b4502010-02-20 20:34:29 +010087{
88 unsigned int bytenum = bytenum_from_bitnum(bitnr);
89 unsigned int bitnum = 7 - (bitnr % 8);
90 uint8_t bitval;
91
92 if (bytenum >= bv->data_len)
93 return -EINVAL;
94
95 bitval = bitval2mask(ONE, bitnum);
96
97 if (bv->data[bytenum] & bitval)
98 return ONE;
99
100 return ZERO;
101}
102
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200103/*! check if the bit is L or H for a given position inside a bitvec
Harald Welteba6988b2011-08-17 12:46:48 +0200104 * \param[in] bv the bit vector on which to check
105 * \param[in] bitnr the bit number inside the bit vector to check
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200106 * \return value of the requested bit
Harald Welteba6988b2011-08-17 12:46:48 +0200107 */
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000108enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv,
109 unsigned int bitnr)
110{
111 unsigned int bytenum = bytenum_from_bitnum(bitnr);
112 unsigned int bitnum = 7 - (bitnr % 8);
113 uint8_t bitval;
114
115 if (bytenum >= bv->data_len)
116 return -EINVAL;
117
118 bitval = bitval2mask(H, bitnum);
119
Andreas.Eversbergdc0ebdf2010-10-24 11:59:33 +0200120 if ((bv->data[bytenum] & (1 << bitnum)) == bitval)
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000121 return H;
122
123 return L;
124}
125
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200126/*! get the Nth set bit inside the bit vector
Harald Welteba6988b2011-08-17 12:46:48 +0200127 * \param[in] bv the bit vector to use
128 * \param[in] n the bit number to get
129 * \returns the bit number (offset) of the Nth set bit in \a bv
130 */
Harald Welted9abf012010-03-06 11:28:49 +0100131unsigned int bitvec_get_nth_set_bit(const struct bitvec *bv, unsigned int n)
Harald Welteec8b4502010-02-20 20:34:29 +0100132{
133 unsigned int i, k = 0;
134
135 for (i = 0; i < bv->data_len*8; i++) {
136 if (bitvec_get_bit_pos(bv, i) == ONE) {
137 k++;
138 if (k == n)
139 return i;
140 }
141 }
142
143 return 0;
144}
145
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200146/*! set a bit at given position in a bit vector
Harald Welteba6988b2011-08-17 12:46:48 +0200147 * \param[in] bv bit vector on which to operate
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100148 * \param[in] bitnr number of bit to be set
Harald Welteba6988b2011-08-17 12:46:48 +0200149 * \param[in] bit value to which the bit is to be set
Max912bc6f2016-01-28 12:07:12 +0100150 * \returns 0 on success, negative value on error
Harald Welteba6988b2011-08-17 12:46:48 +0200151 */
Holger Hans Peter Freyther511b4482016-07-13 12:26:10 +0200152inline int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnr,
Harald Welteec8b4502010-02-20 20:34:29 +0100153 enum bit_value bit)
154{
155 unsigned int bytenum = bytenum_from_bitnum(bitnr);
156 unsigned int bitnum = 7 - (bitnr % 8);
157 uint8_t bitval;
158
159 if (bytenum >= bv->data_len)
160 return -EINVAL;
161
162 /* first clear the bit */
163 bitval = bitval2mask(ONE, bitnum);
164 bv->data[bytenum] &= ~bitval;
165
166 /* then set it to desired value */
167 bitval = bitval2mask(bit, bitnum);
168 bv->data[bytenum] |= bitval;
169
170 return 0;
171}
172
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200173/*! set the next bit inside a bitvec
Harald Welteba6988b2011-08-17 12:46:48 +0200174 * \param[in] bv bit vector to be used
175 * \param[in] bit value of the bit to be set
Max912bc6f2016-01-28 12:07:12 +0100176 * \returns 0 on success, negative value on error
Harald Welteba6988b2011-08-17 12:46:48 +0200177 */
Holger Hans Peter Freyther511b4482016-07-13 12:26:10 +0200178inline int bitvec_set_bit(struct bitvec *bv, enum bit_value bit)
Harald Welteec8b4502010-02-20 20:34:29 +0100179{
180 int rc;
181
182 rc = bitvec_set_bit_pos(bv, bv->cur_bit, bit);
183 if (!rc)
184 bv->cur_bit++;
185
186 return rc;
187}
188
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200189/*! get the next bit (low/high) inside a bitvec
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200190 * \return value of th next bit in the vector */
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000191int bitvec_get_bit_high(struct bitvec *bv)
192{
193 int rc;
194
195 rc = bitvec_get_bit_pos_high(bv, bv->cur_bit);
196 if (rc >= 0)
197 bv->cur_bit++;
198
199 return rc;
200}
201
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200202/*! set multiple bits (based on array of bitvals) at current pos
Harald Welteba6988b2011-08-17 12:46:48 +0200203 * \param[in] bv bit vector
204 * \param[in] bits array of \ref bit_value
205 * \param[in] count number of bits to set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200206 * \return 0 on success; negative in case of error */
Harald Welte14bf28a2016-06-27 15:19:10 +0200207int bitvec_set_bits(struct bitvec *bv, const enum bit_value *bits, unsigned int count)
Harald Welteec8b4502010-02-20 20:34:29 +0100208{
209 int i, rc;
210
211 for (i = 0; i < count; i++) {
212 rc = bitvec_set_bit(bv, bits[i]);
213 if (rc)
214 return rc;
215 }
216
217 return 0;
218}
219
Max0b3db502017-10-18 13:48:10 +0200220/*! set multiple bits (based on numeric value) at current pos.
221 * \param[in] bv bit vector.
222 * \param[in] v mask representing which bits needs to be set.
223 * \param[in] num_bits number of meaningful bits in the mask.
224 * \param[in] use_lh whether to interpret the bits as L/H values or as 0/1.
225 * \return 0 on success; negative in case of error. */
226int bitvec_set_u64(struct bitvec *bv, uint64_t v, uint8_t num_bits, bool use_lh)
Harald Welteec8b4502010-02-20 20:34:29 +0100227{
Max0b3db502017-10-18 13:48:10 +0200228 uint8_t i;
229
230 if (num_bits > 64)
231 return -E2BIG;
232
Harald Welteec8b4502010-02-20 20:34:29 +0100233 for (i = 0; i < num_bits; i++) {
Max0b3db502017-10-18 13:48:10 +0200234 int rc;
235 enum bit_value bit = use_lh ? L : 0;
236
237 if (v & ((uint64_t)1 << (num_bits - i - 1)))
238 bit = use_lh ? H : 1;
239
Harald Welteec8b4502010-02-20 20:34:29 +0100240 rc = bitvec_set_bit(bv, bit);
Max0b3db502017-10-18 13:48:10 +0200241 if (rc != 0)
Harald Welteec8b4502010-02-20 20:34:29 +0100242 return rc;
243 }
244
245 return 0;
246}
247
Max0b3db502017-10-18 13:48:10 +0200248/*! set multiple bits (based on numeric value) at current pos.
249 * \return 0 in case of success; negative in case of error. */
250int bitvec_set_uint(struct bitvec *bv, unsigned int ui, unsigned int num_bits)
251{
252 return bitvec_set_u64(bv, ui, num_bits, false);
253}
254
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200255/*! get multiple bits (num_bits) from beginning of vector (MSB side)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200256 * \return 16bit signed integer retrieved from bit vector */
Max0a59e982016-02-05 13:55:37 +0100257int16_t bitvec_get_int16_msb(const struct bitvec *bv, unsigned int num_bits)
258{
259 if (num_bits > 15 || bv->cur_bit < num_bits)
260 return -EINVAL;
261
262 if (num_bits < 9)
263 return bv->data[0] >> (8 - num_bits);
264
265 return osmo_load16be(bv->data) >> (16 - num_bits);
266}
267
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200268/*! get multiple bits (based on numeric value) from current pos
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200269 * \return integer value retrieved from bit vector */
Maxe49af082016-01-22 16:46:56 +0100270int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits)
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +0000271{
272 int i;
273 unsigned int ui = 0;
274
275 for (i = 0; i < num_bits; i++) {
276 int bit = bitvec_get_bit_pos(bv, bv->cur_bit);
277 if (bit < 0)
278 return bit;
279 if (bit)
280 ui |= (1 << (num_bits - i - 1));
281 bv->cur_bit++;
282 }
283
284 return ui;
285}
286
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200287/*! fill num_bits with \fill starting from the current position
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200288 * \return 0 on success; negative otherwise (out of vector boundary)
Max0a59e982016-02-05 13:55:37 +0100289 */
290int bitvec_fill(struct bitvec *bv, unsigned int num_bits, enum bit_value fill)
291{
292 unsigned i, stop = bv->cur_bit + num_bits;
293 for (i = bv->cur_bit; i < stop; i++)
294 if (bitvec_set_bit(bv, fill) < 0)
295 return -EINVAL;
296
297 return 0;
298}
299
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200300/*! pad all remaining bits up to num_bits
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200301 * \return 0 on success; negative otherwise */
Harald Welteec8b4502010-02-20 20:34:29 +0100302int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit)
303{
Max0a59e982016-02-05 13:55:37 +0100304 int n = up_to_bit - bv->cur_bit + 1;
305 if (n < 1)
306 return 0;
Harald Welteec8b4502010-02-20 20:34:29 +0100307
Max0a59e982016-02-05 13:55:37 +0100308 return bitvec_fill(bv, n, L);
Harald Welteec8b4502010-02-20 20:34:29 +0100309}
Pablo Neira Ayuso36bdf2c2011-03-28 19:24:19 +0200310
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200311/*! find first bit set in bit vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200312 * \return 0 on success; negative otherwise */
Pablo Neira Ayuso36bdf2c2011-03-28 19:24:19 +0200313int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n,
314 enum bit_value val)
315{
316 unsigned int i;
317
318 for (i = n; i < bv->data_len*8; i++) {
319 if (bitvec_get_bit_pos(bv, i) == val)
320 return i;
321 }
322
323 return -1;
324}
Harald Welteba6988b2011-08-17 12:46:48 +0200325
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200326/*! get multiple bytes from current pos
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100327 * Assumes MSB first encoding.
328 * \param[in] bv bit vector
329 * \param[in] bytes array
330 * \param[in] count number of bytes to copy
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200331 * \return 0 on success; negative otherwise
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100332 */
Maxe49af082016-01-22 16:46:56 +0100333int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count)
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100334{
335 int byte_offs = bytenum_from_bitnum(bv->cur_bit);
336 int bit_offs = bv->cur_bit % 8;
337 uint8_t c, last_c;
338 int i;
339 uint8_t *src;
340
341 if (byte_offs + count + (bit_offs ? 1 : 0) > bv->data_len)
342 return -EINVAL;
343
344 if (bit_offs == 0) {
345 memcpy(bytes, bv->data + byte_offs, count);
346 } else {
347 src = bv->data + byte_offs;
348 last_c = *(src++);
349 for (i = count; i > 0; i--) {
350 c = *(src++);
351 *(bytes++) =
352 (last_c << bit_offs) |
353 (c >> (8 - bit_offs));
354 last_c = c;
355 }
356 }
357
358 bv->cur_bit += count * 8;
359 return 0;
360}
361
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200362/*! set multiple bytes at current pos
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100363 * Assumes MSB first encoding.
364 * \param[in] bv bit vector
365 * \param[in] bytes array
366 * \param[in] count number of bytes to copy
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200367 * \return 0 on success; negative otherwise
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100368 */
Maxe49af082016-01-22 16:46:56 +0100369int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count)
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100370{
371 int byte_offs = bytenum_from_bitnum(bv->cur_bit);
372 int bit_offs = bv->cur_bit % 8;
373 uint8_t c, last_c;
374 int i;
375 uint8_t *dst;
376
377 if (byte_offs + count + (bit_offs ? 1 : 0) > bv->data_len)
378 return -EINVAL;
379
380 if (bit_offs == 0) {
381 memcpy(bv->data + byte_offs, bytes, count);
382 } else if (count > 0) {
383 dst = bv->data + byte_offs;
384 /* Get lower bits of first dst byte */
385 last_c = *dst >> (8 - bit_offs);
386 for (i = count; i > 0; i--) {
387 c = *(bytes++);
388 *(dst++) =
389 (last_c << (8 - bit_offs)) |
390 (c >> bit_offs);
391 last_c = c;
392 }
393 /* Overwrite lower bits of N+1 dst byte */
394 *dst = (*dst & ((1 << (8 - bit_offs)) - 1)) |
395 (last_c << (8 - bit_offs));
396 }
397
398 bv->cur_bit += count * 8;
399 return 0;
400}
Maxa15f05f2016-01-26 10:43:15 +0100401
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200402/*! Allocate a bit vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200403 * \param[in] size Number of bits in the vector
404 * \param[in] ctx Context from which to allocate
405 * \return pointer to allocated vector; NULL in case of error */
Maxa15f05f2016-01-26 10:43:15 +0100406struct bitvec *bitvec_alloc(unsigned int size, TALLOC_CTX *ctx)
407{
408 struct bitvec *bv = talloc_zero(ctx, struct bitvec);
409 if (!bv)
410 return NULL;
411
412 bv->data = talloc_zero_array(bv, uint8_t, size);
413 if (!(bv->data)) {
414 talloc_free(bv);
415 return NULL;
416 }
417
418 bv->data_len = size;
419 bv->cur_bit = 0;
420 return bv;
421}
422
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200423/*! Free a bit vector (release its memory)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200424 * \param[in] bit vector to free */
Maxa15f05f2016-01-26 10:43:15 +0100425void bitvec_free(struct bitvec *bv)
426{
427 talloc_free(bv->data);
428 talloc_free(bv);
429}
430
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200431/*! Export a bit vector to a buffer
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200432 * \param[in] bitvec (unpacked bits)
433 * \param[out] buffer for the unpacked bits
434 * \return number of bytes (= bits) copied */
Maxa15f05f2016-01-26 10:43:15 +0100435unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer)
436{
437 unsigned int i = 0;
438 for (i = 0; i < bv->data_len; i++)
439 buffer[i] = bv->data[i];
440
441 return i;
442}
443
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200444/*! Copy buffer of unpacked bits into bit vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200445 * \param[in] buffer unpacked input bits
446 * \param[out] bv unpacked bit vector
447 * \return number of bytes (= bits) copied */
Maxa15f05f2016-01-26 10:43:15 +0100448unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer)
449{
450 unsigned int i = 0;
451 for (i = 0; i < bv->data_len; i++)
452 bv->data[i] = buffer[i];
453
454 return i;
455}
456
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200457/*! read hexadecimap string into a bit vector
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200458 * \param[in] src string containing hex digits
459 * \param[out] bv unpacked bit vector
460 * \return 0 in case of success; 1 in case of error
461 */
Maxa15f05f2016-01-26 10:43:15 +0100462int bitvec_unhex(struct bitvec *bv, const char *src)
463{
Holger Hans Peter Freyther2745b482016-01-27 17:08:02 +0100464 unsigned i;
Maxa15f05f2016-01-26 10:43:15 +0100465 unsigned val;
466 unsigned write_index = 0;
467 unsigned digits = bv->data_len * 2;
Holger Hans Peter Freyther2745b482016-01-27 17:08:02 +0100468
469 for (i = 0; i < digits; i++) {
Maxa15f05f2016-01-26 10:43:15 +0100470 if (sscanf(src + i, "%1x", &val) < 1) {
471 return 1;
472 }
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100473 bitvec_write_field(bv, &write_index, val, 4);
Maxa15f05f2016-01-26 10:43:15 +0100474 }
475 return 0;
476}
477
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200478/*! read part of the vector
Max912bc6f2016-01-28 12:07:12 +0100479 * \param[in] bv The boolean vector to work on
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100480 * \param[in,out] read_index Where reading supposed to start in the vector
Max912bc6f2016-01-28 12:07:12 +0100481 * \param[in] len How many bits to read from vector
482 * \returns read bits or negative value on error
483 */
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100484uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned int len)
Maxa15f05f2016-01-26 10:43:15 +0100485{
486 unsigned int i;
487 uint64_t ui = 0;
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100488 bv->cur_bit = *read_index;
Maxa15f05f2016-01-26 10:43:15 +0100489
490 for (i = 0; i < len; i++) {
491 int bit = bitvec_get_bit_pos((const struct bitvec *)bv, bv->cur_bit);
492 if (bit < 0)
493 return bit;
494 if (bit)
495 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
506 * \returns next write index or negative value on error
507 */
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';
533 default: abort();
534 }
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 */
607unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, int max_bits)
608{
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/*! @} */