aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/apps/apps.h25
-rw-r--r--src/apps/asn1.cpp (renamed from src/examples/asn1.cpp)4
-rw-r--r--src/apps/base64.cpp (renamed from doc/examples/base64.cpp)21
-rw-r--r--src/apps/bcrypt.cpp (renamed from src/examples/bcrypt.cpp)4
-rw-r--r--src/apps/bzip.cpp (renamed from doc/examples/bzip.cpp)10
-rw-r--r--src/apps/ca.cpp (renamed from doc/examples/ca.cpp)7
-rw-r--r--src/apps/credentials.h (renamed from doc/examples/credentials.h)29
-rw-r--r--src/apps/factor.cpp (renamed from src/examples/factor.cpp)4
-rw-r--r--src/apps/fpe.cpp (renamed from src/examples/fpe.cpp)4
-rw-r--r--src/apps/hash.cpp (renamed from doc/examples/hash.cpp)16
-rw-r--r--src/apps/keygen.cpp (renamed from doc/examples/rsa_kgen.cpp)7
-rw-r--r--src/apps/pkcs10.cpp (renamed from doc/examples/pkcs10.cpp)11
-rw-r--r--src/apps/read_ssh.cpp (renamed from src/examples/read_ssh.cpp)4
-rw-r--r--src/apps/self_sig.cpp (renamed from src/examples/self_sig.cpp)4
-rw-r--r--src/apps/tls_client.cpp (renamed from doc/examples/tls_client.cpp)11
-rw-r--r--src/apps/tls_server.cpp (renamed from doc/examples/tls_server.cpp)35
-rw-r--r--src/apps/x509print.cpp (renamed from src/examples/x509print.cpp)4
-rw-r--r--src/build-data/cc/gcc.txt2
-rw-r--r--src/examples/examples.h17
-rw-r--r--src/main.cpp116
-rw-r--r--src/speed/speed.cpp40
-rw-r--r--src/speed/speed.h2
22 files changed, 202 insertions, 175 deletions
diff --git a/src/apps/apps.h b/src/apps/apps.h
new file mode 100644
index 000000000..babbea080
--- /dev/null
+++ b/src/apps/apps.h
@@ -0,0 +1,25 @@
+
+#include "../common.h"
+#include <botan/auto_rng.h>
+#include <botan/hex.h>
+#include <iostream>
+
+using namespace Botan;
+
+#define DEFINE_EXAMPLE(cmd) int cmd (int argc, char* argv[]);
+
+DEFINE_EXAMPLE(asn1);
+DEFINE_EXAMPLE(bcrypt);
+DEFINE_EXAMPLE(bzip);
+DEFINE_EXAMPLE(base64);
+DEFINE_EXAMPLE(ca);
+DEFINE_EXAMPLE(factor);
+DEFINE_EXAMPLE(fpe);
+DEFINE_EXAMPLE(hash);
+DEFINE_EXAMPLE(keygen);
+DEFINE_EXAMPLE(pkcs10);
+DEFINE_EXAMPLE(read_ssh);
+DEFINE_EXAMPLE(self_sig);
+DEFINE_EXAMPLE(tls_client);
+DEFINE_EXAMPLE(tls_server);
+DEFINE_EXAMPLE(x509);
diff --git a/src/examples/asn1.cpp b/src/apps/asn1.cpp
index 40f597fc3..447462ae0 100644
--- a/src/examples/asn1.cpp
+++ b/src/apps/asn1.cpp
@@ -1,4 +1,4 @@
-#include "examples.h"
+#include "apps.h"
#include <botan/bigint.h>
#include <botan/der_enc.h>
@@ -47,7 +47,7 @@ std::string url_encode(const std::vector<byte>& in)
}
-int asn1_example(int argc, char* argv[])
+int asn1(int argc, char* argv[])
{
if(argc != 2)
{
diff --git a/doc/examples/base64.cpp b/src/apps/base64.cpp
index dbe8d19e3..697fcb42c 100644
--- a/doc/examples/base64.cpp
+++ b/src/apps/base64.cpp
@@ -5,15 +5,17 @@
* Distributed under the terms of the Botan license
*/
+#include "apps.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <cstdlib>
-#include <botan/botan.h>
+#include <botan/b64_filt.h>
+#include <botan/pipe.h>
-int main(int argc, char* argv[])
+int base64(int argc, char* argv[])
{
if(argc < 2)
{
@@ -25,14 +27,12 @@ int main(int argc, char* argv[])
return 1;
}
- Botan::LibraryInitializer init;
-
int column = 78;
bool wrap = false;
bool encoding = true;
std::vector<std::string> files;
- for(int j = 1; argv[j] != 0; j++)
+ for(int j = 1; argv[j] != nullptr; j++)
{
std::string this_arg = argv[j];
@@ -66,9 +66,14 @@ int main(int argc, char* argv[])
continue;
}
- Botan::Pipe pipe((encoding) ?
- ((Botan::Filter*)new Botan::Base64_Encoder(wrap, column)) :
- ((Botan::Filter*)new Botan::Base64_Decoder));
+ Botan::Filter* f = nullptr;
+
+ if(encoding)
+ f = new Botan::Base64_Encoder(wrap, column);
+ else
+ f = new Botan::Base64_Decoder;
+
+ Botan::Pipe pipe(f);
pipe.start_msg();
*stream >> pipe;
pipe.end_msg();
diff --git a/src/examples/bcrypt.cpp b/src/apps/bcrypt.cpp
index 50205cd4d..497111363 100644
--- a/src/examples/bcrypt.cpp
+++ b/src/apps/bcrypt.cpp
@@ -1,7 +1,7 @@
-#include "examples.h"
+#include "apps.h"
#include <botan/bcrypt.h>
-int bcrypt_example(int argc, char* argv[])
+int bcrypt(int argc, char* argv[])
{
if(argc == 2)
{
diff --git a/doc/examples/bzip.cpp b/src/apps/bzip.cpp
index 74ba431ed..94a8bdff7 100644
--- a/doc/examples/bzip.cpp
+++ b/src/apps/bzip.cpp
@@ -5,6 +5,8 @@
* Distributed under the terms of the Botan license
*/
+#include "apps.h"
+
#include <string>
#include <cstring>
#include <vector>
@@ -26,7 +28,7 @@
const std::string SUFFIX = ".bz2";
-int main(int argc, char* argv[])
+int bzip(int argc, char* argv[])
{
if(argc < 2)
{
@@ -35,8 +37,6 @@ int main(int argc, char* argv[])
return 1;
}
- Botan::LibraryInitializer init;
-
#ifdef BOTAN_HAS_COMPRESSOR_BZIP2
std::vector<std::string> files;
bool decompress = false, small = false;
@@ -98,6 +98,7 @@ int main(int argc, char* argv[])
in.close();
out.close();
+ return 0;
}
}
catch(std::exception& e)
@@ -108,8 +109,7 @@ int main(int argc, char* argv[])
#else
std::cout << "Sorry, support for bzip2 not compiled into Botan\n";
-
#endif
- return 0;
+ return 1;
}
diff --git a/doc/examples/ca.cpp b/src/apps/ca.cpp
index 6fd2eb15b..881036fc8 100644
--- a/doc/examples/ca.cpp
+++ b/src/apps/ca.cpp
@@ -1,4 +1,5 @@
-#include <botan/botan.h>
+
+#include "apps.h"
#include <botan/x509_ca.h>
using namespace Botan;
@@ -6,7 +7,7 @@ using namespace Botan;
#include <memory>
#include <chrono>
-int main(int argc, char* argv[])
+int ca(int argc, char* argv[])
{
if(argc != 5)
{
@@ -15,8 +16,6 @@ int main(int argc, char* argv[])
return 1;
}
- Botan::LibraryInitializer init;
-
try
{
const std::string arg_passphrase = argv[1];
diff --git a/doc/examples/credentials.h b/src/apps/credentials.h
index c2b0fdaed..6c150f881 100644
--- a/doc/examples/credentials.h
+++ b/src/apps/credentials.h
@@ -13,8 +13,8 @@
#include <fstream>
#include <memory>
-bool value_exists(const std::vector<std::string>& vec,
- const std::string& val)
+inline bool value_exists(const std::vector<std::string>& vec,
+ const std::string& val)
{
for(size_t i = 0; i != vec.size(); ++i)
if(vec[i] == val)
@@ -42,25 +42,20 @@ class Credentials_Manager_Simple : public Botan::Credentials_Manager
bool attempt_srp(const std::string& type,
const std::string& hostname)
{
- if(hostname == "srp-host")
+ if(hostname == "srp-host" && (type == "tls-client" || type == "tls-server"))
return true;
return false;
}
std::vector<Botan::Certificate_Store*>
trusted_certificate_authorities(const std::string& type,
- const std::string& hostname)
+ const std::string& /*hostname*/)
{
+ // don't ask for client cert
if(type == "tls-server")
- {
- // don't ask for client cert
return std::vector<Botan::Certificate_Store*>();
- }
- else
- {
- return m_certstores;
- }
+ return m_certstores;
}
void verify_certificate_chain(
@@ -90,7 +85,7 @@ class Credentials_Manager_Simple : public Botan::Credentials_Manager
return "";
}
- bool srp_verifier(const std::string& type,
+ bool srp_verifier(const std::string& /*type*/,
const std::string& context,
const std::string& identifier,
std::string& group_id,
@@ -106,7 +101,7 @@ class Credentials_Manager_Simple : public Botan::Credentials_Manager
return false;
pass.resize(16);
- rng.randomize((Botan::byte*)&pass[0], pass.size());
+ rng.randomize(reinterpret_cast<byte*>(&pass[0]), pass.size());
}
group_id = "modp/srp/2048";
@@ -132,7 +127,7 @@ class Credentials_Manager_Simple : public Botan::Credentials_Manager
std::string psk_identity(const std::string&, const std::string&,
const std::string& identity_hint)
{
- //return "lloyd";
+ std::cout << "Server sent PSK identity_hint " << identity_hint << "\n";
return "Client_identity";
}
@@ -187,7 +182,7 @@ class Credentials_Manager_Simple : public Botan::Credentials_Manager
opts.email = "root@" + hostname;
opts.dns = hostname;
- std::auto_ptr<Private_Key> key;
+ std::unique_ptr<Private_Key> key;
if(key_type == "rsa")
key.reset(new RSA_PrivateKey(rng, 1024));
else if(key_type == "dsa")
@@ -280,8 +275,8 @@ class Credentials_Manager_Simple : public Botan::Credentials_Manager
}
Botan::Private_Key* private_key_for(const Botan::X509_Certificate& cert,
- const std::string& type,
- const std::string& context)
+ const std::string& /*type*/,
+ const std::string& /*context*/)
{
return certs_and_keys[cert];
}
diff --git a/src/examples/factor.cpp b/src/apps/factor.cpp
index 822b62b71..484f024d8 100644
--- a/src/examples/factor.cpp
+++ b/src/apps/factor.cpp
@@ -7,7 +7,7 @@
* primes, and Pollard's Rho algorithm
*/
-#include "examples.h"
+#include "apps.h"
#include <botan/reducer.h>
#include <botan/numthry.h>
@@ -121,7 +121,7 @@ std::vector<BigInt> factorize(const BigInt& n_in,
}
-int factor_example(int argc, char* argv[])
+int factor(int argc, char* argv[])
{
if(argc != 2)
{
diff --git a/src/examples/fpe.cpp b/src/apps/fpe.cpp
index cccb602fd..dd5b9b682 100644
--- a/src/examples/fpe.cpp
+++ b/src/apps/fpe.cpp
@@ -1,4 +1,4 @@
-#include "examples.h"
+#include "apps.h"
#include <botan/fpe_fe1.h>
#include <botan/sha160.h>
@@ -102,7 +102,7 @@ u64bit decrypt_cc_number(u64bit enc_cc,
}
-int fpe_example(int argc, char* argv[])
+int fpe(int argc, char* argv[])
{
if(argc != 4)
{
diff --git a/doc/examples/hash.cpp b/src/apps/hash.cpp
index 1a4ca1b64..c1e5aab3e 100644
--- a/doc/examples/hash.cpp
+++ b/src/apps/hash.cpp
@@ -4,11 +4,14 @@
* Distributed under the terms of the Botan license
*/
+#include "apps.h"
+#include <botan/lookup.h>
#include <iostream>
#include <fstream>
-#include <botan/botan.h>
-int main(int argc, char* argv[])
+using namespace Botan;
+
+int hash(int argc, char* argv[])
{
if(argc < 3)
{
@@ -16,25 +19,22 @@ int main(int argc, char* argv[])
return 1;
}
- Botan::LibraryInitializer init;
-
std::string hash = argv[1];
/* a couple of special cases, kind of a crock */
if(hash == "sha1") hash = "SHA-1";
if(hash == "md5") hash = "MD5";
try {
- if(!Botan::have_hash(hash))
+ if(!have_hash(hash))
{
std::cout << "Unknown hash \"" << argv[1] << "\"" << std::endl;
return 1;
}
- Botan::Pipe pipe(new Botan::Hash_Filter(hash),
- new Botan::Hex_Encoder);
+ Pipe pipe(new Hash_Filter(hash), new Hex_Encoder);
int skipped = 0;
- for(int j = 2; argv[j] != 0; j++)
+ for(int j = 2; argv[j] != nullptr; j++)
{
std::ifstream file(argv[j], std::ios::binary);
if(!file)
diff --git a/doc/examples/rsa_kgen.cpp b/src/apps/keygen.cpp
index a1f0fe71d..885364834 100644
--- a/doc/examples/rsa_kgen.cpp
+++ b/src/apps/keygen.cpp
@@ -1,14 +1,14 @@
+#include "apps.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <memory>
-#include <botan/botan.h>
#include <botan/rsa.h>
using namespace Botan;
-int main(int argc, char* argv[])
+int keygen(int argc, char* argv[])
{
if(argc != 2 && argc != 3)
{
@@ -26,8 +26,6 @@ int main(int argc, char* argv[])
try
{
- Botan::LibraryInitializer init;
-
std::ofstream pub("rsapub.pem");
std::ofstream priv("rsapriv.pem");
if(!priv || !pub)
@@ -50,5 +48,6 @@ int main(int argc, char* argv[])
{
std::cout << "Exception caught: " << e.what() << std::endl;
}
+
return 0;
}
diff --git a/doc/examples/pkcs10.cpp b/src/apps/pkcs10.cpp
index b5ad1d1dd..6e36f73fb 100644
--- a/doc/examples/pkcs10.cpp
+++ b/src/apps/pkcs10.cpp
@@ -1,5 +1,4 @@
-#include <botan/init.h>
-#include <botan/auto_rng.h>
+#include "apps.h"
#include <botan/x509self.h>
#include <botan/rsa.h>
#include <botan/dsa.h>
@@ -9,17 +8,15 @@ using namespace Botan;
#include <fstream>
#include <memory>
-int main(int argc, char* argv[])
+int pkcs10(int argc, char* argv[])
{
if(argc != 6)
{
- std::cout << "Usage: " << argv[0] <<
- " passphrase name country_code organization email" << std::endl;
+ std::cout << "Usage: " << argv[0]
+ << " passphrase name country_code organization email" << std::endl;
return 1;
}
- Botan::LibraryInitializer init;
-
try
{
AutoSeeded_RNG rng;
diff --git a/src/examples/read_ssh.cpp b/src/apps/read_ssh.cpp
index 83f7ff89e..ce72fa064 100644
--- a/src/examples/read_ssh.cpp
+++ b/src/apps/read_ssh.cpp
@@ -8,7 +8,7 @@
* Example of reading SSH2 format public keys (see RFC 4716)
*/
-#include "examples.h"
+#include "apps.h"
#include <botan/x509_key.h>
#include <botan/filters.h>
@@ -111,7 +111,7 @@ Public_Key* read_ssh_pubkey(const std::string& file)
}
-int read_ssh_example(int argc, char* argv[])
+int read_ssh(int argc, char* argv[])
{
if(argc != 2)
{
diff --git a/src/examples/self_sig.cpp b/src/apps/self_sig.cpp
index 89d9f84e7..c4202442c 100644
--- a/src/examples/self_sig.cpp
+++ b/src/apps/self_sig.cpp
@@ -1,4 +1,4 @@
-#include "examples.h"
+#include "apps.h"
#include <botan/x509self.h>
#include <botan/rsa.h>
#include <botan/dsa.h>
@@ -8,7 +8,7 @@ using namespace Botan;
#include <fstream>
#include <memory>
-int self_sig_example(int argc, char* argv[])
+int self_sig(int argc, char* argv[])
{
if(argc != 7)
{
diff --git a/doc/examples/tls_client.cpp b/src/apps/tls_client.cpp
index 4023baba8..24c8197f6 100644
--- a/doc/examples/tls_client.cpp
+++ b/src/apps/tls_client.cpp
@@ -1,4 +1,4 @@
-#include <botan/botan.h>
+#include "apps.h"
#include <botan/tls_client.h>
#include <botan/pkcs8.h>
#include <botan/hex.h>
@@ -30,6 +30,8 @@ using namespace Botan;
using namespace std::placeholders;
+namespace {
+
int connect_to_host(const std::string& host, u16bit port, const std::string& transport)
{
hostent* host_addr = ::gethostbyname(host.c_str());
@@ -109,7 +111,7 @@ void stream_socket_write(int sockfd, const byte buf[], size_t length)
bool got_alert = false;
-void alert_received(TLS::Alert alert, const byte buf[], size_t buf_size)
+void alert_received(TLS::Alert alert, const byte [], size_t )
{
std::cout << "Alert: " << alert.type_string() << "\n";
got_alert = true;
@@ -128,7 +130,9 @@ std::string protocol_chooser(const std::vector<std::string>& protocols)
return "http/1.1";
}
-int main(int argc, char* argv[])
+}
+
+int tls_client(int argc, char* argv[])
{
if(argc != 2 && argc != 3 && argc != 4)
{
@@ -138,7 +142,6 @@ int main(int argc, char* argv[])
try
{
- LibraryInitializer botan_init;
AutoSeeded_RNG rng;
TLS::Policy policy;
diff --git a/doc/examples/tls_server.cpp b/src/apps/tls_server.cpp
index 963e03313..cb212fef6 100644
--- a/doc/examples/tls_server.cpp
+++ b/src/apps/tls_server.cpp
@@ -1,4 +1,4 @@
-#include <botan/botan.h>
+#include "apps.h"
#include <botan/tls_server.h>
#include <botan/hex.h>
@@ -32,6 +32,8 @@ using namespace std::placeholders;
#define MSG_NOSIGNAL 0
#endif
+namespace {
+
int make_server_socket(const std::string& transport, u16bit port)
{
int type = (transport == "tcp") ? SOCK_STREAM : SOCK_DGRAM;
@@ -85,9 +87,9 @@ void dgram_socket_write(int sockfd, const byte buf[], size_t length)
ssize_t sent = ::send(sockfd, buf, length, MSG_NOSIGNAL);
if(sent == -1)
- printf("Error writing to socket %s\n", strerror(errno));
- else if(sent != length)
- printf("Note: packet of length %d truncated to %d\n", length, sent);
+ std::cout << "Error writing to socket - " << strerror(errno) << "\n";
+ else if(sent != static_cast<ssize_t>(length))
+ std::cout << "Packet of length " << length << " truncated to " << sent << "\n";
}
void stream_socket_write(int sockfd, const byte buf[], size_t length)
@@ -117,7 +119,9 @@ void alert_received(TLS::Alert alert, const byte buf[], size_t buf_size)
std::cout << "Alert: " << alert.type_string() << "\n";
}
-int main(int argc, char* argv[])
+}
+
+int tls_server(int argc, char* argv[])
{
int port = 4433;
std::string transport = "tcp";
@@ -129,8 +133,6 @@ int main(int argc, char* argv[])
try
{
- LibraryInitializer botan_init;
-
AutoSeeded_RNG rng;
TLS::Policy policy;
@@ -146,7 +148,7 @@ int main(int argc, char* argv[])
*/
const std::vector<std::string> protocols = { "echo/1.0", "echo/1.1" };
- printf("Listening for new connection on %s port %d\n", transport.c_str(), port);
+ std::cout << "Listening for new connections on " << transport << " port " << port << "\n";
int server_fd = make_server_socket(transport, port);
@@ -173,7 +175,7 @@ int main(int argc, char* argv[])
fd = server_fd;
}
- printf("New connection received\n");
+ std::cout << "New connection received\n";
auto socket_write =
(transport == "tcp") ?
@@ -212,17 +214,17 @@ int main(int argc, char* argv[])
while(!server.is_closed())
{
byte buf[4*1024] = { 0 };
- size_t got = ::read(fd, buf, sizeof(buf));
+ ssize_t got = ::read(fd, buf, sizeof(buf));
if(got == -1)
{
- printf("Error in socket read %s\n", strerror(errno));
+ std::cout << "Error in socket read - " << strerror(errno) << "\n";
break;
}
if(got == 0)
{
- printf("EOF on socket\n");
+ std::cout << "EOF on socket\n";
break;
}
@@ -243,13 +245,18 @@ int main(int argc, char* argv[])
::close(fd);
}
- catch(std::exception& e) { printf("Connection problem: %s\n", e.what()); }
+ catch(std::exception& e)
+ {
+ std::cout << "Connection problem: " << e.what() << "\n";
+ return 1;
+ }
}
}
catch(std::exception& e)
{
- printf("%s\n", e.what());
+ std::cout << e.what() << "\n";
return 1;
}
+
return 0;
}
diff --git a/src/examples/x509print.cpp b/src/apps/x509print.cpp
index 88e1dfbaa..c79ae7a6b 100644
--- a/src/examples/x509print.cpp
+++ b/src/apps/x509print.cpp
@@ -1,7 +1,7 @@
-#include "examples.h"
+#include "apps.h"
#include <botan/x509cert.h>
-int x509_example(int argc, char* argv[])
+int x509(int argc, char* argv[])
{
if(argc < 1)
{
diff --git a/src/build-data/cc/gcc.txt b/src/build-data/cc/gcc.txt
index fe89512c0..be51a46ad 100644
--- a/src/build-data/cc/gcc.txt
+++ b/src/build-data/cc/gcc.txt
@@ -9,7 +9,7 @@ add_lib_dir_option -L
add_lib_option -l
lang_flags "-std=c++11 -D_REENTRANT -fstack-protector"
-maintainer_warning_flags "-Werror -Wno-error=old-style-cast -Wno-error=zero-as-null-pointer-constant"
+maintainer_warning_flags "-Werror -Wno-error=old-style-cast -Wno-error=zero-as-null-pointer-constant -Wno-error=unused-parameter"
warning_flags "-Wall -Wextra -Wstrict-aliasing -Wstrict-overflow=5 -Wcast-align -Wmissing-declarations -Wpointer-arith -Wcast-qual -Wold-style-cast -Wzero-as-null-pointer-constant"
lib_opt_flags "-O3"
diff --git a/src/examples/examples.h b/src/examples/examples.h
deleted file mode 100644
index 2a9a361c7..000000000
--- a/src/examples/examples.h
+++ /dev/null
@@ -1,17 +0,0 @@
-
-#include "../common.h"
-#include <botan/auto_rng.h>
-#include <botan/hex.h>
-#include <iostream>
-
-using namespace Botan;
-
-int asn1_example(int argc, char* argv[]);
-int bcrypt_example(int argc, char* argv[]);
-int factor_example(int argc, char* argv[]);
-int fpe_example(int argc, char* argv[]);
-int read_ssh_example(int argc, char* argv[]);
-
-int self_sig_example(int argc, char* argv[]);
-
-int x509_example(int argc, char* argv[]);
diff --git a/src/main.cpp b/src/main.cpp
index baf37e895..956413d4c 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -29,7 +29,7 @@ using namespace Botan;
#include "common.h"
#include "speed/speed.h"
#include "tests/tests.h"
-#include "examples/examples.h"
+#include "apps/apps.h"
// from common.h
void strip_comments(std::string& line)
@@ -76,6 +76,18 @@ std::vector<std::string> parse(const std::string& line)
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() ||
@@ -94,37 +106,36 @@ int main(int argc, char* argv[])
try
{
- OptionParser opts("help|test|validate|"
- "algo=|seconds=|buf-size=");
- opts.parse(argv);
-
Botan::LibraryInitializer init;
- if(opts.is_set("help") || argc < 2)
- {
- std::cout << "Commands: test version time bcrypt\n";
- return 1;
- }
+ 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")
{
const size_t failures = run_all_tests();
return failures ? 1 : 0;
}
- if(cmd == "cpuid")
- {
- CPUID::print(std::cout);
- return 0;
- }
+ if(cmd == "speed")
+ return speed_main(argc - 1, argv + 1);
if(cmd == "http_get")
{
@@ -132,63 +143,24 @@ int main(int argc, char* argv[])
std::cout << resp << "\n";
}
- if(cmd == "bcrypt")
- return bcrypt_example(argc - 1, argv + 1);
-
- if(cmd == "fpe")
- return fpe_example(argc - 1, argv + 1);
-
- if(cmd == "read_ssh_key")
- return read_ssh_example(argc - 1, argv + 1);
-
- if(cmd == "self_sig")
- return self_sig_example(argc - 1, argv + 1);
-
- if(cmd == "x509")
- return x509_example(argc - 1, argv + 1);
-
- if(cmd == "factor")
- return factor_example(argc - 1, argv + 1);
-
- if(cmd == "asn1")
- return asn1_example(argc - 1, argv + 1);
-
- if(cmd == "speed")
- {
- double seconds = 5;
- u32bit buf_size = 16;
-
- if(opts.is_set("seconds"))
- {
- seconds = std::atof(opts.value("seconds").c_str());
- if(seconds < 0.1 || seconds > (5 * 60))
- {
- std::cout << "Invalid argument to --seconds\n";
- return 2;
- }
- }
-
- if(opts.is_set("buf-size"))
- {
- buf_size = std::atoi(opts.value("buf-size").c_str());
- if(buf_size == 0 || buf_size > 1024)
- {
- std::cout << "Invalid argument to --buf-size\n";
- return 2;
- }
- }
-
- if(opts.is_set("--algo"))
- {
- AutoSeeded_RNG rng;
- for(auto alg: Botan::split_on(opts.value("algo"), ','))
- bench_algo(alg, rng, seconds, buf_size);
- }
- /*
- else
- benchmark(seconds, buf_size);
- */
- }
+#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(pkcs10);
+ CALL_CMD(read_ssh);
+ CALL_CMD(self_sig);
+ CALL_CMD(tls_client);
+ CALL_CMD(tls_server);
+ CALL_CMD(x509);
}
catch(std::exception& e)
{
diff --git a/src/speed/speed.cpp b/src/speed/speed.cpp
index b8c14ee59..1cd3ec0f7 100644
--- a/src/speed/speed.cpp
+++ b/src/speed/speed.cpp
@@ -198,3 +198,43 @@ void benchmark(double seconds, size_t buf_size)
for(size_t i = 0; algos[i] != ""; ++i)
bench_algo(algos[i], rng, seconds, buf_size);
}
+
+int speed_main(int , char* argv[])
+ {
+ OptionParser opts("algo=|seconds=|buf-size=");
+ opts.parse(argv);
+
+ double seconds = .5;
+ u32bit buf_size = 16;
+
+ if(opts.is_set("seconds"))
+ {
+ seconds = std::atof(opts.value("seconds").c_str());
+ if(seconds < 0.1 || seconds > (5 * 60))
+ {
+ std::cout << "Invalid argument to --seconds\n";
+ return 2;
+ }
+ }
+
+ if(opts.is_set("buf-size"))
+ {
+ buf_size = std::atoi(opts.value("buf-size").c_str());
+ if(buf_size == 0 || buf_size > 1024)
+ {
+ std::cout << "Invalid argument to --buf-size\n";
+ return 2;
+ }
+ }
+
+ if(opts.is_set("algo"))
+ {
+ AutoSeeded_RNG rng;
+ for(auto alg: Botan::split_on(opts.value("algo"), ','))
+ bench_algo(alg, rng, seconds, buf_size);
+ }
+ else
+ benchmark(seconds, buf_size);
+
+ return 0;
+ }
diff --git a/src/speed/speed.h b/src/speed/speed.h
index d115960fb..ec0d06733 100644
--- a/src/speed/speed.h
+++ b/src/speed/speed.h
@@ -9,6 +9,8 @@
using namespace Botan;
+int speed_main(int argc, char* argv[]);
+
void benchmark(double seconds,
size_t buf_size);