1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/*
Copyright (c) Ralph Boehme 2012-2014
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "replace.h"
#include <talloc.h>
#include "dalloc.h"
#include "marshalling.h"
#include "lib/util/charset/charset.h"
#include "lib/util/talloc_stack.h"
#include "system/time.h"
/**
* Dynamic Datastore
**/
struct dalloc_ctx {
void **dd_talloc_array;
};
void *_dalloc_new(TALLOC_CTX *mem_ctx, const char *type)
{
void *p;
p = talloc_zero(mem_ctx, DALLOC_CTX);
if (p == NULL) {
return NULL;
}
talloc_set_name_const(p, type);
return p;
}
int _dalloc_add_talloc_chunk(DALLOC_CTX *dd, void *obj, const char *type, size_t size)
{
size_t array_len = talloc_array_length(dd->dd_talloc_array);
dd->dd_talloc_array = talloc_realloc(dd,
dd->dd_talloc_array,
void *,
array_len + 1);
if (dd->dd_talloc_array == NULL) {
return -1;
}
if (size != 0) {
void *p;
p = talloc_named_const(dd->dd_talloc_array, size, type);
if (p == NULL) {
return -1;
}
memcpy(p, obj, size);
obj = p;
} else {
_talloc_get_type_abort(obj, type, __location__);
}
dd->dd_talloc_array[array_len] = obj;
return 0;
}
/* Get number of elements, returns 0 if the structure is empty or not initialized */
size_t dalloc_size(const DALLOC_CTX *d)
{
if (d == NULL) {
return 0;
}
return talloc_array_length(d->dd_talloc_array);
}
/* Return element at position */
void *dalloc_get_object(const DALLOC_CTX *d, int i)
{
size_t size = dalloc_size(d);
if (i >= size) {
return NULL;
}
return d->dd_talloc_array[i];
}
/* Return typename of element at position */
const char *dalloc_get_name(const DALLOC_CTX *d, int i)
{
void *o = dalloc_get_object(d, i);
if (o == NULL) {
return NULL;
}
return talloc_get_name(o);
}
/*
* Get pointer to value from a DALLOC object
*
* Returns pointer to object from a DALLOC object. Nested object integration
* is supported by using the type string "DALLOC_CTX". Any other type string
* designates the requested objects type.
*/
void *dalloc_get(const DALLOC_CTX *d, ...)
{
int result = 0;
void *p = NULL;
va_list args;
const char *type;
int elem;
va_start(args, d);
type = va_arg(args, const char *);
while (strcmp(type, "DALLOC_CTX") == 0) {
elem = va_arg(args, int);
if (elem >= talloc_array_length(d->dd_talloc_array)) {
result = -1;
goto done;
}
d = d->dd_talloc_array[elem];
type = va_arg(args, const char *);
}
elem = va_arg(args, int);
if (elem >= talloc_array_length(d->dd_talloc_array)) {
result = -1;
goto done;
}
p = talloc_check_name(d->dd_talloc_array[elem], type);
if (p == NULL) {
result = -1;
goto done;
}
done:
va_end(args);
if (result != 0) {
p = NULL;
}
return p;
}
void *dalloc_value_for_key(const DALLOC_CTX *d, ...)
{
int result = 0;
void *p = NULL;
va_list args;
const char *type = NULL;
int elem;
size_t array_len;
va_start(args, d);
type = va_arg(args, const char *);
while (strcmp(type, "DALLOC_CTX") == 0) {
array_len = talloc_array_length(d->dd_talloc_array);
elem = va_arg(args, int);
if (elem >= array_len) {
result = -1;
goto done;
}
d = d->dd_talloc_array[elem];
type = va_arg(args, const char *);
}
array_len = talloc_array_length(d->dd_talloc_array);
for (elem = 0; elem + 1 < array_len; elem += 2) {
if (strcmp(talloc_get_name(d->dd_talloc_array[elem]), "char *") != 0) {
result = -1;
goto done;
}
if (strcmp((char *)d->dd_talloc_array[elem],type) == 0) {
p = d->dd_talloc_array[elem + 1];
break;
}
}
if (p == NULL) {
goto done;
}
type = va_arg(args, const char *);
if (strcmp(talloc_get_name(p), type) != 0) {
p = NULL;
}
done:
va_end(args);
if (result != 0) {
p = NULL;
}
return p;
}
static char *dalloc_strdup(TALLOC_CTX *mem_ctx, const char *string)
{
char *p;
p = talloc_strdup(mem_ctx, string);
if (p == NULL) {
return NULL;
}
talloc_set_name_const(p, "char *");
return p;
}
int dalloc_stradd(DALLOC_CTX *d, const char *string)
{
int result;
char *p;
p = dalloc_strdup(d, string);
if (p == NULL) {
return -1;
}
result = dalloc_add(d, p, char *);
if (result != 0) {
return -1;
}
return 0;
}
static char *tab_level(TALLOC_CTX *mem_ctx, int level)
{
int i;
char *string = talloc_array(mem_ctx, char, level + 1);
for (i = 0; i < level; i++) {
string[i] = '\t';
}
string[i] = '\0';
return string;
}
char *dalloc_dump(DALLOC_CTX *dd, int nestinglevel)
{
const char *type;
int n, result;
uint64_t i;
sl_bool_t bl;
sl_time_t t;
struct tm *tm;
char datestring[256];
sl_cnids_t cnids;
char *logstring, *nested_logstring;
char *tab_string1, *tab_string2;
void *p;
bool ok;
char *utf8string;
size_t utf8len;
tab_string1 = tab_level(dd, nestinglevel);
if (tab_string1 == NULL) {
return NULL;
}
tab_string2 = tab_level(dd, nestinglevel + 1);
if (tab_string2 == NULL) {
return NULL;
}
logstring = talloc_asprintf(dd,
"%s%s(#%zu): {\n",
tab_string1,
talloc_get_name(dd),
dalloc_size(dd));
if (logstring == NULL) {
return NULL;
}
for (n = 0; n < dalloc_size(dd); n++) {
type = dalloc_get_name(dd, n);
if (type == NULL) {
return NULL;
}
p = dalloc_get_object(dd, n);
if (p == NULL) {
return NULL;
}
if (strcmp(type, "DALLOC_CTX") == 0
|| strcmp(type, "sl_array_t") == 0
|| strcmp(type, "sl_filemeta_t") == 0
|| strcmp(type, "sl_dict_t") == 0) {
nested_logstring = dalloc_dump(p, nestinglevel + 1);
if (nested_logstring == NULL) {
return NULL;
}
logstring = talloc_strdup_append(logstring,
nested_logstring);
} else if (strcmp(type, "uint64_t") == 0) {
memcpy(&i, p, sizeof(uint64_t));
logstring = talloc_asprintf_append(
logstring,
"%suint64_t: 0x%04jx\n",
tab_string2, (uintmax_t)i);
} else if (strcmp(type, "char *") == 0) {
logstring = talloc_asprintf_append(
logstring,
"%sstring: %s\n",
tab_string2,
(char *)p);
} else if (strcmp(type, "smb_ucs2_t *") == 0) {
ok = convert_string_talloc(talloc_tos(),
CH_UTF16LE,
CH_UTF8,
p,
talloc_get_size(p),
&utf8string,
&utf8len);
if (!ok) {
return NULL;
}
logstring = talloc_asprintf_append(
logstring,
"%sUTF16-string: %s\n",
tab_string2,
utf8string);
TALLOC_FREE(utf8string);
} else if (strcmp(type, "sl_bool_t") == 0) {
memcpy(&bl, p, sizeof(sl_bool_t));
logstring = talloc_asprintf_append(
logstring,
"%sbool: %s\n",
tab_string2,
bl ? "true" : "false");
} else if (strcmp(type, "sl_nil_t") == 0) {
logstring = talloc_asprintf_append(
logstring,
"%snil\n",
tab_string2);
} else if (strcmp(type, "sl_time_t") == 0) {
memcpy(&t, p, sizeof(sl_time_t));
tm = localtime(&t.tv_sec);
if (tm == NULL) {
return NULL;
}
result = strftime(datestring,
sizeof(datestring),
"%Y-%m-%d %H:%M:%S", tm);
if (result == 0) {
return NULL;
}
logstring = talloc_asprintf_append(
logstring,
"%ssl_time_t: %s.%06lu\n",
tab_string2,
datestring,
(unsigned long)t.tv_usec);
} else if (strcmp(type, "sl_cnids_t") == 0) {
memcpy(&cnids, p, sizeof(sl_cnids_t));
logstring = talloc_asprintf_append(
logstring,
"%sCNIDs: unkn1: 0x%" PRIx16 ", unkn2: 0x%" PRIx32 "\n",
tab_string2,
cnids.ca_unkn1,
cnids.ca_context);
if (logstring == NULL) {
return NULL;
}
if (cnids.ca_cnids) {
nested_logstring = dalloc_dump(
cnids.ca_cnids,
nestinglevel + 2);
if (!nested_logstring) {
return NULL;
}
logstring = talloc_strdup_append(logstring,
nested_logstring);
}
} else {
logstring = talloc_asprintf_append(
logstring,
"%stype: %s\n",
tab_string2,
type);
}
if (logstring == NULL) {
return NULL;
}
}
logstring = talloc_asprintf_append(logstring,
"%s}\n",
tab_string1);
if (logstring == NULL) {
return NULL;
}
return logstring;
}
|