|
1 /* |
|
2 * skulltag::BitWriter class - Enables writing arbitrary bit lengths of data. |
|
3 * |
|
4 * Copyright 2009 Timothy Landers |
|
5 * email: code.vortexcortex@gmail.com |
|
6 * |
|
7 * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 * of this software and associated documentation files (the "Software"), to deal |
|
9 * in the Software without restriction, including without limitation the rights |
|
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 * copies of the Software, and to permit persons to whom the Software is |
|
12 * furnished to do so, subject to the following conditions: |
|
13 * |
|
14 * The above copyright notice and this permission notice shall be included in |
|
15 * all copies or substantial portions of the Software. |
|
16 * |
|
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 * THE SOFTWARE. |
|
24 */ |
|
25 |
|
26 #ifndef _BIT_WRITER_VERSION |
|
27 #define _BIT_WRITER_VERSION 1 |
|
28 |
|
29 /** Prevents naming convention problems via encapsulation. */ |
|
30 namespace skulltag { // scope limitation |
|
31 |
|
32 /** BitWriter - Allows writing of varying amounts of bits to a char buffer. <br> |
|
33 * Very usefull for outputting variable bit length encodings such as Huffman. */ |
|
34 |
|
35 class BitWriter { |
|
36 int bufferBits; /**< intermediary buffer of bits. */ |
|
37 int bitsLeft; /**< number of bits left in the buffer. */ |
|
38 unsigned char * currentByte; /**< position in memory the next char will be stored. */ |
|
39 int bytesAvailable; /**< amount of available space left in the output buffer in bytes. Excludes the contents of bufferBits. */ |
|
40 int bitsAvailable; /**< amount of available space left in the output buffer in bits. Includes the contents of bufferBits. */ |
|
41 int maximumBytes; /**< total amount of bytes that can be written to the output buffer. */ |
|
42 static int mask[]; /**< maps a number of bits to a bit mask containing as many bits. */ |
|
43 static int intSize; /**< number of chars in an int. */ |
|
44 public: |
|
45 |
|
46 /** Creates a new BitWriter. */ |
|
47 BitWriter(); |
|
48 |
|
49 /** Creates a new BitWriter. |
|
50 * @param output Destination that bits will be written to. |
|
51 * @param max Maximum number of chars to output. */ |
|
52 BitWriter( unsigned char * const output, int const &max ); |
|
53 |
|
54 /** Appends a char worth of bits to the buffer. |
|
55 * @param bits the bits to append. |
|
56 * @return true if successful, false if an error occurs. */ |
|
57 bool put( unsigned char const &bits ); |
|
58 |
|
59 /** Appends a short worth of bits to the buffer. |
|
60 * @param bits the bits to append. |
|
61 * @return true if successful, false otherwise. |
|
62 * @return true if successful, false if an error occurs. */ |
|
63 bool put( short const &bits ); |
|
64 |
|
65 /** Appends an int worth of bits to the buffer. |
|
66 * @param bits the bits to append. |
|
67 * @return true if successful, false if an error occurs. */ |
|
68 bool put( int const &bits ); |
|
69 |
|
70 /** Appends a specified number of bits from an int to the buffer. |
|
71 * @param bits the bits to append. <br> |
|
72 * The bits should be stored in the least significant portion of the int. |
|
73 * @param count the number of bits to append. |
|
74 * @return true if successful, false if an error occurs. */ |
|
75 bool put( int const &bits, int count ); |
|
76 |
|
77 /** Appends multiple chars from a buffer to this BitWriter. |
|
78 * @param inputBuffer pointer to char data |
|
79 * @param count number of chars to read |
|
80 * @return true if successful, false if an error occurs. */ |
|
81 bool put( unsigned char const * const inputBuffer, int count ); |
|
82 |
|
83 /** Sets the output buffer that bytes will be written to. |
|
84 * @param output Destination that bits will be written to. |
|
85 * @param max Maximum number of chars to output. |
|
86 * @return true if successful, false if an error occurs. */ |
|
87 bool outputBuffer( unsigned char * const output, int const &max ); |
|
88 |
|
89 /** Writes any full chars of data stored in this BitWriter to the output char buffer. |
|
90 * @return true if successful, false if an error occurs. */ |
|
91 bool flush(); |
|
92 |
|
93 /** Flushes this BitWriter then outputs any partial chars by padding them with zeros. <br> |
|
94 * After calling finish() all other calls to update the BitWriter will fail until a buffer is set via outputBuffer(). |
|
95 * @param bytesWritten out: the number of bytes written to the output buffer. |
|
96 * @param paddingBits out: the number of padding bits used in the final byte of output. |
|
97 * @return true if successful, false if an error occurs. */ |
|
98 bool finish( int &bytesWritten, int &paddingBits ); |
|
99 |
|
100 private: |
|
101 |
|
102 /** Initializes this BitWriter. */ |
|
103 void init(); |
|
104 |
|
105 }; |
|
106 |
|
107 } |
|
108 #endif |
|
109 |