1 /***********************************************************
2 pm2tree.c -- tree for pmext2 decoding
3 ***********************************************************/
5 Copyright (c) 1999 Maarten ter Huurne
7 Permission is hereby granted, free of charge, to any person
8 obtaining a copy of this software and associated documentation files
9 (the "Software"), to deal in the Software without restriction,
10 including without limitation the rights to use, copy, modify, merge,
11 publish, distribute, sublicense, and/or sell copies of the Software,
12 and to permit persons to whom the Software is furnished to do so,
13 subject to the following conditions:
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 unsigned char root, *leftarr, *rightarr;
33 static unsigned char tree1left[32];
34 static unsigned char tree1right[32];
35 static struct tree tree1 = { 0, tree1left, tree1right };
37 static unsigned char tree2left[8];
38 static unsigned char tree2right[8];
39 static struct tree tree2 = { 0, tree2left, tree2right };
41 static void tree_setsingle(struct tree *t, unsigned char value);
42 static void tree_rebuild(struct tree *t, unsigned char bound,
43 unsigned char mindepth, unsigned char maxdepth,
44 unsigned char *table);
45 static void tree_setsingle(struct tree *t, unsigned char value);
47 static unsigned char tree1bound;
48 static unsigned char mindepth;
54 unsigned char table1[32];
56 tree1bound = getbits(5);
57 mindepth = getbits(3);
59 tree_setsingle(&tree1, tree1bound - 1);
62 for (i = 0; i < 32; i++)
65 for (i = 0; i < tree1bound; i++) {
67 table1[i] = (x == 0 ? 0 : x - 1 + mindepth);
69 tree_rebuild(&tree1, tree1bound, mindepth, 31, table1);
74 maketree2(int tree2bound) /* in use: 5 <= tree2bound <= 8 */
77 unsigned char table2[8];
81 /* tree1bound=1..8: character only, offset value is no needed. */
82 /* tree1bound=9: offset value is not encoded by Huffman tree */
85 if (tree1bound == 29 && mindepth == 0)
86 /* the length value is just 256 and offset value is just 0 */
89 /* need to build tree2 for offset value */
91 for (i = 0; i < 8; i++)
93 for (i = 0; i < tree2bound; i++)
94 table2[i] = getbits(3);
98 for (i = 0; i < tree2bound; i++) {
106 tree_setsingle(&tree2, index);
108 else if (count > 1) {
109 tree_rebuild(&tree2, tree2bound, 1, 7, table2);
111 // Note: count == 0 is possible!
112 // Excluding that possibility was a bug in version 1.
117 tree_get(struct tree *t)
122 i = (getbits(1) == 0 ? t->leftarr[i] : t->rightarr[i]);
130 return tree_get(&tree1);
136 return tree_get(&tree2);
140 tree_setsingle(struct tree *t, unsigned char value)
142 t->root = 128 | value;
146 tree_rebuild(struct tree *t,
148 unsigned char mindepth,
149 unsigned char maxdepth,
150 unsigned char *table)
152 unsigned char parentarr[32], d;
153 int i, curr, empty, n;
157 unsigned int count[32];
160 memset(count, 0, sizeof(count));
161 for (i = 0; i < bound; i++) {
162 if (table[i] > maxdepth) {
169 for (i = mindepth; i <= maxdepth; i++) {
170 int max_leaves = (1<<i);
171 if (count[i] > max_leaves) {
175 total += 1.0/max_leaves * count[i];
178 /* check the Kraft's inequality */
184 /* initialize tree */
186 for (i = 0; i < bound; i++) {
193 for (i = 0; i < mindepth - 1; i++) {
194 t->leftarr[i] = i + 1;
195 parentarr[i + 1] = i;
200 for (d = mindepth; d <= maxdepth; d++) {
201 for (i = 0; i < bound; i++) {
205 if (t->leftarr[curr] == 0) {
206 t->leftarr[curr] = i | 128;
210 t->rightarr[curr] = i | 128;
212 while (t->rightarr[curr] != 0) {
213 if (curr == 0) { /* root? -> done */
216 curr = parentarr[curr];
220 t->rightarr[curr] = empty;
222 parentarr[empty] = curr;
229 t->leftarr[curr] = empty;
233 if (t->leftarr[curr] == 0)
234 t->leftarr[curr] = empty;
236 t->rightarr[curr] = empty;
238 parentarr[empty] = curr;