1 /***********************************************************
3 ***********************************************************/
6 #define CRCPOLY 0xA001 /* ANSI CRC-16 */
13 ushort crctable[UCHAR_MAX + 1];
14 static uint subbitbuf;
22 for (i = 0; i <= UCHAR_MAX; i++) {
24 for (j = 0; j < CHAR_BIT; j++)
26 r = (r >> 1) ^ CRCPOLY;
34 fillbuf(struct lzh_istream *rp, int n)
35 { /* Shift bitbuf n bits left, read n bits */
37 while (n > bitcount) {
38 bitbuf |= subbitbuf << (n -= bitcount);
41 subbitbuf = (uchar) getc(rp->fp);
47 bitbuf |= subbitbuf >> (bitcount -= n);
51 getbits(struct lzh_istream *rp, int n)
55 x = bitbuf >> (BITBUFSIZ - n);
61 putbits(struct lzh_ostream *wp, int n, uint x)
62 { /* Write rightmost n bits of x */
64 subbitbuf |= x << (bitcount -= n);
67 if (compsize < origsize) {
68 putc(subbitbuf | (x >> (n -= bitcount)), wp->fp);
74 subbitbuf = x << (bitcount = CHAR_BIT - n);
77 if (compsize < origsize) {
78 putc(x >> (n - CHAR_BIT), wp->fp);
83 subbitbuf = x << (bitcount = 2 * CHAR_BIT - n);
89 fread_crc(void *p, int n, FILE * f)
93 i = n = fread(p, 1, n, f);
96 UPDATE_CRC(*(unsigned char*)p++);
101 fwrite_crc(void *p, int n, FILE * f)
103 if (fwrite(p, 1, n, f) < n)
104 error("Unable to write");
106 UPDATE_CRC(*(unsigned char*)p++);
110 init_getbits(struct lzh_istream *rp)
115 fillbuf(rp, BITBUFSIZ);