Hi Wei,

Below (and attached) is a patch which adds a SecByteBlockSink. I've
found it to be very convenient when using filters (even though its
embarrassingly simple).

filters.h seemed like a natural choice. In addition, the filter
properly detects overflow in case the input is tainted.

Jeff

$ cat filters.h.patch
Index: filters.h
===================================================================
--- filters.h   (revision 525)
+++ filters.h   (working copy)
@@ -10,6 +10,7 @@
 #include "queue.h"
 #include "algparam.h"
 #include <deque>
+#include <limits>

 NAMESPACE_BEGIN(CryptoPP)

@@ -805,6 +806,31 @@
                {SourceInitialize(pumpAll,
MakeParameters("RandomNumberGeneratorPointer",
&rng)("RandomNumberStoreSize", length));}
 };

+class CRYPTOPP_DLL SecByteBlockSink : public Bufferless<Sink>
+{
+public:
+       SecByteBlockSink(SecByteBlock& sbb) : m_sbb(sbb) { }
+
+       size_t Put2(const byte *inString, size_t length, int /*messageEnd*/,
bool /*blocking*/)
+       {
+               if(!inString || !length) return length;
+
+               const size_t size = m_sbb.size();
+               const size_t max = std::numeric_limits<std::size_t>::max() - 
size;
+
+               if(length > max)
+                       InvalidArgument("SecByteBlockSink: buffer overflow");
+
+               m_sbb.resize(size+length);
+               memcpy(m_sbb.begin()+size, inString, length);
+               
+               return 0;
+       }
+
+private:
+       SecByteBlock& m_sbb;
+};
+
 NAMESPACE_END

 #endif

-- 
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
Index: filters.h
===================================================================
--- filters.h	(revision 525)
+++ filters.h	(working copy)
@@ -10,6 +10,7 @@
 #include "queue.h"
 #include "algparam.h"
 #include <deque>
+#include <limits>
 
 NAMESPACE_BEGIN(CryptoPP)
 
@@ -805,6 +806,31 @@
 		{SourceInitialize(pumpAll, MakeParameters("RandomNumberGeneratorPointer", &rng)("RandomNumberStoreSize", length));}
 };
 
+class CRYPTOPP_DLL SecByteBlockSink : public Bufferless<Sink>
+{
+public:
+	SecByteBlockSink(SecByteBlock& sbb) : m_sbb(sbb) { }
+
+	size_t Put2(const byte *inString, size_t length, int /*messageEnd*/, bool /*blocking*/)
+	{
+		if(!inString || !length) return length;
+
+		const size_t size = m_sbb.size();
+		const size_t max = std::numeric_limits<std::size_t>::max() - size;
+
+		if(length > max)
+			InvalidArgument("SecByteBlockSink: buffer overflow");
+
+		m_sbb.resize(size+length);
+		memcpy(m_sbb.begin()+size, inString, length);
+		
+		return 0;
+	}
+
+private:
+	SecByteBlock& m_sbb;
+};
+
 NAMESPACE_END
 
 #endif

Reply via email to