aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/aesxx/utils/helpers/cmd_parser_block.hpp
blob: 009361759d0fbf05a21cf0faee4e4b83c0bb4112 (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
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
// 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 <deque>
#include <iterator>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

class BlockSettings : public SettingsParser {
public:
    class Input {
    public:
        Input(std::string_view key, std::string_view iv, std::vector<std::string>&& blocks)
            : key{key}, iv{iv}, blocks{std::move(blocks)} {}

        Input(std::string key, std::vector<std::string>&& blocks)
            : key{key}, blocks{std::move(blocks)} {}

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

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

        std::vector<std::string> get_blocks() const {
            return blocks;
        }

    private:
        std::string key;
        std::string iv;
        std::vector<std::string> blocks;
    };

    aes::Algorithm algorithm = AES_AES128;
    aes::Mode mode = AES_ECB;

    bool use_boxes = false;
    bool verbose = false;

    std::vector<Input> inputs;

    explicit BlockSettings(std::string_view argv0) : SettingsParser{argv0} {
        visible.add_options()(
            "verbose,v", boost::program_options::bool_switch(&verbose), "enable verbose output"
        );
        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()(
            "use-boxes,b",
            boost::program_options::bool_switch(&use_boxes),
            "use the \"boxes\" interface"
        );
        hidden.add_options()(
            "args",
            boost::program_options::value<std::vector<std::string>>(&args),
            "shouldn't be visible"
        );
        positional.add("args", -1);
    }

    const char* get_short_description() const override {
        return "[-h|--help] [-v|--verbose] [-a|--algorithm NAME] [-m|--mode MODE]"
               " [-- KEY [IV] [BLOCK]...]...";
    }

    void parse(int argc, char* argv[]) override {
        SettingsParser::parse(argc, argv);
        parse_inputs(
            std::deque<std::string>{
                std::make_move_iterator(args.begin()), std::make_move_iterator(args.end())
            }
        );
    }

private:
    void parse_inputs(std::deque<std::string>&& src) {
        while (!src.empty())
            inputs.emplace_back(parse_input(src));
    }

    Input parse_input(std::deque<std::string>& src) const {
        std::string key{std::move(src.front())};
        src.pop_front();

        std::string iv;

        if (aes::mode_requires_init_vector(mode)) {
            if (src.empty()) {
                throw boost::program_options::error{
                    "an initialization vector is required for the selected mode of operation"
                };
            }
            iv = std::move(src.front());
            src.pop_front();
        }

        auto blocks = parse_blocks(src);

        if (aes::mode_requires_init_vector(mode))
            return {key, iv, std::move(blocks)};
        else
            return {key, std::move(blocks)};
    }

    static std::vector<std::string> parse_blocks(std::deque<std::string>& src) {
        std::vector<std::string> blocks;

        while (!src.empty()) {
            std::string block{std::move(src.front())};
            src.pop_front();
            if (block == "--")
                break;
            blocks.emplace_back(std::move(block));
        }

        return blocks;
    }

    std::vector<std::string> args;
};