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

2014-11-24 08:13:32 · 作者: · 浏览: 3
Byte* pPos = (Byte*)APos;
Byte* pEnd = (Byte*)AEnd;
Byte byteAnd = Bits_Prefix[ABitBegin];
Byte byteCount = 0;
// 扫描完整前缀码
if ((*pPos & byteAnd) == byteAnd)
{
byteCount = 8 - ABitBegin;
ABitBegin = 0;
pPos++;
// 整个字符
for (; pPos < pEnd; pPos++, byteCount += 8)
if (*pPos != 0xFF)
break;
}
// 范围检查
if (pPos < pEnd)
{
// 扫描剩余前缀码
for (; ABitBegin < 8; ABitBegin++, byteCount++)
if ((*pPos & Bits_Bool[ABitBegin]) == 0)
break;
// 判断是否成功
if (byteCount <= AMax)
{
// 处理前缀码结束符 0
ABitBegin++;
if (ABitBegin == 8)
{
ABitBegin = 0;
pPos++;
}
// 结果
ACount = byteCount;
result = true;
}
}
// 更新位置
APos = (char*)pPos;
}
// 返回结果
return result;
}
// 缓冲区 -> Depth
// 注: APos 和 AEnd 非空, ABitBegin 取值 [0..7]
static bool BufferToDepth(char* &APos, char* AEnd, Byte& ABitBegin, Word& ADepth)
{
// 初始化
Byte byteIndex;
bool result = false;
// 取索引
if (BufferToPrefix(APos, AEnd, ABitBegin, byteIndex, High_Depth))
{
// 初始化
Byte byteva lue = 0;
Byte byteBits = _D_Bits[byteIndex];
// 取后缀
if (BufferToByte(APos, AEnd, ABitBegin, byteva lue, byteBits))
{
ADepth = _D_Base[byteIndex] + byteva lue;
result = true;
}
}
// 返回结果
return result;
}
// 缓冲区 -> Offset(含编/解码)
// 注: APos 和 AEnd 非空, ABitBegin 取值 [0..7]
static bool BufferToOffset(char* &APos, char* AEnd,
Byte& ABitBegin, Word& AOffset)
{
// 初始化
Byte byteIndex;
bool result = false;
// 取索引
if (BufferToByte(APos, AEnd, ABitBegin, byteIndex, Prefix_Offset))
{
// 初始化
Word wordValue = 0;
Byte byteBits = _O_Bits[byteIndex];
// 取后缀
if ((byteBits == 0)
|| BufferToWord(APos, AEnd, ABitBegin, wordValue, byteBits))
{
AOffset = _O_Base[byteIndex] + wordValue;
result = true;
}
}
// 返回结果
return result;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* TKYFixedPack - 固定缓冲区的压缩类(基于LZ压缩算法) */
// ---------------- 静态成员 ----------------
// 深度和偏移量的静态编码表
TKYFixedPack::TCode TKYFixedPack::_Codes_Depth[256];
TKYFixedPack::TCode TKYFixedPack::_Codes_Offset[1024];
// TKYFixedPack 的静态成员初始化对象
TKYFixedPack::TInitialization TKYFixedPack::_Initialization;
// TKYFixedPack 的静态成员初始化类的构造函数
TKYFixedPack::TInitialization::TInitialization()
{
// 初始化深度和偏移量的静态编码表
_Init_Depth_Codes(_Codes_Depth, _D_Base, _D_Bits, High_Depth);
_Init_Offset_Codes();
}
// TKYFixedPack 的静态成员初始化类的析构函数
TKYFixedPack::TInitialization::~TInitialization()
{
}
// ---------------- 静态函数 ----------------
// 初始化深度的静态编码表
void TKYFixedPack::_Init_Depth_Codes(TCode* ACodes, const Byte* ABase,
const Byte* ASuffix, Byte AHigh)
{
// 初始化
char* pBits;