2 * @(#)xdr.h 2.1 97/10/23
5 /* @(#)xdr.h 2.2 88/07/29 4.0 RPCSRC */
7 * Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
9 * Standard Performance Evaluation Corporation (SPEC)
10 * 6585 Merchant Place, Suite 100
13 * This product contains benchmarks acquired from several sources who
14 * understand and agree with SPEC's goal of creating fair and objective
15 * benchmarks to measure computer performance.
17 * This copyright notice is placed here only to protect SPEC in the
18 * event the source is misused in any manner that is contrary to the
19 * spirit, the goals and the intent of SPEC.
21 * The source code is provided to the user or company under the license
22 * agreement for the SPEC Benchmark Suite for this product.
25 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
26 * unrestricted use provided that this legend is included on all tape
27 * media and as a part of the software program in whole or part. Users
28 * may copy or modify Sun RPC without charge, but are not authorized
29 * to license or distribute it to anyone else except as part of a product or
30 * program developed by the user.
32 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
33 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
36 * Sun RPC is provided with no support and without any obligation on the
37 * part of Sun Microsystems, Inc. to assist in its use, correction,
38 * modification or enhancement.
40 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
41 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
42 * OR ANY PART THEREOF.
44 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
45 * or profits or other special, indirect and consequential damages, even if
46 * Sun has been advised of the possibility of such damages.
48 * Sun Microsystems, Inc.
50 * Mountain View, California 94043
52 /* @(#)xdr.h 1.19 87/04/22 SMI */
55 * xdr.h, External Data Representation Serialization Routines.
57 * Copyright (C) 1984, Sun Microsystems, Inc.
60 #ifndef __XDR_HEADER__
61 #define __XDR_HEADER__
66 * XDR provides a conventional way for converting between C data
67 * types and an external bit-string representation. Library supplied
68 * routines provide for the conversion on built-in C data types. These
69 * routines and utility routines defined here are used to help implement
70 * a type encode/decode routine for each user-defined type.
72 * Each data type provides a single procedure which takes two arguments:
75 * xdrproc(xdrs, argresp)
79 * xdrs is an instance of a XDR handle, to which or from which the data
80 * type is to be converted. argresp is a pointer to the structure to be
81 * converted. The XDR handle contains an operation field which indicates
82 * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
84 * XDR_DECODE may allocate space if the pointer argresp is null. This
85 * data can be freed with the XDR_FREE operation.
87 * We write only one procedure per data type to make it easy
88 * to keep the encode and decode procedures for a data type consistent.
89 * In many cases the same code performs all operations on a user defined type,
90 * because all the hard work is done in the component type routines.
91 * decode as a series of calls on the nested data types.
95 * Xdr operations. XDR_ENCODE causes the type to be encoded into the
96 * stream. XDR_DECODE causes the type to be extracted from the stream.
97 * XDR_FREE can be used to release the space allocated by an XDR_DECODE
107 * This is the number of bytes per unit of external data.
109 #define BYTES_PER_XDR_UNIT (4)
110 #define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
111 * BYTES_PER_XDR_UNIT)
114 * A xdrproc_t exists for each data type which is to be encoded or decoded.
116 * The second argument to the xdrproc_t is a pointer to an opaque pointer.
117 * The opaque pointer generally points to a structure of the data type
118 * to be decoded. If this pointer is 0, then the type routines should
119 * allocate dynamic storage of the appropriate size and return it.
120 * bool_t (*xdrproc_t)(XDR *, void **);
122 typedef bool_t (*xdrproc_t)();
126 * Contains operation which is being applied to the stream,
127 * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
128 * and two private fields for the use of the particular impelementation.
131 enum xdr_op x_op; /* operation; fast additional param */
132 struct xdr_ops *x_ops;
133 char * x_public; /* users' data */
134 char * x_private; /* pointer to private data */
135 char * x_base; /* private used for position info */
136 int x_handy; /* extra private word */
140 /* get a long from underlying stream */
141 bool_t (*x_getlong)(XDR *, int32_t *);
142 /* put a long to " */
143 bool_t (*x_putlong)(XDR *, int32_t *);
144 /* get some bytes from " */
145 bool_t (*x_getbytes)(XDR *, void *, uint_t);
146 /* put some bytes to " */
147 bool_t (*x_putbytes)(XDR *, void *, uint_t);
148 /* returns bytes off from beginning */
149 uint_t (*x_getpostn)(XDR *);
150 /* lets you reposition the stream */
151 bool_t (*x_setpostn)(XDR *, uint_t);
152 /* buf quick ptr to buffered data */
153 int32_t *(*x_inline)(XDR *, uint_t);
154 /* free privates of this xdr_stream */
155 void (*x_destroy)(XDR *);
159 * Operations defined on a XDR handle
167 #define XDR_GETLONG(xdrs, longp) \
168 (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
169 #define xdr_getlong(xdrs, longp) \
170 (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
172 #define XDR_PUTLONG(xdrs, longp) \
173 (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
174 #define xdr_putlong(xdrs, longp) \
175 (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
177 #define XDR_GETBYTES(xdrs, addr, len) \
178 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
179 #define xdr_getbytes(xdrs, addr, len) \
180 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
182 #define XDR_PUTBYTES(xdrs, addr, len) \
183 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
184 #define xdr_putbytes(xdrs, addr, len) \
185 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
187 #define XDR_GETPOS(xdrs) \
188 (*(xdrs)->x_ops->x_getpostn)(xdrs)
189 #define xdr_getpos(xdrs) \
190 (*(xdrs)->x_ops->x_getpostn)(xdrs)
192 #define XDR_SETPOS(xdrs, pos) \
193 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
194 #define xdr_setpos(xdrs, pos) \
195 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
197 #define XDR_INLINE(xdrs, len) \
198 (*(xdrs)->x_ops->x_inline)(xdrs, len)
199 #define xdr_inline(xdrs, len) \
200 (*(xdrs)->x_ops->x_inline)(xdrs, len)
202 #define XDR_DESTROY(xdrs) \
203 if ((xdrs)->x_ops->x_destroy) \
204 (*(xdrs)->x_ops->x_destroy)(xdrs)
205 #define xdr_destroy(xdrs) \
206 if ((xdrs)->x_ops->x_destroy) \
207 (*(xdrs)->x_ops->x_destroy)(xdrs)
210 * Support struct for discriminated unions.
211 * You create an array of xdrdiscrim structures, terminated with
212 * a entry with a null procedure pointer. The xdr_union routine gets
213 * the discriminant value and then searches the array of structures
214 * for a matching value. If a match is found the associated xdr routine
215 * is called to handle that part of the union. If there is
216 * no match, then a default routine may be called.
217 * If there is no match and no default routine it is an error.
219 #define NULL_xdrproc_t ((xdrproc_t)0)
226 * In-line routines for fast encode/decode of primitve data types.
227 * Caveat emptor: these use single memory cycles to get the
228 * data from the underlying buffer, and will fail to operate
229 * properly if the data is not aligned. The standard way to use these
231 * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
233 * <<< macro calls >>>
234 * where ``count'' is the number of bytes of data occupied
235 * by the primitive data types.
237 * N.B. and frozen for all time: each data type here uses 4 bytes
238 * of external representation.
240 #define IXDR_GET_LONG(buf) ((int32_t)ntohl((uint32_t)*(buf)++))
241 #define IXDR_PUT_LONG(buf, v) (*(buf)++ = (int32_t)htonl((uint32_t)v))
243 #define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
244 #define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
245 #define IXDR_GET_U_LONG(buf) ((uint32_t)IXDR_GET_LONG(buf))
246 #define IXDR_GET_SHORT(buf) ((int16_t)IXDR_GET_LONG(buf))
247 #define IXDR_GET_U_SHORT(buf) ((uint16_t)IXDR_GET_LONG(buf))
249 #define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG((buf), ((int32_t)(v)))
250 #define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG((buf), ((int32_t)(v)))
251 #define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG((buf), ((int32_t)(v)))
252 #define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG((buf), ((int32_t)(v)))
253 #define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG((buf), ((int32_t)(v)))
256 * These are the "generic" xdr routines.
258 extern bool_t xdr_void(void);
259 extern bool_t xdr_int(XDR *, int *);
260 extern bool_t xdr_u_int(XDR *, uint_t *);
261 extern bool_t xdr_long(XDR *, int32_t *);
262 extern bool_t xdr_u_long(XDR *, uint32_t *);
263 extern bool_t xdr_int32_t(XDR *, int32_t *);
264 extern bool_t xdr_uint32_t(XDR *, uint32_t *);
265 extern bool_t xdr_short(XDR *, int16_t *);
266 extern bool_t xdr_u_short(XDR *, uint16_t *);
267 extern bool_t xdr_int16_t(XDR *, int16_t *);
268 extern bool_t xdr_uint16_t(XDR *, uint16_t *);
269 extern bool_t xdr_bool(XDR *, bool_t *);
270 extern bool_t xdr_enum(XDR *, enum_t *);
271 extern bool_t xdr_array(XDR *, void **, uint_t *, uint_t, uint_t, xdrproc_t);
272 extern bool_t xdr_bytes(XDR *, char **cp, uint_t *, uint_t);
273 extern bool_t xdr_opaque(XDR *, void *, uint_t);
274 extern bool_t xdr_string(XDR *, char **cp, uint_t);
275 extern bool_t xdr_union(XDR *, enum_t *, char *, struct xdr_discrim *, xdrproc_t);
276 extern bool_t xdr_char(XDR *, char *);
277 extern bool_t xdr_u_char(XDR *, uchar_t *);
278 extern bool_t xdr_vector(XDR *, char *, uint_t, uint_t, xdrproc_t);
279 extern bool_t xdr_float(XDR *, float *);
280 extern bool_t xdr_double(XDR *, double *);
281 extern bool_t xdr_reference(XDR *, void **, uint_t, xdrproc_t);
282 extern bool_t xdr_pointer(XDR *, void **, uint_t, xdrproc_t);
283 extern bool_t xdr_wrapstring(XDR *, char **);
286 * Common opaque bytes objects used by many rpc protocols;
287 * declared here due to commonality.
289 #define MAX_NETOBJ_SZ 1024
294 typedef struct netobj netobj;
295 extern bool_t xdr_netobj(XDR *, netobj *);
298 * These are the public routines for the various implementations of
301 extern void xdrmem_create(XDR *, void *, uint_t, enum xdr_op);
302 extern void xdrstdio_create(XDR *, FILE *, enum xdr_op);
303 extern void xdrrec_create(XDR *, uint_t, uint_t, void *, int (*)(), int (*)());
304 extern bool_t xdrrec_endofrecord(XDR *, bool_t);
305 extern bool_t xdrrec_skiprecord(XDR *);
306 extern bool_t xdrrec_eof(XDR *);
308 #endif /* !__XDR_HEADER__ */