Coverage Report

Created: 2026-10-13 06:31

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