home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / AR002.LZH / DECODE.C < prev    next >
C/C++ Source or Header  |  1990-08-14  |  1KB  |  48 lines

  1. /***********************************************************
  2.     decode.c
  3. ***********************************************************/
  4. #include "ar.h"
  5.  
  6. static int j;  /* remaining bytes to copy */
  7.  
  8. void decode_start(void)
  9. {
  10.     huf_decode_start();
  11.     j = 0;
  12. }
  13.  
  14. void decode(uint count, uchar buffer[])
  15.     /* The calling function must keep the number of
  16.        bytes to be processed.  This function decodes
  17.        either 'count' bytes or 'DICSIZ' bytes, whichever
  18.        is smaller, into the array 'buffer[]' of size
  19.        'DICSIZ' or more.
  20.        Call decode_start() once for each new file
  21.        before calling this function. */
  22. {
  23.     static uint i;
  24.     uint r, c;
  25.  
  26.     r = 0;
  27.     while (--j >= 0) {
  28.         buffer[r] = buffer[i];
  29.         i = (i + 1) & (DICSIZ - 1);
  30.         if (++r == count) return;
  31.     }
  32.     for ( ; ; ) {
  33.         c = decode_c();
  34.         if (c <= UCHAR_MAX) {
  35.             buffer[r] = c;
  36.             if (++r == count) return;
  37.         } else {
  38.             j = c - (UCHAR_MAX + 1 - THRESHOLD);
  39.             i = (r - decode_p() - 1) & (DICSIZ - 1);
  40.             while (--j >= 0) {
  41.                 buffer[r] = buffer[i];
  42.                 i = (i + 1) & (DICSIZ - 1);
  43.                 if (++r == count) return;
  44.             }
  45.         }
  46.     }
  47. }
  48.