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