master
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 * from: @(#)xdr.h 1.19 87/04/22 SMI
33 * from: @(#)xdr.h 2.2 88/07/29 4.0 RPCSRC
34 */
35
36/*
37 * xdr.h, External Data Representation Serialization Routines.
38 *
39 * Copyright (C) 1984, Sun Microsystems, Inc.
40 */
41
42#ifndef _RPC_XDR_H
43#define _RPC_XDR_H
44#include <sys/cdefs.h>
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 */
81enum 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#define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
92 * BYTES_PER_XDR_UNIT)
93
94/*
95 * The XDR handle.
96 * Contains operation which is being applied to the stream,
97 * an operations vector for the particular implementation (e.g. see xdr_mem.c),
98 * and two private fields for the use of the particular implementation.
99 */
100typedef struct XDR {
101 enum xdr_op x_op; /* operation; fast additional param */
102 const struct xdr_ops {
103 /* get a long from underlying stream */
104 bool_t (*x_getlong)(struct XDR *, long *);
105 /* put a long to " */
106 bool_t (*x_putlong)(struct XDR *, const long *);
107 /* get some bytes from " */
108 bool_t (*x_getbytes)(struct XDR *, char *, u_int);
109 /* put some bytes to " */
110 bool_t (*x_putbytes)(struct XDR *, const char *, u_int);
111 /* returns bytes off from beginning */
112 u_int (*x_getpostn)(struct XDR *);
113 /* lets you reposition the stream */
114 bool_t (*x_setpostn)(struct XDR *, u_int);
115 /* buf quick ptr to buffered data */
116 int32_t *(*x_inline)(struct XDR *, u_int);
117 /* free privates of this xdr_stream */
118 void (*x_destroy)(struct XDR *);
119 bool_t (*x_control)(struct XDR *, int, void *);
120 } *x_ops;
121 char * x_public; /* users' data */
122 void * x_private; /* pointer to private data */
123 char * x_base; /* private used for position info */
124 u_int x_handy; /* extra private word */
125} XDR;
126
127/*
128 * A xdrproc_t exists for each data type which is to be encoded or decoded.
129 *
130 * The second argument to the xdrproc_t is a pointer to an opaque pointer.
131 * The opaque pointer generally points to a structure of the data type
132 * to be decoded. If this pointer is 0, then the type routines should
133 * allocate dynamic storage of the appropriate size and return it.
134 */
135#ifdef _KERNEL
136typedef bool_t (*xdrproc_t)(XDR *, void *, u_int);
137#else
138/*
139 * XXX can't actually prototype it, because some take three args!!!
140 */
141typedef bool_t (*xdrproc_t)(XDR *, ...);
142#endif
143
144/*
145 * Operations defined on a XDR handle
146 *
147 * XDR *xdrs;
148 * long *longp;
149 * char * addr;
150 * u_int len;
151 * u_int pos;
152 */
153#define XDR_GETLONG(xdrs, longp) \
154 (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
155#define xdr_getlong(xdrs, longp) \
156 (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
157
158#define XDR_PUTLONG(xdrs, longp) \
159 (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
160#define xdr_putlong(xdrs, longp) \
161 (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
162
163static __inline int
164xdr_getint32(XDR *xdrs, int32_t *ip)
165{
166 long l;
167
168 if (!xdr_getlong(xdrs, &l))
169 return (FALSE);
170 *ip = (int32_t)l;
171 return (TRUE);
172}
173
174static __inline int
175xdr_putint32(XDR *xdrs, int32_t *ip)
176{
177 long l;
178
179 l = (long)*ip;
180 return xdr_putlong(xdrs, &l);
181}
182
183#define XDR_GETINT32(xdrs, int32p) xdr_getint32(xdrs, int32p)
184#define XDR_PUTINT32(xdrs, int32p) xdr_putint32(xdrs, int32p)
185
186#define XDR_GETBYTES(xdrs, addr, len) \
187 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
188#define xdr_getbytes(xdrs, addr, len) \
189 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
190
191#define XDR_PUTBYTES(xdrs, addr, len) \
192 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
193#define xdr_putbytes(xdrs, addr, len) \
194 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
195
196#define XDR_GETPOS(xdrs) \
197 (*(xdrs)->x_ops->x_getpostn)(xdrs)
198#define xdr_getpos(xdrs) \
199 (*(xdrs)->x_ops->x_getpostn)(xdrs)
200
201#define XDR_SETPOS(xdrs, pos) \
202 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
203#define xdr_setpos(xdrs, pos) \
204 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
205
206#define XDR_INLINE(xdrs, len) \
207 (*(xdrs)->x_ops->x_inline)(xdrs, len)
208#define xdr_inline(xdrs, len) \
209 (*(xdrs)->x_ops->x_inline)(xdrs, len)
210
211#define XDR_DESTROY(xdrs) \
212 if ((xdrs)->x_ops->x_destroy) \
213 (*(xdrs)->x_ops->x_destroy)(xdrs)
214#define xdr_destroy(xdrs) \
215 if ((xdrs)->x_ops->x_destroy) \
216 (*(xdrs)->x_ops->x_destroy)(xdrs)
217
218#define XDR_CONTROL(xdrs, req, op) \
219 if ((xdrs)->x_ops->x_control) \
220 (*(xdrs)->x_ops->x_control)(xdrs, req, op)
221#define xdr_control(xdrs, req, op) XDR_CONTROL(xdrs, req, op)
222
223#define xdr_rpcvers(xdrs, versp) xdr_u_int32_t(xdrs, versp)
224#define xdr_rpcprog(xdrs, progp) xdr_u_int32_t(xdrs, progp)
225#define xdr_rpcproc(xdrs, procp) xdr_u_int32_t(xdrs, procp)
226#define xdr_rpcprot(xdrs, protp) xdr_u_int32_t(xdrs, protp)
227#define xdr_rpcport(xdrs, portp) xdr_u_int32_t(xdrs, portp)
228
229/*
230 * Support struct for discriminated unions.
231 * You create an array of xdrdiscrim structures, terminated with
232 * an entry with a null procedure pointer. The xdr_union routine gets
233 * the discriminant value and then searches the array of structures
234 * for a matching value. If a match is found the associated xdr routine
235 * is called to handle that part of the union. If there is
236 * no match, then a default routine may be called.
237 * If there is no match and no default routine it is an error.
238 */
239#define NULL_xdrproc_t ((xdrproc_t)0)
240struct xdr_discrim {
241 int value;
242 xdrproc_t proc;
243};
244
245/*
246 * In-line routines for fast encode/decode of primitive data types.
247 * Caveat emptor: these use single memory cycles to get the
248 * data from the underlying buffer, and will fail to operate
249 * properly if the data is not aligned. The standard way to use these
250 * is to say:
251 * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
252 * return (FALSE);
253 * <<< macro calls >>>
254 * where ``count'' is the number of bytes of data occupied
255 * by the primitive data types.
256 *
257 * N.B. and frozen for all time: each data type here uses 4 bytes
258 * of external representation.
259 */
260#define IXDR_GET_INT32(buf) ((int32_t)__ntohl((u_int32_t)*(buf)++))
261#define IXDR_PUT_INT32(buf, v) (*(buf)++ =(int32_t)__htonl((u_int32_t)v))
262#define IXDR_GET_U_INT32(buf) ((u_int32_t)IXDR_GET_INT32(buf))
263#define IXDR_PUT_U_INT32(buf, v) IXDR_PUT_INT32((buf), ((int32_t)(v)))
264
265#define IXDR_GET_LONG(buf) ((long)__ntohl((u_int32_t)*(buf)++))
266#define IXDR_PUT_LONG(buf, v) (*(buf)++ =(int32_t)__htonl((u_int32_t)v))
267
268#define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
269#define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
270#define IXDR_GET_U_LONG(buf) ((u_long)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), (v))
275#define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG((buf), (v))
276#define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG((buf), (v))
277#define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG((buf), (v))
278#define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG((buf), (v))
279
280/*
281 * These are the "generic" xdr routines.
282 */
283__BEGIN_DECLS
284extern bool_t xdr_void(XDR *, void *);
285extern bool_t xdr_int(XDR *, int *);
286extern bool_t xdr_u_int(XDR *, u_int *);
287extern bool_t xdr_long(XDR *, long *);
288extern bool_t xdr_u_long(XDR *, u_long *);
289extern bool_t xdr_short(XDR *, short *);
290extern bool_t xdr_u_short(XDR *, u_short *);
291extern bool_t xdr_int16_t(XDR *, int16_t *);
292extern bool_t xdr_u_int16_t(XDR *, u_int16_t *);
293extern bool_t xdr_uint16_t(XDR *, u_int16_t *);
294extern bool_t xdr_int32_t(XDR *, int32_t *);
295extern bool_t xdr_u_int32_t(XDR *, u_int32_t *);
296extern bool_t xdr_uint32_t(XDR *, u_int32_t *);
297extern bool_t xdr_int64_t(XDR *, int64_t *);
298extern bool_t xdr_u_int64_t(XDR *, u_int64_t *);
299extern bool_t xdr_uint64_t(XDR *, u_int64_t *);
300extern bool_t xdr_bool(XDR *, bool_t *);
301extern bool_t xdr_enum(XDR *, enum_t *);
302extern bool_t xdr_array(XDR *, char **, u_int *, u_int, u_int, xdrproc_t);
303extern bool_t xdr_bytes(XDR *, char **, u_int *, u_int);
304extern bool_t xdr_opaque(XDR *, char *, u_int);
305extern bool_t xdr_string(XDR *, char **, u_int);
306extern bool_t xdr_union(XDR *, enum_t *, char *, const struct xdr_discrim *, xdrproc_t);
307extern bool_t xdr_char(XDR *, char *);
308extern bool_t xdr_u_char(XDR *, u_char *);
309extern bool_t xdr_vector(XDR *, char *, u_int, u_int, xdrproc_t);
310extern bool_t xdr_float(XDR *, float *);
311extern bool_t xdr_double(XDR *, double *);
312extern bool_t xdr_quadruple(XDR *, long double *);
313extern bool_t xdr_reference(XDR *, char **, u_int, xdrproc_t);
314extern bool_t xdr_pointer(XDR *, char **, u_int, xdrproc_t);
315extern bool_t xdr_wrapstring(XDR *, char **);
316extern void xdr_free(xdrproc_t, void *);
317extern bool_t xdr_hyper(XDR *, quad_t *);
318extern bool_t xdr_u_hyper(XDR *, u_quad_t *);
319extern bool_t xdr_longlong_t(XDR *, quad_t *);
320extern bool_t xdr_u_longlong_t(XDR *, u_quad_t *);
321extern unsigned long xdr_sizeof(xdrproc_t, void *);
322__END_DECLS
323
324/*
325 * Common opaque bytes objects used by many rpc protocols;
326 * declared here due to commonality.
327 */
328#define MAX_NETOBJ_SZ 1024
329struct netobj {
330 u_int n_len;
331 char *n_bytes;
332};
333typedef struct netobj netobj;
334extern bool_t xdr_netobj(XDR *, struct netobj *);
335
336/*
337 * These are the public routines for the various implementations of
338 * xdr streams.
339 */
340__BEGIN_DECLS
341/* XDR using memory buffers */
342extern void xdrmem_create(XDR *, char *, u_int, enum xdr_op);
343
344/* XDR using stdio library */
345#ifdef _STDIO_H_
346extern void xdrstdio_create(XDR *, FILE *, enum xdr_op);
347#endif
348
349/* XDR pseudo records for tcp */
350extern void xdrrec_create(XDR *, u_int, u_int, void *,
351 int (*)(void *, void *, int),
352 int (*)(void *, void *, int));
353
354/* make end of xdr record */
355extern bool_t xdrrec_endofrecord(XDR *, int);
356
357/* move to beginning of next record */
358extern bool_t xdrrec_skiprecord(XDR *);
359
360/* true if no more input */
361extern bool_t xdrrec_eof(XDR *);
362extern u_int xdrrec_readbytes(XDR *, caddr_t, u_int);
363__END_DECLS
364
365#endif /* !_RPC_XDR_H */