首页 > 解决方案 > 我可以安全地使用哪个分隔符来分隔节点中的 zlib 压缩字符串

问题描述

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

每条内容zlib.deflate在发送到远程之前都经过压缩。我不希望进行多次往返并一次发送整个内容。

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

标签: node.jszlibdeflate

解决方案


没有这样的字符或字符序列。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;
            }
        }
}

推荐阅读