huffman/huffcodec.h

Wed, 20 Jul 2016 14:48:47 +0300

author
Teemu Piippo <teemu@compsta2.com>
date
Wed, 20 Jul 2016 14:48:47 +0300
changeset 137
485cb6d6b98c
parent 76
6de6d9a64ebd
permissions
-rw-r--r--

Made Range not include the 'max' parameter, replaced a lot of for()-loops with C++11-style range-for-loops

8
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * skulltag::HuffmanCodec class - Huffman encoder and decoder.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 *
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 * Copyright 2009 Timothy Landers
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * email: code.vortexcortex@gmail.com
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 *
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * of this software and associated documentation files (the "Software"), to deal
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 * in the Software without restriction, including without limitation the rights
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * copies of the Software, and to permit persons to whom the Software is
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * furnished to do so, subject to the following conditions:
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 *
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 * The above copyright notice and this permission notice shall be included in
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * all copies or substantial portions of the Software.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 *
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 * THE SOFTWARE.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 #ifndef _HUFFMAN_CODEC_VERSION
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 #define _HUFFMAN_CODEC_VERSION 1
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 #define _HUFFMAN_CODEC_REV 0
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 #include "codec.h"
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 #include "bitwriter.h"
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 #include "bitreader.h"
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 /** Prevents naming convention problems via encapsulation. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 namespace skulltag {
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 /** HuffmanCodec class - Encodes and Decodes data using a Huffman tree. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 class HuffmanCodec : public Codec {
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 /** top level node of the Huffman tree used for decoding. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 HuffmanNode * root;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 /** table of Huffman codes and bit lengths used for encoding. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 HuffmanNode ** codeTable;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 /** intermediary destination of huffman codes. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 BitWriter * writer;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 /** When true this HuffmanCodec reverses its bytes after encoding and before decoding to
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 * provide compatibility with the backwards bit ordering of the original ST Huffman Encoding.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 * Default value is "false" (do not reverse bits). */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 bool reverseBits;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 /** When false this HuffmanCodec return -1 instead of expanding data during encoding.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 * Default value is "true" (allow data expansion). */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 bool expandable;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 /** Determines if this HuffmanCodec owns its Huffman tree nodes. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 bool huffResourceOwner;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 /** Reverses the order of bits in a byte.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 * EG: The statement <code>reverseMap[0xAF] == 0xF5</code> is <code>true</code>. <br>
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 * The index <code>10101111</code> stores the reverse value: <code>11110101</code>. <br>
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 * Note: One array lookup is much faster than Eight bit manipulating loop iterations. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 static unsigned char const reverseMap[];
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 /** Number of bits the shortest huffman code in the tree has. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 int shortestCode;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 public:
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 /** Creates a new HuffmanCodec from the Huffman tree data.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 * @param treeData pointer to a buffer containing the Huffman tree structure definition.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 * @param dataLength length in bytes of the Huffman tree structure data. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 HuffmanCodec( unsigned char const * const treeData, int dataLength );
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 /** Creates a new HuffmanCodec that uses the specified Huffman resources.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 * @param treeRootNode The root node of a valid huffman tree.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 * @param leafCodeTable A code lookup table where references to HuffmanNodes are stored with their array index equal to their value.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 * Note: The tree nodes will not be released upon destruction of this HuffmanCodec. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 HuffmanCodec(
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 HuffmanNode * treeRootNode,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 HuffmanNode ** leafCodeTable
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 );
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 /** Frees resources used internally by this HuffmanCodec. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 virtual ~HuffmanCodec();
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 /** Decodes data read from an input buffer and stores the result in the output buffer.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 * @return number of bytes stored in the output buffer or -1 if an error occurs while encoding. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 virtual int encode(
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 unsigned char const * const input, /**< in: pointer to the first byte to encode. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 unsigned char * const output, /**< out: pointer to an output buffer to store data. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 int const &inLength, /**< in: number of bytes of input buffer to encoded. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 int const &outLength /**< in: maximum length of data to output. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 ) const;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 /** Decodes data read from an input buffer and stores the result in the output buffer.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 * @return number of bytes stored in the output buffer or -1 if an error occurs while decoding. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 virtual int decode(
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 unsigned char const * const input, /**< in: pointer to data that needs decoding. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 unsigned char * const output, /**< out: pointer to output buffer to store decoded data. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 int const &inLength, /**< in: number of bytes of input buffer to read. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 int const &outLength /**< in: maximum length of data to output. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 );
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 /** Enables or Disables backwards bit ordering of bytes.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 * @param backwards "true" enables reversed bit order bytes, "false" uses standard byte bit ordering. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 void reversedBytes( bool backwards );
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 /** Check the state of backwards bit ordering for bytes.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 * @return true: bits within bytes are reversed. false: bits within bytes are normal. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 bool reversedBytes();
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 /** Enable or Disable data expansion during encoding.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 * @param expandingAllowed "true" allows encoding to expand data. "false" causes failure upon expansion. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 void allowExpansion( bool expandable );
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 /** Check the state of data expandability.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 * @return true: data expansion is allowed. false: data is not allowed to expand. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 bool allowExpansion();
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 /** Sets the ownership of this HuffmanCodec's resources.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 * @param ownsResources When false the tree will not be released upon destruction of this HuffmanCodec.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 * When true deleting this HuffmanCodec will cause the Huffman tree to be released. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 void huffmanResourceOwner( bool ownsResources );
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 /** Checks the ownership state of this HuffmanCodec's resources.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 * @return ownsResources When false the tree will not be released upon destruction of this HuffmanCodec.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 * When true deleting this HuffmanCodec will cause the Huffman tree to be released. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 bool huffmanResourceOwner();
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 /** Deletes all sub nodes of a HuffmanNode by traversing and deleting its child nodes.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 * @param treeNode pointer to a HuffmanNode whos children will be deleted. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 static void deleteTree( HuffmanNode * treeNode );
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 /** Recursively builds a Huffman Tree. <br>
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 * The initial root node should have the following field values: <br>
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 * <pre>
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 * bitCount : 0
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 * code : 0
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 * value : -1
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 * branch : 0 (NULL)
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 * </pre>
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 * @param node in/out: branch node of the Huffman Tree.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 * @param treeData in: char array containing the Huffman Tree's byte representation.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 * @param index in: Current array element to read the next tree node from.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 * @param dataLength in: Length of treeData
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149 * @param codeTable in/out: array of pointers to HuffmanNode structs.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 * @param tableLength in: maximum index allowed in the codeTable.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151 * @return the next index to read from or -1 if an error occurs.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 * */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153 int buildTree(
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 HuffmanNode * node,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 unsigned char const * const treeData,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 int index,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 int dataLength,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 HuffmanNode ** const &codeTable,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 int tableLength
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160 );
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 /** Decreases a codeLength to the shortest Huffman code bit length found in the node or any of its children. <br>
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163 * Set to Zero before calling to determine minimum code bit length.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164 * @param node in: The node to begin searching at.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165 * @param codeLength out: Variable to hold the longest code bit length found. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166 static void minCodeLength( HuffmanNode const * const node, int &codeLength );
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168 /** Increases a codeLength up to the longest Huffman code bit length found in the node or any of its children. <br>
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169 * Set to Zero before calling to determine maximum code bit length.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 * @param node in: The node to begin searching at.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 * @param codeLength out: Variable to hold the longest code bit length found. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172 static void maxCodeLength( HuffmanNode const * const node, int &codeLength );
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 private:
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 /** Perform initialization procedures common to all constructors. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177 void init();
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 }; // end class Huffman Codec.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180 } // end namespace skulltag
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 #endif

mercurial