RFC 1832
XDR: External Data Representation Standard
XDR: External Data Representation Standard π
Okay bestie, this is RFC 1832 from the Network Working Group, and it's giving Standards Track π It was written by R. Srinivasan of Sun Microsystems, dated August 1995. It doesn't say it obsoletes or updates anything, so no drama there β¨
Status of This Memo π
So this bit says this is an Internet standards track protocol for the Internet community, and they're literally asking for discussion and suggestions for improvements π«Ά For the current standardization state and status, check the current edition of the "Internet Official Protocol Standards" (STD 1). Distribution of this memo is unlimited π
Abstract π
This doc describes the External Data Representation Standard (XDR) protocol as it's currently deployed and accepted. The vibe is "this is what people actually use" π
Table of Contents π
1. INTRODUCTION 2
2. BASIC BLOCK SIZE 2
3. XDR DATA TYPES 3
3.1 Integer 3
3.2 Unsigned Integer 4
3.3 Enumeration 4
3.4 Boolean 4
3.5 Hyper Integer and Unsigned Hyper Integer 4
3.6 Floating-point 5
3.7 Double-precision Floating-point 6
3.8 Quadruple-precision Floating-point 7
3.9 Fixed-length Opaque Data 8
3.10 Variable-length Opaque Data 8
3.11 String 9
3.12 Fixed-length Array 10
3.13 Variable-length Array 10
3.14 Structure 11
3.15 Discriminated Union 11
3.16 Void 12
3.17 Constant 12
3.18 Typedef 13
3.19 Optional-data 14
3.20 Areas for Future Enhancement 15
4. DISCUSSION 15
5. THE XDR LANGUAGE SPECIFICATION 17
5.1 Notational Conventions 17
5.2 Lexical Notes 17
5.3 Syntax Information 18
5.4 Syntax Notes 19
6. AN EXAMPLE OF AN XDR DATA DESCRIPTION 20
7. TRADEMARKS AND OWNERS 21
APPENDIX A: ANSI/IEEE Standard 754-1985 22
APPENDIX B: REFERENCES 24
Security Considerations 24
Author's Address 24
1. INTRODUCTION β¨
XDR is a standard for describing and encoding data π¦ It's useful for moving data between different computer architectures, and it's been used to talk between machines as different as the SUN WORKSTATION*, VAX*, IBM-PC*, and Cray* π₯οΈ XDR fits into the ISO presentation layer, and it's roughly analogous in purpose to X.409, ISO Abstract Syntax Notation. The major difference is that XDR uses implicit typing, while X.409 uses explicit typing π€
XDR uses a language to describe data formats. Important: this language can only be used to describe data. It is not a programming language π It lets you describe intricate data formats in a concise way, which is way better than graphical representations (which are their own informal language) because those get incomprehensible once things get complex π The XDR language is similar to the C language [1], just as Courier [4] is similar to Mesa. Protocols like ONC RPC (Remote Procedure Call) and NFS* (Network File System) use XDR to describe the format of their data π¦
The XDR standard makes one big assumption: that bytes (or octets) are portable, where a byte is defined to be 8 bits of data π A given hardware device should encode the bytes onto the various media so that other hardware devices can decode them without loss of meaning. For example, the Ethernet* standard suggests that bytes be encoded in "little-endian" style [2], or least significant bit first π§
2. BASIC BLOCK SIZE πΈ
Every item is represented using a multiple of four bytes (or 32 bits) of data π The bytes are numbered 0 through n-1, and they're read or written to some byte stream so that byte m always comes before byte m+1. If the n bytes needed to contain the data aren't a multiple of four, then the n bytes get followed by enough (0 to 3) residual zero bytes, r, to make the total byte count a multiple of 4 β¨
We include the familiar graphic box notation to help illustrate things. In most illustrations, each box (outlined with plus signs at the corners and vertical bars and dashes) is one byte, and the ellipses (...) between boxes show zero or more additional bytes where needed π
+--------+--------+...+--------+--------+...+--------+
| byte 0 | byte 1 |...|byte n-1| 0 |...| 0 | BLOCK
+--------+--------+...+--------+--------+...+--------+
|<-----------n bytes---------->|<------r bytes------>|
|<-----------n+r (where (n+r) mod 4 = 0)>----------->|
3. XDR DATA TYPES π
Each section that follows describes a data type defined in the XDR standard, shows how it's declared in the language, and includes a graphic illustration of its encoding πΌοΈ
For each data type, we show a general paradigm declaration. Angle brackets (< and >) denote variable-length sequences of data, and square brackets ([ and ]) denote fixed-length sequences of data. "n", "m" and "r" denote integers π’ For the full language specification and more formal definitions of terms like "identifier" and "declaration", check section 5: "The XDR Language Specification". Some data types also come with more specific examples, and there's a much bigger one in section 6: "An Example of an XDR Data Description" π
3.1 Integer π₯
An XDR signed integer is a 32-bit datum that encodes an integer in the range [-2147483648,2147483647]. It's represented in two's complement notation. The most and least significant bytes are 0 and 3, respectively. Integers are declared like this:
int identifier;
(MSB) (LSB)
+-------+-------+-------+-------+
|byte 0 |byte 1 |byte 2 |byte 3 | INTEGER
+-------+-------+-------+-------+
<------------32 bits------------>
3.2. Unsigned Integer π
An XDR unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0,4294967295]. It's represented by an unsigned binary number whose most and least significant bytes are 0 and 3, respectively. An unsigned integer is declared like this:
unsigned int identifier;
(MSB) (LSB)
+-------+-------+-------+-------+
|byte 0 |byte 1 |byte 2 |byte 3 | UNSIGNED INTEGER
+-------+-------+-------+-------+
<------------32 bits------------>
3.3 Enumeration π
Enumerations have the same representation as signed integers. They're handy for describing subsets of the integers π§ Enumerated data is declared like this:
enum { name-identifier = constant, ... } identifier;
For example, the three colors red, yellow, and blue could be described by an enumerated type:
enum { RED = 2, YELLOW = 3, BLUE = 5 } colors;
It is an error to encode as an enum any other integer than those that have been given assignments in the enum declaration. Don't be that person π ββοΈ
3.4 Boolean π¦
Booleans are important enough, and show up often enough, that the standard gives them their own explicit type π Booleans are declared like this:
bool identifier;
This is equivalent to:
enum { FALSE = 0, TRUE = 1 } identifier;
3.5 Hyper Integer and Unsigned Hyper Integer π
The standard also defines 64-bit (8-byte) numbers called hyper integer and unsigned hyper integer. Their representations are the obvious extensions of integer and unsigned integer from above π
They're represented in two's complement notation. The most and least significant bytes are 0 and 7, respectively. Their declarations:
hyper identifier; unsigned hyper identifier;
(MSB) (LSB)
+-------+-------+-------+-------+-------+-------+-------+-------+
|byte 0 |byte 1 |byte 2 |byte 3 |byte 4 |byte 5 |byte 6 |byte 7 |
+-------+-------+-------+-------+-------+-------+-------+-------+
<----------------------------64 bits---------------------------->
HYPER INTEGER
UNSIGNED HYPER INTEGER
3.6 Floating-point π§
The standard defines the floating-point data type "float" (32 bits or 4 bytes) πΈ The encoding used is the IEEE standard for normalized single-precision floating-point numbers [3]. The following three fields describe the single-precision floating-point number:
- S: The sign of the number. Values 0 and 1 represent positive and negative, respectively. One bit.
- E: The exponent of the number, base 2. 8 bits are devoted to this field. The exponent is biased by 127.
- F: The fractional part of the number's mantissa, base 2. 23 bits are devoted to this field.
So the floating-point number is described by:
(-1)**S * 2**(E-Bias) * 1.F
It's declared like this:
float identifier;
+-------+-------+-------+-------+
|byte 0 |byte 1 |byte 2 |byte 3 | SINGLE-PRECISION
S| E | F | FLOATING-POINT NUMBER
+-------+-------+-------+-------+
1|<- 8 ->|<-------23 bits------>|
<------------32 bits------------>
Just like the most and least significant bytes of a number are 0 and 3, the most and least significant bits of a single-precision floating-point number are 0 and 31. The beginning bit (and most significant bit) offsets of S, E, and F are 0, 1, and 9, respectively. Note that these numbers refer to the mathematical positions of the bits, and not to their actual physical locations (which vary from medium to medium) π«Ά
The IEEE specifications should be consulted for the encoding of signed zero, signed infinity (overflow), and denormalized numbers (underflow) [3]. According to IEEE specifications, "NaN" (not a number) is system dependent and should not be interpreted within XDR as anything other than "NaN" π
3.7 Double-precision Floating-point β¨
okay bestie, let's talk about the double-precision float π XDR defines the encoding for the double type, which is 64 bits or 8 bytes. It uses the IEEE standard for normalized double-precision floating-point numbers [3]. The number is split into three fields:
- S: the sign. 0 means positive and 1 means negative. That's one bit. π«Ά
- E: the exponent, base 2. It gets 11 bits and is biased by 1023. π₯
- F: the fractional part of the mantissa, base 2. It gets 52 bits. π
So the number works out to:
(-1)**S * 2**(E-Bias) * 1.F
Here's how you declare it and how the bytes are laid out:
double identifier;
+------+------+------+------+------+------+------+------+
|byte 0|byte 1|byte 2|byte 3|byte 4|byte 5|byte 6|byte 7|
S| E | F |
+------+------+------+------+------+------+------+------+
1|<--11-->|<-----------------52 bits------------------->|
<-----------------------64 bits------------------------->
DOUBLE-PRECISION FLOATING-POINT
Bytes 0 and 3 are the most and least significant bytes of a number, and the same idea works for bits here. The most significant bit is 0 and the least significant bit is 63. The starting bit offsets for S, E, and F are 0, 1, and 12. These are just mathematical positions, though, NOT the actual physical locations, which change depending on the medium. π§
For signed zero, signed infinity (overflow), and denormalized numbers (underflow), the IEEE specs are the place to look [3]. And the "NaN" (not a number) is system dependent, so XDR should treat it as nothing other than "NaN" π
3.8 Quadruple-precision Floating-point π
next up, quadruple precision! π XDR defines the encoding for the quadruple type, which is 128 bits or 16 bytes. The design is a simple analog of the single and double-precision encodings, using one form of IEEE double extended precision. It also has three fields:
- S: the sign. 0 means positive and 1 means negative. One bit.
- E: the exponent, base 2. It gets 15 bits and is biased by 16383.
- F: the fractional part of the mantissa, base 2. It gets 112 bits.
The formula is the same vibe as before:
(-1)**S * 2**(E-Bias) * 1.F
The declaration and layout look like this:
quadruple identifier;
+------+------+------+------+------+------+-...--+------+
|byte 0|byte 1|byte 2|byte 3|byte 4|byte 5| ... |byte15|
S| E | F |
+------+------+------+------+------+------+-...--+------+
1|<----15---->|<-------------112 bits------------------>|
<-----------------------128 bits------------------------>
QUADRUPLE-PRECISION FLOATING-POINT
The most significant bit is 0 and the least significant bit is 127. The starting bit offsets for S, E, and F are 0, 1, and 16. Again, those are mathematical positions, NOT physical locations (which vary from medium to medium) π¦
The encodings for signed zero, signed infinity (overflow), and denormalized numbers are analogs of the single and double-precision ones [5], [6]. The "NaN" encoding is system dependent too, so XDR should not read it as anything other than "NaN" β¨
3.9 Fixed-length Opaque Data π
sometimes you just need to pass around raw, uninterpreted data between machines, and that's what "opaque" data is for π¦ It gets declared like this:
opaque identifier[n];
The constant n is the fixed number of bytes needed to hold the opaque data. If n isn't a multiple of four, the n bytes get padded with enough residual zero bytes, r (anywhere from 0 to 3), so the total count is a multiple of four π
0 1 ...
+--------+--------+...+--------+--------+...+--------+
| byte 0 | byte 1 |...|byte n-1| 0 |...| 0 |
+--------+--------+...+--------+--------+...+--------+
|<-----------n bytes---------->|<------r bytes------>|
|<-----------n+r (where (n+r) mod 4 = 0)------------>|
FIXED-LENGTH OPAQUE
3.10 Variable-length Opaque Data πΈ
now the variable-length version, which is basically "counted" opaque data π§ You've got a sequence of n arbitrary bytes, numbered 0 through n-1. The count n gets encoded as an unsigned integer (like we talked about earlier), and then the n bytes follow.
Byte m always comes before byte m+1, and byte 0 always comes right after the length. Same padding rule as before: if n isn't a multiple of four, you add 0 to 3 residual zero bytes, r, so the total is a multiple of four. You declare it one of two ways:
opaque identifier<m>;
or
opaque identifier<>;
The constant m is the upper bound on how many bytes the sequence can hold. If you leave m out, like in the second form, it's assumed to be (2**32) - 1, which is the max length. The constant m usually comes from a protocol spec. For example, a filing protocol might say the maximum data transfer size is 8192 bytes, so you'd write:
opaque filedata<8192>;
Here's the layout:
0 1 2 3 4 5 ...
+-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+
| length n |byte0|byte1|...| n-1 | 0 |...| 0 |
+-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+
|<-------4 bytes------->|<------n bytes------>|<---r bytes--->|
|<----n+r (where (n+r) mod 4 = 0)---->|
VARIABLE-LENGTH OPAQUE
π¨ It's an error to encode a length bigger than the maximum the specification describes. No sneaking past the limit!
3.11 String π
strings are basically the same deal, but for ASCII bytes π A string of n bytes (numbered 0 through n-1) gets encoded as the count n, as an unsigned integer (as described above), followed by the n bytes of the string. Byte m comes before byte m+1, and byte 0 comes right after the length. If n isn't a multiple of four, you pad with 0 to 3 residual zero bytes, r, so the total is a multiple of four.
Counted strings get declared like this:
string object<m>;
or
string object<>;
Same rules for m: it's the upper bound on the number of bytes the string can hold, and if you skip it, it's (2**32) - 1. It usually lives in a protocol spec too. For instance, a filing protocol might say a file name can be no longer than 255 bytes:
string filename<255>;
And the layout:
0 1 2 3 4 5 ...
+-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+
| length n |byte0|byte1|...| n-1 | 0 |...| 0 |
+-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+
|<-------4 bytes------->|<------n bytes------>|<---r bytes--->|
|<----n+r (where (n+r) mod 4 = 0)---->|
STRING
π¨ Same warning as before: encoding a length greater than the maximum in the specification is an error. π
3.12 Fixed-length Array π¦
fixed-length arrays are for homogeneous elements, meaning they're all the same type π The declaration looks like this:
type-name identifier[n];
The elements, numbered 0 through n-1, get encoded one at a time in their natural order, 0 through n-1. Each element's size is a multiple of four bytes. Here's the twist: even though every element is the same type, they can still be different sizes. For example, in a fixed-length array of strings, every element is type "string", but each one can have a different length π
+---+---+---+---+---+---+---+---+...+---+---+---+---+
| element 0 | element 1 |...| element n-1 |
+---+---+---+---+---+---+---+---+...+---+---+---+---+
|<--------------------n elements------------------->|
FIXED-LENGTH ARRAY
3.13 Variable-length Array β¨
counted arrays are the variable-length version, and they're giving flexibility π§ The array gets encoded as the element count n (an unsigned integer), followed by the encoding of each element, starting with element 0 and going all the way through element n-1. The declarations look like this:
type-name identifier<m>;
or
type-name identifier<>;
The constant m is the maximum number of elements the array can have. If you leave it out, like in the second form, it's assumed to be (2**32) - 1.
0 1 2 3
+--+--+--+--+--+--+--+--+--+--+--+--+...+--+--+--+--+
| n | element 0 | element 1 |...|element n-1|
+--+--+--+--+--+--+--+--+--+--+--+--+...+--+--+--+--+
|<-4 bytes->|<--------------n elements------------->|
COUNTED ARRAY
π¨ It's an error to encode a value of n that's greater than the maximum in the specification.
3.14 Structure π
last one for this part: structures! π They get declared like this:
struct {
component-declaration-A;
component-declaration-B;
...
} identifier;
The components are encoded in the same order they were declared in the structure. Each component's size is a multiple of four bytes, but the components can be different sizes from each other π«Ά
+-------------+-------------+...
| component A | component B |... STRUCTURE
+-------------+-------------+...
3.15 Discriminated Union
Okay so a discriminated union is basically a type with a little label (the discriminant) up front, and then the actual data is one of a bunch of pre-agreed options, picked by that label π
The label can be an int, an unsigned int, or an enumerated type like bool π Each option is called an "arm," and the label's value tells you which arm you're looking at, so the encoding is implied by the label β¨
Here's how it's declared:
union switch (discriminant-declaration) {
case discriminant-value-A:
arm-declaration-A;
case discriminant-value-B:
arm-declaration-B;
...
default: default-declaration;
} identifier;
Every case keyword is followed by a legal value of the discriminant π¦ The default arm is optional πΈ If you don't include one, then a valid encoding of the union can't take on discriminant values that weren't specified, which is kind of a vibe check on the label π Also, the size of the implied arm is always a multiple of four bytes, so everything lines up nicely π«Ά
The whole union gets encoded as its discriminant, followed by the encoding of the implied arm. Picture it like this:
0 1 2 3
+---+---+---+---+---+---+---+---+
| discriminant | implied arm | DISCRIMINATED UNION
+---+---+---+---+---+---+---+---+
|<---4 bytes--->|
3.16 Void
An XDR void is a 0-byte quantity, aka literally nothing, and it's iconic π Voids are super useful for describing operations that take no data as input or no data as output π They're also great inside unions, where some arms carry data and others don't π§ The declaration is just this:
void;
And here's how a void looks:
++
|| VOID
++
--><-- 0 bytes
3.17 Constant
The data declaration for a constant follows this form:
const name-identifier = n;
const is used to give a symbolic name to a constant, and it doesn't declare any data at all π That symbolic constant can be used anywhere a regular constant could be used β¨ For example, this defines a symbolic constant DOZEN, equal to 12:
const DOZEN = 12;
3.18 Typedef
typedef also doesn't declare any data, but it lets you define new identifiers for declaring data π
The syntax is:
typedef declaration;
The new type name is actually the variable name in the declaration part of the typedef π For example, this defines a new type called "eggbox" using an existing type called "egg":
typedef egg eggbox[DOZEN];
Variables declared with the new type name have the same type that the new type name would have in the typedef, if it were treated as a variable π So these two declarations are equivalent when declaring the variable "fresheggs":
eggbox fresheggs; egg fresheggs[DOZEN];
When a typedef involves a struct, enum, or union definition, there's another syntax that's preferred for defining the same type π¦ In general, a typedef of this form:
typedef <<struct, union, or enum definition>> identifier;
can be converted to the alternative form by removing the typedef part and putting the identifier after the struct, union, or enum keyword, instead of at the end π For example, here are the two ways to define the type "bool":
typedef enum { /* using typedef */
FALSE = 0,
TRUE = 1
} bool;
enum bool { /* preferred alternative */
FALSE = 0,
TRUE = 1
};
The reason this syntax is preferred is that you don't have to wait until the end of a declaration to figure out the name of the new type π«Ά Main character energy, no waiting required β¨
3.19 Optional-data
Optional-data is one kind of union, but it shows up so often that it gets its own special syntax πΈ It's declared like this:
type-name *identifier;
That's equivalent to this union:
union switch (bool opted) {
case TRUE:
type-name element;
case FALSE:
void;
} identifier;
It's also equivalent to the following variable-length array declaration, since the boolean "opted" can be read as the length of the array π§
type-name identifier<1>;
Optional-data isn't that exciting on its own, but it's super useful for describing recursive data structures like linked lists and trees π For example, this defines a type "stringlist" that encodes lists of arbitrary length strings:
struct *stringlist {
string item<>;
stringlist next;
};
It could have been declared equivalently as this union:
union stringlist switch (bool opted) {
case TRUE:
struct {
string item<>;
stringlist next;
} element;
case FALSE:
void;
};
or as a variable-length array:
struct stringlist<1> {
string item<>;
stringlist next;
};
Both of those obscure what the stringlist type is really about, so the optional-data declaration is preferred over both of them π Optional-data also has a close connection to how recursive data structures get represented in high-level languages like Pascal or C with pointers π In fact, the syntax is the same as the C language's syntax for pointers π
3.20 Areas for Future Enhancement
Okay, real talk: XDR doesn't have representations for bit fields and bitmaps, because the standard is based on bytes π Packed (or binary-coded) decimals are missing too π
The intent of XDR was never to describe every kind of data people have ever sent or will ever want to send between machines πΈ It only describes the most commonly used data types of high-level languages like Pascal or C, so applications written in those languages can talk to each other easily over some medium π«Ά
You could imagine extensions to XDR that let it describe almost any existing protocol, such as TCP π¦ The minimum needed for that is support for different block sizes and byte-orders π The XDR discussed here could then be considered the 4-byte big-endian member of a larger XDR family π Bestie, a whole family, iconic β¨
4. DISCUSSION π
(1) Why use a language for describing data? What's wrong with diagrams?
There are lots of advantages to using a data-description language like XDR instead of diagrams π Languages are more formal than diagrams, which leads to less ambiguous descriptions of data π They're also easier to understand, and they let you think about other issues instead of the low-level details of bit-encoding π§ There's also a close analogy between XDR's types and a high-level language like C or Pascal, which makes implementing XDR encoding and decoding modules an easier task π₯ And finally, the language specification is itself an ASCII string that can be passed from machine to machine to do on-the-fly data interpretation π€―
(2) Why is there only one byte-order for an XDR unit?
Supporting two byte-orderings would need a higher-level protocol to determine which byte-order the data is encoded in π Since XDR isn't a protocol, that can't be done πΈ The upside, though, is that XDR data can be written to a magnetic tape, for example, and any machine will be able to interpret it, because no higher-level protocol is needed to figure out the byte-order π
(3) Why is the XDR byte-order big-endian instead of little-endian? Isn't this unfair to little-endian machines such as the VAX(r), which has to convert from one form to the other?
Yes, it's unfair π But having only one byte-order means you have to be unfair to somebody π
Many architectures, such as the Motorola 68000* and IBM 370*, support the big-endian byte-order π¦
(4) Why is the XDR unit four bytes wide?
There's a tradeoff in choosing the XDR unit size π Picking a small size like two makes the encoded data small, but it causes alignment problems for machines that aren't aligned on those boundaries π A large size like eight means the data will be aligned on virtually every machine, but it makes the encoded data grow too big π So we chose four as a compromise β¨ Four is big enough to support most architectures efficiently, except for rare machines like the eight-byte aligned Cray* π§ Four is also small enough to keep the encoded data at a reasonable size π
(5) Why must variable-length data be padded with zeros?
It's desirable that the same data encodes into the same thing on all machines, so that encoded data can be meaningfully compared or checksummed π«Ά Forcing the padded bytes to be zero is what makes sure of that π₯
(6) Why is there no explicit data-typing?
Data-typing has a pretty high cost for whatever small advantages it might have π One cost is the expansion of data from the inserted type fields π Another is the added cost of interpreting those type fields and acting on them π And most protocols already know what type they expect, so data-typing just supplies redundant information π That said, you can still get the benefits of data-typing with XDR πΈ One way is to encode two things: first, a string that's the XDR data description of the encoded data, and then the encoded data itself π Another way is to assign a value to all the types in XDR, then define a universal type that takes this value as its discriminant and, for each value, describes the corresponding data type π
5. THE XDR LANGUAGE SPECIFICATION π
5.1 Notational Conventions
This spec uses an extended Back-Naur Form notation to describe the XDR language π¦ Here's the quick rundown:
- The characters
'|','(',')','[',']','"', and'*'are special π - Terminal symbols are strings of any characters surrounded by double quotes π
- Non-terminal symbols are strings of non-special characters πΈ
- Alternative items are separated by a vertical bar (
"|") π§ - Optional items are enclosed in brackets π
- Items are grouped together by enclosing them in parentheses π«Ά
- A
'*'following an item means 0 or more occurrences of that item π
For example, consider this pattern:
"a " "very" (", " "very")* [" cold " "and "] " rainy "
("day" | "night")
An infinite number of strings match this pattern π€― A few of them are:
"a very rainy day"
"a very, very rainy day"
"a very cold and rainy day"
"a very, very, very cold and rainy night"
5.2 Lexical Notes
A few lexical rules to know πΈ
- (1) Comments begin with
/*and end with*/π - (2) White space separates items and is otherwise ignored π
- (3) An identifier is a letter followed by an optional sequence of letters, digits, or underbar (
'_') π§ The case of identifiers is not ignored, so capitalization matters, babe π - (4) A constant is a sequence of one or more decimal digits, optionally preceded by a minus-sign (
'-') β¨
5.3 Syntax Information β¨
okay so this is the whole grammar in one place, bestie π«Ά it's basically the cheat sheet for how a declaration, a value, a type-specifier and a definition all snap together. read it top to bottom and it clicks.
declaration:
type-specifier identifier
| type-specifier identifier "[" value "]"
| type-specifier identifier "<" [ value ] ">"
| "opaque" identifier "[" value "]"
| "opaque" identifier "<" [ value ] ">"
| "string" identifier "<" [ value ] ">"
| type-specifier "*" identifier
| "void"
value:
constant
| identifier
type-specifier:
[ "unsigned" ] "int"
| [ "unsigned" ] "hyper"
| "float"
| "double"
| "quadruple"
| "bool"
| enum-type-spec
| struct-type-spec
| union-type-spec
| identifier
enum-type-spec:
"enum" enum-body
enum-body:
"{"
( identifier "=" value )
( "," identifier "=" value )*
"}"
struct-type-spec:
"struct" struct-body
struct-body:
"{"
( declaration ";" )
( declaration ";" )*
"}"
union-type-spec:
"union" union-body
union-body:
"switch" "(" declaration ")" "{"
( "case" value ":" declaration ";" )
( "case" value ":" declaration ";" )*
[ "default" ":" declaration ";" ]
"}"
constant-def:
"const" identifier "=" constant ";"
type-def:
"typedef" declaration ";"
| "enum" identifier enum-body ";"
| "struct" identifier struct-body ";"
| "union" identifier union-body ";"
definition:
type-def
| constant-def
specification:
definition *
5.4 Syntax Notes π
these are the rules that sit on top of the grammar, and honestly they're the ones that save you from the sneaky bugs π
(1) keywords are off-limits as identifiers. these ones are reserved, so don't try to name anything with them: bool, case, const, default, double, quadruple, enum, float, hyper, opaque, string, struct, switch, typedef, union, unsigned and void.
(2) array sizes are unsigned constants only. only unsigned constants may be used as size specifications for arrays. if you use an identifier there, it must have been declared previously as an unsigned constant in a "const" definition. no vibes, just receipts π§Ύ
(3) constants and types share one name space. constant and type identifiers within the scope of a specification are in the same name space and must be declared uniquely within this scope. so you can't have a constant and a type both called the same thing and expect it to be fine π
(4) struct and union fields need unique names. similarly, variable names must be unique within the scope of struct and union declarations. nested struct and union declarations create new scopes, so the same name can show up again inside a nested one πͺ
(5) union discriminants have rules. the discriminant of a union must be of a type that evaluates to an integer. that is, "int", "unsigned int", "bool", an enumerated type or any typedefed type that evaluates to one of these is legal. also, the case values must be one of the legal values of the discriminant. finally, a case value may not be specified more than once within the scope of a union declaration. one case per value, no duplicates, we don't do drama here π
6. AN EXAMPLE OF AN XDR DATA DESCRIPTION β¨
okay this part is so cute π₯Ή here's a short XDR description of a thing called a "file", the kind of thing you'd use to send files from one machine to another π
const MAXUSERNAME = 32; /* max length of a user name */
const MAXFILELEN = 65535; /* max length of a file */
const MAXNAMELEN = 255; /* max length of a file name */
/*
* Types of files:
*/
enum filekind {
TEXT = 0, /* ascii data */
DATA = 1, /* raw data */
EXEC = 2 /* executable */
};
/*
* File information, per kind of file:
*/
union filetype switch (filekind kind) {
case TEXT:
void; /* no extra information */
case DATA:
string creator<MAXNAMELEN>; /* data creator */
case EXEC:
string interpretor<MAXNAMELEN>; /* program interpretor */
};
/*
* A complete file:
*/
struct file {
string filename<MAXNAMELEN>; /* name of file */
filetype type; /* info about file */
string owner<MAXUSERNAME>; /* owner of file */
opaque data<MAXFILELEN>; /* file data */
};
now say there's a user named "john" who wants to store his lisp program "sillyprog" that just contains the data "(quit)". π§βπ» his file gets encoded like this:
OFFSET HEX BYTES ASCII COMMENTS
------ --------- ----- --------
0 00 00 00 09 .... -- length of filename = 9
4 73 69 6c 6c sill -- filename characters
8 79 70 72 6f ypro -- ... and more characters ...
12 67 00 00 00 g... -- ... and 3 zero-bytes of fill
16 00 00 00 02 .... -- filekind is EXEC = 2
20 00 00 00 04 .... -- length of interpretor = 4
24 6c 69 73 70 lisp -- interpretor characters
28 00 00 00 04 .... -- length of owner = 4
32 6a 6f 68 6e john -- owner characters
36 00 00 00 06 .... -- length of file data = 6
40 28 71 75 69 (qui -- file data bytes ...
44 74 29 00 00 t).. -- ... and 2 zero-bytes of fill
7. TRADEMARKS AND OWNERS π·οΈ
here are the trademarks and who owns them, all lined up nicely πΈ
SUN WORKSTATION Sun Microsystems, Inc.
VAX Digital Equipment Corporation
IBM-PC International Business Machines Corporation
Cray Cray Research
NFS Sun Microsystems, Inc.
Ethernet Xerox Corporation.
Motorola 68000 Motorola, Inc.
IBM 370 International Business Machines Corporation
APPENDIX A: ANSI/IEEE Standard 754-1985 π
the definitions of NaNs, signed zero and infinity, and denormalized numbers from [3] are copied here for convenience π§ the quadruple-precision definitions are analogs of the single and double-precision ones, and they're defined in [3].
in what follows, 'S' stands for the sign bit, 'E' for the exponent, and 'F' for the fractional part. the symbol 'u' stands for an undefined bit (0 or 1).
for single-precision floating point numbers:
Type S (1 bit) E (8 bits) F (23 bits)
---- --------- ---------- -----------
signalling NaN u 255 (max) .0uuuuu---u
(with at least
one 1 bit)
quiet NaN u 255 (max) .1uuuuu---u
negative infinity 1 255 (max) .000000---0
positive infinity 0 255 (max) .000000---0
negative zero 1 0 .000000---0
positive zero 0 0 .000000---0
for double-precision floating point numbers:
Type S (1 bit) E (11 bits) F (52 bits)
---- --------- ----------- -----------
signalling NaN u 2047 (max) .0uuuuu---u
(with at least
one 1 bit)
quiet NaN u 2047 (max) .1uuuuu---u
negative infinity 1 2047 (max) .000000---0
positive infinity 0 2047 (max) .000000---0
negative zero 1 0 .000000---0
positive zero 0 0 .000000---0
for quadruple-precision floating point numbers:
Type S (1 bit) E (15 bits) F (112 bits)
---- --------- ----------- ------------
signalling NaN u 32767 (max) .0uuuuu---u
(with at least
one 1 bit)
quiet NaN u 32767 (max) .1uuuuu---u
negative infinity 1 32767 (max) .000000---0
positive infinity 0 32767 (max) .000000---0
negative zero 1 0 .000000---0
positive zero 0 0 .000000---0
and subnormal numbers are represented like this π«§
Precision Exponent Value
--------- -------- -----
Single 0 (-1)**S * 2**(-126) * 0.F
Double 0 (-1)**S * 2**(-1022) * 0.F
Quadruple 0 (-1)**S * 2**(-16382) * 0.F
APPENDIX B: REFERENCES π
here's the reading list, with every detail kept π
- [1] Brian W. Kernighan & Dennis M. Ritchie, "The C Programming Language", Bell Laboratories, Murray Hill, New Jersey, 1978.
- [2] Danny Cohen, "On Holy Wars and a Plea for Peace", IEEE Computer, October 1981.
- [3] "IEEE Standard for Binary Floating-Point Arithmetic", ANSI/IEEE Standard 754-1985, Institute of Electrical and Electronics Engineers, August 1985.
- [4] "Courier: The Remote Procedure Call Protocol", XEROX Corporation, XSIS 038112, December 1981.
- [5] "The SPARC Architecture Manual: Version 8", Prentice Hall, ISBN 0-13-825001-4.
- [6] "HP Precision Architecture Handbook", June 1987, 5954-9906.
- [7] Srinivasan, R., "Remote Procedure Call Protocol Version 2", RFC 1831, Sun Microsystems, Inc., August 1995.
Security Considerations π
okay, the serious bit: Security issues are not discussed in this memo. π± please keep that in mind when you're building on top of this.
Author's Address π
the author, with all the contact details π«Ά
- Raj Srinivasan
- Sun Microsystems, Inc.
- ONC Technologies
- 2550 Garcia Avenue
- M/S MTV-5-40
- Mountain View, CA 94043
- USA
- Phone: 415-336-2478
- Fax: 415-336-6015
- EMail: raj@eng.sun.com
that's the whole thing, bestie πβ¨
π¦ where this page comes from
- ietf: rfc1832 1995-08
- π girlified by claude-haiku-5-5 on : a language model's rewrite of the original, under its terms