/src/botan/src/lib/asn1/oids.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * OID Registry |
3 | | * (C) 1999-2008,2013 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #include <botan/oids.h> |
9 | | #include <botan/mutex.h> |
10 | | |
11 | | namespace Botan { |
12 | | |
13 | | namespace { |
14 | | |
15 | | class OID_Map final |
16 | | { |
17 | | public: |
18 | | void add_oid(const OID& oid, const std::string& str) |
19 | 0 | { |
20 | 0 | add_str2oid(oid, str); |
21 | 0 | add_oid2str(oid, str); |
22 | 0 | } |
23 | | |
24 | | void add_str2oid(const OID& oid, const std::string& str) |
25 | 0 | { |
26 | 0 | lock_guard_type<mutex_type> lock(m_mutex); |
27 | 0 | auto i = m_str2oid.find(str); |
28 | 0 | if(i == m_str2oid.end()) |
29 | 0 | m_str2oid.insert(std::make_pair(str, oid)); |
30 | 0 | } |
31 | | |
32 | | void add_oid2str(const OID& oid, const std::string& str) |
33 | 0 | { |
34 | 0 | const std::string oid_str = oid.to_string(); |
35 | 0 | lock_guard_type<mutex_type> lock(m_mutex); |
36 | 0 | auto i = m_oid2str.find(oid_str); |
37 | 0 | if(i == m_oid2str.end()) |
38 | 0 | m_oid2str.insert(std::make_pair(oid_str, str)); |
39 | 0 | } |
40 | | |
41 | | std::string oid2str(const OID& oid) |
42 | 119k | { |
43 | 119k | const std::string oid_str = oid.to_string(); |
44 | 119k | |
45 | 119k | lock_guard_type<mutex_type> lock(m_mutex); |
46 | 119k | |
47 | 119k | auto i = m_oid2str.find(oid_str); |
48 | 119k | if(i != m_oid2str.end()) |
49 | 102k | return i->second; |
50 | 17.0k | |
51 | 17.0k | return ""; |
52 | 17.0k | } |
53 | | |
54 | | OID str2oid(const std::string& str) |
55 | 19.9k | { |
56 | 19.9k | lock_guard_type<mutex_type> lock(m_mutex); |
57 | 19.9k | auto i = m_str2oid.find(str); |
58 | 19.9k | if(i != m_str2oid.end()) |
59 | 19.9k | return i->second; |
60 | 5 | |
61 | 5 | return OID(); |
62 | 5 | } |
63 | | |
64 | | bool have_oid(const std::string& str) |
65 | 0 | { |
66 | 0 | lock_guard_type<mutex_type> lock(m_mutex); |
67 | 0 | return m_str2oid.find(str) != m_str2oid.end(); |
68 | 0 | } |
69 | | |
70 | | static OID_Map& global_registry() |
71 | 139k | { |
72 | 139k | static OID_Map g_map; |
73 | 139k | return g_map; |
74 | 139k | } |
75 | | |
76 | | private: |
77 | | |
78 | | OID_Map() |
79 | 12 | { |
80 | 12 | m_str2oid = OIDS::load_str2oid_map(); |
81 | 12 | m_oid2str = OIDS::load_oid2str_map(); |
82 | 12 | } |
83 | | |
84 | | mutex_type m_mutex; |
85 | | std::unordered_map<std::string, OID> m_str2oid; |
86 | | std::unordered_map<std::string, std::string> m_oid2str; |
87 | | }; |
88 | | |
89 | | } |
90 | | |
91 | | void OIDS::add_oid(const OID& oid, const std::string& name) |
92 | 0 | { |
93 | 0 | OID_Map::global_registry().add_oid(oid, name); |
94 | 0 | } |
95 | | |
96 | | void OIDS::add_oidstr(const char* oidstr, const char* name) |
97 | 0 | { |
98 | 0 | add_oid(OID(oidstr), name); |
99 | 0 | } |
100 | | |
101 | | void OIDS::add_oid2str(const OID& oid, const std::string& name) |
102 | 0 | { |
103 | 0 | OID_Map::global_registry().add_oid2str(oid, name); |
104 | 0 | } |
105 | | |
106 | | void OIDS::add_str2oid(const OID& oid, const std::string& name) |
107 | 0 | { |
108 | 0 | OID_Map::global_registry().add_str2oid(oid, name); |
109 | 0 | } |
110 | | |
111 | | std::string OIDS::oid2str_or_empty(const OID& oid) |
112 | 119k | { |
113 | 119k | return OID_Map::global_registry().oid2str(oid); |
114 | 119k | } |
115 | | |
116 | | OID OIDS::str2oid_or_empty(const std::string& name) |
117 | 19.9k | { |
118 | 19.9k | return OID_Map::global_registry().str2oid(name); |
119 | 19.9k | } |
120 | | |
121 | | std::string OIDS::oid2str_or_throw(const OID& oid) |
122 | 0 | { |
123 | 0 | const std::string s = OIDS::oid2str_or_empty(oid); |
124 | 0 | if(s.empty()) |
125 | 0 | throw Lookup_Error("No name associated with OID " + oid.to_string()); |
126 | 0 | return s; |
127 | 0 | } |
128 | | |
129 | | bool OIDS::have_oid(const std::string& name) |
130 | 0 | { |
131 | 0 | return OID_Map::global_registry().have_oid(name); |
132 | 0 | } |
133 | | |
134 | | } |