DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
XDR/RPC protocol specification

XDR data type declarations

The following sections describe data types defined in the XDR standard, how they are declared in the language, and include graphic illustrations of the encoding.

We show a general paradigm declaration for each data type in the language. 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 and more formal definitions of terms such as ``identifier'' and ``declaration'', refer to ``The XDR language specification''.

For some data types, more specific examples are included. A more extensive example of a data description is in ``An example of an XDR data description''.

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.

Integers are declared as follows:

   int identifier;
   (MSB)                   (LSB)
   +-------+-------+-------+-------+
   |byte 0 |byte 1 |byte 2 |byte 3 |
   +-------+-------+-------+-------+
   <------------32 bits------------>

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 whose most and least significant bytes are 0 and 3, respectively.

An unsigned integer is declared as follows:

   unsigned int identifier;
   (MSB)                   (LSB)
   +-------+-------+-------+-------+
   |byte 0 |byte 1 |byte 2 |byte 3 |
   +-------+-------+-------+-------+
   <------------32 bits------------>
Enumerations have the same representation as signed integers and are handy for describing subsets of the integers.

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;
It is an error to assign to an enum an integer that has not been assigned in the enum declaration.

For a description of encoding, see ``Integer''.

Boolean

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. Booleans are declared as follows:

   bool identifier;
This is equivalent to:
   enum { FALSE = 0, TRUE = 1 } identifier;
For a description of encoding, see ``Integer''.

Hyper integer and unsigned hyper integer

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

Hyper integers are declared as follows:

   hyper int identifier;

unsigned hyper int identifier;

   (MSB)                                                   (LSB)
   +-------+-------+-------+-------+-------+-------+-------+-------+
   |byte 0 |byte 1 |byte 2 |byte 3 |byte 4 |byte 5 |byte 6 |byte 7 |
   +-------+-------+-------+-------+-------+-------+-------+-------+
   <----------------------------64 bits---------------------------->

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 (see reference 1 in ``References''). 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 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.
Therefore, the floating-point number is described by ((-1)**S)*(2**(E-Bias))*1.F. Single-precision floating-point data is declared as follows:
   float identifier;
   +-------+-------+-------+-------+
   |byte 0 |byte 1 |byte 2 |byte 3 |
   S|   E   |           F          |
   +-------+-------+-------+-------+
   1|<- 8 ->|<-------23 bits------>|
   <------------32 bits------------>
Just as the most and least significant bytes of an integer 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: These offsets refer to the logical positions of the bits, not to their physical locations (which vary from medium to medium).

The IEEE specifications should be consulted about the encoding for signed zero, signed infinity (overflow), and denormalized numbers (underflow) (see reference 1 in ``References''). According to IEEE specifications, the NaN (not a number) is system dependent and should not be used externally.

Double-precision floating-point

The standard defines the encoding for the double-precision floating-point data type ``double'' (64 bits or 8 bytes). The encoding used is the IEEE standard for normalized double-precision floating-point numbers (see reference 1 in ``References''). The standard encodes the following three fields, which describe the double-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. 11 bits are devoted to this field. The exponent is biased by 1023.

F
The fractional part of the number's mantissa, base 2. 52 bits are devoted to this field.
Therefore, the floating-point number is described by ((-1)**S)*(2**(E-Bias))*1.F. and declared as:
   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------------------------->
Just as the most and least significant bytes of an integer are 0 and 3, the most and least significant bits of a double-precision floating- point number are 0 and 63. The beginning bit (and most significant bit) offsets of S, E , and F are 0, 1, and 12, respectively.


NOTE: These offsets refer to the logical positions of the bits, not to their physical locations (which vary from medium to medium).

The IEEE specifications should be consulted about the encoding for signed zero, signed infinity (overflow), and denormalized numbers (underflow) (see reference 1 in ``References''). According to IEEE specifications, the NaN (not a number) is system dependent and should not be used externally.

Fixed-length opaque data

At times, fixed-length uninterpreted data needs to be passed among machines. This data is called ``opaque''.

Opaque data is declared as follows:

   opaque identifier[n];
where the constant n is the (static) number of bytes necessary to contain the opaque data.

The n bytes are followed by enough (0 to 3) residual zero bytes, r, to make the total byte count of the opaque object 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)------------>|

Variable-length opaque data

The standard also provides for variable-length (counted) opaque data, defined as a sequence of n (numbered 0 through n-1) arbitrary bytes to be the number n encoded as an unsigned integer (as described below), and followed by the n bytes of the sequence.

Byte b of the sequence always precedes byte b+1 of the sequence, and byte 0 of the sequence always follows the sequence's length (count). The n bytes are followed by enough (0 to 3) residual zero bytes, r, to make the total byte count a multiple of four.

Variable-length opaque data is declared in the following way:

   opaque identifier<m>;
or
   opaque identifier<>;
The constant m denotes an upper bound of the number of bytes that the sequence may contain. If m is not specified, as in the second declaration, it is assumed to be (2**32)-1, the maximum length. For example, a filing protocol may state that the maximum data transfer size is 8192 bytes, as follows:
   opaque filedata<8192>;
   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)---->|
It is an error to encode a length greater than the maximum described in the specification.

String

The standard defines a string of n (numbered 0 through n-1) ASCII bytes to be the number n encoded as an unsigned integer (as described above), and followed by the n bytes of the string. Byte b of the string always precedes byte b+1 of the string, and byte 0 of the string always follows the string's length. The n bytes are followed by enough (0 to 3) residual zero bytes, r, to make the total byte count a multiple of four.

Counted byte strings are declared as follows:

   string object<m>;
or
   string object<>;
The constant m denotes an upper bound of the number of bytes that a string may contain. If m is not specified, as in the second declaration, it is assumed to be (2**32)-1, the maximum length. The constant m would normally be found in a protocol specification. For example, a filing protocol may state that a file name can be no longer than 255 bytes, as follows:
   string filename<255>;
   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)---->|
It is an error to encode a length greater than the maximum described in the specification.

Fixed-length array

Fixed-length arrays of elements numbered 0 through n-1 are encoded by individually encoding the elements of the array in their natural order, 0 through n-1. Each element's size is a multiple of four bytes. Though all elements are of the same type, the elements may have different sizes. For example, in a fixed-length array of strings, all elements are of type ``string'', yet each element will vary in its length.

Declarations for fixed-length arrays of homogeneous elements are in the following form:

   type-name identifier[n];
   +---+---+---+---+---+---+---+---+...+---+---+---+---+
   |   element 0   |   element 1   |...|  element n-1  |
   +---+---+---+---+---+---+---+---+...+---+---+---+---+
   |<--------------------n elements------------------->|

Variable-length array

Counted arrays provide the ability to encode variable-length arrays of homogeneous elements. The array is encoded as the element count n (an unsigned integer) followed by the encoding of each of the array's elements, starting with element 0 and progressing through element n-1.

The declaration for variable-length arrays follows this form:

   type-name identifier<m>;
or
   type-name identifier<>;
The constant m specifies the maximum acceptable element count of an array. Note that if m is not specified, as is the case in the second declaration format above, it is assumed to be (2**32)-1.
   0  1  2  3
   +--+--+--+--+--+--+--+--+--+--+--+--+...+--+--+--+--+
   |     n     | element 0 | element 1 |...|element n-1|
   +--+--+--+--+--+--+--+--+--+--+--+--+...+--+--+--+--+
   |<-4 bytes->|<--------------n elements------------->|
It is an error to encode a value of n that is greater than the maximum described in the specification.

Structure

The components of the structure are encoded in the order of their declaration in the structure. Each component's size is a multiple of four bytes, though the components may be different sizes.

Structures are declared as follows:

   struct {
   	component-declaration-A;
   	component-declaration-B;
   	. . .
   } identifier;
   +-------------+-------------+...
   | component A | component B |...
   +-------------+-------------+...

Discriminated union

A discriminated union is a type composed of a discriminant followed by a type selected from a set of prearranged types according to the value of the discriminant. The type of discriminant is either ``int'', ``unsigned int'', or an enumerated type, such as ``bool''. The component types are called ``arms'' of the union, and are preceded by the value of the discriminant which implies their encoding.

Discriminated unions are declared as follows:

   union switch (discriminant-declaration) {
   	case <discriminant-value-A>:
   		<arm-declaration-A>;
   	case <discriminant-value-B>:
   		<arm-declaration-B>;
   	. . .
   	default:
   		<default-declaration>;
   } <identifier>;
Each ``case'' keyword is followed by a valid value of the discriminant. The default arm is optional. If it is not specified, then a valid encoding of the union cannot take on unspecified discriminant values. The size of the implied arm is always a multiple of four bytes.

The discriminated union is encoded as its discriminant followed by the encoding of the implied arm.

   0   1   2   3
   +---+---+---+---+---+---+---+---+
   |  discriminant |  implied arm  |
   +---+---+---+---+---+---+---+---+
   |<---4 bytes--->|

Void

An XDR ``void'' is a 0-byte quantity. Voids are useful for describing operations that take no data as input or no data as output. They are also useful in unions, where some arms may contain data and others do not.

The declaration is simply:

   void;
Voids are illustrated as follows:
  ++
  ||
  ++
--><-- 0 bytes

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004