xref: /titanic_41/usr/src/lib/libbc/inc/include/rpc/xdr.h (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 #pragma ident	"%Z%%M%	%I%	%E% SMI"
23 
24 /*
25  * xdr.h, External Data Representation Serialization Routines.
26  *
27  * Copyright (C) 1984, Sun Microsystems, Inc.
28  */
29 
30 #ifndef _rpc_xdr_h
31 #define	_rpc_xdr_h
32 
33 #include <rpc/types.h>
34 /*
35  * XDR provides a conventional way for converting between C data
36  * types and an external bit-string representation.  Library supplied
37  * routines provide for the conversion on built-in C data types.  These
38  * routines and utility routines defined here are used to help implement
39  * a type encode/decode routine for each user-defined type.
40  *
41  * Each data type provides a single procedure which takes two arguments:
42  *
43  *	bool_t
44  *	xdrproc(xdrs, argresp)
45  *		XDR *xdrs;
46  *		<type> *argresp;
47  *
48  * xdrs is an instance of a XDR handle, to which or from which the data
49  * type is to be converted.  argresp is a pointer to the structure to be
50  * converted.  The XDR handle contains an operation field which indicates
51  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
52  *
53  * XDR_DECODE may allocate space if the pointer argresp is null.  This
54  * data can be freed with the XDR_FREE operation.
55  *
56  * We write only one procedure per data type to make it easy
57  * to keep the encode and decode procedures for a data type consistent.
58  * In many cases the same code performs all operations on a user defined type,
59  * because all the hard work is done in the component type routines.
60  * decode as a series of calls on the nested data types.
61  */
62 
63 /*
64  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
65  * stream.  XDR_DECODE causes the type to be extracted from the stream.
66  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
67  * request.
68  */
69 enum xdr_op {
70 	XDR_ENCODE=0,
71 	XDR_DECODE=1,
72 	XDR_FREE=2
73 };
74 
75 /*
76  * This is the number of bytes per unit of external data.
77  */
78 #define	BYTES_PER_XDR_UNIT	(4)
79 #define	RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
80 			* BYTES_PER_XDR_UNIT)
81 
82 /*
83  * A xdrproc_t exists for each data type which is to be encoded or decoded.
84  *
85  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
86  * The opaque pointer generally points to a structure of the data type
87  * to be decoded.  If this pointer is 0, then the type routines should
88  * allocate dynamic storage of the appropriate size and return it.
89  * bool_t	(*xdrproc_t)(XDR *, caddr_t *);
90  */
91 typedef	bool_t (*xdrproc_t)();
92 
93 /*
94  * The XDR handle.
95  * Contains operation which is being applied to the stream,
96  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
97  * and two private fields for the use of the particular impelementation.
98  */
99 typedef struct {
100 	enum xdr_op	x_op;		/* operation; fast additional param */
101 	struct xdr_ops {
102 		bool_t	(*x_getlong)();	/* get a long from underlying stream */
103 		bool_t	(*x_putlong)();	/* put a long to " */
104 		bool_t	(*x_getbytes)(); /* get some bytes from " */
105 		bool_t	(*x_putbytes)(); /* put some bytes to " */
106 		u_int	(*x_getpostn)(); /* returns bytes off from beginning */
107 		bool_t	(*x_setpostn)(); /* lets you reposition the stream */
108 		long *	(*x_inline)();	/* buf quick ptr to buffered data */
109 		void	(*x_destroy)();	/* free privates of this xdr_stream */
110 	} *x_ops;
111 	caddr_t 	x_public;	/* users' data */
112 	caddr_t		x_private;	/* pointer to private data */
113 	caddr_t 	x_base;		/* private used for position info */
114 	int		x_handy;	/* extra private word */
115 } XDR;
116 
117 /*
118  * Operations defined on a XDR handle
119  *
120  * XDR		*xdrs;
121  * long		*longp;
122  * caddr_t	 addr;
123  * u_int	 len;
124  * u_int	 pos;
125  */
126 #define	XDR_GETLONG(xdrs, longp)			\
127 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
128 #define	xdr_getlong(xdrs, longp)			\
129 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
130 
131 #define	XDR_PUTLONG(xdrs, longp)			\
132 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
133 #define	xdr_putlong(xdrs, longp)			\
134 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
135 
136 #define	XDR_GETBYTES(xdrs, addr, len)			\
137 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
138 #define	xdr_getbytes(xdrs, addr, len)			\
139 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
140 
141 #define	XDR_PUTBYTES(xdrs, addr, len)			\
142 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
143 #define	xdr_putbytes(xdrs, addr, len)			\
144 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
145 
146 #define	XDR_GETPOS(xdrs)				\
147 	(*(xdrs)->x_ops->x_getpostn)(xdrs)
148 #define	xdr_getpos(xdrs)				\
149 	(*(xdrs)->x_ops->x_getpostn)(xdrs)
150 
151 #define	XDR_SETPOS(xdrs, pos)				\
152 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
153 #define	xdr_setpos(xdrs, pos)				\
154 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
155 
156 #define	XDR_INLINE(xdrs, len)				\
157 	(*(xdrs)->x_ops->x_inline)(xdrs, len)
158 #define	xdr_inline(xdrs, len)				\
159 	(*(xdrs)->x_ops->x_inline)(xdrs, len)
160 
161 #define	XDR_DESTROY(xdrs)				\
162 	(*(xdrs)->x_ops->x_destroy)(xdrs)
163 #define	xdr_destroy(xdrs) XDR_DESTROY(xdrs)
164 
165 /*
166  * Support struct for discriminated unions.
167  * You create an array of xdrdiscrim structures, terminated with
168  * a entry with a null procedure pointer.  The xdr_union routine gets
169  * the discriminant value and then searches the array of structures
170  * for a matching value.  If a match is found the associated xdr routine
171  * is called to handle that part of the union.  If there is
172  * no match, then a default routine may be called.
173  * If there is no match and no default routine it is an error.
174  */
175 #define	NULL_xdrproc_t ((xdrproc_t)0)
176 struct xdr_discrim {
177 	int	value;
178 	xdrproc_t proc;
179 };
180 
181 /*
182  * In-line routines for fast encode/decode of primitve data types.
183  * Caveat emptor: these use single memory cycles to get the
184  * data from the underlying buffer, and will fail to operate
185  * properly if the data is not aligned.  The standard way to use these
186  * is to say:
187  *	if ((buf = XDR_INLINE(xdrs, count)) == NULL)
188  *		return (FALSE);
189  *	<<< macro calls >>>
190  * where ``count'' is the number of bytes of data occupied
191  * by the primitive data types.
192  *
193  * N.B. and frozen for all time: each data type here uses 4 bytes
194  * of external representation.
195  */
196 #define	IXDR_GET_LONG(buf)		((long)ntohl((u_long)*(buf)++))
197 #define	IXDR_PUT_LONG(buf, v)		(*(buf)++ = (long)htonl((u_long)v))
198 
199 #define	IXDR_GET_BOOL(buf)		((bool_t)IXDR_GET_LONG(buf))
200 #define	IXDR_GET_ENUM(buf, t)		((t)IXDR_GET_LONG(buf))
201 #define	IXDR_GET_U_LONG(buf)		((u_long)IXDR_GET_LONG(buf))
202 #define	IXDR_GET_SHORT(buf)		((short)IXDR_GET_LONG(buf))
203 #define	IXDR_GET_U_SHORT(buf)		((u_short)IXDR_GET_LONG(buf))
204 
205 #define	IXDR_PUT_BOOL(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
206 #define	IXDR_PUT_ENUM(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
207 #define	IXDR_PUT_U_LONG(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
208 #define	IXDR_PUT_SHORT(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
209 #define	IXDR_PUT_U_SHORT(buf, v)	IXDR_PUT_LONG((buf), ((long)(v)))
210 
211 /*
212  * These are the "generic" xdr routines.
213  */
214 extern bool_t	xdr_void();
215 extern bool_t	xdr_int();
216 extern bool_t	xdr_u_int();
217 extern bool_t	xdr_long();
218 extern bool_t	xdr_u_long();
219 extern bool_t	xdr_short();
220 extern bool_t	xdr_u_short();
221 extern bool_t	xdr_bool();
222 extern bool_t	xdr_enum();
223 extern bool_t	xdr_array();
224 extern bool_t	xdr_bytes();
225 extern bool_t	xdr_opaque();
226 extern bool_t	xdr_string();
227 extern bool_t	xdr_union();
228 #ifndef KERNEL
229 extern void	xdr_free();
230 extern bool_t	xdr_char();
231 extern bool_t	xdr_u_char();
232 extern bool_t	xdr_vector();
233 extern bool_t	xdr_float();
234 extern bool_t	xdr_double();
235 extern bool_t	xdr_reference();
236 extern bool_t	xdr_pointer();
237 extern bool_t	xdr_wrapstring();
238 #endif !KERNEL
239 
240 /*
241  * Common opaque bytes objects used by many rpc protocols;
242  * declared here due to commonality.
243  */
244 #define	MAX_NETOBJ_SZ 1024
245 struct netobj {
246 	u_int	n_len;
247 	char	*n_bytes;
248 };
249 typedef struct netobj netobj;
250 extern bool_t	xdr_netobj();
251 
252 /*
253  * These are the public routines for the various implementations of
254  * xdr streams.
255  */
256 extern void	xdrmem_create();	/* XDR using memory buffers */
257 #ifndef KERNEL
258 extern void	xdrstdio_create();	/* XDR using stdio library */
259 extern void	xdrrec_create();	/* XDR pseudo records for tcp */
260 extern bool_t	xdrrec_endofrecord();	/* make end of xdr record */
261 extern int	xdrrec_readbytes();	/* like a read on a pipe */
262 extern bool_t	xdrrec_skiprecord();	/* move to beginning of next record */
263 extern bool_t	xdrrec_eof();		/* true if no more input */
264 #else
265 extern void xdrmbuf_init();		/* XDR using kernel mbufs */
266 #endif !KERNEL
267 
268 #endif /*!_rpc_xdr_h*/
269