s3:printing Make print_queue_receive public
[Samba/vl.git] / source3 / registry / reg_parse_prs.c
blobd831f19b79f5b8afdb058e0666575255e25944b5
1 /*
2 Unix SMB/CIFS implementation.
3 Samba memory buffer functions
4 Copyright (C) Andrew Tridgell 1992-1997
5 Copyright (C) Luke Kenneth Casson Leighton 1996-1997
6 Copyright (C) Jeremy Allison 1999
7 Copyright (C) Andrew Bartlett 2003.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "reg_parse_prs.h"
25 #include "rpc_dce.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_RPC_PARSE
30 /*******************************************************************
31 Debug output for parsing info
33 XXXX side-effect of this function is to increase the debug depth XXXX.
35 ********************************************************************/
37 void prs_debug(prs_struct *ps, int depth, const char *desc, const char *fn_name)
39 DEBUG(5+depth, ("%s%06x %s %s\n", tab_depth(5+depth,depth), ps->data_offset, fn_name, desc));
42 /**
43 * Initialise an expandable parse structure.
45 * @param size Initial buffer size. If >0, a new buffer will be
46 * created with talloc().
48 * @return False if allocation fails, otherwise True.
49 **/
51 bool prs_init(prs_struct *ps, uint32 size, TALLOC_CTX *ctx, bool io)
53 ZERO_STRUCTP(ps);
54 ps->io = io;
55 ps->bigendian_data = RPC_LITTLE_ENDIAN;
56 ps->align = RPC_PARSE_ALIGN;
57 ps->is_dynamic = False;
58 ps->data_offset = 0;
59 ps->buffer_size = 0;
60 ps->data_p = NULL;
61 ps->mem_ctx = ctx;
63 if (size != 0) {
64 ps->buffer_size = size;
65 ps->data_p = (char *)talloc_zero_size(ps->mem_ctx, size);
66 if(ps->data_p == NULL) {
67 DEBUG(0,("prs_init: talloc fail for %u bytes.\n", (unsigned int)size));
68 return False;
70 ps->is_dynamic = True; /* We own this memory. */
71 } else if (MARSHALLING(ps)) {
72 /* If size is zero and we're marshalling we should allocate memory on demand. */
73 ps->is_dynamic = True;
76 return True;
79 /*******************************************************************
80 Delete the memory in a parse structure - if we own it.
82 NOTE: Contrary to the somewhat confusing naming, this function is not
83 intended for freeing memory allocated by prs_alloc_mem().
84 That memory is also attached to the talloc context given by
85 ps->mem_ctx, but is only freed when that talloc context is
86 freed. prs_mem_free() is used to delete "dynamic" memory
87 allocated in marshalling/unmarshalling.
88 ********************************************************************/
90 void prs_mem_free(prs_struct *ps)
92 if(ps->is_dynamic) {
93 TALLOC_FREE(ps->data_p);
95 ps->is_dynamic = False;
96 ps->buffer_size = 0;
97 ps->data_offset = 0;
100 /*******************************************************************
101 Clear the memory in a parse structure.
102 ********************************************************************/
104 void prs_mem_clear(prs_struct *ps)
106 if (ps->buffer_size)
107 memset(ps->data_p, '\0', (size_t)ps->buffer_size);
110 /*******************************************************************
111 Allocate memory when unmarshalling... Always zero clears.
112 ********************************************************************/
114 #if defined(PARANOID_MALLOC_CHECKER)
115 char *prs_alloc_mem_(prs_struct *ps, size_t size, unsigned int count)
116 #else
117 char *prs_alloc_mem(prs_struct *ps, size_t size, unsigned int count)
118 #endif
120 char *ret = NULL;
122 if (size && count) {
123 /* We can't call the type-safe version here. */
124 ret = (char *)_talloc_zero_array(ps->mem_ctx, size, count,
125 "parse_prs");
127 return ret;
130 /*******************************************************************
131 Return the current talloc context we're using.
132 ********************************************************************/
134 TALLOC_CTX *prs_get_mem_context(prs_struct *ps)
136 return ps->mem_ctx;
139 /*******************************************************************
140 Hand some already allocated memory to a prs_struct.
141 If "is_dynamic" is true, this is a talloc_steal.
142 If "is_dynamic" is false, just make ps->data_p a pointer to
143 the unwritable memory.
144 ********************************************************************/
146 void prs_give_memory(prs_struct *ps, char *buf, uint32 size, bool is_dynamic)
148 ps->is_dynamic = is_dynamic;
149 if (ps->is_dynamic && buf) {
150 talloc_steal(ps->mem_ctx, buf);
152 ps->data_p = buf;
153 ps->buffer_size = size;
156 /*******************************************************************
157 Set a prs_struct to exactly a given size. Will grow or tuncate if neccessary.
158 ********************************************************************/
160 bool prs_set_buffer_size(prs_struct *ps, uint32 newsize)
162 if (newsize > ps->buffer_size)
163 return prs_force_grow(ps, newsize - ps->buffer_size);
165 if (newsize < ps->buffer_size) {
166 ps->buffer_size = newsize;
168 /* newsize == 0 acts as a free and set pointer to NULL */
169 if (newsize == 0) {
170 TALLOC_FREE(ps->data_p);
171 } else {
172 ps->data_p = talloc_realloc(ps->mem_ctx,
173 ps->data_p,
174 char,
175 newsize);
177 if (ps->data_p == NULL) {
178 DEBUG(0,("prs_set_buffer_size: Realloc failure for size %u.\n",
179 (unsigned int)newsize));
180 return False;
185 return True;
188 /*******************************************************************
189 Attempt, if needed, to grow a data buffer.
190 Also depends on the data stream mode (io).
191 ********************************************************************/
193 bool prs_grow(prs_struct *ps, uint32 extra_space)
195 uint32 new_size;
197 ps->grow_size = MAX(ps->grow_size, ps->data_offset + extra_space);
199 if(ps->data_offset + extra_space <= ps->buffer_size)
200 return True;
203 * We cannot grow the buffer if we're not reading
204 * into the prs_struct, or if we don't own the memory.
207 if(UNMARSHALLING(ps) || !ps->is_dynamic) {
208 DEBUG(0,("prs_grow: Buffer overflow - unable to expand buffer by %u bytes.\n",
209 (unsigned int)extra_space));
210 return False;
214 * Decide how much extra space we really need.
217 extra_space -= (ps->buffer_size - ps->data_offset);
218 if(ps->buffer_size == 0) {
221 * Start with 128 bytes (arbitrary value), enough for small rpc
222 * requests
224 new_size = MAX(128, extra_space);
226 ps->data_p = (char *)talloc_zero_size(ps->mem_ctx, new_size);
227 if(ps->data_p == NULL) {
228 DEBUG(0,("prs_grow: talloc failure for size %u.\n", (unsigned int)new_size));
229 return False;
231 } else {
233 * If the current buffer size is bigger than the space needed,
234 * just double it, else add extra_space. Always keep 64 bytes
235 * more, so that after we added a large blob we don't have to
236 * realloc immediately again.
238 new_size = MAX(ps->buffer_size*2,
239 ps->buffer_size + extra_space + 64);
241 ps->data_p = talloc_realloc(ps->mem_ctx,
242 ps->data_p,
243 char,
244 new_size);
245 if (ps->data_p == NULL) {
246 DEBUG(0,("prs_grow: Realloc failure for size %u.\n",
247 (unsigned int)new_size));
248 return False;
251 memset(&ps->data_p[ps->buffer_size], '\0', (size_t)(new_size - ps->buffer_size));
253 ps->buffer_size = new_size;
255 return True;
258 /*******************************************************************
259 Attempt to force a data buffer to grow by len bytes.
260 This is only used when appending more data onto a prs_struct
261 when reading an rpc reply, before unmarshalling it.
262 ********************************************************************/
264 bool prs_force_grow(prs_struct *ps, uint32 extra_space)
266 uint32 new_size = ps->buffer_size + extra_space;
268 if(!UNMARSHALLING(ps) || !ps->is_dynamic) {
269 DEBUG(0,("prs_force_grow: Buffer overflow - unable to expand buffer by %u bytes.\n",
270 (unsigned int)extra_space));
271 return False;
274 ps->data_p = (char *)talloc_realloc(ps->mem_ctx,
275 ps->data_p,
276 char,
277 new_size);
278 if(ps->data_p == NULL) {
279 DEBUG(0,("prs_force_grow: Realloc failure for size %u.\n",
280 (unsigned int)new_size));
281 return False;
284 memset(&ps->data_p[ps->buffer_size], '\0', (size_t)(new_size - ps->buffer_size));
286 ps->buffer_size = new_size;
288 return True;
291 /*******************************************************************
292 Get the data pointer (external interface).
293 ********************************************************************/
295 char *prs_data_p(prs_struct *ps)
297 return ps->data_p;
300 /*******************************************************************
301 Get the current data size (external interface).
302 ********************************************************************/
304 uint32 prs_data_size(prs_struct *ps)
306 return ps->buffer_size;
309 /*******************************************************************
310 Fetch the current offset (external interface).
311 ********************************************************************/
313 uint32 prs_offset(prs_struct *ps)
315 return ps->data_offset;
318 /*******************************************************************
319 Set the current offset (external interface).
320 ********************************************************************/
322 bool prs_set_offset(prs_struct *ps, uint32 offset)
324 if ((offset > ps->data_offset)
325 && !prs_grow(ps, offset - ps->data_offset)) {
326 return False;
329 ps->data_offset = offset;
330 return True;
333 /*******************************************************************
334 Append the data from one parse_struct into another.
335 ********************************************************************/
337 bool prs_append_prs_data(prs_struct *dst, prs_struct *src)
339 if (prs_offset(src) == 0)
340 return True;
342 if(!prs_grow(dst, prs_offset(src)))
343 return False;
345 memcpy(&dst->data_p[dst->data_offset], src->data_p, (size_t)prs_offset(src));
346 dst->data_offset += prs_offset(src);
348 return True;
351 /*******************************************************************
352 Append some data from one parse_struct into another.
353 ********************************************************************/
355 bool prs_append_some_data(prs_struct *dst, void *src_base, uint32_t start,
356 uint32_t len)
358 if (len == 0) {
359 return true;
362 if(!prs_grow(dst, len)) {
363 return false;
366 memcpy(&dst->data_p[dst->data_offset], ((char *)src_base) + start, (size_t)len);
367 dst->data_offset += len;
368 return true;
371 bool prs_append_some_prs_data(prs_struct *dst, prs_struct *src, int32 start,
372 uint32 len)
374 return prs_append_some_data(dst, src->data_p, start, len);
377 /*******************************************************************
378 Append the data from a buffer into a parse_struct.
379 ********************************************************************/
381 bool prs_copy_data_in(prs_struct *dst, const char *src, uint32 len)
383 if (len == 0)
384 return True;
386 if(!prs_grow(dst, len))
387 return False;
389 memcpy(&dst->data_p[dst->data_offset], src, (size_t)len);
390 dst->data_offset += len;
392 return True;
395 /*******************************************************************
396 Copy some data from a parse_struct into a buffer.
397 ********************************************************************/
399 bool prs_copy_data_out(char *dst, prs_struct *src, uint32 len)
401 if (len == 0)
402 return True;
404 if(!prs_mem_get(src, len))
405 return False;
407 memcpy(dst, &src->data_p[src->data_offset], (size_t)len);
408 src->data_offset += len;
410 return True;
413 /*******************************************************************
414 Copy all the data from a parse_struct into a buffer.
415 ********************************************************************/
417 bool prs_copy_all_data_out(char *dst, prs_struct *src)
419 uint32 len = prs_offset(src);
421 if (!len)
422 return True;
424 prs_set_offset(src, 0);
425 return prs_copy_data_out(dst, src, len);
428 /*******************************************************************
429 Set the data as X-endian (external interface).
430 ********************************************************************/
432 void prs_set_endian_data(prs_struct *ps, bool endian)
434 ps->bigendian_data = endian;
437 /*******************************************************************
438 Align a the data_len to a multiple of align bytes - filling with
439 zeros.
440 ********************************************************************/
442 bool prs_align(prs_struct *ps)
444 uint32 mod = ps->data_offset & (ps->align-1);
446 if (ps->align != 0 && mod != 0) {
447 uint32 extra_space = (ps->align - mod);
448 if(!prs_grow(ps, extra_space))
449 return False;
450 memset(&ps->data_p[ps->data_offset], '\0', (size_t)extra_space);
451 ps->data_offset += extra_space;
454 return True;
457 /******************************************************************
458 Align on a 2 byte boundary
459 *****************************************************************/
461 bool prs_align_uint16(prs_struct *ps)
463 bool ret;
464 uint8 old_align = ps->align;
466 ps->align = 2;
467 ret = prs_align(ps);
468 ps->align = old_align;
470 return ret;
473 /******************************************************************
474 Align on a 8 byte boundary
475 *****************************************************************/
477 bool prs_align_uint64(prs_struct *ps)
479 bool ret;
480 uint8 old_align = ps->align;
482 ps->align = 8;
483 ret = prs_align(ps);
484 ps->align = old_align;
486 return ret;
489 /******************************************************************
490 Align on a specific byte boundary
491 *****************************************************************/
493 bool prs_align_custom(prs_struct *ps, uint8 boundary)
495 bool ret;
496 uint8 old_align = ps->align;
498 ps->align = boundary;
499 ret = prs_align(ps);
500 ps->align = old_align;
502 return ret;
507 /*******************************************************************
508 Align only if required (for the unistr2 string mainly)
509 ********************************************************************/
511 bool prs_align_needed(prs_struct *ps, uint32 needed)
513 if (needed==0)
514 return True;
515 else
516 return prs_align(ps);
519 /*******************************************************************
520 Ensure we can read/write to a given offset.
521 ********************************************************************/
523 char *prs_mem_get(prs_struct *ps, uint32 extra_size)
525 if(UNMARSHALLING(ps)) {
527 * If reading, ensure that we can read the requested size item.
529 if (ps->data_offset + extra_size > ps->buffer_size) {
530 DEBUG(0,("prs_mem_get: reading data of size %u would overrun "
531 "buffer by %u bytes.\n",
532 (unsigned int)extra_size,
533 (unsigned int)(ps->data_offset + extra_size - ps->buffer_size) ));
534 return NULL;
536 } else {
538 * Writing - grow the buffer if needed.
540 if(!prs_grow(ps, extra_size))
541 return NULL;
543 return &ps->data_p[ps->data_offset];
546 /*******************************************************************
547 Change the struct type.
548 ********************************************************************/
550 void prs_switch_type(prs_struct *ps, bool io)
552 if ((ps->io ^ io) == True)
553 ps->io=io;
556 /*******************************************************************
557 Stream a uint8.
558 ********************************************************************/
560 bool prs_uint8(const char *name, prs_struct *ps, int depth, uint8 *data8)
562 char *q = prs_mem_get(ps, 1);
563 if (q == NULL)
564 return False;
566 if (UNMARSHALLING(ps))
567 *data8 = CVAL(q,0);
568 else
569 SCVAL(q,0,*data8);
571 DEBUGADD(5,("%s%04x %s: %02x\n", tab_depth(5,depth), ps->data_offset, name, *data8));
573 ps->data_offset += 1;
575 return True;
578 /*******************************************************************
579 Stream a uint16.
580 ********************************************************************/
582 bool prs_uint16(const char *name, prs_struct *ps, int depth, uint16 *data16)
584 char *q = prs_mem_get(ps, sizeof(uint16));
585 if (q == NULL)
586 return False;
588 if (UNMARSHALLING(ps)) {
589 if (ps->bigendian_data)
590 *data16 = RSVAL(q,0);
591 else
592 *data16 = SVAL(q,0);
593 } else {
594 if (ps->bigendian_data)
595 RSSVAL(q,0,*data16);
596 else
597 SSVAL(q,0,*data16);
600 DEBUGADD(5,("%s%04x %s: %04x\n", tab_depth(5,depth), ps->data_offset, name, *data16));
602 ps->data_offset += sizeof(uint16);
604 return True;
607 /*******************************************************************
608 Stream a uint32.
609 ********************************************************************/
611 bool prs_uint32(const char *name, prs_struct *ps, int depth, uint32 *data32)
613 char *q = prs_mem_get(ps, sizeof(uint32));
614 if (q == NULL)
615 return False;
617 if (UNMARSHALLING(ps)) {
618 if (ps->bigendian_data)
619 *data32 = RIVAL(q,0);
620 else
621 *data32 = IVAL(q,0);
622 } else {
623 if (ps->bigendian_data)
624 RSIVAL(q,0,*data32);
625 else
626 SIVAL(q,0,*data32);
629 DEBUGADD(5,("%s%04x %s: %08x\n", tab_depth(5,depth), ps->data_offset, name, *data32));
631 ps->data_offset += sizeof(uint32);
633 return True;
636 /*******************************************************************
637 Stream an int32.
638 ********************************************************************/
640 bool prs_int32(const char *name, prs_struct *ps, int depth, int32 *data32)
642 char *q = prs_mem_get(ps, sizeof(int32));
643 if (q == NULL)
644 return False;
646 if (UNMARSHALLING(ps)) {
647 if (ps->bigendian_data)
648 *data32 = RIVALS(q,0);
649 else
650 *data32 = IVALS(q,0);
651 } else {
652 if (ps->bigendian_data)
653 RSIVALS(q,0,*data32);
654 else
655 SIVALS(q,0,*data32);
658 DEBUGADD(5,("%s%04x %s: %08x\n", tab_depth(5,depth), ps->data_offset, name, *data32));
660 ps->data_offset += sizeof(int32);
662 return True;
665 /*******************************************************************
666 Stream a uint64_struct
667 ********************************************************************/
668 bool prs_uint64(const char *name, prs_struct *ps, int depth, uint64 *data64)
670 if (UNMARSHALLING(ps)) {
671 uint32 high, low;
673 if (!prs_uint32(name, ps, depth+1, &low))
674 return False;
676 if (!prs_uint32(name, ps, depth+1, &high))
677 return False;
679 *data64 = ((uint64_t)high << 32) + low;
681 return True;
682 } else {
683 uint32 high = (*data64) >> 32, low = (*data64) & 0xFFFFFFFF;
684 return prs_uint32(name, ps, depth+1, &low) &&
685 prs_uint32(name, ps, depth+1, &high);
689 /*******************************************************************
690 Stream a DCE error code
691 ********************************************************************/
693 bool prs_dcerpc_status(const char *name, prs_struct *ps, int depth, NTSTATUS *status)
695 char *q = prs_mem_get(ps, sizeof(uint32));
696 if (q == NULL)
697 return False;
699 if (UNMARSHALLING(ps)) {
700 if (ps->bigendian_data)
701 *status = NT_STATUS(RIVAL(q,0));
702 else
703 *status = NT_STATUS(IVAL(q,0));
704 } else {
705 if (ps->bigendian_data)
706 RSIVAL(q,0,NT_STATUS_V(*status));
707 else
708 SIVAL(q,0,NT_STATUS_V(*status));
711 DEBUGADD(5,("%s%04x %s: %s\n", tab_depth(5,depth), ps->data_offset, name,
712 dcerpc_errstr(talloc_tos(), NT_STATUS_V(*status))));
714 ps->data_offset += sizeof(uint32);
716 return True;
719 /******************************************************************
720 Stream an array of uint8s. Length is number of uint8s.
721 ********************************************************************/
723 bool prs_uint8s(bool charmode, const char *name, prs_struct *ps, int depth, uint8 *data8s, int len)
725 int i;
726 char *q = prs_mem_get(ps, len);
727 if (q == NULL)
728 return False;
730 if (UNMARSHALLING(ps)) {
731 for (i = 0; i < len; i++)
732 data8s[i] = CVAL(q,i);
733 } else {
734 for (i = 0; i < len; i++)
735 SCVAL(q, i, data8s[i]);
738 DEBUGADD(5,("%s%04x %s: ", tab_depth(5,depth), ps->data_offset ,name));
739 if (charmode)
740 print_asc(5, (unsigned char*)data8s, len);
741 else {
742 for (i = 0; i < len; i++)
743 DEBUGADD(5,("%02x ", data8s[i]));
745 DEBUGADD(5,("\n"));
747 ps->data_offset += len;
749 return True;
752 /******************************************************************
753 Stream an array of uint16s. Length is number of uint16s.
754 ********************************************************************/
756 bool prs_uint16s(bool charmode, const char *name, prs_struct *ps, int depth, uint16 *data16s, int len)
758 int i;
759 char *q = prs_mem_get(ps, len * sizeof(uint16));
760 if (q == NULL)
761 return False;
763 if (UNMARSHALLING(ps)) {
764 if (ps->bigendian_data) {
765 for (i = 0; i < len; i++)
766 data16s[i] = RSVAL(q, 2*i);
767 } else {
768 for (i = 0; i < len; i++)
769 data16s[i] = SVAL(q, 2*i);
771 } else {
772 if (ps->bigendian_data) {
773 for (i = 0; i < len; i++)
774 RSSVAL(q, 2*i, data16s[i]);
775 } else {
776 for (i = 0; i < len; i++)
777 SSVAL(q, 2*i, data16s[i]);
781 DEBUGADD(5,("%s%04x %s: ", tab_depth(5,depth), ps->data_offset, name));
782 if (charmode)
783 print_asc(5, (unsigned char*)data16s, 2*len);
784 else {
785 for (i = 0; i < len; i++)
786 DEBUGADD(5,("%04x ", data16s[i]));
788 DEBUGADD(5,("\n"));
790 ps->data_offset += (len * sizeof(uint16));
792 return True;
795 /******************************************************************
796 Stream an array of uint32s. Length is number of uint32s.
797 ********************************************************************/
799 bool prs_uint32s(bool charmode, const char *name, prs_struct *ps, int depth, uint32 *data32s, int len)
801 int i;
802 char *q = prs_mem_get(ps, len * sizeof(uint32));
803 if (q == NULL)
804 return False;
806 if (UNMARSHALLING(ps)) {
807 if (ps->bigendian_data) {
808 for (i = 0; i < len; i++)
809 data32s[i] = RIVAL(q, 4*i);
810 } else {
811 for (i = 0; i < len; i++)
812 data32s[i] = IVAL(q, 4*i);
814 } else {
815 if (ps->bigendian_data) {
816 for (i = 0; i < len; i++)
817 RSIVAL(q, 4*i, data32s[i]);
818 } else {
819 for (i = 0; i < len; i++)
820 SIVAL(q, 4*i, data32s[i]);
824 DEBUGADD(5,("%s%04x %s: ", tab_depth(5,depth), ps->data_offset, name));
825 if (charmode)
826 print_asc(5, (unsigned char*)data32s, 4*len);
827 else {
828 for (i = 0; i < len; i++)
829 DEBUGADD(5,("%08x ", data32s[i]));
831 DEBUGADD(5,("\n"));
833 ps->data_offset += (len * sizeof(uint32));
835 return True;
838 /*******************************************************************
839 creates a new prs_struct containing a DATA_BLOB
840 ********************************************************************/
841 bool prs_init_data_blob(prs_struct *prs, DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
843 if (!prs_init( prs, RPC_MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL ))
844 return False;
847 if (!prs_copy_data_in(prs, (char *)blob->data, blob->length))
848 return False;
850 return True;
853 /*******************************************************************
854 return the contents of a prs_struct in a DATA_BLOB
855 ********************************************************************/
856 bool prs_data_blob(prs_struct *prs, DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
858 blob->length = prs_data_size(prs);
859 blob->data = (uint8 *)TALLOC_ZERO_SIZE(mem_ctx, blob->length);
861 /* set the pointer at the end of the buffer */
862 prs_set_offset( prs, prs_data_size(prs) );
864 if (!prs_copy_all_data_out((char *)blob->data, prs))
865 return False;
867 return True;