gpt4 book ai didi

c++ - Zlib 膨胀和收缩错误

转载 作者:太空宇宙 更新时间:2023-11-04 16:26:13 25 4
gpt4 key购买 nike

大家好,我基本上是在尝试使用 zlib 库,但是当我尝试膨胀一个 self 收缩的文件时遇到了麻烦,尽管当我膨胀其他 zlib´d 文件时它工作正常。

压缩代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned char BYTE;
typedef unsigned int UINT;
#include "zlib.h"
#define HEADERSIZE (1024)
#define CHUNKSIZE 4096
#define SWAPINT(x) (((x)&0xFF) << 24)|(((x)&0xFF00) << 8) | (((x)&0xFF0000) >> 8) | (((x)&0xFF000000) >> 24)
#define DEFLATESIZE 65536
const char *mz_error(int err)
{
static struct { int m_err; const char *m_pDesc; } s_error_descs[] =
{
{ Z_OK, "" }, { Z_STREAM_END, "stream end" }, { Z_NEED_DICT, "need dictionary" }, { Z_ERRNO, "file error" }, { Z_STREAM_ERROR, "stream error" },
{ Z_DATA_ERROR, "data error" }, { Z_MEM_ERROR, "out of memory" }, { Z_BUF_ERROR, "buf error" }, { Z_VERSION_ERROR, "version error" }
};
UINT i; for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i) if (s_error_descs[i].m_err == err) return s_error_descs[i].m_pDesc;
return NULL;
}

char* myinflate(char* buffer, int bufsize, int* inflatedSize)
{
BYTE tmpinf[DEFLATESIZE];
char* inflated=(char*)malloc(DEFLATESIZE);
int ret,have;
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = bufsize;
strm.next_in = (Bytef*)buffer;
strm.avail_out = sizeof(tmpinf);
strm.next_out = tmpinf;
ret = inflateInit(&strm);
printf("%s",mz_error(ret));
ret = inflate(&strm, Z_FINISH);
printf("%s",mz_error(ret));
if(ret == Z_DATA_ERROR)
printf(strm.msg);
inflateEnd(&strm);
have = strm.total_out;
printf("%d\n",have);
memcpy(inflated,tmpinf,have);
*inflatedSize = have;
return inflated;
}
char* mydeflate(char* buffer, int bufsize, int* deflatedSize)
{
BYTE tmpdef[CHUNKSIZE*4];
char* deflated=(char*)malloc(DEFLATESIZE);
int have,ret;
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = bufsize;
strm.next_in = (Bytef*)buffer;
strm.avail_out = sizeof(tmpdef);
strm.next_out = tmpdef;
ret = deflateInit(&strm,Z_DEFAULT_COMPRESSION);
printf("%s",mz_error(ret));
ret = deflate(&strm, Z_FINISH);
printf("%s",mz_error(ret));
ret = deflateEnd(&strm);
printf("%s",mz_error(ret));
have = strm.total_out;
printf("%d\n",have);
memcpy(deflated,tmpdef,have);
*deflatedSize = have;
return deflated;
}
int main()
{
FILE* fptr = fopen("test.in","rb");
fseek (fptr, 0, SEEK_END);
int size = ftell(fptr);
fseek (fptr, 0, SEEK_SET);
char* buffer = (char*)malloc(size);
fread(buffer,1,size,fptr);
int infsize,defsize;
char* buf = myinflate(buffer,size,&infsize);
FILE* out = fopen("testinf.hex","wb");
fwrite(buf,1,infsize,out);
fclose(out);
char* buf2 = mydeflate(buf,infsize,&defsize);
out = fopen("testdef.hex","wb");
fwrite(buf2,1,defsize,out);
fclose(out);
int buf3size;
char* buf3 = myinflate(buf2,defsize,&buf3size);
out = fopen("testinfdef.hex","wb");
fwrite(buf3,1,buf3size,out);
}

http://www.filehosting.org/file/details/364574/eEyzAbMuCp93MmGk/test.in

最佳答案

您没有检查任何 返回码。你怎么可能期望知道发生了什么事? 检查所有可能返回错误的函数的返回码!

可能根本没有提供足够的空间用于压缩和/或解压缩。 inflate()compress() 的返回码表明是否是这种情况。

顺便说一下,您 malloc() inflated 两次,覆盖了第一次导致大量内存泄漏。

您还愉快地将一个 int 指针转换为一个无符号长指针。如果它们的长度不同,那么您就会遇到问题。

关于c++ - Zlib 膨胀和收缩错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11803606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com