固定缓冲区的压缩/解压缩类源码及测试例子: http://download.youkuaiyun.com/detail/kyee/5052133
固定缓冲区的压缩/解压缩算法,适用于网络传输数据包的压缩,具有压缩速度快、压缩率适中特点,而且压缩后数据缓冲区可控。
其中包含固定缓冲区的压缩类(TKYFixedPack)和解压缩类(TKYFixedUnpack),源码如下:
// =======================================
// Unit : 固定缓冲区的压缩/解压缩单元
// Version: 3.0.0.0 (build 2012.04.19)
// Author : Kyee Ye
// Email : kyee_ye(at)126.com
// Copyright (C) Kyee workroom
// =======================================
#ifndef _KYFixedPack_H_
#define _KYFixedPack_H_
#include "KYPackObj.h"
namespace KYLib
{
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* TKYFixedPack - 固定缓冲区的压缩类(基于LZ压缩算法) */
class TKYFixedPack
{
public:
TKYFixedPack();
virtual ~TKYFixedPack();
// 属性
char* Dest() const { return FDest; } // default: NULL
Word Size() const { return FSize; } // default: 0
Word MaxSize() const { return FMaxSize; } // default: 0
Longword DataSize() const { return FDataSize; } // default: 0
bool IsPacking() const { return FIsPacking; } // default: false
// 设置压缩结果缓冲区的最大尺寸
bool SetMaxSize(Word ASize);
// 重置
void Reset();
// 压缩缓冲区
bool Pack(const char* ABuffer, long ASize, Word AMax = 0xFFFF);
private:
void DoCopy(const char* ABuffer, long ASize);
bool DoMatch(Byte* APos, Byte* AEnd, Word& AOffset, Byte& ADepth);
private:
char FContext[1024]; // 压缩数据的上下文
Word FHash[4096]; // 哈希表
char* FDest; // 压缩后的缓冲区
Word FSize; // 压缩后数据尺寸
Word FMaxSize; // 缓冲区最大尺寸
Longword FDataSize; // 总压缩数据尺寸
Longword FLastSize; // 上一次 pack 结束后的尺寸
Byte FBitBegin; // 第一个字节数据开始位
bool FIsPacking; // 判断是否正在压缩
private:
// 编码项
typedef struct
{
Byte Length; // 编码位数
char Bits[3]; // 编码
} TCode, *PCode;
private:
// 初始化深度和偏移量的静态编码表
static void _Init_Depth_Codes(TCode* ACodes, const Byte* ABase,
const Byte* ASuffix, Byte AHigh);
static void _Init_Offset_Codes();
private:
// 深度和偏移量的静态编码表
static TCode _Codes_Depth[256];
static TCode _Codes_Offset[1024];
// TKYFixedPack 的静态成员初始化类
static class TInitialization
{
public:
TInitialization();
~TInitialization();
} _Initialization;
friend class TInitialization;
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* TKYFixedUnpack - 固定缓冲区的解压缩类(基于LZ压缩算法) */
class TKYFixedUnpack
{
public:
// 解压缩缓冲区
// 1. ABuffer 存放 ASize 字节的已压缩数据
// 2. ADest 存放解压缩后的数据, ADestSize >= ASize
// 3. 若返回值 > 0 表示解压缩后数据的尺寸
// 4. 若返回值为 0 表示解压缩失败, 可能 ADestSize 太小或数据未压缩
// 5. 若返回值为 -1 表示参数不合法
// 6. 若返回值为 -2 表示解压缩失败, 原因是数据已损坏
static long Unpack(const char* ABuffer, long ASize,
char* ADest, long ADestSize);
};
}
#endif
// =======================================
// Unit : 固定缓冲区的压缩/解压缩单元
// Version: 3.0.0.0 (build 2012.04.19)
// Author : Kyee Ye
// Email : kyee_ye(at)126.com
// Copyright (C) Kyee workroom
// =======================================
#include "KYFixedPack.h"
namespace KYLib
{
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* 深度和偏移量编码操作的相关静态函数 */
// 常量
#define Max_Depth 258 // 最大匹配深度
#define Max_Offset 1024 // 最大偏移量
#define Mask_Index 0x7FFF // 索引的掩码
#define Mask_Hash 0x0FFF // 哈希的掩码
#define Hash_RandomGene 45673 // 哈希的随机因子必须是素数,
// 且除以 4096 的商和余数也是素数
// 深度的编码
#define High_Depth 8 // 后缀的最高项数
// 偏移量的编码
#define Prefix_Offset 4 // 偏移量的前缀位数
#define High_Offset 15 // = 2^Prefix_Offset - 1
// 前缀码的位值
static const Byte Bits_Prefix[8] = {0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01};
// 高位与模值
static const Byte Bits_HiAnd[8] = {0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE};
// 布尔位值
static const Byte Bits_Bool[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
// 深度前缀编码的基数
static const Byte _D_Base[9] = {0, 2, 4, 8, 12, 20, 36, 68,
132};
// 深度前缀编码的后缀位数
static const Byte _D_Bits[9] = {1, 1, 2, 2, 3, 4, 5, 6,
7};
// 偏移量前缀编码的基数
static const Word _O_Base[16] = {0, 1, 2, 3, 4, 6, 8, 12,
16, 24, 32, 48, 64, 128, 256, 512};
// 偏移量前缀编码的后缀位数
static const Byte _O_Bits[16] = {0, 0, 0, 0, 1, 1, 2, 2,
3, 3, 4, 4, 6, 7, 8, 9};
// Prefix前缀码 -> 缓冲区
// 注: APos 非空, AB