Added Gnulib intprops module.
[portablexdr.git] / portablexdr-5 / rpc / xdr.h
1 /* PortableXDR - a free, portable XDR implementation.
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17  */
18
19 /* xdr_int8_t, xdr_uint8_t, xdr_int16_t, xdr_uint16_t, xdr_int32_t,
20  * xdr_uint32_t, xdr_int64_t and xdr_uint64_t functions:
21  *
22  * Copyright (c) 1998, 1999, 2000, 2004, 2005 Free Software Foundation, Inc.
23  * This file is part of the GNU C Library.
24  * Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1998.
25  *
26  * (Under the same license as above).
27  */
28
29 #ifndef PORTABLEXDR_XDR_H
30 #define PORTABLEXDR_XDR_H
31
32 #include <rpc/types.h>
33 #include <rpc/xdr_internal.h>
34 #include <stdarg.h>
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 /* Please read the documentation that comes with PortableXDR,
41  * otherwise you won't understand how to use the xdr_* conversion
42  * functions.
43  */
44 typedef bool_t (*xdrproc_t) ();
45 #define NULL_xdrproc_t NULL
46
47 /* XDR procedures for the basic types. */
48 static inline bool_t
49 xdr_void (XDR *xdrs ATTRIBUTE_UNUSED, void *vp ATTRIBUTE_UNUSED)
50 {
51   return TRUE;
52 }
53
54 static inline bool_t
55 xdr_bool (XDR *xdrs, bool_t *p)
56 {
57   int32_t i;
58   switch (xdrs->x_op) {
59   case XDR_ENCODE:
60     i = *p ? TRUE : FALSE;
61     return xdr_putlong (xdrs, &i);
62   case XDR_DECODE:
63     if (!xdr_getlong (xdrs, &i)) return FALSE;
64     *p = i ? TRUE : FALSE;
65     return TRUE;
66   case XDR_FREE:
67     return TRUE;
68   default:
69     return FALSE;
70   }
71 }
72
73 /* XDR 64bit integers */
74 bool_t
75 xdr_int64_t (XDR *xdrs, int64_t *ip)
76 {
77   int32_t t1, t2;
78
79   switch (xdrs->x_op)
80     {
81     case XDR_ENCODE:
82       t1 = (int32_t) ((*ip) >> 32);
83       t2 = (int32_t) (*ip);
84       return (XDR_PUTINT32(xdrs, &t1) && XDR_PUTINT32(xdrs, &t2));
85     case XDR_DECODE:
86       if (!XDR_GETINT32(xdrs, &t1) || !XDR_GETINT32(xdrs, &t2))
87         return FALSE;
88       *ip = ((int64_t) t1) << 32;
89       *ip |= (uint32_t) t2;     /* Avoid sign extension.  */
90       return TRUE;
91     case XDR_FREE:
92       return TRUE;
93     default:
94       return FALSE;
95     }
96 }
97
98 /* XDR 64bit unsigned integers */
99 bool_t
100 xdr_uint64_t (XDR *xdrs, uint64_t *uip)
101 {
102   uint32_t t1;
103   uint32_t t2;
104
105   switch (xdrs->x_op)
106     {
107     case XDR_ENCODE:
108       t1 = (uint32_t) ((*uip) >> 32);
109       t2 = (uint32_t) (*uip);
110       return (XDR_PUTINT32 (xdrs, (int32_t *) &t1) &&
111               XDR_PUTINT32(xdrs, (int32_t *) &t2));
112     case XDR_DECODE:
113       if (!XDR_GETINT32(xdrs, (int32_t *) &t1) ||
114           !XDR_GETINT32(xdrs, (int32_t *) &t2))
115         return FALSE;
116       *uip = ((uint64_t) t1) << 32;
117       *uip |= t2;
118       return TRUE;
119     case XDR_FREE:
120       return TRUE;
121     default:
122       return FALSE;
123     }
124 }
125
126 /* XDR 32bit integers */
127 bool_t
128 xdr_int32_t (XDR *xdrs, int32_t *lp)
129 {
130   switch (xdrs->x_op)
131     {
132     case XDR_ENCODE:
133       return XDR_PUTINT32 (xdrs, lp);
134     case XDR_DECODE:
135       return XDR_GETINT32 (xdrs, lp);
136     case XDR_FREE:
137       return TRUE;
138     default:
139       return FALSE;
140     }
141 }
142
143 /* XDR 32bit unsigned integers */
144 bool_t
145 xdr_uint32_t (XDR *xdrs, uint32_t *ulp)
146 {
147   switch (xdrs->x_op)
148     {
149     case XDR_ENCODE:
150       return XDR_PUTINT32 (xdrs, (int32_t *) ulp);
151     case XDR_DECODE:
152       return XDR_GETINT32 (xdrs, (int32_t *) ulp);
153     case XDR_FREE:
154       return TRUE;
155     default:
156       return FALSE;
157     }
158 }
159
160 /* XDR 16bit integers */
161 bool_t
162 xdr_int16_t (XDR *xdrs, int16_t *ip)
163 {
164   int32_t t;
165
166   switch (xdrs->x_op)
167     {
168     case XDR_ENCODE:
169       t = (int32_t) *ip;
170       return XDR_PUTINT32 (xdrs, &t);
171     case XDR_DECODE:
172       if (!XDR_GETINT32 (xdrs, &t))
173         return FALSE;
174       *ip = (int16_t) t;
175       return TRUE;
176     case XDR_FREE:
177       return TRUE;
178     default:
179       return FALSE;
180     }
181 }
182
183 /* XDR 16bit unsigned integers */
184 bool_t
185 xdr_uint16_t (XDR *xdrs, uint16_t *uip)
186 {
187   uint32_t ut;
188
189   switch (xdrs->x_op)
190     {
191     case XDR_ENCODE:
192       ut = (uint32_t) *uip;
193       return XDR_PUTINT32 (xdrs, (int32_t *) &ut);
194     case XDR_DECODE:
195       if (!XDR_GETINT32 (xdrs, (int32_t *) &ut))
196         return FALSE;
197       *uip = (uint16_t) ut;
198       return TRUE;
199     case XDR_FREE:
200       return TRUE;
201     default:
202       return FALSE;
203     }
204 }
205
206 /* XDR 8bit integers */
207 bool_t
208 xdr_int8_t (XDR *xdrs, int8_t *ip)
209 {
210   int32_t t;
211
212   switch (xdrs->x_op)
213     {
214     case XDR_ENCODE:
215       t = (int32_t) *ip;
216       return XDR_PUTINT32 (xdrs, &t);
217     case XDR_DECODE:
218       if (!XDR_GETINT32 (xdrs, &t))
219         return FALSE;
220       *ip = (int8_t) t;
221       return TRUE;
222     case XDR_FREE:
223       return TRUE;
224     default:
225       return FALSE;
226     }
227 }
228
229 /* XDR 8bit unsigned integers */
230 bool_t
231 xdr_uint8_t (XDR *xdrs, uint8_t *uip)
232 {
233   uint32_t ut;
234
235   switch (xdrs->x_op)
236     {
237     case XDR_ENCODE:
238       ut = (uint32_t) *uip;
239       return XDR_PUTINT32 (xdrs, (int32_t *) &ut);
240     case XDR_DECODE:
241       if (!XDR_GETINT32 (xdrs, (int32_t *) &ut))
242         return FALSE;
243       *uip = (uint8_t) ut;
244       return TRUE;
245     case XDR_FREE:
246       return TRUE;
247     default:
248       return FALSE;
249     }
250 }
251
252 #if 0
253 static inline bool_t
254 xdr_float (XDR *xdrs, float *p)
255 {
256   XXX;
257 }
258
259 static inline bool_t
260 xdr_double (XDR *xdrs, double *p)
261 {
262   XXX;
263 }
264 #endif
265
266 /* Some very common aliases for the basic integer functions. */
267 #define xdr_int xdr_int32_t
268 #define xdr_u_int xdr_uint32_t
269 #define xdr_long xdr_int32_t
270 #define xdr_u_long xdr_uint32_t
271
272 #define xdr_short xdr_int16_t
273 #define xdr_u_short xdr_uint16_t
274
275   /* NB: In PortableXDR, char is ALWAYS treated as signed octet. */
276 #define xdr_char xdr_int8_t
277 #define xdr_u_char xdr_uint8_t
278
279 /*
280 #define xdr_hyper xdr_int64_t
281 #define xdr_u_hyper xdr_uint64_t
282 #define xdr_quad xdr_int64_t
283 #define xdr_u_quad xdr_uint64_t
284 */
285
286 /* Enumerations. */
287 static inline bool_t
288 xdr_enum (XDR *xdrs, enum_t *ep)
289 {
290   return xdr_int32_t (xdrs, ep);
291 }
292
293 /* Union with discriminator. */
294 extern bool_t xdr_union (XDR *xdrs, enum_t *discrim, void *p, struct xdr_discrim *choices, xdrproc_t default_proc);
295
296 /* Variable-size array of arbitrary elements. */
297 extern bool_t xdr_array (XDR *xdrs, void *p, size_t num_elements, size_t max_elements, size_t element_size, xdrproc_t element_proc);
298
299 /* Fixed-size array of arbitrary elements. */
300 extern bool_t xdr_vector (XDR *xdrs, void *p, size_t num_elements, size_t element_size, xdrproc_t element_size);
301
302 /* Variable-size array of bytes. */
303 extern bool_t xdr_bytes (XDR *xdrs, char **bytes, size_t num_bytes, size_t max_bytes);
304
305 /* Fixed-size array of bytes. */
306 extern bool_t xdr_opaque (XDR *xdrs, void *p, size_t num_bytes);
307
308 /* Variable-size string (handled as normal C string in the program, but
309  * serialized as length + bytes).
310  */
311 extern bool_t xdr_string (XDR *xdrs, char **str, size_t max_bytes);
312
313 #define xdr_wrapstring(xdrs,str) (xdr_string ((xdrs),(str),~0))
314
315 #if 0
316 /* A reference is a pointer which cannot be NULL. */
317 static inline bool_t
318 xdr_reference (XDR *xdrs, void **p, size_t size, xdrproc_t proc)
319 {
320   XXX
321 }
322
323 /* A pointer is a pointer to an object that can be NULL.  It is
324  * serialized as a boolean (TRUE = not null, FALSE = null), followed
325  * by the object.
326  */
327 static inline bool_t
328 xdr_pointer (XDR *xdrs, void **p, size_t size, xdrproc_t proc)
329 {
330   XXX
331 }
332 #endif
333
334 /* Free an XDR object (recursively). */
335 extern void xdr_free (xdrproc_t, void *);
336
337 /* Construct an XDR stream from an in-memory buffer. */
338 extern void xdrmem_create (XDR *xdrs, void *p, size_t size, enum xdr_op);
339
340 /* Construct an XDR stream from a FILE* or file descriptor.  Note
341  * that neither of these will close the underlying file.
342  */
343 extern void xdrstdio_create (XDR *xdrs, FILE *, enum xdr_op);
344 extern void xdrfd_create (XDR *xdrs, int fd, enum xdr_op);
345
346 /* PortableXDR variations which allow you to close the underlying
347  * file after use.
348  */
349 #define XDR_CLOSE_FILE 1
350
351 extern void xdrstdio_create2 (XDR *xdrs, FILE *, enum xdr_op, uint32_t flags);
352 extern void xdrfd_create2 (XDR *xdrs, int fd, enum xdr_op, uint32_t flags);
353
354 /*
355   Does anyone ever use these?  Contributions welcome.
356 extern void xdrrec_create (...);
357 extern bool_t xdrrec_endofrecord (...);
358 extern bool_t xdrrec_skiprecord (...);
359 extern bool_t xdrrec_eof (...);
360 */
361
362 #ifdef __cplusplus
363 }
364 #endif
365
366 #endif /* PORTABLEXDR_XDR_H */