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
@@ -67,12 +67,26 @@ binary_t binary_t::md5() const {
67 (const unsigned char*)&(front()),size(), 67 (const unsigned char*)&(front()),size(),
68 (unsigned char*)&(rv.front()) )) 68 (unsigned char*)&(rv.front()) ))
69 throw std::runtime_error("failed to md5()"); 69 throw std::runtime_error("failed to md5()");
70 return rv; 70 return rv;
71} 71}
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) {
74 struct stat st; 88 struct stat st;
75 std::string pp; 89 std::string pp;
76 for(std::string::size_type sl=p.find('/',1); 90 for(std::string::size_type sl=p.find('/',1);
77 sl!=std::string::npos; 91 sl!=std::string::npos;
78 sl=p.find('/',sl+1)) { 92 sl=p.find('/',sl+1)) {
diff --git a/src/eyetil.h b/src/eyetil.h
index d946e71..eff2c43 100644
--- a/src/eyetil.h
+++ b/src/eyetil.h
@@ -2,12 +2,13 @@
2#define __EYETIL_H 2#define __EYETIL_H
3 3
4#include <vector> 4#include <vector>
5#include <string> 5#include <string>
6#include <archive.h> 6#include <archive.h>
7#include <archive_entry.h> 7#include <archive_entry.h>
8#include "openssl/md5.h"
8 9
9class binary_t : public std::vector<unsigned char> { 10class binary_t : public std::vector<unsigned char> {
10 public: 11 public:
11 binary_t() { } 12 binary_t() { }
12 binary_t(size_type n) : std::vector<unsigned char>(n) { } 13 binary_t(size_type n) : std::vector<unsigned char>(n) { }
13 binary_t(const std::string& h) { from_hex(h); } 14 binary_t(const std::string& h) { from_hex(h); }
@@ -18,12 +19,48 @@ class binary_t : public std::vector<unsigned char> {
18 binary_t& make_nonce(); 19 binary_t& make_nonce();
19 20
20 std::string hex() const; 21 std::string hex() const;
21 binary_t md5() const; 22 binary_t md5() const;
22}; 23};
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 {
25 public: 62 public:
26 std::string dir; 63 std::string dir;
27 64
28 tmpdir_t(const std::string& dt); 65 tmpdir_t(const std::string& dt);
29 ~tmpdir_t(); 66 ~tmpdir_t();