1: /* 2: * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 3: * unrestricted use provided that this legend is included on all tape 4: * media and as a part of the software program in whole or part. Users 5: * may copy or modify Sun RPC without charge, but are not authorized 6: * to license or distribute it to anyone else except as part of a product or 7: * program developed by the user. 8: * 9: * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 10: * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 11: * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 12: * 13: * Sun RPC is provided with no support and without any obligation on the 14: * part of Sun Microsystems, Inc. to assist in its use, correction, 15: * modification or enhancement. 16: * 17: * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 18: * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 19: * OR ANY PART THEREOF. 20: * 21: * In no event will Sun Microsystems, Inc. be liable for any lost revenue 22: * or profits or other special, indirect and consequential damages, even if 23: * Sun has been advised of the possibility of such damages. 24: * 25: * Sun Microsystems, Inc. 26: * 2550 Garcia Avenue 27: * Mountain View, California 94043 28: */ 29: /* @(#)xdr.h 1.4 85/02/08 SMI */ 30: 31: /* 32: * xdr.h, External Data Representation Serialization Routines. 33: * 34: * Copyright (C) 1984, Sun Microsystems, Inc. 35: */ 36: 37: /* 38: * XDR provides a conventional way for converting between C data 39: * types and an external bit-string representation. Library supplied 40: * routines provide for the conversion on built-in C data types. These 41: * routines and utility routines defined here are used to help implement 42: * a type encode/decode routine for each user-defined type. 43: * 44: * Each data type provides a single procedure which takes two arguments: 45: * 46: * bool_t 47: * xdrproc(xdrs, argresp) 48: * XDR *xdrs; 49: * <type> *argresp; 50: * 51: * xdrs is an instance of a XDR handle, to which or from which the data 52: * type is to be converted. argresp is a pointer to the structure to be 53: * converted. The XDR handle contains an operation field which indicates 54: * which of the operations (ENCODE, DECODE * or FREE) is to be performed. 55: * 56: * XDR_DECODE may allocate space if the pointer argresp is null. This 57: * data can be freed with the XDR_FREE operation. 58: * 59: * We write only one procedure per data type to make it easy 60: * to keep the encode and decode procedures for a data type consistent. 61: * In many cases the same code performs all operations on a user defined type, 62: * because all the hard work is done in the component type routines. 63: * decode as a series of calls on the nested data types. 64: */ 65: 66: /* 67: * Xdr operations. XDR_ENCODE causes the type to be encoded into the 68: * stream. XDR_DECODE causes the type to be extracted from the stream. 69: * XDR_FREE can be used to release the space allocated by an XDR_DECODE 70: * request. 71: */ 72: enum xdr_op { 73: XDR_ENCODE=0, 74: XDR_DECODE=1, 75: XDR_FREE=2 76: }; 77: 78: /* 79: * This is the number of bytes per unit of external data. 80: */ 81: #define BYTES_PER_XDR_UNIT (4) 82: 83: /* 84: * A xdrproc_t exists for each data type which is to be encoded or decoded. 85: * 86: * The second argument to the xdrproc_t is a pointer to an opaque pointer. 87: * The opaque pointer generally points to a structure of the data type 88: * to be decoded. If this pointer is 0, then the type routines should 89: * allocate dynamic storage of the appropriate size and return it. 90: * bool_t (*xdrproc_t)(XDR *, caddr_t *); 91: */ 92: typedef bool_t (*xdrproc_t)(); 93: 94: /* 95: * The XDR handle. 96: * Contains operation which is being applied to the stream, 97: * an operations vector for the paticular implementation (e.g. see xdr_mem.c), 98: * and two private fields for the use of the particular impelementation. 99: */ 100: typedef struct { 101: enum xdr_op x_op; /* operation; fast additional param */ 102: struct xdr_ops { 103: bool_t (*x_getlong)(); /* get a long from underlying stream */ 104: bool_t (*x_putlong)(); /* put a long to " */ 105: bool_t (*x_getbytes)();/* get some bytes from " */ 106: bool_t (*x_putbytes)();/* put some bytes to " */ 107: u_int (*x_getpostn)();/* returns bytes off from beginning */ 108: bool_t (*x_setpostn)();/* lets you reposition the stream */ 109: long * (*x_inline)(); /* buf quick ptr to buffered data */ 110: void (*x_destroy)(); /* free privates of this xdr_stream */ 111: } *x_ops; 112: caddr_t x_public; /* users' data */ 113: caddr_t x_private; /* pointer to private data */ 114: caddr_t x_base; /* private used for position info */ 115: int x_handy; /* extra private word */ 116: } XDR; 117: 118: /* 119: * Operations defined on a XDR handle 120: * 121: * XDR *xdrs; 122: * long *longp; 123: * caddr_t addr; 124: * u_int len; 125: * u_int pos; 126: */ 127: #define XDR_GETLONG(xdrs, longp) \ 128: (*(xdrs)->x_ops->x_getlong)(xdrs, longp) 129: #define xdr_getlong(xdrs, longp) \ 130: (*(xdrs)->x_ops->x_getlong)(xdrs, longp) 131: 132: #define XDR_PUTLONG(xdrs, longp) \ 133: (*(xdrs)->x_ops->x_putlong)(xdrs, longp) 134: #define xdr_putlong(xdrs, longp) \ 135: (*(xdrs)->x_ops->x_putlong)(xdrs, longp) 136: 137: #define XDR_GETBYTES(xdrs, addr, len) \ 138: (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len) 139: #define xdr_getbytes(xdrs, addr, len) \ 140: (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len) 141: 142: #define XDR_PUTBYTES(xdrs, addr, len) \ 143: (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len) 144: #define xdr_putbytes(xdrs, addr, len) \ 145: (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len) 146: 147: #define XDR_GETPOS(xdrs) \ 148: (*(xdrs)->x_ops->x_getpostn)(xdrs) 149: #define xdr_getpos(xdrs) \ 150: (*(xdrs)->x_ops->x_getpostn)(xdrs) 151: 152: #define XDR_SETPOS(xdrs, pos) \ 153: (*(xdrs)->x_ops->x_setpostn)(xdrs, pos) 154: #define xdr_setpos(xdrs, pos) \ 155: (*(xdrs)->x_ops->x_setpostn)(xdrs, pos) 156: 157: #define XDR_INLINE(xdrs, len) \ 158: (*(xdrs)->x_ops->x_inline)(xdrs, len) 159: #define xdr_inline(xdrs, len) \ 160: (*(xdrs)->x_ops->x_inline)(xdrs, len) 161: 162: #define XDR_DESTROY(xdrs) \ 163: if ((xdrs)->x_ops->x_destroy) \ 164: (*(xdrs)->x_ops->x_destroy)(xdrs) 165: #define xdr_destroy(xdrs) \ 166: if ((xdrs)->x_ops->x_destroy) \ 167: (*(xdrs)->x_ops->x_destroy)(xdrs) 168: 169: /* 170: * Support struct for discriminated unions. 171: * You create an array of xdrdiscrim structures, terminated with 172: * a entry with a null procedure pointer. The xdr_union routine gets 173: * the discriminant value and then searches the array of structures 174: * for a matching value. If a match is found the associated xdr routine 175: * is called to handle that part of the union. If there is 176: * no match, then a default routine may be called. 177: * If there is no match and no default routine it is an error. 178: */ 179: #define NULL_xdrproc_t ((xdrproc_t)0) 180: struct xdr_discrim { 181: int value; 182: xdrproc_t proc; 183: }; 184: 185: /* 186: * In-line routines for fast encode/decode of primitve data types. 187: * Caveat emptor: these use single memory cycles to get the 188: * data from the underlying buffer, and will fail to operate 189: * properly if the data is not aligned. The standard way to use these 190: * is to say: 191: * if ((buf = XDR_INLINE(xdrs, count)) == NULL) 192: * return (FALSE); 193: * <<< macro calls >>> 194: * where ``count'' is the number of bytes of data occupied 195: * by the primitive data types. 196: * 197: * N.B. and frozen for all time: each data type here uses 4 bytes 198: * of external representation. 199: */ 200: #define IXDR_GET_LONG(buf) ntohl(*buf++) 201: #define IXDR_PUT_LONG(buf, v) (*buf++ = htonl(v)) 202: 203: #define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf)) 204: #define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf)) 205: #define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_LONG(buf)) 206: #define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf)) 207: #define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_LONG(buf)) 208: 209: #define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG((buf), ((long)(v))) 210: #define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG((buf), ((long)(v))) 211: #define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG((buf), ((long)(v))) 212: #define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG((buf), ((long)(v))) 213: #define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG((buf), ((long)(v))) 214: 215: /* 216: * These are the "generic" xdr routines. 217: */ 218: extern bool_t xdr_void(); 219: extern bool_t xdr_int(); 220: extern bool_t xdr_u_int(); 221: extern bool_t xdr_long(); 222: extern bool_t xdr_u_long(); 223: extern bool_t xdr_short(); 224: extern bool_t xdr_u_short(); 225: extern bool_t xdr_float(); 226: extern bool_t xdr_double(); 227: extern bool_t xdr_bool(); 228: extern bool_t xdr_enum(); 229: extern bool_t xdr_array(); 230: extern bool_t xdr_bytes(); 231: extern bool_t xdr_opaque(); 232: extern bool_t xdr_string(); 233: extern bool_t xdr_wrapstring(); 234: extern bool_t xdr_union(); 235: extern bool_t xdr_reference(); 236: 237: /* 238: * These are the public routines for the various implementations of 239: * xdr streams. 240: */ 241: 242: extern void xdrmem_create(); /* XDR using memory buffers */ 243: extern void xdrstdio_create(); /* XDR using stdio library */ 244: extern void xdrrec_create(); /* XDR pseudo records for tcp */ 245: extern bool_t xdrrec_endofrecord(); /* make end of xdr record */ 246: extern bool_t xdrrec_skiprecord(); /* move to begining of next record */ 247: extern bool_t xdrrec_eof(); /* true iff no more input */