source: svn/trunk/newcon3bcm2_21bu/toolchain/mipsel-linux-uclibc/include/rpc/xdr.h

Last change on this file was 2, checked in by jglee, 11 years ago

first commit

  • Property svn:executable set to *
File size: 14.6 KB
Line 
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, MERCHANTIBILITY 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
30/*
31 * xdr.h, External Data Representation Serialization Routines.
32 *
33 * Copyright (C) 1984, Sun Microsystems, Inc.
34 */
35
36#ifndef _RPC_XDR_H
37#define _RPC_XDR_H 1
38
39#ifdef _LIBC
40/* Some adjustments to make the libc source from glibc
41 * compile more easily with uClibc... */
42#ifndef __FORCE_GLIBC
43#define __FORCE_GLIBC
44#endif
45#ifndef _GNU_SOUCE
46#define _GNU_SOUCE
47#endif
48#define _(X)    X
49#endif
50#include <features.h>
51#include <sys/types.h>
52#include <rpc/types.h>
53
54/* We need FILE.  */
55#include <stdio.h>
56
57__BEGIN_DECLS
58
59/*
60 * XDR provides a conventional way for converting between C data
61 * types and an external bit-string representation.  Library supplied
62 * routines provide for the conversion on built-in C data types.  These
63 * routines and utility routines defined here are used to help implement
64 * a type encode/decode routine for each user-defined type.
65 *
66 * Each data type provides a single procedure which takes two arguments:
67 *
68 *      bool_t
69 *      xdrproc(xdrs, argresp)
70 *              XDR *xdrs;
71 *              <type> *argresp;
72 *
73 * xdrs is an instance of a XDR handle, to which or from which the data
74 * type is to be converted.  argresp is a pointer to the structure to be
75 * converted.  The XDR handle contains an operation field which indicates
76 * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
77 *
78 * XDR_DECODE may allocate space if the pointer argresp is null.  This
79 * data can be freed with the XDR_FREE operation.
80 *
81 * We write only one procedure per data type to make it easy
82 * to keep the encode and decode procedures for a data type consistent.
83 * In many cases the same code performs all operations on a user defined type,
84 * because all the hard work is done in the component type routines.
85 * decode as a series of calls on the nested data types.
86 */
87
88/*
89 * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
90 * stream.  XDR_DECODE causes the type to be extracted from the stream.
91 * XDR_FREE can be used to release the space allocated by an XDR_DECODE
92 * request.
93 */
94enum xdr_op {
95  XDR_ENCODE = 0,
96  XDR_DECODE = 1,
97  XDR_FREE = 2
98};
99
100/*
101 * This is the number of bytes per unit of external data.
102 */
103#define BYTES_PER_XDR_UNIT      (4)
104/*
105 * This only works if the above is a power of 2.  But it's defined to be
106 * 4 by the appropriate RFCs.  So it will work.  And it's normally quicker
107 * than the old routine.
108 */
109#if 1
110#define RNDUP(x)  (((x) + BYTES_PER_XDR_UNIT - 1) & ~(BYTES_PER_XDR_UNIT - 1))
111#else /* this is the old routine */
112#define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
113                    * BYTES_PER_XDR_UNIT)
114#endif
115
116/*
117 * The XDR handle.
118 * Contains operation which is being applied to the stream,
119 * an operations vector for the particular implementation (e.g. see xdr_mem.c),
120 * and two private fields for the use of the particular implementation.
121 */
122typedef struct XDR XDR;
123struct XDR
124  {
125    enum xdr_op x_op;           /* operation; fast additional param */
126    struct xdr_ops
127      {
128        bool_t (*x_getlong) (XDR *__xdrs, long *__lp);
129        /* get a long from underlying stream */
130        bool_t (*x_putlong) (XDR *__xdrs, __const long *__lp);
131        /* put a long to " */
132        bool_t (*x_getbytes) (XDR *__xdrs, caddr_t __addr, u_int __len);
133        /* get some bytes from " */
134        bool_t (*x_putbytes) (XDR *__xdrs, __const char *__addr, u_int __len);
135        /* put some bytes to " */
136        u_int (*x_getpostn) (__const XDR *__xdrs);
137        /* returns bytes off from beginning */
138        bool_t (*x_setpostn) (XDR *__xdrs, u_int __pos);
139        /* lets you reposition the stream */
140        int32_t *(*x_inline) (XDR *__xdrs, int __len);
141        /* buf quick ptr to buffered data */
142        void (*x_destroy) (XDR *__xdrs);
143        /* free privates of this xdr_stream */
144        bool_t (*x_getint32) (XDR *__xdrs, int32_t *__ip);
145        /* get a int from underlying stream */
146        bool_t (*x_putint32) (XDR *__xdrs, __const int32_t *__ip);
147        /* put a int to " */
148      }
149     *x_ops;
150    caddr_t x_public;           /* users' data */
151    caddr_t x_private;          /* pointer to private data */
152    caddr_t x_base;             /* private used for position info */
153    int x_handy;                /* extra private word */
154  };
155
156/*
157 * A xdrproc_t exists for each data type which is to be encoded or decoded.
158 *
159 * The second argument to the xdrproc_t is a pointer to an opaque pointer.
160 * The opaque pointer generally points to a structure of the data type
161 * to be decoded.  If this pointer is 0, then the type routines should
162 * allocate dynamic storage of the appropriate size and return it.
163 * bool_t       (*xdrproc_t)(XDR *, caddr_t *);
164 */
165typedef bool_t (*xdrproc_t) (XDR *, void *,...);
166
167
168/*
169 * Operations defined on a XDR handle
170 *
171 * XDR          *xdrs;
172 * int32_t      *int32p;
173 * long         *longp;
174 * caddr_t       addr;
175 * u_int         len;
176 * u_int         pos;
177 */
178#define XDR_GETINT32(xdrs, int32p)                      \
179        (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
180#define xdr_getint32(xdrs, int32p)                      \
181        (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
182
183#define XDR_PUTINT32(xdrs, int32p)                      \
184        (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
185#define xdr_putint32(xdrs, int32p)                      \
186        (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
187
188#define XDR_GETLONG(xdrs, longp)                        \
189        (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
190#define xdr_getlong(xdrs, longp)                        \
191        (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
192
193#define XDR_PUTLONG(xdrs, longp)                        \
194        (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
195#define xdr_putlong(xdrs, longp)                        \
196        (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
197
198#define XDR_GETBYTES(xdrs, addr, len)                   \
199        (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
200#define xdr_getbytes(xdrs, addr, len)                   \
201        (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
202
203#define XDR_PUTBYTES(xdrs, addr, len)                   \
204        (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
205#define xdr_putbytes(xdrs, addr, len)                   \
206        (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
207
208#define XDR_GETPOS(xdrs)                                \
209        (*(xdrs)->x_ops->x_getpostn)(xdrs)
210#define xdr_getpos(xdrs)                                \
211        (*(xdrs)->x_ops->x_getpostn)(xdrs)
212
213#define XDR_SETPOS(xdrs, pos)                           \
214        (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
215#define xdr_setpos(xdrs, pos)                           \
216        (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
217
218#define XDR_INLINE(xdrs, len)                           \
219        (*(xdrs)->x_ops->x_inline)(xdrs, len)
220#define xdr_inline(xdrs, len)                           \
221        (*(xdrs)->x_ops->x_inline)(xdrs, len)
222
223#define XDR_DESTROY(xdrs)                                       \
224        do {                                                    \
225                if ((xdrs)->x_ops->x_destroy)                   \
226                        (*(xdrs)->x_ops->x_destroy)(xdrs);      \
227        } while (0)
228#define xdr_destroy(xdrs)                                       \
229        do {                                                    \
230                if ((xdrs)->x_ops->x_destroy)                   \
231                        (*(xdrs)->x_ops->x_destroy)(xdrs);      \
232        } while (0)
233
234/*
235 * Support struct for discriminated unions.
236 * You create an array of xdrdiscrim structures, terminated with
237 * a entry with a null procedure pointer.  The xdr_union routine gets
238 * the discriminant value and then searches the array of structures
239 * for a matching value.  If a match is found the associated xdr routine
240 * is called to handle that part of the union.  If there is
241 * no match, then a default routine may be called.
242 * If there is no match and no default routine it is an error.
243 */
244#define NULL_xdrproc_t ((xdrproc_t)0)
245struct xdr_discrim
246{
247  int value;
248  xdrproc_t proc;
249};
250
251/*
252 * Inline routines for fast encode/decode of primitive data types.
253 * Caveat emptor: these use single memory cycles to get the
254 * data from the underlying buffer, and will fail to operate
255 * properly if the data is not aligned.  The standard way to use these
256 * is to say:
257 *      if ((buf = XDR_INLINE(xdrs, count)) == NULL)
258 *              return (FALSE);
259 *      <<< macro calls >>>
260 * where ``count'' is the number of bytes of data occupied
261 * by the primitive data types.
262 *
263 * N.B. and frozen for all time: each data type here uses 4 bytes
264 * of external representation.
265 */
266
267#define IXDR_GET_INT32(buf)           ((int32_t)ntohl((uint32_t)*(buf)++))
268#define IXDR_PUT_INT32(buf, v)        (*(buf)++ = (int32_t)htonl((uint32_t)(v)))
269#define IXDR_GET_U_INT32(buf)         ((uint32_t)IXDR_GET_INT32(buf))
270#define IXDR_PUT_U_INT32(buf, v)      IXDR_PUT_INT32(buf, (int32_t)(v))
271
272/* WARNING: The IXDR_*_LONG defines are removed by Sun for new platforms
273 * and shouldn't be used any longer. Code which use this defines or longs
274 * in the RPC code will not work on 64bit Solaris platforms !
275 */
276/* #define IXDR_GET_LONG(buf) \ */
277/*      ((long)ntohl((u_long)*(*(u_int32_t**)&(buf))++)) */
278/* #define IXDR_PUT_LONG(buf, v) \ */
279/*      (*(*(u_int32_t**)&(buf))++ = (long)htonl((u_long)(v))) */
280
281/* WARNING: These macros are not safe against side effects for the 'buf'
282 * argument.  But the old versions they're replacing took the address of
283 * 'buf' and were probably not safe in that situation either. */
284#define IXDR_GET_LONG(buf) \
285        ((long) ntohl((u_long) (((u_int32_t *)(buf = (void *)(((char *) buf) + sizeof(u_int32_t))))[-1]) ))
286#define IXDR_PUT_LONG(buf, v) \
287        (((u_int32_t *)(buf = (void *)(((char *) buf) + sizeof(u_int32_t))))[-1]) = (long)htonl((u_long)(v))
288
289
290#define IXDR_GET_U_LONG(buf)          ((u_long)IXDR_GET_LONG(buf))
291#define IXDR_PUT_U_LONG(buf, v)       IXDR_PUT_LONG(buf, (long)(v))
292
293
294#define IXDR_GET_BOOL(buf)            ((bool_t)IXDR_GET_LONG(buf))
295#define IXDR_GET_ENUM(buf, t)         ((t)IXDR_GET_LONG(buf))
296#define IXDR_GET_SHORT(buf)           ((short)IXDR_GET_LONG(buf))
297#define IXDR_GET_U_SHORT(buf)         ((u_short)IXDR_GET_LONG(buf))
298
299#define IXDR_PUT_BOOL(buf, v)         IXDR_PUT_LONG(buf, (long)(v))
300#define IXDR_PUT_ENUM(buf, v)         IXDR_PUT_LONG(buf, (long)(v))
301#define IXDR_PUT_SHORT(buf, v)        IXDR_PUT_LONG(buf, (long)(v))
302#define IXDR_PUT_U_SHORT(buf, v)      IXDR_PUT_LONG(buf, (long)(v))
303
304/*
305 * These are the "generic" xdr routines.
306 * None of these can have const applied because it's not possible to
307 * know whether the call is a read or a write to the passed parameter
308 * also, the XDR structure is always updated by some of these calls.
309 */
310extern bool_t xdr_void (void) __THROW;
311extern bool_t xdr_short (XDR *__xdrs, short *__sp) __THROW;
312extern bool_t xdr_u_short (XDR *__xdrs, u_short *__usp) __THROW;
313extern bool_t xdr_int (XDR *__xdrs, int *__ip) __THROW;
314extern bool_t xdr_u_int (XDR *__xdrs, u_int *__up) __THROW;
315extern bool_t xdr_long (XDR *__xdrs, long *__lp) __THROW;
316extern bool_t xdr_u_long (XDR *__xdrs, u_long *__ulp) __THROW;
317extern bool_t xdr_hyper (XDR *__xdrs, quad_t *__llp) __THROW;
318extern bool_t xdr_u_hyper (XDR *__xdrs, u_quad_t *__ullp) __THROW;
319extern bool_t xdr_longlong_t (XDR *__xdrs, quad_t *__llp) __THROW;
320extern bool_t xdr_u_longlong_t (XDR *__xdrs, u_quad_t *__ullp) __THROW;
321extern bool_t xdr_int8_t (XDR *__xdrs, int8_t *__ip) __THROW;
322extern bool_t xdr_uint8_t (XDR *__xdrs, uint8_t *__up) __THROW;
323extern bool_t xdr_int16_t (XDR *__xdrs, int16_t *__ip) __THROW;
324extern bool_t xdr_uint16_t (XDR *__xdrs, uint16_t *__up) __THROW;
325extern bool_t xdr_int32_t (XDR *__xdrs, int32_t *__ip) __THROW;
326extern bool_t xdr_uint32_t (XDR *__xdrs, uint32_t *__up) __THROW;
327extern bool_t xdr_int64_t (XDR *__xdrs, int64_t *__ip) __THROW;
328extern bool_t xdr_uint64_t (XDR *__xdrs, uint64_t *__up) __THROW;
329extern bool_t xdr_bool (XDR *__xdrs, bool_t *__bp) __THROW;
330extern bool_t xdr_enum (XDR *__xdrs, enum_t *__ep) __THROW;
331extern bool_t xdr_array (XDR * _xdrs, caddr_t *__addrp, u_int *__sizep,
332                         u_int __maxsize, u_int __elsize, xdrproc_t __elproc)
333     __THROW;
334extern bool_t xdr_bytes (XDR *__xdrs, char **__cpp, u_int *__sizep,
335                         u_int __maxsize) __THROW;
336extern bool_t xdr_opaque (XDR *__xdrs, caddr_t __cp, u_int __cnt) __THROW;
337extern bool_t xdr_string (XDR *__xdrs, char **__cpp, u_int __maxsize) __THROW;
338extern bool_t xdr_union (XDR *__xdrs, enum_t *__dscmp, char *__unp,
339                         __const struct xdr_discrim *__choices,
340                         xdrproc_t dfault) __THROW;
341extern bool_t xdr_char (XDR *__xdrs, char *__cp) __THROW;
342extern bool_t xdr_u_char (XDR *__xdrs, u_char *__cp) __THROW;
343extern bool_t xdr_vector (XDR *__xdrs, char *__basep, u_int __nelem,
344                          u_int __elemsize, xdrproc_t __xdr_elem) __THROW;
345extern bool_t xdr_float (XDR *__xdrs, float *__fp) __THROW;
346extern bool_t xdr_double (XDR *__xdrs, double *__dp) __THROW;
347extern bool_t xdr_reference (XDR *__xdrs, caddr_t *__xpp, u_int __size,
348                             xdrproc_t __proc) __THROW;
349extern bool_t xdr_pointer (XDR *__xdrs, char **__objpp,
350                           u_int __obj_size, xdrproc_t __xdr_obj) __THROW;
351extern bool_t xdr_wrapstring (XDR *__xdrs, char **__cpp) __THROW;
352extern u_long xdr_sizeof (xdrproc_t, void *) __THROW;
353
354/*
355 * Common opaque bytes objects used by many rpc protocols;
356 * declared here due to commonality.
357 */
358#define MAX_NETOBJ_SZ 1024
359struct netobj
360{
361  u_int n_len;
362  char *n_bytes;
363};
364typedef struct netobj netobj;
365extern bool_t xdr_netobj (XDR *__xdrs, struct netobj *__np) __THROW;
366
367/*
368 * These are the public routines for the various implementations of
369 * xdr streams.
370 */
371
372/* XDR using memory buffers */
373extern void xdrmem_create (XDR *__xdrs, __const caddr_t __addr,
374                           u_int __size, enum xdr_op __xop) __THROW;
375
376/* XDR using stdio library */
377extern void xdrstdio_create (XDR *__xdrs, FILE *__file, enum xdr_op __xop)
378     __THROW;
379
380/* XDR pseudo records for tcp */
381extern void xdrrec_create (XDR *__xdrs, u_int __sendsize,
382                           u_int __recvsize, caddr_t __tcp_handle,
383                           int (*__readit) (char *, char *, int),
384                           int (*__writeit) (char *, char *, int)) __THROW;
385
386/* make end of xdr record */
387extern bool_t xdrrec_endofrecord (XDR *__xdrs, bool_t __sendnow) __THROW;
388
389/* move to beginning of next record */
390extern bool_t xdrrec_skiprecord (XDR *__xdrs) __THROW;
391
392/* true if no more input */
393extern bool_t xdrrec_eof (XDR *__xdrs) __THROW;
394
395/* free memory buffers for xdr */
396extern void xdr_free (xdrproc_t __proc, char *__objp) __THROW;
397
398__END_DECLS
399
400#endif /* rpc/xdr.h */
Note: See TracBrowser for help on using the repository browser.