Coverage Report

Created: 2024-02-25 06:16

/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
10
#include <botan/exceptn.h>
11
#include <botan/internal/parsing.h>
12
13
namespace Botan {
14
15
namespace {
16
17
90.4k
std::string make_arg(const std::vector<std::pair<size_t, std::string>>& name, size_t start) {
18
90.4k
   std::string output = name[start].second;
19
90.4k
   size_t level = name[start].first;
20
21
90.4k
   size_t paren_depth = 0;
22
23
104k
   for(size_t i = start + 1; i != name.size(); ++i) {
24
43.2k
      if(name[i].first <= name[start].first) {
25
28.8k
         break;
26
28.8k
      }
27
28
14.3k
      if(name[i].first > level) {
29
14.3k
         output += "(" + name[i].second;
30
14.3k
         ++paren_depth;
31
14.3k
      } else if(name[i].first < level) {
32
0
         for(size_t j = name[i].first; j < level; j++) {
33
0
            output += ")";
34
0
            --paren_depth;
35
0
         }
36
0
         output += "," + name[i].second;
37
0
      } else {
38
0
         if(output[output.size() - 1] != '(') {
39
0
            output += ",";
40
0
         }
41
0
         output += name[i].second;
42
0
      }
43
44
14.3k
      level = name[i].first;
45
14.3k
   }
46
47
104k
   for(size_t i = 0; i != paren_depth; ++i) {
48
14.3k
      output += ")";
49
14.3k
   }
50
51
90.4k
   return output;
52
90.4k
}
53
54
}  // namespace
55
56
0
SCAN_Name::SCAN_Name(const char* algo_spec) : SCAN_Name(std::string(algo_spec)) {}
57
58
67.5k
SCAN_Name::SCAN_Name(std::string_view algo_spec) : m_orig_algo_spec(algo_spec), m_alg_name(), m_args(), m_mode_info() {
59
67.5k
   if(algo_spec.empty()) {
60
0
      throw Invalid_Argument("Expected algorithm name, got empty string");
61
0
   }
62
63
67.5k
   std::vector<std::pair<size_t, std::string>> name;
64
67.5k
   size_t level = 0;
65
67.5k
   std::pair<size_t, std::string> accum = std::make_pair(level, "");
66
67
67.5k
   const std::string decoding_error = "Bad SCAN name '" + m_orig_algo_spec + "': ";
68
69
995k
   for(char c : algo_spec) {
70
995k
      if(c == '/' || c == ',' || c == '(' || c == ')') {
71
177k
         if(c == '(') {
72
72.2k
            ++level;
73
104k
         } else if(c == ')') {
74
72.2k
            if(level == 0) {
75
0
               throw Decoding_Error(decoding_error + "Mismatched parens");
76
0
            }
77
72.2k
            --level;
78
72.2k
         }
79
80
177k
         if(c == '/' && level > 0) {
81
0
            accum.second.push_back(c);
82
177k
         } else {
83
177k
            if(!accum.second.empty()) {
84
166k
               name.push_back(accum);
85
166k
            }
86
177k
            accum = std::make_pair(level, "");
87
177k
         }
88
817k
      } else {
89
817k
         accum.second.push_back(c);
90
817k
      }
91
995k
   }
92
93
67.5k
   if(!accum.second.empty()) {
94
5.87k
      name.push_back(accum);
95
5.87k
   }
96
97
67.5k
   if(level != 0) {
98
0
      throw Decoding_Error(decoding_error + "Missing close paren");
99
0
   }
100
101
67.5k
   if(name.empty()) {
102
0
      throw Decoding_Error(decoding_error + "Empty name");
103
0
   }
104
105
67.5k
   m_alg_name = name[0].second;
106
107
67.5k
   bool in_modes = false;
108
109
172k
   for(size_t i = 1; i != name.size(); ++i) {
110
104k
      if(name[i].first == 0) {
111
5.77k
         m_mode_info.push_back(make_arg(name, i));
112
5.77k
         in_modes = true;
113
99.0k
      } else if(name[i].first == 1 && !in_modes) {
114
84.7k
         m_args.push_back(make_arg(name, i));
115
84.7k
      }
116
104k
   }
117
67.5k
}
118
119
43.2k
std::string SCAN_Name::arg(size_t i) const {
120
43.2k
   if(i >= arg_count()) {
121
0
      throw Invalid_Argument("SCAN_Name::arg " + std::to_string(i) + " out of range for '" + to_string() + "'");
122
0
   }
123
43.2k
   return m_args[i];
124
43.2k
}
125
126
968
std::string SCAN_Name::arg(size_t i, std::string_view def_value) const {
127
968
   if(i >= arg_count()) {
128
716
      return std::string(def_value);
129
716
   }
130
252
   return m_args[i];
131
968
}
132
133
15.5k
size_t SCAN_Name::arg_as_integer(size_t i, size_t def_value) const {
134
15.5k
   if(i >= arg_count()) {
135
3.10k
      return def_value;
136
3.10k
   }
137
12.4k
   return to_u32bit(m_args[i]);
138
15.5k
}
139
140
488
size_t SCAN_Name::arg_as_integer(size_t i) const {
141
488
   return to_u32bit(arg(i));
142
488
}
143
144
}  // namespace Botan