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