Using zlib deflateBound() dynamically

瘦欲@ 提交于 2019-12-23 04:59:10

问题


I have been looking at how to use deflateBound() dynamically and have not found exactly what I am looking for.

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?

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);

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.

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;
}

回答1:


In both examples you are not setting next_out after the deflateBound(). You need defstream.next_out = (Bytef *)outBuf; after the malloc().

You do not need to do a realloc().



来源:https://stackoverflow.com/questions/54559911/using-zlib-deflatebound-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!