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

  1. /***********************************************************
  2.     io.c -- input/output
  3. ***********************************************************/
  4. #include "ar.h"
  5. #include <stdlib.h>
  6. #include <stdarg.h>
  7.  
  8. #define CRCPOLY  0xA001  /* ANSI CRC-16 */
  9.                          /* CCITT: 0x8408 */
  10. #define UPDATE_CRC(c) \
  11.     crc = crctable[(crc ^ (c)) & 0xFF] ^ (crc >> CHAR_BIT)
  12.  
  13. FILE *arcfile, *infile, *outfile;
  14. uint crc, bitbuf;
  15.  
  16. static ushort crctable[UCHAR_MAX + 1];
  17. static uint  subbitbuf;
  18. static int   bitcount;
  19.  
  20. void error(char *fmt, ...)
  21. {
  22.     va_list args;
  23.  
  24.     va_start(args, fmt);
  25.     putc('\n', stderr);
  26.     vfprintf(stderr, fmt, args);
  27.     putc('\n', stderr);
  28.     va_end(args);
  29.     exit(EXIT_FAILURE);
  30. }
  31.  
  32. void make_crctable(void)
  33. {
  34.     uint i, j, r;
  35.  
  36.     for (i = 0; i <= UCHAR_MAX; i++) {
  37.         r = i;
  38.         for (j = 0; j < CHAR_BIT; j++)
  39.             if (r & 1) r = (r >> 1) ^ CRCPOLY;
  40.             else       r >>= 1;
  41.         crctable[i] = r;
  42.     }
  43. }
  44.  
  45. void fillbuf(int n)  /* Shift bitbuf n bits left, read n bits */
  46. {
  47.     bitbuf <<= n;
  48.     while (n > bitcount) {
  49.         bitbuf |= subbitbuf << (n -= bitcount);
  50.         if (compsize != 0) {
  51.             compsize--;  subbitbuf = (uchar) getc(arcfile);
  52.         } else subbitbuf = 0;
  53.         bitcount = CHAR_BIT;
  54.     }
  55.     bitbuf |= subbitbuf >> (bitcount -= n);
  56. }
  57.  
  58. uint getbits(int n)
  59. {
  60.     uint x;
  61.  
  62.     x = bitbuf >> (BITBUFSIZ - n);  fillbuf(n);
  63.     return x;
  64. }
  65.  
  66. void putbits(int n, uint x)  /* Write rightmost n bits of x */
  67. {
  68.     if (n < bitcount) {
  69.         subbitbuf |= x << (bitcount -= n);
  70.     } else {
  71.         if (compsize < origsize) {
  72.             putc(subbitbuf | (x >> (n -= bitcount)), outfile);
  73.             compsize++;
  74.         } else unpackable = 1;
  75.         if (n < CHAR_BIT) {
  76.             subbitbuf = x << (bitcount = CHAR_BIT - n);
  77.         } else {
  78.             if (compsize < origsize) {
  79.                 putc(x >> (n - CHAR_BIT), outfile);
  80.                 compsize++;
  81.             } else unpackable = 1;
  82.             subbitbuf = x << (bitcount = 2 * CHAR_BIT - n);
  83.         }
  84.     }
  85. }
  86.  
  87. int fread_crc(uchar *p, int n, FILE *f)
  88. {
  89.     int i;
  90.  
  91.     i = n = fread(p, 1, n, f);  origsize += n;
  92.     while (--i >= 0) UPDATE_CRC(*p++);
  93.     return n;
  94. }
  95.  
  96. void fwrite_crc(uchar *p, int n, FILE *f)
  97. {
  98.     if (fwrite(p, 1, n, f) < n) error("Unable to write");
  99.     while (--n >= 0) UPDATE_CRC(*p++);
  100. }
  101.  
  102. void init_getbits(void)
  103. {
  104.     bitbuf = 0;  subbitbuf = 0;  bitcount = 0;
  105.     fillbuf(BITBUFSIZ);
  106. }
  107.  
  108. void init_putbits(void)
  109. {
  110.     bitcount = CHAR_BIT;  subbitbuf = 0;
  111. }
  112.