aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.cpp
blob: 8bbad670da18d50d1c98e76abb814faa80c5d2f1 (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
/*
* (C) 2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

/*
 * Test Driver for Botan
 */

#include <vector>
#include <string>

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <limits>
#include <memory>

#include <botan/init.h>
#include <botan/version.h>
#include <botan/auto_rng.h>
#include <botan/cpuid.h>
#include <botan/http_util.h>

using namespace Botan;

#include "common.h"
#include "speed/speed.h"
#include "tests/tests.h"
#include "apps/apps.h"

// from common.h
void strip_comments(std::string& line)
   {
   if(line.find('#') != std::string::npos)
      line = line.erase(line.find('#'), std::string::npos);
   }

void strip_newlines(std::string& line)
   {
   while(line.find('\n') != std::string::npos)
      line = line.erase(line.find('\n'), 1);
   }

/* Strip comments, whitespace, etc */
void strip(std::string& line)
   {
   strip_comments(line);

#if 0
   while(line.find(' ') != std::string::npos)
      line = line.erase(line.find(' '), 1);
#endif

   while(line.find('\t') != std::string::npos)
      line = line.erase(line.find('\t'), 1);
   }

std::vector<std::string> parse(const std::string& line)
   {
   const char DELIMITER = ':';
   std::vector<std::string> substr;
   std::string::size_type start = 0, end = line.find(DELIMITER);
   while(end != std::string::npos)
      {
      substr.push_back(line.substr(start, end-start));
      start = end+1;
      end = line.find(DELIMITER, start);
      }
   if(line.size() > start)
      substr.push_back(line.substr(start));
   while(substr.size() <= 4) // at least 5 substr, some possibly empty
      substr.push_back("");
   return substr;
   }

namespace {

int help(int , char* argv[])
   {
   std::cout << "Usage: " << argv[0] << " subcommand\n";
   std::cout << "Common commands: help version test speed\n";
   std::cout << "Other commands: cpuid bcrypt x509 factor tls_client asn1 base64 hash self_sig\n";
   return 1;
   }

}

int main(int argc, char* argv[])
   {
   if(BOTAN_VERSION_MAJOR != version_major() ||
      BOTAN_VERSION_MINOR != version_minor() ||
      BOTAN_VERSION_PATCH != version_patch())
      {
      std::cout << "Warning: linked version ("
                << version_major() << '.'
                << version_minor() << '.'
                << version_patch()
                << ") does not match version built against ("
                << BOTAN_VERSION_MAJOR << '.'
                << BOTAN_VERSION_MINOR << '.'
                << BOTAN_VERSION_PATCH << ")\n";
      }

   try
      {
      Botan::LibraryInitializer init;

      if(argc < 2)
         return help(argc, argv);

      const std::string cmd = argv[1];

      if(cmd == "help")
         return help(argc, argv);

      if(cmd == "version")
         {
         std::cout << Botan::version_string() << "\n";
         return 0;
         }

      if(cmd == "cpuid")
         {
         CPUID::print(std::cout);
         return 0;
         }

      if(cmd == "test")
         return test_main(argc - 1, argv + 1);

      if(cmd == "speed")
         return speed_main(argc - 1, argv + 1);

      if(cmd == "http_get")
         {
         auto resp = HTTP::GET_sync(argv[2]);
         std::cout << resp << "\n";
         }

#define CALL_CMD(cmdsym)                           \
      do { if(cmd == #cmdsym) { return cmdsym (argc - 1, argv + 1); } } while(0)

      CALL_CMD(asn1);
      CALL_CMD(base64);
      CALL_CMD(bcrypt);
      CALL_CMD(bzip);
      CALL_CMD(ca);
      CALL_CMD(factor);
      CALL_CMD(fpe);
      CALL_CMD(hash);
      CALL_CMD(keygen);
      CALL_CMD(dsa_sign);
      CALL_CMD(dsa_verify);
      CALL_CMD(pkcs10);
      CALL_CMD(read_ssh);
      CALL_CMD(self_sig);
      CALL_CMD(tls_client);
      CALL_CMD(tls_server);
      CALL_CMD(x509);
      }
   catch(std::exception& e)
      {
      std::cerr << "Exception: " << e.what() << std::endl;
      return 1;
      }
   catch(...)
      {
      std::cerr << "Unknown (...) exception caught" << std::endl;
      return 1;
      }

   return 0;
   }