xref: /openbsd-src/include/rpc/xdr.h (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: xdr.h,v 1.2 1997/09/21 10:46:18 niklas Exp $	*/
2 /*	$NetBSD: xdr.h,v 1.7 1995/04/29 05:28:06 cgd Exp $	*/
3 
4 /*
5  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
6  * unrestricted use provided that this legend is included on all tape
7  * media and as a part of the software program in whole or part.  Users
8  * may copy or modify Sun RPC without charge, but are not authorized
9  * to license or distribute it to anyone else except as part of a product or
10  * program developed by the user.
11  *
12  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15  *
16  * Sun RPC is provided with no support and without any obligation on the
17  * part of Sun Microsystems, Inc. to assist in its use, correction,
18  * modification or enhancement.
19  *
20  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22  * OR ANY PART THEREOF.
23  *
24  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25  * or profits or other special, indirect and consequential damages, even if
26  * Sun has been advised of the possibility of such damages.
27  *
28  * Sun Microsystems, Inc.
29  * 2550 Garcia Avenue
30  * Mountain View, California  94043
31  *
32  *	from: @(#)xdr.h 1.19 87/04/22 SMI
33  *	@(#)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  */
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  * The XDR handle.
96  * Contains operation which is being applied to the stream,
97  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
98  * and two private fields for the use of the particular impelementation.
99  */
100 typedef struct __rpc_xdr {
101 	enum xdr_op	x_op;		/* operation; fast additional param */
102 	struct xdr_ops {
103 		/* get a long from underlying stream */
104 		bool_t	(*x_getlong) __P((struct __rpc_xdr *, long *));
105 		/* put a long to " */
106 		bool_t	(*x_putlong) __P((struct __rpc_xdr *, long *));
107 		/* get some bytes from " */
108 		bool_t	(*x_getbytes) __P((struct __rpc_xdr *, caddr_t, u_int));
109 		/* put some bytes to " */
110 		bool_t	(*x_putbytes) __P((struct __rpc_xdr *, caddr_t, u_int));
111 		/* returns bytes off from beginning */
112 		u_int	(*x_getpostn) __P((struct __rpc_xdr *));
113 		/* lets you reposition the stream */
114 		bool_t  (*x_setpostn) __P((struct __rpc_xdr *, u_int));
115 		/* buf quick ptr to buffered data */
116 		int32_t	*(*x_inline) __P((struct __rpc_xdr *, u_int));
117 		/* free privates of this xdr_stream */
118 		void	(*x_destroy) __P((struct __rpc_xdr *));
119 	} *x_ops;
120 	caddr_t 	x_public;	/* users' data */
121 	caddr_t		x_private;	/* pointer to private data */
122 	caddr_t 	x_base;		/* private used for position info */
123 	int		x_handy;	/* extra private word */
124 } XDR;
125 
126 /*
127  * A xdrproc_t exists for each data type which is to be encoded or decoded.
128  *
129  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
130  * The opaque pointer generally points to a structure of the data type
131  * to be decoded.  If this pointer is 0, then the type routines should
132  * allocate dynamic storage of the appropriate size and return it.
133  *
134  * XXX can't actually prototype it, because some take three args!!!
135  */
136 typedef	bool_t (*xdrproc_t) __P((/* XDR *, void *, u_int */));
137 
138 /*
139  * Operations defined on a XDR handle
140  *
141  * XDR		*xdrs;
142  * long		*longp;
143  * caddr_t	 addr;
144  * u_int	 len;
145  * u_int	 pos;
146  */
147 #define XDR_GETLONG(xdrs, longp)			\
148 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
149 #define xdr_getlong(xdrs, longp)			\
150 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
151 
152 #define XDR_PUTLONG(xdrs, longp)			\
153 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
154 #define xdr_putlong(xdrs, longp)			\
155 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
156 
157 #define XDR_GETBYTES(xdrs, addr, len)			\
158 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
159 #define xdr_getbytes(xdrs, addr, len)			\
160 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
161 
162 #define XDR_PUTBYTES(xdrs, addr, len)			\
163 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
164 #define xdr_putbytes(xdrs, addr, len)			\
165 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
166 
167 #define XDR_GETPOS(xdrs)				\
168 	(*(xdrs)->x_ops->x_getpostn)(xdrs)
169 #define xdr_getpos(xdrs)				\
170 	(*(xdrs)->x_ops->x_getpostn)(xdrs)
171 
172 #define XDR_SETPOS(xdrs, pos)				\
173 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
174 #define xdr_setpos(xdrs, pos)				\
175 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
176 
177 #define	XDR_INLINE(xdrs, len)				\
178 	(*(xdrs)->x_ops->x_inline)(xdrs, len)
179 #define	xdr_inline(xdrs, len)				\
180 	(*(xdrs)->x_ops->x_inline)(xdrs, len)
181 
182 #define	XDR_DESTROY(xdrs)				\
183 	if ((xdrs)->x_ops->x_destroy) 			\
184 		(*(xdrs)->x_ops->x_destroy)(xdrs)
185 #define	xdr_destroy(xdrs)				\
186 	if ((xdrs)->x_ops->x_destroy) 			\
187 		(*(xdrs)->x_ops->x_destroy)(xdrs)
188 
189 /*
190  * Support struct for discriminated unions.
191  * You create an array of xdrdiscrim structures, terminated with
192  * a entry with a null procedure pointer.  The xdr_union routine gets
193  * the discriminant value and then searches the array of structures
194  * for a matching value.  If a match is found the associated xdr routine
195  * is called to handle that part of the union.  If there is
196  * no match, then a default routine may be called.
197  * If there is no match and no default routine it is an error.
198  */
199 #define NULL_xdrproc_t ((xdrproc_t)0)
200 struct xdr_discrim {
201 	int	value;
202 	xdrproc_t proc;
203 };
204 
205 /*
206  * In-line routines for fast encode/decode of primitve data types.
207  * Caveat emptor: these use single memory cycles to get the
208  * data from the underlying buffer, and will fail to operate
209  * properly if the data is not aligned.  The standard way to use these
210  * is to say:
211  *	if ((buf = XDR_INLINE(xdrs, count)) == NULL)
212  *		return (FALSE);
213  *	<<< macro calls >>>
214  * where ``count'' is the number of bytes of data occupied
215  * by the primitive data types.
216  *
217  * N.B. and frozen for all time: each data type here uses 4 bytes
218  * of external representation.
219  */
220 #define IXDR_GET_LONG(buf)		((long)ntohl((u_long)*(buf)++))
221 #define IXDR_PUT_LONG(buf, v)		(*(buf)++ = (long)htonl((u_long)v))
222 
223 #define IXDR_GET_BOOL(buf)		((bool_t)IXDR_GET_LONG(buf))
224 #define IXDR_GET_ENUM(buf, t)		((t)IXDR_GET_LONG(buf))
225 #define IXDR_GET_U_LONG(buf)		((u_long)IXDR_GET_LONG(buf))
226 #define IXDR_GET_SHORT(buf)		((short)IXDR_GET_LONG(buf))
227 #define IXDR_GET_U_SHORT(buf)		((u_short)IXDR_GET_LONG(buf))
228 
229 #define IXDR_PUT_BOOL(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
230 #define IXDR_PUT_ENUM(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
231 #define IXDR_PUT_U_LONG(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
232 #define IXDR_PUT_SHORT(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
233 #define IXDR_PUT_U_SHORT(buf, v)	IXDR_PUT_LONG((buf), ((long)(v)))
234 
235 /*
236  * These are the "generic" xdr routines.
237  */
238 __BEGIN_DECLS
239 extern bool_t	xdr_void	__P((void));
240 extern bool_t	xdr_int		__P((XDR *, int *));
241 extern bool_t	xdr_u_int	__P((XDR *, u_int *));
242 extern bool_t	xdr_long	__P((XDR *, long *));
243 extern bool_t	xdr_u_long	__P((XDR *, u_long *));
244 extern bool_t	xdr_short	__P((XDR *, short *));
245 extern bool_t	xdr_u_short	__P((XDR *, u_short *));
246 extern bool_t	xdr_int16_t	__P((XDR *, int16_t *));
247 extern bool_t	xdr_u_int16_t	__P((XDR *, u_int16_t *));
248 extern bool_t	xdr_int32_t	__P((XDR *, int32_t *));
249 extern bool_t	xdr_u_int32_t	__P((XDR *, u_int32_t *));
250 extern bool_t	xdr_bool	__P((XDR *, bool_t *));
251 extern bool_t	xdr_enum	__P((XDR *, enum_t *));
252 extern bool_t	xdr_array	__P((XDR *, char **, u_int *, u_int, u_int, xdrproc_t));
253 extern bool_t	xdr_bytes	__P((XDR *, char **, u_int *, u_int));
254 extern bool_t	xdr_opaque	__P((XDR *, caddr_t, u_int));
255 extern bool_t	xdr_string	__P((XDR *, char **, u_int));
256 extern bool_t	xdr_union	__P((XDR *, enum_t *, char *, struct xdr_discrim *, xdrproc_t));
257 extern bool_t	xdr_char	__P((XDR *, char *));
258 extern bool_t	xdr_u_char	__P((XDR *, u_char *));
259 extern bool_t	xdr_vector	__P((XDR *, char *, u_int, u_int, xdrproc_t));
260 extern bool_t	xdr_float	__P((XDR *, float *));
261 extern bool_t	xdr_double	__P((XDR *, double *));
262 extern bool_t	xdr_reference	__P((XDR *, caddr_t *, u_int, xdrproc_t));
263 extern bool_t	xdr_pointer	__P((XDR *, caddr_t *, u_int, xdrproc_t));
264 extern bool_t	xdr_wrapstring	__P((XDR *, char **));
265 extern void	xdr_free 	__P((xdrproc_t, char *));
266 __END_DECLS
267 
268 /*
269  * Common opaque bytes objects used by many rpc protocols;
270  * declared here due to commonality.
271  */
272 #define MAX_NETOBJ_SZ 1024
273 struct netobj {
274 	u_int	n_len;
275 	char	*n_bytes;
276 };
277 typedef struct netobj netobj;
278 extern bool_t   xdr_netobj __P((XDR *, struct netobj *));
279 
280 /*
281  * These are the public routines for the various implementations of
282  * xdr streams.
283  */
284 __BEGIN_DECLS
285 /* XDR using memory buffers */
286 extern void   xdrmem_create	__P((XDR *, char *, u_int, enum xdr_op));
287 
288 #ifdef _STDIO_H_
289 /* XDR using stdio library */
290 extern void   xdrstdio_create	__P((XDR *, FILE *, enum xdr_op));
291 #endif
292 
293 /* XDR pseudo records for tcp */
294 extern void   xdrrec_create	__P((XDR *, u_int, u_int, char *,
295 				    int (*) __P((caddr_t, caddr_t, int)),
296 				    int (*) __P((caddr_t, caddr_t, int))));
297 
298 /* make end of xdr record */
299 extern bool_t xdrrec_endofrecord __P((XDR *, int));
300 
301 /* move to beginning of next record */
302 extern bool_t xdrrec_skiprecord	__P((XDR *));
303 
304 /* true if no more input */
305 extern bool_t xdrrec_eof	__P((XDR *));
306 __END_DECLS
307 
308 #endif /* !_RPC_XDR_H */
309