Coverage Report

Created: 2024-06-28 06:19

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