Initial commit. FBReader 0.8.12
[lbook_fbreader.git] / fbreader / src / formats / chm / BitStream.h
blob8b25d135f7013e8bdf2b80248209f9f484f38aea
1 /*
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
17 * 02110-1301, USA.
20 #ifndef __BITSTREAM_H__
21 #define __BITSTREAM_H__
23 #include <string>
25 class BitStream {
27 public:
28 static const int BufferSize;
30 public:
31 BitStream();
32 void setData(const std::string &data);
33 void reset();
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);
42 private:
43 bool ensure(unsigned char length);
45 private:
46 unsigned int myBuffer;
47 unsigned char myBitCounter;
48 const unsigned char *myByteStream;
49 const unsigned char *myByteStreamEnd;
51 private:
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();
62 myBuffer = 0;
63 myBitCounter = 0;
66 inline void BitStream::reset() {
67 myByteStream -= myBitCounter / 8;
68 myBuffer = 0;
69 myBitCounter = 0;
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);
75 myBitCounter += 16;
76 myByteStream += 2;
78 return myBitCounter >= length;
81 inline unsigned int BitStream::peek(unsigned char length) {
82 ensure(length);
83 return (length > 0) ? (myBuffer >> (BufferSize - length)) : 0;
86 inline void BitStream::remove(unsigned char length) {
87 if (ensure(length)) {
88 myBuffer <<= length;
89 myBitCounter -= length;
93 inline unsigned int BitStream::get(unsigned char length) {
94 unsigned int bits;
95 if (length > 16) {
96 bits = peek(length - 16) << 16;
97 remove(length - 16);
98 bits += peek(16);
99 remove(16);
100 } else {
101 bits = peek(length);
102 remove(length);
104 return bits;
107 inline unsigned int BitStream::bytesLeft() const {
108 return myByteStreamEnd - myByteStream;
111 #endif /* __BITSTREAM_H__ */