【问题标题】:which delimiter can I use safely to separate zlib deflated strings in node我可以安全地使用哪个分隔符来分隔节点中的 zlib 压缩字符串
【发布时间】:2023-03-13 04:45:02
【问题描述】:

我需要使用 node.js 将内容从客户端发送到远程服务器。 内容可以是任何内容(用户可以上传任何文件)。

每条内容在发送到远程之前都经过zlib.deflate 压缩。 我不喜欢多次往返并一次发送全部内容。

为了分隔每条内容,我需要一个不能在压缩字符串中使用的字符,这样我就可以在遥控器上安全地拆分它。

【问题讨论】:

    标签: node.js zlib deflate


    【解决方案1】:

    没有这样的字符或字符序列。 zlib 压缩数据可以包含任何个字节序列。

    您可以对 zlib 压缩数据进行编码以避免一个字节值,稍微扩展压缩数据。然后你可以使用那个一个字节的值作为分隔符。

    示例代码:

    // Example of encoding binary data to a sequence of bytes with no zero values.
    // The result is expanded slightly. On average, assuming random input, the
    // expansion is less than 0.1%. The maximum expansion is less than 14.3%, which
    // is reached only if the input is a sequence of bytes all with value 255.
    
    #include <stdio.h>
    
    // Encode binary data read from in, to a sequence of byte values in 1..255
    // written to out. There will be no zero byte values in the output. The
    // encoding is decoding a flat (equiprobable) Huffman code of 255 symbols.
    void no_zeros_encode(FILE *in, FILE *out) {
        unsigned buf = 0;
        int bits = 0, ch;
        do {
            if (bits < 8) {
                ch = getc(in);
                if (ch != EOF) {
                    buf += (unsigned)ch << bits;
                    bits += 8;
                }
                else if (bits == 0)
                    break;
            }
            if ((buf & 127) == 127) {
                putc(255, out);
                buf >>= 7;
                bits -= 7;
            }
            else {
                unsigned val = buf & 255;
                buf >>= 8;
                bits -= 8;
                if (val < 127)
                    val++;
                putc(val, out);
            }
        } while (ch != EOF);
    }
    
    // Decode a sequence of byte values made by no_zeros_encode() read from in, to
    // the original binary data written to out. The decoding is encoding a flat
    // Huffman code of 255 symbols. no_zeros_encode() will not generate any zero
    // byte values in its output (that's the whole point), but if there are any
    // zeros in the input to no_zeros_decode(), they are ignored.
    void no_zeros_decode(FILE *in, FILE *out) {
        unsigned buf = 0;
        int bits = 0, ch;
        while ((ch = getc(in)) != EOF)
            if (ch != 0) {              // could flag any zeros as an error
                if (ch == 255) {
                    buf += 127 << bits;
                    bits += 7;
                }
                else {
                    if (ch <= 127)
                        ch--;
                    buf += (unsigned)ch << bits;
                    bits += 8;
                }
                if (bits >= 8) {
                    putc(buf, out);
                    buf >>= 8;
                    bits -= 8;
                }
            }
    }
    

    【讨论】:

    • 谢谢马克。关于你的建议,我不确定我是否完全理解。是关于做类似zlib.deflateSync(myContent).toString('base64') 这样的事情,然后内容被编码为base64,然后我可以使用任何在base64 中无效的字符吗?如果是这样,我想它可以工作,但数据会变得更大。
    • 这样,但不是那样。 Base 64 导致 35% 的扩展。您可以通过 0.1% 的扩展摆脱一个字节值。
    • “去掉一个字节值”是什么意思,你能详细说明我该怎么做吗?谢谢!
    • 要编码,解码 255 个符号的平坦(等概率)霍夫曼码。使用字节值 1..255 作为符号。那么零字节就不会出现了。要解码,请encode 255 个符号的扁平 Huffman 码。
    猜你喜欢
    • 2011-02-28
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 2023-02-25
    • 2014-04-05
    • 2014-10-13
    • 2011-02-06
    相关资源
    最近更新 更多