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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

94.29
/src/lib/utils/uuid/uuid.cpp
1
/*
2
* UUID type
3
* (C) 2015,2018 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/uuid.h>
9

10
#include <botan/hex.h>
11
#include <botan/rng.h>
12
#include <botan/internal/fmt.h>
13
#include <sstream>
14

15
namespace Botan {
16

17
UUID::UUID(RandomNumberGenerator& rng) {
7✔
18
   m_uuid.resize(16);
7✔
19
   rng.randomize(m_uuid.data(), m_uuid.size());
7✔
20

21
   // Mark as a random v4 UUID (RFC 4122 sec 4.4)
22
   m_uuid[6] = 0x40 | (m_uuid[6] & 0x0F);
7✔
23

24
   // Set reserved bits
25
   m_uuid[8] = 0x80 | (m_uuid[8] & 0x3F);
7✔
26
}
7✔
27

28
UUID::UUID(const std::vector<uint8_t>& blob) {
3✔
29
   if(blob.size() != 16) {
3✔
30
      throw Invalid_Argument("Bad UUID blob " + hex_encode(blob));
2✔
31
   }
32

33
   m_uuid = blob;
2✔
34
}
3✔
35

36
UUID::UUID(std::string_view uuid_str) {
2✔
37
   if(uuid_str.size() != 36 || uuid_str[8] != '-' || uuid_str[13] != '-' || uuid_str[18] != '-' ||
2✔
38
      uuid_str[23] != '-') {
2✔
39
      throw Invalid_Argument(fmt("Bad UUID '{}'", uuid_str));
×
40
   }
41

42
   std::string just_hex;
2✔
43
   for(char c : uuid_str) {
74✔
44
      if(c == '-')
72✔
45
         continue;
8✔
46

47
      just_hex += c;
136✔
48
   }
49

50
   m_uuid = hex_decode(just_hex);
2✔
51

52
   if(m_uuid.size() != 16) {
2✔
53
      throw Invalid_Argument(fmt("Bad UUID '{}'", uuid_str));
×
54
   }
55
}
2✔
56

57
std::string UUID::to_string() const {
9✔
58
   if(is_valid() == false)
9✔
59
      throw Invalid_State("UUID object is empty cannot convert to string");
1✔
60

61
   const std::string raw = hex_encode(m_uuid);
8✔
62

63
   std::ostringstream formatted;
8✔
64

65
   for(size_t i = 0; i != raw.size(); ++i) {
264✔
66
      if(i == 8 || i == 12 || i == 16 || i == 20)
256✔
67
         formatted << "-";
32✔
68
      formatted << raw[i];
256✔
69
   }
70

71
   return formatted.str();
8✔
72
}
16✔
73

74
}
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