aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar David Leverton <levertond@googlemail.com> 2011-10-06 22:26:59 +0100
committerAvatar David Leverton <levertond@googlemail.com> 2011-10-14 23:04:28 +0100
commit2ff049ddef190325fac1a1933c71a5884a64478f (patch)
treef32afec227387039801dc275299a2efa4f04e4dd
parent1053487ae4134de9e3067f2e17a453332b4aa49c (diff)
downloadpaludis-2ff049ddef190325fac1a1933c71a5884a64478f.tar.gz
paludis-2ff049ddef190325fac1a1933c71a5884a64478f.tar.xz
Add DigestRegistry
This allows a digest function to be computed given its name, rather than hard-coding the specific digest class.
-rw-r--r--paludis/util/digest_registry.cc70
-rw-r--r--paludis/util/digest_registry.hh73
-rw-r--r--paludis/util/files.m41
-rw-r--r--paludis/util/md5.cc6
-rw-r--r--paludis/util/rmd160.cc6
-rw-r--r--paludis/util/sha1.cc6
-rw-r--r--paludis/util/sha256.cc6
7 files changed, 168 insertions, 0 deletions
diff --git a/paludis/util/digest_registry.cc b/paludis/util/digest_registry.cc
new file mode 100644
index 0000000..ec7ab1e
--- /dev/null
+++ b/paludis/util/digest_registry.cc
@@ -0,0 +1,70 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2011 David Leverton
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <paludis/util/digest_registry.hh>
+#include <paludis/util/pimp-impl.hh>
+#include <paludis/util/singleton-impl.hh>
+#include <map>
+
+using namespace paludis;
+
+namespace
+{
+ typedef std::map<std::string, DigestRegistry::Function> FunctionMap;
+}
+
+namespace paludis
+{
+ template <>
+ struct Imp<DigestRegistry>
+ {
+ FunctionMap functions;
+ };
+}
+
+DigestRegistry::DigestRegistry()
+{
+}
+
+DigestRegistry::~DigestRegistry()
+{
+}
+
+DigestRegistry::Function
+DigestRegistry::get(const std::string & algo) const
+{
+ FunctionMap::const_iterator it(_imp->functions.find(algo));
+ if (_imp->functions.end() == it)
+ return Function();
+ return it->second;
+}
+
+void
+DigestRegistry::register_function(const std::string & algo, const Function & func)
+{
+ _imp->functions.insert(std::make_pair(algo, func));
+}
+
+namespace paludis
+{
+ template class Pimp<DigestRegistry>;
+ template class Singleton<DigestRegistry>;
+}
+
+
diff --git a/paludis/util/digest_registry.hh b/paludis/util/digest_registry.hh
new file mode 100644
index 0000000..cfa86ce
--- /dev/null
+++ b/paludis/util/digest_registry.hh
@@ -0,0 +1,73 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2011 David Leverton
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef PALUDIS_GUARD_PALUDIS_UTIL_DIGEST_REGISTRY_HH
+#define PALUDIS_GUARD_PALUDIS_UTIL_DIGEST_REGISTRY_HH 1
+
+#include <iosfwd>
+#include <string>
+#include <paludis/util/attributes.hh>
+#include <paludis/util/pimp.hh>
+#include <paludis/util/singleton.hh>
+#include <functional>
+
+namespace paludis
+{
+ class PALUDIS_VISIBLE DigestRegistry :
+ public Singleton<DigestRegistry>
+ {
+ friend class Singleton<DigestRegistry>;
+
+ public:
+ typedef std::function<std::string (std::istream &)> Function;
+
+ Function get(const std::string & algo) const;
+
+ template <typename T_>
+ class Registration
+ {
+ public:
+ Registration(const std::string & algo)
+ {
+ get_instance()->register_function(algo, do_digest<T_>);
+ }
+ };
+
+ private:
+ DigestRegistry();
+ ~DigestRegistry();
+
+ Pimp<DigestRegistry> _imp;
+
+ void register_function(const std::string & algo, const Function & func);
+
+ template <typename T_>
+ static std::string
+ do_digest(std::istream & stream)
+ {
+ T_ digest(stream);
+ return digest.hexsum();
+ }
+ };
+
+ extern template class Pimp<DigestRegistry>;
+ extern template class Singleton<DigestRegistry>;
+}
+
+#endif
diff --git a/paludis/util/files.m4 b/paludis/util/files.m4
index 2e8efb4..219d7a4 100644
--- a/paludis/util/files.m4
+++ b/paludis/util/files.m4
@@ -23,6 +23,7 @@ add(`create_iterator', `hh', `fwd', `impl', `gtest')
add(`damerau_levenshtein', `hh', `cc', `gtest')
add(`destringify', `hh', `cc', `gtest')
add(`deferred_construction_ptr', `hh', `cc', `fwd', `gtest')
+add(`digest_registry', `hh', `cc')
add(`discard_output_stream', `hh', `cc')
add(`elf', `hh', `cc')
add(`elf_dynamic_section', `hh', `cc')
diff --git a/paludis/util/md5.cc b/paludis/util/md5.cc
index 114d4a1..727ee16 100644
--- a/paludis/util/md5.cc
+++ b/paludis/util/md5.cc
@@ -18,6 +18,7 @@
*/
#include <paludis/util/md5.hh>
+#include <paludis/util/digest_registry.hh>
#include <sstream>
#include <istream>
#include <iomanip>
@@ -237,3 +238,8 @@ const uint32_t MD5::_t[64] = {
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
};
+namespace
+{
+ DigestRegistry::Registration<MD5> registration("MD5");
+}
+
diff --git a/paludis/util/rmd160.cc b/paludis/util/rmd160.cc
index 024459b..394cf60 100644
--- a/paludis/util/rmd160.cc
+++ b/paludis/util/rmd160.cc
@@ -19,6 +19,7 @@
#include "rmd160.hh"
#include <paludis/util/attributes.hh>
+#include <paludis/util/digest_registry.hh>
#include <sstream>
#include <istream>
#include <iomanip>
@@ -229,3 +230,8 @@ const uint32_t RMD160::_kp[5] = {
0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9, 0x00000000
};
+namespace
+{
+ DigestRegistry::Registration<RMD160> registration("RMD160");
+}
+
diff --git a/paludis/util/sha1.cc b/paludis/util/sha1.cc
index fc2b54f..7092017 100644
--- a/paludis/util/sha1.cc
+++ b/paludis/util/sha1.cc
@@ -21,6 +21,7 @@
#include <paludis/util/sha1.hh>
#include <paludis/util/byte_swap.hh>
+#include <paludis/util/digest_registry.hh>
#include <sstream>
#include <istream>
#include <iomanip>
@@ -209,3 +210,8 @@ SHA1::hexsum() const
return result.str();
}
+namespace
+{
+ DigestRegistry::Registration<SHA1> registration("SHA1");
+}
+
diff --git a/paludis/util/sha256.cc b/paludis/util/sha256.cc
index d9900f5..b44b11f 100644
--- a/paludis/util/sha256.cc
+++ b/paludis/util/sha256.cc
@@ -19,6 +19,7 @@
#include "sha256.hh"
#include <paludis/util/attributes.hh>
+#include <paludis/util/digest_registry.hh>
#include <istream>
#include <iomanip>
#include <sstream>
@@ -226,3 +227,8 @@ paludis::SHA256::_k[64] = {
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
+namespace
+{
+ DigestRegistry::Registration<SHA256> registration("SHA256");
+}
+