libosmocore  0.9.3.20160317
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 <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, enum bit_value *bits, unsigned int count);
74 int bitvec_set_uint(struct bitvec *bv, uint32_t 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 void bitvec_shiftl(struct bitvec *bv, unsigned int n);
93 int16_t bitvec_get_int16_msb(const struct bitvec *bv, unsigned int num_bits);
94 unsigned int bitvec_add_array(struct bitvec *bv, const uint32_t *array,
95  unsigned int array_len, bool dry_run,
96  unsigned int num_bits);
97