Skip to content

Commit 6356189

Browse files
committed
Saving work on commsdsl2wireshark.
1 parent fab6723 commit 6356189

9 files changed

Lines changed: 112 additions & 7 deletions

File tree

app/commsdsl2wireshark/src/Wireshark.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,14 @@ std::string Wireshark::wiresharkDissectFuncBodyInternal() const
199199
auto& wiresharkFrame = WiresharkFrame::wiresharkCast(*f);
200200
static const std::string FrameTempl =
201201
"result, next_offset = #^#NAME#$#(tvb, tree)\n"
202-
"if result then\n"
202+
"if result ~= #^#SUCCESS#$# then\n"
203203
" break\n"
204204
"end\n"
205205
;
206206

207207
util::GenReplacementMap frameRepl = {
208-
{"NAME", wiresharkFrame.wiresharkDissectName()}
208+
{"NAME", wiresharkFrame.wiresharkDissectName()},
209+
{"SUCCESS", wiresharkStatusCodeStr(m_wiresharkGenerator, StatusCode::Success)},
209210
};
210211

211212
elems.push_back(util::genProcessTemplate(FrameTempl, frameRepl));

app/commsdsl2wireshark/src/WiresharkField.cpp

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,46 @@ std::string WiresharkField::wiresharkTvbRangeAccessImpl() const
289289
return strings::genEmptyString();
290290
}
291291

292+
std::string WiresharkField::wiresharkDissectLengthCheckImpl() const
293+
{
294+
static const std::string Templ =
295+
"#^#MIN#$#\n"
296+
"#^#MAX#$#\n"
297+
;
298+
299+
static const std::string LenTempl =
300+
"if offset_limit < (offset + #^#LEN#$#) then\n"
301+
" return #^#ERROR#$#, offset\n"
302+
"end\n"
303+
;
304+
305+
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(m_genField.genGenerator());
306+
util::GenReplacementMap repl = {
307+
{"ERROR", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::StatusCode::NotEnoughData)},
308+
};
309+
310+
auto parseObj = m_genField.genParseObj();
311+
if (0 < parseObj.parseMinLength()) {
312+
auto minRepl = repl;
313+
minRepl["LEN"] = std::to_string(parseObj.parseMinLength());
314+
repl["MIN"] = util::genProcessTemplate(LenTempl, minRepl);
315+
}
316+
317+
if ((parseObj.parseMaxLength() != commsdsl::parse::ParseField::parseMaxPossibleLength()) &&
318+
(parseObj.parseMaxLength() != parseObj.parseMinLength())) {
319+
auto maxRepl = repl;
320+
maxRepl["LEN"] = std::to_string(parseObj.parseMaxLength());
321+
repl["MAX"] = util::genProcessTemplate(LenTempl, maxRepl);
322+
}
323+
324+
return util::genProcessTemplate(Templ, repl);
325+
}
326+
292327
std::string WiresharkField::wiresharkDissectBodyImpl() const
293328
{
294-
return strings::genEmptyString();
329+
// TODO
330+
return "-- TODO: not implemented";
331+
//return strings::genEmptyString();
295332
}
296333

297334
std::string WiresharkField::wiresharkFieldRefName(const WiresharkField* refField) const
@@ -520,9 +557,11 @@ std::string WiresharkField::wiresharkDissectBodyInternal(const WiresharkField* r
520557
{
521558
static const std::string Templ =
522559
"field = field or #^#FIELD#$#\n"
560+
"#^#LENGTH#$#\n"
523561
"local result = #^#ERROR#$#\n"
524562
"local next_offset = offset\n"
525563
"#^#REST#$#\n"
564+
"#^#VALID#$#\n"
526565
"return result, next_offset\n"
527566
;
528567

@@ -531,6 +570,8 @@ std::string WiresharkField::wiresharkDissectBodyInternal(const WiresharkField* r
531570
{"FIELD", wiresharkFieldObjName(refField)},
532571
{"ERROR", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::StatusCode::InvalidMsgData)},
533572
{"REST", wiresharkDissectBodyImpl()},
573+
{"LENGTH", wiresharkDissectLengthCheckImpl()},
574+
{"VALID", wiresharkDissectValidCheckInternal()},
534575
};
535576

536577
return util::genProcessTemplate(Templ, repl);
@@ -563,4 +604,15 @@ std::string WiresharkField::wiresharkCustomNameCodeInternal(bool& hasRealCode) c
563604
return wiresharkGenerator.genReadCodeInjectCode(replaceFileName, "Replace name value", &hasRealCode);
564605
}
565606

607+
std::string WiresharkField::wiresharkDissectValidCheckInternal() const
608+
{
609+
auto parseObj = m_genField.genParseObj();
610+
if (!parseObj.parseIsFailOnInvalid()) {
611+
return strings::genEmptyString();
612+
}
613+
614+
// TODO:
615+
return "-- TODO: implement validity check";
616+
}
617+
566618
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkField.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class WiresharkField
6767
virtual std::string wiresharkFieldRegistrationImpl(const WiresharkField* refField) const;
6868
virtual std::string wiresharkMembersDissectCodeImpl() const;
6969
virtual std::string wiresharkTvbRangeAccessImpl() const;
70+
virtual std::string wiresharkDissectLengthCheckImpl() const;
7071
virtual std::string wiresharkDissectBodyImpl() const;
7172

7273
std::string wiresharkFieldRefName(const WiresharkField* refField) const;
@@ -108,6 +109,7 @@ class WiresharkField
108109
std::string wiresharkCustomReadCodeInternal(bool& hasRealCode) const;
109110
std::string wiresharkCustomValidCodeInternal(bool& hasRealCode) const;
110111
std::string wiresharkCustomNameCodeInternal(bool& hasRealCode) const;
112+
std::string wiresharkDissectValidCheckInternal() const;
111113

112114
GenField& m_genField;
113115
WiresharkCustomCode m_customCode;

app/commsdsl2wireshark/src/WiresharkIntField.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,47 @@ std::string WiresharkIntField::wiresharkTvbRangeAccessImpl() const
166166
return wiresharkTvbRangeAccessIntegralValue(obj.parseType(), obj.parseEndian(), obj.parseMaxLength());
167167
}
168168

169+
std::string WiresharkIntField::wiresharkDissectLengthCheckImpl() const
170+
{
171+
auto parseObj = genIntFieldParseObj();
172+
173+
if (parseObj.parseAvailableLengthLimit()) {
174+
static const std::string Templ =
175+
"if offset == offset_limit then"
176+
" return #^#ERROR#$#, offset\n"
177+
"end"
178+
;
179+
180+
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genGenerator());
181+
util::GenReplacementMap repl = {
182+
{"ERROR", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::StatusCode::NotEnoughData)},
183+
};
184+
185+
return util::genProcessTemplate(Templ, repl);
186+
}
187+
188+
return WiresharkBase::wiresharkDissectLengthCheckImpl();
189+
}
190+
191+
std::string WiresharkIntField::wiresharkDissectBodyImpl() const
192+
{
193+
static const std::string Templ =
194+
"local len = math.min(#^#LEN#$#, offset_limit - offset)\n"
195+
"tree:add(field, tvb(offset, len)#^#COMMA#$##^#VAL#$#)\n"
196+
"result = #^#SUCCESS#$#\n"
197+
"next_offset = offset + len\n"
198+
;
199+
200+
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genGenerator());
201+
auto parseObj = genParseObj();
202+
util::GenReplacementMap repl = {
203+
{"LEN", std::to_string(parseObj.parseMaxLength())},
204+
{"SUCCESS", Wireshark::wiresharkStatusCodeStr(wiresharkGenerator, Wireshark::StatusCode::Success)},
205+
};
206+
207+
return util::genProcessTemplate(Templ, repl);
208+
}
209+
169210
std::string WiresharkIntField::wiresharkSpecialsInternal(const WiresharkField* refField) const
170211
{
171212
auto& specials = genSpecialsSortedByValue();

app/commsdsl2wireshark/src/WiresharkIntField.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class WiresharkIntField final : public commsdsl::gen::GenIntField, public Wiresh
4444
virtual bool genPrepareImpl() override;
4545
virtual std::string wiresharkFieldRegistrationImpl(const WiresharkField* refField) const override;
4646
virtual std::string wiresharkTvbRangeAccessImpl() const override;
47+
virtual std::string wiresharkDissectLengthCheckImpl() const override;
48+
virtual std::string wiresharkDissectBodyImpl() const override;
4749

4850
private:
4951
std::string wiresharkSpecialsInternal(const WiresharkField* refField) const;

app/commsdsl2wireshark/src/WiresharkLayer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ std::string WiresharkLayer::wiresharkDissectCode() const
6767
"#^#FIELD#$#\n"
6868
"#^#EXTRA#$#\n"
6969
"#^#PREPEND#$#\n"
70-
"local function #^#NAME#$##^#SUFFIX#$#(tvb, tree, offset, offset-limit, funcs, next_idx, msg)\n"
70+
"local function #^#NAME#$##^#SUFFIX#$#(tvb, tree, offset, offset_limit, funcs, next_idx, msg)\n"
7171
" #^#REPLACE#$#\n"
7272
" local result = #^#SUCCESS#$#\n"
7373
" local next_offset = offset\n"
@@ -153,7 +153,7 @@ std::string WiresharkLayer::wiresharkDissectFieldCode() const
153153
std::string WiresharkLayer::wiresharkNextFuncCode() const
154154
{
155155
static const std::string Templ =
156-
"local next_func = func[next_idx]\n"
156+
"local next_func = funcs[next_idx]\n"
157157
"if next_func == #^#NIL#$# then\n"
158158
" return #^#ERROR#$#, offset\n"
159159
"end\n"

app/commsdsl2wireshark/src/WiresharkPayloadLayer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ std::string WiresharkPayloadLayer::wiresharkDissectBodyImpl() const
5454
" end\n"
5555
"\n"
5656
" -- Do not show partially dissected malformed data\n"
57-
" data_subtree.set_hidden(true)\n"
57+
" data_subtree:set_hidden(true)\n"
5858
"end\n"
5959
"tree:add_expert_info(PI_MALFORMED, PI_WARN, \"Invalid message data\")\n"
6060
"result, next_offset = #^#SUCCESS#$#, offset_limit\n"
@@ -72,7 +72,7 @@ std::string WiresharkPayloadLayer::wiresharkDissectBodyImpl() const
7272
std::string WiresharkPayloadLayer::wiresharkExtraDissectCodeImpl() const
7373
{
7474
static const std::string Templ =
75-
"local #^#NAME#$# = #^#CREATE_FUNC#$#(ProtoField.bytes(\"#^#REF_NAME#$#\", #^#DISP_NAME#$#, base.SPACE, #^#DESC#$#))\n"
75+
"local #^#NAME#$# = #^#CREATE_FUNC#$#(ProtoField.bytes(\"#^#REF_NAME#$#\", \"#^#DISP_NAME#$#\", base.SPACE, #^#DESC#$#))\n"
7676
;
7777

7878
auto parseObj = genParseObj();

lib/include/commsdsl/parse/ParseField.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class COMMSDSL_API ParseField
7474
ParseSemanticType parseSemanticType() const;
7575
std::size_t parseMinLength() const;
7676
std::size_t parseMaxLength() const;
77+
static std::size_t parseMaxPossibleLength();
7778
std::size_t parseBitLength() const;
7879
unsigned parseSinceVersion() const;
7980
unsigned parseDeprecatedSince() const;

lib/src/parse/ParseField.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "commsdsl/parse/ParseField.h"
1717

1818
#include "ParseFieldImpl.h"
19+
#include "parse_common.h"
1920

2021
#include <cassert>
2122

@@ -81,6 +82,11 @@ std::size_t ParseField::parseMaxLength() const
8182
return m_pImpl->parseMaxLength();
8283
}
8384

85+
std::size_t ParseField::parseMaxPossibleLength()
86+
{
87+
return common::parseMaxPossibleLength();
88+
}
89+
8490
std::size_t ParseField::parseBitLength() const
8591
{
8692
assert(m_pImpl != nullptr);

0 commit comments

Comments
 (0)