⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 liteunzip.c

📁 是一个C语言的压缩和解压缩的DLL源程序.
💻 C
📖 第 1 页 / 共 5 页
字号:
	return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
}





/******************** inflate_trees_bits() ********************
 * c =		19 code lengths
 * bb =		Bits tree desired/actual depth
 * tb =		Bits tree result
 * hp =		Space for trees
 */

static int inflate_trees_bits(uInt *c, uInt *bb, INFLATE_HUFT * *tb, INFLATE_HUFT *hp, Z_STREAM * z)
{
	int		r;
	uInt	hn;			// hufts used in space 
	uInt	*v;
	
	// Allocate work area for huft_build 
	if (!(v = (uInt *)GlobalAlloc(GMEM_FIXED, 19 * sizeof(uInt))))
		return(Z_MEM_ERROR);

	hn = 0;
	r = huft_build(c, 19, 19, 0, 0, tb, bb, hp, &hn, v);
#ifndef NDEBUG
	if (r == Z_DATA_ERROR)
		z->msg = (char*)"oversubscribed dynamic bit lengths tree";
	else if (r == Z_BUF_ERROR || *bb == 0)
	{
		z->msg = (char*)"incomplete dynamic bit lengths tree";
		r = Z_DATA_ERROR;
	}
#else
	if (r == Z_BUF_ERROR || *bb == 0) r = Z_DATA_ERROR;
#endif

	GlobalFree(v);
	return(r);
}





static int inflate_trees_dynamic(
uInt nl,                // number of literal/length codes
uInt nd,                // number of distance codes
uInt *c,               // that many (total) code lengths
uInt *bl,              // literal desired/actual bit depth
uInt *bd,              // distance desired/actual bit depth
INFLATE_HUFT * *tl, // literal/length tree result
INFLATE_HUFT * *td, // distance tree result
INFLATE_HUFT *hp,       // space for trees
Z_STREAM * z)            // for messages
{
	int		r;
	uInt	hn;			// hufts used in space 
	uInt	*v;			// work area for huft_build 

	// Allocate work area 
	if (!(v = (uInt *)GlobalAlloc(GMEM_FIXED, 288 * sizeof(uInt))))
		return(Z_MEM_ERROR);

	// Build literal/length tree 
	hn = 0;
	r = huft_build(c, nl, 257, CpLens, CpLExt, tl, bl, hp, &hn, v);
	if (r != Z_OK || *bl == 0)
	{
#ifndef NDEBUG
		if (r == Z_DATA_ERROR)
			z->msg = (char *)"oversubscribed literal/length tree";
		else if (r != Z_MEM_ERROR)
		{
			z->msg = (char *)"incomplete literal/length tree";
			r = Z_DATA_ERROR;
		}
#else
		if (r == Z_MEM_ERROR) r = Z_DATA_ERROR;
#endif
		goto bad;
	}

	// Build distance tree 
	r = huft_build(c + nl, nd, 0, CpDist, CpDExt, td, bd, hp, &hn, v);
	if (r != Z_OK || (*bd == 0 && nl > 257))
	{
#ifndef NDEBUG
		if (r == Z_DATA_ERROR)
			z->msg = (char*)"oversubscribed distance tree";
		else if (r == Z_BUF_ERROR)
		{
			z->msg = (char*)"incomplete distance tree";
			r = Z_DATA_ERROR;
		}
		else if (r != Z_MEM_ERROR)
		{
			z->msg = (char*)"empty distance tree with lengths";
			r = Z_DATA_ERROR;
		}
#else
		if (r == Z_MEM_ERROR || r == Z_BUF_ERROR) r = Z_DATA_ERROR;
#endif
bad:	GlobalFree(v);
		return(r);
	}

	// done 
	GlobalFree(v);
	return(Z_OK);
}





// inffast.c -- process literals and length/distance pairs fast
// Copyright (C) 1995-1998 Mark Adler
// For conditions of distribution and use, see copyright notice in zlib.h
//




// macros for bit input with no checking and for returning unused bytes 
#define GRABBITS(j) {while(k<(j)){b|=((ULG)NEXTBYTE)<<k;k+=8;}}
#define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}

// Called with number of bytes left to write in window at least 258
// (the maximum string length) and number of input bytes available
// at least ten.  The ten bytes are six bytes for the longest length/
// distance pair plus four bytes for overloading the bit buffer. 

static int inflate_fast(uInt bl, uInt bd, const INFLATE_HUFT *tl, const INFLATE_HUFT *td, INFLATE_BLOCKS_STATE *s, Z_STREAM * z)
{
	const INFLATE_HUFT *t;      // temporary pointer 
	uInt e;               // extra bits or operation 
	ULG b;              // bit buffer 
	uInt k;               // bits in bit buffer 
	UCH *p;             // input data pointer 
	uInt n;               // bytes available there 
	UCH *q;             // output window write pointer 
	uInt m;               // bytes to end of window or read pointer 
	uInt ml;              // mask for literal/length tree
	uInt md;              // mask for distance tree 
	uInt c;               // bytes to copy 
	uInt d;               // distance back to copy from 
	UCH *r;             // copy source pointer 

	// load input, output, bit values 
	LOAD

	// initialize masks 
	ml = inflate_mask[bl];
	md = inflate_mask[bd];

	// do until not enough input or output space for fast loop 
	do 
	{		// assume called with m >= 258 && n >= 10 
		// get literal/length code 
		GRABBITS(20)                // max bits for literal/length code 
		if ((e = (t = tl + ((uInt)b & ml))->word.what.Exop) == 0)
		{
			DUMPBITS(t->word.what.Bits)
#ifndef NDEBUG
			LuTracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? "inflate:         * literal '%c'\n" : "inflate:         * literal 0x%02x\n", t->base));
#endif
			*q++ = (UCH)t->base;
			--m;
			continue;
		}

		for (;;)
		{
			DUMPBITS(t->word.what.Bits)
			if (e & 16)
			{
				// get extra bits for length 
				e &= 15;
				c = t->base + ((uInt)b & inflate_mask[e]);
				DUMPBITS(e)
#ifndef NDEBUG
				LuTracevv((stderr, "inflate:         * length %u\n", c));
#endif
				// decode distance base of block to copy 
				GRABBITS(15);           // max bits for distance code 
				e = (t = td + ((uInt)b & md))->word.what.Exop;
				for (;;)
				{
					DUMPBITS(t->word.what.Bits)
					if (e & 16)
					{
						// get extra bits to add to distance base 
						e &= 15;
						GRABBITS(e)         // get extra bits (up to 13) 
						d = t->base + ((uInt)b & inflate_mask[e]);
						DUMPBITS(e)
#ifndef NDEBUG
						LuTracevv((stderr, "inflate:         * distance %u\n", d));
#endif
						// do the copy						m -= c;						r = q - d;						if (r < s->window)                  // wrap if needed						{							do 
							{								r += s->end - s->window;        // force pointer in window							} while (r < s->window);          // covers invalid distances							e = (uInt) (s->end - r);							if (c > e)							{								c -= e;                         // wrapped copy								do 
								{									*q++ = *r++;								} while (--e);								r = s->window;
								do
								{									*q++ = *r++;								} while (--c);							}							else                              // normal copy							{								*q++ = *r++;  c--;								*q++ = *r++;  c--;								do 
								{									*q++ = *r++;								} while (--c);							}						}						else                                /* normal copy */						{							*q++ = *r++;  c--;							*q++ = *r++;  c--;							do
							{								*q++ = *r++;							} while (--c);						}						break;					}					else if ((e & 64) == 0)
					{
						t += t->base;
						e = (t += ((uInt)b & inflate_mask[e]))->word.what.Exop;
					}
					else
					{
#ifndef NDEBUG
						z->msg = (char*)"invalid distance code";
#endif
						UNGRAB
						UPDATE
						return Z_DATA_ERROR;
					}
				};

				break;
			}

			if ((e & 64) == 0)
			{
				t += t->base;
				if ((e = (t += ((uInt)b & inflate_mask[e]))->word.what.Exop) == 0)
				{
					DUMPBITS(t->word.what.Bits)
#ifndef NDEBUG
					LuTracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? "inflate:         * literal '%c'\n" : "inflate:         * literal 0x%02x\n", t->base));
#endif
					*q++ = (UCH)t->base;
					--m;
					break;
				}
			}
			else if (e & 32)
			{
#ifndef NDEBUG
				LuTracevv((stderr, "inflate:         * end of block\n"));
#endif
				UNGRAB
				UPDATE
				return Z_STREAM_END;
			}
			else
			{
#ifndef NDEBUG
				z->msg = (char*)"invalid literal/length code";
#endif
				UNGRAB
				UPDATE
				return Z_DATA_ERROR;
			}
		};
	} while (m >= 258 && n >= 10);

	// not enough input or output--restore pointers and return
	UNGRAB
	UPDATE
	return Z_OK;
}






/************************ ucrc32() ********************
 * Computes the CRC-32 of the bytes in the specified
 * buffer.
 *
 * crc =	Initial CRC-32 value.
 * buf =	Pointer to buffer containing bytes.
 * len =	Length of "buf" in bytes.
 *
 * RETURNS: The updated CRC-32.
 *
 * Copyright (C) 1995-1998 Mark Adler. For conditions of
 * distribution and use, see copyright notice in zlib.h
 */

#define CRC_DO1(buf) crc = Crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
#define CRC_DO2(buf)  CRC_DO1(buf); CRC_DO1(buf);
#define CRC_DO4(buf)  CRC_DO2(buf); CRC_DO2(buf);
#define CRC_DO8(buf)  CRC_DO4(buf); CRC_DO4(buf);

static ULG ucrc32(ULG crc, const UCH *buf, DWORD len)
{
	crc = crc ^ 0xffffffffL;
	while (len >= 8)
	{
		CRC_DO8(buf);
		len -= 8;
	}
	if (len)
	{
		do
		{
			CRC_DO1(buf);
		} while (--len);
	}
	return(crc ^ 0xffffffffL);
}




/************************ adler32() ******************
 * Computes the Adler-32 checksum of the bytes in the
 * specified buffer.
 *
 * crc =	Initial Adler-32 value.
 * buf =	Pointer to buffer containing bytes.
 * len =	Length of "buf" in bytes.
 *
 * RETURNS: The updated Adler-32.
 *
 * An Adler-32 checksum is almost as reliable as a CRC32
 * but can be computed much faster. Usage example:
 *
 * Copyright (C) 1995-1998 Mark Adler. For conditions of
 * distribution and use, see copyright notice in zlib.h
 */
/*
#define BASE 65521L // largest prime smaller than 65536
#define NMAX 5552
// NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1

#define AD_DO1(buf,i)  {s1 += buf[i]; s2 += s1;}
#define AD_DO2(buf,i)  AD_DO1(buf,i); AD_DO1(buf,i+1);
#define AD_DO4(buf,i)  AD_DO2(buf,i); AD_DO2(buf,i+2);
#define AD_DO8(buf,i)  AD_DO4(buf,i); AD_DO4(buf,i+4);
#define AD_DO16(buf)   AD_DO8(buf,0); AD_DO8(buf,8);

static ULG adler32(ULG adler, const UCH *buf, DWORD len)
{
	unsigned long s1 = adler & 0xffff;
	unsigned long s2 = (adler >> 16) & 0xffff;
	int k;

	while (len > 0)
	{
		k = len < NMAX ? len : NMAX;
		len -= k;
		while (k >= 16)
		{
            AD_DO16(buf);
			buf += 16;
			k -= 16;
		}
		if (k != 0) do
		{
			s1 += *buf++;
			s2 += s1;
		} while (--k);
		s1 %= BASE;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -