zlib膨胀返回Z_BUF_ERROR

时间:2014-04-03 09:12:20

标签: android zlib

我正在接收来自android客户端的流,该流使用standard zlib进行压缩(链接到-lz)。尝试对其进行充气时,以下方式,我得到Z_BUF_ERROR错误

if(!rawData)
    rawData= (char *)malloc(480 * 800 * 8);
int bufferLength = 128 * 1024;
char *tmpbuffer = malloc(bufferLength);
zUnzip.next_in = (Bytef *)inputStream;
zUnzip.avail_in = size;
zUnzip.next_out = (Bytef *)tmpbuffer; //output location of uncompressed stream
zUnzip.avail_out = bufferLength; //total size of output buffer
zUnzip.data_type = Z_BINARY;

inflateResult = inflateInit( &zUnzip); //standard zlib function, see <zlib.h>
if ( inflateResult != Z_OK ) {
    LOGE("inflateInit returned error: %d, msg: %s\n",inflateResult,zUnzip.msg);
}

do{
    inflateResult = inflate( &zUnzip, Z_SYNC_FLUSH );
    LOGE("avail_out = %d : difference = %d",zUnzip.avail_out,bufferLength-zUnzip.avail_out);
    memcpy(rawData,tmpbuffer,bufferLength);
    rawData+=bufferLength;
    zUnzip.next_out = (Bytef *)tmpbuffer; //output location of uncompressed stream
    zUnzip.avail_out = bufferLength; //total size of output buffer

}while(( inflateResult == Z_STREAM_END )) ;

if ( inflateResult < 0 ) {
        LOGE("zlib inflate returned error: %d, msg: %s\n",inflateResult,zUnzip.msg);
        return FALSE;
    }

inputStream的logcat输出,大小为16741字节

04-03 07:32:01.900: E/sc_client(25100): avail_out = 0 : difference = 131072
04-03 07:32:01.900: E/sc_client(25100): avail_out = 0 : difference = 131072
04-03 07:32:01.905: E/sc_client(25100): avail_out = 45672 : difference = 85400
04-03 07:32:01.905: E/sc_client(25100): avail_out = 131072 : difference = 0
04-03 07:32:01.905: E/sc_client(25100): zlib inflate returned error: -5, msg: (null)

注意:输入流是raw compressed stream(从zlib deflate调用直接输出)!

更新:缩小部分

rfbBool zrleOutStreamFlush(zrleOutStream *os){
  os->zs.next_in = os->in.start;
  os->zs.avail_in = ZRLE_BUFFER_LENGTH (&os->in);

  while (os->zs.avail_in != 0) {
    do {
      int ret;
      os->zs.next_out = os->out.ptr;
      os->zs.avail_out = os->out.end - os->out.ptr;


      if ((ret = deflate(&os->zs, Z_SYNC_FLUSH)) != Z_OK) {
        Log("zrleOutStreamFlush: deflate failed with error code %d\n", ret);
        return FALSE;
      }

      os->out.ptr = os->zs.next_out;
    } while (os->zs.avail_out == 0);
  }

  os->in.ptr = os->in.start;
  LOGE("zrleOutStreamFlush: total_in %ld : total_out %ld ", os->zs.total_in,os->zs.total_out);
  return TRUE;
}

结构定义:

typedef struct {
  zrle_U8 *start; 
  zrle_U8 *ptr; //from start to ptr, we have data
  zrle_U8 *end;
} zrleBuffer;

typedef struct {
  zrleBuffer in; //input buffer
  zrleBuffer out; //output buffer

  z_stream   zs;
} zrleOutStream;

1 个答案:

答案 0 :(得分:2)

您提供的压缩流inputStream[0..size-1]未完成。 inflate()的第三次调用已经处理了您提供的所有输入数据,但尚未看到deflate流的结束。如果您在该点检查avail_in,您会看到它为零。

顺便说一句,您不需要设置data_type。这是由zlib设置的。它仅供参考,可以忽略。