1 /*********************************************************************
2  * RPC for the Windows NT Operating System
3  * 1993 by Martin F. Gergeleit
4  * Users may use, copy or modify Sun RPC for the Windows NT Operating
5  * System according to the Sun copyright below.
6  *
7  * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO
8  * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE
9  * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
10  *********************************************************************/
11 
12 /* @(#)xdr.h	2.2 88/07/29 4.0 RPCSRC */
13 /*
14  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
15  * unrestricted use provided that this legend is included on all tape
16  * media and as a part of the software program in whole or part.  Users
17  * may copy or modify Sun RPC without charge, but are not authorized
18  * to license or distribute it to anyone else except as part of a product or
19  * program developed by the user.
20  *
21  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
22  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
24  *
25  * Sun RPC is provided with no support and without any obligation on the
26  * part of Sun Microsystems, Inc. to assist in its use, correction,
27  * modification or enhancement.
28  *
29  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
30  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
31  * OR ANY PART THEREOF.
32  *
33  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
34  * or profits or other special, indirect and consequential damages, even if
35  * Sun has been advised of the possibility of such damages.
36  *
37  * Sun Microsystems, Inc.
38  * 2550 Garcia Avenue
39  * Mountain View, California  94043
40  */
41 /*      @(#)xdr.h 1.19 87/04/22 SMI      */
42 
43 /*
44  * xdr.h, External Data Representation Serialization Routines.
45  *
46  * Copyright (C) 1984, Sun Microsystems, Inc.
47  */
48 
49 #ifndef __XDR_HEADER__
50 #define __XDR_HEADER__
51 
52 /*
53  * XDR provides a conventional way for converting between C data
54  * types and an external bit-string representation.  Library supplied
55  * routines provide for the conversion on built-in C data types.  These
56  * routines and utility routines defined here are used to help implement
57  * a type encode/decode routine for each user-defined type.
58  *
59  * Each data type provides a single procedure which takes two arguments:
60  *
61  *	bool_t
62  *	xdrproc(xdrs, argresp)
63  *		XDR *xdrs;
64  *		<type> *argresp;
65  *
66  * xdrs is an instance of a XDR handle, to which or from which the data
67  * type is to be converted.  argresp is a pointer to the structure to be
68  * converted.  The XDR handle contains an operation field which indicates
69  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
70  *
71  * XDR_DECODE may allocate space if the pointer argresp is null.  This
72  * data can be freed with the XDR_FREE operation.
73  *
74  * We write only one procedure per data type to make it easy
75  * to keep the encode and decode procedures for a data type consistent.
76  * In many cases the same code performs all operations on a user defined type,
77  * because all the hard work is done in the component type routines.
78  * decode as a series of calls on the nested data types.
79  */
80 
81 /*
82  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
83  * stream.  XDR_DECODE causes the type to be extracted from the stream.
84  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
85  * request.
86  */
87 enum xdr_op {
88 	XDR_ENCODE=0,
89 	XDR_DECODE=1,
90 	XDR_FREE=2
91 };
92 
93 /*
94  * This is the number of bytes per unit of external data.
95  */
96 #define BYTES_PER_XDR_UNIT	(4)
97 #define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
98 		    * BYTES_PER_XDR_UNIT)
99 
100 /*
101  * A xdrproc_t exists for each data type which is to be encoded or decoded.
102  *
103  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
104  * The opaque pointer generally points to a structure of the data type
105  * to be decoded.  If this pointer is 0, then the type routines should
106  * allocate dynamic storage of the appropriate size and return it.
107  * bool_t	(*xdrproc_t)(XDR *, caddr_t *);
108  */
109 typedef	bool_t (*xdrproc_t)();
110 
111 /*
112  * The XDR handle.
113  * Contains operation which is being applied to the stream,
114  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
115  * and two private fields for the use of the particular impelementation.
116  */
117 typedef struct {
118 	enum xdr_op	x_op;		/* operation; fast additional param */
119 	struct xdr_ops {
120 		bool_t	(*x_getlong)();	/* get a long from underlying stream */
121 		bool_t	(*x_putlong)();	/* put a long to " */
122 		bool_t	(*x_getbytes)();/* get some bytes from " */
123 		bool_t	(*x_putbytes)();/* put some bytes to " */
124 		u_int	(*x_getpostn)();/* returns bytes off from beginning */
125 		bool_t  (*x_setpostn)();/* lets you reposition the stream */
126 		long *	(*x_inline)();	/* buf quick ptr to buffered data */
127 		void	(*x_destroy)();	/* free privates of this xdr_stream */
128 	} *x_ops;
129 	caddr_t 	x_public;	/* users' data */
130 	caddr_t		x_private;	/* pointer to private data */
131 	caddr_t 	x_base;		/* private used for position info */
132 	int		x_handy;	/* extra private word */
133 } XDR;
134 
135 /*
136  * Operations defined on a XDR handle
137  *
138  * XDR		*xdrs;
139  * long		*longp;
140  * caddr_t	 addr;
141  * u_int	 len;
142  * u_int	 pos;
143  */
144 #define XDR_GETLONG(xdrs, longp)			\
145 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
146 #define xdr_getlong(xdrs, longp)			\
147 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
148 
149 #define XDR_PUTLONG(xdrs, longp)			\
150 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
151 #define xdr_putlong(xdrs, longp)			\
152 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
153 
154 #define XDR_GETBYTES(xdrs, addr, len)			\
155 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
156 #define xdr_getbytes(xdrs, addr, len)			\
157 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
158 
159 #define XDR_PUTBYTES(xdrs, addr, len)			\
160 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
161 #define xdr_putbytes(xdrs, addr, len)			\
162 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
163 
164 #define XDR_GETPOS(xdrs)				\
165 	(*(xdrs)->x_ops->x_getpostn)(xdrs)
166 #define xdr_getpos(xdrs)				\
167 	(*(xdrs)->x_ops->x_getpostn)(xdrs)
168 
169 #define XDR_SETPOS(xdrs, pos)				\
170 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
171 #define xdr_setpos(xdrs, pos)				\
172 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
173 
174 #define	XDR_INLINE(xdrs, len)				\
175 	(*(xdrs)->x_ops->x_inline)(xdrs, len)
176 #define	xdr_inline(xdrs, len)				\
177 	(*(xdrs)->x_ops->x_inline)(xdrs, len)
178 
179 #define	XDR_DESTROY(xdrs)				\
180 	if ((xdrs)->x_ops->x_destroy) 			\
181 		(*(xdrs)->x_ops->x_destroy)(xdrs)
182 #define	xdr_destroy(xdrs)				\
183 	if ((xdrs)->x_ops->x_destroy) 			\
184 		(*(xdrs)->x_ops->x_destroy)(xdrs)
185 
186 /*
187  * Support struct for discriminated unions.
188  * You create an array of xdrdiscrim structures, terminated with
189  * a entry with a null procedure pointer.  The xdr_union routine gets
190  * the discriminant value and then searches the array of structures
191  * for a matching value.  If a match is found the associated xdr routine
192  * is called to handle that part of the union.  If there is
193  * no match, then a default routine may be called.
194  * If there is no match and no default routine it is an error.
195  */
196 #define NULL_xdrproc_t ((xdrproc_t)0)
197 struct xdr_discrim {
198 	int	value;
199 	xdrproc_t proc;
200 };
201 
202 /*
203  * In-line routines for fast encode/decode of primitve data types.
204  * Caveat emptor: these use single memory cycles to get the
205  * data from the underlying buffer, and will fail to operate
206  * properly if the data is not aligned.  The standard way to use these
207  * is to say:
208  *	if ((buf = XDR_INLINE(xdrs, count)) == NULL)
209  *		return (FALSE);
210  *	<<< macro calls >>>
211  * where ``count'' is the number of bytes of data occupied
212  * by the primitive data types.
213  *
214  * N.B. and frozen for all time: each data type here uses 4 bytes
215  * of external representation.
216  */
217 
218 #ifdef UNUSED
219 #define IXDR_GET_LONG(buf)		((long)ntohl((u_long)*(buf)++))
220 #define IXDR_PUT_LONG(buf, v)		(*(buf)++ = (long)htonl((u_long)v))
221 
222 #define IXDR_GET_BOOL(buf)		((bool_t)IXDR_GET_LONG(buf))
223 #define IXDR_GET_ENUM(buf, t)		((t)IXDR_GET_LONG(buf))
224 #define IXDR_GET_U_LONG(buf)		((u_long)IXDR_GET_LONG(buf))
225 #define IXDR_GET_SHORT(buf)		((short)IXDR_GET_LONG(buf))
226 #define IXDR_GET_U_SHORT(buf)		((u_short)IXDR_GET_LONG(buf))
227 
228 #define IXDR_PUT_BOOL(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
229 #define IXDR_PUT_ENUM(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
230 #define IXDR_PUT_U_LONG(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
231 #define IXDR_PUT_SHORT(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
232 #define IXDR_PUT_U_SHORT(buf, v)	IXDR_PUT_LONG((buf), ((long)(v)))
233 #endif
234 
235 /*
236  * These are the "generic" xdr routines.
237  */
238 extern bool_t	xdr_void();
239 extern bool_t	xdr_int();
240 extern bool_t	xdr_u_int();
241 extern bool_t	xdr_long();
242 extern bool_t	xdr_u_long();
243 extern bool_t	xdr_short();
244 extern bool_t	xdr_u_short();
245 extern bool_t	xdr_bool();
246 extern bool_t	xdr_enum();
247 extern bool_t	xdr_array();
248 extern bool_t	xdr_bytes();
249 extern bool_t	xdr_opaque();
250 extern bool_t	xdr_string();
251 extern bool_t	xdr_union();
252 extern bool_t	xdr_char();
253 extern bool_t	xdr_u_char();
254 extern bool_t	xdr_vector();
255 extern bool_t	xdr_float();
256 extern bool_t	xdr_double();
257 extern bool_t	xdr_reference();
258 extern bool_t	xdr_pointer();
259 extern bool_t	xdr_wrapstring();
260 
261 /*
262  * Common opaque bytes objects used by many rpc protocols;
263  * declared here due to commonality.
264  */
265 #define MAX_NETOBJ_SZ 1024
266 struct netobj {
267 	u_int	n_len;
268 	char	*n_bytes;
269 };
270 typedef struct netobj netobj;
271 extern bool_t   xdr_netobj();
272 
273 /*
274  * These are the public routines for the various implementations of
275  * xdr streams.
276  */
277 extern void   xdrmem_create();		/* XDR using memory buffers */
278 extern void   xdrstdio_create();	/* XDR using stdio library */
279 extern void   xdrrec_create();		/* XDR pseudo records for tcp */
280 extern bool_t xdrrec_endofrecord();	/* make end of xdr record */
281 extern bool_t xdrrec_skiprecord();	/* move to beginning of next record */
282 extern bool_t xdrrec_eof();		/* true if no more input */
283 
284 #endif /* __XDR_HEADER__ */
285