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