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
12#pragma once
13#include <wireshark.h>
14
15#include <stdio.h>
16#include <stdarg.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif /* __cplusplus */
21
22#define PBL_DEFAULT_PACKAGE_NAME ""
23
24typedef void(*pbl_report_error_cb_t)(const char *msg_format, ...);
25
26/* Node types of protocol buffers language */
27typedef enum {
28 PBL_UNKNOWN = 0,
29 PBL_PACKAGE,
30 PBL_MESSAGE,
31 PBL_FIELD,
32 PBL_ONEOF,
33 PBL_MAP_FIELD,
34 PBL_ENUM,
35 PBL_ENUM_VALUE,
36 PBL_SERVICE,
37 PBL_METHOD, /* contains the rpc and stream node of service */
38 PBL_OPTIONS,
39 PBL_OPTION
40} pbl_node_type_t;
41
42/* like google::protobuf::descriptor_pool of protobuf cpp library */
43typedef struct {
44 GQueue* source_paths; /* the directories in which to search for proto file */
45 pbl_report_error_cb_t error_cb; /* error call back function */
46 GHashTable* packages; /* all packages parsed from proto files */
47 GHashTable* proto_files; /* all proto files that are parsed or to be parsed */
48 GQueue* proto_files_to_be_parsed; /* files is to be parsed */
49 struct _protobuf_lang_state_t *parser_state; /* current parser state */
51
52/* file descriptor */
53typedef struct {
54 const char* filename;
55 int syntax_version;
56 const char* package_name;
57 int package_name_lineno;
60
61/* Basic information of node */
62typedef struct pbl_node_t{
63 pbl_node_type_t nodetype;
64 char* name;
65 char* full_name; /* constructed during first access */
66 struct pbl_node_t* parent;
67 GQueue* children; /* child is a pbl_node_t */
68 GHashTable* children_by_name; /* take children names as keys */
70 int lineno;
72
73/* like google::protobuf::MethodDescriptor of protobuf cpp library */
74typedef struct {
75 pbl_node_t basic_info;
76 char* in_msg_type;
77 bool in_is_stream;
78 char* out_msg_type;
79 bool out_is_stream;
81
82/* like google::protobuf::Descriptor of protobuf cpp library */
83typedef struct {
84 pbl_node_t basic_info;
85 GQueue* fields;
86 GHashTable* fields_by_number;
88
89/* like google::protobuf::EnumValueDescriptor of protobuf cpp library */
90typedef struct {
91 pbl_node_t basic_info;
92 int number;
94
131
132/* like google::protobuf::EnumDescriptor of protobuf cpp library */
133typedef struct {
134 pbl_node_t basic_info;
135 GQueue* values;
136 GHashTable* values_by_number;
138
139/* Option node. The name of basic_info is optionName.
140 Now, we only care about fieldOption. */
141typedef struct {
142 pbl_node_t basic_info;
143 char* value;
145
146/* the struct of token used by the parser */
148 char* v; /* token string value */
149 int ln; /* line number of this token in the .proto file */
151
152/* parser state */
154 pbl_descriptor_pool_t* pool; /* pool will keep the parsing result */
155 pbl_file_descriptor_t* file; /* info of current parsing file */
156 GSList* lex_string_tokens;
157 GSList* lex_struct_tokens;
158 void* scanner;
159 void* pParser;
160 bool grammar_error;
161 protobuf_lang_token_t* tmp_token; /* just for passing token value from protobuf_lang_lex() to ProtobufLangParser() */
163
164/* Store chars created by strdup or g_strconcat into protobuf_lang_state_t temporarily,
165 and return back the input chars pointer.
166 It will be freed when protobuf_lang_state_t is released */
167
175static inline char*
176pbl_store_string_token(protobuf_lang_state_t* parser_state, char* dupstr)
177{
178 parser_state->lex_string_tokens = g_slist_prepend(parser_state->lex_string_tokens, dupstr);
179 return dupstr;
180}
181
182/* Store a protobuf_lang_token_t in protobuf_lang_state_t temporarily, and return back
183 the input pointer. It will be freed when protobuf_lang_state_t is released */
184static inline protobuf_lang_token_t*
185pbl_store_struct_token(protobuf_lang_state_t* parser_state, protobuf_lang_token_t* newtoken)
186{
187 parser_state->lex_struct_tokens = g_slist_prepend(parser_state->lex_struct_tokens, newtoken);
188 return newtoken;
189}
190
191/* default error_cb */
192
198static inline void
199pbl_printf(const char* fmt, ...)
200{
201 va_list ap;
202 va_start(ap, fmt);
203 vprintf(fmt, ap);
204 va_end(ap);
205}
206
212void
213pbl_reinit_descriptor_pool(pbl_descriptor_pool_t** ppool, const char** directories, pbl_report_error_cb_t error_cb);
214
215/* free all memory used by this protocol buffers language pool */
221void
223
224/* add a proto file to pool. this file will not be parsed until run_pbl_parser function is invoked. */
231bool
232pbl_add_proto_file_to_be_parsed(pbl_descriptor_pool_t* pool, const char* filepath);
233
234/* run C protocol buffers language parser, return 0 if success */
235
243
244/* like descriptor_pool::FindMethodByName */
254
255/* like MethodDescriptor::name() */
262const char*
264
265/* like MethodDescriptor::full_name() */
272const char*
274
275/* like MethodDescriptor::input_type() */
284
285/* like MethodDescriptor::output_type() */
294
295/* like descriptor_pool::FindMessageTypeByName() */
305
306/* like Descriptor::name() */
313const char*
315
316/* like Descriptor::full_name() */
323const char*
325
326/* like Descriptor::field_count() */
333int
335
336/* like Descriptor::field() */
345pbl_message_descriptor_field(const pbl_message_descriptor_t* message, int field_index);
346
347/* like Descriptor::FindFieldByNumber() */
357
358/* like Descriptor::FindFieldByName() */
368
369/* like FieldDescriptor::full_name() */
376const char*
378
379/* like FieldDescriptor::name() */
386const char*
388
389/* like FieldDescriptor::number() */
396int
398
399/* like FieldDescriptor::type() */
406int
408
409/* like FieldDescriptor::is_repeated() */
416int
418
419/* like FieldDescriptor::is_packed() */
426int
427
429
430/* like FieldDescriptor::TypeName() */
438const char*
439pbl_field_descriptor_TypeName(wmem_allocator_t* scope, int field_type);
440
441/* like FieldDescriptor::message_type() */
449
450/* like FieldDescriptor::enum_type() */
459
460/* like FieldDescriptor::is_required() */
467bool
469
470/* like FieldDescriptor::has_default_value().
471 * Does this field have an explicitly-declared default value? */
478bool
480
481/* like FieldDescriptor::default_value_int32() */
488int32_t
490
491/* like FieldDescriptor::default_value_int64() */
498int64_t
500
501/* like FieldDescriptor::default_value_uint32() */
508uint32_t
510
511/* like FieldDescriptor::default_value_uint64() */
518uint64_t
520
521/* like FieldDescriptor::default_value_float() */
528float
530
531/* like FieldDescriptor::default_value_double() */
538double
540
541/* like FieldDescriptor::default_value_bool() */
548bool
550
551/* like FieldDescriptor::default_value_string() */
559const char*
561
562/* like FieldDescriptor::default_value_enum() */
571
572/* like EnumDescriptor::name() */
579const char*
581
582/* like EnumDescriptor::full_name() */
589const char*
591
592/* like EnumDescriptor::value_count() */
599int
601
602/* like EnumDescriptor::value() */
611pbl_enum_descriptor_value(const pbl_enum_descriptor_t* anEnum, int value_index);
612
613/* like EnumDescriptor::FindValueByNumber() */
623
624/* like EnumDescriptor::FindValueByName() */
633pbl_enum_descriptor_FindValueByName(const pbl_enum_descriptor_t* anEnum, const char* name);
634
635/* like EnumValueDescriptor::name() */
642const char*
644
645/* like EnumValueDescriptor::full_name() */
652const char*
654
655/* like EnumValueDescriptor::number() */
662int
664
665/* visit all message in this pool */
673void
674pbl_foreach_message(const pbl_descriptor_pool_t* pool, void (*cb)(const pbl_message_descriptor_t*, void*), void* userdata);
675
676/*
677 * Following are tree building functions.
678 */
679
680/* create a normal node */
691pbl_create_node(pbl_file_descriptor_t* file, int lineno, pbl_node_type_t nodetype, const char* name);
692
693/* change the name of node */
703pbl_set_node_name(pbl_node_t* node, int lineno, const char* newname);
704
705/* get the name of node */
712static inline const char*
713pbl_get_node_name(pbl_node_t* node)
714{
715 return node->name;
716}
717
718/* get the full name of node. if it is NULL, it will be built. */
725const char*
727
728/* append a node as a child of the parent node, and return the parent pointer */
737pbl_add_child(pbl_node_t* parent, pbl_node_t* child);
738
739/* create an enumeration field node */
740
751pbl_create_enum_value_node(pbl_file_descriptor_t* file, int lineno, const char* name, int number);
752
753/* merge one('from') node's children to another('to') node, and return the 'to' pointer */
766
767/* create a field node */
781pbl_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);
782
783/* create a map field node */
795pbl_create_map_field_node(pbl_file_descriptor_t* file, int lineno, const char* name, int number, pbl_node_t* options);
796
797/* create a method (rpc or stream of service) node */
811pbl_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);
812
813/* create an option node */
814
825pbl_create_option_node(pbl_file_descriptor_t* file, int lineno, const char* name, const char* value);
826
827/* free a pbl_node_t and its children. */
836void
837pbl_free_node(void *anode);
838
839#ifdef __cplusplus
840}
841#endif /* __cplusplus */
842
843/*
844 * Editor modelines - https://www.wireshark.org/tools/modelines.html
845 *
846 * Local variables:
847 * c-basic-offset: 4
848 * tab-width: 8
849 * indent-tabs-mode: nil
850 * End:
851 *
852 * vi: set shiftwidth=4 tabstop=8 expandtab:
853 * :indentSize=4:tabSize=8:noTabs=true:
854 */
const pbl_field_descriptor_t * pbl_message_descriptor_FindFieldByNumber(const pbl_message_descriptor_t *message, int number)
Finds a field descriptor by its number in a message descriptor.
Definition protobuf_lang_tree.c:465
int pbl_field_descriptor_type(const pbl_field_descriptor_t *field)
Get the type of a field descriptor.
Definition protobuf_lang_tree.c:508
uint64_t pbl_field_descriptor_default_value_uint64(const pbl_field_descriptor_t *field)
Retrieves the default value of a field descriptor as a 64-bit unsigned integer.
Definition protobuf_lang_tree.c:640
int pbl_field_descriptor_is_repeated(const pbl_field_descriptor_t *field)
Checks if a field descriptor is repeated.
Definition protobuf_lang_tree.c:529
bool pbl_field_descriptor_default_value_bool(const pbl_field_descriptor_t *field)
Retrieves the default value of a boolean field descriptor.
Definition protobuf_lang_tree.c:661
pbl_node_t * 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)
Create a field node for a Protocol Buffers message.
Definition protobuf_lang_tree.c:892
pbl_node_t * pbl_create_option_node(pbl_file_descriptor_t *file, int lineno, const char *name, const char *value)
Creates a new option node for Protocol Buffers Language Tree.
Definition protobuf_lang_tree.c:998
int run_pbl_parser(pbl_descriptor_pool_t *pool)
Runs the Protocol Buffers Language parser.
pbl_node_t * pbl_create_enum_value_node(pbl_file_descriptor_t *file, int lineno, const char *name, int number)
Create a new enum value node.
Definition protobuf_lang_tree.c:988
const pbl_enum_descriptor_t * pbl_field_descriptor_enum_type(const pbl_field_descriptor_t *field)
Get the enum descriptor for a field.
Definition protobuf_lang_tree.c:593
void pbl_foreach_message(const pbl_descriptor_pool_t *pool, void(*cb)(const pbl_message_descriptor_t *, void *), void *userdata)
Iterates over all message descriptors in a descriptor pool.
Definition protobuf_lang_tree.c:791
bool pbl_field_descriptor_is_required(const pbl_field_descriptor_t *field)
Checks if a field descriptor is required.
Definition protobuf_lang_tree.c:605
const pbl_enum_value_descriptor_t * pbl_enum_descriptor_FindValueByNumber(const pbl_enum_descriptor_t *anEnum, int number)
Finds an enumeration value descriptor by its number.
Definition protobuf_lang_tree.c:725
const char * pbl_message_descriptor_name(const pbl_message_descriptor_t *message)
Retrieves the name of a message descriptor.
Definition protobuf_lang_tree.c:437
const pbl_method_descriptor_t * pbl_message_descriptor_pool_FindMethodByName(const pbl_descriptor_pool_t *pool, const char *name)
Finds a method descriptor by its name in a message descriptor pool.
Definition protobuf_lang_tree.c:391
const char * pbl_field_descriptor_default_value_string(const pbl_field_descriptor_t *field, int *size)
Get the default value string of a Protocol Buffers field descriptor.
Definition protobuf_lang_tree.c:668
const char * pbl_method_descriptor_full_name(const pbl_method_descriptor_t *method)
Retrieves the full name of a method descriptor.
Definition protobuf_lang_tree.c:406
const char * pbl_field_descriptor_name(const pbl_field_descriptor_t *field)
Retrieves the name of a field descriptor.
Definition protobuf_lang_tree.c:494
void pbl_free_pool(pbl_descriptor_pool_t *pool)
Free a descriptor pool and its associated resources.
Definition protobuf_lang_tree.c:129
const pbl_field_descriptor_t * pbl_message_descriptor_FindFieldByName(const pbl_message_descriptor_t *message, const char *name)
Finds a field descriptor by name in a message descriptor.
Definition protobuf_lang_tree.c:476
void pbl_free_node(void *anode)
Frees a protocol buffer language tree node.
Definition protobuf_lang_tree.c:1155
double pbl_field_descriptor_default_value_double(const pbl_field_descriptor_t *field)
Retrieves the default value of a double field descriptor.
Definition protobuf_lang_tree.c:654
const char * pbl_get_node_full_name(pbl_node_t *node)
Get the full name of a node.
Definition protobuf_lang_tree.c:296
const pbl_enum_value_descriptor_t * pbl_enum_descriptor_FindValueByName(const pbl_enum_descriptor_t *anEnum, const char *name)
Finds an enumeration value descriptor by name within a given enumeration descriptor.
Definition protobuf_lang_tree.c:736
const char * pbl_field_descriptor_full_name(const pbl_field_descriptor_t *field)
Retrieves the full name of a field descriptor.
Definition protobuf_lang_tree.c:487
pbl_node_t * pbl_create_node(pbl_file_descriptor_t *file, int lineno, pbl_node_type_t nodetype, const char *name)
Creates a new node in the Protocol Buffers language tree.
Definition protobuf_lang_tree.c:817
pbl_node_t * pbl_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)
Create a method node for Protocol Buffers.
Definition protobuf_lang_tree.c:863
int pbl_message_descriptor_field_count(const pbl_message_descriptor_t *message)
Get the count of fields in a message descriptor.
Definition protobuf_lang_tree.c:451
uint32_t pbl_field_descriptor_default_value_uint32(const pbl_field_descriptor_t *field)
Retrieves the default value of a field descriptor as a uint32.
Definition protobuf_lang_tree.c:633
int pbl_enum_value_descriptor_number(const pbl_enum_value_descriptor_t *enumValue)
Retrieves the number associated with an enum value descriptor.
Definition protobuf_lang_tree.c:761
const char * pbl_enum_value_descriptor_full_name(const pbl_enum_value_descriptor_t *enumValue)
Get the full name of an enum value descriptor.
Definition protobuf_lang_tree.c:754
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
const char * pbl_method_descriptor_name(const pbl_method_descriptor_t *method)
Retrieves the name of a method descriptor.
Definition protobuf_lang_tree.c:399
int64_t pbl_field_descriptor_default_value_int64(const pbl_field_descriptor_t *field)
Retrieves the default value of a field descriptor as an int64.
Definition protobuf_lang_tree.c:626
const pbl_enum_value_descriptor_t * pbl_enum_descriptor_value(const pbl_enum_descriptor_t *anEnum, int value_index)
Retrieves an enum value descriptor by its index.
Definition protobuf_lang_tree.c:718
pbl_node_t * pbl_merge_children(pbl_node_t *to, pbl_node_t *from)
Merges children from one node to another.
Definition protobuf_lang_tree.c:1104
bool pbl_field_descriptor_has_default_value(const pbl_field_descriptor_t *field)
Checks if a field descriptor has a default value.
Definition protobuf_lang_tree.c:612
const pbl_field_descriptor_t * pbl_message_descriptor_field(const pbl_message_descriptor_t *message, int field_index)
Retrieves a field descriptor from a message descriptor by index.
Definition protobuf_lang_tree.c:458
const char * pbl_field_descriptor_TypeName(wmem_allocator_t *scope, int field_type)
Retrieves the type name for a given field type.
Definition protobuf_lang_tree.c:574
pbl_node_t * pbl_set_node_name(pbl_node_t *node, int lineno, const char *newname)
Set the name of a node.
Definition protobuf_lang_tree.c:842
const char * pbl_message_descriptor_full_name(const pbl_message_descriptor_t *message)
Retrieves the full name of a message descriptor.
Definition protobuf_lang_tree.c:444
const pbl_message_descriptor_t * pbl_message_descriptor_pool_FindMessageTypeByName(const pbl_descriptor_pool_t *pool, const char *name)
Finds a message descriptor by its name in a descriptor pool.
Definition protobuf_lang_tree.c:429
const char * pbl_enum_descriptor_name(const pbl_enum_descriptor_t *anEnum)
Get the name of an enum descriptor.
Definition protobuf_lang_tree.c:697
const char * pbl_enum_value_descriptor_name(const pbl_enum_value_descriptor_t *enumValue)
Retrieves the name of an enum value descriptor.
Definition protobuf_lang_tree.c:747
int pbl_enum_descriptor_value_count(const pbl_enum_descriptor_t *anEnum)
Get the count of values in an enum descriptor.
Definition protobuf_lang_tree.c:711
const pbl_enum_value_descriptor_t * pbl_field_descriptor_default_value_enum(const pbl_field_descriptor_t *field)
Retrieves the default value enum for a field descriptor.
Definition protobuf_lang_tree.c:676
pbl_node_t * pbl_add_child(pbl_node_t *parent, pbl_node_t *child)
Adds a child node to a parent node.
Definition protobuf_lang_tree.c:1012
int pbl_field_descriptor_number(const pbl_field_descriptor_t *field)
Retrieves the number associated with a field descriptor.
Definition protobuf_lang_tree.c:501
const pbl_message_descriptor_t * pbl_method_descriptor_output_type(const pbl_method_descriptor_t *method)
Retrieves the output type of a method descriptor.
Definition protobuf_lang_tree.c:421
const pbl_message_descriptor_t * pbl_method_descriptor_input_type(const pbl_method_descriptor_t *method)
Retrieves the input message descriptor type for a method.
Definition protobuf_lang_tree.c:413
const char * pbl_enum_descriptor_full_name(const pbl_enum_descriptor_t *anEnum)
Get the full name of an enum descriptor.
Definition protobuf_lang_tree.c:704
bool pbl_add_proto_file_to_be_parsed(pbl_descriptor_pool_t *pool, const char *filepath)
Adds a Protocol Buffers file to be parsed.
Definition protobuf_lang_tree.c:185
pbl_node_t * pbl_create_map_field_node(pbl_file_descriptor_t *file, int lineno, const char *name, int number, pbl_node_t *options)
Create a map field node in the Protocol Buffers language tree.
Definition protobuf_lang_tree.c:971
const pbl_message_descriptor_t * pbl_field_descriptor_message_type(const pbl_field_descriptor_t *field)
Retrieves the message type descriptor for a field.
Definition protobuf_lang_tree.c:581
float pbl_field_descriptor_default_value_float(const pbl_field_descriptor_t *field)
Retrieves the default value of a float field descriptor.
Definition protobuf_lang_tree.c:647
int32_t pbl_field_descriptor_default_value_int32(const pbl_field_descriptor_t *field)
Retrieves the default value for an int32 field descriptor.
Definition protobuf_lang_tree.c:619
int pbl_field_descriptor_is_packed(const pbl_field_descriptor_t *field)
Checks if a field descriptor is packed.
Definition protobuf_lang_tree.c:536
Definition protobuf_lang_tree.h:153
Definition protobuf_lang_tree.h:147
Internal memory allocator interface used by the wmem subsystem.
Definition wmem_allocator.h:34
Definition protobuf_lang_tree.h:43
Definition protobuf_lang_tree.h:133
Definition protobuf_lang_tree.h:90
Describes a field in a Protocol Buffer message, similar to google::protobuf::FieldDescriptor.
Definition protobuf_lang_tree.h:101
bool has_default_value
Definition protobuf_lang_tree.h:110
pbl_node_t * options_node
Definition protobuf_lang_tree.h:106
pbl_node_t basic_info
Definition protobuf_lang_tree.h:102
double d
Definition protobuf_lang_tree.h:125
int64_t i64
Definition protobuf_lang_tree.h:121
bool b
Definition protobuf_lang_tree.h:126
char * type_name
Definition protobuf_lang_tree.h:105
uint64_t u64
Definition protobuf_lang_tree.h:123
int type
Definition protobuf_lang_tree.h:104
float f
Definition protobuf_lang_tree.h:124
char * s
Definition protobuf_lang_tree.h:127
char * orig_default_value
Definition protobuf_lang_tree.h:111
int32_t i32
Definition protobuf_lang_tree.h:120
bool is_required
Definition protobuf_lang_tree.h:109
const pbl_enum_value_descriptor_t * e
Definition protobuf_lang_tree.h:128
int string_or_bytes_default_value_length
Definition protobuf_lang_tree.h:112
bool is_repeated
Definition protobuf_lang_tree.h:108
int number
Definition protobuf_lang_tree.h:103
uint32_t u32
Definition protobuf_lang_tree.h:122
Definition protobuf_lang_tree.h:53
Definition protobuf_lang_tree.h:83
Definition protobuf_lang_tree.h:74
Definition protobuf_lang_tree.h:62
Definition protobuf_lang_tree.h:141