1
/*************************************************
2
* Boost.Python module definition *
3
* (C) 1999-2007 The Botan Project *
4
*************************************************/
6
#include <boost/python.hpp>
7
using namespace boost::python;
9
#include <botan/pipe.h>
10
#include <botan/lookup.h>
11
using namespace Botan;
13
class Py_Filter : public Filter
16
virtual void write_str(const std::string&) = 0;
18
void write(const byte data[], u32bit length)
20
write_str(std::string((const char*)data, length));
23
void send_str(const std::string& str)
25
printf("Py_Filter::send_str\n");
26
send((const byte*)str.data(), str.length());
30
class FilterWrapper : public Py_Filter, public wrapper<Py_Filter>
35
printf("wrapper start_msg\n");
36
if(override start_msg = this->get_override("start_msg"))
42
printf("wrapper end_msg\n");
43
if(override end_msg = this->get_override("end_msg"))
47
void default_start_msg() {}
48
void default_end_msg() {}
50
virtual void write_str(const std::string& str)
52
printf("wrapper write\n");
53
this->get_override("write")(str);
57
Filter* return_or_raise(Filter* filter, const std::string& name)
61
throw Invalid_Argument("Filter " + name + " could not be found");
64
Filter* make_filter1(const std::string& name)
68
if(have_hash(name)) filter = new Hash_Filter(name);
69
else if(name == "Hex_Encoder") filter = new Hex_Encoder;
70
else if(name == "Hex_Decoder") filter = new Hex_Decoder;
71
else if(name == "Base64_Encoder") filter = new Base64_Encoder;
72
else if(name == "Base64_Decoder") filter = new Base64_Decoder;
74
return return_or_raise(filter, name);
77
Filter* make_filter2(const std::string& name,
78
const SymmetricKey& key)
83
filter = new MAC_Filter(name, key);
84
else if(have_stream_cipher(name))
85
filter = new StreamCipher_Filter(name, key);
87
return return_or_raise(filter, name);
90
// FIXME: add new wrapper for Keyed_Filter here
91
Filter* make_filter3(const std::string& name,
92
const SymmetricKey& key,
95
return return_or_raise(get_cipher(name, key, direction), name);
98
Filter* make_filter4(const std::string& name,
99
const SymmetricKey& key,
100
const InitializationVector& iv,
101
Cipher_Dir direction)
103
return return_or_raise(get_cipher(name, key, iv, direction), name);
106
void append_filter(Pipe& pipe, std::auto_ptr<Filter> filter)
108
pipe.append(filter.get());
112
void prepend_filter(Pipe& pipe, std::auto_ptr<Filter> filter)
114
pipe.prepend(filter.get());
118
void do_send(std::auto_ptr<FilterWrapper> filter, const std::string& data)
120
printf("Sending %s to %p\n", data.c_str(), filter.get());
121
filter->send_str(data);
124
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(rallas_ovls, read_all_as_string, 0, 1)
126
void export_filters()
128
class_<Filter, std::auto_ptr<Filter>, boost::noncopyable>
129
("__Internal_FilterObj", no_init);
131
def("make_filter", make_filter1,
132
return_value_policy<manage_new_object>());
133
def("make_filter", make_filter2,
134
return_value_policy<manage_new_object>());
135
def("make_filter", make_filter3,
136
return_value_policy<manage_new_object>());
137
def("make_filter", make_filter4,
138
return_value_policy<manage_new_object>());
140
// This might not work - Pipe will delete the filter, but Python
141
// might have allocated the space with malloc() or who-knows-what -> bad
142
class_<FilterWrapper, std::auto_ptr<FilterWrapper>,
143
bases<Filter>, boost::noncopyable>
145
.def("write", pure_virtual(&Py_Filter::write_str))
146
.def("send", &do_send)
147
.def("start_msg", &Filter::start_msg, &FilterWrapper::default_start_msg)
148
.def("end_msg", &Filter::end_msg, &FilterWrapper::default_end_msg);
150
implicitly_convertible<std::auto_ptr<FilterWrapper>,
151
std::auto_ptr<Filter> >();
153
void (Pipe::*pipe_write_str)(const std::string&) = &Pipe::write;
154
void (Pipe::*pipe_process_str)(const std::string&) = &Pipe::process_msg;
156
class_<Pipe, boost::noncopyable>("PipeObj")
158
.def_readonly("LAST_MESSAGE", &Pipe::LAST_MESSAGE)
159
.def_readonly("DEFAULT_MESSAGE", &Pipe::DEFAULT_MESSAGE)
160
.add_property("default_msg", &Pipe::default_msg, &Pipe::set_default_msg)
161
.add_property("msg_count", &Pipe::message_count)
162
.def("append", append_filter)
163
.def("prepend", prepend_filter)
164
.def("reset", &Pipe::reset)
165
.def("pop", &Pipe::pop)
166
.def("end_of_data", &Pipe::end_of_data)
167
.def("start_msg", &Pipe::start_msg)
168
.def("end_msg", &Pipe::end_msg)
169
.def("write", pipe_write_str)
170
.def("process_msg", pipe_process_str)
171
.def("read_all", &Pipe::read_all_as_string, rallas_ovls());