1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
12 distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 #ifndef BITBUF2_H
30 #define BITBUF2_H
31
32 #include "igzip_lib.h"
33 #include "unaligned.h"
34
35 #ifdef _MSC_VER
36 #define inline __inline
37 #endif
38
39 /* MAX_BITBUF_BIT WRITE is the maximum number of bits than can be safely written
40 * by consecutive calls of write_bits. Note this assumes the bitbuf is in a
41 * state that is possible at the exit of write_bits */
42 #define MAX_BITBUF_BIT_WRITE 56
43
44 static inline void
init(struct BitBuf2 * me)45 init(struct BitBuf2 *me)
46 {
47 me->m_bits = 0;
48 me->m_bit_count = 0;
49 }
50
51 static inline void
set_buf(struct BitBuf2 * me,unsigned char * buf,unsigned int len)52 set_buf(struct BitBuf2 *me, unsigned char *buf, unsigned int len)
53 {
54 unsigned int slop = 8;
55 me->m_out_buf = me->m_out_start = buf;
56 me->m_out_end = buf + len - slop;
57 }
58
59 static inline int
is_full(struct BitBuf2 * me)60 is_full(struct BitBuf2 *me)
61 {
62 return (me->m_out_buf > me->m_out_end);
63 }
64
65 static inline uint8_t *
buffer_ptr(struct BitBuf2 * me)66 buffer_ptr(struct BitBuf2 *me)
67 {
68 return me->m_out_buf;
69 }
70
71 static inline uint32_t
buffer_used(struct BitBuf2 * me)72 buffer_used(struct BitBuf2 *me)
73 {
74 return (uint32_t) (me->m_out_buf - me->m_out_start);
75 }
76
77 static inline uint32_t
buffer_bits_used(struct BitBuf2 * me)78 buffer_bits_used(struct BitBuf2 *me)
79 {
80 return (8 * (uint32_t) (me->m_out_buf - me->m_out_start) + me->m_bit_count);
81 }
82
83 static inline void
flush_bits(struct BitBuf2 * me)84 flush_bits(struct BitBuf2 *me)
85 {
86 uint32_t bits;
87 store_le_u64(me->m_out_buf, me->m_bits);
88 bits = me->m_bit_count & ~7;
89 me->m_bit_count -= bits;
90 me->m_out_buf += bits / 8;
91 me->m_bits >>= bits;
92 }
93
94 /* Can write up to 8 bytes to output buffer */
95 static inline void
flush(struct BitBuf2 * me)96 flush(struct BitBuf2 *me)
97 {
98 uint32_t bytes;
99 if (me->m_bit_count) {
100 store_le_u64(me->m_out_buf, me->m_bits);
101 bytes = (me->m_bit_count + 7) / 8;
102 me->m_out_buf += bytes;
103 }
104 me->m_bits = 0;
105 me->m_bit_count = 0;
106 }
107
108 static inline void
check_space(struct BitBuf2 * me,uint32_t num_bits)109 check_space(struct BitBuf2 *me, uint32_t num_bits)
110 {
111 /* Checks if bitbuf has num_bits extra space and flushes the bytes in
112 * the bitbuf if it doesn't. */
113 if (63 - me->m_bit_count < num_bits)
114 flush_bits(me);
115 }
116
117 static inline void
write_bits_unsafe(struct BitBuf2 * me,uint64_t code,uint32_t count)118 write_bits_unsafe(struct BitBuf2 *me, uint64_t code, uint32_t count)
119 {
120 me->m_bits |= code << me->m_bit_count;
121 me->m_bit_count += count;
122 }
123
124 static inline void
write_bits(struct BitBuf2 * me,uint64_t code,uint32_t count)125 write_bits(struct BitBuf2 *me, uint64_t code, uint32_t count)
126 { /* Assumes there is space to fit code into m_bits. */
127 me->m_bits |= code << me->m_bit_count;
128 me->m_bit_count += count;
129 flush_bits(me);
130 }
131
132 static inline void
write_bits_flush(struct BitBuf2 * me,uint64_t code,uint32_t count)133 write_bits_flush(struct BitBuf2 *me, uint64_t code, uint32_t count)
134 { /* Assumes there is space to fit code into m_bits. */
135 me->m_bits |= code << me->m_bit_count;
136 me->m_bit_count += count;
137 flush(me);
138 }
139
140 #endif // BITBUF2_H
141