summaryrefslogtreecommitdiffabout
path: root/src
authorMichael Krelin <hacker@klever.net>2009-04-05 13:32:09 (UTC)
committer Michael Krelin <hacker@klever.net>2009-04-05 13:32:09 (UTC)
commit878315238f71307b5b62ed314096f4a7c465bf3e (patch) (unidiff)
treedad1579d95b1f7189dc6be5cbd66c36cf340cb94 /src
parent01eedb36de69f92fc896c525047df78b34f87324 (diff)
downloadiii-878315238f71307b5b62ed314096f4a7c465bf3e.zip
iii-878315238f71307b5b62ed314096f4a7c465bf3e.tar.gz
iii-878315238f71307b5b62ed314096f4a7c465bf3e.tar.bz2
integrity digest calculation
implemented integrity digest calculation for uploaded files, thanks to cdavies of eye-fi forums. Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (limited to 'src') (more/less context) (ignore whitespace changes)
-rw-r--r--src/eyetil.cc29
-rw-r--r--src/eyetil.h3
2 files changed, 31 insertions, 1 deletions
diff --git a/src/eyetil.cc b/src/eyetil.cc
index 2fbd687..fe816a6 100644
--- a/src/eyetil.cc
+++ b/src/eyetil.cc
@@ -1,119 +1,146 @@
1#include <stdlib.h> 1#include <stdlib.h>
2#include <sys/stat.h> 2#include <sys/stat.h>
3#include <syslog.h> 3#include <syslog.h>
4#include <iostream> 4#include <iostream>
5#include <cassert> 5#include <cassert>
6#include <stdexcept> 6#include <stdexcept>
7#include <algorithm>
8#include <numeric>
7#include <openssl/md5.h> 9#include <openssl/md5.h>
8#include "eyetil.h" 10#include "eyetil.h"
9 11
10binary_t& binary_t::from_hex(const std::string& h) { 12binary_t& binary_t::from_hex(const std::string& h) {
11 /* TODO: algorithmize */
12 std::string::size_type hs = h.length(); 13 std::string::size_type hs = h.length();
13 if(hs&1) 14 if(hs&1)
14 throw std::runtime_error("odd number of characters in hexadecimal number"); 15 throw std::runtime_error("odd number of characters in hexadecimal number");
15 int rvs = hs>>1; 16 int rvs = hs>>1;
16 resize(rvs); 17 resize(rvs);
17 const unsigned char *hp = (const unsigned char*)h.data(); 18 const unsigned char *hp = (const unsigned char*)h.data();
18 iterator oi=begin(); 19 iterator oi=begin();
19 char t[3] = { 0,0,0 }; 20 char t[3] = { 0,0,0 };
20 for(int i=0;i<rvs;++i) { 21 for(int i=0;i<rvs;++i) {
21 t[0]=*(hp++); t[1]=*(hp++); 22 t[0]=*(hp++); t[1]=*(hp++);
22 *(oi++) = strtol(t,0,16); 23 *(oi++) = strtol(t,0,16);
23 } 24 }
24 return *this; 25 return *this;
25} 26}
26 27
27binary_t& binary_t::from_data(const void *d,size_t s) { 28binary_t& binary_t::from_data(const void *d,size_t s) {
28 resize(s); 29 resize(s);
29 std::copy((const unsigned char*)d,(const unsigned char *)d+s, 30 std::copy((const unsigned char*)d,(const unsigned char *)d+s,
30 begin() ); 31 begin() );
31 return *this; 32 return *this;
32} 33}
33 34
34std::string binary_t::hex() const { 35std::string binary_t::hex() const {
35 std::string rv; 36 std::string rv;
36 rv.reserve((size()<<1)+1); 37 rv.reserve((size()<<1)+1);
37 char t[3] = {0,0,0}; 38 char t[3] = {0,0,0};
38 for(const_iterator i=begin(),ie=end();i!=ie;++i) { 39 for(const_iterator i=begin(),ie=end();i!=ie;++i) {
39 int rc = snprintf(t,sizeof(t),"%02x",*i); 40 int rc = snprintf(t,sizeof(t),"%02x",*i);
40 assert(rc<sizeof(t)); 41 assert(rc<sizeof(t));
41 rv += t; 42 rv += t;
42 } 43 }
43 return rv; 44 return rv;
44} 45}
45 46
46binary_t binary_t::md5() const { 47binary_t binary_t::md5() const {
47 binary_t rv(MD5_DIGEST_LENGTH); 48 binary_t rv(MD5_DIGEST_LENGTH);
48 if(!MD5( 49 if(!MD5(
49 (const unsigned char*)&(front()),size(), 50 (const unsigned char*)&(front()),size(),
50 (unsigned char*)&(rv.front()) )) 51 (unsigned char*)&(rv.front()) ))
51 throw std::runtime_error("failed to md5()"); 52 throw std::runtime_error("failed to md5()");
52 return rv; 53 return rv;
53} 54}
54 55
55static void make_path_for_template(const std::string& p,mode_t m) { 56static void make_path_for_template(const std::string& p,mode_t m) {
56 struct stat st; 57 struct stat st;
57 std::string pp; 58 std::string pp;
58 for(std::string::size_type sl=p.find('/',1); 59 for(std::string::size_type sl=p.find('/',1);
59 sl!=std::string::npos; 60 sl!=std::string::npos;
60 sl=p.find('/',sl+1)) { 61 sl=p.find('/',sl+1)) {
61 if(stat( (pp=p.substr(0,sl)).c_str() ,&st) 62 if(stat( (pp=p.substr(0,sl)).c_str() ,&st)
62 || !S_ISDIR(st.st_mode)) { 63 || !S_ISDIR(st.st_mode)) {
63 if(mkdir(pp.c_str(),m)) 64 if(mkdir(pp.c_str(),m))
64 throw std::runtime_error("failed to mkdir()"); 65 throw std::runtime_error("failed to mkdir()");
65 } 66 }
66 } 67 }
67} 68}
68 69
69tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) { 70tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) {
70 make_path_for_template(dt,0777); 71 make_path_for_template(dt,0777);
71 if(!mkdtemp((char*)dir.data())) 72 if(!mkdtemp((char*)dir.data()))
72 throw std::runtime_error("failed to mkdtmp()"); 73 throw std::runtime_error("failed to mkdtmp()");
73} 74}
74tmpdir_t::~tmpdir_t() { 75tmpdir_t::~tmpdir_t() {
75 assert(!dir.empty()); 76 assert(!dir.empty());
76 if(rmdir(dir.c_str())) { 77 if(rmdir(dir.c_str())) {
77 syslog(LOG_WARNING,"Failed to remove '%s' directory",dir.c_str()); 78 syslog(LOG_WARNING,"Failed to remove '%s' directory",dir.c_str());
78 } 79 }
79} 80}
80 81
81std::string tmpdir_t::get_file(const std::string& f) { 82std::string tmpdir_t::get_file(const std::string& f) {
82 std::string::size_type ls = f.rfind('/'); 83 std::string::size_type ls = f.rfind('/');
83 return dir+'/'+( 84 return dir+'/'+(
84 (ls==std::string::npos) 85 (ls==std::string::npos)
85 ? f 86 ? f
86 : f.substr(ls+1) 87 : f.substr(ls+1)
87 ); 88 );
88} 89}
89 90
90tarchive_t::tarchive_t(void *p,size_t s) : a(archive_read_new()), e(0) { 91tarchive_t::tarchive_t(void *p,size_t s) : a(archive_read_new()), e(0) {
91 if(!a) throw std::runtime_error("failed to archive_read_new()"); 92 if(!a) throw std::runtime_error("failed to archive_read_new()");
92 if(archive_read_support_format_tar(a)) { 93 if(archive_read_support_format_tar(a)) {
93 archive_read_finish(a); 94 archive_read_finish(a);
94 throw std::runtime_error("failed to archive_read_support_format_tar()"); 95 throw std::runtime_error("failed to archive_read_support_format_tar()");
95 } 96 }
96 if(archive_read_open_memory(a,p,s)) { 97 if(archive_read_open_memory(a,p,s)) {
97 archive_read_finish(a); 98 archive_read_finish(a);
98 throw std::runtime_error("failed to archive_read_open_memory()"); 99 throw std::runtime_error("failed to archive_read_open_memory()");
99 } 100 }
100} 101}
101tarchive_t::~tarchive_t() { 102tarchive_t::~tarchive_t() {
102 assert(a); 103 assert(a);
103 archive_read_finish(a); 104 archive_read_finish(a);
104} 105}
105 106
106bool tarchive_t::read_next_header() { 107bool tarchive_t::read_next_header() {
107 assert(a); 108 assert(a);
108 return archive_read_next_header(a,&e)==ARCHIVE_OK; 109 return archive_read_next_header(a,&e)==ARCHIVE_OK;
109} 110}
110 111
111std::string tarchive_t::entry_pathname() { 112std::string tarchive_t::entry_pathname() {
112 assert(a); assert(e); 113 assert(a); assert(e);
113 return archive_entry_pathname(e); 114 return archive_entry_pathname(e);
114} 115}
115 116
116bool tarchive_t::read_data_into_fd(int fd) { 117bool tarchive_t::read_data_into_fd(int fd) {
117 assert(a); 118 assert(a);
118 return archive_read_data_into_fd(a,fd)==ARCHIVE_OK; 119 return archive_read_data_into_fd(a,fd)==ARCHIVE_OK;
119} 120}
121
122#pragma pack(1)
123struct block512_t {
124 enum { words = 512 / sizeof(uint16_t) };
125 uint16_t data[words];
126
127 static uint16_t tcpcksum(block512_t& data) {
128 uint32_t sum = std::accumulate(data.data,data.data+words,0);
129 while(uint32_t hw = sum>>16) sum = (sum&0xffff)+hw;
130 return ~sum;
131 }
132
133};
134#pragma pack()
135
136binary_t integrity_digest(const void *ptr,size_t size,const std::string& ukey) {
137 binary_t key; key.from_hex(ukey);
138 std::vector<uint16_t> blksums; blksums.reserve(size/sizeof(block512_t));
139 block512_t *db = (block512_t*)ptr,
140 *de = db + size/sizeof(block512_t);
141 std::transform( db, de, std::back_inserter(blksums), block512_t::tcpcksum );
142 binary_t subject;
143 subject.from_data((void*)&(blksums.front()),blksums.size()*sizeof(uint16_t));
144 std::copy( key.begin(), key.end(), std::back_inserter(subject) );
145 return subject.md5();
146}
diff --git a/src/eyetil.h b/src/eyetil.h
index 195d24f..378f703 100644
--- a/src/eyetil.h
+++ b/src/eyetil.h
@@ -1,48 +1,51 @@
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 8
9class binary_t : public std::vector<unsigned char> { 9class binary_t : public std::vector<unsigned char> {
10 public: 10 public:
11 binary_t() { } 11 binary_t() { }
12 binary_t(size_type n) : std::vector<unsigned char>(n) { } 12 binary_t(size_type n) : std::vector<unsigned char>(n) { }
13 binary_t(const std::string& h) { from_hex(h); } 13 binary_t(const std::string& h) { from_hex(h); }
14 binary_t(const void *d,size_t s) { from_data(d,s); } 14 binary_t(const void *d,size_t s) { from_data(d,s); }
15 15
16 binary_t& from_hex(const std::string& h); 16 binary_t& from_hex(const std::string& h);
17 binary_t& from_data(const void *d,size_t s); 17 binary_t& from_data(const void *d,size_t s);
18 18
19 std::string hex() const; 19 std::string hex() const;
20 binary_t md5() const; 20 binary_t md5() const;
21}; 21};
22 22
23class tmpdir_t { 23class tmpdir_t {
24 public: 24 public:
25 std::string dir; 25 std::string dir;
26 26
27 tmpdir_t(const std::string& dt); 27 tmpdir_t(const std::string& dt);
28 ~tmpdir_t(); 28 ~tmpdir_t();
29 29
30 std::string get_file(const std::string& f); 30 std::string get_file(const std::string& f);
31}; 31};
32 32
33class tarchive_t { 33class tarchive_t {
34 public: 34 public:
35 struct archive *a; 35 struct archive *a;
36 struct archive_entry *e; 36 struct archive_entry *e;
37 37
38 tarchive_t(void *p,size_t s); 38 tarchive_t(void *p,size_t s);
39 ~tarchive_t(); 39 ~tarchive_t();
40 40
41 bool read_next_header(); 41 bool read_next_header();
42 42
43 std::string entry_pathname(); 43 std::string entry_pathname();
44 44
45 bool read_data_into_fd(int fd); 45 bool read_data_into_fd(int fd);
46}; 46};
47 47
48binary_t integrity_digest(const void *ptr,size_t size,
49 const std::string& ukey);
50
48#endif /* __EYETIL_H */ 51#endif /* __EYETIL_H */