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
|
#include "codegen_c.h"
#include "flatcc/flatcc_types.h"
/* -DFLATCC_PORTABLE may help if inttypes.h is missing. */
#ifndef PRId64
#include <inttypes.h>
#endif
static int gen_verifier_pretext(fb_output_t *out)
{
fprintf(out->fp,
"#ifndef %s_VERIFIER_H\n"
"#define %s_VERIFIER_H\n",
out->S->basenameup, out->S->basenameup);
fprintf(out->fp, "\n/* " FLATCC_GENERATED_BY " */\n\n");
/* Needed to get the file identifiers */
fprintf(out->fp, "#ifndef %s_READER_H\n", out->S->basenameup);
fprintf(out->fp, "#include \"%s_reader.h\"\n", out->S->basename);
fprintf(out->fp, "#endif\n");
fprintf(out->fp, "#include \"flatcc/flatcc_verifier.h\"\n");
fb_gen_c_includes(out, "_verifier.h", "_VERIFIER_H");
gen_prologue(out);
fprintf(out->fp, "\n");
return 0;
}
static int gen_verifier_footer(fb_output_t *out)
{
gen_epilogue(out);
fprintf(out->fp,
"#endif /* %s_VERIFIER_H */\n",
out->S->basenameup);
return 0;
}
static int gen_union_verifier(fb_output_t *out, fb_compound_type_t *ct)
{
fb_symbol_t *sym;
fb_member_t *member;
fb_scoped_name_t snt, snref;
int n;
const char *s;
fb_clear(snt);
fb_clear(snref);
fb_compound_name(ct, &snt);
fprintf(out->fp,
"static int %s_union_verifier(flatcc_union_verifier_descriptor_t *ud)\n{\n switch (ud->type) {\n",
snt.text);
for (sym = ct->members; sym; sym = sym->link) {
member = (fb_member_t *)sym;
symbol_name(sym, &n, &s);
switch (member->type.type) {
case vt_missing:
/* NONE is of type vt_missing and already handled. */
continue;
case vt_compound_type_ref:
fb_compound_name(member->type.ct, &snref);
switch (member->type.ct->symbol.kind) {
case fb_is_table:
fprintf(out->fp,
" case %u: return flatcc_verify_union_table(ud, %s_verify_table); /* %.*s */\n",
(unsigned)member->value.u, snref.text, n, s);
continue;
case fb_is_struct:
fprintf(out->fp,
" case %u: return flatcc_verify_union_struct(ud, %"PRIu64", %"PRIu16"); /* %.*s */\n",
(unsigned)member->value.u, member->type.ct->size, member->type.ct->align, n, s);
continue;
default:
gen_panic(out, "internal error: unexpected compound type for union verifier");
return -1;
}
case vt_string_type:
fprintf(out->fp,
" case %u: return flatcc_verify_union_string(ud); /* %.*s */\n",
(unsigned)member->value.u, n, s);
continue;
default:
gen_panic(out, "internal error: unexpected type for union verifier");
return -1;
}
}
fprintf(out->fp,
" default: return flatcc_verify_ok;\n }\n}\n\n");
return 0;
}
static int gen_table_verifier(fb_output_t *out, fb_compound_type_t *ct)
{
fb_symbol_t *sym;
fb_member_t *member;
fb_scoped_name_t snt, snref;
int required, first = 1;
const char *nsc = out->nsc;
fb_clear(snt);
fb_clear(snref);
fb_compound_name(ct, &snt);
fprintf(out->fp,
"static int %s_verify_table(flatcc_table_verifier_descriptor_t *td)\n{\n",
snt.text);
for (sym = ct->members; sym; sym = sym->link) {
member = (fb_member_t *)sym;
if (member->metadata_flags & fb_f_deprecated) {
continue;
}
if (first) {
fprintf(out->fp, " int ret;\n if ((ret = ");
} else {
fprintf(out->fp, ")) return ret;\n if ((ret = ");
}
first = 0;
required = (member->metadata_flags & fb_f_required) != 0;
switch (member->type.type) {
case vt_scalar_type:
fprintf(
out->fp,
"flatcc_verify_field(td, %"PRIu64", %"PRIu64", %"PRIu16")",
member->id, member->size, member->align);
break;
case vt_vector_type:
if (member->nest) {
fb_compound_name((fb_compound_type_t *)&member->nest->symbol, &snref);
if (member->nest->symbol.kind == fb_is_table) {
fprintf(out->fp,
"flatcc_verify_table_as_nested_root(td, %"PRIu64", "
"%u, 0, %"PRIu16", %s_verify_table)",
member->id, required, member->align, snref.text);
} else {
fprintf(out->fp,
"flatcc_verify_struct_as_nested_root(td, %"PRIu64", "
"%u, 0, %"PRIu64", %"PRIu16")",
member->id, required, member->size, member->align);
}
} else {
fprintf(out->fp,
"flatcc_verify_vector_field(td, %"PRIu64", %d, %"PRIu64", %"PRIu16", INT64_C(%"PRIu64"))",
member->id, required, member->size, member->align, (uint64_t)FLATBUFFERS_COUNT_MAX(member->size));
};
break;
case vt_string_type:
fprintf(out->fp,
"flatcc_verify_string_field(td, %"PRIu64", %d)",
member->id, required);
break;
case vt_vector_string_type:
fprintf(out->fp,
"flatcc_verify_string_vector_field(td, %"PRIu64", %d)",
member->id, required);
break;
case vt_compound_type_ref:
fb_compound_name(member->type.ct, &snref);
switch (member->type.ct->symbol.kind) {
case fb_is_enum:
case fb_is_struct:
fprintf(out->fp,
"flatcc_verify_field(td, %"PRIu64", %"PRIu64", %"PRIu16")",
member->id, member->size, member->align);
break;
case fb_is_table:
fprintf(out->fp,
"flatcc_verify_table_field(td, %"PRIu64", %d, &%s_verify_table)",
member->id, required, snref.text);
break;
case fb_is_union:
fprintf(out->fp,
"flatcc_verify_union_field(td, %"PRIu64", %d, &%s_union_verifier)",
member->id, required, snref.text);
break;
default:
gen_panic(out, "internal error: unexpected compound type for table verifier");
return -1;
}
break;
case vt_vector_compound_type_ref:
fb_compound_name(member->type.ct, &snref);
switch (member->type.ct->symbol.kind) {
case fb_is_table:
fprintf(out->fp,
"flatcc_verify_table_vector_field(td, %"PRIu64", %d, &%s_verify_table)",
member->id, required, snref.text);
break;
case fb_is_enum:
case fb_is_struct:
fprintf(out->fp,
"flatcc_verify_vector_field(td, %"PRIu64", %d, %"PRIu64", %"PRIu16", INT64_C(%"PRIu64"))",
member->id, required, member->size, member->align, (uint64_t)FLATBUFFERS_COUNT_MAX(member->size));
break;
case fb_is_union:
fprintf(out->fp,
"flatcc_verify_union_vector_field(td, %"PRIu64", %d, &%s_union_verifier)",
member->id, required, snref.text);
break;
default:
gen_panic(out, "internal error: unexpected vector compound type for table verifier");
return -1;
}
break;
}
fprintf(out->fp, " /* %.*s */", (int)sym->ident->len, sym->ident->text);
}
if (!first) {
fprintf(out->fp, ")) return ret;\n");
}
fprintf(out->fp, " return flatcc_verify_ok;\n");
fprintf(out->fp, "}\n\n");
fprintf(out->fp,
"static inline int %s_verify_as_root(const void *buf, size_t bufsiz)\n"
"{\n return flatcc_verify_table_as_root(buf, bufsiz, %s_identifier, &%s_verify_table);\n}\n\n",
snt.text, snt.text, snt.text);
fprintf(out->fp,
"static inline int %s_verify_as_typed_root(const void *buf, size_t bufsiz)\n"
"{\n return flatcc_verify_table_as_root(buf, bufsiz, %s_type_identifier, &%s_verify_table);\n}\n\n",
snt.text, snt.text, snt.text);
fprintf(out->fp,
"static inline int %s_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const char *fid)\n"
"{\n return flatcc_verify_table_as_root(buf, bufsiz, fid, &%s_verify_table);\n}\n\n",
snt.text, snt.text);
fprintf(out->fp,
"static inline int %s_verify_as_root_with_type_hash(const void *buf, size_t bufsiz, %sthash_t thash)\n"
"{\n return flatcc_verify_table_as_typed_root(buf, bufsiz, thash, &%s_verify_table);\n}\n\n",
snt.text, nsc, snt.text);
return 0;
}
static int gen_struct_verifier(fb_output_t *out, fb_compound_type_t *ct)
{
fb_scoped_name_t snt;
fb_clear(snt);
fb_compound_name(ct, &snt);
fprintf(out->fp,
"static inline int %s_verify_as_root(const void *buf, size_t bufsiz)\n"
"{\n return flatcc_verify_struct_as_root(buf, bufsiz, %s_identifier, %"PRIu64", %"PRIu16");\n}\n\n",
snt.text, snt.text, ct->size, ct->align);
fprintf(out->fp,
"static inline int %s_verify_as_typed_root(const void *buf, size_t bufsiz)\n"
"{\n return flatcc_verify_struct_as_typed_root(buf, bufsiz, %s_type_hash, %"PRIu64", %"PRIu16");\n}\n\n",
snt.text, snt.text, ct->size, ct->align);
fprintf(out->fp,
"static inline int %s_verify_as_root_with_type_hash(const void *buf, size_t bufsiz, %sthash_t thash)\n"
"{\n return flatcc_verify_struct_as_typed_root(buf, bufsiz, thash, %"PRIu64", %"PRIu16");\n}\n\n",
snt.text, out->nsc, ct->size, ct->align);
fprintf(out->fp,
"static inline int %s_verify_as_root_with_identifier(const void *buf, size_t bufsiz, const char *fid)\n"
"{\n return flatcc_verify_struct_as_root(buf, bufsiz, fid, %"PRIu64", %"PRIu16");\n}\n\n",
snt.text, ct->size, ct->align);
return 0;
}
static int gen_verifier_prototypes(fb_output_t *out)
{
fb_symbol_t *sym;
fb_scoped_name_t snt;
fb_clear(snt);
for (sym = out->S->symbols; sym; sym = sym->link) {
switch (sym->kind) {
case fb_is_table:
fb_compound_name((fb_compound_type_t *)sym, &snt);
fprintf(out->fp,
"static int %s_verify_table(flatcc_table_verifier_descriptor_t *td);\n",
snt.text);
}
}
fprintf(out->fp, "\n");
return 0;
}
static int gen_union_verifiers(fb_output_t *out)
{
fb_symbol_t *sym;
for (sym = out->S->symbols; sym; sym = sym->link) {
switch (sym->kind) {
case fb_is_union:
gen_union_verifier(out, (fb_compound_type_t *)sym);
}
}
return 0;
}
static int gen_struct_verifiers(fb_output_t *out)
{
fb_symbol_t *sym;
for (sym = out->S->symbols; sym; sym = sym->link) {
switch (sym->kind) {
case fb_is_struct:
gen_struct_verifier(out, (fb_compound_type_t *)sym);
}
}
return 0;
}
static int gen_table_verifiers(fb_output_t *out)
{
fb_symbol_t *sym;
for (sym = out->S->symbols; sym; sym = sym->link) {
switch (sym->kind) {
case fb_is_table:
gen_table_verifier(out, (fb_compound_type_t *)sym);
}
}
return 0;
}
int fb_gen_c_verifier(fb_output_t *out)
{
gen_verifier_pretext(out);
gen_verifier_prototypes(out);
gen_union_verifiers(out);
gen_struct_verifiers(out);
gen_table_verifiers(out);
gen_verifier_footer(out);
return 0;
}
|