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