s3: tests: Add new test_stream_dir_rename.sh test.
[Samba.git] / lib / util / data_blob.c
blob677f7c192111ffa71f3353b89abb36f028f8c996
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 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/>.
21 #include "replace.h"
22 #include "attr.h"
23 #include "data_blob.h"
24 #include "lib/util/samba_util.h"
26 const DATA_BLOB data_blob_null = { NULL, 0 };
28 /**
29 * @file
30 * @brief Manipulation of arbitrary data blobs
31 **/
33 /**
34 construct a data blob, must be freed with data_blob_free()
35 you can pass NULL for p and get a blank data blob
36 **/
37 _PUBLIC_ DATA_BLOB data_blob_named(const void *p, size_t length, const char *name)
39 return data_blob_talloc_named(NULL, p, length, name);
42 /**
43 construct a data blob, using supplied TALLOC_CTX
44 **/
45 _PUBLIC_ DATA_BLOB data_blob_talloc_named(TALLOC_CTX *mem_ctx, const void *p, size_t length, const char *name)
47 DATA_BLOB ret;
49 if (p == NULL && length == 0) {
50 ZERO_STRUCT(ret);
51 return ret;
54 if (p) {
55 ret.data = (uint8_t *)talloc_memdup(mem_ctx, p, length);
56 } else {
57 ret.data = talloc_array(mem_ctx, uint8_t, length);
59 if (ret.data == NULL) {
60 ret.length = 0;
61 return ret;
63 talloc_set_name_const(ret.data, name);
64 ret.length = length;
65 return ret;
68 /**
69 construct a zero data blob, using supplied TALLOC_CTX.
70 use this sparingly as it initialises data - better to initialise
71 yourself if you want specific data in the blob
72 **/
73 _PUBLIC_ DATA_BLOB data_blob_talloc_zero(TALLOC_CTX *mem_ctx, size_t length)
75 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, length);
76 data_blob_clear(&blob);
77 return blob;
80 /**
81 free a data blob
82 **/
83 _PUBLIC_ void data_blob_free(DATA_BLOB *d)
85 if (d) {
86 TALLOC_FREE(d->data);
87 d->length = 0;
91 /**
92 clear a DATA_BLOB's contents
93 **/
94 _PUBLIC_ void data_blob_clear(DATA_BLOB *d)
96 if (d->data) {
97 memset_s(d->data, d->length, 0, d->length);
102 free a data blob and clear its contents
104 _PUBLIC_ void data_blob_clear_free(DATA_BLOB *d)
106 data_blob_clear(d);
107 data_blob_free(d);
112 check if two data blobs are equal
114 _PUBLIC_ int data_blob_cmp(const DATA_BLOB *d1, const DATA_BLOB *d2)
116 int ret;
117 if (d1->data == NULL && d2->data != NULL) {
118 return -1;
120 if (d1->data != NULL && d2->data == NULL) {
121 return 1;
123 if (d1->data == d2->data) {
124 return d1->length - d2->length;
126 ret = memcmp(d1->data, d2->data, MIN(d1->length, d2->length));
127 if (ret == 0) {
128 return d1->length - d2->length;
130 return ret;
134 check if two data blobs are equal, where the time taken should not depend on the
135 contents of either blob.
137 _PUBLIC_ bool data_blob_equal_const_time(const DATA_BLOB *d1, const DATA_BLOB *d2)
139 bool ret;
140 if (d1->data == NULL && d2->data != NULL) {
141 return false;
143 if (d1->data != NULL && d2->data == NULL) {
144 return false;
146 if (d1->length != d2->length) {
147 return false;
149 if (d1->data == d2->data) {
150 return true;
152 ret = mem_equal_const_time(d1->data, d2->data, d1->length);
153 return ret;
157 print the data_blob as hex string
159 _PUBLIC_ char *data_blob_hex_string_lower(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob)
161 size_t i;
162 char *hex_string;
164 hex_string = talloc_array(mem_ctx, char, (blob->length*2)+1);
165 if (!hex_string) {
166 return NULL;
169 /* this must be lowercase or w2k8 cannot join a samba domain,
170 as this routine is used to encode extended DNs and windows
171 only accepts lowercase hexadecimal numbers */
172 for (i = 0; i < blob->length; i++)
173 slprintf(&hex_string[i*2], 3, "%02x", blob->data[i]);
175 hex_string[(blob->length*2)] = '\0';
176 return hex_string;
179 _PUBLIC_ char *data_blob_hex_string_upper(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob)
181 size_t i;
182 char *hex_string;
184 hex_string = talloc_array(mem_ctx, char, (blob->length*2)+1);
185 if (!hex_string) {
186 return NULL;
189 for (i = 0; i < blob->length; i++)
190 slprintf(&hex_string[i*2], 3, "%02X", blob->data[i]);
192 hex_string[(blob->length*2)] = '\0';
193 return hex_string;
197 useful for constructing data blobs in test suites, while
198 avoiding const warnings
200 _PUBLIC_ DATA_BLOB data_blob_string_const(const char *str)
202 DATA_BLOB blob;
203 blob.data = discard_const_p(uint8_t, str);
204 blob.length = str ? strlen(str) : 0;
205 return blob;
209 useful for constructing data blobs in test suites, while
210 avoiding const warnings
212 _PUBLIC_ DATA_BLOB data_blob_string_const_null(const char *str)
214 DATA_BLOB blob;
215 blob.data = discard_const_p(uint8_t, str);
216 blob.length = str ? strlen(str)+1 : 0;
217 return blob;
221 * Create a new data blob from const data
224 _PUBLIC_ DATA_BLOB data_blob_const(const void *p, size_t length)
226 DATA_BLOB blob;
227 blob.data = discard_const_p(uint8_t, p);
228 blob.length = length;
229 return blob;
234 realloc a data_blob
236 _PUBLIC_ bool data_blob_realloc(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, size_t length)
238 uint8_t *tmp = talloc_realloc(mem_ctx, blob->data, uint8_t, length);
239 if (tmp == NULL) {
240 return false;
242 blob->data = tmp;
243 blob->length = length;
244 return true;
249 append some data to a data blob
251 _PUBLIC_ bool data_blob_append(TALLOC_CTX *mem_ctx, DATA_BLOB *blob,
252 const void *p, size_t length)
254 size_t old_len = blob->length;
255 size_t new_len = old_len + length;
257 if (length == 0) {
258 return true;
261 if (new_len < length || new_len < old_len) {
262 return false;
265 if ((const uint8_t *)p + length < (const uint8_t *)p) {
266 return false;
269 if (!data_blob_realloc(mem_ctx, blob, new_len)) {
270 return false;
273 memcpy(blob->data + old_len, p, length);
274 return true;
278 pad the length of a data blob to a multiple of
279 'pad'. 'pad' must be a power of two.
281 _PUBLIC_ bool data_blob_pad(TALLOC_CTX *mem_ctx, DATA_BLOB *blob,
282 size_t pad)
284 size_t old_len = blob->length;
285 size_t new_len = (old_len + pad - 1) & ~(pad - 1);
287 if (new_len < old_len) {
288 return false;
291 if (!data_blob_realloc(mem_ctx, blob, new_len)) {
292 return false;
295 memset(blob->data + old_len, 0, new_len - old_len);
296 return true;