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