📄 zlib.cpp
字号:
// Copy in the contents of these files here before compiling
#include "StdAfx.h"
#include "ZLib.h"
BYTE* CZLib::Compress(LPCVOID pInput, DWORD nInput, DWORD* pnOutput, DWORD nSuggest)
{
// If we were given nSuggest, use it as the output buffer size, otherwise call compressBound to set it
*pnOutput = nSuggest ? nSuggest : compressBound( nInput ); // compressBound just uses math to guess, it doesn't look at the data
// Allocate a new buffer of pnOutput bytes
BYTE* pBuffer = new BYTE[ *pnOutput ];
// Compress the data at pInput into pBuffer, putting how many bytes it wrote under pnOutput
if ( compress( // Compress data from one buffer to another, returns Z_OK 0 false if it works
pBuffer, // The output buffer where ZLib can write compressed data
pnOutput, // Reads how much space it has there, writes how much space it used
(const BYTE *)pInput, // The source buffer with data to compress
nInput ) != Z_OK ) // The number of bytes there
{
// The compress function reported error
delete[] pBuffer;
return NULL;
}
return pBuffer;
}
//////////////////////////////////////////////////////////////////////
// CZLib decompression
// Takes a pointer to compressed input bytes, and how many are there
// Decompresses the memory into a new buffer this function allocates
// Returns a pointer to the new buffer, and writes its size under pnOutput
BYTE* CZLib::Decompress(LPCVOID pInput, DWORD nInput, DWORD* pnOutput, DWORD nSuggest)
{
// Guess how big the data will be decompressed, use nSuggest, or just guess it will be 6 times as big
*pnOutput = nSuggest ? nSuggest : nInput * 6;
// Allocate a buffer that big
BYTE* pBuffer = new BYTE[ *pnOutput ];
// Uncompress the data from pInput into pBuffer, writing how big it is now in pnOutput
if ( uncompress( // Uncompress data
pBuffer, // Destination buffer where uncompress can write uncompressed data
pnOutput, // Reads how much space it has there, and writes how much space it used
(const BYTE *)pInput, // Source buffer of compressed data
nInput ) != Z_OK ) // Number of bytes there
{
// The uncompress function returned an error, delete the buffer we allocated and return error
delete[] pBuffer;
return NULL;
}
return pBuffer;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -