r2653: - data_blob() and data_blob_talloc() now get automatic names
[samba.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  construct a data blob, must be freed with data_blob_free()
26  you can pass NULL for p and get a blank data blob
27 *******************************************************************/
28 DATA_BLOB data_blob_named(const void *p, size_t length, const char *name)
29 {
30         DATA_BLOB ret;
31
32         if (length == 0) {
33                 ZERO_STRUCT(ret);
34                 return ret;
35         }
36
37         if (p) {
38                 ret.data = talloc_memdup(NULL, p, length);
39         } else {
40                 ret.data = talloc(NULL, length);
41         }
42         if (ret.data == NULL) {
43                 ret.length = 0;
44                 return ret;
45         }
46         talloc_set_name_const(ret.data, name);
47         ret.length = length;
48         return ret;
49 }
50
51 /*******************************************************************
52  construct a data blob, using supplied TALLOC_CTX
53 *******************************************************************/
54 DATA_BLOB data_blob_talloc(TALLOC_CTX *mem_ctx, const void *p, size_t length)
55 {
56         DATA_BLOB ret = data_blob(p, length);
57
58         if (ret.data) {
59                 ret.data = talloc_steal(mem_ctx, ret.data);
60         } else {
61                 /* this ensures the blob has the context attached, so a zero length call
62                    to data_blob_talloc followed by a realloc doesn't cause the memory to come
63                    from the NULL context */
64                 ret.data = talloc(mem_ctx, 0);
65         }
66         return ret;
67 }
68
69 /*******************************************************************
70  construct a zero data blob, using supplied TALLOC_CTX. 
71  use this sparingly as it initialises data - better to initialise
72  yourself if you want specific data in the blob
73 *******************************************************************/
74 DATA_BLOB data_blob_talloc_zero(TALLOC_CTX *mem_ctx, size_t length)
75 {
76         DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, length);
77         data_blob_clear(&blob);
78         return blob;
79 }
80
81 /*******************************************************************
82 free a data blob
83 *******************************************************************/
84 void data_blob_free(DATA_BLOB *d)
85 {
86         if (d) {
87                 talloc_free(d->data);
88                 d->data = NULL;
89                 d->length = 0;
90         }
91 }
92
93 /*******************************************************************
94 clear a DATA_BLOB's contents
95 *******************************************************************/
96 void data_blob_clear(DATA_BLOB *d)
97 {
98         if (d->data) {
99                 memset(d->data, 0, d->length);
100         }
101 }
102
103 /*******************************************************************
104 free a data blob and clear its contents
105 *******************************************************************/
106 void data_blob_clear_free(DATA_BLOB *d)
107 {
108         data_blob_clear(d);
109         data_blob_free(d);
110 }
111
112
113 /*******************************************************************
114 check if two data blobs are equal
115 *******************************************************************/
116 BOOL data_blob_equal(const DATA_BLOB *d1, const DATA_BLOB *d2)
117 {
118         if (d1->length != d2->length) {
119                 return False;
120         }
121         if (d1->data == d2->data) {
122                 return True;
123         }
124         if (d1->data == NULL || d2->data == NULL) {
125                 return False;
126         }
127         if (memcmp(d1->data, d2->data, d1->length) == 0) {
128                 return True;
129         }
130         return False;
131 }
132