summaryrefslogtreecommitdiffabout
authorMichael Krelin <hacker@klever.net>2011-08-30 20:04:30 (UTC)
committer Michael Krelin <hacker@klever.net>2011-08-30 20:04:30 (UTC)
commit111044b60ba5f40e003d1ba1d4082794985aa3ad (patch) (unidiff)
treece7949dad0cded81f47f7f067b43d2c29a795067
parent09b6e86aaad70bc7488604b2e291d098a7de0eac (diff)
downloadiii-111044b60ba5f40e003d1ba1d4082794985aa3ad.zip
iii-111044b60ba5f40e003d1ba1d4082794985aa3ad.tar.gz
iii-111044b60ba5f40e003d1ba1d4082794985aa3ad.tar.bz2
introduce incremental md5 digester
Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--src/eyetil.cc14
-rw-r--r--src/eyetil.h37
2 files changed, 51 insertions, 0 deletions
diff --git a/src/eyetil.cc b/src/eyetil.cc
index 7669cb6..57ae607 100644
--- a/src/eyetil.cc
+++ b/src/eyetil.cc
@@ -72,2 +72,16 @@ binary_t binary_t::md5() const {
72 72
73void md5_digester::init() {
74 if(!MD5_Init(&ctx)) throw std::runtime_error("failed to MD5_Init()");
75}
76void md5_digester::update(const void *d,size_t l) {
77 if(!MD5_Update(&ctx,d,l)) throw std::runtime_error("failed to MD5_Update()");
78}
79binary_t md5_digester::final() {
80 binary_t rv(MD5_DIGEST_LENGTH);
81 if(!MD5_Final((unsigned char*)&(rv.front()), &ctx))
82 throw std::runtime_error("failed to MD5_Final()");
83 return rv;
84}
85
86
73static void make_path_for_template(const std::string& p,mode_t m) { 87static void make_path_for_template(const std::string& p,mode_t m) {
diff --git a/src/eyetil.h b/src/eyetil.h
index d946e71..eff2c43 100644
--- a/src/eyetil.h
+++ b/src/eyetil.h
@@ -7,2 +7,3 @@
7#include <archive_entry.h> 7#include <archive_entry.h>
8#include "openssl/md5.h"
8 9
@@ -23,2 +24,38 @@ class binary_t : public std::vector<unsigned char> {
23 24
25struct md5_digester {
26 MD5_CTX ctx;
27 md5_digester() { init(); }
28
29 void init();
30 void update(const void *d,size_t l);
31 binary_t final();
32
33 template<typename T>
34 void update(const T& x) { update(&x,sizeof(x)); }
35
36 template<typename T>
37 struct update_iterator : public std::iterator<std::output_iterator_tag,T,void,T*,T&> {
38 md5_digester *d;
39 update_iterator(md5_digester *d_) : d(d_) { }
40 update_iterator(const update_iterator& x) : d(x.d) { }
41
42 update_iterator& operator*() { return *this; }
43 update_iterator& operator++() { return *this; }
44 update_iterator& operator++(int) { return *this; }
45
46 update_iterator& operator=(const T& x) {
47 d->update(x); return *this;
48 }
49 };
50
51 template<typename T>
52 update_iterator<T> updater() {
53 return update_iterator<T>(this);
54 }
55
56};
57template<> inline void md5_digester::update<binary_t>(const binary_t& x) {
58 update((const unsigned char*)&(x.front()),x.size());
59}
60
24class tmpdir_t { 61class tmpdir_t {