libosmocore  0.9.6-11.20170220git32ee5af8.fc31
Osmocom core library
bitvec.h
Go to the documentation of this file.
1 #pragma once
2 
3 /* bit vector utility routines */
4 
5 /* (C) 2009 by Harald Welte <laforge@gnumonks.org>
6  * (C) 2012 Ivan Klyuchnikov
7  * (C) 2015 Sysmocom s.f.m.c. GmbH
8  *
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 
43 #include <stdint.h>
44 #include <osmocom/core/talloc.h>
45 #include <stdbool.h>
46 
51 enum bit_value {
52  ZERO = 0,
53  ONE = 1,
54  L = 2,
55  H = 3,
56 };
57 
59 struct bitvec {
60  unsigned int cur_bit;
61  unsigned int data_len;
62  uint8_t *data;
63 };
64 
65 enum bit_value bitvec_get_bit_pos(const struct bitvec *bv, unsigned int bitnr);
66 enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv,
67  unsigned int bitnr);
68 unsigned int bitvec_get_nth_set_bit(const struct bitvec *bv, unsigned int n);
69 int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnum,
70  enum bit_value bit);
71 int bitvec_set_bit(struct bitvec *bv, enum bit_value bit);
72 int bitvec_get_bit_high(struct bitvec *bv);
73 int bitvec_set_bits(struct bitvec *bv, const enum bit_value *bits, unsigned int count);
74 int bitvec_set_uint(struct bitvec *bv, unsigned int in, unsigned int count);
75 int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits);
76 int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n, enum bit_value val);
77 int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit);
78 int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count);
79 int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count);
80 struct bitvec *bitvec_alloc(unsigned int size, TALLOC_CTX *bvctx);
81 void bitvec_free(struct bitvec *bv);
82 int bitvec_unhex(struct bitvec *bv, const char *src);
83 unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer);
84 unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer);
85 uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned int len);
86 int bitvec_write_field(struct bitvec *bv, unsigned int *write_index, uint64_t val, unsigned int len);
87 int bitvec_fill(struct bitvec *bv, unsigned int num_bits, enum bit_value fill);
88 char bit_value_to_char(enum bit_value v);
89 void bitvec_to_string_r(const struct bitvec *bv, char *str);
90 void bitvec_zero(struct bitvec *bv);
91 unsigned bitvec_rl(const struct bitvec *bv, bool b);
92 unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, int max_bits);
93 void bitvec_shiftl(struct bitvec *bv, unsigned int n);
94 int16_t bitvec_get_int16_msb(const struct bitvec *bv, unsigned int num_bits);
95 unsigned int bitvec_add_array(struct bitvec *bv, const uint32_t *array,
96  unsigned int array_len, bool dry_run,
97  unsigned int num_bits);
98 
unsigned bitvec_rl(const struct bitvec *bv, bool b)
Return number (bits) of uninterrupted bit run in vector starting from the MSB.
Definition: bitvec.c:567
unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer)
Copy buffer of unpacked bits into bit vector.
Definition: bitvec.c:425
int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits)
get multiple bits (based on numeric value) from current pos
Definition: bitvec.c:247
int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count)
set multiple bytes at current pos Assumes MSB first encoding.
Definition: bitvec.c:346
unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, int max_bits)
Return number (bits) of uninterrupted bit run in vector starting from the current bit.
Definition: bitvec.c:586
int bitvec_get_bit_high(struct bitvec *bv)
get the next bit (low/high) inside a bitvec
Definition: bitvec.c:185
A CSN.1 "L" bit.
Definition: bitvec.h:54
int bitvec_unhex(struct bitvec *bv, const char *src)
read hexadecimap string into a bit vector
Definition: bitvec.c:439
int bitvec_set_bits(struct bitvec *bv, const enum bit_value *bits, unsigned int count)
set multiple bits (based on array of bitvals) at current pos
Definition: bitvec.c:201
int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnum, enum bit_value bit)
set a bit at given position in a bit vector
Definition: bitvec.c:146
unsigned int data_len
length of data array in bytes
Definition: bitvec.h:61
A one (1) bit.
Definition: bitvec.h:53
int bitvec_write_field(struct bitvec *bv, unsigned int *write_index, uint64_t val, unsigned int len)
write into the vector
Definition: bitvec.c:485
void bitvec_free(struct bitvec *bv)
Free a bit vector (release its memory)
Definition: bitvec.c:402
int16_t bitvec_get_int16_msb(const struct bitvec *bv, unsigned int num_bits)
get multiple bits (num_bits) from beginning of vector (MSB side)
Definition: bitvec.c:234
bit_value
A single GSM bit.
Definition: bitvec.h:51
int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count)
get multiple bytes from current pos Assumes MSB first encoding.
Definition: bitvec.c:310
structure describing a bit vector
Definition: bitvec.h:59
void bitvec_zero(struct bitvec *bv)
force bit vector to all 0 and current bit to the beginnig of the vector
Definition: bitvec.c:556
enum bit_value bitvec_get_bit_pos(const struct bitvec *bv, unsigned int bitnr)
check if the bit is 0 or 1 for a given position inside a bitvec
Definition: bitvec.c:80
unsigned int bitvec_add_array(struct bitvec *bv, const uint32_t *array, unsigned int array_len, bool dry_run, unsigned int num_bits)
Add given array to bitvec.
Definition: bitvec.c:666
unsigned int cur_bit
cursor to the next unused bit
Definition: bitvec.h:60
unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer)
Export a bit vector to a buffer.
Definition: bitvec.c:412
uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned int len)
read part of the vector
Definition: bitvec.c:461
int bitvec_set_bit(struct bitvec *bv, enum bit_value bit)
set the next bit inside a bitvec
Definition: bitvec.c:172
uint8_t * data
pointer to data array
Definition: bitvec.h:62
int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit)
pad all remaining bits up to num_bits
Definition: bitvec.c:279
void bitvec_shiftl(struct bitvec *bv, unsigned n)
Shifts bitvec to the left, n MSB bits lost.
Definition: bitvec.c:624
A CSN.1 "H" bit.
Definition: bitvec.h:55
void bitvec_to_string_r(const struct bitvec *bv, char *str)
prints bit vector to provided string It's caller's responsibility to ensure that we won't shoot him i...
Definition: bitvec.c:520
int bitvec_fill(struct bitvec *bv, unsigned int num_bits, enum bit_value fill)
fill num_bits with \fill starting from the current position
Definition: bitvec.c:267
unsigned int bitvec_get_nth_set_bit(const struct bitvec *bv, unsigned int n)
get the Nth set bit inside the bit vector
Definition: bitvec.c:125
int bitvec_set_uint(struct bitvec *bv, unsigned int in, unsigned int count)
set multiple bits (based on numeric value) at current pos
Definition: bitvec.c:216
enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv, unsigned int bitnr)
check if the bit is L or H for a given position inside a bitvec
Definition: bitvec.c:102
A zero (0) bit.
Definition: bitvec.h:52
char bit_value_to_char(enum bit_value v)
convert enum to corresponding character
Definition: bitvec.c:505
int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n, enum bit_value val)
find first bit set in bit vector
Definition: bitvec.c:290
struct bitvec * bitvec_alloc(unsigned int size, TALLOC_CTX *bvctx)
Allocate a bit vector.
Definition: bitvec.c:383