libosmocore 1.13.1.1-afb4 Osmocom core library

bitvec.h
Go to the documentation of this file.
1/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
2 * (C) 2012 Ivan Klyuchnikov
3 * (C) 2015 sysmocom - s.f.m.c. GmbH
4 *
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 *
17 */
18
19#pragma once
20
25#include <stdint.h>
26#include <osmocom/core/defs.h>
27#include <stdbool.h>
28
33 enum bit_value {
34 ZERO = 0,
35 ONE = 1,
36 L = 2,
37 H = 3,
38};
39
41 struct bitvec {
42 unsigned int cur_bit;
43 unsigned int data_len;
44 uint8_t *data;
45};
46
47 enum bit_value bitvec_get_bit_pos(const struct bitvec *bv, unsigned int bitnr);
48 enum bit_value bitvec_get_bit_pos_high(const struct bitvec *bv,
49 unsigned int bitnr);
50 unsigned int bitvec_get_nth_set_bit(const struct bitvec *bv, unsigned int n);
51 int bitvec_set_bit_pos(struct bitvec *bv, unsigned int bitnum,
52 enum bit_value bit);
53 int bitvec_set_bit(struct bitvec *bv, enum bit_value bit);
54 int bitvec_get_bit_high(struct bitvec *bv);
55 int bitvec_set_bits(struct bitvec *bv, const enum bit_value *bits, unsigned int count);
56 int bitvec_set_u64(struct bitvec *bv, uint64_t v, uint8_t num_bits, bool use_lh);
57 int bitvec_set_uint(struct bitvec *bv, unsigned int in, unsigned int count);
58 int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits);
59 int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n, enum bit_value val);
60 int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit);
61 int bitvec_get_bytes(struct bitvec *bv, uint8_t *bytes, unsigned int count);
62 int bitvec_set_bytes(struct bitvec *bv, const uint8_t *bytes, unsigned int count);
63 struct bitvec *bitvec_alloc(unsigned int size, void *bvctx);
64 void bitvec_free(struct bitvec *bv);
65 int bitvec_unhex(struct bitvec *bv, const char *src);
66 unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer);
67 unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer);
68uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned int len);
69 int bitvec_write_field(struct bitvec *bv, unsigned int *write_index, uint64_t val, unsigned int len);
70 int bitvec_fill(struct bitvec *bv, unsigned int num_bits, enum bit_value fill);
71 char bit_value_to_char(enum bit_value v);
72 void bitvec_to_string_r(const struct bitvec *bv, char *str);
73 void bitvec_zero(struct bitvec *bv);
74 unsigned bitvec_rl(const struct bitvec *bv, bool b);
75 unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, unsigned int max_bits);
76 void bitvec_shiftl(struct bitvec *bv, unsigned int n);
77int16_t bitvec_get_int16_msb(const struct bitvec *bv, unsigned int num_bits);
78 unsigned int bitvec_add_array(struct bitvec *bv, const uint32_t *array,
79 unsigned int array_len, bool dry_run,
80 unsigned int num_bits);
81
83 static 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
92 static inline unsigned int bitvec_tailroom_bits(const struct bitvec *bv)
93{
94 return bv->data_len*8 - bv->cur_bit;
95}
96
defs.h
General definitions that are meant to be included from header files.
bitvec_get_int16_msb
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:250
bitvec_pack
unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer)
Export a bit vector to a buffer.
Definition: bitvec.c:430
bitvec_set_u64
int bitvec_set_u64(struct bitvec *bv, uint64_t v, uint8_t num_bits, bool use_lh)
set multiple bits (based on numeric value) at current pos.
Definition: bitvec.c:219
bitvec_rl_curbit
unsigned bitvec_rl_curbit(struct bitvec *bv, bool b, unsigned int max_bits)
Return number (bits) of uninterrupted bit run in vector starting from the current bit.
Definition: bitvec.c:606
bit_value_to_char
char bit_value_to_char(enum bit_value v)
convert enum to corresponding character
Definition: bitvec.c:527
bitvec_read_field
uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned int len)
read part of the vector
Definition: bitvec.c:477
bitvec_alloc
struct bitvec * bitvec_alloc(unsigned int size, void *bvctx)
Allocate a bit vector.
Definition: bitvec.c:399
bitvec_get_bytes
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:326
bitvec_get_nth_set_bit
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:123
bitvec_set_bit_pos
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:144
bitvec_fill
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:283
bitvec_to_string_r
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:542
bitvec_shiftl
void bitvec_shiftl(struct bitvec *bv, unsigned int n)
bitvec_used_bytes
static unsigned int bitvec_used_bytes(const struct bitvec *bv)
Return the number of bytes used within the bit vector.
Definition: bitvec.h:83
bitvec_spare_padding
int bitvec_spare_padding(struct bitvec *bv, unsigned int up_to_bit)
pad all remaining bits up to a given bit number
Definition: bitvec.c:295
bitvec_find_bit_pos
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:306
bitvec_tailroom_bits
static unsigned int bitvec_tailroom_bits(const struct bitvec *bv)
Return the tailroom in number of unused bits remaining in the bit-vector.
Definition: bitvec.h:92
bitvec_set_bits
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:199
bitvec_get_bit_pos_high
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:100
bitvec_get_bit_high
int bitvec_get_bit_high(struct bitvec *bv)
get the next bit (low/high) inside a bitvec
Definition: bitvec.c:183
bit_value
bit_value
A single GSM bit.
Definition: bitvec.h:33
bitvec_set_bit
int bitvec_set_bit(struct bitvec *bv, enum bit_value bit)
set the next bit inside a bitvec
Definition: bitvec.c:170
bitvec_rl
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:587
bitvec_get_bit_pos
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:78
bitvec_zero
void bitvec_zero(struct bitvec *bv)
force bit vector to all 0 and current bit to the beginnig of the vector
Definition: bitvec.c:576
bitvec_write_field
int bitvec_write_field(struct bitvec *bv, unsigned int *write_index, uint64_t val, unsigned int len)
write into the vector
Definition: bitvec.c:509
bitvec_get_uint
int bitvec_get_uint(struct bitvec *bv, unsigned int num_bits)
get multiple bits (based on numeric value) from current pos
Definition: bitvec.c:263
bitvec_unpack
unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer)
Copy buffer of unpacked bits into bit vector.
Definition: bitvec.c:443
bitvec_set_uint
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:243
bitvec_set_bytes
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:362
bitvec_unhex
int bitvec_unhex(struct bitvec *bv, const char *src)
read hexadecimap string into a bit vector
Definition: bitvec.c:457
bitvec_add_array
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:686
bitvec_free
void bitvec_free(struct bitvec *bv)
Free a bit vector (release its memory)
Definition: bitvec.c:418
H
@ H
A CSN.1 "H" bit.
Definition: bitvec.h:37
ONE
@ ONE
A one (1) bit.
Definition: bitvec.h:35
ZERO
@ ZERO
A zero (0) bit.
Definition: bitvec.h:34
L
@ L
A CSN.1 "L" bit.
Definition: bitvec.h:36
len
static size_t len(const char *str)
n
write Write running configuration to or terminal n Write configuration to the copy running config startup Copy configuration n Copy running config to n Copy running config to startup write Write running configuration to or terminal n Write to terminal n
bitvec
structure describing a bit vector
Definition: bitvec.h:41
bitvec::data
uint8_t * data
pointer to data array
Definition: bitvec.h:44
bitvec::cur_bit
unsigned int cur_bit
cursor to the next unused bit
Definition: bitvec.h:42
bitvec::data_len
unsigned int data_len
length of data array in bytes
Definition: bitvec.h:43
buffer