r4327: add usefull function if you don't want that the data will talloc_memdup()'ed
[Samba/gebeck_regimport.git] / source4 / lib / data_blob.c
blob18ecc2793ac7e14eab1dc417ee70fcedb391065a
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
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.
22 #include "includes.h"
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)
30 DATA_BLOB ret;
32 if (length == 0) {
33 ZERO_STRUCT(ret);
34 return ret;
37 if (p) {
38 ret.data = talloc_memdup(NULL, p, length);
39 } else {
40 ret.data = talloc(NULL, length);
42 if (ret.data == NULL) {
43 ret.length = 0;
44 return ret;
46 talloc_set_name_const(ret.data, name);
47 ret.length = length;
48 return ret;
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);
58 if (ret.data) {
59 talloc_steal(mem_ctx, ret.data);
61 return ret;
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);
75 if (!ret.data) {
76 return data_blob(NULL, 0);
78 return ret;
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);
90 return blob;
93 /*******************************************************************
94 free a data blob
95 *******************************************************************/
96 void data_blob_free(DATA_BLOB *d)
98 if (d) {
99 talloc_free(d->data);
100 d->data = NULL;
101 d->length = 0;
105 /*******************************************************************
106 clear a DATA_BLOB's contents
107 *******************************************************************/
108 void data_blob_clear(DATA_BLOB *d)
110 if (d->data) {
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)
120 data_blob_clear(d);
121 data_blob_free(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) {
131 return False;
133 if (d1->data == d2->data) {
134 return True;
136 if (d1->data == NULL || d2->data == NULL) {
137 return False;
139 if (memcmp(d1->data, d2->data, d1->length) == 0) {
140 return True;
142 return False;
145 /*******************************************************************
146 print the data_blob as hex string
147 *******************************************************************/
148 char *data_blob_hex_string(TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
150 int i;
151 char *hex_string;
153 hex_string = talloc_array_p(mem_ctx, char, (blob->length*2)+1);
154 if (!hex_string) {
155 return NULL;
158 for (i = 0; i < blob->length; i++)
159 slprintf(&hex_string[i*2], 3, "%02X", blob->data[i]);
161 return hex_string;
165 useful for constructing data blobs in test suites, while
166 avoiding const warnings
168 DATA_BLOB data_blob_string_const(const char *str)
170 DATA_BLOB blob;
171 blob.data = discard_const(str);
172 blob.length = strlen(str);
173 return blob;
176 DATA_BLOB data_blob_const(const void *p, size_t length)
178 DATA_BLOB blob;
179 blob.data = discard_const(p);
180 blob.length = length;
181 return blob;