diff --git a/cryptlib.h b/cryptlib.h index 1b0c1791..0c42959d 100644 --- a/cryptlib.h +++ b/cryptlib.h @@ -50,7 +50,7 @@
Input Source Classes
StringSource, ArraySource, FileSource, RandomNumberSource
Output Sink Classes
- StringSinkTemplate, StringSink, ArraySink, FileSink, RandomNumberSink + StringSinkTemplate, StringSink, VectorSink, ArraySink, FileSink, RandomNumberSink
Filter Wrappers
StreamTransformationFilter, AuthenticatedEncryptionFilter, AuthenticatedDecryptionFilter, HashFilter, HashVerificationFilter, SignerFilter, SignatureVerificationFilter diff --git a/filters.h b/filters.h index 9d39d4d9..d7503f8a 100644 --- a/filters.h +++ b/filters.h @@ -1063,12 +1063,13 @@ template class StringSinkTemplate : public Bufferless { public: + typedef typename T::value_type value_type; virtual ~StringSinkTemplate() {} /// \brief Construct a StringSinkTemplate /// \param output std::basic_string 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, StringSink) CRYPTOPP_DLL_TEMPLATE_CLASS StringSinkTemplate; +/// \brief Append input to a std::vector object +/// \details VectorSink is a typedef for StringSinkTemplate >. +DOCUMENTED_TYPEDEF(StringSinkTemplate >, VectorSink); +CRYPTOPP_DLL_TEMPLATE_CLASS StringSinkTemplate >; + /// \brief Incorporates input into RNG as additional entropy /// \since Crypto++ 4.0 class RandomNumberSink : public Bufferless 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 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"; diff --git a/validate.h b/validate.h index 58e1eca8..b11afb4d 100644 --- a/validate.h +++ b/validate.h @@ -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)