1 /* ------------------------------------------------------------------------ */
3 /* bitio.c -- bit stream */
5 /* Modified Nobutaka Watazaki */
7 /* Ver. 1.14 Source All chagned 1995.01.14 N.Watazaki */
8 /* Separated from crcio.c 2002.10.26 Koji Arai */
9 /* ------------------------------------------------------------------------ */
12 static unsigned char subbitbuf, bitcount;
15 fillbuf(n) /* Shift bitbuf n bits left, read n bits */
18 while (n > bitcount) {
20 bitbuf = (bitbuf << bitcount) + (subbitbuf >> (CHAR_BIT - bitcount));
23 subbitbuf = (unsigned char) getc(infile);
30 bitbuf = (bitbuf << n) + (subbitbuf >> (CHAR_BIT - n));
40 x = bitbuf >> (2 * CHAR_BIT - n);
46 putcode(n, x) /* Write leftmost n bits of x */
50 while (n >= bitcount) {
52 subbitbuf += x >> (USHRT_BIT - bitcount);
54 if (compsize < origsize) {
55 if (fwrite(&subbitbuf, 1, 1, outfile) == 0) {
56 fatal_error("Write error in bitio.c(putcode)");
65 subbitbuf += x >> (USHRT_BIT - bitcount);
70 putbits(n, x) /* Write rightmost n bits of x */
79 init_getbits( /* void */ )
84 fillbuf(2 * CHAR_BIT);
88 init_putbits( /* void */ )