xref: /freebsd/lib/libc/xdr/xdr_array.c (revision eae561b30ec984ce171d99b1fd182575acc2c639)
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_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";*/
32eae561b3SGarrett Wollman /*static char *sccsid = "from: @(#)xdr_array.c	2.1 88/07/29 4.0 RPCSRC";*/
33eae561b3SGarrett Wollman static char *rcsid = "$Id: xdr_array.c,v 1.1 1993/10/27 05:41:09 paul Exp $";
34eae561b3SGarrett Wollman #endif
35eae561b3SGarrett Wollman 
36eae561b3SGarrett Wollman /*
37eae561b3SGarrett Wollman  * xdr_array.c, Generic XDR routines impelmentation.
38eae561b3SGarrett Wollman  *
39eae561b3SGarrett Wollman  * Copyright (C) 1984, Sun Microsystems, Inc.
40eae561b3SGarrett Wollman  *
41eae561b3SGarrett Wollman  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
42eae561b3SGarrett Wollman  * arrays.  See xdr.h for more info on the interface to xdr.
43eae561b3SGarrett Wollman  */
44eae561b3SGarrett Wollman 
45eae561b3SGarrett Wollman #include <stdio.h>
46eae561b3SGarrett Wollman #include <stdlib.h>
47eae561b3SGarrett Wollman #include <rpc/types.h>
48eae561b3SGarrett Wollman #include <rpc/xdr.h>
49eae561b3SGarrett Wollman 
50eae561b3SGarrett Wollman #define LASTUNSIGNED	((u_int)0-1)
51eae561b3SGarrett Wollman 
52eae561b3SGarrett Wollman 
53eae561b3SGarrett Wollman /*
54eae561b3SGarrett Wollman  * XDR an array of arbitrary elements
55eae561b3SGarrett Wollman  * *addrp is a pointer to the array, *sizep is the number of elements.
56eae561b3SGarrett Wollman  * If addrp is NULL (*sizep * elsize) bytes are allocated.
57eae561b3SGarrett Wollman  * elsize is the size (in bytes) of each element, and elproc is the
58eae561b3SGarrett Wollman  * xdr procedure to call to handle each element of the array.
59eae561b3SGarrett Wollman  */
60eae561b3SGarrett Wollman bool_t
61eae561b3SGarrett Wollman xdr_array(xdrs, addrp, sizep, maxsize, elsize, elproc)
62eae561b3SGarrett Wollman 	register XDR *xdrs;
63eae561b3SGarrett Wollman 	caddr_t *addrp;		/* array pointer */
64eae561b3SGarrett Wollman 	u_int *sizep;		/* number of elements */
65eae561b3SGarrett Wollman 	u_int maxsize;		/* max numberof elements */
66eae561b3SGarrett Wollman 	u_int elsize;		/* size in bytes of each element */
67eae561b3SGarrett Wollman 	xdrproc_t elproc;	/* xdr routine to handle each element */
68eae561b3SGarrett Wollman {
69eae561b3SGarrett Wollman 	register u_int i;
70eae561b3SGarrett Wollman 	register caddr_t target = *addrp;
71eae561b3SGarrett Wollman 	register u_int c;  /* the actual element count */
72eae561b3SGarrett Wollman 	register bool_t stat = TRUE;
73eae561b3SGarrett Wollman 	register u_int nodesize;
74eae561b3SGarrett Wollman 
75eae561b3SGarrett Wollman 	/* like strings, arrays are really counted arrays */
76eae561b3SGarrett Wollman 	if (! xdr_u_int(xdrs, sizep)) {
77eae561b3SGarrett Wollman 		return (FALSE);
78eae561b3SGarrett Wollman 	}
79eae561b3SGarrett Wollman 	c = *sizep;
80eae561b3SGarrett Wollman 	if ((c > maxsize) && (xdrs->x_op != XDR_FREE)) {
81eae561b3SGarrett Wollman 		return (FALSE);
82eae561b3SGarrett Wollman 	}
83eae561b3SGarrett Wollman 	nodesize = c * elsize;
84eae561b3SGarrett Wollman 
85eae561b3SGarrett Wollman 	/*
86eae561b3SGarrett Wollman 	 * if we are deserializing, we may need to allocate an array.
87eae561b3SGarrett Wollman 	 * We also save time by checking for a null array if we are freeing.
88eae561b3SGarrett Wollman 	 */
89eae561b3SGarrett Wollman 	if (target == NULL)
90eae561b3SGarrett Wollman 		switch (xdrs->x_op) {
91eae561b3SGarrett Wollman 		case XDR_DECODE:
92eae561b3SGarrett Wollman 			if (c == 0)
93eae561b3SGarrett Wollman 				return (TRUE);
94eae561b3SGarrett Wollman 			*addrp = target = mem_alloc(nodesize);
95eae561b3SGarrett Wollman 			if (target == NULL) {
96eae561b3SGarrett Wollman 				(void) fprintf(stderr,
97eae561b3SGarrett Wollman 					"xdr_array: out of memory\n");
98eae561b3SGarrett Wollman 				return (FALSE);
99eae561b3SGarrett Wollman 			}
100eae561b3SGarrett Wollman 			bzero(target, nodesize);
101eae561b3SGarrett Wollman 			break;
102eae561b3SGarrett Wollman 
103eae561b3SGarrett Wollman 		case XDR_FREE:
104eae561b3SGarrett Wollman 			return (TRUE);
105eae561b3SGarrett Wollman 	}
106eae561b3SGarrett Wollman 
107eae561b3SGarrett Wollman 	/*
108eae561b3SGarrett Wollman 	 * now we xdr each element of array
109eae561b3SGarrett Wollman 	 */
110eae561b3SGarrett Wollman 	for (i = 0; (i < c) && stat; i++) {
111eae561b3SGarrett Wollman 		stat = (*elproc)(xdrs, target, LASTUNSIGNED);
112eae561b3SGarrett Wollman 		target += elsize;
113eae561b3SGarrett Wollman 	}
114eae561b3SGarrett Wollman 
115eae561b3SGarrett Wollman 	/*
116eae561b3SGarrett Wollman 	 * the array may need freeing
117eae561b3SGarrett Wollman 	 */
118eae561b3SGarrett Wollman 	if (xdrs->x_op == XDR_FREE) {
119eae561b3SGarrett Wollman 		mem_free(*addrp, nodesize);
120eae561b3SGarrett Wollman 		*addrp = NULL;
121eae561b3SGarrett Wollman 	}
122eae561b3SGarrett Wollman 	return (stat);
123eae561b3SGarrett Wollman }
124eae561b3SGarrett Wollman 
125eae561b3SGarrett Wollman /*
126eae561b3SGarrett Wollman  * xdr_vector():
127eae561b3SGarrett Wollman  *
128eae561b3SGarrett Wollman  * XDR a fixed length array. Unlike variable-length arrays,
129eae561b3SGarrett Wollman  * the storage of fixed length arrays is static and unfreeable.
130eae561b3SGarrett Wollman  * > basep: base of the array
131eae561b3SGarrett Wollman  * > size: size of the array
132eae561b3SGarrett Wollman  * > elemsize: size of each element
133eae561b3SGarrett Wollman  * > xdr_elem: routine to XDR each element
134eae561b3SGarrett Wollman  */
135eae561b3SGarrett Wollman bool_t
136eae561b3SGarrett Wollman xdr_vector(xdrs, basep, nelem, elemsize, xdr_elem)
137eae561b3SGarrett Wollman 	register XDR *xdrs;
138eae561b3SGarrett Wollman 	register char *basep;
139eae561b3SGarrett Wollman 	register u_int nelem;
140eae561b3SGarrett Wollman 	register u_int elemsize;
141eae561b3SGarrett Wollman 	register xdrproc_t xdr_elem;
142eae561b3SGarrett Wollman {
143eae561b3SGarrett Wollman 	register u_int i;
144eae561b3SGarrett Wollman 	register char *elptr;
145eae561b3SGarrett Wollman 
146eae561b3SGarrett Wollman 	elptr = basep;
147eae561b3SGarrett Wollman 	for (i = 0; i < nelem; i++) {
148eae561b3SGarrett Wollman 		if (! (*xdr_elem)(xdrs, elptr, LASTUNSIGNED)) {
149eae561b3SGarrett Wollman 			return(FALSE);
150eae561b3SGarrett Wollman 		}
151eae561b3SGarrett Wollman 		elptr += elemsize;
152eae561b3SGarrett Wollman 	}
153eae561b3SGarrett Wollman 	return(TRUE);
154eae561b3SGarrett Wollman }
155eae561b3SGarrett Wollman 
156