Coverage Report

Created: 2020-02-14 15:38

/src/botan/src/lib/base/scan_name.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* SCAN Name Abstraction
3
* (C) 2008-2009,2015 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/scan_name.h>
9
#include <botan/parsing.h>
10
#include <botan/exceptn.h>
11
12
namespace Botan {
13
14
namespace {
15
16
std::string make_arg(const std::vector<std::pair<size_t, std::string>>& name, size_t start)
17
76.6k
   {
18
76.6k
   std::string output = name[start].second;
19
76.6k
   size_t level = name[start].first;
20
76.6k
21
76.6k
   size_t paren_depth = 0;
22
76.6k
23
76.6k
   for(size_t i = start + 1; i != name.size(); ++i)
24
1.12k
      {
25
1.12k
      if(name[i].first <= name[start].first)
26
1.12k
         break;
27
0
28
0
      if(name[i].first > level)
29
0
         {
30
0
         output += "(" + name[i].second;
31
0
         ++paren_depth;
32
0
         }
33
0
      else if(name[i].first < level)
34
0
         {
35
0
         output += ")," + name[i].second;
36
0
         --paren_depth;
37
0
         }
38
0
      else
39
0
         {
40
0
         if(output[output.size() - 1] != '(')
41
0
            output += ",";
42
0
         output += name[i].second;
43
0
         }
44
0
45
0
      level = name[i].first;
46
0
      }
47
76.6k
48
76.6k
   for(size_t i = 0; i != paren_depth; ++i)
49
0
      output += ")";
50
76.6k
51
76.6k
   return output;
52
76.6k
   }
53
54
}
55
56
SCAN_Name::SCAN_Name(const char* algo_spec) : SCAN_Name(std::string(algo_spec))
57
0
   {
58
0
   }
59
60
SCAN_Name::SCAN_Name(std::string algo_spec) : m_orig_algo_spec(algo_spec), m_alg_name(), m_args(), m_mode_info()
61
92.4k
   {
62
92.4k
   if(algo_spec.size() == 0)
63
0
      throw Invalid_Argument("Expected algorithm name, got empty string");
64
92.4k
65
92.4k
   std::vector<std::pair<size_t, std::string>> name;
66
92.4k
   size_t level = 0;
67
92.4k
   std::pair<size_t, std::string> accum = std::make_pair(level, "");
68
92.4k
69
92.4k
   const std::string decoding_error = "Bad SCAN name '" + algo_spec + "': ";
70
92.4k
71
1.41M
   for(size_t i = 0; i != algo_spec.size(); ++i)
72
1.31M
      {
73
1.31M
      char c = algo_spec[i];
74
1.31M
75
1.31M
      if(c == '/' || c == ',' || c == '(' || c == ')')
76
152k
         {
77
152k
         if(c == '(')
78
75.5k
            ++level;
79
76.6k
         else if(c == ')')
80
75.5k
            {
81
75.5k
            if(level == 0)
82
0
               throw Decoding_Error(decoding_error + "Mismatched parens");
83
75.5k
            --level;
84
75.5k
            }
85
152k
86
152k
         if(c == '/' && level > 0)
87
0
            accum.second.push_back(c);
88
152k
         else
89
152k
            {
90
152k
            if(accum.second != "")
91
152k
               name.push_back(accum);
92
152k
            accum = std::make_pair(level, "");
93
152k
            }
94
152k
         }
95
1.16M
      else
96
1.16M
         accum.second.push_back(c);
97
1.31M
      }
98
92.4k
99
92.4k
   if(accum.second != "")
100
16.9k
      name.push_back(accum);
101
92.4k
102
92.4k
   if(level != 0)
103
0
      throw Decoding_Error(decoding_error + "Missing close paren");
104
92.4k
105
92.4k
   if(name.size() == 0)
106
0
      throw Decoding_Error(decoding_error + "Empty name");
107
92.4k
108
92.4k
   m_alg_name = name[0].second;
109
92.4k
110
92.4k
   bool in_modes = false;
111
92.4k
112
169k
   for(size_t i = 1; i != name.size(); ++i)
113
76.6k
      {
114
76.6k
      if(name[i].first == 0)
115
0
         {
116
0
         m_mode_info.push_back(make_arg(name, i));
117
0
         in_modes = true;
118
0
         }
119
76.6k
      else if(name[i].first == 1 && !in_modes)
120
76.6k
         m_args.push_back(make_arg(name, i));
121
76.6k
      }
122
92.4k
   }
123
124
std::string SCAN_Name::arg(size_t i) const
125
65.0k
   {
126
65.0k
   if(i >= arg_count())
127
0
      throw Invalid_Argument("SCAN_Name::arg " + std::to_string(i) +
128
0
                             " out of range for '" + to_string() + "'");
129
65.0k
   return m_args[i];
130
65.0k
   }
131
132
std::string SCAN_Name::arg(size_t i, const std::string& def_value) const
133
88
   {
134
88
   if(i >= arg_count())
135
0
      return def_value;
136
88
   return m_args[i];
137
88
   }
138
139
size_t SCAN_Name::arg_as_integer(size_t i, size_t def_value) const
140
2.31k
   {
141
2.31k
   if(i >= arg_count())
142
1.44k
      return def_value;
143
865
   return to_u32bit(m_args[i]);
144
865
   }
145
146
}