-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbitstream.h
More file actions
59 lines (47 loc) · 1.67 KB
/
bitstream.h
File metadata and controls
59 lines (47 loc) · 1.67 KB
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
/**
* @file
* Bitstream lib header
*/
#ifndef BITSTREAM_H
#define BITSTREAM_H
#include <stdint.h>
#include <stdio.h>
enum bs_type {
BS_READ,
BS_WRITE
};
struct bitstream {
uint8_t buf; ///< buffer
int bufoffset; ///< offset (unread/written bits in buffer)
FILE *fp; ///< file pointer
enum bs_type type; ///< bitstream type {BS_READ, BS_WRITE}
int padding; ///< padding bits in last byte
int last; ///< is current byte the last one
};
/** Initialize a bitstream.
* @param[in] fp Pointer to file
* @param[in] type Type of bitstream {BS_READ, BS_WRITE}
* @param[in] padding Optional parameter, to be sent if type is BS_READ
* @retval bs Pointer to bitstream context
*/
struct bitstream * initbitstream(FILE *fp, enum bs_type type, ...);
/** Close bitstream.
* Frees the bitstream context, and returns the padding.
* Note: It doesn't close the file stream
* @param[in] bs Pointer to bitstream context
* @retval padding Number of bits padded with 0 in the last
* byte written to the file
* NB: If type is BS_READ, returns 0
*/
int closebitstream(struct bitstream *bs);
/** Write a bit character to the stream. **/
void write_bit(struct bitstream *bs, char c);
/** Write a bitstream string to the stream. **/
void write_bitstring(struct bitstream *bs, char *str);
/** Read a bit (int) from the bitstream **/
int read_bit(struct bitstream *bs);
/** Read n-bits from the bitstream (n <= 64) **/
uint64_t read_nbits(struct bitstream *bs, uint8_t n);
/** Read next byte from the bitstream **/
uint8_t read_byte(struct bitstream *bs);
#endif