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) (side-by-side diff) | |
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 @@ -68,48 +68,76 @@ binary_t binary_t::md5() const { (unsigned char*)&(rv.front()) )) throw std::runtime_error("failed to md5()"); return rv; } void md5_digester::init() { if(!MD5_Init(&ctx)) throw std::runtime_error("failed to MD5_Init()"); } void md5_digester::update(const void *d,size_t l) { if(!MD5_Update(&ctx,d,l)) throw std::runtime_error("failed to MD5_Update()"); } binary_t md5_digester::final() { binary_t rv(MD5_DIGEST_LENGTH); if(!MD5_Final((unsigned char*)&(rv.front()), &ctx)) throw std::runtime_error("failed to MD5_Final()"); return rv; } uint16_t block512_t::tcpcksum(block512_t& data) { uint32_t sum = std::accumulate(data.data,data.data+words,0); while(uint32_t hw = sum>>16) sum = (sum&0xffff)+hw; return 0xffff&~sum; } +void integrity_digester::update(const void *d_,size_t s) { + uint8_t *d=(uint8_t*)d_; + if(data_size) { + int l = sizeof(data)-data_size; + if(l>s) { + memmove(data.dptr(data_size),d,s); data_size+=s; return; + } + memmove(data.dptr(data_size),d,l); d+=l; s-=l; + md5.update<uint16_t>( data.tcpcksum(data) ); + } + if(s<sizeof(data)) { + memmove(data.dptr(0),d,s); data_size=s; return; + } + size_t bb=s/sizeof(block512_t); + std::transform((block512_t*)d,((block512_t*)d)+bb, + md5.updater<uint16_t>(),block512_t::tcpcksum); + size_t ss=bb*sizeof(block512_t); + d+=ss; s-=ss; + assert(s<sizeof(block512_t)); + if(s) memmove(data.dptr(0),d,data_size=s); +} + +binary_t integrity_digester::final(const std::string& ukey) { + assert(!data_size); + md5.update( binary_t(ukey) ); + return md5.final(); +} + static void make_path_for_template(const std::string& p,mode_t m) { struct stat st; std::string pp; for(std::string::size_type sl=p.find('/',1); sl!=std::string::npos; sl=p.find('/',sl+1)) { if(stat( (pp=p.substr(0,sl)).c_str() ,&st) || !S_ISDIR(st.st_mode)) { if(mkdir(pp.c_str(),m)) throw std::runtime_error("failed to mkdir()"); } } } tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) { make_path_for_template(dt,0777); if(!mkdtemp((char*)dir.data())) throw std::runtime_error("failed to mkdtmp()"); } tmpdir_t::~tmpdir_t() { assert(!dir.empty()); if(rmdir(dir.c_str())) { syslog(LOG_WARNING,"Failed to remove '%s' directory",dir.c_str()); } diff --git a/src/eyetil.h b/src/eyetil.h index 7517ba6..8784cb4 100644 --- a/src/eyetil.h +++ b/src/eyetil.h @@ -53,48 +53,59 @@ struct md5_digester { } }; template<typename T> update_iterator<T> updater() { return update_iterator<T>(this); } }; template<> inline void md5_digester::update<binary_t>(const binary_t& x) { update((const unsigned char*)&(x.front()),x.size()); } #pragma pack(1) struct block512_t { enum { words = 512 / sizeof(uint16_t) }; uint16_t data[words]; inline uint8_t *dptr(size_t o) { return ((uint8_t*)this)+o; } static uint16_t tcpcksum(block512_t& data); }; #pragma pack() +struct integrity_digester { + md5_digester md5; + size_t data_size; + block512_t data; + + integrity_digester() : data_size(0) { } + void update(const void *d,size_t s); + binary_t final(const std::string& ukey); +}; + + class tmpdir_t { public: std::string dir; tmpdir_t(const std::string& dt); ~tmpdir_t(); std::string get_file(const std::string& f); }; class tarchive_t { public: struct archive *a; struct archive_entry *e; tarchive_t(void *p,size_t s); ~tarchive_t(); bool read_next_header(); std::string entry_pathname(); bool read_data_into_fd(int fd); }; |