monotone

monotone Mtn Source Tree

Root/botan/oids.cpp

1/*************************************************
2* OID Registry Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/oids.h>
7#include <botan/exceptn.h>
8#include <botan/mutex.h>
9#include <botan/init.h>
10#include <map>
11
12namespace Botan {
13
14namespace {
15
16/*************************************************
17* OID<->String Mappings *
18*************************************************/
19class OID_Mapper
20 {
21 public:
22 void add_oid(const OID&, const std::string&);
23
24 bool have_oid(const std::string&);
25 std::string lookup(const OID&);
26 OID lookup(const std::string&);
27
28 OID_Mapper() { oid_mutex = get_mutex(); }
29 ~OID_Mapper() { delete oid_mutex; }
30 private:
31 std::map<OID, std::string> oid_to_str;
32 std::map<std::string, OID> str_to_oid;
33 Mutex* oid_mutex;
34 };
35
36/*************************************************
37* Register an OID to string mapping *
38*************************************************/
39void OID_Mapper::add_oid(const OID& oid, const std::string& name)
40 {
41 Mutex_Holder lock(oid_mutex);
42
43 if(oid_to_str.find(oid) == oid_to_str.end())
44 oid_to_str[oid] = name;
45 if(str_to_oid.find(name) == str_to_oid.end())
46 str_to_oid[name] = oid;
47 }
48
49/*************************************************
50* Do an OID to string lookup *
51*************************************************/
52std::string OID_Mapper::lookup(const OID& oid)
53 {
54 Mutex_Holder lock(oid_mutex);
55
56 std::map<OID, std::string>::const_iterator info = oid_to_str.find(oid);
57 if(info == oid_to_str.end())
58 return oid.as_string();
59 return info->second;
60 }
61
62/*************************************************
63* Do a string to OID lookup *
64*************************************************/
65OID OID_Mapper::lookup(const std::string& name)
66 {
67 Mutex_Holder lock(oid_mutex);
68
69 std::map<std::string, OID>::const_iterator info = str_to_oid.find(name);
70 if(info == str_to_oid.end())
71 throw Lookup_Error("No known OID for " + name);
72 return info->second;
73 }
74
75/*************************************************
76* Check to see if an OID exists in the table *
77*************************************************/
78bool OID_Mapper::have_oid(const std::string& name)
79 {
80 Mutex_Holder lock(oid_mutex);
81
82 return (str_to_oid.find(name) != str_to_oid.end());
83 }
84
85/*************************************************
86* Global OID map *
87*************************************************/
88OID_Mapper* mapping = 0;
89
90}
91
92namespace Init {
93
94/*************************************************
95* Startup the OID mapping system *
96*************************************************/
97void startup_oids()
98 {
99 mapping = new OID_Mapper;
100 }
101
102/*************************************************
103* Shutdown the OID mapping system *
104*************************************************/
105void shutdown_oids()
106 {
107 delete mapping;
108 mapping = 0;
109 }
110
111}
112
113namespace OIDS {
114
115/*************************************************
116* Register an OID to string mapping *
117*************************************************/
118void add_oid(const OID& oid, const std::string& name)
119 {
120 if(!mapping)
121 throw Internal_Error("OIDS::add_oid: Mapping not initialized");
122 mapping->add_oid(oid, name);
123 }
124
125/*************************************************
126* Do an OID to string lookup *
127*************************************************/
128std::string lookup(const OID& oid)
129 {
130 if(!mapping)
131 throw Internal_Error("OIDS::lookup: Mapping not initialized");
132 return mapping->lookup(oid);
133 }
134
135/*************************************************
136* Do a string to OID lookup *
137*************************************************/
138OID lookup(const std::string& name)
139 {
140 if(!mapping)
141 throw Internal_Error("OIDS::lookup: Mapping not initialized");
142 return mapping->lookup(name);
143 }
144
145/*************************************************
146* Check to see if an OID exists in the table *
147*************************************************/
148bool have_oid(const std::string& name)
149 {
150 if(!mapping)
151 throw Internal_Error("OIDS::lookup: Mapping not initialized");
152 return mapping->have_oid(name);
153 }
154
155}
156
157}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status