summaryrefslogtreecommitdiffabout
authorMichael Krelin <hacker@klever.net>2013-02-13 22:32:22 (UTC)
committer Michael Krelin <hacker@klever.net>2013-02-13 22:32:22 (UTC)
commitdd545573337e3c54ad4c2c64d72f750ad03aa2c9 (patch) (unidiff)
treeedf4e4294fc20418f5590f2698b8ae9b8bb31c65
parentd6d845ab3cdbc76943d68329aa0aaa3ddf2af21a (diff)
downloadiii-dd545573337e3c54ad4c2c64d72f750ad03aa2c9.zip
iii-dd545573337e3c54ad4c2c64d72f750ad03aa2c9.tar.gz
iii-dd545573337e3c54ad4c2c64d72f750ad03aa2c9.tar.bz2
introduced streaming integrity digest updater
Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--src/eyetil.cc28
-rw-r--r--src/eyetil.h11
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
@@ -80,24 +80,52 @@ 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
86uint16_t block512_t::tcpcksum(block512_t& data) { 86uint16_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
92void 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
114binary_t integrity_digester::final(const std::string& ukey) {
115 assert(!data_size);
116 md5.update( binary_t(ukey) );
117 return md5.final();
118}
119
92static void make_path_for_template(const std::string& p,mode_t m) { 120static 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 }
diff --git a/src/eyetil.h b/src/eyetil.h
index 7517ba6..8784cb4 100644
--- a/src/eyetil.h
+++ b/src/eyetil.h
@@ -65,24 +65,35 @@ template<> inline void md5_digester::update<binary_t>(const binary_t& x) {
65 65
66#pragma pack(1) 66#pragma pack(1)
67struct block512_t { 67struct 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
77struct 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
77class tmpdir_t { 88class 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
87class tarchive_t { 98class tarchive_t {
88 public: 99 public: