2 * xDMS v1.3 - Portable DMS archive unpacker - Public Domain
3 * Written by Andre Rodrigues de la Rocha <adlroc@usa.net>
5 * Makes decoding table for Heavy LZH decompression
6 * From UNIX LHA made by Masaru Oki
14 UWORD n
, tblsiz
, len
, depth
, maxdepth
, avail
;
15 UWORD codeword
, bit
, *tbl
, TabErr
;
19 static UWORD
mktbl (struct xdms_data
*xdms
, struct mktbl_data
*mt
);
21 UWORD
make_table (struct xdms_data
*xdms
, UWORD nchar
, UBYTE bitlen
[],UWORD tablebits
, UWORD table
[]) {
22 struct mktbl_data mt_s
, *mt
= &mt_s
;
23 memset(mt
, 0, sizeof(*mt
));
24 mt
->n
= mt
->avail
= nchar
;
27 mt
->tblsiz
= (UWORD
)(1U << tablebits
);
28 mt
->bit
= (UWORD
)(mt
->tblsiz
/ 2);
29 mt
->maxdepth
= (UWORD
)(tablebits
+ 1);
30 mt
->depth
= mt
->len
= 1;
34 mktbl(xdms
, mt
); /* left subtree */
35 if (mt
->TabErr
) return mt
->TabErr
;
36 mktbl(xdms
, mt
); /* right subtree */
37 if (mt
->TabErr
) return mt
->TabErr
;
38 if (mt
->codeword
!= mt
->tblsiz
) return 5;
42 static UWORD
mktbl (struct xdms_data
*xdms
, struct mktbl_data
*mt
) {
45 if (mt
->TabErr
) return 0;
47 if (mt
->len
== mt
->depth
) {
48 while (++mt
->c
< mt
->n
)
49 if (mt
->blen
[mt
->c
] == mt
->len
) {
51 mt
->codeword
+= mt
->bit
;
52 if (mt
->codeword
> mt
->tblsiz
) {
56 while (i
< mt
->codeword
) mt
->tbl
[i
++] = (UWORD
)mt
->c
;
64 if (mt
->depth
< mt
->maxdepth
) {
67 } else if (mt
->depth
> 32) {
71 if ((i
= mt
->avail
++) >= 2 * mt
->n
- 1) {
75 xdms
->u_heavy
.left
[i
] = mktbl(xdms
, mt
);
76 xdms
->u_heavy
.right
[i
] = mktbl(xdms
, mt
);
77 if (mt
->codeword
>= mt
->tblsiz
) {
81 if (mt
->depth
== mt
->maxdepth
) mt
->tbl
[mt
->codeword
++] = i
;