Variable-Length Opaque Data

XDR also defines variable-length uninterpreted data as opaque. Variable-length (counted) opaque data is defined as a sequence of n arbitrary bytes, numbered 0 through n-1. Opaque data is encoded as an unsigned integer and followed by the n bytes of the sequence.

Byte m of the sequence always precedes byte m+1, and byte 0 of the sequence always follows the sequence length (count). Enough (0 to 3) residual 0 bytes, r, are added to make the total byte count a multiple of 4.

Variable-length opaque data is declared in one of the following forms:

opaque identifier<m>;
OR

opaque identifier<>;

The constant m denotes an upper bound for the number of bytes that the sequence can contain. If m is not specified, as in the second declaration, it is assumed to be (2**32) - 1, which is the maximum length. The constant m would normally be found in a protocol specification. See the Variable-Length Opaque figure (Figure 1).

Figure 1. Variable-Length Opaque
This diagram contains 4 lines of information. The second line of the diagram is the main line, listing segments as follows: length n, byte 0, byte 1, and then dots signifying the bytes between byte 1 and byte n -1. The next byte is labeled:n -1, followed by residual byte 0. Dots signify more residual bytes that end in a final byte 0. The remaining lines of the diagram describe this main line. The first line assigns numbers as follows: numbers 0 through 3 for length n, number 4 for byte 0, number 5 for byte 1, and dots signifying a continuing sequence. The third line assigns byte values to the main line as follows: length n is 4 bytes, byte 0 through byte n -1 yield n bytes. All the residual bytes together equal r bytes. The fourth line, which spans the entire diagram, shows the following equation:n +r (where (n +r) mod 4 = 0).
Note: Encoding a length n that is greater than the maximum described in the protocol specification causes an error.