• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.29
/src/lib/utils/scan_name.cpp
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
std::string make_arg(const std::vector<std::pair<size_t, std::string>>& name, size_t start) {
218,002✔
18
   std::string output = name[start].second;
218,002✔
19
   size_t level = name[start].first;
218,002✔
20

21
   size_t paren_depth = 0;
218,002✔
22

23
   for(size_t i = start + 1; i != name.size(); ++i) {
221,128✔
24
      if(name[i].first <= name[start].first) {
20,424✔
25
         break;
26
      }
27

28
      if(name[i].first > level) {
3,126✔
29
         output += "(" + name[i].second;
6,116✔
30
         ++paren_depth;
3,058✔
31
      } else if(name[i].first < level) {
68✔
32
         for(size_t j = name[i].first; j < level; j++) {
×
33
            output += ")";
×
34
            --paren_depth;
×
35
         }
36
         output += "," + name[i].second;
×
37
      } else {
38
         if(output[output.size() - 1] != '(') {
68✔
39
            output += ",";
68✔
40
         }
41
         output += name[i].second;
68✔
42
      }
43

44
      level = name[i].first;
3,126✔
45
   }
46

47
   for(size_t i = 0; i != paren_depth; ++i) {
221,060✔
48
      output += ")";
3,058✔
49
   }
50

51
   return output;
218,002✔
52
}
×
53

54
}  // namespace
55

56
SCAN_Name::SCAN_Name(const char* algo_spec) : SCAN_Name(std::string(algo_spec)) {}
×
57

58
SCAN_Name::SCAN_Name(std::string_view algo_spec) : m_orig_algo_spec(algo_spec), m_alg_name(), m_args(), m_mode_info() {
242,026✔
59
   if(algo_spec.empty()) {
242,026✔
60
      throw Invalid_Argument("Expected algorithm name, got empty string");
×
61
   }
62

63
   std::vector<std::pair<size_t, std::string>> name;
242,026✔
64
   size_t level = 0;
242,026✔
65
   std::pair<size_t, std::string> accum = std::make_pair(level, "");
242,026✔
66

67
   const std::string decoding_error = "Bad SCAN name '" + m_orig_algo_spec + "': ";
242,026✔
68

69
   for(char c : algo_spec) {
3,181,759✔
70
      if(c == '/' || c == ',' || c == '(' || c == ')') {
2,939,733✔
71
         if(c == '(') {
416,183✔
72
            ++level;
194,886✔
73
         } else if(c == ')') {
221,297✔
74
            if(level == 0) {
194,886✔
75
               throw Decoding_Error(decoding_error + "Mismatched parens");
×
76
            }
77
            --level;
194,886✔
78
         }
79

80
         if(c == '/' && level > 0) {
416,183✔
81
            accum.second.push_back(c);
169✔
82
         } else {
83
            if(!accum.second.empty()) {
416,014✔
84
               name.push_back(accum);
413,344✔
85
            }
86
            accum = std::make_pair(level, "");
3,355,747✔
87
         }
88
      } else {
89
         accum.second.push_back(c);
2,523,550✔
90
      }
91
   }
92

93
   if(!accum.second.empty()) {
242,026✔
94
      name.push_back(accum);
49,810✔
95
   }
96

97
   if(level != 0) {
242,026✔
98
      throw Decoding_Error(decoding_error + "Missing close paren");
×
99
   }
100

101
   if(name.empty()) {
242,026✔
102
      throw Decoding_Error(decoding_error + "Empty name");
×
103
   }
104

105
   m_alg_name = name[0].second;
242,026✔
106

107
   bool in_modes = false;
108

109
   for(size_t i = 1; i != name.size(); ++i) {
463,154✔
110
      if(name[i].first == 0) {
221,128✔
111
         m_mode_info.push_back(make_arg(name, i));
21,738✔
112
         in_modes = true;
10,869✔
113
      } else if(name[i].first == 1 && !in_modes) {
210,259✔
114
         m_args.push_back(make_arg(name, i));
414,266✔
115
      }
116
   }
117
}
245,938✔
118

119
std::string SCAN_Name::arg(size_t i) const {
173,552✔
120
   if(i >= arg_count()) {
173,552✔
121
      throw Invalid_Argument("SCAN_Name::arg " + std::to_string(i) + " out of range for '" + to_string() + "'");
×
122
   }
123
   return m_args[i];
173,552✔
124
}
125

126
std::string SCAN_Name::arg(size_t i, std::string_view def_value) const {
7,545✔
127
   if(i >= arg_count()) {
7,545✔
128
      return std::string(def_value);
3,307✔
129
   }
130
   return m_args[i];
4,238✔
131
}
132

133
size_t SCAN_Name::arg_as_integer(size_t i, size_t def_value) const {
134,175✔
134
   if(i >= arg_count()) {
134,175✔
135
      return def_value;
136
   }
137
   return to_u32bit(m_args[i]);
21,991✔
138
}
139

140
size_t SCAN_Name::arg_as_integer(size_t i) const { return to_u32bit(arg(i)); }
2,986✔
141

142
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc