r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[kai/samba.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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22
23 const DATA_BLOB data_blob_null = { NULL, 0, NULL };
24
25 /*******************************************************************
26  Free() a data blob.
27 *******************************************************************/
28
29 static void free_data_blob(DATA_BLOB *d)
30 {
31         if ((d) && (d->free)) {
32                 SAFE_FREE(d->data);
33         }
34 }
35
36 /*******************************************************************
37  Construct a data blob, must be freed with data_blob_free().
38  You can pass NULL for p and get a blank data blob
39 *******************************************************************/
40
41 DATA_BLOB data_blob(const void *p, size_t length)
42 {
43         DATA_BLOB ret;
44
45         if (!length) {
46                 ZERO_STRUCT(ret);
47                 return ret;
48         }
49
50         if (p) {
51                 ret.data = (uint8 *)smb_xmemdup(p, length);
52         } else {
53                 ret.data = SMB_XMALLOC_ARRAY(uint8, length);
54         }
55         ret.length = length;
56         ret.free = free_data_blob;
57         return ret;
58 }
59
60 /*******************************************************************
61  Construct a data blob, using supplied TALLOC_CTX.
62 *******************************************************************/
63
64 DATA_BLOB data_blob_talloc(TALLOC_CTX *mem_ctx, const void *p, size_t length)
65 {
66         DATA_BLOB ret;
67
68         if (!length) {
69                 ZERO_STRUCT(ret);
70                 return ret;
71         }
72
73         if (p) {
74                 ret.data = (uint8 *)TALLOC_MEMDUP(mem_ctx, p, length);
75                 if (ret.data == NULL)
76                         smb_panic("data_blob_talloc: TALLOC_MEMDUP failed");
77         } else {
78                 ret.data = (uint8 *)TALLOC(mem_ctx, length);
79                 if (ret.data == NULL)
80                         smb_panic("data_blob_talloc: TALLOC failed");
81         }
82
83         ret.length = length;
84         ret.free = NULL;
85         return ret;
86 }
87
88 /*******************************************************************
89  Free a data blob.
90 *******************************************************************/
91
92 void data_blob_free(DATA_BLOB *d)
93 {
94         if (d) {
95                 if (d->free) {
96                         (d->free)(d);
97                 }
98                 d->length = 0;
99         }
100 }
101
102 /*******************************************************************
103  Clear a DATA_BLOB's contents
104 *******************************************************************/
105
106 void data_blob_clear(DATA_BLOB *d)
107 {
108         if (d->data) {
109                 memset(d->data, 0, d->length);
110         }
111 }
112
113 /*******************************************************************
114  Free a data blob and clear its contents
115 *******************************************************************/
116
117 void data_blob_clear_free(DATA_BLOB *d)
118 {
119         data_blob_clear(d);
120         data_blob_free(d);
121 }
122
123 /**
124   useful for constructing data blobs in test suites, while
125   avoiding const warnings
126 **/
127 DATA_BLOB data_blob_string_const(const char *str)
128 {
129         DATA_BLOB blob;
130         blob.data = CONST_DISCARD(uint8 *, str);
131         blob.length = strlen(str);
132         blob.free = NULL;
133         return blob;
134 }
135
136 /**
137  * Create a new data blob from const data 
138  */
139 DATA_BLOB data_blob_const(const void *p, size_t length)
140 {
141         DATA_BLOB blob;
142         blob.data = CONST_DISCARD(uint8 *, p);
143         blob.length = length;
144         blob.free = NULL;
145         return blob;
146 }