File: bit_io.h

package info (click to toggle)
libepsilon 0.9.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,168 kB
  • ctags: 1,205
  • sloc: ansic: 11,329; sh: 9,321; perl: 936; xml: 86; makefile: 84
file content (128 lines) | stat: -rw-r--r-- 3,439 bytes parent folder | download | duplicates (6)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * $Id: bit_io.h,v 1.17 2010/02/05 23:50:22 simakov Exp $
 *
 * EPSILON - wavelet image compression library.
 * Copyright (C) 2006,2007,2010 Alexander Simakov, <xander@entropyware.info>
 *
 * This file is part of EPSILON
 *
 * EPSILON is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * EPSILON is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with EPSILON.  If not, see <http://www.gnu.org/licenses/>.
 *
 * http://epsilon-project.sourceforge.net
 */

/** \file
 *
 *  \brief Bit I/O
 *
 *  This file contains bit I/O routines. */

#ifndef __BIT_IO_H__
#define __BIT_IO_H__

#ifdef __cplusplus
extern "C" {
#endif

/** \addtogroup bit_io Bit I/O */
/*@{*/

#include <common.h>

/** All bits are successfuly processed */
#define BIT_BUFFER_OK           0
/** Cannot write bits, output buffer is full */
#define BIT_BUFFER_OVERFLOW     1
/** Cannot read bits, input buffer is empty */
#define BIT_BUFFER_UNDERFLOW    2

/** Bit-buffer structure
 *
 *  This structure represents bit-buffer. */
typedef struct bit_buffer_tag {
    /** Start of buffer */
    unsigned char *start;
    /** End of buffer */
    unsigned char *end;
    /** Next input/output byte */
    unsigned char *next;
    /** Bit buffer */
    unsigned long bits;
    /** Pending bits */
    int pending;
} bit_buffer;

/** Write one zero bit */
#define write_0(_bb)            write_bits(_bb, 0UL, 1)
/** Write one unity bit */
#define write_1(_bb)            write_bits(_bb, 1UL, 1)
/** Read one bit */
#define read_bit(_bb, _bit)     read_bits(_bb, _bit, 1)

/** Initialize bit-buffer for reading or writting
 *
 *  This function initializes bit-buffer \a bb for reading or
 *  writting bits.
 *
 *  \param bb Bit-buffer to initialize
 *  \param buf Actual input/output buffer
 *  \param size Actual buffer size
 *
 *  \return \c VOID */
void init_bits(bit_buffer *bb, unsigned char *buf, int size);

/** Write bits
 *
 *  This function writes \a size least significant bits of
 *  the \a value to the bit-buffer \a bb.
 *
 *  \note The function expects that \a size <= 24.
 *
 *  \param bb Bit-buffer
 *  \param value Bits to write
 *  \param size Number of bits to write
 *
 *  \return Either \ref BIT_BUFFER_OK or \ref BIT_BUFFER_OVERFLOW */
int write_bits(bit_buffer *bb, int value, int size);

/** Read bits
 *
 *  This function reads \a size least significant bits
 *  from the bit-buffer \a bb  into the \a value argument.
 *
 *  \note Function expects that \a size <= 24.
 *
 *  \param bb Bit-buffer
 *  \param value Location to store the bits
 *  \param size Number of bits to read
 *
 *  \return Either \ref BIT_BUFFER_OK or \ref BIT_BUFFER_UNDERFLOW */
int read_bits(bit_buffer *bb, int *value, int size);

/** Flush bits
 *
 *  This function flushes all pending bits if there are any.
 *
 *  \param bb Bit-buffer
 *
 *  \return Either \ref BIT_BUFFER_OK or \ref BIT_BUFFER_OVERFLOW */
int flush_bits(bit_buffer *bb);

/*@}*/

#ifdef __cplusplus
}
#endif

#endif /* __BIT_IO_H__ */