1 /***********************************************************
2 pm2tree.c -- tree for pmext2 decoding
3 ***********************************************************/
7 unsigned char root, *leftarr, *rightarr;
10 static unsigned char tree1left[32];
11 static unsigned char tree1right[32];
12 static struct tree tree1 = { 0, tree1left, tree1right };
14 static unsigned char tree2left[8];
15 static unsigned char tree2right[8];
16 static struct tree tree2 = { 0, tree2left, tree2right };
18 static void tree_setsingle(struct tree *t, unsigned char value);
19 static void tree_rebuild(struct tree *t, unsigned char bound,
20 unsigned char mindepth, unsigned char maxdepth,
21 unsigned char *table);
22 static void tree_setsingle(struct tree *t, unsigned char value);
24 static unsigned char tree1bound;
25 static unsigned char mindepth;
31 unsigned char table1[32];
33 tree1bound = getbits(5);
34 mindepth = getbits(3);
36 tree_setsingle(&tree1, tree1bound - 1);
39 for (i = 0; i < 32; i++)
42 for (i = 0; i < tree1bound; i++) {
44 table1[i] = (x == 0 ? 0 : x - 1 + mindepth);
46 tree_rebuild(&tree1, tree1bound, mindepth, 31, table1);
51 maketree2(int tree2bound) /* in use: 5 <= tree2bound <= 8 */
54 unsigned char table2[8];
58 /* tree1bound=1..8: character only, offset value is no needed. */
59 /* tree1bound=9: offset value is not encoded by Huffman tree */
62 if (tree1bound == 29 && mindepth == 0)
63 /* the length value is just 256 and offset value is just 0 */
66 /* need to build tree2 for offset value */
68 for (i = 0; i < 8; i++)
70 for (i = 0; i < tree2bound; i++)
71 table2[i] = getbits(3);
75 for (i = 0; i < tree2bound; i++) {
83 tree_setsingle(&tree2, index);
86 tree_rebuild(&tree2, tree2bound, 1, 7, table2);
88 // Note: count == 0 is possible!
89 // Excluding that possibility was a bug in version 1.
94 tree_get(struct tree *t)
99 i = (getbits(1) == 0 ? t->leftarr[i] : t->rightarr[i]);
107 return tree_get(&tree1);
113 return tree_get(&tree2);
117 tree_setsingle(struct tree *t, unsigned char value)
119 t->root = 128 | value;
123 tree_rebuild(struct tree *t,
125 unsigned char mindepth,
126 unsigned char maxdepth,
127 unsigned char *table)
129 unsigned char parentarr[32], d;
130 int i, curr, empty, n;
134 unsigned int count[32];
137 memset(count, 0, sizeof(count));
138 for (i = 0; i < bound; i++) {
139 if (table[i] > maxdepth) {
146 for (i = mindepth; i <= maxdepth; i++) {
147 int max_leaves = (1<<i);
148 if (count[i] > max_leaves) {
152 total += 1.0/max_leaves * count[i];
155 /* check the Kraft's inequality */
161 /* initialize tree */
163 for (i = 0; i < bound; i++) {
170 for (i = 0; i < mindepth - 1; i++) {
171 t->leftarr[i] = i + 1;
172 parentarr[i + 1] = i;
177 for (d = mindepth; d <= maxdepth; d++) {
178 for (i = 0; i < bound; i++) {
182 if (t->leftarr[curr] == 0) {
183 t->leftarr[curr] = i | 128;
187 t->rightarr[curr] = i | 128;
189 while (t->rightarr[curr] != 0) {
190 if (curr == 0) { /* root? -> done */
193 curr = parentarr[curr];
197 t->rightarr[curr] = empty;
199 parentarr[empty] = curr;
206 t->leftarr[curr] = empty;
210 if (t->leftarr[curr] == 0)
211 t->leftarr[curr] = empty;
213 t->rightarr[curr] = empty;
215 parentarr[empty] = curr;