From 4e42ee425f77c48c1acea2d5b50e61e39623835d Mon Sep 17 00:00:00 2001 From: Francis Dupont Date: Tue, 3 Nov 2015 10:25:17 +0100 Subject: [PATCH] [4091] Implemented, need tests --- src/lib/eval/token.cc | 24 ++++++++++++++++++++++++ src/lib/eval/token.h | 26 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/lib/eval/token.cc b/src/lib/eval/token.cc index 5720158ca3..81126a2c19 100644 --- a/src/lib/eval/token.cc +++ b/src/lib/eval/token.cc @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -26,6 +27,29 @@ TokenString::evaluate(const Pkt& /*pkt*/, ValueStack& values) { values.push(value_); } +void +TokenString::evaluate(const Pkt& /*pkt*/, ValueStack& values) { + // Transform string of hexadecimal digits into binary format + std::vector binary; + try { + // The decodeHex function expects that the string contains an + // even number of digits. If we don't meet this requirement, + // we have to insert a leading 0. + if (!repr_.empty() && repr_.length() % 2) { + repr_ = repr_.insert(0, "0"); + } + util::encode::decodeHex(repr_, binary); + } catch (...) { + values.push(""); + return; + } + // Convert to a string + std::string chars(binary.size(), '\0'); + std::memmove(&chars[0], &binary[0], binary.size()); + // Literals only push, nothing to pop + values.push(chars_); +} + void TokenOption::evaluate(const Pkt& pkt, ValueStack& values) { OptionPtr opt = pkt.getOption(option_code_); diff --git a/src/lib/eval/token.h b/src/lib/eval/token.h index 1e34051225..699d335eb5 100644 --- a/src/lib/eval/token.h +++ b/src/lib/eval/token.h @@ -101,6 +101,32 @@ protected: std::string value_; ///< Constant value }; +/// @brief Token representing a constant string in hexadecimal format +/// +/// This token holds value of a constant string giving in an hexadecimal +/// format, for instance 0x666f6f is "foo" +class TokenHexString : public Token { +public: + /// Value is set during token construction. + /// + /// @param str constant string to be represented + /// (must be a string of hexadecimal digits or decoding will fail) + TokenHexString(const std::string& str) + :repr_(str){ + } + + /// @brief Token evaluation (puts value of the constant string on + /// the stack after decoding or an empty string if decoding fails + /// (note it should not if the parser is correct) + /// + /// @param pkt (ignored) + /// @param values (represented string will be pushed here) + void evaluate(const Pkt& pkt, ValueStack& values); + +protected: + std::string repr_; ///< Constant value +}; + /// @brief Token that represents a value of an option /// /// This represents a reference to a given option, e.g. in the expression -- 2.47.2