Algorithm 版 (精华区)

发信人: lofe ()感激生活(), 信区: Algorithm
标  题: ar002--io.c
发信站: 哈工大紫丁香 (Sun Sep  3 08:27:58 2000), 转信

/***********************************************************
        io.c -- input/output
***********************************************************/
#include "ar.h"
#include <stdlib.h>
#include <stdarg.h>

#define CRCPOLY  0xA001  /* ANSI CRC-16 */
                         /* CCITT: 0x8408 */
#define UPDATE_CRC(c) \
        crc = crctable[(crc ^ (c)) & 0xFF] ^ (crc >> CHAR_BIT)

FILE *arcfile, *infile, *outfile;
uint crc, bitbuf;

static ushort crctable[UCHAR_MAX + 1];
static uint  subbitbuf;
static int   bitcount;

void error(char *fmt, ...)
{
        va_list args;

        va_start(args, fmt);
        putc('\n', stderr);
        vfprintf(stderr, fmt, args);
        putc('\n', stderr);
        va_end(args);
        exit(EXIT_FAILURE);
}

void make_crctable(void)
{
        uint i, j, r;

        for (i = 0; i <= UCHAR_MAX; i++) {
                r = i;
                for (j = 0; j < CHAR_BIT; j++)
                        if (r & 1) r = (r >> 1) ^ CRCPOLY;
                        else       r >>= 1;
                crctable[i] = r;
        }
}

void fillbuf(int n)  /* Shift bitbuf n bits left, read n bits */
{
        bitbuf <<= n;
        while (n > bitcount) {
                bitbuf |= subbitbuf << (n -= bitcount);
                if (compsize != 0) {
                        compsize--;  subbitbuf = (uchar) getc(arcfile);
                } else subbitbuf = 0;
                bitcount = CHAR_BIT;
        }
        bitbuf |= subbitbuf >> (bitcount -= n);
}

uint getbits(int n)
{
        uint x;

        x = bitbuf >> (BITBUFSIZ - n);  fillbuf(n);
        return x;
}

void putbits(int n, uint x)  /* Write rightmost n bits of x */
{
        if (n < bitcount) {
                subbitbuf |= x << (bitcount -= n);
        } else {
                if (compsize < origsize) {
                        putc(subbitbuf | (x >> (n -= bitcount)), outfile);
                        compsize++;
                } else unpackable = 1;
                if (n < CHAR_BIT) {
                        subbitbuf = x << (bitcount = CHAR_BIT - n);
                } else {
                        if (compsize < origsize) {
                                putc(x >> (n - CHAR_BIT), outfile);
                                compsize++;
                        } else unpackable = 1;
                        subbitbuf = x << (bitcount = 2 * CHAR_BIT - n);
                }
        }
}

int fread_crc(uchar *p, int n, FILE *f)
{
        int i;

        i = n = fread(p, 1, n, f);  origsize += n;
        while (--i >= 0) UPDATE_CRC(*p++);
        return n;
}

void fwrite_crc(uchar *p, int n, FILE *f)
{
        if (fwrite(p, 1, n, f) < n) error("Unable to write");
        while (--n >= 0) UPDATE_CRC(*p++);
}

void init_getbits(void)
{
        bitbuf = 0;  subbitbuf = 0;  bitcount = 0;
        fillbuf(BITBUFSIZ);
}

void init_putbits(void)
{
        bitcount = CHAR_BIT;  subbitbuf = 0;
}

--
We Are the World!
                        infosite@263.net
※ 修改:.haojs 于 Sep  3 08:25:34 修改本文.[FROM: bbs.hit.edu.cn]
--
※ 转寄:.武汉白云黄鹤站 bbs.whnet.edu.cn.[FROM: bbs.hit.edu.cn]

--
☆ 来源:.哈工大紫丁香 bbs.hit.edu.cn.[FROM: haojs.bbs@bbs.whnet.]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.859毫秒