r8141: Update volker's valgrind fix in r8097. Same effect, just helps me
[ira/wip.git] / source3 / lib / data_blob.c
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
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 /*******************************************************************
25  free() a data blob
26 *******************************************************************/
27 static void free_data_blob(DATA_BLOB *d)
28 {
29         if ((d) && (d->free)) {
30                 SAFE_FREE(d->data);
31         }
32 }
33
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
39 DATA_BLOB data_blob(const void *p, size_t length)
40 {
41         DATA_BLOB ret;
42
43         if (!length) {
44                 ZERO_STRUCT(ret);
45                 return ret;
46         }
47
48         if (p) {
49                 ret.data = smb_xmemdup(p, length);
50         } else {
51                 ret.data = SMB_XMALLOC_ARRAY(unsigned char, length);
52         }
53         ret.length = length;
54         ret.free = free_data_blob;
55         return ret;
56 }
57
58 /*******************************************************************
59  construct a data blob, using supplied TALLOC_CTX
60 *******************************************************************/
61 DATA_BLOB data_blob_talloc(TALLOC_CTX *mem_ctx, const void *p, size_t length)
62 {
63         DATA_BLOB ret;
64
65         if (!length) {
66                 ZERO_STRUCT(ret);
67                 return ret;
68         }
69
70         if (p) {
71                 ret.data = TALLOC_MEMDUP(mem_ctx, p, length);
72                 if (ret.data == NULL)
73                         smb_panic("data_blob_talloc: talloc_memdup failed.\n");
74         } else {
75                 ret.data = TALLOC(mem_ctx, length);
76                 if (ret.data == NULL)
77                         smb_panic("data_blob_talloc: talloc failed.\n");
78         }
79
80         ret.length = length;
81         ret.free = NULL;
82         return ret;
83 }
84
85 /*******************************************************************
86 free a data blob
87 *******************************************************************/
88 void data_blob_free(DATA_BLOB *d)
89 {
90         if (d) {
91                 if (d->free) {
92                         (d->free)(d);
93                 }
94                 d->length = 0;
95         }
96 }
97
98 /*******************************************************************
99 clear a DATA_BLOB's contents
100 *******************************************************************/
101 static void data_blob_clear(DATA_BLOB *d)
102 {
103         if (d->data) {
104                 memset(d->data, 0, d->length);
105         }
106 }
107
108 /*******************************************************************
109 free a data blob and clear its contents
110 *******************************************************************/
111 void data_blob_clear_free(DATA_BLOB *d)
112 {
113         data_blob_clear(d);
114         data_blob_free(d);
115 }
116