2 Copyright (c) Ralph Boehme 2012-2014
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "marshalling.h"
22 #include "lib/util/charset/charset.h"
23 #include "lib/util/talloc_stack.h"
24 #include "system/time.h"
30 void **dd_talloc_array;
33 void *_dalloc_new(TALLOC_CTX *mem_ctx, const char *type)
37 p = talloc_zero(mem_ctx, DALLOC_CTX);
41 talloc_set_name_const(p, type);
46 int _dalloc_add_talloc_chunk(DALLOC_CTX *dd, void *obj, const char *type, size_t size)
48 size_t array_len = talloc_array_length(dd->dd_talloc_array);
50 dd->dd_talloc_array = talloc_realloc(dd,
54 if (dd->dd_talloc_array == NULL) {
61 p = talloc_named_const(dd->dd_talloc_array, size, type);
68 _talloc_get_type_abort(obj, type, __location__);
71 dd->dd_talloc_array[array_len] = obj;
76 /* Get number of elements, returns 0 if the structure is empty or not initialized */
77 size_t dalloc_size(const DALLOC_CTX *d)
82 return talloc_array_length(d->dd_talloc_array);
85 /* Return element at position */
86 void *dalloc_get_object(const DALLOC_CTX *d, int i)
88 size_t size = dalloc_size(d);
94 return d->dd_talloc_array[i];
97 /* Return typename of element at position */
98 const char *dalloc_get_name(const DALLOC_CTX *d, int i)
100 void *o = dalloc_get_object(d, i);
106 return talloc_get_name(o);
110 * Get pointer to value from a DALLOC object
112 * Returns pointer to object from a DALLOC object. Nested object interation
113 * is supported by using the type string "DALLOC_CTX". Any other type string
114 * designates the requested objects type.
116 void *dalloc_get(const DALLOC_CTX *d, ...)
125 type = va_arg(args, const char *);
127 while (strcmp(type, "DALLOC_CTX") == 0) {
128 elem = va_arg(args, int);
129 if (elem >= talloc_array_length(d->dd_talloc_array)) {
133 d = d->dd_talloc_array[elem];
134 type = va_arg(args, const char *);
137 elem = va_arg(args, int);
138 if (elem >= talloc_array_length(d->dd_talloc_array)) {
143 p = talloc_check_name(d->dd_talloc_array[elem], type);
157 void *dalloc_value_for_key(const DALLOC_CTX *d, ...)
162 const char *type = NULL;
167 type = va_arg(args, const char *);
169 while (strcmp(type, "DALLOC_CTX") == 0) {
170 array_len = talloc_array_length(d->dd_talloc_array);
171 elem = va_arg(args, int);
172 if (elem >= array_len) {
176 d = d->dd_talloc_array[elem];
177 type = va_arg(args, const char *);
180 array_len = talloc_array_length(d->dd_talloc_array);
182 for (elem = 0; elem + 1 < array_len; elem += 2) {
183 if (strcmp(talloc_get_name(d->dd_talloc_array[elem]), "char *") != 0) {
187 if (strcmp((char *)d->dd_talloc_array[elem],type) == 0) {
188 p = d->dd_talloc_array[elem + 1];
196 type = va_arg(args, const char *);
197 if (strcmp(talloc_get_name(p), type) != 0) {
209 static char *dalloc_strdup(TALLOC_CTX *mem_ctx, const char *string)
213 p = talloc_strdup(mem_ctx, string);
217 talloc_set_name_const(p, "char *");
221 int dalloc_stradd(DALLOC_CTX *d, const char *string)
226 p = dalloc_strdup(d, string);
231 result = dalloc_add(d, p, char *);
239 static char *tab_level(TALLOC_CTX *mem_ctx, int level)
242 char *string = talloc_array(mem_ctx, char, level + 1);
244 for (i = 0; i < level; i++) {
252 char *dalloc_dump(DALLOC_CTX *dd, int nestinglevel)
260 char datestring[256];
262 char *logstring, *nested_logstring;
263 char *tab_string1, *tab_string2;
269 tab_string1 = tab_level(dd, nestinglevel);
270 if (tab_string1 == NULL) {
273 tab_string2 = tab_level(dd, nestinglevel + 1);
274 if (tab_string2 == NULL) {
278 logstring = talloc_asprintf(dd,
283 if (logstring == NULL) {
287 for (n = 0; n < dalloc_size(dd); n++) {
288 type = dalloc_get_name(dd, n);
292 p = dalloc_get_object(dd, n);
296 if (strcmp(type, "DALLOC_CTX") == 0
297 || strcmp(type, "sl_array_t") == 0
298 || strcmp(type, "sl_filemeta_t") == 0
299 || strcmp(type, "sl_dict_t") == 0) {
300 nested_logstring = dalloc_dump(p, nestinglevel + 1);
301 if (nested_logstring == NULL) {
304 logstring = talloc_strdup_append(logstring,
306 } else if (strcmp(type, "uint64_t") == 0) {
307 memcpy(&i, p, sizeof(uint64_t));
308 logstring = talloc_asprintf_append(
310 "%suint64_t: 0x%04jx\n",
311 tab_string2, (uintmax_t)i);
312 } else if (strcmp(type, "char *") == 0) {
313 logstring = talloc_asprintf_append(
318 } else if (strcmp(type, "smb_ucs2_t *") == 0) {
319 ok = convert_string_talloc(talloc_tos(),
329 logstring = talloc_asprintf_append(
331 "%sUTF16-string: %s\n",
334 TALLOC_FREE(utf8string);
335 } else if (strcmp(type, "sl_bool_t") == 0) {
336 memcpy(&bl, p, sizeof(sl_bool_t));
337 logstring = talloc_asprintf_append(
341 bl ? "true" : "false");
342 } else if (strcmp(type, "sl_nil_t") == 0) {
343 logstring = talloc_asprintf_append(
347 } else if (strcmp(type, "sl_time_t") == 0) {
348 memcpy(&t, p, sizeof(sl_time_t));
349 tm = localtime(&t.tv_sec);
353 result = strftime(datestring,
355 "%Y-%m-%d %H:%M:%S", tm);
359 logstring = talloc_asprintf_append(
361 "%ssl_time_t: %s.%06lu\n",
364 (unsigned long)t.tv_usec);
365 } else if (strcmp(type, "sl_cnids_t") == 0) {
366 memcpy(&cnids, p, sizeof(sl_cnids_t));
367 logstring = talloc_asprintf_append(
369 "%sCNIDs: unkn1: 0x%" PRIx16 ", unkn2: 0x%" PRIx32 "\n",
373 if (logstring == NULL) {
376 if (cnids.ca_cnids) {
377 nested_logstring = dalloc_dump(
380 if (!nested_logstring) {
383 logstring = talloc_strdup_append(logstring,
387 logstring = talloc_asprintf_append(
393 if (logstring == NULL) {
397 logstring = talloc_asprintf_append(logstring,
400 if (logstring == NULL) {