summaryrefslogtreecommitdiff
path: root/source3/lib/util.c
diff options
context:
space:
mode:
authorAndrew Bartlett <abartlet@samba.org>2002-01-05 04:23:12 +0000
committerAndrew Bartlett <abartlet@samba.org>2002-01-05 04:23:12 +0000
commit5a9c2f74ab0285859a6942bbc06d9e726cc69d19 (patch)
treef9fa12f9185be06a4dbd11e2fe8cbdad2bfcc04e /source3/lib/util.c
parentaa045c08b26b9fad2b2f4f314b6ce2bfbefe4380 (diff)
downloadsamba-5a9c2f74ab0285859a6942bbc06d9e726cc69d19.tar.gz
samba-5a9c2f74ab0285859a6942bbc06d9e726cc69d19.tar.bz2
samba-5a9c2f74ab0285859a6942bbc06d9e726cc69d19.zip
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)
Diffstat (limited to 'source3/lib/util.c')
-rw-r--r--source3/lib/util.c51
1 files changed, 48 insertions, 3 deletions
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);
}