s3-registry: make sure root has full registry access (in particular for make test).
[Samba/nascimento.git] / lib / util / data_blob.c
blob3448e943162dbd265dc3b878b90c526893c1373a
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 const DATA_BLOB data_blob_null = { NULL, 0 };
25 /**
26 * @file
27 * @brief Manipulation of arbitrary data blobs
28 **/
30 /**
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
33 **/
34 _PUBLIC_ DATA_BLOB data_blob_named(const void *p, size_t length, const char *name)
36 DATA_BLOB ret;
38 if (p == NULL && length == 0) {
39 ZERO_STRUCT(ret);
40 return ret;
43 if (p) {
44 ret.data = (uint8_t *)talloc_memdup(NULL, p, length);
45 } else {
46 ret.data = talloc_array(NULL, uint8_t, length);
48 if (ret.data == NULL) {
49 ret.length = 0;
50 return ret;
52 talloc_set_name_const(ret.data, name);
53 ret.length = length;
54 return ret;
57 /**
58 construct a data blob, using supplied TALLOC_CTX
59 **/
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);
64 if (ret.data) {
65 talloc_steal(mem_ctx, ret.data);
67 return ret;
70 /**
71 construct a zero data blob, using supplied TALLOC_CTX.
72 use this sparingly as it initialises data - better to initialise
73 yourself if you want specific data in the blob
74 **/
75 _PUBLIC_ DATA_BLOB data_blob_talloc_zero(TALLOC_CTX *mem_ctx, size_t length)
77 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, length);
78 data_blob_clear(&blob);
79 return blob;
82 /**
83 free a data blob
84 **/
85 _PUBLIC_ void data_blob_free(DATA_BLOB *d)
87 if (d) {
88 talloc_free(d->data);
89 d->data = NULL;
90 d->length = 0;
94 /**
95 clear a DATA_BLOB's contents
96 **/
97 _PUBLIC_ void data_blob_clear(DATA_BLOB *d)
99 if (d->data) {
100 memset(d->data, 0, d->length);
105 free a data blob and clear its contents
107 _PUBLIC_ void data_blob_clear_free(DATA_BLOB *d)
109 data_blob_clear(d);
110 data_blob_free(d);
115 check if two data blobs are equal
117 _PUBLIC_ int data_blob_cmp(const DATA_BLOB *d1, const DATA_BLOB *d2)
119 int ret;
120 if (d1->data == NULL && d2->data != NULL) {
121 return -1;
123 if (d1->data != NULL && d2->data == NULL) {
124 return 1;
126 if (d1->data == d2->data) {
127 return d1->length - d2->length;
129 ret = memcmp(d1->data, d2->data, MIN(d1->length, d2->length));
130 if (ret == 0) {
131 return d1->length - d2->length;
133 return ret;
137 print the data_blob as hex string
139 _PUBLIC_ char *data_blob_hex_string_lower(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob)
141 int i;
142 char *hex_string;
144 hex_string = talloc_array(mem_ctx, char, (blob->length*2)+1);
145 if (!hex_string) {
146 return NULL;
149 /* this must be lowercase or w2k8 cannot join a samba domain,
150 as this routine is used to encode extended DNs and windows
151 only accepts lowercase hexadecimal numbers */
152 for (i = 0; i < blob->length; i++)
153 slprintf(&hex_string[i*2], 3, "%02x", blob->data[i]);
155 hex_string[(blob->length*2)] = '\0';
156 return hex_string;
159 _PUBLIC_ char *data_blob_hex_string_upper(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob)
161 int 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 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';
173 return hex_string;
177 useful for constructing data blobs in test suites, while
178 avoiding const warnings
180 _PUBLIC_ DATA_BLOB data_blob_string_const(const char *str)
182 DATA_BLOB blob;
183 blob.data = discard_const_p(uint8_t, str);
184 blob.length = str ? strlen(str) : 0;
185 return blob;
189 useful for constructing data blobs in test suites, while
190 avoiding const warnings
192 _PUBLIC_ DATA_BLOB data_blob_string_const_null(const char *str)
194 DATA_BLOB blob;
195 blob.data = discard_const_p(uint8_t, str);
196 blob.length = str ? strlen(str)+1 : 0;
197 return blob;
201 * Create a new data blob from const data
204 _PUBLIC_ DATA_BLOB data_blob_const(const void *p, size_t length)
206 DATA_BLOB blob;
207 blob.data = discard_const_p(uint8_t, p);
208 blob.length = length;
209 return blob;
214 realloc a data_blob
216 _PUBLIC_ bool data_blob_realloc(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, size_t length)
218 blob->data = talloc_realloc(mem_ctx, blob->data, uint8_t, length);
219 if (blob->data == NULL)
220 return false;
221 blob->length = length;
222 return true;
227 append some data to a data blob
229 _PUBLIC_ bool data_blob_append(TALLOC_CTX *mem_ctx, DATA_BLOB *blob,
230 const void *p, size_t length)
232 size_t old_len = blob->length;
233 size_t new_len = old_len + length;
234 if (new_len < length || new_len < old_len) {
235 return false;
238 if ((const uint8_t *)p + length < (const uint8_t *)p) {
239 return false;
242 if (!data_blob_realloc(mem_ctx, blob, new_len)) {
243 return false;
246 memcpy(blob->data + old_len, p, length);
247 return true;