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
|
---|
6 |
|
---|
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.
|
---|
11 |
|
---|
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.
|
---|
16 |
|
---|
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/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "includes.h"
|
---|
22 |
|
---|
23 | const DATA_BLOB data_blob_null = { NULL, 0 };
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * @file
|
---|
27 | * @brief Manipulation of arbitrary data blobs
|
---|
28 | **/
|
---|
29 |
|
---|
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)
|
---|
35 | {
|
---|
36 | return data_blob_talloc_named(NULL, p, length, name);
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | construct a data blob, using supplied TALLOC_CTX
|
---|
41 | **/
|
---|
42 | _PUBLIC_ DATA_BLOB data_blob_talloc_named(TALLOC_CTX *mem_ctx, const void *p, size_t length, const char *name)
|
---|
43 | {
|
---|
44 | DATA_BLOB ret;
|
---|
45 |
|
---|
46 | if (p == NULL && length == 0) {
|
---|
47 | ZERO_STRUCT(ret);
|
---|
48 | return ret;
|
---|
49 | }
|
---|
50 |
|
---|
51 | if (p) {
|
---|
52 | ret.data = (uint8_t *)talloc_memdup(mem_ctx, p, length);
|
---|
53 | } else {
|
---|
54 | ret.data = talloc_array(mem_ctx, uint8_t, length);
|
---|
55 | }
|
---|
56 | if (ret.data == NULL) {
|
---|
57 | ret.length = 0;
|
---|
58 | return ret;
|
---|
59 | }
|
---|
60 | talloc_set_name_const(ret.data, name);
|
---|
61 | ret.length = length;
|
---|
62 | return ret;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | construct a zero data blob, using supplied TALLOC_CTX.
|
---|
67 | use this sparingly as it initialises data - better to initialise
|
---|
68 | yourself if you want specific data in the blob
|
---|
69 | **/
|
---|
70 | _PUBLIC_ DATA_BLOB data_blob_talloc_zero(TALLOC_CTX *mem_ctx, size_t length)
|
---|
71 | {
|
---|
72 | DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, length);
|
---|
73 | data_blob_clear(&blob);
|
---|
74 | return blob;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | free a data blob
|
---|
79 | **/
|
---|
80 | _PUBLIC_ void data_blob_free(DATA_BLOB *d)
|
---|
81 | {
|
---|
82 | if (d) {
|
---|
83 | talloc_free(d->data);
|
---|
84 | d->data = NULL;
|
---|
85 | d->length = 0;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | clear a DATA_BLOB's contents
|
---|
91 | **/
|
---|
92 | _PUBLIC_ void data_blob_clear(DATA_BLOB *d)
|
---|
93 | {
|
---|
94 | if (d->data) {
|
---|
95 | memset(d->data, 0, d->length);
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | free a data blob and clear its contents
|
---|
101 | **/
|
---|
102 | _PUBLIC_ void data_blob_clear_free(DATA_BLOB *d)
|
---|
103 | {
|
---|
104 | data_blob_clear(d);
|
---|
105 | data_blob_free(d);
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | /**
|
---|
110 | check if two data blobs are equal
|
---|
111 | **/
|
---|
112 | _PUBLIC_ int data_blob_cmp(const DATA_BLOB *d1, const DATA_BLOB *d2)
|
---|
113 | {
|
---|
114 | int ret;
|
---|
115 | if (d1->data == NULL && d2->data != NULL) {
|
---|
116 | return -1;
|
---|
117 | }
|
---|
118 | if (d1->data != NULL && d2->data == NULL) {
|
---|
119 | return 1;
|
---|
120 | }
|
---|
121 | if (d1->data == d2->data) {
|
---|
122 | return d1->length - d2->length;
|
---|
123 | }
|
---|
124 | ret = memcmp(d1->data, d2->data, MIN(d1->length, d2->length));
|
---|
125 | if (ret == 0) {
|
---|
126 | return d1->length - d2->length;
|
---|
127 | }
|
---|
128 | return ret;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | print the data_blob as hex string
|
---|
133 | **/
|
---|
134 | _PUBLIC_ char *data_blob_hex_string_lower(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob)
|
---|
135 | {
|
---|
136 | int i;
|
---|
137 | char *hex_string;
|
---|
138 |
|
---|
139 | hex_string = talloc_array(mem_ctx, char, (blob->length*2)+1);
|
---|
140 | if (!hex_string) {
|
---|
141 | return NULL;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /* this must be lowercase or w2k8 cannot join a samba domain,
|
---|
145 | as this routine is used to encode extended DNs and windows
|
---|
146 | only accepts lowercase hexadecimal numbers */
|
---|
147 | for (i = 0; i < blob->length; i++)
|
---|
148 | slprintf(&hex_string[i*2], 3, "%02x", blob->data[i]);
|
---|
149 |
|
---|
150 | hex_string[(blob->length*2)] = '\0';
|
---|
151 | return hex_string;
|
---|
152 | }
|
---|
153 |
|
---|
154 | _PUBLIC_ char *data_blob_hex_string_upper(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob)
|
---|
155 | {
|
---|
156 | int i;
|
---|
157 | char *hex_string;
|
---|
158 |
|
---|
159 | hex_string = talloc_array(mem_ctx, char, (blob->length*2)+1);
|
---|
160 | if (!hex_string) {
|
---|
161 | return NULL;
|
---|
162 | }
|
---|
163 |
|
---|
164 | for (i = 0; i < blob->length; i++)
|
---|
165 | slprintf(&hex_string[i*2], 3, "%02X", blob->data[i]);
|
---|
166 |
|
---|
167 | hex_string[(blob->length*2)] = '\0';
|
---|
168 | return hex_string;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | useful for constructing data blobs in test suites, while
|
---|
173 | avoiding const warnings
|
---|
174 | **/
|
---|
175 | _PUBLIC_ DATA_BLOB data_blob_string_const(const char *str)
|
---|
176 | {
|
---|
177 | DATA_BLOB blob;
|
---|
178 | blob.data = discard_const_p(uint8_t, str);
|
---|
179 | blob.length = str ? strlen(str) : 0;
|
---|
180 | return blob;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | useful for constructing data blobs in test suites, while
|
---|
185 | avoiding const warnings
|
---|
186 | **/
|
---|
187 | _PUBLIC_ DATA_BLOB data_blob_string_const_null(const char *str)
|
---|
188 | {
|
---|
189 | DATA_BLOB blob;
|
---|
190 | blob.data = discard_const_p(uint8_t, str);
|
---|
191 | blob.length = str ? strlen(str)+1 : 0;
|
---|
192 | return blob;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Create a new data blob from const data
|
---|
197 | */
|
---|
198 |
|
---|
199 | _PUBLIC_ DATA_BLOB data_blob_const(const void *p, size_t length)
|
---|
200 | {
|
---|
201 | DATA_BLOB blob;
|
---|
202 | blob.data = discard_const_p(uint8_t, p);
|
---|
203 | blob.length = length;
|
---|
204 | return blob;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | /**
|
---|
209 | realloc a data_blob
|
---|
210 | **/
|
---|
211 | _PUBLIC_ bool data_blob_realloc(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, size_t length)
|
---|
212 | {
|
---|
213 | blob->data = talloc_realloc(mem_ctx, blob->data, uint8_t, length);
|
---|
214 | if (blob->data == NULL)
|
---|
215 | return false;
|
---|
216 | blob->length = length;
|
---|
217 | return true;
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | /**
|
---|
222 | append some data to a data blob
|
---|
223 | **/
|
---|
224 | _PUBLIC_ bool data_blob_append(TALLOC_CTX *mem_ctx, DATA_BLOB *blob,
|
---|
225 | const void *p, size_t length)
|
---|
226 | {
|
---|
227 | size_t old_len = blob->length;
|
---|
228 | size_t new_len = old_len + length;
|
---|
229 | if (new_len < length || new_len < old_len) {
|
---|
230 | return false;
|
---|
231 | }
|
---|
232 |
|
---|
233 | if ((const uint8_t *)p + length < (const uint8_t *)p) {
|
---|
234 | return false;
|
---|
235 | }
|
---|
236 |
|
---|
237 | if (!data_blob_realloc(mem_ctx, blob, new_len)) {
|
---|
238 | return false;
|
---|
239 | }
|
---|
240 |
|
---|
241 | memcpy(blob->data + old_len, p, length);
|
---|
242 | return true;
|
---|
243 | }
|
---|
244 |
|
---|