util: rewrite fromHex using security::transform instead of cryptopp
Change-Id: I487255d3c0944679d18de8c44978f9525db5e5bd
Refs: #3006
diff --git a/src/util/string-helper.cpp b/src/util/string-helper.cpp
index 0f54e14..ed3c961 100644
--- a/src/util/string-helper.cpp
+++ b/src/util/string-helper.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -21,13 +21,15 @@
#include "string-helper.hpp"
#include "../encoding/buffer-stream.hpp"
-#include "../security/v1/cryptopp.hpp"
+#include "../security/transform/buffer-source.hpp"
+#include "../security/transform/hex-decode.hpp"
+#include "../security/transform/stream-sink.hpp"
+
+#include <boost/algorithm/string/trim.hpp>
#include <sstream>
#include <iomanip>
-#include <boost/algorithm/string/trim.hpp>
-
namespace ndn {
void
@@ -73,7 +75,7 @@
}
int
-fromHexChar(uint8_t c)
+fromHexChar(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
@@ -85,25 +87,20 @@
return -1;
}
-shared_ptr<const Buffer>
+shared_ptr<Buffer>
fromHex(const std::string& hexString)
{
- if (hexString.size() % 2 != 0) {
- BOOST_THROW_EXCEPTION(StringHelperError("Invalid number of characters in the supplied hex "
- "string"));
- }
-
- using namespace CryptoPP;
+ namespace tr = security::transform;
OBufferStream os;
- StringSource(hexString, true, new HexDecoder(new FileSink(os)));
- shared_ptr<const Buffer> buffer = os.buf();
-
- if (buffer->size() * 2 != hexString.size()) {
- BOOST_THROW_EXCEPTION(StringHelperError("The supplied hex string contains non-hex characters"));
+ try {
+ tr::bufferSource(hexString) >> tr::hexDecode() >> tr::streamSink(os);
+ }
+ catch (const tr::Error& e) {
+ BOOST_THROW_EXCEPTION(StringHelperError(std::string("Conversion from hex failed: ") + e.what()));
}
- return buffer;
+ return os.buf();
}
void
diff --git a/src/util/string-helper.hpp b/src/util/string-helper.hpp
index 4be9e4d..3b2011f 100644
--- a/src/util/string-helper.hpp
+++ b/src/util/string-helper.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -19,8 +19,8 @@
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*/
-#ifndef NDN_STRING_HELPER_HPP
-#define NDN_STRING_HELPER_HPP
+#ifndef NDN_UTIL_STRING_HELPER_HPP
+#define NDN_UTIL_STRING_HELPER_HPP
#include "../common.hpp"
#include "../encoding/buffer.hpp"
@@ -96,12 +96,18 @@
toHex(const Buffer& buffer, bool isUpperCase = true);
/**
+ * @brief Convert the hex character to an integer from 0 to 15, or -1 if not a hex character
+ */
+int
+fromHexChar(char c);
+
+/**
* @brief Convert the hex string to buffer
* @param hexString sequence of pairs of hex numbers (lower and upper case can be mixed)
* without any whitespace separators (e.g., "48656C6C6F2C20776F726C6421")
* @throw StringHelperError if input is invalid
*/
-shared_ptr<const Buffer>
+shared_ptr<Buffer>
fromHex(const std::string& hexString);
/**
@@ -123,12 +129,6 @@
trim(std::string& str);
/**
- * @brief Convert the hex character to an integer from 0 to 15, or -1 if not a hex character
- */
-int
-fromHexChar(uint8_t c);
-
-/**
* @brief Decode a percent-encoded string
* @see RFC 3986 section 2
*
@@ -144,4 +144,4 @@
} // namespace ndn
-#endif // NDN_STRING_HELPER_HPP
+#endif // NDN_UTIL_STRING_HELPER_HPP