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

randombit / botan / 5528662884

12 Jul 2023 07:14AM UTC coverage: 91.714% (-0.02%) from 91.735%
5528662884

Pull #3618

github

web-flow
Merge 4c5d0bf50 into 054e13f3d
Pull Request #3618: [TLS 1.3] PSK Support

78400 of 85483 relevant lines covered (91.71%)

12336202.33 hits per line

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

84.34
/src/lib/utils/stl_util.h
1
/*
2
* STL Utility Functions
3
* (C) 1999-2007 Jack Lloyd
4
* (C) 2015 Simon Warta (Kullo GmbH)
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#ifndef BOTAN_STL_UTIL_H_
10
#define BOTAN_STL_UTIL_H_
11

12
#include <map>
13
#include <set>
14
#include <span>
15
#include <string>
16
#include <tuple>
17
#include <variant>
18
#include <vector>
19

20
#include <botan/concepts.h>
21
#include <botan/secmem.h>
22
#include <botan/strong_type.h>
23

24
namespace Botan {
25

26
template <concepts::contiguous_container T = std::vector<uint8_t>>
27
inline T to_byte_vector(std::string_view s) {
739✔
28
   return T(s.cbegin(), s.cend());
739✔
29
}
30

31
inline std::string to_string(std::span<const uint8_t> bytes) {
5✔
32
   return std::string(bytes.begin(), bytes.end());
5✔
33
}
34

35
/**
36
* Return the keys of a map as a std::set
37
*/
38
template <typename K, typename V>
39
std::set<K> map_keys_as_set(const std::map<K, V>& kv) {
×
40
   std::set<K> s;
×
41
   for(auto&& i : kv) {
×
42
      s.insert(i.first);
×
43
   }
44
   return s;
×
45
}
×
46

47
/**
48
* Return the keys of a multimap as a std::set
49
*/
50
template <typename K, typename V>
51
std::set<K> map_keys_as_set(const std::multimap<K, V>& kv) {
×
52
   std::set<K> s;
×
53
   for(auto&& i : kv) {
×
54
      s.insert(i.first);
×
55
   }
56
   return s;
×
57
}
×
58

59
/*
60
* Searching through a std::map
61
* @param mapping the map to search
62
* @param key is what to look for
63
* @param null_result is the value to return if key is not in mapping
64
* @return mapping[key] or null_result
65
*/
66
template <typename K, typename V>
67
inline V search_map(const std::map<K, V>& mapping, const K& key, const V& null_result = V()) {
17✔
68
   auto i = mapping.find(key);
17✔
69
   if(i == mapping.end())
17✔
70
      return null_result;
17✔
71
   return i->second;
16✔
72
}
73

74
template <typename K, typename V, typename R>
75
inline R search_map(const std::map<K, V>& mapping, const K& key, const R& null_result, const R& found_result) {
76
   auto i = mapping.find(key);
77
   if(i == mapping.end())
78
      return null_result;
79
   return found_result;
80
}
81

82
/*
83
* Insert a key/value pair into a multimap
84
*/
85
template <typename K, typename V>
86
void multimap_insert(std::multimap<K, V>& multimap, const K& key, const V& value) {
646,356✔
87
   multimap.insert(std::make_pair(key, value));
646,356✔
88
}
646,356✔
89

90
/**
91
* Existence check for values
92
*/
93
template <typename T, typename OT>
94
bool value_exists(const std::vector<T>& vec, const OT& val) {
193,412✔
95
   for(size_t i = 0; i != vec.size(); ++i)
2,825,945✔
96
      if(vec[i] == val)
2,798,923✔
97
         return true;
98,973✔
98
   return false;
99
}
100

101
template <typename T, typename Pred>
102
void map_remove_if(Pred pred, T& assoc) {
2,060✔
103
   auto i = assoc.begin();
2,060✔
104
   while(i != assoc.end()) {
2,060✔
105
      if(pred(i->first))
4,120✔
106
         assoc.erase(i++);
2,060✔
107
      else
108
         i++;
6,180✔
109
   }
110
}
2,060✔
111

112
/**
113
 * Helper class to ease unmarshalling of concatenated fixed-length values
114
 */
115
class BufferSlicer final {
116
   public:
117
      BufferSlicer(std::span<const uint8_t> buffer) : m_remaining(buffer) {}
4,411✔
118

119
      template <concepts::contiguous_container ContainerT>
120
      auto copy(const size_t count) {
3,712✔
121
         const auto result = take(count);
3,712✔
122
         return ContainerT(result.begin(), result.end());
3,710✔
123
      }
124

125
      auto copy_as_vector(const size_t count) { return copy<std::vector<uint8_t>>(count); }
2,064✔
126

127
      auto copy_as_secure_vector(const size_t count) { return copy<secure_vector<uint8_t>>(count); }
1,264✔
128

129
      std::span<const uint8_t> take(const size_t count) {
31,987✔
130
         BOTAN_STATE_CHECK(remaining() >= count);
6✔
131
         auto result = m_remaining.first(count);
31,981✔
132
         m_remaining = m_remaining.subspan(count);
31,981✔
133
         return result;
31,981✔
134
      }
135

136
      template <concepts::contiguous_strong_type T>
137
      StrongSpan<const T> take(const size_t count) {
25,219✔
138
         return StrongSpan<const T>(take(count));
24,357✔
139
      }
140

141
      void copy_into(std::span<uint8_t> sink) {
2✔
142
         const auto data = take(sink.size());
2✔
143
         std::copy(data.begin(), data.end(), sink.begin());
2✔
144
      }
2✔
145

146
      void skip(const size_t count) { take(count); }
593✔
147

148
      size_t remaining() const { return m_remaining.size(); }
33,289✔
149

150
      bool empty() const { return m_remaining.empty(); }
2,245✔
151

152
   private:
153
      std::span<const uint8_t> m_remaining;
154
};
155

156
/**
157
 * @brief Helper class to ease in-place marshalling of concatenated fixed-length
158
 *        values.
159
 *
160
 * The size of the final buffer must be known from the start, reallocations are
161
 * not performed.
162
 */
163
class BufferStuffer {
164
   public:
165
      BufferStuffer(std::span<uint8_t> buffer) : m_buffer(buffer) {}
30,044✔
166

167
      /**
168
       * @returns a span for the next @p bytes bytes in the concatenated buffer.
169
       *          Checks that the buffer is not exceded.
170
       */
171
      std::span<uint8_t> next(size_t bytes) {
1,394,147✔
172
         BOTAN_STATE_CHECK(m_buffer.size() >= bytes);
1,394,147✔
173

174
         auto result = m_buffer.first(bytes);
1,394,143✔
175
         m_buffer = m_buffer.subspan(bytes);
1,394,143✔
176
         return result;
1,394,143✔
177
      }
178

179
      template <concepts::contiguous_strong_type StrongT>
180
      StrongSpan<StrongT> next(size_t bytes) {
1,391,906✔
181
         return StrongSpan<StrongT>(next(bytes));
1,393,187✔
182
      }
183

184
      void append(std::span<const uint8_t> buffer) {
3✔
185
         auto sink = next(buffer.size());
3✔
186
         std::copy(buffer.begin(), buffer.end(), sink.begin());
1✔
187
      }
1✔
188

189
      bool full() const { return m_buffer.empty(); }
92✔
190

191
      size_t remaining_capacity() const { return m_buffer.size(); }
246✔
192

193
   private:
194
      std::span<uint8_t> m_buffer;
195
};
196

197
/**
198
 * Concatenate an arbitrary number of buffers.
199
 * @return the concatenation of \p buffers as the container type of the first buffer
200
 */
201
template <typename... Ts>
202
decltype(auto) concat(Ts&&... buffers) {
3,708,754✔
203
   static_assert(sizeof...(buffers) > 0, "concat requires at least one buffer");
204

205
   using result_t = std::remove_cvref_t<std::tuple_element_t<0, std::tuple<Ts...>>>;
206
   result_t result;
3,708,754✔
207
   result.reserve((buffers.size() + ...));
3,708,754✔
208
   (result.insert(result.end(), buffers.begin(), buffers.end()), ...);
3,708,754✔
209
   return result;
3,708,754✔
210
}
×
211

212
/**
213
 * Concatenate an arbitrary number of buffers and define the output buffer
214
 * type as a mandatory template parameter.
215
 * @return the concatenation of \p buffers as the user-defined container type
216
 */
217
template <typename ResultT, typename... Ts>
218
ResultT concat_as(Ts&&... buffers) {
3,519,838✔
219
   return concat(ResultT(), std::forward<Ts>(buffers)...);
7,039,676✔
220
}
221

222
template <typename... Alts, typename... Ts>
223
constexpr bool holds_any_of(const std::variant<Ts...>& v) noexcept {
3,633✔
224
   return (std::holds_alternative<Alts>(v) || ...);
3,633✔
225
}
226

227
template <typename GeneralVariantT, typename SpecialT>
228
constexpr bool is_generalizable_to(const SpecialT&) noexcept {
229
   return std::is_constructible_v<GeneralVariantT, SpecialT>;
230
}
231

232
template <typename GeneralVariantT, typename... SpecialTs>
233
constexpr bool is_generalizable_to(const std::variant<SpecialTs...>&) noexcept {
234
   return (std::is_constructible_v<GeneralVariantT, SpecialTs> && ...);
235
}
236

237
/**
238
 * @brief Converts a given variant into another variant-ish whose type states
239
 *        are a super set of the given variant.
240
 *
241
 * This is useful to convert restricted variant types into more general
242
 * variants types.
243
 */
244
template <typename GeneralVariantT, typename SpecialT>
245
constexpr GeneralVariantT generalize_to(SpecialT&& specific) noexcept
1,373✔
246
   requires(std::is_constructible_v<GeneralVariantT, std::decay_t<SpecialT>>)
247
{
248
   return std::forward<SpecialT>(specific);
1,373✔
249
}
250

251
/**
252
 * @brief Converts a given variant into another variant-ish whose type states
253
 *        are a super set of the given variant.
254
 *
255
 * This is useful to convert restricted variant types into more general
256
 * variants types.
257
 */
258
template <typename GeneralVariantT, typename... SpecialTs>
259
constexpr GeneralVariantT generalize_to(std::variant<SpecialTs...> specific) noexcept {
3,238✔
260
   static_assert(
261
      is_generalizable_to<GeneralVariantT>(specific),
262
      "Desired general type must be implicitly constructible by all types of the specialized std::variant<>");
263
   return std::visit([](auto s) -> GeneralVariantT { return s; }, std::move(specific));
5,103✔
264
}
265

266
// This is a helper utility to emulate pattern matching with std::visit.
267
// See https://en.cppreference.com/w/cpp/utility/variant/visit for more info.
268
template <class... Ts>
269
struct overloaded : Ts... {
270
      using Ts::operator()...;
271
};
272
// explicit deduction guide (not needed as of C++20)
273
template <class... Ts>
274
overloaded(Ts...) -> overloaded<Ts...>;
275

276
}  // namespace Botan
277

278
#endif
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