Botan: src/utils/datastor/datastor.cpp Source File

Botan 1.10.16

datastor.cpp
Go to the documentation of this file.
1 /*
2 * Data Store
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/datastor.h>
9 #include <botan/exceptn.h>
10 #include <botan/parsing.h>
11 #include <botan/hex.h>
12 #include <botan/internal/stl_util.h>
13
14 namespace Botan {
15
16 /*
17 * Default Matcher transform operation (identity)
18 */
19 std::pair<std::string, std::string>
20 Data_Store::Matcher::transform(const std::string& key,
21 const std::string& value) const
22 {
23 return std::make_pair(key, value);
24 }
25
26 /*
27 * Data_Store Equality Comparison
28 */
29 bool Data_Store::operator==(const Data_Store& other) const
30 {
31 return (contents == other.contents);
32 }
33
34 /*
35 * Check if this key has at least one value
36 */
37 bool Data_Store::has_value(const std::string& key) const
38 {
39 return (contents.lower_bound(key) != contents.end());
40 }
41
42 /*
43 * Search based on an arbitrary predicate
44 */
45 std::multimap<std::string, std::string>
46 Data_Store::search_with(const Matcher& matcher) const
47 {
48 std::multimap<std::string, std::string> out;
49
50 std::multimap<std::string, std::string>::const_iterator i =
51 contents.begin();
52
53 while(i != contents.end())
54 {
55 if(matcher(i->first, i->second))
56 {
57 std::pair<std::string, std::string> p(
58 matcher.transform(i->first, i->second));
59
60 multimap_insert(out, p.first, p.second);
61 }
62
63 ++i;
64 }
65
66 return out;
67 }
68
69 /*
70 * Search based on key equality
71 */
72 std::vector<std::string> Data_Store::get(const std::string& looking_for) const
73 {
74 typedef std::multimap<std::string, std::string>::const_iterator iter;
75
76 std::pair<iter, iter> range = contents.equal_range(looking_for);
77
78 std::vector<std::string> out;
79 for(iter i = range.first; i != range.second; ++i)
80 out.push_back(i->second);
81 return out;
82 }
83
84 /*
85 * Get a single atom
86 */
87 std::string Data_Store::get1(const std::string& key) const
88 {
89 std::vector<std::string> vals = get(key);
90
91 if(vals.empty())
92 throw Invalid_State("Data_Store::get1: Not values for " + key);
93 if(vals.size() > 1)
94 throw Invalid_State("Data_Store::get1: More than one value for " + key);
95
96 return vals[0];
97 }
98
99 /*
100 * Get a single MemoryVector atom
101 */
102 MemoryVector<byte>
103 Data_Store::get1_memvec(const std::string& key) const
104 {
105 std::vector<std::string> vals = get(key);
106
107 if(vals.empty())
108 return MemoryVector<byte>();
109
110 if(vals.size() > 1)
111 throw Invalid_State("Data_Store::get1_memvec: Multiple values for " +
112 key);
113
114 return hex_decode(vals[0]);
115 }
116
117 /*
118 * Get a single u32bit atom
119 */
120 u32bit Data_Store::get1_u32bit(const std::string& key,
121 u32bit default_val) const
122 {
123 std::vector<std::string> vals = get(key);
124
125 if(vals.empty())
126 return default_val;
127 else if(vals.size() > 1)
128 throw Invalid_State("Data_Store::get1_u32bit: Multiple values for " +
129 key);
130
131 return to_u32bit(vals[0]);
132 }
133
134 /*
135 * Insert a single key and value
136 */
137 void Data_Store::add(const std::string& key, const std::string& val)
138 {
139 multimap_insert(contents, key, val);
140 }
141
142 /*
143 * Insert a single key and value
144 */
145 void Data_Store::add(const std::string& key, u32bit val)
146 {
147 add(key, to_string(val));
148 }
149
150 /*
151 * Insert a single key and value
152 */
153 void Data_Store::add(const std::string& key, const MemoryRegion<byte>& val)
154 {
155 add(key, hex_encode(&val[0], val.size()));
156 }
157
158 /*
159 * Insert a mapping of key/value pairs
160 */
161 void Data_Store::add(const std::multimap<std::string, std::string>& in)
162 {
163 std::multimap<std::string, std::string>::const_iterator i = in.begin();
164 while(i != in.end())
165 {
166 contents.insert(*i);
167 ++i;
168 }
169 }
170
171 }
Botan::Data_Store::operator==
bool operator==(const Data_Store &) const
Definition: datastor.cpp:29
Botan::hex_decode
size_t hex_decode(byte output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition: hex.cpp:55
Botan::Data_Store::get1_memvec
MemoryVector< byte > get1_memvec(const std::string &) const
Definition: datastor.cpp:103
Botan::Invalid_State
Definition: exceptn.h:25
Botan::Data_Store::has_value
bool has_value(const std::string &) const
Definition: datastor.cpp:37
Botan::Data_Store::get
std::vector< std::string > get(const std::string &) const
Definition: datastor.cpp:72
Botan::Data_Store::Matcher::transform
virtual std::pair< std::string, std::string > transform(const std::string &, const std::string &) const
Definition: datastor.cpp:20
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::MemoryVector< byte >
Botan::Data_Store::Matcher
Definition: datastor.h:28
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::Data_Store
Definition: datastor.h:22
Botan::Data_Store::search_with
std::multimap< std::string, std::string > search_with(const Matcher &) const
Definition: datastor.cpp:46
Botan::Data_Store::get1_u32bit
u32bit get1_u32bit(const std::string &, u32bit=0) const
Definition: datastor.cpp:120
Botan::to_string
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
Botan::multimap_insert
void multimap_insert(std::multimap< K, V > &multimap, const K &key, const V &value)
Definition: stl_util.h:76
Botan::to_u32bit
u32bit to_u32bit(const std::string &number)
Definition: parsing.cpp:18
Botan::u32bit
unsigned int u32bit
Definition: types.h:32
Botan::Data_Store::get1
std::string get1(const std::string &) const
Definition: datastor.cpp:87
Botan::Data_Store::add
void add(const std::multimap< std::string, std::string > &)
Definition: datastor.cpp:161
Botan::hex_encode
void hex_encode(char output[], const byte input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14