forked from starwing/lua-protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pb.h
1784 lines (1545 loc) · 54.1 KB
/
pb.h
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
#ifndef pb_h
#define pb_h
#ifndef PB_NS_BEGIN
# ifdef __cplusplus
# define PB_NS_BEGIN extern "C" {
# define PB_NS_END }
# else
# define PB_NS_BEGIN
# define PB_NS_END
# endif
#endif /* PB_NS_BEGIN */
#ifndef PB_STATIC
# if __GNUC__
# define PB_STATIC static __attribute((unused))
# else
# define PB_STATIC static
# endif
#endif
/* Compile with -DBP_IO=0 to exclude IO lib */
#ifndef BP_IO
#define BP_IO 1
#endif
/* Compile with -DUSE_DLMALLOC if using Doug Lea's memory allocator */
#ifdef USE_DLMALLOC
#define PB_REALLOC dlrealloc
#define PB_MALLOC dlmalloc
#define PB_FREE dlfree
#endif
/* Fine tune what allocator functions to use */
#ifndef PB_REALLOC
#define PB_REALLOC realloc
#endif
#ifndef PB_MALLOC
#define PB_MALLOC malloc
#endif
#ifndef PB_FREE
#define PB_FREE free
#endif
#ifdef PB_STATIC_API
# ifndef PB_IMPLEMENTATION
# define PB_IMPLEMENTATION
# endif
# define PB_API PB_STATIC
#endif
#if !defined(PB_API) && defined(_WIN32)
# ifdef PB_IMPLEMENTATION
# define PB_API __declspec(dllexport)
# else
# define PB_API __declspec(dllimport)
# endif
#endif
#ifndef PB_API
# define PB_API extern
#endif
#if defined(_MSC_VER) || defined(__UNIXOS2__) || defined(__SOL64__)
typedef unsigned char uint8_t;
typedef signed char int8_t;
typedef unsigned short uint16_t;
typedef signed short int16_t;
typedef unsigned int uint32_t;
typedef signed int int32_t;
typedef unsigned long long uint64_t;
typedef signed long long int64_t;
#ifndef INT64_MIN
# define INT64_MIN LLONG_MIN
#endif
#ifndef INT64_MAX
# define INT64_MAX LLONG_MAX
#endif
#elif defined(__SCO__) || defined(__USLC__) || defined(__MINGW32__)
# include <stdint.h>
#else
# include <inttypes.h>
# if (defined(__sun__) || defined(__digital__))
# if defined(__STDC__) && (defined(__arch64__) || defined(_LP64))
typedef unsigned long int uint64_t;
typedef signed long int int64_t;
# else
typedef unsigned long long uint64_t;
typedef signed long long int64_t;
# endif /* LP64 */
# endif /* __sun__ || __digital__ */
#endif
#include <stddef.h>
#include <limits.h>
#include <lua.h>
PB_NS_BEGIN
/* types */
#define PB_WIRETYPES(X) /* X(id, name, fmt) */\
X(VARINT, "varint", 'v') X(64BIT, "64bit", 'q') X(BYTES, "bytes", 's') \
X(GSTART, "gstart", '!') X(GEND, "gend", '!') X(32BIT, "32bit", 'd') \
#define PB_TYPES(X) /* X(name, type, fmt) */\
X(double, double, 'F') X(float, float, 'f') \
X(int64, int64_t, 'I') X(uint64, uint64_t, 'U') \
X(int32, int32_t, 'i') X(fixed64, uint64_t, 'X') \
X(fixed32, uint32_t, 'x') X(bool, int, 'b') \
X(string, pb_Slice, 't') X(group, pb_Slice, 'g') \
X(message, pb_Slice, 'S') X(bytes, pb_Slice, 's') \
X(uint32, uint32_t, 'u') X(enum, int32_t, 'v') \
X(sfixed32, int32_t, 'y') X(sfixed64, int64_t, 'Y') \
X(sint32, int32_t, 'j') X(sint64, int64_t, 'J') \
typedef enum pb_WireType {
#define X(id,name,fmt) PB_T##id,
PB_WIRETYPES(X)
#undef X
PB_TWIRECOUNT
} pb_WireType;
typedef enum pb_FieldType {
PB_TNONE,
#define X(name,type,fmt) PB_T##name,
PB_TYPES(X)
#undef X
PB_TYPECOUNT
} pb_FieldType;
/* conversions */
PB_API uint64_t pb_expandsig (uint32_t v);
PB_API uint32_t pb_encode_sint32 ( int32_t v);
PB_API int32_t pb_decode_sint32 (uint32_t v);
PB_API uint64_t pb_encode_sint64 ( int64_t v);
PB_API int64_t pb_decode_sint64 (uint64_t v);
PB_API uint32_t pb_encode_float (float v);
PB_API float pb_decode_float (uint32_t v);
PB_API uint64_t pb_encode_double (double v);
PB_API double pb_decode_double (uint64_t v);
/* decode */
typedef struct pb_Slice { const char *p, *start, *end; } pb_Slice;
#define pb_gettype(v) ((v) & 7)
#define pb_gettag(v) ((v) >> 3)
#define pb_pair(tag,type) ((tag) << 3 | ((type) & 7))
PB_API pb_Slice pb_slice (const char *p);
PB_API pb_Slice pb_lslice (const char *p, size_t len);
PB_API size_t pb_pos (const pb_Slice s);
PB_API size_t pb_len (const pb_Slice s);
PB_API size_t pb_readvarint32 (pb_Slice *s, uint32_t *pv);
PB_API size_t pb_readvarint64 (pb_Slice *s, uint64_t *pv);
PB_API size_t pb_readfixed32 (pb_Slice *s, uint32_t *pv);
PB_API size_t pb_readfixed64 (pb_Slice *s, uint64_t *pv);
PB_API size_t pb_readslice (pb_Slice *s, size_t len, pb_Slice *pv);
PB_API size_t pb_readbytes (pb_Slice *s, pb_Slice *pv);
PB_API size_t pb_readgroup (pb_Slice *s, uint32_t tag, pb_Slice *pv);
PB_API size_t pb_skipvarint (pb_Slice *s);
PB_API size_t pb_skipbytes (pb_Slice *s);
PB_API size_t pb_skipslice (pb_Slice *s, size_t len);
PB_API size_t pb_skipvalue (pb_Slice *s, uint32_t tag);
PB_API const char *pb_wtypename (int wiretype, const char *def);
PB_API const char *pb_typename (int type, const char *def);
PB_API int pb_typebyname (const char *name, int def);
PB_API int pb_wtypebyname (const char *name, int def);
PB_API int pb_wtypebytype (int type);
/* encode */
#define PB_SSO_SIZE (sizeof(pb_HeapBuffer))
typedef struct pb_HeapBuffer {
unsigned capacity;
char *buff;
} pb_HeapBuffer;
typedef struct pb_Buffer {
unsigned size : sizeof(unsigned)*CHAR_BIT - 1;
unsigned heap : 1;
union {
char buff[PB_SSO_SIZE];
pb_HeapBuffer h;
} u;
} pb_Buffer;
#define pb_onheap(b) ((b)->heap)
#define pb_bufflen(b) ((b)->size)
#define pb_buffer(b) (pb_onheap(b) ? (b)->u.h.buff : (b)->u.buff)
#define pb_addsize(b,sz) ((void)((b)->size += (unsigned)(sz)))
PB_API void pb_initbuffer (pb_Buffer *b);
PB_API void pb_resetbuffer (pb_Buffer *b);
PB_API char *pb_prepbuffsize (pb_Buffer *b, size_t len);
PB_API pb_Slice pb_result (const pb_Buffer *b);
PB_API size_t pb_addvarint32 (pb_Buffer *b, uint32_t v);
PB_API size_t pb_addvarint64 (pb_Buffer *b, uint64_t v);
PB_API size_t pb_addfixed32 (pb_Buffer *b, uint32_t v);
PB_API size_t pb_addfixed64 (pb_Buffer *b, uint64_t v);
PB_API size_t pb_addslice (pb_Buffer *b, pb_Slice s);
PB_API size_t pb_addbytes (pb_Buffer *b, pb_Slice s);
PB_API size_t pb_addlength (pb_Buffer *b, size_t len, size_t prealloc);
/* type info database state and name table */
typedef struct pb_State pb_State;
typedef struct pb_Name pb_Name;
typedef struct pb_Cache pb_Cache;
PB_API void pb_init (pb_State *S);
PB_API void pb_free (pb_State *S);
PB_API pb_Name *pb_newname (pb_State *S, pb_Slice s, pb_Cache *cache);
PB_API void pb_delname (pb_State *S, pb_Name *name);
PB_API pb_Name *pb_usename (pb_Name *name);
PB_API const pb_Name *pb_name (const pb_State *S, pb_Slice s, pb_Cache *cache);
/* type info */
typedef struct pb_Type pb_Type;
typedef struct pb_Field pb_Field;
#define PB_OK 0
#define PB_ERROR 1
#define PB_ENOMEM 2
PB_API int pb_load (pb_State *S, pb_Slice *s);
PB_API pb_Type *pb_newtype (pb_State *S, pb_Name *tname);
PB_API void pb_deltype (pb_State *S, pb_Type *t);
PB_API pb_Field *pb_newfield (pb_State *S, pb_Type *t, pb_Name *fname, int32_t number);
PB_API void pb_delfield (pb_State *S, pb_Type *t, pb_Field *f);
PB_API const pb_Type *pb_type (const pb_State *S, const pb_Name *tname);
PB_API const pb_Field *pb_fname (const pb_Type *t, const pb_Name *tname);
PB_API const pb_Field *pb_field (const pb_Type *t, int32_t number);
PB_API const pb_Name *pb_oneofname (const pb_Type *t, int oneof_index);
PB_API int pb_nexttype (const pb_State *S, const pb_Type **ptype);
PB_API int pb_nextfield (const pb_Type *t, const pb_Field **pfield);
/* util: memory pool */
#define PB_POOLSIZE 4096
typedef struct pb_Pool {
void *pages;
void *freed;
size_t obj_size;
} pb_Pool;
PB_API void pb_initpool (pb_Pool *pool, size_t obj_size);
PB_API void pb_freepool (pb_Pool *pool);
PB_API void *pb_poolalloc (pb_Pool *pool);
PB_API void pb_poolfree (pb_Pool *pool, void *obj);
/* util: hash table */
typedef struct pb_Table pb_Table;
typedef struct pb_Entry pb_Entry;
typedef ptrdiff_t pb_Key;
PB_API void pb_inittable (pb_Table *t, size_t entrysize);
PB_API void pb_freetable (pb_Table *t);
PB_API size_t pb_resizetable (pb_Table *t, size_t size);
PB_API pb_Entry *pb_gettable (const pb_Table *t, pb_Key key);
PB_API pb_Entry *pb_settable (pb_Table *t, pb_Key key);
PB_API int pb_nextentry (const pb_Table *t, const pb_Entry **pentry);
struct pb_Table {
unsigned size;
unsigned lastfree;
unsigned entry_size : sizeof(unsigned)*CHAR_BIT - 1;
unsigned has_zero : 1;
pb_Entry *hash;
};
struct pb_Entry {
ptrdiff_t next;
pb_Key key;
};
/* fields */
#define PB_CACHE_SIZE (53)
typedef struct pb_NameEntry {
struct pb_NameEntry *next;
unsigned hash : 32;
unsigned length : 16;
unsigned refcount : 16;
} pb_NameEntry;
typedef struct pb_NameTable {
size_t size;
size_t count;
pb_NameEntry **hash;
} pb_NameTable;
typedef struct pb_CacheSlot {
const char *name;
unsigned hash;
} pb_CacheSlot;
struct pb_Cache {
pb_CacheSlot slots[PB_CACHE_SIZE][2];
unsigned hash;
};
struct pb_State {
pb_NameTable nametable;
pb_Table types;
pb_Pool typepool;
pb_Pool fieldpool;
};
struct pb_Field {
pb_Name *name;
pb_Type *type;
pb_Name *default_value;
int32_t number;
int32_t sort_index;
unsigned oneof_idx : 24;
unsigned type_id : 5; /* PB_T* enum */
unsigned repeated : 1;
unsigned packed : 1;
unsigned scalar : 1;
};
struct pb_Type {
pb_Name *name;
const char *basename;
pb_Field **field_sort;
pb_Table field_tags;
pb_Table field_names;
pb_Table oneof_index;
unsigned oneof_count; /* extra field count from oneof entries */
unsigned oneof_field; /* extra field in oneof declarations */
unsigned field_count : 28;
unsigned is_enum : 1;
unsigned is_map : 1;
unsigned is_proto3 : 1;
unsigned is_dead : 1;
};
PB_NS_END
#endif /* pb_h */
#if defined(PB_IMPLEMENTATION) && !defined(pb_implemented)
#define pb_implemented
#define PB_MAX_SIZET ((unsigned)~0 - 100)
#define PB_MAX_HASHSIZE ((unsigned)~0 - 100)
#define PB_MIN_STRTABLE_SIZE 16
#define PB_MIN_HASHTABLE_SIZE 8
#define PB_HASHLIMIT 5
#include <assert.h>
#include <stdlib.h>
#include <string.h>
PB_NS_BEGIN
/* conversions */
PB_API uint32_t pb_encode_sint32(int32_t value)
{ return ((uint32_t)value << 1) ^ -(value < 0); }
PB_API int32_t pb_decode_sint32(uint32_t value)
{ return (value >> 1) ^ -(int32_t)(value & 1); }
PB_API uint64_t pb_encode_sint64(int64_t value)
{ return ((uint64_t)value << 1) ^ -(value < 0); }
PB_API int64_t pb_decode_sint64(uint64_t value)
{ return (value >> 1) ^ -(int64_t)(value & 1); }
PB_API uint64_t pb_expandsig(uint32_t value)
{ uint64_t m = (uint64_t)1U << 31; return (value ^ m) - m; }
PB_API uint32_t pb_encode_float(float value)
{ union { uint32_t u32; float f; } u; u.f = value; return u.u32; }
PB_API float pb_decode_float(uint32_t value)
{ union { uint32_t u32; float f; } u; u.u32 = value; return u.f; }
PB_API uint64_t pb_encode_double(double value)
{ union { uint64_t u64; double d; } u; u.d = value; return u.u64; }
PB_API double pb_decode_double(uint64_t value)
{ union { uint64_t u64; double d; } u; u.u64 = value; return u.d; }
/* decode */
PB_API pb_Slice pb_slice(const char *s)
{ return s ? pb_lslice(s, strlen(s)) : pb_lslice(NULL, 0); }
PB_API size_t pb_pos(const pb_Slice s) { return s.p - s.start; }
PB_API size_t pb_len(const pb_Slice s) { return s.end - s.p; }
static size_t pb_readvarint_slow(pb_Slice *s, uint64_t *pv) {
const char *p = s->p;
uint64_t n = 0;
int i = 0;
while (s->p < s->end && i < 10) {
int b = *s->p++;
n |= ((uint64_t)b & 0x7F) << (7*i++);
if ((b & 0x80) == 0) {
*pv = n;
return i;
}
}
s->p = p;
return 0;
}
static size_t pb_readvarint32_fallback(pb_Slice *s, uint32_t *pv) {
const uint8_t *p = (const uint8_t*)s->p, *o = p;
uint32_t b, n;
for (;;) {
n = *p++ - 0x80, n += (b = *p++) << 7; if (!(b & 0x80)) break;
n -= 0x80 << 7, n += (b = *p++) << 14; if (!(b & 0x80)) break;
n -= 0x80 << 14, n += (b = *p++) << 21; if (!(b & 0x80)) break;
n -= 0x80 << 21, n += (b = *p++) << 28; if (!(b & 0x80)) break;
/* n -= 0x80 << 28; */
if (!(*p++ & 0x80)) break;
if (!(*p++ & 0x80)) break;
if (!(*p++ & 0x80)) break;
if (!(*p++ & 0x80)) break;
if (!(*p++ & 0x80)) break;
return 0;
}
*pv = n;
s->p = (const char*)p;
return p - o;
}
static size_t pb_readvarint64_fallback(pb_Slice *s, uint64_t *pv) {
const uint8_t *p = (const uint8_t*)s->p, *o = p;
uint32_t b, n1, n2 = 0, n3 = 0;
for (;;) {
n1 = *p++ - 0x80, n1 += (b = *p++) << 7; if (!(b & 0x80)) break;
n1 -= 0x80 << 7, n1 += (b = *p++) << 14; if (!(b & 0x80)) break;
n1 -= 0x80 << 14, n1 += (b = *p++) << 21; if (!(b & 0x80)) break;
n1 -= 0x80 << 21, n2 += (b = *p++) ; if (!(b & 0x80)) break;
n2 -= 0x80 , n2 += (b = *p++) << 7; if (!(b & 0x80)) break;
n2 -= 0x80 << 7, n2 += (b = *p++) << 14; if (!(b & 0x80)) break;
n2 -= 0x80 << 14, n2 += (b = *p++) << 21; if (!(b & 0x80)) break;
n2 -= 0x80 << 21, n3 += (b = *p++) ; if (!(b & 0x80)) break;
n3 -= 0x80 , n3 += (b = *p++) << 7; if (!(b & 0x80)) break;
return 0;
}
*pv = n1 | ((uint64_t)n2 << 28) | ((uint64_t)n3 << 56);
s->p = (const char*)p;
return p - o;
}
PB_API pb_Slice pb_lslice(const char *s, size_t len) {
pb_Slice slice;
slice.start = slice.p = s;
slice.end = s + len;
return slice;
}
PB_API size_t pb_readvarint32(pb_Slice *s, uint32_t *pv) {
uint64_t u64;
size_t ret;
if (s->p >= s->end) return 0;
if (!(*s->p & 0x80)) return *pv = *s->p++, 1;
if (pb_len(*s) >= 10 || !(s->end[-1] & 0x80))
return pb_readvarint32_fallback(s, pv);
if ((ret = pb_readvarint_slow(s, &u64)) != 0)
*pv = (uint32_t)u64;
return ret;
}
PB_API size_t pb_readvarint64(pb_Slice *s, uint64_t *pv) {
if (s->p >= s->end) return 0;
if (!(*s->p & 0x80)) return *pv = *s->p++, 1;
if (pb_len(*s) >= 10 || !(s->end[-1] & 0x80))
return pb_readvarint64_fallback(s, pv);
return pb_readvarint_slow(s, pv);
}
PB_API size_t pb_readfixed32(pb_Slice *s, uint32_t *pv) {
int i;
uint32_t n = 0;
if (s->p + 4 > s->end)
return 0;
for (i = 3; i >= 0; --i) {
n <<= 8;
n |= s->p[i] & 0xFF;
}
s->p += 4;
*pv = n;
return 4;
}
PB_API size_t pb_readfixed64(pb_Slice *s, uint64_t *pv) {
int i;
uint64_t n = 0;
if (s->p + 8 > s->end)
return 0;
for (i = 7; i >= 0; --i) {
n <<= 8;
n |= s->p[i] & 0xFF;
}
s->p += 8;
*pv = n;
return 8;
}
PB_API size_t pb_readslice(pb_Slice *s, size_t len, pb_Slice *pv) {
if (pb_len(*s) < len)
return 0;
pv->start = s->start;
pv->p = s->p;
pv->end = s->p + len;
s->p = pv->end;
return len;
}
PB_API size_t pb_readbytes(pb_Slice *s, pb_Slice *pv) {
const char *p = s->p;
uint64_t len;
if (pb_readvarint64(s, &len) == 0 || pb_len(*s) < len) {
s->p = p;
return 0;
}
pv->start = s->start;
pv->p = s->p;
pv->end = s->p + len;
s->p = pv->end;
return s->p - p;
}
PB_API size_t pb_readgroup(pb_Slice *s, uint32_t tag, pb_Slice *pv) {
const char *p = s->p;
uint32_t newtag = 0;
size_t count;
assert(pb_gettype(tag) == PB_TGSTART);
while ((count = pb_readvarint32(s, &newtag)) != 0) {
if (pb_gettype(newtag) == PB_TGEND) {
if (pb_gettag(newtag) != pb_gettag(tag))
break;
pv->start = s->start;
pv->p = p;
pv->end = s->p - count;
return s->p - p;
}
if (pb_skipvalue(s, newtag) == 0) break;
}
s->p = p;
return 0;
}
PB_API size_t pb_skipvalue(pb_Slice *s, uint32_t tag) {
const char *p = s->p;
size_t ret = 0;
pb_Slice data;
switch (pb_gettype(tag)) {
default: break;
case PB_TVARINT: ret = pb_skipvarint(s); break;
case PB_T64BIT: ret = pb_skipslice(s, 8); break;
case PB_TBYTES: ret = pb_skipbytes(s); break;
case PB_T32BIT: ret = pb_skipslice(s, 4); break;
case PB_TGSTART: ret = pb_readgroup(s, tag, &data); break;
}
if (!ret) s->p = p;
return ret;
}
PB_API size_t pb_skipvarint(pb_Slice *s) {
const char *p = s->p, *op = p;
while (p < s->end && (*p & 0x80) != 0) ++p;
if (p >= s->end) return 0;
s->p = ++p;
return p - op;
}
PB_API size_t pb_skipbytes(pb_Slice *s) {
const char *p = s->p;
uint64_t var;
if (!pb_readvarint64(s, &var)) return 0;
if (pb_len(*s) < var) {
s->p = p;
return 0;
}
s->p += var;
return s->p - p;
}
PB_API size_t pb_skipslice(pb_Slice *s, size_t len) {
if (s->p + len > s->end) return 0;
s->p += len;
return len;
}
PB_API int pb_wtypebytype(int type) {
switch (type) {
case PB_Tdouble: return PB_T64BIT;
case PB_Tfloat: return PB_T32BIT;
case PB_Tint64: return PB_TVARINT;
case PB_Tuint64: return PB_TVARINT;
case PB_Tint32: return PB_TVARINT;
case PB_Tfixed64: return PB_T64BIT;
case PB_Tfixed32: return PB_T32BIT;
case PB_Tbool: return PB_TVARINT;
case PB_Tstring: return PB_TBYTES;
case PB_Tmessage: return PB_TBYTES;
case PB_Tbytes: return PB_TBYTES;
case PB_Tuint32: return PB_TVARINT;
case PB_Tenum: return PB_TVARINT;
case PB_Tsfixed32: return PB_T32BIT;
case PB_Tsfixed64: return PB_T64BIT;
case PB_Tsint32: return PB_TVARINT;
case PB_Tsint64: return PB_TVARINT;
default: return PB_TWIRECOUNT;
}
}
PB_API const char *pb_wtypename(int wiretype, const char *def) {
switch (wiretype) {
#define X(id,name,fmt) case PB_T##id: return name;
PB_WIRETYPES(X)
#undef X
default: return def ? def : "<unknown>";
}
}
PB_API const char *pb_typename(int type, const char *def) {
switch (type) {
#define X(name,type,fmt) case PB_T##name: return #name;
PB_TYPES(X)
#undef X
default: return def ? def : "<unknown>";
}
}
PB_API int pb_typebyname(const char *name, int def) {
static struct entry { const char *name; int value; } names[] = {
#define X(name,type,fmt) { #name, PB_T##name },
PB_TYPES(X)
#undef X
{ NULL, 0 }
};
struct entry *p;
for (p = names; p->name != NULL; ++p)
if (strcmp(p->name, name) == 0)
return p->value;
return def;
}
PB_API int pb_wtypebyname(const char *name, int def) {
static struct entry { const char *name; int value; } names[] = {
#define X(id,name,fmt) { name, PB_T##id },
PB_WIRETYPES(X)
#undef X
{ NULL, 0 }
};
struct entry *p;
for (p = names; p->name != NULL; ++p)
if (strcmp(p->name, name) == 0)
return p->value;
return def;
}
/* encode */
PB_API pb_Slice pb_result(const pb_Buffer *b)
{ pb_Slice slice = pb_lslice(pb_buffer(b), b->size); return slice; }
PB_API void pb_initbuffer(pb_Buffer *b)
{ memset(b, 0, sizeof(pb_Buffer)); }
PB_API void pb_resetbuffer(pb_Buffer *b)
{ if (pb_onheap(b)) PB_FREE(b->u.h.buff); pb_initbuffer(b); }
static int pb_write32(char *buff, uint32_t n) {
int p, c = 0;
do {
p = n & 0x7F; if ((n >>= 7) == 0) break; *buff++ = p | 0x80, ++c;
p = n & 0x7F; if ((n >>= 7) == 0) break; *buff++ = p | 0x80, ++c;
p = n & 0x7F; if ((n >>= 7) == 0) break; *buff++ = p | 0x80, ++c;
p = n & 0x7F; if ((n >>= 7) == 0) break; *buff++ = p | 0x80, ++c;
p = n;
} while (0);
return *buff++ = p, ++c;
}
static int pb_write64(char *buff, uint64_t n) {
int p, c = 0;
do {
p = n & 0x7F; if ((n >>= 7) == 0) break; *buff++ = p | 0x80, ++c;
p = n & 0x7F; if ((n >>= 7) == 0) break; *buff++ = p | 0x80, ++c;
p = n & 0x7F; if ((n >>= 7) == 0) break; *buff++ = p | 0x80, ++c;
p = n & 0x7F; if ((n >>= 7) == 0) break; *buff++ = p | 0x80, ++c;
p = n & 0x7F; if ((n >>= 7) == 0) break; *buff++ = p | 0x80, ++c;
p = n & 0x7F; if ((n >>= 7) == 0) break; *buff++ = p | 0x80, ++c;
p = n & 0x7F; if ((n >>= 7) == 0) break; *buff++ = p | 0x80, ++c;
p = n & 0x7F; if ((n >>= 7) == 0) break; *buff++ = p | 0x80, ++c;
p = n & 0x7F; if ((n >>= 7) == 0) break; *buff++ = p | 0x80, ++c;
p = n & 0x7F;
} while (0);
return *buff++ = p, ++c;
}
PB_API char *pb_prepbuffsize(pb_Buffer *b, size_t len) {
size_t capacity = pb_onheap(b) ? b->u.h.capacity : PB_SSO_SIZE;
if (b->size + len > capacity) {
char *newp, *oldp = pb_onheap(b) ? b->u.h.buff : NULL;
size_t expected = b->size + len;
size_t newsize = PB_SSO_SIZE;
while (newsize < PB_MAX_SIZET/2 && newsize < expected)
newsize += newsize >> 1;
if (newsize < expected) return NULL;
if ((newp = (char*)PB_REALLOC(oldp, newsize)) == NULL) return NULL;
if (!pb_onheap(b)) memcpy(newp, pb_buffer(b), b->size);
b->heap = 1;
b->u.h.buff = newp;
b->u.h.capacity = (unsigned)newsize;
}
return &pb_buffer(b)[b->size];
}
PB_API size_t pb_addslice(pb_Buffer *b, pb_Slice s) {
size_t len = pb_len(s);
char *buff = pb_prepbuffsize(b, len);
if (buff == NULL) return 0;
memcpy(buff, s.p, len);
pb_addsize(b, len);
return len;
}
PB_API size_t pb_addlength(pb_Buffer *b, size_t len, size_t prealloc) {
char buff[10], *s;
size_t bl, ml, rl = 0;
if ((bl = pb_bufflen(b)) < len)
return 0;
ml = pb_write64(buff, bl - len);
s = pb_buffer(b) + len - prealloc;
assert(ml >= prealloc);
if (ml > prealloc) {
if (pb_prepbuffsize(b, (rl = ml - prealloc)) == NULL) return 0;
s = pb_buffer(b) + len - prealloc;
memmove(s+ml, s+prealloc, bl - len);
}
memcpy(s, buff, ml);
pb_addsize(b, rl);
return ml + (bl - len);
}
PB_API size_t pb_addbytes(pb_Buffer *b, pb_Slice s) {
size_t ret, len = pb_len(s);
if (pb_prepbuffsize(b, len+5) == NULL) return 0;
ret = pb_addvarint32(b, (uint32_t)len);
return ret + pb_addslice(b, s);
}
PB_API size_t pb_addvarint32(pb_Buffer *b, uint32_t n) {
char *buff = pb_prepbuffsize(b, 5);
size_t l;
if (buff == NULL) return 0;
pb_addsize(b, l = pb_write32(buff, n));
return l;
}
PB_API size_t pb_addvarint64(pb_Buffer *b, uint64_t n) {
char *buff = pb_prepbuffsize(b, 10);
size_t l;
if (buff == NULL) return 0;
pb_addsize(b, l = pb_write64(buff, n));
return l;
}
PB_API size_t pb_addfixed32(pb_Buffer *b, uint32_t n) {
char *ch = pb_prepbuffsize(b, 4);
if (ch == NULL) return 0;
*ch++ = n & 0xFF; n >>= 8;
*ch++ = n & 0xFF; n >>= 8;
*ch++ = n & 0xFF; n >>= 8;
*ch = n & 0xFF;
pb_addsize(b, 4);
return 4;
}
PB_API size_t pb_addfixed64(pb_Buffer *b, uint64_t n) {
char *ch = pb_prepbuffsize(b, 8);
if (ch == NULL) return 0;
*ch++ = n & 0xFF; n >>= 8;
*ch++ = n & 0xFF; n >>= 8;
*ch++ = n & 0xFF; n >>= 8;
*ch++ = n & 0xFF; n >>= 8;
*ch++ = n & 0xFF; n >>= 8;
*ch++ = n & 0xFF; n >>= 8;
*ch++ = n & 0xFF; n >>= 8;
*ch = n & 0xFF;
pb_addsize(b, 8);
return 8;
}
/* memory pool */
PB_API void pb_initpool(pb_Pool *pool, size_t obj_size) {
memset(pool, 0, sizeof(pb_Pool));
pool->obj_size = obj_size;
assert(obj_size > sizeof(void*) && obj_size < PB_POOLSIZE/4);
}
PB_API void pb_freepool(pb_Pool *pool) {
void *page = pool->pages;
while (page) {
void *next = *(void**)((char*)page + PB_POOLSIZE - sizeof(void*));
PB_FREE(page);
page = next;
}
pb_initpool(pool, pool->obj_size);
}
PB_API void *pb_poolalloc(pb_Pool *pool) {
void *obj = pool->freed;
if (obj == NULL) {
size_t objsize = pool->obj_size, offset;
void *newpage = PB_MALLOC(PB_POOLSIZE);
if (newpage == NULL) return NULL;
offset = ((PB_POOLSIZE - sizeof(void*)) / objsize - 1) * objsize;
for (; offset > 0; offset -= objsize) {
void **entry = (void**)((char*)newpage + offset);
*entry = pool->freed, pool->freed = (void*)entry;
}
*(void**)((char*)newpage + PB_POOLSIZE - sizeof(void*)) = pool->pages;
pool->pages = newpage;
return newpage;
}
pool->freed = *(void**)obj;
return obj;
}
PB_API void pb_poolfree(pb_Pool *pool, void *obj)
{ *(void**)obj = pool->freed, pool->freed = obj; }
/* hash table */
#define pbT_offset(a,b) ((char*)(a) - (char*)(b))
#define pbT_index(a,b) ((pb_Entry*)((char*)(a) + (b)))
PB_API void pb_inittable(pb_Table *t, size_t entrysize)
{ memset(t, 0, sizeof(pb_Table)), t->entry_size = (unsigned)entrysize; }
PB_API void pb_freetable(pb_Table *t)
{ PB_FREE(t->hash); pb_inittable(t, t->entry_size); }
static pb_Entry *pbT_hash(const pb_Table *t, pb_Key key) {
size_t h = ((size_t)key*2654435761U)&(t->size-1);
if (key && h == 0) h = 1;
return pbT_index(t->hash, h*t->entry_size);
}
static pb_Entry *pbT_newkey(pb_Table *t, pb_Key key) {
pb_Entry *mp, *on, *next, *f = NULL;
if (t->size == 0 && pb_resizetable(t, (size_t)t->size*2) == 0) return NULL;
if (key == 0) {
mp = t->hash;
t->has_zero = 1;
} else if ((mp = pbT_hash(t, key))->key != 0) {
while (t->lastfree > t->entry_size) {
pb_Entry *cur = pbT_index(t->hash, t->lastfree -= t->entry_size);
if (cur->key == 0 && cur->next == 0) { f = cur; break; }
}
if (f == NULL) return pb_resizetable(t, (size_t)t->size*2u) ?
pbT_newkey(t, key) : NULL;
if ((on = pbT_hash(t, mp->key)) != mp) {
while ((next = pbT_index(on, on->next)) != mp) on = next;
on->next = pbT_offset(f, on);
memcpy(f, mp, t->entry_size);
if (mp->next != 0) f->next += pbT_offset(mp, f), mp->next = 0;
} else {
if (mp->next != 0) f->next = pbT_offset(mp, f) + mp->next;
else assert(f->next == 0);
mp->next = pbT_offset(f, mp);
mp = f;
}
}
mp->key = key;
if (t->entry_size != sizeof(pb_Entry))
memset(mp+1, 0, t->entry_size - sizeof(pb_Entry));
return mp;
}
PB_API size_t pb_resizetable(pb_Table *t, size_t size) {
pb_Table nt = *t;
unsigned i, rawsize = t->size*t->entry_size;
unsigned newsize = PB_MIN_HASHTABLE_SIZE;
while (newsize < PB_MAX_HASHSIZE/t->entry_size && newsize < size)
newsize <<= 1;
if (newsize < size) return 0;
nt.size = newsize;
nt.lastfree = nt.entry_size * newsize;
nt.hash = (pb_Entry*)PB_MALLOC(nt.lastfree);
if (nt.hash == NULL) return 0;
memset(nt.hash, 0, nt.lastfree);
for (i = 0; i < rawsize; i += t->entry_size) {
pb_Entry *olde = (pb_Entry*)((char*)t->hash + i);
pb_Entry *newe = pbT_newkey(&nt, olde->key);
if (nt.entry_size > sizeof(pb_Entry))
memcpy(newe+1, olde+1, nt.entry_size - sizeof(pb_Entry));
}
PB_FREE(t->hash);
*t = nt;
return newsize;
}
PB_API pb_Entry *pb_gettable(const pb_Table *t, pb_Key key) {
pb_Entry *entry;
if (t == NULL || t->size == 0)
return NULL;
if (key == 0)
return t->has_zero ? t->hash : NULL;
for (entry = pbT_hash(t, key);
entry->key != key;
entry = pbT_index(entry, entry->next))
if (entry->next == 0) return NULL;
return entry;
}
PB_API pb_Entry *pb_settable(pb_Table *t, pb_Key key) {
pb_Entry *entry;
if ((entry = pb_gettable(t, key)) != NULL)
return entry;
return pbT_newkey(t, key);
}
PB_API int pb_nextentry(const pb_Table *t, const pb_Entry **pentry) {
size_t i = *pentry ? pbT_offset(*pentry, t->hash) : 0;
size_t size = (size_t)t->size*t->entry_size;
if (*pentry == NULL && t->has_zero) {
*pentry = t->hash;
return 1;
}
while (i += t->entry_size, i < size) {
pb_Entry *entry = pbT_index(t->hash, i);
if (entry->key != 0) {
*pentry = entry;
return 1;
}
}
*pentry = NULL;
return 0;
}
/* name table */
static void pbN_init(pb_State *S)
{ memset(&S->nametable, 0, sizeof(pb_NameTable)); }
PB_API pb_Name *pb_usename(pb_Name *name)
{ if (name != NULL) ++((pb_NameEntry*)name-1)->refcount; return name; }
static void pbN_free(pb_State *S) {
pb_NameTable *nt = &S->nametable;
size_t i;
for (i = 0; i < nt->size; ++i) {