TO NoSwing: 都有什么?
我就是在把下面的C程序转换为Java程序的时候碰到问题的。谁能够把它转换为Java程序?
// Compress.h: interface for the CCompress class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_COMPRESS_H__20E933F4_C65C_11D1_9249_0080C88672DB__INCLUDED_)
#define AFX_COMPRESS_H__20E933F4_C65C_11D1_9249_0080C88672DB__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#define _LZH_N 4096
#define _LZH_F 60
#define _LZH_THRESHOLD 2
#define _LZH_NIL _LZH_N
#define _LZH_N_CHAR (256 - _LZH_THRESHOLD + _LZH_F)
#define _LZH_T (_LZH_N_CHAR * 2 - 1)
#define _LZH_R (_LZH_T - 1)
#define _LZH_MAX_FREQ 0x8000
class CCompress
{
public:
char * m_pcIn;
long m_lIn;
char * m_pcOut;
long m_lOut;
BOOL Encode();
BOOL Decode();
CCompress();
CCompress(int nLength);
virtual ~CCompress();
private:
long m_lInCount;
long m_lOutCount;
BYTE m_pbyBuffer[_LZH_N + _LZH_F - 1];
short m_nMatchPos;
short m_nMatchLen;
short m_pnLeftSon[_LZH_N + 1];
short m_pnRightSon[_LZH_N + 257];
short m_pnDad[_LZH_N + 1];
WORD m_pwFreq[_LZH_T + 1];
short m_pnParent[_LZH_T + _LZH_N_CHAR];
short m_pnSon[_LZH_T];
WORD m_wGetBuf;
BYTE m_byGetLen;
WORD m_wPutBuf;
BYTE m_byPutLen;
WORD m_wCode;
WORD m_wLen;
void StartHuff();
short DecodeChar();
short DecodePosition();
short GetBit();
short GetByte();
void Update(short);
void ReConstruct();
void InitTree();
void InsertNode(short);
void DeleteNode(short);
void EncodeChar(WORD);
void EncodePosition(WORD);
void EncodeEnd();
void PutCode(short, WORD);
};
#endif // !defined(AFX_COMPRESS_H__20E933F4_C65C_11D1_9249_0080C88672DB__INCLUDED_)
//=============================================================================
// Compress.cpp: implementation of the CCompress class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Compress.h"
#include <windowsx.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
unsigned char p_len[64] = {
0x03, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08
};
unsigned char p_code[64] = {
0x00, 0x20, 0x30, 0x40, 0x50, 0x58, 0x60, 0x68,
0x70, 0x78, 0x80, 0x88, 0x90, 0x94, 0x98, 0x9C,
0xA0, 0xA4, 0xA8, 0xAC, 0xB0, 0xB4, 0xB8, 0xBC,
0xC0, 0xC2, 0xC4, 0xC6, 0xC8, 0xCA, 0xCC, 0xCE,
0xD0, 0xD2, 0xD4, 0xD6, 0xD8, 0xDA, 0xDC, 0xDE,
0xE0, 0xE2, 0xE4, 0xE6, 0xE8, 0xEA, 0xEC, 0xEE,
0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
};
unsigned char d_code[256] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D,
0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F,
0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11,
0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13,
0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15,
0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,
0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B,
0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F,
0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23,
0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27,
0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B,
0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
};
unsigned char d_len[256] = {
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCompress::CCompress(int nLength)
{
m_wGetBuf = 0;
m_byGetLen = 0;
m_wPutBuf = 0;
m_byPutLen = 0;
m_pcIn = (char *)GlobalAllocPtr(GHND, nLength);
m_lIn = 0;
m_pcOut = (char *)GlobalAllocPtr(GHND, nLength);
m_lOut = 0;
}
CCompress::CCompress()
{
m_wGetBuf = 0;
m_byGetLen = 0;
m_wPutBuf = 0;
m_byPutLen = 0;
m_pcIn = (char *)GlobalAllocPtr(GHND, 128l*1024l);
m_lIn = 0;
m_pcOut = (char *)GlobalAllocPtr(GHND, 128l*1024l);
m_lOut = 0;
}
CCompress::~CCompress()
{
if (m_pcIn)
GlobalFreePtr(m_pcIn);
if (m_pcOut)
GlobalFreePtr(m_pcOut);//hwg???
}
BOOL CCompress:
ecode()
{
long lSize;
memmove(&lSize, m_pcIn, 4);
if (lSize == 0)
return FALSE;
m_lInCount = 4;
m_lOutCount = 0;
m_wGetBuf = 0;
m_byGetLen = 0;
StartHuff();
short nR = _LZH_N - _LZH_F;
memset(m_pbyBuffer, ' ', nR);
for (m_lOutCount = 0;
m_lOutCount < lSize;
)
{
short nC = DecodeChar();
if (nC < 256)
{
m_pcOut[m_lOutCount++] = (char)nC;
m_pbyBuffer[nR++] = (char)nC;
nR &= (_LZH_N - 1);
}
else
{
int i = (nR - DecodePosition() - 1) &
(_LZH_N - 1);
int j = nC - 255 + _LZH_THRESHOLD;
for (int k = 0;
k < j;
k++)
{
nC = m_pbyBuffer[(i + k) &
(_LZH_N - 1)];
m_pcOut[m_lOutCount++] = (char)nC;
m_pbyBuffer[nR++] = (char)nC;
nR &= (_LZH_N - 1);
}
}
}
m_lOut = m_lOutCount;
return TRUE;
}
void CCompress::StartHuff()
{
for (int i = 0;
i < _LZH_N_CHAR;
i++)
{
m_pwFreq
= 1;
m_pnSon = i+_LZH_T;
m_pnParent[i+_LZH_T] = i;
}
i = 0;
int j = _LZH_N_CHAR;
while (j <= _LZH_R)
{
m_pwFreq[j] = m_pwFreq + m_pwFreq[i+1];
m_pnSon[j] = i;
m_pnParent = m_pnParent[i+1] = j;
i += 2;
j++;
}
m_pwFreq[_LZH_T] = 0xffff;
m_pnParent[_LZH_R] = 0;
}
short CCompress:ecodeChar()
{
WORD c = m_pnSon[_LZH_R];
while (c < _LZH_T)
{
c += GetBit();
c = m_pnSon[c];
}
c -= _LZH_T;
Update(c);
return c;
}
short CCompress:ecodePosition()
{
WORD i = GetByte();
WORD c = (WORD)d_code << 6;
WORD j = d_len - 2;
while (j--)
i = (i << 1) + GetBit();
return c | i &
0x3f;
}
short CCompress::GetBit()
{
short i;
while (m_byGetLen <= 8)
{
if (m_lInCount < m_lIn)
i = (BYTE)m_pcIn[m_lInCount++];
else
i = 0;
m_wGetBuf |= i << (8 - m_byGetLen);
m_byGetLen += 8;
}
i = m_wGetBuf;
m_wGetBuf <<= 1;
m_byGetLen -= 1;
return (i < 0);
}
short CCompress::GetByte()
{
WORD i;
while (m_byGetLen <= 8)
{
if (m_lInCount < m_lIn)
i = (BYTE)m_pcIn[m_lInCount++];
else
i = 0;
m_wGetBuf |= i << (8 - m_byGetLen);
m_byGetLen += 8;
}
i = m_wGetBuf;
m_wGetBuf <<= 8;
m_byGetLen -= 8;
return i >> 8;
}
void CCompress::Update(short c)
{
short i, j, k, l;
if (m_pwFreq[_LZH_R] == _LZH_MAX_FREQ)
ReConstruct();
c = m_pnParent[c + _LZH_T];
do
{
k = ++m_pwFreq[c];
if (k > m_pwFreq[l = c + 1])
{
while (k > m_pwFreq[++l])
;
l--;
m_pwFreq[c] = m_pwFreq[l];
m_pwFreq[l] = k;
i = m_pnSon[c];
m_pnParent = l;
if (i < _LZH_T)
m_pnParent[i + 1] = l;
j = m_pnSon[l];
m_pnSon[l] = i;
m_pnParent[j] = c;
if (j < _LZH_T)
m_pnParent[j + 1] = c;
m_pnSon[c] = j;
c = l;
}
} while ((c = m_pnParent[c]) != 0); /*do
it until reaching the root */
}
void CCompress::ReConstruct()
{
short i, j, k;
WORD f, l;
j = 0;
for (i = 0;
i < _LZH_T;
i++)
{
if (m_pnSon >= _LZH_T)
{
m_pwFreq[j] = (m_pwFreq + 1) / 2;
m_pnSon[j] = m_pnSon;
j++;
}
}
for (i = 0, j = _LZH_N_CHAR;
j < _LZH_T;
i += 2, j++)
{
k = i + 1;
f = m_pwFreq[j] = m_pwFreq + m_pwFreq[k];
for (k = j - 1;
f < m_pwFreq[k];
k--)
;
k++;
l = (j - k) * 2;
memmove(&m_pwFreq[k + 1], &m_pwFreq[k], l);
m_pwFreq[k] = f;
memmove(&m_pnSon[k + 1], &m_pnSon[k], l);
m_pnSon[k] = i;
}
for (i = 0;
i < _LZH_T;
i++)
{
if ((k = m_pnSon) >= _LZH_T)
m_pnParent[k] = i;
else
m_pnParent[k] = m_pnParent[k + 1] = i;
}
}
BOOL CCompress::Encode()
{
short i, c, len, r, s, last_match_length;
memmove(m_pcOut, &m_lIn, 4);
m_lInCount = 0;
m_lOutCount = 4;
m_wPutBuf = 0;
m_byPutLen = 0;
StartHuff();
InitTree();
s = 0;
r = _LZH_N - _LZH_F;
memset(m_pbyBuffer, ' ', r);
for (len = 0;
len < _LZH_F &&
m_lInCount < m_lIn;
len++)
m_pbyBuffer[r + len] = (BYTE)m_pcIn[m_lInCount++];
for (i = 1;
i <= _LZH_F;
i++)
InsertNode(r - i);
InsertNode(r);
do
{
if (m_nMatchLen > len)
m_nMatchLen = len;
if (m_nMatchLen <= _LZH_THRESHOLD)
{
m_nMatchLen = 1;
EncodeChar(m_pbyBuffer[r]);
}
else
{
EncodeChar(255 - _LZH_THRESHOLD + m_nMatchLen);
EncodePosition(m_nMatchPos);
}
last_match_length = m_nMatchLen;
for (i = 0;
i < last_match_length &&
m_lInCount < m_lIn;
i++)
{
DeleteNode(s);
c = (BYTE)m_pcIn[m_lInCount++];
m_pbyBuffer = (BYTE)c;
if (s < _LZH_F - 1)
m_pbyBuffer[s + _LZH_N] = (BYTE)c;
s = (s + 1) &
(_LZH_N - 1);
r = (r + 1) &
(_LZH_N - 1);
InsertNode(r);
}
while (i++ < last_match_length)
{
DeleteNode(s);
s = (s + 1) &
(_LZH_N - 1);
r = (r + 1) &
(_LZH_N - 1);
if (--len)
InsertNode(r);
}
} while (len > 0);
EncodeEnd();
m_lOut = m_lOutCount;
return TRUE;
}
void CCompress::InitTree()
{
for (int i = _LZH_N + 1;
i <= _LZH_N + 256;
i++)
m_pnRightSon = _LZH_NIL;
for (i = 0;
i < _LZH_N;
i++)
m_pnDad = _LZH_NIL;
}
void CCompress::InsertNode(short r)
{
short i, p, cmp;
BYTE *key;
WORD c;
cmp = 1;
key = &m_pbyBuffer[r];
p = _LZH_N + 1 + key[0];
m_pnRightSon[r] = m_pnLeftSon[r] = _LZH_NIL;
m_nMatchLen = 0;
for ( ;
;
)
{
if (cmp >= 0)
{
if (m_pnRightSon[p] != _LZH_NIL)
p = m_pnRightSon[p];
else
{
m_pnRightSon[p] = r;
m_pnDad[r] = p;
return;
}
}
else
{
if (m_pnLeftSon[p] != _LZH_NIL)
p = m_pnLeftSon[p];
else
{
m_pnLeftSon[p] = r;
m_pnDad[r] = p;
return;
}
}
for (i = 1;
i < _LZH_F;
i++)
if ((cmp = key - m_pbyBuffer[p + i]) != 0)
break;
if (i > _LZH_THRESHOLD)
{
if (i > m_nMatchLen)
{
m_nMatchPos = ((r - p) &
(_LZH_N - 1)) - 1;
if ((m_nMatchLen = i) >= _LZH_F)
break;
}
if (i == m_nMatchLen)
{
if ((c = ((r - p) &
(_LZH_N - 1)) - 1) < m_nMatchPos)
m_nMatchPos = c;
}
}
}
m_pnDad[r] = m_pnDad[p];
m_pnLeftSon[r] = m_pnLeftSon[p];
m_pnRightSon[r] = m_pnRightSon[p];
m_pnDad[m_pnLeftSon[p]] = r;
m_pnDad[m_pnRightSon[p]] = r;
if (m_pnRightSon[m_pnDad[p]] == p)
m_pnRightSon[m_pnDad[p]] = r;
else
m_pnLeftSon[m_pnDad[p]] = r;
m_pnDad[p] = _LZH_NIL;
/* remove p */
}
void CCompress:eleteNode(short p)
{
short q;
if (m_pnDad[p] == _LZH_NIL)
return; /* unregistered */
if (m_pnRightSon[p] == _LZH_NIL)
q = m_pnLeftSon[p];
else
if (m_pnLeftSon[p] == _LZH_NIL)
q = m_pnRightSon[p];
else
{
q = m_pnLeftSon[p];
if (m_pnRightSon[q] != _LZH_NIL)
{
do
{
q = m_pnRightSon[q];
} while (m_pnRightSon[q] != _LZH_NIL);
m_pnRightSon[m_pnDad[q]] = m_pnLeftSon[q];
m_pnDad[m_pnLeftSon[q]] = m_pnDad[q];
m_pnLeftSon[q] = m_pnLeftSon[p];
m_pnDad[m_pnLeftSon[p]] = q;
}
m_pnRightSon[q] = m_pnRightSon[p];
m_pnDad[m_pnRightSon[p]] = q;
}
m_pnDad[q] = m_pnDad[p];
if (m_pnRightSon[m_pnDad[p]] == p)
m_pnRightSon[m_pnDad[p]] = q;
else
m_pnLeftSon[m_pnDad[p]] = q;
m_pnDad[p] = _LZH_NIL;
}
void CCompress::EncodeChar(WORD c)
{
WORD i;
short j, k;
i = 0;
j = 0;
k = m_pnParent[c + _LZH_T];
do
{
i >>= 1;
if (k &
1)
i += 0x8000;
j++;
} while ((k = m_pnParent[k]) != _LZH_R);
PutCode(j, i);
m_wCode = i;
m_wLen = j;
Update(c);
}
void CCompress::EncodePosition(WORD c)
{
WORD i;
i = c >> 6;
PutCode(p_len, (WORD)p_code << 8);
PutCode(6, (c &
0x3f) << 10);
}
void CCompress::EncodeEnd()
{
if (m_byPutLen)
m_pcOut[m_lOutCount++] = m_wPutBuf >> 8;
}
void CCompress:utCode(short l, WORD c)
{
m_wPutBuf |= c >> m_byPutLen;
if ((m_byPutLen += l) >= 8)
{
m_pcOut[m_lOutCount++] = m_wPutBuf >> 8;
if ((m_byPutLen -= 8) >= 8)
{
m_pcOut[m_lOutCount++] = (char)m_wPutBuf;
m_byPutLen -= 8;
m_wPutBuf = c << (l - m_byPutLen);
}
else
m_wPutBuf <<= 8;
}
}
void main()
{
CCompress *pMyComp;
pMyComp=new CCompress();
char TestBuf[256];
int i;
memset(TestBuf,0,256);
for(i=0;i<256;i++)
TestBuf='1';
memcpy(pMyComp->m_pcIn,TestBuf,256);
pMyComp->m_lIn=256;
pMyComp->Encode();
memcpy(pMyComp->m_pcIn,pMyComp->m_pcOut,pMyComp->m_lOut);
pMyComp->m_lIn=pMyComp->m_lOut;
pMyComp->Decode();
}