2 Unix SMB/CIFS implementation.
3 Easy management of byte-length data
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Andrew Bartlett 2001
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.
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.
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.
24 /*******************************************************************
26 *******************************************************************/
27 static void free_data_blob(DATA_BLOB *d)
29 if ((d) && (d->free)) {
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)
48 ret.data = smb_xmemdup(p, length);
50 ret.data = smb_xmalloc(length);
53 ret.free = free_data_blob;
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)
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");
80 ret.data = talloc_memdup(mem_ctx, p, length);
81 if (ret.data == NULL) {
82 smb_panic("data_blob_talloc: talloc_memdup failed.\n");
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)
97 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, length);
98 data_blob_clear(&blob);
102 /*******************************************************************
104 *******************************************************************/
105 void data_blob_free(DATA_BLOB *d)
116 /*******************************************************************
117 clear a DATA_BLOB's contents
118 *******************************************************************/
119 void data_blob_clear(DATA_BLOB *d)
122 memset(d->data, 0, d->length);
126 /*******************************************************************
127 free a data blob and clear its contents
128 *******************************************************************/
129 void data_blob_clear_free(DATA_BLOB *d)
136 /*******************************************************************
137 check if two data blobs are equal
138 *******************************************************************/
139 BOOL data_blob_equal(const DATA_BLOB *d1, const DATA_BLOB *d2)
141 if (d1->length != d2->length) {
144 if (d1->data == d2->data) {
147 if (d1->data == NULL || d2->data == NULL) {
150 if (memcmp(d1->data, d2->data, d1->length) == 0) {