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