1*45095Smckusick /* @(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC */
2*45095Smckusick /*
3*45095Smckusick * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4*45095Smckusick * unrestricted use provided that this legend is included on all tape
5*45095Smckusick * media and as a part of the software program in whole or part. Users
6*45095Smckusick * may copy or modify Sun RPC without charge, but are not authorized
7*45095Smckusick * to license or distribute it to anyone else except as part of a product or
8*45095Smckusick * program developed by the user.
9*45095Smckusick *
10*45095Smckusick * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11*45095Smckusick * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12*45095Smckusick * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13*45095Smckusick *
14*45095Smckusick * Sun RPC is provided with no support and without any obligation on the
15*45095Smckusick * part of Sun Microsystems, Inc. to assist in its use, correction,
16*45095Smckusick * modification or enhancement.
17*45095Smckusick *
18*45095Smckusick * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19*45095Smckusick * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20*45095Smckusick * OR ANY PART THEREOF.
21*45095Smckusick *
22*45095Smckusick * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23*45095Smckusick * or profits or other special, indirect and consequential damages, even if
24*45095Smckusick * Sun has been advised of the possibility of such damages.
25*45095Smckusick *
26*45095Smckusick * Sun Microsystems, Inc.
27*45095Smckusick * 2550 Garcia Avenue
28*45095Smckusick * Mountain View, California 94043
29*45095Smckusick */
30*45095Smckusick #if !defined(lint) && defined(SCCSIDS)
31*45095Smckusick static char sccsid[] = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
32*45095Smckusick #endif
33*45095Smckusick
34*45095Smckusick /*
35*45095Smckusick * xdr_array.c, Generic XDR routines impelmentation.
36*45095Smckusick *
37*45095Smckusick * Copyright (C) 1984, Sun Microsystems, Inc.
38*45095Smckusick *
39*45095Smckusick * These are the "non-trivial" xdr primitives used to serialize and de-serialize
40*45095Smckusick * arrays. See xdr.h for more info on the interface to xdr.
41*45095Smckusick */
42*45095Smckusick
43*45095Smckusick #include <stdio.h>
44*45095Smckusick
45*45095Smckusick #include <rpc/types.h>
46*45095Smckusick #include <rpc/xdr.h>
47*45095Smckusick
48*45095Smckusick #define LASTUNSIGNED ((u_int)0-1)
49*45095Smckusick
50*45095Smckusick
51*45095Smckusick /*
52*45095Smckusick * XDR an array of arbitrary elements
53*45095Smckusick * *addrp is a pointer to the array, *sizep is the number of elements.
54*45095Smckusick * If addrp is NULL (*sizep * elsize) bytes are allocated.
55*45095Smckusick * elsize is the size (in bytes) of each element, and elproc is the
56*45095Smckusick * xdr procedure to call to handle each element of the array.
57*45095Smckusick */
58*45095Smckusick bool_t
xdr_array(xdrs,addrp,sizep,maxsize,elsize,elproc)59*45095Smckusick xdr_array(xdrs, addrp, sizep, maxsize, elsize, elproc)
60*45095Smckusick register XDR *xdrs;
61*45095Smckusick caddr_t *addrp; /* array pointer */
62*45095Smckusick u_int *sizep; /* number of elements */
63*45095Smckusick u_int maxsize; /* max numberof elements */
64*45095Smckusick u_int elsize; /* size in bytes of each element */
65*45095Smckusick xdrproc_t elproc; /* xdr routine to handle each element */
66*45095Smckusick {
67*45095Smckusick register u_int i;
68*45095Smckusick register caddr_t target = *addrp;
69*45095Smckusick register u_int c; /* the actual element count */
70*45095Smckusick register bool_t stat = TRUE;
71*45095Smckusick register u_int nodesize;
72*45095Smckusick
73*45095Smckusick /* like strings, arrays are really counted arrays */
74*45095Smckusick if (! xdr_u_int(xdrs, sizep)) {
75*45095Smckusick return (FALSE);
76*45095Smckusick }
77*45095Smckusick c = *sizep;
78*45095Smckusick if ((c > maxsize) && (xdrs->x_op != XDR_FREE)) {
79*45095Smckusick return (FALSE);
80*45095Smckusick }
81*45095Smckusick nodesize = c * elsize;
82*45095Smckusick
83*45095Smckusick /*
84*45095Smckusick * if we are deserializing, we may need to allocate an array.
85*45095Smckusick * We also save time by checking for a null array if we are freeing.
86*45095Smckusick */
87*45095Smckusick if (target == NULL)
88*45095Smckusick switch (xdrs->x_op) {
89*45095Smckusick case XDR_DECODE:
90*45095Smckusick if (c == 0)
91*45095Smckusick return (TRUE);
92*45095Smckusick *addrp = target = mem_alloc(nodesize);
93*45095Smckusick if (target == NULL) {
94*45095Smckusick (void) fprintf(stderr,
95*45095Smckusick "xdr_array: out of memory\n");
96*45095Smckusick return (FALSE);
97*45095Smckusick }
98*45095Smckusick bzero(target, nodesize);
99*45095Smckusick break;
100*45095Smckusick
101*45095Smckusick case XDR_FREE:
102*45095Smckusick return (TRUE);
103*45095Smckusick }
104*45095Smckusick
105*45095Smckusick /*
106*45095Smckusick * now we xdr each element of array
107*45095Smckusick */
108*45095Smckusick for (i = 0; (i < c) && stat; i++) {
109*45095Smckusick stat = (*elproc)(xdrs, target, LASTUNSIGNED);
110*45095Smckusick target += elsize;
111*45095Smckusick }
112*45095Smckusick
113*45095Smckusick /*
114*45095Smckusick * the array may need freeing
115*45095Smckusick */
116*45095Smckusick if (xdrs->x_op == XDR_FREE) {
117*45095Smckusick mem_free(*addrp, nodesize);
118*45095Smckusick *addrp = NULL;
119*45095Smckusick }
120*45095Smckusick return (stat);
121*45095Smckusick }
122*45095Smckusick
123*45095Smckusick /*
124*45095Smckusick * xdr_vector():
125*45095Smckusick *
126*45095Smckusick * XDR a fixed length array. Unlike variable-length arrays,
127*45095Smckusick * the storage of fixed length arrays is static and unfreeable.
128*45095Smckusick * > basep: base of the array
129*45095Smckusick * > size: size of the array
130*45095Smckusick * > elemsize: size of each element
131*45095Smckusick * > xdr_elem: routine to XDR each element
132*45095Smckusick */
133*45095Smckusick bool_t
xdr_vector(xdrs,basep,nelem,elemsize,xdr_elem)134*45095Smckusick xdr_vector(xdrs, basep, nelem, elemsize, xdr_elem)
135*45095Smckusick register XDR *xdrs;
136*45095Smckusick register char *basep;
137*45095Smckusick register u_int nelem;
138*45095Smckusick register u_int elemsize;
139*45095Smckusick register xdrproc_t xdr_elem;
140*45095Smckusick {
141*45095Smckusick register u_int i;
142*45095Smckusick register char *elptr;
143*45095Smckusick
144*45095Smckusick elptr = basep;
145*45095Smckusick for (i = 0; i < nelem; i++) {
146*45095Smckusick if (! (*xdr_elem)(xdrs, elptr, LASTUNSIGNED)) {
147*45095Smckusick return(FALSE);
148*45095Smckusick }
149*45095Smckusick elptr += elemsize;
150*45095Smckusick }
151*45095Smckusick return(TRUE);
152*45095Smckusick }
153*45095Smckusick
154