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