blob: 1e2fe7b400e080bebf861d4471662bdaadb5f3e0 [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 sysmocom - s.f.m.c. GmbH
Harald Welteec8b4502010-02-20 20:34:29 +01004 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
Harald Welteec8b4502010-02-20 20:34:29 +010017 */
18
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020019#pragma once
20
Harald Welteba6988b2011-08-17 12:46:48 +020021/*! \defgroup bitvec Bit vectors
22 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020023 * \file bitvec.h */
Harald Welteba6988b2011-08-17 12:46:48 +020024
Harald Weltecd623eb2011-05-29 15:37:38 +020025#include <stdint.h>
Max0b3db502017-10-18 13:48:10 +020026#include <osmocom/core/defs.h>
Max0a59e982016-02-05 13:55:37 +010027#include <stdbool.h>
Harald Welteec8b4502010-02-20 20:34:29 +010028
Neels Hofmeyr87e45502017-06-20 00:17:59 +020029/*! A single GSM bit
Harald Welteba6988b2011-08-17 12:46:48 +020030 *
31 * In GSM mac blocks, every bit can be 0 or 1, or L or H. L/H are
Harald Welteec8b4502010-02-20 20:34:29 +010032 * defined relative to the 0x2b padding pattern */
33enum bit_value {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020034 ZERO = 0, /*!< A zero (0) bit */
35 ONE = 1, /*!< A one (1) bit */
36 L = 2, /*!< A CSN.1 "L" bit */
37 H = 3, /*!< A CSN.1 "H" bit */
Harald Welteec8b4502010-02-20 20:34:29 +010038};
39
Neels Hofmeyr87e45502017-06-20 00:17:59 +020040/*! structure describing a bit vector */
Harald Welteec8b4502010-02-20 20:34:29 +010041struct bitvec {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020042 unsigned int cur_bit; /*!< cursor to the next unused bit */
43 unsigned int data_len; /*!< length of data array in bytes */
44 uint8_t *data; /*!< pointer to data array */
Harald Welteec8b4502010-02-20 20:34:29 +010045};
46
Harald Welted9abf012010-03-06 11:28:49 +010047enum bit_value bitvec_get_bit_pos(const struct bitvec *bv, unsigned int bitnr);
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +000048enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv,
49 unsigned int bitnr);
Harald Welted9abf012010-03-06 11:28:49 +010050unsigned int bitvec_get_nth_set_bit(const struct bitvec *bv, unsigned int n);
Harald Welteec8b4502010-02-20 20:34:29 +010051int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnum,
52 enum bit_value bit);
Harald Welteec8b4502010-02-20 20:34:29 +010053int bitvec_set_bit(struct bitvec *bv, enum bit_value bit);
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +000054int bitvec_get_bit_high(struct bitvec *bv);
Harald Welte14bf28a2016-06-27 15:19:10 +020055int bitvec_set_bits(struct bitvec *bv, const enum bit_value *bits, unsigned int count);
Max0b3db502017-10-18 13:48:10 +020056int bitvec_set_u64(struct bitvec *bv, uint64_t v, uint8_t num_bits, bool use_lh);
Harald Welte449324b2017-01-15 17:55:32 +010057int bitvec_set_uint(struct bitvec *bv, unsigned int in, unsigned int count);
Maxe49af082016-01-22 16:46:56 +010058int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits);
Pablo Neira Ayuso36bdf2c2011-03-28 19:24:19 +020059int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n, enum bit_value val);
Harald Welteec8b4502010-02-20 20:34:29 +010060int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit);
Maxe49af082016-01-22 16:46:56 +010061int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count);
62int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count);
Vadim Yanitskiy37dc9952023-03-31 05:39:04 +070063struct bitvec *bitvec_alloc(unsigned int size, void *bvctx);
Maxa15f05f2016-01-26 10:43:15 +010064void bitvec_free(struct bitvec *bv);
65int bitvec_unhex(struct bitvec *bv, const char *src);
66unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer);
67unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer);
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +010068uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned int len);
69int bitvec_write_field(struct bitvec *bv, unsigned int *write_index, uint64_t val, unsigned int len);
Max0a59e982016-02-05 13:55:37 +010070int bitvec_fill(struct bitvec *bv, unsigned int num_bits, enum bit_value fill);
71char bit_value_to_char(enum bit_value v);
72void bitvec_to_string_r(const struct bitvec *bv, char *str);
73void bitvec_zero(struct bitvec *bv);
74unsigned bitvec_rl(const struct bitvec *bv, bool b);
Harald Weltec85aaed2022-01-09 11:56:19 +010075unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, unsigned int max_bits);
Max0a59e982016-02-05 13:55:37 +010076void bitvec_shiftl(struct bitvec *bv, unsigned int n);
77int16_t bitvec_get_int16_msb(const struct bitvec *bv, unsigned int num_bits);
Maxd4793212016-03-17 11:51:08 +010078unsigned int bitvec_add_array(struct bitvec *bv, const uint32_t *array,
79 unsigned int array_len, bool dry_run,
80 unsigned int num_bits);
Harald Welteec8b4502010-02-20 20:34:29 +010081
Harald Welteae7966d2019-02-03 12:04:46 +010082/*! Return the number of bytes used within the bit vector */
83static inline unsigned int bitvec_used_bytes(const struct bitvec *bv)
84{
85 unsigned int bytes = bv->cur_bit/8;
86 if (bv->cur_bit%8)
87 bytes++;
88 return bytes;
89}
90
Harald Welteb0708d32019-02-04 12:09:05 +010091/*! Return the tailroom in number of unused bits remaining in the bit-vector */
92static inline unsigned int bitvec_tailroom_bits(const struct bitvec *bv)
93{
94 return bv->data_len*8 - bv->cur_bit;
95}
96
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020097/*! @} */