home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Workbench / Archivers / PPCxDMS.lha / PPCxDMS / source.lha / src / maketbl.c < prev    next >
C/C++ Source or Header  |  1998-02-28  |  2KB  |  94 lines

  1.  
  2. /*
  3.  *     xDMS  v1.1  -  Portable DMS archive unpacker  -  Public Domain
  4.  *     Written by     Andre R. de la Rocha  <adlroc@usa.net>
  5.  *
  6.  *     Makes decoding table for Heavy LZH decompression
  7.  *     From  UNIX LHA made by Masaru Oki
  8.  *
  9.  */
  10.  
  11.  
  12. #include "cdata.h"
  13. #include "maketbl.h"
  14.  
  15.  
  16. static SHORT c;
  17. static USHORT n, tblsiz, len, depth, maxdepth, avail;
  18. static USHORT codeword, bit, *tbl, TabErr;
  19. static UCHAR *blen;
  20.  
  21.  
  22. static USHORT mktbl(void);
  23.  
  24.  
  25.  
  26. USHORT make_table(USHORT nchar, UCHAR bitlen[],USHORT tablebits, USHORT table[]){
  27.     n = avail = nchar;
  28.     blen = bitlen;
  29.     tbl = table;
  30.     tblsiz = (USHORT) (1U << tablebits);
  31.     bit = (USHORT) (tblsiz / 2);
  32.     maxdepth = (USHORT)(tablebits + 1);
  33.     depth = len = 1;
  34.     c = -1;
  35.     codeword = 0;
  36.     TabErr = 0;
  37.     (void) mktbl();    /* left subtree */
  38.     if (TabErr) return TabErr;
  39.     (void) mktbl();    /* right subtree */
  40.     if (TabErr) return TabErr;
  41.     if (codeword != tblsiz) return 5;
  42.     return 0;
  43. }
  44.  
  45.  
  46.  
  47. static USHORT mktbl(void)
  48. {
  49.     USHORT i=0;
  50.  
  51.     if (TabErr) return 0;
  52.  
  53.     if (len == depth) {
  54.         while (++c < n)
  55.             if (blen[c] == len) {
  56.                 i = codeword;
  57.                 codeword += bit;
  58.                 if (codeword > tblsiz) {
  59.                     TabErr=1;
  60.                     return 0;
  61.                 }
  62.                 while (i < codeword) tbl[i++] = (USHORT)c;
  63.                 return (USHORT)c;
  64.             }
  65.         c = -1;
  66.         len++;
  67.         bit >>= 1;
  68.     }
  69.     depth++;
  70.     if (depth < maxdepth) {
  71.         (void) mktbl();
  72.         (void) mktbl();
  73.     } else if (depth > 32) {
  74.         TabErr = 2;
  75.         return 0;
  76.     } else {
  77.         if ((i = avail++) >= 2 * n - 1) {
  78.             TabErr = 3;
  79.             return 0;
  80.         }
  81.         left[i] = mktbl();
  82.         right[i] = mktbl();
  83.         if (codeword >= tblsiz) {
  84.             TabErr = 4;
  85.             return 0;
  86.         }
  87.         if (depth == maxdepth) tbl[codeword++] = i;
  88.     }
  89.     depth--;
  90.     return i;
  91. }
  92.  
  93.  
  94.