Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 56 lines (51 sloc) 1.261 kB
30c9490 @nu774 use bitstream
authored
1 #include "bitstream.h"
2
3 uint32_t BitStream::peek(uint32_t nbits)
4 {
5 uint8_t *p = &m_buffer[m_cur];
6 uint32_t v = (*p++ << m_pos) & 0xff;
7 if (nbits <= 8 - m_pos)
8 return v >> (8 - nbits);
9 v >>= m_pos;
10 nbits = nbits - 8 + m_pos;
11 for (; nbits >= 8; nbits -= 8)
12 v = v << 8 | *p++;
13 if (nbits > 0)
14 v = v << nbits | (*p << nbits) >> 8;
15 return v;
16 }
17
18 uint32_t BitStream::get(uint32_t nbits)
19 {
20 uint32_t value = peek(nbits);
21 advance(nbits);
22 return value;
23 }
24
25 void BitStream::put(uint32_t value, uint32_t nbits)
26 {
27 uint32_t free_bits = 8 - m_pos;
28 while (nbits > 0) {
29 uint32_t width = std::min(free_bits, nbits);
30 uint32_t new_free_bits = free_bits - width;
31 uint32_t v = value >> (nbits - width);
32 uint32_t mask = 0xffu >> (8 - width);
33 mask <<= new_free_bits;
34 v = (v << new_free_bits) & mask;
35 while (m_buffer.size() <= m_cur)
36 m_buffer.push_back(0);
37 m_buffer[m_cur] = (m_buffer[m_cur] & ~mask) | v;
38 nbits -= width;
39 free_bits = new_free_bits;
40 if (free_bits == 0) {
41 ++m_cur;
42 free_bits = 8;
43 }
44 }
45 m_pos = 8 - free_bits;
46 }
47
48 void BitStream::advance(size_t nbits)
49 {
50 if (nbits) {
51 m_pos += nbits;
52 m_cur += (m_pos >> 3);
53 m_pos &= 7;
54 }
55 }
Something went wrong with that request. Please try again.