-
Notifications
You must be signed in to change notification settings - Fork 253
/
sproto.c
1437 lines (1360 loc) · 29.3 KB
/
sproto.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "msvcint.h"
#include "sproto.h"
#define CHUNK_SIZE 1000
#define SIZEOF_LENGTH 4
#define SIZEOF_HEADER 2
#define SIZEOF_FIELD 2
#define SIZEOF_INT64 ((int)sizeof(uint64_t))
#define SIZEOF_INT32 ((int)sizeof(uint32_t))
struct field {
int tag;
int type;
const char * name;
struct sproto_type * st;
int key;
int map; // interpreted two fields struct as map
int extra;
};
struct sproto_type {
const char * name;
int n;
int base;
int maxn;
struct field *f;
};
struct protocol {
const char *name;
int tag;
int confirm; // confirm == 1 where response nil
struct sproto_type * p[2];
};
struct chunk {
struct chunk * next;
};
struct pool {
struct chunk * header;
struct chunk * current;
int current_used;
};
struct sproto {
struct pool memory;
int type_n;
int protocol_n;
struct sproto_type * type;
struct protocol * proto;
};
static void
pool_init(struct pool *p) {
p->header = NULL;
p->current = NULL;
p->current_used = 0;
}
static void
pool_release(struct pool *p) {
struct chunk * tmp = p->header;
while (tmp) {
struct chunk * n = tmp->next;
free(tmp);
tmp = n;
}
}
static void *
pool_newchunk(struct pool *p, size_t sz) {
struct chunk * t = malloc(sz + sizeof(struct chunk));
if (t == NULL)
return NULL;
t->next = p->header;
p->header = t;
return t+1;
}
static void *
pool_alloc(struct pool *p, size_t sz) {
// align by 8
sz = (sz + 7) & ~7;
if (sz >= CHUNK_SIZE) {
return pool_newchunk(p, sz);
}
if (p->current == NULL) {
if (pool_newchunk(p, CHUNK_SIZE) == NULL)
return NULL;
p->current = p->header;
}
if (sz + p->current_used <= CHUNK_SIZE) {
void * ret = (char *)(p->current+1) + p->current_used;
p->current_used += sz;
return ret;
}
if (sz >= p->current_used) {
return pool_newchunk(p, sz);
} else {
void * ret = pool_newchunk(p, CHUNK_SIZE);
p->current = p->header;
p->current_used = sz;
return ret;
}
}
static inline int
toword(const uint8_t * p) {
return p[0] | p[1]<<8;
}
static inline uint32_t
todword(const uint8_t *p) {
return p[0] | p[1]<<8 | p[2]<<16 | p[3]<<24;
}
static int
count_array(const uint8_t * stream) {
uint32_t length = todword(stream);
int n = 0;
stream += SIZEOF_LENGTH;
while (length > 0) {
uint32_t nsz;
if (length < SIZEOF_LENGTH)
return -1;
nsz = todword(stream);
nsz += SIZEOF_LENGTH;
if (nsz > length)
return -1;
++n;
stream += nsz;
length -= nsz;
}
return n;
}
static int
struct_field(const uint8_t * stream, size_t sz) {
const uint8_t * field;
int fn, header, i;
if (sz < SIZEOF_LENGTH)
return -1;
fn = toword(stream);
header = SIZEOF_HEADER + SIZEOF_FIELD * fn;
if (sz < header)
return -1;
field = stream + SIZEOF_HEADER;
sz -= header;
stream += header;
for (i=0;i<fn;i++) {
int value= toword(field + i * SIZEOF_FIELD);
uint32_t dsz;
if (value != 0)
continue;
if (sz < SIZEOF_LENGTH)
return -1;
dsz = todword(stream);
if (sz < SIZEOF_LENGTH + dsz)
return -1;
stream += SIZEOF_LENGTH + dsz;
sz -= SIZEOF_LENGTH + dsz;
}
return fn;
}
static const char *
import_string(struct sproto *s, const uint8_t * stream) {
uint32_t sz = todword(stream);
char * buffer = pool_alloc(&s->memory, sz+1);
memcpy(buffer, stream+SIZEOF_LENGTH, sz);
buffer[sz] = '\0';
return buffer;
}
static int
calc_pow(int base, int n) {
int r;
if (n == 0)
return 1;
r = calc_pow(base * base , n / 2);
if (n&1) {
r *= base;
}
return r;
}
static const uint8_t *
import_field(struct sproto *s, struct field *f, const uint8_t * stream) {
uint32_t sz;
const uint8_t * result;
int fn;
int i;
int array = 0;
int tag = -1;
f->tag = -1;
f->type = -1;
f->name = NULL;
f->st = NULL;
f->key = -1;
f->map = -1;
f->extra = 0;
sz = todword(stream);
stream += SIZEOF_LENGTH;
result = stream + sz;
fn = struct_field(stream, sz);
if (fn < 0)
return NULL;
stream += SIZEOF_HEADER;
for (i=0;i<fn;i++) {
int value;
++tag;
value = toword(stream + SIZEOF_FIELD * i);
if (value & 1) {
tag+= value/2;
continue;
}
if (tag == 0) { // name
if (value != 0)
return NULL;
f->name = import_string(s, stream + fn * SIZEOF_FIELD);
continue;
}
if (value == 0)
return NULL;
value = value/2 - 1;
switch(tag) {
case 1: // buildin
if (value >= SPROTO_TSTRUCT)
return NULL; // invalid buildin type
f->type = value;
break;
case 2: // type index
if (f->type == SPROTO_TINTEGER) {
f->extra = calc_pow(10, value);
} else if (f->type == SPROTO_TSTRING) {
f->extra = value; // string if 0 ; binary is 1
} else {
if (value >= s->type_n)
return NULL; // invalid type index
if (f->type >= 0)
return NULL;
f->type = SPROTO_TSTRUCT;
f->st = &s->type[value];
}
break;
case 3: // tag
f->tag = value;
break;
case 4: // array
if (value)
array = SPROTO_TARRAY;
break;
case 5: // key
f->key = value;
break;
case 6: // map
if (value)
f->map = 1;
break;
default:
return NULL;
}
}
if (f->tag < 0 || f->type < 0 || f->name == NULL)
return NULL;
f->type |= array;
return result;
}
/*
.type {
.field {
name 0 : string
buildin 1 : integer
type 2 : integer
tag 3 : integer
array 4 : boolean
key 5 : integer
map 6 : boolean // Interpreted two fields struct as map when decoding
}
name 0 : string
fields 1 : *field
}
*/
static const uint8_t *
import_type(struct sproto *s, struct sproto_type *t, const uint8_t * stream) {
const uint8_t * result;
uint32_t sz = todword(stream);
int i;
int fn;
int n;
int maxn;
int last;
stream += SIZEOF_LENGTH;
result = stream + sz;
fn = struct_field(stream, sz);
if (fn <= 0 || fn > 2)
return NULL;
for (i=0;i<fn*SIZEOF_FIELD;i+=SIZEOF_FIELD) {
// name and fields must encode to 0
int v = toword(stream + SIZEOF_HEADER + i);
if (v != 0)
return NULL;
}
memset(t, 0, sizeof(*t));
stream += SIZEOF_HEADER + fn * SIZEOF_FIELD;
t->name = import_string(s, stream);
if (fn == 1) {
return result;
}
stream += todword(stream)+SIZEOF_LENGTH; // second data
n = count_array(stream);
if (n<0)
return NULL;
stream += SIZEOF_LENGTH;
maxn = n;
last = -1;
t->n = n;
t->f = pool_alloc(&s->memory, sizeof(struct field) * n);
for (i=0;i<n;i++) {
int tag;
struct field *f = &t->f[i];
stream = import_field(s, f, stream);
if (stream == NULL)
return NULL;
tag = f->tag;
if (tag <= last)
return NULL; // tag must in ascending order
if (tag > last+1) {
++maxn;
}
last = tag;
}
t->maxn = maxn;
t->base = t->f[0].tag;
n = t->f[n-1].tag - t->base + 1;
if (n != t->n) {
t->base = -1;
}
return result;
}
/*
.protocol {
name 0 : string
tag 1 : integer
request 2 : integer
response 3 : integer
}
*/
static const uint8_t *
import_protocol(struct sproto *s, struct protocol *p, const uint8_t * stream) {
const uint8_t * result;
uint32_t sz = todword(stream);
int fn;
int i;
int tag;
stream += SIZEOF_LENGTH;
result = stream + sz;
fn = struct_field(stream, sz);
stream += SIZEOF_HEADER;
p->name = NULL;
p->tag = -1;
p->p[SPROTO_REQUEST] = NULL;
p->p[SPROTO_RESPONSE] = NULL;
p->confirm = 0;
tag = 0;
for (i=0;i<fn;i++,tag++) {
int value = toword(stream + SIZEOF_FIELD * i);
if (value & 1) {
tag += (value-1)/2;
continue;
}
value = value/2 - 1;
switch (i) {
case 0: // name
if (value != -1) {
return NULL;
}
p->name = import_string(s, stream + SIZEOF_FIELD *fn);
break;
case 1: // tag
if (value < 0) {
return NULL;
}
p->tag = value;
break;
case 2: // request
if (value < 0 || value>=s->type_n)
return NULL;
p->p[SPROTO_REQUEST] = &s->type[value];
break;
case 3: // response
if (value < 0 || value>=s->type_n)
return NULL;
p->p[SPROTO_RESPONSE] = &s->type[value];
break;
case 4: // confirm
p->confirm = value;
break;
default:
return NULL;
}
}
if (p->name == NULL || p->tag<0) {
return NULL;
}
return result;
}
static struct sproto *
create_from_bundle(struct sproto *s, const uint8_t * stream, size_t sz) {
const uint8_t * content;
const uint8_t * typedata = NULL;
const uint8_t * protocoldata = NULL;
int fn = struct_field(stream, sz);
int i;
if (fn < 0 || fn > 2)
return NULL;
stream += SIZEOF_HEADER;
content = stream + fn*SIZEOF_FIELD;
for (i=0;i<fn;i++) {
int value = toword(stream + i*SIZEOF_FIELD);
int n;
if (value != 0)
return NULL;
n = count_array(content);
if (n<0)
return NULL;
if (i == 0) {
typedata = content+SIZEOF_LENGTH;
s->type_n = n;
s->type = pool_alloc(&s->memory, n * sizeof(*s->type));
} else {
protocoldata = content+SIZEOF_LENGTH;
s->protocol_n = n;
s->proto = pool_alloc(&s->memory, n * sizeof(*s->proto));
}
content += todword(content) + SIZEOF_LENGTH;
}
for (i=0;i<s->type_n;i++) {
typedata = import_type(s, &s->type[i], typedata);
if (typedata == NULL) {
return NULL;
}
}
for (i=0;i<s->protocol_n;i++) {
protocoldata = import_protocol(s, &s->proto[i], protocoldata);
if (protocoldata == NULL) {
return NULL;
}
}
return s;
}
struct sproto *
sproto_create(const void * proto, size_t sz) {
struct pool mem;
struct sproto * s;
pool_init(&mem);
s = pool_alloc(&mem, sizeof(*s));
if (s == NULL)
return NULL;
memset(s, 0, sizeof(*s));
s->memory = mem;
if (create_from_bundle(s, proto, sz) == NULL) {
pool_release(&s->memory);
return NULL;
}
return s;
}
void
sproto_release(struct sproto * s) {
if (s == NULL)
return;
pool_release(&s->memory);
}
static const char *
get_typename(int type, struct field *f) {
if (type == SPROTO_TSTRUCT) {
return f->st->name;
} else {
switch (type) {
case SPROTO_TINTEGER:
if (f->extra)
return "decimal";
else
return "integer";
case SPROTO_TBOOLEAN:
return "boolean";
case SPROTO_TSTRING:
if (f->extra == SPROTO_TSTRING_BINARY)
return "binary";
else
return "string";
case SPROTO_TDOUBLE:
return "double";
default:
return "invalid";
}
}
}
void
sproto_dump(struct sproto *s) {
int i,j;
printf("=== %d types ===\n", s->type_n);
for (i=0;i<s->type_n;i++) {
struct sproto_type *t = &s->type[i];
printf("%s\n", t->name);
for (j=0;j<t->n;j++) {
char container[2] = { 0, 0 };
const char * typename = NULL;
struct field *f = &t->f[j];
int type = f->type & ~SPROTO_TARRAY;
if (f->type & SPROTO_TARRAY) {
container[0] = '*';
} else {
container[0] = 0;
}
typename = get_typename(type, f);
printf("\t%s (%d) %s%s", f->name, f->tag, container, typename);
if (type == SPROTO_TINTEGER && f->extra > 0) {
printf("(%d)", f->extra);
}
if (f->key >= 0) {
printf(" key[%d]", f->key);
if (f->map >= 0) {
printf(" value[%d]", f->st->f[1].tag);
}
}
printf("\n");
}
}
printf("=== %d protocol ===\n", s->protocol_n);
for (i=0;i<s->protocol_n;i++) {
struct protocol *p = &s->proto[i];
if (p->p[SPROTO_REQUEST]) {
printf("\t%s (%d) request:%s", p->name, p->tag, p->p[SPROTO_REQUEST]->name);
} else {
printf("\t%s (%d) request:(null)", p->name, p->tag);
}
if (p->p[SPROTO_RESPONSE]) {
printf(" response:%s", p->p[SPROTO_RESPONSE]->name);
} else if (p->confirm) {
printf(" response nil");
}
printf("\n");
}
}
// query
int
sproto_prototag(const struct sproto *sp, const char * name) {
int i;
for (i=0;i<sp->protocol_n;i++) {
if (strcmp(name, sp->proto[i].name) == 0) {
return sp->proto[i].tag;
}
}
return -1;
}
static struct protocol *
query_proto(const struct sproto *sp, int tag) {
int begin = 0, end = sp->protocol_n;
while(begin<end) {
int mid = (begin+end)/2;
int t = sp->proto[mid].tag;
if (t==tag) {
return &sp->proto[mid];
}
if (tag > t) {
begin = mid+1;
} else {
end = mid;
}
}
return NULL;
}
struct sproto_type *
sproto_protoquery(const struct sproto *sp, int proto, int what) {
struct protocol * p;
if (what <0 || what >1) {
return NULL;
}
p = query_proto(sp, proto);
if (p) {
return p->p[what];
}
return NULL;
}
int
sproto_protoresponse(const struct sproto * sp, int proto) {
struct protocol * p = query_proto(sp, proto);
return (p!=NULL && (p->p[SPROTO_RESPONSE] || p->confirm));
}
const char *
sproto_protoname(const struct sproto *sp, int proto) {
struct protocol * p = query_proto(sp, proto);
if (p) {
return p->name;
}
return NULL;
}
struct sproto_type *
sproto_type(const struct sproto *sp, const char * type_name) {
int i;
for (i=0;i<sp->type_n;i++) {
if (strcmp(type_name, sp->type[i].name) == 0) {
return &sp->type[i];
}
}
return NULL;
}
const char *
sproto_name(struct sproto_type * st) {
return st->name;
}
static struct field *
findtag(const struct sproto_type *st, int tag) {
int begin, end;
if (st->base >=0 ) {
tag -= st->base;
if (tag < 0 || tag >= st->n)
return NULL;
return &st->f[tag];
}
begin = 0;
end = st->n;
while (begin < end) {
int mid = (begin+end)/2;
struct field *f = &st->f[mid];
int t = f->tag;
if (t == tag) {
return f;
}
if (tag > t) {
begin = mid + 1;
} else {
end = mid;
}
}
return NULL;
}
// encode & decode
// sproto_callback(void *ud, int tag, int type, struct sproto_type *, void *value, int length)
// return size, -1 means error
static inline int
fill_size(uint8_t * data, int sz) {
data[0] = sz & 0xff;
data[1] = (sz >> 8) & 0xff;
data[2] = (sz >> 16) & 0xff;
data[3] = (sz >> 24) & 0xff;
return sz + SIZEOF_LENGTH;
}
static int
encode_integer(uint32_t v, uint8_t * data, int size) {
if (size < SIZEOF_LENGTH + sizeof(v))
return -1;
data[4] = v & 0xff;
data[5] = (v >> 8) & 0xff;
data[6] = (v >> 16) & 0xff;
data[7] = (v >> 24) & 0xff;
return fill_size(data, sizeof(v));
}
static int
encode_uint64(uint64_t v, uint8_t * data, int size) {
if (size < SIZEOF_LENGTH + sizeof(v))
return -1;
data[4] = v & 0xff;
data[5] = (v >> 8) & 0xff;
data[6] = (v >> 16) & 0xff;
data[7] = (v >> 24) & 0xff;
data[8] = (v >> 32) & 0xff;
data[9] = (v >> 40) & 0xff;
data[10] = (v >> 48) & 0xff;
data[11] = (v >> 56) & 0xff;
return fill_size(data, sizeof(v));
}
/*
//#define CB(tagname,type,index,subtype,value,length) cb(ud, tagname,type,index,subtype,value,length)
static int
do_cb(sproto_callback cb, void *ud, const char *tagname, int type, int index, struct sproto_type *subtype, void *value, int length) {
if (subtype) {
if (type >= 0) {
printf("callback: tag=%s[%d], subtype[%s]:%d\n",tagname,index, subtype->name, type);
} else {
printf("callback: tag=%s[%d], subtype[%s]\n",tagname,index, subtype->name);
}
} else if (index > 0) {
printf("callback: tag=%s[%d]\n",tagname,index);
} else if (index == 0) {
printf("callback: tag=%s\n",tagname);
} else {
printf("callback: tag=%s [mainkey]\n",tagname);
}
return cb(ud, tagname,type,index,subtype,value,length);
}
#define CB(tagname,type,index,subtype,value,length) do_cb(cb,ud, tagname,type,index,subtype,value,length)
*/
static int
encode_object(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int size) {
int sz;
if (size < SIZEOF_LENGTH)
return -1;
args->value = data+SIZEOF_LENGTH;
args->length = size-SIZEOF_LENGTH;
sz = cb(args);
if (sz < 0) {
if (sz == SPROTO_CB_NIL)
return 0;
return -1; // sz == SPROTO_CB_ERROR
}
assert(sz <= size-SIZEOF_LENGTH); // verify buffer overflow
return fill_size(data, sz);
}
static inline void
uint32_to_uint64(int negative, uint8_t *buffer) {
if (negative) {
buffer[4] = 0xff;
buffer[5] = 0xff;
buffer[6] = 0xff;
buffer[7] = 0xff;
} else {
buffer[4] = 0;
buffer[5] = 0;
buffer[6] = 0;
buffer[7] = 0;
}
}
static uint8_t *
encode_integer_array(sproto_callback cb, struct sproto_arg *args, uint8_t *buffer, int size, int *noarray) {
uint8_t * header = buffer;
int intlen;
int index;
if (size < 1)
return NULL;
buffer++;
size--;
intlen = SIZEOF_INT32;
index = 1;
*noarray = 0;
for (;;) {
int sz;
union {
uint64_t u64;
uint32_t u32;
} u;
args->value = &u;
args->length = sizeof(u);
args->index = index;
sz = cb(args);
if (sz <= 0) {
if (sz == SPROTO_CB_NIL) // nil object, end of array
break;
if (sz == SPROTO_CB_NOARRAY) { // no array, don't encode it
*noarray = 1;
break;
}
return NULL; // sz == SPROTO_CB_ERROR
}
// notice: sizeof(uint64_t) is size_t (unsigned) , size may be negative. See issue #75
// so use MACRO SIZOF_INT64 instead
if (size < SIZEOF_INT64)
return NULL;
if (sz == SIZEOF_INT32) {
uint32_t v = u.u32;
buffer[0] = v & 0xff;
buffer[1] = (v >> 8) & 0xff;
buffer[2] = (v >> 16) & 0xff;
buffer[3] = (v >> 24) & 0xff;
if (intlen == SIZEOF_INT64) {
uint32_to_uint64(v & 0x80000000, buffer);
}
} else {
uint64_t v;
if (sz != SIZEOF_INT64)
return NULL;
if (intlen == SIZEOF_INT32) {
int i;
// rearrange
size -= (index-1) * SIZEOF_INT32;
if (size < SIZEOF_INT64)
return NULL;
buffer += (index-1) * SIZEOF_INT32;
for (i=index-2;i>=0;i--) {
int negative;
memcpy(header+1+i*SIZEOF_INT64, header+1+i*SIZEOF_INT32, SIZEOF_INT32);
negative = header[1+i*SIZEOF_INT64+3] & 0x80;
uint32_to_uint64(negative, header+1+i*SIZEOF_INT64);
}
intlen = SIZEOF_INT64;
}
v = u.u64;
buffer[0] = v & 0xff;
buffer[1] = (v >> 8) & 0xff;
buffer[2] = (v >> 16) & 0xff;
buffer[3] = (v >> 24) & 0xff;
buffer[4] = (v >> 32) & 0xff;
buffer[5] = (v >> 40) & 0xff;
buffer[6] = (v >> 48) & 0xff;
buffer[7] = (v >> 56) & 0xff;
}
size -= intlen;
buffer += intlen;
index++;
}
if (buffer == header + 1) {
return header;
}
*header = (uint8_t)intlen;
return buffer;
}
static uint8_t *
encode_array_object(sproto_callback cb, struct sproto_arg *args, uint8_t *buffer, int size, int *noarray) {
int sz;
*noarray = 0;
args->index = 1;
for (;;) {
if (size < SIZEOF_LENGTH)
return NULL;
size -= SIZEOF_LENGTH;
args->value = buffer + SIZEOF_LENGTH;
args->length = size;
sz = cb(args);
if (sz < 0) {
if (sz == SPROTO_CB_NIL) {
break;
}
if (sz == SPROTO_CB_NOARRAY) { // no array, don't encode it
*noarray = 1;
break;
}
return NULL; // sz == SPROTO_CB_ERROR
}
fill_size(buffer, sz);
buffer += SIZEOF_LENGTH+sz;
size -= sz;
++args->index;
}
return buffer;
}
static int
encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int size) {
uint8_t * buffer;
int sz;
if (size < SIZEOF_LENGTH)
return -1;
size -= SIZEOF_LENGTH;
buffer = data + SIZEOF_LENGTH;
switch (args->type) {
case SPROTO_TDOUBLE:
case SPROTO_TINTEGER: {
int noarray;
buffer = encode_integer_array(cb,args,buffer,size, &noarray);
if (buffer == NULL)
return -1;
if (noarray) {
return 0;
}
break;
}
case SPROTO_TBOOLEAN:
args->index = 1;
for (;;) {
int v = 0;
args->value = &v;
args->length = sizeof(v);
sz = cb(args);
if (sz < 0) {
if (sz == SPROTO_CB_NIL) // nil object , end of array
break;
if (sz == SPROTO_CB_NOARRAY) // no array, don't encode it
return 0;
return -1; // sz == SPROTO_CB_ERROR
}
if (size < 1)
return -1;
buffer[0] = v ? 1: 0;
size -= 1;
buffer += 1;
++args->index;
}
break;
default: {
int noarray;
buffer = encode_array_object(cb, args, buffer, size, &noarray);
if (buffer == NULL)
return -1;
if (noarray)
return 0;
break;
}
}
sz = buffer - (data + SIZEOF_LENGTH);
return fill_size(data, sz);
}
int
sproto_encode(const struct sproto_type *st, void * buffer, int size, sproto_callback cb, void *ud) {
struct sproto_arg args;
uint8_t * header = buffer;
uint8_t * data;
int header_sz = SIZEOF_HEADER + st->maxn * SIZEOF_FIELD;
int i;
int index;
int lasttag;
int datasz;
if (size < header_sz)
return -1;
args.ud = ud;
data = header + header_sz;
size -= header_sz;
index = 0;
lasttag = -1;
for (i=0;i<st->n;i++) {
struct field *f = &st->f[i];
int type = f->type;
int value = 0;
int sz = -1;
args.tagname = f->name;
args.tagid = f->tag;
args.subtype = f->st;
args.mainindex = f->key;
args.extra = f->extra;
args.ktagname = NULL;
args.vtagname = NULL;
if (type & SPROTO_TARRAY) {
args.type = type & (~SPROTO_TARRAY);
if (f->map > 0) {
args.ktagname = f->st->f[0].name;
args.vtagname = f->st->f[1].name;
}
sz = encode_array(cb, &args, data, size);
} else {
args.type = type;
args.index = 0;
switch(type) {
case SPROTO_TDOUBLE:
case SPROTO_TINTEGER:
case SPROTO_TBOOLEAN: {
union {
uint64_t u64;
uint32_t u32;
} u;
args.value = &u;
args.length = sizeof(u);
sz = cb(&args);
if (sz < 0) {
if (sz == SPROTO_CB_NIL)
continue;
if (sz == SPROTO_CB_NOARRAY) // no array, don't encode it
return 0;
return -1; // sz == SPROTO_CB_ERROR
}
if (sz == SIZEOF_INT32) {