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)
38 ret.data = talloc_memdup(NULL, p, length);
40 ret.data = talloc(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(TALLOC_CTX *mem_ctx, const void *p, size_t length)
56 DATA_BLOB ret = data_blob(p, length);
59 ret.data = talloc_steal(mem_ctx, ret.data);
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);
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)
76 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, length);
77 data_blob_clear(&blob);
81 /*******************************************************************
83 *******************************************************************/
84 void data_blob_free(DATA_BLOB *d)
93 /*******************************************************************
94 clear a DATA_BLOB's contents
95 *******************************************************************/
96 void data_blob_clear(DATA_BLOB *d)
99 memset(d->data, 0, d->length);
103 /*******************************************************************
104 free a data blob and clear its contents
105 *******************************************************************/
106 void data_blob_clear_free(DATA_BLOB *d)
113 /*******************************************************************
114 check if two data blobs are equal
115 *******************************************************************/
116 BOOL data_blob_equal(const DATA_BLOB *d1, const DATA_BLOB *d2)
118 if (d1->length != d2->length) {
121 if (d1->data == d2->data) {
124 if (d1->data == NULL || d2->data == NULL) {
127 if (memcmp(d1->data, d2->data, d1->length) == 0) {