Coverage Report

Created: 2025-11-16 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/botan/src/lib/asn1/oid_map.cpp
Line
Count
Source
1
/*
2
* (C) 2023 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#include <botan/internal/oid_map.h>
8
9
namespace Botan {
10
11
2
OID_Map::OID_Map() {
12
2
   m_str2oid = OID_Map::load_str2oid_map();
13
2
   m_oid2str = OID_Map::load_oid2str_map();
14
2
}
15
16
1.03k
OID_Map& OID_Map::global_registry() {
17
1.03k
   static OID_Map g_map;
18
1.03k
   return g_map;
19
1.03k
}
20
21
8
void OID_Map::add_oid(const OID& oid, std::string_view str) {
22
8
   if(auto name = lookup_static_oid(oid)) {
23
0
      if(*name != str) {
24
0
         throw Invalid_State("Cannot register two different names to a single OID");
25
0
      } else {
26
0
         return;
27
0
      }
28
0
   }
29
30
8
   lock_guard_type<mutex_type> lock(m_mutex);
31
32
8
   auto o2s = m_oid2str.find(oid);
33
34
8
   if(o2s == m_oid2str.end()) {
35
8
      m_oid2str.insert(std::make_pair(oid, str));
36
8
   } else if(o2s->second != str) {
37
0
      throw Invalid_State("Cannot register two different names to a single OID");
38
0
   }
39
40
8
   auto s2o = m_str2oid.find(std::string(str));
41
42
8
   if(s2o == m_str2oid.end()) {
43
8
      m_str2oid.insert(std::make_pair(str, oid));
44
8
   }
45
8
}
46
47
0
void OID_Map::add_str2oid(const OID& oid, std::string_view str) {
48
0
   if(lookup_static_oid_name(str).has_value()) {
49
0
      return;
50
0
   }
51
52
0
   lock_guard_type<mutex_type> lock(m_mutex);
53
0
   if(!m_str2oid.contains(std::string(str))) {
54
0
      m_str2oid.insert(std::make_pair(str, oid));
55
0
   }
56
0
}
57
58
0
void OID_Map::add_oid2str(const OID& oid, std::string_view str) {
59
0
   if(lookup_static_oid(oid).has_value()) {
60
0
      return;
61
0
   }
62
63
0
   lock_guard_type<mutex_type> lock(m_mutex);
64
0
   if(!m_oid2str.contains(oid)) {
65
0
      m_oid2str.insert(std::make_pair(oid, str));
66
0
   }
67
0
}
68
69
9
std::string OID_Map::oid2str(const OID& oid) {
70
9
   if(auto name = lookup_static_oid(oid)) {
71
1
      return std::string(*name);
72
1
   }
73
74
8
   lock_guard_type<mutex_type> lock(m_mutex);
75
76
8
   auto i = m_oid2str.find(oid);
77
8
   if(i != m_oid2str.end()) {
78
0
      return i->second;
79
0
   }
80
81
8
   return "";
82
8
}
83
84
1.01k
OID OID_Map::str2oid(std::string_view str) {
85
1.01k
   if(auto oid = lookup_static_oid_name(str)) {
86
1.01k
      return std::move(*oid);
87
1.01k
   }
88
89
0
   lock_guard_type<mutex_type> lock(m_mutex);
90
0
   auto i = m_str2oid.find(std::string(str));
91
0
   if(i != m_str2oid.end()) {
92
0
      return i->second;
93
0
   }
94
95
0
   return OID();
96
0
}
97
98
}  // namespace Botan