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