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