Coverage Report

Created: 2023-02-13 06:21

/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
87.6k
   {
18
87.6k
   std::string output = name[start].second;
19
87.6k
   size_t level = name[start].first;
20
21
87.6k
   size_t paren_depth = 0;
22
23
87.6k
   for(size_t i = start + 1; i != name.size(); ++i)
24
17.2k
      {
25
17.2k
      if(name[i].first <= name[start].first)
26
17.2k
         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
87.6k
   for(size_t i = 0; i != paren_depth; ++i)
52
0
      output += ")";
53
54
87.6k
   return output;
55
87.6k
   }
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(const std::string& algo_spec) :
64
   m_orig_algo_spec(algo_spec),
65
   m_alg_name(),
66
   m_args(),
67
   m_mode_info()
68
123k
   {
69
123k
   if(algo_spec.empty())
70
0
      throw Invalid_Argument("Expected algorithm name, got empty string");
71
72
123k
   std::vector<std::pair<size_t, std::string>> name;
73
123k
   size_t level = 0;
74
123k
   std::pair<size_t, std::string> accum = std::make_pair(level, "");
75
76
123k
   const std::string decoding_error = "Bad SCAN name '" + algo_spec + "': ";
77
78
123k
   for(char c : algo_spec)
79
1.94M
      {
80
1.94M
      if(c == '/' || c == ',' || c == '(' || c == ')')
81
158k
         {
82
158k
         if(c == '(')
83
70.4k
            ++level;
84
87.6k
         else if(c == ')')
85
70.4k
            {
86
70.4k
            if(level == 0)
87
0
               throw Decoding_Error(decoding_error + "Mismatched parens");
88
70.4k
            --level;
89
70.4k
            }
90
91
158k
         if(c == '/' && level > 0)
92
0
            accum.second.push_back(c);
93
158k
         else
94
158k
            {
95
158k
            if(!accum.second.empty())
96
158k
               name.push_back(accum);
97
158k
            accum = std::make_pair(level, "");
98
158k
            }
99
158k
         }
100
1.78M
      else
101
1.78M
         accum.second.push_back(c);
102
1.94M
      }
103
104
123k
   if(!accum.second.empty())
105
53.2k
      name.push_back(accum);
106
107
123k
   if(level != 0)
108
0
      throw Decoding_Error(decoding_error + "Missing close paren");
109
110
123k
   if(name.empty())
111
0
      throw Decoding_Error(decoding_error + "Empty name");
112
113
123k
   m_alg_name = name[0].second;
114
115
123k
   bool in_modes = false;
116
117
211k
   for(size_t i = 1; i != name.size(); ++i)
118
87.6k
      {
119
87.6k
      if(name[i].first == 0)
120
0
         {
121
0
         m_mode_info.push_back(make_arg(name, i));
122
0
         in_modes = true;
123
0
         }
124
87.6k
      else if(name[i].first == 1 && !in_modes)
125
87.6k
         m_args.push_back(make_arg(name, i));
126
87.6k
      }
127
123k
   }
128
129
std::string SCAN_Name::arg(size_t i) const
130
89.4k
   {
131
89.4k
   if(i >= arg_count())
132
0
      throw Invalid_Argument("SCAN_Name::arg " + std::to_string(i) +
133
0
                             " out of range for '" + to_string() + "'");
134
89.4k
   return m_args[i];
135
89.4k
   }
136
137
std::string SCAN_Name::arg(size_t i, const std::string& def_value) const
138
8.40k
   {
139
8.40k
   if(i >= arg_count())
140
0
      return def_value;
141
8.40k
   return m_args[i];
142
8.40k
   }
143
144
size_t SCAN_Name::arg_as_integer(size_t i, size_t def_value) const
145
9.23k
   {
146
9.23k
   if(i >= arg_count())
147
588
      return def_value;
148
8.64k
   return to_u32bit(m_args[i]);
149
9.23k
   }
150
151
size_t SCAN_Name::arg_as_integer(size_t i) const
152
0
   {
153
0
   return to_u32bit(arg(i));
154
0
   }
155
156
}