Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

datastor.cpp
Go to the documentation of this file.
1 /*
2 * Data Store
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
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 * Data_Store Equality Comparison
18 */
19 bool Data_Store::operator==(const Data_Store& other) const
20 {
21 return (m_contents == other.m_contents);
22 }
23
24 /*
25 * Check if this key has at least one value
26 */
27 bool Data_Store::has_value(const std::string& key) const
28 {
29 return (m_contents.lower_bound(key) != m_contents.end());
30 }
31
32 /*
33 * Search based on an arbitrary predicate
34 */
35 std::multimap<std::string, std::string> Data_Store::search_for(
36 std::function<bool (std::string, std::string)> predicate) const
37 {
38 std::multimap<std::string, std::string> out;
39
40 for(auto i = m_contents.begin(); i != m_contents.end(); ++i)
41 if(predicate(i->first, i->second))
42 out.insert(std::make_pair(i->first, i->second));
43
44 return out;
45 }
46
47 /*
48 * Search based on key equality
49 */
50 std::vector<std::string> Data_Store::get(const std::string& looking_for) const
51 {
52 std::vector<std::string> out;
53 auto range = m_contents.equal_range(looking_for);
54 for(auto i = range.first; i != range.second; ++i)
55 out.push_back(i->second);
56 return out;
57 }
58
59 /*
60 * Get a single atom
61 */
62 std::string Data_Store::get1(const std::string& key) const
63 {
64 std::vector<std::string> vals = get(key);
65
66 if(vals.empty())
67 throw Invalid_State("Data_Store::get1: No values set for " + key);
68 if(vals.size() > 1)
69 throw Invalid_State("Data_Store::get1: More than one value for " + key);
70
71 return vals[0];
72 }
73
74 std::string Data_Store::get1(const std::string& key,
75 const std::string& default_value) const
76 {
77 std::vector<std::string> vals = get(key);
78
79 if(vals.size() > 1)
80 throw Invalid_State("Data_Store::get1: More than one value for " + key);
81
82 if(vals.empty())
83 return default_value;
84
85 return vals[0];
86 }
87
88 /*
89 * Get a single std::vector atom
90 */
91 std::vector<uint8_t>
92 Data_Store::get1_memvec(const std::string& key) const
93 {
94 std::vector<std::string> vals = get(key);
95
96 if(vals.empty())
97 return std::vector<uint8_t>();
98
99 if(vals.size() > 1)
100 throw Invalid_State("Data_Store::get1_memvec: Multiple values for " +
101 key);
102
103 return hex_decode(vals[0]);
104 }
105
106 /*
107 * Get a single uint32_t atom
108 */
109 uint32_t Data_Store::get1_uint32(const std::string& key,
110 uint32_t default_val) const
111 {
112 std::vector<std::string> vals = get(key);
113
114 if(vals.empty())
115 return default_val;
116 else if(vals.size() > 1)
117 throw Invalid_State("Data_Store::get1_uint32: Multiple values for " + key);
118
119 return to_u32bit(vals[0]);
120 }
121
122 /*
123 * Insert a single key and value
124 */
125 void Data_Store::add(const std::string& key, const std::string& val)
126 {
127 multimap_insert(m_contents, key, val);
128 }
129
130 /*
131 * Insert a single key and value
132 */
133 void Data_Store::add(const std::string& key, uint32_t val)
134 {
135 add(key, std::to_string(val));
136 }
137
138 /*
139 * Insert a single key and value
140 */
141 void Data_Store::add(const std::string& key, const secure_vector<uint8_t>& val)
142 {
143 add(key, hex_encode(val.data(), val.size()));
144 }
145
146 void Data_Store::add(const std::string& key, const std::vector<uint8_t>& val)
147 {
148 add(key, hex_encode(val.data(), val.size()));
149 }
150
151 /*
152 * Insert a mapping of key/value pairs
153 */
154 void Data_Store::add(const std::multimap<std::string, std::string>& in)
155 {
156 std::multimap<std::string, std::string>::const_iterator i = in.begin();
157 while(i != in.end())
158 {
159 m_contents.insert(*i);
160 ++i;
161 }
162 }
163
164 }
Botan::to_u32bit
uint32_t to_u32bit(const std::string &str)
Definition: parsing.cpp:18
Botan::Data_Store::get1_memvec
std::vector< uint8_t > get1_memvec(const std::string &) const
Definition: datastor.cpp:92
Botan::Data_Store::get1_uint32
uint32_t get1_uint32(const std::string &, uint32_t=0) const
Definition: datastor.cpp:109
Botan::Data_Store::search_for
std::multimap< std::string, std::string > search_for(std::function< bool(std::string, std::string)> predicate) const
Definition: datastor.cpp:35
Botan::Invalid_State
Definition: exceptn.h:61
Botan::ASN1::to_string
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
Botan::Data_Store::get1
std::string get1(const std::string &key) const
Definition: datastor.cpp:62
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan
Definition: alg_id.cpp:13
Botan::Data_Store
Definition: datastor.h:27
Botan::Data_Store::has_value
bool has_value(const std::string &) const
Definition: datastor.cpp:27
Botan::Data_Store::operator==
bool operator==(const Data_Store &) const
Definition: datastor.cpp:19
Botan::hex_encode
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14
Botan::hex_decode
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition: hex.cpp:49
Botan::multimap_insert
void multimap_insert(std::multimap< K, V > &multimap, const K &key, const V &value)
Definition: stl_util.h:76
Botan::Data_Store::get
std::vector< std::string > get(const std::string &) const
Definition: datastor.cpp:50
Botan::Data_Store::add
void add(const std::multimap< std::string, std::string > &)
Definition: datastor.cpp:154