Strings

The XDR standard defines a string of n (numbered 0 through n-1) ASCII bytes to be the number n encoded as an unsigned integer and followed by the n bytes of the string.

Byte m of the string always precedes byte m+1, and byte 0 of the string always follows the string length. If n is not a multiple of 4, 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.

Counted byte strings are declared as one of the following:

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, which is 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>;

See the Counted Byte String figure (Figure 1).

Figure 1. Counted Byte String
This diagram contains 4 lines of information. The second line of the diagram is the main line, listing as follows: length n, byte 0, byte 1, 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 equal 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 greater than the maximum described in the protocol specification causes an error.