?? peeperzip.cpp
字號:
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 + -