r9685: Add tests for samba3sam mapping module
[sfrench/samba-autobuild/.git] / source4 / lib / data_blob.c
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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /*******************************************************************
25  construct a data blob, must be freed with data_blob_free()
26  you can pass NULL for p and get a blank data blob
27 *******************************************************************/
28 DATA_BLOB data_blob_named(const void *p, size_t length, const char *name)
29 {
30         DATA_BLOB ret;
31
32         if (p == NULL && length == 0) {
33                 ZERO_STRUCT(ret);
34                 return ret;
35         }
36
37         if (p) {
38                 ret.data = talloc_memdup(NULL, p, length);
39         } else {
40                 ret.data = talloc_size(NULL, length);
41         }
42         if (ret.data == NULL) {
43                 ret.length = 0;
44                 return ret;
45         }
46         talloc_set_name_const(ret.data, name);
47         ret.length = length;
48         return ret;
49 }
50
51 /*******************************************************************
52  construct a data blob, using supplied TALLOC_CTX
53 *******************************************************************/
54 DATA_BLOB data_blob_talloc_named(TALLOC_CTX *mem_ctx, const void *p, size_t length, const char *name)
55 {
56         DATA_BLOB ret = data_blob_named(p, length, name);
57
58         if (ret.data) {
59                 talloc_steal(mem_ctx, ret.data);
60         }
61         return ret;
62 }
63
64
65 /*******************************************************************
66  reference a data blob, to the supplied TALLOC_CTX.  
67  Returns a NULL DATA_BLOB on failure
68 *******************************************************************/
69 DATA_BLOB data_blob_talloc_reference(TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
70 {
71         DATA_BLOB ret = *blob;
72
73         ret.data = talloc_reference(mem_ctx, blob->data);
74
75         if (!ret.data) {
76                 return data_blob(NULL, 0);
77         }
78         return ret;
79 }
80
81 /*******************************************************************
82  construct a zero data blob, using supplied TALLOC_CTX. 
83  use this sparingly as it initialises data - better to initialise
84  yourself if you want specific data in the blob
85 *******************************************************************/
86 DATA_BLOB data_blob_talloc_zero(TALLOC_CTX *mem_ctx, size_t length)
87 {
88         DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, length);
89         data_blob_clear(&blob);
90         return blob;
91 }
92
93 /*******************************************************************
94 free a data blob
95 *******************************************************************/
96 void data_blob_free(DATA_BLOB *d)
97 {
98         if (d) {
99                 talloc_free(d->data);
100                 d->data = NULL;
101                 d->length = 0;
102         }
103 }
104
105 /*******************************************************************
106 clear a DATA_BLOB's contents
107 *******************************************************************/
108 void data_blob_clear(DATA_BLOB *d)
109 {
110         if (d->data) {
111                 memset(d->data, 0, d->length);
112         }
113 }
114
115 /*******************************************************************
116 free a data blob and clear its contents
117 *******************************************************************/
118 void data_blob_clear_free(DATA_BLOB *d)
119 {
120         data_blob_clear(d);
121         data_blob_free(d);
122 }
123
124
125 /*******************************************************************
126 check if two data blobs are equal
127 *******************************************************************/
128 BOOL data_blob_equal(const DATA_BLOB *d1, const DATA_BLOB *d2)
129 {
130         if (d1->length != d2->length) {
131                 return False;
132         }
133         if (d1->data == d2->data) {
134                 return True;
135         }
136         if (d1->data == NULL || d2->data == NULL) {
137                 return False;
138         }
139         if (memcmp(d1->data, d2->data, d1->length) == 0) {
140                 return True;
141         }
142         return False;
143 }
144
145 /*******************************************************************
146 print the data_blob as hex string
147 *******************************************************************/
148 char *data_blob_hex_string(TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
149 {
150         int i;
151         char *hex_string;
152
153         hex_string = talloc_array(mem_ctx, char, (blob->length*2)+1);
154         if (!hex_string) {
155                 return NULL;
156         }
157
158         for (i = 0; i < blob->length; i++)
159                 slprintf(&hex_string[i*2], 3, "%02X", blob->data[i]);
160
161         return hex_string;
162 }
163
164 /*
165   useful for constructing data blobs in test suites, while
166   avoiding const warnings
167 */
168 DATA_BLOB data_blob_string_const(const char *str)
169 {
170         DATA_BLOB blob;
171         blob.data = discard_const(str);
172         blob.length = strlen(str);
173         return blob;
174 }
175
176 DATA_BLOB data_blob_const(const void *p, size_t length)
177 {
178         DATA_BLOB blob;
179         blob.data = discard_const(p);
180         blob.length = length;
181         return blob;
182 }
183
184
185 /*
186   append some data to a data blob
187 */
188 NTSTATUS data_blob_append(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, void *p, size_t length)
189 {
190         blob->data = talloc_realloc_size(mem_ctx, blob->data, blob->length + length);
191         NT_STATUS_HAVE_NO_MEMORY(blob->data);   
192         memcpy(blob->data + blob->length, p, length);
193         blob->length += length;
194         return NT_STATUS_OK;
195 }