Coverage Report

Created: 2020-02-14 15:38

/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
120k
         {
43
120k
         const std::string oid_str = oid.to_string();
44
120k
45
120k
         lock_guard_type<mutex_type> lock(m_mutex);
46
120k
47
120k
         auto i = m_oid2str.find(oid_str);
48
120k
         if(i != m_oid2str.end())
49
102k
            return i->second;
50
17.5k
51
17.5k
         return "";
52
17.5k
         }
53
54
      OID str2oid(const std::string& str)
55
17.1k
         {
56
17.1k
         lock_guard_type<mutex_type> lock(m_mutex);
57
17.1k
         auto i = m_str2oid.find(str);
58
17.1k
         if(i != m_str2oid.end())
59
17.1k
            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
137k
         {
72
137k
         static OID_Map g_map;
73
137k
         return g_map;
74
137k
         }
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
120k
   {
113
120k
   return OID_Map::global_registry().oid2str(oid);
114
120k
   }
115
116
OID OIDS::str2oid_or_empty(const std::string& name)
117
17.1k
   {
118
17.1k
   return OID_Map::global_registry().str2oid(name);
119
17.1k
   }
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
}