Merge ldb_search() and ldb_search_exp_fmt() into a simgle function.
[Samba.git] / source4 / lib / util / data_blob.c
blob57b34b7ae73d8aa698dc9df4fc164dc22a8b4512
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 "includes.h"
23 /**
24 * @file
25 * @brief Manipulation of arbitrary data blobs
26 **/
28 /**
29 construct a data blob, must be freed with data_blob_free()
30 you can pass NULL for p and get a blank data blob
31 **/
32 _PUBLIC_ DATA_BLOB data_blob_named(const void *p, size_t length, const char *name)
34 DATA_BLOB ret;
36 if (p == NULL && length == 0) {
37 ZERO_STRUCT(ret);
38 return ret;
41 if (p) {
42 ret.data = (uint8_t *)talloc_memdup(NULL, p, length);
43 } else {
44 ret.data = talloc_array(NULL, uint8_t, length);
46 if (ret.data == NULL) {
47 ret.length = 0;
48 return ret;
50 talloc_set_name_const(ret.data, name);
51 ret.length = length;
52 return ret;
55 /**
56 construct a data blob, using supplied TALLOC_CTX
57 **/
58 _PUBLIC_ DATA_BLOB data_blob_talloc_named(TALLOC_CTX *mem_ctx, const void *p, size_t length, const char *name)
60 DATA_BLOB ret = data_blob_named(p, length, name);
62 if (ret.data) {
63 talloc_steal(mem_ctx, ret.data);
65 return ret;
69 /**
70 reference a data blob, to the supplied TALLOC_CTX.
71 Returns a NULL DATA_BLOB on failure
72 **/
73 _PUBLIC_ DATA_BLOB data_blob_talloc_reference(TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
75 DATA_BLOB ret = *blob;
77 ret.data = talloc_reference(mem_ctx, blob->data);
79 if (!ret.data) {
80 return data_blob(NULL, 0);
82 return ret;
85 /**
86 construct a zero data blob, using supplied TALLOC_CTX.
87 use this sparingly as it initialises data - better to initialise
88 yourself if you want specific data in the blob
89 **/
90 _PUBLIC_ DATA_BLOB data_blob_talloc_zero(TALLOC_CTX *mem_ctx, size_t length)
92 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, length);
93 data_blob_clear(&blob);
94 return blob;
97 /**
98 free a data blob
99 **/
100 _PUBLIC_ void data_blob_free(DATA_BLOB *d)
102 if (d) {
103 talloc_free(d->data);
104 d->data = NULL;
105 d->length = 0;
110 clear a DATA_BLOB's contents
112 _PUBLIC_ void data_blob_clear(DATA_BLOB *d)
114 if (d->data) {
115 memset(d->data, 0, d->length);
120 free a data blob and clear its contents
122 _PUBLIC_ void data_blob_clear_free(DATA_BLOB *d)
124 data_blob_clear(d);
125 data_blob_free(d);
130 check if two data blobs are equal
132 _PUBLIC_ int data_blob_cmp(const DATA_BLOB *d1, const DATA_BLOB *d2)
134 int ret;
135 if (d1->data == NULL && d2->data != NULL) {
136 return -1;
138 if (d1->data != NULL && d2->data == NULL) {
139 return 1;
141 if (d1->data == d2->data) {
142 return d1->length - d2->length;
144 ret = memcmp(d1->data, d2->data, MIN(d1->length, d2->length));
145 if (ret == 0) {
146 return d1->length - d2->length;
148 return ret;
152 print the data_blob as hex string
154 _PUBLIC_ char *data_blob_hex_string(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob)
156 int i;
157 char *hex_string;
159 hex_string = talloc_array(mem_ctx, char, (blob->length*2)+1);
160 if (!hex_string) {
161 return NULL;
164 for (i = 0; i < blob->length; i++)
165 slprintf(&hex_string[i*2], 3, "%02X", blob->data[i]);
167 hex_string[(blob->length*2)] = '\0';
168 return hex_string;
172 useful for constructing data blobs in test suites, while
173 avoiding const warnings
175 _PUBLIC_ DATA_BLOB data_blob_string_const(const char *str)
177 DATA_BLOB blob;
178 blob.data = discard_const_p(uint8_t, str);
179 blob.length = str ? strlen(str) : 0;
180 return blob;
184 * Create a new data blob from const data
187 _PUBLIC_ DATA_BLOB data_blob_const(const void *p, size_t length)
189 DATA_BLOB blob;
190 blob.data = discard_const_p(uint8_t, p);
191 blob.length = length;
192 return blob;
197 realloc a data_blob
199 _PUBLIC_ bool data_blob_realloc(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, size_t length)
201 blob->data = talloc_realloc(mem_ctx, blob->data, uint8_t, length);
202 if (blob->data == NULL)
203 return false;
204 blob->length = length;
205 return true;
210 append some data to a data blob
212 _PUBLIC_ bool data_blob_append(TALLOC_CTX *mem_ctx, DATA_BLOB *blob,
213 const void *p, size_t length)
215 size_t old_len = blob->length;
216 size_t new_len = old_len + length;
217 if (new_len < length || new_len < old_len) {
218 return false;
221 if ((const uint8_t *)p + length < (const uint8_t *)p) {
222 return false;
225 if (!data_blob_realloc(mem_ctx, blob, new_len)) {
226 return false;
229 memcpy(blob->data + old_len, p, length);
230 return true;