2 * Copyright (C) 2004-2008 Geometer Plus <contact@geometerplus.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 #ifndef __BITSTREAM_H__
21 #define __BITSTREAM_H__
28 static const int BufferSize
;
32 void setData(const std::string
&data
);
34 unsigned int peek(unsigned char length
);
35 void remove(unsigned char length
);
36 unsigned int get(unsigned char length
);
37 unsigned int bytesLeft() const;
39 unsigned int get4BytesDirect();
40 bool getBytesDirect(unsigned char *buffer
, unsigned int length
);
43 bool ensure(unsigned char length
);
46 unsigned int myBuffer
;
47 unsigned char myBitCounter
;
48 const unsigned char *myByteStream
;
49 const unsigned char *myByteStreamEnd
;
52 BitStream(const BitStream
&);
53 const BitStream
&operator = (const BitStream
&);
56 inline BitStream::BitStream() : myBuffer(0), myBitCounter(0) {
59 inline void BitStream::setData(const std::string
&data
) {
60 myByteStream
= (const unsigned char*)data
.data();
61 myByteStreamEnd
= myByteStream
+ data
.length();
66 inline void BitStream::reset() {
67 myByteStream
-= myBitCounter
/ 8;
72 inline bool BitStream::ensure(unsigned char length
) {
73 while ((myBitCounter
< length
) && (bytesLeft() >= 2)) {
74 myBuffer
|= ((myByteStream
[1] << 8) | myByteStream
[0]) << (BitStream::BufferSize
- 16 - myBitCounter
);
78 return myBitCounter
>= length
;
81 inline unsigned int BitStream::peek(unsigned char length
) {
83 return (length
> 0) ? (myBuffer
>> (BufferSize
- length
)) : 0;
86 inline void BitStream::remove(unsigned char length
) {
89 myBitCounter
-= length
;
93 inline unsigned int BitStream::get(unsigned char length
) {
96 bits
= peek(length
- 16) << 16;
107 inline unsigned int BitStream::bytesLeft() const {
108 return myByteStreamEnd
- myByteStream
;
111 #endif /* __BITSTREAM_H__ */