Print this page
1575 untangle libmlrpc ... (libmlrpc)
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/smbsrv/libmlrpc/common/ndr_heap.c
+++ new/usr/src/lib/libmlrpc/common/ndr_heap.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 + *
25 + * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
24 26 */
25 27
26 28 /*
27 29 * NDR heap management. The heap is used for temporary storage by
28 30 * both the client and server side library routines. In order to
29 31 * support the different requirements of the various RPCs, the heap
30 32 * can grow dynamically if required. We start with a single block
31 33 * and perform sub-allocations from it. If an RPC requires more space
32 34 * we will continue to add it a block at a time. This means that we
33 35 * don't hog lots of memory on every call to support the few times
34 36 * that we actually need a lot heap space.
35 37 *
36 38 * Note that there is no individual free function. Once space has been
37 39 * allocated, it remains allocated until the heap is destroyed. This
38 40 * shouldn't be an issue because the heap is being filled with data to
↓ open down ↓ |
5 lines elided |
↑ open up ↑ |
39 41 * be marshalled or unmarshalled and we need it all to be there until
40 42 * the point that the entire heap is no longer required.
41 43 */
42 44
43 45 #include <sys/errno.h>
44 46 #include <stdlib.h>
45 47 #include <string.h>
46 48 #include <strings.h>
47 49 #include <sys/uio.h>
48 50
49 -#include <smbsrv/libsmb.h>
50 -#include <smbsrv/libmlrpc.h>
51 -#include <smbsrv/smb_sid.h>
51 +#include <libmlrpc.h>
52 +#include <ndr_wchar.h>
52 53
53 54 /*
54 55 * Allocate a heap structure and the first heap block. For many RPC
55 56 * operations this will be the only time we need to malloc memory
56 57 * in this instance of the heap. The only point of note here is that
57 58 * we put the heap management data in the first block to avoid a
58 59 * second malloc. Make sure that sizeof(ndr_heap_t) is smaller
59 60 * than NDR_HEAP_BLKSZ.
60 61 *
61 62 * Note that the heap management data is at the start of the first block.
62 63 *
63 64 * Returns a pointer to the newly created heap, which is used like an
64 65 * opaque handle with the rest of the heap management interface..
65 66 */
66 67 ndr_heap_t *
67 68 ndr_heap_create(void)
68 69 {
69 70 ndr_heap_t *heap;
70 71 char *base;
71 72 size_t allocsize = sizeof (ndr_heap_t) + NDR_HEAP_BLKSZ;
72 73
73 74 if ((heap = malloc(allocsize)) == NULL)
74 75 return (NULL);
75 76
76 77 base = (char *)heap;
77 78 bzero(heap, sizeof (ndr_heap_t));
78 79
79 80 heap->iovcnt = NDR_HEAP_MAXIOV;
80 81 heap->iov = heap->iovec;
81 82 heap->iov->iov_base = base;
82 83 heap->iov->iov_len = sizeof (ndr_heap_t);
83 84 heap->top = base + allocsize;
84 85 heap->next = base + sizeof (ndr_heap_t);
85 86
86 87 return (heap);
87 88 }
88 89
89 90 /*
90 91 * Deallocate all of the memory associated with a heap. This is the
91 92 * only way to deallocate heap memory, it isn't possible to free the
92 93 * space obtained by individual malloc calls.
93 94 *
94 95 * Note that the first block contains the heap management data, which
95 96 * is deleted last.
96 97 */
97 98 void
98 99 ndr_heap_destroy(ndr_heap_t *heap)
99 100 {
100 101 int i;
101 102 char *p;
102 103
103 104 if (heap) {
104 105 for (i = 1; i < NDR_HEAP_MAXIOV; ++i) {
105 106 if ((p = heap->iovec[i].iov_base) != NULL)
106 107 free(p);
107 108 }
108 109
109 110 free(heap);
110 111 }
111 112 }
112 113
113 114 /*
114 115 * Allocate space in the specified heap. All requests are padded, if
115 116 * required, to ensure dword alignment. If the current iov will be
116 117 * exceeded, we allocate a new block and setup the next iov. Otherwise
117 118 * all we have to do is move the next pointer and update the current
118 119 * iov length.
119 120 *
120 121 * On success, a pointer to the allocated (dword aligned) area is
121 122 * returned. Otherwise a null pointer is returned.
122 123 */
123 124 void *
124 125 ndr_heap_malloc(ndr_heap_t *heap, unsigned size)
125 126 {
126 127 char *p;
127 128 int incr_size;
128 129
129 130 size += NDR_ALIGN4(size);
130 131
131 132 if (heap == NULL || size == 0)
132 133 return (NULL);
133 134
134 135 p = heap->next;
135 136
136 137 if (p + size > heap->top) {
137 138 if ((heap->iovcnt == 0) || ((--heap->iovcnt) == 0))
138 139 return (NULL);
139 140
140 141 incr_size = (size < NDR_HEAP_BLKSZ) ? NDR_HEAP_BLKSZ : size;
141 142
142 143 if ((p = (char *)malloc(incr_size)) == NULL)
143 144 return (NULL);
144 145
145 146 ++heap->iov;
146 147 heap->iov->iov_base = p;
↓ open down ↓ |
85 lines elided |
↑ open up ↑ |
147 148 heap->iov->iov_len = 0;
148 149 heap->top = p + incr_size;
149 150 }
150 151
151 152 heap->next = p + size;
152 153 heap->iov->iov_len += size;
153 154 return ((void *)p);
154 155 }
155 156
156 157 /*
158 + * Convenience function to copy some memory into the heap.
159 + */
160 +void *
161 +ndr_heap_dupmem(ndr_heap_t *heap, const void *mem, size_t len)
162 +{
163 + void *p;
164 +
165 + if (mem == NULL)
166 + return (NULL);
167 +
168 + if ((p = ndr_heap_malloc(heap, len)) != NULL)
169 + (void) memcpy(p, mem, len);
170 +
171 + return (p);
172 +}
173 +
174 +/*
157 175 * Convenience function to do heap strdup.
158 176 */
159 177 void *
160 178 ndr_heap_strdup(ndr_heap_t *heap, const char *s)
161 179 {
162 180 int len;
163 181 void *p;
164 182
165 183 if (s == NULL)
166 184 return (NULL);
167 185
168 186 /*
169 187 * We don't need to clutter the heap with empty strings.
170 188 */
171 189 if ((len = strlen(s)) == 0)
172 190 return ("");
173 191
174 - if ((p = ndr_heap_malloc(heap, len+1)) != NULL)
175 - (void) strcpy((char *)p, s);
192 + p = ndr_heap_dupmem(heap, s, len+1);
176 193
177 194 return (p);
178 195 }
179 196
180 197 /*
181 198 * Make an ndr_mstring_t from a regular string.
182 199 */
183 200 int
184 201 ndr_heap_mstring(ndr_heap_t *heap, const char *s, ndr_mstring_t *out)
185 202 {
203 + size_t slen;
204 +
186 205 if (s == NULL || out == NULL)
187 206 return (-1);
188 207
189 - out->length = smb_wcequiv_strlen(s);
190 - out->allosize = out->length + sizeof (smb_wchar_t);
208 + /*
209 + * Determine the WC strlen of s
210 + * Was ndr__wcequiv_strlen(s)
211 + */
212 + slen = ndr__mbstowcs(NULL, s, NDR_STRING_MAX);
213 + if (slen == (size_t)-1)
214 + return (-1);
191 215
216 + out->length = slen * sizeof (ndr_wchar_t);
217 + out->allosize = out->length + sizeof (ndr_wchar_t);
218 +
192 219 if ((out->str = ndr_heap_strdup(heap, s)) == NULL)
193 220 return (-1);
194 221
195 222 return (0);
196 223 }
197 224
198 225 /*
199 226 * Our regular string marshalling always creates null terminated strings
200 227 * but some Windows clients and servers are pedantic about the string
201 228 * formats they will accept and require non-null terminated strings.
202 229 * This function can be used to build a wide-char, non-null terminated
203 230 * string in the heap as a varying/conformant array. We need to do the
204 231 * wide-char conversion here because the marshalling code won't be
205 232 * aware that this is really a string.
206 233 */
207 234 void
208 235 ndr_heap_mkvcs(ndr_heap_t *heap, char *s, ndr_vcstr_t *vc)
209 236 {
237 + size_t slen;
210 238 int mlen;
211 239
212 - vc->wclen = smb_wcequiv_strlen(s);
240 + /*
241 + * Determine the WC strlen of s
242 + * Was ndr__wcequiv_strlen(s)
243 + */
244 + slen = ndr__mbstowcs(NULL, s, NDR_STRING_MAX);
245 + if (slen == (size_t)-1)
246 + slen = 0;
247 +
248 + vc->wclen = slen * sizeof (ndr_wchar_t);
213 249 vc->wcsize = vc->wclen;
214 250
215 - mlen = sizeof (ndr_vcs_t) + vc->wcsize + sizeof (smb_wchar_t);
216 -
251 + /*
252 + * alloc one extra wchar for a null
253 + * See slen + 1 arg for mbstowcs
254 + */
255 + mlen = sizeof (ndr_vcs_t) + vc->wcsize + sizeof (ndr_wchar_t);
217 256 vc->vcs = ndr_heap_malloc(heap, mlen);
218 257
219 258 if (vc->vcs) {
220 259 vc->vcs->vc_first_is = 0;
221 - vc->vcs->vc_length_is = vc->wclen / sizeof (smb_wchar_t);
222 - (void) smb_mbstowcs((smb_wchar_t *)vc->vcs->buffer, s,
223 - vc->vcs->vc_length_is);
260 + vc->vcs->vc_length_is = slen;
261 + (void) ndr__mbstowcs(vc->vcs->buffer, s, slen + 1);
224 262 }
225 263 }
226 264
227 265 void
228 266 ndr_heap_mkvcb(ndr_heap_t *heap, uint8_t *data, uint32_t datalen,
229 267 ndr_vcbuf_t *vcbuf)
230 268 {
231 269 int mlen;
232 270
233 271 if (data == NULL || datalen == 0) {
234 272 bzero(vcbuf, sizeof (ndr_vcbuf_t));
235 273 return;
236 274 }
237 275
238 276 vcbuf->len = datalen;
239 277 vcbuf->size = datalen;
240 278
241 279 mlen = sizeof (ndr_vcbuf_t) + datalen;
242 280
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
243 281 vcbuf->vcb = ndr_heap_malloc(heap, mlen);
244 282
245 283 if (vcbuf->vcb) {
246 284 vcbuf->vcb->vc_first_is = 0;
247 285 vcbuf->vcb->vc_length_is = datalen;
248 286 bcopy(data, vcbuf->vcb->buffer, datalen);
249 287 }
250 288 }
251 289
252 290 /*
253 - * Duplcate a SID in the heap.
291 + * Removed ndr_heap_siddup(), now using ndr_heap_dupmem().
254 292 */
255 -smb_sid_t *
256 -ndr_heap_siddup(ndr_heap_t *heap, smb_sid_t *sid)
257 -{
258 - smb_sid_t *new_sid;
259 - unsigned size;
260 293
261 - if (sid == NULL)
262 - return (NULL);
263 -
264 - size = smb_sid_len(sid);
265 -
266 - if ((new_sid = ndr_heap_malloc(heap, size)) == NULL)
267 - return (NULL);
268 -
269 - bcopy(sid, new_sid, size);
270 - return (new_sid);
271 -}
272 -
273 294 int
274 295 ndr_heap_used(ndr_heap_t *heap)
275 296 {
276 297 int used = 0;
277 298 int i;
278 299
279 300 for (i = 0; i < NDR_HEAP_MAXIOV; ++i)
280 301 used += heap->iovec[i].iov_len;
281 302
282 303 return (used);
283 304 }
284 305
285 306 int
286 307 ndr_heap_avail(ndr_heap_t *heap)
287 308 {
288 309 int avail;
289 310 int count;
290 311
291 312 count = (heap->iovcnt == 0) ? 0 : (heap->iovcnt - 1);
292 313
293 314 avail = count * NDR_HEAP_BLKSZ;
294 315 avail += (heap->top - heap->next);
295 316
296 317 return (avail);
297 318 }
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX