Coverage Report

Created: 2020-11-21 08:34

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