home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 December
/
VPR9712A.ISO
/
OLS
/
OS2
/
LHA2P205
/
LHA2P205.LZH
/
lha2-2.05pre
/
source.lzh
/
src
/
crcio.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-10-15
|
4KB
|
218 lines
/*
* crcio.c --- input/output
* Copyright (C) 1988-1992, Haruyasu YOSHIZAKI
* Copyright (C) 1991-1995, Satoshi HIRAMATSU (OS/2 HPFS version)
*
* $Log$
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <io.h>
#include <time.h>
#include <limits.h>
#include "typedef.h"
#include "port2.h"
#include "lh.h"
#include "header.h"
#include "disp.h"
#include "slidehuf.h"
#include "intrface.h"
#include "errmes.h"
#define CRCPOLY 0xa001U /* CRC-16 */
#define UPDATE_CRC(c) \
crc = crctable[((uint)crc ^ (c)) & 0xFF] ^ ((uint)crc >> CHAR_BIT)
FILE *infile, *outfile;
ushort bitbuf;
int dispflg;
static ushort crctable[UCHAR_MAX + 1];
static uchar subbitbuf, bitcount;
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;
}
}
ushort
calccrc(uchar *p, ulong n, hword crc)
{
while(n-- > 0)
UPDATE_CRC(*p++);
return crc;
}
void
fillbuf(uchar n) /* Shift bitbuf n bits left, read n bits */
{
while(n > bitcount)
{
n -= bitcount;
bitbuf = (bitbuf << bitcount) + (subbitbuf >> (CHAR_BIT - bitcount));
if(compsize != 0)
{
compsize--;
subbitbuf = infile ? (uchar) getc(infile) : *use_ptr++;
}
else
subbitbuf = 0;
bitcount = CHAR_BIT;
}
bitcount -= n;
bitbuf = (bitbuf << n) + (subbitbuf >> (CHAR_BIT - n));
subbitbuf <<= n;
}
ushort
getbits(uchar n)
{
ushort x;
x = bitbuf >> (2 * CHAR_BIT - n);
fillbuf(n);
return x;
}
void
putcode(uchar n, ushort x) /* Write leftmost n bits of x */
{
while(n >= bitcount)
{
n -= bitcount;
subbitbuf += x >> (USHRT_BIT - bitcount);
x <<= bitcount;
if(compsize < origsize)
{
if(putc(subbitbuf, outfile) == EOF)
fileerror(WTERR, outfile);
compsize++;
}
else
unpackable = 1;
subbitbuf = 0;
bitcount = CHAR_BIT;
}
subbitbuf += x >> (USHRT_BIT - bitcount);
bitcount -= n;
}
void
putbits(uchar n, ushort x) /* Write rightmost n bits of x */
{
x <<= USHRT_BIT - n;
while(n >= bitcount)
{
n -= bitcount;
subbitbuf += x >> (USHRT_BIT - bitcount);
x <<= bitcount;
if(compsize < origsize)
{
if(putc(subbitbuf, outfile) == EOF)
fileerror(WTERR, outfile);
compsize++;
}
else
unpackable = 1;
subbitbuf = 0;
bitcount = CHAR_BIT;
}
subbitbuf += x >> (USHRT_BIT - bitcount);
bitcount -= n;
}
int
fread_crc(uchar *p, int n, FILE *f, hword *crc)
{
n = fread(p, 1, n, f);
*crc = calccrc(p, n, *crc);
if(!dispflg)
dispmark('o');
return n;
}
void
fwrite_crc(uchar *p, int n, FILE *f, hword *crc)
{
if(f)
if(fwrite(p, 1, n, f) < n && !isatty(fileno(f)))
fileerror(WTERR, f);
*crc = calccrc(p, n, *crc);
if(!dispflg)
dispmark('o');
}
void
init_getbits(void)
{
bitbuf = 0;
subbitbuf = 0;
bitcount = 0;
fillbuf(2 * CHAR_BIT);
}
void
init_putbits(void)
{
bitcount = CHAR_BIT;
subbitbuf = 0;
}
off_t
crcfread(fp, size, crc)
FILE *fp;
off_t size;
hword *crc;
{
byte *buffer;
off_t rsize = READ_MAX;
off_t lsize = size;
buffer = (byte *)e_malloc(READ_MAX);
do
{
if(lsize > rsize)
rsize = fread(buffer, 1, rsize, fp);
else
rsize = fread(buffer, 1, lsize, fp);
*crc = calccrc(buffer, rsize, *crc);
}
while(lsize -= rsize);
free(buffer);
return size;
}