Coverage Report

Created: 2021-02-21 07:20

/src/botan/src/lib/utils/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/internal/scan_name.h>
9
#include <botan/internal/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
63.5k
   {
18
63.5k
   std::string output = name[start].second;
19
63.5k
   size_t level = name[start].first;
20
21
63.5k
   size_t paren_depth = 0;
22
23
63.5k
   for(size_t i = start + 1; i != name.size(); ++i)
24
1.09k
      {
25
1.09k
      if(name[i].first <= name[start].first)
26
1.09k
         break;
27
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
         for (size_t j = name[i].first; j < level; j++) {
36
0
            output += ")";
37
0
            --paren_depth;
38
0
         }
39
0
         output += "," + name[i].second;
40
0
         }
41
0
      else
42
0
         {
43
0
         if(output[output.size() - 1] != '(')
44
0
            output += ",";
45
0
         output += name[i].second;
46
0
         }
47
48
0
      level = name[i].first;
49
0
      }
50
51
63.5k
   for(size_t i = 0; i != paren_depth; ++i)
52
0
      output += ")";
53
54
63.5k
   return output;
55
63.5k
   }
56
57
}
58
59
SCAN_Name::SCAN_Name(const char* algo_spec) : SCAN_Name(std::string(algo_spec))
60
0
   {
61
0
   }
62
63
SCAN_Name::SCAN_Name(std::string algo_spec) : m_orig_algo_spec(algo_spec), m_alg_name(), m_args(), m_mode_info()
64
62.6k
   { 
65
62.6k
   if(algo_spec.size() == 0)
66
0
      throw Invalid_Argument("Expected algorithm name, got empty string");
67
68
62.6k
   std::vector<std::pair<size_t, std::string>> name;
69
62.6k
   size_t level = 0;
70
62.6k
   std::pair<size_t, std::string> accum = std::make_pair(level, "");
71
72
62.6k
   const std::string decoding_error = "Bad SCAN name '" + algo_spec + "': ";
73
74
1.03M
   for(size_t i = 0; i != algo_spec.size(); ++i)
75
973k
      {
76
973k
      char c = algo_spec[i];
77
78
973k
      if(c == '/' || c == ',' || c == '(' || c == ')')
79
125k
         {
80
125k
         if(c == '(')
81
62.4k
            ++level;
82
63.5k
         else if(c == ')')
83
62.4k
            {
84
62.4k
            if(level == 0)
85
0
               throw Decoding_Error(decoding_error + "Mismatched parens");
86
62.4k
            --level;
87
62.4k
            }
88
89
125k
         if(c == '/' && level > 0)
90
0
            accum.second.push_back(c);
91
125k
         else
92
125k
            {
93
125k
            if(accum.second != "")
94
125k
               name.push_back(accum);
95
125k
            accum = std::make_pair(level, "");
96
125k
            }
97
125k
         }
98
847k
      else
99
847k
         accum.second.push_back(c);
100
973k
      }
101
102
62.6k
   if(accum.second != "")
103
218
      name.push_back(accum);
104
105
62.6k
   if(level != 0)
106
0
      throw Decoding_Error(decoding_error + "Missing close paren");
107
108
62.6k
   if(name.size() == 0)
109
0
      throw Decoding_Error(decoding_error + "Empty name");
110
111
62.6k
   m_alg_name = name[0].second;
112
113
62.6k
   bool in_modes = false;
114
115
126k
   for(size_t i = 1; i != name.size(); ++i)
116
63.5k
      {
117
63.5k
      if(name[i].first == 0)
118
0
         {
119
0
         m_mode_info.push_back(make_arg(name, i));
120
0
         in_modes = true;
121
0
         }
122
63.5k
      else if(name[i].first == 1 && !in_modes)
123
63.5k
         m_args.push_back(make_arg(name, i));
124
63.5k
      }
125
62.6k
   }
126
127
std::string SCAN_Name::arg(size_t i) const
128
56.5k
   {
129
56.5k
   if(i >= arg_count())
130
0
      throw Invalid_Argument("SCAN_Name::arg " + std::to_string(i) +
131
0
                             " out of range for '" + to_string() + "'");
132
56.5k
   return m_args[i];
133
56.5k
   }
134
135
std::string SCAN_Name::arg(size_t i, const std::string& def_value) const
136
90
   {
137
90
   if(i >= arg_count())
138
0
      return def_value;
139
90
   return m_args[i];
140
90
   }
141
142
size_t SCAN_Name::arg_as_integer(size_t i, size_t def_value) const
143
2.30k
   {
144
2.30k
   if(i >= arg_count())
145
1.47k
      return def_value;
146
828
   return to_u32bit(m_args[i]);
147
828
   }
148
149
size_t SCAN_Name::arg_as_integer(size_t i) const
150
0
   {
151
0
   return to_u32bit(arg(i));
152
0
   }
153
154
}