home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / rfc / 1000s / rfc1014.txt < prev    next >
Text File  |  1987-06-29  |  38KB  |  1,119 lines

  1.  
  2. Network Working Group                             Sun Microsystems, Inc.
  3. Request for Comments: 1014                                     June 1987
  4.  
  5.  
  6.                XDR: External Data Representation Standard
  7.  
  8. STATUS OF THIS MEMO
  9.  
  10.    This RFC describes a standard that Sun Microsystems, Inc., and others
  11.    are using, one we wish to propose for the Internet's consideration.
  12.    Distribution of this memo is unlimited.
  13.  
  14. 1. INTRODUCTION
  15.  
  16.    XDR is a standard for the description and encoding of data.  It is
  17.    useful for transferring data between different computer
  18.    architectures, and has been used to communicate data between such
  19.    diverse machines as the SUN WORKSTATION*, VAX*, IBM-PC*, and Cray*.
  20.    XDR fits into the ISO presentation layer, and is roughly analogous in
  21.    purpose to X.409, ISO Abstract Syntax Notation.  The major difference
  22.    between these two is that XDR uses implicit typing, while X.409 uses
  23.    explicit typing.
  24.  
  25.    XDR uses a language to describe data formats.  The language can only
  26.    be used only to describe data; it is not a programming language.
  27.    This language allows one to describe intricate data formats in a
  28.    concise manner. The alternative of using graphical representations
  29.    (itself an informal language) quickly becomes incomprehensible when
  30.    faced with complexity.  The XDR language itself is similar to the C
  31.    language [1], just as Courier [4] is similar to Mesa. Protocols such
  32.    as Sun RPC (Remote Procedure Call) and the NFS* (Network File System)
  33.    use XDR to describe the format of their data.
  34.  
  35.    The XDR standard makes the following assumption: that bytes (or
  36.    octets) are portable, where a byte is defined to be 8 bits of data.
  37.    A given hardware device should encode the bytes onto the various
  38.    media in such a way that other hardware devices may decode the bytes
  39.    without loss of meaning.  For example, the Ethernet* standard
  40.    suggests that bytes be encoded in "little-endian" style [2], or least
  41.    significant bit first.
  42.  
  43. 2. BASIC BLOCK SIZE
  44.  
  45.    The representation of all items requires a multiple of four bytes (or
  46.    32 bits) of data.  The bytes are numbered 0 through n-1.  The bytes
  47.    are read or written to some byte stream such that byte m always
  48.    precedes byte m+1.  If the n bytes needed to contain the data are not
  49.    a multiple of four, then the n bytes are followed by enough (0 to 3)
  50.  
  51.  
  52.  
  53. SUN Microsystems                                                [Page 1]
  54.  
  55. RFC 1014              External Data Representation             June 1987
  56.  
  57.  
  58.    residual zero bytes, r, to make the total byte count a multiple of 4.
  59.  
  60.    We include the familiar graphic box notation for illustration and
  61.    comparison.  In most illustrations, each box (delimited by a plus
  62.    sign at the 4 corners and vertical bars and dashes) depicts a byte.
  63.    Ellipses (...) between boxes show zero or more additional bytes where
  64.    required.
  65.  
  66.         +--------+--------+...+--------+--------+...+--------+
  67.         | byte 0 | byte 1 |...|byte n-1|    0   |...|    0   |   BLOCK
  68.         +--------+--------+...+--------+--------+...+--------+
  69.         |<-----------n bytes---------->|<------r bytes------>|
  70.         |<-----------n+r (where (n+r) mod 4 = 0)>----------->|
  71.  
  72. 3. XDR DATA TYPES
  73.  
  74.    Each of the sections that follow describes a data type defined in the
  75.    XDR standard, shows how it is declared in the language, and includes
  76.    a graphic illustration of its encoding.
  77.  
  78.    For each data type in the language we show a general paradigm
  79.    declaration.  Note that angle brackets (< and >) denote
  80.    variablelength sequences of data and square brackets ([ and ]) denote
  81.    fixed-length sequences of data.  "n", "m" and "r" denote integers.
  82.    For the full language specification and more formal definitions of
  83.    terms such as "identifier" and "declaration", refer to section 5:
  84.    "The XDR Language Specification".
  85.  
  86.    For some data types, more specific examples are included.  A more
  87.    extensive example of a data description is in section 6:  "An Example
  88.    of an XDR Data Description".
  89.  
  90. 3.1 Integer
  91.  
  92.    An XDR signed integer is a 32-bit datum that encodes an integer in
  93.    the range [-2147483648,2147483647].  The integer is represented in
  94.    two's complement notation.  The most and least significant bytes are
  95.    0 and 3, respectively.  Integers are declared as follows:
  96.  
  97.          int identifier;
  98.  
  99.            (MSB)                   (LSB)
  100.          +-------+-------+-------+-------+
  101.          |byte 0 |byte 1 |byte 2 |byte 3 |                      INTEGER
  102.          +-------+-------+-------+-------+
  103.          <------------32 bits------------>
  104.  
  105.  
  106.  
  107.  
  108.  
  109. SUN Microsystems                                                [Page 2]
  110.  
  111. RFC 1014              External Data Representation             June 1987
  112.  
  113.  
  114. 3.2.Unsigned Integer
  115.  
  116.    An XDR unsigned integer is a 32-bit datum that encodes a nonnegative
  117.    integer in the range [0,4294967295].  It is represented by an
  118.    unsigned binary number whose most and least significant bytes are 0
  119.    and 3, respectively.  An unsigned integer is declared as follows:
  120.  
  121.          unsigned int identifier;
  122.  
  123.            (MSB)                   (LSB)
  124.          +-------+-------+-------+-------+
  125.          |byte 0 |byte 1 |byte 2 |byte 3 |             UNSIGNED INTEGER
  126.          +-------+-------+-------+-------+
  127.          <------------32 bits------------>
  128.  
  129. 3.3 Enumeration
  130.  
  131.    Enumerations have the same representation as signed integers.
  132.    Enumerations are handy for describing subsets of the integers.
  133.    Enumerated data is declared as follows:
  134.  
  135.          enum { name-identifier = constant, ... } identifier;
  136.  
  137.    For example, the three colors red, yellow, and blue could be
  138.    described by an enumerated type:
  139.  
  140.          enum { RED = 2, YELLOW = 3, BLUE = 5 } colors;
  141.  
  142.    It is an error to encode as an enum any other integer than those that
  143.    have been given assignments in the enum declaration.
  144.  
  145. 3.4 Boolean
  146.  
  147.    Booleans are important enough and occur frequently enough to warrant
  148.    their own explicit type in the standard.  Booleans are declared as
  149.    follows:
  150.  
  151.       bool identifier;
  152.  
  153.       This is equivalent to:
  154.  
  155.          enum { FALSE = 0, TRUE = 1 } identifier;
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. SUN Microsystems                                                [Page 3]
  166.  
  167. RFC 1014              External Data Representation             June 1987
  168.  
  169.  
  170. 3.5 Hyper Integer and Unsigned Hyper Integer
  171.  
  172.    The standard also defines 64-bit (8-byte) numbers called hyper
  173.    integer and unsigned hyper integer.  Their representations are the
  174.    obvious extensions of integer and unsigned integer defined above.
  175.    They are represented in two's complement notation.  The most and
  176.    least significant bytes are 0 and 7, respectively.  Their
  177.    declarations:
  178.  
  179.    hyper identifier; unsigned hyper identifier;
  180.  
  181.         (MSB)                                                   (LSB)
  182.       +-------+-------+-------+-------+-------+-------+-------+-------+
  183.       |byte 0 |byte 1 |byte 2 |byte 3 |byte 4 |byte 5 |byte 6 |byte 7 |
  184.       +-------+-------+-------+-------+-------+-------+-------+-------+
  185.       <----------------------------64 bits---------------------------->
  186.                                                  HYPER INTEGER
  187.                                                  UNSIGNED HYPER INTEGER
  188.  
  189. 3.6 Floating-point
  190.  
  191.    The standard defines the floating-point data type "float" (32 bits or
  192.    4 bytes).  The encoding used is the IEEE standard for normalized
  193.    single-precision floating-point numbers [3].  The following three
  194.    fields describe the single-precision floating-point number:
  195.  
  196.       S: The sign of the number.  Values 0 and 1 represent positive and
  197.          negative, respectively.  One bit.
  198.  
  199.       E: The exponent of the number, base 2.  8 bits are devoted to this
  200.          field.  The exponent is biased by 127.
  201.  
  202.       F: The fractional part of the number's mantissa, base 2.  23 bits
  203.          are devoted to this field.
  204.  
  205.    Therefore, the floating-point number is described by:
  206.  
  207.          (-1)**S * 2**(E-Bias) * 1.F
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221. SUN Microsystems                                                [Page 4]
  222.  
  223. RFC 1014              External Data Representation             June 1987
  224.  
  225.  
  226.    It is declared as follows:
  227.          float identifier;
  228.  
  229.          +-------+-------+-------+-------+
  230.          |byte 0 |byte 1 |byte 2 |byte 3 |              SINGLE-PRECISION
  231.          S|   E   |           F          |         FLOATING-POINT NUMBER
  232.          +-------+-------+-------+-------+
  233.          1|<- 8 ->|<-------23 bits------>|
  234.          <------------32 bits------------>
  235.  
  236.    Just as the most and least significant bytes of a number are 0 and 3,
  237.    the most and least significant bits of a single-precision floating-
  238.    point number are 0 and 31.  The beginning bit (and most significant
  239.    bit) offsets of S, E, and F are 0, 1, and 9, respectively.  Note that
  240.    these numbers refer to the mathematical positions of the bits, and
  241.    NOT to their actual physical locations (which vary from medium to
  242.    medium).
  243.  
  244.    The EEE specifications should be consulted concerning the encoding
  245.    for signed zero, signed infinity (overflow), and denormalized numbers
  246.    (underflow) [3].  According to IEEE specifications, the "NaN" (not a
  247.    number) is system dependent and should not be used externally.
  248.  
  249. 3.7 Double-precision Floating-point
  250.  
  251.    The standard defines the encoding for the double-precision floating-
  252.    point data type "double" (64 bits or 8 bytes).  The encoding used is
  253.    the IEEE standard for normalized double-precision floating-point
  254.    numbers [3].  The standard encodes the following three fields, which
  255.    describe the double-precision floating-point number:
  256.  
  257.       S: The sign of the number.  Values 0 and 1 represent positive and
  258.          negative, respectively.  One bit.
  259.  
  260.       E: The exponent of the number, base 2.  11 bits are devoted to
  261.          this field.  The exponent is biased by 1023.
  262.  
  263.       F: The fractional part of the number's mantissa, base 2.  52 bits
  264.          are devoted to this field.
  265.  
  266.    Therefore, the floating-point number is described by:
  267.  
  268.          (-1)**S * 2**(E-Bias) * 1.F
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277. SUN Microsystems                                                [Page 5]
  278.  
  279. RFC 1014              External Data Representation             June 1987
  280.  
  281.  
  282.    It is declared as follows:
  283.  
  284.          double identifier;
  285.  
  286.          +------+------+------+------+------+------+------+------+
  287.          |byte 0|byte 1|byte 2|byte 3|byte 4|byte 5|byte 6|byte 7|
  288.          S|    E   |                    F                        |
  289.          +------+------+------+------+------+------+------+------+
  290.          1|<--11-->|<-----------------52 bits------------------->|
  291.          <-----------------------64 bits------------------------->
  292.                                         DOUBLE-PRECISION FLOATING-POINT
  293.  
  294.    Just as the most and least significant bytes of a number are 0 and 3,
  295.    the most and least significant bits of a double-precision floating-
  296.    point number are 0 and 63.  The beginning bit (and most significant
  297.    bit) offsets of S, E , and F are 0, 1, and 12, respectively.  Note
  298.    that these numbers refer to the mathematical positions of the bits,
  299.    and NOT to their actual physical locations (which vary from medium to
  300.    medium).
  301.  
  302.    The IEEE specifications should be consulted concerning the encoding
  303.    for signed zero, signed infinity (overflow), and denormalized numbers
  304.    (underflow) [3].  According to IEEE specifications, the "NaN" (not a
  305.    number) is system dependent and should not be used externally.
  306.  
  307. 3.8 Fixed-length Opaque Data
  308.  
  309.    At times, fixed-length uninterpreted data needs to be passed among
  310.    machines.  This data is called "opaque" and is declared as follows:
  311.  
  312.          opaque identifier[n];
  313.  
  314.    where the constant n is the (static) number of bytes necessary to
  315.    contain the opaque data.  If n is not a multiple of four, then the n
  316.    bytes are followed by enough (0 to 3) residual zero bytes, r, to make
  317.    the total byte count of the opaque object a multiple of four.
  318.  
  319.           0        1     ...
  320.       +--------+--------+...+--------+--------+...+--------+
  321.       | byte 0 | byte 1 |...|byte n-1|    0   |...|    0   |
  322.       +--------+--------+...+--------+--------+...+--------+
  323.       |<-----------n bytes---------->|<------r bytes------>|
  324.       |<-----------n+r (where (n+r) mod 4 = 0)------------>|
  325.                                                    FIXED-LENGTH OPAQUE
  326.  
  327. 3.9 Variable-length Opaque Data
  328.  
  329.    The standard also provides for variable-length (counted) opaque data,
  330.  
  331.  
  332.  
  333. SUN Microsystems                                                [Page 6]
  334.  
  335. RFC 1014              External Data Representation             June 1987
  336.  
  337.  
  338.    defined as a sequence of n (numbered 0 through n-1) arbitrary bytes
  339.    to be the number n encoded as an unsigned integer (as described
  340.    below), and followed by the n bytes of the sequence.
  341.  
  342.    Byte m of the sequence always precedes byte m+1 of the sequence, and
  343.    byte 0 of the sequence always follows the sequence's length (count).
  344.    If n is not a multiple of four, then the n bytes are followed by
  345.    enough (0 to 3) residual zero bytes, r, to make the total byte count
  346.    a multiple of four.  Variable-length opaque data is declared in the
  347.    following way:
  348.  
  349.          opaque identifier<m>;
  350.       or
  351.          opaque identifier<>;
  352.  
  353.    The constant m denotes an upper bound of the number of bytes that the
  354.    sequence may contain.  If m is not specified, as in the second
  355.    declaration, it is assumed to be (2**32) - 1, the maximum length.
  356.    The constant m would normally be found in a protocol specification.
  357.    For example, a filing protocol may state that the maximum data
  358.    transfer size is 8192 bytes, as follows:
  359.  
  360.          opaque filedata<8192>;
  361.  
  362.             0     1     2     3     4     5   ...
  363.          +-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+
  364.          |        length n       |byte0|byte1|...| n-1 |  0  |...|  0  |
  365.          +-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+
  366.          |<-------4 bytes------->|<------n bytes------>|<---r bytes--->|
  367.                                  |<----n+r (where (n+r) mod 4 = 0)---->|
  368.                                                   VARIABLE-LENGTH OPAQUE
  369.  
  370.    It is an error to encode a length greater than the maximum described
  371.    in the specification.
  372.  
  373. 3.10 String
  374.  
  375.    The standard defines a string of n (numbered 0 through n-1) ASCII
  376.    bytes to be the number n encoded as an unsigned integer (as described
  377.    above), and followed by the n bytes of the string.  Byte m of the
  378.    string always precedes byte m+1 of the string, and byte 0 of the
  379.    string always follows the string's length.  If n is not a multiple of
  380.    four, then the n bytes are followed by enough (0 to 3) residual zero
  381.    bytes, r, to make the total byte count a multiple of four.  Counted
  382.    byte strings are declared as follows:
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389. SUN Microsystems                                                [Page 7]
  390.  
  391. RFC 1014              External Data Representation             June 1987
  392.  
  393.  
  394.          string object<m>;
  395.       or
  396.          string object<>;
  397.  
  398.  
  399.    The constant m denotes an upper bound of the number of bytes that a
  400.    string may contain.  If m is not specified, as in the second
  401.    declaration, it is assumed to be (2**32) - 1, the maximum length.
  402.    The constant m would normally be found in a protocol specification.
  403.    For example, a filing protocol may state that a file name can be no
  404.    longer than 255 bytes, as follows:
  405.  
  406.          string filename<255>;
  407.  
  408.             0     1     2     3     4     5   ...
  409.          +-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+
  410.          |        length n       |byte0|byte1|...| n-1 |  0  |...|  0  |
  411.          +-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+
  412.          |<-------4 bytes------->|<------n bytes------>|<---r bytes--->|
  413.                                  |<----n+r (where (n+r) mod 4 = 0)---->|
  414.                                                                   STRING
  415.  
  416.    It is an error to encode a length greater than the maximum described
  417.    in the specification.
  418.  
  419. 3.11 Fixed-length Array
  420.  
  421.    Declarations for fixed-length arrays of homogeneous elements are in
  422.    the following form:
  423.  
  424.          type-name identifier[n];
  425.  
  426.    Fixed-length arrays of elements numbered 0 through n-1 are encoded by
  427.    individually encoding the elements of the array in their natural
  428.    order, 0 through n-1.  Each element's size is a multiple of four
  429.    bytes. Though all elements are of the same type, the elements may
  430.    have different sizes.  For example, in a fixed-length array of
  431.    strings, all elements are of type "string", yet each element will
  432.    vary in its length.
  433.  
  434.          +---+---+---+---+---+---+---+---+...+---+---+---+---+
  435.          |   element 0   |   element 1   |...|  element n-1  |
  436.          +---+---+---+---+---+---+---+---+...+---+---+---+---+
  437.          |<--------------------n elements------------------->|
  438.  
  439.                                                FIXED-LENGTH ARRAY
  440.  
  441.  
  442.  
  443.  
  444.  
  445. SUN Microsystems                                                [Page 8]
  446.  
  447. RFC 1014              External Data Representation             June 1987
  448.  
  449.  
  450. 3.12 Variable-length Array
  451.  
  452.    Counted arrays provide the ability to encode variable-length arrays
  453.    of homogeneous elements.  The array is encoded as the element count n
  454.    (an unsigned integer) followed by the encoding of each of the array's
  455.    elements, starting with element 0 and progressing through element n-
  456.    1.  The declaration for variable-length arrays follows this form:
  457.  
  458.          type-name identifier<m>;
  459.       or
  460.          type-name identifier<>;
  461.  
  462.    The constant m specifies the maximum acceptable element count of an
  463.    array; if m is not specified, as in the second declaration, it is
  464.    assumed to be (2**32) - 1.
  465.  
  466.            0  1  2  3
  467.          +--+--+--+--+--+--+--+--+--+--+--+--+...+--+--+--+--+
  468.          |     n     | element 0 | element 1 |...|element n-1|
  469.          +--+--+--+--+--+--+--+--+--+--+--+--+...+--+--+--+--+
  470.          |<-4 bytes->|<--------------n elements------------->|
  471.                                                          COUNTED ARRAY
  472.  
  473.    It is an error to encode a value of n that is greater than the
  474.    maximum described in the specification.
  475.  
  476. 3.13 Structure
  477.  
  478.    Structures are declared as follows:
  479.  
  480.          struct {
  481.             component-declaration-A;
  482.             component-declaration-B;
  483.             ...
  484.          } identifier;
  485.  
  486.    The components of the structure are encoded in the order of their
  487.    declaration in the structure.  Each component's size is a multiple of
  488.    four bytes, though the components may be different sizes.
  489.  
  490.          +-------------+-------------+...
  491.          | component A | component B |...                      STRUCTURE
  492.          +-------------+-------------+...
  493.  
  494. 3.14 Discriminated Union
  495.  
  496.    A discriminated union is a type composed of a discriminant followed
  497.    by a type selected from a set of prearranged types according to the
  498.  
  499.  
  500.  
  501. SUN Microsystems                                                [Page 9]
  502.  
  503. RFC 1014              External Data Representation             June 1987
  504.  
  505.  
  506.    value of the discriminant.  The type of discriminant is either "int",
  507.    "unsigned int", or an enumerated type, such as "bool".  The component
  508.    types are called "arms" of the union, and are preceded by the value
  509.    of the discriminant which implies their encoding.  Discriminated
  510.    unions are declared as follows:
  511.  
  512.          union switch (discriminant-declaration) {
  513.          case discriminant-value-A:
  514.             arm-declaration-A;
  515.          case discriminant-value-B:
  516.             arm-declaration-B;
  517.          ...
  518.          default: default-declaration;
  519.          } identifier;
  520.  
  521.    Each "case" keyword is followed by a legal value of the discriminant.
  522.    The default arm is optional.  If it is not specified, then a valid
  523.    encoding of the union cannot take on unspecified discriminant values.
  524.    The size of the implied arm is always a multiple of four bytes.
  525.  
  526.    The discriminated union is encoded as its discriminant followed by
  527.    the encoding of the implied arm.
  528.  
  529.            0   1   2   3
  530.          +---+---+---+---+---+---+---+---+
  531.          |  discriminant |  implied arm  |          DISCRIMINATED UNION
  532.          +---+---+---+---+---+---+---+---+
  533.          |<---4 bytes--->|
  534.  
  535. 3.15 Void
  536.  
  537.    An XDR void is a 0-byte quantity.  Voids are useful for describing
  538.    operations that take no data as input or no data as output. They are
  539.    also useful in unions, where some arms may contain data and others do
  540.    not.  The declaration is simply as follows:
  541.          void;
  542.  
  543.    Voids are illustrated as follows:
  544.  
  545.            ++
  546.            ||                                                     VOID
  547.            ++
  548.          --><-- 0 bytes
  549.  
  550. 3.16 Constant
  551.  
  552.    The data declaration for a constant follows this form:
  553.  
  554.  
  555.  
  556.  
  557. SUN Microsystems                                               [Page 10]
  558.  
  559. RFC 1014              External Data Representation             June 1987
  560.  
  561.  
  562.          const name-identifier = n;
  563.  
  564.    "const" is used to define a symbolic name for a constant; it does not
  565.    declare any data.  The symbolic constant may be used anywhere a
  566.    regular constant may be used.  For example, the following defines a
  567.    symbolic constant DOZEN, equal to 12.
  568.  
  569.          const DOZEN = 12;
  570.  
  571. 3.17 Typedef
  572.  
  573.    "typedef" does not declare any data either, but serves to define new
  574.    identifiers for declaring data. The syntax is:
  575.  
  576.          typedef declaration;
  577.  
  578.    The new type name is actually the variable name in the declaration
  579.    part of the typedef.  For example, the following defines a new type
  580.    called "eggbox" using an existing type called "egg":
  581.  
  582.          typedef egg eggbox[DOZEN];
  583.  
  584.    Variables declared using the new type name have the same type as the
  585.    new type name would have in the typedef, if it was considered a
  586.    variable.  For example, the following two declarations are equivalent
  587.    in declaring the variable "fresheggs":
  588.  
  589.          eggbox  fresheggs;
  590.          egg     fresheggs[DOZEN];
  591.  
  592.    When a typedef involves a struct, enum, or union definition, there is
  593.    another (preferred) syntax that may be used to define the same type.
  594.    In general, a typedef of the following form:
  595.  
  596.          typedef <<struct, union, or enum definition>> identifier;
  597.  
  598.    may be converted to the alternative form by removing the "typedef"
  599.    part and placing the identifier after the "struct", "union", or
  600.    "enum" keyword, instead of at the end.  For example, here are the two
  601.    ways to define the type "bool":
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613. SUN Microsystems                                               [Page 11]
  614.  
  615. RFC 1014              External Data Representation             June 1987
  616.  
  617.  
  618.          typedef enum {    /* using typedef */
  619.             FALSE = 0,
  620.             TRUE = 1
  621.          } bool;
  622.  
  623.          enum bool {       /* preferred alternative */
  624.             FALSE = 0,
  625.             TRUE = 1
  626.          };
  627.  
  628.    The reason this syntax is preferred is one does not have to wait
  629.    until the end of a declaration to figure out the name of the new
  630.    type.
  631.  
  632. 3.18 Optional-data
  633.  
  634.    Optional-data is one kind of union that occurs so frequently that we
  635.    give it a special syntax of its own for declaring it.  It is declared
  636.    as follows:
  637.  
  638.          type-name *identifier;
  639.  
  640.    This is equivalent to the following union:
  641.  
  642.          union switch (bool opted) {
  643.          case TRUE:
  644.             type-name element;
  645.          case FALSE:
  646.             void;
  647.          } identifier;
  648.  
  649.    It is also equivalent to the following variable-length array
  650.    declaration, since the boolean "opted" can be interpreted as the
  651.    length of the array:
  652.  
  653.          type-name identifier<1>;
  654.  
  655.    Optional-data is not so interesting in itself, but it is very useful
  656.    for describing recursive data-structures such as linked-lists and
  657.    trees.  For example, the following defines a type "stringlist" that
  658.    encodes lists of arbitrary length strings:
  659.  
  660.          struct *stringlist {
  661.             string item<>;
  662.             stringlist next;
  663.          };
  664.  
  665.  
  666.  
  667.  
  668.  
  669. SUN Microsystems                                               [Page 12]
  670.  
  671. RFC 1014              External Data Representation             June 1987
  672.  
  673.  
  674.    It could have been equivalently declared as the following union:
  675.  
  676.          union stringlist switch (bool opted) {
  677.          case TRUE:
  678.             struct {
  679.                string item<>;
  680.                stringlist next;
  681.             } element;
  682.          case FALSE:
  683.             void;
  684.          };
  685.  
  686.       or as a variable-length array:
  687.  
  688.          struct stringlist<1> {
  689.             string item<>;
  690.             stringlist next;
  691.          };
  692.  
  693.    Both of these declarations obscure the intention of the stringlist
  694.    type, so the optional-data declaration is preferred over both of
  695.    them.  The optional-data type also has a close correlation to how
  696.    recursive data structures are represented in high-level languages
  697.    such as Pascal or C by use of pointers. In fact, the syntax is the
  698.    same as that of the C language for pointers.
  699.  
  700. 3.19 Areas for Future Enhancement
  701.  
  702.    The XDR standard lacks representations for bit fields and bitmaps,
  703.    since the standard is based on bytes.  Also missing are packed (or
  704.    binary-coded) decimals.
  705.  
  706.    The intent of the XDR standard was not to describe every kind of data
  707.    that people have ever sent or will ever want to send from machine to
  708.    machine. Rather, it only describes the most commonly used data-types
  709.    of high-level languages such as Pascal or C so that applications
  710.    written in these languages will be able to communicate easily over
  711.    some medium.
  712.  
  713.    One could imagine extensions to XDR that would let it describe almost
  714.    any existing protocol, such as TCP.  The minimum necessary for this
  715.    are support for different block sizes and byte-orders.  The XDR
  716.    discussed here could then be considered the 4-byte big-endian member
  717.    of a larger XDR family.
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725. SUN Microsystems                                               [Page 13]
  726.  
  727. RFC 1014              External Data Representation             June 1987
  728.  
  729.  
  730. 4. DISCUSSION
  731.  
  732.    (1) Why use a language for describing data?  What's wrong with
  733.    diagrams?
  734.  
  735.    There are many advantages in using a data-description language such
  736.    as  XDR  versus using  diagrams.   Languages are  more  formal than
  737.    diagrams   and   lead  to less  ambiguous   descriptions  of  data.
  738.    Languages are also easier  to understand and allow  one to think of
  739.    other   issues instead of  the   low-level details of bit-encoding.
  740.    Also,  there is  a close analogy  between the  types  of XDR and  a
  741.    high-level language   such  as C   or    Pascal.   This makes   the
  742.    implementation of XDR encoding and decoding modules an easier task.
  743.    Finally, the language specification itself  is an ASCII string that
  744.    can be passed from  machine to machine  to perform  on-the-fly data
  745.    interpretation.
  746.  
  747.    (2) Why is there only one byte-order for an XDR unit?
  748.  
  749.    Supporting two byte-orderings requires a higher level protocol for
  750.    determining in which byte-order the data is encoded.  Since XDR is
  751.    not a protocol, this can't be done.  The advantage of this, though,
  752.    is that data in XDR format can be written to a magnetic tape, for
  753.    example, and any machine will be able to interpret it, since no
  754.    higher level protocol is necessary for determining the byte-order.
  755.  
  756.    (3) Why is the XDR byte-order big-endian instead of little-endian?
  757.    Isn't this unfair to little-endian machines such as the VAX(r), which
  758.    has to convert from one form to the other?
  759.  
  760.    Yes, it is unfair, but having only one byte-order means you have to
  761.    be unfair to somebody.  Many architectures, such as the Motorola
  762.    68000* and IBM 370*, support the big-endian byte-order.
  763.  
  764.    (4) Why is the XDR unit four bytes wide?
  765.  
  766.    There is a tradeoff in choosing the XDR unit size.  Choosing a small
  767.    size such as two makes the encoded data small, but causes alignment
  768.    problems for machines that aren't aligned on these boundaries.  A
  769.    large size such as eight means the data will be aligned on virtually
  770.    every machine, but causes the encoded data to grow too big.  We chose
  771.    four as a compromise.  Four is big enough to support most
  772.    architectures efficiently, except for rare machines such as the
  773.    eight-byte aligned Cray*.  Four is also small enough to keep the
  774.    encoded data restricted to a reasonable size.
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781. SUN Microsystems                                               [Page 14]
  782.  
  783. RFC 1014              External Data Representation             June 1987
  784.  
  785.  
  786.    (5) Why must variable-length data be padded with zeros?
  787.  
  788.    It is desirable that the same data encode into the same thing on all
  789.    machines, so that encoded data can be meaningfully compared or
  790.    checksummed.  Forcing the padded bytes to be zero ensures this.
  791.  
  792.    (6) Why is there no explicit data-typing?
  793.  
  794.    Data-typing has a relatively high cost for what small advantages it
  795.    may have.  One cost is the expansion of data due to the inserted type
  796.    fields.  Another is the added cost of interpreting these type fields
  797.    and acting accordingly.  And most protocols already know what type
  798.    they expect, so data-typing supplies only redundant information.
  799.    However, one can still get the benefits of data-typing using XDR. One
  800.    way is to encode two things: first a string which is the XDR data
  801.    description of the encoded data, and then the encoded data itself.
  802.    Another way is to assign a value to all the types in XDR, and then
  803.    define a universal type which takes this value as its discriminant
  804.    and for each value, describes the corresponding data type.
  805.  
  806.  
  807. 5. THE XDR LANGUAGE SPECIFICATION
  808.  
  809.    5.1 Notational Conventions
  810.  
  811.    This specification uses an extended Back-Naur Form notation for
  812.    describing the XDR language.  Here is a brief description of the
  813.    notation:
  814.  
  815.  
  816.    (1) The characters '|', '(', ')', '[', ']', '"', and '*' are special.
  817.    (2) Terminal symbols are strings of any characters surrounded by
  818.    double quotes.
  819.    (3) Non-terminal symbols are strings of non-special characters.
  820.    (4) Alternative items are separated by a vertical bar ("|").
  821.    (5) Optional items are enclosed in brackets.
  822.    (6) Items are grouped together by enclosing them in parentheses.
  823.    (7) A '*' following an item means 0 or more occurrences of that item.
  824.  
  825.    For example,  consider  the  following pattern:
  826.  
  827.          "a " "very" (", " "very")* [" cold " "and "]  " rainy "
  828.          ("day" | "night")
  829.  
  830.    An infinite number of strings match this pattern. A few of them are:
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837. SUN Microsystems                                               [Page 15]
  838.  
  839. RFC 1014              External Data Representation             June 1987
  840.  
  841.  
  842.          "a very rainy day"
  843.          "a very, very rainy day"
  844.          "a very cold and  rainy day"
  845.          "a very, very, very cold and  rainy night"
  846.  
  847. 5.2 Lexical Notes
  848.  
  849.    (1) Comments begin with '/*' and terminate with '*/'.
  850.    (2) White space serves to separate items and is otherwise ignored.
  851.    (3) An identifier is a letter followed by an optional sequence of
  852.    letters, digits or underbar ('_'). The case of identifiers is not
  853.    ignored.
  854.    (4) A constant is a sequence of one or more decimal digits,
  855.    optionally preceded by a minus-sign ('-').
  856.  
  857. 5.3 Syntax Information
  858.  
  859.       declaration:
  860.            type-specifier identifier
  861.          | type-specifier identifier "[" value "]"
  862.          | type-specifier identifier "<" [ value ] ">"
  863.          | "opaque" identifier "[" value "]"
  864.          | "opaque" identifier "<" [ value ] ">"
  865.          | "string" identifier "<" [ value ] ">"
  866.          | type-specifier "*" identifier
  867.          | "void"
  868.  
  869.       value:
  870.            constant
  871.          | identifier
  872.  
  873.       type-specifier:
  874.            [ "unsigned" ] "int"
  875.          | [ "unsigned" ] "hyper"
  876.          | "float"
  877.          | "double"
  878.          | "bool"
  879.          | enum-type-spec
  880.          | struct-type-spec
  881.          | union-type-spec
  882.          | identifier
  883.  
  884.       enum-type-spec:
  885.          "enum" enum-body
  886.  
  887.       enum-body:
  888.          "{"
  889.             ( identifier "=" value )
  890.  
  891.  
  892.  
  893. SUN Microsystems                                               [Page 16]
  894.  
  895. RFC 1014              External Data Representation             June 1987
  896.  
  897.  
  898.             ( "," identifier "=" value )*
  899.          "}"
  900.  
  901.       struct-type-spec:
  902.          "struct" struct-body
  903.  
  904.       struct-body:
  905.          "{"
  906.             ( declaration ";" )
  907.             ( declaration ";" )*
  908.          "}"
  909.  
  910.       union-type-spec:
  911.          "union" union-body
  912.  
  913.       union-body:
  914.          "switch" "(" declaration ")" "{"
  915.             ( "case" value ":" declaration ";" )
  916.             ( "case" value ":" declaration ";" )*
  917.             [ "default" ":" declaration ";" ]
  918.          "}"
  919.  
  920.       constant-def:
  921.          "const" identifier "=" constant ";"
  922.  
  923.       type-def:
  924.            "typedef" declaration ";"
  925.          | "enum" identifier enum-body ";"
  926.          | "struct" identifier struct-body ";"
  927.          | "union" identifier union-body ";"
  928.  
  929.       definition:
  930.            type-def
  931.          | constant-def
  932.  
  933.       specification:
  934.            definition *
  935.  
  936. 5.4 Syntax Notes
  937.  
  938.    (1) The following are keywords and cannot be used as identifiers:
  939.    "bool", "case", "const", "default", "double", "enum", "float",
  940.    "hyper", "opaque", "string", "struct", "switch", "typedef", "union",
  941.    "unsigned" and "void".
  942.  
  943.    (2) Only unsigned constants may be used as size specifications for
  944.    arrays.  If an identifier is used, it must have been declared
  945.    previously as an unsigned constant in a "const" definition.
  946.  
  947.  
  948.  
  949. SUN Microsystems                                               [Page 17]
  950.  
  951. RFC 1014              External Data Representation             June 1987
  952.  
  953.  
  954.    (3) Constant and type identifiers within the scope of a specification
  955.    are in the same name space and must be declared uniquely within this
  956.    scope.
  957.  
  958.    (4) Similarly, variable names must  be unique within  the scope  of
  959.    struct and union declarations. Nested struct and union declarations
  960.    create new scopes.
  961.  
  962.    (5) The discriminant of a union must be of a type that evaluates to
  963.    an integer. That is, "int", "unsigned int", "bool", an enumerated
  964.    type or any typedefed type that evaluates to one of these is legal.
  965.    Also, the case values must be one of the legal values of the
  966.    discriminant.  Finally, a case value may not be specified more than
  967.    once within the scope of a union declaration.
  968.  
  969. 6. AN EXAMPLE OF AN XDR DATA DESCRIPTION
  970.  
  971.    Here is a short XDR data description of a thing called a "file",
  972.    which might be used to transfer files from one machine to another.
  973.  
  974.          const MAXUSERNAME = 32;     /* max length of a user name */
  975.          const MAXFILELEN = 65535;   /* max length of a file      */
  976.          const MAXNAMELEN = 255;     /* max length of a file name */
  977.  
  978.          /*
  979.           * Types of files:
  980.           */
  981.          enum filekind {
  982.             TEXT = 0,       /* ascii data */
  983.             DATA = 1,       /* raw data   */
  984.             EXEC = 2        /* executable */
  985.          };
  986.  
  987.          /*
  988.           * File information, per kind of file:
  989.           */
  990.          union filetype switch (filekind kind) {
  991.          case TEXT:
  992.             void;                           /* no extra information */
  993.          case DATA:
  994.             string creator<MAXNAMELEN>;     /* data creator         */
  995.          case EXEC:
  996.             string interpretor<MAXNAMELEN>; /* program interpretor  */
  997.          };
  998.  
  999.  
  1000.  
  1001.  
  1002.  
  1003.  
  1004.  
  1005. SUN Microsystems                                               [Page 18]
  1006.  
  1007. RFC 1014              External Data Representation             June 1987
  1008.  
  1009.  
  1010.          /*
  1011.           * A complete file:
  1012.           */
  1013.          struct file {
  1014.             string filename<MAXNAMELEN>; /* name of file    */
  1015.             filetype type;               /* info about file */
  1016.             string owner<MAXUSERNAME>;   /* owner of file   */
  1017.             opaque data<MAXFILELEN>;     /* file data       */
  1018.          };
  1019.  
  1020.    Suppose now that there is a user named "john" who wants to store his
  1021.    lisp program "sillyprog" that contains just the data "(quit)".  His
  1022.    file would be encoded as follows:
  1023.  
  1024.        OFFSET  HEX BYTES       ASCII    COMMENTS
  1025.        ------  ---------       -----    --------
  1026.         0      00 00 00 09     ....     -- length of filename = 9
  1027.         4      73 69 6c 6c     sill     -- filename characters
  1028.         8      79 70 72 6f     ypro     -- ... and more characters ...
  1029.        12      67 00 00 00     g...     -- ... and 3 zero-bytes of fill
  1030.        16      00 00 00 02     ....     -- filekind is EXEC = 2
  1031.        20      00 00 00 04     ....     -- length of interpretor = 4
  1032.        24      6c 69 73 70     lisp     -- interpretor characters
  1033.        28      00 00 00 04     ....     -- length of owner = 4
  1034.        32      6a 6f 68 6e     john     -- owner characters
  1035.        36      00 00 00 06     ....     -- length of file data = 6
  1036.        40      28 71 75 69     (qui     -- file data bytes ...
  1037.        44      74 29 00 00     t)..     -- ... and 2 zero-bytes of fill
  1038.  
  1039. 7. REFERENCES
  1040.  
  1041.    [1]  Brian W. Kernighan & Dennis M. Ritchie, "The C Programming
  1042.         Language", Bell Laboratories, Murray Hill, New Jersey, 1978.
  1043.  
  1044.    [2]  Danny Cohen, "On Holy Wars and a Plea for Peace", IEEE Computer,
  1045.         October 1981.
  1046.  
  1047.    [3]  "IEEE Standard for Binary Floating-Point Arithmetic", ANSI/IEEE
  1048.         Standard 754-1985, Institute of Electrical and Electronics
  1049.         Engineers, August 1985.
  1050.  
  1051.    [4]  "Courier: The Remote Procedure Call Protocol", XEROX
  1052.         Corporation, XSIS 038112, December 1981.
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061. SUN Microsystems                                               [Page 19]
  1062.  
  1063. RFC 1014              External Data Representation             June 1987
  1064.  
  1065.  
  1066. 8. TRADEMARKS AND OWNERS
  1067.  
  1068.         SUN WORKSTATION  Sun Microsystems, Inc.
  1069.         VAX              Digital Equipment Corporation
  1070.         IBM-PC           International Business Machines Corporation
  1071.         Cray             Cray Research
  1072.         NFS              Sun Microsystems, Inc.
  1073.         Ethernet         Xerox Corporation.
  1074.         Motorola 68000   Motorola, Inc.
  1075.         IBM 370          International Business Machines Corporation
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117. SUN Microsystems                                               [Page 20]
  1118.  
  1119.