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
diff --git a/tests/unit-tests/util/string-helper.t.cpp b/tests/unit-tests/util/string-helper.t.cpp
index 22e8659..65b3e50 100644
--- a/tests/unit-tests/util/string-helper.t.cpp
+++ b/tests/unit-tests/util/string-helper.t.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).
*
@@ -53,7 +53,7 @@
// if (ch % 8 == 7)
// std::cout << std::endl;
// }
- std::vector<std::pair<unsigned char, int>> hexMap{
+ static const std::vector<std::pair<char, int>> hexMap{
{0x0, -1}, {0x1, -1}, {0x2, -1}, {0x3, -1}, {0x4, -1}, {0x5, -1}, {0x6, -1}, {0x7, -1},
{0x8, -1}, {0x9, -1}, {0xa, -1}, {0xb, -1}, {0xc, -1}, {0xd, -1}, {0xe, -1}, {0xf, -1},
{0x10, -1}, {0x11, -1}, {0x12, -1}, {0x13, -1}, {0x14, -1}, {0x15, -1}, {0x16, -1}, {0x17, -1},
@@ -87,24 +87,26 @@
{0xf0, -1}, {0xf1, -1}, {0xf2, -1}, {0xf3, -1}, {0xf4, -1}, {0xf5, -1}, {0xf6, -1}, {0xf7, -1},
{0xf8, -1}, {0xf9, -1}, {0xfa, -1}, {0xfb, -1}, {0xfc, -1}, {0xfd, -1}, {0xfe, -1}, {0xff, -1}
};
+
for (const auto& item : hexMap) {
- BOOST_CHECK_EQUAL(fromHexChar(static_cast<char>(item.first)), item.second);
+ BOOST_CHECK_EQUAL(fromHexChar(item.first), item.second);
}
}
BOOST_AUTO_TEST_CASE(FromHex)
{
- BOOST_CHECK_NO_THROW(fromHex("48656c6c6f2c20776f726c6421"));
+ BOOST_CHECK(*fromHex("") == Buffer{});
BOOST_CHECK(*fromHex("48656c6c6f2c20776f726c6421") ==
- (std::vector<uint8_t>{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21}));
-
- BOOST_CHECK_NO_THROW(fromHex("012a3Bc4defAB5CdEF"));
+ (std::vector<uint8_t>{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20,
+ 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21}));
BOOST_CHECK(*fromHex("012a3Bc4defAB5CdEF") ==
- (std::vector<uint8_t>{0x01, 0x2a, 0x3b, 0xc4, 0xde, 0xfa, 0xb5, 0xcd, 0xef}));
+ (std::vector<uint8_t>{0x01, 0x2a, 0x3b, 0xc4, 0xde,
+ 0xfa, 0xb5, 0xcd, 0xef}));
BOOST_CHECK_THROW(fromHex("1"), StringHelperError);
BOOST_CHECK_THROW(fromHex("zz"), StringHelperError);
BOOST_CHECK_THROW(fromHex("00az"), StringHelperError);
+ BOOST_CHECK_THROW(fromHex("1234z"), StringHelperError);
}
BOOST_AUTO_TEST_CASE(Trim)