1 /* ------------------------------------------------------------------------ */
3 /* bitio.c -- bit stream */
5 /* Modified Nobutaka Watazaki */
7 /* Ver. 1.14 Source All chagned 1995.01.14 N.Watazaki */
8 /* ------------------------------------------------------------------------ */
11 static unsigned char subbitbuf, bitcount;
14 fillbuf(n) /* Shift bitbuf n bits left, read n bits */
17 while (n > bitcount) {
19 bitbuf = (bitbuf << bitcount) + (subbitbuf >> (CHAR_BIT - bitcount));
22 subbitbuf = (unsigned char) getc(infile);
29 bitbuf = (bitbuf << n) + (subbitbuf >> (CHAR_BIT - n));
39 x = bitbuf >> (2 * CHAR_BIT - n);
45 putcode(n, x) /* Write leftmost n bits of x */
49 while (n >= bitcount) {
51 subbitbuf += x >> (USHRT_BIT - bitcount);
53 if (compsize < origsize) {
54 if (fwrite(&subbitbuf, 1, 1, outfile) == 0) {
55 fatal_error("Write error in bitio.c(putcode)");
64 subbitbuf += x >> (USHRT_BIT - bitcount);
69 putbits(n, x) /* Write rightmost n bits of x */
78 init_getbits( /* void */ )
83 fillbuf(2 * CHAR_BIT);
87 init_putbits( /* void */ )