Botan
1.11.15
|
00001 /* 00002 * Data Store 00003 * (C) 1999-2007 Jack Lloyd 00004 * 00005 * Botan is released under the Simplified BSD License (see license.txt) 00006 */ 00007 00008 #include <botan/datastor.h> 00009 #include <botan/exceptn.h> 00010 #include <botan/parsing.h> 00011 #include <botan/hex.h> 00012 #include <botan/internal/stl_util.h> 00013 00014 namespace Botan { 00015 00016 /* 00017 * Data_Store Equality Comparison 00018 */ 00019 bool Data_Store::operator==(const Data_Store& other) const 00020 { 00021 return (contents == other.contents); 00022 } 00023 00024 /* 00025 * Check if this key has at least one value 00026 */ 00027 bool Data_Store::has_value(const std::string& key) const 00028 { 00029 return (contents.lower_bound(key) != contents.end()); 00030 } 00031 00032 /* 00033 * Search based on an arbitrary predicate 00034 */ 00035 std::multimap<std::string, std::string> Data_Store::search_for( 00036 std::function<bool (std::string, std::string)> predicate) const 00037 { 00038 std::multimap<std::string, std::string> out; 00039 00040 for(auto i = contents.begin(); i != contents.end(); ++i) 00041 if(predicate(i->first, i->second)) 00042 out.insert(std::make_pair(i->first, i->second)); 00043 00044 return out; 00045 } 00046 00047 /* 00048 * Search based on key equality 00049 */ 00050 std::vector<std::string> Data_Store::get(const std::string& looking_for) const 00051 { 00052 std::vector<std::string> out; 00053 auto range = contents.equal_range(looking_for); 00054 for(auto i = range.first; i != range.second; ++i) 00055 out.push_back(i->second); 00056 return out; 00057 } 00058 00059 /* 00060 * Get a single atom 00061 */ 00062 std::string Data_Store::get1(const std::string& key) const 00063 { 00064 std::vector<std::string> vals = get(key); 00065 00066 if(vals.empty()) 00067 throw Invalid_State("Data_Store::get1: No values set for " + key); 00068 if(vals.size() > 1) 00069 throw Invalid_State("Data_Store::get1: More than one value for " + key); 00070 00071 return vals[0]; 00072 } 00073 00074 std::string Data_Store::get1(const std::string& key, 00075 const std::string& default_value) const 00076 { 00077 std::vector<std::string> vals = get(key); 00078 00079 if(vals.size() > 1) 00080 throw Invalid_State("Data_Store::get1: More than one value for " + key); 00081 00082 if(vals.empty()) 00083 return default_value; 00084 00085 return vals[0]; 00086 } 00087 00088 /* 00089 * Get a single std::vector atom 00090 */ 00091 std::vector<byte> 00092 Data_Store::get1_memvec(const std::string& key) const 00093 { 00094 std::vector<std::string> vals = get(key); 00095 00096 if(vals.empty()) 00097 return std::vector<byte>(); 00098 00099 if(vals.size() > 1) 00100 throw Invalid_State("Data_Store::get1_memvec: Multiple values for " + 00101 key); 00102 00103 return hex_decode(vals[0]); 00104 } 00105 00106 /* 00107 * Get a single u32bit atom 00108 */ 00109 u32bit Data_Store::get1_u32bit(const std::string& key, 00110 u32bit default_val) const 00111 { 00112 std::vector<std::string> vals = get(key); 00113 00114 if(vals.empty()) 00115 return default_val; 00116 else if(vals.size() > 1) 00117 throw Invalid_State("Data_Store::get1_u32bit: Multiple values for " + 00118 key); 00119 00120 return to_u32bit(vals[0]); 00121 } 00122 00123 /* 00124 * Insert a single key and value 00125 */ 00126 void Data_Store::add(const std::string& key, const std::string& val) 00127 { 00128 multimap_insert(contents, key, val); 00129 } 00130 00131 /* 00132 * Insert a single key and value 00133 */ 00134 void Data_Store::add(const std::string& key, u32bit val) 00135 { 00136 add(key, std::to_string(val)); 00137 } 00138 00139 /* 00140 * Insert a single key and value 00141 */ 00142 void Data_Store::add(const std::string& key, const secure_vector<byte>& val) 00143 { 00144 add(key, hex_encode(&val[0], val.size())); 00145 } 00146 00147 void Data_Store::add(const std::string& key, const std::vector<byte>& val) 00148 { 00149 add(key, hex_encode(&val[0], val.size())); 00150 } 00151 00152 /* 00153 * Insert a mapping of key/value pairs 00154 */ 00155 void Data_Store::add(const std::multimap<std::string, std::string>& in) 00156 { 00157 std::multimap<std::string, std::string>::const_iterator i = in.begin(); 00158 while(i != in.end()) 00159 { 00160 contents.insert(*i); 00161 ++i; 00162 } 00163 } 00164 00165 }