diff options
author | Mouse <mouse07410@users.noreply.github.com> | 2018-09-05 15:56:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-05 15:56:55 -0400 |
commit | ac43bee6985044fb2e539b6fb5f67bc69792db82 (patch) | |
tree | 17510a45d4ce1912b18debebc17f3b80e4a680ce | |
parent | 44cd7eb1ed8460d4b6471101c67a78ca9e1ae44d (diff) | |
parent | 9b81a545fc50b13f933687550ef116818ab29de6 (diff) | |
download | cryptopp-git-ac43bee6985044fb2e539b6fb5f67bc69792db82.tar.gz |
Merge pull request #334 from orangefour/feature/vector_sink
Add VectorSink
-rw-r--r-- | cryptlib.h | 2 | ||||
-rw-r--r-- | filters.h | 12 | ||||
-rw-r--r-- | validat3.cpp | 21 | ||||
-rw-r--r-- | validate.h | 1 |
4 files changed, 31 insertions, 5 deletions
@@ -50,7 +50,7 @@ <dt>Input Source Classes<dd>
StringSource, ArraySource, FileSource, RandomNumberSource
<dt>Output Sink Classes<dd>
- StringSinkTemplate, StringSink, ArraySink, FileSink, RandomNumberSink
+ StringSinkTemplate, StringSink, VectorSink, ArraySink, FileSink, RandomNumberSink
<dt>Filter Wrappers<dd>
StreamTransformationFilter, AuthenticatedEncryptionFilter, AuthenticatedDecryptionFilter, HashFilter,
HashVerificationFilter, SignerFilter, SignatureVerificationFilter
@@ -1063,12 +1063,13 @@ template <class T> class StringSinkTemplate : public Bufferless<Sink>
{
public:
+ typedef typename T::value_type value_type;
virtual ~StringSinkTemplate() {}
/// \brief Construct a StringSinkTemplate
/// \param output std::basic_string<char> type
StringSinkTemplate(T &output)
- : m_output(&output) {CRYPTOPP_ASSERT(sizeof(output[0])==1);}
+ : m_output(&output) {CRYPTOPP_ASSERT(sizeof(value_type)==1);}
void IsolatedInitialize(const NameValuePairs ¶meters)
{if (!parameters.GetValue("OutputStringPointer", m_output)) throw InvalidArgument("StringSink: OutputStringPointer not specified");}
@@ -1076,14 +1077,12 @@ public: size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
{
CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking);
- typedef typename T::traits_type::char_type char_type;
-
if (length > 0)
{
typename T::size_type size = m_output->size();
if (length < size && size + length > m_output->capacity())
m_output->reserve(2*size);
- m_output->append((const char_type *)inString, (const char_type *)inString+length);
+ m_output->insert(m_output->end(), (const value_type *)inString, (const value_type *)inString+length);
}
return 0;
}
@@ -1099,6 +1098,11 @@ private: DOCUMENTED_TYPEDEF(StringSinkTemplate<std::string>, StringSink)
CRYPTOPP_DLL_TEMPLATE_CLASS StringSinkTemplate<std::string>;
+/// \brief Append input to a std::vector<byte> object
+/// \details VectorSink is a typedef for StringSinkTemplate<std::vector<byte> >.
+DOCUMENTED_TYPEDEF(StringSinkTemplate<std::vector<byte> >, VectorSink);
+CRYPTOPP_DLL_TEMPLATE_CLASS StringSinkTemplate<std::vector<byte> >;
+
/// \brief Incorporates input into RNG as additional entropy
/// \since Crypto++ 4.0
class RandomNumberSink : public Bufferless<Sink>
diff --git a/validat3.cpp b/validat3.cpp index 3cc46615..fc658501 100644 --- a/validat3.cpp +++ b/validat3.cpp @@ -41,6 +41,7 @@ bool ValidateAll(bool thorough) {
bool pass=TestSettings();
pass=TestOS_RNG() && pass;
+ pass=TestStringSink() && pass;
pass=TestRandomPool() && pass;
#if !defined(NO_OS_DEPENDENCE) && defined(OS_RNG_AVAILABLE)
pass=TestAutoSeededX917() && pass;
@@ -561,6 +562,26 @@ bool TestOS_RNG() return pass;
}
+bool TestStringSink()
+{
+ try
+ {
+ std::string in = "The quick brown fox jumps over the lazy dog";
+
+ std::string str;
+ StringSource s1(in, true, new StringSink(str));
+
+ std::vector<byte> vec;
+ StringSource s2(in, true, new VectorSink(vec));
+
+ return str.size() == vec.size() && std::equal(str.begin(), str.end(), vec.begin());
+ }
+ catch(...)
+ {
+ }
+ return false;
+}
+
bool TestRandomPool()
{
std::cout << "\nTesting RandomPool generator...\n\n";
@@ -23,6 +23,7 @@ NAMESPACE_BEGIN(Test) bool ValidateAll(bool thorough);
bool TestSettings();
bool TestOS_RNG();
+bool TestStringSink();
// bool TestSecRandom();
bool TestRandomPool();
#if !defined(NO_OS_DEPENDENCE)
|