xref: /freebsd/crypto/krb5/src/include/gssrpc/xdr.h (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
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 
106 /*
107  * The XDR handle.
108  * Contains operation which is being applied to the stream,
109  * an operations vector for the particular implementation (e.g. see xdr_mem.c),
110  * and two private fields for the use of the particular impelementation.
111  */
112 typedef struct XDR {
113 	enum xdr_op	x_op;		/* operation; fast additional param */
114 	struct xdr_ops {
115 		/* get a long from underlying stream */
116 		bool_t	(*x_getlong)(struct XDR *, long *);
117 
118 		/* put a long to underlying stream */
119 		bool_t	(*x_putlong)(struct XDR *, long *);
120 
121 		/* get some bytes from underlying stream */
122 		bool_t	(*x_getbytes)(struct XDR *, caddr_t, u_int);
123 
124 		/* put some bytes to underlying stream */
125 		bool_t	(*x_putbytes)(struct XDR *, caddr_t, u_int);
126 
127 		/* returns bytes off from beginning */
128 		u_int	(*x_getpostn)(struct XDR *);
129 
130 		/* lets you reposition the stream */
131 		bool_t	(*x_setpostn)(struct XDR *, u_int);
132 
133 		/* buf quick ptr to buffered data */
134 		rpc_inline_t *(*x_inline)(struct XDR *, int);
135 
136 		/* free privates of this xdr_stream */
137 		void	(*x_destroy)(struct XDR *);
138 	} *x_ops;
139 	caddr_t 	x_public;	/* users' data */
140 	void *		x_private;	/* pointer to private data */
141 	caddr_t 	x_base;		/* private used for position info */
142 	int		x_handy;	/* extra private word */
143 } XDR;
144 
145 typedef	bool_t (*xdrproc_t)(XDR *, void *);
146 
147 /*
148  * Operations defined on a XDR handle
149  *
150  * XDR		*xdrs;
151  * int32_t	*longp;
152  * caddr_t	 addr;
153  * u_int	 len;
154  * u_int	 pos;
155  */
156 #define XDR_GETLONG(xdrs, longp)			\
157 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
158 #define xdr_getlong(xdrs, longp)			\
159 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
160 
161 #define XDR_PUTLONG(xdrs, longp)			\
162 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
163 #define xdr_putlong(xdrs, longp)			\
164 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
165 
166 #define XDR_GETBYTES(xdrs, addr, len)			\
167 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
168 #define xdr_getbytes(xdrs, addr, len)			\
169 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
170 
171 #define XDR_PUTBYTES(xdrs, addr, len)			\
172 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
173 #define xdr_putbytes(xdrs, addr, len)			\
174 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
175 
176 #define XDR_GETPOS(xdrs)				\
177 	(*(xdrs)->x_ops->x_getpostn)(xdrs)
178 #define xdr_getpos(xdrs)				\
179 	(*(xdrs)->x_ops->x_getpostn)(xdrs)
180 
181 #define XDR_SETPOS(xdrs, pos)				\
182 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
183 #define xdr_setpos(xdrs, pos)				\
184 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
185 
186 #define	XDR_INLINE(xdrs, len)				\
187 	(*(xdrs)->x_ops->x_inline)(xdrs, len)
188 #define	xdr_inline(xdrs, len)				\
189 	(*(xdrs)->x_ops->x_inline)(xdrs, len)
190 
191 #define	XDR_DESTROY(xdrs)				\
192 	if ((xdrs)->x_ops->x_destroy) 			\
193 		(*(xdrs)->x_ops->x_destroy)(xdrs)
194 #define	xdr_destroy(xdrs)				\
195 	if ((xdrs)->x_ops->x_destroy) 			\
196 		(*(xdrs)->x_ops->x_destroy)(xdrs)
197 
198 /*
199  * Support struct for discriminated unions.
200  * You create an array of xdrdiscrim structures, terminated with
201  * a entry with a null procedure pointer.  The xdr_union routine gets
202  * the discriminant value and then searches the array of structures
203  * for a matching value.  If a match is found the associated xdr routine
204  * is called to handle that part of the union.  If there is
205  * no match, then a default routine may be called.
206  * If there is no match and no default routine it is an error.
207  */
208 #define NULL_xdrproc_t ((xdrproc_t)0)
209 struct xdr_discrim {
210 	int	value;
211 	xdrproc_t proc;
212 };
213 
214 /*
215  * In-line routines for fast encode/decode of primitive data types.
216  * Caveat emptor: these use single memory cycles to get the
217  * data from the underlying buffer, and will fail to operate
218  * properly if the data is not aligned.  The standard way to use these
219  * is to say:
220  *	if ((buf = XDR_INLINE(xdrs, count)) == NULL)
221  *		return (FALSE);
222  *	<<< macro calls >>>
223  * where ``count'' is the number of bytes of data occupied
224  * by the primitive data types.
225  *
226  * N.B. and frozen for all time: each data type here uses 4 bytes
227  * of external representation.
228  */
229 #define IXDR_GET_INT32(buf)		((int32_t)IXDR_GET_U_INT32(buf))
230 #define IXDR_PUT_INT32(buf, v)		IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
231 #define IXDR_GET_U_INT32(buf)		(ntohl((uint32_t)*(buf)++))
232 #define IXDR_PUT_U_INT32(buf, v)	(*(buf)++ = (int32_t)htonl((v)))
233 
234 #define IXDR_GET_LONG(buf)		((long)IXDR_GET_INT32(buf))
235 #define IXDR_PUT_LONG(buf, v)		IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
236 
237 #define IXDR_GET_BOOL(buf)		((bool_t)IXDR_GET_LONG(buf))
238 #define IXDR_GET_ENUM(buf, t)		((t)IXDR_GET_INT32(buf))
239 #define IXDR_GET_U_LONG(buf)		((u_long)IXDR_GET_U_INT32(buf))
240 #define IXDR_GET_SHORT(buf)		((short)IXDR_GET_INT32(buf))
241 #define IXDR_GET_U_SHORT(buf)		((u_short)IXDR_GET_U_INT32(buf))
242 
243 #define IXDR_PUT_BOOL(buf, v)		IXDR_PUT_INT32((buf),((int32_t)(v)))
244 #define IXDR_PUT_ENUM(buf, v)		IXDR_PUT_INT32((buf),((int32_t)(v)))
245 #define IXDR_PUT_U_LONG(buf, v)		IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
246 #define IXDR_PUT_SHORT(buf, v)		IXDR_PUT_INT32((buf),((int32_t)(v)))
247 #define IXDR_PUT_U_SHORT(buf, v)	IXDR_PUT_U_INT32((buf),((uint32_t)(v)))
248 
249 /*
250  * These are the "generic" xdr routines.
251  */
252 extern bool_t	xdr_void(XDR *, void *);
253 extern bool_t	xdr_int(XDR *, int *);
254 extern bool_t	xdr_u_int(XDR *, u_int *);
255 extern bool_t	xdr_long(XDR *, long *);
256 extern bool_t	xdr_u_long(XDR *, u_long *);
257 extern bool_t	xdr_short(XDR *, short *);
258 extern bool_t	xdr_u_short(XDR *, u_short *);
259 extern bool_t	xdr_bool(XDR *, bool_t *);
260 extern bool_t	xdr_enum(XDR *, enum_t *);
261 extern bool_t	xdr_array(XDR *, caddr_t *, u_int *,
262 			u_int, u_int, xdrproc_t);
263 extern bool_t	xdr_bytes(XDR *, char **, u_int *, u_int);
264 extern bool_t	xdr_opaque(XDR *, caddr_t, u_int);
265 extern bool_t	xdr_string(XDR *, char **, u_int);
266 extern bool_t	xdr_union(XDR *, enum_t *, char *, struct xdr_discrim *,
267 			xdrproc_t);
268 extern bool_t	xdr_char(XDR *, char *);
269 extern bool_t	xdr_u_char(XDR *, u_char *);
270 extern bool_t	xdr_vector(XDR *, char *, u_int, u_int, xdrproc_t);
271 extern bool_t	xdr_float(XDR *, float *);
272 extern bool_t	xdr_double(XDR *, double *);
273 extern bool_t	xdr_reference(XDR *, caddr_t *, u_int, xdrproc_t);
274 extern bool_t	xdr_pointer(XDR *, char **, u_int, xdrproc_t);
275 extern bool_t	xdr_wrapstring(XDR *, char **);
276 
277 extern unsigned long xdr_sizeof(xdrproc_t, void *);
278 
279 #define xdr_rpcprog	xdr_u_int32
280 #define xdr_rpcvers	xdr_u_int32
281 #define xdr_rpcprot	xdr_u_int32
282 #define xdr_rpcproc	xdr_u_int32
283 #define xdr_rpcport	xdr_u_int32
284 
285 /*
286  * Common opaque bytes objects used by many rpc protocols;
287  * declared here due to commonality.
288  */
289 #define MAX_NETOBJ_SZ 2048
290 struct netobj {
291 	u_int	n_len;
292 	char	*n_bytes;
293 };
294 typedef struct netobj netobj;
295 
296 extern bool_t   xdr_netobj(XDR *, struct netobj *);
297 
298 extern bool_t	xdr_int32(XDR *, int32_t *);
299 extern bool_t	xdr_u_int32(XDR *, uint32_t *);
300 
301 /*
302  * These are the public routines for the various implementations of
303  * xdr streams.
304  */
305 
306 /* XDR allocating memory buffer */
307 extern void	xdralloc_create(XDR *, enum xdr_op);
308 
309 /* destroy xdralloc, save buf */
310 extern void	xdralloc_release(XDR *);
311 
312 /* get buffer from xdralloc */
313 extern caddr_t	xdralloc_getdata(XDR *);
314 
315 /* XDR using memory buffers */
316 extern void	xdrmem_create(XDR *, caddr_t, u_int, enum xdr_op);
317 
318 /* XDR using stdio library */
319 extern void	xdrstdio_create(XDR *, FILE *, enum xdr_op);
320 
321 /* XDR pseudo records for tcp */
322 extern void	xdrrec_create(XDR *xdrs, u_int, u_int, caddr_t,
323 			int (*) (caddr_t, caddr_t, int),
324 			int (*) (caddr_t, caddr_t, int));
325 
326 /* make end of xdr record */
327 extern bool_t	xdrrec_endofrecord(XDR *, bool_t);
328 
329 /* move to beginning of next record */
330 extern bool_t	xdrrec_skiprecord (XDR *xdrs);
331 
332 /* true if no more input */
333 extern bool_t	xdrrec_eof (XDR *xdrs);
334 
335 /* free memory buffers for xdr */
336 extern void	xdr_free (xdrproc_t, void *);
337 GSSRPC__END_DECLS
338 
339 #endif /* !defined(GSSRPC_XDR_H) */
340