Coverage Report

Created: 2021-05-04 09:02

/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
50.2k
   {
18
50.2k
   std::string output = name[start].second;
19
50.2k
   size_t level = name[start].first;
20
21
50.2k
   size_t paren_depth = 0;
22
23
50.2k
   for(size_t i = start + 1; i != name.size(); ++i)
24
829
      {
25
829
      if(name[i].first <= name[start].first)
26
829
         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
50.2k
   for(size_t i = 0; i != paren_depth; ++i)
52
0
      output += ")";
53
54
50.2k
   return output;
55
50.2k
   }
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
49.5k
   { 
65
49.5k
   if(algo_spec.size() == 0)
66
0
      throw Invalid_Argument("Expected algorithm name, got empty string");
67
68
49.5k
   std::vector<std::pair<size_t, std::string>> name;
69
49.5k
   size_t level = 0;
70
49.5k
   std::pair<size_t, std::string> accum = std::make_pair(level, "");
71
72
49.5k
   const std::string decoding_error = "Bad SCAN name '" + algo_spec + "': ";
73
74
776k
   for(size_t i = 0; i != algo_spec.size(); ++i)
75
727k
      {
76
727k
      char c = algo_spec[i];
77
78
727k
      if(c == '/' || c == ',' || c == '(' || c == ')')
79
99.6k
         {
80
99.6k
         if(c == '(')
81
49.4k
            ++level;
82
50.2k
         else if(c == ')')
83
49.4k
            {
84
49.4k
            if(level == 0)
85
0
               throw Decoding_Error(decoding_error + "Mismatched parens");
86
49.4k
            --level;
87
49.4k
            }
88
89
99.6k
         if(c == '/' && level > 0)
90
0
            accum.second.push_back(c);
91
99.6k
         else
92
99.6k
            {
93
99.6k
            if(accum.second != "")
94
99.6k
               name.push_back(accum);
95
99.6k
            accum = std::make_pair(level, "");
96
99.6k
            }
97
99.6k
         }
98
627k
      else
99
627k
         accum.second.push_back(c);
100
727k
      }
101
102
49.5k
   if(accum.second != "")
103
116
      name.push_back(accum);
104
105
49.5k
   if(level != 0)
106
0
      throw Decoding_Error(decoding_error + "Missing close paren");
107
108
49.5k
   if(name.size() == 0)
109
0
      throw Decoding_Error(decoding_error + "Empty name");
110
111
49.5k
   m_alg_name = name[0].second;
112
113
49.5k
   bool in_modes = false;
114
115
99.8k
   for(size_t i = 1; i != name.size(); ++i)
116
50.2k
      {
117
50.2k
      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
50.2k
      else if(name[i].first == 1 && !in_modes)
123
50.2k
         m_args.push_back(make_arg(name, i));
124
50.2k
      }
125
49.5k
   }
126
127
std::string SCAN_Name::arg(size_t i) const
128
55.9k
   {
129
55.9k
   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
55.9k
   return m_args[i];
133
55.9k
   }
134
135
std::string SCAN_Name::arg(size_t i, const std::string& def_value) const
136
65
   {
137
65
   if(i >= arg_count())
138
0
      return def_value;
139
65
   return m_args[i];
140
65
   }
141
142
size_t SCAN_Name::arg_as_integer(size_t i, size_t def_value) const
143
1.55k
   {
144
1.55k
   if(i >= arg_count())
145
917
      return def_value;
146
641
   return to_u32bit(m_args[i]);
147
641
   }
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
}