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 /*******************************************************************
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)
32 if (p == NULL && length == 0) {
38 ret.data = talloc_memdup(NULL, p, length);
40 ret.data = talloc_size(NULL, length);
42 if (ret.data == NULL) {
46 talloc_set_name_const(ret.data, name);
51 /*******************************************************************
52 construct a data blob, using supplied TALLOC_CTX
53 *******************************************************************/
54 DATA_BLOB data_blob_talloc_named(TALLOC_CTX *mem_ctx, const void *p, size_t length, const char *name)
56 DATA_BLOB ret = data_blob_named(p, length, name);
59 talloc_steal(mem_ctx, ret.data);
65 /*******************************************************************
66 reference a data blob, to the supplied TALLOC_CTX.
67 Returns a NULL DATA_BLOB on failure
68 *******************************************************************/
69 DATA_BLOB data_blob_talloc_reference(TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
71 DATA_BLOB ret = *blob;
73 ret.data = talloc_reference(mem_ctx, blob->data);
76 return data_blob(NULL, 0);
81 /*******************************************************************
82 construct a zero data blob, using supplied TALLOC_CTX.
83 use this sparingly as it initialises data - better to initialise
84 yourself if you want specific data in the blob
85 *******************************************************************/
86 DATA_BLOB data_blob_talloc_zero(TALLOC_CTX *mem_ctx, size_t length)
88 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, length);
89 data_blob_clear(&blob);
93 /*******************************************************************
95 *******************************************************************/
96 void data_blob_free(DATA_BLOB *d)
105 /*******************************************************************
106 clear a DATA_BLOB's contents
107 *******************************************************************/
108 void data_blob_clear(DATA_BLOB *d)
111 memset(d->data, 0, d->length);
115 /*******************************************************************
116 free a data blob and clear its contents
117 *******************************************************************/
118 void data_blob_clear_free(DATA_BLOB *d)
125 /*******************************************************************
126 check if two data blobs are equal
127 *******************************************************************/
128 BOOL data_blob_equal(const DATA_BLOB *d1, const DATA_BLOB *d2)
130 if (d1->length != d2->length) {
133 if (d1->data == d2->data) {
136 if (d1->data == NULL || d2->data == NULL) {
139 if (memcmp(d1->data, d2->data, d1->length) == 0) {
145 /*******************************************************************
146 print the data_blob as hex string
147 *******************************************************************/
148 char *data_blob_hex_string(TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
153 hex_string = talloc_array(mem_ctx, char, (blob->length*2)+1);
158 for (i = 0; i < blob->length; i++)
159 slprintf(&hex_string[i*2], 3, "%02X", blob->data[i]);
165 useful for constructing data blobs in test suites, while
166 avoiding const warnings
168 DATA_BLOB data_blob_string_const(const char *str)
171 blob.data = discard_const(str);
172 blob.length = strlen(str);
176 DATA_BLOB data_blob_const(const void *p, size_t length)
179 blob.data = discard_const(p);
180 blob.length = length;
186 append some data to a data blob
188 NTSTATUS data_blob_append(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, void *p, size_t length)
190 blob->data = talloc_realloc_size(mem_ctx, blob->data, blob->length + length);
191 NT_STATUS_HAVE_NO_MEMORY(blob->data);
192 memcpy(blob->data + blob->length, p, length);
193 blob->length += length;