]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurityd/lib/sec_xdr_reference.c
Security-57336.1.9.tar.gz
[apple/security.git] / OSX / libsecurityd / lib / sec_xdr_reference.c
1 /*
2 * Copyright (c) 2006,2011,2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /* $NetBSD: xdr_reference.c,v 1.13 2000/01/22 22:19:18 mycroft Exp $ */
25
26 /*
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.
33 *
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.
37 *
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.
41 *
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.
45 *
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.
49 *
50 * Sun Microsystems, Inc.
51 * 2550 Garcia Avenue
52 * Mountain View, California 94043
53 */
54
55 #include <sys/cdefs.h>
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char *sccsid = "@(#)xdr_reference.c 1.11 87/08/11 SMI";
58 static char *sccsid = "@(#)xdr_reference.c 2.1 88/07/29 4.0 RPCSRC";
59 static char *rcsid = "$FreeBSD: src/lib/libc/xdr/xdr_reference.c,v 1.11 2002/03/22 21:53:26 obrien Exp $";
60 #endif
61 #include <sys/cdefs.h>
62 #include <assert.h>
63 /*
64 * xdr_reference.c, Generic XDR routines impelmentation.
65 *
66 * Copyright (C) 1987, Sun Microsystems, Inc.
67 *
68 * These are the "non-trivial" xdr primitives used to serialize and de-serialize
69 * "pointers". See xdr.h for more info on the interface to xdr.
70 */
71
72 #include <err.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76
77 #include "sec_xdr.h"
78
79 /*
80 * XDR an indirect pointer
81 * xdr_reference is for recursively translating a structure that is
82 * referenced by a pointer inside the structure that is currently being
83 * translated. pp references a pointer to storage. If *pp is null
84 * the necessary storage is allocated.
85 * size is the sizeof the referneced structure.
86 * proc is the routine to handle the referenced structure.
87 */
88 bool_t
89 sec_xdr_reference(XDR *xdrs, uint8_t **pp, u_int size, xdrproc_t proc)
90 {
91 uint8_t *loc = pp ? *pp : NULL;
92 bool_t stat;
93
94 if (size > 1024) {
95 // Structure suspiciously large: 1024 is arbitrary upper bound
96 // for struct sizes (non-nested size)
97 assert(FALSE);
98 return (FALSE);
99 }
100 uint8_t obj[size];
101
102 bool_t sizeof_alloc = sec_xdr_arena_size_allocator(xdrs);
103
104 if (loc == NULL)
105 switch (xdrs->x_op) {
106 case XDR_FREE:
107 return (TRUE);
108 case XDR_DECODE:
109 {
110 if (!sec_mem_alloc(xdrs, size, &loc))
111 return (FALSE);
112 if (!loc) {
113 memset(obj, 0, size);
114 loc = &obj[0];
115 }
116 if (!sizeof_alloc)
117 *pp = loc;
118 break;
119 }
120 case XDR_ENCODE:
121 break;
122 }
123
124 stat = (*proc)(xdrs, loc, 0);
125
126 if (xdrs->x_op == XDR_FREE) {
127 sec_mem_free(xdrs, loc, size);
128 *pp = NULL;
129 }
130 return (stat);
131 }
132
133
134 bool_t
135 sec_xdr_pointer(XDR *xdrs, uint8_t **objpp, u_int obj_size, xdrproc_t xdr_obj)
136 {
137 bool_t more_data;
138
139 more_data = (objpp ? (*objpp != NULL) : FALSE);
140 if (! xdr_bool(xdrs,&more_data))
141 return (FALSE);
142
143 bool_t sizeof_alloc = sec_xdr_arena_size_allocator(xdrs);
144
145 if (! more_data) {
146 if ((xdrs->x_op == XDR_DECODE) && !sizeof_alloc)
147 *objpp = NULL;
148 return (TRUE);
149 }
150 return (sec_xdr_reference(xdrs,objpp,obj_size,xdr_obj));
151 }
152
153 /**
154 * This is almost a straight copy of the standard implementation, except
155 * that all calls made that allocate memory can defer to an alternate
156 * mechanism, with the purpose to allocate from one block of memory on
157 * *decode*
158 */