1 /* ------------------------------------------------------------------------ */
3 /* crcio.c -- crc input / output */
5 /* Modified Nobutaka Watazaki */
7 /* Ver. 1.14 Source All chagned 1995.01.14 N.Watazaki */
8 /* ------------------------------------------------------------------------ */
11 /* ------------------------------------------------------------------------ */
12 static unsigned short crctable[UCHAR_MAX + 1];
13 static unsigned char subbitbuf, bitcount;
15 static int putc_euc_cache;
17 static int getc_euc_cache;
19 /* ------------------------------------------------------------------------ */
21 make_crctable( /* void */ )
25 for (i = 0; i <= UCHAR_MAX; i++) {
27 for (j = 0; j < CHAR_BIT; j++)
29 r = (r >> 1) ^ CRCPOLY;
36 /* ------------------------------------------------------------------------ */
37 #ifdef NEED_INCREMENTAL_INDICATOR
42 if (!quiet && indicator_threshold) {
43 while (count > indicator_count) {
46 indicator_count += indicator_threshold;
52 /* ------------------------------------------------------------------------ */
59 #ifdef NEED_INCREMENTAL_INDICATOR
60 put_indicator(reading_size);
67 /* ------------------------------------------------------------------------ */
69 fillbuf(n) /* Shift bitbuf n bits left, read n bits */
72 while (n > bitcount) {
74 bitbuf = (bitbuf << bitcount) + (subbitbuf >> (CHAR_BIT - bitcount));
77 subbitbuf = (unsigned char) getc(infile);
84 bitbuf = (bitbuf << n) + (subbitbuf >> (CHAR_BIT - n));
88 /* ------------------------------------------------------------------------ */
95 x = bitbuf >> (2 * CHAR_BIT - n);
100 /* ------------------------------------------------------------------------ */
102 putcode(n, x) /* Write rightmost n bits of x */
106 while (n >= bitcount) {
108 subbitbuf += x >> (USHRT_BIT - bitcount);
110 if (compsize < origsize) {
111 if (fwrite(&subbitbuf, 1, 1, outfile) == 0) {
112 fatal_error("Write error in crcio.c(putcode)");
121 subbitbuf += x >> (USHRT_BIT - bitcount);
125 /* ------------------------------------------------------------------------ */
127 putbits(n, x) /* Write rightmost n bits of x */
132 while (n >= bitcount) {
134 subbitbuf += x >> (USHRT_BIT - bitcount);
136 if (compsize < origsize) {
137 if (fwrite(&subbitbuf, 1, 1, outfile) == 0) {
138 fatal_error("Write error in crcio.c(putbits)");
147 subbitbuf += x >> (USHRT_BIT - bitcount);
151 /* ------------------------------------------------------------------------ */
159 n = fread_txt(p, n, fp);
161 n = fread(p, 1, n, fp);
167 /* ------------------------------------------------------------------------ */
180 if (fwrite_txt(p, n, fp))
181 fatal_error("File write error");
184 if (fwrite(p, 1, n, fp) < n)
185 fatal_error("File write error");
190 /* ------------------------------------------------------------------------ */
192 init_code_cache( /* void */ )
193 { /* called from copyfile() in util.c */
195 putc_euc_cache = EOF;
197 getc_euc_cache = EOF;
201 init_getbits( /* void */ )
206 fillbuf(2 * CHAR_BIT);
208 putc_euc_cache = EOF;
212 /* ------------------------------------------------------------------------ */
214 init_putbits( /* void */ )
218 getc_euc_cache = EOF;
221 /* ------------------------------------------------------------------------ */
230 if (putc_euc_cache == EOF) {
231 if (!euc_mode || c < 0x81 || c > 0xFC) {
234 if (c >= 0xA0 && c < 0xE0) {
235 if (putc(0x8E, fd) == EOF) return EOF; /* single shift */
238 putc_euc_cache = c; /* save first byte */
242 putc_euc_cache = EOF;
247 d = (d - 0x81) * 2 + 0x22;
253 d = (d - 0x81) * 2 + 0x21;
255 if (putc(0x80 | d, fd) == EOF) return EOF;
256 return putc(0x80 | c, fd);
260 /* ------------------------------------------------------------------------ */
268 if (*p != '\015' && *p != '\032') {
270 if (putc_euc(*p, fp) == EOF)
273 if (putc(*p, fp) == EOF)
283 /* ------------------------------------------------------------------------ */
294 if (getc_euc_cache != EOF) {
296 getc_euc_cache = EOF;
299 if ((c = fgetc(fp)) == EOF)
307 else if (euc_mode && (c == 0x8E || (0xA0 < c && c < 0xFF))) {
314 if (c == 0x8E) { /* single shift (KANA) */
315 if ((0x20 < d && d < 0x7F) || (0xA0 < d && d < 0xFF))
321 if (0xA0 < d && d < 0xFF) { /* if GR */
322 c &= 0x7F; /* convert to MS-kanji */
328 if ((d += 0x40 - 0x21) > 0x7E)
330 if ((c = (c >> 1) + 0x71) >= 0xA0)
344 /* ------------------------------------------------------------------------ */
346 calc_header_crc(p, n) /* Thanks T.Okamoto */
355 /* Local Variables: */
358 /* vi: set tabstop=4: */