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/>.
24 #include "../librpc/gen_ndr/ndr_schannel.h"
27 #define DBGC_CLASS DBGC_RPC_PARSE
30 * Dump a prs to a file: from the current location through to the end.
32 void prs_dump(const char *name, int v, prs_struct *ps)
34 prs_dump_region(name, v, ps, ps->data_offset, ps->buffer_size);
38 * Dump from the start of the prs to the current location.
40 void prs_dump_before(const char *name, int v, prs_struct *ps)
42 prs_dump_region(name, v, ps, 0, ps->data_offset);
46 * Dump everything from the start of the prs up to the current location.
48 void prs_dump_region(const char *name, int v, prs_struct *ps,
49 int from_off, int to_off)
54 if (DEBUGLEVEL < 50) return;
57 if (asprintf(&fname,"/tmp/%s_%d.%d.prs", name, v, i) < 0) {
61 if (asprintf(&fname,"/tmp/%s.%d.prs", name, i) < 0) {
65 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
66 if (fd != -1 || errno != EEXIST) break;
69 sz = write(fd, ps->data_p + from_off, to_off - from_off);
71 if ( (sz != to_off-from_off) || (i != 0) ) {
72 DEBUG(0,("Error writing/closing %s: %ld!=%ld %d\n", fname, (unsigned long)sz, (unsigned long)to_off-from_off, i ));
74 DEBUG(0,("created %s\n", fname));
80 /*******************************************************************
81 Debug output for parsing info
83 XXXX side-effect of this function is to increase the debug depth XXXX.
85 ********************************************************************/
87 void prs_debug(prs_struct *ps, int depth, const char *desc, const char *fn_name)
89 DEBUG(5+depth, ("%s%06x %s %s\n", tab_depth(5+depth,depth), ps->data_offset, fn_name, desc));
93 * Initialise an expandable parse structure.
95 * @param size Initial buffer size. If >0, a new buffer will be
96 * created with talloc().
98 * @return False if allocation fails, otherwise True.
101 bool prs_init(prs_struct *ps, uint32 size, TALLOC_CTX *ctx, bool io)
105 ps->bigendian_data = RPC_LITTLE_ENDIAN;
106 ps->align = RPC_PARSE_ALIGN;
107 ps->is_dynamic = False;
114 ps->buffer_size = size;
115 ps->data_p = (char *)talloc_zero_size(ps->mem_ctx, size);
116 if(ps->data_p == NULL) {
117 DEBUG(0,("prs_init: talloc fail for %u bytes.\n", (unsigned int)size));
120 ps->is_dynamic = True; /* We own this memory. */
121 } else if (MARSHALLING(ps)) {
122 /* If size is zero and we're marshalling we should allocate memory on demand. */
123 ps->is_dynamic = True;
129 /*******************************************************************
130 Delete the memory in a parse structure - if we own it.
132 NOTE: Contrary to the somewhat confusing naming, this function is not
133 intended for freeing memory allocated by prs_alloc_mem().
134 That memory is also attached to the talloc context given by
135 ps->mem_ctx, but is only freed when that talloc context is
136 freed. prs_mem_free() is used to delete "dynamic" memory
137 allocated in marshalling/unmarshalling.
138 ********************************************************************/
140 void prs_mem_free(prs_struct *ps)
143 TALLOC_FREE(ps->data_p);
145 ps->is_dynamic = False;
150 /*******************************************************************
151 Clear the memory in a parse structure.
152 ********************************************************************/
154 void prs_mem_clear(prs_struct *ps)
157 memset(ps->data_p, '\0', (size_t)ps->buffer_size);
160 /*******************************************************************
161 Allocate memory when unmarshalling... Always zero clears.
162 ********************************************************************/
164 #if defined(PARANOID_MALLOC_CHECKER)
165 char *prs_alloc_mem_(prs_struct *ps, size_t size, unsigned int count)
167 char *prs_alloc_mem(prs_struct *ps, size_t size, unsigned int count)
173 /* We can't call the type-safe version here. */
174 ret = (char *)_talloc_zero_array(ps->mem_ctx, size, count,
180 /*******************************************************************
181 Return the current talloc context we're using.
182 ********************************************************************/
184 TALLOC_CTX *prs_get_mem_context(prs_struct *ps)
189 /*******************************************************************
190 Hand some already allocated memory to a prs_struct.
191 If "is_dynamic" is true, this is a talloc_steal.
192 If "is_dynamic" is false, just make ps->data_p a pointer to
193 the unwritable memory.
194 ********************************************************************/
196 void prs_give_memory(prs_struct *ps, char *buf, uint32 size, bool is_dynamic)
198 ps->is_dynamic = is_dynamic;
199 if (ps->is_dynamic && buf) {
200 talloc_steal(ps->mem_ctx, buf);
203 ps->buffer_size = size;
206 /*******************************************************************
207 Set a prs_struct to exactly a given size. Will grow or tuncate if neccessary.
208 ********************************************************************/
210 bool prs_set_buffer_size(prs_struct *ps, uint32 newsize)
212 if (newsize > ps->buffer_size)
213 return prs_force_grow(ps, newsize - ps->buffer_size);
215 if (newsize < ps->buffer_size) {
216 ps->buffer_size = newsize;
218 /* newsize == 0 acts as a free and set pointer to NULL */
220 TALLOC_FREE(ps->data_p);
222 ps->data_p = talloc_realloc(ps->mem_ctx,
227 if (ps->data_p == NULL) {
228 DEBUG(0,("prs_set_buffer_size: Realloc failure for size %u.\n",
229 (unsigned int)newsize));
238 /*******************************************************************
239 Attempt, if needed, to grow a data buffer.
240 Also depends on the data stream mode (io).
241 ********************************************************************/
243 bool prs_grow(prs_struct *ps, uint32 extra_space)
247 ps->grow_size = MAX(ps->grow_size, ps->data_offset + extra_space);
249 if(ps->data_offset + extra_space <= ps->buffer_size)
253 * We cannot grow the buffer if we're not reading
254 * into the prs_struct, or if we don't own the memory.
257 if(UNMARSHALLING(ps) || !ps->is_dynamic) {
258 DEBUG(0,("prs_grow: Buffer overflow - unable to expand buffer by %u bytes.\n",
259 (unsigned int)extra_space));
264 * Decide how much extra space we really need.
267 extra_space -= (ps->buffer_size - ps->data_offset);
268 if(ps->buffer_size == 0) {
271 * Start with 128 bytes (arbitrary value), enough for small rpc
274 new_size = MAX(128, extra_space);
276 ps->data_p = (char *)talloc_zero_size(ps->mem_ctx, new_size);
277 if(ps->data_p == NULL) {
278 DEBUG(0,("prs_grow: talloc failure for size %u.\n", (unsigned int)new_size));
283 * If the current buffer size is bigger than the space needed,
284 * just double it, else add extra_space. Always keep 64 bytes
285 * more, so that after we added a large blob we don't have to
286 * realloc immediately again.
288 new_size = MAX(ps->buffer_size*2,
289 ps->buffer_size + extra_space + 64);
291 ps->data_p = talloc_realloc(ps->mem_ctx,
295 if (ps->data_p == NULL) {
296 DEBUG(0,("prs_grow: Realloc failure for size %u.\n",
297 (unsigned int)new_size));
301 memset(&ps->data_p[ps->buffer_size], '\0', (size_t)(new_size - ps->buffer_size));
303 ps->buffer_size = new_size;
308 /*******************************************************************
309 Attempt to force a data buffer to grow by len bytes.
310 This is only used when appending more data onto a prs_struct
311 when reading an rpc reply, before unmarshalling it.
312 ********************************************************************/
314 bool prs_force_grow(prs_struct *ps, uint32 extra_space)
316 uint32 new_size = ps->buffer_size + extra_space;
318 if(!UNMARSHALLING(ps) || !ps->is_dynamic) {
319 DEBUG(0,("prs_force_grow: Buffer overflow - unable to expand buffer by %u bytes.\n",
320 (unsigned int)extra_space));
324 ps->data_p = (char *)talloc_realloc(ps->mem_ctx,
328 if(ps->data_p == NULL) {
329 DEBUG(0,("prs_force_grow: Realloc failure for size %u.\n",
330 (unsigned int)new_size));
334 memset(&ps->data_p[ps->buffer_size], '\0', (size_t)(new_size - ps->buffer_size));
336 ps->buffer_size = new_size;
341 /*******************************************************************
342 Get the data pointer (external interface).
343 ********************************************************************/
345 char *prs_data_p(prs_struct *ps)
350 /*******************************************************************
351 Get the current data size (external interface).
352 ********************************************************************/
354 uint32 prs_data_size(prs_struct *ps)
356 return ps->buffer_size;
359 /*******************************************************************
360 Fetch the current offset (external interface).
361 ********************************************************************/
363 uint32 prs_offset(prs_struct *ps)
365 return ps->data_offset;
368 /*******************************************************************
369 Set the current offset (external interface).
370 ********************************************************************/
372 bool prs_set_offset(prs_struct *ps, uint32 offset)
374 if ((offset > ps->data_offset)
375 && !prs_grow(ps, offset - ps->data_offset)) {
379 ps->data_offset = offset;
383 /*******************************************************************
384 Append the data from one parse_struct into another.
385 ********************************************************************/
387 bool prs_append_prs_data(prs_struct *dst, prs_struct *src)
389 if (prs_offset(src) == 0)
392 if(!prs_grow(dst, prs_offset(src)))
395 memcpy(&dst->data_p[dst->data_offset], src->data_p, (size_t)prs_offset(src));
396 dst->data_offset += prs_offset(src);
401 /*******************************************************************
402 Append some data from one parse_struct into another.
403 ********************************************************************/
405 bool prs_append_some_data(prs_struct *dst, void *src_base, uint32_t start,
412 if(!prs_grow(dst, len)) {
416 memcpy(&dst->data_p[dst->data_offset], ((char *)src_base) + start, (size_t)len);
417 dst->data_offset += len;
421 bool prs_append_some_prs_data(prs_struct *dst, prs_struct *src, int32 start,
424 return prs_append_some_data(dst, src->data_p, start, len);
427 /*******************************************************************
428 Append the data from a buffer into a parse_struct.
429 ********************************************************************/
431 bool prs_copy_data_in(prs_struct *dst, const char *src, uint32 len)
436 if(!prs_grow(dst, len))
439 memcpy(&dst->data_p[dst->data_offset], src, (size_t)len);
440 dst->data_offset += len;
445 /*******************************************************************
446 Copy some data from a parse_struct into a buffer.
447 ********************************************************************/
449 bool prs_copy_data_out(char *dst, prs_struct *src, uint32 len)
454 if(!prs_mem_get(src, len))
457 memcpy(dst, &src->data_p[src->data_offset], (size_t)len);
458 src->data_offset += len;
463 /*******************************************************************
464 Copy all the data from a parse_struct into a buffer.
465 ********************************************************************/
467 bool prs_copy_all_data_out(char *dst, prs_struct *src)
469 uint32 len = prs_offset(src);
474 prs_set_offset(src, 0);
475 return prs_copy_data_out(dst, src, len);
478 /*******************************************************************
479 Set the data as X-endian (external interface).
480 ********************************************************************/
482 void prs_set_endian_data(prs_struct *ps, bool endian)
484 ps->bigendian_data = endian;
487 /*******************************************************************
488 Align a the data_len to a multiple of align bytes - filling with
490 ********************************************************************/
492 bool prs_align(prs_struct *ps)
494 uint32 mod = ps->data_offset & (ps->align-1);
496 if (ps->align != 0 && mod != 0) {
497 uint32 extra_space = (ps->align - mod);
498 if(!prs_grow(ps, extra_space))
500 memset(&ps->data_p[ps->data_offset], '\0', (size_t)extra_space);
501 ps->data_offset += extra_space;
507 /******************************************************************
508 Align on a 2 byte boundary
509 *****************************************************************/
511 bool prs_align_uint16(prs_struct *ps)
514 uint8 old_align = ps->align;
518 ps->align = old_align;
523 /******************************************************************
524 Align on a 8 byte boundary
525 *****************************************************************/
527 bool prs_align_uint64(prs_struct *ps)
530 uint8 old_align = ps->align;
534 ps->align = old_align;
539 /******************************************************************
540 Align on a specific byte boundary
541 *****************************************************************/
543 bool prs_align_custom(prs_struct *ps, uint8 boundary)
546 uint8 old_align = ps->align;
548 ps->align = boundary;
550 ps->align = old_align;
557 /*******************************************************************
558 Align only if required (for the unistr2 string mainly)
559 ********************************************************************/
561 bool prs_align_needed(prs_struct *ps, uint32 needed)
566 return prs_align(ps);
569 /*******************************************************************
570 Ensure we can read/write to a given offset.
571 ********************************************************************/
573 char *prs_mem_get(prs_struct *ps, uint32 extra_size)
575 if(UNMARSHALLING(ps)) {
577 * If reading, ensure that we can read the requested size item.
579 if (ps->data_offset + extra_size > ps->buffer_size) {
580 DEBUG(0,("prs_mem_get: reading data of size %u would overrun "
581 "buffer by %u bytes.\n",
582 (unsigned int)extra_size,
583 (unsigned int)(ps->data_offset + extra_size - ps->buffer_size) ));
588 * Writing - grow the buffer if needed.
590 if(!prs_grow(ps, extra_size))
593 return &ps->data_p[ps->data_offset];
596 /*******************************************************************
597 Change the struct type.
598 ********************************************************************/
600 void prs_switch_type(prs_struct *ps, bool io)
602 if ((ps->io ^ io) == True)
606 /*******************************************************************
608 ********************************************************************/
610 bool prs_uint8(const char *name, prs_struct *ps, int depth, uint8 *data8)
612 char *q = prs_mem_get(ps, 1);
616 if (UNMARSHALLING(ps))
621 DEBUGADD(5,("%s%04x %s: %02x\n", tab_depth(5,depth), ps->data_offset, name, *data8));
623 ps->data_offset += 1;
628 /*******************************************************************
630 ********************************************************************/
632 bool prs_uint16(const char *name, prs_struct *ps, int depth, uint16 *data16)
634 char *q = prs_mem_get(ps, sizeof(uint16));
638 if (UNMARSHALLING(ps)) {
639 if (ps->bigendian_data)
640 *data16 = RSVAL(q,0);
644 if (ps->bigendian_data)
650 DEBUGADD(5,("%s%04x %s: %04x\n", tab_depth(5,depth), ps->data_offset, name, *data16));
652 ps->data_offset += sizeof(uint16);
657 /*******************************************************************
659 ********************************************************************/
661 bool prs_uint32(const char *name, prs_struct *ps, int depth, uint32 *data32)
663 char *q = prs_mem_get(ps, sizeof(uint32));
667 if (UNMARSHALLING(ps)) {
668 if (ps->bigendian_data)
669 *data32 = RIVAL(q,0);
673 if (ps->bigendian_data)
679 DEBUGADD(5,("%s%04x %s: %08x\n", tab_depth(5,depth), ps->data_offset, name, *data32));
681 ps->data_offset += sizeof(uint32);
686 /*******************************************************************
688 ********************************************************************/
690 bool prs_int32(const char *name, prs_struct *ps, int depth, int32 *data32)
692 char *q = prs_mem_get(ps, sizeof(int32));
696 if (UNMARSHALLING(ps)) {
697 if (ps->bigendian_data)
698 *data32 = RIVALS(q,0);
700 *data32 = IVALS(q,0);
702 if (ps->bigendian_data)
703 RSIVALS(q,0,*data32);
708 DEBUGADD(5,("%s%04x %s: %08x\n", tab_depth(5,depth), ps->data_offset, name, *data32));
710 ps->data_offset += sizeof(int32);
715 /*******************************************************************
716 Stream a uint64_struct
717 ********************************************************************/
718 bool prs_uint64(const char *name, prs_struct *ps, int depth, uint64 *data64)
720 if (UNMARSHALLING(ps)) {
723 if (!prs_uint32(name, ps, depth+1, &low))
726 if (!prs_uint32(name, ps, depth+1, &high))
729 *data64 = ((uint64_t)high << 32) + low;
733 uint32 high = (*data64) >> 32, low = (*data64) & 0xFFFFFFFF;
734 return prs_uint32(name, ps, depth+1, &low) &&
735 prs_uint32(name, ps, depth+1, &high);
739 /*******************************************************************
740 Stream a DCE error code
741 ********************************************************************/
743 bool prs_dcerpc_status(const char *name, prs_struct *ps, int depth, NTSTATUS *status)
745 char *q = prs_mem_get(ps, sizeof(uint32));
749 if (UNMARSHALLING(ps)) {
750 if (ps->bigendian_data)
751 *status = NT_STATUS(RIVAL(q,0));
753 *status = NT_STATUS(IVAL(q,0));
755 if (ps->bigendian_data)
756 RSIVAL(q,0,NT_STATUS_V(*status));
758 SIVAL(q,0,NT_STATUS_V(*status));
761 DEBUGADD(5,("%s%04x %s: %s\n", tab_depth(5,depth), ps->data_offset, name,
762 dcerpc_errstr(talloc_tos(), NT_STATUS_V(*status))));
764 ps->data_offset += sizeof(uint32);
769 /******************************************************************
770 Stream an array of uint8s. Length is number of uint8s.
771 ********************************************************************/
773 bool prs_uint8s(bool charmode, const char *name, prs_struct *ps, int depth, uint8 *data8s, int len)
776 char *q = prs_mem_get(ps, len);
780 if (UNMARSHALLING(ps)) {
781 for (i = 0; i < len; i++)
782 data8s[i] = CVAL(q,i);
784 for (i = 0; i < len; i++)
785 SCVAL(q, i, data8s[i]);
788 DEBUGADD(5,("%s%04x %s: ", tab_depth(5,depth), ps->data_offset ,name));
790 print_asc(5, (unsigned char*)data8s, len);
792 for (i = 0; i < len; i++)
793 DEBUGADD(5,("%02x ", data8s[i]));
797 ps->data_offset += len;
802 /******************************************************************
803 Stream an array of uint16s. Length is number of uint16s.
804 ********************************************************************/
806 bool prs_uint16s(bool charmode, const char *name, prs_struct *ps, int depth, uint16 *data16s, int len)
809 char *q = prs_mem_get(ps, len * sizeof(uint16));
813 if (UNMARSHALLING(ps)) {
814 if (ps->bigendian_data) {
815 for (i = 0; i < len; i++)
816 data16s[i] = RSVAL(q, 2*i);
818 for (i = 0; i < len; i++)
819 data16s[i] = SVAL(q, 2*i);
822 if (ps->bigendian_data) {
823 for (i = 0; i < len; i++)
824 RSSVAL(q, 2*i, data16s[i]);
826 for (i = 0; i < len; i++)
827 SSVAL(q, 2*i, data16s[i]);
831 DEBUGADD(5,("%s%04x %s: ", tab_depth(5,depth), ps->data_offset, name));
833 print_asc(5, (unsigned char*)data16s, 2*len);
835 for (i = 0; i < len; i++)
836 DEBUGADD(5,("%04x ", data16s[i]));
840 ps->data_offset += (len * sizeof(uint16));
845 /******************************************************************
846 Stream an array of uint32s. Length is number of uint32s.
847 ********************************************************************/
849 bool prs_uint32s(bool charmode, const char *name, prs_struct *ps, int depth, uint32 *data32s, int len)
852 char *q = prs_mem_get(ps, len * sizeof(uint32));
856 if (UNMARSHALLING(ps)) {
857 if (ps->bigendian_data) {
858 for (i = 0; i < len; i++)
859 data32s[i] = RIVAL(q, 4*i);
861 for (i = 0; i < len; i++)
862 data32s[i] = IVAL(q, 4*i);
865 if (ps->bigendian_data) {
866 for (i = 0; i < len; i++)
867 RSIVAL(q, 4*i, data32s[i]);
869 for (i = 0; i < len; i++)
870 SIVAL(q, 4*i, data32s[i]);
874 DEBUGADD(5,("%s%04x %s: ", tab_depth(5,depth), ps->data_offset, name));
876 print_asc(5, (unsigned char*)data32s, 4*len);
878 for (i = 0; i < len; i++)
879 DEBUGADD(5,("%08x ", data32s[i]));
883 ps->data_offset += (len * sizeof(uint32));
888 /*******************************************************************
889 creates a new prs_struct containing a DATA_BLOB
890 ********************************************************************/
891 bool prs_init_data_blob(prs_struct *prs, DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
893 if (!prs_init( prs, RPC_MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL ))
897 if (!prs_copy_data_in(prs, (char *)blob->data, blob->length))
903 /*******************************************************************
904 return the contents of a prs_struct in a DATA_BLOB
905 ********************************************************************/
906 bool prs_data_blob(prs_struct *prs, DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
908 blob->length = prs_data_size(prs);
909 blob->data = (uint8 *)TALLOC_ZERO_SIZE(mem_ctx, blob->length);
911 /* set the pointer at the end of the buffer */
912 prs_set_offset( prs, prs_data_size(prs) );
914 if (!prs_copy_all_data_out((char *)blob->data, prs))