home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / OLS / Os2 / LHA2P205 / LHA2P205.LZH / lha2-2.05pre / source.lzh / src / crcio.c < prev    next >
C/C++ Source or Header  |  1995-10-15  |  4KB  |  218 lines

  1. /*
  2.  * crcio.c --- input/output
  3.  *   Copyright (C) 1988-1992, Haruyasu YOSHIZAKI
  4.  *   Copyright (C) 1991-1995, Satoshi HIRAMATSU (OS/2 HPFS version)
  5.  *
  6.  * $Log$
  7.  */
  8.  
  9.  
  10. #include <sys/types.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <stdarg.h>
  14. #include <io.h>
  15. #include <time.h>
  16. #include <limits.h>
  17. #include "typedef.h"
  18. #include "port2.h"
  19. #include "lh.h"
  20. #include "header.h"
  21. #include "disp.h"
  22. #include "slidehuf.h"
  23. #include "intrface.h"
  24. #include "errmes.h"
  25.  
  26.  
  27. #define CRCPOLY 0xa001U        /* CRC-16 */
  28. #define UPDATE_CRC(c) \
  29.   crc = crctable[((uint)crc ^ (c)) & 0xFF] ^ ((uint)crc >> CHAR_BIT)
  30.  
  31.  
  32. FILE *infile, *outfile;
  33. ushort bitbuf;
  34. int dispflg;
  35.  
  36.  
  37. static ushort crctable[UCHAR_MAX + 1];
  38. static uchar  subbitbuf, bitcount;
  39.  
  40.  
  41. void
  42. make_crctable(void)
  43. {
  44.   uint i, j, r;
  45.  
  46.   for(i = 0; i <= UCHAR_MAX; i++)
  47.     {
  48.       r = i;
  49.       for(j = 0; j < CHAR_BIT; j++)
  50.     if(r & 1)
  51.       r = (r >> 1) ^ CRCPOLY;
  52.     else
  53.       r >>= 1;
  54.       crctable[i] = r;
  55.     }
  56. }
  57.  
  58.  
  59. ushort
  60. calccrc(uchar *p, ulong n, hword crc)
  61. {
  62.   while(n-- > 0)
  63.     UPDATE_CRC(*p++);
  64.  
  65.   return crc;
  66. }
  67.  
  68.  
  69. void
  70. fillbuf(uchar n)        /* Shift bitbuf n bits left, read n bits */
  71. {
  72.   while(n > bitcount)
  73.     {
  74.       n -= bitcount;
  75.       bitbuf = (bitbuf << bitcount) + (subbitbuf >> (CHAR_BIT - bitcount));
  76.       if(compsize != 0)
  77.     {
  78.       compsize--;
  79.       subbitbuf = infile ? (uchar) getc(infile) : *use_ptr++;
  80.     }
  81.       else
  82.     subbitbuf = 0;
  83.       bitcount = CHAR_BIT;
  84.     }
  85.   bitcount -= n;
  86.   bitbuf = (bitbuf << n) + (subbitbuf >> (CHAR_BIT - n));
  87.   subbitbuf <<= n;
  88. }
  89.  
  90.  
  91. ushort
  92. getbits(uchar n)
  93. {
  94.   ushort x;
  95.  
  96.   x = bitbuf >> (2 * CHAR_BIT - n);
  97.   fillbuf(n);
  98.  
  99.   return x;
  100. }
  101.  
  102. void
  103. putcode(uchar n, ushort x)    /* Write leftmost n bits of x */
  104. {
  105.   while(n >= bitcount)
  106.     {
  107.       n -= bitcount;
  108.       subbitbuf += x >> (USHRT_BIT - bitcount);
  109.       x <<= bitcount;
  110.       if(compsize < origsize)
  111.     {
  112.       if(putc(subbitbuf, outfile) == EOF)
  113.         fileerror(WTERR, outfile);
  114.       compsize++;
  115.     }
  116.       else
  117.     unpackable = 1;
  118.       subbitbuf = 0;
  119.       bitcount = CHAR_BIT;
  120.     }
  121.   subbitbuf += x >> (USHRT_BIT - bitcount);
  122.   bitcount -= n;
  123. }
  124.  
  125.  
  126. void
  127. putbits(uchar n, ushort x)    /* Write rightmost n bits of x */
  128. {
  129.   x <<= USHRT_BIT - n;
  130.   while(n >= bitcount)
  131.     {
  132.       n -= bitcount;
  133.       subbitbuf += x >> (USHRT_BIT - bitcount);
  134.       x <<= bitcount;
  135.       if(compsize < origsize)
  136.     {
  137.       if(putc(subbitbuf, outfile) == EOF)
  138.         fileerror(WTERR, outfile);
  139.       compsize++;
  140.     }
  141.       else
  142.     unpackable = 1;
  143.       subbitbuf = 0;
  144.       bitcount = CHAR_BIT;
  145.     }
  146.   subbitbuf += x >> (USHRT_BIT - bitcount);
  147.   bitcount -= n;
  148. }
  149.  
  150.  
  151. int
  152. fread_crc(uchar *p, int n, FILE *f, hword *crc)
  153. {
  154.   n = fread(p, 1, n, f);
  155.   *crc = calccrc(p, n, *crc);
  156.   if(!dispflg)
  157.     dispmark('o');
  158.  
  159.   return n;
  160. }
  161.  
  162.  
  163. void
  164. fwrite_crc(uchar *p, int n, FILE *f, hword *crc)
  165. {
  166.   if(f)
  167.     if(fwrite(p, 1, n, f) < n && !isatty(fileno(f))) 
  168.       fileerror(WTERR, f);
  169.   *crc = calccrc(p, n, *crc);
  170.   if(!dispflg)
  171.     dispmark('o');
  172. }
  173.  
  174.  
  175. void
  176. init_getbits(void)
  177. {
  178.   bitbuf = 0;
  179.   subbitbuf = 0;
  180.   bitcount = 0;
  181.   fillbuf(2 * CHAR_BIT);
  182. }
  183.  
  184.  
  185. void
  186. init_putbits(void)
  187. {
  188.   bitcount = CHAR_BIT;
  189.   subbitbuf = 0;
  190. }
  191.  
  192.  
  193. off_t
  194. crcfread(fp, size, crc)
  195.      FILE *fp;
  196.      off_t size;
  197.      hword *crc;
  198. {
  199.   byte *buffer;
  200.  
  201.   off_t rsize = READ_MAX;
  202.   off_t lsize = size;
  203.  
  204.   buffer = (byte *)e_malloc(READ_MAX);
  205.   do
  206.     {
  207.       if(lsize > rsize)
  208.     rsize = fread(buffer, 1, rsize, fp);
  209.       else
  210.     rsize = fread(buffer, 1, lsize, fp);
  211.       *crc = calccrc(buffer, rsize, *crc);
  212.     }
  213.   while(lsize -= rsize);
  214.   free(buffer);
  215.  
  216.   return size;
  217. }
  218.