-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 03b9ba8..7517ba6 100644 --- a/src/eyetil.h +++ b/src/eyetil.h | |||
@@ -1,94 +1,105 @@ | |||
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) | ||
67 | struct block512_t { | ||
68 | enum { words = 512 / sizeof(uint16_t) }; | ||
69 | uint16_t data[words]; | ||
70 | |||
71 | inline uint8_t *dptr(size_t o) { return ((uint8_t*)this)+o; } | ||
72 | |||
73 | static uint16_t tcpcksum(block512_t& data); | ||
74 | }; | ||
75 | #pragma pack() | ||
76 | |||
66 | class tmpdir_t { | 77 | class tmpdir_t { |
67 | public: | 78 | public: |
68 | std::string dir; | 79 | std::string dir; |
69 | 80 | ||
70 | tmpdir_t(const std::string& dt); | 81 | tmpdir_t(const std::string& dt); |
71 | ~tmpdir_t(); | 82 | ~tmpdir_t(); |
72 | 83 | ||
73 | std::string get_file(const std::string& f); | 84 | std::string get_file(const std::string& f); |
74 | }; | 85 | }; |
75 | 86 | ||
76 | class tarchive_t { | 87 | class tarchive_t { |
77 | public: | 88 | public: |
78 | struct archive *a; | 89 | struct archive *a; |
79 | struct archive_entry *e; | 90 | struct archive_entry *e; |
80 | 91 | ||
81 | tarchive_t(void *p,size_t s); | 92 | tarchive_t(void *p,size_t s); |
82 | ~tarchive_t(); | 93 | ~tarchive_t(); |
83 | 94 | ||
84 | bool read_next_header(); | 95 | bool read_next_header(); |
85 | 96 | ||
86 | std::string entry_pathname(); | 97 | std::string entry_pathname(); |
87 | 98 | ||
88 | bool read_data_into_fd(int fd); | 99 | bool read_data_into_fd(int fd); |
89 | }; | 100 | }; |
90 | 101 | ||
91 | binary_t integrity_digest(const void *ptr,size_t size, | 102 | binary_t integrity_digest(const void *ptr,size_t size, |
92 | const std::string& ukey); | 103 | const std::string& ukey); |
93 | 104 | ||
94 | #endif /* __EYETIL_H */ | 105 | #endif /* __EYETIL_H */ |