OSDN Git Service

Avoid compile error on libapplefile
[lha/lha.git] / src / bitio.c
1 /* ------------------------------------------------------------------------ */
2 /* LHa for UNIX                                                             */
3 /*              bitio.c -- bit stream                                       */
4 /*                                                                          */
5 /*      Modified                Nobutaka Watazaki                           */
6 /*                                                                          */
7 /*  Ver. 1.14   Source All chagned              1995.01.14  N.Watazaki      */
8 /*              Separated from crcio.c          2002.10.26  Koji Arai       */
9 /* ------------------------------------------------------------------------ */
10 #include "lha.h"
11
12 static unsigned char subbitbuf, bitcount;
13
14 void
15 fillbuf(n)          /* Shift bitbuf n bits left, read n bits */
16     unsigned char   n;
17 {
18     while (n > bitcount) {
19         n -= bitcount;
20         bitbuf = (bitbuf << bitcount) + (subbitbuf >> (CHAR_BIT - bitcount));
21         if (compsize != 0) {
22             compsize--;
23             int c = getc(infile);
24             if (c == EOF) {
25                 fatal_error("cannot read stream");
26             }
27             subbitbuf = (unsigned char)c;
28         }
29         else
30             subbitbuf = 0;
31         bitcount = CHAR_BIT;
32     }
33     bitcount -= n;
34     bitbuf = (bitbuf << n) + (subbitbuf >> (CHAR_BIT - n));
35     subbitbuf <<= n;
36 }
37
38 unsigned short
39 getbits(n)
40     unsigned char   n;
41 {
42     unsigned short  x;
43
44     x = bitbuf >> (2 * CHAR_BIT - n);
45     fillbuf(n);
46     return x;
47 }
48
49 void
50 putcode(n, x)           /* Write leftmost n bits of x */
51     unsigned char   n;
52     unsigned short  x;
53 {
54     while (n >= bitcount) {
55         n -= bitcount;
56         subbitbuf += x >> (USHRT_BIT - bitcount);
57         x <<= bitcount;
58         if (compsize < origsize) {
59             if (fwrite(&subbitbuf, 1, 1, outfile) == 0) {
60                 fatal_error("Write error in bitio.c(putcode)");
61             }
62             compsize++;
63         }
64         else
65             unpackable = 1;
66         subbitbuf = 0;
67         bitcount = CHAR_BIT;
68     }
69     subbitbuf += x >> (USHRT_BIT - bitcount);
70     bitcount -= n;
71 }
72
73 void
74 putbits(n, x)           /* Write rightmost n bits of x */
75     unsigned char   n;
76     unsigned short  x;
77 {
78     x <<= USHRT_BIT - n;
79     putcode(n, x);
80 }
81
82 void
83 init_getbits( /* void */ )
84 {
85     bitbuf = 0;
86     subbitbuf = 0;
87     bitcount = 0;
88     fillbuf(2 * CHAR_BIT);
89 }
90
91 void
92 init_putbits( /* void */ )
93 {
94     bitcount = CHAR_BIT;
95     subbitbuf = 0;
96 }