source: rtems-libbsd/freebsd/lib/libc/xdr/xdr_array.c @ f41a394

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f41a394 was f41a394, checked in by Sebastian Huber <sebastian.huber@…>, on 06/09/16 at 09:46:09

XDR(3): Import from FreeBSD

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