]> git.saurik.com Git - apple/security.git/blob - Security/libsecurityd/lib/sec_xdr_sizeof.c
Security-57031.40.6.tar.gz
[apple/security.git] / Security / libsecurityd / lib / sec_xdr_sizeof.c
1 /*
2 * Copyright (c) 2006,2011-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 /*
25 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
26 * unrestricted use provided that this legend is included on all tape
27 * media and as a part of the software program in whole or part. Users
28 * may copy or modify Sun RPC without charge, but are not authorized
29 * to license or distribute it to anyone else except as part of a product or
30 * program developed by the user.
31 *
32 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
33 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
35 *
36 * Sun RPC is provided with no support and without any obligation on the
37 * part of Sun Microsystems, Inc. to assist in its use, correction,
38 * modification or enhancement.
39 *
40 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
41 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
42 * OR ANY PART THEREOF.
43 *
44 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
45 * or profits or other special, indirect and consequential damages, even if
46 * Sun has been advised of the possibility of such damages.
47 *
48 * Sun Microsystems, Inc.
49 * 2550 Garcia Avenue
50 * Mountain View, California 94043
51 */
52 /*
53 * xdr_sizeof.c
54 *
55 * Copyright 1990 Sun Microsystems, Inc.
56 *
57 * General purpose routine to see how much space something will use
58 * when serialized using XDR.
59 */
60 #include <stdlib.h>
61
62 #include "sec_xdr.h"
63
64 /* ARGSUSED */
65 static bool_t
66 sec_x_putlong(xdrs, intp)
67 XDR *xdrs;
68 int *intp;
69 {
70 xdrs->x_handy += BYTES_PER_XDR_UNIT;
71 return (TRUE);
72 }
73
74 /* ARGSUSED */
75 static bool_t
76 sec_x_putbytes(xdrs, bp, len)
77 XDR *xdrs;
78 char *bp;
79 u_int len;
80 {
81 xdrs->x_handy += len;
82 return (TRUE);
83 }
84
85 static u_int
86 sec_x_getpostn(xdrs)
87 XDR *xdrs;
88 {
89 return (xdrs->x_handy);
90 }
91
92 /* ARGSUSED */
93 static bool_t
94 sec_x_setpostn(xdrs, pos)
95 XDR *xdrs;
96 u_int pos;
97 {
98 /* This is not allowed */
99 return (FALSE);
100 }
101
102 static int32_t *
103 sec_x_inline(xdrs, len)
104 XDR *xdrs;
105 u_int len;
106 {
107 intptr_t llen;
108
109 if (len == 0) {
110 return (NULL);
111 }
112
113 if (xdrs->x_op != XDR_ENCODE) {
114 return (NULL);
115 }
116
117 llen = len;
118
119 if (llen < (intptr_t)xdrs->x_base) {
120 /* x_private was already allocated */
121 xdrs->x_handy += llen;
122 return ((int32_t *) xdrs->x_private);
123 } else {
124 /* Free the earlier space and allocate new area */
125 if (xdrs->x_private)
126 free(xdrs->x_private);
127 if ((xdrs->x_private = (caddr_t) malloc(len)) == NULL) {
128 xdrs->x_base = 0;
129 return (NULL);
130 }
131 xdrs->x_base = (caddr_t)llen;
132 xdrs->x_handy += llen;
133 return ((int32_t *) xdrs->x_private);
134 }
135 }
136
137 static int
138 sec_harmless()
139 {
140 /* Always return FALSE/NULL, as the case may be */
141 return (0);
142 }
143
144 static void
145 sec_x_destroy(xdrs)
146 XDR *xdrs;
147 {
148 xdrs->x_handy = 0;
149 xdrs->x_base = 0;
150 if (xdrs->x_private) {
151 free(xdrs->x_private);
152 xdrs->x_private = NULL;
153 }
154 return;
155 }
156
157 u_int
158 sec_xdr_sizeof_in(func, data)
159 xdrproc_t func;
160 void *data;
161 {
162 XDR x;
163 struct xdr_ops ops;
164 bool_t stat;
165 /* to stop ANSI-C compiler from complaining */
166 #ifdef __LP64__
167 typedef bool_t (* dummyfunc1)(XDR *, int *);
168 #else
169 typedef bool_t (* dummyfunc1)(XDR *, long *);
170 #endif
171 typedef bool_t (* dummyfunc2)(XDR *, caddr_t, u_int);
172
173 ops.x_putlong = sec_x_putlong;
174 ops.x_putbytes = sec_x_putbytes;
175 ops.x_inline = sec_x_inline;
176 ops.x_getpostn = sec_x_getpostn;
177 ops.x_setpostn = sec_x_setpostn;
178 ops.x_destroy = sec_x_destroy;
179
180 /* the other harmless ones */
181 ops.x_getlong = (dummyfunc1) sec_harmless;
182 ops.x_getbytes = (dummyfunc2) sec_harmless;
183
184 x.x_op = XDR_ENCODE;
185 x.x_ops = &ops;
186 x.x_handy = 0;
187 x.x_public = (caddr_t) NULL; // explicitly unsetting to avoid confusion with custom allocator
188 x.x_private = (caddr_t) NULL;
189 x.x_base = (caddr_t) 0;
190
191 sec_xdr_arena_allocator_t size_alloc;
192 sec_xdr_arena_init_size_alloc(&size_alloc, &x);
193 stat = func(&x, data, 0);
194 if (x.x_private)
195 free(x.x_private);
196 return (stat == TRUE ? (unsigned) x.x_handy: 0);
197 }
198
199 u_int
200 sec_xdr_sizeof_out(copy, size, func, data)
201 const void *copy;
202 u_int size;
203 xdrproc_t func;
204 void **data;
205 {
206 XDR x;
207 bool_t stat;
208
209 sec_xdrmem_create(&x, (void *)copy, size, XDR_DECODE);
210
211 sec_xdr_arena_allocator_t size_alloc;
212 sec_xdr_arena_init_size_alloc(&size_alloc, &x);
213 stat = func(&x, data, 0);
214 if (size_alloc.data)
215 free(size_alloc.data);
216 return (stat == TRUE ? (u_int)size_alloc.offset : 0);
217 }