#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>
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_);
}
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
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)));
}
/// @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.
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
#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>
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));
}