| 1 | /*	$OpenBSD: xdr.h,v 1.14 2022/12/27 07:44:56 jmc Exp $	*/ |
| 2 | /*	$NetBSD: xdr.h,v 1.7 1995/04/29 05:28:06 cgd Exp $	*/ |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 2010, Oracle America, Inc. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are |
| 9 | * met: |
| 10 | * |
| 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following |
| 15 | * disclaimer in the documentation and/or other materials |
| 16 | * provided with the distribution. |
| 17 | * * Neither the name of the "Oracle America, Inc." nor the names of its |
| 18 | * contributors may be used to endorse or promote products derived |
| 19 | * from this software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 24 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 25 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 28 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 30 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 31 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | * |
| 34 | *	from: @(#)xdr.h 1.19 87/04/22 SMI |
| 35 | *	@(#)xdr.h	2.2 88/07/29 4.0 RPCSRC |
| 36 | */ |
| 37 | |
| 38 | /* |
| 39 | * xdr.h, External Data Representation Serialization Routines. |
| 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 particular implementation (e.g. see xdr_mem.c), |
| 98 | * and two private fields for the use of the particular implementation. |
| 99 | */ |
| 100 | typedef struct __rpc_xdr { |
| 101 | 	enum xdr_op	x_op;		/* operation; fast additional param */ |
| 102 | 	const struct xdr_ops { |
| 103 | 		/* get a long from underlying stream */ |
| 104 | 		bool_t	(*x_getlong)(struct __rpc_xdr *, long *); |
| 105 | 		/* put a long to " */ |
| 106 | 		bool_t	(*x_putlong)(struct __rpc_xdr *, long *); |
| 107 | 		/* get some bytes from " */ |
| 108 | 		bool_t	(*x_getbytes)(struct __rpc_xdr *, caddr_t, |
| 109 | 		 unsigned int); |
| 110 | 		/* put some bytes to " */ |
| 111 | 		bool_t	(*x_putbytes)(struct __rpc_xdr *, caddr_t, |
| 112 | 		 unsigned int); |
| 113 | 		/* returns bytes off from beginning */ |
| 114 | 		unsigned int	(*x_getpostn)(struct __rpc_xdr *); |
| 115 | 		/* lets you reposition the stream */ |
| 116 | 		bool_t (*x_setpostn)(struct __rpc_xdr *, unsigned int); |
| 117 | 		/* buf quick ptr to buffered data */ |
| 118 | 		int32_t	*(*x_inline)(struct __rpc_xdr *, unsigned int); |
| 119 | 		/* free privates of this xdr_stream */ |
| 120 | 		void	(*x_destroy)(struct __rpc_xdr *); |
| 121 | 		bool_t	(*x_control)(struct __rpc_xdr *, int, void *); |
| 122 | 	} *x_ops; |
| 123 | 	caddr_t 	x_public;	/* users' data */ |
| 124 | 	caddr_t		x_private;	/* pointer to private data */ |
| 125 | 	caddr_t 	x_base;		/* private used for position info */ |
| 126 | 	unsigned int	x_handy;	/* extra private word */ |
| 127 | } XDR; |
| 128 | |
| 129 | /* |
| 130 | * A xdrproc_t exists for each data type which is to be encoded or decoded. |
| 131 | * |
| 132 | * The second argument to the xdrproc_t is a pointer to an opaque pointer. |
| 133 | * The opaque pointer generally points to a structure of the data type |
| 134 | * to be decoded. If this pointer is 0, then the type routines should |
| 135 | * allocate dynamic storage of the appropriate size and return it. |
| 136 | * |
| 137 | * XXX can't actually prototype it, because some take three args!!! |
| 138 | */ |
| 139 | typedef	bool_t (*xdrproc_t)(/* XDR *, void *, unsigned int */); |
| 140 | |
| 141 | /* |
| 142 | * Operations defined on a XDR handle |
| 143 | * |
| 144 | * XDR		*xdrs; |
| 145 | * long		*longp; |
| 146 | * caddr_t	 addr; |
| 147 | * unsigned int	 len; |
| 148 | * unsigned int	 pos; |
| 149 | */ |
| 150 | #define XDR_GETLONG(xdrs, longp)			\ |
| 151 | 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp) |
| 152 | #define xdr_getlong(xdrs, longp)			\ |
| 153 | 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp) |
| 154 | |
| 155 | #define XDR_PUTLONG(xdrs, longp)			\ |
| 156 | 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp) |
| 157 | #define xdr_putlong(xdrs, longp)			\ |
| 158 | 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp) |
| 159 | |
| 160 | #define XDR_GETBYTES(xdrs, addr, len)			\ |
| 161 | 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len) |
| 162 | #define xdr_getbytes(xdrs, addr, len)			\ |
| 163 | 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len) |
| 164 | |
| 165 | #define XDR_PUTBYTES(xdrs, addr, len)			\ |
| 166 | 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len) |
| 167 | #define xdr_putbytes(xdrs, addr, len)			\ |
| 168 | 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len) |
| 169 | |
| 170 | #define XDR_GETPOS(xdrs)				\ |
| 171 | 	(*(xdrs)->x_ops->x_getpostn)(xdrs) |
| 172 | #define xdr_getpos(xdrs)				\ |
| 173 | 	(*(xdrs)->x_ops->x_getpostn)(xdrs) |
| 174 | |
| 175 | #define XDR_SETPOS(xdrs, pos)				\ |
| 176 | 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos) |
| 177 | #define xdr_setpos(xdrs, pos)				\ |
| 178 | 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos) |
| 179 | |
| 180 | #define	XDR_INLINE(xdrs, len)				\ |
| 181 | 	(*(xdrs)->x_ops->x_inline)(xdrs, len) |
| 182 | #define	xdr_inline(xdrs, len)				\ |
| 183 | 	(*(xdrs)->x_ops->x_inline)(xdrs, len) |
| 184 | |
| 185 | #define	XDR_DESTROY(xdrs)				\ |
| 186 | 	if ((xdrs)->x_ops->x_destroy) 			\ |
| 187 | 		(*(xdrs)->x_ops->x_destroy)(xdrs) |
| 188 | #define	xdr_destroy(xdrs)				\ |
| 189 | 	if ((xdrs)->x_ops->x_destroy) 			\ |
| 190 | 		(*(xdrs)->x_ops->x_destroy)(xdrs) |
| 191 | |
| 192 | /* |
| 193 | * Support struct for discriminated unions. |
| 194 | * You create an array of xdrdiscrim structures, terminated with |
| 195 | * a entry with a null procedure pointer. The xdr_union routine gets |
| 196 | * the discriminant value and then searches the array of structures |
| 197 | * for a matching value. If a match is found the associated xdr routine |
| 198 | * is called to handle that part of the union. If there is |
| 199 | * no match, then a default routine may be called. |
| 200 | * If there is no match and no default routine it is an error. |
| 201 | */ |
| 202 | #define NULL_xdrproc_t ((xdrproc_t)0) |
| 203 | struct xdr_discrim { |
| 204 | 	int	value; |
| 205 | 	xdrproc_t proc; |
| 206 | }; |
| 207 | |
| 208 | /* |
| 209 | * In-line routines for fast encode/decode of primitive data types. |
| 210 | * Caveat emptor: these use single memory cycles to get the |
| 211 | * data from the underlying buffer, and will fail to operate |
| 212 | * properly if the data is not aligned. The standard way to use these |
| 213 | * is to say: |
| 214 | *	if ((buf = XDR_INLINE(xdrs, count)) == NULL) |
| 215 | *		return (FALSE); |
| 216 | *	<<< macro calls >>> |
| 217 | * where ``count'' is the number of bytes of data occupied |
| 218 | * by the primitive data types. |
| 219 | * |
| 220 | * N.B. and frozen for all time: each data type here uses 4 bytes |
| 221 | * of external representation. |
| 222 | */ |
| 223 | #define IXDR_GET_LONG(buf)		((long)ntohl((unsigned long)*(buf)++)) |
| 224 | #define IXDR_PUT_LONG(buf, v)		(*(buf)++ = (long)htonl((unsigned long)v)) |
| 225 | |
| 226 | #define IXDR_GET_BOOL(buf)		((bool_t)IXDR_GET_LONG(buf)) |
| 227 | #define IXDR_GET_ENUM(buf, t)		((t)IXDR_GET_LONG(buf)) |
| 228 | #define IXDR_GET_U_LONG(buf)		((unsigned long)IXDR_GET_LONG(buf)) |
| 229 | #define IXDR_GET_SHORT(buf)		((short)IXDR_GET_LONG(buf)) |
| 230 | #define IXDR_GET_U_SHORT(buf)		((unsigned short)IXDR_GET_LONG(buf)) |
| 231 | |
| 232 | #define IXDR_PUT_BOOL(buf, v)		IXDR_PUT_LONG((buf), ((long)(v))) |
| 233 | #define IXDR_PUT_ENUM(buf, v)		IXDR_PUT_LONG((buf), ((long)(v))) |
| 234 | #define IXDR_PUT_U_LONG(buf, v)		IXDR_PUT_LONG((buf), ((long)(v))) |
| 235 | #define IXDR_PUT_SHORT(buf, v)		IXDR_PUT_LONG((buf), ((long)(v))) |
| 236 | #define IXDR_PUT_U_SHORT(buf, v)	IXDR_PUT_LONG((buf), ((long)(v))) |
| 237 | |
| 238 | /* |
| 239 | * These are the "generic" xdr routines. |
| 240 | */ |
| 241 | __BEGIN_DECLS |
| 242 | extern bool_t	xdr_void(void); |
| 243 | extern bool_t	xdr_int(XDR *, int *); |
| 244 | extern bool_t	xdr_u_int(XDR *, unsigned int *); |
| 245 | extern bool_t	xdr_long(XDR *, long *); |
| 246 | extern bool_t	xdr_u_long(XDR *, unsigned long *); |
| 247 | extern bool_t	xdr_short(XDR *, short *); |
| 248 | extern bool_t	xdr_u_short(XDR *, unsigned short *); |
| 249 | extern bool_t	xdr_int16_t(XDR *, int16_t *); |
| 250 | extern bool_t	xdr_u_int16_t(XDR *, u_int16_t *); |
| 251 | extern bool_t	xdr_int32_t(XDR *, int32_t *); |
| 252 | extern bool_t	xdr_u_int32_t(XDR *, u_int32_t *); |
| 253 | extern bool_t	xdr_int64_t(XDR *, int64_t *); |
| 254 | extern bool_t	xdr_u_int64_t(XDR *, u_int64_t *); |
| 255 | extern bool_t	xdr_bool(XDR *, bool_t *); |
| 256 | extern bool_t	xdr_enum(XDR *, enum_t *); |
| 257 | extern bool_t	xdr_array(XDR *, char **, unsigned int *, unsigned int, |
| 258 | unsigned int, xdrproc_t); |
| 259 | extern bool_t	xdr_bytes(XDR *, char **, unsigned int *, unsigned int); |
| 260 | extern bool_t	xdr_opaque(XDR *, caddr_t, unsigned int); |
| 261 | extern bool_t	xdr_string(XDR *, char **, unsigned int); |
| 262 | extern bool_t	xdr_union(XDR *, enum_t *, char *, struct xdr_discrim *, |
| 263 | xdrproc_t); |
| 264 | extern bool_t	xdr_char(XDR *, char *); |
| 265 | extern bool_t	xdr_u_char(XDR *, unsigned char *); |
| 266 | extern bool_t	xdr_vector(XDR *, char *, unsigned int, unsigned int, |
| 267 | xdrproc_t); |
| 268 | extern bool_t	xdr_float(XDR *, float *); |
| 269 | extern bool_t	xdr_double(XDR *, double *); |
| 270 | extern bool_t	xdr_reference(XDR *, caddr_t *, unsigned int, xdrproc_t); |
| 271 | extern bool_t	xdr_pointer(XDR *, caddr_t *, unsigned int, xdrproc_t); |
| 272 | extern bool_t	xdr_wrapstring(XDR *, char **); |
| 273 | extern void	xdr_free(xdrproc_t, char *); |
| 274 | __END_DECLS |
| 275 | |
| 276 | /* |
| 277 | * Common opaque bytes objects used by many rpc protocols; |
| 278 | * declared here due to commonality. |
| 279 | */ |
| 280 | #define MAX_NETOBJ_SZ 1024 |
| 281 | struct netobj { |
| 282 | 	unsigned int	 n_len; |
| 283 | 	char		*n_bytes; |
| 284 | }; |
| 285 | typedef struct netobj netobj; |
| 286 | extern bool_t xdr_netobj(XDR *, struct netobj *); |
| 287 | |
| 288 | /* |
| 289 | * These are the public routines for the various implementations of |
| 290 | * xdr streams. |
| 291 | */ |
| 292 | __BEGIN_DECLS |
| 293 | /* XDR using memory buffers */ |
| 294 | extern void xdrmem_create(XDR *, char *, unsigned int, enum xdr_op); |
| 295 | |
| 296 | #ifdef _STDIO_H_ |
| 297 | /* XDR using stdio library */ |
| 298 | extern void xdrstdio_create(XDR *, FILE *, enum xdr_op); |
| 299 | #endif |
| 300 | |
| 301 | /* XDR pseudo records for tcp */ |
| 302 | extern void xdrrec_create(XDR *, unsigned int, unsigned int, char *, |
| 303 | 			 int (*)(caddr_t, caddr_t, int), |
| 304 | 			 int (*)(caddr_t, caddr_t, int)); |
| 305 | |
| 306 | /* make end of xdr record */ |
| 307 | extern bool_t xdrrec_endofrecord(XDR *, int); |
| 308 | |
| 309 | /* move to beginning of next record */ |
| 310 | extern bool_t xdrrec_skiprecord(XDR *); |
| 311 | |
| 312 | /* true if no more input */ |
| 313 | extern bool_t xdrrec_eof(XDR *); |
| 314 | __END_DECLS |
| 315 | |
| 316 | #endif /* !_RPC_XDR_H */ |