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 /* fileerror(WTERR, outfile); */
113 fatal_error("Write error in crcio.c(putcode)\n");
123 subbitbuf += x >> (USHRT_BIT - bitcount);
127 /* ------------------------------------------------------------------------ */
129 putbits(n, x) /* Write rightmost n bits of x */
134 while (n >= bitcount) {
136 subbitbuf += x >> (USHRT_BIT - bitcount);
138 if (compsize < origsize) {
139 if (fwrite(&subbitbuf, 1, 1, outfile) == 0) {
140 /* fileerror(WTERR, outfile); */
141 fatal_error("Write error in crcio.c(putbits)\n");
151 subbitbuf += x >> (USHRT_BIT - bitcount);
155 /* ------------------------------------------------------------------------ */
163 n = fread_txt(p, n, fp);
165 n = fread(p, 1, n, fp);
171 /* ------------------------------------------------------------------------ */
184 if (fwrite_txt(p, n, fp))
185 fatal_error("File write error\n");
188 if (fwrite(p, 1, n, fp) < n)
189 fatal_error("File write error\n");
194 /* ------------------------------------------------------------------------ */
196 init_code_cache( /* void */ )
197 { /* called from copyfile() in util.c */
199 putc_euc_cache = EOF;
201 getc_euc_cache = EOF;
205 init_getbits( /* void */ )
210 fillbuf(2 * CHAR_BIT);
212 putc_euc_cache = EOF;
216 /* ------------------------------------------------------------------------ */
218 init_putbits( /* void */ )
222 getc_euc_cache = EOF;
225 /* ------------------------------------------------------------------------ */
234 if (putc_euc_cache == EOF) {
235 if (!euc_mode || c < 0x81 || c > 0xFC) {
239 if (c >= 0xA0 && c < 0xE0) {
240 putc(0x8E, fd); /* single shift */
244 putc_euc_cache = c; /* save first byte */
248 putc_euc_cache = EOF;
253 d = (d - 0x81) * 2 + 0x22;
259 d = (d - 0x81) * 2 + 0x21;
266 /* ------------------------------------------------------------------------ */
274 if (*p != '\015' && *p != '\032') {
287 /* ------------------------------------------------------------------------ */
298 if (getc_euc_cache != EOF) {
300 getc_euc_cache = EOF;
303 if ((c = fgetc(fp)) == EOF)
311 else if (euc_mode && (c == 0x8E || 0xA0 < c && c < 0xFF)) {
318 if (c == 0x8E) { /* single shift (KANA) */
319 if ((0x20 < d && d < 0x7F) || (0xA0 < d && d < 0xFF))
325 if (0xA0 < d && d < 0xFF) { /* if GR */
326 c &= 0x7F; /* convert to MS-kanji */
332 if ((d += 0x40 - 0x21) > 0x7E)
334 if ((c = (c >> 1) + 0x71) >= 0xA0)
348 /* ------------------------------------------------------------------------ */
350 calc_header_crc(p, n) /* Thanks T.Okamoto */
359 /* Local Variables: */
362 /* vi: set tabstop=4: */