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

📄 peeperzip.cpp

📁 这是个soket的代码不是太实用但是对出学者还是有些帮助的 有需要的可以
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		return NULL;

//缓冲数据区大小
const int nMaxSize = 65536;

	HGLOBAL hUnZip = NULL;
	hUnZip = ::GlobalAlloc(GHND, nMaxSize);
	if(hUnZip == NULL)
	{
		return NULL;
	}
	LPBYTE lpUnZipData = (LPBYTE)::GlobalLock(hUnZip);
	BYTE byMaxBuf[nMaxSize];
	ZeroMemory(byMaxBuf, nMaxSize);

	int nCurPos = 0; //记下数据的位置

	int nPos = 0; //记下解压数据的位置
	int nBufPos = 0; //记下当前存储位置hUnZip
	int nSize = 0; //解压后数据的大小

	int  i, j, k, r, c;
	unsigned char cc; //add
	unsigned int  flags;
	for (i = 0; i < LZSS_N - LZSS_F; i++)
	{
		text_buf[i] = ' ';
	}
	r = LZSS_N - LZSS_F;
	flags = 0;
	for( ; ; )
	{
		if(((flags >>= 1) & 256) == 0)
		{
			if(nCurPos >= nZipSize)//(c = getc(infile)) == EOF)
			{
				break;
			}
			c = (unsigned char)chZipData[nCurPos]; //add
			nCurPos ++; //add
			
			flags = c | 0xff00;
		}
		if(flags & 1)
		{
			if(nCurPos >= nZipSize)//(c = getc(infile)) == EOF)
			{
				break;
			}
			c = (unsigned char)chZipData[nCurPos]; //add
			cc = c; //add
			nCurPos ++; //add

			//存储这个压缩字节
			if(nPos >= nMaxSize)
			{
				nPos = 0;
				memcpy(lpUnZipData + nBufPos, byMaxBuf, nMaxSize);
				nBufPos += nMaxSize;
				
				::GlobalUnlock(hUnZip); //重新分配内存
				hUnZip = ::GlobalReAlloc(hUnZip, nBufPos + nMaxSize, 0);
				lpUnZipData = (LPBYTE)::GlobalLock(hUnZip);
			}
			byMaxBuf[nPos] = cc;
			nPos ++;
			nSize ++; //当前压缩数据的大小

			text_buf[r++] = c;
			r &= (LZSS_N - 1);
		}
		else
		{
			if(nCurPos >= nZipSize)//(i = getc(infile)) == EOF)
			{
				break;
			}
			i = (unsigned char)chZipData[nCurPos]; //add
			nCurPos ++; //add

			if(nCurPos >= nZipSize)//(j = getc(infile)) == EOF)
			{
				break;
			}
			j = chZipData[nCurPos]; //add
			nCurPos ++; //add

			i |= ((j & 0xf0) << 4);
			j = (j & 0x0f) + LZSS_THRESHOLD;
			for(k = 0; k <= j; k++)
			{
				c = text_buf[(i + k) & (LZSS_N - 1)];
				cc = c; //add

				//存储这个压缩字节
				if(nPos >= nMaxSize)
				{
					nPos = 0;
					memcpy(lpUnZipData + nBufPos, byMaxBuf, nMaxSize);
					nBufPos += nMaxSize;
					
					::GlobalUnlock(hUnZip); //重新分配内存
					hUnZip = ::GlobalReAlloc(hUnZip, nBufPos + nMaxSize, 0);
					lpUnZipData = (LPBYTE)::GlobalLock(hUnZip);
				}
				byMaxBuf[nPos] = cc;
				nPos ++;
				nSize ++; //当前压缩数据的大小

				text_buf[r++] = c; 
				r &= (LZSS_N - 1);
			}
		}
	}

	//存储剩余的解压数据
	if(nPos > 0)
	{
		memcpy(lpUnZipData + nBufPos, byMaxBuf, nPos);
	}
	::GlobalUnlock(hUnZip); //重新分配内存
	hUnZip = ::GlobalReAlloc(hUnZip, nSize, 0);

	return hUnZip;
}
///////////////////////////////////////////////////////////////////////////////
//LZSS Part END--------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////
//ARI Part START--------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
HGLOBAL C_ARI::Encode(char *chData, int nSize)
{
	if(!AfxIsValidAddress(chData, nSize))
		return NULL;

	if(nSize <= 0)
		return NULL;

	m_nTotalSize = nSize;

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

	low = 0;
	high = ARI_Q4;
	value = 0;
	shifts = 0;

	nCurPos = 0; //记下数据的位置

	nZipPos = 0; //记下压缩数据的位置
	nBufPos = 0; //记下当前存储位置hZip
	nZipSize = 0; //压缩后数据的大小

	m_hData = NULL;
	m_lpMemData = NULL;
	m_lpBuffer = NULL;

	m_lpBuffer = (BYTE *)chData;
	m_hData = ::GlobalAlloc(GHND, MAXSIZE);
	if(m_hData == NULL)
	{
		return NULL;
	}
	m_lpMemData = (LPBYTE)::GlobalLock(m_hData);
	ZeroMemory(byMaxBuf, MAXSIZE);

	nCurPos = 0; //记下数据的位置

	nZipPos = 0; //记下压缩数据的位置
	nBufPos = 0; //记下当前存储位置hZip
	nZipSize = 0; //压缩后数据的大小

	int  i, c, len, r, s, last_match_length;
	
	textsize = nSize;
	int n1 = sizeof(textsize);
	memcpy(byMaxBuf, &textsize, sizeof(textsize));

	nZipSize += sizeof(textsize);
	nZipPos += sizeof(textsize);
/*	if(fwrite(&textsize, sizeof textsize, 1, outfile) < 1)
	{
		Error("Write Error");
	}
*/
	codesize += sizeof(textsize);
/*	if(textsize == 0)
	{
		return NULL;
	}
*/
//	rewind(infile);
	textsize = 0;
	StartModel(); 
	InitTree();
	s = 0;
	r = ARI_N - ARI_F;
	for(i = s; i < r; i++)
	{
		text_buf[i] = ' ';
	}
	for(len = 0; len < ARI_F && (nCurPos < nSize)/*c = getc(infile)) != EOF*/; len++)
	{
		c = m_lpBuffer[nCurPos]; //add
		nCurPos ++; //add
		text_buf[r + len] = c;
	}
	textsize = len;
	for(i = 1; i <= ARI_F; i++)
	{
		InsertNode(r - i);
	}
	InsertNode(r);
	do
	{
		if(match_length > len)
		{
			match_length = len;
		}
		if(match_length <= ARI_THRESHOLD)
		{
			match_length = 1; 
			EncodeChar(text_buf[r]);
		}
		else
		{
			EncodeChar(255 - ARI_THRESHOLD + match_length);
			EncodePosition(match_position - 1);
		}
		last_match_length = match_length;
		for(i = 0; i < last_match_length && (nCurPos < nSize)/*c = getc(infile)) != EOF*/; i++)
		{
			c = m_lpBuffer[nCurPos]; //add
			nCurPos ++; //add

			DeleteNode(s); 
			text_buf[s] = c;
			if(s < ARI_F - 1)
			{
				text_buf[s + ARI_N] = c;
			}
			s = (s + 1) & (ARI_N - 1);
			r = (r + 1) & (ARI_N - 1);
			InsertNode(r);
		}
		if((textsize += i) > printcount)
		{
			printcount += 1024;
		}
		while(i++ < last_match_length)
		{
			DeleteNode(s);
			s = (s + 1) & (ARI_N - 1);
			r = (r + 1) & (ARI_N - 1);
			if (--len) InsertNode(r);
		}
	} while (len > 0);
	EncodeEnd();
	//存储剩余的压缩数据
	if(nZipPos > 0)
	{
		memcpy(m_lpMemData + nBufPos, byMaxBuf, nZipPos);
	}
	::GlobalUnlock(m_hData); //重新分配内存
	m_hData = ::GlobalReAlloc(m_hData, nZipSize, 0);
	return m_hData;
}

HGLOBAL C_ARI::Decode(char *chData, int nSize)
{
	if(!AfxIsValidAddress(chData, nSize))
		return NULL;

	if(nSize <= 4)
		return NULL;

	m_nTotalSize = nSize;

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

	low = 0;
	high = ARI_Q4;
	value = 0;
	shifts = 0;

	nCurPos = 0; //记下数据的位置

	nZipPos = 0; //记下压缩数据的位置
	nBufPos = 0; //记下当前存储位置hZip
	nZipSize = 0; //压缩后数据的大小

	m_hData = NULL;
	m_lpMemData = NULL;
	m_lpBuffer = NULL;

	m_lpBuffer = (BYTE *)chData;
	m_hData = ::GlobalAlloc(GHND, MAXSIZE);
	if(m_hData == NULL)
	{
		return NULL;
	}
	m_lpMemData = (LPBYTE)::GlobalLock(m_hData);
	ZeroMemory(byMaxBuf, MAXSIZE);

	nCurPos = 0; //记下数据的位置

	nZipPos = 0; //记下压缩数据的位置
	nBufPos = 0; //记下当前存储位置hZip
	nZipSize = 0; //压缩后数据的大小

	int  i, j, k, r, c;
	unsigned long int  count;

	textsize = *(unsigned long int *)(m_lpBuffer);
/*	if(fread(&textsize, sizeof textsize, 1, infile) < 1)
	{
		Error("Read Error");
	}
*/
	nCurPos += sizeof(textsize);

	if(textsize == 0)
	{
		return NULL;
	}
	StartDecode();
	StartModel();
	for(i = 0; i < ARI_N - ARI_F; i++)
	{
		text_buf[i] = ' ';
	}
	r = ARI_N - ARI_F;
	for(count = 0; count < textsize; )
	{
		c = DecodeChar();
		if(c < 256)
		{
			//存储这个压缩字节
			if(nZipPos >= MAXSIZE)
			{
				nZipPos = 0;
				memcpy(m_lpMemData + nBufPos, byMaxBuf, MAXSIZE);
				nBufPos += MAXSIZE;
				
				::GlobalUnlock(m_hData); //重新分配内存
				m_hData = ::GlobalReAlloc(m_hData, nBufPos + MAXSIZE, 0);
				m_lpMemData = (LPBYTE)::GlobalLock(m_hData);
			}
			byMaxBuf[nZipPos] = c;
			nZipPos ++;
			nZipSize ++; //当前压缩数据的大小

			text_buf[r++] = c;
			r &= (ARI_N - 1);
			count++;
		} 
		else
		{
			i = (r - DecodePosition() - 1) & (ARI_N - 1);
			j = c - 255 + ARI_THRESHOLD;
			for(k = 0; k < j; k++)
			{
				c = text_buf[(i + k) & (ARI_N - 1)];
				//存储这个压缩字节
				if(nZipPos >= MAXSIZE)
				{
					nZipPos = 0;
					memcpy(m_lpMemData + nBufPos, byMaxBuf, MAXSIZE);
					nBufPos += MAXSIZE;
					
					::GlobalUnlock(m_hData); //重新分配内存
					m_hData = ::GlobalReAlloc(m_hData, nBufPos + MAXSIZE, 0);
					m_lpMemData = (LPBYTE)::GlobalLock(m_hData);
				}
				byMaxBuf[nZipPos] = c;
				nZipPos ++;
				nZipSize ++; //当前压缩数据的大小

				text_buf[r++] = c;
				r &= (ARI_N - 1);
				count++;
			}
		}
	}

	//存储剩余的压缩数据
	if(nZipPos > 0)
	{
		memcpy(m_lpMemData + nBufPos, byMaxBuf, nZipPos);
	}
	::GlobalUnlock(m_hData); //重新分配内存
	m_hData = ::GlobalReAlloc(m_hData, nZipSize, 0);
	return m_hData;
}

///////////////////////////////////////////////////////////////////////////////
//ARI Part END--------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////
//LZW Part START--------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////

CLZWDecodeTable::CLZWDecodeTable(BOOL fInit)
{
	m_pbContain=NULL;
	m_dwTableEntryNumber=0;
	if(fInit)
		InitLZWTable();
}
CLZWDecodeTable::~CLZWDecodeTable()
{
	ClearDecodeTable();
}
void CLZWDecodeTable::ClearDecodeTable(void)
{   
	if(m_pbContain==NULL)
		return;
	for(int i=0;i<4096;i++)
	{
		if(m_pbContain[i])
			delete (m_pbContain[i]);
	}
	delete m_pbContain;
	m_pbContain=NULL;
	m_dwTableEntryNumber=0;
}
void CLZWDecodeTable::InitLZWTable(void)
{
	ClearDecodeTable();
	m_pbContain=new BYTE*[4096];
	int iSize=sizeof(m_pbContain);
	//if(NULL==m_pbContain)
	//	AfxMessageBox("error new m_pbContain=BYTE*[4096]");
	for(int i=0;i<4096;i++)
	{
		m_pbContain[i]=NULL;
	}
	for(i=0;i<=255;i++)
	{
		m_pbContain[i]=new BYTE[1+2];
		*((WORD*)m_pbContain[i])=1;
		m_pbContain[i][2]=(BYTE)i;
	}
	m_dwTableEntryNumber=LZW_BEGIN_ENTRY;
}
BYTE* CLZWDecodeTable::GetMatchData(WORD wCode)
{
	return m_pbContain[wCode];
}
void CLZWDecodeTable::AddToChild(WORD wCode,BYTE *pbContain,int iLength)
{
	ASSERT(wCode<4096);
	if(m_pbContain[wCode])
		delete m_pbContain[wCode];
	m_pbContain[wCode]=new BYTE[iLength+2];
	*((WORD*)m_pbContain[wCode])=(WORD)iLength;
	memcpy(m_pbContain[wCode]+2,pbContain,iLength);
}

/*
class CLZWEncodeTable
*/
CLZWEncodeTable::CLZWEncodeTable(BOOL fInit)
{
	if(fInit)
		InitLZWTable();
	else
	{
		m_dwTableEntryNumber=0;
		m_EntryHead.pRightBrother=NULL;
		m_EntryHead.pChild=NULL;
	}
}
CLZWEncodeTable::~CLZWEncodeTable()
{
	ClearLZWTable();
}
void CLZWEncodeTable::ClearLZWTable(void)
{
	if(m_EntryHead.pChild==NULL)
		return;
	m_dwTableEntryNumber=0;
	int iRe=0;
	while(m_EntryHead.pChild)
	{
		RemoveFirstChild();
		iRe++;
		//printf("remove %d\n",++iRe);;
	}
}
void CLZWEncodeTable::RemoveFirstChild(void)
{
	PLZWENCODEENTRY pFirstChild=m_EntryHead.pChild;// this child will be removed
	if(pFirstChild->pChild)
	{
		m_EntryHead.pChild=pFirstChild->pChild;
		if(m_EntryHead.pChild->pRightBrother)
		{
			PLZWENCODEENTRY pRightBrother=FindRightBrother(m_EntryHead.pChild);
			pRightBrother->pRightBrother=pFirstChild->pRightBrother;
		}
		else
			(m_EntryHead.pChild)->pRightBrother=pFirstChild->pRightBrother;
		//delete pFirstChild;
	}
	else
		m_EntryHead.pChild=pFirstChild->pRightBrother;
	delete pFirstChild;
}
PLZWENCODEENTRY CLZWEncodeTable::FindRightBrother(PLZWENCODEENTRY pCurrent)
{
	PLZWENCODEENTRY pFind;
	pFind=pCurrent;
	while(pFind->pRightBrother)
	{
		pFind=pFind->pRightBrother;
	}
	return pFind;
}
void CLZWEncodeTable::InitLZWTable(void)
{// init the table ,it has 256 items code from 0 to 255
	ClearLZWTable();
	PLZWENCODEENTRY pEntryFirst=new LZWENCODEENTRY;
	pEntryFirst->wCode=(WORD)0;
	pEntryFirst->bLast=(BYTE)0;
	pEntryFirst->pRightBrother=pEntryFirst->pChild=NULL;
	m_EntryHead.pChild=pEntryFirst;
	m_EntryHead.pRightBrother=NULL;
	PLZWENCODEENTRY pPrev=pEntryFirst;
	for(int i=1;i<=255;i++)
	{// set the brother nodes
		PLZWENCODEENTRY pEntry=new LZWENCODEENTRY;
		pEntry->wCode=(WORD)i;
		pEntry->bLast=(BYTE)i;
		pEntry->pChild=pEntry->pRightBrother=NULL;
		pPrev->pRightBrother=pEntry;
		pPrev=pEntry;
	}
	m_dwTableEntryNumber=258;
	m_uNextCodeForUse=LZW_BEGIN_ENTRY;
}
PLZWENCODEENTRY CLZWEncodeTable::FindMatchChild(BYTE bChildLast,PLZWENCODEENTRY pCurrent)
{// return the find child entry
	if(pCurrent->pChild==NULL)
		return NULL;
	PLZWENCODEENTRY pChild=pCurrent->pChild;
	// pChild is the current's child
	// and all pChild's brother is the current's child
	while(pChild!=NULL)
	{
		if(pChild->bLast==bChildLast)
			return pChild;

⌨️ 快捷键说明

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