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));
25 fatal_error("cannot read stream");
27 subbitbuf = (unsigned char)c;
34 bitbuf = (bitbuf << n) + (subbitbuf >> (CHAR_BIT - n));
44 x = bitbuf >> (2 * CHAR_BIT - n);
50 putcode(n, x) /* Write leftmost n bits of x */
54 while (n >= bitcount) {
56 subbitbuf += x >> (USHRT_BIT - bitcount);
58 if (compsize < origsize) {
59 if (fwrite(&subbitbuf, 1, 1, outfile) == 0) {
60 fatal_error("Write error in bitio.c(putcode)");
69 subbitbuf += x >> (USHRT_BIT - bitcount);
74 putbits(n, x) /* Write rightmost n bits of x */
83 init_getbits( /* void */ )
88 fillbuf(2 * CHAR_BIT);
92 init_putbits( /* void */ )