aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/utils/data_parsers.hpp
blob: 333962e91e5cb72437c2066a894b495062555eb6 (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
/**
 * \file
 * \author Egor Tensin <Egor.Tensin@gmail.com>
 * \date 2015
 * \copyright This file is licensed under the terms of the MIT License.
 *            See LICENSE.txt for details.
 */

#pragma once

#include <aesnixx/all.hpp>

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

#include <istream>
#include <map>
#include <string>

static std::istream& operator>>(std::istream& is, aesni::Mode& dest)
{
    static const char* const argument_name = "mode";

    std::string src;
    is >> src;

    static std::map<std::string, aesni::Mode> lookup_table =
    {
        { "ecb", AESNI_ECB },
        { "cbc", AESNI_CBC },
        { "cfb", AESNI_CFB },
        { "ofb", AESNI_OFB },
        { "ctr", AESNI_CTR },
    };

    const auto it = lookup_table.find(boost::algorithm::to_lower_copy(src));

    if (it == lookup_table.cend())
        throw boost::program_options::invalid_option_value(src);

    dest = it->second;
    return is;
}

static std::istream& operator>>(std::istream& is, aesni::Algorithm& dest)
{
    static const char* const argument_name = "algorithm";

    std::string src;
    is >> src;

    static std::map<std::string, aesni::Algorithm> lookup_table =
    {
        { "aes128", AESNI_AES128 },
        { "aes192", AESNI_AES192 },
        { "aes256", AESNI_AES256 },
    };

    const auto it = lookup_table.find(boost::algorithm::to_lower_copy(src));

    if (it == lookup_table.cend())
        throw boost::program_options::invalid_option_value(src);

    dest = it->second;
    return is;
}