forked from chmod222/cNBT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbt_treeops.c
528 lines (404 loc) · 13.5 KB
/
nbt_treeops.c
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
* -----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Lukas Niederbremer <[email protected]> and Clark Gaebel <[email protected]>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If we meet some day, and you think this stuff is worth
* it, you can buy us a beer in return.
* -----------------------------------------------------------------------------
*/
#include "nbt.h"
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
/* strdup isn't standard. GNU extension. */
static inline char* __strdup(const char* s)
{
char* r = malloc(strlen(s) + 1);
if(r == NULL) return NULL;
strcpy(r, s);
return r;
}
#define CHECKED_MALLOC(var, n, on_error) do { \
if((var = malloc(n)) == NULL) \
{ \
errno = NBT_EMEM; \
on_error; \
} \
} while(0)
void nbt_free_list(struct nbt_list* list)
{
if (!list)
return;
struct list_head* current;
struct list_head* temp;
list_for_each_safe(current, temp, &list->entry)
{
struct nbt_list* entry = list_entry(current, struct nbt_list, entry);
nbt_free(entry->data);
free(entry);
}
free(list->data);
free(list);
}
void nbt_free(nbt_node* tree)
{
if(tree == NULL) return;
if(tree->type == TAG_LIST)
nbt_free_list(tree->payload.tag_list);
else if (tree->type == TAG_COMPOUND)
nbt_free_list(tree->payload.tag_compound);
else if(tree->type == TAG_BYTE_ARRAY)
free(tree->payload.tag_byte_array.data);
else if(tree->type == TAG_INT_ARRAY)
free(tree->payload.tag_int_array.data);
else if(tree->type == TAG_STRING)
free(tree->payload.tag_string);
free(tree->name);
free(tree);
}
static struct nbt_list* clone_list(struct nbt_list* list)
{
/* even empty lists are valid pointers! */
assert(list);
struct nbt_list* ret;
CHECKED_MALLOC(ret, sizeof *ret, goto clone_error);
INIT_LIST_HEAD(&ret->entry);
ret->data = NULL;
if(list->data != NULL)
{
CHECKED_MALLOC(ret->data, sizeof *ret->data, goto clone_error);
ret->data->type = list->data->type;
}
struct list_head* pos;
list_for_each(pos, &list->entry)
{
struct nbt_list* current = list_entry(pos, struct nbt_list, entry);
struct nbt_list* new;
CHECKED_MALLOC(new, sizeof *new, goto clone_error);
new->data = nbt_clone(current->data);
if(new->data == NULL)
{
free(new);
goto clone_error;
}
list_add_tail(&new->entry, &ret->entry);
}
return ret;
clone_error:
nbt_free_list(ret);
return NULL;
}
/* same as strdup, but handles NULL gracefully */
static inline char* safe_strdup(const char* s)
{
return s ? __strdup(s) : NULL;
}
nbt_node* nbt_clone(nbt_node* tree)
{
if(tree == NULL) return NULL;
assert(tree->type != TAG_INVALID);
nbt_node* ret = NULL;
CHECKED_MALLOC(ret, sizeof *ret, return NULL);
ret->type = tree->type;
ret->name = safe_strdup(tree->name);
if(tree->name && ret->name == NULL) goto clone_error;
if(tree->type == TAG_STRING)
{
ret->payload.tag_string = __strdup(tree->payload.tag_string);
if(ret->payload.tag_string == NULL) goto clone_error;
}
else if(tree->type == TAG_BYTE_ARRAY)
{
unsigned char* newbuf;
CHECKED_MALLOC(newbuf, tree->payload.tag_byte_array.length, goto clone_error);
memcpy(newbuf,
tree->payload.tag_byte_array.data,
tree->payload.tag_byte_array.length);
ret->payload.tag_byte_array.data = newbuf;
ret->payload.tag_byte_array.length = tree->payload.tag_byte_array.length;
}
else if(tree->type == TAG_INT_ARRAY)
{
int32_t* newbuf;
CHECKED_MALLOC(newbuf, tree->payload.tag_int_array.length * sizeof(int32_t), goto clone_error);
memcpy(newbuf,
tree->payload.tag_int_array.data,
tree->payload.tag_int_array.length);
ret->payload.tag_int_array.data = newbuf;
ret->payload.tag_int_array.length = tree->payload.tag_int_array.length;
}
else if(tree->type == TAG_LIST)
{
ret->payload.tag_list = clone_list(tree->payload.tag_list);
if(ret->payload.tag_list == NULL) goto clone_error;
}
else if(tree->type == TAG_COMPOUND)
{
ret->payload.tag_compound = clone_list(tree->payload.tag_compound);
if(ret->payload.tag_compound == NULL) goto clone_error;
}
else
{
ret->payload = tree->payload;
}
return ret;
clone_error:
if(ret) free(ret->name);
free(ret);
return NULL;
}
bool nbt_map(nbt_node* tree, nbt_visitor_t v, void* aux)
{
assert(v);
if(tree == NULL) return true;
if(!v(tree, aux)) return false;
/* And if the item is a list or compound, recurse through each of their elements. */
if(tree->type == TAG_COMPOUND)
{
struct list_head* pos;
list_for_each(pos, &tree->payload.tag_compound->entry)
if(!nbt_map(list_entry(pos, struct nbt_list, entry)->data, v, aux))
return false;
}
if(tree->type == TAG_LIST)
{
struct list_head* pos;
list_for_each(pos, &tree->payload.tag_list->entry)
if(!nbt_map(list_entry(pos, struct nbt_list, entry)->data, v, aux))
return false;
}
return true;
}
/* Only returns NULL on error. An empty list is still a valid pointer */
static struct nbt_list* filter_list(const struct nbt_list* list, nbt_predicate_t predicate, void* aux)
{
assert(list);
struct nbt_list* ret = NULL;
CHECKED_MALLOC(ret, sizeof *ret, goto filter_error);
ret->data = NULL;
INIT_LIST_HEAD(&ret->entry);
const struct list_head* pos;
list_for_each(pos, &list->entry)
{
const struct nbt_list* p = list_entry(pos, struct nbt_list, entry);
nbt_node* new_node = nbt_filter(p->data, predicate, aux);
if(errno != NBT_OK) goto filter_error;
if(new_node == NULL) continue;
struct nbt_list* new_entry;
CHECKED_MALLOC(new_entry, sizeof *new_entry, goto filter_error);
new_entry->data = new_node;
list_add_tail(&new_entry->entry, &ret->entry);
}
return ret;
filter_error:
if(errno == NBT_OK)
errno = NBT_EMEM;
nbt_free_list(ret);
return NULL;
}
nbt_node* nbt_filter(const nbt_node* tree, nbt_predicate_t filter, void* aux)
{
assert(filter);
errno = NBT_OK;
if(tree == NULL) return NULL;
if(!filter(tree, aux)) return NULL;
nbt_node* ret = NULL;
CHECKED_MALLOC(ret, sizeof *ret, goto filter_error);
ret->type = tree->type;
ret->name = safe_strdup(tree->name);
if(tree->name && ret->name == NULL) goto filter_error;
if(tree->type == TAG_STRING)
{
ret->payload.tag_string = __strdup(tree->payload.tag_string);
if(ret->payload.tag_string == NULL) goto filter_error;
}
else if(tree->type == TAG_BYTE_ARRAY)
{
CHECKED_MALLOC(ret->payload.tag_byte_array.data,
tree->payload.tag_byte_array.length,
goto filter_error);
memcpy(ret->payload.tag_byte_array.data,
tree->payload.tag_byte_array.data,
tree->payload.tag_byte_array.length);
ret->payload.tag_byte_array.length = tree->payload.tag_byte_array.length;
}
else if(tree->type == TAG_INT_ARRAY)
{
CHECKED_MALLOC(ret->payload.tag_int_array.data,
tree->payload.tag_int_array.length * sizeof(int32_t),
goto filter_error);
memcpy(ret->payload.tag_int_array.data,
tree->payload.tag_int_array.data,
tree->payload.tag_int_array.length);
ret->payload.tag_int_array.length = tree->payload.tag_int_array.length;
}
/* Okay, we want to keep this node, but keep traversing the tree! */
else if(tree->type == TAG_LIST)
{
ret->payload.tag_list = filter_list(tree->payload.tag_list, filter, aux);
if(ret->payload.tag_list == NULL) goto filter_error;
}
else if(tree->type == TAG_COMPOUND)
{
ret->payload.tag_compound = filter_list(tree->payload.tag_compound, filter, aux);
if(ret->payload.tag_compound == NULL) goto filter_error;
}
else
{
ret->payload = tree->payload;
}
return ret;
filter_error:
if(errno == NBT_OK)
errno = NBT_EMEM;
if(ret) free(ret->name);
free(ret);
return NULL;
}
nbt_node* nbt_filter_inplace(nbt_node* tree, nbt_predicate_t filter, void* aux)
{
assert(filter);
if(tree == NULL) return NULL;
if(!filter(tree, aux)) return nbt_free(tree), NULL;
if(tree->type != TAG_LIST &&
tree->type != TAG_COMPOUND) return tree;
struct list_head* pos;
struct list_head* n;
struct nbt_list* list = tree->type == TAG_LIST ? tree->payload.tag_list : tree->payload.tag_compound;
list_for_each_safe(pos, n, &list->entry)
{
struct nbt_list* cur = list_entry(pos, struct nbt_list, entry);
cur->data = nbt_filter_inplace(cur->data, filter, aux);
if(cur->data == NULL)
{
list_del(pos);
free(cur);
}
}
return tree;
}
nbt_node* nbt_find(nbt_node* tree, nbt_predicate_t predicate, void* aux)
{
if(tree == NULL) return NULL;
if(predicate(tree, aux)) return tree;
if(tree->type != TAG_LIST &&
tree->type != TAG_COMPOUND) return NULL;
struct list_head* pos;
struct nbt_list* list = tree->type == TAG_LIST ? tree->payload.tag_list : tree->payload.tag_compound;
list_for_each(pos, &list->entry)
{
struct nbt_list* p = list_entry(pos, struct nbt_list, entry);
struct nbt_node* found;
if((found = nbt_find(p->data, predicate, aux)))
return found;
}
return NULL;
}
static bool names_are_equal(const nbt_node* node, void* vname)
{
const char* name = vname;
assert(node);
if(name == NULL && node->name == NULL)
return true;
if(name == NULL || node->name == NULL)
return false;
return strcmp(node->name, name) == 0;
}
nbt_node* nbt_find_by_name(nbt_node* tree, const char* name)
{
return nbt_find(tree, &names_are_equal, (void*)name);
}
/*
* Returns the index of the first occurence of `c' in `s', or the index of the
* NULL-terminator. Whichever comes first.
*/
static size_t index_of(const char* s, char c)
{
const char* p = s;
for(; *p; p++)
if(*p == c)
return p - s;
return p - s;
}
/*
* Pretends that s1 ends after `len' bytes, and does a strcmp.
*/
static int partial_strcmp(const char* s1, size_t len, const char* s2)
{
assert(s1);
if(s2 == NULL) return len != 0;
int r;
if((r = strncmp(s1, s2, len)) != 0)
return r;
/* at this point, the first `len' characters match. Check for NULL. */
return s2[len] != '\0';
}
/*
* Format:
* current_name.[other shit]
* OR
* current_name'\0'
*
* where current_name can be empty.
*/
nbt_node* nbt_find_by_path(nbt_node* tree, const char* path)
{
assert(tree);
assert(path);
/* The end of the "current_name" piece. */
size_t e = index_of(path, '.');
bool names_match = partial_strcmp(path, e, tree->name) == 0;
/* Names don't match. These aren't the droids you're looking for. */
if(!names_match) return NULL;
/* We're a leaf node, and the names match. Wooo found it. */
if(path[e] == '\0') return tree;
/*
* Initial names match, but the string isn't at the end. We're expecting a
* list, but haven't hit one.
*/
if(tree->type != TAG_LIST && tree->type != TAG_COMPOUND) return NULL;
/* At this point, the inital names match, and we're not at a leaf node. */
struct list_head* pos;
struct nbt_list* list = tree->type == TAG_LIST ? tree->payload.tag_list : tree->payload.tag_compound;
list_for_each(pos, &list->entry)
{
struct nbt_list* elem = list_entry(pos, struct nbt_list, entry);
nbt_node* r;
if((r = nbt_find_by_path(elem->data, path + e + 1)) != NULL)
return r;
}
/* Wasn't found in the list (or the current node isn't a list). Give up. */
return NULL;
}
/* Gets the length of the list, plus the length of all its children. */
static inline size_t nbt_full_list_length(struct nbt_list* list)
{
size_t accum = 0;
struct list_head* pos;
list_for_each(pos, &list->entry)
accum += nbt_size(list_entry(pos, const struct nbt_list, entry)->data);
return accum;
}
size_t nbt_size(const nbt_node* tree)
{
if(tree == NULL)
return 0;
if(tree->type == TAG_LIST)
return nbt_full_list_length(tree->payload.tag_list) + 1;
if(tree->type == TAG_COMPOUND)
return nbt_full_list_length(tree->payload.tag_compound) + 1;
return 1;
}
nbt_node* nbt_list_item(nbt_node* list, int n) {
if (list == NULL || (list->type != TAG_LIST && list->type != TAG_COMPOUND))
return NULL;
int i = 0;
const struct list_head* pos;
list_for_each(pos, &list->payload.tag_list->entry) {
if (i++ == n)
return list_entry(pos, struct nbt_list, entry)->data;
}
return NULL;
}