-rw-r--r-- | src/eyetil.h | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/eyetil.h b/src/eyetil.h index 7517ba6..8784cb4 100644 --- a/src/eyetil.h +++ b/src/eyetil.h | |||
@@ -1,105 +1,116 @@ | |||
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 | #include "openssl/md5.h" |
9 | 9 | ||
10 | struct throwable_exit { | 10 | struct throwable_exit { |
11 | int rc; | 11 | int rc; |
12 | throwable_exit(int rc_) : rc(rc_) { } | 12 | throwable_exit(int rc_) : rc(rc_) { } |
13 | }; | 13 | }; |
14 | 14 | ||
15 | class binary_t : public std::vector<unsigned char> { | 15 | class binary_t : public std::vector<unsigned char> { |
16 | public: | 16 | public: |
17 | binary_t() { } | 17 | binary_t() { } |
18 | binary_t(size_type n) : std::vector<unsigned char>(n) { } | 18 | binary_t(size_type n) : std::vector<unsigned char>(n) { } |
19 | binary_t(const std::string& h) { from_hex(h); } | 19 | binary_t(const std::string& h) { from_hex(h); } |
20 | binary_t(const void *d,size_t s) { from_data(d,s); } | 20 | binary_t(const void *d,size_t s) { from_data(d,s); } |
21 | 21 | ||
22 | binary_t& from_hex(const std::string& h); | 22 | binary_t& from_hex(const std::string& h); |
23 | binary_t& from_data(const void *d,size_t s); | 23 | binary_t& from_data(const void *d,size_t s); |
24 | binary_t& make_nonce(); | 24 | binary_t& make_nonce(); |
25 | 25 | ||
26 | std::string hex() const; | 26 | std::string hex() const; |
27 | binary_t md5() const; | 27 | binary_t md5() const; |
28 | }; | 28 | }; |
29 | 29 | ||
30 | struct md5_digester { | 30 | struct md5_digester { |
31 | MD5_CTX ctx; | 31 | MD5_CTX ctx; |
32 | md5_digester() { init(); } | 32 | md5_digester() { init(); } |
33 | 33 | ||
34 | void init(); | 34 | void init(); |
35 | void update(const void *d,size_t l); | 35 | void update(const void *d,size_t l); |
36 | binary_t final(); | 36 | binary_t final(); |
37 | 37 | ||
38 | template<typename T> | 38 | template<typename T> |
39 | void update(const T& x) { update(&x,sizeof(x)); } | 39 | void update(const T& x) { update(&x,sizeof(x)); } |
40 | 40 | ||
41 | template<typename T> | 41 | template<typename T> |
42 | struct update_iterator : public std::iterator<std::output_iterator_tag,T,void,T*,T&> { | 42 | struct update_iterator : public std::iterator<std::output_iterator_tag,T,void,T*,T&> { |
43 | md5_digester *d; | 43 | md5_digester *d; |
44 | update_iterator(md5_digester *d_) : d(d_) { } | 44 | update_iterator(md5_digester *d_) : d(d_) { } |
45 | update_iterator(const update_iterator& x) : d(x.d) { } | 45 | update_iterator(const update_iterator& x) : d(x.d) { } |
46 | 46 | ||
47 | update_iterator& operator*() { return *this; } | 47 | update_iterator& operator*() { return *this; } |
48 | update_iterator& operator++() { return *this; } | 48 | update_iterator& operator++() { return *this; } |
49 | update_iterator& operator++(int) { return *this; } | 49 | update_iterator& operator++(int) { return *this; } |
50 | 50 | ||
51 | update_iterator& operator=(const T& x) { | 51 | update_iterator& operator=(const T& x) { |
52 | d->update(x); return *this; | 52 | d->update(x); return *this; |
53 | } | 53 | } |
54 | }; | 54 | }; |
55 | 55 | ||
56 | template<typename T> | 56 | template<typename T> |
57 | update_iterator<T> updater() { | 57 | update_iterator<T> updater() { |
58 | return update_iterator<T>(this); | 58 | return update_iterator<T>(this); |
59 | } | 59 | } |
60 | 60 | ||
61 | }; | 61 | }; |
62 | template<> inline void md5_digester::update<binary_t>(const binary_t& x) { | 62 | template<> inline void md5_digester::update<binary_t>(const binary_t& x) { |
63 | update((const unsigned char*)&(x.front()),x.size()); | 63 | update((const unsigned char*)&(x.front()),x.size()); |
64 | } | 64 | } |
65 | 65 | ||
66 | #pragma pack(1) | 66 | #pragma pack(1) |
67 | struct block512_t { | 67 | struct block512_t { |
68 | enum { words = 512 / sizeof(uint16_t) }; | 68 | enum { words = 512 / sizeof(uint16_t) }; |
69 | uint16_t data[words]; | 69 | uint16_t data[words]; |
70 | 70 | ||
71 | inline uint8_t *dptr(size_t o) { return ((uint8_t*)this)+o; } | 71 | inline uint8_t *dptr(size_t o) { return ((uint8_t*)this)+o; } |
72 | 72 | ||
73 | static uint16_t tcpcksum(block512_t& data); | 73 | static uint16_t tcpcksum(block512_t& data); |
74 | }; | 74 | }; |
75 | #pragma pack() | 75 | #pragma pack() |
76 | 76 | ||
77 | struct integrity_digester { | ||
78 | md5_digester md5; | ||
79 | size_t data_size; | ||
80 | block512_t data; | ||
81 | |||
82 | integrity_digester() : data_size(0) { } | ||
83 | void update(const void *d,size_t s); | ||
84 | binary_t final(const std::string& ukey); | ||
85 | }; | ||
86 | |||
87 | |||
77 | class tmpdir_t { | 88 | class tmpdir_t { |
78 | public: | 89 | public: |
79 | std::string dir; | 90 | std::string dir; |
80 | 91 | ||
81 | tmpdir_t(const std::string& dt); | 92 | tmpdir_t(const std::string& dt); |
82 | ~tmpdir_t(); | 93 | ~tmpdir_t(); |
83 | 94 | ||
84 | std::string get_file(const std::string& f); | 95 | std::string get_file(const std::string& f); |
85 | }; | 96 | }; |
86 | 97 | ||
87 | class tarchive_t { | 98 | class tarchive_t { |
88 | public: | 99 | public: |
89 | struct archive *a; | 100 | struct archive *a; |
90 | struct archive_entry *e; | 101 | struct archive_entry *e; |
91 | 102 | ||
92 | tarchive_t(void *p,size_t s); | 103 | tarchive_t(void *p,size_t s); |
93 | ~tarchive_t(); | 104 | ~tarchive_t(); |
94 | 105 | ||
95 | bool read_next_header(); | 106 | bool read_next_header(); |
96 | 107 | ||
97 | std::string entry_pathname(); | 108 | std::string entry_pathname(); |
98 | 109 | ||
99 | bool read_data_into_fd(int fd); | 110 | bool read_data_into_fd(int fd); |
100 | }; | 111 | }; |
101 | 112 | ||
102 | binary_t integrity_digest(const void *ptr,size_t size, | 113 | binary_t integrity_digest(const void *ptr,size_t size, |
103 | const std::string& ukey); | 114 | const std::string& ukey); |
104 | 115 | ||
105 | #endif /* __EYETIL_H */ | 116 | #endif /* __EYETIL_H */ |