Coverage Report

Created: 2021-02-21 07:20

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