]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurityd/lib/sec_xdr_array.c
2 * Copyright (c) 2006,2011,2013-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 /* $NetBSD: xdr_array.c,v 1.12 2000/01/22 22:19:18 mycroft Exp $ */
27 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
28 * unrestricted use provided that this legend is included on all tape
29 * media and as a part of the software program in whole or part. Users
30 * may copy or modify Sun RPC without charge, but are not authorized
31 * to license or distribute it to anyone else except as part of a product or
32 * program developed by the user.
34 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
35 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
36 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
38 * Sun RPC is provided with no support and without any obligation on the
39 * part of Sun Microsystems, Inc. to assist in its use, correction,
40 * modification or enhancement.
42 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
43 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
44 * OR ANY PART THEREOF.
46 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
47 * or profits or other special, indirect and consequential damages, even if
48 * Sun has been advised of the possibility of such damages.
50 * Sun Microsystems, Inc.
52 * Mountain View, California 94043
55 #if defined(LIBC_SCCS) && !defined(lint)
56 static char *sccsid
= "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
57 static char *sccsid
= "@(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC";
59 #include <sys/cdefs.h>
62 * xdr_array.c, Generic XDR routines impelmentation.
64 * Copyright (C) 1984, Sun Microsystems, Inc.
66 * These are the "non-trivial" xdr primitives used to serialize and de-serialize
67 * arrays. See xdr.h for more info on the interface to xdr.
80 * XDR an array of arbitrary elements
81 * *addrp is a pointer to the array, *sizep is the number of elements.
82 * If addrp is NULL (*sizep * elsize) bytes are allocated.
83 * elsize is the size (in bytes) of each element, and elproc is the
84 * xdr procedure to call to handle each element of the array.
87 sec_xdr_array(XDR
*xdrs
, uint8_t **addrp
, u_int
*sizep
, u_int maxsize
, u_int elsize
, xdrproc_t elproc
)
92 u_int c
= sizep
? *sizep
: 0; /* the actual element count */
93 /* like strings, arrays are really counted arrays */
94 if (!xdr_u_int(xdrs
, &c
))
97 if (sizep
&& (xdrs
->x_op
== XDR_DECODE
))
100 // XXX/cs on decode if c == 0 return
102 if ((c
> maxsize
|| UINT_MAX
/elsize
< c
) && (xdrs
->x_op
!= XDR_FREE
))
106 // Structure suspiciously large: 1024 is arbitrary upper bound
107 // for struct sizes (non-nested size)
112 u_int nodesize
= c
* elsize
;
113 uint8_t *target
= addrp
? *addrp
: NULL
;
117 bool_t sizeof_alloc
= sec_xdr_arena_size_allocator(xdrs
);
120 * if we are deserializing, we may need to allocate an array.
121 * We also save time by checking for a null array if we are freeing.
123 if (target
== NULL
) {
124 switch (xdrs
->x_op
) {
128 if (!sec_mem_alloc(xdrs
, nodesize
, &target
))
145 * now we xdr each element of array
147 for (i
= 0; (i
< c
) && stat
; i
++) {
148 if ((xdrs
->x_op
== XDR_DECODE
) && sizeof_alloc
)
149 memset(obj
, 0, elsize
);
150 stat
= (*elproc
)(xdrs
, target
, 0);
151 if ((xdrs
->x_op
== XDR_ENCODE
) || !sizeof_alloc
)
156 * the array may need freeing
158 if (xdrs
->x_op
== XDR_FREE
) {
159 sec_mem_free(xdrs
, *addrp
, nodesize
);
166 * This is almost a straight copy of the standard implementation, except
167 * that all calls made that allocate memory can defer to an alternate
168 * mechanism, with the purpose to allocate from one block of memory on