windows平台(不包括ARM的CE)通用的压缩和解压缩
2024-10-14 23:55:39
通用是相对的,这里指的是xp和win7(其他版本我没测试过,不要用不要来找我)
#define CMP_FRM COMPRESSION_FORMAT_LZNT1|COMPRESSION_ENGINE_MAXIMUM typedef DWORD(__stdcall *RtlCompressBuffer_Fn)(
IN ULONG CompressionFormat,
IN PVOID SourceBuffer,
IN ULONG SourceBufferLength,
OUT PVOID DestinationBuffer,
IN ULONG DestinationBufferLength,
IN ULONG Unknown,
OUT PULONG pDestinationSize,
IN PVOID WorkspaceBuffer); typedef DWORD(__stdcall *RtlDecompressBuffer_Fn)(
IN ULONG CompressionFormat,
OUT PVOID DestinationBuffer,
IN ULONG DestinationBufferLength,
IN PVOID SourceBuffer,
IN ULONG SourceBufferLength,
OUT PULONG pDestinationSize); typedef DWORD(__stdcall *RtlGetCompressionWorkSpaceSize_Fn)(
IN ULONG CompressionFormat,
OUT PULONG pNeededBufferSize,
OUT PULONG pUnknown); LPBYTE CompressBuffer(LPBYTE lpIn, int iInLen, int & iOut); LPBYTE DecompressBuffer(LPBYTE lpIn, int iInlen, int & iOut);
static RtlCompressBuffer_Fn compress = NULL;
static RtlDecompressBuffer_Fn decompress = NULL;
static RtlGetCompressionWorkSpaceSize_Fn getcompressionworkspacesize = NULL;
void GetCompressApis()
{ if (compress && decompress && getcompressionworkspacesize)
{
return;
}
static WCHAR wszntdll[] = { L'n', L't', L'd', L'l', L'l', L'.', L'd', L'l', L'l', L'\0' };
static HMODULE hDll = LoadLibrary(wszntdll);
static char szRtlCompressBuffer[] = { 'R', 't', 'l', 'C', 'o', 'm', 'p', 'r', 'e', 's', 's', 'B', 'u', 'f', 'f', 'e', 'r', '\0' };
static char szRtlDecompressBuffer[] = { 'R', 't', 'l', 'D', 'e', 'c', 'o', 'm', 'p', 'r', 'e', 's', 's', 'B', 'u', 'f', 'f', 'e', 'r', '\0' };
static char szRtlGetCompressionWorkSpaceSize[] = { 'R', 't', 'l', 'G', 'e', 't', 'C', 'o', 'm', 'p', 'r', 'e', 's', 's', 'i', 'o', 'n', 'W', 'o', 'r', 'k', 'S', 'p', 'a', 'c', 'e', 'S', 'i', 'z', 'e', '\0' };
if (hDll)
{
compress = (RtlCompressBuffer_Fn)GetProcAddress(hDll, szRtlCompressBuffer);
decompress = (RtlDecompressBuffer_Fn)GetProcAddress(hDll, szRtlDecompressBuffer);
getcompressionworkspacesize = (RtlGetCompressionWorkSpaceSize_Fn)GetProcAddress(hDll, szRtlGetCompressionWorkSpaceSize); } }
//
// compress buffer
//
LPBYTE CompressBuffer(LPBYTE lpIn, int iInLen, int & iOut)
{
// 压缩格式 |原大小 2 | 压缩后大小 2| 数据 .....|hash 4|
GetCompressApis();
int dwOutBufferLen = iInLen*1.2 + ;
LPBYTE lpOut = (LPBYTE)zMalloc(dwOutBufferLen);
// 获取工作缓冲区大小
DWORD dwNeed, rc;
ULONG unKnow;
void* tempMeme;
rc = getcompressionworkspacesize(CMP_FRM, &dwNeed, &unKnow);
tempMeme = LocalAlloc(LPTR, dwNeed);
iOut = ;
rc = compress(CMP_FRM, lpIn, iInLen, lpOut + , dwOutBufferLen, unKnow, &dwNeed, tempMeme); LocalFree(tempMeme);
if (rc != )
{
zFree(lpOut);
return NULL;
}
*(short*)lpOut = iInLen;
iOut = dwNeed + ; return lpOut; }
//
// decompress buffer
//
LPBYTE DecompressBuffer(LPBYTE lpIn, int iInlen, int & iOut)
{
GetCompressApis();
short len = *(short*)lpIn;
if (len < || len > *)
{
return NULL ;
}
LPBYTE lpOut = (LPBYTE)zMalloc(len);
iOut = ;
DWORD dwRc = decompress(CMP_FRM, lpOut, len, lpIn + , iInlen - , (PULONG)&iOut);
if (dwRc != )
{
zFree(lpOut);
return NULL;
}
return lpOut; }
最新文章
- windows下安装kibana出 ";EPERM: operation not permitted
- SQLPROMPT5.3对各种加密对象的解密测试
- 1&#183;3 对 git 的认识
- cf 731f
- Xcode常用快捷键
- adroid 目录
- 乱侃c++
- Android(java)学习笔记265:Android线程形态之 HandlerThread
- hdu4597Play Game(记忆化)
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
- fedora 下安装 文泉驿正黑体
- 使用SQLServer Audit来监控触发器的启用、禁用情况
- 201521123098 《Java程序设计》第7周学习总结
- Spring_Aop的xml和注解的使用
- makefile讲解
- Python Learning - Three
- 【十三】jvm 性能调优工具之 jstack
- 130、 Android OkHttp完全解析(转载)
- python assert断言函数
- COGS.1272.[AHOI2009]行星序列(线段树 区间加、乘、求和)