00001 /* @(#)xdr.h 2.2 88/07/29 4.0 RPCSRC */ 00002 /* 00003 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 00004 * unrestricted use provided that this legend is included on all tape 00005 * media and as a part of the software program in whole or part. Users 00006 * may copy or modify Sun RPC without charge, but are not authorized 00007 * to license or distribute it to anyone else except as part of a product or 00008 * program developed by the user. 00009 * 00010 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 00011 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 00012 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 00013 * 00014 * Sun RPC is provided with no support and without any obligation on the 00015 * part of Sun Microsystems, Inc. to assist in its use, correction, 00016 * modification or enhancement. 00017 * 00018 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 00019 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 00020 * OR ANY PART THEREOF. 00021 * 00022 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 00023 * or profits or other special, indirect and consequential damages, even if 00024 * Sun has been advised of the possibility of such damages. 00025 * 00026 * Sun Microsystems, Inc. 00027 * 2550 Garcia Avenue 00028 * Mountain View, California 94043 00029 */ 00030 /* @(#)xdr.h 1.19 87/04/22 SMI */ 00031 00032 /* 00033 * xdr.h, External Data Representation Serialization Routines. 00034 * 00035 * Copyright (C) 1984, Sun Microsystems, Inc. 00036 */ 00037 00042 #ifndef __XDR_HEADER__ 00043 #define __XDR_HEADER__ 00044 00045 /* 00046 * XDR provides a conventional way for converting between C data 00047 * types and an external bit-string representation. Library supplied 00048 * routines provide for the conversion on built-in C data types. These 00049 * routines and utility routines defined here are used to help implement 00050 * a type encode/decode routine for each user-defined type. 00051 * 00052 * Each data type provides a single procedure which takes two arguments: 00053 * 00054 * bool_t 00055 * xdrproc(xdrs, argresp) 00056 * XDR *xdrs; 00057 * <type> *argresp; 00058 * 00059 * xdrs is an instance of a XDR handle, to which or from which the data 00060 * type is to be converted. argresp is a pointer to the structure to be 00061 * converted. The XDR handle contains an operation field which indicates 00062 * which of the operations (ENCODE, DECODE * or FREE) is to be performed. 00063 * 00064 * XDR_DECODE may allocate space if the pointer argresp is null. This 00065 * data can be freed with the XDR_FREE operation. 00066 * 00067 * We write only one procedure per data type to make it easy 00068 * to keep the encode and decode procedures for a data type consistent. 00069 * In many cases the same code performs all operations on a user defined type, 00070 * because all the hard work is done in the component type routines. 00071 * decode as a series of calls on the nested data types. 00072 */ 00073 00074 /* 00075 * Xdr operations. XDR_ENCODE causes the type to be encoded into the 00076 * stream. XDR_DECODE causes the type to be extracted from the stream. 00077 * XDR_FREE can be used to release the space allocated by an XDR_DECODE 00078 * request. 00079 */ 00080 enum xdr_op { 00081 XDR_ENCODE=0, 00082 XDR_DECODE=1, 00083 XDR_FREE=2 00084 }; 00085 00086 /* 00087 * This is the number of bytes per unit of external data. 00088 */ 00089 #define BYTES_PER_XDR_UNIT (4) 00090 #define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \ 00091 * BYTES_PER_XDR_UNIT) 00092 00093 /* 00094 * A xdrproc_t exists for each data type which is to be encoded or decoded. 00095 * 00096 * The second argument to the xdrproc_t is a pointer to an opaque pointer. 00097 * The opaque pointer generally points to a structure of the data type 00098 * to be decoded. If this pointer is 0, then the type routines should 00099 * allocate dynamic storage of the appropriate size and return it. 00100 * bool_t (*xdrproc_t)(XDR *, caddr_t *); 00101 */ 00102 typedef bool_t (*xdrproc_t)(); 00103 00104 /* 00105 * The XDR handle. 00106 * Contains operation which is being applied to the stream, 00107 * an operations vector for the paticular implementation (e.g. see xdr_mem.c), 00108 * and two private fields for the use of the particular impelementation. 00109 */ 00110 typedef struct { 00111 enum xdr_op x_op; /* operation; fast additional param */ 00112 struct xdr_ops { 00113 bool_t (*x_getlong)(); /* get a long from underlying stream */ 00114 bool_t (*x_putlong)(); /* put a long to " */ 00115 bool_t (*x_getbytes)();/* get some bytes from " */ 00116 bool_t (*x_putbytes)();/* put some bytes to " */ 00117 u_int (*x_getpostn)();/* returns bytes off from beginning */ 00118 bool_t (*x_setpostn)();/* lets you reposition the stream */ 00119 long * (*x_inline)(); /* buf quick ptr to buffered data */ 00120 void (*x_destroy)(); /* free privates of this xdr_stream */ 00121 } *x_ops; 00122 caddr_t x_public; /* users' data */ 00123 caddr_t x_private; /* pointer to private data */ 00124 caddr_t x_base; /* private used for position info */ 00125 int x_handy; /* extra private word */ 00126 } XDR; 00127 00128 /* 00129 * Operations defined on a XDR handle 00130 * 00131 * XDR *xdrs; 00132 * long *longp; 00133 * caddr_t addr; 00134 * u_int len; 00135 * u_int pos; 00136 */ 00137 #define XDR_GETLONG(xdrs, longp) \ 00138 (*(xdrs)->x_ops->x_getlong)(xdrs, longp) 00139 #define xdr_getlong(xdrs, longp) \ 00140 (*(xdrs)->x_ops->x_getlong)(xdrs, longp) 00141 00142 #define XDR_PUTLONG(xdrs, longp) \ 00143 (*(xdrs)->x_ops->x_putlong)(xdrs, longp) 00144 #define xdr_putlong(xdrs, longp) \ 00145 (*(xdrs)->x_ops->x_putlong)(xdrs, longp) 00146 00147 #define XDR_GETBYTES(xdrs, addr, len) \ 00148 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len) 00149 #define xdr_getbytes(xdrs, addr, len) \ 00150 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len) 00151 00152 #define XDR_PUTBYTES(xdrs, addr, len) \ 00153 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len) 00154 #define xdr_putbytes(xdrs, addr, len) \ 00155 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len) 00156 00157 #define XDR_GETPOS(xdrs) \ 00158 (*(xdrs)->x_ops->x_getpostn)(xdrs) 00159 #define xdr_getpos(xdrs) \ 00160 (*(xdrs)->x_ops->x_getpostn)(xdrs) 00161 00162 #define XDR_SETPOS(xdrs, pos) \ 00163 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos) 00164 #define xdr_setpos(xdrs, pos) \ 00165 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos) 00166 00167 #define XDR_INLINE(xdrs, len) \ 00168 (*(xdrs)->x_ops->x_inline)(xdrs, len) 00169 #define xdr_inline(xdrs, len) \ 00170 (*(xdrs)->x_ops->x_inline)(xdrs, len) 00171 00172 #define XDR_DESTROY(xdrs) \ 00173 if ((xdrs)->x_ops->x_destroy) \ 00174 (*(xdrs)->x_ops->x_destroy)(xdrs) 00175 #define xdr_destroy(xdrs) \ 00176 if ((xdrs)->x_ops->x_destroy) \ 00177 (*(xdrs)->x_ops->x_destroy)(xdrs) 00178 00179 /* 00180 * Support struct for discriminated unions. 00181 * You create an array of xdrdiscrim structures, terminated with 00182 * a entry with a null procedure pointer. The xdr_union routine gets 00183 * the discriminant value and then searches the array of structures 00184 * for a matching value. If a match is found the associated xdr routine 00185 * is called to handle that part of the union. If there is 00186 * no match, then a default routine may be called. 00187 * If there is no match and no default routine it is an error. 00188 */ 00189 #define NULL_xdrproc_t ((xdrproc_t)0) 00190 struct xdr_discrim { 00191 int value; 00192 xdrproc_t proc; 00193 }; 00194 00195 /* 00196 * In-line routines for fast encode/decode of primitve data types. 00197 * Caveat emptor: these use single memory cycles to get the 00198 * data from the underlying buffer, and will fail to operate 00199 * properly if the data is not aligned. The standard way to use these 00200 * is to say: 00201 * if ((buf = XDR_INLINE(xdrs, count)) == NULL) 00202 * return (FALSE); 00203 * <<< macro calls >>> 00204 * where ``count'' is the number of bytes of data occupied 00205 * by the primitive data types. 00206 * 00207 * N.B. and frozen for all time: each data type here uses 4 bytes 00208 * of external representation. 00209 */ 00210 #define IXDR_GET_LONG(buf) ((long)ntohl((u_long)*(buf)++)) 00211 #define IXDR_PUT_LONG(buf, v) (*(buf)++ = (long)htonl((u_long)v)) 00212 00213 #define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf)) 00214 #define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf)) 00215 #define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_LONG(buf)) 00216 #define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf)) 00217 #define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_LONG(buf)) 00218 00219 #define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG((buf), ((long)(v))) 00220 #define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG((buf), ((long)(v))) 00221 #define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG((buf), ((long)(v))) 00222 #define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG((buf), ((long)(v))) 00223 #define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG((buf), ((long)(v))) 00224 00225 /* 00226 * These are the "generic" xdr routines. 00227 */ 00228 extern bool_t xdr_void(); 00229 extern bool_t xdr_int(); 00230 extern bool_t xdr_u_int(); 00231 extern bool_t xdr_long(); 00232 extern bool_t xdr_u_long(); 00233 extern bool_t xdr_short(); 00234 extern bool_t xdr_u_short(); 00235 extern bool_t xdr_bool(); 00236 extern bool_t xdr_enum(); 00237 extern bool_t xdr_array(); 00238 extern bool_t xdr_bytes(); 00239 extern bool_t xdr_opaque(); 00240 extern bool_t xdr_string(); 00241 extern bool_t xdr_union(); 00242 extern bool_t xdr_char(); 00243 extern bool_t xdr_u_char(); 00244 extern bool_t xdr_vector(); 00245 extern bool_t xdr_float(); 00246 extern bool_t xdr_double(); 00247 extern bool_t xdr_reference(); 00248 extern bool_t xdr_pointer(); 00249 extern bool_t xdr_wrapstring(); 00250 00251 /* 00252 * Common opaque bytes objects used by many rpc protocols; 00253 * declared here due to commonality. 00254 */ 00255 #define MAX_NETOBJ_SZ 1024 00256 struct netobj { 00257 u_int n_len; 00258 char *n_bytes; 00259 }; 00260 typedef struct netobj netobj; 00261 extern bool_t xdr_netobj(); 00262 00263 /* 00264 * These are the public routines for the various implementations of 00265 * xdr streams. 00266 */ 00267 extern void xdrmem_create(); /* XDR using memory buffers */ 00268 extern void xdrstdio_create(); /* XDR using stdio library */ 00269 extern void xdrrec_create(); /* XDR pseudo records for tcp */ 00270 extern bool_t xdrrec_endofrecord(); /* make end of xdr record */ 00271 extern bool_t xdrrec_skiprecord(); /* move to beginning of next record */ 00272 extern bool_t xdrrec_eof(); /* true if no more input */ 00273 00274 #endif !__XDR_HEADER__