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 3 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, see <http://www.gnu.org/licenses/>.
23 const DATA_BLOB data_blob_null
= { NULL
, 0 };
27 * @brief Manipulation of arbitrary data blobs
31 construct a data blob, must be freed with data_blob_free()
32 you can pass NULL for p and get a blank data blob
34 _PUBLIC_ DATA_BLOB
data_blob_named(const void *p
, size_t length
, const char *name
)
38 if (p
== NULL
&& length
== 0) {
44 ret
.data
= (uint8_t *)talloc_memdup(NULL
, p
, length
);
46 ret
.data
= talloc_array(NULL
, uint8_t, length
);
48 if (ret
.data
== NULL
) {
52 talloc_set_name_const(ret
.data
, name
);
58 construct a data blob, using supplied TALLOC_CTX
60 _PUBLIC_ DATA_BLOB
data_blob_talloc_named(TALLOC_CTX
*mem_ctx
, const void *p
, size_t length
, const char *name
)
62 DATA_BLOB ret
= data_blob_named(p
, length
, name
);
65 talloc_steal(mem_ctx
, ret
.data
);
72 reference a data blob, to the supplied TALLOC_CTX.
73 Returns a NULL DATA_BLOB on failure
75 _PUBLIC_ DATA_BLOB
data_blob_talloc_reference(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
)
77 DATA_BLOB ret
= *blob
;
79 ret
.data
= talloc_reference(mem_ctx
, blob
->data
);
82 return data_blob(NULL
, 0);
88 construct a zero data blob, using supplied TALLOC_CTX.
89 use this sparingly as it initialises data - better to initialise
90 yourself if you want specific data in the blob
92 _PUBLIC_ DATA_BLOB
data_blob_talloc_zero(TALLOC_CTX
*mem_ctx
, size_t length
)
94 DATA_BLOB blob
= data_blob_talloc(mem_ctx
, NULL
, length
);
95 data_blob_clear(&blob
);
102 _PUBLIC_
void data_blob_free(DATA_BLOB
*d
)
105 talloc_free(d
->data
);
112 clear a DATA_BLOB's contents
114 _PUBLIC_
void data_blob_clear(DATA_BLOB
*d
)
117 memset(d
->data
, 0, d
->length
);
122 free a data blob and clear its contents
124 _PUBLIC_
void data_blob_clear_free(DATA_BLOB
*d
)
132 check if two data blobs are equal
134 _PUBLIC_
int data_blob_cmp(const DATA_BLOB
*d1
, const DATA_BLOB
*d2
)
137 if (d1
->data
== NULL
&& d2
->data
!= NULL
) {
140 if (d1
->data
!= NULL
&& d2
->data
== NULL
) {
143 if (d1
->data
== d2
->data
) {
144 return d1
->length
- d2
->length
;
146 ret
= memcmp(d1
->data
, d2
->data
, MIN(d1
->length
, d2
->length
));
148 return d1
->length
- d2
->length
;
154 print the data_blob as hex string
156 _PUBLIC_
char *data_blob_hex_string_lower(TALLOC_CTX
*mem_ctx
, const DATA_BLOB
*blob
)
161 hex_string
= talloc_array(mem_ctx
, char, (blob
->length
*2)+1);
166 /* this must be lowercase or w2k8 cannot join a samba domain,
167 as this routine is used to encode extended DNs and windows
168 only accepts lowercase hexadecimal numbers */
169 for (i
= 0; i
< blob
->length
; i
++)
170 slprintf(&hex_string
[i
*2], 3, "%02x", blob
->data
[i
]);
172 hex_string
[(blob
->length
*2)] = '\0';
176 _PUBLIC_
char *data_blob_hex_string_upper(TALLOC_CTX
*mem_ctx
, const DATA_BLOB
*blob
)
181 hex_string
= talloc_array(mem_ctx
, char, (blob
->length
*2)+1);
186 for (i
= 0; i
< blob
->length
; i
++)
187 slprintf(&hex_string
[i
*2], 3, "%02X", blob
->data
[i
]);
189 hex_string
[(blob
->length
*2)] = '\0';
194 useful for constructing data blobs in test suites, while
195 avoiding const warnings
197 _PUBLIC_ DATA_BLOB
data_blob_string_const(const char *str
)
200 blob
.data
= discard_const_p(uint8_t, str
);
201 blob
.length
= str
? strlen(str
) : 0;
206 useful for constructing data blobs in test suites, while
207 avoiding const warnings
209 _PUBLIC_ DATA_BLOB
data_blob_string_const_null(const char *str
)
212 blob
.data
= discard_const_p(uint8_t, str
);
213 blob
.length
= str
? strlen(str
)+1 : 0;
218 * Create a new data blob from const data
221 _PUBLIC_ DATA_BLOB
data_blob_const(const void *p
, size_t length
)
224 blob
.data
= discard_const_p(uint8_t, p
);
225 blob
.length
= length
;
233 _PUBLIC_
bool data_blob_realloc(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
, size_t length
)
235 blob
->data
= talloc_realloc(mem_ctx
, blob
->data
, uint8_t, length
);
236 if (blob
->data
== NULL
)
238 blob
->length
= length
;
244 append some data to a data blob
246 _PUBLIC_
bool data_blob_append(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
,
247 const void *p
, size_t length
)
249 size_t old_len
= blob
->length
;
250 size_t new_len
= old_len
+ length
;
251 if (new_len
< length
|| new_len
< old_len
) {
255 if ((const uint8_t *)p
+ length
< (const uint8_t *)p
) {
259 if (!data_blob_realloc(mem_ctx
, blob
, new_len
)) {
263 memcpy(blob
->data
+ old_len
, p
, length
);