00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Jeff Lait 00008 * Side Effects Software Inc 00009 * 477 Richmond Street West 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 * 00014 * NAME: UT_BitStream.h ( UT Library, C++) 00015 * 00016 * COMMENTS: This allows you to read and write from binary 00017 * buffers on a per bit basis. It can handle up to 32bit 00018 * reads or writes. 00019 */ 00020 00021 #ifndef __UT_BitStream__ 00022 #define __UT_BitStream__ 00023 00024 #include "UT_API.h" 00025 00026 #include "UT_IntArray.h" 00027 00028 class UT_String; 00029 class UT_WorkBuffer; 00030 00031 class UT_API UT_BitStream 00032 { 00033 public: 00034 UT_BitStream(); 00035 UT_BitStream(int nbits); 00036 virtual ~UT_BitStream(); 00037 00038 // Delete & reset the buffer. 00039 void clear(); 00040 00041 int getLen() const; 00042 unsigned int *getBuf() const { return myBuf; } 00043 void setBuf(unsigned int *buf, int len); 00044 00045 int getBitLength() const { return myBitLength; } 00046 00047 // Note that the write/read head is shared, so it is not proper 00048 // to try and write and read to the same buffer. 00049 // You can write to the buffer, do a reset, and then read, however. 00050 // Just be careful you don't try to read past the end or you'll assert. 00051 void write(int val, int bits); 00052 int read(int bits); // Read, sign extended 00053 unsigned uread(int bits); // Read, zero extended 00054 00055 // Seek nbits into the stream. When: 00056 // whence[0] -- Seek from beginning of stream 00057 // whence[1] -- Seek from current location 00058 // whence[2] -- Seek from end of stream 00059 int seek(int nbits, int whence = 0); 00060 int tell() const; 00061 int rewind() { return seek(0, 0); } 00062 00063 // 00064 // All the save methods take a number of "words" to process. The number of 00065 // bits per word is defined by the method. For example, saveToHex() has a 00066 // word-size of 4 bits. This may be counter intuitive, but that's how it 00067 // works. 00068 // The offset parameter determines how many BITS into the stream to start 00069 // reading from when generating the string. 00070 // 00071 00072 // Load from a hexidecimal string or generate a hex string 00073 // (word size = 4 bits) 00074 void loadFromHex(const char *hex); 00075 void saveToHex(UT_String &result, int nwords=-1, 00076 int offset=0); 00077 void saveToHex(UT_WorkBuffer &result, int nwords=-1, 00078 int offset=0); 00079 00080 // Load or save a 6-bit encoded string 00081 // (word size = 6 bits) 00082 void loadFrom6Bit(const char *data); 00083 void saveTo6Bit(UT_String &result, int nwords=-1, 00084 int offset=0); 00085 void saveTo6Bit(UT_WorkBuffer &result, int nwords=-1, 00086 int offset=0); 00087 00088 // Load or save a 8-bit encoded string. Note that the binary data 00089 // may not be handled correctly since nulls can be inserted into the 00090 // UT_String (and are considered terminators). 00091 // (word size = 8 bits) 00092 void loadFromByte(const char *data); 00093 void saveToByte(UT_String &result, int nwords=-1, 00094 int offset=0); 00095 // Load or save a 8-bit encoded buffer. These are used for the binary 00096 // data so that it handles the nulls within the data. 00097 void loadFromByte(const char *data, int nwords); 00098 int saveToByte(unsigned char *data, int nwords, 00099 int offset=0); 00100 00101 // Initialize from a penta string. If failed, returns non-zero 00102 // Also initializes the errors array to the start and end character 00103 // of each penta which failed. 00104 // A penta is 5 character sequense delimitted by non-alnum chars. 00105 // Each one stores 20 bits of data and has a 5 bits of checksum data. 00106 int loadFromPenta(const char *penta, UT_IntArray &errors); 00107 // Write out the penta, only using the preferred characters. 00108 void saveToPenta(UT_String &result, int nwords=-1, 00109 int offset=0); 00110 00111 protected: 00112 // Load a single penta word. 00113 int loadPenta(const char *penta); 00114 int padTo(int nbits, int max, int off, int &savelen); 00115 void popPad(int savelen) 00116 { 00117 myBitLength = savelen; 00118 seek(0, 2); // Seek to end of file again 00119 } 00120 00121 // Local data. 00122 unsigned int *myBuf; // The buffer to read bits from 00123 int myHard; // Whether we own the buffer. 00124 int myBitOff, myWordOff; // The seek position 00125 int myBitLength; // Total written length 00126 int mySize; // Allocated size 00127 }; 00128 00129 #endif 00130
1.5.9