OSDN Git Service

added add.c extract.c list.c
[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 warn(char *fmt, ...)
44 {
45     va_list args;
46
47     va_start(args, fmt);
48     vfprintf(stderr, fmt, args);
49     putc('\n', stderr);
50     va_end(args);
51 }
52
53 void
54 make_crctable(void)
55 {
56     uint i, j, r;
57
58     for (i = 0; i <= UCHAR_MAX; i++) {
59         r = i;
60         for (j = 0; j < CHAR_BIT; j++)
61             if (r & 1)
62                 r = (r >> 1) ^ CRCPOLY;
63             else
64                 r >>= 1;
65         crctable[i] = r;
66     }
67 }
68
69 void
70 fillbuf(int n)
71 {                               /* Shift bitbuf n bits left, read n bits */
72     bitbuf <<= n;
73     while (n > bitcount) {
74         bitbuf |= subbitbuf << (n -= bitcount);
75         if (compsize != 0) {
76             compsize--;
77             subbitbuf = (uchar) getc(arcfile);
78         }
79         else
80             subbitbuf = 0;
81         bitcount = CHAR_BIT;
82     }
83     bitbuf |= subbitbuf >> (bitcount -= n);
84 }
85
86 uint
87 getbits(int n)
88 {
89     uint x;
90
91     x = bitbuf >> (BITBUFSIZ - n);
92     fillbuf(n);
93     return x;
94 }
95
96 void
97 putbits(int n, uint x)
98 {                               /* Write rightmost n bits of x */
99     if (n < bitcount) {
100         subbitbuf |= x << (bitcount -= n);
101     }
102     else {
103         if (compsize < origsize) {
104             putc(subbitbuf | (x >> (n -= bitcount)), outfile);
105             compsize++;
106         }
107         else
108             unpackable = 1;
109         if (n < CHAR_BIT) {
110             subbitbuf = x << (bitcount = CHAR_BIT - n);
111         }
112         else {
113             if (compsize < origsize) {
114                 putc(x >> (n - CHAR_BIT), outfile);
115                 compsize++;
116             }
117             else
118                 unpackable = 1;
119             subbitbuf = x << (bitcount = 2 * CHAR_BIT - n);
120         }
121     }
122 }
123
124 int
125 fread_crc(void *p, int n, FILE * f)
126 {
127     int i;
128
129     i = n = fread(p, 1, n, f);
130     origsize += n;
131     while (--i >= 0)
132         UPDATE_CRC(*(unsigned char*)p++);
133     return n;
134 }
135
136 void
137 fwrite_crc(void *p, int n, FILE * f)
138 {
139     if (fwrite(p, 1, n, f) < n)
140         error("Unable to write");
141     while (--n >= 0)
142         UPDATE_CRC(*(unsigned char*)p++);
143 }
144
145 void
146 init_getbits(void)
147 {
148     bitbuf = 0;
149     subbitbuf = 0;
150     bitcount = 0;
151     fillbuf(BITBUFSIZ);
152 }
153
154 void
155 init_putbits(void)
156 {
157     bitcount = CHAR_BIT;
158     subbitbuf = 0;
159 }