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

📄 zip.cpp

📁 zip解压源码.
💻 CPP
📖 第 1 页 / 共 4 页
字号:
#include "stdafx.h"
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>

#include "zipsha.h"
#include "revision.h"
#include "tailor.h"

#include "Zip.h"
#include "ZipDate.h"

//bits.cpp--------------------------------
typedef struct config {
   ush good_length; /* reduce lazy search above this match length */
   ush max_lazy;    /* do not perform lazy search above this match length */
   ush nice_length; /* quit search above this match length */
   ush max_chain;
} config;

local config configuration_table[10] = {
/*      good lazy nice chain */
/* 0 */ {0,    0,  0,    0},  /* store only */
/* 1 */ {4,    4,  8,    4},  /* maximum speed, no lazy matches */
/* 2 */ {4,    5, 16,    8},
/* 3 */ {4,    6, 32,   32},

/* 4 */ {4,    4, 16,   16},  /* lazy matches */
/* 5 */ {8,   16, 32,   32},
/* 6 */ {8,   16, 128, 128},
/* 7 */ {8,   32, 128, 256},
/* 8 */ {32, 128, 258, 1024},
/* 9 */ {32, 258, 258, 4096}}; /* maximum compression */


//////////////////////////////////////////////////
local int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
   = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};

local int extra_dbits[D_CODES] /* extra bits for each distance code */
   = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};

local int extra_blbits[BL_CODES]/* extra bits for each bit length code */
   = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};

extern ulg __crc_32_tab[];

//part1-----------------------------------------------
#define source1 zipdate-> source1 
#define dest1 zipdate-> dest1 
#define sourceLen1 zipdate-> sourceLen1 
#define destLen1 zipdate-> destLen1 
#define inSize zipdate-> inSize 
#define outSize zipdate-> outSize
#define errorNumber zipdate->errorNumber

#define crc zipdate-> crc 
#define static_crc zipdate-> static_crc 
#define header_bytes zipdate-> header_bytes 
#define verbose zipdate-> verbose 
#define level zipdate-> level 
#define bytes_in zipdate-> bytes_in 
#define bytes_out zipdate-> bytes_out
#define insize zipdate-> insize
#define inptr zipdate-> inptr
#define outcnt zipdate-> outcnt
//part2----------------------------------------
//bits
#define bi_buf zipdate-> bi_buf
#define bi_valid zipdate-> bi_valid
//tree
#define file_type zipdate-> file_type
#define bl_count zipdate-> bl_count
#define bl_order zipdate-> bl_order
#define heap zipdate-> heap
#define heap_len  zipdate-> heap_len 
#define heap_max zipdate-> heap_max
#define depth zipdate-> depth
#define length_code zipdate-> length_code
#define dist_code zipdate-> dist_code
#define base_length zipdate-> base_length
#define base_dist zipdate-> base_dist
#define flag_buf zipdate-> flag_buf
#define last_lit  zipdate-> last_lit 
#define last_dist zipdate-> last_dist
#define last_flags zipdate-> last_flags
#define flags zipdate-> flags
#define flag_bit zipdate-> flag_bit
#define opt_len zipdate-> opt_len 
#define static_len zipdate-> static_len
#define compressed_len zipdate-> compressed_len
#define input_len zipdate-> input_len
//part3-----------------------------------------------------
//deflate
#define block_start zipdate-> block_start
#define strstart zipdate-> strstart
#define window_size zipdate-> window_size
#define nice_match zipdate-> nice_match
#define match_start zipdate-> match_start
#define prev_length zipdate-> prev_length
#define max_chain_length zipdate-> max_chain_length
#define good_match zipdate-> good_match
#define ins_h zipdate-> ins_h
#define eofile zipdate-> eofile
#define lookahead zipdate-> lookahead
#define max_lazy_match zipdate-> max_lazy_match
//part4-----------------------------------------------------
#define dyn_ltree zipdate-> dyn_ltree
#define dyn_dtree zipdate-> dyn_dtree
#define static_ltree zipdate-> static_ltree
#define static_dtree zipdate-> static_dtree
#define bl_tree zipdate-> bl_tree
#define l_desc zipdate-> l_desc
#define d_desc zipdate-> d_desc
#define bl_desc zipdate-> bl_desc

#define inbuf zipdate-> inbuf
#define outbuf zipdate-> outbuf
#define d_buf zipdate-> d_buf
#define window zipdate-> window
#ifndef MAXSEG_64K
//	#define tab_prefix zipdate-> tab_prefix
	#define prev zipdate->prev
#else
	#define tab_prefix0 zipdate-> tab_prefix0
	#define prev zipdate->prev
	#define tab_prefix1 zipdate-> tab_prefix1
	#define head zipdate->tab_prefix1
#endif
//part5------------------------------------------
//gzip
extern DWORD ZipMain(ZipDate* zipdate);
extern int read_buf(ZipDate* zipdate,char *buf,  unsigned size);
extern ulg  updcrc(ZipDate* zipdate,uch *s, unsigned n);
extern void flush_outbuf(ZipDate* zipdate);
extern void write_buf(ZipDate* zipdate,voidp buf, unsigned cnt);
extern UINT Read(ZipDate* zipdate,char *buf,UINT size);
extern UINT Write(ZipDate* zipdate,char *buf,UINT size);

//deflate
extern void fill_window(ZipDate* zipdate);
extern int  longest_match(ZipDate* zipdate,IPos cur_match);
extern void lm_init(ZipDate* zipdate,int pack_level);
extern ulg  deflate(ZipDate* zipdate);

//tree------------------
//	
extern void ct_init(ZipDate* zipdate);
extern int  ct_tally(ZipDate* zipdate,int dist, int lc);
extern ulg  flush_block(ZipDate* zipdate,char *buf, ulg stored_len, int eof);
extern void init_block(ZipDate* zipdate);
extern void pqdownheap(ZipDate* zipdate,ct_data *tree, int k);
extern void gen_bitlen(ZipDate* zipdate,tree_desc *desc);
extern void gen_codes(ZipDate* zipdate,ct_data *tree, int max_code);
extern void build_tree(ZipDate* zipdate,tree_desc  *desc);
extern void scan_tree(ZipDate* zipdate,ct_data *tree, int max_code);
extern void send_tree(ZipDate* zipdate,ct_data *tree, int max_code);
extern int  build_bl_tree(ZipDate* zipdate);
extern void send_all_trees(ZipDate* zipdate,int lcodes, int dcodes, int blcodes);
extern void compress_block(ZipDate* zipdate,ct_data *ltree, ct_data *dtree);
extern void set_file_type(ZipDate* zipdate);
////////////////
//bits
extern void     bi_init(ZipDate* zipdate);
extern void     send_bits(ZipDate* zipdate,int value, int length);
extern unsigned bi_reverse(ZipDate* zipdate,unsigned value, int length);
extern void     bi_windup(ZipDate* zipdate);
extern void     copy_block(ZipDate* zipdate,char *buf, unsigned len, int header);

//part6------------------------------------------

void ZipDate_init(ZipDate* zipdate)
{
	tree_desc desc1={dyn_ltree, static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS, 0};
	l_desc =desc1;
	tree_desc desc2={dyn_dtree, static_dtree, extra_dbits, 0,          D_CODES, MAX_BITS, 0};
	d_desc =desc2;
	tree_desc desc3={bl_tree, (ct_data near *)0, extra_blbits, 0,      BL_CODES, MAX_BL_BITS, 0};
	bl_desc =desc3;
	uch bl_order1[BL_CODES]  = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
	for(int i=0;i<BL_CODES;i++)	bl_order[i]= bl_order1[i];

}

/* ======================================================================== */
DWORD ZipMain(ZipDate* zipdate)
{	
	inSize=outSize=0;
	errorNumber=0;
	
	ZipDate_init(zipdate);
	verbose = 0;
	window_size = (ulg)2*WSIZE;
	level = 6;
	file_type = 0;          /* ascii/binary flag */
    outcnt = 0;
	insize = inptr = 0;
	bytes_in = bytes_out = 0L;
	static_crc=(ulg)0xffffffffL;

	/* Write the header to the gzip file. See algorithm.doc for the format */
	put_byte(GZIP_MAGIC[0]); /* magic header */
	put_byte(GZIP_MAGIC[1]);
	put_byte(DEFLATED);      /* compression method */
	crc = updcrc( zipdate,0, 0);
	bi_init( zipdate);
	ct_init( zipdate);
	lm_init( zipdate,level);
	header_bytes = (long)outcnt;
	deflate( zipdate);
	put_long(crc);
	put_long(isize);
	header_bytes += 2*sizeof(long);
	flush_outbuf(zipdate);

	if(errorNumber!=0) return -2;

	if(destLen1<outSize)return 0;
	return DEFLATED;
}

/* ===========================================================================
 * Read a new buffer from the current input file, perform end-of-line
 * translation, and update the crc and input file size.
 * IN assertion: size >= 2 (for end-of-line translation)
 */
int read_buf(ZipDate* zipdate,char* buf, unsigned size)
{
    unsigned len;

	len = Read( zipdate, buf, size);

    if (len == (unsigned)(-1) || len == 0) return (int)len;

    crc = updcrc( zipdate,(uch*)buf, len);
    isize += (ulg)len;
    return (int)len;
}

/* ===========================================================================
 * Run a set of bytes through the crc shift register.  If s is a NULL
 * pointer, then initialize the crc shift register contents instead.
 * Return the current crc in either case.
 */
ulg updcrc(ZipDate* zipdate,	uch *	 s,                 /* pointer to bytes to pump through */
			unsigned n)             /* number of bytes in s[] */
{
    register ulg c;         /* temporary variable */

//    static ulg crc = (ulg)0xffffffffL; /* shift register contents */

    if (s == NULL) {
	c = 0xffffffffL;
    } else {
	c = static_crc;
        if (n) do {
            c = __crc_32_tab[((int)c ^ (*s++)) & 0xff] ^ (c >> 8);
        } while (--n);
    }
    static_crc = c;
    return c ^ 0xffffffffL;       /* (instead of ~c for 64-bit machines) */
}

/* ===========================================================================
 * Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
 * (used for the compressed data only)
 */
void flush_outbuf(ZipDate* zipdate)
{
    if (outcnt == 0) return;

    write_buf( zipdate,(char *)outbuf, outcnt);
    bytes_out += (ulg)outcnt;
    outcnt = 0;
}

/* ===========================================================================
 * Does the same as write(), but also handles partial pipe writes and checks for error return.
 */
void write_buf(ZipDate* zipdate,	voidp     buf,		unsigned  cnt)
{
    unsigned  n;
    n = Write( zipdate, buf, cnt);
	cnt -= n;
	buf = (voidp)((char*)buf+n);
}

UINT Read(ZipDate* zipdate,char *buf,UINT size)
{
	UINT i,size0;
	DWORD temp;
	char *p1;
	BYTE *p2;
	p1=buf;		p2=source1+inSize;


	if(sourceLen1>inSize)
	{	temp=sourceLen1-inSize;
		size0=size;
		if(temp<size)size0=temp;
		if(inSize+size0<inSize)
		{	errorNumber=1;
			return EOF;
		}

		i=0;
		while(i<size0)
		{	
			*p1++=*p2++;
			i++;
		}
		inSize+=size0;
		return size0;
	}
	else
		return EOF;
}

UINT Write(ZipDate* zipdate,char *buf,UINT size)
{
	UINT i,size0;
	DWORD temp;
	char *p1;
	BYTE *p2;
	p1=buf;		p2=dest1+outSize;

	if(outSize+size<outSize)
	{	errorNumber=2;
		return size;
	}

	if(destLen1>outSize)
	{
		temp=destLen1-outSize;
		size0=size;
		if(temp<size)	size0=temp;
		i=0;
		while(i<size0)
		{	*p2++=*p1++;
			i++;
		}
	}
	outSize+=size;
	return size;
}

//===============================================
//#include "Bits.h"
/* =========Initialize the bit string routines.===================*/
void bi_init (ZipDate* zipdate) /* output zip file, NO_FILE for in-memory compression */
{
    bi_buf = 0;
    bi_valid = 0;
}

/* =======================================*/
void send_bits(ZipDate* zipdate,	int value,  /* value to send */
				int length) /* number of bits */
{
    /* If not enough room in bi_buf, use (valid) bits from bi_buf and
     * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
     * unused bits in value.
     */
    if (bi_valid > (int)Buf_size - length) {

⌨️ 快捷键说明

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