1 /***********************************************************
2 pm2tree.c -- tree for pmext2 decoding
3 ***********************************************************/
11 static unsigned char tree1left[32];
12 static unsigned char tree1right[32];
13 struct tree tree1 = { 0, tree1left, tree1right };
14 static unsigned char table1[32];
16 static unsigned char tree2left[8];
17 static unsigned char tree2right[8];
18 struct tree tree2 = { 0, tree2left, tree2right };
19 static unsigned char table2[8];
21 static unsigned char tree1bound;
22 static unsigned char mindepth;
27 tree1bound = getbits(5);
28 mindepth = getbits(3);
31 tree_setsingle(&tree1, tree1bound - 1);
35 for (i = 0; i<32; i++) table1[i] = 0;
37 for (i = 0; i < tree1bound; i++) {
39 table1[i] = ( x == 0 ? 0 : x - 1 + mindepth );
41 tree_rebuild(&tree1, tree1bound, mindepth, table1);
45 void maketree2(int par_b) /* in use: 5 <= par_b <= 8 */
48 if (tree1bound < 10) return;
49 if (tree1bound == 29 && mindepth == 0) return;
51 for (i = 0; i < 8; i++) table2[i] = 0;
52 for (i = 0; i < par_b; i++) table2[i] = getbits(3);
55 for (i = 0; i < 8; i++)
66 tree_setsingle(&tree2, index);
71 tree_rebuild(&tree2, 8, mindepth, table2);
73 // Note: count == 0 is possible!
74 // Excluding that possibility was a bug in version 1.
78 int tree_get(struct tree *t)
84 i = ( getbits(1) == 0 ? t->leftarr[i] : t->rightarr[i] );
89 void tree_setsingle(struct tree *t, unsigned char value)
91 t->root = 128 | value;
94 void tree_rebuild(t, bound, mindepth, table)
97 unsigned char mindepth;
100 unsigned char *parentarr, d;
101 int i, curr, empty, n;
102 parentarr = (unsigned char *)malloc(bound * sizeof(unsigned char));
104 for (i = 0; i < bound; i++)
111 for (i = 0; i < mindepth - 1; i++)
113 t->leftarr[i] = i + 1;
119 for (d = mindepth; TRUE; d++)
121 for (i = 0; i < bound; i++)
125 if (t->leftarr[curr] == 0) t->leftarr[curr] = i | 128;
128 t->rightarr[curr] = i | 128;
130 while (t->rightarr[curr] != 0)
132 if (curr == 0) /* root? -> done */
137 curr = parentarr[curr];
140 t->rightarr[curr] = empty;
143 parentarr[empty] = curr;
147 n--; if (n == 0) break;
148 t->leftarr[curr] = empty;
153 if (t->leftarr[curr] == 0) t->leftarr[curr] = empty; else t->rightarr[curr] = empty;
155 parentarr[empty] = curr;