1177633Sdfr/*	$NetBSD: xdr_array.c,v 1.12 2000/01/22 22:19:18 mycroft Exp $	*/
2177633Sdfr
3177633Sdfr/*
4177633Sdfr * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5177633Sdfr * unrestricted use provided that this legend is included on all tape
6177633Sdfr * media and as a part of the software program in whole or part.  Users
7177633Sdfr * may copy or modify Sun RPC without charge, but are not authorized
8177633Sdfr * to license or distribute it to anyone else except as part of a product or
9177633Sdfr * program developed by the user.
10177633Sdfr *
11177633Sdfr * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12177633Sdfr * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13177633Sdfr * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14177633Sdfr *
15177633Sdfr * Sun RPC is provided with no support and without any obligation on the
16177633Sdfr * part of Sun Microsystems, Inc. to assist in its use, correction,
17177633Sdfr * modification or enhancement.
18177633Sdfr *
19177633Sdfr * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20177633Sdfr * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21177633Sdfr * OR ANY PART THEREOF.
22177633Sdfr *
23177633Sdfr * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24177633Sdfr * or profits or other special, indirect and consequential damages, even if
25177633Sdfr * Sun has been advised of the possibility of such damages.
26177633Sdfr *
27177633Sdfr * Sun Microsystems, Inc.
28177633Sdfr * 2550 Garcia Avenue
29177633Sdfr * Mountain View, California  94043
30177633Sdfr */
31177633Sdfr
32177633Sdfr#if defined(LIBC_SCCS) && !defined(lint)
33177633Sdfrstatic char *sccsid2 = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
34177633Sdfrstatic char *sccsid = "@(#)xdr_array.c	2.1 88/07/29 4.0 RPCSRC";
35177633Sdfr#endif
36177633Sdfr#include <sys/cdefs.h>
37177633Sdfr__FBSDID("$FreeBSD$");
38177633Sdfr
39177633Sdfr/*
40177633Sdfr * xdr_array.c, Generic XDR routines impelmentation.
41177633Sdfr *
42177633Sdfr * Copyright (C) 1984, Sun Microsystems, Inc.
43177633Sdfr *
44177633Sdfr * These are the "non-trivial" xdr primitives used to serialize and de-serialize
45177633Sdfr * arrays.  See xdr.h for more info on the interface to xdr.
46177633Sdfr */
47177633Sdfr
48177633Sdfr#include <sys/param.h>
49177633Sdfr#include <sys/systm.h>
50177633Sdfr#include <sys/limits.h>
51177633Sdfr#include <sys/malloc.h>
52177633Sdfr
53177633Sdfr#include <rpc/types.h>
54177633Sdfr#include <rpc/xdr.h>
55177633Sdfr
56177633Sdfr/*
57177633Sdfr * XDR an array of arbitrary elements
58177633Sdfr * *addrp is a pointer to the array, *sizep is the number of elements.
59177633Sdfr * If addrp is NULL (*sizep * elsize) bytes are allocated.
60177633Sdfr * elsize is the size (in bytes) of each element, and elproc is the
61177633Sdfr * xdr procedure to call to handle each element of the array.
62177633Sdfr */
63177633Sdfrbool_t
64177633Sdfrxdr_array(XDR *xdrs,
65177633Sdfr    caddr_t *addrp,		/* array pointer */
66177633Sdfr    u_int *sizep,		/* number of elements */
67177633Sdfr    u_int maxsize,		/* max numberof elements */
68177633Sdfr    u_int elsize,		/* size in bytes of each element */
69177633Sdfr    xdrproc_t elproc)		/* xdr routine to handle each element */
70177633Sdfr{
71177633Sdfr	u_int i;
72177633Sdfr	caddr_t target = *addrp;
73177633Sdfr	u_int c;  /* the actual element count */
74177633Sdfr	bool_t stat = TRUE;
75177633Sdfr	u_int nodesize;
76177633Sdfr
77177633Sdfr	/* like strings, arrays are really counted arrays */
78177633Sdfr	if (!xdr_u_int(xdrs, sizep)) {
79177633Sdfr		return (FALSE);
80177633Sdfr	}
81177633Sdfr	c = *sizep;
82177633Sdfr	if ((c > maxsize || UINT_MAX/elsize < c) &&
83177633Sdfr	    (xdrs->x_op != XDR_FREE)) {
84177633Sdfr		return (FALSE);
85177633Sdfr	}
86177633Sdfr	nodesize = c * elsize;
87177633Sdfr
88177633Sdfr	/*
89177633Sdfr	 * if we are deserializing, we may need to allocate an array.
90177633Sdfr	 * We also save time by checking for a null array if we are freeing.
91177633Sdfr	 */
92177633Sdfr	if (target == NULL)
93177633Sdfr		switch (xdrs->x_op) {
94177633Sdfr		case XDR_DECODE:
95177633Sdfr			if (c == 0)
96177633Sdfr				return (TRUE);
97177633Sdfr			*addrp = target = mem_alloc(nodesize);
98177633Sdfr			if (target == NULL) {
99177633Sdfr				printf("xdr_array: out of memory");
100177633Sdfr				return (FALSE);
101177633Sdfr			}
102177633Sdfr			memset(target, 0, nodesize);
103177633Sdfr			break;
104177633Sdfr
105177633Sdfr		case XDR_FREE:
106177633Sdfr			return (TRUE);
107177633Sdfr
108177633Sdfr		case XDR_ENCODE:
109177633Sdfr			break;
110177633Sdfr	}
111177633Sdfr
112177633Sdfr	/*
113177633Sdfr	 * now we xdr each element of array
114177633Sdfr	 */
115177633Sdfr	for (i = 0; (i < c) && stat; i++) {
116177633Sdfr		stat = (*elproc)(xdrs, target);
117177633Sdfr		target += elsize;
118177633Sdfr	}
119177633Sdfr
120177633Sdfr	/*
121177633Sdfr	 * the array may need freeing
122177633Sdfr	 */
123177633Sdfr	if (xdrs->x_op == XDR_FREE) {
124177633Sdfr		mem_free(*addrp, nodesize);
125177633Sdfr		*addrp = NULL;
126177633Sdfr	}
127177633Sdfr	return (stat);
128177633Sdfr}
129177633Sdfr
130177633Sdfr/*
131177633Sdfr * xdr_vector():
132177633Sdfr *
133177633Sdfr * XDR a fixed length array. Unlike variable-length arrays,
134177633Sdfr * the storage of fixed length arrays is static and unfreeable.
135177633Sdfr * > basep: base of the array
136177633Sdfr * > size: size of the array
137177633Sdfr * > elemsize: size of each element
138177633Sdfr * > xdr_elem: routine to XDR each element
139177633Sdfr */
140177633Sdfrbool_t
141177633Sdfrxdr_vector(XDR *xdrs, char *basep, u_int nelem, u_int elemsize,
142177633Sdfr    xdrproc_t xdr_elem)
143177633Sdfr{
144177633Sdfr	u_int i;
145177633Sdfr	char *elptr;
146177633Sdfr
147177633Sdfr	elptr = basep;
148177633Sdfr	for (i = 0; i < nelem; i++) {
149177633Sdfr		if (!(*xdr_elem)(xdrs, elptr)) {
150177633Sdfr			return(FALSE);
151177633Sdfr		}
152177633Sdfr		elptr += elemsize;
153177633Sdfr	}
154177633Sdfr	return(TRUE);
155177633Sdfr}
156