关于C++类库KYLib: 固定缓冲区的压缩/解压缩类源码(四)

2014-11-24 08:13:32 · 作者: · 浏览: 2
PCode pCodes, pEnd;
Byte byteNo, byteBits, byteLength, byteBegin, byteva lue;
// 循环添加静态编码表
for (byteNo = 0, pCodes = ACodes; byteNo <= AHigh; byteNo++)
{
pEnd = ACodes + ((byteNo != AHigh) ABase[byteNo+1] : 256);
byteBits = ASuffix[byteNo];
byteLength = byteNo + 1 + byteBits;
// 判断后缀码位数是否非 0
if (byteBits != 0)
for (byteva lue = 0; pCodes < pEnd; pCodes++, byteva lue++)
{
pCodes->Length = byteLength;
pBits = pCodes->Bits;
byteBegin = 0;
PrefixToBuffer(pBits, byteBegin, byteNo);
ByteToBuffer(pBits, byteBegin, byteva lue, byteBits);
}
else
for (; pCodes < pEnd; pCodes++)
{
pCodes->Length = byteLength;
pBits = pCodes->Bits;
byteBegin = 0;
PrefixToBuffer(pBits, byteBegin, byteNo);
}
}
}
// 初始化偏移量的静态编码表
void TKYFixedPack::_Init_Offset_Codes()
{
// 初始化
char* pBits;
PCode pCodes, pEnd;
Word wValue;
Byte byteNo, byteBits, byteLength, byteBegin;
// 循环添加静态编码表
for (byteNo = 0, pCodes = _Codes_Offset; byteNo <= High_Offset; byteNo++)
{
pEnd = _Codes_Offset + ((byteNo < High_Offset)
_O_Base[byteNo+1] : Max_Offset);
byteBits = _O_Bits[byteNo];
byteLength = Prefix_Offset + byteBits;
// 判断后缀码位数是否非 0
if (byteBits != 0)
for (wValue = 0; pCodes < pEnd; pCodes++, wValue++)
{
pCodes->Length = byteLength;
pBits = pCodes->Bits;
byteBegin = 0;
ByteToBuffer(pBits, byteBegin, byteNo, Prefix_Offset);
WordToBuffer(pBits, byteBegin, wValue, byteBits);
}
else
for (; pCodes < pEnd; pCodes++)
{
pCodes->Length = byteLength;
pBits = pCodes->Bits;
byteBegin = 0;
ByteToBuffer(pBits, byteBegin, byteNo, Prefix_Offset);
}
}
}
// ---------------- 构造函数和析构函数 ----------------
// 构造函数
TKYFixedPack::TKYFixedPack()
{
// 初始化
FDest = NULL;
FMaxSize = 0;
FIsPacking = false;
// 清空
Reset();
}
// 析构函数
TKYFixedPack::~TKYFixedPack()
{
if (FDest != NULL)
{
free(FDest);
FDest = NULL;
}
}
// ---------------- 私有函数 ----------------
// 拷贝上下文
void TKYFixedPack::DoCopy(const char* ABuffer, long ASize)
{
// 初始化
long intSize;
char* pDest = (char*)FContext;
char* pPos = (char*)ABuffer;
char* pEnd = pPos + ASize;
// 检查尺寸
if (ASize >= Max_Offset)
memcpy(pDest, pEnd - Max_Offset, Max_Offset);
else if (FLastSize >= Max_Offset)
{
intSize = Max_Offset - ASize;
memmove(pDest, pDest + ASize, intSize);
memcpy(pDest + intSize, pPos, ASize);
}
else if (FLastSize + ASize > Max_Offset)
{
intSize = Max_Offset - ASize;
memmove(pDest, pDest + (FLastSize - intSize), intSize);
memcpy(pDest + intSize, pPos, ASize);
}
else
memcpy(pDest + FLastSize, pPos, ASize);
// 修改最后尺寸
FDataSize = FLastSize + ASize;
FLastSize = FDataSize;
}
// 索引项匹配
bool TKYFixedPack::DoMatch(Byte* APos, Byte* AEnd, Word& AOffset, Byte& ADepth)
{
// 初始化
bool re