Wireshark  4.3.0
The Wireshark network protocol analyzer
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
22 extern "C" {
23 #endif /* __cplusplus */
24 
25 #define PBL_DEFAULT_PACKAGE_NAME ""
26 
27 typedef void(*pbl_report_error_cb_t)(const char *msg_format, ...);
28 
29 /* Node types of protocol buffers language */
30 typedef 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 */
46 typedef 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 */
56 typedef 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 */
65 typedef struct pbl_node_t{
66  pbl_node_type_t nodetype;
67  gchar* name;
68  gchar* 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;
74 } pbl_node_t;
75 
76 /* like google::protobuf::MethodDescriptor of protobuf cpp library */
77 typedef struct {
78  pbl_node_t basic_info;
79  gchar* in_msg_type;
80  gboolean in_is_stream;
81  gchar* out_msg_type;
82  gboolean out_is_stream;
84 
85 /* like google::protobuf::Descriptor of protobuf cpp library */
86 typedef 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 */
93 typedef struct {
94  pbl_node_t basic_info;
95  int number;
97 
98 /* like google::protobuf::FieldDescriptor of protobuf cpp library */
99 typedef struct {
100  pbl_node_t basic_info;
101  int number;
102  int type; /* refer to PROTOBUF_TYPE_XXX of protobuf-helper.h */
103  gchar* type_name;
104  pbl_node_t* options_node;
105  gboolean is_repeated;
106  gboolean is_required;
107  gboolean has_default_value; /* Does this field have an explicitly-declared default value? */
108  gchar* orig_default_value;
109  int string_or_bytes_default_value_length;
110  union {
111  gint32 i32;
112  gint64 i64;
113  guint32 u32;
114  guint64 u64;
115  gfloat f;
116  gdouble d;
117  gboolean b;
118  gchar* s;
120  } default_value;
122 
123 /* like google::protobuf::EnumDescriptor of protobuf cpp library */
124 typedef 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. */
132 typedef struct {
133  pbl_node_t basic_info;
134  char* value;
136 
137 /* the struct of token used by the parser */
138 typedef struct _protobuf_lang_token_t {
139  gchar* v; /* token string value */
140  int ln; /* line number of this token in the .proto file */
142 
143 /* parser state */
144 typedef struct _protobuf_lang_state_t {
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  gboolean 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 */
158 static inline gchar*
159 pbl_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 */
167 static inline protobuf_lang_token_t*
168 pbl_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 */
175 static inline void
176 pbl_printf(const char* fmt, ...)
177 {
178  va_list ap;
179  va_start(ap, fmt);
180  vprintf(fmt, ap);
181  va_end(ap);
182 }
183 
189 void
190 pbl_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 */
193 void
194 pbl_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. */
197 gboolean
198 pbl_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 */
201 int run_pbl_parser(pbl_descriptor_pool_t* pool);
202 
203 /* like descriptor_pool::FindMethodByName */
205 pbl_message_descriptor_pool_FindMethodByName(const pbl_descriptor_pool_t* pool, const char* name);
206 
207 /* like MethodDescriptor::name() */
208 const char*
209 pbl_method_descriptor_name(const pbl_method_descriptor_t* method);
210 
211 /* like MethodDescriptor::full_name() */
212 const char*
213 pbl_method_descriptor_full_name(const pbl_method_descriptor_t* method);
214 
215 /* like MethodDescriptor::input_type() */
217 pbl_method_descriptor_input_type(const pbl_method_descriptor_t* method);
218 
219 /* like MethodDescriptor::output_type() */
221 pbl_method_descriptor_output_type(const pbl_method_descriptor_t* method);
222 
223 /* like descriptor_pool::FindMessageTypeByName() */
225 pbl_message_descriptor_pool_FindMessageTypeByName(const pbl_descriptor_pool_t* pool, const char* name);
226 
227 /* like Descriptor::name() */
228 const char*
229 pbl_message_descriptor_name(const pbl_message_descriptor_t* message);
230 
231 /* like Descriptor::full_name() */
232 const char*
233 pbl_message_descriptor_full_name(const pbl_message_descriptor_t* message);
234 
235 /* like Descriptor::field_count() */
236 int
237 pbl_message_descriptor_field_count(const pbl_message_descriptor_t* message);
238 
239 /* like Descriptor::field() */
241 pbl_message_descriptor_field(const pbl_message_descriptor_t* message, int field_index);
242 
243 /* like Descriptor::FindFieldByNumber() */
245 pbl_message_descriptor_FindFieldByNumber(const pbl_message_descriptor_t* message, int number);
246 
247 /* like Descriptor::FindFieldByName() */
249 pbl_message_descriptor_FindFieldByName(const pbl_message_descriptor_t* message, const char* name);
250 
251 /* like FieldDescriptor::full_name() */
252 const char*
253 pbl_field_descriptor_full_name(const pbl_field_descriptor_t* field);
254 
255 /* like FieldDescriptor::name() */
256 const char*
257 pbl_field_descriptor_name(const pbl_field_descriptor_t* field);
258 
259 /* like FieldDescriptor::number() */
260 int
261 pbl_field_descriptor_number(const pbl_field_descriptor_t* field);
262 
263 /* like FieldDescriptor::type() */
264 int
265 pbl_field_descriptor_type(const pbl_field_descriptor_t* field);
266 
267 /* like FieldDescriptor::is_repeated() */
268 int
269 pbl_field_descriptor_is_repeated(const pbl_field_descriptor_t* field);
270 
271 /* like FieldDescriptor::is_packed() */
272 int
273 pbl_field_descriptor_is_packed(const pbl_field_descriptor_t* field);
274 
275 /* like FieldDescriptor::TypeName() */
276 const char*
277 pbl_field_descriptor_TypeName(int field_type);
278 
279 /* like FieldDescriptor::message_type() */
281 pbl_field_descriptor_message_type(const pbl_field_descriptor_t* field);
282 
283 /* like FieldDescriptor::enum_type() */
285 pbl_field_descriptor_enum_type(const pbl_field_descriptor_t* field);
286 
287 /* like FieldDescriptor::is_required() */
288 gboolean
289 pbl_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? */
293 gboolean
294 pbl_field_descriptor_has_default_value(const pbl_field_descriptor_t* field);
295 
296 /* like FieldDescriptor::default_value_int32() */
297 gint32
298 pbl_field_descriptor_default_value_int32(const pbl_field_descriptor_t* field);
299 
300 /* like FieldDescriptor::default_value_int64() */
301 gint64
302 pbl_field_descriptor_default_value_int64(const pbl_field_descriptor_t* field);
303 
304 /* like FieldDescriptor::default_value_uint32() */
305 guint32
306 pbl_field_descriptor_default_value_uint32(const pbl_field_descriptor_t* field);
307 
308 /* like FieldDescriptor::default_value_uint64() */
309 guint64
310 pbl_field_descriptor_default_value_uint64(const pbl_field_descriptor_t* field);
311 
312 /* like FieldDescriptor::default_value_float() */
313 gfloat
314 pbl_field_descriptor_default_value_float(const pbl_field_descriptor_t* field);
315 
316 /* like FieldDescriptor::default_value_double() */
317 gdouble
318 pbl_field_descriptor_default_value_double(const pbl_field_descriptor_t* field);
319 
320 /* like FieldDescriptor::default_value_bool() */
321 gboolean
322 pbl_field_descriptor_default_value_bool(const pbl_field_descriptor_t* field);
323 
324 /* like FieldDescriptor::default_value_string() */
325 const gchar*
326 pbl_field_descriptor_default_value_string(const pbl_field_descriptor_t* field, int* size);
327 
328 /* like FieldDescriptor::default_value_enum() */
330 pbl_field_descriptor_default_value_enum(const pbl_field_descriptor_t* field);
331 
332 /* like EnumDescriptor::name() */
333 const char*
334 pbl_enum_descriptor_name(const pbl_enum_descriptor_t* anEnum);
335 
336 /* like EnumDescriptor::full_name() */
337 const char*
338 pbl_enum_descriptor_full_name(const pbl_enum_descriptor_t* anEnum);
339 
340 /* like EnumDescriptor::value_count() */
341 int
342 pbl_enum_descriptor_value_count(const pbl_enum_descriptor_t* anEnum);
343 
344 /* like EnumDescriptor::value() */
346 pbl_enum_descriptor_value(const pbl_enum_descriptor_t* anEnum, int value_index);
347 
348 /* like EnumDescriptor::FindValueByNumber() */
350 pbl_enum_descriptor_FindValueByNumber(const pbl_enum_descriptor_t* anEnum, int number);
351 
352 /* like EnumDescriptor::FindValueByName() */
354 pbl_enum_descriptor_FindValueByName(const pbl_enum_descriptor_t* anEnum, const gchar* name);
355 
356 /* like EnumValueDescriptor::name() */
357 const char*
358 pbl_enum_value_descriptor_name(const pbl_enum_value_descriptor_t* enumValue);
359 
360 /* like EnumValueDescriptor::full_name() */
361 const char*
362 pbl_enum_value_descriptor_full_name(const pbl_enum_value_descriptor_t* enumValue);
363 
364 /* like EnumValueDescriptor::number() */
365 int
366 pbl_enum_value_descriptor_number(const pbl_enum_value_descriptor_t* enumValue);
367 
368 /* visit all message in this pool */
369 void
370 pbl_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 */
377 pbl_node_t*
378 pbl_create_node(pbl_file_descriptor_t* file, int lineno, pbl_node_type_t nodetype, const char* name);
379 
380 /* change the name of node */
381 pbl_node_t*
382 pbl_set_node_name(pbl_node_t* node, int lineno, const char* newname);
383 
384 /* get the name of node */
385 static inline const char*
386 pbl_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. */
392 const char*
393 pbl_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 */
396 pbl_node_t*
397 pbl_add_child(pbl_node_t* parent, pbl_node_t* child);
398 
399 /* create an enumeration field node */
400 pbl_node_t*
401 pbl_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 */
404 pbl_node_t*
405 pbl_merge_children(pbl_node_t* to, pbl_node_t* from);
406 
407 /* create a field node */
408 pbl_node_t*
409 pbl_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 */
412 pbl_node_t*
413 pbl_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 */
416 pbl_node_t*
417 pbl_create_method_node(pbl_file_descriptor_t* file, int lineno, const char* name, const char* in_msg_type, gboolean in_is_stream, const char* out_msg_type, gboolean out_is_stream);
418 
419 /* create an option node */
420 pbl_node_t*
421 pbl_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. */
424 void
425 pbl_free_node(gpointer 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