From: Marcin Siodelski Date: Wed, 6 Apr 2016 13:00:12 +0000 (+0200) Subject: [4301] DUID and HWAddr classes use strutil to convert text to binary. X-Git-Tag: trac4106_update_base~50^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7d93d78cdafb21913283b2870e309c94eb4a8b49;p=thirdparty%2Fkea.git [4301] DUID and HWAddr classes use strutil to convert text to binary. --- diff --git a/src/lib/dhcp/duid.cc b/src/lib/dhcp/duid.cc index aa9cd0f4f5..1219ca9b59 100644 --- a/src/lib/dhcp/duid.cc +++ b/src/lib/dhcp/duid.cc @@ -6,11 +6,8 @@ #include #include -#include #include -#include -#include -#include +#include #include #include #include @@ -42,57 +39,6 @@ DUID::DUID(const uint8_t* data, size_t len) { duid_ = std::vector(data, data + len); } -std::vector -DUID::decode(const std::string& text) { - /// @todo optimize stream operations here. - std::vector split_text; - boost::split(split_text, text, boost::is_any_of(":"), - boost::algorithm::token_compress_off); - - std::ostringstream s; - for (size_t i = 0; i < split_text.size(); ++i) { - // Check that only hexadecimal digits are used. - size_t ch_index = 0; - while (ch_index < split_text[i].length()) { - if (!isxdigit(split_text[i][ch_index])) { - isc_throw(isc::BadValue, "invalid value '" - << split_text[i][ch_index] << "' in" - << " DUID '" << text << "'"); - } - ++ch_index; - } - - if (split_text.size() > 1) { - // If there are multiple tokens and the current one is empty, it - // means that two consecutive colons were specified. This is not - // allowed for client identifier. - if (split_text[i].empty()) { - isc_throw(isc::BadValue, "invalid identifier '" - << text << "': tokens must be" - " separated with a single colon"); - } else if (split_text[i].size() > 2) { - isc_throw(isc::BadValue, "invalid identifier '" - << text << "'"); - } - } - - if (split_text[i].size() % 2) { - s << "0"; - } - - s << split_text[i]; - } - - std::vector binary; - try { - util::encode::decodeHex(s.str(), binary); - } catch (const Exception& ex) { - isc_throw(isc::BadValue, "failed to create identifier from text '" - << text << "': " << ex.what()); - } - return (binary); -} - const std::vector& DUID::getDuid() const { return (duid_); } @@ -111,8 +57,9 @@ DUID::DUIDType DUID::getType() const { DUID DUID::fromText(const std::string& text) { - std::vector binary = decode(text); - return DUID(binary); + std::vector binary; + util::str::decodeFormattedHexString(text, binary); + return (DUID(binary)); } DuidPtr @@ -185,7 +132,8 @@ std::string ClientId::toText() const { ClientIdPtr ClientId::fromText(const std::string& text) { - std::vector binary = decode(text); + std::vector binary; + util::str::decodeFormattedHexString(text, binary); return (ClientIdPtr(new ClientId(binary))); } diff --git a/src/lib/dhcp/duid.h b/src/lib/dhcp/duid.h index 2e3fc06d50..b0ac73ae26 100644 --- a/src/lib/dhcp/duid.h +++ b/src/lib/dhcp/duid.h @@ -79,7 +79,6 @@ class DUID { /// @brief Create DUID from the textual format. /// /// This static function parses a DUID specified in the textual format. - /// Internally it uses @c DUID::decode to parse the DUID. /// /// @param text DUID in the hexadecimal format with digits representing /// individual bytes separated by colons. @@ -98,22 +97,6 @@ class DUID { protected: - /// @brief Decodes the textual format of the DUID. - /// - /// The format being parsed should match the DUID representation returned - /// by the @c DUID::toText method, i.e. the pairs of hexadecimal digits - /// representing bytes of DUID must be separated by colons. Usually the - /// single byte is represented by two hexadecimal digits. However, this - /// function allows one digit per byte. In this case, a zero is prepended - /// before the conversion. For example, a DUID 0:1:2:3:4:5 equals to - /// 00:01:02:03:04:05. - /// - /// @param text DUID in the hexadecimal format with digits representing - /// individual bytes separated by colons. - /// - /// @throw isc::BadValue if parsing the DUID failed. - static std::vector decode(const std::string& text); - /// The actual content of the DUID std::vector duid_; }; diff --git a/src/lib/dhcp/hwaddr.cc b/src/lib/dhcp/hwaddr.cc index 69722de79a..92f62247e4 100644 --- a/src/lib/dhcp/hwaddr.cc +++ b/src/lib/dhcp/hwaddr.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2016 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -7,10 +7,7 @@ #include #include #include -#include -#include -#include -#include +#include #include #include #include @@ -69,37 +66,8 @@ std::string HWAddr::toText(bool include_htype) const { HWAddr HWAddr::fromText(const std::string& text, const uint16_t htype) { - /// @todo optimize stream operations here. - std::vector split_text; - boost::split(split_text, text, boost::is_any_of(":"), - boost::algorithm::token_compress_off); - - std::ostringstream s; - for (size_t i = 0; i < split_text.size(); ++i) { - // If there are multiple tokens and the current one is empty, it - // means that two consecutive colons were specified. This is not - // allowed for hardware address. - if ((split_text.size() > 1) && split_text[i].empty()) { - isc_throw(isc::BadValue, "failed to create hardware address" - " from text '" << text << "': tokens of the hardware" - " address must be separated with a single colon"); - - } else if (split_text[i].size() == 1) { - s << "0"; - - } else if (split_text[i].size() > 2) { - isc_throw(isc::BadValue, "invalid hwaddr '" << text << "'"); - } - s << split_text[i]; - } - std::vector binary; - try { - util::encode::decodeHex(s.str(), binary); - } catch (const Exception& ex) { - isc_throw(isc::BadValue, "failed to create hwaddr from text '" - << text << "': " << ex.what()); - } + util::str::decodeColonSeparatedHexString(text, binary); return (HWAddr(binary, htype)); }