-rw-r--r-- | src/eyetil.cc | 28 |
1 files changed, 28 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 | |||
@@ -60,64 +60,92 @@ std::string binary_t::hex() const { | |||
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) |