aboutsummaryrefslogtreecommitdiffstats
path: root/misc
diff options
context:
space:
mode:
authorlloyd <[email protected]>2006-08-23 07:24:29 +0000
committerlloyd <[email protected]>2006-08-23 07:24:29 +0000
commit77ceb5a3f5e6b2774d37d7a0464a7607814e1f7c (patch)
treeb317ac7a6889cff3b5f00d96cd17180386c3f857 /misc
parent5ed6ee611a65e3390e9e60660335b1d21cefbb64 (diff)
Initial (only partially working) attempt to allow filters to be implemented
in Python.
Diffstat (limited to 'misc')
-rw-r--r--misc/python/src/filter.cpp60
-rw-r--r--misc/python/src/x509.cpp4
-rwxr-xr-xmisc/python/test.py24
3 files changed, 79 insertions, 9 deletions
diff --git a/misc/python/src/filter.cpp b/misc/python/src/filter.cpp
index bf3632fab..edd282048 100644
--- a/misc/python/src/filter.cpp
+++ b/misc/python/src/filter.cpp
@@ -1,6 +1,6 @@
/*************************************************
-* Wrappers for Botan Filters *
-* (C) 2005-2006 Jack Lloyd <[email protected]> *
+* Boost.Python module definition *
+* (C) 1999-2006 The Botan Project *
*************************************************/
#include <boost/python.hpp>
@@ -10,6 +10,46 @@ using namespace boost::python;
#include <botan/lookup.h>
using namespace Botan;
+class StringFilter : public Filter
+ {
+ public:
+ virtual void write_str(const std::string&) = 0;
+ void write(const byte data[], u32bit length)
+ {
+ write_str(std::string((const char*)data, length));
+ }
+
+ void send_str(const std::string& str)
+ {
+ send((const byte*)str.data(), str.length());
+ }
+ };
+
+class FilterWrapper : public StringFilter, public wrapper<StringFilter>
+ {
+ public:
+ void start_msg()
+ {
+ if(override start_msg = this->get_override("start_msg"))
+ start_msg();
+ }
+
+ void end_msg()
+ {
+ if(override end_msg = this->get_override("end_msg"))
+ end_msg();
+ }
+
+ void default_start_msg() {}
+ void default_end_msg() {}
+
+ virtual void write_str(const std::string& str)
+ {
+ this->get_override("write")(str);
+ }
+
+ };
+
Filter* return_or_raise(Filter* filter, const std::string& name)
{
if(filter)
@@ -65,6 +105,12 @@ void append_filter(Pipe& pipe, std::auto_ptr<Filter> filter)
filter.release();
}
+void append_pyfilter(Pipe& pipe, std::auto_ptr<FilterWrapper> filter)
+ {
+ pipe.append(filter.get());
+ filter.release();
+ }
+
void prepend_filter(Pipe& pipe, std::auto_ptr<Filter> filter)
{
pipe.prepend(filter.get());
@@ -87,6 +133,15 @@ void export_filters()
def("make_filter", make_filter4,
return_value_policy<manage_new_object>());
+ // This might not work - Pipe will delete the filter, but Python
+ // might have allocated the space with malloc() or who-knows-what -> bad
+ class_<FilterWrapper, std::auto_ptr<FilterWrapper>, boost::noncopyable>
+ ("FilterObj")
+ .def("write", pure_virtual(&StringFilter::write_str))
+ .def("send_str", &StringFilter::send_str)
+ .def("start_msg", &Filter::start_msg, &FilterWrapper::default_start_msg)
+ .def("end_msg", &Filter::end_msg, &FilterWrapper::default_end_msg);
+
void (Pipe::*pipe_write_str)(const std::string&) = &Pipe::write;
void (Pipe::*pipe_process_str)(const std::string&) = &Pipe::process_msg;
@@ -97,6 +152,7 @@ void export_filters()
.add_property("default_msg", &Pipe::default_msg, &Pipe::set_default_msg)
.add_property("msg_count", &Pipe::message_count)
.def("append", append_filter)
+ .def("append", append_pyfilter)
.def("prepend", prepend_filter)
.def("reset", &Pipe::reset)
.def("pop", &Pipe::pop)
diff --git a/misc/python/src/x509.cpp b/misc/python/src/x509.cpp
index af50dc903..d51d5e706 100644
--- a/misc/python/src/x509.cpp
+++ b/misc/python/src/x509.cpp
@@ -1,6 +1,6 @@
/*************************************************
-* Wrappers for X.509 types *
-* (C) 2005-2006 Jack Lloyd <[email protected]> *
+* Boost.Python module definition *
+* (C) 1999-2006 The Botan Project *
*************************************************/
#include <botan/oids.h>
diff --git a/misc/python/test.py b/misc/python/test.py
index e2f1e61fa..416797099 100755
--- a/misc/python/test.py
+++ b/misc/python/test.py
@@ -2,16 +2,30 @@
import sys, botan
+class PyFilter(botan.FilterObj):
+ def start_msg(self):
+ print "PyFilter start_msg"
+ self.send_str('initial')
+
+ def end_msg(self):
+ print "PyFilter end_msg"
+
+ def write(self, data):
+ print "PyFilter write called with", data
+ self.send_str(data.replace('h', 'r'))
+
def encrypt(input):
- pipe = botan.Pipe(botan.Filter("ARC4",
- key = botan.SymmetricKey("AABB")),
- botan.Filter("Hex_Encoder"))
+ filter = PyFilter()
+
+ pipe = botan.Pipe(filter)
pipe.start_msg()
pipe.write(input)
pipe.end_msg()
- return pipe.read_all()
+ str = pipe.read_all()
+ print str
+ return str
def decrypt(input):
pipe = botan.Pipe(botan.Filter("Hex_Decoder"),
@@ -24,7 +38,7 @@ def decrypt(input):
def main():
ciphertext = encrypt("hi chappy")
print ciphertext
- print decrypt(ciphertext)
+ #print decrypt(ciphertext)
if __name__ == "__main__":
sys.exit(main())