1 /* protobuf-helper.c
2  *
3  * Wrapper of Protocol Buffers Language library which generated by protobuf_lang_parser.lemon and protobuf_lang_scanner.l.
4  * Copyright 2019, Huang Qiangxiong <qiangxiong.huang@qq.com>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12 
13 /* The packet-protobuf dissector needs get information from *.proto files for dissecting
14  * protobuf packet correctly. The information includes:
15  *   - The names of MESSAGE, ENUM, FIELD, ENUM_VALUE;
16  *   - The data type of FIELD which assuring the value of protobuf field of packet can be dissected correctly.
17  *
18  * At present, we use C Protocol Buffers Language Parser which generated by protobuf_lang_parser.lemon and protobuf_lang_scanner.l.
19  * Because wireshark is mainly implemented in plain ANSI C but the offical protobuf library is implemented in C++ language.
20  *
21  * One day, if C++ library is allowd, we can create a protobuf-helper.cpp file, that invoking offical protobuf C++ library directly,
22  * to replace protobuf-helper.c. The packet-protobuf.c can keep unchanged.
23  */
24 
25 #include "config.h"
26 
27 #include "protobuf-helper.h"
28 #include "protobuf_lang_tree.h"
29 
30 VALUE_STRING_ARRAY_GLOBAL_DEF(protobuf_field_type);
31 
32 void
pbw_reinit_DescriptorPool(PbwDescriptorPool ** pool,const char ** directories,pbw_report_error_cb_t error_cb)33 pbw_reinit_DescriptorPool(PbwDescriptorPool** pool, const char** directories, pbw_report_error_cb_t error_cb)
34 {
35     pbl_reinit_descriptor_pool((pbl_descriptor_pool_t**) pool, directories, (pbl_report_error_cb_t) error_cb);
36 }
37 
38 /* load a proto file, return 0 if successed */
39 int
pbw_load_proto_file(PbwDescriptorPool * pool,const char * filename)40 pbw_load_proto_file(PbwDescriptorPool* pool, const char* filename) {
41     if (pbl_add_proto_file_to_be_parsed((pbl_descriptor_pool_t*) pool, filename)) {
42         return run_pbl_parser((pbl_descriptor_pool_t*) pool);
43     } else {
44         return 2;
45     }
46 }
47 
48 /* like DescriptorPool::FindMethodByName */
49 const PbwMethodDescriptor*
pbw_DescriptorPool_FindMethodByName(const PbwDescriptorPool * pool,const char * name)50 pbw_DescriptorPool_FindMethodByName(const PbwDescriptorPool* pool, const char* name) {
51     return (const PbwMethodDescriptor*)pbl_message_descriptor_pool_FindMethodByName((pbl_descriptor_pool_t*) pool, name);
52 }
53 
54 /* like MethodDescriptor::name() */
55 const char*
pbw_MethodDescriptor_name(const PbwMethodDescriptor * method)56 pbw_MethodDescriptor_name(const PbwMethodDescriptor* method) {
57     return pbl_method_descriptor_name((const pbl_method_descriptor_t*) method);
58 }
59 
60 /* like MethodDescriptor::full_name() */
61 const char*
pbw_MethodDescriptor_full_name(const PbwMethodDescriptor * method)62 pbw_MethodDescriptor_full_name(const PbwMethodDescriptor* method) {
63     return pbl_method_descriptor_full_name((const pbl_method_descriptor_t*) method);
64 }
65 
66 /* like MethodDescriptor::input_type() */
67 const PbwDescriptor*
pbw_MethodDescriptor_input_type(const PbwMethodDescriptor * method)68 pbw_MethodDescriptor_input_type(const PbwMethodDescriptor* method) {
69     return (const PbwDescriptor*)pbl_method_descriptor_input_type((const pbl_method_descriptor_t*) method);
70 }
71 
72 /* like MethodDescriptor::output_type() */
73 const PbwDescriptor*
pbw_MethodDescriptor_output_type(const PbwMethodDescriptor * method)74 pbw_MethodDescriptor_output_type(const PbwMethodDescriptor* method) {
75     return (const PbwDescriptor*)pbl_method_descriptor_output_type((const pbl_method_descriptor_t*) method);
76 }
77 
78 /* like DescriptorPool::FindMessageTypeByName() */
79 const PbwDescriptor*
pbw_DescriptorPool_FindMessageTypeByName(const PbwDescriptorPool * pool,const char * name)80 pbw_DescriptorPool_FindMessageTypeByName(const PbwDescriptorPool* pool, const char* name) {
81     return (const PbwDescriptor*)pbl_message_descriptor_pool_FindMessageTypeByName((const pbl_descriptor_pool_t*) pool, name);
82 }
83 
84 /* like Descriptor::full_name() */
85 const char*
pbw_Descriptor_full_name(const PbwDescriptor * message)86 pbw_Descriptor_full_name(const PbwDescriptor* message) {
87     return pbl_message_descriptor_full_name((const pbl_message_descriptor_t*) message);
88 }
89 
90 /* like Descriptor::name() */
91 const char*
pbw_Descriptor_name(const PbwDescriptor * message)92 pbw_Descriptor_name(const PbwDescriptor* message) {
93     return pbl_message_descriptor_name((const pbl_message_descriptor_t*) message);
94 }
95 
96 /* like Descriptor::field_count() */
97 int
pbw_Descriptor_field_count(const PbwDescriptor * message)98 pbw_Descriptor_field_count(const PbwDescriptor* message) {
99     return pbl_message_descriptor_field_count((const pbl_message_descriptor_t*) message);
100 }
101 
102 /* like Descriptor::field() */
103 const PbwFieldDescriptor*
pbw_Descriptor_field(const PbwDescriptor * message,int field_index)104 pbw_Descriptor_field(const PbwDescriptor* message, int field_index) {
105     return (const PbwFieldDescriptor*)pbl_message_descriptor_field((const pbl_message_descriptor_t*) message, field_index);
106 }
107 
108 /* like Descriptor::FindFieldByNumber() */
109 const PbwFieldDescriptor*
pbw_Descriptor_FindFieldByNumber(const PbwDescriptor * message,int number)110 pbw_Descriptor_FindFieldByNumber(const PbwDescriptor* message, int number) {
111     return (const PbwFieldDescriptor*)pbl_message_descriptor_FindFieldByNumber((const pbl_message_descriptor_t*) message, number);
112 }
113 
114 /* like Descriptor::FindFieldByName() */
115 const PbwFieldDescriptor*
pbw_Descriptor_FindFieldByName(const PbwDescriptor * message,const char * name)116 pbw_Descriptor_FindFieldByName(const PbwDescriptor* message, const char* name) {
117     return (const PbwFieldDescriptor*)pbl_message_descriptor_FindFieldByName((const pbl_message_descriptor_t*) message, name);
118 }
119 
120 /* like FieldDescriptor::full_name() */
121 const char*
pbw_FieldDescriptor_full_name(const PbwFieldDescriptor * field)122 pbw_FieldDescriptor_full_name(const PbwFieldDescriptor* field) {
123     return pbl_field_descriptor_full_name((const pbl_field_descriptor_t*) field);
124 }
125 
126 /* like FieldDescriptor::name() */
127 const char*
pbw_FieldDescriptor_name(const PbwFieldDescriptor * field)128 pbw_FieldDescriptor_name(const PbwFieldDescriptor* field) {
129     return pbl_field_descriptor_name((const pbl_field_descriptor_t*) field);
130 }
131 
132 /* like FieldDescriptor::number() */
133 int
pbw_FieldDescriptor_number(const PbwFieldDescriptor * field)134 pbw_FieldDescriptor_number(const PbwFieldDescriptor* field) {
135     return pbl_field_descriptor_number((const pbl_field_descriptor_t*) field);
136 }
137 
138 /* like FieldDescriptor::type() */
139 int
pbw_FieldDescriptor_type(const PbwFieldDescriptor * field)140 pbw_FieldDescriptor_type(const PbwFieldDescriptor* field) {
141     return pbl_field_descriptor_type((const pbl_field_descriptor_t*) field);
142 }
143 
144 /* like FieldDescriptor::is_repeated() */
145 int
pbw_FieldDescriptor_is_repeated(const PbwFieldDescriptor * field)146 pbw_FieldDescriptor_is_repeated(const PbwFieldDescriptor* field) {
147     return pbl_field_descriptor_is_repeated((const pbl_field_descriptor_t*) field);;
148 }
149 
150 /* like FieldDescriptor::is_packed() */
151 int
pbw_FieldDescriptor_is_packed(const PbwFieldDescriptor * field)152 pbw_FieldDescriptor_is_packed(const PbwFieldDescriptor* field) {
153     return pbl_field_descriptor_is_packed((const pbl_field_descriptor_t*) field);;
154 }
155 
156 /* like FieldDescriptor::typeName() */
157 const char*
pbw_FieldDescriptor_typeName(int field_type)158 pbw_FieldDescriptor_typeName(int field_type) {
159     return pbl_field_descriptor_TypeName(field_type);
160 }
161 
162 /* like FieldDescriptor::message_type() */
163 const PbwDescriptor*
pbw_FieldDescriptor_message_type(const PbwFieldDescriptor * field)164 pbw_FieldDescriptor_message_type(const PbwFieldDescriptor* field) {
165     return (const PbwDescriptor*)pbl_field_descriptor_message_type((const pbl_field_descriptor_t*) field);
166 }
167 
168 /* like FieldDescriptor::enum_type() */
169 const PbwEnumDescriptor*
pbw_FieldDescriptor_enum_type(const PbwFieldDescriptor * field)170 pbw_FieldDescriptor_enum_type(const PbwFieldDescriptor* field) {
171     return (const PbwEnumDescriptor*)pbl_field_descriptor_enum_type((const pbl_field_descriptor_t*) field);
172 }
173 
174 /* like FieldDescriptor::is_required() */
175 gboolean
pbw_FieldDescriptor_is_required(const PbwFieldDescriptor * field)176 pbw_FieldDescriptor_is_required(const PbwFieldDescriptor* field) {
177     return pbl_field_descriptor_is_required((const pbl_field_descriptor_t*)field);
178 }
179 
180 /* like FieldDescriptor::has_default_value() */
181 gboolean
pbw_FieldDescriptor_has_default_value(const PbwFieldDescriptor * field)182 pbw_FieldDescriptor_has_default_value(const PbwFieldDescriptor* field) {
183     return pbl_field_descriptor_has_default_value((const pbl_field_descriptor_t*)field);
184 }
185 
186 /* like FieldDescriptor::default_value_int32() */
187 gint32
pbw_FieldDescriptor_default_value_int32(const PbwFieldDescriptor * field)188 pbw_FieldDescriptor_default_value_int32(const PbwFieldDescriptor* field) {
189     return pbl_field_descriptor_default_value_int32((const pbl_field_descriptor_t*)field);
190 }
191 
192 /* like FieldDescriptor::default_value_int64() */
193 gint64
pbw_FieldDescriptor_default_value_int64(const PbwFieldDescriptor * field)194 pbw_FieldDescriptor_default_value_int64(const PbwFieldDescriptor* field) {
195     return pbl_field_descriptor_default_value_int64((const pbl_field_descriptor_t*)field);
196 }
197 
198 /* like FieldDescriptor::default_value_uint32() */
199 guint32
pbw_FieldDescriptor_default_value_uint32(const PbwFieldDescriptor * field)200 pbw_FieldDescriptor_default_value_uint32(const PbwFieldDescriptor* field) {
201     return pbl_field_descriptor_default_value_uint32((const pbl_field_descriptor_t*)field);
202 }
203 
204 /* like FieldDescriptor::default_value_uint64() */
205 guint64
pbw_FieldDescriptor_default_value_uint64(const PbwFieldDescriptor * field)206 pbw_FieldDescriptor_default_value_uint64(const PbwFieldDescriptor* field) {
207     return pbl_field_descriptor_default_value_uint64((const pbl_field_descriptor_t*)field);
208 }
209 
210 /* like FieldDescriptor::default_value_float() */
211 gfloat
pbw_FieldDescriptor_default_value_float(const PbwFieldDescriptor * field)212 pbw_FieldDescriptor_default_value_float(const PbwFieldDescriptor* field) {
213     return pbl_field_descriptor_default_value_float((const pbl_field_descriptor_t*)field);
214 }
215 
216 /* like FieldDescriptor::default_value_double() */
217 gdouble
pbw_FieldDescriptor_default_value_double(const PbwFieldDescriptor * field)218 pbw_FieldDescriptor_default_value_double(const PbwFieldDescriptor* field) {
219     return pbl_field_descriptor_default_value_double((const pbl_field_descriptor_t*)field);
220 }
221 
222 /* like FieldDescriptor::default_value_bool() */
223 gboolean
pbw_FieldDescriptor_default_value_bool(const PbwFieldDescriptor * field)224 pbw_FieldDescriptor_default_value_bool(const PbwFieldDescriptor* field) {
225     return pbl_field_descriptor_default_value_bool((const pbl_field_descriptor_t*)field);
226 }
227 
228 /* like FieldDescriptor::default_value_string() */
229 const gchar*
pbw_FieldDescriptor_default_value_string(const PbwFieldDescriptor * field,int * size)230 pbw_FieldDescriptor_default_value_string(const PbwFieldDescriptor* field, int* size) {
231     return pbl_field_descriptor_default_value_string((const pbl_field_descriptor_t*)field, size);
232 }
233 
234 /* like FieldDescriptor::default_value_enum() */
235 const PbwEnumValueDescriptor*
pbw_FieldDescriptor_default_value_enum(const PbwFieldDescriptor * field)236 pbw_FieldDescriptor_default_value_enum(const PbwFieldDescriptor* field) {
237     return (const PbwEnumValueDescriptor*) pbl_field_descriptor_default_value_enum((const pbl_field_descriptor_t*)field);
238 }
239 
240 /* like EnumDescriptor::name() */
241 const char*
pbw_EnumDescriptor_name(const PbwEnumDescriptor * anEnum)242 pbw_EnumDescriptor_name(const PbwEnumDescriptor* anEnum) {
243     return pbl_enum_descriptor_name((const pbl_enum_descriptor_t*) anEnum);
244 }
245 
246 /* like EnumDescriptor::full_name() */
247 const char*
pbw_EnumDescriptor_full_name(const PbwEnumDescriptor * anEnum)248 pbw_EnumDescriptor_full_name(const PbwEnumDescriptor* anEnum) {
249     return pbl_enum_descriptor_full_name((const pbl_enum_descriptor_t*) anEnum);
250 }
251 
252 /* like EnumDescriptor::value_count() */
253 int
pbw_EnumDescriptor_value_count(const PbwEnumDescriptor * anEnum)254 pbw_EnumDescriptor_value_count(const PbwEnumDescriptor* anEnum) {
255     return pbl_enum_descriptor_value_count((const pbl_enum_descriptor_t*) anEnum);
256 }
257 
258 /* like EnumDescriptor::value() */
259 const PbwEnumValueDescriptor*
pbw_EnumDescriptor_value(const PbwEnumDescriptor * anEnum,int value_index)260 pbw_EnumDescriptor_value(const PbwEnumDescriptor* anEnum, int value_index) {
261     return (const PbwEnumValueDescriptor*) pbl_enum_descriptor_value((const pbl_enum_descriptor_t*) anEnum, value_index);
262 }
263 
264 /* like EnumDescriptor::FindValueByNumber() */
265 const PbwEnumValueDescriptor*
pbw_EnumDescriptor_FindValueByNumber(const PbwEnumDescriptor * anEnum,int number)266 pbw_EnumDescriptor_FindValueByNumber(const PbwEnumDescriptor* anEnum, int number) {
267     return (const PbwEnumValueDescriptor*)pbl_enum_descriptor_FindValueByNumber((const pbl_enum_descriptor_t*) anEnum, number);
268 }
269 
270 /* like EnumDescriptor::FindValueByName() */
271 const PbwEnumValueDescriptor*
pbw_EnumDescriptor_FindValueByName(const PbwEnumDescriptor * anEnum,const gchar * name)272 pbw_EnumDescriptor_FindValueByName(const PbwEnumDescriptor* anEnum, const gchar* name) {
273     return (const PbwEnumValueDescriptor*)pbl_enum_descriptor_FindValueByName((const pbl_enum_descriptor_t*)anEnum, name);
274 }
275 
276 /* like EnumValueDescriptor::name() */
277 const char*
pbw_EnumValueDescriptor_name(const PbwEnumValueDescriptor * enumValue)278 pbw_EnumValueDescriptor_name(const PbwEnumValueDescriptor* enumValue) {
279     return pbl_enum_value_descriptor_name((const pbl_enum_value_descriptor_t*) enumValue);
280 }
281 
282 /* like EnumValueDescriptor::full_name() */
283 const char*
pbw_EnumValueDescriptor_full_name(const PbwEnumValueDescriptor * enumValue)284 pbw_EnumValueDescriptor_full_name(const PbwEnumValueDescriptor* enumValue) {
285     return pbl_enum_value_descriptor_full_name((const pbl_enum_value_descriptor_t*) enumValue);
286 }
287 
288 /* like EnumValueDescriptor::number() */
289 int
pbw_EnumValueDescriptor_number(const PbwEnumValueDescriptor * enumValue)290 pbw_EnumValueDescriptor_number(const PbwEnumValueDescriptor* enumValue) {
291     return pbl_enum_value_descriptor_number((const pbl_enum_value_descriptor_t*) enumValue);
292 }
293 
294 /* visit all messages of this pool */
295 void
pbw_foreach_message(const PbwDescriptorPool * pool,void (* cb)(const PbwDescriptor * message,void * userdata),void * userdata)296 pbw_foreach_message(const PbwDescriptorPool* pool, void (*cb)(const PbwDescriptor* message, void* userdata), void* userdata)
297 {
298     pbl_foreach_message((const pbl_descriptor_pool_t*) pool, (void (*)(const pbl_message_descriptor_t*, void*)) cb, userdata);
299 }
300 
301 /*
302  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
303  *
304  * Local variables:
305  * c-basic-offset: 4
306  * tab-width: 8
307  * indent-tabs-mode: nil
308  * End:
309  *
310  * vi: set shiftwidth=4 tabstop=8 expandtab:
311  * :indentSize=4:tabSize=8:noTabs=true:
312  */
313