RFC 4506 - XDR: External Data Representation Standard¶
XDR uses a language to describe data formats. The language can be used only to describe data; it is not a programming language.
XDR 是一种用来描述数据格式的语言。和 C 语言比较类似。
+--------+--------+...+--------+--------+...+--------+
| byte 0 | byte 1 |...|byte n-1| 0 |...| 0 | BLOCK
+--------+--------+...+--------+--------+...+--------+
|<-----------n bytes---------->|<------r bytes------>|
|<-----------n+r (where (n+r) mod 4 = 0)>----------->|
XDR 数据的 item 总是由 4 字节的 block 组成。如果 item 长度不是 4 的倍数,会用 0 补齐。
XDR 定义以下数据类型:
英文 | 中文 | 类型名 | 说明 |
---|---|---|---|
Integer | 整数 | int |
32 bits |
Unsigned Integer | 无符号整数 | unsigned int |
32 bits |
Enumeration | 枚举 | enum { name-identifier = constant, ... } |
|
Boolean | 布尔 | bool |
|
Hyper Integer and Unsigned Hyper Integer | 高精度整数和无符号高精度整数 | hyper , unsigned hyper |
64 bits |
Floating-point | 浮点数 | float |
|
Double-Precision Floating-point | 双精度浮点数 | double |
|
Quadruple-Precision Floating-point | 四精度浮点数 | quadruple |
128 bits |
Fixed-Length Opaque Data | 固定长度的不透明数据 | opaque identifier[n] |
|
Variable-Length Opaque Data | 可变长度的不透明数据 | opaque identifier<n> |
至多前四个字节用于表示长度,之后接数据,故最长 \(2^{32}-1\) bytes |
String | 字符串 | string object<m> |
至多前四个字节用于表示长度,之后接数据,故最长 \(2^{32}-1\) bytes |
Fixed-Length Array | 固定长度的数组 | type-name identifier[n] |
每个元素类型相同,但长度可以不同,比如 string 作为元素 |
Variable-Length Array | 可变长度的数组 | type-name identifier<m> |
至多前四个字节用于表示长度,之后接数据,故最长 \(2^{32}-1\) 个元素 |
Structure | 结构体 | struct { component-declaration-A; ... } |
|
Discriminated Union | 区分联合 | union switch (discriminant-declaration) { case discriminant-value-A: arm-declaration-A; ... default: default-declaration; } |
discriminant 可以是 int 、unsigned int 、enum ,均为 4 bytes,后接对应的数据 |
Void | 空 | void |
0 bytes,可以放在 union 中 |
Constant | 常量 | const |
不代表数据,只是符号常量 |
Typedef | 类型定义 | typedef |
|
Optional Data | 可选数据 | type-name * |
“可选”的意思是数据可能不存在,等价于最长长度为 1 的变长数组,常用于递归数据结构(链表的 next 指针) |
RFC 第五部分解释了 XDR 中一些设计的原因,比如块大小为四字节是为了内存对齐。
其余部分描述了 BNF 范式下的语法定义和一些范例。