1 
2 /*
3  * Copyright (c) 2009, Sun Microsystems, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * - Redistributions of source code must retain the above copyright notice,
9  *   this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright notice,
11  *   this list of conditions and the following disclaimer in the documentation
12  *   and/or other materials provided with the distribution.
13  * - Neither the name of Sun Microsystems, Inc. nor the names of its
14  *   contributors may be used to endorse or promote products derived
15  *   from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
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.23 2003/03/07 13:19:40 nectar Exp $
32  *      $NetBSD: xdr.h,v 1.19 2000/07/17 05:00:45 matt 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 <_ansi.h>
44 #include <rpc/types.h>
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 /*
51  * XDR provides a conventional way for converting between C data
52  * types and an external bit-string representation.  Library supplied
53  * routines provide for the conversion on built-in C data types.  These
54  * routines and utility routines defined here are used to help implement
55  * a type encode/decode routine for each user-defined type.
56  *
57  * Each data type provides a single procedure which takes two arguments:
58  *
59  *        bool_t
60  *        xdrproc(XDR *xdrs, <type> *argresp)
61  *
62  * xdrs is an instance of a XDR handle, to which or from which the data
63  * type is to be converted.  argresp is a pointer to the structure to be
64  * converted.  The XDR handle contains an operation field which indicates
65  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
66  *
67  * XDR_DECODE may allocate space if the pointer argresp is null.  This
68  * data can be freed with the XDR_FREE operation.
69  *
70  * We write only one procedure per data type to make it easy
71  * to keep the encode and decode procedures for a data type consistent.
72  * In many cases the same code performs all operations on a user defined type,
73  * because all the hard work is done in the component type routines.
74  * decode as a series of calls on the nested data types.
75  */
76 
77 /*
78  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
79  * stream.  XDR_DECODE causes the type to be extracted from the stream.
80  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
81  * request.
82  */
83 enum xdr_op
84 {
85   XDR_ENCODE = 0,
86   XDR_DECODE = 1,
87   XDR_FREE = 2
88 };
89 
90 /*
91  * This is the number of bytes per unit of external data.
92  */
93 #define BYTES_PER_XDR_UNIT        (4)
94 #if 1
95 /* faster version when BYTES_PER_XDR_UNIT is a power of two */
96 # define RNDUP(x)  (((x) + BYTES_PER_XDR_UNIT - 1) & ~(BYTES_PER_XDR_UNIT - 1))
97 #else /* old version */
98 #define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
99     * BYTES_PER_XDR_UNIT)
100 #endif
101 
102 /*
103  * The XDR handle.
104  * Contains operation which is being applied to the stream,
105  * an operations vector for the particular implementation (e.g. see xdr_mem.c),
106  * and two private fields for the use of the particular implementation.
107  */
108 typedef struct __rpc_xdr
109 {
110   enum xdr_op x_op;             /* operation; fast additional param */
111   const struct xdr_ops
112   {
113     /* get a long from underlying stream */
114     bool_t (*x_getlong) (struct __rpc_xdr *, long *);
115 
116     /* put a long to " */
117     bool_t (*x_putlong) (struct __rpc_xdr *, const long *);
118 
119     /* get some bytes from " */
120     bool_t (*x_getbytes) (struct __rpc_xdr *, char *, u_int);
121 
122     /* put some bytes to " */
123     bool_t (*x_putbytes) (struct __rpc_xdr *, const char *, u_int);
124 
125     /* returns bytes off from beginning */
126     u_int (*x_getpostn) (struct __rpc_xdr *);
127 
128     /* lets you reposition the stream */
129     bool_t (*x_setpostn) (struct __rpc_xdr *, u_int);
130 
131     /* buf quick ptr to buffered data */
132     int32_t * (*x_inline) (struct __rpc_xdr *, u_int);
133 
134     /* free privates of this xdr_stream */
135     void (*x_destroy) (struct __rpc_xdr *);
136 
137     /* get an int32 from this xdr_stream */
138     bool_t (*x_getint32) (struct __rpc_xdr *, int32_t *);
139 
140     /* put an int32 to the underlying stream */
141     bool_t (*x_putint32) (struct __rpc_xdr *, const int32_t *);
142 
143   } *x_ops;
144   char *x_public;               /* users' data */
145   void *x_private;              /* pointer to private data */
146   char *x_base;                 /* private used for position info */
147   u_int x_handy;                /* extra private word */
148 } XDR;
149 
150 /*
151  * A xdrproc_t exists for each data type which is to be encoded or decoded.
152  *
153  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
154  * The opaque pointer generally points to a structure of the data type
155  * to be decoded.  If this pointer is 0, then the type routines should
156  * allocate dynamic storage of the appropriate size and return it.
157  * bool_t (*xdrproc_t)(XDR *, some_type *)
158  */
159 typedef bool_t (*xdrproc_t) (XDR *, ...);
160 
161 /*
162  * Operations defined on a XDR handle
163  *
164  * XDR   *xdrs;
165  * long  *longp;
166  * char  *addr;
167  * u_int  len;
168  * u_int  pos;
169  */
170 #define XDR_GETINT32(xdrs, int32p)                      \
171         (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
172 #define xdr_getint32(xdrs, int32p)                      \
173         (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
174 
175 #define XDR_PUTINT32(xdrs, int32p)                      \
176         (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
177 #define xdr_putint32(xdrs, int32p)                      \
178         (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
179 
180 #define XDR_GETLONG(xdrs, longp)                        \
181     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
182 #define xdr_getlong(xdrs, longp)                        \
183     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
184 
185 #define XDR_PUTLONG(xdrs, longp)                        \
186     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
187 #define xdr_putlong(xdrs, longp)                        \
188     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
189 
190 #define XDR_GETBYTES(xdrs, addr, len)                   \
191     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
192 #define xdr_getbytes(xdrs, addr, len)                   \
193     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
194 
195 #define XDR_PUTBYTES(xdrs, addr, len)                   \
196     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
197 #define xdr_putbytes(xdrs, addr, len)                   \
198     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
199 
200 #define XDR_GETPOS(xdrs)                                \
201     (*(xdrs)->x_ops->x_getpostn)(xdrs)
202 #define xdr_getpos(xdrs)                                \
203     (*(xdrs)->x_ops->x_getpostn)(xdrs)
204 
205 #define XDR_SETPOS(xdrs, pos)                           \
206     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
207 #define xdr_setpos(xdrs, pos)                           \
208     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
209 
210 #define XDR_INLINE(xdrs, len)                           \
211     (*(xdrs)->x_ops->x_inline)(xdrs, len)
212 #define xdr_inline(xdrs, len)                           \
213     (*(xdrs)->x_ops->x_inline)(xdrs, len)
214 
215 #define XDR_DESTROY(xdrs)                               \
216     do {                                                \
217         if ((xdrs)->x_ops->x_destroy)                   \
218             (*(xdrs)->x_ops->x_destroy)(xdrs);          \
219     } while (0)
220 #define xdr_destroy(xdrs)                               \
221     do {                                                \
222         if ((xdrs)->x_ops->x_destroy)                   \
223             (*(xdrs)->x_ops->x_destroy)(xdrs);          \
224     } while (0)
225 
226 /*
227  * Solaris strips the '_t' from these types -- not sure why.
228  * But, let's be compatible.
229  */
230 #define xdr_rpcvers(xdrs, versp) xdr_u_int32(xdrs, versp)
231 #define xdr_rpcprog(xdrs, progp) xdr_u_int32(xdrs, progp)
232 #define xdr_rpcproc(xdrs, procp) xdr_u_int32(xdrs, procp)
233 #define xdr_rpcprot(xdrs, protp) xdr_u_int32(xdrs, protp)
234 #define xdr_rpcport(xdrs, portp) xdr_u_int32(xdrs, portp)
235 
236 /*
237  * Support struct for discriminated unions.
238  * You create an array of xdrdiscrim structures, terminated with
239  * an entry with a null procedure pointer.  The xdr_union routine gets
240  * the discriminant value and then searches the array of structures
241  * for a matching value.  If a match is found the associated xdr routine
242  * is called to handle that part of the union.  If there is
243  * no match, then a default routine may be called.
244  * If there is no match and no default routine it is an error.
245  */
246 #define NULL_xdrproc_t ((xdrproc_t)0)
247 struct xdr_discrim
248 {
249   int value;
250   xdrproc_t proc;
251 };
252 
253 /*
254  * In-line routines for fast encode/decode of primitive data types.
255  * Caveat emptor: these use single memory cycles to get the
256  * data from the underlying buffer, and will fail to operate
257  * properly if the data is not aligned.  The standard way to use these
258  * is to say:
259  *        if ((buf = XDR_INLINE(xdrs, count)) == NULL)
260  *            return (FALSE);
261  *        <<< macro calls >>>
262  * where ``count'' is the number of bytes of data occupied
263  * by the primitive data types.
264  *
265  * N.B. and frozen for all time: each data type here uses 4 bytes
266  * of external representation.
267  */
268 #define IXDR_GET_INT32(buf)           ((int32_t)ntohl((u_int32_t)*(buf)++))
269 #define IXDR_PUT_INT32(buf, v)        (*(buf)++ =(int32_t)htonl((u_int32_t)v))
270 #define IXDR_GET_U_INT32(buf)         ((uint32_t)IXDR_GET_INT32(buf))
271 #define IXDR_PUT_U_INT32(buf, v)      IXDR_PUT_INT32((buf), ((int32_t)(v)))
272 
273 /* Warning: inline long routines are broken for 64 bit platforms.
274  * Because the other inline routines below are implemented in terms
275  * of them, they are all also broken for 64 bit platforms.
276  */
277 #define IXDR_GET_LONG(buf)            ((long)ntohl((u_int32_t)*(buf)++))
278 #define IXDR_PUT_LONG(buf, v)         (*(buf)++ =(int32_t)htonl((u_int32_t)v))
279 #define IXDR_GET_U_LONG(buf)          ((u_long)IXDR_GET_LONG(buf))
280 #define IXDR_PUT_U_LONG(buf, v)       IXDR_PUT_LONG((buf), (v))
281 
282 #define IXDR_GET_BOOL(buf)            ((bool_t)IXDR_GET_LONG(buf))
283 #define IXDR_GET_ENUM(buf, t)         ((t)IXDR_GET_LONG(buf))
284 #define IXDR_GET_SHORT(buf)           ((short)IXDR_GET_LONG(buf))
285 #define IXDR_GET_U_SHORT(buf)         ((u_short)IXDR_GET_LONG(buf))
286 
287 #define IXDR_PUT_BOOL(buf, v)         IXDR_PUT_LONG((buf), (v))
288 #define IXDR_PUT_ENUM(buf, v)         IXDR_PUT_LONG((buf), (v))
289 #define IXDR_PUT_SHORT(buf, v)        IXDR_PUT_LONG((buf), (v))
290 #define IXDR_PUT_U_SHORT(buf, v)      IXDR_PUT_LONG((buf), (v))
291 
292 /*
293  * These are the "generic" xdr routines.
294  */
295 extern bool_t xdr_void (void);
296 extern bool_t xdr_short (XDR *, short *);
297 extern bool_t xdr_u_short (XDR *, u_short *);
298 extern bool_t xdr_int (XDR *, int *);
299 extern bool_t xdr_u_int (XDR *, u_int *);
300 extern bool_t xdr_long (XDR *, long *);
301 extern bool_t xdr_u_long (XDR *, u_long *);
302 extern bool_t xdr_int8_t (XDR *, int8_t *);
303 extern bool_t xdr_uint8_t (XDR *, uint8_t *);
304 extern bool_t xdr_u_int8_t (XDR *, u_int8_t *);
305 extern bool_t xdr_int16_t (XDR *, int16_t *);
306 extern bool_t xdr_uint16_t (XDR *, uint16_t *);
307 extern bool_t xdr_u_int16_t (XDR *, u_int16_t *);
308 extern bool_t xdr_int32_t (XDR *, int32_t *);
309 extern bool_t xdr_uint32_t (XDR *, uint32_t *);
310 extern bool_t xdr_u_int32_t (XDR *, u_int32_t *);
311 #if defined(___int64_t_defined)
312 extern bool_t xdr_int64_t (XDR *, int64_t *);
313 extern bool_t xdr_uint64_t (XDR *, uint64_t *);
314 extern bool_t xdr_u_int64_t (XDR *, u_int64_t *);
315 #endif /* ___int64_t_defined */
316 extern bool_t xdr_bool (XDR *, bool_t *);
317 extern bool_t xdr_enum (XDR *, enum_t *);
318 extern bool_t xdr_array (XDR *, char **, u_int *, u_int, u_int, xdrproc_t);
319 extern bool_t xdr_bytes (XDR *, char **, u_int *, u_int);
320 extern bool_t xdr_opaque (XDR *, char *, u_int);
321 extern bool_t xdr_string (XDR *, char **, u_int);
322 extern bool_t xdr_union (XDR *, enum_t *, char *,
323                                   const struct xdr_discrim *, xdrproc_t);
324 extern bool_t xdr_char (XDR *, char *);
325 extern bool_t xdr_u_char (XDR *, u_char *);
326 extern bool_t xdr_vector (XDR *, char *, u_int, u_int, xdrproc_t);
327 extern bool_t xdr_float (XDR *, float *);
328 extern bool_t xdr_double (XDR *, double *);
329 /* extern bool_t xdr_quadruple (XDR *, long double *); */
330 extern bool_t xdr_reference (XDR *, char **, u_int, xdrproc_t);
331 extern bool_t xdr_pointer (XDR *, char **, u_int, xdrproc_t);
332 extern bool_t xdr_wrapstring (XDR *, char **);
333 #if defined(___int64_t_defined)
334 extern bool_t xdr_hyper (XDR *, quad_t *);
335 extern bool_t xdr_u_hyper (XDR *, u_quad_t *);
336 extern bool_t xdr_longlong_t (XDR *, quad_t *);
337 extern bool_t xdr_u_longlong_t (XDR *, u_quad_t *);
338 #endif /* ___int64_t_defined */
339 extern u_long xdr_sizeof (xdrproc_t, void *);
340 
341 /*
342  * Common opaque bytes objects used by many rpc protocols;
343  * declared here due to commonality.
344  */
345 #define MAX_NETOBJ_SZ 1024
346 struct netobj
347 {
348   u_int n_len;
349   char *n_bytes;
350 };
351 typedef struct netobj netobj;
352 extern bool_t xdr_netobj (XDR *, struct netobj *);
353 
354 /*
355  * These are the public routines for the various implementations of
356  * xdr streams.
357  */
358 
359 /* XDR using memory buffers */
360 extern void xdrmem_create (XDR *, char *, u_int, enum xdr_op);
361 
362 /* XDR using stdio library */
363 #if defined(_STDIO_H_)
364 extern void xdrstdio_create (XDR *, FILE *, enum xdr_op);
365 #endif
366 
367 /* XDR pseudo records for tcp */
368 extern void xdrrec_create (XDR *, u_int, u_int, void *,
369                                     int (*) (void *, void *, int),
370                                     int (*) (void *, void *, int));
371 
372 /* make end of xdr record */
373 extern bool_t xdrrec_endofrecord (XDR *, bool_t);
374 
375 /* move to beginning of next record */
376 extern bool_t xdrrec_skiprecord (XDR *);
377 
378 /* true if no more input */
379 extern bool_t xdrrec_eof (XDR *);
380 extern u_int xdrrec_readbytes (XDR *, caddr_t, u_int);
381 
382 /* free memory buffers for xdr */
383 extern void xdr_free (xdrproc_t, void *);
384 
385 #ifdef __cplusplus
386 }
387 #endif
388 
389 #endif /* !_RPC_XDR_H */
390