From 5a9c2f74ab0285859a6942bbc06d9e726cc69d19 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Sat, 5 Jan 2002 04:23:12 +0000 Subject: Add a talloc varient of the data_blob functions. Also change the structure so it has its own (optional) 'free' pointer - so we don't free() a talloc'ed version. also split out the data_blob_clear() functionaility. Andrew Bartlett (This used to be commit 207ee8aac42cf4b35f07e496b15fdeabe50950bc) --- source3/lib/util.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) (limited to 'source3/lib/util.c') diff --git a/source3/lib/util.c b/source3/lib/util.c index 2f2affffe9..63939e0ecf 100644 --- a/source3/lib/util.c +++ b/source3/lib/util.c @@ -2086,6 +2086,16 @@ BOOL unix_wild_match(char *pattern, char *string) return unix_do_match(p2, s2) == 0; } +/******************************************************************* + free() a data blob +*******************************************************************/ +static void free_data_blob(DATA_BLOB *d) +{ + if ((d) && (d->free)) { + SAFE_FREE(d->data); + } +} + /******************************************************************* construct a data blob, must be freed with data_blob_free() *******************************************************************/ @@ -2100,6 +2110,28 @@ DATA_BLOB data_blob(const void *p, size_t length) ret.data = smb_xmemdup(p, length); ret.length = length; + ret.free = free_data_blob; + return ret; +} + +/******************************************************************* + construct a data blob, using supplied TALLOC_CTX +*******************************************************************/ +DATA_BLOB data_blob_talloc(TALLOC_CTX *mem_ctx, const void *p, size_t length) +{ + DATA_BLOB ret; + + if (!p || !length) { + ZERO_STRUCT(ret); + return ret; + } + + ret.data = talloc_memdup(mem_ctx, p, length); + if (ret.data == NULL) + smb_panic("data_blob_talloc: talloc_memdup failed.\n"); + + ret.length = length; + ret.free = NULL; return ret; } @@ -2108,17 +2140,30 @@ free a data blob *******************************************************************/ void data_blob_free(DATA_BLOB *d) { - SAFE_FREE(d->data); + if (d) { + if (d->free) { + d->free(d); + } + ZERO_STRUCTP(d); + } } /******************************************************************* -free a data blob and clear its contents +clear a DATA_BLOB's contents *******************************************************************/ -void data_blob_clear_free(DATA_BLOB *d) +void data_blob_clear(DATA_BLOB *d) { if (d->data) { memset(d->data, 0, d->length); } +} + +/******************************************************************* +free a data blob and clear its contents +*******************************************************************/ +void data_blob_clear_free(DATA_BLOB *d) +{ + data_blob_clear(d); data_blob_free(d); } -- cgit