aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/aesxx/utils/helpers/data_parsers.hpp
blob: baa8d699f1c435ee1b52aa097c1ed892b6f2fb13 (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
// Copyright (c) 2015 Egor Tensin <egor@tensin.name>
// 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 <aesxx/all.hpp>

#include <boost/algorithm/string.hpp>
#include <boost/any.hpp>
#include <boost/program_options.hpp>

#include <string>
#include <unordered_map>
#include <vector>

namespace boost {

inline void validate(any& dest, const std::vector<std::string>& values, aes::Mode*, int) {
    using namespace program_options;

    validators::check_first_occurrence(dest);
    const auto& src = validators::get_single_string(values);

    static const std::unordered_map<std::string, aes::Mode> lookup_table = {
        {"ecb", AES_ECB},
        {"cbc", AES_CBC},
        {"cfb", AES_CFB},
        {"ofb", AES_OFB},
        {"ctr", AES_CTR},
    };

    const auto it = lookup_table.find(algorithm::to_lower_copy(src));
    if (it == lookup_table.cend())
        throw invalid_option_value(src);
    dest = it->second;
}

inline void validate(any& dest, const std::vector<std::string>& values, aes::Algorithm*, int) {
    using namespace program_options;

    validators::check_first_occurrence(dest);
    const auto& src = validators::get_single_string(values);

    static const std::unordered_map<std::string, aes::Algorithm> lookup_table = {
        {"aes128", AES_AES128},
        {"aes192", AES_AES192},
        {"aes256", AES_AES256},
    };

    const auto it = lookup_table.find(algorithm::to_lower_copy(src));
    if (it == lookup_table.cend())
        throw invalid_option_value(src);
    dest = it->second;
}

inline void validate(any& dest, const std::vector<std::string>& values, aes::Block*, int) {
    const std::string& src = program_options::validators::get_single_string(values);
    dest = aes::Block::parse(src);
}

} // namespace boost