xref: /freebsd/lib/libc/xdr/xdr.c (revision 1ad08a09e9732e9e6e44f069c55f4abe27ed392a)
1eae561b3SGarrett Wollman /*
2eae561b3SGarrett Wollman  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3eae561b3SGarrett Wollman  * unrestricted use provided that this legend is included on all tape
4eae561b3SGarrett Wollman  * media and as a part of the software program in whole or part.  Users
5eae561b3SGarrett Wollman  * may copy or modify Sun RPC without charge, but are not authorized
6eae561b3SGarrett Wollman  * to license or distribute it to anyone else except as part of a product or
7eae561b3SGarrett Wollman  * program developed by the user.
8eae561b3SGarrett Wollman  *
9eae561b3SGarrett Wollman  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10eae561b3SGarrett Wollman  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11eae561b3SGarrett Wollman  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12eae561b3SGarrett Wollman  *
13eae561b3SGarrett Wollman  * Sun RPC is provided with no support and without any obligation on the
14eae561b3SGarrett Wollman  * part of Sun Microsystems, Inc. to assist in its use, correction,
15eae561b3SGarrett Wollman  * modification or enhancement.
16eae561b3SGarrett Wollman  *
17eae561b3SGarrett Wollman  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18eae561b3SGarrett Wollman  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19eae561b3SGarrett Wollman  * OR ANY PART THEREOF.
20eae561b3SGarrett Wollman  *
21eae561b3SGarrett Wollman  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22eae561b3SGarrett Wollman  * or profits or other special, indirect and consequential damages, even if
23eae561b3SGarrett Wollman  * Sun has been advised of the possibility of such damages.
24eae561b3SGarrett Wollman  *
25eae561b3SGarrett Wollman  * Sun Microsystems, Inc.
26eae561b3SGarrett Wollman  * 2550 Garcia Avenue
27eae561b3SGarrett Wollman  * Mountain View, California  94043
28eae561b3SGarrett Wollman  */
29eae561b3SGarrett Wollman 
30eae561b3SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint)
31eae561b3SGarrett Wollman /*static char *sccsid = "from: @(#)xdr.c 1.35 87/08/12";*/
32eae561b3SGarrett Wollman /*static char *sccsid = "from: @(#)xdr.c	2.1 88/07/29 4.0 RPCSRC";*/
331ad08a09SPeter Wemm static char *rcsid = "$Id: xdr.c,v 1.3 1995/10/22 14:53:51 phk Exp $";
34eae561b3SGarrett Wollman #endif
35eae561b3SGarrett Wollman 
36eae561b3SGarrett Wollman /*
37eae561b3SGarrett Wollman  * xdr.c, Generic XDR routines implementation.
38eae561b3SGarrett Wollman  *
39eae561b3SGarrett Wollman  * Copyright (C) 1986, Sun Microsystems, Inc.
40eae561b3SGarrett Wollman  *
41eae561b3SGarrett Wollman  * These are the "generic" xdr routines used to serialize and de-serialize
42eae561b3SGarrett Wollman  * most common data items.  See xdr.h for more info on the interface to
43eae561b3SGarrett Wollman  * xdr.
44eae561b3SGarrett Wollman  */
45eae561b3SGarrett Wollman 
46eae561b3SGarrett Wollman #include <stdio.h>
4729285d6cSPoul-Henning Kamp #include <stdlib.h>
481ad08a09SPeter Wemm #include <string.h>
49eae561b3SGarrett Wollman 
50eae561b3SGarrett Wollman #include <rpc/types.h>
51eae561b3SGarrett Wollman #include <rpc/xdr.h>
52eae561b3SGarrett Wollman 
53eae561b3SGarrett Wollman /*
54eae561b3SGarrett Wollman  * constants specific to the xdr "protocol"
55eae561b3SGarrett Wollman  */
56eae561b3SGarrett Wollman #define XDR_FALSE	((long) 0)
57eae561b3SGarrett Wollman #define XDR_TRUE	((long) 1)
58eae561b3SGarrett Wollman #define LASTUNSIGNED	((u_int) 0-1)
59eae561b3SGarrett Wollman 
60eae561b3SGarrett Wollman /*
61eae561b3SGarrett Wollman  * for unit alignment
62eae561b3SGarrett Wollman  */
63eae561b3SGarrett Wollman static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
64eae561b3SGarrett Wollman 
65eae561b3SGarrett Wollman /*
66eae561b3SGarrett Wollman  * Free a data structure using XDR
67eae561b3SGarrett Wollman  * Not a filter, but a convenient utility nonetheless
68eae561b3SGarrett Wollman  */
69eae561b3SGarrett Wollman void
70eae561b3SGarrett Wollman xdr_free(proc, objp)
71eae561b3SGarrett Wollman 	xdrproc_t proc;
72eae561b3SGarrett Wollman 	char *objp;
73eae561b3SGarrett Wollman {
74eae561b3SGarrett Wollman 	XDR x;
75eae561b3SGarrett Wollman 
76eae561b3SGarrett Wollman 	x.x_op = XDR_FREE;
77eae561b3SGarrett Wollman 	(*proc)(&x, objp);
78eae561b3SGarrett Wollman }
79eae561b3SGarrett Wollman 
80eae561b3SGarrett Wollman /*
81eae561b3SGarrett Wollman  * XDR nothing
82eae561b3SGarrett Wollman  */
83eae561b3SGarrett Wollman bool_t
84eae561b3SGarrett Wollman xdr_void(/* xdrs, addr */)
85eae561b3SGarrett Wollman 	/* XDR *xdrs; */
86eae561b3SGarrett Wollman 	/* caddr_t addr; */
87eae561b3SGarrett Wollman {
88eae561b3SGarrett Wollman 
89eae561b3SGarrett Wollman 	return (TRUE);
90eae561b3SGarrett Wollman }
91eae561b3SGarrett Wollman 
921ad08a09SPeter Wemm 
93eae561b3SGarrett Wollman /*
94eae561b3SGarrett Wollman  * XDR integers
95eae561b3SGarrett Wollman  */
96eae561b3SGarrett Wollman bool_t
97eae561b3SGarrett Wollman xdr_int(xdrs, ip)
98eae561b3SGarrett Wollman 	XDR *xdrs;
99eae561b3SGarrett Wollman 	int *ip;
100eae561b3SGarrett Wollman {
1011ad08a09SPeter Wemm 	long l;
102eae561b3SGarrett Wollman 
1031ad08a09SPeter Wemm 	switch (xdrs->x_op) {
1041ad08a09SPeter Wemm 
1051ad08a09SPeter Wemm 	case XDR_ENCODE:
1061ad08a09SPeter Wemm 		l = (long) *ip;
1071ad08a09SPeter Wemm 		return (XDR_PUTLONG(xdrs, &l));
1081ad08a09SPeter Wemm 
1091ad08a09SPeter Wemm 	case XDR_DECODE:
1101ad08a09SPeter Wemm 		if (!XDR_GETLONG(xdrs, &l)) {
1111ad08a09SPeter Wemm 			return (FALSE);
112eae561b3SGarrett Wollman 		}
1131ad08a09SPeter Wemm 		*ip = (int) l;
1141ad08a09SPeter Wemm 		return (TRUE);
1151ad08a09SPeter Wemm 
1161ad08a09SPeter Wemm 	case XDR_FREE:
1171ad08a09SPeter Wemm 		return (TRUE);
1181ad08a09SPeter Wemm 	}
1191ad08a09SPeter Wemm 	return (FALSE);
120eae561b3SGarrett Wollman }
121eae561b3SGarrett Wollman 
122eae561b3SGarrett Wollman /*
123eae561b3SGarrett Wollman  * XDR unsigned integers
124eae561b3SGarrett Wollman  */
125eae561b3SGarrett Wollman bool_t
126eae561b3SGarrett Wollman xdr_u_int(xdrs, up)
127eae561b3SGarrett Wollman 	XDR *xdrs;
128eae561b3SGarrett Wollman 	u_int *up;
129eae561b3SGarrett Wollman {
1301ad08a09SPeter Wemm 	u_long l;
131eae561b3SGarrett Wollman 
1321ad08a09SPeter Wemm 	switch (xdrs->x_op) {
1331ad08a09SPeter Wemm 
1341ad08a09SPeter Wemm 	case XDR_ENCODE:
1351ad08a09SPeter Wemm 		l = (u_long) *up;
1361ad08a09SPeter Wemm 		return (XDR_PUTLONG(xdrs, (long *)&l));
1371ad08a09SPeter Wemm 
1381ad08a09SPeter Wemm 	case XDR_DECODE:
1391ad08a09SPeter Wemm 		if (!XDR_GETLONG(xdrs, (long *)&l)) {
1401ad08a09SPeter Wemm 			return (FALSE);
141eae561b3SGarrett Wollman 		}
1421ad08a09SPeter Wemm 		*up = (u_int) l;
1431ad08a09SPeter Wemm 		return (TRUE);
1441ad08a09SPeter Wemm 
1451ad08a09SPeter Wemm 	case XDR_FREE:
1461ad08a09SPeter Wemm 		return (TRUE);
147eae561b3SGarrett Wollman 	}
1481ad08a09SPeter Wemm 	return (FALSE);
1491ad08a09SPeter Wemm }
1501ad08a09SPeter Wemm 
151eae561b3SGarrett Wollman 
152eae561b3SGarrett Wollman /*
153eae561b3SGarrett Wollman  * XDR long integers
154eae561b3SGarrett Wollman  * same as xdr_u_long - open coded to save a proc call!
155eae561b3SGarrett Wollman  */
156eae561b3SGarrett Wollman bool_t
157eae561b3SGarrett Wollman xdr_long(xdrs, lp)
158eae561b3SGarrett Wollman 	register XDR *xdrs;
159eae561b3SGarrett Wollman 	long *lp;
160eae561b3SGarrett Wollman {
1611ad08a09SPeter Wemm 	switch (xdrs->x_op) {
1621ad08a09SPeter Wemm 	case XDR_ENCODE:
163eae561b3SGarrett Wollman 		return (XDR_PUTLONG(xdrs, lp));
1641ad08a09SPeter Wemm 	case XDR_DECODE:
165eae561b3SGarrett Wollman 		return (XDR_GETLONG(xdrs, lp));
1661ad08a09SPeter Wemm 	case XDR_FREE:
167eae561b3SGarrett Wollman 		return (TRUE);
1681ad08a09SPeter Wemm 	}
169eae561b3SGarrett Wollman 
170eae561b3SGarrett Wollman 	return (FALSE);
171eae561b3SGarrett Wollman }
172eae561b3SGarrett Wollman 
173eae561b3SGarrett Wollman /*
174eae561b3SGarrett Wollman  * XDR unsigned long integers
175eae561b3SGarrett Wollman  * same as xdr_long - open coded to save a proc call!
176eae561b3SGarrett Wollman  */
177eae561b3SGarrett Wollman bool_t
178eae561b3SGarrett Wollman xdr_u_long(xdrs, ulp)
179eae561b3SGarrett Wollman 	register XDR *xdrs;
180eae561b3SGarrett Wollman 	u_long *ulp;
181eae561b3SGarrett Wollman {
1821ad08a09SPeter Wemm 	switch (xdrs->x_op) {
1831ad08a09SPeter Wemm 	case XDR_ENCODE:
184eae561b3SGarrett Wollman 		return (XDR_PUTLONG(xdrs, (long *)ulp));
1851ad08a09SPeter Wemm 	case XDR_DECODE:
1861ad08a09SPeter Wemm 		return (XDR_GETLONG(xdrs, (long *)ulp));
1871ad08a09SPeter Wemm 	case XDR_FREE:
188eae561b3SGarrett Wollman 		return (TRUE);
1891ad08a09SPeter Wemm 	}
190eae561b3SGarrett Wollman 	return (FALSE);
191eae561b3SGarrett Wollman }
192eae561b3SGarrett Wollman 
1931ad08a09SPeter Wemm 
1941ad08a09SPeter Wemm /*
1951ad08a09SPeter Wemm  * XDR 32-bit integers
1961ad08a09SPeter Wemm  * same as xdr_u_int32_t - open coded to save a proc call!
1971ad08a09SPeter Wemm  */
1981ad08a09SPeter Wemm bool_t
1991ad08a09SPeter Wemm xdr_int32_t(xdrs, int32_p)
2001ad08a09SPeter Wemm 	register XDR *xdrs;
2011ad08a09SPeter Wemm 	int32_t *int32_p;
2021ad08a09SPeter Wemm {
2031ad08a09SPeter Wemm 	long l;
2041ad08a09SPeter Wemm 
2051ad08a09SPeter Wemm 	switch (xdrs->x_op) {
2061ad08a09SPeter Wemm 
2071ad08a09SPeter Wemm 	case XDR_ENCODE:
2081ad08a09SPeter Wemm 		l = (long) *int32_p;
2091ad08a09SPeter Wemm 		return (XDR_PUTLONG(xdrs, &l));
2101ad08a09SPeter Wemm 
2111ad08a09SPeter Wemm 	case XDR_DECODE:
2121ad08a09SPeter Wemm 		if (!XDR_GETLONG(xdrs, &l)) {
2131ad08a09SPeter Wemm 			return (FALSE);
2141ad08a09SPeter Wemm 		}
2151ad08a09SPeter Wemm 		*int32_p = (int32_t) l;
2161ad08a09SPeter Wemm 		return (TRUE);
2171ad08a09SPeter Wemm 
2181ad08a09SPeter Wemm 	case XDR_FREE:
2191ad08a09SPeter Wemm 		return (TRUE);
2201ad08a09SPeter Wemm 	}
2211ad08a09SPeter Wemm 	return (FALSE);
2221ad08a09SPeter Wemm }
2231ad08a09SPeter Wemm 
2241ad08a09SPeter Wemm /*
2251ad08a09SPeter Wemm  * XDR unsigned 32-bit integers
2261ad08a09SPeter Wemm  * same as xdr_int32_t - open coded to save a proc call!
2271ad08a09SPeter Wemm  */
2281ad08a09SPeter Wemm bool_t
2291ad08a09SPeter Wemm xdr_u_int32_t(xdrs, u_int32_p)
2301ad08a09SPeter Wemm 	register XDR *xdrs;
2311ad08a09SPeter Wemm 	u_int32_t *u_int32_p;
2321ad08a09SPeter Wemm {
2331ad08a09SPeter Wemm 	u_long l;
2341ad08a09SPeter Wemm 
2351ad08a09SPeter Wemm 	switch (xdrs->x_op) {
2361ad08a09SPeter Wemm 
2371ad08a09SPeter Wemm 	case XDR_ENCODE:
2381ad08a09SPeter Wemm 		l = (u_long) *u_int32_p;
2391ad08a09SPeter Wemm 		return (XDR_PUTLONG(xdrs, (long *)&l));
2401ad08a09SPeter Wemm 
2411ad08a09SPeter Wemm 	case XDR_DECODE:
2421ad08a09SPeter Wemm 		if (!XDR_GETLONG(xdrs, (long *)&l)) {
2431ad08a09SPeter Wemm 			return (FALSE);
2441ad08a09SPeter Wemm 		}
2451ad08a09SPeter Wemm 		*u_int32_p = (u_int32_t) l;
2461ad08a09SPeter Wemm 		return (TRUE);
2471ad08a09SPeter Wemm 
2481ad08a09SPeter Wemm 	case XDR_FREE:
2491ad08a09SPeter Wemm 		return (TRUE);
2501ad08a09SPeter Wemm 	}
2511ad08a09SPeter Wemm 	return (FALSE);
2521ad08a09SPeter Wemm }
2531ad08a09SPeter Wemm 
2541ad08a09SPeter Wemm 
255eae561b3SGarrett Wollman /*
256eae561b3SGarrett Wollman  * XDR short integers
257eae561b3SGarrett Wollman  */
258eae561b3SGarrett Wollman bool_t
259eae561b3SGarrett Wollman xdr_short(xdrs, sp)
260eae561b3SGarrett Wollman 	register XDR *xdrs;
261eae561b3SGarrett Wollman 	short *sp;
262eae561b3SGarrett Wollman {
263eae561b3SGarrett Wollman 	long l;
264eae561b3SGarrett Wollman 
265eae561b3SGarrett Wollman 	switch (xdrs->x_op) {
266eae561b3SGarrett Wollman 
267eae561b3SGarrett Wollman 	case XDR_ENCODE:
268eae561b3SGarrett Wollman 		l = (long) *sp;
269eae561b3SGarrett Wollman 		return (XDR_PUTLONG(xdrs, &l));
270eae561b3SGarrett Wollman 
271eae561b3SGarrett Wollman 	case XDR_DECODE:
272eae561b3SGarrett Wollman 		if (!XDR_GETLONG(xdrs, &l)) {
273eae561b3SGarrett Wollman 			return (FALSE);
274eae561b3SGarrett Wollman 		}
275eae561b3SGarrett Wollman 		*sp = (short) l;
276eae561b3SGarrett Wollman 		return (TRUE);
277eae561b3SGarrett Wollman 
278eae561b3SGarrett Wollman 	case XDR_FREE:
279eae561b3SGarrett Wollman 		return (TRUE);
280eae561b3SGarrett Wollman 	}
281eae561b3SGarrett Wollman 	return (FALSE);
282eae561b3SGarrett Wollman }
283eae561b3SGarrett Wollman 
284eae561b3SGarrett Wollman /*
285eae561b3SGarrett Wollman  * XDR unsigned short integers
286eae561b3SGarrett Wollman  */
287eae561b3SGarrett Wollman bool_t
288eae561b3SGarrett Wollman xdr_u_short(xdrs, usp)
289eae561b3SGarrett Wollman 	register XDR *xdrs;
290eae561b3SGarrett Wollman 	u_short *usp;
291eae561b3SGarrett Wollman {
292eae561b3SGarrett Wollman 	u_long l;
293eae561b3SGarrett Wollman 
294eae561b3SGarrett Wollman 	switch (xdrs->x_op) {
295eae561b3SGarrett Wollman 
296eae561b3SGarrett Wollman 	case XDR_ENCODE:
297eae561b3SGarrett Wollman 		l = (u_long) *usp;
2981ad08a09SPeter Wemm 		return (XDR_PUTLONG(xdrs, (long *)&l));
2991ad08a09SPeter Wemm 
3001ad08a09SPeter Wemm 	case XDR_DECODE:
3011ad08a09SPeter Wemm 		if (!XDR_GETLONG(xdrs, (long *)&l)) {
3021ad08a09SPeter Wemm 			return (FALSE);
3031ad08a09SPeter Wemm 		}
3041ad08a09SPeter Wemm 		*usp = (u_short) l;
3051ad08a09SPeter Wemm 		return (TRUE);
3061ad08a09SPeter Wemm 
3071ad08a09SPeter Wemm 	case XDR_FREE:
3081ad08a09SPeter Wemm 		return (TRUE);
3091ad08a09SPeter Wemm 	}
3101ad08a09SPeter Wemm 	return (FALSE);
3111ad08a09SPeter Wemm }
3121ad08a09SPeter Wemm 
3131ad08a09SPeter Wemm 
3141ad08a09SPeter Wemm /*
3151ad08a09SPeter Wemm  * XDR 16-bit integers
3161ad08a09SPeter Wemm  */
3171ad08a09SPeter Wemm bool_t
3181ad08a09SPeter Wemm xdr_int16_t(xdrs, int16_p)
3191ad08a09SPeter Wemm 	register XDR *xdrs;
3201ad08a09SPeter Wemm 	int16_t *int16_p;
3211ad08a09SPeter Wemm {
3221ad08a09SPeter Wemm 	long l;
3231ad08a09SPeter Wemm 
3241ad08a09SPeter Wemm 	switch (xdrs->x_op) {
3251ad08a09SPeter Wemm 
3261ad08a09SPeter Wemm 	case XDR_ENCODE:
3271ad08a09SPeter Wemm 		l = (long) *int16_p;
328eae561b3SGarrett Wollman 		return (XDR_PUTLONG(xdrs, &l));
329eae561b3SGarrett Wollman 
330eae561b3SGarrett Wollman 	case XDR_DECODE:
331eae561b3SGarrett Wollman 		if (!XDR_GETLONG(xdrs, &l)) {
332eae561b3SGarrett Wollman 			return (FALSE);
333eae561b3SGarrett Wollman 		}
3341ad08a09SPeter Wemm 		*int16_p = (int16_t) l;
3351ad08a09SPeter Wemm 		return (TRUE);
3361ad08a09SPeter Wemm 
3371ad08a09SPeter Wemm 	case XDR_FREE:
3381ad08a09SPeter Wemm 		return (TRUE);
3391ad08a09SPeter Wemm 	}
3401ad08a09SPeter Wemm 	return (FALSE);
3411ad08a09SPeter Wemm }
3421ad08a09SPeter Wemm 
3431ad08a09SPeter Wemm /*
3441ad08a09SPeter Wemm  * XDR unsigned 16-bit integers
3451ad08a09SPeter Wemm  */
3461ad08a09SPeter Wemm bool_t
3471ad08a09SPeter Wemm xdr_u_int16_t(xdrs, u_int16_p)
3481ad08a09SPeter Wemm 	register XDR *xdrs;
3491ad08a09SPeter Wemm 	u_int16_t *u_int16_p;
3501ad08a09SPeter Wemm {
3511ad08a09SPeter Wemm 	u_long l;
3521ad08a09SPeter Wemm 
3531ad08a09SPeter Wemm 	switch (xdrs->x_op) {
3541ad08a09SPeter Wemm 
3551ad08a09SPeter Wemm 	case XDR_ENCODE:
3561ad08a09SPeter Wemm 		l = (u_long) *u_int16_p;
3571ad08a09SPeter Wemm 		return (XDR_PUTLONG(xdrs, (long *)&l));
3581ad08a09SPeter Wemm 
3591ad08a09SPeter Wemm 	case XDR_DECODE:
3601ad08a09SPeter Wemm 		if (!XDR_GETLONG(xdrs, (long *)&l)) {
3611ad08a09SPeter Wemm 			return (FALSE);
3621ad08a09SPeter Wemm 		}
3631ad08a09SPeter Wemm 		*u_int16_p = (u_int16_t) l;
364eae561b3SGarrett Wollman 		return (TRUE);
365eae561b3SGarrett Wollman 
366eae561b3SGarrett Wollman 	case XDR_FREE:
367eae561b3SGarrett Wollman 		return (TRUE);
368eae561b3SGarrett Wollman 	}
369eae561b3SGarrett Wollman 	return (FALSE);
370eae561b3SGarrett Wollman }
371eae561b3SGarrett Wollman 
372eae561b3SGarrett Wollman 
373eae561b3SGarrett Wollman /*
374eae561b3SGarrett Wollman  * XDR a char
375eae561b3SGarrett Wollman  */
376eae561b3SGarrett Wollman bool_t
377eae561b3SGarrett Wollman xdr_char(xdrs, cp)
378eae561b3SGarrett Wollman 	XDR *xdrs;
379eae561b3SGarrett Wollman 	char *cp;
380eae561b3SGarrett Wollman {
381eae561b3SGarrett Wollman 	int i;
382eae561b3SGarrett Wollman 
383eae561b3SGarrett Wollman 	i = (*cp);
384eae561b3SGarrett Wollman 	if (!xdr_int(xdrs, &i)) {
385eae561b3SGarrett Wollman 		return (FALSE);
386eae561b3SGarrett Wollman 	}
387eae561b3SGarrett Wollman 	*cp = i;
388eae561b3SGarrett Wollman 	return (TRUE);
389eae561b3SGarrett Wollman }
390eae561b3SGarrett Wollman 
391eae561b3SGarrett Wollman /*
392eae561b3SGarrett Wollman  * XDR an unsigned char
393eae561b3SGarrett Wollman  */
394eae561b3SGarrett Wollman bool_t
395eae561b3SGarrett Wollman xdr_u_char(xdrs, cp)
396eae561b3SGarrett Wollman 	XDR *xdrs;
3971ad08a09SPeter Wemm 	u_char *cp;
398eae561b3SGarrett Wollman {
399eae561b3SGarrett Wollman 	u_int u;
400eae561b3SGarrett Wollman 
401eae561b3SGarrett Wollman 	u = (*cp);
402eae561b3SGarrett Wollman 	if (!xdr_u_int(xdrs, &u)) {
403eae561b3SGarrett Wollman 		return (FALSE);
404eae561b3SGarrett Wollman 	}
405eae561b3SGarrett Wollman 	*cp = u;
406eae561b3SGarrett Wollman 	return (TRUE);
407eae561b3SGarrett Wollman }
408eae561b3SGarrett Wollman 
409eae561b3SGarrett Wollman /*
410eae561b3SGarrett Wollman  * XDR booleans
411eae561b3SGarrett Wollman  */
412eae561b3SGarrett Wollman bool_t
413eae561b3SGarrett Wollman xdr_bool(xdrs, bp)
414eae561b3SGarrett Wollman 	register XDR *xdrs;
415eae561b3SGarrett Wollman 	bool_t *bp;
416eae561b3SGarrett Wollman {
417eae561b3SGarrett Wollman 	long lb;
418eae561b3SGarrett Wollman 
419eae561b3SGarrett Wollman 	switch (xdrs->x_op) {
420eae561b3SGarrett Wollman 
421eae561b3SGarrett Wollman 	case XDR_ENCODE:
422eae561b3SGarrett Wollman 		lb = *bp ? XDR_TRUE : XDR_FALSE;
423eae561b3SGarrett Wollman 		return (XDR_PUTLONG(xdrs, &lb));
424eae561b3SGarrett Wollman 
425eae561b3SGarrett Wollman 	case XDR_DECODE:
426eae561b3SGarrett Wollman 		if (!XDR_GETLONG(xdrs, &lb)) {
427eae561b3SGarrett Wollman 			return (FALSE);
428eae561b3SGarrett Wollman 		}
429eae561b3SGarrett Wollman 		*bp = (lb == XDR_FALSE) ? FALSE : TRUE;
430eae561b3SGarrett Wollman 		return (TRUE);
431eae561b3SGarrett Wollman 
432eae561b3SGarrett Wollman 	case XDR_FREE:
433eae561b3SGarrett Wollman 		return (TRUE);
434eae561b3SGarrett Wollman 	}
435eae561b3SGarrett Wollman 	return (FALSE);
436eae561b3SGarrett Wollman }
437eae561b3SGarrett Wollman 
438eae561b3SGarrett Wollman /*
439eae561b3SGarrett Wollman  * XDR enumerations
440eae561b3SGarrett Wollman  */
441eae561b3SGarrett Wollman bool_t
442eae561b3SGarrett Wollman xdr_enum(xdrs, ep)
443eae561b3SGarrett Wollman 	XDR *xdrs;
444eae561b3SGarrett Wollman 	enum_t *ep;
445eae561b3SGarrett Wollman {
446eae561b3SGarrett Wollman #ifndef lint
447eae561b3SGarrett Wollman 	enum sizecheck { SIZEVAL };	/* used to find the size of an enum */
448eae561b3SGarrett Wollman 
449eae561b3SGarrett Wollman 	/*
450eae561b3SGarrett Wollman 	 * enums are treated as ints
451eae561b3SGarrett Wollman 	 */
452eae561b3SGarrett Wollman 	if (sizeof (enum sizecheck) == sizeof (long)) {
453eae561b3SGarrett Wollman 		return (xdr_long(xdrs, (long *)ep));
4541ad08a09SPeter Wemm 	} else if (sizeof (enum sizecheck) == sizeof (int)) {
4551ad08a09SPeter Wemm 		return (xdr_int(xdrs, (int *)ep));
456eae561b3SGarrett Wollman 	} else if (sizeof (enum sizecheck) == sizeof (short)) {
457eae561b3SGarrett Wollman 		return (xdr_short(xdrs, (short *)ep));
458eae561b3SGarrett Wollman 	} else {
459eae561b3SGarrett Wollman 		return (FALSE);
460eae561b3SGarrett Wollman 	}
461eae561b3SGarrett Wollman #else
462eae561b3SGarrett Wollman 	(void) (xdr_short(xdrs, (short *)ep));
4631ad08a09SPeter Wemm 	(void) (xdr_int(xdrs, (int *)ep));
464eae561b3SGarrett Wollman 	return (xdr_long(xdrs, (long *)ep));
465eae561b3SGarrett Wollman #endif
466eae561b3SGarrett Wollman }
467eae561b3SGarrett Wollman 
468eae561b3SGarrett Wollman /*
469eae561b3SGarrett Wollman  * XDR opaque data
470eae561b3SGarrett Wollman  * Allows the specification of a fixed size sequence of opaque bytes.
471eae561b3SGarrett Wollman  * cp points to the opaque object and cnt gives the byte length.
472eae561b3SGarrett Wollman  */
473eae561b3SGarrett Wollman bool_t
474eae561b3SGarrett Wollman xdr_opaque(xdrs, cp, cnt)
475eae561b3SGarrett Wollman 	register XDR *xdrs;
476eae561b3SGarrett Wollman 	caddr_t cp;
477eae561b3SGarrett Wollman 	register u_int cnt;
478eae561b3SGarrett Wollman {
479eae561b3SGarrett Wollman 	register u_int rndup;
480eae561b3SGarrett Wollman 	static crud[BYTES_PER_XDR_UNIT];
481eae561b3SGarrett Wollman 
482eae561b3SGarrett Wollman 	/*
483eae561b3SGarrett Wollman 	 * if no data we are done
484eae561b3SGarrett Wollman 	 */
485eae561b3SGarrett Wollman 	if (cnt == 0)
486eae561b3SGarrett Wollman 		return (TRUE);
487eae561b3SGarrett Wollman 
488eae561b3SGarrett Wollman 	/*
489eae561b3SGarrett Wollman 	 * round byte count to full xdr units
490eae561b3SGarrett Wollman 	 */
491eae561b3SGarrett Wollman 	rndup = cnt % BYTES_PER_XDR_UNIT;
492eae561b3SGarrett Wollman 	if (rndup > 0)
493eae561b3SGarrett Wollman 		rndup = BYTES_PER_XDR_UNIT - rndup;
494eae561b3SGarrett Wollman 
495eae561b3SGarrett Wollman 	if (xdrs->x_op == XDR_DECODE) {
496eae561b3SGarrett Wollman 		if (!XDR_GETBYTES(xdrs, cp, cnt)) {
497eae561b3SGarrett Wollman 			return (FALSE);
498eae561b3SGarrett Wollman 		}
499eae561b3SGarrett Wollman 		if (rndup == 0)
500eae561b3SGarrett Wollman 			return (TRUE);
5011ad08a09SPeter Wemm 		return (XDR_GETBYTES(xdrs, (caddr_t)crud, rndup));
502eae561b3SGarrett Wollman 	}
503eae561b3SGarrett Wollman 
504eae561b3SGarrett Wollman 	if (xdrs->x_op == XDR_ENCODE) {
505eae561b3SGarrett Wollman 		if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
506eae561b3SGarrett Wollman 			return (FALSE);
507eae561b3SGarrett Wollman 		}
508eae561b3SGarrett Wollman 		if (rndup == 0)
509eae561b3SGarrett Wollman 			return (TRUE);
510eae561b3SGarrett Wollman 		return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
511eae561b3SGarrett Wollman 	}
512eae561b3SGarrett Wollman 
513eae561b3SGarrett Wollman 	if (xdrs->x_op == XDR_FREE) {
514eae561b3SGarrett Wollman 		return (TRUE);
515eae561b3SGarrett Wollman 	}
516eae561b3SGarrett Wollman 
517eae561b3SGarrett Wollman 	return (FALSE);
518eae561b3SGarrett Wollman }
519eae561b3SGarrett Wollman 
520eae561b3SGarrett Wollman /*
521eae561b3SGarrett Wollman  * XDR counted bytes
522eae561b3SGarrett Wollman  * *cpp is a pointer to the bytes, *sizep is the count.
523eae561b3SGarrett Wollman  * If *cpp is NULL maxsize bytes are allocated
524eae561b3SGarrett Wollman  */
525eae561b3SGarrett Wollman bool_t
526eae561b3SGarrett Wollman xdr_bytes(xdrs, cpp, sizep, maxsize)
527eae561b3SGarrett Wollman 	register XDR *xdrs;
528eae561b3SGarrett Wollman 	char **cpp;
529eae561b3SGarrett Wollman 	register u_int *sizep;
530eae561b3SGarrett Wollman 	u_int maxsize;
531eae561b3SGarrett Wollman {
532eae561b3SGarrett Wollman 	register char *sp = *cpp;  /* sp is the actual string pointer */
533eae561b3SGarrett Wollman 	register u_int nodesize;
534eae561b3SGarrett Wollman 
535eae561b3SGarrett Wollman 	/*
536eae561b3SGarrett Wollman 	 * first deal with the length since xdr bytes are counted
537eae561b3SGarrett Wollman 	 */
538eae561b3SGarrett Wollman 	if (! xdr_u_int(xdrs, sizep)) {
539eae561b3SGarrett Wollman 		return (FALSE);
540eae561b3SGarrett Wollman 	}
541eae561b3SGarrett Wollman 	nodesize = *sizep;
542eae561b3SGarrett Wollman 	if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
543eae561b3SGarrett Wollman 		return (FALSE);
544eae561b3SGarrett Wollman 	}
545eae561b3SGarrett Wollman 
546eae561b3SGarrett Wollman 	/*
547eae561b3SGarrett Wollman 	 * now deal with the actual bytes
548eae561b3SGarrett Wollman 	 */
549eae561b3SGarrett Wollman 	switch (xdrs->x_op) {
550eae561b3SGarrett Wollman 
551eae561b3SGarrett Wollman 	case XDR_DECODE:
552eae561b3SGarrett Wollman 		if (nodesize == 0) {
553eae561b3SGarrett Wollman 			return (TRUE);
554eae561b3SGarrett Wollman 		}
555eae561b3SGarrett Wollman 		if (sp == NULL) {
556eae561b3SGarrett Wollman 			*cpp = sp = (char *)mem_alloc(nodesize);
557eae561b3SGarrett Wollman 		}
558eae561b3SGarrett Wollman 		if (sp == NULL) {
559eae561b3SGarrett Wollman 			(void) fprintf(stderr, "xdr_bytes: out of memory\n");
560eae561b3SGarrett Wollman 			return (FALSE);
561eae561b3SGarrett Wollman 		}
562eae561b3SGarrett Wollman 		/* fall into ... */
563eae561b3SGarrett Wollman 
564eae561b3SGarrett Wollman 	case XDR_ENCODE:
565eae561b3SGarrett Wollman 		return (xdr_opaque(xdrs, sp, nodesize));
566eae561b3SGarrett Wollman 
567eae561b3SGarrett Wollman 	case XDR_FREE:
568eae561b3SGarrett Wollman 		if (sp != NULL) {
569eae561b3SGarrett Wollman 			mem_free(sp, nodesize);
570eae561b3SGarrett Wollman 			*cpp = NULL;
571eae561b3SGarrett Wollman 		}
572eae561b3SGarrett Wollman 		return (TRUE);
573eae561b3SGarrett Wollman 	}
574eae561b3SGarrett Wollman 	return (FALSE);
575eae561b3SGarrett Wollman }
576eae561b3SGarrett Wollman 
577eae561b3SGarrett Wollman /*
578eae561b3SGarrett Wollman  * Implemented here due to commonality of the object.
579eae561b3SGarrett Wollman  */
580eae561b3SGarrett Wollman bool_t
581eae561b3SGarrett Wollman xdr_netobj(xdrs, np)
582eae561b3SGarrett Wollman 	XDR *xdrs;
583eae561b3SGarrett Wollman 	struct netobj *np;
584eae561b3SGarrett Wollman {
585eae561b3SGarrett Wollman 
586eae561b3SGarrett Wollman 	return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
587eae561b3SGarrett Wollman }
588eae561b3SGarrett Wollman 
589eae561b3SGarrett Wollman /*
590eae561b3SGarrett Wollman  * XDR a descriminated union
591eae561b3SGarrett Wollman  * Support routine for discriminated unions.
592eae561b3SGarrett Wollman  * You create an array of xdrdiscrim structures, terminated with
593eae561b3SGarrett Wollman  * an entry with a null procedure pointer.  The routine gets
594eae561b3SGarrett Wollman  * the discriminant value and then searches the array of xdrdiscrims
595eae561b3SGarrett Wollman  * looking for that value.  It calls the procedure given in the xdrdiscrim
596eae561b3SGarrett Wollman  * to handle the discriminant.  If there is no specific routine a default
597eae561b3SGarrett Wollman  * routine may be called.
598eae561b3SGarrett Wollman  * If there is no specific or default routine an error is returned.
599eae561b3SGarrett Wollman  */
600eae561b3SGarrett Wollman bool_t
601eae561b3SGarrett Wollman xdr_union(xdrs, dscmp, unp, choices, dfault)
602eae561b3SGarrett Wollman 	register XDR *xdrs;
603eae561b3SGarrett Wollman 	enum_t *dscmp;		/* enum to decide which arm to work on */
604eae561b3SGarrett Wollman 	char *unp;		/* the union itself */
605eae561b3SGarrett Wollman 	struct xdr_discrim *choices;	/* [value, xdr proc] for each arm */
606eae561b3SGarrett Wollman 	xdrproc_t dfault;	/* default xdr routine */
607eae561b3SGarrett Wollman {
608eae561b3SGarrett Wollman 	register enum_t dscm;
609eae561b3SGarrett Wollman 
610eae561b3SGarrett Wollman 	/*
611eae561b3SGarrett Wollman 	 * we deal with the discriminator;  it's an enum
612eae561b3SGarrett Wollman 	 */
613eae561b3SGarrett Wollman 	if (! xdr_enum(xdrs, dscmp)) {
614eae561b3SGarrett Wollman 		return (FALSE);
615eae561b3SGarrett Wollman 	}
616eae561b3SGarrett Wollman 	dscm = *dscmp;
617eae561b3SGarrett Wollman 
618eae561b3SGarrett Wollman 	/*
619eae561b3SGarrett Wollman 	 * search choices for a value that matches the discriminator.
620eae561b3SGarrett Wollman 	 * if we find one, execute the xdr routine for that value.
621eae561b3SGarrett Wollman 	 */
622eae561b3SGarrett Wollman 	for (; choices->proc != NULL_xdrproc_t; choices++) {
623eae561b3SGarrett Wollman 		if (choices->value == dscm)
624eae561b3SGarrett Wollman 			return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
625eae561b3SGarrett Wollman 	}
626eae561b3SGarrett Wollman 
627eae561b3SGarrett Wollman 	/*
628eae561b3SGarrett Wollman 	 * no match - execute the default xdr routine if there is one
629eae561b3SGarrett Wollman 	 */
630eae561b3SGarrett Wollman 	return ((dfault == NULL_xdrproc_t) ? FALSE :
631eae561b3SGarrett Wollman 	    (*dfault)(xdrs, unp, LASTUNSIGNED));
632eae561b3SGarrett Wollman }
633eae561b3SGarrett Wollman 
634eae561b3SGarrett Wollman 
635eae561b3SGarrett Wollman /*
636eae561b3SGarrett Wollman  * Non-portable xdr primitives.
637eae561b3SGarrett Wollman  * Care should be taken when moving these routines to new architectures.
638eae561b3SGarrett Wollman  */
639eae561b3SGarrett Wollman 
640eae561b3SGarrett Wollman 
641eae561b3SGarrett Wollman /*
642eae561b3SGarrett Wollman  * XDR null terminated ASCII strings
643eae561b3SGarrett Wollman  * xdr_string deals with "C strings" - arrays of bytes that are
644eae561b3SGarrett Wollman  * terminated by a NULL character.  The parameter cpp references a
645eae561b3SGarrett Wollman  * pointer to storage; If the pointer is null, then the necessary
646eae561b3SGarrett Wollman  * storage is allocated.  The last parameter is the max allowed length
647eae561b3SGarrett Wollman  * of the string as specified by a protocol.
648eae561b3SGarrett Wollman  */
649eae561b3SGarrett Wollman bool_t
650eae561b3SGarrett Wollman xdr_string(xdrs, cpp, maxsize)
651eae561b3SGarrett Wollman 	register XDR *xdrs;
652eae561b3SGarrett Wollman 	char **cpp;
653eae561b3SGarrett Wollman 	u_int maxsize;
654eae561b3SGarrett Wollman {
655eae561b3SGarrett Wollman 	register char *sp = *cpp;  /* sp is the actual string pointer */
656eae561b3SGarrett Wollman 	u_int size;
657eae561b3SGarrett Wollman 	u_int nodesize;
658eae561b3SGarrett Wollman 
659eae561b3SGarrett Wollman 	/*
660eae561b3SGarrett Wollman 	 * first deal with the length since xdr strings are counted-strings
661eae561b3SGarrett Wollman 	 */
662eae561b3SGarrett Wollman 	switch (xdrs->x_op) {
663eae561b3SGarrett Wollman 	case XDR_FREE:
664eae561b3SGarrett Wollman 		if (sp == NULL) {
665eae561b3SGarrett Wollman 			return(TRUE);	/* already free */
666eae561b3SGarrett Wollman 		}
667eae561b3SGarrett Wollman 		/* fall through... */
668eae561b3SGarrett Wollman 	case XDR_ENCODE:
669eae561b3SGarrett Wollman 		size = strlen(sp);
670eae561b3SGarrett Wollman 		break;
671eae561b3SGarrett Wollman 	}
672eae561b3SGarrett Wollman 	if (! xdr_u_int(xdrs, &size)) {
673eae561b3SGarrett Wollman 		return (FALSE);
674eae561b3SGarrett Wollman 	}
675eae561b3SGarrett Wollman 	if (size > maxsize) {
676eae561b3SGarrett Wollman 		return (FALSE);
677eae561b3SGarrett Wollman 	}
678eae561b3SGarrett Wollman 	nodesize = size + 1;
679eae561b3SGarrett Wollman 
680eae561b3SGarrett Wollman 	/*
681eae561b3SGarrett Wollman 	 * now deal with the actual bytes
682eae561b3SGarrett Wollman 	 */
683eae561b3SGarrett Wollman 	switch (xdrs->x_op) {
684eae561b3SGarrett Wollman 
685eae561b3SGarrett Wollman 	case XDR_DECODE:
686eae561b3SGarrett Wollman 		if (nodesize == 0) {
687eae561b3SGarrett Wollman 			return (TRUE);
688eae561b3SGarrett Wollman 		}
689eae561b3SGarrett Wollman 		if (sp == NULL)
690eae561b3SGarrett Wollman 			*cpp = sp = (char *)mem_alloc(nodesize);
691eae561b3SGarrett Wollman 		if (sp == NULL) {
692eae561b3SGarrett Wollman 			(void) fprintf(stderr, "xdr_string: out of memory\n");
693eae561b3SGarrett Wollman 			return (FALSE);
694eae561b3SGarrett Wollman 		}
695eae561b3SGarrett Wollman 		sp[size] = 0;
696eae561b3SGarrett Wollman 		/* fall into ... */
697eae561b3SGarrett Wollman 
698eae561b3SGarrett Wollman 	case XDR_ENCODE:
699eae561b3SGarrett Wollman 		return (xdr_opaque(xdrs, sp, size));
700eae561b3SGarrett Wollman 
701eae561b3SGarrett Wollman 	case XDR_FREE:
702eae561b3SGarrett Wollman 		mem_free(sp, nodesize);
703eae561b3SGarrett Wollman 		*cpp = NULL;
704eae561b3SGarrett Wollman 		return (TRUE);
705eae561b3SGarrett Wollman 	}
706eae561b3SGarrett Wollman 	return (FALSE);
707eae561b3SGarrett Wollman }
708eae561b3SGarrett Wollman 
709eae561b3SGarrett Wollman /*
710eae561b3SGarrett Wollman  * Wrapper for xdr_string that can be called directly from
711eae561b3SGarrett Wollman  * routines like clnt_call
712eae561b3SGarrett Wollman  */
713eae561b3SGarrett Wollman bool_t
714eae561b3SGarrett Wollman xdr_wrapstring(xdrs, cpp)
715eae561b3SGarrett Wollman 	XDR *xdrs;
716eae561b3SGarrett Wollman 	char **cpp;
717eae561b3SGarrett Wollman {
7181ad08a09SPeter Wemm 	return xdr_string(xdrs, cpp, LASTUNSIGNED);
719eae561b3SGarrett Wollman }
720