xdr.h revision 74462
1/* $NetBSD: xdr.h,v 1.19 2000/07/17 05:00:45 matt Exp $ */ 2 3/* 4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 5 * unrestricted use provided that this legend is included on all tape 6 * media and as a part of the software program in whole or part. Users 7 * may copy or modify Sun RPC without charge, but are not authorized 8 * to license or distribute it to anyone else except as part of a product or 9 * program developed by the user. 10 * 11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 12 * WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 14 * 15 * Sun RPC is provided with no support and without any obligation on the 16 * part of Sun Microsystems, Inc. to assist in its use, correction, 17 * modification or enhancement. 18 * 19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 21 * OR ANY PART THEREOF. 22 * 23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 24 * or profits or other special, indirect and consequential damages, even if 25 * Sun has been advised of the possibility of such damages. 26 * 27 * Sun Microsystems, Inc. 28 * 2550 Garcia Avenue 29 * Mountain View, California 94043 30 * 31 * from: @(#)xdr.h 1.19 87/04/22 SMI 32 * from: @(#)xdr.h 2.2 88/07/29 4.0 RPCSRC 33 * $FreeBSD: head/include/rpc/xdr.h 74462 2001-03-19 12:50:13Z alfred $ 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 */ 81enum 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 particular implementation (e.g. see xdr_mem.c), 98 * and two private fields for the use of the particular implementation. 99 */ 100typedef struct __rpc_xdr { 101 enum xdr_op x_op; /* operation; fast additional param */ 102 const struct xdr_ops { 103 /* get a long from underlying stream */ 104 bool_t (*x_getlong) __P((struct __rpc_xdr *, long *)); 105 /* put a long to " */ 106 bool_t (*x_putlong) __P((struct __rpc_xdr *, const long *)); 107 /* get some bytes from " */ 108 bool_t (*x_getbytes) __P((struct __rpc_xdr *, char *, u_int)); 109 /* put some bytes to " */ 110 bool_t (*x_putbytes) __P((struct __rpc_xdr *, const char *, u_int)); 111 /* returns bytes off from beginning */ 112 u_int (*x_getpostn) __P((struct __rpc_xdr *)); 113 /* lets you reposition the stream */ 114 bool_t (*x_setpostn) __P((struct __rpc_xdr *, u_int)); 115 /* buf quick ptr to buffered data */ 116 int32_t *(*x_inline) __P((struct __rpc_xdr *, u_int)); 117 /* free privates of this xdr_stream */ 118 void (*x_destroy) __P((struct __rpc_xdr *)); 119 bool_t (*x_control) __P((struct __rpc_xdr *, int, void *)); 120 } *x_ops; 121 char * x_public; /* users' data */ 122 void * x_private; /* pointer to private data */ 123 char * x_base; /* private used for position info */ 124 int x_handy; /* extra private word */ 125} XDR; 126 127/* 128 * A xdrproc_t exists for each data type which is to be encoded or decoded. 129 * 130 * The second argument to the xdrproc_t is a pointer to an opaque pointer. 131 * The opaque pointer generally points to a structure of the data type 132 * to be decoded. If this pointer is 0, then the type routines should 133 * allocate dynamic storage of the appropriate size and return it. 134 * 135 * XXX can't actually prototype it, because some take three args!!! 136 */ 137typedef bool_t (*xdrproc_t) __P((/* XDR *, void *, u_int */)); 138 139/* 140 * Operations defined on a XDR handle 141 * 142 * XDR *xdrs; 143 * long *longp; 144 * char * addr; 145 * u_int len; 146 * u_int pos; 147 */ 148#define XDR_GETLONG(xdrs, longp) \ 149 (*(xdrs)->x_ops->x_getlong)(xdrs, longp) 150#define xdr_getlong(xdrs, longp) \ 151 (*(xdrs)->x_ops->x_getlong)(xdrs, longp) 152 153#define XDR_PUTLONG(xdrs, longp) \ 154 (*(xdrs)->x_ops->x_putlong)(xdrs, longp) 155#define xdr_putlong(xdrs, longp) \ 156 (*(xdrs)->x_ops->x_putlong)(xdrs, longp) 157 158static __inline int 159xdr_getint32(XDR *xdrs, int32_t *ip) 160{ 161 long l; 162 163 if (!xdr_getlong(xdrs, &l)) 164 return (FALSE); 165 *ip = (int32_t)l; 166 return (TRUE); 167} 168 169static __inline int 170xdr_putint32(XDR *xdrs, int32_t *ip) 171{ 172 long l; 173 174 l = (long)*ip; 175 return xdr_putlong(xdrs, &l); 176} 177 178#define XDR_GETINT32(xdrs, int32p) xdr_getint32(xdrs, int32p) 179#define XDR_PUTINT32(xdrs, int32p) xdr_putint32(xdrs, int32p) 180 181#define XDR_GETBYTES(xdrs, addr, len) \ 182 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len) 183#define xdr_getbytes(xdrs, addr, len) \ 184 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len) 185 186#define XDR_PUTBYTES(xdrs, addr, len) \ 187 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len) 188#define xdr_putbytes(xdrs, addr, len) \ 189 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len) 190 191#define XDR_GETPOS(xdrs) \ 192 (*(xdrs)->x_ops->x_getpostn)(xdrs) 193#define xdr_getpos(xdrs) \ 194 (*(xdrs)->x_ops->x_getpostn)(xdrs) 195 196#define XDR_SETPOS(xdrs, pos) \ 197 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos) 198#define xdr_setpos(xdrs, pos) \ 199 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos) 200 201#define XDR_INLINE(xdrs, len) \ 202 (*(xdrs)->x_ops->x_inline)(xdrs, len) 203#define xdr_inline(xdrs, len) \ 204 (*(xdrs)->x_ops->x_inline)(xdrs, len) 205 206#define XDR_DESTROY(xdrs) \ 207 if ((xdrs)->x_ops->x_destroy) \ 208 (*(xdrs)->x_ops->x_destroy)(xdrs) 209#define xdr_destroy(xdrs) \ 210 if ((xdrs)->x_ops->x_destroy) \ 211 (*(xdrs)->x_ops->x_destroy)(xdrs) 212 213#define XDR_CONTROL(xdrs, req, op) \ 214 if ((xdrs)->x_ops->x_control) \ 215 (*(xdrs)->x_ops->x_control)(xdrs, req, op) 216#define xdr_control(xdrs, req, op) XDR_CONTROL(xdrs, req, op) 217 218/* 219 * Solaris strips the '_t' from these types -- not sure why. 220 * But, let's be compatible. 221 */ 222#define xdr_rpcvers(xdrs, versp) xdr_u_int32(xdrs, versp) 223#define xdr_rpcprog(xdrs, progp) xdr_u_int32(xdrs, progp) 224#define xdr_rpcproc(xdrs, procp) xdr_u_int32(xdrs, procp) 225#define xdr_rpcprot(xdrs, protp) xdr_u_int32(xdrs, protp) 226#define xdr_rpcport(xdrs, portp) xdr_u_int32(xdrs, portp) 227 228/* 229 * Support struct for discriminated unions. 230 * You create an array of xdrdiscrim structures, terminated with 231 * a entry with a null procedure pointer. The xdr_union routine gets 232 * the discriminant value and then searches the array of structures 233 * for a matching value. If a match is found the associated xdr routine 234 * is called to handle that part of the union. If there is 235 * no match, then a default routine may be called. 236 * If there is no match and no default routine it is an error. 237 */ 238#define NULL_xdrproc_t ((xdrproc_t)0) 239struct xdr_discrim { 240 int value; 241 xdrproc_t proc; 242}; 243 244/* 245 * In-line routines for fast encode/decode of primitive data types. 246 * Caveat emptor: these use single memory cycles to get the 247 * data from the underlying buffer, and will fail to operate 248 * properly if the data is not aligned. The standard way to use these 249 * is to say: 250 * if ((buf = XDR_INLINE(xdrs, count)) == NULL) 251 * return (FALSE); 252 * <<< macro calls >>> 253 * where ``count'' is the number of bytes of data occupied 254 * by the primitive data types. 255 * 256 * N.B. and frozen for all time: each data type here uses 4 bytes 257 * of external representation. 258 */ 259#define IXDR_GET_INT32(buf) ((int32_t)ntohl((u_int32_t)*(buf)++)) 260#define IXDR_PUT_INT32(buf, v) (*(buf)++ =(int32_t)htonl((u_int32_t)v)) 261#define IXDR_GET_U_INT32(buf) ((u_int32_t)IXDR_GET_INT32(buf)) 262#define IXDR_PUT_U_INT32(buf, v) IXDR_PUT_INT32((buf), ((int32_t)(v))) 263 264#define IXDR_GET_LONG(buf) ((long)ntohl((u_int32_t)*(buf)++)) 265#define IXDR_PUT_LONG(buf, v) (*(buf)++ =(int32_t)htonl((u_int32_t)v)) 266 267#define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf)) 268#define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf)) 269#define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_LONG(buf)) 270#define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf)) 271#define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_LONG(buf)) 272 273#define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG((buf), (v)) 274#define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG((buf), (v)) 275#define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG((buf), (v)) 276#define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG((buf), (v)) 277#define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG((buf), (v)) 278 279/* 280 * These are the "generic" xdr routines. 281 */ 282__BEGIN_DECLS 283extern bool_t xdr_void __P((void)); 284extern bool_t xdr_int __P((XDR *, int *)); 285extern bool_t xdr_u_int __P((XDR *, u_int *)); 286extern bool_t xdr_long __P((XDR *, long *)); 287extern bool_t xdr_u_long __P((XDR *, u_long *)); 288extern bool_t xdr_short __P((XDR *, short *)); 289extern bool_t xdr_u_short __P((XDR *, u_short *)); 290extern bool_t xdr_int16_t __P((XDR *, int16_t *)); 291extern bool_t xdr_u_int16_t __P((XDR *, u_int16_t *)); 292extern bool_t xdr_int32_t __P((XDR *, int32_t *)); 293extern bool_t xdr_u_int32_t __P((XDR *, u_int32_t *)); 294extern bool_t xdr_int64_t __P((XDR *, int64_t *)); 295extern bool_t xdr_u_int64_t __P((XDR *, u_int64_t *)); 296extern bool_t xdr_bool __P((XDR *, bool_t *)); 297extern bool_t xdr_enum __P((XDR *, enum_t *)); 298extern bool_t xdr_array __P((XDR *, char **, u_int *, u_int, u_int, xdrproc_t)); 299extern bool_t xdr_bytes __P((XDR *, char **, u_int *, u_int)); 300extern bool_t xdr_opaque __P((XDR *, char *, u_int)); 301extern bool_t xdr_string __P((XDR *, char **, u_int)); 302extern bool_t xdr_union __P((XDR *, enum_t *, char *, const struct xdr_discrim *, xdrproc_t)); 303extern bool_t xdr_char __P((XDR *, char *)); 304extern bool_t xdr_u_char __P((XDR *, u_char *)); 305extern bool_t xdr_vector __P((XDR *, char *, u_int, u_int, xdrproc_t)); 306extern bool_t xdr_float __P((XDR *, float *)); 307extern bool_t xdr_double __P((XDR *, double *)); 308extern bool_t xdr_quadruple __P((XDR *, long double *)); 309extern bool_t xdr_reference __P((XDR *, char **, u_int, xdrproc_t)); 310extern bool_t xdr_pointer __P((XDR *, char **, u_int, xdrproc_t)); 311extern bool_t xdr_wrapstring __P((XDR *, char **)); 312extern void xdr_free __P((xdrproc_t, char *)); 313extern bool_t xdr_hyper __P((XDR *, quad_t *)); 314extern bool_t xdr_u_hyper __P((XDR *, u_quad_t *)); 315extern bool_t xdr_longlong_t __P((XDR *, quad_t *)); 316extern bool_t xdr_u_longlong_t __P((XDR *, u_quad_t *)); 317__END_DECLS 318 319/* 320 * Common opaque bytes objects used by many rpc protocols; 321 * declared here due to commonality. 322 */ 323#define MAX_NETOBJ_SZ 1024 324struct netobj { 325 u_int n_len; 326 char *n_bytes; 327}; 328typedef struct netobj netobj; 329extern bool_t xdr_netobj __P((XDR *, struct netobj *)); 330 331/* 332 * These are the public routines for the various implementations of 333 * xdr streams. 334 */ 335__BEGIN_DECLS 336/* XDR using memory buffers */ 337extern void xdrmem_create __P((XDR *, char *, u_int, enum xdr_op)); 338 339/* XDR using stdio library */ 340#ifdef _STDIO_H_ 341extern void xdrstdio_create __P((XDR *, FILE *, enum xdr_op)); 342#endif 343 344/* XDR pseudo records for tcp */ 345extern void xdrrec_create __P((XDR *, u_int, u_int, char *, 346 int (*) __P((char *, char *, int)), 347 int (*) __P((char *, char *, int)))); 348 349/* make end of xdr record */ 350extern bool_t xdrrec_endofrecord __P((XDR *, int)); 351 352/* move to beginning of next record */ 353extern bool_t xdrrec_skiprecord __P((XDR *)); 354 355/* true if no more input */ 356extern bool_t xdrrec_eof __P((XDR *)); 357extern u_int xdrrec_readbytes __P((XDR *, caddr_t, u_int)); 358__END_DECLS 359 360#endif /* !_RPC_XDR_H */ 361