forked from chmod222/cNBT
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnbt_parsing.c
839 lines (637 loc) · 21.5 KB
/
nbt_parsing.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*
* -----------------------------------------------------------------------------
* "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 "include/nbt.h"
#include "buffer.h"
#include "include/list.h"
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
/* are we running on a little-endian system? */
static int little_endian()
{
uint16_t t = 0x0001;
char c[2];
memcpy(c, &t, sizeof t);
return c[0];
}
static void* swap_bytes(void* s, size_t len)
{
for(char* b = s,
* e = b + len - 1;
b < e;
b++, e--)
{
char t = *b;
*b = *e;
*e = t;
}
return s;
}
/* big endian to native endian. works in-place */
static void* be2ne(void* s, size_t len)
{
return little_endian() ? swap_bytes(s, len) : s;
}
/* native endian to big endian. works the exact same as its inverse */
#define ne2be be2ne
/* A special form of memcpy which copies `n' bytes into `dest', then returns
* `src' + n.
*/
static const void* memscan(void* dest, const void* src, size_t n)
{
memcpy(dest, src, n);
return (const char*)src + n;
}
/* Does a memscan, then goes from big endian to native endian on the
* destination.
*/
static const void* swapped_memscan(void* dest, const void* src, size_t n)
{
const void* ret = memscan(dest, src, n);
return be2ne(dest, n), ret;
}
#define CHECKED_MALLOC(var, n, on_error) do { \
if((var = malloc(n)) == NULL) \
{ \
errno = NBT_EMEM; \
on_error; \
} \
} while(0)
#define CHECKED_APPEND(b, ptr, len) do { \
if(buffer_append((b), (ptr), (len))) \
return NBT_EMEM; \
} while(0)
/* Parses a tag, given a name (may be NULL) and a type. Fills in the payload. */
static nbt_node* parse_unnamed_tag(nbt_type type, char* name, const char** memory, size_t* length);
/*
* Reads some bytes from the memory stream. This macro will read `n'
* bytes into `dest', call either memscan or swapped_memscan depending on
* `scanner', then fix the length. If anything funky goes down, `on_failure'
* will be executed.
*/
#define READ_GENERIC(dest, n, scanner, on_failure) do { \
if(*length < (n)) { on_failure; } \
*memory = scanner((dest), *memory, (n)); \
*length -= (n); \
} while(0)
/* printfs into the end of a buffer. Note: no null-termination! */
static void bprintf(struct buffer* b, const char* restrict format, ...)
{
va_list args;
int siz;
va_start(args, format);
siz = vsnprintf(NULL, 0, format, args);
va_end(args);
buffer_reserve(b, b->len + siz + 1);
va_start(args, format);
vsnprintf((char*)(b->data + b->len), siz + 1, format, args);
va_end(args);
b->len += siz; // remember - no null terminator!
}
/*
* Reads a string from memory, moving the pointer and updating the length
* appropriately. Returns NULL on failure.
*/
static char* read_string(const char** memory, size_t* length)
{
int16_t string_length;
char* ret = NULL;
READ_GENERIC(&string_length, sizeof string_length, swapped_memscan, goto parse_error);
if(string_length < 0) goto parse_error;
if(*length < (size_t)string_length) goto parse_error;
CHECKED_MALLOC(ret, string_length + 1, goto parse_error);
READ_GENERIC(ret, (size_t)string_length, memscan, goto parse_error);
ret[string_length] = '\0'; /* don't forget to NULL-terminate ;) */
return ret;
parse_error:
if(errno == NBT_OK)
errno = NBT_ERR;
free(ret);
return NULL;
}
static nbt_node* parse_named_tag(const char** memory, size_t* length)
{
char* name = NULL;
uint8_t type;
READ_GENERIC(&type, sizeof type, memscan, goto parse_error);
name = read_string(memory, length);
nbt_node* ret = parse_unnamed_tag((nbt_type)type, name, memory, length);
if(ret == NULL) goto parse_error;
return ret;
parse_error:
if(errno == NBT_OK)
errno = NBT_ERR;
free(name);
return NULL;
}
static struct nbt_byte_array read_byte_array(const char** memory, size_t* length)
{
struct nbt_byte_array ret;
ret.data = NULL;
READ_GENERIC(&ret.length, sizeof ret.length, swapped_memscan, goto parse_error);
if(ret.length < 0) goto parse_error;
CHECKED_MALLOC(ret.data, ret.length, goto parse_error);
READ_GENERIC(ret.data, (size_t)ret.length, memscan, goto parse_error);
return ret;
parse_error:
if(errno == NBT_OK)
errno = NBT_ERR;
free(ret.data);
ret.data = NULL;
return ret;
}
static struct nbt_int_array read_int_array(const char** memory, size_t* length)
{
struct nbt_int_array ret;
ret.data = NULL;
READ_GENERIC(&ret.length, sizeof ret.length, swapped_memscan, goto parse_error);
if(ret.length < 0) goto parse_error;
CHECKED_MALLOC(ret.data, ret.length * sizeof(int32_t), goto parse_error);
READ_GENERIC(ret.data, (size_t)ret.length * sizeof(int32_t), memscan, goto parse_error);
// Byteswap the whole array.
for(int32_t i = 0; i < ret.length; i++)
be2ne(ret.data + i, sizeof(int32_t));
return ret;
parse_error:
if(errno == NBT_OK)
errno = NBT_ERR;
free(ret.data);
ret.data = NULL;
return ret;
}
static struct nbt_long_array read_long_array(const char** memory, size_t* length)
{
struct nbt_long_array ret;
ret.data = NULL;
READ_GENERIC(&ret.length, sizeof ret.length, swapped_memscan, goto parse_error);
if(ret.length < 0) goto parse_error;
CHECKED_MALLOC(ret.data, ret.length * sizeof(int64_t), goto parse_error);
READ_GENERIC(ret.data, (size_t)ret.length * sizeof(int64_t), memscan, goto parse_error);
// Byteswap the whole array.
for(int32_t i = 0; i < ret.length; i++)
be2ne(ret.data + i, sizeof(int64_t));
return ret;
parse_error:
if(errno == NBT_OK)
errno = NBT_ERR;
free(ret.data);
ret.data = NULL;
return ret;
}
/*
* Is the list all one type? If yes, return the type. Otherwise, return
* TAG_INVALID
*/
static nbt_type list_is_homogenous(const struct nbt_list* list)
{
nbt_type type = TAG_INVALID;
const struct list_head* pos;
list_for_each(pos, &list->entry)
{
const struct nbt_list* cur = list_entry(pos, const struct nbt_list, entry);
assert(cur->data);
assert(cur->data->type != TAG_INVALID);
if(cur->data->type == TAG_INVALID)
return TAG_INVALID;
/* if we're the first type, just set it to our current type */
if(type == TAG_INVALID) type = cur->data->type;
if(type != cur->data->type)
return TAG_INVALID;
}
/* if the list was empty, use the sentinel type */
if(type == TAG_INVALID && list->data != NULL)
type = list->data->type;
return type;
}
static struct nbt_list* read_list(const char** memory, size_t* length)
{
uint8_t type;
int32_t elems;
struct nbt_list* ret;
CHECKED_MALLOC(ret, sizeof *ret, goto parse_error);
/* we allocate the data pointer to store the type of the list in the first
* sentinel element */
CHECKED_MALLOC(ret->data, sizeof *ret->data, goto parse_error);
INIT_LIST_HEAD(&ret->entry);
READ_GENERIC(&type, sizeof type, swapped_memscan, goto parse_error);
READ_GENERIC(&elems, sizeof elems, swapped_memscan, goto parse_error);
ret->data->type = type == TAG_INVALID ? TAG_COMPOUND : (nbt_type)type;
for(int32_t i = 0; i < elems; i++)
{
struct nbt_list* new;
CHECKED_MALLOC(new, sizeof *new, goto parse_error);
new->data = parse_unnamed_tag((nbt_type)type, NULL, memory, length);
if(new->data == NULL)
{
free(new);
goto parse_error;
}
list_add_tail(&new->entry, &ret->entry);
}
return ret;
parse_error:
if(errno == NBT_OK)
errno = NBT_ERR;
nbt_free_list(ret);
return NULL;
}
static struct nbt_list* read_compound(const char** memory, size_t* length)
{
struct nbt_list* ret;
CHECKED_MALLOC(ret, sizeof *ret, goto parse_error);
ret->data = NULL;
INIT_LIST_HEAD(&ret->entry);
for(;;)
{
uint8_t type;
char* name = NULL;
struct nbt_list* new_entry;
READ_GENERIC(&type, sizeof type, swapped_memscan, goto parse_error);
if(type == 0) break; /* TAG_END == 0. We've hit the end of the list when type == TAG_END. */
name = read_string(memory, length);
if(name == NULL) goto parse_error;
CHECKED_MALLOC(new_entry, sizeof *new_entry,
free(name);
goto parse_error;
);
new_entry->data = parse_unnamed_tag((nbt_type)type, name, memory, length);
if(new_entry->data == NULL)
{
free(new_entry);
free(name);
goto parse_error;
}
list_add_tail(&new_entry->entry, &ret->entry);
}
return ret;
parse_error:
if(errno == NBT_OK)
errno = NBT_ERR;
nbt_free_list(ret);
return NULL;
}
/*
* Parses a tag, given a name (may be NULL) and a type. Fills in the payload.
*/
static nbt_node* parse_unnamed_tag(nbt_type type, char* name, const char** memory, size_t* length)
{
nbt_node* node;
CHECKED_MALLOC(node, sizeof *node, goto parse_error);
node->type = type;
node->name = name;
#define COPY_INTO_PAYLOAD(payload_name) \
READ_GENERIC(&node->payload.payload_name, sizeof node->payload.payload_name, swapped_memscan, goto parse_error);
switch(type)
{
case TAG_BYTE:
COPY_INTO_PAYLOAD(tag_byte);
break;
case TAG_SHORT:
COPY_INTO_PAYLOAD(tag_short);
break;
case TAG_INT:
COPY_INTO_PAYLOAD(tag_int);
break;
case TAG_LONG:
COPY_INTO_PAYLOAD(tag_long);
break;
case TAG_FLOAT:
COPY_INTO_PAYLOAD(tag_float);
break;
case TAG_DOUBLE:
COPY_INTO_PAYLOAD(tag_double);
break;
case TAG_BYTE_ARRAY:
node->payload.tag_byte_array = read_byte_array(memory, length);
break;
case TAG_INT_ARRAY:
node->payload.tag_int_array = read_int_array(memory, length);
break;
case TAG_LONG_ARRAY:
node->payload.tag_long_array = read_long_array(memory, length);
break;
case TAG_STRING:
node->payload.tag_string = read_string(memory, length);
break;
case TAG_LIST:
node->payload.tag_list = read_list(memory, length);
break;
case TAG_COMPOUND:
node->payload.tag_compound = read_compound(memory, length);
break;
default:
goto parse_error; /* Unknown node or TAG_END. Either way, we shouldn't be parsing this. */
}
#undef COPY_INTO_PAYLOAD
if(errno != NBT_OK) goto parse_error;
return node;
parse_error:
if(errno == NBT_OK)
errno = NBT_ERR;
free(node);
return NULL;
}
nbt_node* nbt_parse(const void* mem, size_t* length)
{
errno = NBT_OK;
const char** memory = (const char**)&mem;
return parse_named_tag(memory, length);
}
/* spaces, not tabs ;) */
static void indent(struct buffer* b, size_t amount)
{
size_t spaces = amount * 4; /* 4 spaces per indent */
char temp[spaces + 1];
for(size_t i = 0; i < spaces; ++i)
temp[i] = ' ';
temp[spaces] = '\0';
bprintf(b, "%s", temp);
}
static nbt_status __nbt_dump_ascii(const nbt_node*, struct buffer*, size_t ident);
/* prints the node's name, or (null) if it has none. */
#define SAFE_NAME(node) ((node)->name ? (node)->name : "<null>")
static void dump_byte_array(const struct nbt_byte_array ba, struct buffer* b)
{
assert(ba.length >= 0);
bprintf(b, "[ ");
for(int32_t i = 0; i < ba.length; ++i)
bprintf(b, "%u ", +ba.data[i]);
bprintf(b, "]");
}
static void dump_int_array(const struct nbt_int_array ia, struct buffer* b)
{
assert(ia.length >= 0);
bprintf(b, "[ ");
for(int32_t i = 0; i < ia.length; ++i)
bprintf(b, "%u ", +ia.data[i]);
bprintf(b, "]");
}
static void dump_long_array(const struct nbt_long_array la, struct buffer* b)
{
assert(la.length >= 0);
bprintf(b, "[ ");
for(int32_t i = 0; i < la.length; ++i)
bprintf(b, "%u ", +la.data[i]);
bprintf(b, "]");
}
static nbt_status dump_list_contents_ascii(const struct nbt_list* list, struct buffer* b, size_t ident)
{
const struct list_head* pos;
list_for_each(pos, &list->entry)
{
const struct nbt_list* entry = list_entry(pos, const struct nbt_list, entry);
nbt_status err;
if((err = __nbt_dump_ascii(entry->data, b, ident)) != NBT_OK)
return err;
}
return NBT_OK;
}
static nbt_status __nbt_dump_ascii(const nbt_node* tree, struct buffer* b, size_t ident)
{
if(tree == NULL) return NBT_OK;
indent(b, ident);
if(tree->type == TAG_BYTE)
bprintf(b, "TAG_Byte(\"%s\"): %i\n", SAFE_NAME(tree), (int)tree->payload.tag_byte);
else if(tree->type == TAG_SHORT)
bprintf(b, "TAG_Short(\"%s\"): %i\n", SAFE_NAME(tree), (int)tree->payload.tag_short);
else if(tree->type == TAG_INT)
bprintf(b, "TAG_Int(\"%s\"): %i\n", SAFE_NAME(tree), (int)tree->payload.tag_int);
else if(tree->type == TAG_LONG)
bprintf(b, "TAG_Long(\"%s\"): %" PRIi64 "\n", SAFE_NAME(tree), tree->payload.tag_long);
else if(tree->type == TAG_FLOAT)
bprintf(b, "TAG_Float(\"%s\"): %f\n", SAFE_NAME(tree), (double)tree->payload.tag_float);
else if(tree->type == TAG_DOUBLE)
bprintf(b, "TAG_Double(\"%s\"): %f\n", SAFE_NAME(tree), tree->payload.tag_double);
else if(tree->type == TAG_BYTE_ARRAY)
{
bprintf(b, "TAG_Byte_Array(\"%s\"): ", SAFE_NAME(tree));
dump_byte_array(tree->payload.tag_byte_array, b);
bprintf(b, "\n");
}
else if(tree->type == TAG_INT_ARRAY)
{
bprintf(b, "Tag_Int_Array(\"%s\"): ", SAFE_NAME(tree));
dump_int_array(tree->payload.tag_int_array, b);
bprintf(b, "\n");
}
else if(tree->type == TAG_LONG_ARRAY)
{
bprintf(b, "Tag_Long_Array(\"%s\"): ", SAFE_NAME(tree));
dump_long_array(tree->payload.tag_long_array, b);
bprintf(b, "\n");
}
else if(tree->type == TAG_STRING)
{
if(tree->payload.tag_string == NULL)
return NBT_ERR;
bprintf(b, "TAG_String(\"%s\"): %s\n", SAFE_NAME(tree), tree->payload.tag_string);
}
else if(tree->type == TAG_LIST)
{
bprintf(b, "TAG_List(\"%s\") [%s]\n", SAFE_NAME(tree), nbt_type_to_string(tree->payload.tag_list->data->type));
indent(b, ident);
bprintf(b, "{\n");
nbt_status err = dump_list_contents_ascii(tree->payload.tag_list, b, ident + 1);
indent(b, ident);
bprintf(b, "}\n");
if(err != NBT_OK)
return err;
}
else if(tree->type == TAG_COMPOUND)
{
bprintf(b, "TAG_Compound(\"%s\")\n", SAFE_NAME(tree));
indent(b, ident);
bprintf(b, "{\n");
nbt_status err = dump_list_contents_ascii(tree->payload.tag_compound, b, ident + 1);
indent(b, ident);
bprintf(b, "}\n");
if(err != NBT_OK)
return err;
}
else
return NBT_ERR;
return NBT_OK;
}
char* nbt_dump_ascii(const nbt_node* tree)
{
errno = NBT_OK;
assert(tree);
struct buffer b = BUFFER_INIT;
if((errno = __nbt_dump_ascii(tree, &b, 0)) != NBT_OK) goto OOM;
if( buffer_reserve(&b, b.len + 1)) goto OOM;
b.data[b.len] = '\0'; /* null-terminate that biatch, since bprintf doesn't
do that for us. */
return (char*)b.data;
OOM:
if(errno != NBT_OK)
errno = NBT_EMEM;
buffer_free(&b);
return NULL;
}
static nbt_status dump_byte_array_binary(const struct nbt_byte_array ba, struct buffer* b)
{
int32_t dumped_length = ba.length;
ne2be(&dumped_length, sizeof dumped_length);
CHECKED_APPEND(b, &dumped_length, sizeof dumped_length);
if(ba.length) assert(ba.data);
CHECKED_APPEND(b, ba.data, ba.length);
return NBT_OK;
}
static nbt_status dump_int_array_binary(const struct nbt_int_array ia, struct buffer* b)
{
int32_t dumped_length = ia.length;
ne2be(&dumped_length, sizeof dumped_length);
CHECKED_APPEND(b, &dumped_length, sizeof dumped_length);
if(ia.length) assert(ia.data);
for(int32_t i = 0; i < ia.length; i++)
{
int32_t swappedElem = ia.data[i];
ne2be(&swappedElem, sizeof(swappedElem));
CHECKED_APPEND(b, &swappedElem, sizeof(swappedElem));
}
return NBT_OK;
}
static nbt_status dump_long_array_binary(const struct nbt_long_array la, struct buffer* b)
{
int32_t dumped_length = la.length;
ne2be(&dumped_length, sizeof dumped_length);
CHECKED_APPEND(b, &dumped_length, sizeof dumped_length);
if(la.length) assert(la.data);
for(int32_t i = 0; i < la.length; i++)
{
int64_t swappedElem = la.data[i];
ne2be(&swappedElem, sizeof(swappedElem));
CHECKED_APPEND(b, &swappedElem, sizeof(swappedElem));
}
return NBT_OK;
}
static nbt_status dump_string_binary(const char* name, struct buffer* b)
{
assert(name);
size_t len = strlen(name);
if(len > 32767 /* SHORT_MAX */)
return NBT_ERR;
{ /* dump the length */
int16_t dumped_len = (int16_t)len;
ne2be(&dumped_len, sizeof dumped_len);
CHECKED_APPEND(b, &dumped_len, sizeof dumped_len);
}
CHECKED_APPEND(b, name, len);
return NBT_OK;
}
static nbt_status __dump_binary(const nbt_node*, bool, struct buffer*);
static nbt_status dump_list_binary(const struct nbt_list* list, struct buffer* b)
{
nbt_type type = list_is_homogenous(list);
size_t len = list_length(&list->entry);
if(len > 2147483647 /* INT_MAX */)
return NBT_ERR;
assert(type != TAG_INVALID);
if(type == TAG_INVALID)
return NBT_ERR;
{
int8_t _type = (int8_t)type;
ne2be(&_type, sizeof _type); /* unnecessary, but left in to keep similar code looking similar */
CHECKED_APPEND(b, &_type, sizeof _type);
}
{
int32_t dumped_len = (int32_t)len;
ne2be(&dumped_len, sizeof dumped_len);
CHECKED_APPEND(b, &dumped_len, sizeof dumped_len);
}
const struct list_head* pos;
list_for_each(pos, &list->entry)
{
const struct nbt_list* entry = list_entry(pos, const struct nbt_list, entry);
nbt_status ret;
if((ret = __dump_binary(entry->data, false, b)) != NBT_OK)
return ret;
}
return NBT_OK;
}
static nbt_status dump_compound_binary(const struct nbt_list* list, struct buffer* b)
{
const struct list_head* pos;
list_for_each(pos, &list->entry)
{
const struct nbt_list* entry = list_entry(pos, const struct nbt_list, entry);
nbt_status ret;
if((ret = __dump_binary(entry->data, true, b)) != NBT_OK)
return ret;
}
/* write out TAG_End */
uint8_t zero = 0;
CHECKED_APPEND(b, &zero, sizeof zero);
return NBT_OK;
}
/*
* @param dump_type Should we dump the type, or just skip it? We need to skip
* when dumping lists, because the list header already says
* the type.
*/
static nbt_status __dump_binary(const nbt_node* tree, bool dump_type, struct buffer* b)
{
if(dump_type)
{ /* write out the type */
int8_t type = (int8_t)tree->type;
CHECKED_APPEND(b, &type, sizeof type);
}
if(tree->name)
{
nbt_status err;
if((err = dump_string_binary(tree->name, b)) != NBT_OK)
return err;
}
#define DUMP_NUM(type, x) do { \
type temp = x; \
ne2be(&temp, sizeof temp); \
CHECKED_APPEND(b, &temp, sizeof temp); \
} while(0)
if(tree->type == TAG_BYTE)
DUMP_NUM(int8_t, tree->payload.tag_byte);
else if(tree->type == TAG_SHORT)
DUMP_NUM(int16_t, tree->payload.tag_short);
else if(tree->type == TAG_INT)
DUMP_NUM(int32_t, tree->payload.tag_int);
else if(tree->type == TAG_LONG)
DUMP_NUM(int64_t, tree->payload.tag_long);
else if(tree->type == TAG_FLOAT)
DUMP_NUM(float, tree->payload.tag_float);
else if(tree->type == TAG_DOUBLE)
DUMP_NUM(double, tree->payload.tag_double);
else if(tree->type == TAG_BYTE_ARRAY)
return dump_byte_array_binary(tree->payload.tag_byte_array, b);
else if(tree->type == TAG_INT_ARRAY)
return dump_int_array_binary(tree->payload.tag_int_array, b);
else if(tree->type == TAG_LONG_ARRAY)
return dump_long_array_binary(tree->payload.tag_long_array, b);
else if(tree->type == TAG_STRING)
return dump_string_binary(tree->payload.tag_string, b);
else if(tree->type == TAG_LIST)
return dump_list_binary(tree->payload.tag_list, b);
else if(tree->type == TAG_COMPOUND)
return dump_compound_binary(tree->payload.tag_compound, b);
else
return NBT_ERR;
return NBT_OK;
#undef DUMP_NUM
}
struct buffer nbt_dump_binary(const nbt_node* tree)
{
errno = NBT_OK;
if(tree == NULL) return BUFFER_INIT;
struct buffer ret = BUFFER_INIT;
errno = __dump_binary(tree, true, &ret);
return ret;
}