/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 | 54.1k | { |
43 | 54.1k | const std::string oid_str = oid.to_string(); |
44 | | |
45 | 54.1k | lock_guard_type<mutex_type> lock(m_mutex); |
46 | | |
47 | 54.1k | auto i = m_oid2str.find(oid_str); |
48 | 54.1k | if(i != m_oid2str.end()) |
49 | 41.7k | return i->second; |
50 | | |
51 | 12.4k | return ""; |
52 | 54.1k | } |
53 | | |
54 | | OID str2oid(const std::string& str) |
55 | 22.9k | { |
56 | 22.9k | lock_guard_type<mutex_type> lock(m_mutex); |
57 | 22.9k | auto i = m_str2oid.find(str); |
58 | 22.9k | if(i != m_str2oid.end()) |
59 | 22.9k | return i->second; |
60 | | |
61 | 0 | return OID(); |
62 | 22.9k | } |
63 | | |
64 | | static OID_Map& global_registry() |
65 | 77.1k | { |
66 | 77.1k | static OID_Map g_map; |
67 | 77.1k | return g_map; |
68 | 77.1k | } |
69 | | |
70 | | private: |
71 | | |
72 | | OID_Map() |
73 | 11 | { |
74 | 11 | m_str2oid = OIDS::load_str2oid_map(); |
75 | 11 | m_oid2str = OIDS::load_oid2str_map(); |
76 | 11 | } |
77 | | |
78 | | mutex_type m_mutex; |
79 | | std::unordered_map<std::string, OID> m_str2oid; |
80 | | std::unordered_map<std::string, std::string> m_oid2str; |
81 | | }; |
82 | | |
83 | | } |
84 | | |
85 | | void OIDS::add_oid(const OID& oid, const std::string& name) |
86 | 0 | { |
87 | 0 | OID_Map::global_registry().add_oid(oid, name); |
88 | 0 | } |
89 | | |
90 | | void OIDS::add_oidstr(const char* oidstr, const char* name) |
91 | 0 | { |
92 | 0 | add_oid(OID(oidstr), name); |
93 | 0 | } |
94 | | |
95 | | void OIDS::add_oid2str(const OID& oid, const std::string& name) |
96 | 0 | { |
97 | 0 | OID_Map::global_registry().add_oid2str(oid, name); |
98 | 0 | } |
99 | | |
100 | | void OIDS::add_str2oid(const OID& oid, const std::string& name) |
101 | 0 | { |
102 | 0 | OID_Map::global_registry().add_str2oid(oid, name); |
103 | 0 | } |
104 | | |
105 | | std::string OIDS::oid2str_or_empty(const OID& oid) |
106 | 54.1k | { |
107 | 54.1k | return OID_Map::global_registry().oid2str(oid); |
108 | 54.1k | } |
109 | | |
110 | | OID OIDS::str2oid_or_empty(const std::string& name) |
111 | 22.9k | { |
112 | 22.9k | return OID_Map::global_registry().str2oid(name); |
113 | 22.9k | } |
114 | | |
115 | | std::string OIDS::oid2str_or_throw(const OID& oid) |
116 | 0 | { |
117 | 0 | std::string s = OIDS::oid2str_or_empty(oid); |
118 | 0 | if(s.empty()) |
119 | 0 | throw Lookup_Error("No name associated with OID " + oid.to_string()); |
120 | 0 | return s; |
121 | 0 | } |
122 | | |
123 | | } |