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