home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / fileutil / scan.lha / src / lh5.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-21  |  4.7 KB  |  205 lines

  1. #include "lh5.h"
  2. #include <time.h>
  3. extern clock_t StrtTime,EndTime,TotTime; extern long NumTimes;
  4.  
  5. extern long WinSiz, HWinSiz, TotSiz, cursize;
  6. long BCount=0, TotRd=0, CurRd;
  7. char out_buf_adr[16384];
  8. extern unsigned short *BfrPtr;
  9. extern int NotInterruptedCall, ForceReturn;
  10. extern char *archive_name;
  11. /*
  12.        Note!!!! make sure to change following to LZBuf[ HWinSiz ]
  13.        and previous to out_buf_adr[ WinSiz ]
  14. */
  15. unsigned short LZBuf[8192];
  16.  
  17. #define NP (DICBIT + 1)
  18. #define NT (CODE_BIT + 3)
  19. #define PBIT 4  /* smallest integer such that (1U << PBIT) > NP */
  20. #define TBIT 5  /* smallest integer such that (1U << TBIT) > NT */
  21. #if NT > NP
  22. # define NPT NT
  23. #else
  24. # define NPT NP
  25. #endif
  26.  
  27. FILE *arcfile;
  28. ulong bitbuf;
  29.  
  30. extern unsigned int crccode;
  31.  
  32. int decoded;            /* for use in decode.c */
  33. int   bitcount;
  34.  
  35. uint jb;  /* remaining bytes to copy */
  36.  
  37. ushort left[2 * NC - 1], right[2 * NC - 1];
  38. uchar c_len[NC], pt_len[NPT];
  39. uint  blocksize;
  40. ushort c_table[4096], pt_table[256];
  41.  
  42. int read_pt_len(nn, nbit, i_special)
  43. int nn;
  44. int nbit;
  45. int i_special;
  46. {
  47.    int i, c, n;
  48.    uint mask;
  49.  
  50.    n = getbits(nbit);
  51.    if (n == 0) {
  52.       c = getbits(nbit);
  53.       for (i = 0; i < nn; i++) pt_len[i] = 0;
  54.       for (i = 0; i < 256; i++) pt_table[i] = c;
  55.    } else {
  56.       i = 0;
  57.       while (i < n) {
  58.          c = bitbuf >> (BITBUFSIZ - 3);
  59.          if (c == 7) {
  60.             mask = (unsigned) 1 << (BITBUFSIZ - 1 - 3);
  61.             while (mask & bitbuf) {  mask >>= 1;  c++; }
  62.          }
  63.          fillbuf((c < 7) ? 3 : c - 3);
  64.          pt_len[i++] = c;
  65.          if (i == i_special) {
  66.             c = getbits(2);
  67.             while (--c >= 0) pt_len[i++] = 0;
  68.          }
  69.       }
  70.       while (i < nn) pt_len[i++] = 0;
  71.       make_table(nn, pt_len, 8, pt_table);
  72.    }
  73. }
  74.  
  75. int read_c_len()
  76. {
  77.    int i, c, n;
  78.    uint mask;
  79.  
  80.    n = getbits(CBIT);
  81.    if (n == 0) {
  82.       c = getbits(CBIT);
  83.       for (i = 0; i < NC; i++) c_len[i] = 0;
  84.       for (i = 0; i < 4096; i++) c_table[i] = c;
  85.    } else {
  86.       i = 0;
  87.       while (i < n) {
  88.          c = pt_table[bitbuf >> (BITBUFSIZ - 8)];
  89.          if (c >= NT) {
  90.             mask = (unsigned) 1 << (BITBUFSIZ - 1 - 8);
  91.             do {
  92.                if (bitbuf & mask) c = right[c];
  93.                else               c = left [c];
  94.                mask >>= 1;
  95.             } while (c >= NT);
  96.          }
  97.          fillbuf((int) pt_len[c]);
  98.          if (c <= 2) {
  99.             if      (c == 0) c = 1;
  100.             else if (c == 1) c = getbits(4) + 3;
  101.             else             c = getbits(CBIT) + 20;
  102.             while (--c >= 0) c_len[i++] = 0;
  103.          } else c_len[i++] = c - 2;
  104.       }
  105.       while (i < NC) c_len[i++] = 0;
  106.       make_table(NC, c_len, 12, c_table);
  107.    }
  108. }
  109.  
  110. int huf_decode_start()
  111. {
  112.    init_getbits();  blocksize = 1;
  113. }
  114.  
  115. BufRead()
  116. {
  117.         long n;
  118.         if( TotRd < WinSiz ) {  CurRd = TotRd;  }
  119.         else { TotRd -= WinSiz; CurRd = WinSiz; }
  120.         n = fread( LZBuf, 1, CurRd, arcfile);
  121.         if( n < 0 ) fatal_error(archive_name);
  122.         if( CurRd != WinSiz && (CurRd & 1)) LZBuf[ CurRd>>1 ] &= 0xff00;
  123.         BCount = 0;
  124. }
  125.  
  126. int fillbuf(n)  /* Shift bitbuf n bits left, read n bits */
  127. int n;
  128. {
  129.         bitbuf <<= n;
  130.         bitcount += n;
  131.         while ( bitcount >= 16) {
  132.            bitcount -= 16;
  133.            if( BCount == HWinSiz ) BufRead();
  134.            bitbuf = (LZBuf[ BCount++] | ((bitbuf >> bitcount) & 0xffff0000)) << bitcount;
  135.         }
  136. }
  137.  
  138. uint getbits(n)
  139. int n;
  140. {
  141.         uint x;
  142.  
  143.         x = bitbuf >> (BITBUFSIZ - n);  fillbuf(n);
  144.         return x;
  145. }
  146.  
  147. int fwrite_crc(p, n)
  148. uchar *p;
  149. int n;
  150. {
  151.         addbfcrc((char *) p, (unsigned) n);
  152. }
  153.  
  154. int init_getbits()
  155. {
  156.         bitbuf = 0; bitcount = 32;
  157.         fillbuf(0);
  158. }
  159.  
  160. /* must call this before decoding each file */
  161. int decode_start()
  162. {
  163.    huf_decode_start();
  164.    jb = 0;
  165.    decoded = 0;
  166. }
  167.  
  168. /*
  169. decode_lh5 decodes its input and sends it to output.
  170. Should return error status or byte count, but currently
  171. returns 0.
  172. */
  173.  
  174. int decode_lh5(infile, original_size, name)
  175. FILE *infile;
  176. long original_size;
  177. char *name;
  178. {
  179.    long n;
  180.    extern int decoded;
  181.    arcfile = infile;             /* stream to be decoded */
  182.  
  183.    if( NotInterruptedCall ) { /* on 1st call with new internal file, reinit */
  184.       gentab();
  185.       crccode = 0;
  186.       decode_start();
  187.    }
  188.    if (!decoded && cursize > 0) {
  189.       if( cursize > WinSiz ) {
  190.          n = decodeit( (uint) WinSiz, (uchar *)out_buf_adr);
  191.       } else {
  192.          n = decodeit( (uint) cursize, (uchar *)out_buf_adr);
  193.       }
  194.       /* n = count of chars decoded */
  195.       fwrite_crc(out_buf_adr, n);
  196.       bmov( out_buf_adr, BfrPtr, n );
  197. /*      memcpy( BfrPtr, out_buf_adr, n ); */
  198.       cursize -= n;
  199.       TotSiz = n;
  200.    }
  201.    if( cursize > 0 ) ForceReturn = 1;
  202.    NotInterruptedCall = 1; /* this will be set to 0 in main if ForceReturn=1 */
  203.    return (int)crccode;
  204. }
  205.