home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c019 / 1.ddi / AR001 / IO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-22  |  2.5 KB  |  120 lines

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