Skip to content

Commit 5b4b726

Browse files
committed
Saving work on commsdsl2wireshark.
1 parent f43d43a commit 5b4b726

6 files changed

Lines changed: 182 additions & 16 deletions

File tree

app/commsdsl2wireshark/src/WiresharkBitfieldField.cpp

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,22 +169,100 @@ std::string WiresharkBitfieldField::wiresharkMembersDissectCodeImpl() const
169169
return util::genStrListToString(elems, "\n", "\n");
170170
}
171171

172-
std::string WiresharkBitfieldField::wiresharkDissectBodyImpl(const WiresharkField* refField) const
172+
std::string WiresharkBitfieldField::wiresharkDissectBodyImpl([[maybe_unused]] const WiresharkField* refField) const
173173
{
174-
// TODO
175-
static_cast<void>(refField);
176-
return "-- TODO: bitfield dissect body not implemented\n";
174+
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genGenerator());
175+
util::GenStringsList members;
176+
for (auto* f : m_wiresharkFields) {
177+
static const std::string MemTempl =
178+
"#^#RESULT#$#, #^#NEXT_OFFSET#$# = #^#DISSECT#$#(#^#TVB#$#, #^#SUBTREE#$#, #^#OFFSET#$#, #^#LIMIT#$#)\n"
179+
"if #^#RESULT#$# ~= #^#SUCCESS#$# then\n"
180+
" return #^#RESULT#$#, #^#OFFSET#$#\n"
181+
"end\n"
182+
;
183+
184+
util::GenReplacementMap memRepl = {
185+
{"RESULT", wiresharkResultStr()},
186+
{"NEXT_OFFSET", wiresharkNextOffsetStr()},
187+
{"DISSECT", f->wiresharkDissectName()},
188+
{"TVB", wiresharkTvbStr()},
189+
{"SUBTREE", wiresharkFieldSubtreeStr()},
190+
{"OFFSET", wiresharkOffsetStr()},
191+
{"LIMIT", wiresharkOffsetLimitStr()},
192+
{"SUCCESS", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::StatusCode::Success)},
193+
};
194+
195+
members.push_back(util::genProcessTemplate(MemTempl, memRepl));
196+
}
197+
198+
static const std::string Templ =
199+
"local #^#RANGE#$# = tvb(#^#OFFSET#$#, #^#LEN#$#)\n"
200+
"local #^#SUBTREE#$# = tree:add#^#SUFFIX#$#(field, #^#RANGE#$#)\n"
201+
"#^#MEMBERS#$#\n"
202+
"#^#NEXT_OFFSET#$# = #^#OFFSET#$# + #^#LEN#$#\n"
203+
;
204+
205+
auto parseObj = genBitfieldFieldParseObj();
206+
util::GenReplacementMap repl = {
207+
{"RANGE", wiresharkRangeStr()},
208+
{"LEN", std::to_string(parseObj.parseMaxLength())},
209+
{"SUBTREE", wiresharkFieldSubtreeStr()},
210+
{"OFFSET", wiresharkOffsetStr()},
211+
{"NEXT_OFFSET", wiresharkNextOffsetStr()},
212+
{"MEMBERS", util::genStrListToString(members, "\n", "")},
213+
};
214+
215+
if (parseObj.parseEndian() == commsdsl::parse::ParseEndian_Little) {
216+
repl["SUFFIX"] = strings::genLittleEndianSuffixStr();
217+
}
218+
219+
return util::genProcessTemplate(Templ, repl);
177220
}
178221

179222
std::string WiresharkBitfieldField::wiresharkValidFuncBodyImpl(const WiresharkField* refField) const
180223
{
181-
// TODO
182-
static_cast<void>(refField);
183-
return "-- TODO: bitfield valid body not implemented\n";
224+
util::GenStringsList members;
225+
for (auto* f : m_wiresharkFields) {
226+
if (f->wiresharkHasTrivialValid()) {
227+
continue;
228+
}
229+
230+
static const std::string MemTempl =
231+
"if not #^#FUNC#$#(#^#FIELD#$#) then\n"
232+
" return false\n"
233+
"endif\n"
234+
;
235+
236+
util::GenReplacementMap memRepl = {
237+
{"FUNC", f->wiresharkValidFuncName()},
238+
{"FIELD", f->wiresharkFieldObjName(refField)},
239+
};
240+
241+
members.push_back(util::genProcessTemplate(MemTempl, memRepl));
242+
}
243+
244+
static const std::string Templ =
245+
"#^#MEMBERS#$#\n"
246+
"#^#CONDS#$#\n"
247+
"return true\n"
248+
;
249+
250+
util::GenReplacementMap repl = {
251+
{"MEMBERS", util::genStrListToString(members, "\n", "")},
252+
{"CONDS", wiresharkExtraValidCondsCodeInternal()},
253+
};
254+
255+
return util::genProcessTemplate(Templ, repl);
184256
}
185257

186258
bool WiresharkBitfieldField::wiresharkHasTrivialValidImpl() const
187259
{
260+
auto parseObj = genBitfieldFieldParseObj();
261+
auto validCond = parseObj.parseValidCond();
262+
if (validCond.parseValid()) {
263+
return false;
264+
}
265+
188266
return
189267
std::all_of(
190268
m_wiresharkFields.begin(), m_wiresharkFields.end(),
@@ -194,4 +272,16 @@ bool WiresharkBitfieldField::wiresharkHasTrivialValidImpl() const
194272
});
195273
}
196274

275+
std::string WiresharkBitfieldField::wiresharkExtraValidCondsCodeInternal() const
276+
{
277+
auto parseObj = genBitfieldFieldParseObj();
278+
auto validCond = parseObj.parseValidCond();
279+
if (!validCond.parseValid()) {
280+
return strings::genEmptyString();
281+
}
282+
283+
// TODO:
284+
return "--TODO: implement check of extra conditions\n";
285+
}
286+
197287
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkBitfieldField.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class WiresharkBitfieldField final : public commsdsl::gen::GenBitfieldField, pub
4848
virtual bool wiresharkHasTrivialValidImpl() const override;
4949

5050
private:
51+
std::string wiresharkExtraValidCondsCodeInternal() const;
5152
WiresharkFieldsList m_wiresharkFields;
5253
};
5354

app/commsdsl2wireshark/src/WiresharkField.cpp

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ std::string WiresharkField::wiresharkFieldRegistration(const WiresharkField* ref
156156
return wiresharkFieldRegistrationImpl(refField);
157157
}
158158

159+
std::string WiresharkField::wiresharkValidFuncName() const
160+
{
161+
return wiresharkValidFuncNameImpl();
162+
}
163+
159164
const std::string& WiresharkField::wiresharkCustomNameCode(const WiresharkField* refField) const
160165
{
161166
if (refField != nullptr) {
@@ -204,6 +209,12 @@ std::string WiresharkField::wiresharkDissectNameImpl(const WiresharkField* refFi
204209
return wiresharkGenerator.wiresharkDissectNameFor(*genField);
205210
}
206211

212+
std::string WiresharkField::wiresharkValidFuncNameImpl() const
213+
{
214+
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(m_genField.genGenerator());
215+
return wiresharkGenerator.wiresharkFuncNameFor(m_genField, strings::genValidSuffixStr());
216+
}
217+
207218
std::string WiresharkField::wiresharkDissectCodeImpl(const WiresharkField* refField) const
208219
{
209220
if (comms::genIsGlobalField(m_genField) && (!m_genField.genIsReferenced())) {
@@ -554,6 +565,36 @@ const std::string& WiresharkField::wiresharkFieldStr()
554565
return Str;
555566
}
556567

568+
const std::string& WiresharkField::wiresharkNextOffsetStr()
569+
{
570+
static const std::string Str("next_offset");
571+
return Str;
572+
}
573+
574+
const std::string& WiresharkField::wiresharkOffsetStr()
575+
{
576+
static const std::string Str("offset");
577+
return Str;
578+
}
579+
580+
const std::string& WiresharkField::wiresharkOffsetLimitStr()
581+
{
582+
static const std::string Str("offset_limit");
583+
return Str;
584+
}
585+
586+
const std::string& WiresharkField::wiresharkResultStr()
587+
{
588+
static const std::string Str("result");
589+
return Str;
590+
}
591+
592+
const std::string& WiresharkField::wiresharkTvbStr()
593+
{
594+
static const std::string Str("tvb");
595+
return Str;
596+
}
597+
557598
bool WiresharkField::wiresharkCopyCodeFromInternal()
558599
{
559600
auto obj = m_genField.genParseObj();
@@ -704,7 +745,7 @@ std::string WiresharkField::wiresharkDissectValidCheckInternal() const
704745
;
705746

706747
util::GenReplacementMap repl = {
707-
{"VALID_FUNC", wiresharkValidFuncNameInternal()},
748+
{"VALID_FUNC", wiresharkValidFuncName()},
708749
{"SUBTREE", wiresharkFieldSubtreeStr()},
709750
{"FIELD", wiresharkFieldStr()}
710751
};
@@ -749,7 +790,7 @@ std::string WiresharkField::wiresharkValidFuncCodeInternal(const WiresharkField*
749790
bool replaced = false;
750791
bool extended = false;
751792
util::GenReplacementMap repl = {
752-
{"NAME", wiresharkValidFuncNameInternal()},
793+
{"NAME", wiresharkValidFuncName()},
753794
{"REPLACE", wiresharkGenerator.genReadCodeInjectCode(replaceFileName, "Replace this function body", &replaced)},
754795
{"EXTEND", wiresharkGenerator.genReadCodeInjectCode(extendFileName, "Extend function above", &extended)},
755796
{"FIELD", wiresharkFieldStr()},
@@ -766,12 +807,6 @@ std::string WiresharkField::wiresharkValidFuncCodeInternal(const WiresharkField*
766807
return util::genProcessTemplate(Templ, repl);
767808
}
768809

769-
std::string WiresharkField::wiresharkValidFuncNameInternal() const
770-
{
771-
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(m_genField.genGenerator());
772-
return wiresharkGenerator.wiresharkFuncNameFor(m_genField, strings::genValidSuffixStr());
773-
}
774-
775810
bool WiresharkField::wiresharkHasTrivialValidInternal() const
776811
{
777812
if (m_customCode.m_hasValid) {

app/commsdsl2wireshark/src/WiresharkField.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class WiresharkField
5555
std::string wiresharkExtractorsRegCode() const;
5656
std::string wiresharkFieldObjName(const WiresharkField* refField) const;
5757
std::string wiresharkFieldRegistration(const WiresharkField* refField = nullptr) const;
58+
std::string wiresharkValidFuncName() const;
5859

5960
const std::string& wiresharkCustomNameCode(const WiresharkField* refField = nullptr) const;
6061
bool wiresharkHasCustomNameCode(const WiresharkField* refField = nullptr) const;
@@ -66,6 +67,7 @@ class WiresharkField
6667

6768
protected:
6869
virtual std::string wiresharkDissectNameImpl(const WiresharkField* refField) const;
70+
virtual std::string wiresharkValidFuncNameImpl() const;
6971
virtual std::string wiresharkDissectCodeImpl(const WiresharkField* refField) const;
7072
virtual std::string wiresharkExtractorsRegCodeImpl() const;
7173
virtual std::string wiresharkFieldObjNameImpl(const WiresharkField* refField) const;
@@ -94,6 +96,11 @@ class WiresharkField
9496
static const std::string& wiresharkFieldSubtreeStr();
9597
static const std::string& wiresharkValStr();
9698
static const std::string& wiresharkFieldStr();
99+
static const std::string& wiresharkNextOffsetStr();
100+
static const std::string& wiresharkOffsetStr();
101+
static const std::string& wiresharkOffsetLimitStr();
102+
static const std::string& wiresharkResultStr();
103+
static const std::string& wiresharkTvbStr();
97104

98105
private:
99106
using WiresharkCustomCodeFunc = std::string (WiresharkField::*)(bool& hasCode) const;
@@ -124,7 +131,6 @@ class WiresharkField
124131
std::string wiresharkCustomNameCodeInternal(bool& hasRealCode) const;
125132
std::string wiresharkDissectValidCheckInternal() const;
126133
std::string wiresharkValidFuncCodeInternal(const WiresharkField* refField) const;
127-
std::string wiresharkValidFuncNameInternal() const;
128134
bool wiresharkHasTrivialValidInternal() const;
129135

130136
GenField& m_genField;

app/commsdsl2wireshark/src/WiresharkRefField.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ std::string WiresharkRefField::wiresharkDissectNameImpl(const WiresharkField* re
7777
return m_wiresharkField->wiresharkDissectName(refField);
7878
}
7979

80+
std::string WiresharkRefField::wiresharkValidFuncNameImpl() const
81+
{
82+
if (!m_alias) {
83+
return WiresharkBase::wiresharkValidFuncNameImpl();
84+
}
85+
86+
assert(m_wiresharkField != nullptr);
87+
return m_wiresharkField->wiresharkValidFuncName();
88+
}
89+
8090
std::string WiresharkRefField::wiresharkDissectCodeImpl(const WiresharkField* refField) const
8191
{
8292
if (m_alias) {
@@ -151,6 +161,27 @@ std::string WiresharkRefField::wiresharkDissectBodyImpl([[maybe_unused]] const W
151161
return util::genProcessTemplate(Templ, repl);
152162
}
153163

164+
std::string WiresharkRefField::wiresharkValidFuncBodyImpl(const WiresharkField* refField) const
165+
{
166+
static const std::string Templ =
167+
"return #^#FUNC#$#(#^#FIELD#$#)\n"
168+
;
169+
170+
assert(!m_alias);
171+
util::GenReplacementMap repl = {
172+
{"FUNC", m_wiresharkField->wiresharkValidFuncName()},
173+
{"FIELD", wiresharkFieldObjName(refField)},
174+
};
175+
176+
return util::genProcessTemplate(Templ, repl);
177+
}
178+
179+
bool WiresharkRefField::wiresharkHasTrivialValidImpl() const
180+
{
181+
assert(m_wiresharkField != nullptr);
182+
return m_wiresharkField->wiresharkHasTrivialValid();
183+
}
184+
154185
bool WiresharkRefField::wiresharkIsAliasInternal() const
155186
{
156187
if (wiresharkMustCopyDissectInternal()) {

app/commsdsl2wireshark/src/WiresharkRefField.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ class WiresharkRefField final : public commsdsl::gen::GenRefField, public Wiresh
3838
virtual bool genPrepareImpl() override;
3939

4040
virtual std::string wiresharkDissectNameImpl(const WiresharkField* refField) const override;
41+
virtual std::string wiresharkValidFuncNameImpl() const override;
4142
virtual std::string wiresharkDissectCodeImpl(const WiresharkField* refField) const override;
4243
virtual std::string wiresharkExtractorsRegCodeImpl() const override;
4344
virtual std::string wiresharkFieldObjNameImpl(const WiresharkField* refField) const override;
4445
virtual std::string wiresharkFieldRegistrationImpl(const WiresharkField* refField) const override;
4546
virtual std::string wiresharkDissectBodyImpl(const WiresharkField* refField) const override;
47+
virtual std::string wiresharkValidFuncBodyImpl(const WiresharkField* refField) const override;
48+
virtual bool wiresharkHasTrivialValidImpl() const override;
4649

4750
private:
4851
bool wiresharkIsAliasInternal() const;

0 commit comments

Comments
 (0)