~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to misc/python/src/filter.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*************************************************
2
 
* Boost.Python module definition                 *
3
 
* (C) 1999-2007 The Botan Project                *
4
 
*************************************************/
5
 
 
6
 
#include <boost/python.hpp>
7
 
using namespace boost::python;
8
 
 
9
 
#include <botan/pipe.h>
10
 
#include <botan/lookup.h>
11
 
using namespace Botan;
12
 
 
13
 
class Py_Filter : public Filter
14
 
   {
15
 
   public:
16
 
      virtual void write_str(const std::string&) = 0;
17
 
 
18
 
      void write(const byte data[], u32bit length)
19
 
         {
20
 
         write_str(std::string((const char*)data, length));
21
 
         }
22
 
 
23
 
      void send_str(const std::string& str)
24
 
         {
25
 
         printf("Py_Filter::send_str\n");
26
 
         send((const byte*)str.data(), str.length());
27
 
         }
28
 
   };
29
 
 
30
 
class FilterWrapper : public Py_Filter, public wrapper<Py_Filter>
31
 
   {
32
 
   public:
33
 
      void start_msg()
34
 
         {
35
 
         printf("wrapper start_msg\n");
36
 
         if(override start_msg = this->get_override("start_msg"))
37
 
            start_msg();
38
 
         }
39
 
 
40
 
      void end_msg()
41
 
         {
42
 
         printf("wrapper end_msg\n");
43
 
         if(override end_msg = this->get_override("end_msg"))
44
 
            end_msg();
45
 
         }
46
 
 
47
 
      void default_start_msg() {}
48
 
      void default_end_msg() {}
49
 
 
50
 
      virtual void write_str(const std::string& str)
51
 
         {
52
 
         printf("wrapper write\n");
53
 
         this->get_override("write")(str);
54
 
         }
55
 
   };
56
 
 
57
 
Filter* return_or_raise(Filter* filter, const std::string& name)
58
 
   {
59
 
   if(filter)
60
 
      return filter;
61
 
   throw Invalid_Argument("Filter " + name + " could not be found");
62
 
   }
63
 
 
64
 
Filter* make_filter1(const std::string& name)
65
 
   {
66
 
   Filter* filter = 0;
67
 
 
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;
73
 
 
74
 
   return return_or_raise(filter, name);
75
 
   }
76
 
 
77
 
Filter* make_filter2(const std::string& name,
78
 
                     const SymmetricKey& key)
79
 
   {
80
 
   Filter* filter = 0;
81
 
 
82
 
   if(have_mac(name))
83
 
      filter = new MAC_Filter(name, key);
84
 
   else if(have_stream_cipher(name))
85
 
      filter = new StreamCipher_Filter(name, key);
86
 
 
87
 
   return return_or_raise(filter, name);
88
 
   }
89
 
 
90
 
// FIXME: add new wrapper for Keyed_Filter here
91
 
Filter* make_filter3(const std::string& name,
92
 
                     const SymmetricKey& key,
93
 
                     Cipher_Dir direction)
94
 
   {
95
 
   return return_or_raise(get_cipher(name, key, direction), name);
96
 
   }
97
 
 
98
 
Filter* make_filter4(const std::string& name,
99
 
                     const SymmetricKey& key,
100
 
                     const InitializationVector& iv,
101
 
                     Cipher_Dir direction)
102
 
   {
103
 
   return return_or_raise(get_cipher(name, key, iv, direction), name);
104
 
   }
105
 
 
106
 
void append_filter(Pipe& pipe, std::auto_ptr<Filter> filter)
107
 
   {
108
 
   pipe.append(filter.get());
109
 
   filter.release();
110
 
   }
111
 
 
112
 
void prepend_filter(Pipe& pipe, std::auto_ptr<Filter> filter)
113
 
   {
114
 
   pipe.prepend(filter.get());
115
 
   filter.release();
116
 
   }
117
 
 
118
 
void do_send(std::auto_ptr<FilterWrapper> filter, const std::string& data)
119
 
   {
120
 
   printf("Sending %s to %p\n", data.c_str(), filter.get());
121
 
   filter->send_str(data);
122
 
   }
123
 
 
124
 
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(rallas_ovls, read_all_as_string, 0, 1)
125
 
 
126
 
void export_filters()
127
 
   {
128
 
   class_<Filter, std::auto_ptr<Filter>, boost::noncopyable>
129
 
      ("__Internal_FilterObj", no_init);
130
 
 
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>());
139
 
 
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>
144
 
      ("FilterObj")
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);
149
 
 
150
 
   implicitly_convertible<std::auto_ptr<FilterWrapper>,
151
 
                          std::auto_ptr<Filter> >();
152
 
 
153
 
   void (Pipe::*pipe_write_str)(const std::string&) = &Pipe::write;
154
 
   void (Pipe::*pipe_process_str)(const std::string&) = &Pipe::process_msg;
155
 
 
156
 
   class_<Pipe, boost::noncopyable>("PipeObj")
157
 
      .def(init<>())
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());
172
 
   }