1e1ca7e353eb0b177654ff50e5ed1212bd0cd708
[dragonfly.git] / include / rpc / xdr.h
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  * $FreeBSD: src/include/rpc/xdr.h,v 1.14.2.1 2003/03/20 12:59:55 jedgar Exp $
32  * $DragonFly: src/include/rpc/xdr.h,v 1.3 2003/11/14 01:01:50 dillon Exp $
33  */
34
35 /*
36  * xdr.h, External Data Representation Serialization Routines.
37  *
38  * Copyright (C) 1984, Sun Microsystems, Inc.
39  */
40
41 #ifndef _RPC_XDR_H
42 #define _RPC_XDR_H
43 #include <sys/cdefs.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  */
80 enum 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  */
99 typedef 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) (struct __rpc_xdr *, long *);
104                 /* put a long to underlying stream */
105                 bool_t  (*x_putlong) (struct __rpc_xdr *, long *);
106                 /* get some bytes from underlying stream */
107                 bool_t  (*x_getbytes) (struct __rpc_xdr *, caddr_t, u_int);
108                 /* put some bytes to underlying stream */
109                 bool_t  (*x_putbytes) (struct __rpc_xdr *, caddr_t, u_int);
110                 /* returns bytes off from beginning */
111                 u_int   (*x_getpostn) (struct __rpc_xdr *);
112                 /* lets you reposition the stream */
113                 bool_t  (*x_setpostn) (struct __rpc_xdr *, u_int);
114                 /* buf quick ptr to buffered data */
115                 int32_t *(*x_inline) (struct __rpc_xdr *, u_int);
116                 /* free privates of this xdr_stream */
117                 void    (*x_destroy) (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         u_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 #ifdef _KERNEL
134 typedef bool_t (*xdrproc_t) (XDR *, void *, u_int);
135 #else
136 /*
137  * XXX can't actually prototype it, because some take two args!!!
138  */
139 typedef bool_t (*xdrproc_t) (/* XDR *, void *, u_int */);
140 #endif
141
142 /*
143  * Operations defined on a XDR handle
144  *
145  * XDR          *xdrs;
146  * long         *longp;
147  * caddr_t       addr;
148  * u_int         len;
149  * u_int         pos;
150  */
151 #define XDR_GETLONG(xdrs, longp)                        \
152         (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
153 #define xdr_getlong(xdrs, longp)                        \
154         (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
155
156 #define XDR_PUTLONG(xdrs, longp)                        \
157         (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
158 #define xdr_putlong(xdrs, longp)                        \
159         (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
160
161 #define XDR_GETBYTES(xdrs, addr, len)                   \
162         (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
163 #define xdr_getbytes(xdrs, addr, len)                   \
164         (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
165
166 #define XDR_PUTBYTES(xdrs, addr, len)                   \
167         (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
168 #define xdr_putbytes(xdrs, addr, len)                   \
169         (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
170
171 #define XDR_GETPOS(xdrs)                                \
172         (*(xdrs)->x_ops->x_getpostn)(xdrs)
173 #define xdr_getpos(xdrs)                                \
174         (*(xdrs)->x_ops->x_getpostn)(xdrs)
175
176 #define XDR_SETPOS(xdrs, pos)                           \
177         (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
178 #define xdr_setpos(xdrs, pos)                           \
179         (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
180
181 #define XDR_INLINE(xdrs, len)                           \
182         (*(xdrs)->x_ops->x_inline)(xdrs, len)
183 #define xdr_inline(xdrs, len)                           \
184         (*(xdrs)->x_ops->x_inline)(xdrs, len)
185
186 #define XDR_DESTROY(xdrs)                               \
187         if ((xdrs)->x_ops->x_destroy)                   \
188                 (*(xdrs)->x_ops->x_destroy)(xdrs)
189 #define xdr_destroy(xdrs)                               \
190         if ((xdrs)->x_ops->x_destroy)                   \
191                 (*(xdrs)->x_ops->x_destroy)(xdrs)
192
193 /*
194  * Support struct for discriminated unions.
195  * You create an array of xdrdiscrim structures, terminated with
196  * a entry with a null procedure pointer.  The xdr_union routine gets
197  * the discriminant value and then searches the array of structures
198  * for a matching value.  If a match is found the associated xdr routine
199  * is called to handle that part of the union.  If there is
200  * no match, then a default routine may be called.
201  * If there is no match and no default routine it is an error.
202  */
203 #define NULL_xdrproc_t ((xdrproc_t)0)
204 struct xdr_discrim {
205         int     value;
206         xdrproc_t proc;
207 };
208
209 /*
210  * In-line routines for fast encode/decode of primitive data types.
211  * Caveat emptor: these use single memory cycles to get the
212  * data from the underlying buffer, and will fail to operate
213  * properly if the data is not aligned.  The standard way to use these
214  * is to say:
215  *      if ((buf = XDR_INLINE(xdrs, count)) == NULL)
216  *              return (FALSE);
217  *      <<< macro calls >>>
218  * where ``count'' is the number of bytes of data occupied
219  * by the primitive data types.
220  *
221  * N.B. and frozen for all time: each data type here uses 4 bytes
222  * of external representation.
223  */
224 #define IXDR_GET_LONG(buf)              ((long)ntohl((u_long)*(buf)++))
225 #define IXDR_PUT_LONG(buf, v)           (*(buf)++ = (long)htonl((u_long)v))
226
227 #define IXDR_GET_BOOL(buf)              ((bool_t)IXDR_GET_LONG(buf))
228 #define IXDR_GET_ENUM(buf, t)           ((t)IXDR_GET_LONG(buf))
229 #define IXDR_GET_U_LONG(buf)            ((u_long)IXDR_GET_LONG(buf))
230 #define IXDR_GET_SHORT(buf)             ((short)IXDR_GET_LONG(buf))
231 #define IXDR_GET_U_SHORT(buf)           ((u_short)IXDR_GET_LONG(buf))
232
233 #define IXDR_PUT_BOOL(buf, v)           IXDR_PUT_LONG((buf), ((long)(v)))
234 #define IXDR_PUT_ENUM(buf, v)           IXDR_PUT_LONG((buf), ((long)(v)))
235 #define IXDR_PUT_U_LONG(buf, v)         IXDR_PUT_LONG((buf), ((long)(v)))
236 #define IXDR_PUT_SHORT(buf, v)          IXDR_PUT_LONG((buf), ((long)(v)))
237 #define IXDR_PUT_U_SHORT(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
238
239 /*
240  * These are the "generic" xdr routines.
241  */
242 __BEGIN_DECLS
243 extern bool_t   xdr_void        (void);
244 extern bool_t   xdr_int         (XDR *, int *);
245 extern bool_t   xdr_u_int       (XDR *, u_int *);
246 extern bool_t   xdr_long        (XDR *, long *);
247 extern bool_t   xdr_u_long      (XDR *, u_long *);
248 extern bool_t   xdr_short       (XDR *, short *);
249 extern bool_t   xdr_u_short     (XDR *, u_short *);
250 extern bool_t   xdr_int16_t     (XDR *, int16_t *);
251 extern bool_t   xdr_u_int16_t   (XDR *, u_int16_t *);
252 extern bool_t   xdr_int32_t     (XDR *, int32_t *);
253 extern bool_t   xdr_u_int32_t   (XDR *, u_int32_t *);
254 extern bool_t   xdr_int64_t     (XDR *, int64_t *);
255 extern bool_t   xdr_u_int64_t   (XDR *, u_int64_t *);
256 extern bool_t   xdr_bool        (XDR *, bool_t *);
257 extern bool_t   xdr_enum        (XDR *, enum_t *);
258 extern bool_t   xdr_array       (XDR *, char **, u_int *, u_int, u_int, xdrproc_t);
259 extern bool_t   xdr_bytes       (XDR *, char **, u_int *, u_int);
260 extern bool_t   xdr_opaque      (XDR *, caddr_t, u_int);
261 extern bool_t   xdr_string      (XDR *, char **, u_int);
262 extern bool_t   xdr_union       (XDR *, enum_t *, char *, struct xdr_discrim *, xdrproc_t);
263 extern unsigned long    xdr_sizeof (xdrproc_t, void *);
264 extern bool_t   xdr_char        (XDR *, char *);
265 extern bool_t   xdr_u_char      (XDR *, u_char *);
266 extern bool_t   xdr_vector      (XDR *, char *, u_int, u_int, xdrproc_t);
267 extern bool_t   xdr_float       (XDR *, float *);
268 extern bool_t   xdr_double      (XDR *, double *);
269 extern bool_t   xdr_reference   (XDR *, caddr_t *, u_int, xdrproc_t);
270 extern bool_t   xdr_pointer     (XDR *, caddr_t *, u_int, xdrproc_t);
271 extern bool_t   xdr_wrapstring  (XDR *, char **);
272 extern void     xdr_free        (xdrproc_t, char *);
273 __END_DECLS
274
275 /*
276  * Common opaque bytes objects used by many rpc protocols;
277  * declared here due to commonality.
278  */
279 #define MAX_NETOBJ_SZ 1024
280 struct netobj {
281         u_int   n_len;
282         char    *n_bytes;
283 };
284 typedef struct netobj netobj;
285 extern bool_t   xdr_netobj (XDR *, struct netobj *);
286
287 /*
288  * These are the public routines for the various implementations of
289  * xdr streams.
290  */
291 __BEGIN_DECLS
292 /* XDR using memory buffers */
293 extern void   xdrmem_create     (XDR *, char *, u_int, enum xdr_op);
294
295 #ifdef _STDIO_H_
296 /* XDR using stdio library */
297 extern void   xdrstdio_create   (XDR *, FILE *, enum xdr_op);
298 #endif
299
300 /* XDR pseudo records for tcp */
301 extern void   xdrrec_create     (XDR *, u_int, u_int, char *,
302                                 int (*) (caddr_t, caddr_t, int),
303                                 int (*) (caddr_t, caddr_t, int));
304
305 /* make end of xdr record */
306 extern bool_t xdrrec_endofrecord (XDR *, int);
307
308 /* move to beginning of next record */
309 extern bool_t xdrrec_skiprecord (XDR *);
310
311 /* true if no more input */
312 extern bool_t xdrrec_eof        (XDR *);
313 __END_DECLS
314
315 #endif /* !_RPC_XDR_H */