Wireshark 4.7.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
protobuf_lang_tree.h
Go to the documentation of this file.
1
13#ifndef __PROTOBUF_LANG_TREE_H__
14#define __PROTOBUF_LANG_TREE_H__
15
16#include <wireshark.h>
17
18#include <stdio.h>
19#include <stdarg.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif /* __cplusplus */
24
25#define PBL_DEFAULT_PACKAGE_NAME ""
26
27typedef void(*pbl_report_error_cb_t)(const char *msg_format, ...);
28
29/* Node types of protocol buffers language */
30typedef enum {
31 PBL_UNKNOWN = 0,
32 PBL_PACKAGE,
33 PBL_MESSAGE,
34 PBL_FIELD,
35 PBL_ONEOF,
36 PBL_MAP_FIELD,
37 PBL_ENUM,
38 PBL_ENUM_VALUE,
39 PBL_SERVICE,
40 PBL_METHOD, /* contains the rpc and stream node of service */
41 PBL_OPTIONS,
42 PBL_OPTION
43} pbl_node_type_t;
44
45/* like google::protobuf::descriptor_pool of protobuf cpp library */
46typedef struct {
47 GQueue* source_paths; /* the directories in which to search for proto file */
48 pbl_report_error_cb_t error_cb; /* error call back function */
49 GHashTable* packages; /* all packages parsed from proto files */
50 GHashTable* proto_files; /* all proto files that are parsed or to be parsed */
51 GQueue* proto_files_to_be_parsed; /* files is to be parsed */
52 struct _protobuf_lang_state_t *parser_state; /* current parser state */
54
55/* file descriptor */
56typedef struct {
57 const char* filename;
58 int syntax_version;
59 const char* package_name;
60 int package_name_lineno;
63
64/* Basic information of node */
65typedef struct pbl_node_t{
66 pbl_node_type_t nodetype;
67 char* name;
68 char* full_name; /* constructed during first access */
69 struct pbl_node_t* parent;
70 GQueue* children; /* child is a pbl_node_t */
71 GHashTable* children_by_name; /* take children names as keys */
73 int lineno;
75
76/* like google::protobuf::MethodDescriptor of protobuf cpp library */
77typedef struct {
78 pbl_node_t basic_info;
79 char* in_msg_type;
80 bool in_is_stream;
81 char* out_msg_type;
82 bool out_is_stream;
84
85/* like google::protobuf::Descriptor of protobuf cpp library */
86typedef struct {
87 pbl_node_t basic_info;
88 GQueue* fields;
89 GHashTable* fields_by_number;
91
92/* like google::protobuf::EnumValueDescriptor of protobuf cpp library */
93typedef struct {
94 pbl_node_t basic_info;
95 int number;
97
134
135/* like google::protobuf::EnumDescriptor of protobuf cpp library */
136typedef struct {
137 pbl_node_t basic_info;
138 GQueue* values;
139 GHashTable* values_by_number;
141
142/* Option node. The name of basic_info is optionName.
143 Now, we only care about fieldOption. */
144typedef struct {
145 pbl_node_t basic_info;
146 char* value;
148
149/* the struct of token used by the parser */
151 char* v; /* token string value */
152 int ln; /* line number of this token in the .proto file */
154
155/* parser state */
157 pbl_descriptor_pool_t* pool; /* pool will keep the parsing result */
158 pbl_file_descriptor_t* file; /* info of current parsing file */
159 GSList* lex_string_tokens;
160 GSList* lex_struct_tokens;
161 void* scanner;
162 void* pParser;
163 bool grammar_error;
164 protobuf_lang_token_t* tmp_token; /* just for passing token value from protobuf_lang_lex() to ProtobufLangParser() */
166
167/* Store chars created by strdup or g_strconcat into protobuf_lang_state_t temporarily,
168 and return back the input chars pointer.
169 It will be freed when protobuf_lang_state_t is released */
170static inline char*
171pbl_store_string_token(protobuf_lang_state_t* parser_state, char* dupstr)
172{
173 parser_state->lex_string_tokens = g_slist_prepend(parser_state->lex_string_tokens, dupstr);
174 return dupstr;
175}
176
177/* Store a protobuf_lang_token_t in protobuf_lang_state_t temporarily, and return back
178 the input pointer. It will be freed when protobuf_lang_state_t is released */
179static inline protobuf_lang_token_t*
180pbl_store_struct_token(protobuf_lang_state_t* parser_state, protobuf_lang_token_t* newtoken)
181{
182 parser_state->lex_struct_tokens = g_slist_prepend(parser_state->lex_struct_tokens, newtoken);
183 return newtoken;
184}
185
186/* default error_cb */
187static inline void
188pbl_printf(const char* fmt, ...)
189{
190 va_list ap;
191 va_start(ap, fmt);
192 vprintf(fmt, ap);
193 va_end(ap);
194}
195
201void
202pbl_reinit_descriptor_pool(pbl_descriptor_pool_t** ppool, const char** directories, pbl_report_error_cb_t error_cb);
203
204/* free all memory used by this protocol buffers language pool */
205void
206pbl_free_pool(pbl_descriptor_pool_t* pool);
207
208/* add a proto file to pool. this file will not be parsed until run_pbl_parser function is invoked. */
209bool
210pbl_add_proto_file_to_be_parsed(pbl_descriptor_pool_t* pool, const char* filepath);
211
212/* run C protocol buffers language parser, return 0 if success */
213int run_pbl_parser(pbl_descriptor_pool_t* pool);
214
215/* like descriptor_pool::FindMethodByName */
217pbl_message_descriptor_pool_FindMethodByName(const pbl_descriptor_pool_t* pool, const char* name);
218
219/* like MethodDescriptor::name() */
220const char*
221pbl_method_descriptor_name(const pbl_method_descriptor_t* method);
222
223/* like MethodDescriptor::full_name() */
224const char*
225pbl_method_descriptor_full_name(const pbl_method_descriptor_t* method);
226
227/* like MethodDescriptor::input_type() */
229pbl_method_descriptor_input_type(const pbl_method_descriptor_t* method);
230
231/* like MethodDescriptor::output_type() */
233pbl_method_descriptor_output_type(const pbl_method_descriptor_t* method);
234
235/* like descriptor_pool::FindMessageTypeByName() */
237pbl_message_descriptor_pool_FindMessageTypeByName(const pbl_descriptor_pool_t* pool, const char* name);
238
239/* like Descriptor::name() */
240const char*
241pbl_message_descriptor_name(const pbl_message_descriptor_t* message);
242
243/* like Descriptor::full_name() */
244const char*
245pbl_message_descriptor_full_name(const pbl_message_descriptor_t* message);
246
247/* like Descriptor::field_count() */
248int
249pbl_message_descriptor_field_count(const pbl_message_descriptor_t* message);
250
251/* like Descriptor::field() */
253pbl_message_descriptor_field(const pbl_message_descriptor_t* message, int field_index);
254
255/* like Descriptor::FindFieldByNumber() */
257pbl_message_descriptor_FindFieldByNumber(const pbl_message_descriptor_t* message, int number);
258
259/* like Descriptor::FindFieldByName() */
261pbl_message_descriptor_FindFieldByName(const pbl_message_descriptor_t* message, const char* name);
262
263/* like FieldDescriptor::full_name() */
264const char*
265pbl_field_descriptor_full_name(const pbl_field_descriptor_t* field);
266
267/* like FieldDescriptor::name() */
268const char*
269pbl_field_descriptor_name(const pbl_field_descriptor_t* field);
270
271/* like FieldDescriptor::number() */
272int
273pbl_field_descriptor_number(const pbl_field_descriptor_t* field);
274
275/* like FieldDescriptor::type() */
276int
277pbl_field_descriptor_type(const pbl_field_descriptor_t* field);
278
279/* like FieldDescriptor::is_repeated() */
280int
281pbl_field_descriptor_is_repeated(const pbl_field_descriptor_t* field);
282
283/* like FieldDescriptor::is_packed() */
284int
285pbl_field_descriptor_is_packed(const pbl_field_descriptor_t* field);
286
287/* like FieldDescriptor::TypeName() */
288const char*
289pbl_field_descriptor_TypeName(wmem_allocator_t* scope, int field_type);
290
291/* like FieldDescriptor::message_type() */
293pbl_field_descriptor_message_type(const pbl_field_descriptor_t* field);
294
295/* like FieldDescriptor::enum_type() */
297pbl_field_descriptor_enum_type(const pbl_field_descriptor_t* field);
298
299/* like FieldDescriptor::is_required() */
300bool
301pbl_field_descriptor_is_required(const pbl_field_descriptor_t* field);
302
303/* like FieldDescriptor::has_default_value().
304 * Does this field have an explicitly-declared default value? */
305bool
306pbl_field_descriptor_has_default_value(const pbl_field_descriptor_t* field);
307
308/* like FieldDescriptor::default_value_int32() */
309int32_t
310pbl_field_descriptor_default_value_int32(const pbl_field_descriptor_t* field);
311
312/* like FieldDescriptor::default_value_int64() */
313int64_t
314pbl_field_descriptor_default_value_int64(const pbl_field_descriptor_t* field);
315
316/* like FieldDescriptor::default_value_uint32() */
317uint32_t
318pbl_field_descriptor_default_value_uint32(const pbl_field_descriptor_t* field);
319
320/* like FieldDescriptor::default_value_uint64() */
321uint64_t
322pbl_field_descriptor_default_value_uint64(const pbl_field_descriptor_t* field);
323
324/* like FieldDescriptor::default_value_float() */
325float
326pbl_field_descriptor_default_value_float(const pbl_field_descriptor_t* field);
327
328/* like FieldDescriptor::default_value_double() */
329double
330pbl_field_descriptor_default_value_double(const pbl_field_descriptor_t* field);
331
332/* like FieldDescriptor::default_value_bool() */
333bool
334pbl_field_descriptor_default_value_bool(const pbl_field_descriptor_t* field);
335
336/* like FieldDescriptor::default_value_string() */
337const char*
338pbl_field_descriptor_default_value_string(const pbl_field_descriptor_t* field, int* size);
339
340/* like FieldDescriptor::default_value_enum() */
342pbl_field_descriptor_default_value_enum(const pbl_field_descriptor_t* field);
343
344/* like EnumDescriptor::name() */
345const char*
346pbl_enum_descriptor_name(const pbl_enum_descriptor_t* anEnum);
347
348/* like EnumDescriptor::full_name() */
349const char*
350pbl_enum_descriptor_full_name(const pbl_enum_descriptor_t* anEnum);
351
352/* like EnumDescriptor::value_count() */
353int
354pbl_enum_descriptor_value_count(const pbl_enum_descriptor_t* anEnum);
355
356/* like EnumDescriptor::value() */
358pbl_enum_descriptor_value(const pbl_enum_descriptor_t* anEnum, int value_index);
359
360/* like EnumDescriptor::FindValueByNumber() */
362pbl_enum_descriptor_FindValueByNumber(const pbl_enum_descriptor_t* anEnum, int number);
363
364/* like EnumDescriptor::FindValueByName() */
366pbl_enum_descriptor_FindValueByName(const pbl_enum_descriptor_t* anEnum, const char* name);
367
368/* like EnumValueDescriptor::name() */
369const char*
370pbl_enum_value_descriptor_name(const pbl_enum_value_descriptor_t* enumValue);
371
372/* like EnumValueDescriptor::full_name() */
373const char*
374pbl_enum_value_descriptor_full_name(const pbl_enum_value_descriptor_t* enumValue);
375
376/* like EnumValueDescriptor::number() */
377int
378pbl_enum_value_descriptor_number(const pbl_enum_value_descriptor_t* enumValue);
379
380/* visit all message in this pool */
381void
382pbl_foreach_message(const pbl_descriptor_pool_t* pool, void (*cb)(const pbl_message_descriptor_t*, void*), void* userdata);
383
384/*
385 * Following are tree building functions.
386 */
387
388/* create a normal node */
390pbl_create_node(pbl_file_descriptor_t* file, int lineno, pbl_node_type_t nodetype, const char* name);
391
392/* change the name of node */
394pbl_set_node_name(pbl_node_t* node, int lineno, const char* newname);
395
396/* get the name of node */
397static inline const char*
398pbl_get_node_name(pbl_node_t* node)
399{
400 return node->name;
401}
402
403/* get the full name of node. if it is NULL, it will be built. */
404const char*
405pbl_get_node_full_name(pbl_node_t* node);
406
407/* append a node as a child of the parent node, and return the parent pointer */
409pbl_add_child(pbl_node_t* parent, pbl_node_t* child);
410
411/* create an enumeration field node */
413pbl_create_enum_value_node(pbl_file_descriptor_t* file, int lineno, const char* name, int number);
414
415/* merge one('from') node's children to another('to') node, and return the 'to' pointer */
417pbl_merge_children(pbl_node_t* to, pbl_node_t* from);
418
419/* create a field node */
421pbl_create_field_node(pbl_file_descriptor_t* file, int lineno, const char* label, const char* type_name, const char* name, int number, pbl_node_t* options);
422
423/* create a map field node */
425pbl_create_map_field_node(pbl_file_descriptor_t* file, int lineno, const char* name, int number, pbl_node_t* options);
426
427/* create a method (rpc or stream of service) node */
429pbl_create_method_node(pbl_file_descriptor_t* file, int lineno, const char* name, const char* in_msg_type, bool in_is_stream, const char* out_msg_type, bool out_is_stream);
430
431/* create an option node */
433pbl_create_option_node(pbl_file_descriptor_t* file, int lineno, const char* name, const char* value);
434
435/* free a pbl_node_t and its children. */
436void
437pbl_free_node(void *anode);
438
439#ifdef __cplusplus
440}
441#endif /* __cplusplus */
442
443#endif /* __PROTOBUF_LANG_TREE_H__ */
444
445/*
446 * Editor modelines - https://www.wireshark.org/tools/modelines.html
447 *
448 * Local variables:
449 * c-basic-offset: 4
450 * tab-width: 8
451 * indent-tabs-mode: nil
452 * End:
453 *
454 * vi: set shiftwidth=4 tabstop=8 expandtab:
455 * :indentSize=4:tabSize=8:noTabs=true:
456 */
void pbl_reinit_descriptor_pool(pbl_descriptor_pool_t **ppool, const char **directories, pbl_report_error_cb_t error_cb)
Definition protobuf_lang_tree.c:107
Definition protobuf_lang_tree.h:156
Definition protobuf_lang_tree.h:150
Internal memory allocator interface used by the wmem subsystem.
Definition wmem_allocator.h:34
Definition protobuf_lang_tree.h:46
Definition protobuf_lang_tree.h:136
Definition protobuf_lang_tree.h:93
Describes a field in a Protocol Buffer message, similar to google::protobuf::FieldDescriptor.
Definition protobuf_lang_tree.h:104
bool has_default_value
Definition protobuf_lang_tree.h:113
pbl_node_t * options_node
Definition protobuf_lang_tree.h:109
pbl_node_t basic_info
Definition protobuf_lang_tree.h:105
double d
Definition protobuf_lang_tree.h:128
int64_t i64
Definition protobuf_lang_tree.h:124
bool b
Definition protobuf_lang_tree.h:129
char * type_name
Definition protobuf_lang_tree.h:108
uint64_t u64
Definition protobuf_lang_tree.h:126
int type
Definition protobuf_lang_tree.h:107
float f
Definition protobuf_lang_tree.h:127
char * s
Definition protobuf_lang_tree.h:130
char * orig_default_value
Definition protobuf_lang_tree.h:114
int32_t i32
Definition protobuf_lang_tree.h:123
bool is_required
Definition protobuf_lang_tree.h:112
const pbl_enum_value_descriptor_t * e
Definition protobuf_lang_tree.h:131
int string_or_bytes_default_value_length
Definition protobuf_lang_tree.h:115
bool is_repeated
Definition protobuf_lang_tree.h:111
int number
Definition protobuf_lang_tree.h:106
uint32_t u32
Definition protobuf_lang_tree.h:125
Definition protobuf_lang_tree.h:56
Definition protobuf_lang_tree.h:86
Definition protobuf_lang_tree.h:77
Definition protobuf_lang_tree.h:65
Definition protobuf_lang_tree.h:144