home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ZOO21E.EXE / DECODE.C < prev    next >
C/C++ Source or Header  |  1991-07-11  |  2KB  |  70 lines

  1. /*$Source: /usr/home/dhesi/zoo/RCS/decode.c,v $*/
  2. /*$Id: decode.c,v 1.6 91/07/09 01:39:49 dhesi Exp $*/
  3. /***********************************************************
  4.     decode.c
  5.  
  6. Adapted from "ar" archiver written by Haruhiko Okumura.
  7. ***********************************************************/
  8.  
  9. #include "options.h"
  10. #include "zoo.h"
  11. #include "ar.h"
  12. #include "lzh.h"
  13.  
  14. extern int decoded;        /* from huf.c */
  15.  
  16. static int j;  /* remaining bytes to copy */
  17.  
  18. void decode_start()
  19. {
  20.     huf_decode_start();
  21.     j = 0;
  22.     decoded = 0;
  23. }
  24.  
  25. /*
  26. decodes; returns no. of chars decoded
  27. */
  28.  
  29. int decode(count, buffer)
  30. uint count;
  31. uchar buffer[];
  32.     /* The calling function must keep the number of
  33.        bytes to be processed.  This function decodes
  34.        either 'count' bytes or 'DICSIZ' bytes, whichever
  35.        is smaller, into the array 'buffer[]' of size
  36.        'DICSIZ' or more.
  37.        Call decode_start() once for each new file
  38.        before calling this function. */
  39. {
  40.     static uint i;
  41.     uint r, c;
  42.  
  43.     r = 0;
  44.     while (--j >= 0) {
  45.         buffer[r] = buffer[i];
  46.         i = (i + 1) & (DICSIZ - 1);
  47.         if (++r == count)
  48.             return r;
  49.     }
  50.     for ( ; ; ) {
  51.         c = decode_c();
  52.         if (decoded)
  53.             return r;
  54.         if (c <= UCHAR_MAX) {
  55.             buffer[r] = c;
  56.             if (++r == count)
  57.                 return r;
  58.         } else {
  59.             j = c - (UCHAR_MAX + 1 - THRESHOLD);
  60.             i = (r - decode_p() - 1) & (DICSIZ - 1);
  61.             while (--j >= 0) {
  62.                 buffer[r] = buffer[i];
  63.                 i = (i + 1) & (DICSIZ - 1);
  64.                 if (++r == count)
  65.                     return r;
  66.             }
  67.         }
  68.     }
  69. }
  70.