iteSetup中的压缩解压代码
zlib只是对内存段压缩解压,对于文件,我们可以分段 读取到内存然后操作
注意compressBound 有BUG
如果存在压缩后的大小大于原大小,此函数返回的是压缩后最大大小 就会有错误,导致内存溢出
LiteSetup项目中发现这个BUG(如压缩一个已经rar压缩的文件,就可能溢出)
UnCompressBlock* Core::UnCompressBuffer(char*buffer, unsigned int len, unsigned int len_orign)
{
UnCompressBlock *ret = new UnCompressBlock;
char *dest = new char[len_orign + 10];
if (0 == uncompress((Bytef*)dest, (uLongf*)&len_orign, (Bytef*)buffer, len))
{
ret->buffer = dest;
ret->_size = len_orign;
return ret;
}
delete[] dest;
delete ret;
return nullptr;
}
CompressBlock* Core::CompressBuffer(char * buffer, unsigned int _size)
{
CompressBlock *ret = new CompressBlock;
unsigned int len = compressBound((uLong)_size);
char * dest = new char[len+1024*1024];
if (compress((Bytef*)dest, (uLongf*)&len, (Bytef*)buffer, _size) == 0)
{
ret->buffer = dest;
ret->_size = len;
ret->_size_orign = _size;
return ret;
}
delete ret;
delete[]dest;
return nullptr;
}
- UnCompressBlock* Core::UnCompressBuffer(char*buffer, unsigned int len, unsigned int len_orign)
- {
- UnCompressBlock *ret = new UnCompressBlock;
- char *dest = new char[len_orign + 10];
- if (0 == uncompress((Bytef*)dest, (uLongf*)&len_orign, (Bytef*)buffer, len))
- {
- ret->buffer = dest;
- ret->_size = len_orign;
- return ret;
- }
- delete[] dest;
- delete ret;
- return nullptr;
- }
- CompressBlock* Core::CompressBuffer(char * buffer, unsigned int _size)
- {
- CompressBlock *ret = new CompressBlock;
- unsigned int len = compressBound((uLong)_size);
- char * dest = new char[len+1024*1024];
- if (compress((Bytef*)dest, (uLongf*)&len, (Bytef*)buffer, _size) == 0)
- {
- ret->buffer = dest;
- ret->_size = len;
- ret->_size_orign = _size;
- return ret;
- }
- delete ret;
- delete[]dest;
- return nullptr;
- }
UnCompressBlock* Core::UnCompressBuffer(char*buffer, unsigned int len, unsigned int len_orign)
{
UnCompressBlock *ret = new UnCompressBlock;
char *dest = new char[len_orign + 10];
if (0 == uncompress((Bytef*)dest, (uLongf*)&len_orign, (Bytef*)buffer, len))
{
ret->buffer = dest;
ret->_size = len_orign;
return ret;
}
delete[] dest;
delete ret;
return nullptr;
}
CompressBlock* Core::CompressBuffer(char * buffer, unsigned int _size)
{
CompressBlock *ret = new CompressBlock;
unsigned int len = compressBound((uLong)_size);
char * dest = new char[len+1024*1024];
if (compress((Bytef*)dest, (uLongf*)&len, (Bytef*)buffer, _size) == 0)
{
ret->buffer = dest;
ret->_size = len;
ret->_size_orign = _size;
return ret;
}
delete ret;
delete[]dest;
return nullptr;
}