ce188d4d |
1 | // license:BSD-3-Clause |
2 | // copyright-holders:Aaron Giles |
3 | /*************************************************************************** |
4 | |
5 | bitstream.c |
6 | |
7 | Helper classes for reading/writing at the bit level. |
8 | |
9 | ***************************************************************************/ |
10 | |
11 | #include "bitstream.h" |
12 | #include <stdlib.h> |
13 | |
14 | //************************************************************************** |
15 | // INLINE FUNCTIONS |
16 | //************************************************************************** |
17 | |
18 | int bitstream_overflow(struct bitstream* bitstream) { return ((bitstream->doffset - bitstream->bits / 8) > bitstream->dlength); } |
19 | |
20 | //------------------------------------------------- |
21 | // create_bitstream - constructor |
22 | //------------------------------------------------- |
23 | |
24 | struct bitstream* create_bitstream(const void *src, uint32_t srclength) |
25 | { |
26 | struct bitstream* bitstream = (struct bitstream*)malloc(sizeof(struct bitstream)); |
27 | bitstream->buffer = 0; |
28 | bitstream->bits = 0; |
29 | bitstream->read = (const uint8_t*)src; |
30 | bitstream->doffset = 0; |
31 | bitstream->dlength = srclength; |
32 | return bitstream; |
33 | } |
34 | |
35 | |
36 | //----------------------------------------------------- |
37 | // bitstream_peek - fetch the requested number of bits |
38 | // but don't advance the input pointer |
39 | //----------------------------------------------------- |
40 | |
41 | uint32_t bitstream_peek(struct bitstream* bitstream, int numbits) |
42 | { |
43 | if (numbits == 0) |
44 | return 0; |
45 | |
46 | // fetch data if we need more |
47 | if (numbits > bitstream->bits) |
48 | { |
49 | while (bitstream->bits <= 24) |
50 | { |
51 | if (bitstream->doffset < bitstream->dlength) |
52 | bitstream->buffer |= bitstream->read[bitstream->doffset] << (24 - bitstream->bits); |
53 | bitstream->doffset++; |
54 | bitstream->bits += 8; |
55 | } |
56 | } |
57 | |
58 | // return the data |
59 | return bitstream->buffer >> (32 - numbits); |
60 | } |
61 | |
62 | |
63 | //----------------------------------------------------- |
64 | // bitstream_remove - advance the input pointer by the |
65 | // specified number of bits |
66 | //----------------------------------------------------- |
67 | |
68 | void bitstream_remove(struct bitstream* bitstream, int numbits) |
69 | { |
70 | bitstream->buffer <<= numbits; |
71 | bitstream->bits -= numbits; |
72 | } |
73 | |
74 | |
75 | //----------------------------------------------------- |
76 | // bitstream_read - fetch the requested number of bits |
77 | //----------------------------------------------------- |
78 | |
79 | uint32_t bitstream_read(struct bitstream* bitstream, int numbits) |
80 | { |
81 | uint32_t result = bitstream_peek(bitstream, numbits); |
82 | bitstream_remove(bitstream, numbits); |
83 | return result; |
84 | } |
85 | |
86 | |
87 | //------------------------------------------------- |
88 | // read_offset - return the current read offset |
89 | //------------------------------------------------- |
90 | |
91 | uint32_t bitstream_read_offset(struct bitstream* bitstream) |
92 | { |
93 | uint32_t result = bitstream->doffset; |
94 | int bits = bitstream->bits; |
95 | while (bits >= 8) |
96 | { |
97 | result--; |
98 | bits -= 8; |
99 | } |
100 | return result; |
101 | } |
102 | |
103 | |
104 | //------------------------------------------------- |
105 | // flush - flush to the nearest byte |
106 | //------------------------------------------------- |
107 | |
108 | uint32_t bitstream_flush(struct bitstream* bitstream) |
109 | { |
110 | while (bitstream->bits >= 8) |
111 | { |
112 | bitstream->doffset--; |
113 | bitstream->bits -= 8; |
114 | } |
115 | bitstream->bits = bitstream->buffer = 0; |
116 | return bitstream->doffset; |
117 | } |
118 | |