From ae75bf96c41bf8ec7563149889ff5358925e9a8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20=C3=96hrstr=C3=B6m?= Date: Sat, 7 May 2022 19:40:46 +0200 Subject: [PATCH] Auto-detect ascii in model-version vif. --- src/dvparser.cc | 18 +++++++++++++----- src/testinternals.cc | 26 ++++++++++++++++++++++++++ src/util.cc | 32 ++++++++++++++++++++++++++++++++ src/util.h | 2 ++ tests/test_analyze.sh | 2 +- 5 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/dvparser.cc b/src/dvparser.cc index c681efc..414b573 100644 --- a/src/dvparser.cc +++ b/src/dvparser.cc @@ -417,10 +417,11 @@ bool parseDV(Telegram *t, DVEntry *dve = &(*dv_entries)[key].second; + /* if (isDebugEnabled()) { debug("(dvparser) entry %s\n", dve->str().c_str()); - } + }*/ assert(key == dve->dif_vif_key.str()); @@ -949,10 +950,17 @@ bool DVEntry::extractReadableString(string *out) t == 0x7 || // 64 Bit Integer/Binary t == 0xD) // Variable length { - // For example an enhanced id 32 bits binary looks like: - // 44434241 and will be reversed to: 41424344 and translated using ascii - // to ABCD - v = reverseBinaryAsciiSafeToString(v); + if (isLikelyAscii(v)) + { + // For example an enhanced id 32 bits binary looks like: + // 44434241 and will be reversed to: 41424344 and translated using ascii + // to ABCD + v = reverseBinaryAsciiSafeToString(v); + } + else + { + v = reverseBCD(v); + } } if (t == 0x9 || // 2 digit BCD t == 0xA || // 4 digit BCD diff --git a/src/testinternals.cc b/src/testinternals.cc index eda7dca..29b2929 100644 --- a/src/testinternals.cc +++ b/src/testinternals.cc @@ -48,6 +48,7 @@ void test_hex(); void test_translate(); void test_slip(); void test_dvs(); +void test_ascii_detection(); int main(int argc, char **argv) { @@ -82,6 +83,7 @@ int main(int argc, char **argv) test_translate(); test_slip(); test_dvs(); + test_ascii_detection(); return 0; } @@ -1254,3 +1256,27 @@ void test_dvs() printf("ERROR test_dvs 1\n"); } } + +void test_ascii_detection() +{ + string s = "000008"; + if (isLikelyAscii(s)) + { + printf("ERROR >%s< should not be likely ascii\n", s.c_str()); + } + s = "41424344"; + if (!isLikelyAscii(s)) + { + printf("ERROR >%s< should be likely ascii\n", s.c_str()); + } + s = "000041424344"; + if (!isLikelyAscii(s)) + { + printf("ERROR >%s< should be likely ascii\n", s.c_str()); + } + s = "000041194300"; + if (isLikelyAscii(s)) + { + printf("ERROR >%s< should not be likely ascii\n", s.c_str()); + } +} diff --git a/src/util.cc b/src/util.cc index 505265a..c0f8e62 100644 --- a/src/util.cc +++ b/src/util.cc @@ -2192,3 +2192,35 @@ void removeSlipFraming(vector& from, size_t *frame_length, vector } } + +// Check if hex string is likely to be ascii +bool isLikelyAscii(std::string v) +{ + vector val; + bool ok = hex2bin(v, &val); + + // For example 64 bits: + // 0000 0000 4142 4344 + // is probably the string DCBA + + if (!ok) return false; + + size_t i = 0; + for (; i < val.size(); ++i) + { + if (val[i] != 0) break; + } + + if (i == val.size()) + { + // Value is all zeroes, this is probably a number. + return false; + } + + for (; i < val.size(); ++i) + { + if (val[i] < 20 || val[i] > 126) return false; + } + + return true; +} diff --git a/src/util.h b/src/util.h index 4267b52..2984dfd 100644 --- a/src/util.h +++ b/src/util.h @@ -43,6 +43,8 @@ uchar reverse(uchar c); std::string reverseBCD(std::string v); // A hex string encoding ascii chars is reversed and safely translated into a readble string. std::string reverseBinaryAsciiSafeToString(std::string v); +// Check if hex string is likely to be ascii +bool isLikelyAscii(std::string v); bool isHexChar(uchar c); diff --git a/tests/test_analyze.sh b/tests/test_analyze.sh index b1d797f..1ee64e1 100755 --- a/tests/test_analyze.sh +++ b/tests/test_analyze.sh @@ -142,7 +142,7 @@ Using driver : multical21(driver should be upgraded) 00/00 019 : 78 tpl-ci-field (EN 13757-3 Application Layer (no tplh)) 020 : 02 dif (16 Bit Integer/Binary Instantaneous value) 021 : FF vif (Vendor extension) -022 : 20 vife (per second) +022 : 20 combinable vif (PerSecond) 023 C!: 7100 info codes (DRY(dry 22-31 days)) 025 : 04 dif (32 Bit Integer/Binary Instantaneous value) 026 : 13 vif (Volume l)