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