我有 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[] 数组编组为非托管指针吗?或者可能是什么问题?我的输入数组无效吗?
此致