sharesec: Check if share exists in configuration
[samba.git] / lib / util / data_blob.c
blob9372612d8a3e4e12bb1f2fd06aa3300a057949e7
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"
25 #include "lib/util/tsort.h"
27 const DATA_BLOB data_blob_null = { NULL, 0 };
29 /**
30 * @file
31 * @brief Manipulation of arbitrary data blobs
32 **/
34 /**
35 construct a data blob, must be freed with data_blob_free()
36 you can pass NULL for p and get a blank data blob
37 **/
38 _PUBLIC_ DATA_BLOB data_blob_named(const void *p, size_t length, const char *name)
40 return data_blob_talloc_named(NULL, p, length, name);
43 /**
44 construct a data blob, using supplied TALLOC_CTX
45 **/
46 _PUBLIC_ DATA_BLOB data_blob_talloc_named(TALLOC_CTX *mem_ctx, const void *p, size_t length, const char *name)
48 DATA_BLOB ret;
50 if (p == NULL && length == 0) {
51 ZERO_STRUCT(ret);
52 return ret;
55 if (p) {
56 ret.data = (uint8_t *)talloc_memdup(mem_ctx, p, length);
57 } else {
58 ret.data = talloc_array(mem_ctx, uint8_t, length);
60 if (ret.data == NULL) {
61 ret.length = 0;
62 return ret;
64 talloc_set_name_const(ret.data, name);
65 ret.length = length;
66 return ret;
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 _PUBLIC_ 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);
78 return blob;
81 /**
82 free a data blob
83 **/
84 _PUBLIC_ void data_blob_free(DATA_BLOB *d)
86 if (d) {
87 TALLOC_FREE(d->data);
88 d->length = 0;
92 /**
93 clear a DATA_BLOB's contents
94 **/
95 _PUBLIC_ void data_blob_clear(DATA_BLOB *d)
97 if (d->data) {
98 ZERO_ARRAY_LEN(d->data, d->length);
103 free a data blob and clear its contents
105 _PUBLIC_ void data_blob_clear_free(DATA_BLOB *d)
107 data_blob_clear(d);
108 data_blob_free(d);
113 check if two data blobs are equal
115 _PUBLIC_ int data_blob_cmp(const DATA_BLOB *d1, const DATA_BLOB *d2)
117 int ret;
118 if (d1->data == NULL && d2->data != NULL) {
119 return -1;
121 if (d1->data != NULL && d2->data == NULL) {
122 return 1;
124 if (d1->data == d2->data) {
125 return NUMERIC_CMP(d1->length, d2->length);
127 ret = memcmp(d1->data, d2->data, MIN(d1->length, d2->length));
128 if (ret == 0) {
129 /* Note this ordering is used in conditional aces */
130 return NUMERIC_CMP(d1->length, d2->length);
132 return ret;
136 check if two data blobs are equal, where the time taken should not depend on the
137 contents of either blob.
139 _PUBLIC_ bool data_blob_equal_const_time(const DATA_BLOB *d1, const DATA_BLOB *d2)
141 bool ret;
142 if (d1->data == NULL && d2->data != NULL) {
143 return false;
145 if (d1->data != NULL && d2->data == NULL) {
146 return false;
148 if (d1->length != d2->length) {
149 return false;
151 if (d1->data == d2->data) {
152 return true;
154 ret = mem_equal_const_time(d1->data, d2->data, d1->length);
155 return ret;
159 print the data_blob as hex string
161 _PUBLIC_ char *data_blob_hex_string_lower(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob)
163 size_t i;
164 char *hex_string;
166 hex_string = talloc_array(mem_ctx, char, (blob->length*2)+1);
167 if (!hex_string) {
168 return NULL;
171 /* this must be lowercase or w2k8 cannot join a samba domain,
172 as this routine is used to encode extended DNs and windows
173 only accepts lowercase hexadecimal numbers */
174 for (i = 0; i < blob->length; i++) {
175 hex_string[i * 2] = nybble_to_hex_lower(blob->data[i] >> 4);
176 hex_string[i * 2 + 1] = nybble_to_hex_lower(blob->data[i]);
179 hex_string[(blob->length*2)] = '\0';
180 return hex_string;
183 _PUBLIC_ char *data_blob_hex_string_upper(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob)
185 size_t i;
186 char *hex_string;
188 hex_string = talloc_array(mem_ctx, char, (blob->length*2)+1);
189 if (!hex_string) {
190 return NULL;
193 for (i = 0; i < blob->length; i++) {
194 hex_string[i * 2] = nybble_to_hex_upper(blob->data[i] >> 4);
195 hex_string[i * 2 + 1] = nybble_to_hex_upper(blob->data[i]);
198 hex_string[(blob->length*2)] = '\0';
199 return hex_string;
203 useful for constructing data blobs in test suites, while
204 avoiding const warnings
206 _PUBLIC_ DATA_BLOB data_blob_string_const(const char *str)
208 DATA_BLOB blob;
209 blob.data = discard_const_p(uint8_t, str);
210 blob.length = str ? strlen(str) : 0;
211 return blob;
215 useful for constructing data blobs in test suites, while
216 avoiding const warnings
218 _PUBLIC_ DATA_BLOB data_blob_string_const_null(const char *str)
220 DATA_BLOB blob;
221 blob.data = discard_const_p(uint8_t, str);
222 blob.length = str ? strlen(str)+1 : 0;
223 return blob;
227 * Create a new data blob from const data
230 _PUBLIC_ DATA_BLOB data_blob_const(const void *p, size_t length)
232 DATA_BLOB blob;
233 blob.data = discard_const_p(uint8_t, p);
234 blob.length = length;
235 return blob;
240 realloc a data_blob
242 _PUBLIC_ bool data_blob_realloc(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, size_t length)
244 uint8_t *tmp = talloc_realloc(mem_ctx, blob->data, uint8_t, length);
245 if (tmp == NULL) {
246 return false;
248 blob->data = tmp;
249 blob->length = length;
250 return true;
255 append some data to a data blob
257 _PUBLIC_ bool data_blob_append(TALLOC_CTX *mem_ctx, DATA_BLOB *blob,
258 const void *p, size_t length)
260 size_t old_len = blob->length;
261 size_t new_len = old_len + length;
263 if (length == 0) {
264 return true;
267 if (new_len < length || new_len < old_len) {
268 return false;
271 if ((const uint8_t *)p + length < (const uint8_t *)p) {
272 return false;
275 if (!data_blob_realloc(mem_ctx, blob, new_len)) {
276 return false;
279 memcpy(blob->data + old_len, p, length);
280 return true;
284 pad the length of a data blob to a multiple of
285 'pad'. 'pad' must be a power of two.
287 _PUBLIC_ bool data_blob_pad(TALLOC_CTX *mem_ctx, DATA_BLOB *blob,
288 size_t pad)
290 size_t old_len = blob->length;
291 size_t new_len = (old_len + pad - 1) & ~(pad - 1);
293 if (new_len < old_len || (pad & (pad - 1)) != 0) {
294 return false;
297 if (!data_blob_realloc(mem_ctx, blob, new_len)) {
298 return false;
301 memset(blob->data + old_len, 0, new_len - old_len);
302 return true;