1 /* @(#)xdr.h	2.2 88/07/29 4.0 RPCSRC */
2 /*
3  * Copyright (c) 2010, Oracle America, Inc.
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *
18  *     * Neither the name of the "Oracle America, Inc." nor the names of
19  *       its contributors may be used to endorse or promote products
20  *       derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 /*      @(#)xdr.h 1.19 87/04/22 SMI      */
35 
36 /*
37  * xdr.h, External Data Representation Serialization Routines.
38  */
39 
40 #ifndef GSSRPC_XDR_H
41 #define GSSRPC_XDR_H
42 
43 #include <stdio.h>		/* for FILE */
44 
45 GSSRPC__BEGIN_DECLS
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  */
81 enum 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  * A xdrproc_t exists for each data type which is to be encoded or decoded.
96  *
97  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
98  * The opaque pointer generally points to a structure of the data type
99  * to be decoded.  If this pointer is 0, then the type routines should
100  * allocate dynamic storage of the appropriate size and return it.
101  * bool_t	(*xdrproc_t)(XDR *, caddr_t *);
102  *
103  * XXX can't actually prototype it, because some take three args!!!
104  */
105 typedef	bool_t (*xdrproc_t)();
106 
107 /*
108  * The XDR handle.
109  * Contains operation which is being applied to the stream,
110  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
111  * and two private fields for the use of the particular impelementation.
112  */
113 typedef struct XDR {
114 	enum xdr_op	x_op;		/* operation; fast additional param */
115 	struct xdr_ops {
116 		/* get a long from underlying stream */
117 		bool_t	(*x_getlong)(struct XDR *, long *);
118 
119 		/* put a long to underlying stream */
120 		bool_t	(*x_putlong)(struct XDR *, long *);
121 
122 		/* get some bytes from underlying stream */
123 		bool_t	(*x_getbytes)(struct XDR *, caddr_t, u_int);
124 
125 		/* put some bytes to underlying stream */
126 		bool_t	(*x_putbytes)(struct XDR *, caddr_t, u_int);
127 
128 		/* returns bytes off from beginning */
129 		u_int	(*x_getpostn)(struct XDR *);
130 
131 		/* lets you reposition the stream */
132 		bool_t	(*x_setpostn)(struct XDR *, u_int);
133 
134 		/* buf quick ptr to buffered data */
135 		rpc_inline_t *(*x_inline)(struct XDR *, int);
136 
137 		/* free privates of this xdr_stream */
138 		void	(*x_destroy)(struct XDR *);
139 	} *x_ops;
140 	caddr_t 	x_public;	/* users' data */
141 	void *		x_private;	/* pointer to private data */
142 	caddr_t 	x_base;		/* private used for position info */
143 	int		x_handy;	/* extra private word */
144 } XDR;
145 
146 /*
147  * Operations defined on a XDR handle
148  *
149  * XDR		*xdrs;
150  * int32_t	*longp;
151  * caddr_t	 addr;
152  * u_int	 len;
153  * u_int	 pos;
154  */
155 #define XDR_GETLONG(xdrs, longp)			\
156 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
157 #define xdr_getlong(xdrs, longp)			\
158 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
159 
160 #define XDR_PUTLONG(xdrs, longp)			\
161 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
162 #define xdr_putlong(xdrs, longp)			\
163 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
164 
165 #define XDR_GETBYTES(xdrs, addr, len)			\
166 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
167 #define xdr_getbytes(xdrs, addr, len)			\
168 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
169 
170 #define XDR_PUTBYTES(xdrs, addr, len)			\
171 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
172 #define xdr_putbytes(xdrs, addr, len)			\
173 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
174 
175 #define XDR_GETPOS(xdrs)				\
176 	(*(xdrs)->x_ops->x_getpostn)(xdrs)
177 #define xdr_getpos(xdrs)				\
178 	(*(xdrs)->x_ops->x_getpostn)(xdrs)
179 
180 #define XDR_SETPOS(xdrs, pos)				\
181 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
182 #define xdr_setpos(xdrs, pos)				\
183 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
184 
185 #define	XDR_INLINE(xdrs, len)				\
186 	(*(xdrs)->x_ops->x_inline)(xdrs, len)
187 #define	xdr_inline(xdrs, len)				\
188 	(*(xdrs)->x_ops->x_inline)(xdrs, len)
189 
190 #define	XDR_DESTROY(xdrs)				\
191 	if ((xdrs)->x_ops->x_destroy) 			\
192 		(*(xdrs)->x_ops->x_destroy)(xdrs)
193 #define	xdr_destroy(xdrs)				\
194 	if ((xdrs)->x_ops->x_destroy) 			\
195 		(*(xdrs)->x_ops->x_destroy)(xdrs)
196 
197 /*
198  * Support struct for discriminated unions.
199  * You create an array of xdrdiscrim structures, terminated with
200  * a entry with a null procedure pointer.  The xdr_union routine gets
201  * the discriminant value and then searches the array of structures
202  * for a matching value.  If a match is found the associated xdr routine
203  * is called to handle that part of the union.  If there is
204  * no match, then a default routine may be called.
205  * If there is no match and no default routine it is an error.
206  */
207 #define NULL_xdrproc_t ((xdrproc_t)0)
208 struct xdr_discrim {
209 	int	value;
210 	xdrproc_t proc;
211 };
212 
213 /*
214  * In-line routines for fast encode/decode of primitve data types.
215  * Caveat emptor: these use single memory cycles to get the
216  * data from the underlying buffer, and will fail to operate
217  * properly if the data is not aligned.  The standard way to use these
218  * is to say:
219  *	if ((buf = XDR_INLINE(xdrs, count)) == NULL)
220  *		return (FALSE);
221  *	<<< macro calls >>>
222  * where ``count'' is the number of bytes of data occupied
223  * by the primitive data types.
224  *
225  * N.B. and frozen for all time: each data type here uses 4 bytes
226  * of external representation.
227  */
228 #define IXDR_GET_INT32(buf)		((int32_t)IXDR_GET_U_INT32(buf))
229 #define IXDR_PUT_INT32(buf, v)		IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
230 #define IXDR_GET_U_INT32(buf)		(ntohl((uint32_t)*(buf)++))
231 #define IXDR_PUT_U_INT32(buf, v)	(*(buf)++ = (int32_t)htonl((v)))
232 
233 #define IXDR_GET_LONG(buf)		((long)IXDR_GET_INT32(buf))
234 #define IXDR_PUT_LONG(buf, v)		IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
235 
236 #define IXDR_GET_BOOL(buf)		((bool_t)IXDR_GET_LONG(buf))
237 #define IXDR_GET_ENUM(buf, t)		((t)IXDR_GET_INT32(buf))
238 #define IXDR_GET_U_LONG(buf)		((u_long)IXDR_GET_U_INT32(buf))
239 #define IXDR_GET_SHORT(buf)		((short)IXDR_GET_INT32(buf))
240 #define IXDR_GET_U_SHORT(buf)		((u_short)IXDR_GET_U_INT32(buf))
241 
242 #define IXDR_PUT_BOOL(buf, v)		IXDR_PUT_INT32((buf),((int32_t)(v)))
243 #define IXDR_PUT_ENUM(buf, v)		IXDR_PUT_INT32((buf),((int32_t)(v)))
244 #define IXDR_PUT_U_LONG(buf, v)		IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
245 #define IXDR_PUT_SHORT(buf, v)		IXDR_PUT_INT32((buf),((int32_t)(v)))
246 #define IXDR_PUT_U_SHORT(buf, v)	IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
247 
248 /*
249  * These are the "generic" xdr routines.
250  */
251 extern bool_t	xdr_void(XDR *, void *);
252 extern bool_t	xdr_int(XDR *, int *);
253 extern bool_t	xdr_u_int(XDR *, u_int *);
254 extern bool_t	xdr_long(XDR *, long *);
255 extern bool_t	xdr_u_long(XDR *, u_long *);
256 extern bool_t	xdr_short(XDR *, short *);
257 extern bool_t	xdr_u_short(XDR *, u_short *);
258 extern bool_t	xdr_bool(XDR *, bool_t *);
259 extern bool_t	xdr_enum(XDR *, enum_t *);
260 extern bool_t	xdr_array(XDR *, caddr_t *, u_int *,
261 			u_int, u_int, xdrproc_t);
262 extern bool_t	xdr_bytes(XDR *, char **, u_int *, u_int);
263 extern bool_t	xdr_opaque(XDR *, caddr_t, u_int);
264 extern bool_t	xdr_string(XDR *, char **, u_int);
265 extern bool_t	xdr_union(XDR *, enum_t *, char *, struct xdr_discrim *,
266 			xdrproc_t);
267 extern bool_t	xdr_char(XDR *, char *);
268 extern bool_t	xdr_u_char(XDR *, u_char *);
269 extern bool_t	xdr_vector(XDR *, char *, u_int, u_int, xdrproc_t);
270 extern bool_t	xdr_float(XDR *, float *);
271 extern bool_t	xdr_double(XDR *, double *);
272 extern bool_t	xdr_reference(XDR *, caddr_t *, u_int, xdrproc_t);
273 extern bool_t	xdr_pointer(XDR *, char **, u_int, xdrproc_t);
274 extern bool_t	xdr_wrapstring(XDR *, char **);
275 
276 extern unsigned long xdr_sizeof(xdrproc_t, void *);
277 
278 #define xdr_rpcprog	xdr_u_int32
279 #define xdr_rpcvers	xdr_u_int32
280 #define xdr_rpcprot	xdr_u_int32
281 #define xdr_rpcproc	xdr_u_int32
282 #define xdr_rpcport	xdr_u_int32
283 
284 /*
285  * Common opaque bytes objects used by many rpc protocols;
286  * declared here due to commonality.
287  */
288 #define MAX_NETOBJ_SZ 2048
289 struct netobj {
290 	u_int	n_len;
291 	char	*n_bytes;
292 };
293 typedef struct netobj netobj;
294 
295 extern bool_t   xdr_netobj(XDR *, struct netobj *);
296 
297 extern bool_t	xdr_int32(XDR *, int32_t *);
298 extern bool_t	xdr_u_int32(XDR *, uint32_t *);
299 
300 /*
301  * These are the public routines for the various implementations of
302  * xdr streams.
303  */
304 
305 /* XDR allocating memory buffer */
306 extern void	xdralloc_create(XDR *, enum xdr_op);
307 
308 /* destroy xdralloc, save buf */
309 extern void	xdralloc_release(XDR *);
310 
311 /* get buffer from xdralloc */
312 extern caddr_t	xdralloc_getdata(XDR *);
313 
314 /* XDR using memory buffers */
315 extern void	xdrmem_create(XDR *, caddr_t, u_int, enum xdr_op);
316 
317 /* XDR using stdio library */
318 extern void	xdrstdio_create(XDR *, FILE *, enum xdr_op);
319 
320 /* XDR pseudo records for tcp */
321 extern void	xdrrec_create(XDR *xdrs, u_int, u_int, caddr_t,
322 			int (*) (caddr_t, caddr_t, int),
323 			int (*) (caddr_t, caddr_t, int));
324 
325 /* make end of xdr record */
326 extern bool_t	xdrrec_endofrecord(XDR *, bool_t);
327 
328 /* move to beginning of next record */
329 extern bool_t	xdrrec_skiprecord (XDR *xdrs);
330 
331 /* true if no more input */
332 extern bool_t	xdrrec_eof (XDR *xdrs);
333 
334 /* free memory buffers for xdr */
335 extern void	xdr_free (xdrproc_t, void *);
336 GSSRPC__END_DECLS
337 
338 #endif /* !defined(GSSRPC_XDR_H) */
339