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