v13i091: Sun RPC, release 3.9, Part14/15

Rich Salz rsalz at bbn.com
Fri Mar 4 22:58:36 AEST 1988


Submitted-by: Stephen X. Nahm <sxn at Sun.COM>
Posting-number: Volume 13, Issue 91
Archive-name: rpc3.9/part14

#! /bin/sh
# This is a shell archive. To extract, remove the header and type "sh filename"
#
cd doc
echo x - Makefile
cat > Makefile <<'Funky_Stuff'
#
# @(#)Makefile	1.4 87/11/18 3.9 RPCSRC
#
#
#   The targets all.nroff and all.troff will make monolithic documents
#   with nroff and troff, respectively.  The other *.nroff and *.troff
#   targets will make individual documents
#
TROFF=	ditroff
TOPTS=  -t
NROFF=	nroff
NOPTS=
PIC=	pic
TBL=	tbl
EQN=	eqn

SRC=	 rpc.prog.ms rpcgen.ms xdr.nts.ms xdr.rfc.ms rpc.rfc.ms nfs.rfc.ms

all default: all.nroff

install:	all.nroff
	@echo "Nothing installed."

all.nroff:	${SRC}
	${TBL} ${SRC} | ${EQN} | ${NROFF} ${NOPTS} -ms >all.nroff

all.troff:	${SRC}
	${TBL} ${SRC} | ${PIC} | ${EQN} | ${TROFF} ${TOPTS} -ms >all.troff

#

rpc.prog.nroff: rpc.prog.ms
	${TBL} rpc.prog.ms | ${NROFF} ${NOPTS} -ms >rpc.prog.nroff

rpc.prog.troff:	rpc.prog.ms
	${TBL} rpc.prog.ms | ${PIC} | ${TROFF} ${TOPTS} -ms >rpc.prog.troff

#

rpcgen.troff:	rpcgen.ms
	${TBL} rpcgen.ms | ${TROFF} ${TOPTS} -ms >rpcgen.troff

rpcgen.nroff:	rpcgen.ms
	${TBL} rpcgen.ms | ${NROFF} ${NOPTS} -ms >rpcgen.nroff

#

xdr.nts.troff: xdr.nts.ms
	${TBL} xdr.nts.ms | ${EQN} | ${TROFF} ${TOPTS} -ms >xdr.nts.troff

xdr.nts.nroff: xdr.nts.ms
	${TBL} xdr.nts.ms | ${EQN} | ${NROFF} ${NOPTS} -ms >xdr.nts.nroff

#

xdr.rfc.troff: xdr.rfc.ms
	${TBL} xdr.rfc.ms | ${TROFF} ${TOPTS} -ms >xdr.rfc.troff

xdr.rfc.nroff: xdr.rfc.ms
	${TBL} xdr.rfc.ms | ${NROFF} ${NOPTS} -ms >xdr.rfc.nroff

#

rpc.rfc.troff: rpc.rfc.ms
	${TBL} rpc.rfc.ms | ${TROFF} ${TOPTS} -ms >rpc.rfc.troff

rpc.rfc.nroff: rpc.rfc.ms
	${TBL} rpc.rfc.ms | ${NROFF} ${NOPTS} -ms >rpc.rfc.nroff

#

nfs.rfc.troff: nfs.rfc.ms
	${TBL} nfs.rfc.ms | ${TROFF} ${TOPTS} -ms >nfs.rfc.troff

nfs.rfc.nroff: nfs.rfc.ms
	${TBL} nfs.rfc.ms | ${NROFF} ${NOPTS} -ms >nfs.rfc.nroff

clean:
	rm -f *.nroff *.troff

spell:	${SRC}
	@for i in ${SRC}; do \
		echo $$i; spell $$i | sort | comm -23 - spell.ok > $$i.spell; \
	done
Funky_Stuff
len=`wc -c < Makefile`
if [ $len !=     1822 ] ; then
  echo error: Makefile was $len bytes long, should have been     1822
fi
echo x - xdr.rfc.ms
sed -e 's/^X//' > xdr.rfc.ms <<'Funky_Stuff'
X.\" @(#)xdr.rfc.ms	1.2 87/11/09 3.9 RPCSRC
X.de BT
X.if \\n%=1 .tl ''- % -''
X..
X.ND
X.\" prevent excess underlining in nroff
X.if n .fp 2 R
X.OH 'eXternal Data Representation Standard''Page %'
X.EH 'Page %''eXternal Data Representation Standard'
X.if \\n%=1 .bp
X.SH
\&eXternal Data Representation Standard: Protocol Specification
X.IX XDR RFC
X.IX XDR "protocol specification"
X.LP
X.NH 0
\&Status of this Standard
X.nr OF 1
X.IX XDR "RFC status"
X.LP
Note: This chapter specifies a protocol that Sun Microsystems, Inc., and 
others are using.  It has been designated RFC1014 by the ARPA Network
Information Center.
X.NH 1
\&Introduction
X.LP
XDR is a standard for the description and encoding of data.  It is
useful for transferring data between different computer
architectures, and has been used to communicate data between such
diverse machines as the Sun Workstation, VAX, IBM-PC, and Cray.
XDR fits into the ISO presentation layer, and is roughly analogous in
purpose to X.409, ISO Abstract Syntax Notation.  The major difference
between these two is that XDR uses implicit typing, while X.409 uses
explicit typing.
X.LP
XDR uses a language to describe data formats.  The language can only
be used only to describe data; it is not a programming language.
This language allows one to describe intricate data formats in a
concise manner. The alternative of using graphical representations
(itself an informal language) quickly becomes incomprehensible when
faced with complexity.  The XDR language itself is similar to the C
language [1], just as Courier [4] is similar to Mesa. Protocols such
as Sun RPC (Remote Procedure Call) and the NFS (Network File System)
use XDR to describe the format of their data.
X.LP
The XDR standard makes the following 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 in such a way that other hardware devices may decode the bytes
without loss of meaning.  For example, the Ethernet standard
suggests that bytes be encoded in "little-endian" style [2], or least
significant bit first.
X.NH 2
\&Basic Block Size
X.IX XDR "basic block size"
X.IX XDR "block size"
X.LP
The representation of all items requires a multiple of four 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.  If the n bytes needed to contain the data are not
a multiple of four, then the n bytes are followed by enough (0 to 3)
residual zero bytes, r, to make the total byte count a multiple of 4.
X.LP
We include the familiar graphic box notation for illustration and
comparison.  In most illustrations, each box (delimited by a plus
sign at the 4 corners and vertical bars and dashes) depicts a byte.
Ellipses (...) between boxes show zero or more additional bytes where
required.
X.ie t .DS
X.el .DS L
\fIA Block\fP

\f(CW+--------+--------+...+--------+--------+...+--------+
| byte 0 | byte 1 |...|byte n-1|    0   |...|    0   |
+--------+--------+...+--------+--------+...+--------+
|<-----------n bytes---------->|<------r bytes------>|
|<-----------n+r (where (n+r) mod 4 = 0)>----------->|\fP

X.DE
X.NH 1
\&XDR Data Types
X.IX XDR "data types"
X.IX "XDR data types"
X.LP
Each of the sections that follow describes a data type defined in the
XDR standard, shows how it is declared in the language, and includes
a graphic illustration of its encoding.
X.LP
For each data type in the language we show a general paradigm
declaration.  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
X.I "The XDR Language Specification" ,
below.
X.LP
For some data types, more specific examples are included.  A more
extensive example of a data description is in 
X.I "An Example of an XDR Data Description" ,
below.
X.NH 2
\&Integer
X.IX XDR integer
X.LP
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:
X.ie t .DS
X.el .DS L
\fIInteger\fP

\f(CW(MSB)                   (LSB)
+-------+-------+-------+-------+
|byte 0 |byte 1 |byte 2 |byte 3 |
+-------+-------+-------+-------+
<------------32 bits------------>\fP
X.DE
X.NH 2
\&Unsigned Integer
X.IX XDR "unsigned integer"
X.IX XDR "integer, unsigned"
X.LP
An XDR unsigned integer is a 32-bit datum that encodes a nonnegative
integer in the range [0,4294967295].  It 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:
X.ie t .DS
X.el .DS L
\fIUnsigned Integer\fP

\f(CW(MSB)                   (LSB)
+-------+-------+-------+-------+
|byte 0 |byte 1 |byte 2 |byte 3 |
+-------+-------+-------+-------+
<------------32 bits------------>\fP
X.DE
X.NH 2
\&Enumeration
X.IX XDR enumeration
X.LP
Enumerations have the same representation as signed integers.
Enumerations are handy for describing subsets of the integers.
Enumerated data is declared as follows:
X.ft CW
X.DS
enum { name-identifier = constant, ... } identifier;
X.DE
For example, the three colors red, yellow, and blue could be
described by an enumerated type:
X.DS
X.ft CW
enum { RED = 2, YELLOW = 3, BLUE = 5 } colors;
X.DE
It is an error to encode as an enum any other integer than those that
have been given assignments in the enum declaration.
X.NH 2
\&Boolean
X.IX XDR boolean
X.LP
Booleans are important enough and occur frequently enough to warrant
their own explicit type in the standard.  Booleans are declared as
follows:
X.DS
X.ft CW
bool identifier;
X.DE
This is equivalent to:
X.DS
X.ft CW
enum { FALSE = 0, TRUE = 1 } identifier;
X.DE
X.NH 2
\&Hyper Integer and Unsigned Hyper Integer
X.IX XDR "hyper integer"
X.IX XDR "integer, hyper"
X.LP
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 defined above.
They are represented in two's complement notation.  The most and
least significant bytes are 0 and 7, respectively.  Their
declarations:
X.ie t .DS
X.el .DS L
\fIHyper Integer\fP
\fIUnsigned Hyper Integer\fP

\f(CW(MSB)                                                   (LSB)
+-------+-------+-------+-------+-------+-------+-------+-------+
|byte 0 |byte 1 |byte 2 |byte 3 |byte 4 |byte 5 |byte 6 |byte 7 |
+-------+-------+-------+-------+-------+-------+-------+-------+
<----------------------------64 bits---------------------------->\fP
X.DE
X.NH 2
\&Floating-point
X.IX XDR "integer, floating point"
X.IX XDR "floating-point integer"
X.LP
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:
X.RS
X.IP \fBS\fP:
The sign of the number.  Values 0 and  1 represent  positive and
negative, respectively.  One bit.
X.IP \fBE\fP:
The exponent of the number, base 2.  8  bits are devoted to this
field.  The exponent is biased by 127.
X.IP \fBF\fP:
The fractional part of the number's mantissa,  base 2.   23 bits
are devoted to this field.
X.RE
X.LP
Therefore, the floating-point number is described by:
X.DS
(-1)**S * 2**(E-Bias) * 1.F
X.DE
It is declared as follows:
X.ie t .DS
X.el .DS L
\fISingle-Precision Floating-Point\fP

\f(CW+-------+-------+-------+-------+
|byte 0 |byte 1 |byte 2 |byte 3 |
S|   E   |           F          |
+-------+-------+-------+-------+
1|<- 8 ->|<-------23 bits------>|
<------------32 bits------------>\fP
X.DE
Just as 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).
X.LP
The IEEE specifications should be consulted concerning the encoding
for signed zero, signed infinity (overflow), and denormalized numbers
(underflow) [3].  According to IEEE specifications, the "NaN" (not a
number) is system dependent and should not be used externally.
X.NH 2
\&Double-precision Floating-point
X.IX XDR "integer, double-precision floating point"
X.IX XDR "double-precision floating-point integer"
X.LP
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 [3].  The standard encodes the following three fields, which
describe the double-precision floating-point number:
X.RS
X.IP \fBS\fP:
The sign of the number.  Values  0 and 1  represent positive and
negative, respectively.  One bit.
X.IP \fBE\fP:
The exponent of the number, base 2.  11 bits are devoted to this
field.  The exponent is biased by 1023.
X.IP \fBF\fP:
The fractional part of the number's  mantissa, base 2.   52 bits
are devoted to this field.
X.RE
X.LP
Therefore, the floating-point number is described by:
X.DS
(-1)**S * 2**(E-Bias) * 1.F
X.DE
It is declared as follows:
X.ie t .DS
X.el .DS L
\fIDouble-Precision Floating-Point\fP

\f(CW+------+------+------+------+------+------+------+------+
|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------------------------->\fP
X.DE
Just as the most and least significant bytes of a number 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
that these numbers refer to the mathematical positions of the bits,
and NOT to their actual physical locations (which vary from medium to
medium).
X.LP
The IEEE specifications should be consulted concerning the encoding
for signed zero, signed infinity (overflow), and denormalized numbers
(underflow) [3].  According to IEEE specifications, the "NaN" (not a
number) is system dependent and should not be used externally.
X.NH 2
\&Fixed-length Opaque Data
X.IX XDR "fixed-length opaque data"
X.IX XDR "opaque data, fixed length"
X.LP
At times, fixed-length uninterpreted data needs to be passed among
machines.  This data is called "opaque" and is declared as follows:
X.DS
X.ft CW
opaque identifier[n];
X.DE
where the constant n is the (static) number of bytes necessary to
contain the opaque data.  If n is not a multiple of four, then 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.
X.ie t .DS
X.el .DS L
\fIFixed-Length Opaque\fP

\f(CW0        1     ...
+--------+--------+...+--------+--------+...+--------+
| byte 0 | byte 1 |...|byte n-1|    0   |...|    0   |
+--------+--------+...+--------+--------+...+--------+
|<-----------n bytes---------->|<------r bytes------>|
|<-----------n+r (where (n+r) mod 4 = 0)------------>|\fP
X.DE
X.NH 2
\&Variable-length Opaque Data
X.IX XDR "variable-length opaque data"
X.IX XDR "opaque data, variable length"
X.LP
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.
X.LP
Byte m of the sequence always precedes byte m+1 of the sequence, and
byte 0 of the sequence always follows the sequence's length (count).
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:
X.DS
X.ft CW
opaque identifier<m>;
X.DE
or
X.DS
X.ft CW
opaque identifier<>;
X.DE
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.
The constant m would normally be found in a protocol specification.
For example, a filing protocol may state that the maximum data
transfer size is 8192 bytes, as follows:
X.DS
X.ft CW
opaque filedata<8192>;
X.DE
This can be illustrated as follows:
X.ie t .DS
X.el .DS L
\fIVariable-Length Opaque\fP

\f(CW0     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)---->|\fP
X.DE
X.LP
It   is  an error  to  encode  a  length  greater  than the maximum
described in the specification.
X.NH 2
\&String
X.IX XDR string
X.LP
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 m of the
string always precedes byte m+1 of the string, and byte 0 of the
string always follows the string's length.  If n is not a multiple of
four, then 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:
X.DS
X.ft CW
string object<m>;
X.DE
or
X.DS
X.ft CW
string object<>;
X.DE
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:
X.DS
X.ft CW
string filename<255>;
X.DE
Which can be illustrated as:
X.ie t .DS
X.el .DS L
\fIA String\fP

\f(CW0     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)---->|\fP
X.DE
X.LP
It   is an  error  to  encode  a length greater  than   the maximum
described in the specification.
X.NH 2
\&Fixed-length Array
X.IX XDR "fixed-length array"
X.IX XDR "array, fixed length"
X.LP
Declarations for fixed-length arrays of homogeneous elements are in
the following form:
X.DS
X.ft CW
type-name identifier[n];
X.DE
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.
X.ie t .DS
X.el .DS L
\fIFixed-Length Array\fP

\f(CW+---+---+---+---+---+---+---+---+...+---+---+---+---+
|   element 0   |   element 1   |...|  element n-1  |
+---+---+---+---+---+---+---+---+...+---+---+---+---+
|<--------------------n elements------------------->|\fP
X.DE
X.NH 2
\&Variable-length Array
X.IX XDR "variable-length array"
X.IX XDR "array, variable length"
X.LP
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:
X.DS
X.ft CW
type-name identifier<m>;
X.DE
or
X.DS
X.ft CW
type-name identifier<>;
X.DE
The constant m specifies the maximum acceptable element count of an
array; if  m is not specified, as  in the second declaration, it is
assumed to be (2**32) - 1.
X.ie t .DS
X.el .DS L
\fICounted Array\fP

\f(CW0  1  2  3
+--+--+--+--+--+--+--+--+--+--+--+--+...+--+--+--+--+
|     n     | element 0 | element 1 |...|element n-1|
+--+--+--+--+--+--+--+--+--+--+--+--+...+--+--+--+--+
|<-4 bytes->|<--------------n elements------------->|\fP
X.DE
It is  an error to  encode  a  value of n that  is greater than the
maximum described in the specification.
X.NH 2
\&Structure
X.IX XDR structure
X.LP
Structures are declared as follows:
X.DS
X.ft CW
struct {
	component-declaration-A;
	component-declaration-B;
	\&...
} identifier;
X.DE
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.
X.ie t .DS
X.el .DS L
\fIStructure\fP

\f(CW+-------------+-------------+...
| component A | component B |...
+-------------+-------------+...\fP
X.DE
X.NH 2
\&Discriminated Union
X.IX XDR "discriminated union"
X.IX XDR union discriminated
X.LP
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:
X.DS
X.ft CW
union switch (discriminant-declaration) {
	case discriminant-value-A:
	arm-declaration-A;
	case discriminant-value-B:
	arm-declaration-B;
	\&...
	default: default-declaration;
} identifier;
X.DE
Each "case" keyword is followed by a legal 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.
X.LP
The discriminated union is encoded as its discriminant followed by
the encoding of the implied arm.
X.ie t .DS
X.el .DS L
\fIDiscriminated Union\fP

\f(CW0   1   2   3
+---+---+---+---+---+---+---+---+
|  discriminant |  implied arm  |
+---+---+---+---+---+---+---+---+
|<---4 bytes--->|\fP
X.DE
X.NH 2
\&Void
X.IX XDR void
X.LP
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 as follows:
X.DS
X.ft CW
void;
X.DE
Voids are illustrated as follows:
X.ie t .DS
X.el .DS L
\fIVoid\fP

\f(CW  ++
  ||
  ++
--><-- 0 bytes\fP
X.DE
X.NH 2
\&Constant
X.IX XDR constant
X.LP
The data declaration for a constant follows this form:
X.DS
X.ft CW
const name-identifier = n;
X.DE
"const" is used to define a symbolic name for a constant; it does not
declare any data.  The symbolic constant may be used anywhere a
regular constant may be used.  For example, the following defines a
symbolic constant DOZEN, equal to 12.
X.DS
X.ft CW
const DOZEN = 12;
X.DE
X.NH 2
\&Typedef
X.IX XDR typedef
X.LP
"typedef" does not declare any data either, but serves to define new
identifiers for declaring data. The syntax is:
X.DS
X.ft CW
typedef declaration;
X.DE
The new type name is actually the variable name in the declaration
part of the typedef.  For example, the following defines a new type
called "eggbox" using an existing type called "egg":
X.DS
X.ft CW
typedef egg eggbox[DOZEN];
X.DE
Variables declared using the new type name have the same type as the
new type name would have in the typedef, if it was considered a
variable.  For example, the following two declarations are equivalent
in declaring the variable "fresheggs":
X.DS
X.ft CW
eggbox  fresheggs;
egg     fresheggs[DOZEN];
X.DE
When a typedef involves a struct, enum, or union definition, there is
another (preferred) syntax that may be used to define the same type.
In general, a typedef of the following form:
X.DS
X.ft CW
typedef <<struct, union, or enum definition>> identifier;
X.DE
may be converted to the alternative form by removing the "typedef"
part and placing 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":
X.DS
X.ft CW
typedef enum {    /* \fIusing typedef\fP */
	FALSE = 0,
	TRUE = 1
	} bool;

enum bool {       /* \fIpreferred alternative\fP */
	FALSE = 0,
	TRUE = 1
	};
X.DE
The reason this syntax is preferred is one does not have to wait
until the end of a declaration to figure out the name of the new
type.
X.NH 2
\&Optional-data
X.IX XDR "optional data"
X.IX XDR "data, optional"
X.LP
Optional-data is one kind of union that occurs so frequently that we
give it a special syntax of its own for declaring it.  It is declared
as follows:
X.DS
X.ft CW
type-name *identifier;
X.DE
This is equivalent to the following union:
X.DS
X.ft CW
union switch (bool opted) {
	case TRUE:
	type-name element;
	case FALSE:
	void;
} identifier;
X.DE
It is also equivalent to the following variable-length array
declaration, since the boolean "opted" can be interpreted as the
length of the array:
X.DS
X.ft CW
type-name identifier<1>;
X.DE
Optional-data is not so interesting in itself, but it is very useful
for describing recursive data-structures such as linked-lists and
trees.  For example, the following defines a type "stringlist" that
encodes lists of arbitrary length strings:
X.DS
X.ft CW
struct *stringlist {
	string item<>;
	stringlist next;
};
X.DE
It could have been equivalently declared as the following union:
X.DS
X.ft CW
union stringlist switch (bool opted) {
	case TRUE:
		struct {
			string item<>;
			stringlist next;
		} element;
	case FALSE:
		void;
};
X.DE
or as a variable-length array:
X.DS
X.ft CW
struct stringlist<1> {
	string item<>;
	stringlist next;
};
X.DE
Both of these declarations obscure the intention of the stringlist
type, so the optional-data declaration is preferred over both of
them.  The optional-data type also has a close correlation to how
recursive data structures are represented in high-level languages
such as Pascal or C by use of pointers. In fact, the syntax is the
same as that of the C language for pointers.
X.NH 2
\&Areas for Future Enhancement
X.IX XDR futures
X.LP
The XDR standard lacks representations for bit fields and bitmaps,
since the standard is based on bytes.  Also missing are packed (or
binary-coded) decimals.
X.LP
The intent of the XDR standard was not to describe every kind of data
that people have ever sent or will ever want to send from machine to
machine. Rather, it only describes the most commonly used data-types
of high-level languages such as Pascal or C so that applications
written in these languages will be able to communicate easily over
some medium.
X.LP
One could imagine extensions to XDR that would let it describe almost
any existing protocol, such as TCP.  The minimum necessary for this
are 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.
X.NH 1
\&Discussion
X.sp 2
X.NH 2
\&Why a Language for Describing Data?
X.IX XDR language
X.LP
There are many advantages in using a data-description language such
as  XDR  versus using  diagrams.   Languages are  more  formal than
diagrams   and   lead  to less  ambiguous   descriptions  of  data.
Languages are also easier  to understand and allow  one to think of
other   issues instead of  the   low-level details of bit-encoding.
Also,  there is  a close analogy  between the  types  of XDR and  a
high-level language   such  as C   or    Pascal.   This makes   the
implementation of XDR encoding and decoding modules an easier task.
Finally, the language specification itself  is an ASCII string that
can be passed from  machine to machine  to perform  on-the-fly data
interpretation.
X.NH 2
\&Why Only one Byte-Order for an XDR Unit?
X.IX XDR "byte order"
X.LP
Supporting two byte-orderings requires a higher level protocol for
determining in which byte-order the data is encoded.  Since XDR is
not a protocol, this can't be done.  The advantage of this, though,
is that data in XDR format can be written to a magnetic tape, for
example, and any machine will be able to interpret it, since no
higher level protocol is necessary for determining the byte-order.
X.NH 2
\&Why does XDR use Big-Endian Byte-Order?
X.LP
Yes, it is 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.
X.NH 2
\&Why is the XDR Unit Four Bytes Wide?
X.LP
There is a tradeoff in choosing the XDR unit size.  Choosing a small
size such as two makes the encoded data small, but causes alignment
problems for machines that aren't aligned on these boundaries.  A
large size such as eight means the data will be aligned on virtually
every machine, but causes the encoded data to grow too big.  We chose
four as a compromise.  Four is big enough to support most
architectures efficiently, except for rare machines such as the
eight-byte aligned Cray.  Four is also small enough to keep the
encoded data restricted to a reasonable size.
X.NH 2
\&Why must Variable-Length Data be Padded with Zeros?
X.IX XDR "variable-length data"
X.LP
It is desirable that the same data encode into the same thing on all
machines, so that encoded data can be meaningfully compared or
checksummed.  Forcing the padded bytes to be zero ensures this.
X.NH 2
\&Why is there No Explicit Data-Typing?
X.LP
Data-typing has a relatively high cost for what small advantages it
may have.  One cost is the expansion of data due to the inserted type
fields.  Another is the added cost of interpreting these type fields
and acting accordingly.  And most protocols already know what type
they expect, so data-typing supplies only redundant information.
However, one can still get the benefits of data-typing using XDR. One
way is to encode two things: first a string which is 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, and then
define a universal type which takes this value as its discriminant
and for each value, describes the corresponding data type.
X.NH 1
\&The XDR Language Specification
X.IX XDR language
X.sp 1
X.NH 2
\&Notational Conventions
X.IX "XDR language" notation
X.LP
This specification  uses an extended Backus-Naur Form  notation for
describing the XDR language.   Here is  a brief description  of the
notation:
X.IP  1.
The characters
X.I | ,
X.I ( ,
X.I ) ,
X.I [ ,
X.I ] ,
X.I " ,
and
X.I * 
are special.
X.IP  2.
Terminal symbols are  strings of any  characters surrounded by
double quotes.
X.IP  3.
Non-terminal symbols are strings of non-special characters.
X.IP  4.
Alternative items are separated by a vertical bar ("\fI|\fP").
X.IP  5.
Optional items are enclosed in brackets.
X.IP  6.
Items are grouped together by enclosing them in parentheses.
X.IP  7.
A
X.I * 
following an item means  0 or more  occurrences of that item.
X.LP
For example,  consider  the  following pattern:
X.DS L
"a " "very" (", " " very")* [" cold " "and"]  " rainy " ("day" | "night")
X.DE
X.LP
An infinite  number of  strings match  this pattern. A few  of them
are:
X.DS
"a very rainy day"
"a very, very rainy day"
"a very cold and  rainy day"
"a very, very, very cold and  rainy night"
X.DE
X.NH 2
\&Lexical Notes
X.IP  1.
Comments begin with '/*' and terminate with '*/'.
X.IP  2.
White space serves to separate items and is otherwise ignored.
X.IP  3.
An identifier is a letter followed by  an optional sequence of
letters, digits or underbar ('_').  The case of identifiers is
not ignored.
X.IP  4.
A  constant is  a  sequence  of  one  or  more decimal digits,
optionally preceded by a minus-sign ('-').
X.NH 2
\&Syntax Information
X.IX "XDR language" syntax
X.DS
X.ft CW
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"
X.DE
X.DS
X.ft CW
value:
	constant
	| identifier

type-specifier:
	  [ "unsigned" ] "int"
	| [ "unsigned" ] "hyper"
	| "float"
	| "double"
	| "bool"
	| enum-type-spec
	| struct-type-spec
	| union-type-spec
	| identifier
X.DE
X.DS
X.ft CW
enum-type-spec:
	"enum" enum-body

enum-body:
	"{"
	( identifier "=" value )
	( "," identifier "=" value )*
	"}"
X.DE
X.DS
X.ft CW
struct-type-spec:
	"struct" struct-body

struct-body:
	"{"
	( declaration ";" )
	( declaration ";" )*
	"}"
X.DE
X.DS
X.ft CW
union-type-spec:
	"union" union-body

union-body:
	"switch" "(" declaration ")" "{"
	( "case" value ":" declaration ";" )
	( "case" value ":" declaration ";" )*
	[ "default" ":" declaration ";" ]
	"}"

constant-def:
	"const" identifier "=" constant ";"
X.DE
X.DS
X.ft CW
type-def:
	"typedef" declaration ";"
	| "enum" identifier enum-body ";"
	| "struct" identifier struct-body ";"
	| "union" identifier union-body ";"

definition:
	type-def
	| constant-def

specification:
	definition *
X.DE
X.NH 3
\&Syntax Notes
X.IX "XDR language" syntax
X.LP
X.IP  1.
The following are keywords and cannot be used as identifiers:
"bool", "case", "const", "default", "double", "enum", "float",
"hyper", "opaque", "string", "struct", "switch", "typedef", "union",
"unsigned" and "void".
X.IP  2.
Only unsigned constants may be used as size specifications for
arrays.  If an identifier is used, it must have been declared
previously as an unsigned constant in a "const" definition.
X.IP  3.
Constant and type identifiers within the scope of a specification
are in the same name space and must be declared uniquely within this
scope.
X.IP  4.
Similarly, variable names must  be unique within  the scope  of
struct and union declarations. Nested struct and union declarations
create new scopes.
X.IP  5.
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.
X.NH 1
\&An Example of an XDR Data Description
X.LP
Here is a short XDR data description of a thing called a "file",
which might be used to transfer files from one machine to another.
X.ie t .DS
X.el .DS L
X.ft CW

const MAXUSERNAME = 32;     /*\fI max length of a user name \fP*/
const MAXFILELEN = 65535;   /*\fI max length of a file      \fP*/
const MAXNAMELEN = 255;     /*\fI max length of a file name \fP*/

X.ft I
/*
 * Types of files:
 */
X.ft CW

enum filekind {
	TEXT = 0,       /*\fI ascii data \fP*/
	DATA = 1,       /*\fI raw data   \fP*/
	EXEC = 2        /*\fI executable \fP*/
};

X.ft I
/*
 * File information, per kind of file:
 */
X.ft CW

union filetype switch (filekind kind) {
	case TEXT:
		void;                           /*\fI no extra information \fP*/
	case DATA:
		string creator<MAXNAMELEN>;     /*\fI data creator         \fP*/
	case EXEC:
		string interpretor<MAXNAMELEN>; /*\fI program interpretor  \fP*/
};

X.ft I
/*
 * A complete file:
 */
X.ft CW

struct file {
	string filename<MAXNAMELEN>; /*\fI name of file \fP*/
	filetype type;               /*\fI info about file \fP*/
	string owner<MAXUSERNAME>;   /*\fI owner of file   \fP*/
	opaque data<MAXFILELEN>;     /*\fI file data       \fP*/
};
X.DE
X.LP
Suppose now that there is  a user named  "john" who wants to  store
his lisp program "sillyprog" that contains just  the data "(quit)".
His file would be encoded as follows:
X.TS
box tab (&) ;
lfI lfI lfI lfI
rfL rfL rfL l .
Offset&Hex Bytes&ASCII&Description
_
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
X.TE
X.NH 1
\&References
X.LP
[1]  Brian W. Kernighan & Dennis M. Ritchie, "The C Programming
Language", Bell Laboratories, Murray Hill, New Jersey, 1978.
X.LP
[2]  Danny Cohen, "On Holy Wars and a Plea for Peace", IEEE Computer,
October 1981.
X.LP
[3]  "IEEE Standard for Binary Floating-Point Arithmetic", ANSI/IEEE
Standard 754-1985, Institute of Electrical and Electronics
Engineers, August 1985.
X.LP
[4]  "Courier: The Remote Procedure Call Protocol", XEROX
Corporation, XSIS 038112, December 1981.
Funky_Stuff
cd ..
echo more files to follow
exit
-- 
Please send comp.sources.unix-related mail to rsalz at uunet.uu.net.



More information about the Comp.sources.unix mailing list