从c#调用c zlib uncompress

时间:2012-04-23 14:15:42

标签: c# c marshalling dllimport zlib

我有dllimport

[DllImport("../../zlib-1.2.5/zlib1.dll", CallingConvention = CallingConvention.Cdecl)]   
static extern int uncompress(byte[] destBuffer, ref uint destLen, byte[] sourceBuffer, uint sourceLen);

并将其称为

 uint _dLen = (uint) 8192;
 byte[] data = compressed_data;
 byte[] _d = new byte[_dLen];

 if (uncompress(_d,  ref _dLen, data, (uint)data.Length) != 0)
     return null;

zlib中的解压缩功能看起来像

unsigned int ZEXPORT uncompress (dest, destLen, source, sourceLen)
unsigned char *dest;
Uint32 destLen;
unsigned char *source;
Uint32 sourceLen;
{
z_stream stream;
int err;

stream.next_in = source;
stream.avail_in = sourceLen;
/* Check for source > 64K on 16-bit machine: */
if ((Uint32)stream.avail_in != sourceLen) return Z_BUF_ERROR;

stream.next_out = dest;
stream.avail_out = destLen;
if ((Uint32)stream.avail_out != destLen) return Z_BUF_ERROR;

stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;

err = inflateInit(&stream);
if (err != Z_OK) return err;


err = inflate(&stream, Z_FINISH);

if (err != Z_STREAM_END) {
    inflateEnd(&stream);
    if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
        return Z_DATA_ERROR;
    return err;
}

err = inflateEnd(&stream);
return stream.total_out;
}

但我总是在c#方面收到空 编辑:错误是z_data_error edit2:如果输入数据已损坏,则为Z_DATA_ERROR。

我需要将byte []数组编组为非托管指针吗? 或者可能是什么问题?我的输入数组无效吗?

最好的问候

1 个答案:

答案 0 :(得分:0)

经过越来越多的研究,我感觉一切都在这里, 并且错误必须在其他地方。