author | Michael Krelin <hacker@klever.net> | 2013-02-13 22:32:22 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2013-02-13 22:32:22 (UTC) |
commit | dd545573337e3c54ad4c2c64d72f750ad03aa2c9 (patch) (unidiff) | |
tree | edf4e4294fc20418f5590f2698b8ae9b8bb31c65 | |
parent | d6d845ab3cdbc76943d68329aa0aaa3ddf2af21a (diff) | |
download | iii-dd545573337e3c54ad4c2c64d72f750ad03aa2c9.zip iii-dd545573337e3c54ad4c2c64d72f750ad03aa2c9.tar.gz iii-dd545573337e3c54ad4c2c64d72f750ad03aa2c9.tar.bz2 |
introduced streaming integrity digest updater
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | src/eyetil.cc | 28 | ||||
-rw-r--r-- | src/eyetil.h | 11 |
2 files changed, 39 insertions, 0 deletions
diff --git a/src/eyetil.cc b/src/eyetil.cc index cd22eea..6ccc4ae 100644 --- a/src/eyetil.cc +++ b/src/eyetil.cc | |||
@@ -28,128 +28,156 @@ binary_t& binary_t::from_hex(const std::string& h) { | |||
28 | *(oi++) = static_cast<binary_t::value_type>(0xff&strtol(t,0,16)); | 28 | *(oi++) = static_cast<binary_t::value_type>(0xff&strtol(t,0,16)); |
29 | } | 29 | } |
30 | return *this; | 30 | return *this; |
31 | } | 31 | } |
32 | 32 | ||
33 | binary_t& binary_t::from_data(const void *d,size_t s) { | 33 | binary_t& binary_t::from_data(const void *d,size_t s) { |
34 | resize(s); | 34 | resize(s); |
35 | std::copy((const unsigned char*)d,(const unsigned char *)d+s, | 35 | std::copy((const unsigned char*)d,(const unsigned char *)d+s, |
36 | begin() ); | 36 | begin() ); |
37 | return *this; | 37 | return *this; |
38 | } | 38 | } |
39 | 39 | ||
40 | binary_t& binary_t::make_nonce() { | 40 | binary_t& binary_t::make_nonce() { |
41 | #ifdef HAVE_LIBUUID | 41 | #ifdef HAVE_LIBUUID |
42 | uuid_t uuid; | 42 | uuid_t uuid; |
43 | uuid_generate(uuid); | 43 | uuid_generate(uuid); |
44 | from_data((unsigned char*)uuid,sizeof(uuid)); | 44 | from_data((unsigned char*)uuid,sizeof(uuid)); |
45 | #else | 45 | #else |
46 | resize(16); | 46 | resize(16); |
47 | std::generate_n(begin(),16,rand); | 47 | std::generate_n(begin(),16,rand); |
48 | #endif /* HAVE_LIBUUID */ | 48 | #endif /* HAVE_LIBUUID */ |
49 | return *this; | 49 | return *this; |
50 | } | 50 | } |
51 | 51 | ||
52 | std::string binary_t::hex() const { | 52 | std::string binary_t::hex() const { |
53 | std::string rv; | 53 | std::string rv; |
54 | rv.reserve((size()<<1)+1); | 54 | rv.reserve((size()<<1)+1); |
55 | char t[3] = {0,0,0}; | 55 | char t[3] = {0,0,0}; |
56 | for(const_iterator i=begin(),ie=end();i!=ie;++i) { | 56 | for(const_iterator i=begin(),ie=end();i!=ie;++i) { |
57 | size_t rc = snprintf(t,sizeof(t),"%02x",*i); | 57 | size_t rc = snprintf(t,sizeof(t),"%02x",*i); |
58 | assert(rc<sizeof(t)); | 58 | assert(rc<sizeof(t)); |
59 | rv += t; | 59 | rv += t; |
60 | } | 60 | } |
61 | return rv; | 61 | return rv; |
62 | } | 62 | } |
63 | 63 | ||
64 | binary_t binary_t::md5() const { | 64 | binary_t binary_t::md5() const { |
65 | binary_t rv(MD5_DIGEST_LENGTH); | 65 | binary_t rv(MD5_DIGEST_LENGTH); |
66 | if(!MD5( | 66 | if(!MD5( |
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 | ||
73 | void md5_digester::init() { | 73 | void md5_digester::init() { |
74 | if(!MD5_Init(&ctx)) throw std::runtime_error("failed to MD5_Init()"); | 74 | if(!MD5_Init(&ctx)) throw std::runtime_error("failed to MD5_Init()"); |
75 | } | 75 | } |
76 | void md5_digester::update(const void *d,size_t l) { | 76 | void md5_digester::update(const void *d,size_t l) { |
77 | if(!MD5_Update(&ctx,d,l)) throw std::runtime_error("failed to MD5_Update()"); | 77 | if(!MD5_Update(&ctx,d,l)) throw std::runtime_error("failed to MD5_Update()"); |
78 | } | 78 | } |
79 | binary_t md5_digester::final() { | 79 | binary_t md5_digester::final() { |
80 | binary_t rv(MD5_DIGEST_LENGTH); | 80 | binary_t rv(MD5_DIGEST_LENGTH); |
81 | if(!MD5_Final((unsigned char*)&(rv.front()), &ctx)) | 81 | if(!MD5_Final((unsigned char*)&(rv.front()), &ctx)) |
82 | throw std::runtime_error("failed to MD5_Final()"); | 82 | throw std::runtime_error("failed to MD5_Final()"); |
83 | return rv; | 83 | return rv; |
84 | } | 84 | } |
85 | 85 | ||
86 | uint16_t block512_t::tcpcksum(block512_t& data) { | 86 | uint16_t block512_t::tcpcksum(block512_t& data) { |
87 | uint32_t sum = std::accumulate(data.data,data.data+words,0); | 87 | uint32_t sum = std::accumulate(data.data,data.data+words,0); |
88 | while(uint32_t hw = sum>>16) sum = (sum&0xffff)+hw; | 88 | while(uint32_t hw = sum>>16) sum = (sum&0xffff)+hw; |
89 | return 0xffff&~sum; | 89 | return 0xffff&~sum; |
90 | } | 90 | } |
91 | 91 | ||
92 | void integrity_digester::update(const void *d_,size_t s) { | ||
93 | uint8_t *d=(uint8_t*)d_; | ||
94 | if(data_size) { | ||
95 | int l = sizeof(data)-data_size; | ||
96 | if(l>s) { | ||
97 | memmove(data.dptr(data_size),d,s); data_size+=s; return; | ||
98 | } | ||
99 | memmove(data.dptr(data_size),d,l); d+=l; s-=l; | ||
100 | md5.update<uint16_t>( data.tcpcksum(data) ); | ||
101 | } | ||
102 | if(s<sizeof(data)) { | ||
103 | memmove(data.dptr(0),d,s); data_size=s; return; | ||
104 | } | ||
105 | size_t bb=s/sizeof(block512_t); | ||
106 | std::transform((block512_t*)d,((block512_t*)d)+bb, | ||
107 | md5.updater<uint16_t>(),block512_t::tcpcksum); | ||
108 | size_t ss=bb*sizeof(block512_t); | ||
109 | d+=ss; s-=ss; | ||
110 | assert(s<sizeof(block512_t)); | ||
111 | if(s) memmove(data.dptr(0),d,data_size=s); | ||
112 | } | ||
113 | |||
114 | binary_t integrity_digester::final(const std::string& ukey) { | ||
115 | assert(!data_size); | ||
116 | md5.update( binary_t(ukey) ); | ||
117 | return md5.final(); | ||
118 | } | ||
119 | |||
92 | static void make_path_for_template(const std::string& p,mode_t m) { | 120 | static void make_path_for_template(const std::string& p,mode_t m) { |
93 | struct stat st; | 121 | struct stat st; |
94 | std::string pp; | 122 | std::string pp; |
95 | for(std::string::size_type sl=p.find('/',1); | 123 | for(std::string::size_type sl=p.find('/',1); |
96 | sl!=std::string::npos; | 124 | sl!=std::string::npos; |
97 | sl=p.find('/',sl+1)) { | 125 | sl=p.find('/',sl+1)) { |
98 | if(stat( (pp=p.substr(0,sl)).c_str() ,&st) | 126 | if(stat( (pp=p.substr(0,sl)).c_str() ,&st) |
99 | || !S_ISDIR(st.st_mode)) { | 127 | || !S_ISDIR(st.st_mode)) { |
100 | if(mkdir(pp.c_str(),m)) | 128 | if(mkdir(pp.c_str(),m)) |
101 | throw std::runtime_error("failed to mkdir()"); | 129 | throw std::runtime_error("failed to mkdir()"); |
102 | } | 130 | } |
103 | } | 131 | } |
104 | } | 132 | } |
105 | 133 | ||
106 | tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) { | 134 | tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) { |
107 | make_path_for_template(dt,0777); | 135 | make_path_for_template(dt,0777); |
108 | if(!mkdtemp((char*)dir.data())) | 136 | if(!mkdtemp((char*)dir.data())) |
109 | throw std::runtime_error("failed to mkdtmp()"); | 137 | throw std::runtime_error("failed to mkdtmp()"); |
110 | } | 138 | } |
111 | tmpdir_t::~tmpdir_t() { | 139 | tmpdir_t::~tmpdir_t() { |
112 | assert(!dir.empty()); | 140 | assert(!dir.empty()); |
113 | if(rmdir(dir.c_str())) { | 141 | if(rmdir(dir.c_str())) { |
114 | syslog(LOG_WARNING,"Failed to remove '%s' directory",dir.c_str()); | 142 | syslog(LOG_WARNING,"Failed to remove '%s' directory",dir.c_str()); |
115 | } | 143 | } |
116 | } | 144 | } |
117 | 145 | ||
118 | std::string tmpdir_t::get_file(const std::string& f) { | 146 | std::string tmpdir_t::get_file(const std::string& f) { |
119 | std::string::size_type ls = f.rfind('/'); | 147 | std::string::size_type ls = f.rfind('/'); |
120 | return dir+'/'+( | 148 | return dir+'/'+( |
121 | (ls==std::string::npos) | 149 | (ls==std::string::npos) |
122 | ? f | 150 | ? f |
123 | : f.substr(ls+1) | 151 | : f.substr(ls+1) |
124 | ); | 152 | ); |
125 | } | 153 | } |
126 | 154 | ||
127 | tarchive_t::tarchive_t(void *p,size_t s) : a(archive_read_new()), e(0) { | 155 | tarchive_t::tarchive_t(void *p,size_t s) : a(archive_read_new()), e(0) { |
128 | if(!a) throw std::runtime_error("failed to archive_read_new()"); | 156 | if(!a) throw std::runtime_error("failed to archive_read_new()"); |
129 | if(archive_read_support_format_tar(a)) { | 157 | if(archive_read_support_format_tar(a)) { |
130 | archive_read_finish(a); | 158 | archive_read_finish(a); |
131 | throw std::runtime_error("failed to archive_read_support_format_tar()"); | 159 | throw std::runtime_error("failed to archive_read_support_format_tar()"); |
132 | } | 160 | } |
133 | if(archive_read_open_memory(a,p,s)) { | 161 | if(archive_read_open_memory(a,p,s)) { |
134 | archive_read_finish(a); | 162 | archive_read_finish(a); |
135 | throw std::runtime_error("failed to archive_read_open_memory()"); | 163 | throw std::runtime_error("failed to archive_read_open_memory()"); |
136 | } | 164 | } |
137 | } | 165 | } |
138 | tarchive_t::~tarchive_t() { | 166 | tarchive_t::~tarchive_t() { |
139 | assert(a); | 167 | assert(a); |
140 | archive_read_finish(a); | 168 | archive_read_finish(a); |
141 | } | 169 | } |
142 | 170 | ||
143 | bool tarchive_t::read_next_header() { | 171 | bool tarchive_t::read_next_header() { |
144 | assert(a); | 172 | assert(a); |
145 | return archive_read_next_header(a,&e)==ARCHIVE_OK; | 173 | return archive_read_next_header(a,&e)==ARCHIVE_OK; |
146 | } | 174 | } |
147 | 175 | ||
148 | std::string tarchive_t::entry_pathname() { | 176 | std::string tarchive_t::entry_pathname() { |
149 | assert(a); assert(e); | 177 | assert(a); assert(e); |
150 | return archive_entry_pathname(e); | 178 | return archive_entry_pathname(e); |
151 | } | 179 | } |
152 | 180 | ||
153 | bool tarchive_t::read_data_into_fd(int fd) { | 181 | bool tarchive_t::read_data_into_fd(int fd) { |
154 | assert(a); | 182 | assert(a); |
155 | return archive_read_data_into_fd(a,fd)==ARCHIVE_OK; | 183 | return archive_read_data_into_fd(a,fd)==ARCHIVE_OK; |
diff --git a/src/eyetil.h b/src/eyetil.h index 7517ba6..8784cb4 100644 --- a/src/eyetil.h +++ b/src/eyetil.h | |||
@@ -13,93 +13,104 @@ struct throwable_exit { | |||
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 */ |