ONC+ Developer's Guide

XDR Data Description

Here is a short XDR data description of a file data structure, which might be used to transfer files from one machine to another.


Example C-2 XDR File Data Structur

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

/* Types of files: */
enum filekind {
 	TEXT = 0, /* ascii data */
 	DATA = 1, /* raw data */
 	EXEC = 2  /* executable */
 };

/* File information, per kind of file: */
union filetype switch (filekind kind) {
 	case TEXT:
 		void;                           /* no extra information */
 	case DATA:
 		string creator<MAXNAMELEN>;     /* data creator */
 	case EXEC:
 		string interpreter<MAXNAMELEN>; /*proginterptr*/
};

/* A complete file: */
struct file {
 	string filename<MAXNAMELEN>;        /* name of file */
 	filetype type;                      /* info about file */
 	string owner<MAXUSERNAME>;          /* owner of file */
 	opaque data<MAXFILELEN>;            /* file data */
};

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:

Table C-2 XDR Data Description Example

Offset 

Hex Bytes 

ASCII 

Description 

00 00 00 09 

.... 

Length of filename = 9 

73 69 6c 6c 

sill 

Filename characters 

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 interpreter = 4 

24 

6c 69 73 70 

lisp 

Interpreter 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 

(qu 

File data bytes ... 

44  

74 29 00 00 

t).. 

... and 2 zero-bytes of fill