OSDN Git Service

* src/lharc.c (init_variable): discard `prof' variable.
[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 /* ------------------------------------------------------------------------ */
9 #include "lha.h"
10
11 static unsigned char subbitbuf, bitcount;
12
13 void
14 fillbuf(n)          /* Shift bitbuf n bits left, read n bits */
15     unsigned char   n;
16 {
17     while (n > bitcount) {
18         n -= bitcount;
19         bitbuf = (bitbuf << bitcount) + (subbitbuf >> (CHAR_BIT - bitcount));
20         if (compsize != 0) {
21             compsize--;
22             subbitbuf = (unsigned char) getc(infile);
23         }
24         else
25             subbitbuf = 0;
26         bitcount = CHAR_BIT;
27     }
28     bitcount -= n;
29     bitbuf = (bitbuf << n) + (subbitbuf >> (CHAR_BIT - n));
30     subbitbuf <<= n;
31 }
32
33 unsigned short
34 getbits(n)
35     unsigned char   n;
36 {
37     unsigned short  x;
38
39     x = bitbuf >> (2 * CHAR_BIT - n);
40     fillbuf(n);
41     return x;
42 }
43
44 void
45 putcode(n, x)           /* Write leftmost n bits of x */
46     unsigned char   n;
47     unsigned short  x;
48 {
49     while (n >= bitcount) {
50         n -= bitcount;
51         subbitbuf += x >> (USHRT_BIT - bitcount);
52         x <<= bitcount;
53         if (compsize < origsize) {
54             if (fwrite(&subbitbuf, 1, 1, outfile) == 0) {
55                 fatal_error("Write error in bitio.c(putcode)");
56             }
57             compsize++;
58         }
59         else
60             unpackable = 1;
61         subbitbuf = 0;
62         bitcount = CHAR_BIT;
63     }
64     subbitbuf += x >> (USHRT_BIT - bitcount);
65     bitcount -= n;
66 }
67
68 void
69 putbits(n, x)           /* Write rightmost n bits of x */
70     unsigned char   n;
71     unsigned short  x;
72 {
73     x <<= USHRT_BIT - n;
74     putcode(n, x);
75 }
76
77 void
78 init_getbits( /* void */ )
79 {
80     bitbuf = 0;
81     subbitbuf = 0;
82     bitcount = 0;
83     fillbuf(2 * CHAR_BIT);
84 }
85
86 void
87 init_putbits( /* void */ )
88 {
89     bitcount = CHAR_BIT;
90     subbitbuf = 0;
91 }