1 /***********************************************************
2 pm2tree.c -- tree for pmext2 decoding
3 ***********************************************************/
7 static unsigned char tree1left[32];
8 static unsigned char tree1right[32];
9 struct tree tree1 = { 0, tree1left, tree1right };
10 static unsigned char table1[32];
12 static unsigned char tree2left[8];
13 static unsigned char tree2right[8];
14 struct tree tree2 = { 0, tree2left, tree2right };
15 static unsigned char table2[8];
17 static unsigned char tree1bound;
18 static unsigned char mindepth;
24 tree1bound = getbits(5);
25 mindepth = getbits(3);
27 tree_setsingle(&tree1, tree1bound - 1);
30 for (i = 0; i < 32; i++)
33 for (i = 0; i < tree1bound; i++) {
35 table1[i] = (x == 0 ? 0 : x - 1 + mindepth);
37 tree_rebuild(&tree1, tree1bound, mindepth, table1);
42 maketree2(int par_b) /* in use: 5 <= par_b <= 8 */
49 if (tree1bound == 29 && mindepth == 0)
52 for (i = 0; i < 8; i++)
54 for (i = 0; i < par_b; i++)
55 table2[i] = getbits(3);
59 for (i = 0; i < 8; i++) {
67 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.
79 tree_get(struct tree *t)
84 i = (getbits(1) == 0 ? t->leftarr[i] : t->rightarr[i]);
90 tree_setsingle(struct tree *t, unsigned char value)
92 t->root = 128 | value;
96 tree_rebuild(struct tree *t,
98 unsigned char mindepth,
101 unsigned char *parentarr, d;
102 int i, curr, empty, n;
104 parentarr = (unsigned char *)malloc(bound * sizeof(unsigned char));
106 for (i = 0; i < bound; i++) {
112 for (i = 0; i < mindepth - 1; i++) {
113 t->leftarr[i] = i + 1;
114 parentarr[i + 1] = i;
119 for (d = mindepth; TRUE; d++) {
120 for (i = 0; i < bound; i++) {
122 if (t->leftarr[curr] == 0)
123 t->leftarr[curr] = i | 128;
125 t->rightarr[curr] = i | 128;
127 while (t->rightarr[curr] != 0) {
128 if (curr == 0) { /* root? -> done */
132 curr = parentarr[curr];
135 t->rightarr[curr] = empty;
137 parentarr[empty] = curr;
144 t->leftarr[curr] = empty;
149 if (t->leftarr[curr] == 0)
150 t->leftarr[curr] = empty;
152 t->rightarr[curr] = empty;
154 parentarr[empty] = curr;