diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2015-12-27 12:56:13 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2015-12-27 12:56:13 +0300 |
commit | 1db94ae5d2201edb5f9421c2c30be049efc678f6 (patch) | |
tree | cb67b8f1e8a04dda4de297cc011dc9de57d1e824 /utils/encrypt_bmp.cpp | |
parent | README update (diff) | |
download | aes-tools-1db94ae5d2201edb5f9421c2c30be049efc678f6.tar.gz aes-tools-1db94ae5d2201edb5f9421c2c30be049efc678f6.zip |
utils: refactor command line parsing
Diffstat (limited to 'utils/encrypt_bmp.cpp')
-rw-r--r-- | utils/encrypt_bmp.cpp | 78 |
1 files changed, 32 insertions, 46 deletions
diff --git a/utils/encrypt_bmp.cpp b/utils/encrypt_bmp.cpp index 2f019d8..3ac5906 100644 --- a/utils/encrypt_bmp.cpp +++ b/utils/encrypt_bmp.cpp @@ -15,7 +15,6 @@ #include <cstdlib> #include <cstring> -#include <deque> #include <exception> #include <fstream> #include <iostream> @@ -43,12 +42,12 @@ namespace ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); ifs.open(path, std::ifstream::binary); - std::vector<char> src_buf; - src_buf.reserve(size); - src_buf.assign( + std::vector<char> plaintext_buf; + plaintext_buf.reserve(size); + plaintext_buf.assign( std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>()); - return src_buf; + return plaintext_buf; } void write_file( @@ -63,64 +62,54 @@ namespace void encrypt_bmp( aesni::Box& box, - std::deque<std::string>& args) + const std::string& plaintext_path, + const std::string& ciphertext_path) { - if (args.empty()) - throw_src_path_required(); - const auto src_path = args.front(); - args.pop_front(); + const auto plaintext_buf = read_file(plaintext_path); - if (args.empty()) - throw_dest_path_required(); - const auto dest_path = args.front(); - args.pop_front(); - - const auto src_buf = read_file(src_path); - - const auto bmp_header = reinterpret_cast<const BITMAPFILEHEADER*>(src_buf.data()); + const auto bmp_header = reinterpret_cast<const BITMAPFILEHEADER*>(plaintext_buf.data()); const auto header_size = bmp_header->bfOffBits; - const auto pixels = src_buf.data() + header_size; - const auto pixels_size = src_buf.size() - header_size; + const auto pixels = plaintext_buf.data() + header_size; + const auto pixels_size = plaintext_buf.size() - header_size; const auto cipherpixels = box.encrypt_buffer( pixels, pixels_size); - std::vector<unsigned char> dest_buf(header_size + cipherpixels.size()); - std::memcpy(&dest_buf[0], bmp_header, header_size); - std::memcpy(&dest_buf[0] + header_size, cipherpixels.data(), cipherpixels.size()); + std::vector<unsigned char> ciphertext_buf(header_size + cipherpixels.size()); + std::memcpy(ciphertext_buf.data(), bmp_header, header_size); + std::memcpy(ciphertext_buf.data() + header_size, cipherpixels.data(), cipherpixels.size()); - write_file(dest_path, dest_buf); + write_file(ciphertext_path, ciphertext_buf); } - void encrypt_bmp( - aesni::Algorithm algorithm, - aesni::Mode mode, - std::deque<std::string>& args) + void encrypt_bmp(const Settings& settings) { - if (args.empty()) - throw_key_required(); + const auto algorithm = settings.get_algorithm(); + const auto mode = settings.get_mode(); + + const auto plaintext_path = settings.get_input_path(); + const auto ciphertext_path = settings.get_output_path(); aesni::Box::Key key; - aesni::Box::parse_key(key, algorithm, args.front()); - args.pop_front(); + aesni::Box::parse_key(key, algorithm, settings.get_key_string()); if (aesni::mode_requires_initialization_vector(mode)) { - if (args.empty()) - throw_iv_required(); - aesni::Box::Block iv; - aesni::Box::parse_block(iv, algorithm, args.front()); - args.pop_front(); + aesni::Box::parse_block(iv, algorithm, settings.get_iv_string()); encrypt_bmp( - aesni::Box(algorithm, key, mode, iv), args); + aesni::Box(algorithm, key, mode, iv), + plaintext_path, + ciphertext_path); } else { encrypt_bmp( - aesni::Box(algorithm, key), args); + aesni::Box(algorithm, key), + plaintext_path, + ciphertext_path); } } } @@ -132,19 +121,16 @@ int main(int argc, char** argv) CommandLineParser cmd_parser(argv[0]); try { - cmd_parser.parse(argc, argv); + Settings settings; + cmd_parser.parse(settings, argc, argv); - if (cmd_parser.requested_help()) + if (cmd_parser.exit_with_usage()) { std::cout << cmd_parser; return 0; } - std::deque<std::string> args( - std::make_move_iterator(cmd_parser.args.begin()), - std::make_move_iterator(cmd_parser.args.end())); - - encrypt_bmp(cmd_parser.algorithm, cmd_parser.mode, args); + encrypt_bmp(settings); } catch (const boost::program_options::error& e) { |