Coverage Report

Created: 2025-11-16 06:36

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