xref: /titanic_51/usr/src/lib/libbc/inc/include/rpc/xdr.h (revision 5d54f3d8999eac1762fe0a8c7177d20f1f201fae)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*5d54f3d8Smuffin  * Copyright 1984 Sun Microsystems, Inc.  All rights reserved.
24*5d54f3d8Smuffin  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef _rpc_xdr_h
287c478bd9Sstevel@tonic-gate #define	_rpc_xdr_h
297c478bd9Sstevel@tonic-gate 
30*5d54f3d8Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*5d54f3d8Smuffin 
32*5d54f3d8Smuffin /*
33*5d54f3d8Smuffin  * xdr.h, External Data Representation Serialization Routines.
34*5d54f3d8Smuffin  */
35*5d54f3d8Smuffin 
367c478bd9Sstevel@tonic-gate #include <rpc/types.h>
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * XDR provides a conventional way for converting between C data
397c478bd9Sstevel@tonic-gate  * types and an external bit-string representation.  Library supplied
407c478bd9Sstevel@tonic-gate  * routines provide for the conversion on built-in C data types.  These
417c478bd9Sstevel@tonic-gate  * routines and utility routines defined here are used to help implement
427c478bd9Sstevel@tonic-gate  * a type encode/decode routine for each user-defined type.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * Each data type provides a single procedure which takes two arguments:
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  *	bool_t
477c478bd9Sstevel@tonic-gate  *	xdrproc(xdrs, argresp)
487c478bd9Sstevel@tonic-gate  *		XDR *xdrs;
497c478bd9Sstevel@tonic-gate  *		<type> *argresp;
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * xdrs is an instance of a XDR handle, to which or from which the data
527c478bd9Sstevel@tonic-gate  * type is to be converted.  argresp is a pointer to the structure to be
537c478bd9Sstevel@tonic-gate  * converted.  The XDR handle contains an operation field which indicates
547c478bd9Sstevel@tonic-gate  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * XDR_DECODE may allocate space if the pointer argresp is null.  This
577c478bd9Sstevel@tonic-gate  * data can be freed with the XDR_FREE operation.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * We write only one procedure per data type to make it easy
607c478bd9Sstevel@tonic-gate  * to keep the encode and decode procedures for a data type consistent.
617c478bd9Sstevel@tonic-gate  * In many cases the same code performs all operations on a user defined type,
627c478bd9Sstevel@tonic-gate  * because all the hard work is done in the component type routines.
637c478bd9Sstevel@tonic-gate  * decode as a series of calls on the nested data types.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
687c478bd9Sstevel@tonic-gate  * stream.  XDR_DECODE causes the type to be extracted from the stream.
697c478bd9Sstevel@tonic-gate  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
707c478bd9Sstevel@tonic-gate  * request.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate enum xdr_op {
737c478bd9Sstevel@tonic-gate 	XDR_ENCODE=0,
747c478bd9Sstevel@tonic-gate 	XDR_DECODE=1,
757c478bd9Sstevel@tonic-gate 	XDR_FREE=2
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * This is the number of bytes per unit of external data.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate #define	BYTES_PER_XDR_UNIT	(4)
827c478bd9Sstevel@tonic-gate #define	RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
837c478bd9Sstevel@tonic-gate 			* BYTES_PER_XDR_UNIT)
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * A xdrproc_t exists for each data type which is to be encoded or decoded.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
897c478bd9Sstevel@tonic-gate  * The opaque pointer generally points to a structure of the data type
907c478bd9Sstevel@tonic-gate  * to be decoded.  If this pointer is 0, then the type routines should
917c478bd9Sstevel@tonic-gate  * allocate dynamic storage of the appropriate size and return it.
927c478bd9Sstevel@tonic-gate  * bool_t	(*xdrproc_t)(XDR *, caddr_t *);
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate typedef	bool_t (*xdrproc_t)();
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * The XDR handle.
987c478bd9Sstevel@tonic-gate  * Contains operation which is being applied to the stream,
997c478bd9Sstevel@tonic-gate  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
1007c478bd9Sstevel@tonic-gate  * and two private fields for the use of the particular impelementation.
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate typedef struct {
1037c478bd9Sstevel@tonic-gate 	enum xdr_op	x_op;		/* operation; fast additional param */
1047c478bd9Sstevel@tonic-gate 	struct xdr_ops {
1057c478bd9Sstevel@tonic-gate 		bool_t	(*x_getlong)();	/* get a long from underlying stream */
1067c478bd9Sstevel@tonic-gate 		bool_t	(*x_putlong)();	/* put a long to " */
1077c478bd9Sstevel@tonic-gate 		bool_t	(*x_getbytes)(); /* get some bytes from " */
1087c478bd9Sstevel@tonic-gate 		bool_t	(*x_putbytes)(); /* put some bytes to " */
1097c478bd9Sstevel@tonic-gate 		u_int	(*x_getpostn)(); /* returns bytes off from beginning */
1107c478bd9Sstevel@tonic-gate 		bool_t	(*x_setpostn)(); /* lets you reposition the stream */
1117c478bd9Sstevel@tonic-gate 		long *	(*x_inline)();	/* buf quick ptr to buffered data */
1127c478bd9Sstevel@tonic-gate 		void	(*x_destroy)();	/* free privates of this xdr_stream */
1137c478bd9Sstevel@tonic-gate 	} *x_ops;
1147c478bd9Sstevel@tonic-gate 	caddr_t 	x_public;	/* users' data */
1157c478bd9Sstevel@tonic-gate 	caddr_t		x_private;	/* pointer to private data */
1167c478bd9Sstevel@tonic-gate 	caddr_t 	x_base;		/* private used for position info */
1177c478bd9Sstevel@tonic-gate 	int		x_handy;	/* extra private word */
1187c478bd9Sstevel@tonic-gate } XDR;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * Operations defined on a XDR handle
1227c478bd9Sstevel@tonic-gate  *
1237c478bd9Sstevel@tonic-gate  * XDR		*xdrs;
1247c478bd9Sstevel@tonic-gate  * long		*longp;
1257c478bd9Sstevel@tonic-gate  * caddr_t	 addr;
1267c478bd9Sstevel@tonic-gate  * u_int	 len;
1277c478bd9Sstevel@tonic-gate  * u_int	 pos;
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate #define	XDR_GETLONG(xdrs, longp)			\
1307c478bd9Sstevel@tonic-gate 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
1317c478bd9Sstevel@tonic-gate #define	xdr_getlong(xdrs, longp)			\
1327c478bd9Sstevel@tonic-gate 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate #define	XDR_PUTLONG(xdrs, longp)			\
1357c478bd9Sstevel@tonic-gate 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
1367c478bd9Sstevel@tonic-gate #define	xdr_putlong(xdrs, longp)			\
1377c478bd9Sstevel@tonic-gate 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate #define	XDR_GETBYTES(xdrs, addr, len)			\
1407c478bd9Sstevel@tonic-gate 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
1417c478bd9Sstevel@tonic-gate #define	xdr_getbytes(xdrs, addr, len)			\
1427c478bd9Sstevel@tonic-gate 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate #define	XDR_PUTBYTES(xdrs, addr, len)			\
1457c478bd9Sstevel@tonic-gate 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
1467c478bd9Sstevel@tonic-gate #define	xdr_putbytes(xdrs, addr, len)			\
1477c478bd9Sstevel@tonic-gate 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate #define	XDR_GETPOS(xdrs)				\
1507c478bd9Sstevel@tonic-gate 	(*(xdrs)->x_ops->x_getpostn)(xdrs)
1517c478bd9Sstevel@tonic-gate #define	xdr_getpos(xdrs)				\
1527c478bd9Sstevel@tonic-gate 	(*(xdrs)->x_ops->x_getpostn)(xdrs)
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate #define	XDR_SETPOS(xdrs, pos)				\
1557c478bd9Sstevel@tonic-gate 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
1567c478bd9Sstevel@tonic-gate #define	xdr_setpos(xdrs, pos)				\
1577c478bd9Sstevel@tonic-gate 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #define	XDR_INLINE(xdrs, len)				\
1607c478bd9Sstevel@tonic-gate 	(*(xdrs)->x_ops->x_inline)(xdrs, len)
1617c478bd9Sstevel@tonic-gate #define	xdr_inline(xdrs, len)				\
1627c478bd9Sstevel@tonic-gate 	(*(xdrs)->x_ops->x_inline)(xdrs, len)
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate #define	XDR_DESTROY(xdrs)				\
1657c478bd9Sstevel@tonic-gate 	(*(xdrs)->x_ops->x_destroy)(xdrs)
1667c478bd9Sstevel@tonic-gate #define	xdr_destroy(xdrs) XDR_DESTROY(xdrs)
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Support struct for discriminated unions.
1707c478bd9Sstevel@tonic-gate  * You create an array of xdrdiscrim structures, terminated with
1717c478bd9Sstevel@tonic-gate  * a entry with a null procedure pointer.  The xdr_union routine gets
1727c478bd9Sstevel@tonic-gate  * the discriminant value and then searches the array of structures
1737c478bd9Sstevel@tonic-gate  * for a matching value.  If a match is found the associated xdr routine
1747c478bd9Sstevel@tonic-gate  * is called to handle that part of the union.  If there is
1757c478bd9Sstevel@tonic-gate  * no match, then a default routine may be called.
1767c478bd9Sstevel@tonic-gate  * If there is no match and no default routine it is an error.
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate #define	NULL_xdrproc_t ((xdrproc_t)0)
1797c478bd9Sstevel@tonic-gate struct xdr_discrim {
1807c478bd9Sstevel@tonic-gate 	int	value;
1817c478bd9Sstevel@tonic-gate 	xdrproc_t proc;
1827c478bd9Sstevel@tonic-gate };
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * In-line routines for fast encode/decode of primitve data types.
1867c478bd9Sstevel@tonic-gate  * Caveat emptor: these use single memory cycles to get the
1877c478bd9Sstevel@tonic-gate  * data from the underlying buffer, and will fail to operate
1887c478bd9Sstevel@tonic-gate  * properly if the data is not aligned.  The standard way to use these
1897c478bd9Sstevel@tonic-gate  * is to say:
1907c478bd9Sstevel@tonic-gate  *	if ((buf = XDR_INLINE(xdrs, count)) == NULL)
1917c478bd9Sstevel@tonic-gate  *		return (FALSE);
1927c478bd9Sstevel@tonic-gate  *	<<< macro calls >>>
1937c478bd9Sstevel@tonic-gate  * where ``count'' is the number of bytes of data occupied
1947c478bd9Sstevel@tonic-gate  * by the primitive data types.
1957c478bd9Sstevel@tonic-gate  *
1967c478bd9Sstevel@tonic-gate  * N.B. and frozen for all time: each data type here uses 4 bytes
1977c478bd9Sstevel@tonic-gate  * of external representation.
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate #define	IXDR_GET_LONG(buf)		((long)ntohl((u_long)*(buf)++))
2007c478bd9Sstevel@tonic-gate #define	IXDR_PUT_LONG(buf, v)		(*(buf)++ = (long)htonl((u_long)v))
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate #define	IXDR_GET_BOOL(buf)		((bool_t)IXDR_GET_LONG(buf))
2037c478bd9Sstevel@tonic-gate #define	IXDR_GET_ENUM(buf, t)		((t)IXDR_GET_LONG(buf))
2047c478bd9Sstevel@tonic-gate #define	IXDR_GET_U_LONG(buf)		((u_long)IXDR_GET_LONG(buf))
2057c478bd9Sstevel@tonic-gate #define	IXDR_GET_SHORT(buf)		((short)IXDR_GET_LONG(buf))
2067c478bd9Sstevel@tonic-gate #define	IXDR_GET_U_SHORT(buf)		((u_short)IXDR_GET_LONG(buf))
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate #define	IXDR_PUT_BOOL(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
2097c478bd9Sstevel@tonic-gate #define	IXDR_PUT_ENUM(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
2107c478bd9Sstevel@tonic-gate #define	IXDR_PUT_U_LONG(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
2117c478bd9Sstevel@tonic-gate #define	IXDR_PUT_SHORT(buf, v)		IXDR_PUT_LONG((buf), ((long)(v)))
2127c478bd9Sstevel@tonic-gate #define	IXDR_PUT_U_SHORT(buf, v)	IXDR_PUT_LONG((buf), ((long)(v)))
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate  * These are the "generic" xdr routines.
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate extern bool_t	xdr_void();
2187c478bd9Sstevel@tonic-gate extern bool_t	xdr_int();
2197c478bd9Sstevel@tonic-gate extern bool_t	xdr_u_int();
2207c478bd9Sstevel@tonic-gate extern bool_t	xdr_long();
2217c478bd9Sstevel@tonic-gate extern bool_t	xdr_u_long();
2227c478bd9Sstevel@tonic-gate extern bool_t	xdr_short();
2237c478bd9Sstevel@tonic-gate extern bool_t	xdr_u_short();
2247c478bd9Sstevel@tonic-gate extern bool_t	xdr_bool();
2257c478bd9Sstevel@tonic-gate extern bool_t	xdr_enum();
2267c478bd9Sstevel@tonic-gate extern bool_t	xdr_array();
2277c478bd9Sstevel@tonic-gate extern bool_t	xdr_bytes();
2287c478bd9Sstevel@tonic-gate extern bool_t	xdr_opaque();
2297c478bd9Sstevel@tonic-gate extern bool_t	xdr_string();
2307c478bd9Sstevel@tonic-gate extern bool_t	xdr_union();
2317c478bd9Sstevel@tonic-gate extern void	xdr_free();
2327c478bd9Sstevel@tonic-gate extern bool_t	xdr_char();
2337c478bd9Sstevel@tonic-gate extern bool_t	xdr_u_char();
2347c478bd9Sstevel@tonic-gate extern bool_t	xdr_vector();
2357c478bd9Sstevel@tonic-gate extern bool_t	xdr_float();
2367c478bd9Sstevel@tonic-gate extern bool_t	xdr_double();
2377c478bd9Sstevel@tonic-gate extern bool_t	xdr_reference();
2387c478bd9Sstevel@tonic-gate extern bool_t	xdr_pointer();
2397c478bd9Sstevel@tonic-gate extern bool_t	xdr_wrapstring();
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate /*
2427c478bd9Sstevel@tonic-gate  * Common opaque bytes objects used by many rpc protocols;
2437c478bd9Sstevel@tonic-gate  * declared here due to commonality.
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate #define	MAX_NETOBJ_SZ 1024
2467c478bd9Sstevel@tonic-gate struct netobj {
2477c478bd9Sstevel@tonic-gate 	u_int	n_len;
2487c478bd9Sstevel@tonic-gate 	char	*n_bytes;
2497c478bd9Sstevel@tonic-gate };
2507c478bd9Sstevel@tonic-gate typedef struct netobj netobj;
2517c478bd9Sstevel@tonic-gate extern bool_t	xdr_netobj();
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * These are the public routines for the various implementations of
2557c478bd9Sstevel@tonic-gate  * xdr streams.
2567c478bd9Sstevel@tonic-gate  */
2577c478bd9Sstevel@tonic-gate extern void	xdrmem_create();	/* XDR using memory buffers */
2587c478bd9Sstevel@tonic-gate extern void	xdrstdio_create();	/* XDR using stdio library */
2597c478bd9Sstevel@tonic-gate extern void	xdrrec_create();	/* XDR pseudo records for tcp */
2607c478bd9Sstevel@tonic-gate extern bool_t	xdrrec_endofrecord();	/* make end of xdr record */
2617c478bd9Sstevel@tonic-gate extern int	xdrrec_readbytes();	/* like a read on a pipe */
2627c478bd9Sstevel@tonic-gate extern bool_t	xdrrec_skiprecord();	/* move to beginning of next record */
2637c478bd9Sstevel@tonic-gate extern bool_t	xdrrec_eof();		/* true if no more input */
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate #endif /* !_rpc_xdr_h */
266