r1983: a completely new implementation of talloc
[jelmer/samba4-debian.git] / source / 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 DATA_BLOB data_blob(const void *p, size_t length)
39 {
40         DATA_BLOB ret;
41
42         if (!length) {
43                 ZERO_STRUCT(ret);
44                 return ret;
45         }
46
47         if (p) {
48                 ret.data = smb_xmemdup(p, length);
49         } else {
50                 ret.data = smb_xmalloc(length);
51         }
52         ret.length = length;
53         ret.free = free_data_blob;
54         return ret;
55 }
56
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)
61 {
62         DATA_BLOB ret;
63
64         if (length == 0) {
65                 ZERO_STRUCT(ret);
66                 return ret;
67         }
68
69         if (p == NULL) {
70                 /* note that we do NOT zero memory in this case */
71                 ret.data = talloc(mem_ctx, length);
72                 if (ret.data == NULL) {
73                         smb_panic("data_blob_talloc: talloc_memdup failed.\n");
74                 }
75                 ret.length = length;
76                 ret.free = NULL;
77                 return ret;
78         }
79
80         ret.data = talloc_memdup(mem_ctx, p, length);
81         if (ret.data == NULL) {
82                 smb_panic("data_blob_talloc: talloc_memdup failed.\n");
83         }
84
85         ret.length = length;
86         ret.free = NULL;
87         return ret;
88 }
89
90 /*******************************************************************
91  construct a zero data blob, using supplied TALLOC_CTX. 
92  use this sparingly as it initialises data - better to initialise
93  yourself if you want specific data in the blob
94 *******************************************************************/
95 DATA_BLOB data_blob_talloc_zero(TALLOC_CTX *mem_ctx, size_t length)
96 {
97         DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, length);
98         data_blob_clear(&blob);
99         return blob;
100 }
101
102 /**
103  * Steal a talloc'ed DATA_BLOB from one context to another
104  */
105
106 DATA_BLOB data_blob_talloc_steal(TALLOC_CTX *old_ctx, TALLOC_CTX *new_ctx, 
107                                  DATA_BLOB *old) 
108 {
109         DATA_BLOB new;
110         new = *old;
111         new.data = talloc_steal(new_ctx, old->data);
112         if (new.data == NULL) {
113                 smb_panic("data_blob_talloc_steal: talloc_steal failed.\n");
114         }
115         return new;
116 }
117
118 /*******************************************************************
119 free a data blob
120 *******************************************************************/
121 void data_blob_free(DATA_BLOB *d)
122 {
123         if (d) {
124                 if (d->free) {
125                         (d->free)(d);
126                 }
127                 d->data = NULL;
128                 d->length = 0;
129         }
130 }
131
132 /*******************************************************************
133 clear a DATA_BLOB's contents
134 *******************************************************************/
135 void data_blob_clear(DATA_BLOB *d)
136 {
137         if (d->data) {
138                 memset(d->data, 0, d->length);
139         }
140 }
141
142 /*******************************************************************
143 free a data blob and clear its contents
144 *******************************************************************/
145 void data_blob_clear_free(DATA_BLOB *d)
146 {
147         data_blob_clear(d);
148         data_blob_free(d);
149 }
150
151
152 /*******************************************************************
153 check if two data blobs are equal
154 *******************************************************************/
155 BOOL data_blob_equal(const DATA_BLOB *d1, const DATA_BLOB *d2)
156 {
157         if (d1->length != d2->length) {
158                 return False;
159         }
160         if (d1->data == d2->data) {
161                 return True;
162         }
163         if (d1->data == NULL || d2->data == NULL) {
164                 return False;
165         }
166         if (memcmp(d1->data, d2->data, d1->length) == 0) {
167                 return True;
168         }
169         return False;
170 }
171