Skip to content

Commit e436915

Browse files
committed
use way of google's protobuf library to load descriptor file.
1 parent 5090277 commit e436915

File tree

1 file changed

+61
-58
lines changed

1 file changed

+61
-58
lines changed

pb.h

+61-58
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ typedef struct pb_Value {
131131
#define pb_slicelen(s) ((size_t)((s)->end - (s)->p))
132132
#define pb_gettag(v) ((unsigned)((v) >> 3))
133133
#define pb_gettype(v) ((unsigned)((v) & 7))
134+
#define pb_(type, tag) (((unsigned)(tag) << 3) | (PB_T##type & 7))
134135

135136
PB_API pb_Slice pb_slice (const char *s);
136137
PB_API pb_Slice pb_lslice (const char *s, size_t len);
@@ -178,7 +179,7 @@ PB_API void pb_addvarint (pb_Buffer *b, uint64_t n);
178179
PB_API void pb_addvar32 (pb_Buffer *b, uint32_t n);
179180
PB_API void pb_addfixed64 (pb_Buffer *b, uint64_t n);
180181
PB_API void pb_addfixed32 (pb_Buffer *b, uint32_t n);
181-
PB_API void pb_addtag (pb_Buffer *b, uint32_t tag, uint32_t type);
182+
PB_API void pb_addpair (pb_Buffer *b, uint32_t tag, uint32_t type);
182183

183184
/* conversions */
184185

@@ -509,7 +510,7 @@ PB_API pb_Slice pb_result(pb_Buffer *b)
509510
PB_API void pb_adddata(pb_Buffer *b, pb_Slice *s)
510511
{ pb_addvar32(b, pb_slicelen(s)); pb_addslice(b, s); }
511512

512-
PB_API void pb_addtag(pb_Buffer *b, uint32_t tag, uint32_t type)
513+
PB_API void pb_addpair(pb_Buffer *b, uint32_t tag, uint32_t type)
513514
{ pb_addvar32(b, (uint32_t)((tag << 3) | (type & 7))); }
514515

515516
PB_API void pb_initbuffer(pb_Buffer *b) {
@@ -783,7 +784,7 @@ PB_API pb_Entry *pbM_seti(pb_Map *m, uint32_t key) {
783784
PB_API pb_Entry *pbM_gets(pb_Map *m, pb_Slice *key) {
784785
uint32_t hash;
785786
pb_Entry *e;
786-
if (m->size == 0) return NULL;
787+
if (m->size == 0 || key->p == NULL) return NULL;
787788
hash = pbM_calchash(key);
788789
assert((m->size & (m->size - 1)) == 0);
789790
e = &m->hash[hash & (m->size - 1)];
@@ -799,6 +800,7 @@ PB_API pb_Entry *pbM_gets(pb_Map *m, pb_Slice *key) {
799800

800801
PB_API pb_Entry *pbM_sets(pb_Map *m, pb_Slice *key) {
801802
pb_Entry e, *ret;
803+
if (key->p == NULL) return NULL;
802804
if ((ret = pbM_gets(m, key)) != NULL)
803805
return ret;
804806
e.key = (uintptr_t)key->p;
@@ -1119,8 +1121,9 @@ static int pbL_rawfield(pb_State *S, pb_Type *t, pb_Field *f, pb_Slice *name) {
11191121

11201122
static int pbL_rawtype(pb_State *S, pb_Type *t, pb_Slice *name) {
11211123
pb_Entry *e = pbM_sets(&S->types, name);
1122-
pb_Type *nt = (pb_Type *)e->value;
1123-
if (nt == NULL)
1124+
pb_Type *nt;
1125+
if (e == NULL) return 0;
1126+
if ((nt = (pb_Type *)e->value) == NULL)
11241127
DO_(nt = (pb_Type*)pbP_newsize(S->typepool, sizeof(pb_Type)));
11251128
else {
11261129
pbM_free(&nt->field_tags);
@@ -1132,23 +1135,23 @@ static int pbL_rawtype(pb_State *S, pb_Type *t, pb_Slice *name) {
11321135
}
11331136

11341137
static int pbL_EnumValueDescriptorProto(pb_State *S, pb_Slice *b, pb_Type *t) {
1135-
uint32_t tag, number;
1136-
pb_Slice name;
1138+
uint32_t pair, number;
1139+
pb_Slice name = { NULL, NULL };
11371140
pb_Field f;
11381141
memset(&f, 0, sizeof(f));
11391142
f.scalar = 1;
1140-
while (pb_readvar32(b, &tag)) {
1141-
switch (pb_gettag(tag)) {
1142-
case 1: /* name */
1143+
while (pb_readvar32(b, &pair)) {
1144+
switch (pair) {
1145+
case pb_(DATA, 1): /* name */
11431146
DO_(pb_readslice(b, &name));
11441147
DO_(f.name = pb_newslice(S, &name).p);
11451148
break;
1146-
case 2: /* number */
1149+
case pb_(VARINT, 2): /* number */
11471150
DO_(pb_readvar32(b, &number));
11481151
f.u.enum_value = number;
11491152
break;
11501153
default:
1151-
DO_(pb_skipvalue(b, pb_gettype(tag)));
1154+
DO_(pb_skipvalue(b, pb_gettype(pair)));
11521155
break;
11531156
}
11541157
}
@@ -1157,24 +1160,24 @@ static int pbL_EnumValueDescriptorProto(pb_State *S, pb_Slice *b, pb_Type *t) {
11571160
}
11581161

11591162
static int pbL_EnumDescriptorProto(pb_State *S, pb_Slice *b, pb_Slice *prefix) {
1160-
uint32_t tag;
1161-
pb_Slice name, slice;
1163+
uint32_t pair;
1164+
pb_Slice name = { NULL, NULL }, slice;
11621165
pb_Type t;
11631166
memset(&t, 0, sizeof(t));
11641167
t.is_enum = 1;
1165-
while (pb_readvar32(b, &tag)) {
1166-
switch (pb_gettag(tag)) {
1167-
case 1: /* name */
1168+
while (pb_readvar32(b, &pair)) {
1169+
switch (pair) {
1170+
case pb_(DATA, 1): /* name */
11681171
DO_(pb_readslice(b, &name));
11691172
DO_(pbL_getqname(S, prefix, &name));
11701173
t.name = name.p;
11711174
break;
1172-
case 2: /* value */
1175+
case pb_(DATA, 2): /* value */
11731176
DO_(pb_readslice(b, &slice));
11741177
DO_(pbL_EnumValueDescriptorProto(S, &slice, &t));
11751178
break;
11761179
default:
1177-
DO_(pb_skipvalue(b, pb_gettype(tag)));
1180+
DO_(pb_skipvalue(b, pb_gettype(pair)));
11781181
break;
11791182
}
11801183
}
@@ -1183,73 +1186,73 @@ static int pbL_EnumDescriptorProto(pb_State *S, pb_Slice *b, pb_Slice *prefix) {
11831186
}
11841187

11851188
static int pbL_FieldOptions(pb_State *S, pb_Slice *b, pb_Field *f) {
1186-
uint32_t tag;
1187-
while (pb_readvar32(b, &tag)) {
1188-
if (pb_gettag(tag) == 2) {
1189+
uint32_t pair;
1190+
while (pb_readvar32(b, &pair)) {
1191+
if (pair == pb_(VARINT, 2)) {
11891192
uint32_t v;
11901193
DO_(pb_readvar32(b, &v));
11911194
f->packed = v;
11921195
continue;
11931196
}
1194-
DO_(pb_skipvalue(b, pb_gettype(tag)));
1197+
DO_(pb_skipvalue(b, pb_gettype(pair)));
11951198
}
11961199
return b->p == b->end;
11971200
}
11981201

11991202
static int pbL_FieldDescriptorProto(pb_State *S, pb_Slice *b, pb_Type *t) {
1200-
uint32_t tag, number;
1201-
pb_Slice name, slice;
1203+
uint32_t pair, number;
1204+
pb_Slice name = { NULL, NULL }, slice;
12021205
pb_Field f;
12031206
memset(&f, 0, sizeof(f));
1204-
while (pb_readvar32(b, &tag)) {
1205-
switch (pb_gettag(tag)) {
1206-
case 1: /* name */
1207+
while (pb_readvar32(b, &pair)) {
1208+
switch (pair) {
1209+
case pb_(DATA, 1): /* name */
12071210
DO_(pb_readslice(b, &name));
12081211
DO_((name = pb_newslice(S, &name)).p);
12091212
f.name = name.p;
12101213
break;
1211-
case 3: /* number */
1214+
case pb_(VARINT, 3): /* number */
12121215
DO_(pb_readvar32(b, &number));
12131216
f.tag = number;
12141217
break;
1215-
case 4: /* label */
1218+
case pb_(VARINT, 4): /* label */
12161219
DO_(pb_readvar32(b, &number));
12171220
if (number == 3) /* LABEL_OPTIONAL */
12181221
f.repeated = 1;
12191222
break;
1220-
case 5: /* type */
1223+
case pb_(VARINT, 5): /* type */
12211224
DO_(pb_readvar32(b, &number));
12221225
DO_(number != PB_Tgroup);
12231226
f.type_id = number;
12241227
if (f.type_id != PB_Tmessage && f.type_id != PB_Tenum)
12251228
f.scalar = 1;
12261229
break;
1227-
case 6: /* type_name */
1230+
case pb_(DATA, 6): /* type_name */
12281231
DO_(pb_readslice(b, &slice));
12291232
if (*slice.p == '.') ++slice.p;
12301233
if ((f.type = pb_type(S, &slice)) == NULL) {
12311234
slice = pb_newslice(S, &slice);
12321235
f.type = pb_newtype(S, &slice);
12331236
}
12341237
break;
1235-
case 2: /* extendee */
1238+
case pb_(DATA, 2): /* extendee */
12361239
DO_(t == NULL);
12371240
DO_(pb_readslice(b, &slice));
12381241
if ((t = pb_type(S, &slice)) == NULL) {
12391242
slice = pb_newslice(S, &slice);
12401243
t = pb_newtype(S, &slice);
12411244
}
12421245
break;
1243-
case 7: /* default_value */
1246+
case pb_(DATA, 7): /* default_value */
12441247
DO_(pb_readslice(b, &slice));
12451248
DO_(f.u.default_value = pb_newslice(S, &slice).p);
12461249
break;
1247-
case 8: /* options */
1250+
case pb_(DATA, 8): /* options */
12481251
DO_(pb_readslice(b, &slice));
12491252
DO_(pbL_FieldOptions(S, b, &f));
12501253
break;
12511254
default:
1252-
DO_(pb_skipvalue(b, pb_gettype(tag)));
1255+
DO_(pb_skipvalue(b, pb_gettype(pair)));
12531256
break;
12541257
}
12551258
}
@@ -1259,35 +1262,35 @@ static int pbL_FieldDescriptorProto(pb_State *S, pb_Slice *b, pb_Type *t) {
12591262
}
12601263

12611264
static int pbL_DescriptorProto(pb_State *S, pb_Slice *b, pb_Slice *prefix) {
1262-
uint32_t tag;
1265+
uint32_t pair;
12631266
pb_Type t;
1264-
pb_Slice name, slice;
1267+
pb_Slice name = { NULL, NULL }, slice;
12651268
memset(&t, 0, sizeof(t));
1266-
while (pb_readvar32(b, &tag)) {
1267-
switch (pb_gettag(tag)) {
1268-
case 1: /* name */
1269+
while (pb_readvar32(b, &pair)) {
1270+
switch (pair) {
1271+
case pb_(DATA, 1): /* name */
12691272
DO_(pb_readslice(b, &name));
12701273
DO_(pbL_getqname(S, prefix, &name));
12711274
t.name = name.p;
12721275
break;
1273-
case 2: /* field */
1276+
case pb_(DATA, 2): /* field */
12741277
DO_(pb_readslice(b, &slice));
12751278
DO_(pbL_FieldDescriptorProto(S, &slice, &t));
12761279
break;
1277-
case 6: /* extension */
1280+
case pb_(DATA, 6): /* extension */
12781281
DO_(pb_readslice(b, &slice));
12791282
DO_(pbL_FieldDescriptorProto(S, &slice, NULL));
12801283
break;
1281-
case 3: /* nested_type */
1284+
case pb_(DATA, 3): /* nested_type */
12821285
DO_(pb_readslice(b, &slice));
12831286
DO_(pbL_DescriptorProto(S, &slice, &name));
12841287
break;
1285-
case 4: /* enum_type */
1288+
case pb_(DATA, 4): /* enum_type */
12861289
DO_(pb_readslice(b, &slice));
12871290
DO_(pbL_EnumDescriptorProto(S, &slice, &name));
12881291
break;
12891292
default:
1290-
DO_(pb_skipvalue(b, pb_gettype(tag)));
1293+
DO_(pb_skipvalue(b, pb_gettype(pair)));
12911294
break;
12921295
}
12931296
}
@@ -1296,43 +1299,43 @@ static int pbL_DescriptorProto(pb_State *S, pb_Slice *b, pb_Slice *prefix) {
12961299
}
12971300

12981301
static int pbL_FileDescriptorProto(pb_State *S, pb_Slice *b) {
1299-
uint32_t tag;
1302+
uint32_t pair;
13001303
pb_Slice package, slice;
1301-
while (pb_readvar32(b, &tag)) {
1302-
switch (pb_gettag(tag)) {
1303-
case 2: /* package */
1304+
while (pb_readvar32(b, &pair)) {
1305+
switch (pair) {
1306+
case pb_(DATA, 2): /* package */
13041307
DO_(pb_readslice(b, &package));
13051308
break;
1306-
case 4: /* message_type */
1309+
case pb_(DATA, 4): /* message_type */
13071310
DO_(pb_readslice(b, &slice));
13081311
DO_(pbL_DescriptorProto(S, &slice, &package));
13091312
break;
1310-
case 5: /* enum_type */
1313+
case pb_(DATA, 5): /* enum_type */
13111314
DO_(pb_readslice(b, &slice));
13121315
DO_(pbL_EnumDescriptorProto(S, &slice, &package));
13131316
break;
1314-
case 7: /* extension */
1317+
case pb_(DATA, 7): /* extension */
13151318
DO_(pb_readslice(b, &slice));
13161319
DO_(pbL_FieldDescriptorProto(S, &slice, NULL));
13171320
break;
13181321
default:
1319-
DO_(pb_skipvalue(b, pb_gettype(tag)));
1322+
DO_(pb_skipvalue(b, pb_gettype(pair)));
13201323
break;
13211324
}
13221325
}
13231326
return b->p == b->end;
13241327
}
13251328

13261329
PB_API int pb_load(pb_State *S, pb_Slice *b) {
1327-
uint32_t tag;
1328-
while (pb_readvar32(b, &tag)) {
1329-
if (pb_gettag(tag) == 1) {
1330+
uint32_t pair;
1331+
while (pb_readvar32(b, &pair)) {
1332+
if (pair == pb_(DATA, 1)) {
13301333
pb_Slice slice;
13311334
DO_(pb_readslice(b, &slice));
13321335
DO_(pbL_FileDescriptorProto(S, &slice));
13331336
continue;
13341337
}
1335-
DO_(pb_skipvalue(b, pb_gettype(tag)));
1338+
DO_(pb_skipvalue(b, pb_gettype(pair)));
13361339
}
13371340
return b->p == b->end;
13381341
}

0 commit comments

Comments
 (0)