File: oids.cpp

package info (click to toggle)
monotone 0.31-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 20,680 kB
  • ctags: 14,801
  • sloc: cpp: 87,711; ansic: 64,862; sh: 5,691; lisp: 954; perl: 783; makefile: 509; python: 265; sql: 98; sed: 16
file content (157 lines) | stat: -rw-r--r-- 4,448 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*************************************************
* OID Registry Source File                       *
* (C) 1999-2005 The Botan Project                *
*************************************************/

#include <botan/oids.h>
#include <botan/exceptn.h>
#include <botan/mutex.h>
#include <botan/init.h>
#include <map>

namespace Botan {

namespace {

/*************************************************
* OID<->String Mappings                          *
*************************************************/
class OID_Mapper
   {
   public:
      void add_oid(const OID&, const std::string&);

      bool have_oid(const std::string&);
      std::string lookup(const OID&);
      OID lookup(const std::string&);

      OID_Mapper() { oid_mutex = get_mutex(); }
      ~OID_Mapper() { delete oid_mutex; }
   private:
      std::map<OID, std::string> oid_to_str;
      std::map<std::string, OID> str_to_oid;
      Mutex* oid_mutex;
   };

/*************************************************
* Register an OID to string mapping              *
*************************************************/
void OID_Mapper::add_oid(const OID& oid, const std::string& name)
   {
   Mutex_Holder lock(oid_mutex);

   if(oid_to_str.find(oid) == oid_to_str.end())
      oid_to_str[oid] = name;
   if(str_to_oid.find(name) == str_to_oid.end())
      str_to_oid[name] = oid;
   }

/*************************************************
* Do an OID to string lookup                     *
*************************************************/
std::string OID_Mapper::lookup(const OID& oid)
   {
   Mutex_Holder lock(oid_mutex);

   std::map<OID, std::string>::const_iterator info = oid_to_str.find(oid);
   if(info == oid_to_str.end())
      return oid.as_string();
   return info->second;
   }

/*************************************************
* Do a string to OID lookup                      *
*************************************************/
OID OID_Mapper::lookup(const std::string& name)
   {
   Mutex_Holder lock(oid_mutex);

   std::map<std::string, OID>::const_iterator info = str_to_oid.find(name);
   if(info == str_to_oid.end())
      throw Lookup_Error("No known OID for " + name);
   return info->second;
   }

/*************************************************
* Check to see if an OID exists in the table     *
*************************************************/
bool OID_Mapper::have_oid(const std::string& name)
   {
   Mutex_Holder lock(oid_mutex);

   return (str_to_oid.find(name) != str_to_oid.end());
   }

/*************************************************
* Global OID map                                 *
*************************************************/
OID_Mapper* mapping = 0;

}

namespace Init {

/*************************************************
* Startup the OID mapping system                 *
*************************************************/
void startup_oids()
   {
   mapping = new OID_Mapper;
   }

/*************************************************
* Shutdown the OID mapping system                *
*************************************************/
void shutdown_oids()
   {
   delete mapping;
   mapping = 0;
   }

}

namespace OIDS {

/*************************************************
* Register an OID to string mapping              *
*************************************************/
void add_oid(const OID& oid, const std::string& name)
   {
   if(!mapping)
      throw Internal_Error("OIDS::add_oid: Mapping not initialized");
   mapping->add_oid(oid, name);
   }

/*************************************************
* Do an OID to string lookup                     *
*************************************************/
std::string lookup(const OID& oid)
   {
   if(!mapping)
      throw Internal_Error("OIDS::lookup: Mapping not initialized");
   return mapping->lookup(oid);
   }

/*************************************************
* Do a string to OID lookup                      *
*************************************************/
OID lookup(const std::string& name)
   {
   if(!mapping)
      throw Internal_Error("OIDS::lookup: Mapping not initialized");
   return mapping->lookup(name);
   }

/*************************************************
* Check to see if an OID exists in the table     *
*************************************************/
bool have_oid(const std::string& name)
   {
   if(!mapping)
      throw Internal_Error("OIDS::lookup: Mapping not initialized");
   return mapping->have_oid(name);
   }

}

}