Coverage Report

Created: 2022-09-23 06:05

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