Import TBBT (NFS trace replay).
[bluesky.git] / TBBT / trace_play / rpc / xdr.h
1 /*
2  * @(#)xdr.h     2.1     97/10/23
3  */
4
5 /* @(#)xdr.h    2.2 88/07/29 4.0 RPCSRC */
6 /*
7  *   Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
8  *      All rights reserved.
9  *              Standard Performance Evaluation Corporation (SPEC)
10  *              6585 Merchant Place, Suite 100
11  *              Warrenton, VA 20187
12  *
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.
16  *
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.
20  *
21  *      The source code is provided to the user or company under the license
22  *      agreement for the SPEC Benchmark Suite for this product.
23  */
24 /*
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.
31  * 
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.
35  * 
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.
39  * 
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.
43  * 
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.
47  * 
48  * Sun Microsystems, Inc.
49  * 2550 Garcia Avenue
50  * Mountain View, California  94043
51  */
52 /*      @(#)xdr.h 1.19 87/04/22 SMI      */
53
54 /*
55  * xdr.h, External Data Representation Serialization Routines.
56  *
57  * Copyright (C) 1984, Sun Microsystems, Inc.
58  */
59
60 #ifndef __XDR_HEADER__
61 #define __XDR_HEADER__
62
63 #include <stdio.h>
64
65 /*
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.
71  *
72  * Each data type provides a single procedure which takes two arguments:
73  *
74  *      bool_t
75  *      xdrproc(xdrs, argresp)
76  *              XDR *xdrs;
77  *              <type> *argresp;
78  *
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.
83  *
84  * XDR_DECODE may allocate space if the pointer argresp is null.  This
85  * data can be freed with the XDR_FREE operation.
86  *
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.
92  */
93
94 /*
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
98  * request.
99  */
100 enum xdr_op {
101         XDR_ENCODE=0,
102         XDR_DECODE=1,
103         XDR_FREE=2
104 };
105
106 /*
107  * This is the number of bytes per unit of external data.
108  */
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)
112
113 /*
114  * A xdrproc_t exists for each data type which is to be encoded or decoded.
115  *
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 **);
121  */
122 typedef bool_t (*xdrproc_t)();
123
124 /*
125  * The XDR handle.
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.
129  */
130 typedef struct {
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 */
137 } XDR;
138
139 struct xdr_ops {
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 *);
156 };
157
158 /*
159  * Operations defined on a XDR handle
160  *
161  * XDR          *xdrs;
162  * int32_t      *longp;
163  * void *        addr;
164  * uint_t        len;
165  * uint_t        pos;
166  */
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)
171
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)
176
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)
181
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)
186
187 #define XDR_GETPOS(xdrs)                                \
188         (*(xdrs)->x_ops->x_getpostn)(xdrs)
189 #define xdr_getpos(xdrs)                                \
190         (*(xdrs)->x_ops->x_getpostn)(xdrs)
191
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)
196
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)
201
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)
208
209 /*
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.
218  */
219 #define NULL_xdrproc_t ((xdrproc_t)0)
220 struct xdr_discrim {
221         int     value;
222         xdrproc_t proc;
223 };
224
225 /*
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
230  * is to say:
231  *      if ((buf = XDR_INLINE(xdrs, count)) == NULL)
232  *              return (FALSE);
233  *      <<< macro calls >>>
234  * where ``count'' is the number of bytes of data occupied
235  * by the primitive data types.
236  *
237  * N.B. and frozen for all time: each data type here uses 4 bytes
238  * of external representation.
239  */
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))
242
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))
248
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)))
254
255 /*
256  * These are the "generic" xdr routines.
257  */
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 **);
284
285 /*
286  * Common opaque bytes objects used by many rpc protocols;
287  * declared here due to commonality.
288  */
289 #define MAX_NETOBJ_SZ 1024 
290 struct netobj {
291         uint_t  n_len;
292         char    *n_bytes;
293 };
294 typedef struct netobj netobj;
295 extern bool_t   xdr_netobj(XDR *, netobj *);
296
297 /*
298  * These are the public routines for the various implementations of
299  * xdr streams.
300  */
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 *);
307
308 #endif /* !__XDR_HEADER__ */