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

📄 public.cpp

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

void LZARI_Output(int bit)  /* Output 1 bit, followed by its complements */
{
	LZARI_PutBit(bit);
	for ( ; shifts > 0; shifts--) 
		LZARI_PutBit(! bit);
}

void LZARI_EncodeChar(int ch)
{
	int  sym;
	unsigned long int  range;

	sym = char_to_sym[ch];
	range = high - low;
	high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
	low +=       (range * sym_cum[sym    ]) / sym_cum[0];
	for ( ; ; ) 
	{
		if (high <= Q2) 
			LZARI_Output(0);
		else if (low >= Q2) 
		{
			LZARI_Output(1);  
			low -= Q2;  
			high -= Q2;
		} 
		else if (low >= Q1 && high <= Q3) 
		{
			shifts++;  
			low -= Q1;  
			high -= Q1;
		} 
		else 
			break;
		low += low;
		high += high;
	}
	LZARI_UpdateModel(sym);
}

void LZARI_EncodePosition(int position)
{
	unsigned long int  range;

	range = high - low;
	high = low + (range * position_cum[position    ]) / position_cum[0];
	low +=       (range * position_cum[position + 1]) / position_cum[0];
	for ( ; ; ) 
	{
		if (high <= Q2) 
			LZARI_Output(0);
		else if (low >= Q2) 
		{
			LZARI_Output(1);  
			low -= Q2;  
			high -= Q2;
		} 
		else if (low >= Q1 && high <= Q3) 
		{
			shifts++;  
			low -= Q1;  
			high -= Q1;
		} 
		else 
			break;
		low += low;
		high += high;
	}
}

void LZARI_EncodeEnd(void)
{
	shifts++;
	if (low < Q1) 
		LZARI_Output(0);  
	else 
		LZARI_Output(1);
	LZARI_FlushBitBuffer();  /* flush bits remaining in buffer */
}

int LZARI_BinarySearchSym(unsigned int x)
	/* 1      if x >= sym_cum[1],
	   N_CHAR if sym_cum[N_CHAR] > x,
	   i such that sym_cum[i - 1] > x >= sym_cum[i] otherwise */
{
	int i, j, k;
	
	i = 1;  j = N_CHAR;
	while (i < j) 
	{
		k = (i + j) / 2;
		if (sym_cum[k] > x) i = k + 1;  else j = k;
	}
	return i;
}

int LZARI_BinarySearchPos(unsigned int x)
	/* 0 if x >= position_cum[1],
	   N - 1 if position_cum[N] > x,
	   i such that position_cum[i] > x >= position_cum[i + 1] otherwise */
{
	int i, j, k;
	
	i = 1;  j = N;
	while (i < j)
	{
		k = (i + j) / 2;
		if (position_cum[k] > x) i = k + 1;  else j = k;
	}
	return i - 1;
}

void LZARI_StartDecode(void)
{
	int i;

	for (i = 0; i < M + 2; i++)
		value = 2 * value + LZARI_GetBit();
}

int LZARI_DecodeChar(void)
{
	int	 sym, ch;
	unsigned long int  range;
	
	range = high - low;
	sym = LZARI_BinarySearchSym((unsigned int)
		(((value - low + 1) * sym_cum[0] - 1) / range));
	high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
	low +=       (range * sym_cum[sym    ]) / sym_cum[0];
	for ( ; ; ) {
		if (low >= Q2) {
			value -= Q2;  low -= Q2;  high -= Q2;
		} else if (low >= Q1 && high <= Q3) {
			value -= Q1;  low -= Q1;  high -= Q1;
		} else if (high > Q2) break;
		low += low;  high += high;
		value = 2 * value + LZARI_GetBit();
	}
	ch = sym_to_char[sym];
	LZARI_UpdateModel(sym);
	return ch;
}

int LZARI_DecodePosition(void)
{
	int position;
	unsigned long int  range;
	
	range = high - low;
	position = LZARI_BinarySearchPos((unsigned int)
		(((value - low + 1) * position_cum[0] - 1) / range));
	high = low + (range * position_cum[position    ]) / position_cum[0];
	low +=       (range * position_cum[position + 1]) / position_cum[0];
	for ( ; ; ) {
		if (low >= Q2) {
			value -= Q2;  low -= Q2;  high -= Q2;
		} else if (low >= Q1 && high <= Q3) {
			value -= Q1;  low -= Q1;  high -= Q1;
		} else if (high > Q2) break;
		low += low;  high += high;
		value = 2 * value + LZARI_GetBit();
	}
	return position;
}

/********** Encode and Decode **********/

void LZARI_Encode(void)
{
	int  i, c, len, r, s, last_match_length;
	unsigned char cSwap;

	//fseek(infile, 0L, SEEK_END);
	textsize = gui_InSize;//ftell(infile);
#if 0
	if (fwrite(&textsize, sizeof textsize, 1, outfile) < 1)
		LZARI_Error("Write Error");  /* output size of text */
#else
	//LZARI_putc( textsize, gp_OutBuff );
	cSwap = textsize & 0xff;
	gp_OutBuff[gui_ObjIndex++] = cSwap;
	cSwap = ( textsize >> 8 ) & 0xff;
	gp_OutBuff[gui_ObjIndex++] = cSwap;
	cSwap = ( textsize >> 16 ) & 0xff;
	gp_OutBuff[gui_ObjIndex++] = cSwap;
	cSwap = ( textsize >> 24 ) & 0xff;
	gp_OutBuff[gui_ObjIndex++] = cSwap;
#endif
	codesize += sizeof textsize;
	if (textsize == 0) return;
	gui_SrcIndex = 0;//rewind(infile);
	textsize = 0;

	LZARI_StartModel(); 
	LZARI_InitTree();
	s = 0;  r = N - F;
	for (i = s; i < r; i++) 
		text_buf[i] = ' ';

	for (len = 0; len < F && (c = LZARI_getc(gp_InBuff)) != EOF; len++) 
		text_buf[r + len] = c;

	textsize = len;
	for (i = 1; i <= F; i++) 
		LZARI_InsertNode(r - i);
	
	LZARI_InsertNode(r);
	
	do {
		if (match_length > len) match_length = len;
		if (match_length <= THRESHOLD) 
		{
			match_length = 1;  
			LZARI_EncodeChar(text_buf[r]);
		} 
		else
		{
			LZARI_EncodeChar(255 - THRESHOLD + match_length);
			LZARI_EncodePosition(match_position - 1);
		}
		last_match_length = match_length;

		for (i = 0; i < last_match_length && (c = LZARI_getc(gp_InBuff)) != EOF; i++)
		{
			LZARI_DeleteNode(s);
			text_buf[s] = c;
			if (s < F - 1) 
				text_buf[s + N] = c;
			s = (s + 1) & (N - 1);
			r = (r + 1) & (N - 1);
			LZARI_InsertNode(r);
		}

		if ((textsize += i) > printcount) 
		{
#ifdef _OUTPUT_STATUS
			printf("%12ld\r", textsize);
#endif	
			printcount += 1024;
		}
		while (i++ < last_match_length) 
		{
			LZARI_DeleteNode(s);
			s = (s + 1) & (N - 1);
			r = (r + 1) & (N - 1);
			if (--len) 
				LZARI_InsertNode(r);
		}
	} while (len > 0);
	
	LZARI_EncodeEnd();

#ifdef _OUTPUT_STATUS	
	printf("In : %lu bytes\n", textsize);
	printf("Out: %lu bytes\n", codesize);
	printf("Out/In: %.3f\n", (double)codesize / textsize);
#endif
}

void LZARI_Decode(void)
{
	int  i, j, k, r, c;
	unsigned long int  count;
#if 0
	if ( fread(&textsize, sizeof textsize, 1, infile ) < 1 )
		LZARI_Error("Read Error");  /* read size of text */
#else
	textsize = c = gp_InBuff[0] | gp_InBuff[1] << 8 | gp_InBuff[2] << 16 | gp_InBuff[3] << 24;

	gui_SrcIndex += 4;

	if( c < 1 )
		return;
#endif
	
	if (textsize == 0) 
		return;
	
	LZARI_StartDecode();
	LZARI_StartModel();
	
	for (i = 0; i < N - F; i++) text_buf[i] = ' ';
	r = N - F;
	for (count = 0; count < textsize; ) 
	{
		c = LZARI_DecodeChar();
		if (c < 256) 
		{
			LZARI_putc(c, gp_OutBuff);
			text_buf[r++] = c;
			r &= (N - 1);
			count++;
		}
		else
		{
			i = (r - LZARI_DecodePosition() - 1) & (N - 1);
			j = c - 255 + THRESHOLD;
			for (k = 0; k < j; k++) 
			{
				c = text_buf[(i + k) & (N - 1)];

				LZARI_putc(c, gp_OutBuff);

				text_buf[r++] = c;
				r &= (N - 1);
				count++;
			}
		}
		if (count > printcount) 
		{
#ifdef _OUTPUT_STATUS
			printf("%12lu\r", count);
#endif			
			printcount += 1024;
		}
	}

#ifdef _OUTPUT_STATUS
	printf("%12lu\n", count);
#endif
}

void LZARI_Compress( unsigned char* pInBuffer,			// 入口缓冲指针
					 unsigned int ui_InSize,			// 入口文件大小
					 unsigned char* pOutBuffer,			// 出口缓冲指针
					 unsigned int *pi_OutSize			// 出口文件大小
					)
{
	LZARI_LZARI();

	if( pInBuffer && pOutBuffer && ui_InSize && pi_OutSize )
	{
		*pi_OutSize		= 0;
		gui_SrcIndex	= 0;
		gui_ObjIndex	= 0;
		gui_InSize		= ui_InSize;
		gp_InBuff		= pInBuffer;
		gp_OutBuff		= pOutBuffer;
		LZARI_Encode();
		*pi_OutSize		= gui_ObjIndex;
	}
	LZARI_LZARITerm();
}

void LZARI_UnCompress( unsigned char* pInBuffer,			// 入口缓冲指针
					 unsigned int ui_InSize,			// 入口文件大小
					 unsigned char* pOutBuffer,			// 出口缓冲指针
					 unsigned int *pi_OutSize			// 出口文件大小
					)
{
	LZARI_LZARI();
	if( pInBuffer && pOutBuffer && ui_InSize && pi_OutSize )
	{
		gp_InBuff		= pInBuffer;
		gp_OutBuff		= pOutBuffer;
		gui_InSize		= ui_InSize;
		*pi_OutSize		= 0;
		gui_SrcIndex	= 0;
		gui_ObjIndex	= 0;
		LZARI_Decode();
		*pi_OutSize		= gui_ObjIndex;
	}
	LZARI_LZARITerm();
}

⌨️ 快捷键说明

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