diff options
author | lloyd <[email protected]> | 2008-09-30 04:36:37 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-09-30 04:36:37 +0000 |
commit | 5bcc0bbbff8916dcaddc7162fbc0ec7fc3ecaeea (patch) | |
tree | 1e5593ed04bf2729debff0653e674546fcb62afa /src/utils | |
parent | 3c55f159b8e2ff80c0f0ab0820de0afc414be7db (diff) |
Move datastore from core to utils
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/datastor.cpp | 169 | ||||
-rw-r--r-- | src/utils/datastor.h | 59 | ||||
-rw-r--r-- | src/utils/info.txt | 16 |
3 files changed, 237 insertions, 7 deletions
diff --git a/src/utils/datastor.cpp b/src/utils/datastor.cpp new file mode 100644 index 000000000..bff6bcf5b --- /dev/null +++ b/src/utils/datastor.cpp @@ -0,0 +1,169 @@ +/************************************************* +* Data Store Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/datastor.h> +#include <botan/filters.h> +#include <botan/parsing.h> +#include <botan/stl_util.h> + +namespace Botan { + +/************************************************* +* Default Matcher transform operation (identity) * +*************************************************/ +std::pair<std::string, std::string> +Data_Store::Matcher::transform(const std::string& key, + const std::string& value) const + { + return std::make_pair(key, value); + } + +/************************************************* +* Data_Store Equality Comparison * +*************************************************/ +bool Data_Store::operator==(const Data_Store& other) const + { + return (contents == other.contents); + } + +/************************************************* +* Check if this key has at least one value * +*************************************************/ +bool Data_Store::has_value(const std::string& key) const + { + return (contents.lower_bound(key) != contents.end()); + } + +/************************************************* +* Search based on an arbitrary predicate * +*************************************************/ +std::multimap<std::string, std::string> +Data_Store::search_with(const Matcher& matcher) const + { + std::multimap<std::string, std::string> out; + + std::multimap<std::string, std::string>::const_iterator i = + contents.begin(); + + while(i != contents.end()) + { + if(matcher(i->first, i->second)) + out.insert(matcher.transform(i->first, i->second)); + ++i; + } + + return out; + } + +/************************************************* +* Search based on key equality * +*************************************************/ +std::vector<std::string> Data_Store::get(const std::string& looking_for) const + { + typedef std::multimap<std::string, std::string>::const_iterator iter; + + std::pair<iter, iter> range = contents.equal_range(looking_for); + + std::vector<std::string> out; + for(iter i = range.first; i != range.second; ++i) + out.push_back(i->second); + return out; + } + +/************************************************* +* Get a single atom * +*************************************************/ +std::string Data_Store::get1(const std::string& key) const + { + std::vector<std::string> vals = get(key); + + if(vals.empty()) + throw Invalid_State("Data_Store::get1: Not values for " + key); + if(vals.size() > 1) + throw Invalid_State("Data_Store::get1: More than one value for " + key); + + return vals[0]; + } + +/************************************************* +* Get a single MemoryVector atom * +*************************************************/ +MemoryVector<byte> +Data_Store::get1_memvec(const std::string& key) const + { + std::vector<std::string> vals = get(key); + + if(vals.size() > 1) + throw Invalid_State("Data_Store::get1_memvec: Multiple values for " + + key); + + if(vals.empty()) + return MemoryVector<byte>(); + + Pipe pipe(new Hex_Decoder(FULL_CHECK)); + pipe.start_msg(); + if(vals.size()) + pipe.write(vals[0]); + pipe.end_msg(); + return pipe.read_all(); + } + +/************************************************* +* Get a single u32bit atom * +*************************************************/ +u32bit Data_Store::get1_u32bit(const std::string& key, + u32bit default_val) const + { + std::vector<std::string> vals = get(key); + + if(vals.empty()) + return default_val; + else if(vals.size() > 1) + throw Invalid_State("Data_Store::get1_u32bit: Multiple values for " + + key); + + return to_u32bit(vals[0]); + } + +/************************************************* +* Insert a single key and value * +*************************************************/ +void Data_Store::add(const std::string& key, const std::string& val) + { + multimap_insert(contents, key, val); + } + +/************************************************* +* Insert a single key and value * +*************************************************/ +void Data_Store::add(const std::string& key, u32bit val) + { + add(key, to_string(val)); + } + +/************************************************* +* Insert a single key and value * +*************************************************/ +void Data_Store::add(const std::string& key, const MemoryRegion<byte>& val) + { + Pipe pipe(new Hex_Encoder); + pipe.process_msg(val); + add(key, pipe.read_all_as_string()); + } + +/************************************************* +* Insert a mapping of key/value pairs * +*************************************************/ +void Data_Store::add(const std::multimap<std::string, std::string>& in) + { + std::multimap<std::string, std::string>::const_iterator i = in.begin(); + while(i != in.end()) + { + contents.insert(*i); + ++i; + } + } + +} diff --git a/src/utils/datastor.h b/src/utils/datastor.h new file mode 100644 index 000000000..0f6029189 --- /dev/null +++ b/src/utils/datastor.h @@ -0,0 +1,59 @@ +/************************************************* +* Data Store Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_DATA_STORE_H__ +#define BOTAN_DATA_STORE_H__ + +#include <botan/secmem.h> +#include <utility> +#include <string> +#include <vector> +#include <map> + +namespace Botan { + +/************************************************* +* Data Store * +*************************************************/ +class BOTAN_DLL Data_Store + { + public: + class BOTAN_DLL Matcher + { + public: + virtual bool operator()(const std::string&, + const std::string&) const = 0; + + virtual std::pair<std::string, std::string> + transform(const std::string&, const std::string&) const; + + virtual ~Matcher() {} + }; + + bool operator==(const Data_Store&) const; + + std::multimap<std::string, std::string> + search_with(const Matcher&) const; + + std::vector<std::string> get(const std::string&) const; + + std::string get1(const std::string&) const; + + MemoryVector<byte> get1_memvec(const std::string&) const; + u32bit get1_u32bit(const std::string&, u32bit = 0) const; + + bool has_value(const std::string&) const; + + void add(const std::multimap<std::string, std::string>&); + void add(const std::string&, const std::string&); + void add(const std::string&, u32bit); + void add(const std::string&, const MemoryRegion<byte>&); + private: + std::multimap<std::string, std::string> contents; + }; + +} + +#endif diff --git a/src/utils/info.txt b/src/utils/info.txt index 820c3069f..a1c87ef44 100644 --- a/src/utils/info.txt +++ b/src/utils/info.txt @@ -9,23 +9,25 @@ tru64 -> rt </libs> <add> -charset.cpp -mlock.cpp -parsing.cpp -util.cpp -version.cpp bit_ops.h bswap.h +charset.cpp charset.h +datastor.cpp +datastor.h loadstor.h mem_ops.h +mlock.cpp +parsing.cpp parsing.h rotate.h stl_util.h types.h +ui.cpp +ui.h +util.cpp util.h +version.cpp version.h xor_buf.h -ui.h -ui.cpp </add> |