OSDN Git Service

should not add signed operand to unsigned
[lha/olha.git] / io.c
1 /***********************************************************
2         io.c -- input/output
3 ***********************************************************/
4 #include "ar.h"
5 #include <stdlib.h>
6 #include <stdarg.h>
7
8 #define CRCPOLY  0xA001         /* ANSI CRC-16 */
9                          /* CCITT: 0x8408 */
10
11 FILE *arcfile, *infile, *outfile;
12 uint crc;
13 ushort bitbuf;
14
15 ushort crctable[UCHAR_MAX + 1];
16 static uint subbitbuf;
17 static int bitcount;
18
19 void
20 error(char *fmt, ...)
21 {
22     va_list args;
23
24     va_start(args, fmt);
25     vfprintf(stderr, fmt, args);
26     putc('\n', stderr);
27     va_end(args);
28     exit(EXIT_FAILURE);
29 }
30
31 void
32 message(char *fmt, ...)
33 {
34     va_list args;
35
36     va_start(args, fmt);
37     vfprintf(stderr, fmt, args);
38     putc('\n', stderr);
39     va_end(args);
40 }
41
42 void
43 make_crctable(void)
44 {
45     uint i, j, r;
46
47     for (i = 0; i <= UCHAR_MAX; i++) {
48         r = i;
49         for (j = 0; j < CHAR_BIT; j++)
50             if (r & 1)
51                 r = (r >> 1) ^ CRCPOLY;
52             else
53                 r >>= 1;
54         crctable[i] = r;
55     }
56 }
57
58 void
59 fillbuf(int n)
60 {                               /* Shift bitbuf n bits left, read n bits */
61     bitbuf <<= n;
62     while (n > bitcount) {
63         bitbuf |= subbitbuf << (n -= bitcount);
64         if (compsize != 0) {
65             compsize--;
66             subbitbuf = (uchar) getc(arcfile);
67         }
68         else
69             subbitbuf = 0;
70         bitcount = CHAR_BIT;
71     }
72     bitbuf |= subbitbuf >> (bitcount -= n);
73 }
74
75 uint
76 getbits(int n)
77 {
78     uint x;
79
80     x = bitbuf >> (BITBUFSIZ - n);
81     fillbuf(n);
82     return x;
83 }
84
85 void
86 putbits(int n, uint x)
87 {                               /* Write rightmost n bits of x */
88     if (n < bitcount) {
89         subbitbuf |= x << (bitcount -= n);
90     }
91     else {
92         if (compsize < origsize) {
93             putc(subbitbuf | (x >> (n -= bitcount)), outfile);
94             compsize++;
95         }
96         else
97             unpackable = 1;
98         if (n < CHAR_BIT) {
99             subbitbuf = x << (bitcount = CHAR_BIT - n);
100         }
101         else {
102             if (compsize < origsize) {
103                 putc(x >> (n - CHAR_BIT), outfile);
104                 compsize++;
105             }
106             else
107                 unpackable = 1;
108             subbitbuf = x << (bitcount = 2 * CHAR_BIT - n);
109         }
110     }
111 }
112
113 int
114 fread_crc(uchar * p, int n, FILE * f)
115 {
116     int i;
117
118     i = n = fread(p, 1, n, f);
119     origsize += n;
120     while (--i >= 0)
121         UPDATE_CRC(*p++);
122     return n;
123 }
124
125 void
126 fwrite_crc(uchar * p, int n, FILE * f)
127 {
128     if (fwrite(p, 1, n, f) < n)
129         error("Unable to write");
130     while (--n >= 0)
131         UPDATE_CRC(*p++);
132 }
133
134 void
135 init_getbits(void)
136 {
137     bitbuf = 0;
138     subbitbuf = 0;
139     bitcount = 0;
140     fillbuf(BITBUFSIZ);
141 }
142
143 void
144 init_putbits(void)
145 {
146     bitcount = CHAR_BIT;
147     subbitbuf = 0;
148 }