aboutsummaryrefslogtreecommitdiffstats
path: root/examples/toolbox.cpp
blob: 622a1f56f451691f8d25c7c6121282c1b1460ed0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* (C) 2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/botan.h>

#if defined(BOTAN_HAS_COMPRESSOR_BZIP2)
  #include <botan/bzip2.h>
#endif

#if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
  #include <botan/zlib.h>
#endif

#include <iostream>
#include <fstream>

using namespace Botan;

#include "../../checks/getopt.h"

class Encoder_Decoder
   {
   public:
      Encoder_Decoder(const std::string& command,
                      bool decode,
                      const std::vector<std::string>& args) :
         type(command),
         args(args),
         decode(decode)
         { }

      void run(std::istream& in, std::ostream& out)
         {
         Filter* filt = decode ? decoder(type) : encoder(type);

         DataSource_Stream src(in);
         Pipe pipe(filt, new DataSink_Stream(out));
         pipe.process_msg(src);
         }

      Filter* encoder(const std::string& type) const
         {
         if(type == "hex") return new Hex_Encoder;
         if(type == "base64") return new Base64_Encoder;

#if defined(BOTAN_HAS_COMPRESSOR_BZIP2)
         if(type == "bzip2") return new Bzip_Compression;
#endif

#if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
         if(type == "zlib") return new Zlib_Compression;
#endif
         return 0;
         }

      Filter* decoder(const std::string& type) const
         {
         if(type == "hex") return new Hex_Decoder;
         if(type == "base64") return new Base64_Decoder;

#if defined(BOTAN_HAS_COMPRESSOR_BZIP2)
         if(type == "bzip2") return new Bzip_Decompression;
#endif

#if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
         if(type == "zlib") return new Zlib_Decompression;
#endif

         return 0;
         }

   private:
      std::string type;
      std::vector<std::string> args;
      bool decode;
   };

void run_command(const std::string& command,
                 const std::vector<std::string>& arguments,
                 const OptionParser& opts)
   {
   if(command == "hex" ||
      command == "base64" ||
      command == "bzip2" ||
      command == "zlib")
      {
      bool decode = opts.is_set("decode");

      std::string output = opts.value_or_else("output", "-");
      std::string input = opts.value_or_else("input", "-");

      Encoder_Decoder enc_dec(command, decode, arguments);

      try
         {
         if(output == "-")
            {
            if(input == "-")
               enc_dec.run(std::cin, std::cout);
            else
               {
               std::ifstream in(input.c_str());
               enc_dec.run(in, std::cout);
               }
            }
         else // output != "-"
            {
            std::ofstream out(output.c_str());

            if(input == "-")
               enc_dec.run(std::cin, out);
            else
               {
               std::ifstream in(input.c_str());
               enc_dec.run(in, out);
               }
            }
         }
      catch(Botan::Stream_IO_Error& e)
         {
         std::cout << "I/O failure - " << e.what() << '\n';
         }
      }
   else if(command == "hash" ||
           command == "sha1" ||
           command == "md5")
      {
      std::string hash;

      if(command == "md5")
         hash = "MD5";
      if(command == "sha1")
         hash = "SHA-160";
      else
         hash = opts.value_or_else("hash", "SHA-160"); // sha1 is default

      Pipe pipe(new Hash_Filter(get_hash(hash)),
                new Hex_Encoder(false, 0, Hex_Encoder::Lowercase));

      for(size_t i = 0; i != arguments.size(); ++i)
         {
         std::string file_name = arguments[i];

         u32bit previously = pipe.message_count();

         if(file_name == "-")
            {
            pipe.start_msg();
            std::cin >> pipe;
            pipe.end_msg();
            }
         else
            {
            std::ifstream in(file_name.c_str());
            if(in)
               {
               pipe.start_msg();
               in >> pipe;
               pipe.end_msg();
               }
            else
               std::cerr << "Could not read " << file_name << '\n';
            }

         if(pipe.message_count() > previously)
            std::cout << pipe.read_all_as_string(Pipe::LAST_MESSAGE) << "  "
                      << file_name << '\n';
         }

      }
   else
      {
      std::cerr << "Command " << command << " not known\n";
      }
   }

int main(int argc, char* argv[])
   {
   LibraryInitializer init;

   OptionParser opts("help|version|seconds=|"
                     "input=|output=|decode|"
                     "hash=|key=");

   try
      {
      opts.parse(argv);
      }
   catch(std::runtime_error& e)
      {
      std::cout << "Command line problem: " << e.what() << '\n';
      return 2;
      }

   if(opts.is_set("version") || argc <= 1)
      {
      std::cerr << "Botan Toolbox v" << version_string() << '\n';
      std::cerr << "Commands: hash hex base64 ";
#if defined(BOTAN_HAS_COMPRESSOR_BZIP2)
      std::cerr << "bzip2 ";
#endif

#if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
      std::cerr << "zlib ";
#endif

      std::cerr << "\n";

      return 0;
      }

   if(opts.is_set("help"))
      {
      std::vector<std::string> what = opts.leftovers();

      for(size_t i = 0; i != what.size(); ++i)
         std::cerr << what[i] << "? Never heard of it\n";
      return 0;
      }

   std::vector<std::string> args = opts.leftovers();

   if(args.size() == 0)
      return 0;

   std::string command = args[0];
   args.erase(args.begin());

   run_command(command, args, opts);

   return 0;
   }