aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/aesxx/utils/helpers/cmd_parser_file.hpp
blob: 2e710b27885981d1bdc2e6f04b126fe508991b6d (plain) (blame)
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
// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "AES tools" project.
// For details, see https://github.com/egor-tensin/aes-tools.
// Distributed under the MIT License.

#pragma once

#include "cmd_parser.hpp"
#include "data_parsers.hpp"

#include <aesxx/all.hpp>

#include <boost/program_options.hpp>

#include <ostream>
#include <string>
#include <string_view>
#include <utility>

class FileSettings : public SettingsParser {
public:
    explicit FileSettings(std::string_view argv0) : SettingsParser{argv0} {
        visible.add_options()(
            "algorithm,a",
            boost::program_options::value<aes::Algorithm>(&algorithm)
                ->required()
                ->value_name("NAME"),
            "set algorithm"
        );
        visible.add_options()(
            "mode,m",
            boost::program_options::value<aes::Mode>(&mode)->required()->value_name("MODE"),
            "set mode of operation"
        );
        visible.add_options()(
            "key,k",
            boost::program_options::value<std::string>(&key)->required()->value_name("KEY"),
            "set encryption key"
        );
        visible.add_options()(
            "iv,v",
            boost::program_options::value<std::string>(&iv)->value_name("BLOCK"),
            "set initialization vector"
        );
        visible.add_options()(
            "input,i",
            boost::program_options::value<std::string>(&input_path)->required()->value_name("PATH"),
            "set input file path"
        );
        visible.add_options()(
            "output,o",
            boost::program_options::value<std::string>(&output_path)
                ->required()
                ->value_name("PATH"),
            "set output file path"
        );
    }

    const char* get_short_description() const override {
        return "[-h|--help] [-a|--algorithm NAME] [-m|--mode MODE]"
               " [-k|--key KEY] [-v|--iv BLOCK]"
               " [-i|--input PATH] [-o|--output PATH]";
    }

    void parse(int argc, char** argv) override {
        SettingsParser::parse(argc, argv);
        if (exit_with_usage())
            return;

        if (aes::mode_requires_init_vector(get_mode()) && iv.empty()) {
            throw boost::program_options::error{
                "an initialization vector is required for the selected mode of operation"
            };
        }
    }

    aes::Algorithm get_algorithm() const {
        return algorithm;
    }

    aes::Mode get_mode() const {
        return mode;
    }

    std::string get_input_path() const {
        return input_path;
    }

    std::string get_output_path() const {
        return output_path;
    }

    std::string get_key() const {
        return key;
    }

    bool has_iv() const {
        return !iv.empty();
    }

    std::string get_iv() const {
        return iv;
    }

private:
    aes::Algorithm algorithm;
    aes::Mode mode;

    std::string input_path;
    std::string output_path;

    std::string key;
    std::string iv;
};