/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  | 0  |          { | 
43  | 0  |          const std::string oid_str = oid.to_string();  | 
44  |  | 
  | 
45  | 0  |          lock_guard_type<mutex_type> lock(m_mutex);  | 
46  |  | 
  | 
47  | 0  |          auto i = m_oid2str.find(oid_str);  | 
48  | 0  |          if(i != m_oid2str.end())  | 
49  | 0  |             return i->second;  | 
50  |  |  | 
51  | 0  |          return "";  | 
52  | 0  |          }  | 
53  |  |  | 
54  |  |       OID str2oid(const std::string& str)  | 
55  | 1.59k  |          { | 
56  | 1.59k  |          lock_guard_type<mutex_type> lock(m_mutex);  | 
57  | 1.59k  |          auto i = m_str2oid.find(str);  | 
58  | 1.59k  |          if(i != m_str2oid.end())  | 
59  | 1.59k  |             return i->second;  | 
60  |  |  | 
61  | 0  |          return OID();  | 
62  | 1.59k  |          }  | 
63  |  |  | 
64  |  |       static OID_Map& global_registry()  | 
65  | 1.59k  |          { | 
66  | 1.59k  |          static OID_Map g_map;  | 
67  | 1.59k  |          return g_map;  | 
68  | 1.59k  |          }  | 
69  |  |  | 
70  |  |    private:  | 
71  |  |  | 
72  |  |       OID_Map()  | 
73  | 1  |          { | 
74  | 1  |          m_str2oid = OIDS::load_str2oid_map();  | 
75  | 1  |          m_oid2str = OIDS::load_oid2str_map();  | 
76  | 1  |          }  | 
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  | 0  |    { | 
107  | 0  |    return OID_Map::global_registry().oid2str(oid);  | 
108  | 0  |    }  | 
109  |  |  | 
110  |  | OID OIDS::str2oid_or_empty(const std::string& name)  | 
111  | 1.59k  |    { | 
112  | 1.59k  |    return OID_Map::global_registry().str2oid(name);  | 
113  | 1.59k  |    }  | 
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  |  | }  |