r6369: update release notes
[Samba.git] / source / lib / data_blob.c
blob35805f861c5f4d0b75a8e7c89f29827abbffa27f
1 /*
2 Unix SMB/CIFS implementation.
3 Easy management of byte-length data
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Andrew Bartlett 2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 /*******************************************************************
25 free() a data blob
26 *******************************************************************/
27 static void free_data_blob(DATA_BLOB *d)
29 if ((d) && (d->free)) {
30 SAFE_FREE(d->data);
34 /*******************************************************************
35 construct a data blob, must be freed with data_blob_free()
36 you can pass NULL for p and get a blank data blob
37 *******************************************************************/
38 DATA_BLOB data_blob(const void *p, size_t length)
40 DATA_BLOB ret;
42 if (!length) {
43 ZERO_STRUCT(ret);
44 return ret;
47 if (p) {
48 ret.data = smb_xmemdup(p, length);
49 } else {
50 ret.data = SMB_XMALLOC_ARRAY(unsigned char, length);
52 ret.length = length;
53 ret.free = free_data_blob;
54 return ret;
57 /*******************************************************************
58 construct a data blob, using supplied TALLOC_CTX
59 *******************************************************************/
60 DATA_BLOB data_blob_talloc(TALLOC_CTX *mem_ctx, const void *p, size_t length)
62 DATA_BLOB ret;
64 if (!length) {
65 ZERO_STRUCT(ret);
66 return ret;
69 if (p) {
70 ret.data = TALLOC_MEMDUP(mem_ctx, p, length);
71 if (ret.data == NULL)
72 smb_panic("data_blob_talloc: talloc_memdup failed.\n");
73 } else {
74 ret.data = TALLOC(mem_ctx, length);
75 if (ret.data == NULL)
76 smb_panic("data_blob_talloc: talloc failed.\n");
79 ret.length = length;
80 ret.free = NULL;
81 return ret;
84 /*******************************************************************
85 free a data blob
86 *******************************************************************/
87 void data_blob_free(DATA_BLOB *d)
89 if (d) {
90 if (d->free) {
91 (d->free)(d);
93 d->length = 0;
97 /*******************************************************************
98 clear a DATA_BLOB's contents
99 *******************************************************************/
100 static void data_blob_clear(DATA_BLOB *d)
102 if (d->data) {
103 memset(d->data, 0, d->length);
107 /*******************************************************************
108 free a data blob and clear its contents
109 *******************************************************************/
110 void data_blob_clear_free(DATA_BLOB *d)
112 data_blob_clear(d);
113 data_blob_free(d);