@@ -98,14 +98,14 @@ const std::string& WiresharkIntField::wiresharkIntegralType(ParseIntField::Parse
9898 return iter->second ;
9999}
100100
101- std::string WiresharkIntField::wiresharkTvbRangeAccessIntegralValue (ParseIntField::ParseType type, ParseEndian endian, std::size_t len)
101+ std::string WiresharkIntField::wiresharkTvbRangeAccessIntegralValue (ParseIntField::ParseType type, ParseEndian endian, std::size_t len, bool forceUnsigned )
102102{
103103 std::string prefix;
104104 if (endian == ParseEndian::ParseEndian_Little) {
105105 prefix = strings::genLittleEndianPrefixStr ();
106106 }
107107
108- if (GenIntField::genIsUnsignedType (type)) {
108+ if (forceUnsigned || GenIntField::genIsUnsignedType (type)) {
109109 prefix += ' u' ;
110110 }
111111
@@ -256,8 +256,20 @@ std::string WiresharkIntField::wiresharkValidFuncBodyImpl([[maybe_unused]] const
256256 auto & ranges = parseObj.parseValidRanges ();
257257 auto & wiresharkGenerator = WiresharkGenerator::wiresharkCast (genGenerator ());
258258 auto numToStr =
259- [this ](std::intmax_t value)
259+ [this , parseObj ](std::intmax_t value)
260260 {
261+ auto scaling = parseObj.parseScaling ();
262+ if (scaling.first != scaling.second ) {
263+ if (genIsUnsignedType ()) {
264+ return
265+ std::to_string (
266+ (static_cast <double >(static_cast <std::uintmax_t >(value)) * static_cast <double >(scaling.first )) /
267+ static_cast <double >(scaling.second ));
268+ }
269+
270+ return std::to_string ((static_cast <double >(value) * static_cast <double >(scaling.first )) / static_cast <double >(scaling.second ));
271+ }
272+
261273 if (genIsUnsignedType ()) {
262274 return std::to_string (static_cast <std::uintmax_t >(value));
263275 }
@@ -460,7 +472,7 @@ std::string WiresharkIntField::wiresharkValDeclCodeInternal() const
460472 ;
461473
462474 util::GenReplacementMap repl = {
463- {" ACC" , wiresharkTvbRangeAccessIntegralValue (parseObj.parseType (), parseObj.parseEndian (), parseObj.parseMinLength ())},
475+ {" ACC" , wiresharkTvbRangeAccessIntegralValue (parseObj.parseType (), parseObj.parseEndian (), parseObj.parseMinLength (), !parseObj. parseSignExt () )},
464476 {" VAL" , wiresharkValStr ()},
465477 {" RANGE" , wiresharkRangeStr ()},
466478 };
@@ -505,19 +517,14 @@ std::string WiresharkIntField::wiresharkSerOffsetCodeInternal(bool& hasVal) cons
505517 }
506518
507519 hasVal = true ;
508- auto minLen = parseObj.parseMinLength ();
509- bool nonStandardLen = (minLen & (minLen - 1 )) != 0 ;
510- if (nonStandardLen && parseObj.parseSignExt () && (minLen != parseObj.parseMaxLength ())) {
511- // TODO: implement sign extension
512- assert (false );
513- }
514-
515520 static const std::string Templ =
521+ " #^#TONUMBER#$#\n "
516522 " #^#VAL#$# = #^#VAL#$# - (#^#OFFSET#$#)" ;
517523
518524 util::GenReplacementMap repl = {
519525 {" OFFSET" , std::to_string (serOffset)},
520526 {" VAL" , wiresharkValStr ()},
527+ {" TONUMBER" , wiresharkValToNumberCodeInternal ()},
521528 };
522529
523530 return util::genProcessTemplate (Templ, repl);
@@ -533,12 +540,14 @@ std::string WiresharkIntField::wiresharkScalingCodeInternal(bool& hasVal) const
533540
534541 hasVal = true ;
535542 static const std::string Templ =
543+ " #^#TONUMBER#$#\n "
536544 " #^#VAL#$# = (#^#VAL#$# * #^#NUM#$#) / #^#DENUM#$#\n " ;
537545
538546 util::GenReplacementMap repl = {
539547 {" VAL" , wiresharkValStr ()},
540548 {" NUM" , std::to_string (scaling.first )},
541549 {" DENUM" , std::to_string (scaling.second )},
550+ {" TONUMBER" , wiresharkValToNumberCodeInternal ()},
542551 };
543552
544553 return util::genProcessTemplate (Templ, repl);
@@ -564,4 +573,18 @@ std::string WiresharkIntField::wiresharkDisplayOffsetCodeInternal(bool& hasVal)
564573 return util::genProcessTemplate (Templ, repl);
565574}
566575
576+ std::string WiresharkIntField::wiresharkValToNumberCodeInternal () const
577+ {
578+ static const std::string Templ =
579+ " if type(#^#VAL#$#) ~= \" number\" then\n "
580+ " #^#VAL#$# = #^#VAL#$#:tonumber()\n "
581+ " end" ;
582+
583+ util::GenReplacementMap repl = {
584+ {" VAL" , wiresharkValStr ()},
585+ };
586+
587+ return util::genProcessTemplate (Templ, repl);
588+ }
589+
567590} // namespace commsdsl2wireshark
0 commit comments