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

📄 public.cpp

📁 一个C语言实现的压缩解压的工具代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"

#define N		 4096	/* size of ring buffer */
#define F		   60	/* upper limit for match_length */
#define THRESHOLD	2   /* encode string into position and length
						   if match_length is greater than this */
#define NIL			N	/* index for root of binary search trees */
/********** Arithmetic Compression **********/

/*  If you are not familiar with arithmetic compression, you should read
		I. E. Witten, R. M. Neal, and J. G. Cleary,
			Communications of the ACM, Vol. 30, pp. 520-540 (1987),
	from which much have been borrowed.  */

#define M   15

/*	Q1 (= 2 to the M) must be sufficiently large, but not so
	large as the unsigned long 4 * Q1 * (Q1 - 1) overflows.  */

#define Q1  (1UL << M)
#define Q2  (2 * Q1)
#define Q3  (3 * Q1)
#define Q4  (4 * Q1)
#define MAX_CUM (Q1 - 1)

#define N_CHAR  (256 - THRESHOLD + F)

unsigned long textsize;
unsigned long codesize;
unsigned long printcount;
unsigned char  text_buf[N + F - 1];	/* ring buffer of size N,with extra F-1 bytes to facilitate string comparison */
int	match_position;
int	match_length;  /* of longest match.  These areset by the InsertNode() procedure. */
int	lson[N + 1];
int rson[N + 257];
int dad[N + 1];  /* left & right children &parents -- These constitute binary search trees. */

/* character code = 0, 1, ..., N_CHAR - 1 */

unsigned long low;
unsigned long high;
unsigned long value;
int  shifts;  /* counts for magnifying low and high around Q2 */
int  char_to_sym[N_CHAR];
int sym_to_char[N_CHAR + 1];
unsigned int sym_freq[N_CHAR + 1];  /* frequency for symbols */
unsigned int sym_cum[N_CHAR + 1];   /* cumulative freq for symbols */
unsigned int position_cum[N + 1];   /* cumulative freq for positions */

int m_nOutLength;
//int m_nOutCur;

const BYTE *m_pInBuffer;
int m_nInLength;
int m_nInCur;

unsigned int  buffer_putbit, mask_putbit;
unsigned int  buffer_getbit, mask_getbit;

unsigned int  gui_SrcIndex, gui_ObjIndex;
unsigned int  gui_InSize;
unsigned char *gp_InBuff, *gp_OutBuff;
void LZARI_LZARI()
{
	gp_InBuff		= NULL;
	gp_OutBuff		= NULL;

	textsize		= 0;
	codesize		= 0;
	printcount		= 0;

	low				= 0;
	high			= Q4;
	value			= 0;
	shifts			= 0;			/* counts for magnifying low and high around Q2 */

	m_pInBuffer		= NULL;
	m_nInLength		= 0;
	m_nInCur		= 0;

	//m_pOutBuffer = NULL;
	m_nOutLength	= 0;
//	m_nOutCur = 0;

	buffer_putbit	= 0;
	mask_putbit		= 128;

	buffer_getbit	= 0;
	mask_getbit		= 0;

}

void LZARI_Release()
{
	gp_InBuff		= NULL;
	gp_OutBuff		= NULL;
	
	textsize		= 0;
	codesize		= 0;
	printcount		= 0;
	
	low				= 0;
	high			= Q4;
	value			= 0;
	shifts			= 0;
	
	m_pInBuffer		= NULL;
	m_nInLength		= 0;
	m_nInCur		= 0;

	m_nOutLength	= 0;

	buffer_putbit	= 0;
	mask_putbit		= 128;
	
	buffer_getbit	= 0;
	mask_getbit		= 0;
}

void LZARI_LZARITerm()
{
	LZARI_Release();
}

void LZARI_Error(char *message)
{
#ifdef _OUTPUT_STATUS
	printf("\n%s\n", message);
#endif
}

int LZARI_putc( unsigned int ui_Write, unsigned char* pOutBuff )
{
#if 0
	unsigned char cSwap;
	cSwap = ui_Write & 0xff;
	pOutBuff[gui_ObjIndex++] = cSwap;
	cSwap = ( ui_Write >> 8 ) & 0xff;
	pOutBuff[gui_ObjIndex++] = cSwap;
	cSwap = ( ui_Write >> 16 ) & 0xff;
	pOutBuff[gui_ObjIndex++] = cSwap;
	cSwap = ( ui_Write >> 24 ) & 0xff;
	pOutBuff[gui_ObjIndex++] = cSwap;
#endif

	pOutBuff[gui_ObjIndex++] = ui_Write;

	return 4;
}

int LZARI_getc( unsigned char* pInBuff )
{
	unsigned int ui_Return = 0;

	if( gui_SrcIndex >= gui_InSize )
	{
		return EOF;
	}

	ui_Return |= pInBuff[gui_SrcIndex ++];
	return ui_Return;
#if 0
	//ui_Return <<= 8;
	ui_Return |= pInBuff[gui_SrcIndex ++];

	if( gui_SrcIndex >= gui_InSize )
	{
		return ui_Return;
	}

	ui_Return <<= 8;
	ui_Return |= pInBuff[gui_SrcIndex ++];
	if( gui_SrcIndex >= gui_InSize )
	{
		return ui_Return;
	}
	ui_Return <<= 8;
	ui_Return |= pInBuff[gui_SrcIndex ++];

	if( gui_SrcIndex >= gui_InSize )
	{
		return ui_Return;
	}

	ui_Return <<= 8;
	ui_Return |= pInBuff[gui_SrcIndex ++];

	return ui_Return;
#endif
}

void LZARI_PutBit(int bit)  /* Output one bit (bit = 0,1) */
{
	if (bit)
		buffer_putbit |= mask_putbit;
	if ((mask_putbit >>= 1) == 0) 
	{
		{
			if (LZARI_putc(buffer_putbit, gp_OutBuff) == EOF) 
				LZARI_Error("Write Error");
		}

		buffer_putbit = 0;  
		mask_putbit = 128;  
		codesize++;
	}
}

void LZARI_FlushBitBuffer(void)  /* Send remaining bits */
{
	int  i;
	
	for (i = 0; i < 7; i++) 
		LZARI_PutBit(0);
}

int LZARI_GetBit(void)  /* Get one bit (0 or 1) */
{	
	if ((mask_getbit >>= 1) == 0) 
	{
		buffer_getbit = LZARI_getc( gp_InBuff );
		mask_getbit = 128;
	}
	return ((buffer_getbit & mask_getbit) != 0);
}

/********** LZSS with multiple binary trees **********/

void LZARI_InitTree(void)  /* Initialize trees */
{
	int  i;

	/* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
	   left children of node i.  These nodes need not be initialized.
	   Also, dad[i] is the parent of node i.  These are initialized to
	   NIL (= N), which stands for 'not used.'
	   For i = 0 to 255, rson[N + i + 1] is the root of the tree
	   for strings that begin with character i.  These are initialized
	   to NIL.  Note there are 256 trees. */

	for (i = N + 1; i <= N + 256; i++) 
		rson[i] = NIL;	/* root */
	for (i = 0; i < N; i++) 
		dad[i] = NIL;	/* node */
}

void LZARI_InsertNode(int r)
	/* Inserts string of length F, text_buf[r..r+F-1], into one of the
	   trees (text_buf[r]'th tree) and returns the longest-match position
	   and length via the global variables match_position and match_length.
	   If match_length = F, then removes the old node in favor of the new
	   one, because the old one will be deleted sooner.
	   Note r plays double role, as tree node and position in buffer. */
{
	int  i, p, cmp, temp;
	unsigned char  *key;

	cmp = 1;  key = &text_buf[r];  p = N + 1 + key[0];
	rson[r] = lson[r] = NIL;  match_length = 0;
	for ( ; ; ) 
	{
		if (cmp >= 0) 
		{
			if (rson[p] != NIL) p = rson[p];
			else {  rson[p] = r;  dad[r] = p;  return;  }
		} else 
		{
			if (lson[p] != NIL) p = lson[p];
			else {  lson[p] = r;  dad[r] = p;  return;  }
		}
		for (i = 1; i < F; i++)
			if ((cmp = key[i] - text_buf[p + i]) != 0)  break;
		if (i > THRESHOLD) 
		{
			if (i > match_length) 
			{
				match_position = (r - p) & (N - 1);
				if ((match_length = i) >= F) break;
			} else if (i == match_length) 
			{
				if ((temp = (r - p) & (N - 1)) < match_position)
					match_position = temp;
			}
		}
	}
	dad[r] = dad[p];  lson[r] = lson[p];  rson[r] = rson[p];
	dad[lson[p]] = r;  dad[rson[p]] = r;
	if (rson[dad[p]] == p) rson[dad[p]] = r;
	else                   lson[dad[p]] = r;
	dad[p] = NIL;  /* remove p */
}

void LZARI_DeleteNode(int p)  /* Delete node p from tree */
{
	int  q;
	
	if (dad[p] == NIL) return;  /* not in tree */
	if (rson[p] == NIL) q = lson[p];
	else if (lson[p] == NIL) q = rson[p];
	else 
	{
		q = lson[p];
		if (rson[q] != NIL) 
		{
			do {  q = rson[q];  } while (rson[q] != NIL);
			rson[dad[q]] = lson[q];  dad[lson[q]] = dad[q];
			lson[q] = lson[p];  dad[lson[p]] = q;
		}
		rson[q] = rson[p];  dad[rson[p]] = q;
	}
	dad[q] = dad[p];
	if (rson[dad[p]] == p) rson[dad[p]] = q;
	else                   lson[dad[p]] = q;
	dad[p] = NIL;
}

/********** Arithmetic Compression **********/

/*  If you are not familiar with arithmetic compression, you should read
		I. E. Witten, R. M. Neal, and J. G. Cleary,
			Communications of the ACM, Vol. 30, pp. 520-540 (1987),
	from which much have been borrowed.  */

	/* character code = 0, 1, ..., N_CHAR - 1 */


void LZARI_StartModel(void)  /* Initialize model */
{
	int ch, sym, i;
	
	sym_cum[N_CHAR] = 0;
	for (sym = N_CHAR; sym >= 1; sym--) 
	{
		ch = sym - 1;
		char_to_sym[ch] = sym;  sym_to_char[sym] = ch;
		sym_freq[sym] = 1;
		sym_cum[sym - 1] = sym_cum[sym] + sym_freq[sym];
	}
	sym_freq[0] = 0;  /* sentinel (!= sym_freq[1]) */
	position_cum[N] = 0;
	for (i = N; i >= 1; i--)
		position_cum[i - 1] = position_cum[i] + 10000 / (i + 200);
			/* empirical distribution function (quite tentative) */
			/* Please devise a better mechanism! */
}

void LZARI_UpdateModel(int sym)
{
	int i, c, ch_i, ch_sym;
	
	if (sym_cum[0] >= MAX_CUM) 
	{
		c = 0;
		for (i = N_CHAR; i > 0; i--) 
		{
			sym_cum[i] = c;
			c += (sym_freq[i] = (sym_freq[i] + 1) >> 1);
		}
		sym_cum[0] = c;
	}
	for (i = sym; sym_freq[i] == sym_freq[i - 1]; i--) ;
	if (i < sym) 
	{
		ch_i = sym_to_char[i];    ch_sym = sym_to_char[sym];
		sym_to_char[i] = ch_sym;  sym_to_char[sym] = ch_i;
		char_to_sym[ch_i] = sym;  char_to_sym[ch_sym] = i;
	}
	sym_freq[i]++;
	while (--i >= 0) sym_cum[i]++;

⌨️ 快捷键说明

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