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

  1. /*$Source: /usr/home/dhesi/zoo/RCS/io.c,v $*/
  2. /*$Id: io.c,v 1.14 91/07/09 01:39:54 dhesi Exp $*/
  3. /***********************************************************
  4.     io.c -- input/output
  5.  
  6. Adapted from "ar" archiver written by Haruhiko Okumura.
  7. ***********************************************************/
  8. #ifdef ANSI_HDRS
  9. # include <stdlib.h>
  10. # include <string.h>
  11. #endif
  12.  
  13. #include "options.h"
  14. #include "zoo.h"
  15. #include "ar.h"
  16. #include "lzh.h"
  17. #include "zooio.h"    /* for NULLFILE */
  18. #include "portable.h"
  19.  
  20. extern void prterror();
  21.  
  22. #include "errors.i"
  23.  
  24. #define JUST_LZH        /* for stand-alone compression */
  25.  
  26. #if 0
  27. # define CRCPOLY  0xA001  /* ANSI CRC-16 */ /* CCITT: 0x8408 */
  28. # define UPDATE_CRC(c) \
  29.     crc = crctable[(crc ^ (c)) & 0xFF] ^ (crc >> CHAR_BIT)
  30. static ushort crctable[UCHAR_MAX + 1];
  31. t_uint16 crc;
  32. #endif
  33.  
  34. extern FILE *arcfile, *lzh_outfile;
  35. t_uint16 bitbuf;
  36. int unpackable;
  37.  
  38. ulong compsize, origsize;
  39.  
  40. static uint  subbitbuf;
  41. static int   bitcount;
  42.  
  43. #if 0
  44. void make_crctable()
  45. {
  46.     uint i, j, r;
  47.  
  48.     for (i = 0; i <= UCHAR_MAX; i++) {
  49.         r = i;
  50.         for (j = 0; j < CHAR_BIT; j++)
  51.             if (r & 1) r = (r >> 1) ^ CRCPOLY;
  52.             else       r >>= 1;
  53.         crctable[i] = r;
  54.     }
  55. }
  56. #endif
  57.  
  58. void fillbuf(n)  /* Shift bitbuf n bits left, read n bits */
  59. int n;
  60. {
  61.     bitbuf <<= n;
  62.     while (n > bitcount) {
  63.         bitbuf |= subbitbuf << (n -= bitcount);
  64. #ifdef JUST_LZH
  65.         if (feof(arcfile))
  66.             subbitbuf = 0;
  67.         else
  68.             subbitbuf = (uchar) zgetc(arcfile);
  69. #else
  70.         if (compsize != 0) {
  71.             compsize--;  subbitbuf = (uchar) zgetc(arcfile);
  72.         } else subbitbuf = 0;
  73. #endif /* JUST_LZH */
  74.         bitcount = CHAR_BIT;
  75.     }
  76.     bitbuf |= subbitbuf >> (bitcount -= n);
  77. }
  78.  
  79. uint getbits(n)
  80. int n;
  81. {
  82.     uint x;
  83.  
  84.     x = bitbuf >> (BITBUFSIZ - n);  fillbuf(n);
  85.     return x;
  86. }
  87.  
  88. void putbits(n, x)  /* Write rightmost n bits of x */
  89. int n;
  90. uint x;
  91. {
  92.     if (n < bitcount) {
  93.         subbitbuf |= x << (bitcount -= n);
  94.     } else {
  95. #ifdef JUST_LZH
  96.         (void) putc((int) (subbitbuf | (x >> (n -= bitcount))), lzh_outfile);
  97.         compsize++;
  98. #else
  99.         if (compsize < origsize) {
  100.             (void) zputc((int) (subbitbuf | (x >> (n -= bitcount))), lzh_outfile);
  101.             compsize++;
  102.         } else unpackable = 1;
  103. #endif /* JUST_LZH */
  104.  
  105.         if (n < CHAR_BIT) {
  106.             subbitbuf = x << (bitcount = CHAR_BIT - n);
  107.         } else {
  108. #ifdef JUST_LZH
  109.             (void) putc((int) (x >> (n - CHAR_BIT)), lzh_outfile);
  110.             compsize++;
  111. #else
  112.             if (compsize < origsize) {
  113.                 (void) zputc((int) (x >> (n - CHAR_BIT)), lzh_outfile);
  114.                 compsize++;
  115.             } else unpackable = 1;
  116. #endif /* JUST_LZH */
  117.             subbitbuf = x << (bitcount = 2 * CHAR_BIT - n);
  118.         }
  119.     }
  120. }
  121.  
  122. extern void addbfcrc();
  123.  
  124. int fread_crc(p, n, f)
  125. uchar *p;
  126. int n;
  127. FILE *f;
  128. {
  129.     int i;
  130.  
  131.     i = n = fread((char *) p, 1, n, f);  origsize += n;
  132.     addbfcrc(p, i);
  133.     return n;
  134. }
  135.  
  136. void fwrite_crc(p, n, f)
  137. uchar *p;
  138. int n;
  139. FILE *f;
  140. {
  141.     if (f != NULLFILE) {
  142.         if (fwrite((char *) p, 1, n, f) < n)
  143.             prterror('f', disk_full);
  144.     }
  145.     addbfcrc(p, n);
  146. }
  147.  
  148. void init_getbits()
  149. {
  150.     bitbuf = 0;  subbitbuf = 0;  bitcount = 0;
  151.     fillbuf(BITBUFSIZ);
  152. }
  153.  
  154. void init_putbits()
  155. {
  156.     bitcount = CHAR_BIT;  subbitbuf = 0;
  157. }
  158.