diff options
author | Simon Warta <[email protected]> | 2015-12-08 19:56:19 +0100 |
---|---|---|
committer | Simon Warta <[email protected]> | 2015-12-08 22:02:28 +0100 |
commit | 368d087e3973db9d8c5b70443d31c27c03dfdbef (patch) | |
tree | 9059a52c70eee8b825d9fd7fa14194940853b86a /src/cmd | |
parent | 53fca25da331d168b0433ba2e41b06aae9d17f5c (diff) |
Replace C interfaces in cli apps with C++ interfaces
Diffstat (limited to 'src/cmd')
-rw-r--r-- | src/cmd/apps.h | 25 | ||||
-rw-r--r-- | src/cmd/asn1.cpp | 8 | ||||
-rw-r--r-- | src/cmd/base64.cpp | 21 | ||||
-rw-r--r-- | src/cmd/bcrypt.cpp | 16 | ||||
-rw-r--r-- | src/cmd/ca.cpp | 16 | ||||
-rw-r--r-- | src/cmd/cert_verify.cpp | 14 | ||||
-rw-r--r-- | src/cmd/compress.cpp | 22 | ||||
-rw-r--r-- | src/cmd/dl_group.cpp | 12 | ||||
-rw-r--r-- | src/cmd/dsa_sign.cpp | 14 | ||||
-rw-r--r-- | src/cmd/dsa_ver.cpp | 12 | ||||
-rw-r--r-- | src/cmd/factor.cpp | 8 | ||||
-rw-r--r-- | src/cmd/fpe.cpp | 12 | ||||
-rw-r--r-- | src/cmd/fuzzer.cpp | 16 | ||||
-rw-r--r-- | src/cmd/getopt.cpp | 15 | ||||
-rw-r--r-- | src/cmd/getopt.h | 10 | ||||
-rw-r--r-- | src/cmd/hash.cpp | 16 | ||||
-rw-r--r-- | src/cmd/is_prime.cpp | 12 | ||||
-rw-r--r-- | src/cmd/keygen.cpp | 5 | ||||
-rw-r--r-- | src/cmd/main.cpp | 47 | ||||
-rw-r--r-- | src/cmd/mce.cpp | 31 | ||||
-rw-r--r-- | src/cmd/ocsp.cpp | 10 | ||||
-rw-r--r-- | src/cmd/pkcs10.cpp | 16 | ||||
-rw-r--r-- | src/cmd/pkcs8.cpp | 9 | ||||
-rw-r--r-- | src/cmd/prime.cpp | 14 | ||||
-rw-r--r-- | src/cmd/rng.cpp | 20 | ||||
-rw-r--r-- | src/cmd/self_sig.cpp | 19 | ||||
-rw-r--r-- | src/cmd/speed.cpp | 17 | ||||
-rw-r--r-- | src/cmd/tls_client.cpp | 12 | ||||
-rw-r--r-- | src/cmd/tls_server.cpp | 14 | ||||
-rw-r--r-- | src/cmd/x509print.cpp | 8 |
30 files changed, 237 insertions, 234 deletions
diff --git a/src/cmd/apps.h b/src/cmd/apps.h index d56fab5ad..9f1f00ba2 100644 --- a/src/cmd/apps.h +++ b/src/cmd/apps.h @@ -15,34 +15,35 @@ using namespace Botan; -typedef std::function<int (int, char*[])> main_fn; +typedef std::function<int (std::vector<std::string>)> app_fn; class AppRegistrations { public: - void add(const std::string& name, main_fn fn) + void add(const std::string& name, app_fn fn) { - m_cmds[name] = fn; + m_apps[name] = fn; } bool has(const std::string& cmd) const { - return m_cmds.count(cmd) > 0; + return m_apps.count(cmd) > 0; } - std::set<std::string> all_apps() const + std::set<std::string> all_appnames() const { std::set<std::string> apps; - for(auto i : m_cmds) + for(auto i : m_apps) apps.insert(i.first); return apps; } - int run(const std::string& cmd, int argc, char* argv[]) const + // TODO: Remove redundancy cmd == args[0] + int run(const std::string& cmd, std::vector<std::string> args) const { - auto i = m_cmds.find(cmd); - if(i != m_cmds.end()) - return i->second(argc, argv); + const auto app = m_apps.find(cmd); + if(app != m_apps.end()) + return app->second(args); return -1; } @@ -55,7 +56,7 @@ class AppRegistrations class AppRegistration { public: - AppRegistration(const std::string& name, main_fn fn) + AppRegistration(const std::string& name, app_fn fn) { AppRegistrations::instance().add(name, fn); } @@ -64,7 +65,7 @@ class AppRegistrations private: AppRegistrations() {} - std::map<std::string, main_fn> m_cmds; + std::map<std::string, app_fn> m_apps; }; #define REGISTER_APP(nm) AppRegistrations::AppRegistration g_ ## nm ## _registration(#nm, nm) diff --git a/src/cmd/asn1.cpp b/src/cmd/asn1.cpp index 7264c1a6d..2aa94cc39 100644 --- a/src/cmd/asn1.cpp +++ b/src/cmd/asn1.cpp @@ -317,16 +317,16 @@ void decode(BER_Decoder& decoder, size_t level) } } -int asn1(int argc, char* argv[]) +int asn1(const std::vector<std::string> &args) { - if(argc != 2) + if(args.size() != 2) { - std::cout << "Usage: " << argv[0] << " <file>" << std::endl; + std::cout << "Usage: " << args[0] << " <file>" << std::endl; return 1; } try { - DataSource_Stream in(argv[1]); + DataSource_Stream in(args[1]); if(!PEM_Code::matches(in)) { diff --git a/src/cmd/base64.cpp b/src/cmd/base64.cpp index d9b2ffa3d..d2a9a1853 100644 --- a/src/cmd/base64.cpp +++ b/src/cmd/base64.cpp @@ -19,11 +19,11 @@ namespace { -int base64(int argc, char* argv[]) +int base64(const std::vector<std::string> &args) { - if(argc < 2) + if(args.size() < 2) { - std::cout << "Usage: " << argv[0] << " [-w] [-c n] [-e|-d] files...\n" + std::cout << "Usage: " << args[0] << " [-w] [-c n] [-e|-d] files...\n" " -e : Encode input to base64 strings (default)\n" " -d : Decode base64 input\n" " -w : Wrap lines\n" @@ -31,14 +31,14 @@ int base64(int argc, char* argv[]) return 1; } - int column = 78; + u32bit column = 78; bool wrap = false; bool encoding = true; std::vector<std::string> files; - for(int j = 1; argv[j] != nullptr; j++) + for(int j = 1; j < args.size(); j++) { - std::string this_arg = argv[j]; + const std::string this_arg = args[j]; if(this_arg == "-w") wrap = true; @@ -47,15 +47,18 @@ int base64(int argc, char* argv[]) encoding = false; else if(this_arg == "-c") { - if(argv[j+1]) - { column = atoi(argv[j+1]); j++; } + if(j+1 < args.size()) + { + column = to_u32bit(args[j+1]); + j++; + } else { std::cout << "No argument for -c option" << std::endl; return 1; } } - else files.push_back(argv[j]); + else files.push_back(args[j]); } for(unsigned int j = 0; j != files.size(); j++) diff --git a/src/cmd/bcrypt.cpp b/src/cmd/bcrypt.cpp index 3cf5f7516..81f7c536e 100644 --- a/src/cmd/bcrypt.cpp +++ b/src/cmd/bcrypt.cpp @@ -12,21 +12,21 @@ namespace { -int bcrypt(int argc, char* argv[]) +int bcrypt(const std::vector<std::string> &args) { - if(argc == 2) + if(args.size() == 2) { AutoSeeded_RNG rng; - const std::string password = argv[1]; + const std::string password = args[1]; std::cout << generate_bcrypt(password, rng, 12) << std::endl; return 0; } - else if(argc == 3) + else if(args.size() == 3) { - const std::string password = argv[1]; - const std::string hash = argv[2]; + const std::string password = args[1]; + const std::string hash = args[2]; if(hash.length() != 60) std::cout << "Note: bcrypt '" << hash << "' has wrong length and cannot be valid" << std::endl; @@ -37,8 +37,8 @@ int bcrypt(int argc, char* argv[]) return (ok ? 0 : 1); } - std::cout << "Usage: " << argv[0] << " password\n" - << " " << argv[0] << " password passhash" << std::endl; + std::cout << "Usage: " << args[0] << " password\n" + << " " << args[0] << " password passhash" << std::endl; return 1; } diff --git a/src/cmd/ca.cpp b/src/cmd/ca.cpp index 3b67d40fb..fb6d9582a 100644 --- a/src/cmd/ca.cpp +++ b/src/cmd/ca.cpp @@ -13,24 +13,24 @@ namespace { -int ca(int argc, char* argv[]) +int ca(const std::vector<std::string> &args) { using namespace Botan; - if(argc != 5) + if(args.size() != 5) { - std::cout << "Usage: " << argv[0] << " <passphrase> " + std::cout << "Usage: " << args[0] << " <passphrase> " << "<ca cert> <ca key> <pkcs10>" << std::endl; return 1; } + const std::string arg_passphrase = args[1]; + const std::string arg_ca_cert = args[2]; + const std::string arg_ca_key = args[3]; + const std::string arg_req_file = args[4]; + try { - const std::string arg_passphrase = argv[1]; - const std::string arg_ca_cert = argv[2]; - const std::string arg_ca_key = argv[3]; - const std::string arg_req_file = argv[4]; - AutoSeeded_RNG rng; X509_Certificate ca_cert(arg_ca_cert); diff --git a/src/cmd/cert_verify.cpp b/src/cmd/cert_verify.cpp index 15cd5b8f8..7a1bec983 100644 --- a/src/cmd/cert_verify.cpp +++ b/src/cmd/cert_verify.cpp @@ -13,22 +13,24 @@ namespace { -int cert_verify(int argc, char* argv[]) +int cert_verify(const std::vector<std::string> &args) { using namespace Botan; - if(argc <= 2) + if(args.size() <= 2) { - std::cout << "Usage: " << argv[0] << " subject.pem [CA certificates...]" << std::endl; + std::cout << "Usage: " << args[0] << " subject.pem [CA certificates...]" << std::endl; return 1; } - X509_Certificate subject_cert(argv[1]); + X509_Certificate subject_cert(args[1]); Certificate_Store_In_Memory certs; - for(size_t i = 2; argv[i]; ++i) - certs.add_certificate(X509_Certificate(argv[i])); + for(const auto certfile : std::vector<std::string>(args.begin()+2, args.end())) + { + certs.add_certificate(X509_Certificate(certfile)); + } Path_Validation_Restrictions restrictions; diff --git a/src/cmd/compress.cpp b/src/cmd/compress.cpp index c9168f3df..93bc76eb4 100644 --- a/src/cmd/compress.cpp +++ b/src/cmd/compress.cpp @@ -35,15 +35,15 @@ void do_compress(Transform& comp, std::ifstream& in, std::ostream& out) out.write(reinterpret_cast<const char*>(&buf[0]), buf.size()); } -int compress(int argc, char* argv[]) +int compress(const std::vector<std::string> &args) { - if(argc != 2 && argc != 3 && argc != 4) + if(args.size() != 2 && args.size() != 3 && args.size() != 4) { - std::cout << "Usage: " << argv[0] << " input [type] [level]" << std::endl; + std::cout << "Usage: " << args[0] << " input [type] [level]" << std::endl; return 1; } - const std::string in_file = argv[1]; + const std::string in_file = args[1]; std::ifstream in(in_file); if(!in.good()) @@ -52,8 +52,8 @@ int compress(int argc, char* argv[]) return 1; } - const std::string suffix = argc >= 3 ? argv[2] : "gz"; - const size_t level = argc >= 4 ? to_u32bit(argv[3]) : 9; + const std::string suffix = args.size() >= 3 ? args[2] : "gz"; + const size_t level = args.size() >= 4 ? to_u32bit(args[3]) : 9; std::unique_ptr<Transform> compress(make_compressor(suffix, level)); @@ -83,20 +83,20 @@ void parse_extension(const std::string& in_file, suffix = in_file.substr(last_dot+1, std::string::npos); } -int uncompress(int argc, char* argv[]) +int uncompress(const std::vector<std::string> &args) { - if(argc != 2) + if(args.size() != 2) { - std::cout << "Usage: " << argv[0] << " <file>" << std::endl; + std::cout << "Usage: " << args[0] << " <file>" << std::endl; return 1; } - const std::string in_file = argv[1]; + const std::string in_file = args[1]; std::ifstream in(in_file); if(!in.good()) { - std::cout << "Couldn't read '" << argv[1] << "'" << std::endl; + std::cout << "Couldn't read '" << args[1] << "'" << std::endl; return 1; } diff --git a/src/cmd/dl_group.cpp b/src/cmd/dl_group.cpp index 2db65ded2..e9a4f3fd4 100644 --- a/src/cmd/dl_group.cpp +++ b/src/cmd/dl_group.cpp @@ -36,20 +36,20 @@ std::string read_file_contents(const std::string& filename) return res; } -int dl_group(int argc, char* argv[]) +int dl_group(const std::vector<std::string> &args) { - if(argc < 2) + if(args.size() < 2) { - std::cout << "Usage: " << argv[0] << " [create bits|info file]" << std::endl; + std::cout << "Usage: " << args[0] << " [create bits|info file]" << std::endl; return 1; } - const std::string cmd = argv[1]; + const std::string cmd = args[1]; if(cmd == "create") { AutoSeeded_RNG rng; - const size_t bits = to_u32bit(argv[2]); + const size_t bits = to_u32bit(args[2]); const DL_Group::PrimeType prime_type = DL_Group::Strong; //const DL_Group::PrimeType prime_type = DL_Group::Prime_Subgroup; @@ -61,7 +61,7 @@ int dl_group(int argc, char* argv[]) else if(cmd == "info") { DL_Group grp; - std::string pem = read_file_contents(argv[2]); + std::string pem = read_file_contents(args[2]); std::cout << pem << "\n"; std::cout << "DL_Group " << grp.get_p().bits() << " bits\n"; diff --git a/src/cmd/dsa_sign.cpp b/src/cmd/dsa_sign.cpp index 25ad6f9de..03aede585 100644 --- a/src/cmd/dsa_sign.cpp +++ b/src/cmd/dsa_sign.cpp @@ -16,30 +16,30 @@ namespace { -int dsa_sign(int argc, char* argv[]) +int dsa_sign(const std::vector<std::string> &args) { using namespace Botan; const std::string SUFFIX = ".sig"; - if(argc != 4) + if(args.size() != 4) { - std::cout << "Usage: " << argv[0] << " keyfile messagefile passphrase" + std::cout << "Usage: " << args[0] << " keyfile messagefile passphrase" << std::endl; return 1; } try { - std::string passphrase(argv[3]); + std::string passphrase(args[3]); - std::ifstream message(argv[2], std::ios::binary); + std::ifstream message(args[2], std::ios::binary); if(!message) { std::cout << "Couldn't read the message file." << std::endl; return 1; } - std::string outfile = argv[2] + SUFFIX; + std::string outfile = args[2] + SUFFIX; std::ofstream sigfile(outfile); if(!sigfile) { @@ -51,7 +51,7 @@ int dsa_sign(int argc, char* argv[]) AutoSeeded_RNG rng; std::unique_ptr<PKCS8_PrivateKey> key( - PKCS8::load_key(argv[1], rng, passphrase) + PKCS8::load_key(args[1], rng, passphrase) ); DSA_PrivateKey* dsakey = dynamic_cast<DSA_PrivateKey*>(key.get()); diff --git a/src/cmd/dsa_ver.cpp b/src/cmd/dsa_ver.cpp index 35841b6cf..64d60a5cf 100644 --- a/src/cmd/dsa_ver.cpp +++ b/src/cmd/dsa_ver.cpp @@ -17,25 +17,25 @@ using namespace Botan; namespace { -int dsa_verify(int argc, char* argv[]) +int dsa_verify(const std::vector<std::string> &args) { - if(argc != 4) + if(args.size() != 4) { - std::cout << "Usage: " << argv[0] + std::cout << "Usage: " << args[0] << " keyfile messagefile sigfile" << std::endl; return 1; } try { - std::ifstream message(argv[2], std::ios::binary); + std::ifstream message(args[2], std::ios::binary); if(!message) { std::cout << "Couldn't read the message file." << std::endl; return 1; } - std::ifstream sigfile(argv[3]); + std::ifstream sigfile(args[3]); if(!sigfile) { std::cout << "Couldn't read the signature file." << std::endl; @@ -45,7 +45,7 @@ int dsa_verify(int argc, char* argv[]) std::string sigstr; getline(sigfile, sigstr); - std::unique_ptr<X509_PublicKey> key(X509::load_key(argv[1])); + std::unique_ptr<X509_PublicKey> key(X509::load_key(args[1])); DSA_PublicKey* dsakey = dynamic_cast<DSA_PublicKey*>(key.get()); if(!dsakey) diff --git a/src/cmd/factor.cpp b/src/cmd/factor.cpp index 5ea53478d..d2c0a2df5 100644 --- a/src/cmd/factor.cpp +++ b/src/cmd/factor.cpp @@ -122,17 +122,17 @@ std::vector<BigInt> factorize(const BigInt& n_in, return factors; } -int factor(int argc, char* argv[]) +int factor(const std::vector<std::string> &args) { - if(argc != 2) + if(args.size() != 2) { - std::cout << "Usage: " << argv[0] << " <integer>" << std::endl; + std::cout << "Usage: " << args[0] << " <integer>" << std::endl; return 1; } try { - BigInt n(argv[1]); + BigInt n(args[1]); AutoSeeded_RNG rng; diff --git a/src/cmd/fpe.cpp b/src/cmd/fpe.cpp index a9da9dcb7..97ca34b24 100644 --- a/src/cmd/fpe.cpp +++ b/src/cmd/fpe.cpp @@ -107,17 +107,17 @@ u64bit decrypt_cc_number(u64bit enc_cc, return cc_derank(dec_cc); } -int fpe(int argc, char* argv[]) +int fpe(const std::vector<std::string> &args) { - if(argc != 4) + if(args.size() != 4) { - std::cout << "Usage: " << argv[0] << " cc-number acct-name passwd" << std::endl; + std::cout << "Usage: " << args[0] << " cc-number acct-name passwd" << std::endl; return 1; } - u64bit cc_number = atoll(argv[1]); - std::string acct_name = argv[2]; - std::string passwd = argv[3]; + u64bit cc_number = atoll(args[1].c_str()); + std::string acct_name = args[2]; + std::string passwd = args[3]; std::cout << "Input was: " << cc_number << ' ' << luhn_check(cc_number) << std::endl; diff --git a/src/cmd/fuzzer.cpp b/src/cmd/fuzzer.cpp index 3174c76b8..35adb9711 100644 --- a/src/cmd/fuzzer.cpp +++ b/src/cmd/fuzzer.cpp @@ -48,18 +48,18 @@ class Fuzzer_Creds : public Credentials_Manager #endif -int fuzzer(int argc, char* argv[]) +int fuzzer(const std::vector<std::string> &args) { - if(argc != 3) + if(args.size() != 3) { - std::cout << "Usage: " << argv[0] << " [type] [input_file]\n"; - std::cout << "Hook for fuzzers such as afl (produces no output)\n"; - std::cout << "Types: cert crl privkey tls_client\n"; + std::cout << "Usage: " << args[0] << " [type] [input_file]\n" + << "Hook for fuzzers such as afl (produces no output)\n" + << "Types: cert crl privkey tls_client" << std::endl; return 1; } - const std::string type = argv[1]; - const std::string input = argv[2]; + const std::string type = args[1]; + const std::string input = args[2]; AutoSeeded_RNG rng; @@ -132,7 +132,7 @@ int fuzzer(int argc, char* argv[]) } #endif - std::cout << "Unknown type '" << type << "'\n"; + std::cout << "Unknown type '" << type << "'" << std::endl; return 1; } diff --git a/src/cmd/getopt.cpp b/src/cmd/getopt.cpp index 48d30b2f2..7b7e14932 100644 --- a/src/cmd/getopt.cpp +++ b/src/cmd/getopt.cpp @@ -7,18 +7,17 @@ #include "getopt.h" #include <iostream> -void OptionParser::parse(char* argv[]) +void OptionParser::parse(const std::vector<std::string> &args) { - std::vector<std::string> args; - for(int j = 1; argv[j]; j++) - args.push_back(argv[j]); + const std::string appname = args[0]; - for(size_t j = 0; j != args.size(); j++) + // ship first, args[0] is the app name + for(size_t j = 1; j != args.size(); j++) { std::string arg = args[j]; if(arg == "help" || arg == "--help" || arg == "-h") - return help(std::cout, argv[0]); + return help(std::cout, appname); if(arg.size() > 2 && arg[0] == '-' && arg[1] == '-') { @@ -43,8 +42,7 @@ void OptionParser::parse(char* argv[]) else { if(mark != std::string::npos) - throw std::runtime_error("Option " + opt_name + - " does not take an argument"); + throw std::runtime_error("Option " + opt_name + " does not take an argument"); options[arg] = ""; } @@ -53,4 +51,3 @@ void OptionParser::parse(char* argv[]) leftover.push_back(arg); } } - diff --git a/src/cmd/getopt.h b/src/cmd/getopt.h index f0ad7c713..435e74cbc 100644 --- a/src/cmd/getopt.h +++ b/src/cmd/getopt.h @@ -48,11 +48,9 @@ class OptionParser return is_set(key) ? Botan::to_u32bit(value(key)) : or_else; } - void help(std::ostream& o, const char* cmd = nullptr) + void help(std::ostream& o, const std::string &appname) { - o << "Usage: "; - if(cmd) - o << cmd << " "; + o << "Usage: " << appname << " "; for(auto flag : flags) { @@ -62,10 +60,10 @@ class OptionParser o << " "; } - o << "\n"; + o << std::endl; } - void parse(char* argv[]); + void parse(const std::vector<std::string> &args); OptionParser(const std::string& opt_string) { diff --git a/src/cmd/hash.cpp b/src/cmd/hash.cpp index 7755bb4a5..81a72ca17 100644 --- a/src/cmd/hash.cpp +++ b/src/cmd/hash.cpp @@ -16,15 +16,15 @@ using namespace Botan; namespace { -int hash(int argc, char* argv[]) +int hash(const std::vector<std::string> &args) { - if(argc < 3) + if(args.size() < 3) { - std::cout << "Usage: " << argv[0] << " digest <filenames>" << std::endl; + std::cout << "Usage: " << args[0] << " digest <filenames>" << std::endl; return 1; } - std::string hash = argv[1]; + std::string hash = args[1]; /* a couple of special cases, kind of a crock */ if(hash == "sha1") hash = "SHA-1"; if(hash == "md5") hash = "MD5"; @@ -33,12 +33,12 @@ int hash(int argc, char* argv[]) Pipe pipe(new Hash_Filter(hash), new Hex_Encoder); int skipped = 0; - for(int j = 2; argv[j] != nullptr; j++) + for(int j = 2; j < args.size(); j++) { - std::ifstream file(argv[j], std::ios::binary); + std::ifstream file(args[j], std::ios::binary); if(!file) { - std::cout << "ERROR: could not open " << argv[j] << std::endl; + std::cout << "ERROR: could not open " << args[j] << std::endl; skipped++; continue; } @@ -46,7 +46,7 @@ int hash(int argc, char* argv[]) file >> pipe; pipe.end_msg(); pipe.set_default_msg(j-2-skipped); - std::cout << pipe << " " << argv[j] << std::endl; + std::cout << pipe << " " << args[j] << std::endl; } } catch(std::exception& e) diff --git a/src/cmd/is_prime.cpp b/src/cmd/is_prime.cpp index 703772477..71fec730b 100644 --- a/src/cmd/is_prime.cpp +++ b/src/cmd/is_prime.cpp @@ -12,20 +12,20 @@ namespace { -int is_prime(int argc, char* argv[]) +int is_prime(const std::vector<std::string> &args) { - if(argc != 2 && argc != 3) + if(args.size() != 2 && args.size() != 3) { - std::cerr << "Usage: " << argv[0] << " n <prob>" << std::endl; + std::cerr << "Usage: " << args[0] << " n <prob>" << std::endl; return 2; } - BigInt n(argv[1]); + BigInt n(args[1]); size_t prob = 56; - if(argc == 3) - prob = to_u32bit(argv[2]); + if(args.size() == 3) + prob = to_u32bit(args[2]); AutoSeeded_RNG rng; diff --git a/src/cmd/keygen.cpp b/src/cmd/keygen.cpp index ead7dd39d..168b27a4a 100644 --- a/src/cmd/keygen.cpp +++ b/src/cmd/keygen.cpp @@ -79,11 +79,10 @@ Private_Key* gen_key(RandomNumberGenerator& rng, const std::string& algo, size_t } -int keygen(int argc, char* argv[]) +int keygen(const std::vector<std::string> &args) { - BOTAN_UNUSED(argc); OptionParser opts("algo=|bits=|passphrase=|pbe="); - opts.parse(argv); + opts.parse(args); const std::string algo = opts.value_or_else("algo", "rsa"); const size_t bits = opts.int_value_or_else("bits", 2048); diff --git a/src/cmd/main.cpp b/src/cmd/main.cpp index 9207659ea..8d229ce0e 100644 --- a/src/cmd/main.cpp +++ b/src/cmd/main.cpp @@ -26,11 +26,11 @@ using namespace Botan; namespace { -int help(int , char* argv[]) +int help(const std::vector<std::string> &args) { - std::cout << "Usage: " << argv[0] << " [subcommand] [subcommand-options]" << std::endl; + std::cout << "Usage: " << args[0] << " [subcommand] [subcommand-options]" << std::endl; - std::set<std::string> apps = AppRegistrations::instance().all_apps(); + std::set<std::string> apps = AppRegistrations::instance().all_appnames(); std::cout << "Available commands:" << std::endl; @@ -51,11 +51,11 @@ int help(int , char* argv[]) return 1; } -int config(int argc, char* argv[]) +int config(const std::vector<std::string> &args) { - if(argc != 2) + if(args.size() != 2) { - std::cout << "Usage: " << argv[0] << " <what>\n" + std::cout << "Usage: " << args[0] << " <what>\n" << " prefix: Print install prefix\n" << " cflags: Print include params\n" << " ldflags: Print linker params\n" @@ -63,7 +63,7 @@ int config(int argc, char* argv[]) return 1; } - const std::string arg = argv[1]; + const std::string arg = args[1]; if(arg == "prefix") std::cout << BOTAN_INSTALL_PREFIX << std::endl; @@ -88,7 +88,7 @@ int config(int argc, char* argv[]) } REGISTER_APP(config); -int version(int argc, char* argv[]) +int version(const std::vector<std::string> &args) { if(BOTAN_VERSION_MAJOR != version_major() || BOTAN_VERSION_MINOR != version_minor() || @@ -104,19 +104,19 @@ int version(int argc, char* argv[]) << BOTAN_VERSION_PATCH << ")" << std::endl; } - if(argc == 1) + if(args.size() == 1) { std::cout << Botan::version_major() << "." << Botan::version_minor() << "." << Botan::version_patch() << std::endl; } - else if(argc == 2 && std::string(argv[1]) == "--full") + else if(args.size() == 2 && args[1] == "--full") { std::cout << Botan::version_string() << std::endl; } else { - std::cout << "Usage: " << argv[0] << " version [--full]" << std::endl; + std::cout << "Usage: " << args[0] << " version [--full]" << std::endl; return 1; } @@ -124,23 +124,24 @@ int version(int argc, char* argv[]) } REGISTER_APP(version); -int cpuid(int, char*[]) +int cpuid(const std::vector<std::string> &args) { + BOTAN_UNUSED(args); CPUID::print(std::cout); return 0; } REGISTER_APP(cpuid); #if defined(BOTAN_HAS_HTTP_UTIL) -int http_get(int argc, char* argv[]) +int http_get(const std::vector<std::string> &args) { - if(argc != 2) + if(args.size() != 2) { - std::cout << "Usage " << argv[0] << " <url>" << std::endl; + std::cout << "Usage " << args[0] << " <url>" << std::endl; return 1; } - auto resp = HTTP::GET_sync(argv[2]); + auto resp = HTTP::GET_sync(args[1]); std::cout << resp << std::endl; return 0; } @@ -152,22 +153,24 @@ REGISTER_APP(http_get); int main(int argc, char* argv[]) { + const std::vector<std::string> args(argv, argv + argc); + try { - if(argc < 2) - return help(argc, argv); + if(args.size() < 2) + return help(args); - const std::string cmd = argv[1]; + const std::string cmd = args[1]; if(cmd == "help" || cmd == "-h") - return help(argc, argv); + return help(args); AppRegistrations& apps = AppRegistrations::instance(); if(apps.has(cmd)) - return apps.run(cmd, argc - 1, argv + 1); + return apps.run(cmd, std::vector<std::string>(args.begin()+1, args.end())); std::cerr << "Unknown command " << cmd << std::endl; - return help(argc, argv); + return help(args); } catch(std::exception& e) { diff --git a/src/cmd/mce.cpp b/src/cmd/mce.cpp index d179e0284..226f21e9c 100644 --- a/src/cmd/mce.cpp +++ b/src/cmd/mce.cpp @@ -15,23 +15,24 @@ namespace { -int mce(int argc, char* argv[]) +int mce(const std::vector<std::string> &args) { - if(argc < 4) + if(args.size() < 4) { - std::cout << "Usage: " << argv[0] << " [keygen n t pass|keybits n t|encrypt file key|decrypt file key pass]\n"; + std::cout << "Usage: " << args[0] << " [keygen n t pass|keybits n t|encrypt file key|decrypt file key pass]" + << std::endl; return 1; } - const std::string cmd = argv[1]; + const std::string cmd = args[1]; AutoSeeded_RNG rng; if(cmd == "keygen") { - const size_t n = std::stol(argv[2]); - const size_t t = std::stol(argv[3]); - const std::string pass = argv[4]; + const u32bit n = to_u32bit(args[2]); + const u32bit t = to_u32bit(args[3]); + const std::string pass = args[4]; McEliece_PrivateKey pk(rng, n, t); @@ -59,14 +60,14 @@ int mce(int argc, char* argv[]) } else if(cmd == "keybits") { - const size_t n = std::stol(argv[2]); - const size_t t = std::stol(argv[3]); + const u32bit n = to_u32bit(args[2]); + const u32bit t = to_u32bit(args[3]); std::cout << "McEliece key with params (" << n << "," << t << ") has " << mceliece_work_factor(n, t) << " bit security\n"; } else if(cmd == "encrypt") { - std::unique_ptr<Public_Key> p8(X509::load_key(argv[3])); + std::unique_ptr<Public_Key> p8(X509::load_key(args[3])); const McEliece_PublicKey* key = dynamic_cast<McEliece_PublicKey*>(p8.get()); if(!key) @@ -74,7 +75,7 @@ int mce(int argc, char* argv[]) throw std::runtime_error("Loading McEliece public key failed"); } - const std::string input_path = argv[2]; + const std::string input_path = args[2]; std::ifstream in(input_path, std::ios::binary); std::string pt((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()); @@ -83,7 +84,7 @@ int mce(int argc, char* argv[]) pt.size(), nullptr, 0, rng, "AES-128/GCM"); - std::cout << pt.size() << " -> " << ct.size() << "\n"; + std::cout << pt.size() << " -> " << ct.size() << std::endl; std::ofstream out(std::string(input_path) + ".ct", std::ios::binary); out.write(reinterpret_cast<const char*>(ct.data()), ct.size()); @@ -91,8 +92,8 @@ int mce(int argc, char* argv[]) } else if(cmd == "decrypt") { - const std::string key_file = argv[3]; - const std::string pass = argv[4]; + const std::string key_file = args[3]; + const std::string pass = args[4]; std::unique_ptr<Private_Key> p8(PKCS8::load_key(key_file, rng, pass)); const McEliece_PrivateKey* key = dynamic_cast<McEliece_PrivateKey*>(p8.get()); @@ -101,7 +102,7 @@ int mce(int argc, char* argv[]) throw std::runtime_error("Loading McEliece private key failed"); } - std::ifstream in(argv[2], std::ios::binary); + std::ifstream in(args[2], std::ios::binary); std::string ct((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()); secure_vector<byte> pt = mceies_decrypt(*key, diff --git a/src/cmd/ocsp.cpp b/src/cmd/ocsp.cpp index a939b4680..e5b42b076 100644 --- a/src/cmd/ocsp.cpp +++ b/src/cmd/ocsp.cpp @@ -17,16 +17,16 @@ using namespace Botan; namespace { -int ocsp_check(int argc, char* argv[]) +int ocsp_check(const std::vector<std::string> &args) { - if(argc != 2) + if(args.size() != 2) { - std::cout << "Usage: ocsp subject.pem issuer.pem"; + std::cout << "Usage: " << args[0] << " subject.pem issuer.pem" << std::endl; return 2; } - X509_Certificate subject(argv[1]); - X509_Certificate issuer(argv[2]); + X509_Certificate subject(args[1]); + X509_Certificate issuer(args[2]); Certificate_Store_In_Memory cas; cas.add_certificate(issuer); diff --git a/src/cmd/pkcs10.cpp b/src/cmd/pkcs10.cpp index 18fdea9bf..106fe2c24 100644 --- a/src/cmd/pkcs10.cpp +++ b/src/cmd/pkcs10.cpp @@ -18,11 +18,11 @@ using namespace Botan; namespace { -int pkcs10(int argc, char* argv[]) +int pkcs10(const std::vector<std::string> &args) { - if(argc != 6) + if(args.size() != 6) { - std::cout << "Usage: " << argv[0] + std::cout << "Usage: " << args[0] << " passphrase name country_code organization email" << std::endl; return 1; } @@ -34,14 +34,14 @@ int pkcs10(int argc, char* argv[]) RSA_PrivateKey priv_key(rng, 1024); std::ofstream key_file("private.pem"); - key_file << PKCS8::PEM_encode(priv_key, rng, argv[1]); + key_file << PKCS8::PEM_encode(priv_key, rng, args[1]); X509_Cert_Options opts; - opts.common_name = argv[2]; - opts.country = argv[3]; - opts.organization = argv[4]; - opts.email = argv[5]; + opts.common_name = args[2]; + opts.country = args[3]; + opts.organization = args[4]; + opts.email = args[5]; PKCS10_Request req = X509::create_cert_req(opts, priv_key, "SHA-256", rng); diff --git a/src/cmd/pkcs8.cpp b/src/cmd/pkcs8.cpp index 19af70eb1..7bc1c2561 100644 --- a/src/cmd/pkcs8.cpp +++ b/src/cmd/pkcs8.cpp @@ -20,19 +20,18 @@ using namespace Botan; namespace { -int pkcs8(int argc, char* argv[]) +int pkcs8(const std::vector<std::string> &args) { - BOTAN_UNUSED(argc); OptionParser opts("in=|out=|passin=|passout=|pbe=|pubout"); - opts.parse(argv); + opts.parse(args); const std::string passin = opts.value_or_else("passin", ""); const std::string passout = opts.value_or_else("passout", ""); const std::string pbe = opts.value_or_else("pbe", ""); - if(argc < 3) + if(args.size() < 3) { - opts.help( std::cout, "pkcs8" ); + opts.help(std::cout, "pkcs8"); return 1; } diff --git a/src/cmd/prime.cpp b/src/cmd/prime.cpp index 61f535dd7..82efa75d2 100644 --- a/src/cmd/prime.cpp +++ b/src/cmd/prime.cpp @@ -15,26 +15,26 @@ namespace { -int prime(int argc, char* argv[]) +int prime(const std::vector<std::string> &args) { - if(argc < 2) + if(args.size() < 2) { - std::cout << "Usage: " << argv[0] << " bits count" << std::endl; + std::cout << "Usage: " << args[0] << " bits count" << std::endl; return 1; } AutoSeeded_RNG rng; - const size_t bits = to_u32bit(argv[1]); - const size_t cnt = argv[2] != nullptr ? to_u32bit(argv[2]) : 1; + const size_t bits = to_u32bit(args[1]); + const size_t cnt = args.size() >= 3 ? to_u32bit(args[2]) : 1; for(size_t i = 0; i != cnt; ++i) { const BigInt p = random_prime(rng, bits); - std::cout << p << "\n"; + std::cout << p << std::endl; if(p.bits() != bits) { - std::cout << "Result not exactly requested bit size, got " << p.bits() << "\n"; + std::cout << "Result not exactly requested bit size, got " << p.bits() << std::endl; } } diff --git a/src/cmd/rng.cpp b/src/cmd/rng.cpp index b89376b4c..3fe8719ce 100644 --- a/src/cmd/rng.cpp +++ b/src/cmd/rng.cpp @@ -14,39 +14,41 @@ namespace { -int rng(int argc, char* argv[]) +int rng(const std::vector<std::string> &args) { - if(argc == 1) + if(args.size() == 1) { - std::cout << "Usage: " << argv[0] << " [--raw-entropy] [n]" << std::endl; + std::cout << "Usage: " << args[0] << " [--raw-entropy] n\n" + << "n: number of bytes" + << std::endl; return 1; } try { - const size_t amt = to_u32bit(argv[argc-1]); - const bool raw = (argc == 3 && std::string(argv[1]) == "--raw-entropy"); + const size_t bytes_count = to_u32bit(args.back()); + const bool raw = (args.size() == 3 && args[1] == "--raw-entropy"); #if defined(BOTAN_HAS_SYSTEM_RNG) - std::cout << "System " << hex_encode(system_rng().random_vec(amt)) << std::endl; + std::cout << "System " << hex_encode(system_rng().random_vec(bytes_count)) << std::endl; #endif if(!raw) { AutoSeeded_RNG rng; - std::cout << hex_encode(rng.random_vec(amt)) << std::endl; + std::cout << hex_encode(rng.random_vec(bytes_count)) << std::endl; } else { double total_collected = 0; Entropy_Accumulator accum( - [amt,&total_collected](const byte in[], size_t in_len, double entropy_estimate) + [bytes_count,&total_collected](const byte in[], size_t in_len, double entropy_estimate) { std::cout << "Collected estimated "<< entropy_estimate << " bits in " << hex_encode(in, in_len) << std::endl; total_collected += entropy_estimate; - return total_collected >= amt; + return total_collected >= bytes_count; }); Entropy_Sources::global_sources().poll(accum); diff --git a/src/cmd/self_sig.cpp b/src/cmd/self_sig.cpp index 6916420ca..2c43f7acc 100644 --- a/src/cmd/self_sig.cpp +++ b/src/cmd/self_sig.cpp @@ -18,17 +18,17 @@ using namespace Botan; namespace { -int self_sig(int argc, char* argv[]) +int self_sig(const std::vector<std::string> &args) { - if(argc != 7) + if(args.size() != 7) { - std::cout << "Usage: " << argv[0] + std::cout << "Usage: " << args[0] << " passphrase [CA|user] name country_code organization email" << std::endl; return 1; } - std::string CA_flag = argv[2]; + std::string CA_flag = args[2]; bool do_CA = false; if(CA_flag == "CA") do_CA = true; @@ -48,14 +48,13 @@ int self_sig(int argc, char* argv[]) std::ofstream priv_key("private.pem"); - priv_key << PKCS8::PEM_encode(key, rng, argv[1]); + priv_key << PKCS8::PEM_encode(key, rng, args[1]); X509_Cert_Options opts; - - opts.common_name = argv[3]; - opts.country = argv[4]; - opts.organization = argv[5]; - opts.email = argv[6]; + opts.common_name = args[3]; + opts.country = args[4]; + opts.organization = args[5]; + opts.email = args[6]; /* Fill in other values of opts here */ //opts.xmpp = "[email protected]"; diff --git a/src/cmd/speed.cpp b/src/cmd/speed.cpp index 990c6a364..e8d30c6f1 100644 --- a/src/cmd/speed.cpp +++ b/src/cmd/speed.cpp @@ -157,11 +157,10 @@ void bench_algo(const std::string& algo, } } -int speed(int argc, char* argv[]) +int speed(const std::vector<std::string> &args) { - BOTAN_UNUSED(argc); OptionParser opts("seconds=|buf-size=|provider="); - opts.parse(argv); + opts.parse(args); double seconds = .5; u32bit buf_size = 16; @@ -188,20 +187,20 @@ int speed(int argc, char* argv[]) const std::string provider = opts.value_if_set("provider"); - std::vector<std::string> args = opts.arguments(); + std::vector<std::string> opt_args = opts.arguments(); - if(args.empty()) - args = default_benchmark_list; + if(opt_args.empty()) + opt_args = default_benchmark_list; - if(args[0] == "help" || args[0] == "-h") + if(opt_args[0] == "help" || opt_args[0] == "-h") { - std::cout << "Usage: " << argv[0] << " [algo name...]" << std::endl; + std::cout << "Usage: " << args[0] << " [algo name...]" << std::endl; return 1; } AutoSeeded_RNG rng; - for(auto alg: args) + for(auto alg: opt_args) { bench_algo(alg, provider, rng, seconds, buf_size); } diff --git a/src/cmd/tls_client.cpp b/src/cmd/tls_client.cpp index f9d32ea6c..7f74e1a37 100644 --- a/src/cmd/tls_client.cpp +++ b/src/cmd/tls_client.cpp @@ -134,11 +134,11 @@ void process_data(const byte buf[], size_t buf_size) std::cout << buf[i]; } -int tls_client(int argc, char* argv[]) +int tls_client(const std::vector<std::string> &args) { - if(argc != 2 && argc != 3 && argc != 4) + if(args.size() != 2 && args.size() != 3 && args.size() != 4) { - std::cout << "Usage " << argv[0] << " host [port] [udp|tcp]" << std::endl; + std::cout << "Usage " << args[0] << " host [port] [udp|tcp]" << std::endl; return 1; } @@ -160,9 +160,9 @@ int tls_client(int argc, char* argv[]) Basic_Credentials_Manager creds; - std::string host = argv[1]; - u32bit port = argc >= 3 ? Botan::to_u32bit(argv[2]) : 443; - const std::string transport = argc >= 4 ? argv[3] : "tcp"; + const std::string host = args[1]; + const u32bit port = args.size() >= 3 ? Botan::to_u32bit(args[2]) : 443; + const std::string transport = args.size() >= 4 ? args[3] : "tcp"; const bool use_tcp = (transport == "tcp"); diff --git a/src/cmd/tls_server.cpp b/src/cmd/tls_server.cpp index cd95cf0d6..ea68208b6 100644 --- a/src/cmd/tls_server.cpp +++ b/src/cmd/tls_server.cpp @@ -122,18 +122,18 @@ void alert_received(TLS::Alert alert, const byte[], size_t) std::cout << "Alert: " << alert.type_string() << std::endl; } -int tls_server(int argc, char* argv[]) +int tls_server(const std::vector<std::string> &args) { - if(argc != 4 && argc != 5) + if(args.size() != 4 && args.size() != 5) { - std::cout << "Usage: " << argv[0] << " server.crt server.key port [tcp|udp]" << std::endl; + std::cout << "Usage: " << args[0] << " server.crt server.key port [tcp|udp]" << std::endl; return 1; } - const std::string server_crt = argv[1]; - const std::string server_key = argv[2]; - const int port = to_u32bit(argv[3]); - const std::string transport = (argc >= 5) ? argv[4] : "tcp"; + const std::string server_crt = args[1]; + const std::string server_key = args[2]; + const int port = to_u32bit(args[3]); + const std::string transport = (args.size() >= 5) ? args[4] : "tcp"; const bool is_tcp = (transport == "tcp"); diff --git a/src/cmd/x509print.cpp b/src/cmd/x509print.cpp index c95ab6cdb..e583c91c2 100644 --- a/src/cmd/x509print.cpp +++ b/src/cmd/x509print.cpp @@ -10,15 +10,15 @@ namespace { -int x509print(int argc, char* argv[]) +int x509print(const std::vector<std::string> &args) { - if(argc < 1) + if(args.size() != 1) { - std::cout << "Usage: " << argv[0] << " cert.pem" << std::endl; + std::cout << "Usage: " << args[0] << " cert.pem" << std::endl; return 1; } - X509_Certificate cert(argv[1]); + X509_Certificate cert(args[1]); std::cout << cert.to_string() << std::endl; |