📄 ipp_zlib.c
字号:
{
IppLZ77State_8u* pLZ77State;
IppStatus status;
IppLZ77ComprLevel comprLevel;
static const char my_version[] = ZLIB_VERSION;
if ( version == Z_NULL || (stream_size != sizeof(z_stream)) ) {
return Z_VERSION_ERROR;
}
if (strm == Z_NULL) return Z_STREAM_ERROR;
strm->msg = Z_NULL;
if ( memLevel != DEF_MEM_LEVEL || method != Z_DEFLATED ||
windowBits != 15 || level < 1 || level > 9 || strategy != 0)
{
return Z_STREAM_ERROR;
}
if(level < 4)
comprLevel = IppLZ77FastCompr;
else if( level >= 4 && level < 8 )
comprLevel = IppLZ77AverageCompr;
else
comprLevel = IppLZ77BestCompr;
#ifdef GZIP
status = ippsEncodeLZ77InitAlloc_8u(comprLevel, IppLZ77CRC32, &pLZ77State);
#endif
#ifndef GZIP
status = ippsEncodeLZ77InitAlloc_8u(comprLevel, IppLZ77Adler32, &pLZ77State);
#endif
if (status != 0)
{
return Z_MEM_ERROR;
}
if (pLZ77State == Z_NULL) return Z_MEM_ERROR;
strm->state = pLZ77State;
return deflateReset(strm);
} /* deflateInit2_() */
/**********************************************************************************/
int
deflateEnd (z_stream* strm)
{
IppLZ77DeflateStatus deflateStatus;
if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
ippsEncodeLZ77GetStatus_8u(&deflateStatus, strm->state);
if(deflateStatus != IppLZ77StatusInit && deflateStatus != IppLZ77StatusLZ77Process &&
deflateStatus != IppLZ77StatusHuffProcess && deflateStatus != IppLZ77StatusFinal) {
return Z_STREAM_ERROR;
}
ippsLZ77Free_8u(strm->state);
return deflateStatus == IppLZ77StatusLZ77Process ? Z_DATA_ERROR : Z_OK;
} /* deflateEnd() */
/**********************************************************************************/
int
deflateReset (z_stream* strm)
{
if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
strm->total_in = strm->total_out = 0;
strm->msg = Z_NULL;
strm->data_type = Z_UNKNOWN;
strm->adler = 0;
if(ippsEncodeLZ77Reset_8u(strm->state) < 0)
return Z_STREAM_ERROR;
return Z_OK;
} /* deflateReset() */
/**********************************************************************************/
#ifdef NATIVE_INFLATE
/**********************************************************************************/
int
inflateInit_(z_stream* strm, const char *version, int stream_size)
{
return inflateInit2_(strm, MAX_WBITS, version, stream_size);
} /* inflateInit_() */
/**********************************************************************************/
int
inflateInit2_(z_stream* strm, int windowBits, const char *version, int stream_size)
{
struct inflate_state *state;
if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
stream_size != (int)(sizeof(z_stream)))
return Z_VERSION_ERROR;
if (strm == Z_NULL) return Z_STREAM_ERROR;
strm->msg = Z_NULL;
if (strm->zalloc == (alloc_func)0) {
strm->zalloc = (alloc_func)0;
strm->opaque = (void*)0;
}
if (strm->zfree == (free_func)0) strm->zfree = (free_func)0;
state = (struct inflate_state *) calloc(1, sizeof(struct inflate_state));
if (state == Z_NULL) return Z_MEM_ERROR;
strm->state = (void*)state;
if (windowBits < 0) {
state->wrap = 0;
windowBits = -windowBits;
}
else {
state->wrap = (windowBits >> 3) + 1;
#ifdef GZIP
if (windowBits < 48) windowBits &= 15;
#endif
}
if (windowBits < 8 || windowBits > 15) {
free(state);
strm->state = Z_NULL;
return Z_STREAM_ERROR;
}
state->wbits = (unsigned)windowBits;
state->window = Z_NULL;
return inflateReset(strm);
} /* inflateInit2_() */
/**********************************************************************************/
int
inflate(z_stream* strm, int flush)
{
struct inflate_state *state;
unsigned char *next; /* next input */
unsigned char *put; /* next output */
unsigned have, left; /* available input and output */
unsigned long hold; /* bit buffer */
unsigned bits; /* bits in bit buffer */
unsigned in, out; /* save starting available input and output */
unsigned copy; /* number of stored or match bytes to copy */
unsigned char FAR *from; /* where to copy match bytes from */
code this; /* current decoding table entry */
code last; /* parent table entry */
unsigned len; /* length to copy for repeats, bits to drop */
int ret; /* return code */
#ifdef GZIP
unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
#endif
static const unsigned short order[19] = /* permutation of code lengths */
{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
(strm->next_in == Z_NULL && strm->avail_in != 0))
return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
LOAD();
in = have;
out = left;
ret = Z_OK;
for (;;)
switch (state->mode) {
case HEAD:
if (state->wrap == 0) {
state->mode = TYPEDO;
break;
}
NEEDBITS(16);
#ifdef GZIP
if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
state->check = crc32(0L, Z_NULL, 0);
CRC2(state->check, hold);
INITBITS();
state->mode = FLAGS;
break;
}
state->flags = 0; /* expect zlib header */
if (!(state->wrap & 1) || /* check if zlib header allowed */
#else
if (
#endif
((BITS(8) << 8) + (hold >> 8)) % 31) {
strm->msg = (char *)"incorrect header check";
state->mode = BAD;
break;
}
if (BITS(4) != Z_DEFLATED) {
strm->msg = (char *)"unknown compression method";
state->mode = BAD;
break;
}
DROPBITS(4);
if (BITS(4) + 8 > state->wbits) {
strm->msg = (char *)"invalid window size";
state->mode = BAD;
break;
}
strm->adler = state->check = adler32(0L, Z_NULL, 0);
state->mode = hold & 0x200 ? DICTID : TYPE;
INITBITS();
break;
#ifdef GZIP
case FLAGS:
NEEDBITS(16);
state->flags = (int)(hold);
if ((state->flags & 0xff) != Z_DEFLATED) {
strm->msg = (char *)"unknown compression method";
state->mode = BAD;
break;
}
if (state->flags & 0xe000) {
strm->msg = (char *)"unknown header flags set";
state->mode = BAD;
break;
}
if (state->flags & 0x0200) CRC2(state->check, hold);
INITBITS();
state->mode = TIME;
case TIME:
NEEDBITS(32);
if (state->flags & 0x0200) CRC4(state->check, hold);
INITBITS();
state->mode = OS;
case OS:
NEEDBITS(16);
if (state->flags & 0x0200) CRC2(state->check, hold);
INITBITS();
state->mode = EXLEN;
case EXLEN:
if (state->flags & 0x0400) {
NEEDBITS(16);
state->length = (unsigned)(hold);
if (state->flags & 0x0200) CRC2(state->check, hold);
INITBITS();
}
state->mode = EXTRA;
case EXTRA:
if (state->flags & 0x0400) {
copy = state->length;
if (copy > have) copy = have;
if (copy) {
if (state->flags & 0x0200)
state->check = crc32(state->check, next, copy);
have -= copy;
next += copy;
state->length -= copy;
}
if (state->length) goto inf_leave;
}
state->mode = NAME;
case NAME:
if (state->flags & 0x0800) {
if (have == 0) goto inf_leave;
copy = 0;
do {
len = (unsigned)(next[copy++]);
} while (len && copy < have);
if (state->flags & 0x02000)
state->check = crc32(state->check, next, copy);
have -= copy;
next += copy;
if (len) goto inf_leave;
}
state->mode = ICOMMENT;
case ICOMMENT:
if (state->flags & 0x1000) {
if (have == 0) goto inf_leave;
copy = 0;
do {
len = (unsigned)(next[copy++]);
} while (len && copy < have);
if (state->flags & 0x02000)
state->check = crc32(state->check, next, copy);
have -= copy;
next += copy;
if (len) goto inf_leave;
}
state->mode = HCRC;
case HCRC:
if (state->flags & 0x0200) {
NEEDBITS(16);
if (hold != (state->check & 0xffff)) {
strm->msg = (char *)"header crc mismatch";
state->mode = BAD;
break;
}
INITBITS();
}
strm->adler = state->check = crc32(0L, Z_NULL, 0);
state->mode = TYPE;
break;
#endif
case DICTID:
NEEDBITS(32);
strm->adler = state->check = REVERSE(hold);
INITBITS();
state->mode = DICT;
case DICT:
if (state->havedict == 0) {
RESTORE();
return Z_NEED_DICT;
}
strm->adler = state->check = adler32(0L, Z_NULL, 0);
state->mode = TYPE;
case TYPE:
if (flush == Z_BLOCK) goto inf_leave;
case TYPEDO:
if (state->last) {
BYTEBITS();
state->mode = CHECK;
break;
}
NEEDBITS(3);
state->last = BITS(1);
DROPBITS(1);
switch (BITS(2)) {
case 0: /* stored block */
state->mode = STORED;
break;
case 1: /* fixed block */
fixedtables(state);
state->mode = LEN; /* decode codes */
break;
case 2: /* dynamic block */
state->mode = TABLE;
break;
case 3:
strm->msg = (char *)"invalid block type";
state->mode = BAD;
}
DROPBITS(2);
break;
case STORED:
BYTEBITS(); /* go to byte boundary */
NEEDBITS(32);
if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
strm->msg = (char *)"invalid stored block lengths";
state->mode = BAD;
break;
}
state->length = (unsigned)hold & 0xffff;
INITBITS();
state->mode = COPY;
case COPY:
copy = state->length;
if (copy) {
if (copy > have) copy = have;
if (copy > left) copy = left;
if (copy == 0) goto inf_leave;
zmemcpy(put, next, copy);
have -= copy;
next += copy;
left -= copy;
put += copy;
state->length -= copy;
break;
}
state->mode = TYPE;
break;
case TABLE:
NEEDBITS(14);
state->nlen = BITS(5) + 257;
DROPBITS(5);
state->ndist = BITS(5) + 1;
DROPBITS(5);
state->ncode = BITS(4) + 4;
DROPBITS(4);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -