home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume35 / freeze / part02 / decode.c < prev    next >
C/C++ Source or Header  |  1993-02-21  |  3KB  |  158 lines

  1. #include "freeze.h"
  2. #include "huf.h"
  3. #include "bitio.h"
  4.  
  5. /*
  6.  * Melt stdin to stdout.
  7.  */
  8.  
  9. void melt2 ()
  10. {
  11.     register short    i, j, k, r, c;
  12.  
  13. /* Huffman-dependent part */
  14.     if(read_header() == EOF)
  15.         return;
  16.     StartHuff(N_CHAR2);
  17.     init(Table2);
  18. /* end of Huffman-dependent part */
  19.  
  20.     InitIO();
  21.     for (i = WINSIZE - LOOKAHEAD; i < WINSIZE; i++)
  22.         text_buf[i] = ' ';
  23.     r = 0;
  24.     for (in_count = 0;; ) {
  25.         c = DecodeChar();
  26.  
  27.         if (c == ENDOF)
  28.             break;
  29.         if (c < 256) {
  30. #ifdef DEBUG
  31.             if (debug)
  32.                 fprintf(stderr, "'%s'\n", pr_char((uc_t)c));
  33. #endif /* DEBUG */
  34.             text_buf[r++] = c;
  35.             in_count++;
  36.             r &= WINMASK;
  37. #ifdef DEBUG
  38.             if (!debug)
  39. #endif
  40.             if (r == 0) {
  41.                 fwrite(text_buf, WINSIZE, 1, stdout);
  42.                 if (ferror(stdout))
  43.                     writeerr();
  44.             }
  45.         } else {
  46.             i = (r - DecodePosition() - 1) & WINMASK;
  47.             j = c - 256 + THRESHOLD;
  48. #ifdef DEBUG
  49.             if (debug)
  50.                 fputc('"', stderr);
  51. #endif
  52.             for (k = 0; k < j; k++) {
  53.                 c = text_buf[(i + k) & WINMASK];
  54. #ifdef DEBUG
  55.                 if (debug)
  56.                     fprintf(stderr, "%s", pr_char((uc_t)c));
  57. #endif
  58.                 text_buf[r++] = c;
  59.                 in_count++;
  60.                 r &= WINMASK;
  61. #ifdef DEBUG
  62.                 if (!debug)
  63. #endif
  64.                 if (r == 0) {
  65.                     fwrite(text_buf, WINSIZE, 1, stdout);
  66.                     if (ferror(stdout))
  67.                         writeerr();
  68.                 }
  69.             }
  70. #ifdef DEBUG
  71.             if (debug)
  72.                 fprintf(stderr, "\"\n");
  73. #endif
  74.         }
  75.         INDICATOR
  76.     }
  77.     if (r) {
  78.         fwrite(text_buf, r, 1, stdout);
  79.         if (ferror(stdout))
  80.             writeerr();
  81.     }
  82.     if (quiet < 0 && file_length != 0)
  83.         fprintf(stderr, "100%%\b\b\b\b");
  84. }
  85.  
  86. #ifdef COMPAT
  87. void melt1 ()
  88. {
  89.     register short    i, j, k, r, c;
  90.  
  91.     StartHuff(N_CHAR1);
  92.     init(Table1);
  93.     InitIO();
  94.     for (i = N1 - F1; i < N1; i++)
  95.         text_buf[i] = ' ';
  96.     r = 0;
  97.     for (in_count = 0;; ) {
  98.         c =  DecodeChar();
  99.  
  100.         if (c == ENDOF)
  101.             break;
  102.  
  103.         if (c < 256) {
  104. #ifdef DEBUG
  105.             if (debug)
  106.                 fprintf(stderr, "'%s'\n", pr_char((uc_t)c));
  107. #endif /* DEBUG */
  108.             text_buf[r++] = c;
  109.             in_count++;
  110.             r &= (N1 - 1);
  111. #ifdef DEBUG
  112.             if (!debug)
  113. #endif
  114.             if (r == 0) {
  115.                 fwrite(text_buf, N1, 1, stdout);
  116.                 if (ferror(stdout))
  117.                     writeerr();
  118.             }
  119.         } else {
  120.             i = (r - DecodePOld() - 1) & (N1 - 1);
  121.             j = c - 256 + THRESHOLD;
  122. #ifdef DEBUG
  123.             if (debug)
  124.                 fputc('"', stderr);
  125. #endif
  126.             for (k = 0; k < j; k++) {
  127.                 c = text_buf[(i + k) & (N1 - 1)];
  128. #ifdef DEBUG
  129.                 if (debug)
  130.                     fprintf(stderr, "%s", pr_char((uc_t)c));
  131. #endif
  132.                 text_buf[r++] = c;
  133.                 in_count++;
  134.                 r &= (N1 - 1);
  135. #ifdef DEBUG
  136.                 if (!debug)
  137. #endif
  138.                 if (r == 0) {
  139.                     fwrite(text_buf, N1, 1, stdout);
  140.                     if (ferror(stdout))
  141.                         writeerr();
  142.                 }
  143.             }
  144. #ifdef DEBUG
  145.             if (debug)
  146.                 fprintf(stderr, "\"\n");
  147. #endif
  148.         }
  149.         INDICATOR
  150.     }
  151.     if (r) {
  152.         fwrite(text_buf, r, 1, stdout);
  153.         if (ferror(stdout))
  154.             writeerr();
  155.     }
  156. }
  157. #endif
  158.