Wireshark 4.5.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
98/* like google::protobuf::FieldDescriptor of protobuf cpp library */
99typedef struct {
100 pbl_node_t basic_info;
101 int number;
102 int type; /* refer to PROTOBUF_TYPE_XXX of protobuf-helper.h */
103 char* type_name;
104 pbl_node_t* options_node;
105 bool is_repeated;
106 bool is_required;
107 bool has_default_value; /* Does this field have an explicitly-declared default value? */
108 char* orig_default_value;
109 int string_or_bytes_default_value_length;
110 union {
111 int32_t i32;
112 int64_t i64;
113 uint32_t u32;
114 uint64_t u64;
115 float f;
116 double d;
117 bool b;
118 char* s;
120 } default_value;
122
123/* like google::protobuf::EnumDescriptor of protobuf cpp library */
124typedef struct {
125 pbl_node_t basic_info;
126 GQueue* values;
127 GHashTable* values_by_number;
129
130/* Option node. The name of basic_info is optionName.
131 Now, we only care about fieldOption. */
132typedef struct {
133 pbl_node_t basic_info;
134 char* value;
136
137/* the struct of token used by the parser */
139 char* v; /* token string value */
140 int ln; /* line number of this token in the .proto file */
142
143/* parser state */
145 pbl_descriptor_pool_t* pool; /* pool will keep the parsing result */
146 pbl_file_descriptor_t* file; /* info of current parsing file */
147 GSList* lex_string_tokens;
148 GSList* lex_struct_tokens;
149 void* scanner;
150 void* pParser;
151 bool grammar_error;
152 protobuf_lang_token_t* tmp_token; /* just for passing token value from protobuf_lang_lex() to ProtobufLangParser() */
154
155/* Store chars created by strdup or g_strconcat into protobuf_lang_state_t temporarily,
156 and return back the input chars pointer.
157 It will be freed when protobuf_lang_state_t is released */
158static inline char*
159pbl_store_string_token(protobuf_lang_state_t* parser_state, char* dupstr)
160{
161 parser_state->lex_string_tokens = g_slist_prepend(parser_state->lex_string_tokens, dupstr);
162 return dupstr;
163}
164
165/* Store a protobuf_lang_token_t in protobuf_lang_state_t temporarily, and return back
166 the input pointer. It will be freed when protobuf_lang_state_t is released */
167static inline protobuf_lang_token_t*
168pbl_store_struct_token(protobuf_lang_state_t* parser_state, protobuf_lang_token_t* newtoken)
169{
170 parser_state->lex_struct_tokens = g_slist_prepend(parser_state->lex_struct_tokens, newtoken);
171 return newtoken;
172}
173
174/* default error_cb */
175static inline void
176pbl_printf(const char* fmt, ...)
177{
178 va_list ap;
179 va_start(ap, fmt);
180 vprintf(fmt, ap);
181 va_end(ap);
182}
183
189void
190pbl_reinit_descriptor_pool(pbl_descriptor_pool_t** ppool, const char** directories, pbl_report_error_cb_t error_cb);
191
192/* free all memory used by this protocol buffers language pool */
193void
194pbl_free_pool(pbl_descriptor_pool_t* pool);
195
196/* add a proto file to pool. this file will not be parsed until run_pbl_parser function is invoked. */
197bool
198pbl_add_proto_file_to_be_parsed(pbl_descriptor_pool_t* pool, const char* filepath);
199
200/* run C protocol buffers language parser, return 0 if success */
201int run_pbl_parser(pbl_descriptor_pool_t* pool);
202
203/* like descriptor_pool::FindMethodByName */
205pbl_message_descriptor_pool_FindMethodByName(const pbl_descriptor_pool_t* pool, const char* name);
206
207/* like MethodDescriptor::name() */
208const char*
209pbl_method_descriptor_name(const pbl_method_descriptor_t* method);
210
211/* like MethodDescriptor::full_name() */
212const char*
213pbl_method_descriptor_full_name(const pbl_method_descriptor_t* method);
214
215/* like MethodDescriptor::input_type() */
217pbl_method_descriptor_input_type(const pbl_method_descriptor_t* method);
218
219/* like MethodDescriptor::output_type() */
221pbl_method_descriptor_output_type(const pbl_method_descriptor_t* method);
222
223/* like descriptor_pool::FindMessageTypeByName() */
225pbl_message_descriptor_pool_FindMessageTypeByName(const pbl_descriptor_pool_t* pool, const char* name);
226
227/* like Descriptor::name() */
228const char*
229pbl_message_descriptor_name(const pbl_message_descriptor_t* message);
230
231/* like Descriptor::full_name() */
232const char*
233pbl_message_descriptor_full_name(const pbl_message_descriptor_t* message);
234
235/* like Descriptor::field_count() */
236int
237pbl_message_descriptor_field_count(const pbl_message_descriptor_t* message);
238
239/* like Descriptor::field() */
241pbl_message_descriptor_field(const pbl_message_descriptor_t* message, int field_index);
242
243/* like Descriptor::FindFieldByNumber() */
245pbl_message_descriptor_FindFieldByNumber(const pbl_message_descriptor_t* message, int number);
246
247/* like Descriptor::FindFieldByName() */
249pbl_message_descriptor_FindFieldByName(const pbl_message_descriptor_t* message, const char* name);
250
251/* like FieldDescriptor::full_name() */
252const char*
253pbl_field_descriptor_full_name(const pbl_field_descriptor_t* field);
254
255/* like FieldDescriptor::name() */
256const char*
257pbl_field_descriptor_name(const pbl_field_descriptor_t* field);
258
259/* like FieldDescriptor::number() */
260int
261pbl_field_descriptor_number(const pbl_field_descriptor_t* field);
262
263/* like FieldDescriptor::type() */
264int
265pbl_field_descriptor_type(const pbl_field_descriptor_t* field);
266
267/* like FieldDescriptor::is_repeated() */
268int
269pbl_field_descriptor_is_repeated(const pbl_field_descriptor_t* field);
270
271/* like FieldDescriptor::is_packed() */
272int
273pbl_field_descriptor_is_packed(const pbl_field_descriptor_t* field);
274
275/* like FieldDescriptor::TypeName() */
276const char*
277pbl_field_descriptor_TypeName(int field_type);
278
279/* like FieldDescriptor::message_type() */
281pbl_field_descriptor_message_type(const pbl_field_descriptor_t* field);
282
283/* like FieldDescriptor::enum_type() */
285pbl_field_descriptor_enum_type(const pbl_field_descriptor_t* field);
286
287/* like FieldDescriptor::is_required() */
288bool
289pbl_field_descriptor_is_required(const pbl_field_descriptor_t* field);
290
291/* like FieldDescriptor::has_default_value().
292 * Does this field have an explicitly-declared default value? */
293bool
294pbl_field_descriptor_has_default_value(const pbl_field_descriptor_t* field);
295
296/* like FieldDescriptor::default_value_int32() */
297int32_t
298pbl_field_descriptor_default_value_int32(const pbl_field_descriptor_t* field);
299
300/* like FieldDescriptor::default_value_int64() */
301int64_t
302pbl_field_descriptor_default_value_int64(const pbl_field_descriptor_t* field);
303
304/* like FieldDescriptor::default_value_uint32() */
305uint32_t
306pbl_field_descriptor_default_value_uint32(const pbl_field_descriptor_t* field);
307
308/* like FieldDescriptor::default_value_uint64() */
309uint64_t
310pbl_field_descriptor_default_value_uint64(const pbl_field_descriptor_t* field);
311
312/* like FieldDescriptor::default_value_float() */
313float
314pbl_field_descriptor_default_value_float(const pbl_field_descriptor_t* field);
315
316/* like FieldDescriptor::default_value_double() */
317double
318pbl_field_descriptor_default_value_double(const pbl_field_descriptor_t* field);
319
320/* like FieldDescriptor::default_value_bool() */
321bool
322pbl_field_descriptor_default_value_bool(const pbl_field_descriptor_t* field);
323
324/* like FieldDescriptor::default_value_string() */
325const char*
326pbl_field_descriptor_default_value_string(const pbl_field_descriptor_t* field, int* size);
327
328/* like FieldDescriptor::default_value_enum() */
330pbl_field_descriptor_default_value_enum(const pbl_field_descriptor_t* field);
331
332/* like EnumDescriptor::name() */
333const char*
334pbl_enum_descriptor_name(const pbl_enum_descriptor_t* anEnum);
335
336/* like EnumDescriptor::full_name() */
337const char*
338pbl_enum_descriptor_full_name(const pbl_enum_descriptor_t* anEnum);
339
340/* like EnumDescriptor::value_count() */
341int
342pbl_enum_descriptor_value_count(const pbl_enum_descriptor_t* anEnum);
343
344/* like EnumDescriptor::value() */
346pbl_enum_descriptor_value(const pbl_enum_descriptor_t* anEnum, int value_index);
347
348/* like EnumDescriptor::FindValueByNumber() */
350pbl_enum_descriptor_FindValueByNumber(const pbl_enum_descriptor_t* anEnum, int number);
351
352/* like EnumDescriptor::FindValueByName() */
354pbl_enum_descriptor_FindValueByName(const pbl_enum_descriptor_t* anEnum, const char* name);
355
356/* like EnumValueDescriptor::name() */
357const char*
358pbl_enum_value_descriptor_name(const pbl_enum_value_descriptor_t* enumValue);
359
360/* like EnumValueDescriptor::full_name() */
361const char*
362pbl_enum_value_descriptor_full_name(const pbl_enum_value_descriptor_t* enumValue);
363
364/* like EnumValueDescriptor::number() */
365int
366pbl_enum_value_descriptor_number(const pbl_enum_value_descriptor_t* enumValue);
367
368/* visit all message in this pool */
369void
370pbl_foreach_message(const pbl_descriptor_pool_t* pool, void (*cb)(const pbl_message_descriptor_t*, void*), void* userdata);
371
372/*
373 * Following are tree building functions.
374 */
375
376/* create a normal node */
378pbl_create_node(pbl_file_descriptor_t* file, int lineno, pbl_node_type_t nodetype, const char* name);
379
380/* change the name of node */
382pbl_set_node_name(pbl_node_t* node, int lineno, const char* newname);
383
384/* get the name of node */
385static inline const char*
386pbl_get_node_name(pbl_node_t* node)
387{
388 return node->name;
389}
390
391/* get the full name of node. if it is NULL, it will be built. */
392const char*
393pbl_get_node_full_name(pbl_node_t* node);
394
395/* append a node as a child of the parent node, and return the parent pointer */
397pbl_add_child(pbl_node_t* parent, pbl_node_t* child);
398
399/* create an enumeration field node */
401pbl_create_enum_value_node(pbl_file_descriptor_t* file, int lineno, const char* name, int number);
402
403/* merge one('from') node's children to another('to') node, and return the 'to' pointer */
405pbl_merge_children(pbl_node_t* to, pbl_node_t* from);
406
407/* create a field node */
409pbl_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);
410
411/* create a map field node */
413pbl_create_map_field_node(pbl_file_descriptor_t* file, int lineno, const char* name, int number, pbl_node_t* options);
414
415/* create a method (rpc or stream of service) node */
417pbl_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);
418
419/* create an option node */
421pbl_create_option_node(pbl_file_descriptor_t* file, int lineno, const char* name, const char* value);
422
423/* free a pbl_node_t and its children. */
424void
425pbl_free_node(void *anode);
426
427#ifdef __cplusplus
428}
429#endif /* __cplusplus */
430
431#endif /* __PROTOBUF_LANG_TREE_H__ */
432
433/*
434 * Editor modelines - https://www.wireshark.org/tools/modelines.html
435 *
436 * Local variables:
437 * c-basic-offset: 4
438 * tab-width: 8
439 * indent-tabs-mode: nil
440 * End:
441 *
442 * vi: set shiftwidth=4 tabstop=8 expandtab:
443 * :indentSize=4:tabSize=8:noTabs=true:
444 */
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:144
Definition protobuf_lang_tree.h:138
Definition protobuf_lang_tree.h:46
Definition protobuf_lang_tree.h:124
Definition protobuf_lang_tree.h:93
Definition protobuf_lang_tree.h:99
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:132