Merge pull request #334 from orangefour/feature/vector_sink

Add VectorSink
This commit is contained in:
Mouse 2018-09-05 15:56:55 -04:00 committed by GitHub
commit ac43bee698
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 5 deletions

View File

@ -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

View File

@ -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 &parameters)
{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>

View File

@ -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";

View File

@ -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)