-rw-r--r-- | src/eyetil.cc | 29 | ||||
-rw-r--r-- | src/eyetil.h | 3 |
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,23 +1,24 @@ | |||
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 | ||
10 | binary_t& binary_t::from_hex(const std::string& h) { | 12 | binary_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 | } |
@@ -108,12 +109,38 @@ bool tarchive_t::read_next_header() { | |||
108 | return archive_read_next_header(a,&e)==ARCHIVE_OK; | 109 | return archive_read_next_header(a,&e)==ARCHIVE_OK; |
109 | } | 110 | } |
110 | 111 | ||
111 | std::string tarchive_t::entry_pathname() { | 112 | std::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 | ||
116 | bool tarchive_t::read_data_into_fd(int fd) { | 117 | bool 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) | ||
123 | struct 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 | |||
136 | binary_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 | |||
@@ -36,13 +36,16 @@ class tarchive_t { | |||
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 | ||
48 | binary_t integrity_digest(const void *ptr,size_t size, | ||
49 | const std::string& ukey); | ||
50 | |||
48 | #endif /* __EYETIL_H */ | 51 | #endif /* __EYETIL_H */ |