Netstrings (1997年)
Netstrings (1997)

原始链接: https://cr.yp.to/proto/netstrings.txt

netstring 由 D. J. Bernstein 于 1997 年开发,是一种简单的自定界格式,旨在对任意 8 位字节字符串进行编码。通过在字符串前加上长度(十进制 ASCII 码)并在末尾加上逗号——格式为 `[len]:[string],`——netstring 使应用程序能够在读取数据之前确定所需的内存缓冲区大小。 主要特点包括: * **多功能性:** 对字符串的长度或内容没有限制,并且可以递归使用 netstring 来编码复杂的数据结构。 * **可靠性:** 它们是网络协议的理想构建块,特别是在通过 TCP 等可靠流协议进行传输时。 * **高效性:** 该格式极易生成和解析,所需的计算开销极小。 该标准确保了通信的一致性,严格的定义证明了这一点,即 `[len]` 不得包含前导零(除非字符串为空)。由于 netstring 预先声明了大小,因此它们在处理字符串序列时为安全管理内存提供了一种可靠的机制。

Hacker News 上的一场讨论重新审视了丹尼尔·J·伯恩斯坦(Daniel J. Bernstein)在 1997 年提出的“Netstrings”方案,这是一种简单的数据序列化格式。评论者们赞扬该格式连贯且简洁,并指出它为后来的其他协议提供了概念基础。 值得注意的是,BitTorrent 的“bencoding”格式被认为是类似于 Netstrings 的一种实际应用,它采用了长度前缀结构(例如“5:hello”)。用户还提到了大约 15 年前出现的相关扩展“tnetstrings”,该格式通过单字符类型标签增加了对整数、浮点数和字典等递归类型的支持。尽管设计优雅,但该协议在主流应用中普及度有限,且许多原始资源已因链接失效而无法访问。
相关文章

原文
Netstrings D. J. Bernstein, [email protected] 19970201 1. Introduction A netstring is a self-delimiting encoding of a string. Netstrings are very easy to generate and to parse. Any string may be encoded as a netstring; there are no restrictions on length or on allowed bytes. Another virtue of a netstring is that it declares the string size up front. Thus an application can check in advance whether it has enough space to store the entire string. Netstrings may be used as a basic building block for reliable network protocols. Most high-level protocols, in effect, transmit a sequence of strings; those strings may be encoded as netstrings and then concatenated into a sequence of characters, which in turn may be transmitted over a reliable stream protocol such as TCP. Note that netstrings can be used recursively. The result of encoding a sequence of strings is a single string. A series of those encoded strings may in turn be encoded into a single string. And so on. In this document, a string of 8-bit bytes may be written in two different forms: as a series of hexadecimal numbers between angle brackets, or as a sequence of ASCII characters between double quotes. For example, is a string of length 12; it is the same as the string "hello world!". Although this document restricts attention to strings of 8-bit bytes, netstrings could be used with any 6-bit-or-larger character set. 2. Definition Any string of 8-bit bytes may be encoded as [len]":"[string]",". Here [string] is the string and [len] is a nonempty sequence of ASCII digits giving the length of [string] in decimal. The ASCII digits are for 0, for 1, and so on up through for 9. Extra zeros at the front of [len] are prohibited: [len] begins with exactly when [string] is empty. For example, the string "hello world!" is encoded as , i.e., "12:hello world!,". The empty string is encoded as "0:,". [len]":"[string]"," is called a netstring. [string] is called the interpretation of the netstring. 3. Sample code The following C code starts with a buffer buf of length len and prints it as a netstring. if (printf("%lu:",len) 999999999 bytes is bad */ if (getchar() != ':') barf(); buf = malloc(len + 1); /* malloc(0) is not portable */ if (!buf) barf(); if (fread(buf,1,len,stdin)
联系我们 contact @ memedata.com