huffman/huffman.cpp

Sat, 23 Jul 2016 12:14:20 +0300

author
Teemu Piippo <teemu@compsta2.com>
date
Sat, 23 Jul 2016 12:14:20 +0300
changeset 161
1c483b54ddcb
parent 76
6de6d9a64ebd
permissions
-rw-r--r--

Added versions of min() and max() that find the smallest and largest values in an iterable.

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 * Replacement for older Skulltag Launcher Protocol's huffman.cpp
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 // required for atexit()
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 #include <stdlib.h>
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 #include <stdio.h>
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 "huffman.h"
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 #include "huffcodec.h"
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 // #include "i_system.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 using namespace skulltag;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 // Global Variables
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 /** Reference to the HuffmanCodec Object that will perform the encoding and decoding. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 static HuffmanCodec * __codec = NULL;
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 // Function Implementation
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 /** Creates and intitializes a HuffmanCodec Object. <br>
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 * Also arranges for HUFFMAN_Destruct() to be called upon termination. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 void HUFFMAN_Construct(){
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 // The exact structure description of a Huffman tree
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 static unsigned char const compatible_huffman_tree[] = {
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 0, 0, 0, 1,128, 0, 0, 0, 3, 38, 34, 2, 1, 80, 3,110,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 144, 67, 0, 2, 1, 74, 3,243,142, 37, 2, 3,124, 58,182, 0,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 0, 1, 36, 0, 3,221,131, 3,245,163, 1, 35, 3,113, 85, 0,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 1, 41, 1, 77, 3,199,130, 0, 1,206, 3,185,153, 3, 70,118,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 0, 3, 3, 5, 0, 0, 1, 24, 0, 2, 3,198,190, 63, 2, 3,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 139,186, 75, 0, 1, 44, 2, 3,240,218, 56, 3, 40, 39, 0, 0,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 2, 2, 3,244,247, 81, 65, 0, 3, 9,125, 3, 68, 60, 0, 0,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 1, 25, 3,191,138, 3, 86, 17, 0, 1, 23, 3,220,178, 2, 3,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 165,194, 14, 1, 0, 2, 2, 0, 0, 2, 1,208, 3,150,157,181,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 1,222, 2, 3,216,230,211, 0, 2, 2, 3,252,141, 10, 42, 0,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 2, 3,134,135,104, 1,103, 3,187,225, 95, 32, 0, 0, 0, 0,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 0, 0, 1, 57, 1, 61, 3,183,237, 0, 0, 3,233,234, 3,246,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 203, 2, 3,250,147, 79, 1,129, 0, 1, 7, 3,143,136, 1, 20,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 3,179,148, 0, 0, 0, 3, 28,106, 3,101, 87, 1, 66, 0, 3,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 180,219, 3,227,241, 0, 1, 26, 1,251, 3,229,214, 3, 54, 69,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 0, 0, 0, 0, 0, 3,231,212, 3,156,176, 3, 93, 83, 0, 3,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 96,253, 3, 30, 13, 0, 0, 2, 3,175,254, 94, 3,159, 27, 2,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 1, 8, 3,204,226, 78, 0, 0, 0, 3,107, 88, 1, 31, 3,137,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 169, 2, 2, 3,215,145, 6, 4, 1,127, 0, 1, 99, 3,209,217,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 0, 3,213,238, 3,177,170, 1,132, 0, 0, 0, 2, 3, 22, 12,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 114, 2, 2, 3,158,197, 97, 45, 0, 1, 46, 1,112, 3,174,249,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 0, 3,224,102, 2, 3,171,151,193, 0, 0, 0, 3, 15, 16, 3,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 2,168, 1, 49, 3, 91,146, 0, 1, 48, 3,173, 29, 0, 3, 19,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 126, 3, 92,242, 0, 0, 0, 0, 0, 0, 3,205,192, 2, 3,235,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 149,255, 2, 3,223,184,248, 0, 0, 3,108,236, 3,111, 90, 2,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 3,117,115, 71, 0, 0, 3, 11, 50, 0, 3,188,119, 1,122, 3,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 167,162, 1,160, 1,133, 3,123, 21, 0, 0, 2, 1, 59, 2, 3,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 155,154, 98, 43, 0, 3, 76, 51, 2, 3,201,116, 72, 2, 0, 2,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 3,109,100,121, 2, 3,195,232, 18, 1, 0, 2, 0, 1,164, 2,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 3,120,189, 73, 0, 1,196, 3,239,210, 3, 64, 62, 89, 0, 0,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 1, 33, 2, 3,228,161, 55, 2, 3, 84,152, 47, 0, 0, 2, 3,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 207,172,140, 3, 82,166, 0, 3, 53,105, 1, 52, 3,202,200
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 };
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 // create a HuffmanCodec that is compatible with the previous implementation.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 __codec = new HuffmanCodec( compatible_huffman_tree, sizeof compatible_huffman_tree );
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 // set up the HuffmanCodec to perform in a backwards compatible fashion.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 __codec->reversedBytes( true );
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 __codec->allowExpansion( false );
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 // request that the destruct function be called upon exit.
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 // atterm( HUFFMAN_Destruct );
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 atexit( HUFFMAN_Destruct );
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 }
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 /** Releases resources allocated by the HuffmanCodec. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 void HUFFMAN_Destruct(){
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 delete __codec;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 __codec = NULL;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 }
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 /** Applies Huffman encoding to a block of data. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 void HUFFMAN_Encode(
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 /** in: Pointer to start of data that is to be encoded. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 unsigned char const * const inputBuffer,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 /** out: Pointer to destination buffer where encoded data will be stored. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 unsigned char * const outputBuffer,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 /** in: Number of chars to read from inputBuffer. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 int const &inputBufferSize,
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 /**< in+out: Max chars to write into outputBuffer. <br>
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 * Upon return holds the number of chars stored or 0 if an error occurs. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 int * outputBufferSize
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 ){
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 int bytesWritten = __codec->encode( inputBuffer, outputBuffer, inputBufferSize, *outputBufferSize );
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 // expansion occured -- provide backwards compatibility
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 if ( bytesWritten < 0 ){
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 // check buffer sizes
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 if ( *outputBufferSize < (inputBufferSize + 1) ){
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 // outputBuffer too small, return "no bytes written"
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 *outputBufferSize = 0;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 return;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 }
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 // perform the unencoded copy
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 for ( int i = 0; i < inputBufferSize; i++ ) outputBuffer[i+1] = inputBuffer[i];
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 // supply the "unencoded" signal and bytesWritten
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 outputBuffer[0] = 0xff;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 *outputBufferSize = inputBufferSize + 1;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 } else {
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 // assign the bytesWritten return value
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 *outputBufferSize = bytesWritten;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 }
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 } // end function HUFFMAN_Encode
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 /** Decodes a block of data that is Huffman encoded. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 void HUFFMAN_Decode(
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 unsigned char const * const inputBuffer, /**< in: Pointer to start of data that is to be decoded. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 unsigned char * const outputBuffer, /**< out: Pointer to destination buffer where decoded data will be stored. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 int const &inputBufferSize, /**< in: Number of chars to read from inputBuffer. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 int *outputBufferSize /**< in+out: Max chars to write into outputBuffer. Upon return holds the number of chars stored or 0 if an error occurs. */
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 ){
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 // check for "unencoded" signal & provide backwards compatibility
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 if ((inputBufferSize > 0) && ((inputBuffer[0]&0xff) == 0xff)){
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 // check buffer sizes
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 if ( *outputBufferSize < (inputBufferSize - 1) ){
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 // outputBuffer too small, return "no bytes written"
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 *outputBufferSize = 0;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 return;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 }
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 // perform the unencoded copy
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151 for ( int i = 1; i < inputBufferSize; i++ ) outputBuffer[i-1] = inputBuffer[i];
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 // supply the bytesWritten
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 *outputBufferSize = inputBufferSize - 1;
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 } else {
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 // decode the data
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 *outputBufferSize = __codec->decode( inputBuffer, outputBuffer, inputBufferSize, *outputBufferSize );
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 }
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 } // end function HUFFMAN_Decode

mercurial