E
eagle_l
Unregistered / Unconfirmed
GUEST, unregistred user!
以下是C++下面的解压部分代码,但小弟看不懂
int nextcode(INTCODE *codeptr, COMP_PARAMS *pparams)
/* Get the next code from input and put it in *codeptr.
* Return (TRUE) on success, or return (FALSE) on end-of-file.
* Adapted from COMPRESS V4.0.
*/
{
register INTCODE code;
//static int size;
//static UCHAR inbuf[MAXBITS];
register int shift;
UCHAR *bp;
/* If the next entry is a different bit-size than the preceeding one
* then we must adjust the size and scrap the old buffer.
*/
if (pparams->prevbits != pparams->bits) {
pparams->prevbits = pparams->bits;
pparams->size = 0;
}
/* If we can't read another code from the buffer, then refill it.
*/
if (pparams->size - (shift = pparams->offset) < pparams->bits) {
/* Read more input and convert size from # of bytes to # of bits */
if ((pparams->size = fread(pparams->inbuf, 1, pparams->bits, pparams->fp_zip) << 3) <= 0 || ferror(pparams->fp_zip))
return(NO);
pparams->offset = shift = 0;
}
/* Get to the first byte. */
bp = pparams->inbuf + (shift >> 3);
/* Get first part (low order bits) */
code = (*bp++ >> (shift &= 7));
/* high order bits. */
code |= *bp++ << (shift = 8 - shift);
if ((shift += 8) < pparams->bits) code |= *bp << shift;
*codeptr = code & pparams->highcode;
pparams->offset += pparams->bits;
return (TRUE);
}
int uncompress(COMP_PARAMS *pparams)
{
register int i;
register INTCODE code;
char sufxchar;
INTCODE savecode;
FLAG fulltable, cleartable;
static char *token= NULL; /* String buffer to build token */
static int maxtoklen = MAXTOKLEN;
exit_stat = OK;
/* Initialze the token buffer. */
if (token == NULL && (token = (char*)malloc(maxtoklen)) == NULL) {
exit_stat = NOMEM;
return NOMEM;
}
if (alloc_tables(pparams->maxcode = ~(~(INTCODE)0 << pparams->maxbits),0, pparams)) /* exit_stat already set */
return NOMEM;
/* if not zcat or filter */
if(is_list && !zcat_flg) { /* Open output file */
//if (freopen(ofname, WRITE_FILE_TYPE, pparams->fp_src) == NULL) {
// exit_stat = NOTOPENED;
//return NOTOPENED;
//}
//if (!quiet)
// fprintf(stderr, "%s: ",ifname);
//setvbuf(pparams->fp_src,xbuf,_IOFBF,XBUFSIZE);
}
cleartable = TRUE;
savecode = CLEAR;
pparams->offset = 0;
do {
if ((code = savecode) == CLEAR && cleartable) {
pparams->highcode = ~(~(INTCODE)0 << (pparams->bits = INITBITS));
fulltable = FALSE;
pparams->nextfree = (cleartable = pparams->block_compress) == FALSE ? 256 : FIRSTFREE;
if (!nextcode(&pparams->prefxcode, pparams))
break;
fputc((sufxchar = (char)pparams->prefxcode), pparams->fp_src);
continue;
}
i = 0;
if (code >= pparams->nextfree && !fulltable) {
if (code != pparams->nextfree){
exit_stat = CODEBAD;
return CODEBAD ; /* Non-existant code */
}
/* Special case for sequence KwKwK (see text of article) */
code = pparams->prefxcode;
token[i++] = sufxchar;
}
/* Build the token string in reverse order by chasing down through
* successive prefix tokens of the current token. Then output it.
*/
while (code >= 256) {
# ifdef DEBUG
/* These are checks to ease paranoia. Prefix codes must decrease
* monotonically, otherwise we must have corrupt tables. We can
* also check that we haven't overrun the token buffer.
*/
if (code <= (INTCODE)prefix(code)){
exit_stat= TABLEBAD;
return TABLEBAD;
}
# endif
if (i >= maxtoklen) {
maxtoklen *= 2; /* double the size of the token buffer */
if ((token = (char *)realloc(token, maxtoklen)) == NULL) {
exit_stat = TOKTOOBIG;
return TOKTOOBIG;
}
}
token[i++] = suffix(code);
code = (INTCODE)prefix(code);
}
fputc(sufxchar = (char)code, pparams->fp_src);
while (--i >= 0)
fputc(token, pparams->fp_src);
if (ferror(pparams->fp_src)) {
exit_stat = WRITEERR;
return WRITEERR;
}
/* If table isn't full, add new token code to the table with
* codeprefix and codesuffix, and remember current code.
*/
if (!fulltable) {
code = pparams->nextfree;
assert(256 <= code && code <= pparams->maxcode);
prefix(code) = (CODE)pparams->prefxcode;
suffix(code) = sufxchar;
pparams->prefxcode = savecode;
if (code++ == pparams->highcode) {
if (pparams->highcode >= pparams->maxcode) {
fulltable = TRUE;
--code;
}
else {
++pparams->bits;
pparams->highcode += code; /* pparams->nextfree == pparams->highcode + 1 */
}
}
pparams->nextfree = code;
}
} while (nextcode(&savecode, pparams));
exit_stat = (ferror(pparams->fp_zip))? READERR : OK;
return exit_stat;
}
int nextcode(INTCODE *codeptr, COMP_PARAMS *pparams)
/* Get the next code from input and put it in *codeptr.
* Return (TRUE) on success, or return (FALSE) on end-of-file.
* Adapted from COMPRESS V4.0.
*/
{
register INTCODE code;
//static int size;
//static UCHAR inbuf[MAXBITS];
register int shift;
UCHAR *bp;
/* If the next entry is a different bit-size than the preceeding one
* then we must adjust the size and scrap the old buffer.
*/
if (pparams->prevbits != pparams->bits) {
pparams->prevbits = pparams->bits;
pparams->size = 0;
}
/* If we can't read another code from the buffer, then refill it.
*/
if (pparams->size - (shift = pparams->offset) < pparams->bits) {
/* Read more input and convert size from # of bytes to # of bits */
if ((pparams->size = fread(pparams->inbuf, 1, pparams->bits, pparams->fp_zip) << 3) <= 0 || ferror(pparams->fp_zip))
return(NO);
pparams->offset = shift = 0;
}
/* Get to the first byte. */
bp = pparams->inbuf + (shift >> 3);
/* Get first part (low order bits) */
code = (*bp++ >> (shift &= 7));
/* high order bits. */
code |= *bp++ << (shift = 8 - shift);
if ((shift += 8) < pparams->bits) code |= *bp << shift;
*codeptr = code & pparams->highcode;
pparams->offset += pparams->bits;
return (TRUE);
}
int uncompress(COMP_PARAMS *pparams)
{
register int i;
register INTCODE code;
char sufxchar;
INTCODE savecode;
FLAG fulltable, cleartable;
static char *token= NULL; /* String buffer to build token */
static int maxtoklen = MAXTOKLEN;
exit_stat = OK;
/* Initialze the token buffer. */
if (token == NULL && (token = (char*)malloc(maxtoklen)) == NULL) {
exit_stat = NOMEM;
return NOMEM;
}
if (alloc_tables(pparams->maxcode = ~(~(INTCODE)0 << pparams->maxbits),0, pparams)) /* exit_stat already set */
return NOMEM;
/* if not zcat or filter */
if(is_list && !zcat_flg) { /* Open output file */
//if (freopen(ofname, WRITE_FILE_TYPE, pparams->fp_src) == NULL) {
// exit_stat = NOTOPENED;
//return NOTOPENED;
//}
//if (!quiet)
// fprintf(stderr, "%s: ",ifname);
//setvbuf(pparams->fp_src,xbuf,_IOFBF,XBUFSIZE);
}
cleartable = TRUE;
savecode = CLEAR;
pparams->offset = 0;
do {
if ((code = savecode) == CLEAR && cleartable) {
pparams->highcode = ~(~(INTCODE)0 << (pparams->bits = INITBITS));
fulltable = FALSE;
pparams->nextfree = (cleartable = pparams->block_compress) == FALSE ? 256 : FIRSTFREE;
if (!nextcode(&pparams->prefxcode, pparams))
break;
fputc((sufxchar = (char)pparams->prefxcode), pparams->fp_src);
continue;
}
i = 0;
if (code >= pparams->nextfree && !fulltable) {
if (code != pparams->nextfree){
exit_stat = CODEBAD;
return CODEBAD ; /* Non-existant code */
}
/* Special case for sequence KwKwK (see text of article) */
code = pparams->prefxcode;
token[i++] = sufxchar;
}
/* Build the token string in reverse order by chasing down through
* successive prefix tokens of the current token. Then output it.
*/
while (code >= 256) {
# ifdef DEBUG
/* These are checks to ease paranoia. Prefix codes must decrease
* monotonically, otherwise we must have corrupt tables. We can
* also check that we haven't overrun the token buffer.
*/
if (code <= (INTCODE)prefix(code)){
exit_stat= TABLEBAD;
return TABLEBAD;
}
# endif
if (i >= maxtoklen) {
maxtoklen *= 2; /* double the size of the token buffer */
if ((token = (char *)realloc(token, maxtoklen)) == NULL) {
exit_stat = TOKTOOBIG;
return TOKTOOBIG;
}
}
token[i++] = suffix(code);
code = (INTCODE)prefix(code);
}
fputc(sufxchar = (char)code, pparams->fp_src);
while (--i >= 0)
fputc(token, pparams->fp_src);
if (ferror(pparams->fp_src)) {
exit_stat = WRITEERR;
return WRITEERR;
}
/* If table isn't full, add new token code to the table with
* codeprefix and codesuffix, and remember current code.
*/
if (!fulltable) {
code = pparams->nextfree;
assert(256 <= code && code <= pparams->maxcode);
prefix(code) = (CODE)pparams->prefxcode;
suffix(code) = sufxchar;
pparams->prefxcode = savecode;
if (code++ == pparams->highcode) {
if (pparams->highcode >= pparams->maxcode) {
fulltable = TRUE;
--code;
}
else {
++pparams->bits;
pparams->highcode += code; /* pparams->nextfree == pparams->highcode + 1 */
}
}
pparams->nextfree = code;
}
} while (nextcode(&savecode, pparams));
exit_stat = (ferror(pparams->fp_zip))? READERR : OK;
return exit_stat;
}