HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_BitStream.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_BitStream.h ( UT Library, C++)
7  *
8  * COMMENTS: This allows you to read and write from binary
9  * buffers on a per bit basis. It can handle up to 32bit
10  * reads or writes.
11  */
12 
13 #ifndef __UT_BitStream__
14 #define __UT_BitStream__
15 
16 #include "UT_API.h"
17 
18 #include "UT_IntArray.h"
19 
20 class UT_String;
21 class UT_WorkBuffer;
22 
24 {
25 public:
26  UT_BitStream();
27  UT_BitStream(int nbits);
28  virtual ~UT_BitStream();
29 
30  // Delete & reset the buffer.
31  void clear();
32 
33  int getLen() const;
34  unsigned int *getBuf() const { return myBuf; }
35  void setBuf(unsigned int *buf, int len);
36 
37  int getBitLength() const { return myBitLength; }
38 
39  // Note that the write/read head is shared, so it is not proper
40  // to try and write and read to the same buffer.
41  // You can write to the buffer, do a reset, and then read, however.
42  // Just be careful you don't try to read past the end or you'll assert.
43  void write(int val, int bits);
44  int read(int bits); // Read, sign extended
45  unsigned uread(int bits); // Read, zero extended
46 
47  // Seek nbits into the stream. When:
48  // whence[0] -- Seek from beginning of stream
49  // whence[1] -- Seek from current location
50  // whence[2] -- Seek from end of stream
51  int seek(int nbits, int whence = 0);
52  int tell() const;
53  int rewind() { return seek(0, 0); }
54 
55  //
56  // All the save methods take a number of "words" to process. The number of
57  // bits per word is defined by the method. For example, saveToHex() has a
58  // word-size of 4 bits. This may be counter intuitive, but that's how it
59  // works.
60  // The offset parameter determines how many BITS into the stream to start
61  // reading from when generating the string.
62  //
63 
64  // Load from a hexidecimal string or generate a hex string
65  // (word size = 4 bits)
66  void loadFromHex(const char *hex);
67  void saveToHex(UT_String &result, int nwords=-1,
68  int offset=0);
69  void saveToHex(UT_WorkBuffer &result, int nwords=-1,
70  int offset=0);
71 
72  // Load or save a 6-bit encoded string
73  // (word size = 6 bits)
74  void loadFrom6Bit(const char *data);
75  void saveTo6Bit(UT_String &result, int nwords=-1,
76  int offset=0);
77  void saveTo6Bit(UT_WorkBuffer &result, int nwords=-1,
78  int offset=0);
79 
80  // Load or save a 8-bit encoded string. Note that the binary data
81  // may not be handled correctly since nulls can be inserted into the
82  // UT_String (and are considered terminators).
83  // (word size = 8 bits)
84  void loadFromByte(const char *data);
85  void saveToByte(UT_String &result, int nwords=-1,
86  int offset=0);
87  // Load or save a 8-bit encoded buffer. These are used for the binary
88  // data so that it handles the nulls within the data.
89  void loadFromByte(const char *data, int nwords);
90  int saveToByte(unsigned char *data, int nwords,
91  int offset=0);
92 
93  // Initialize from a penta string. If failed, returns non-zero
94  // Also initializes the errors array to the start and end character
95  // of each penta which failed.
96  // A penta is 5 character sequense delimitted by non-alnum chars.
97  // Each one stores 20 bits of data and has a 5 bits of checksum data.
98  int loadFromPenta(const char *penta, UT_IntArray &errors);
99  // Write out the penta, only using the preferred characters.
100  void saveToPenta(UT_String &result, int nwords=-1,
101  int offset=0);
102 
103 protected:
104  // Load a single penta word.
105  int loadPenta(const char *penta);
106  int padTo(int nbits, int max, int off, int &savelen);
107  void popPad(int savelen)
108  {
109  myBitLength = savelen;
110  seek(0, 2); // Seek to end of file again
111  }
112 
113  // Local data.
114  unsigned int *myBuf; // The buffer to read bits from
115  int myHard; // Whether we own the buffer.
116  int myBitOff, myWordOff; // The seek position
117  int myBitLength; // Total written length
118  int mySize; // Allocated size
119 };
120 
121 #endif
122 
int getBitLength() const
Definition: UT_BitStream.h:37
#define UT_API
Definition: UT_API.h:13
unsigned int * getBuf() const
Definition: UT_BitStream.h:34
void read(T &in, bool &v)
Definition: ImfXdr.h:611
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:133
GLintptr offset
Definition: glcorearb.h:664
GLboolean * data
Definition: glcorearb.h:130
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2539
unsigned int * myBuf
Definition: UT_BitStream.h:114
GLuint GLfloat * val
Definition: glcorearb.h:1607
void popPad(int savelen)
Definition: UT_BitStream.h:107
void write(T &out, bool v)
Definition: ImfXdr.h:332