diff options
Diffstat (limited to 'source3')
-rw-r--r-- | source3/include/smb.h | 3 | ||||
-rw-r--r-- | source3/lib/util.c | 51 |
2 files changed, 50 insertions, 4 deletions
diff --git a/source3/include/smb.h b/source3/include/smb.h index bbd3fd9f24..bdc718f57b 100644 --- a/source3/include/smb.h +++ b/source3/include/smb.h @@ -433,9 +433,10 @@ typedef struct files_struct } files_struct; /* used to hold an arbitrary blob of data */ -typedef struct { +typedef struct data_blob { uint8 *data; size_t length; + void (*free)(struct data_blob *data_blob); } DATA_BLOB; /* 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 @@ -2087,6 +2087,16 @@ BOOL unix_wild_match(char *pattern, char *string) } /******************************************************************* + 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() *******************************************************************/ DATA_BLOB data_blob(const void *p, size_t length) @@ -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); } |