Coverage Report

Created: 2026-07-16 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.4.0/src/lib/utils/scan_name.cpp
Line
Count
Source
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
183k
std::string make_arg(const std::vector<std::pair<size_t, std::string>>& name, size_t start) {
18
183k
   std::string output = name[start].second;
19
183k
   size_t level = name[start].first;
20
21
183k
   size_t paren_depth = 0;
22
23
193k
   for(size_t i = start + 1; i != name.size(); ++i) {
24
33.8k
      if(name[i].first <= name[start].first) {
25
23.4k
         break;
26
23.4k
      }
27
28
10.3k
      if(name[i].first > level) {
29
10.3k
         output += "(" + name[i].second;
30
10.3k
         ++paren_depth;
31
10.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
10.3k
      level = name[i].first;
45
10.3k
   }
46
47
193k
   for(size_t i = 0; i != paren_depth; ++i) {
48
10.3k
      output += ")";
49
10.3k
   }
50
51
183k
   return output;
52
183k
}
53
54
}  // namespace
55
56
0
SCAN_Name::SCAN_Name(const char* algo_spec) : SCAN_Name(std::string(algo_spec)) {}
57
58
182k
SCAN_Name::SCAN_Name(std::string_view algo_spec) : m_orig_algo_spec(algo_spec), m_alg_name(), m_args(), m_mode_info() {
59
182k
   if(algo_spec.empty()) {
60
0
      throw Invalid_Argument("Expected algorithm name, got empty string");
61
0
   }
62
63
182k
   std::vector<std::pair<size_t, std::string>> name;
64
182k
   size_t level = 0;
65
182k
   std::pair<size_t, std::string> accum = std::make_pair(level, "");
66
67
182k
   const std::string decoding_error = "Bad SCAN name '" + m_orig_algo_spec + "': ";
68
69
2.41M
   for(char c : algo_spec) {
70
2.41M
      if(c == '/' || c == ',' || c == '(' || c == ')') {
71
364k
         if(c == '(') {
72
170k
            ++level;
73
193k
         } else if(c == ')') {
74
170k
            if(level == 0) {
75
0
               throw Decoding_Error(decoding_error + "Mismatched parens");
76
0
            }
77
170k
            --level;
78
170k
         }
79
80
364k
         if(c == '/' && level > 0) {
81
0
            accum.second.push_back(c);
82
364k
         } else {
83
364k
            if(!accum.second.empty()) {
84
354k
               name.push_back(accum);
85
354k
            }
86
364k
            accum = std::make_pair(level, "");
87
364k
         }
88
2.05M
      } else {
89
2.05M
         accum.second.push_back(c);
90
2.05M
      }
91
2.41M
   }
92
93
182k
   if(!accum.second.empty()) {
94
22.8k
      name.push_back(accum);
95
22.8k
   }
96
97
182k
   if(level != 0) {
98
0
      throw Decoding_Error(decoding_error + "Missing close paren");
99
0
   }
100
101
182k
   if(name.empty()) {
102
0
      throw Decoding_Error(decoding_error + "Empty name");
103
0
   }
104
105
182k
   m_alg_name = name[0].second;
106
107
182k
   bool in_modes = false;
108
109
376k
   for(size_t i = 1; i != name.size(); ++i) {
110
193k
      if(name[i].first == 0) {
111
0
         m_mode_info.push_back(make_arg(name, i));
112
0
         in_modes = true;
113
193k
      } else if(name[i].first == 1 && !in_modes) {
114
183k
         m_args.push_back(make_arg(name, i));
115
183k
      }
116
193k
   }
117
182k
}
118
119
137k
std::string SCAN_Name::arg(size_t i) const {
120
137k
   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
137k
   return m_args[i];
124
137k
}
125
126
0
std::string SCAN_Name::arg(size_t i, std::string_view def_value) const {
127
0
   if(i >= arg_count()) {
128
0
      return std::string(def_value);
129
0
   }
130
0
   return m_args[i];
131
0
}
132
133
25.1k
size_t SCAN_Name::arg_as_integer(size_t i, size_t def_value) const {
134
25.1k
   if(i >= arg_count()) {
135
0
      return def_value;
136
0
   }
137
25.1k
   return to_u32bit(m_args[i]);
138
25.1k
}
139
140
0
size_t SCAN_Name::arg_as_integer(size_t i) const {
141
0
   return to_u32bit(arg(i));
142
0
}
143
144
}  // namespace Botan