我可以安全地使用哪个定界符来分隔节点

问题描述

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

zlib.deflate压缩每个内容,然后将其发送到远程。 我不希望进行多次往返并一次发送全部内容

要在每个内容之间进行分隔,我需要一个不能在压缩字符串中使用的字符,因此我可以在遥控器上安全地对其进行分割。

解决方法

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