r5809: try to catch NULL pointers during developerment for rpcbuf_move()
[Samba/gbeck.git] / source / rpc_parse / parse_buffer.c
blobea72df8db45d3229f59f087d1747aa6aa5efce44
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 *
5 * Copyright (C) Andrew Tridgell 1992-2000,
6 * Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
7 * Copyright (C) Jean François Micouleau 1998-2000,
8 * Copyright (C) Gerald Carter 2000-2005,
9 * Copyright (C) Tim Potter 2001-2002.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "includes.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_RPC_PARSE
31 /**********************************************************************
32 Initialize a new spoolss buff for use by a client rpc
33 **********************************************************************/
34 void rpcbuf_init(RPC_BUFFER *buffer, uint32 size, TALLOC_CTX *ctx)
36 buffer->size = size;
37 buffer->string_at_end = size;
38 prs_init(&buffer->prs, size, ctx, MARSHALL);
39 buffer->struct_start = prs_offset(&buffer->prs);
42 /*******************************************************************
43 Read/write a RPC_BUFFER struct.
44 ********************************************************************/
46 BOOL prs_rpcbuffer(const char *desc, prs_struct *ps, int depth, RPC_BUFFER *buffer)
48 prs_debug(ps, depth, desc, "prs_rpcbuffer");
49 depth++;
51 /* reading */
52 if (UNMARSHALLING(ps)) {
53 buffer->size=0;
54 buffer->string_at_end=0;
56 if (!prs_uint32("size", ps, depth, &buffer->size))
57 return False;
60 * JRA. I'm not sure if the data in here is in big-endian format if
61 * the client is big-endian. Leave as default (little endian) for now.
64 if (!prs_init(&buffer->prs, buffer->size, prs_get_mem_context(ps), UNMARSHALL))
65 return False;
67 if (!prs_append_some_prs_data(&buffer->prs, ps, prs_offset(ps), buffer->size))
68 return False;
70 if (!prs_set_offset(&buffer->prs, 0))
71 return False;
73 if (!prs_set_offset(ps, buffer->size+prs_offset(ps)))
74 return False;
76 buffer->string_at_end=buffer->size;
78 return True;
80 else {
81 BOOL ret = False;
83 if (!prs_uint32("size", ps, depth, &buffer->size))
84 goto out;
86 if (!prs_append_some_prs_data(ps, &buffer->prs, 0, buffer->size))
87 goto out;
89 ret = True;
90 out:
92 /* We have finished with the data in buffer->prs - free it. */
93 prs_mem_free(&buffer->prs);
95 return ret;
99 /*******************************************************************
100 Read/write an RPC_BUFFER* struct.(allocate memory if unmarshalling)
101 ********************************************************************/
103 BOOL prs_rpcbuffer_p(const char *desc, prs_struct *ps, int depth, RPC_BUFFER **buffer)
105 uint32 data_p;
107 /* caputure the pointer value to stream */
109 data_p = (uint32) *buffer;
111 if ( !prs_uint32("ptr", ps, depth, &data_p ))
112 return False;
114 /* we're done if there is no data */
116 if ( !data_p )
117 return True;
119 if ( UNMARSHALLING(ps) ) {
120 if ( !(*buffer = PRS_ALLOC_MEM(ps, RPC_BUFFER, 1)) )
121 return False;
124 return prs_rpcbuffer( desc, ps, depth, *buffer);
127 /****************************************************************************
128 Allocate more memory for a RPC_BUFFER.
129 ****************************************************************************/
131 BOOL rpcbuf_alloc_size(RPC_BUFFER *buffer, uint32 buffer_size)
133 prs_struct *ps;
134 uint32 extra_space;
135 uint32 old_offset;
137 ps= &buffer->prs;
139 /* damn, I'm doing the reverse operation of prs_grow() :) */
140 if (buffer_size < prs_data_size(ps))
141 extra_space=0;
142 else
143 extra_space = buffer_size - prs_data_size(ps);
146 * save the offset and move to the end of the buffer
147 * prs_grow() checks the extra_space against the offset
149 old_offset=prs_offset(ps);
150 prs_set_offset(ps, prs_data_size(ps));
152 if (!prs_grow(ps, extra_space))
153 return False;
155 prs_set_offset(ps, old_offset);
157 buffer->string_at_end=prs_data_size(ps);
159 return True;
162 /*******************************************************************
163 move a BUFFER from the query to the reply.
164 As the data pointers in RPC_BUFFER are malloc'ed, not talloc'ed,
165 this is ok. This is an OPTIMIZATION and is not strictly neccessary.
166 Clears the memory to zero also.
167 ********************************************************************/
169 void rpcbuf_move(RPC_BUFFER *src, RPC_BUFFER **dest)
171 SMB_ASSERT( src != NULL );
173 prs_switch_type(&src->prs, MARSHALL);
174 if(!prs_set_offset(&src->prs, 0))
175 return;
176 prs_force_dynamic(&src->prs);
177 prs_mem_clear(&src->prs);
178 *dest=src;
181 /*******************************************************************
182 Get the size of a BUFFER struct.
183 ********************************************************************/
185 uint32 rpcbuf_get_size(RPC_BUFFER *buffer)
187 return (buffer->size);
191 /*******************************************************************
192 * write a UNICODE string and its relative pointer.
193 * used by all the RPC structs passing a buffer
195 * As I'm a nice guy, I'm forcing myself to explain this code.
196 * MS did a good job in the overall spoolss code except in some
197 * functions where they are passing the API buffer directly in the
198 * RPC request/reply. That's to maintain compatiility at the API level.
199 * They could have done it the good way the first time.
201 * So what happen is: the strings are written at the buffer's end,
202 * in the reverse order of the original structure. Some pointers to
203 * the strings are also in the buffer. Those are relative to the
204 * buffer's start.
206 * If you don't understand or want to change that function,
207 * first get in touch with me: jfm@samba.org
209 ********************************************************************/
211 BOOL smb_io_relstr(const char *desc, RPC_BUFFER *buffer, int depth, UNISTR *string)
213 prs_struct *ps=&buffer->prs;
215 if (MARSHALLING(ps)) {
216 uint32 struct_offset = prs_offset(ps);
217 uint32 relative_offset;
219 buffer->string_at_end -= (size_of_relative_string(string) - 4);
220 if(!prs_set_offset(ps, buffer->string_at_end))
221 return False;
222 #if 0 /* JERRY */
224 * Win2k does not align strings in a buffer
225 * Tested against WinNT 4.0 SP 6a & 2k SP2 --jerry
227 if (!prs_align(ps))
228 return False;
229 #endif
230 buffer->string_at_end = prs_offset(ps);
232 /* write the string */
233 if (!smb_io_unistr(desc, string, ps, depth))
234 return False;
236 if(!prs_set_offset(ps, struct_offset))
237 return False;
239 relative_offset=buffer->string_at_end - buffer->struct_start;
240 /* write its offset */
241 if (!prs_uint32("offset", ps, depth, &relative_offset))
242 return False;
244 else {
245 uint32 old_offset;
247 /* read the offset */
248 if (!prs_uint32("offset", ps, depth, &(buffer->string_at_end)))
249 return False;
251 if (buffer->string_at_end == 0)
252 return True;
254 old_offset = prs_offset(ps);
255 if(!prs_set_offset(ps, buffer->string_at_end+buffer->struct_start))
256 return False;
258 /* read the string */
259 if (!smb_io_unistr(desc, string, ps, depth))
260 return False;
262 if(!prs_set_offset(ps, old_offset))
263 return False;
265 return True;
268 /*******************************************************************
269 * write a array of UNICODE strings and its relative pointer.
270 * used by 2 RPC structs
271 ********************************************************************/
273 BOOL smb_io_relarraystr(const char *desc, RPC_BUFFER *buffer, int depth, uint16 **string)
275 UNISTR chaine;
277 prs_struct *ps=&buffer->prs;
279 if (MARSHALLING(ps)) {
280 uint32 struct_offset = prs_offset(ps);
281 uint32 relative_offset;
282 uint16 *p;
283 uint16 *q;
284 uint16 zero=0;
285 p=*string;
286 q=*string;
288 /* first write the last 0 */
289 buffer->string_at_end -= 2;
290 if(!prs_set_offset(ps, buffer->string_at_end))
291 return False;
293 if(!prs_uint16("leading zero", ps, depth, &zero))
294 return False;
296 while (p && (*p!=0)) {
297 while (*q!=0)
298 q++;
300 /* Yes this should be malloc not talloc. Don't change. */
302 chaine.buffer = SMB_MALLOC((q-p+1)*sizeof(uint16));
303 if (chaine.buffer == NULL)
304 return False;
306 memcpy(chaine.buffer, p, (q-p+1)*sizeof(uint16));
308 buffer->string_at_end -= (q-p+1)*sizeof(uint16);
310 if(!prs_set_offset(ps, buffer->string_at_end)) {
311 SAFE_FREE(chaine.buffer);
312 return False;
315 /* write the string */
316 if (!smb_io_unistr(desc, &chaine, ps, depth)) {
317 SAFE_FREE(chaine.buffer);
318 return False;
320 q++;
321 p=q;
323 SAFE_FREE(chaine.buffer);
326 if(!prs_set_offset(ps, struct_offset))
327 return False;
329 relative_offset=buffer->string_at_end - buffer->struct_start;
330 /* write its offset */
331 if (!prs_uint32("offset", ps, depth, &relative_offset))
332 return False;
334 } else {
336 /* UNMARSHALLING */
338 uint32 old_offset;
339 uint16 *chaine2=NULL;
340 int l_chaine=0;
341 int l_chaine2=0;
342 size_t realloc_size = 0;
344 *string=NULL;
346 /* read the offset */
347 if (!prs_uint32("offset", ps, depth, &buffer->string_at_end))
348 return False;
350 old_offset = prs_offset(ps);
351 if(!prs_set_offset(ps, buffer->string_at_end + buffer->struct_start))
352 return False;
354 do {
355 if (!smb_io_unistr(desc, &chaine, ps, depth))
356 return False;
358 l_chaine=str_len_uni(&chaine);
360 /* we're going to add two more bytes here in case this
361 is the last string in the array and we need to add
362 an extra NULL for termination */
363 if (l_chaine > 0)
365 uint16 *tc2;
367 realloc_size = (l_chaine2+l_chaine+2)*sizeof(uint16);
369 /* Yes this should be realloc - it's freed below. JRA */
371 if((tc2=(uint16 *)SMB_REALLOC(chaine2, realloc_size)) == NULL) {
372 SAFE_FREE(chaine2);
373 return False;
375 else chaine2 = tc2;
376 memcpy(chaine2+l_chaine2, chaine.buffer, (l_chaine+1)*sizeof(uint16));
377 l_chaine2+=l_chaine+1;
380 } while(l_chaine!=0);
382 /* the end should be bould NULL terminated so add
383 the second one here */
384 if (chaine2)
386 chaine2[l_chaine2] = '\0';
387 *string=(uint16 *)TALLOC_MEMDUP(prs_get_mem_context(ps),chaine2,realloc_size);
388 SAFE_FREE(chaine2);
391 if(!prs_set_offset(ps, old_offset))
392 return False;
394 return True;
397 /*******************************************************************
398 Parse a DEVMODE structure and its relative pointer.
399 ********************************************************************/
401 BOOL smb_io_relsecdesc(const char *desc, RPC_BUFFER *buffer, int depth, SEC_DESC **secdesc)
403 prs_struct *ps= &buffer->prs;
405 prs_debug(ps, depth, desc, "smb_io_relsecdesc");
406 depth++;
408 if (MARSHALLING(ps)) {
409 uint32 struct_offset = prs_offset(ps);
410 uint32 relative_offset;
412 if (! *secdesc) {
413 relative_offset = 0;
414 if (!prs_uint32("offset", ps, depth, &relative_offset))
415 return False;
416 return True;
419 if (*secdesc != NULL) {
420 buffer->string_at_end -= sec_desc_size(*secdesc);
422 if(!prs_set_offset(ps, buffer->string_at_end))
423 return False;
424 /* write the secdesc */
425 if (!sec_io_desc(desc, secdesc, ps, depth))
426 return False;
428 if(!prs_set_offset(ps, struct_offset))
429 return False;
432 relative_offset=buffer->string_at_end - buffer->struct_start;
433 /* write its offset */
435 if (!prs_uint32("offset", ps, depth, &relative_offset))
436 return False;
437 } else {
438 uint32 old_offset;
440 /* read the offset */
441 if (!prs_uint32("offset", ps, depth, &buffer->string_at_end))
442 return False;
444 old_offset = prs_offset(ps);
445 if(!prs_set_offset(ps, buffer->string_at_end + buffer->struct_start))
446 return False;
448 /* read the sd */
449 if (!sec_io_desc(desc, secdesc, ps, depth))
450 return False;
452 if(!prs_set_offset(ps, old_offset))
453 return False;
455 return True;
460 /*******************************************************************
461 * return the length of a UNICODE string in number of char, includes:
462 * - the leading zero
463 * - the relative pointer size
464 ********************************************************************/
466 uint32 size_of_relative_string(UNISTR *string)
468 uint32 size=0;
470 size=str_len_uni(string); /* the string length */
471 size=size+1; /* add the trailing zero */
472 size=size*2; /* convert in char */
473 size=size+4; /* add the size of the ptr */
475 #if 0 /* JERRY */
477 * Do not include alignment as Win2k does not align relative
478 * strings within a buffer --jerry
480 /* Ensure size is 4 byte multiple (prs_align is being called...). */
481 /* size += ((4 - (size & 3)) & 3); */
482 #endif
484 return size;