-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatautils.c
1325 lines (1232 loc) · 35.7 KB
/
datautils.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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _DEFAULT_SOURCE
#include <endian.h>
#undef _DEFAULT_SOURCE
#include "cNBT/nbt.h"
#include "datautils.h"
#include "sds.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
// 2^30, when we need a max_len but we're sure we wont overflow
// What we really need is an "unsafe" version of functions we're passing this
// to
#define NO_OVERFLOW 1073741824
// Warning: Some deeply monotonous code is in this file.
// ProtoDef Numeric Types
char *enc_byte(char *dest, uint8_t source) {
*dest = source;
return dest + sizeof(source);
}
char *dec_byte(uint8_t *dest, char *source) {
*dest = *source;
return source + sizeof(*dest);
}
char *enc_be16(char *dest, uint16_t source) {
memcpy(dest, &(uint16_t){htobe16(source)}, sizeof(source));
return dest + sizeof(source);
}
char *dec_be16(uint16_t *dest, char *source) {
*dest = be16toh(*(uint16_t *){memcpy(dest, source, sizeof(*dest))});
return source + sizeof(*dest);
}
char *enc_be32(char *dest, uint32_t source) {
memcpy(dest, &(uint32_t){htobe32(source)}, sizeof(source));
return dest + sizeof(source);
}
char *dec_be32(uint32_t *dest, char *source) {
*dest = be32toh(*(uint32_t *){memcpy(dest, source, sizeof(*dest))});
return source + sizeof(*dest);
}
char *enc_be64(char *dest, uint64_t source) {
memcpy(dest, &(uint64_t){htobe64(source)}, sizeof(source));
return dest + sizeof(source);
}
char *dec_be64(uint64_t *dest, char *source) {
*dest = be64toh(*(uint64_t *){memcpy(dest, source, sizeof(*dest))});
return source + sizeof(*dest);
}
char *enc_le16(char *dest, uint16_t source) {
memcpy(dest, &(uint16_t){htole16(source)}, sizeof(source));
return dest + sizeof(source);
}
char *dec_le16(uint16_t *dest, char *source) {
*dest = le16toh(*(uint16_t *){memcpy(dest, source, sizeof(*dest))});
return source + sizeof(*dest);
}
char *enc_le32(char *dest, uint32_t source) {
memcpy(dest, &(uint32_t){htole32(source)}, sizeof(source));
return dest + sizeof(source);
}
char *dec_le32(uint32_t *dest, char *source) {
*dest = le32toh(*(uint32_t *){memcpy(dest, source, sizeof(*dest))});
return source + sizeof(*dest);
}
char *enc_le64(char *dest, uint64_t source) {
memcpy(dest, &(uint64_t){htole64(source)}, sizeof(source));
return dest + sizeof(source);
}
char *dec_le64(uint64_t *dest, char *source) {
*dest = le64toh(*(uint64_t *){memcpy(dest, source, sizeof(*dest))});
return source + sizeof(*dest);
}
char *enc_bef32(char *dest, float source) {
uint32_t i = htobe32(*(uint32_t *) memcpy(&i, &source, sizeof(source)));
memcpy(dest, &i, sizeof(source));
return dest + sizeof(source);
}
char *dec_bef32(float *dest, char *source) {
uint32_t i = be32toh(*(uint32_t *) memcpy(&i, source, sizeof(*dest)));
memcpy(dest, &i, sizeof(*dest));
return source + sizeof(*dest);
}
char *enc_bef64(char *dest, double source) {
uint64_t i = htobe64(*(uint64_t *) memcpy(&i, &source, sizeof(source)));
memcpy(dest, &i, sizeof(source));
return dest + sizeof(source);
}
char *dec_bef64(double *dest, char *source) {
uint64_t i = be64toh(*(uint64_t *) memcpy(&i, source, sizeof(*dest)));
memcpy(dest, &i, sizeof(*dest));
return source + sizeof(*dest);
}
char *enc_lef32(char *dest, float source) {
uint32_t i = htole32(*(uint32_t *) memcpy(&i, &source, sizeof(source)));
memcpy(dest, &i, sizeof(source));
return dest + sizeof(source);
}
char *dec_lef32(float *dest, char *source) {
uint32_t i = le32toh(*(uint32_t *) memcpy(&i, source, sizeof(*dest)));
memcpy(dest, &i, sizeof(*dest));
return source + sizeof(*dest);
}
char *enc_lef64(char *dest, double source) {
uint64_t i = htole64(*(uint64_t *) memcpy(&i, &source, sizeof(source)));
memcpy(dest, &i, sizeof(source));
return dest + sizeof(source);
}
char *dec_lef64(double *dest, char *source) {
uint64_t i = le64toh(*(uint64_t *) memcpy(&i, source, sizeof(*dest)));
memcpy(dest, &i, sizeof(*dest));
return source + sizeof(*dest);
}
char *enc_buffer(char *dest, mc_buffer source) {
memcpy(dest, source.base, source.len);
return dest + source.len;
}
char *dec_buffer(mc_buffer *dest, char *source, size_t len) {
if(!(dest->base = malloc(len)))
return NULL;
dest->len = len;
memcpy(dest->base, source, len);
return source + len;
}
void free_buffer(mc_buffer buffer) { free(buffer.base); }
// size_ functions let you know how big a type structure is going to be whe
// its encoded into a memory buffer
// walk_ functions let you know how big a currently encoded type is, and
// will error if the type is invalid, they can't complete the walk, or max_len
// doesn't have enough room for the calculated size
size_t size_varint(uint32_t varint) {
if(varint < (1 << 7))
return 1;
if(varint < (1 << 14))
return 2;
if(varint < (1 << 21))
return 3;
if(varint < (1 << 28))
return 4;
return 5;
}
int walk_varint(char *source, size_t max_len) {
if(!max_len)
return varnum_overrun;
int len = 1;
for(; *(unsigned char *)source & 0x80; source++, len++) {
if(len > 4)
return varnum_invalid;
if((size_t)len >= max_len)
return varnum_overrun;
}
return len;
}
char *enc_varint(char *dest, uint32_t source) {
for(; source >= 0x80; dest++, source >>= 7) {
*dest = 0x80 | (source & 0x7F);
}
*dest = source & 0x7F;
return ++dest;
}
char *dec_varint(int32_t *dest, char *source) {
*dest = 0;
int i = 0;
for(; *(unsigned char *)source & 0x80; source++, i+=7) {
*(uint32_t *)dest |= (*source & 0x7F) << i;
}
*(uint32_t *)dest |= (*source & 0x7F) << i;
return ++source;
}
// Everything past this point isn't part of ProtoDef, just minecraft
size_t size_varlong(uint64_t varint) {
if(varint < (1 << 7))
return 1;
if(varint < (1 << 14))
return 2;
if(varint < (1 << 21))
return 3;
if(varint < (1 << 28))
return 4;
if(varint < (1ULL << 35))
return 5;
if(varint < (1ULL << 42))
return 6;
if(varint < (1ULL << 49))
return 7;
if(varint < (1ULL << 56))
return 8;
if(varint < (1ULL << 63))
return 9;
return 10;
}
int walk_varlong(char *source, size_t max_len) {
if(!max_len)
return varnum_overrun;
int len = 1;
for(; *(unsigned char *)source & 0x80; source++, len++) {
if(len > 8)
return varnum_invalid;
if((size_t)len >= max_len)
return varnum_overrun;
}
return len;
}
char *enc_varlong(char *dest, uint64_t source) {
for(; source >= 0x80; dest++, source >>= 7) {
*dest = 0x80 | (source & 0x7F);
}
*dest = source & 0x7F;
return ++dest;
}
char *dec_varlong(int64_t *dest, char *source) {
*dest = 0;
int i = 0;
for(; *(unsigned char *)source & 0x80; source++, i+=7) {
*(uint64_t *)dest |= (*source & 0x7F) << i;
}
*(uint64_t *)dest |= (*source & 0x7F) << i;
return ++source;
}
// Varint prefixed string
size_t size_string(sds string) {
return size_varint(sdslen(string)) + sdslen(string);
}
int walk_string(char *source, size_t max_len) {
int ret = walk_varint(source, max_len);
if(ret < 0)
return ret;
int32_t len;
dec_varint(&len, source);
if(max_len < (size_t)ret + len)
return -1;
return ret + len;
}
char *enc_string(char *dest, sds source) {
int32_t len = (int32_t)sdslen(source);
return (char *)memcpy(enc_varint(dest, len), source, len) + len;
}
char *dec_string(sds *dest, char *source) {
int32_t len;
source = dec_varint(&len, source);
*dest = sdsnewlen(source, (size_t)len);
return source + len;
}
// Big Endian 128-bit uint
char *enc_uuid(char *dest, mc_uuid source) {
return enc_be64(enc_be64(dest, source.msb), source.lsb);
}
char *dec_uuid(mc_uuid *dest, char *source) {
return dec_be64(&dest->lsb, dec_be64(&dest->msb, source));
}
// From MSB to LSB x: 26-bits, y: 12-bits, z: 26-bits
// each is an independent signed 2-complement integer
char *enc_position(char *dest, mc_position source) {
return enc_be64(dest, ((uint64_t)source.x & 0x3FFFFFF) << 38 |
((uint64_t)source.y & 0xFFF) << 26 |
(source.z & 0x3FFFFFF));
}
char *dec_position(mc_position *dest, char *source) {
uint64_t i;
source = dec_be64(&i, source);
if((dest->x = i >> 38) >= 1 << 25)
dest->x -= 1 << 26;
if((dest->y = (i >> 26) & 0xFFF) >= 1 << 11)
dest->y -= 1 << 12;
if((dest->z = i & 0x3FFFFFF) >= 1 << 25)
dest->z -= 1 << 26;
return source;
}
size_t size_nbt_list(nbt_node *node) {
size_t size = 0;
struct list_head *pos;
list_for_each(pos, &node->payload.tag_list->entry) {
size += size_unnamed_nbt(list_entry(pos, struct nbt_list, entry)->data);
}
return size;
}
size_t size_nbt_compound(nbt_node *node) {
size_t size = 1;
struct list_head *pos;
list_for_each(pos, &node->payload.tag_compound->entry) {
nbt_node *member = list_entry(pos, struct nbt_list, entry)->data;
if(!member->name) {
size += sizeof(uint8_t) + sizeof(int16_t) + size_unnamed_nbt(member);
} else {
size += sizeof(uint8_t) + sizeof(int16_t) + strlen(member->name);
size += size_unnamed_nbt(member);
}
}
return size;
}
size_t size_unnamed_nbt(nbt_node *node) {
switch(node->type) {
case TAG_BYTE:
return sizeof(char);
case TAG_SHORT:
return sizeof(int16_t);
case TAG_INT:
return sizeof(int32_t);
case TAG_LONG:
return sizeof(int64_t);
case TAG_FLOAT:
return sizeof(float);
case TAG_DOUBLE:
return sizeof(double);
case TAG_BYTE_ARRAY:
return sizeof(int32_t) + node->payload.tag_int_array.length;
case TAG_INT_ARRAY:
return sizeof(int32_t) +
node->payload.tag_int_array.length * sizeof(int32_t);
case TAG_LONG_ARRAY:
return sizeof(int32_t) +
node->payload.tag_int_array.length * sizeof(int64_t);
case TAG_STRING:
return sizeof(int16_t) + strlen(node->payload.tag_string);
case TAG_LIST:
return size_nbt_list(node);
case TAG_COMPOUND:
return size_nbt_compound(node);
default:
return 0;
}
}
size_t size_nbt(nbt_node *nbt) {
if(!nbt->name) {
return sizeof(uint8_t) + sizeof(int16_t) + size_unnamed_nbt(nbt);
} else {
return sizeof(uint8_t) + sizeof(int16_t) + strlen(nbt->name) +
size_unnamed_nbt(nbt);
}
}
int walk_nbt_string(char *source, size_t max_len) {
uint16_t len;
if(max_len < sizeof(len))
return -1;
dec_be16(&len, source);
if(max_len < sizeof(len) + len)
return -1;
return sizeof(len) + len;
}
int walk_nbt_array(char *source, size_t max_len, size_t type_len) {
uint32_t len;
if(max_len < sizeof(len))
return -1;
dec_be32(&len, source);
if(max_len < sizeof(len) + (len * type_len))
return -1;
return sizeof(len) + (len * type_len);
}
int walk_nbt_list(char *source, size_t max_len) {
uint8_t type;
int32_t count;
int size = sizeof(type) + sizeof(count);
if(max_len < (size_t)size)
return -1;
source = dec_byte(&type, source);
source = dec_be32((uint32_t *)&count, source);
for(int i = 0, step; i < count;
i++, source += step, max_len -= step, size += step) {
step = walk_unnamed_nbt(source, type, max_len);
if(step < 0)
return -1;
}
return size;
}
int walk_nbt_compound(char *source, size_t max_len) {
for(int tag_size, total_size = 0;;
source += tag_size, total_size += tag_size, max_len -= tag_size) {
if(!max_len)
return -1;
uint8_t type;
source = dec_byte(&type, source);
total_size += sizeof(type);
if(!type)
return total_size;
if((tag_size = walk_nbt_string(source, --max_len)) < 0)
return -1;
source += tag_size;
total_size += tag_size;
max_len -= tag_size;
if((tag_size = walk_unnamed_nbt(source, type, max_len)) < 0)
return -1;
}
}
int walk_unnamed_nbt(char *source, nbt_type type, size_t max_len) {
int size;
switch(type) {
case TAG_BYTE:
size = sizeof(char);
break;
case TAG_SHORT:
size = sizeof(int16_t);
break;
case TAG_INT:
size = sizeof(int32_t);
break;
case TAG_LONG:
size = sizeof(int64_t);
break;
case TAG_FLOAT:
size = sizeof(float);
break;
case TAG_DOUBLE:
size = sizeof(double);
break;
case TAG_BYTE_ARRAY:
if((size = walk_nbt_array(source, max_len, sizeof(char))) < 0)
return -1;
break;
case TAG_INT_ARRAY:
if((size = walk_nbt_array(source, max_len, sizeof(int32_t))) < 0)
return -1;
break;
case TAG_LONG_ARRAY:
if((size = walk_nbt_array(source, max_len, sizeof(int64_t))) < 0)
return -1;
break;
case TAG_STRING:
if((size = walk_nbt_string(source, max_len)) < 0)
return -1;
break;
case TAG_LIST:
if((size = walk_nbt_list(source, max_len)) < 0)
return -1;
break;
case TAG_COMPOUND:
if((size = walk_nbt_compound(source, max_len)) < 0)
return -1;
break;
default:
return -1;
}
if(max_len < (size_t)size)
return -1;
return size;
}
int walk_nbt(char *source, size_t max_len) {
if(!max_len)
return -1;
uint8_t type;
int size, total = sizeof(type);
source = dec_byte(&type, source);
if((size = walk_nbt_string(source, --max_len)) < 0)
return -1;
source += size;
total += size;
max_len -= size;
if((size = walk_unnamed_nbt(source, type, max_len)) < 0)
return -1;
return total + size;
}
// TODO: Need a nbt_dump_to_buffer method so we don't allocate memory or risk
// failure here. Our own dest buffer should be big enough based on size_
// functions so there is really no excuse for an encode to fail.
char *enc_nbt(char *dest, nbt_node *source) {
struct buffer b = nbt_dump_binary(source);
memcpy(dest, b.data, b.len);
free(b.data);
return dest + b.len;
}
char *dec_nbt(nbt_node **dest, char *source) {
// nbt_parse2 is a stupid hack I kludged in order to pass char** to cNBT and
// advance our source pointer appropriately. Also, max_len shouldn't be
// a thing since we require the nbt to be walked before parsing.
if(!(*dest = nbt_parse2(&source, NO_OVERFLOW)))
return NULL;
return source;
}
int walk_optnbt(char *source, size_t max_len) {
if(!max_len)
return -1;
if(*source != TAG_COMPOUND)
return sizeof(*source);
return walk_nbt(source, max_len);
}
size_t size_optnbt(nbt_node *nbt) {
if(!nbt)
return sizeof(char);
return size_nbt(nbt);
}
char *enc_optnbt(char *dest, nbt_node *source) {
if(!source)
return enc_byte(dest, 0);
return enc_nbt(dest, source);
}
char *dec_optnbt(nbt_node **dest, char *source) {
if(*source != TAG_COMPOUND) {
*dest = NULL;
return ++source;
}
return dec_nbt(dest, source);
}
// Inventory slot
int walk_slot(char *source, size_t max_len) {
mc_slot slot;
if(max_len < sizeof(slot.present))
return -1;
source = dec_byte((uint8_t *) &slot.present, source);
size_t size = sizeof(slot.present);
if(!slot.present)
return size;
int ret = walk_varint(source, --max_len);
if(ret < 0)
return -1;
if(max_len -= ret < sizeof(slot.count))
return -1;
max_len -= sizeof(slot.count);
size += ret + sizeof(slot.count);
source += ret + sizeof(slot.count);
if(*source != TAG_COMPOUND)
return ++size;
ret = walk_nbt(source, max_len);
if(ret < 0)
return -1;
return size + ret;
}
size_t size_slot(mc_slot slot) {
if(!slot.present)
return sizeof(slot.present);
size_t size = sizeof(slot.present) + sizeof(slot.count);
size += size_varint(slot.id);
if(slot.nbt)
size += size_nbt(slot.nbt);
return size;
}
// Just like varint/long, we assume you're being a good citizen and using the
// size function to ensure no buffer overflows.
char *enc_slot(char *dest, mc_slot source) {
dest = enc_byte(dest, (uint8_t) source.present);
if(!source.present)
return dest;
dest = enc_varint(dest, (uint32_t) source.id);
dest = enc_byte(dest, source.count);
if(!source.nbt)
return enc_byte(dest, 0);
return enc_nbt(dest, source.nbt);
}
// dec_nbt allocates memory, this can fail and return NULL
char *dec_slot(mc_slot *dest, char *source) {
source = dec_byte((uint8_t *)&dest->present, source);
if(!dest->present)
return source;
source = dec_varint(&dest->id, source);
source = dec_byte(&dest->count, source);
if(*source != TAG_COMPOUND) {
dest->nbt = NULL;
return ++source;
}
return dec_nbt(&dest->nbt, source);
}
void free_slot(mc_slot slot) {
nbt_free(slot.nbt);
}
// Varint prefixed array of slots
int walk_ingredient(char *source, size_t max_len) {
int ret = walk_varint(source, max_len);
if(ret < 0)
return ret;
int size = ret;
max_len -= ret;
int32_t len;
source = dec_varint(&len, source);
for(int i = 0; i < len; i++, size += ret, max_len -= ret) {
if((ret = walk_slot(source, max_len)) < 0)
return ret;
}
return size;
}
size_t size_ingredient(mc_ingredient ingredient) {
size_t size = size_varint(ingredient.count);
for(int i = 0; i < ingredient.count; i++) {
size += size_slot(ingredient.items[i]);
}
return size;
}
char *enc_ingredient(char *dest, mc_ingredient source) {
dest = enc_varint(dest, source.count);
for(int i = 0; i < source.count; i++) {
dest = enc_slot(dest, source.items[i]);
}
return dest;
}
char *dec_ingredient(mc_ingredient *dest, char *source) {
source = dec_varint(&dest->count, source);
for(int i = 0; i < dest->count; i++) {
if(!(source = dec_slot(&dest->items[i], source)))
return NULL;
}
return source;
}
void free_ingredient(mc_ingredient ingredient) {
for(int i = 0; i < ingredient.count; i++) {
free_slot(ingredient.items[i]);
}
free(ingredient.items);
}
int walk_smelting(char *source, size_t max_len) {
int ret = walk_string(source, max_len);
if(ret < 0)
return ret;
int size = ret;
source += ret;
max_len -= ret;
if((ret = walk_ingredient(source, max_len)) < 0)
return ret;
size += ret;
source += ret;
max_len -= ret;
if((ret = walk_slot(source, max_len)) < 0)
return ret;
size += ret;
source += ret;
max_len -= ret;
if(max_len < ssizeof(mc_smelting, experience))
return -1;
size += ret;
source += ssizeof(mc_smelting, experience);
max_len -= ssizeof(mc_smelting, experience);
if((ret = walk_varint(source, max_len)) < 0)
return ret;
return size + ret;
}
size_t size_smelting(mc_smelting smelting) {
size_t total = size_string(smelting.group);
total += size_ingredient(smelting.ingredient);
total += size_slot(smelting.result);
total += sizeof(smelting.experience);
total += size_varint(smelting.cooking_time);
return total;
}
char *enc_smelting(char *dest, mc_smelting source) {
dest = enc_string(dest, source.group);
dest = enc_ingredient(dest, source.ingredient);
dest = enc_slot(dest, source.result);
dest = enc_bef32(dest, source.experience);
return enc_varint(dest, source.cooking_time);
}
char *dec_smelting(mc_smelting *dest, char *source) {
source = dec_string(&dest->group, source);
source = dec_ingredient(&dest->ingredient, source);
source = dec_slot(&dest->result, source);
source = dec_bef32(&dest->experience, source);
return dec_varint(&dest->cooking_time, source);
}
void free_smelting(mc_smelting smelting) {
free_string(smelting.group);
free_ingredient(smelting.ingredient);
free_slot(smelting.result);
}
int walk_particledata(char *source, size_t max_len, particle_type type) {
int ret = walk_varint(source, max_len);
if(ret < 0)
return -1;
int size = ret;
source += ret;
max_len -= ret;
switch(type) {
case particle_block:
case particle_falling_dust:
if((ret = walk_varint(source, max_len)) < 0)
return ret;
size += ret;
break;
case particle_dust:
if(max_len < sizeof(float) * 4)
return -1;
size += sizeof(float) * 4;
case particle_item:
if((ret = walk_slot(source, max_len)) < 0)
return ret;
size += ret;
default:
// Makes compilers happy
break;
}
return size;
}
size_t size_particledata(mc_particle particle) {
switch(particle.type) {
case particle_block:
case particle_falling_dust:
return size_varint(particle.block_state);
case particle_dust:
return sizeof(float) * 4;
case particle_item:
return size_slot(particle.item);
default:
return 0;
}
}
char *enc_particledata(char *dest, mc_particle source) {
switch(source.type) {
case particle_block:
case particle_falling_dust:
return enc_varint(dest, source.block_state);
break;
case particle_dust:
dest = enc_bef32(dest, source.red);
dest = enc_bef32(dest, source.green);
dest = enc_bef32(dest, source.blue);
return enc_bef32(dest, source.scale);
break;
case particle_item:
return enc_slot(dest, source.item);
break;
default:
return dest;
}
}
char *dec_particledata(mc_particle *dest, char *source, particle_type type) {
dest->type = type;
switch(type) {
case particle_block:
case particle_falling_dust:
return dec_varint(&dest->block_state, source);
break;
case particle_dust:
source = dec_bef32(&dest->red, source);
source = dec_bef32(&dest->green, source);
source = dec_bef32(&dest->blue, source);
return dec_bef32(&dest->scale, source);
break;
case particle_item:
return dec_slot(&dest->item, source);
break;
default:
return source;
}
}
void free_particledata(mc_particle particle) {
if(particle.type == particle_item)
free_slot(particle.item);
}
int walk_particle(char *source, size_t max_len) {
int ret = walk_varint(source, max_len);
if(ret < 0)
return ret;
int size = ret;
max_len -= ret;
int32_t type;
source = dec_varint(&type, source);
ret = walk_particledata(source, max_len, type);
if(ret < 0)
return ret;
return size + ret;
}
size_t size_particle(mc_particle particle) {
size_t size = size_varint(particle.type);
return size + size_particledata(particle);
}
char *enc_particle(char *dest, mc_particle source) {
dest = enc_varint(dest, source.type);
return enc_particledata(dest, source);
}
char *dec_particle(mc_particle *dest, char *source) {
source = dec_varint(&dest->type, source);
return dec_particledata(dest, source, dest->type);
}
// ToDo: Everything about metadata is a fucking mess and needs to be rebuilt
// from the ground up. Metadata alone makes the whole "walk/decode" paradigm
// not worth it. Serious thought should be given to a better buffer system that
// can side-step this insanity. Raw char * is just asking for pain.
int walk_metadata(char *source, size_t max_len) {
if(!max_len)
return -1;
int total = 0;
for(int size, ret; *(unsigned char *) source != 0xFF;
source += size, max_len -= size, total += size) {
total++;
if((ret = walk_varint(++source, --max_len)) < 0)
return ret;
max_len -= ret;
total += ret;
int32_t type;
source = dec_varint(&type, source);
switch(type) {
case meta_byte:
case meta_boolean:
case meta_direction:
size = sizeof(char);
break;
case meta_varint:
case meta_optblockid:
case meta_pose:
if((size = walk_varint(source, max_len)) < 0)
return size;
break;
case meta_float:
size = sizeof(float);
break;
case meta_string:
case meta_chat:
if((size = walk_string(source, max_len)) < 0)
return size;
break;
case meta_optchat:
size = sizeof(char);
if(*source) {
if((ret = walk_string(source + 1, max_len - 1)) < 0)
return ret;
size += ret;
}
break;
case meta_slot:
if((size = walk_slot(source, max_len)) < 0)
return size;
break;
case meta_rotation:
size = sizeof(float) * 3;
break;
case meta_position:
size = sizeof(uint64_t);
break;
case meta_optposition:
size = sizeof(char);
if(*source)
size += sizeof(uint64_t);
break;
case meta_optuuid:
size = sizeof(char);
if(*source)
size += sizeof(uint64_t) * 2;
break;
case meta_nbt:
if((size = walk_nbt(source, max_len)) < 0)
return size;
break;
case meta_particle:
if((size = walk_particle(source, max_len)) < 0)
return size;
break;
case meta_villagerdata:
size = 0;
for(size_t j = 0; j < 3; j++) {
if((ret = walk_varint(source, max_len)) < 0)
return ret;
source += ret;
max_len -= ret;
total += ret;
}
break;
case meta_optvarint:
size = sizeof(char);
if(*source) {
if((ret = walk_varint(source + 1, max_len - 1)) < 0)
return ret;
size += ret;
}
break;
default:
return -1;
break;
}
// Need to read at least one more byte to get the meta close tag 0xFF
// Therefore we use <= here instead of just <
if(max_len <= (size_t) size)
return -1;
}
return ++total;
}
// TODO: counting metatags takes almost as long as decoding them
// we could write a fast_decode that just groups those two
size_t count_metatags(char *source) {
size_t count = 0;
for(int32_t type; *(unsigned char *) source != 0xFF; count++) {
source = dec_varint(&type, ++source);
switch(type) {
case meta_byte:
case meta_boolean:
case meta_direction:
source += sizeof(char);
break;
case meta_varint:
case meta_optblockid:
case meta_pose:
source += walk_varint(source, NO_OVERFLOW);
break;
case meta_float:
source += sizeof(float);
break;
case meta_string:
case meta_chat:
source += walk_string(source, NO_OVERFLOW);
break;
case meta_optchat:
if(*source++)
source += walk_string(source, NO_OVERFLOW);
break;
case meta_slot:
source += walk_slot(source, NO_OVERFLOW);
break;
case meta_rotation:
source += sizeof(float) * 3;
break;
case meta_position:
source += sizeof(uint64_t);
break;
case meta_optposition:
if(*source++)
source += sizeof(uint64_t);
break;
case meta_optuuid:
if(*source++)
source += sizeof(uint64_t) * 2;
break;
case meta_nbt:
source += walk_nbt(source, NO_OVERFLOW);
break;
case meta_particle:
source += walk_particle(source, NO_OVERFLOW);
break;
case meta_villagerdata:
for(size_t j = 0; j < 3; j++)
source += walk_varint(source, NO_OVERFLOW);
break;
case meta_optvarint:
if(*source++)
source += walk_varint(source, NO_OVERFLOW);