Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
  Previous   Contents   Next 
   
 
Appendix C

XDR Protocol Specification

This appendix contains the XDR Protocol Language Specification. It covers the following topics:

XDR Protocol Introduction

External data representation (XDR) is a standard for the description and encoding of data. The XDR protocol is useful for transferring data between different computer architectures and has been used to communicate data between very diverse machines. XDR fits into the ISO reference model's presentation layer (layer 6) and is roughly analogous in purpose to X.409, ISO Abstract Syntax Notation. The major difference between the two is that XDR uses implicit typing, while X.409 uses explicit typing.

XDR uses a language to describe data formats and can only be used to describe data. It is not a programming language. This language enables you to describe intricate data formats in a concise manner. The XDR language is similar to the C language. Protocols such as RPC and NFS use XDR to describe the format of their data.

The XDR standard assumes that bytes, or octets, are portable and that a byte is defined to be 8 bits of data.

Graphic Box Notation

This appendix uses graphic box notation for illustration and comparison. In most illustrations, each box depicts a byte. The representation of all items requires a multiple of 4 bytes (or 32 bits) of data. The bytes are numbered 0 through n -1. The bytes are read or written to some byte stream such that byte m always precedes byte m+1. The n bytes are followed by enough (0 to 3) residual zero bytes, r, to make the total byte count a multiple of four. Ellipses (...) between boxes show zero or more additional bytes where required, as shown in the following illustration.

Basic Block Size

Choosing the XDR block size requires a tradeoff. Choosing a small size such as 2 makes the encoded data small, but causes alignment problems for machines that are not aligned on these boundaries. A large size such as 8 means the data is aligned on virtually every machine, but causes the encoded data to grow too large. Four was chosen as a compromise. Four is big enough to support most architectures efficiently.

This basic block size of 4 does not mean that the computers cannot utilize standard XDR, just that they do so at a greater overhead per data item than 4-byte (32-bit) architectures. Four is also small enough to keep the encoded data restricted to a reasonable size.

The same data should encode into an equivalent result on all machines so that encoded data can be compared or checksummed. So, variable-length data must be padded with trailing zeros.

XDR Data Type Declarations

Each of the sections that follow:

  • Describes a data type defined in the XDR standard

  • Shows how that data type is declared in the language

  • Includes a graphic illustration of the encoding

For each data type in the language a general paradigm declaration is shown. Note that 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, refer to "XDR Language Specification".

Some data types include specific examples. A more extensive example is given in the section "XDR Data Description".

Signed Integer

An XDR signed integer is a 32-bit datum that encodes an integer in the range [-2147483648,2147483647]. The integer is represented in two's complement notation; the most and least significant bytes are 0 and 3, respectively.

Declaration

Integers are declared:

int identifier;

Signed Integer Encoding

Unsigned Integer

An XDR unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0, 4294967295]. The integer is represented by an unsigned binary number that has most- and least-significant bytes of 0 and 3 respectively.

Declaration

An unsigned integer is declared as follows.

unsigned int identifier;

Unsigned Integer Encoding

Enumerations

Enumerations have the same representation as signed integers and are handy for describing subsets of the integers. The encoding for enumerations is the same as shown in "Signed Integer Encoding".

Enumerated data is declared as follows.

enum {name-identifier = constant, ... } identifier;

For example, an enumerated type could represent the three colors red, yellow, and blue as follows.

enum {RED = 2, YELLOW = 3, BLUE = 5} colors;

Do not assign to an enum an integer that has not been assigned in the enum declaration.

Booleans

Booleans are important enough and occur frequently enough to warrant their own explicit type in the standard. Booleans are integers of value 0 or 1. The encoding for Booleans is the same as show in "Signed Integer Encoding".

Booleans are declared as follows.

bool identifier;

This is equivalent to:

enum {FALSE = 0, TRUE = 1} identifier;

Hyper Integer and Unsigned Hyper Integer

The standard defines 64-bit (8-byte) numbers called hyper int and unsigned hyper int with representations that are the obvious extensions of integer and unsigned integer, defined previously. They are represented in two's complement notation; the most-significant and least-significant bytes are 0 and 7, respectively.

Declaration

Hyper integers are declared as follows.

hyper int  identifier;
unsigned hyper int identifier;  

Hyper Integer Encoding

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 [1]. 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. Eight bits are in this field. The exponent is biased by 127.

F: The fractional part of the number's mantissa, base 2. Twenty-three bits are in this field.

Therefore, the floating-point number is described by.

(-1)**S * 2**(E-Bias) * 1.F 

Declaration

Single-precision floating-point data is declared as follows.

float identifier; 

Double-precision floating-point data is declared as follows.

double identifier; 
 
 
 
  Previous   Contents   Next