src/stream/ZBitStream.h

00001 /*  @(#) $Id: ZBitStream.h,v 1.1 2007/04/01 12:51:30 agreen Exp $ */
00002 
00003 /* ------------------------------------------------------------
00004 Copyright (c) 2007 Andrew Green
00005 http://www.zoolib.org
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020 COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00021 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00022 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023 ------------------------------------------------------------ */
00024 
00025 #ifndef __ZBitStream__
00026 #define __ZBitStream__ 1
00027 #include "zconfig.h"
00028 
00029 #include "ZStream.h"
00030 
00031 /*
00032 Bits may be packed from high-to-low (128 down to 1) or from low-to-high (1, 2, 4 ... 128) in
00033 a byte. The BE variant assumes that bit 0 (1) is the first bit in the stream, bit 1 (2) is
00034 the second, bit 3 (8) is the third and so forth. The LE variant assume that bit 7 (128) is the
00035 first, bit 6 (64) is the send and so forth. The current code always returns or takes data
00036 using the BE notation, so reading 8 bits from a LE source will return a byte with the bits
00037 in reverse order. There should probably be BE and LE variants of the Read and Write calls
00038 for each reader and writer. And there should be efficient single bit methods.
00039 */
00040 
00041 // =================================================================================================
00042 #pragma mark -
00043 #pragma mark * ZBitReaderBE
00044 
00046 
00047 class ZBitReaderBE
00048         {
00049 public:
00050         ZBitReaderBE();
00051         ~ZBitReaderBE();
00052 
00053         bool ReadBits(const ZStreamR& iStream, size_t iCountBits, uint32& oResult);
00054         void ReadBits(const void* iSource, size_t iCountBits,
00055                 uint32& oResult, size_t* oCountBytesConsumed);
00056 
00057 private:
00058         uint8 fBuffer;
00059         size_t fAvailBits;
00060         };
00061 
00062 // =================================================================================================
00063 #pragma mark -
00064 #pragma mark * ZBitReaderLE
00065 
00067 
00068 class ZBitReaderLE
00069         {
00070 public:
00071         ZBitReaderLE();
00072         ~ZBitReaderLE();
00073 
00074         bool ReadBits(const ZStreamR& iStream, size_t iCountBits, uint32& oResult);
00075         void ReadBits(const void* iSource, size_t iCountBits,
00076                 uint32& oResult, size_t* oCountBytesConsumed);
00077 
00078 private:
00079         uint8 fBuffer;
00080         size_t fAvailBits;
00081         };
00082 
00083 // =================================================================================================
00084 #pragma mark -
00085 #pragma mark * ZBitWriterBE
00086 
00088 
00089 class ZBitWriterBE
00090         {
00091 public:
00092         ZBitWriterBE();
00093         ~ZBitWriterBE();
00094 
00095         bool WriteBits(const ZStreamW& iStream, size_t iCountBits, uint32 iBits);
00096         void WriteBits(void* iDest, size_t iCountBits, uint32 iBits, size_t* oCountBytesWritten);
00097 
00098         void Finish(const ZStreamW& iStream);
00099         void Finish(void* iDest, size_t* oCountBytesWritten);
00100 
00101 private:
00102         uint8 fBuffer;
00103         size_t fAvailBits;
00104         };
00105 
00106 // =================================================================================================
00107 #pragma mark -
00108 #pragma mark * ZBitWriterLE
00109 
00111 
00112 class ZBitWriterLE
00113         {
00114 public:
00115         ZBitWriterLE();
00116         ~ZBitWriterLE();
00117 
00118         bool WriteBits(const ZStreamW& iStream, size_t iCountBits, uint32 iBits);
00119         void WriteBits(void* iDest, size_t iCountBits, uint32 iBits, size_t* oCountBytesWritten);
00120 
00121         void Finish(const ZStreamW& iStream);
00122         void Finish(void* iDest, size_t* oCountBytesWritten);
00123 
00124 private:
00125         uint8 fBuffer;
00126         size_t fAvailBits;
00127         };
00128 
00129 #endif // __ZBitStream__

Generated on Thu Jul 26 11:21:53 2007 for ZooLib by  doxygen 1.4.7