Coverage Report

Created: 2026-08-30 06:41

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
21
OID_Map::OID_Map() {
12
21
   m_str2oid = OID_Map::load_str2oid_map();
13
21
   m_oid2str = OID_Map::load_oid2str_map();
14
21
}
15
16
248k
OID_Map& OID_Map::global_registry() {
17
248k
   static OID_Map g_map;
18
248k
   return g_map;
19
248k
}
20
21
0
void OID_Map::add_oid(const OID& oid, std::string_view str) {
22
0
   if(str.empty()) {
23
0
      throw Invalid_Argument("Cannot register an empty name for an OID");
24
0
   }
25
26
0
   if(auto name = lookup_static_oid(oid)) {
27
0
      if(*name != str) {
28
0
         throw Invalid_State("Cannot register two different names to a single OID");
29
0
      } else {
30
0
         return;
31
0
      }
32
0
   }
33
34
0
   const lock_guard_type<mutex_type> lock(m_mutex);
35
36
0
   auto o2s = m_oid2str.find(oid);
37
38
0
   if(o2s == m_oid2str.end()) {
39
0
      m_oid2str.insert(std::make_pair(oid, str));
40
0
   } else if(o2s->second != str) {
41
0
      throw Invalid_State("Cannot register two different names to a single OID");
42
0
   }
43
44
0
   auto s2o = m_str2oid.find(std::string(str));
45
46
0
   if(s2o == m_str2oid.end()) {
47
0
      m_str2oid.insert(std::make_pair(str, oid));
48
0
   }
49
0
}
50
51
0
void OID_Map::add_str2oid(const OID& oid, std::string_view str) {
52
0
   if(lookup_static_oid_name(str).has_value()) {
53
0
      return;
54
0
   }
55
56
0
   const lock_guard_type<mutex_type> lock(m_mutex);
57
0
   if(!m_str2oid.contains(std::string(str))) {
58
0
      m_str2oid.insert(std::make_pair(str, oid));
59
0
   }
60
0
}
61
62
0
void OID_Map::add_oid2str(const OID& oid, std::string_view str) {
63
0
   if(lookup_static_oid(oid).has_value()) {
64
0
      return;
65
0
   }
66
67
0
   const lock_guard_type<mutex_type> lock(m_mutex);
68
0
   if(!m_oid2str.contains(oid)) {
69
0
      m_oid2str.insert(std::make_pair(oid, str));
70
0
   }
71
0
}
72
73
163k
std::optional<std::string> OID_Map::oid2str(const OID& oid) {
74
163k
   if(auto name = lookup_static_oid(oid)) {
75
81.7k
      return std::string(*name);
76
81.7k
   }
77
78
82.1k
   const lock_guard_type<mutex_type> lock(m_mutex);
79
80
82.1k
   auto i = m_oid2str.find(oid);
81
82.1k
   if(i != m_oid2str.end()) {
82
255
      return i->second;
83
255
   }
84
85
81.8k
   return {};
86
82.1k
}
87
88
84.7k
OID OID_Map::str2oid(std::string_view str) {
89
84.7k
   if(auto oid = lookup_static_oid_name(str)) {
90
84.7k
      return std::move(*oid);
91
84.7k
   }
92
93
0
   const lock_guard_type<mutex_type> lock(m_mutex);
94
0
   auto i = m_str2oid.find(std::string(str));
95
0
   if(i != m_str2oid.end()) {
96
0
      return i->second;
97
0
   }
98
99
0
   return OID();
100
0
}
101
102
}  // namespace Botan