Commit 7d93d78c authored by Marcin Siodelski's avatar Marcin Siodelski

[4301] DUID and HWAddr classes use strutil to convert text to binary.

parent 0b5aab31
......@@ -6,11 +6,8 @@
#include <dhcp/duid.h>
#include <exceptions/exceptions.h>
#include <util/encode/hex.h>
#include <util/io_utilities.h>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/constants.hpp>
#include <boost/algorithm/string/split.hpp>
#include <util/strutil.h>
#include <iomanip>
#include <cctype>
#include <sstream>
......@@ -42,57 +39,6 @@ DUID::DUID(const uint8_t* data, size_t len) {
duid_ = std::vector<uint8_t>(data, data + len);
}
std::vector<uint8_t>
DUID::decode(const std::string& text) {
/// @todo optimize stream operations here.
std::vector<std::string> 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<uint8_t> 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<uint8_t>& DUID::getDuid() const {
return (duid_);
}
......@@ -111,8 +57,9 @@ DUID::DUIDType DUID::getType() const {
DUID
DUID::fromText(const std::string& text) {
std::vector<uint8_t> binary = decode(text);
return DUID(binary);
std::vector<uint8_t> 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<uint8_t> binary = decode(text);
std::vector<uint8_t> binary;
util::str::decodeFormattedHexString(text, binary);
return (ClientIdPtr(new ClientId(binary)));
}
......
......@@ -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<uint8_t> decode(const std::string& text);
/// The actual content of the DUID
std::vector<uint8_t> duid_;
};
......
// 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 <dhcp/hwaddr.h>
#include <dhcp/dhcp4.h>
#include <exceptions/exceptions.h>
#include <util/encode/hex.h>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/constants.hpp>
#include <boost/algorithm/string/split.hpp>
#include <util/strutil.h>
#include <iomanip>
#include <sstream>
#include <vector>
......@@ -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<std::string> 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<uint8_t> 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));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment