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>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
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.
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 official protobuf library is implemented in C++ language.
21 * One day, if C++ library is allowed, we can create a protobuf-helper.cpp file, that invoking official protobuf C++ library directly,
22 * to replace protobuf-helper.c. The packet-protobuf.c can keep unchanged.
27 #include "protobuf-helper.h"
28 #include "protobuf_lang_tree.h"
30 VALUE_STRING_ARRAY_GLOBAL_DEF(protobuf_field_type
);
33 pbw_reinit_DescriptorPool(PbwDescriptorPool
** pool
, const char** directories
, pbw_report_error_cb_t error_cb
)
35 pbl_reinit_descriptor_pool((pbl_descriptor_pool_t
**) pool
, directories
, (pbl_report_error_cb_t
) error_cb
);
38 /* load a proto file, return 0 if succeeds */
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
);
48 /* like DescriptorPool::FindMethodByName */
49 const PbwMethodDescriptor
*
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
);
54 /* like MethodDescriptor::name() */
56 pbw_MethodDescriptor_name(const PbwMethodDescriptor
* method
) {
57 return pbl_method_descriptor_name((const pbl_method_descriptor_t
*) method
);
60 /* like MethodDescriptor::full_name() */
62 pbw_MethodDescriptor_full_name(const PbwMethodDescriptor
* method
) {
63 return pbl_method_descriptor_full_name((const pbl_method_descriptor_t
*) method
);
66 /* like MethodDescriptor::input_type() */
68 pbw_MethodDescriptor_input_type(const PbwMethodDescriptor
* method
) {
69 return (const PbwDescriptor
*)pbl_method_descriptor_input_type((const pbl_method_descriptor_t
*) method
);
72 /* like MethodDescriptor::output_type() */
74 pbw_MethodDescriptor_output_type(const PbwMethodDescriptor
* method
) {
75 return (const PbwDescriptor
*)pbl_method_descriptor_output_type((const pbl_method_descriptor_t
*) method
);
78 /* like DescriptorPool::FindMessageTypeByName() */
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
);
84 /* like Descriptor::full_name() */
86 pbw_Descriptor_full_name(const PbwDescriptor
* message
) {
87 return pbl_message_descriptor_full_name((const pbl_message_descriptor_t
*) message
);
90 /* like Descriptor::name() */
92 pbw_Descriptor_name(const PbwDescriptor
* message
) {
93 return pbl_message_descriptor_name((const pbl_message_descriptor_t
*) message
);
96 /* like Descriptor::field_count() */
98 pbw_Descriptor_field_count(const PbwDescriptor
* message
) {
99 return pbl_message_descriptor_field_count((const pbl_message_descriptor_t
*) message
);
102 /* like Descriptor::field() */
103 const PbwFieldDescriptor
*
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
);
108 /* like Descriptor::FindFieldByNumber() */
109 const PbwFieldDescriptor
*
110 pbw_Descriptor_FindFieldByNumber(const PbwDescriptor
* message
, int number
) {
111 return (const PbwFieldDescriptor
*)pbl_message_descriptor_FindFieldByNumber((const pbl_message_descriptor_t
*) message
, number
);
114 /* like Descriptor::FindFieldByName() */
115 const PbwFieldDescriptor
*
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
);
120 /* like FieldDescriptor::full_name() */
122 pbw_FieldDescriptor_full_name(const PbwFieldDescriptor
* field
) {
123 return pbl_field_descriptor_full_name((const pbl_field_descriptor_t
*) field
);
126 /* like FieldDescriptor::name() */
128 pbw_FieldDescriptor_name(const PbwFieldDescriptor
* field
) {
129 return pbl_field_descriptor_name((const pbl_field_descriptor_t
*) field
);
132 /* like FieldDescriptor::number() */
134 pbw_FieldDescriptor_number(const PbwFieldDescriptor
* field
) {
135 return pbl_field_descriptor_number((const pbl_field_descriptor_t
*) field
);
138 /* like FieldDescriptor::type() */
140 pbw_FieldDescriptor_type(const PbwFieldDescriptor
* field
) {
141 return pbl_field_descriptor_type((const pbl_field_descriptor_t
*) field
);
144 /* like FieldDescriptor::is_repeated() */
146 pbw_FieldDescriptor_is_repeated(const PbwFieldDescriptor
* field
) {
147 return pbl_field_descriptor_is_repeated((const pbl_field_descriptor_t
*) field
);
150 /* like FieldDescriptor::is_packed() */
152 pbw_FieldDescriptor_is_packed(const PbwFieldDescriptor
* field
) {
153 return pbl_field_descriptor_is_packed((const pbl_field_descriptor_t
*) field
);
156 /* like FieldDescriptor::typeName() */
158 pbw_FieldDescriptor_typeName(int field_type
) {
159 return pbl_field_descriptor_TypeName(field_type
);
162 /* like FieldDescriptor::message_type() */
164 pbw_FieldDescriptor_message_type(const PbwFieldDescriptor
* field
) {
165 return (const PbwDescriptor
*)pbl_field_descriptor_message_type((const pbl_field_descriptor_t
*) field
);
168 /* like FieldDescriptor::enum_type() */
169 const PbwEnumDescriptor
*
170 pbw_FieldDescriptor_enum_type(const PbwFieldDescriptor
* field
) {
171 return (const PbwEnumDescriptor
*)pbl_field_descriptor_enum_type((const pbl_field_descriptor_t
*) field
);
174 /* like FieldDescriptor::is_required() */
176 pbw_FieldDescriptor_is_required(const PbwFieldDescriptor
* field
) {
177 return pbl_field_descriptor_is_required((const pbl_field_descriptor_t
*)field
);
180 /* like FieldDescriptor::has_default_value() */
182 pbw_FieldDescriptor_has_default_value(const PbwFieldDescriptor
* field
) {
183 return pbl_field_descriptor_has_default_value((const pbl_field_descriptor_t
*)field
);
186 /* like FieldDescriptor::default_value_int32() */
188 pbw_FieldDescriptor_default_value_int32(const PbwFieldDescriptor
* field
) {
189 return pbl_field_descriptor_default_value_int32((const pbl_field_descriptor_t
*)field
);
192 /* like FieldDescriptor::default_value_int64() */
194 pbw_FieldDescriptor_default_value_int64(const PbwFieldDescriptor
* field
) {
195 return pbl_field_descriptor_default_value_int64((const pbl_field_descriptor_t
*)field
);
198 /* like FieldDescriptor::default_value_uint32() */
200 pbw_FieldDescriptor_default_value_uint32(const PbwFieldDescriptor
* field
) {
201 return pbl_field_descriptor_default_value_uint32((const pbl_field_descriptor_t
*)field
);
204 /* like FieldDescriptor::default_value_uint64() */
206 pbw_FieldDescriptor_default_value_uint64(const PbwFieldDescriptor
* field
) {
207 return pbl_field_descriptor_default_value_uint64((const pbl_field_descriptor_t
*)field
);
210 /* like FieldDescriptor::default_value_float() */
212 pbw_FieldDescriptor_default_value_float(const PbwFieldDescriptor
* field
) {
213 return pbl_field_descriptor_default_value_float((const pbl_field_descriptor_t
*)field
);
216 /* like FieldDescriptor::default_value_double() */
218 pbw_FieldDescriptor_default_value_double(const PbwFieldDescriptor
* field
) {
219 return pbl_field_descriptor_default_value_double((const pbl_field_descriptor_t
*)field
);
222 /* like FieldDescriptor::default_value_bool() */
224 pbw_FieldDescriptor_default_value_bool(const PbwFieldDescriptor
* field
) {
225 return pbl_field_descriptor_default_value_bool((const pbl_field_descriptor_t
*)field
);
228 /* like FieldDescriptor::default_value_string() */
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
);
234 /* like FieldDescriptor::default_value_enum() */
235 const PbwEnumValueDescriptor
*
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
);
240 /* like EnumDescriptor::name() */
242 pbw_EnumDescriptor_name(const PbwEnumDescriptor
* anEnum
) {
243 return pbl_enum_descriptor_name((const pbl_enum_descriptor_t
*) anEnum
);
246 /* like EnumDescriptor::full_name() */
248 pbw_EnumDescriptor_full_name(const PbwEnumDescriptor
* anEnum
) {
249 return pbl_enum_descriptor_full_name((const pbl_enum_descriptor_t
*) anEnum
);
252 /* like EnumDescriptor::value_count() */
254 pbw_EnumDescriptor_value_count(const PbwEnumDescriptor
* anEnum
) {
255 return pbl_enum_descriptor_value_count((const pbl_enum_descriptor_t
*) anEnum
);
258 /* like EnumDescriptor::value() */
259 const PbwEnumValueDescriptor
*
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
);
264 /* like EnumDescriptor::FindValueByNumber() */
265 const PbwEnumValueDescriptor
*
266 pbw_EnumDescriptor_FindValueByNumber(const PbwEnumDescriptor
* anEnum
, int number
) {
267 return (const PbwEnumValueDescriptor
*)pbl_enum_descriptor_FindValueByNumber((const pbl_enum_descriptor_t
*) anEnum
, number
);
270 /* like EnumDescriptor::FindValueByName() */
271 const PbwEnumValueDescriptor
*
272 pbw_EnumDescriptor_FindValueByName(const PbwEnumDescriptor
* anEnum
, const char* name
) {
273 return (const PbwEnumValueDescriptor
*)pbl_enum_descriptor_FindValueByName((const pbl_enum_descriptor_t
*)anEnum
, name
);
276 /* like EnumValueDescriptor::name() */
278 pbw_EnumValueDescriptor_name(const PbwEnumValueDescriptor
* enumValue
) {
279 return pbl_enum_value_descriptor_name((const pbl_enum_value_descriptor_t
*) enumValue
);
282 /* like EnumValueDescriptor::full_name() */
284 pbw_EnumValueDescriptor_full_name(const PbwEnumValueDescriptor
* enumValue
) {
285 return pbl_enum_value_descriptor_full_name((const pbl_enum_value_descriptor_t
*) enumValue
);
288 /* like EnumValueDescriptor::number() */
290 pbw_EnumValueDescriptor_number(const PbwEnumValueDescriptor
* enumValue
) {
291 return pbl_enum_value_descriptor_number((const pbl_enum_value_descriptor_t
*) enumValue
);
294 /* visit all messages of this pool */
296 pbw_foreach_message(const PbwDescriptorPool
* pool
, void (*cb
)(const PbwDescriptor
* message
, void* userdata
), void* userdata
)
298 pbl_foreach_message((const pbl_descriptor_pool_t
*) pool
, (void (*)(const pbl_message_descriptor_t
*, void*)) cb
, userdata
);
302 * Editor modelines - https://www.wireshark.org/tools/modelines.html
307 * indent-tabs-mode: nil
310 * vi: set shiftwidth=4 tabstop=8 expandtab:
311 * :indentSize=4:tabSize=8:noTabs=true: