动态使用zlib deflateBound() [英] Using zlib deflateBound() dynamically
问题描述
我一直在研究如何动态地使用deflateBound(),却找不到我真正想要的东西。
I have been looking at how to use deflateBound() dynamically and have not found exactly what I am looking for.
我看过zlip的手册,示例包含在库中,并在以下位置找到它们:
ARM Cortex M3的压缩库/ 4
确定已压缩C中Z-lib的/ uncompressed缓冲区大小
zlib,deflate:要分配多少内存?
I looked at the manual for zlip, examples included in library and found these here: Compression Libraries for ARM Cortex M3/4 Determine compressed/uncompressed buffer size for Z-lib in C zlib, deflate: How much memory to allocate?
我缺少的是在调用deflateBound()之后如何分配。
例如看来这会引起问题
What I am missing is how to allocate after calling deflateBound(). E.g. this looks like it will cause problems
z_stream defstream;
uint8_t *outBuf=NULL;
uint32_t outLen=0;
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
defstream.avail_in = (uInt)inLen;
defstream.next_in = (Bytef *)inBuf:
defstream.avail_out = (uInt)0;
defstream.next_out = (Bytef *)outBuf;
deflateInit(&defstream, Z_DEFAULT_COMPRESSION);
uint32_t estimateLen = deflateBound(&defstream, inLen);
outBuf = malloc(estimateLen);
defstream.avail_out = (uInt)estimateLen;
deflate(&defstream, Z_FINISH);
deflateEnd(&defstream);
我看到有人提到realloc,这是否意味着建议从初始(可能太小)缓冲区开始?
I see realloc being mentioned, does this mean starting with an initial (probably too small) buffer is recommended?
z_stream defstream;
uint8_t *outBuf=NULL;
uint32_t outLen=100;
outBuf = malloc(outLen);
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
defstream.avail_in = (uInt)inLen;
defstream.next_in = (Bytef *)inBuf:
defstream.avail_out = (uInt)outLen;
defstream.next_out = (Bytef *)outBuf;
deflateInit(&defstream, Z_DEFAULT_COMPRESSION);
uint32_t estimateLen = deflateBound(&defstream, inLen);
outBuf = realloc(outBufestimateLen);
defstream.avail_out = (uInt)estimateLen;
deflate(&defstream, Z_FINISH);
deflateEnd(&defstream);
作为嵌入式系统,我试图使事情变得简单。
Being an embedded system, I am trying to keep things simple.
更新02/08/2019
以下代码有效(注意Mark的修复):
Update 02/08/2019 the following code works (note Mark's fix):
static uint8_t *compressBuffer(char *inBuf, uint32_t *outLen)
{
uint32_t inLen = strlen(inBuf)+1; // +1 so the null terminator will get encoded
uint8_t *outBuf = NULL;
int result;
uint32_t tmpLen=0;
// initialize zlib
z_stream defstream;
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
defstream.avail_in = inLen;
defstream.next_in = (Bytef *)inBuf;
defstream.avail_out = 0;
defstream.next_out = (Bytef *)outBuf;
if ((result = deflateInit(&defstream, Z_DEFAULT_COMPRESSION)) == Z_OK)
{
// calculate actual output length and update structure
uint32_t estimateLen = deflateBound(&defstream, inLen);
outBuf = malloc(estimateLen+10);
if (outBuf != NULL)
{
// update zlib configuration
defstream.avail_out = (uInt)estimateLen;
defstream.next_out = (Bytef *)outBuf;
// do the compression
deflate(&defstream, Z_FINISH);
tmpLen = (uint8_t*)defstream.next_out - outBuf;
}
}
// do the followimg regardless of outcome to leave in a good place
deflateEnd(&defstream);
// return whatever we have
*outLen = tmpLen;
return outBuf;
}
推荐答案
在两个示例中,您都不是在 deflateBound()
之后设置 next_out
。您需要 defstream.next_out =(Bytef *)outBuf;
在 malloc()
之后。
In both examples you are not setting next_out
after the deflateBound()
. You need defstream.next_out = (Bytef *)outBuf;
after the malloc()
.
您不需要执行 realloc()
。
这篇关于动态使用zlib deflateBound()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!