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 "reg_parse_prs.h"
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
));
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.
51 bool prs_init(prs_struct
*ps
, uint32 size
, TALLOC_CTX
*ctx
, bool io
)
55 ps
->bigendian_data
= RPC_LITTLE_ENDIAN
;
56 ps
->align
= RPC_PARSE_ALIGN
;
57 ps
->is_dynamic
= False
;
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
));
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
;
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
)
93 TALLOC_FREE(ps
->data_p
);
95 ps
->is_dynamic
= False
;
100 /*******************************************************************
101 Clear the memory in a parse structure.
102 ********************************************************************/
104 void prs_mem_clear(prs_struct
*ps
)
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
)
117 char *prs_alloc_mem(prs_struct
*ps
, size_t size
, unsigned int count
)
123 /* We can't call the type-safe version here. */
124 ret
= (char *)_talloc_zero_array(ps
->mem_ctx
, size
, count
,
130 /*******************************************************************
131 Return the current talloc context we're using.
132 ********************************************************************/
134 TALLOC_CTX
*prs_get_mem_context(prs_struct
*ps
)
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
);
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 */
170 TALLOC_FREE(ps
->data_p
);
172 ps
->data_p
= talloc_realloc(ps
->mem_ctx
,
177 if (ps
->data_p
== NULL
) {
178 DEBUG(0,("prs_set_buffer_size: Realloc failure for size %u.\n",
179 (unsigned int)newsize
));
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
)
197 ps
->grow_size
= MAX(ps
->grow_size
, ps
->data_offset
+ extra_space
);
199 if(ps
->data_offset
+ extra_space
<= ps
->buffer_size
)
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
));
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
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
));
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
,
245 if (ps
->data_p
== NULL
) {
246 DEBUG(0,("prs_grow: Realloc failure for size %u.\n",
247 (unsigned int)new_size
));
251 memset(&ps
->data_p
[ps
->buffer_size
], '\0', (size_t)(new_size
- ps
->buffer_size
));
253 ps
->buffer_size
= new_size
;
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
));
274 ps
->data_p
= (char *)talloc_realloc(ps
->mem_ctx
,
278 if(ps
->data_p
== NULL
) {
279 DEBUG(0,("prs_force_grow: Realloc failure for size %u.\n",
280 (unsigned int)new_size
));
284 memset(&ps
->data_p
[ps
->buffer_size
], '\0', (size_t)(new_size
- ps
->buffer_size
));
286 ps
->buffer_size
= new_size
;
291 /*******************************************************************
292 Get the data pointer (external interface).
293 ********************************************************************/
295 char *prs_data_p(prs_struct
*ps
)
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
)) {
329 ps
->data_offset
= offset
;
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)
342 if(!prs_grow(dst
, prs_offset(src
)))
345 memcpy(&dst
->data_p
[dst
->data_offset
], src
->data_p
, (size_t)prs_offset(src
));
346 dst
->data_offset
+= prs_offset(src
);
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
,
362 if(!prs_grow(dst
, len
)) {
366 memcpy(&dst
->data_p
[dst
->data_offset
], ((char *)src_base
) + start
, (size_t)len
);
367 dst
->data_offset
+= len
;
371 bool prs_append_some_prs_data(prs_struct
*dst
, prs_struct
*src
, int32 start
,
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
)
386 if(!prs_grow(dst
, len
))
389 memcpy(&dst
->data_p
[dst
->data_offset
], src
, (size_t)len
);
390 dst
->data_offset
+= len
;
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
)
404 if(!prs_mem_get(src
, len
))
407 memcpy(dst
, &src
->data_p
[src
->data_offset
], (size_t)len
);
408 src
->data_offset
+= len
;
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
);
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
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
))
450 memset(&ps
->data_p
[ps
->data_offset
], '\0', (size_t)extra_space
);
451 ps
->data_offset
+= extra_space
;
457 /******************************************************************
458 Align on a 2 byte boundary
459 *****************************************************************/
461 bool prs_align_uint16(prs_struct
*ps
)
464 uint8 old_align
= ps
->align
;
468 ps
->align
= old_align
;
473 /******************************************************************
474 Align on a 8 byte boundary
475 *****************************************************************/
477 bool prs_align_uint64(prs_struct
*ps
)
480 uint8 old_align
= ps
->align
;
484 ps
->align
= old_align
;
489 /******************************************************************
490 Align on a specific byte boundary
491 *****************************************************************/
493 bool prs_align_custom(prs_struct
*ps
, uint8 boundary
)
496 uint8 old_align
= ps
->align
;
498 ps
->align
= boundary
;
500 ps
->align
= old_align
;
507 /*******************************************************************
508 Align only if required (for the unistr2 string mainly)
509 ********************************************************************/
511 bool prs_align_needed(prs_struct
*ps
, uint32 needed
)
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
) ));
538 * Writing - grow the buffer if needed.
540 if(!prs_grow(ps
, extra_size
))
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
)
556 /*******************************************************************
558 ********************************************************************/
560 bool prs_uint8(const char *name
, prs_struct
*ps
, int depth
, uint8
*data8
)
562 char *q
= prs_mem_get(ps
, 1);
566 if (UNMARSHALLING(ps
))
571 DEBUGADD(5,("%s%04x %s: %02x\n", tab_depth(5,depth
), ps
->data_offset
, name
, *data8
));
573 ps
->data_offset
+= 1;
578 /*******************************************************************
580 ********************************************************************/
582 bool prs_uint16(const char *name
, prs_struct
*ps
, int depth
, uint16
*data16
)
584 char *q
= prs_mem_get(ps
, sizeof(uint16
));
588 if (UNMARSHALLING(ps
)) {
589 if (ps
->bigendian_data
)
590 *data16
= RSVAL(q
,0);
594 if (ps
->bigendian_data
)
600 DEBUGADD(5,("%s%04x %s: %04x\n", tab_depth(5,depth
), ps
->data_offset
, name
, *data16
));
602 ps
->data_offset
+= sizeof(uint16
);
607 /*******************************************************************
609 ********************************************************************/
611 bool prs_uint32(const char *name
, prs_struct
*ps
, int depth
, uint32
*data32
)
613 char *q
= prs_mem_get(ps
, sizeof(uint32
));
617 if (UNMARSHALLING(ps
)) {
618 if (ps
->bigendian_data
)
619 *data32
= RIVAL(q
,0);
623 if (ps
->bigendian_data
)
629 DEBUGADD(5,("%s%04x %s: %08x\n", tab_depth(5,depth
), ps
->data_offset
, name
, *data32
));
631 ps
->data_offset
+= sizeof(uint32
);
636 /*******************************************************************
638 ********************************************************************/
640 bool prs_int32(const char *name
, prs_struct
*ps
, int depth
, int32
*data32
)
642 char *q
= prs_mem_get(ps
, sizeof(int32
));
646 if (UNMARSHALLING(ps
)) {
647 if (ps
->bigendian_data
)
648 *data32
= RIVALS(q
,0);
650 *data32
= IVALS(q
,0);
652 if (ps
->bigendian_data
)
653 RSIVALS(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
);
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
)) {
673 if (!prs_uint32(name
, ps
, depth
+1, &low
))
676 if (!prs_uint32(name
, ps
, depth
+1, &high
))
679 *data64
= ((uint64_t)high
<< 32) + low
;
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
));
699 if (UNMARSHALLING(ps
)) {
700 if (ps
->bigendian_data
)
701 *status
= NT_STATUS(RIVAL(q
,0));
703 *status
= NT_STATUS(IVAL(q
,0));
705 if (ps
->bigendian_data
)
706 RSIVAL(q
,0,NT_STATUS_V(*status
));
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
);
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
)
726 char *q
= prs_mem_get(ps
, len
);
730 if (UNMARSHALLING(ps
)) {
731 for (i
= 0; i
< len
; i
++)
732 data8s
[i
] = CVAL(q
,i
);
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
));
740 print_asc(5, (unsigned char*)data8s
, len
);
742 for (i
= 0; i
< len
; i
++)
743 DEBUGADD(5,("%02x ", data8s
[i
]));
747 ps
->data_offset
+= len
;
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
)
759 char *q
= prs_mem_get(ps
, len
* sizeof(uint16
));
763 if (UNMARSHALLING(ps
)) {
764 if (ps
->bigendian_data
) {
765 for (i
= 0; i
< len
; i
++)
766 data16s
[i
] = RSVAL(q
, 2*i
);
768 for (i
= 0; i
< len
; i
++)
769 data16s
[i
] = SVAL(q
, 2*i
);
772 if (ps
->bigendian_data
) {
773 for (i
= 0; i
< len
; i
++)
774 RSSVAL(q
, 2*i
, data16s
[i
]);
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
));
783 print_asc(5, (unsigned char*)data16s
, 2*len
);
785 for (i
= 0; i
< len
; i
++)
786 DEBUGADD(5,("%04x ", data16s
[i
]));
790 ps
->data_offset
+= (len
* sizeof(uint16
));
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
)
802 char *q
= prs_mem_get(ps
, len
* sizeof(uint32
));
806 if (UNMARSHALLING(ps
)) {
807 if (ps
->bigendian_data
) {
808 for (i
= 0; i
< len
; i
++)
809 data32s
[i
] = RIVAL(q
, 4*i
);
811 for (i
= 0; i
< len
; i
++)
812 data32s
[i
] = IVAL(q
, 4*i
);
815 if (ps
->bigendian_data
) {
816 for (i
= 0; i
< len
; i
++)
817 RSIVAL(q
, 4*i
, data32s
[i
]);
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
));
826 print_asc(5, (unsigned char*)data32s
, 4*len
);
828 for (i
= 0; i
< len
; i
++)
829 DEBUGADD(5,("%08x ", data32s
[i
]));
833 ps
->data_offset
+= (len
* sizeof(uint32
));
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
))
847 if (!prs_copy_data_in(prs
, (char *)blob
->data
, blob
->length
))
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
))