1515
1616#include " WiresharkBitfieldField.h"
1717
18+ #include " Wireshark.h"
1819#include " WiresharkGenerator.h"
20+ #include " WiresharkIntField.h"
21+
22+ #include " commsdsl/gen/util.h"
23+ #include " commsdsl/gen/strings.h"
24+ #include " commsdsl/parse/ParseIntField.h"
25+
26+ #include < cassert>
27+ #include < iomanip>
28+ #include < sstream>
29+ #include < type_traits>
30+
31+ namespace strings = commsdsl::gen::strings;
32+ namespace util = commsdsl::gen::util;
33+
34+ using ParseIntField = commsdsl::parse::ParseIntField;
1935
2036namespace commsdsl2wireshark
2137{
@@ -26,4 +42,120 @@ WiresharkBitfieldField::WiresharkBitfieldField(WiresharkGenerator& generator, Pa
2642{
2743}
2844
45+ std::string WiresharkBitfieldField::wiresharkForcedBitfieldMask (const WiresharkField& member) const
46+ {
47+ std::size_t bitOffset = 0U ;
48+ for (auto & mPtr : genMembers ()) {
49+ auto parseObj = mPtr ->genParseObj ();
50+ auto bitLen = parseObj.parseBitLength ();
51+ assert (bitLen != 0U );
52+ if (mPtr .get () == &member.wiresharkGenField ()) {
53+ auto mask = ((1ULL << bitLen) - 1U ) << bitOffset;
54+ std::stringstream stream;
55+ stream << " 0x" << std::hex << std::setfill (' 0' ) << std::setw (static_cast <int >(genParseObj ().parseMaxLength () * 2 )) << mask;
56+ return stream.str ();
57+ }
58+
59+ bitOffset += bitLen;
60+ }
61+
62+ [[maybe_unused]] static constexpr bool Should_not_happen = false ;
63+ assert (Should_not_happen);
64+ return strings::genNilStr ();
65+ }
66+
67+ std::string WiresharkBitfieldField::wiresharkIntegralType () const
68+ {
69+ using ParseType = ParseIntField::ParseType;
70+ static const ParseType TypeMap[] = {
71+ ParseType::Uint8,
72+ ParseType::Uint16,
73+ ParseType::Uint32,
74+ ParseType::Uint32,
75+ ParseType::Uint64,
76+ ParseType::Uint64,
77+ ParseType::Uint64,
78+ ParseType::Uint64,
79+ };
80+ static const std::size_t TypeMapSize = std::extent_v<decltype (TypeMap)>;
81+
82+ auto parseObj = genParseObj ();
83+ auto len = parseObj.parseMaxLength ();
84+ if (TypeMapSize < len) {
85+ [[maybe_unused]] static constexpr bool Should_not_happen = false ;
86+ assert (Should_not_happen);
87+ return strings::genEmptyString ();
88+ }
89+ auto idx = len - 1U ;
90+ return WiresharkIntField::wiresharkIntegralType (TypeMap[idx], len);
91+ }
92+
93+ unsigned WiresharkBitfieldField::wiresharkMaskShiftFor (const WiresharkField& member) const
94+ {
95+ unsigned bitOffset = 0U ;
96+ for (auto & mPtr : genMembers ()) {
97+ auto parseObj = mPtr ->genParseObj ();
98+ auto bitLen = parseObj.parseBitLength ();
99+ assert (bitLen != 0U );
100+ if (mPtr .get () == &member.wiresharkGenField ()) {
101+ return bitOffset;
102+ }
103+
104+ bitOffset += static_cast <decltype (bitOffset)>(bitLen);
105+ }
106+
107+ [[maybe_unused]] static constexpr bool Should_not_happen = false ;
108+ assert (Should_not_happen);
109+ return 0U ;
110+ }
111+
112+ bool WiresharkBitfieldField::genPrepareImpl ()
113+ {
114+ if (!GenBase::genPrepareImpl ()) {
115+ return false ;
116+ }
117+
118+ m_wiresharkFields = wiresharkTransformFieldsList (genMembers ());
119+ // TODO: force mask
120+ return true ;
121+ }
122+
123+ std::string WiresharkBitfieldField::wiresharkFieldRegistrationImpl () const
124+ {
125+ static const std::string Templ =
126+ " local #^#OBJ_NAME#$# = #^#CREATE_FUNC#$#(ProtoField.#^#TYPE#$#(\" #^#REF_NAME#$#\" , \" #^#DISP_NAME#$#\" , base.HEX, #^#NIL#$#, #^#MASK#$#, #^#DESC#$#))\n "
127+ ;
128+
129+ auto mask = wiresharkForcedIntegralFieldMask ();
130+ auto obj = genParseObj ();
131+ util::GenReplacementMap repl = {
132+ {" OBJ_NAME" , wiresharkFieldObjName ()},
133+ {" CREATE_FUNC" , Wireshark::wiresharkCreateFieldFuncName (WiresharkGenerator::wiresharkCast (genGenerator ()))},
134+ {" TYPE" , wiresharkIntegralType ()},
135+ {" REF_NAME" , wiresharkFieldRefName ()},
136+ {" DISP_NAME" , util::genDisplayName (obj.parseDisplayName (), obj.parseName ())},
137+ {" NIL" , strings::genNilStr ()},
138+ {" MASK" , wiresharkForcedIntegralFieldMask ()},
139+ {" DESC" , wiresharkFieldDescriptionStr ()},
140+ };
141+
142+ assert (!repl[" TYPE" ].empty ());
143+ return util::genProcessTemplate (Templ, repl);
144+ }
145+
146+ std::string WiresharkBitfieldField::wiresharkMembersDissectCodeImpl () const
147+ {
148+ util::GenStringsList elems;
149+ for (auto * f : m_wiresharkFields) {
150+ auto str = f->wiresharkDissectCode ();
151+ if (str.empty ()) {
152+ continue ;
153+ }
154+
155+ elems.push_back (std::move (str));
156+ }
157+
158+ return util::genStrListToString (elems, " \n " , " \n " );
159+ }
160+
29161} // namespace commsdsl2wireshark
0 commit comments