Botan  2.7.0
Crypto and TLS for C++11
oids.cpp
Go to the documentation of this file.
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 OIDS {
14 
15 namespace {
16 
17 class OID_Map
18  {
19  public:
20  void add_oid(const OID& oid, const std::string& str)
21  {
22  add_str2oid(oid, str);
23  add_oid2str(oid, str);
24  }
25 
26  void add_str2oid(const OID& oid, const std::string& str)
27  {
28  lock_guard_type<mutex_type> lock(m_mutex);
29  auto i = m_str2oid.find(str);
30  if(i == m_str2oid.end())
31  m_str2oid.insert(std::make_pair(str, oid.as_string()));
32  }
33 
34  void add_oid2str(const OID& oid, const std::string& str)
35  {
36  const std::string oid_str = oid.as_string();
37  lock_guard_type<mutex_type> lock(m_mutex);
38  auto i = m_oid2str.find(oid_str);
39  if(i == m_oid2str.end())
40  m_oid2str.insert(std::make_pair(oid_str, str));
41  }
42 
43  std::string lookup(const OID& oid)
44  {
45  const std::string oid_str = oid.as_string();
46 
47  lock_guard_type<mutex_type> lock(m_mutex);
48 
49  auto i = m_oid2str.find(oid_str);
50  if(i != m_oid2str.end())
51  return i->second;
52 
53  return "";
54  }
55 
56  OID lookup(const std::string& str)
57  {
58  lock_guard_type<mutex_type> lock(m_mutex);
59  auto i = m_str2oid.find(str);
60  if(i != m_str2oid.end())
61  return i->second;
62 
63  return OID();
64  }
65 
66  bool have_oid(const std::string& str)
67  {
68  lock_guard_type<mutex_type> lock(m_mutex);
69  return m_str2oid.find(str) != m_str2oid.end();
70  }
71 
72  static OID_Map& global_registry()
73  {
74  static OID_Map g_map;
75  return g_map;
76  }
77 
78  private:
79 
80  OID_Map()
81  {
82  m_str2oid = load_str2oid_map();
83  m_oid2str = load_oid2str_map();
84  }
85 
86  mutex_type m_mutex;
87  std::unordered_map<std::string, OID> m_str2oid;
88  std::unordered_map<std::string, std::string> m_oid2str;
89  };
90 
91 }
92 
93 void add_oid(const OID& oid, const std::string& name)
94  {
95  OID_Map::global_registry().add_oid(oid, name);
96  }
97 
98 void add_oidstr(const char* oidstr, const char* name)
99  {
100  add_oid(OID(oidstr), name);
101  }
102 
103 void add_oid2str(const OID& oid, const std::string& name)
104  {
105  OID_Map::global_registry().add_oid2str(oid, name);
106  }
107 
108 void add_str2oid(const OID& oid, const std::string& name)
109  {
110  OID_Map::global_registry().add_str2oid(oid, name);
111  }
112 
113 std::string lookup(const OID& oid)
114  {
115  return OID_Map::global_registry().lookup(oid);
116  }
117 
118 OID lookup(const std::string& name)
119  {
120  return OID_Map::global_registry().lookup(name);
121  }
122 
123 bool have_oid(const std::string& name)
124  {
125  return OID_Map::global_registry().have_oid(name);
126  }
127 
128 bool name_of(const OID& oid, const std::string& name)
129  {
130  return (oid == lookup(name));
131  }
132 
133 }
134 
135 }
std::unordered_map< std::string, std::string > load_oid2str_map()
Definition: oid_maps.cpp:17
std::string as_string() const
Definition: asn1_oid.h:48
std::unordered_map< std::string, OID > load_str2oid_map()
Definition: oid_maps.cpp:233
void add_oid(const OID &oid, const std::string &name)
Definition: oids.cpp:93
void add_str2oid(const OID &oid, const std::string &name)
Definition: oids.cpp:108
bool have_oid(const std::string &name)
Definition: oids.cpp:123
void add_oidstr(const char *oidstr, const char *name)
Definition: oids.cpp:98
Definition: alg_id.cpp:13
bool name_of(const OID &oid, const std::string &name)
Definition: oids.cpp:128
void add_oid2str(const OID &oid, const std::string &name)
Definition: oids.cpp:103
std::string lookup(const OID &oid)
Definition: oids.cpp:113