xref: /freebsd-src/sys/contrib/openzfs/include/os/linux/spl/rpc/xdr.h (revision 1719886f6d08408b834d270c59ffcfd821c8f63a)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  *  Copyright (c) 2008 Sun Microsystems, Inc.
3eda14cbcSMatt Macy  *  Written by Ricardo Correia <Ricardo.M.Correia@Sun.COM>
4eda14cbcSMatt Macy  *
5eda14cbcSMatt Macy  *  This file is part of the SPL, Solaris Porting Layer.
6eda14cbcSMatt Macy  *
7eda14cbcSMatt Macy  *  The SPL is free software; you can redistribute it and/or modify it
8eda14cbcSMatt Macy  *  under the terms of the GNU General Public License as published by the
9eda14cbcSMatt Macy  *  Free Software Foundation; either version 2 of the License, or (at your
10eda14cbcSMatt Macy  *  option) any later version.
11eda14cbcSMatt Macy  *
12eda14cbcSMatt Macy  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
13eda14cbcSMatt Macy  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14eda14cbcSMatt Macy  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15eda14cbcSMatt Macy  *  for more details.
16eda14cbcSMatt Macy  *
17eda14cbcSMatt Macy  *  You should have received a copy of the GNU General Public License along
18eda14cbcSMatt Macy  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
19eda14cbcSMatt Macy  */
20eda14cbcSMatt Macy 
21eda14cbcSMatt Macy #ifndef _SPL_RPC_XDR_H
22eda14cbcSMatt Macy #define	_SPL_RPC_XDR_H
23eda14cbcSMatt Macy 
24eda14cbcSMatt Macy #include <sys/types.h>
25eda14cbcSMatt Macy 
26eda14cbcSMatt Macy /*
27eda14cbcSMatt Macy  * XDR enums and types.
28eda14cbcSMatt Macy  */
29eda14cbcSMatt Macy enum xdr_op {
30eda14cbcSMatt Macy 	XDR_ENCODE,
31eda14cbcSMatt Macy 	XDR_DECODE
32eda14cbcSMatt Macy };
33eda14cbcSMatt Macy 
34eda14cbcSMatt Macy struct xdr_ops;
35eda14cbcSMatt Macy 
36eda14cbcSMatt Macy typedef struct {
37*e92ffd9bSMartin Matuska 	const struct xdr_ops	*x_ops;
38*e92ffd9bSMartin Matuska 	    /* Let caller know xdrmem_create() succeeds */
39eda14cbcSMatt Macy 	caddr_t		x_addr;	/* Current buffer addr */
40eda14cbcSMatt Macy 	caddr_t		x_addr_end;	/* End of the buffer */
41eda14cbcSMatt Macy 	enum xdr_op	x_op;	/* Stream direction */
42eda14cbcSMatt Macy } XDR;
43eda14cbcSMatt Macy 
44eda14cbcSMatt Macy typedef bool_t (*xdrproc_t)(XDR *xdrs, void *ptr);
45eda14cbcSMatt Macy 
46eda14cbcSMatt Macy struct xdr_ops {
47eda14cbcSMatt Macy 	bool_t (*xdr_control)(XDR *, int, void *);
48eda14cbcSMatt Macy 
49eda14cbcSMatt Macy 	bool_t (*xdr_char)(XDR *, char *);
50eda14cbcSMatt Macy 	bool_t (*xdr_u_short)(XDR *, unsigned short *);
51eda14cbcSMatt Macy 	bool_t (*xdr_u_int)(XDR *, unsigned *);
52eda14cbcSMatt Macy 	bool_t (*xdr_u_longlong_t)(XDR *, u_longlong_t *);
53eda14cbcSMatt Macy 
54eda14cbcSMatt Macy 	bool_t (*xdr_opaque)(XDR *, caddr_t, const uint_t);
55eda14cbcSMatt Macy 	bool_t (*xdr_string)(XDR *, char **, const uint_t);
56eda14cbcSMatt Macy 	bool_t (*xdr_array)(XDR *, caddr_t *, uint_t *, const uint_t,
57eda14cbcSMatt Macy 	    const uint_t, const xdrproc_t);
58eda14cbcSMatt Macy };
59eda14cbcSMatt Macy 
60eda14cbcSMatt Macy /*
61eda14cbcSMatt Macy  * XDR control operator.
62eda14cbcSMatt Macy  */
63eda14cbcSMatt Macy #define	XDR_GET_BYTES_AVAIL 1
64eda14cbcSMatt Macy 
65eda14cbcSMatt Macy struct xdr_bytesrec {
66eda14cbcSMatt Macy 	bool_t xc_is_last_record;
67eda14cbcSMatt Macy 	size_t xc_num_avail;
68eda14cbcSMatt Macy };
69eda14cbcSMatt Macy 
70eda14cbcSMatt Macy /*
71eda14cbcSMatt Macy  * XDR functions.
72eda14cbcSMatt Macy  */
73eda14cbcSMatt Macy void xdrmem_create(XDR *xdrs, const caddr_t addr, const uint_t size,
74eda14cbcSMatt Macy     const enum xdr_op op);
75eda14cbcSMatt Macy 
76eda14cbcSMatt Macy #define	xdr_control(xdrs, req, info) \
77eda14cbcSMatt Macy 	(xdrs)->x_ops->xdr_control((xdrs), (req), (info))
78eda14cbcSMatt Macy 
79eda14cbcSMatt Macy /*
80eda14cbcSMatt Macy  * For precaution, the following are defined as static inlines instead of macros
81eda14cbcSMatt Macy  * to get some amount of type safety.
82eda14cbcSMatt Macy  *
83eda14cbcSMatt Macy  * Also, macros wouldn't work in the case where typecasting is done, because it
84eda14cbcSMatt Macy  * must be possible to reference the functions' addresses by these names.
85eda14cbcSMatt Macy  */
xdr_char(XDR * xdrs,char * cp)86eda14cbcSMatt Macy static inline bool_t xdr_char(XDR *xdrs, char *cp)
87eda14cbcSMatt Macy {
88eda14cbcSMatt Macy 	return (xdrs->x_ops->xdr_char(xdrs, cp));
89eda14cbcSMatt Macy }
90eda14cbcSMatt Macy 
xdr_u_short(XDR * xdrs,unsigned short * usp)91eda14cbcSMatt Macy static inline bool_t xdr_u_short(XDR *xdrs, unsigned short *usp)
92eda14cbcSMatt Macy {
93eda14cbcSMatt Macy 	return (xdrs->x_ops->xdr_u_short(xdrs, usp));
94eda14cbcSMatt Macy }
95eda14cbcSMatt Macy 
xdr_short(XDR * xdrs,short * sp)96eda14cbcSMatt Macy static inline bool_t xdr_short(XDR *xdrs, short *sp)
97eda14cbcSMatt Macy {
98eda14cbcSMatt Macy 	BUILD_BUG_ON(sizeof (short) != 2);
99eda14cbcSMatt Macy 	return (xdrs->x_ops->xdr_u_short(xdrs, (unsigned short *) sp));
100eda14cbcSMatt Macy }
101eda14cbcSMatt Macy 
xdr_u_int(XDR * xdrs,unsigned * up)102eda14cbcSMatt Macy static inline bool_t xdr_u_int(XDR *xdrs, unsigned *up)
103eda14cbcSMatt Macy {
104eda14cbcSMatt Macy 	return (xdrs->x_ops->xdr_u_int(xdrs, up));
105eda14cbcSMatt Macy }
106eda14cbcSMatt Macy 
xdr_int(XDR * xdrs,int * ip)107eda14cbcSMatt Macy static inline bool_t xdr_int(XDR *xdrs, int *ip)
108eda14cbcSMatt Macy {
109eda14cbcSMatt Macy 	BUILD_BUG_ON(sizeof (int) != 4);
110eda14cbcSMatt Macy 	return (xdrs->x_ops->xdr_u_int(xdrs, (unsigned *)ip));
111eda14cbcSMatt Macy }
112eda14cbcSMatt Macy 
xdr_u_longlong_t(XDR * xdrs,u_longlong_t * ullp)113eda14cbcSMatt Macy static inline bool_t xdr_u_longlong_t(XDR *xdrs, u_longlong_t *ullp)
114eda14cbcSMatt Macy {
115eda14cbcSMatt Macy 	return (xdrs->x_ops->xdr_u_longlong_t(xdrs, ullp));
116eda14cbcSMatt Macy }
117eda14cbcSMatt Macy 
xdr_longlong_t(XDR * xdrs,longlong_t * llp)118eda14cbcSMatt Macy static inline bool_t xdr_longlong_t(XDR *xdrs, longlong_t *llp)
119eda14cbcSMatt Macy {
120eda14cbcSMatt Macy 	BUILD_BUG_ON(sizeof (longlong_t) != 8);
121eda14cbcSMatt Macy 	return (xdrs->x_ops->xdr_u_longlong_t(xdrs, (u_longlong_t *)llp));
122eda14cbcSMatt Macy }
123eda14cbcSMatt Macy 
124eda14cbcSMatt Macy /*
125eda14cbcSMatt Macy  * Fixed-length opaque data.
126eda14cbcSMatt Macy  */
xdr_opaque(XDR * xdrs,caddr_t cp,const uint_t cnt)127eda14cbcSMatt Macy static inline bool_t xdr_opaque(XDR *xdrs, caddr_t cp, const uint_t cnt)
128eda14cbcSMatt Macy {
129eda14cbcSMatt Macy 	return (xdrs->x_ops->xdr_opaque(xdrs, cp, cnt));
130eda14cbcSMatt Macy }
131eda14cbcSMatt Macy 
132eda14cbcSMatt Macy /*
133eda14cbcSMatt Macy  * Variable-length string.
134eda14cbcSMatt Macy  * The *sp buffer must have (maxsize + 1) bytes.
135eda14cbcSMatt Macy  */
xdr_string(XDR * xdrs,char ** sp,const uint_t maxsize)136eda14cbcSMatt Macy static inline bool_t xdr_string(XDR *xdrs, char **sp, const uint_t maxsize)
137eda14cbcSMatt Macy {
138eda14cbcSMatt Macy 	return (xdrs->x_ops->xdr_string(xdrs, sp, maxsize));
139eda14cbcSMatt Macy }
140eda14cbcSMatt Macy 
141eda14cbcSMatt Macy /*
142eda14cbcSMatt Macy  * Variable-length arrays.
143eda14cbcSMatt Macy  */
xdr_array(XDR * xdrs,caddr_t * arrp,uint_t * sizep,const uint_t maxsize,const uint_t elsize,const xdrproc_t elproc)144eda14cbcSMatt Macy static inline bool_t xdr_array(XDR *xdrs, caddr_t *arrp, uint_t *sizep,
145eda14cbcSMatt Macy     const uint_t maxsize, const uint_t elsize, const xdrproc_t elproc)
146eda14cbcSMatt Macy {
147eda14cbcSMatt Macy 	return xdrs->x_ops->xdr_array(xdrs, arrp, sizep, maxsize, elsize,
148eda14cbcSMatt Macy 	    elproc);
149eda14cbcSMatt Macy }
150eda14cbcSMatt Macy 
151eda14cbcSMatt Macy #endif /* SPL_RPC_XDR_H */
152