forked from xxwei/nvrdemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bits.h
executable file
·67 lines (57 loc) · 1.62 KB
/
bits.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*****************************************************************************
* bits.h
*****************************************************************************/
#define uint8_t unsigned char
typedef struct bits_buffer_s
{
int i_size;
int i_data;
uint8_t i_mask;
uint8_t *p_data;
} bits_buffer_t;
static inline int bits_initwrite( bits_buffer_t *p_buffer,
int i_size, void *p_data )
{
p_buffer->i_size = i_size;
p_buffer->i_data = 0;
p_buffer->i_mask = 0x80;
p_buffer->p_data = (uint8_t *)p_data;
if( !p_buffer->p_data )
{
if( !( p_buffer->p_data =(unsigned char *) malloc( i_size ) ) )
return -1;
}
p_buffer->p_data[0] = 0;
return 0;
}
static inline void bits_align( bits_buffer_t *p_buffer )
{
if( p_buffer->i_mask != 0x80 && p_buffer->i_data < p_buffer->i_size )
{
p_buffer->i_mask = 0x80;
p_buffer->i_data++;
p_buffer->p_data[p_buffer->i_data] = 0x00;
}
}
static inline void bits_write( bits_buffer_t *p_buffer,
int i_count, uint64_t i_bits )
{
while( i_count > 0 )
{
i_count--;
if( ( i_bits >> i_count )&0x01 )
{
p_buffer->p_data[p_buffer->i_data] |= p_buffer->i_mask;
}
else
{
p_buffer->p_data[p_buffer->i_data] &= ~p_buffer->i_mask;
}
p_buffer->i_mask >>= 1;
if( p_buffer->i_mask == 0 )
{
p_buffer->i_data++;
p_buffer->i_mask = 0x80;
}
}
}