尝试以 zlib 格式压缩并对流进行低级别访问

发布于 2024-10-16 08:07:11 字数 796 浏览 9 评论 0原文

我正在尝试实现 message.rpmsg 格式的 Microsoft 规范(此处: http://msdn.microsoft.com/en-us/library/ee625343(v=EXCHG.80).aspx)。该规范是 zlib 压缩流的奇怪组合,打包在数据包中。对于每个数据包,我需要在数据包标头中放入未压缩流中的字节数(必须为 4096,否则 Outlook 咳嗽,尽管有规范)、压缩缓冲区的大小和魔术标记。

我的代码位于 .net 中,最好我正在寻找一个全托管库。

查看各种库(SharpZlibLib、zlib.NET、Microsoft Compression.Deflate 命名空间) - 我找不到每个“Write”都会执行的公开可用条目: - 返回压缩字节数 - 保证字节边界(FLUSH_SYNC) - 理想情况下,无需欺骗或更改太多代码

我所查看的库有一个输出流,您可以从中读取(整个压缩流) - 但不提供对数据包本身的访问。

目前,我正在使用原始的 zlib 库和本机 zlib1.dll,对 contrib/dotzlib 进行多次修改后 - 但是,我想避免当前解决方法带来的明显性能和部署麻烦,

所以我寻找一个可以让我指定 FLUSH 模式并允许我访问每个数据包的大小的库。

另外,如果有人可以评价各种库(zlib.net,SharpZipLib 似乎是最普遍的 - 还有什么?),以及性能/质量/支持等领域,

非常感谢 乌里

I'm trying to implement Microsoft specification for message.rpmsg format (here: http://msdn.microsoft.com/en-us/library/ee625343(v=EXCHG.80).aspx). The specification is weird combination of zlib compressed stream, packaged in packets. For each packet I need to put in the packet header the number of bytes in the uncompressed stream (has to be 4096, otherwise Outlook cough, despite the spec), the size of the compressed buffer, and a magic marker.

My code is in .net, and preferably I'm looking for an all managed library.

Looking at various libraries (SharpZlibLib, zlib.NET, the Microsoft Compression.Deflate namespace) - I couldn't find a publicly available entries that for each 'Write' will do:
- Return the number of compressed bytes
- Guaranty BYTE boundary (FLUSH_SYNC)
- Ideally, without spoofing or changing too much the code

The libraries I've looked at have an Output stream from which you can read (the entire compressed stream) - but do not provide access to the packets itself.

For now, I'm using the original zlib library, and the native zlib1.dll, after many modifications to the contrib/dotzlib - however, I'd like to avoid the obvious performance and deployment hassles of my current workaround

So I'm looking for a library that will let me specify the FLUSH mode, and give me access to the size of each packet.

Also, if anyone can rate the various libraries (zlib.net, SharpZipLib seems to be the most ubiquitous - anything else?), and areas such as performance/quality/support

Thanks Much
Uri

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杯敬自由 2024-10-23 08:07:11

未经测试,但使用 zlib.net 我希望像下面这样工作;请注意,我在这里使用 MemoryStream 以避免必须进行查找(因为这不能得到普遍支持 - 由于块长度最大为 4k,我根本不关心这一点) ,但是如果您知道,您可以寻求为压缩长度写入一个虚拟0,然后写入数据,然后向后查找以更新压缩长度,然后再次向前查找到末尾:

static void WriteBlock(Stream destination, byte[] arr, int offset, int length)
{
    using (var ms = new MemoryStream())
    {
        using (var zlib = new zlib.ZOutputStream(ms))
        {
            zlib.Write(arr, offset, length);
        }
        WriteInt32LittleEndian(destination, 0x00000FA0);
        WriteInt32LittleEndian(destination, length);
        WriteInt32LittleEndian(destination, (int)ms.Length);
        destination.Write(ms.GetBuffer(), 0, (int)ms.Length);

    }
}
static void WriteInt32LittleEndian(Stream destination, int value)
{
    destination.WriteByte((byte)value);
    destination.WriteByte((byte)(value >> 8));
    destination.WriteByte((byte)(value >> 16));
    destination.WriteByte((byte)(value >> 24));
}

Untested, but using zlib.net I would have expected something like below to work; note that I'm using a MemoryStream here to avoid having to seek (since that can't be universally supported - since the block length is a maximum of 4k, I am not concerned about this at all), but if you know you can seek you could write a dummy 0 for the compressed length, then write the data, then seek backwards to update the compressed length, and seek forwards to the end again:

static void WriteBlock(Stream destination, byte[] arr, int offset, int length)
{
    using (var ms = new MemoryStream())
    {
        using (var zlib = new zlib.ZOutputStream(ms))
        {
            zlib.Write(arr, offset, length);
        }
        WriteInt32LittleEndian(destination, 0x00000FA0);
        WriteInt32LittleEndian(destination, length);
        WriteInt32LittleEndian(destination, (int)ms.Length);
        destination.Write(ms.GetBuffer(), 0, (int)ms.Length);

    }
}
static void WriteInt32LittleEndian(Stream destination, int value)
{
    destination.WriteByte((byte)value);
    destination.WriteByte((byte)(value >> 8));
    destination.WriteByte((byte)(value >> 16));
    destination.WriteByte((byte)(value >> 24));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文