summaryrefslogtreecommitdiffabout
path: root/src/eyetil.h
Unidiff
Diffstat (limited to 'src/eyetil.h') (more/less context) (ignore whitespace changes)
-rw-r--r--src/eyetil.h37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/eyetil.h b/src/eyetil.h
index d946e71..eff2c43 100644
--- a/src/eyetil.h
+++ b/src/eyetil.h
@@ -1,35 +1,72 @@
1#ifndef __EYETIL_H 1#ifndef __EYETIL_H
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); }
14 binary_t(const void *d,size_t s) { from_data(d,s); } 15 binary_t(const void *d,size_t s) { from_data(d,s); }
15 16
16 binary_t& from_hex(const std::string& h); 17 binary_t& from_hex(const std::string& h);
17 binary_t& from_data(const void *d,size_t s); 18 binary_t& from_data(const void *d,size_t s);
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();
30 67
31 std::string get_file(const std::string& f); 68 std::string get_file(const std::string& f);
32}; 69};
33 70
34class tarchive_t { 71class tarchive_t {
35 public: 72 public: