aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstate/scan_name.cpp
blob: 13792720c7a77d27bade52de9a0a04436f87f52c (plain)
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
/**
* SCAN Name Abstraction
* (C) 2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/scan_name.h>
#include <botan/parsing.h>
#include <botan/libstate.h>
#include <stdexcept>

namespace Botan {

namespace {

std::vector<std::string>
parse_and_deref_aliases(const std::string& algo_spec)
   {
   std::vector<std::string> parts = parse_algorithm_name(algo_spec);
   std::vector<std::string> out;

   for(size_t i = 0; i != parts.size(); ++i)
      {
      std::string part_i = global_state().deref_alias(parts[i]);

      if(i == 0 && part_i.find_first_of(",()") != std::string::npos)
         {
         std::vector<std::string> parts_i = parse_and_deref_aliases(part_i);

         for(size_t j = 0; j != parts_i.size(); ++j)
            out.push_back(parts_i[j]);
         }
      else
         out.push_back(part_i);
      }

   return out;
   }

}

SCAN_Name::SCAN_Name(const std::string& algo_spec)
   {
   orig_algo_spec = algo_spec;

   try
      {
      name = parse_and_deref_aliases(algo_spec);
      if(name.size() == 0)
         throw Decoding_Error("Bad SCAN name " + algo_spec);
      }
   catch(Invalid_Algorithm_Name)
      {
      name.clear();
      }

   if(name.size() == 0 && algo_spec.find('/') != std::string::npos)
      {
      std::vector<std::string> algo_parts = split_on(algo_spec, '/');

      name = parse_and_deref_aliases(algo_parts[0]);
      if(name.size() == 0)
         throw Decoding_Error("Bad SCAN name " + algo_spec);

      for(size_t i = 1; i != algo_parts.size(); ++i)
         mode_str.push_back(algo_parts[i]);
      }
   }

std::string SCAN_Name::algo_name_and_args() const
   {
   std::string out;

   out = name[0];

   if(arg_count())
      {
      out += '(';
      for(u32bit i = 0; i != arg_count(); ++i)
         {
         out += arg(i);
         if(i != arg_count() - 1)
            out += ',';
         }
      out += ')';

      }

   return out;
   }

std::string SCAN_Name::arg(u32bit i) const
   {
   if(i >= arg_count())
      throw std::range_error("SCAN_Name::argument");
   return name[i+1];
   }

std::string SCAN_Name::arg(u32bit i, const std::string& def_value) const
   {
   if(i >= arg_count())
      return def_value;
   return name[i+1];
   }

u32bit SCAN_Name::arg_as_u32bit(u32bit i, u32bit def_value) const
   {
   if(i >= arg_count())
      return def_value;
   return to_u32bit(name[i+1]);
   }

}