monotone

monotone Mtn Source Tree

Root/botan/lookup.cpp

1/*************************************************
2* Algorithm Lookup Table Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/lookup.h>
7#include <botan/look_add.h>
8#include <botan/algolist.h>
9#include <botan/mutex.h>
10#include <map>
11
12namespace Botan {
13
14namespace {
15
16/*************************************************
17* Name to algorithm mapping tables *
18*************************************************/
19std::map<std::string, S2K*> s2k_map;
20std::map<std::string, BlockCipherModePaddingMethod*> bc_pad_map;
21
22/*************************************************
23* Alias to canonical name mapping table *
24*************************************************/
25std::map<std::string, std::string> alias_map;
26
27/*************************************************
28* Mutexes controlling access to the tables *
29*************************************************/
30Mutex* s2k_map_lock = 0;
31Mutex* bc_pad_map_lock = 0;
32Mutex* alias_map_lock = 0;
33
34}
35
36/*************************************************
37* Retrieve a S2K algorithm *
38*************************************************/
39const S2K* retrieve_s2k(const std::string& name)
40 {
41 S2K* retval = 0;
42 s2k_map_lock->lock();
43 std::map<std::string, S2K*>::const_iterator algo;
44 algo = s2k_map.find(deref_alias(name));
45 if(algo != s2k_map.end())
46 retval = algo->second;
47 s2k_map_lock->unlock();
48 if(!retval)
49 {
50 retval = Algolist::get_s2k(deref_alias(name));
51 add_algorithm(retval);
52 }
53 return retval;
54 }
55
56/*************************************************
57* Retrieve a block cipher padding method *
58*************************************************/
59const BlockCipherModePaddingMethod* retrieve_bc_pad(const std::string& name)
60 {
61 BlockCipherModePaddingMethod* retval = 0;
62 bc_pad_map_lock->lock();
63 std::map<std::string, BlockCipherModePaddingMethod*>::const_iterator algo;
64 algo = bc_pad_map.find(deref_alias(name));
65 if(algo != bc_pad_map.end())
66 retval = algo->second;
67 bc_pad_map_lock->unlock();
68 if(!retval)
69 {
70 retval = Algolist::get_bc_pad(deref_alias(name));
71 add_algorithm(retval);
72 }
73 return retval;
74 }
75
76/*************************************************
77* Add a S2K algorithm to the lookup table *
78*************************************************/
79void add_algorithm(S2K* algo)
80 {
81 if(!algo) return;
82 s2k_map_lock->lock();
83 if(s2k_map.find(algo->name()) != s2k_map.end())
84 delete s2k_map[algo->name()];
85 s2k_map[algo->name()] = algo;
86 s2k_map_lock->unlock();
87 }
88
89/*************************************************
90* Add a padding method to the lookup table *
91*************************************************/
92void add_algorithm(BlockCipherModePaddingMethod* algo)
93 {
94 if(!algo) return;
95 bc_pad_map_lock->lock();
96 if(bc_pad_map.find(algo->name()) != bc_pad_map.end())
97 delete bc_pad_map[algo->name()];
98 bc_pad_map[algo->name()] = algo;
99 bc_pad_map_lock->unlock();
100 }
101
102/*************************************************
103* Add an alias for an algorithm *
104*************************************************/
105void add_alias(const std::string& alias, const std::string& official_name)
106 {
107 if(alias == "" || official_name == "")
108 return;
109
110 Mutex_Holder lock(alias_map_lock);
111
112 if(alias_map.find(alias) != alias_map.end())
113 {
114 if(deref_alias(alias_map[alias]) != deref_alias(official_name))
115 throw Invalid_Argument("add_alias: The alias " + alias +
116 " already exists");
117 return;
118 }
119
120 alias_map[alias] = official_name;
121 }
122
123/*************************************************
124* Dereference an alias *
125*************************************************/
126std::string deref_alias(const std::string& name)
127 {
128 std::map<std::string, std::string>::const_iterator realname;
129 realname = alias_map.find(name);
130 if(realname == alias_map.end())
131 return name;
132 return deref_alias(realname->second);
133 }
134
135/*************************************************
136* Handle startup for the lookup tables *
137*************************************************/
138void init_lookup_tables()
139 {
140 s2k_map_lock = get_mutex();
141 bc_pad_map_lock = get_mutex();
142 alias_map_lock = get_mutex();
143 }
144
145/*************************************************
146* Destroy the lookup tables *
147*************************************************/
148void destroy_lookup_tables()
149 {
150 std::map<std::string, S2K*>::iterator s2k_iter;
151 for(s2k_iter = s2k_map.begin(); s2k_iter != s2k_map.end(); s2k_iter++)
152 delete s2k_iter->second;
153
154 std::map<std::string, BlockCipherModePaddingMethod*>::iterator pad_iter;
155 for(pad_iter = bc_pad_map.begin(); pad_iter != bc_pad_map.end(); pad_iter++)
156 delete pad_iter->second;
157
158 s2k_map.clear();
159 bc_pad_map.clear();
160 alias_map.clear();
161
162 delete s2k_map_lock;
163 delete bc_pad_map_lock;
164 delete alias_map_lock;
165
166 s2k_map_lock = 0;
167 bc_pad_map_lock = 0;
168 alias_map_lock = 0;
169 }
170
171}

Archive Download this file

Branches

Tags

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