Botan 3.9.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(auto name = lookup_static_oid(oid)) {
23 if(*name != str) {
24 throw Invalid_State("Cannot register two different names to a single OID");
25 } else {
26 return;
27 }
28 }
29
31
32 auto o2s = m_oid2str.find(oid);
33
34 if(o2s == m_oid2str.end()) {
35 m_oid2str.insert(std::make_pair(oid, str));
36 } else if(o2s->second != str) {
37 throw Invalid_State("Cannot register two different names to a single OID");
38 }
39
40 auto s2o = m_str2oid.find(std::string(str));
41
42 if(s2o == m_str2oid.end()) {
43 m_str2oid.insert(std::make_pair(str, oid));
44 }
45}
46
47void OID_Map::add_str2oid(const OID& oid, std::string_view str) {
48 if(lookup_static_oid_name(str).has_value()) {
49 return;
50 }
51
53 if(!m_str2oid.contains(std::string(str))) {
54 m_str2oid.insert(std::make_pair(str, oid));
55 }
56}
57
58void OID_Map::add_oid2str(const OID& oid, std::string_view str) {
59 if(lookup_static_oid(oid).has_value()) {
60 return;
61 }
62
64 if(!m_oid2str.contains(oid)) {
65 m_oid2str.insert(std::make_pair(oid, str));
66 }
67}
68
69std::string OID_Map::oid2str(const OID& oid) {
70 if(auto name = lookup_static_oid(oid)) {
71 return std::string(*name);
72 }
73
75
76 auto i = m_oid2str.find(oid);
77 if(i != m_oid2str.end()) {
78 return i->second;
79 }
80
81 return "";
82}
83
84OID OID_Map::str2oid(std::string_view str) {
85 if(auto oid = lookup_static_oid_name(str)) {
86 return std::move(*oid);
87 }
88
90 auto i = m_str2oid.find(std::string(str));
91 if(i != m_str2oid.end()) {
92 return i->second;
93 }
94
95 return OID();
96}
97
98} // namespace Botan
std::string oid2str(const OID &oid)
Definition oid_map.cpp:69
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
void add_str2oid(const OID &oid, std::string_view str)
Definition oid_map.cpp:47
void add_oid2str(const OID &oid, std::string_view str)
Definition oid_map.cpp:58
OID str2oid(std::string_view str)
Definition oid_map.cpp:84
secure_vector< T > lock(const std::vector< T > &in)
Definition secmem.h:81
lock_guard< T > lock_guard_type
Definition mutex.h:55