blob: 9187ce234ae9a984dd0f039e0701069259082e8c [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +01002
3/* bit vector utility routines */
4
5/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
Maxa15f05f2016-01-26 10:43:15 +01006 * (C) 2012 Ivan Klyuchnikov
Max0a59e982016-02-05 13:55:37 +01007 * (C) 2015 Sysmocom s.f.m.c. GmbH
Harald Welteec8b4502010-02-20 20:34:29 +01008 *
9 * All Rights Reserved
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 */
26
Harald Welteba6988b2011-08-17 12:46:48 +020027/*! \defgroup bitvec Bit vectors
28 * @{
29 */
30
31/*! \file bitvec.h
Neels Hofmeyr87e45502017-06-20 00:17:59 +020032 * Osmocom bit vector abstraction
Jacob Erlbeck5f349be2015-12-21 16:04:03 +010033 *
34 * These functions assume a MSB (most significant bit) first layout of the
35 * bits, so that for instance the 5 bit number abcde (a is MSB) can be
36 * embedded into a byte sequence like in xxxxxxab cdexxxxx. The bit count
37 * starts with the MSB, so the bits in a byte are numbered (MSB) 01234567 (LSB).
38 * Note that there are other incompatible encodings, like it is used
39 * for the EGPRS RLC data block headers (there the bits are numbered from LSB
40 * to MSB).
Harald Welteba6988b2011-08-17 12:46:48 +020041 */
42
Harald Weltecd623eb2011-05-29 15:37:38 +020043#include <stdint.h>
Harald Weltef2899c62017-01-15 17:54:11 +010044#include <osmocom/core/talloc.h>
Max0a59e982016-02-05 13:55:37 +010045#include <stdbool.h>
Harald Welteec8b4502010-02-20 20:34:29 +010046
Neels Hofmeyr87e45502017-06-20 00:17:59 +020047/*! A single GSM bit
Harald Welteba6988b2011-08-17 12:46:48 +020048 *
49 * 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 +010050 * defined relative to the 0x2b padding pattern */
51enum bit_value {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020052 ZERO = 0, /*!< A zero (0) bit */
53 ONE = 1, /*!< A one (1) bit */
54 L = 2, /*!< A CSN.1 "L" bit */
55 H = 3, /*!< A CSN.1 "H" bit */
Harald Welteec8b4502010-02-20 20:34:29 +010056};
57
Neels Hofmeyr87e45502017-06-20 00:17:59 +020058/*! structure describing a bit vector */
Harald Welteec8b4502010-02-20 20:34:29 +010059struct bitvec {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020060 unsigned int cur_bit; /*!< cursor to the next unused bit */
61 unsigned int data_len; /*!< length of data array in bytes */
62 uint8_t *data; /*!< pointer to data array */
Harald Welteec8b4502010-02-20 20:34:29 +010063};
64
Harald Welted9abf012010-03-06 11:28:49 +010065enum bit_value bitvec_get_bit_pos(const struct bitvec *bv, unsigned int bitnr);
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +000066enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv,
67 unsigned int bitnr);
Harald Welted9abf012010-03-06 11:28:49 +010068unsigned int bitvec_get_nth_set_bit(const struct bitvec *bv, unsigned int n);
Harald Welteec8b4502010-02-20 20:34:29 +010069int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnum,
70 enum bit_value bit);
Harald Welteec8b4502010-02-20 20:34:29 +010071int bitvec_set_bit(struct bitvec *bv, enum bit_value bit);
Andreas.Eversberg0ebd6882010-05-09 09:36:54 +000072int bitvec_get_bit_high(struct bitvec *bv);
Harald Welte14bf28a2016-06-27 15:19:10 +020073int bitvec_set_bits(struct bitvec *bv, const enum bit_value *bits, unsigned int count);
Harald Welte449324b2017-01-15 17:55:32 +010074int bitvec_set_uint(struct bitvec *bv, unsigned int in, unsigned int count);
Maxe49af082016-01-22 16:46:56 +010075int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits);
Pablo Neira Ayuso36bdf2c2011-03-28 19:24:19 +020076int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n, enum bit_value val);
Harald Welteec8b4502010-02-20 20:34:29 +010077int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit);
Maxe49af082016-01-22 16:46:56 +010078int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count);
79int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count);
Maxa15f05f2016-01-26 10:43:15 +010080struct bitvec *bitvec_alloc(unsigned int size, TALLOC_CTX *bvctx);
81void bitvec_free(struct bitvec *bv);
82int bitvec_unhex(struct bitvec *bv, const char *src);
83unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer);
84unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer);
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +010085uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned int len);
86int bitvec_write_field(struct bitvec *bv, unsigned int *write_index, uint64_t val, unsigned int len);
Max0a59e982016-02-05 13:55:37 +010087int bitvec_fill(struct bitvec *bv, unsigned int num_bits, enum bit_value fill);
88char bit_value_to_char(enum bit_value v);
89void bitvec_to_string_r(const struct bitvec *bv, char *str);
90void bitvec_zero(struct bitvec *bv);
91unsigned bitvec_rl(const struct bitvec *bv, bool b);
Pravin Kumarvel848de8f2016-12-02 15:13:03 +053092unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, int max_bits);
Max0a59e982016-02-05 13:55:37 +010093void bitvec_shiftl(struct bitvec *bv, unsigned int n);
94int16_t bitvec_get_int16_msb(const struct bitvec *bv, unsigned int num_bits);
Maxd4793212016-03-17 11:51:08 +010095unsigned int bitvec_add_array(struct bitvec *bv, const uint32_t *array,
96 unsigned int array_len, bool dry_run,
97 unsigned int num_bits);
Harald Welteec8b4502010-02-20 20:34:29 +010098
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020099/*! @} */