summaryrefslogtreecommitdiffabout
path: root/src/eyetil.cc
Unidiff
Diffstat (limited to 'src/eyetil.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--src/eyetil.cc17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/eyetil.cc b/src/eyetil.cc
index fe816a6..7669cb6 100644
--- a/src/eyetil.cc
+++ b/src/eyetil.cc
@@ -1,82 +1,99 @@
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> 7#include <algorithm>
8#include <numeric> 8#include <numeric>
9#include <openssl/md5.h> 9#include <openssl/md5.h>
10#include "eyetil.h" 10#include "eyetil.h"
11 11
12#include "config.h"
13#ifdef HAVE_LIBUUID
14# include <uuid/uuid.h>
15#endif
16
12binary_t& binary_t::from_hex(const std::string& h) { 17binary_t& binary_t::from_hex(const std::string& h) {
13 std::string::size_type hs = h.length(); 18 std::string::size_type hs = h.length();
14 if(hs&1) 19 if(hs&1)
15 throw std::runtime_error("odd number of characters in hexadecimal number"); 20 throw std::runtime_error("odd number of characters in hexadecimal number");
16 int rvs = hs>>1; 21 int rvs = hs>>1;
17 resize(rvs); 22 resize(rvs);
18 const unsigned char *hp = (const unsigned char*)h.data(); 23 const unsigned char *hp = (const unsigned char*)h.data();
19 iterator oi=begin(); 24 iterator oi=begin();
20 char t[3] = { 0,0,0 }; 25 char t[3] = { 0,0,0 };
21 for(int i=0;i<rvs;++i) { 26 for(int i=0;i<rvs;++i) {
22 t[0]=*(hp++); t[1]=*(hp++); 27 t[0]=*(hp++); t[1]=*(hp++);
23 *(oi++) = strtol(t,0,16); 28 *(oi++) = strtol(t,0,16);
24 } 29 }
25 return *this; 30 return *this;
26} 31}
27 32
28binary_t& binary_t::from_data(const void *d,size_t s) { 33binary_t& binary_t::from_data(const void *d,size_t s) {
29 resize(s); 34 resize(s);
30 std::copy((const unsigned char*)d,(const unsigned char *)d+s, 35 std::copy((const unsigned char*)d,(const unsigned char *)d+s,
31 begin() ); 36 begin() );
32 return *this; 37 return *this;
33} 38}
34 39
40binary_t& binary_t::make_nonce() {
41#ifdef HAVE_LIBUUID
42 uuid_t uuid;
43 uuid_generate(uuid);
44 from_data((unsigned char*)uuid,sizeof(uuid));
45#else
46 resize(16);
47 std::generate_n(begin(),16,rand);
48#endif /* HAVE_LIBUUID */
49 return *this;
50}
51
35std::string binary_t::hex() const { 52std::string binary_t::hex() const {
36 std::string rv; 53 std::string rv;
37 rv.reserve((size()<<1)+1); 54 rv.reserve((size()<<1)+1);
38 char t[3] = {0,0,0}; 55 char t[3] = {0,0,0};
39 for(const_iterator i=begin(),ie=end();i!=ie;++i) { 56 for(const_iterator i=begin(),ie=end();i!=ie;++i) {
40 int rc = snprintf(t,sizeof(t),"%02x",*i); 57 int rc = snprintf(t,sizeof(t),"%02x",*i);
41 assert(rc<sizeof(t)); 58 assert(rc<sizeof(t));
42 rv += t; 59 rv += t;
43 } 60 }
44 return rv; 61 return rv;
45} 62}
46 63
47binary_t binary_t::md5() const { 64binary_t binary_t::md5() const {
48 binary_t rv(MD5_DIGEST_LENGTH); 65 binary_t rv(MD5_DIGEST_LENGTH);
49 if(!MD5( 66 if(!MD5(
50 (const unsigned char*)&(front()),size(), 67 (const unsigned char*)&(front()),size(),
51 (unsigned char*)&(rv.front()) )) 68 (unsigned char*)&(rv.front()) ))
52 throw std::runtime_error("failed to md5()"); 69 throw std::runtime_error("failed to md5()");
53 return rv; 70 return rv;
54} 71}
55 72
56static void make_path_for_template(const std::string& p,mode_t m) { 73static void make_path_for_template(const std::string& p,mode_t m) {
57 struct stat st; 74 struct stat st;
58 std::string pp; 75 std::string pp;
59 for(std::string::size_type sl=p.find('/',1); 76 for(std::string::size_type sl=p.find('/',1);
60 sl!=std::string::npos; 77 sl!=std::string::npos;
61 sl=p.find('/',sl+1)) { 78 sl=p.find('/',sl+1)) {
62 if(stat( (pp=p.substr(0,sl)).c_str() ,&st) 79 if(stat( (pp=p.substr(0,sl)).c_str() ,&st)
63 || !S_ISDIR(st.st_mode)) { 80 || !S_ISDIR(st.st_mode)) {
64 if(mkdir(pp.c_str(),m)) 81 if(mkdir(pp.c_str(),m))
65 throw std::runtime_error("failed to mkdir()"); 82 throw std::runtime_error("failed to mkdir()");
66 } 83 }
67 } 84 }
68} 85}
69 86
70tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) { 87tmpdir_t::tmpdir_t(const std::string& dt) : dir(dt) {
71 make_path_for_template(dt,0777); 88 make_path_for_template(dt,0777);
72 if(!mkdtemp((char*)dir.data())) 89 if(!mkdtemp((char*)dir.data()))
73 throw std::runtime_error("failed to mkdtmp()"); 90 throw std::runtime_error("failed to mkdtmp()");
74} 91}
75tmpdir_t::~tmpdir_t() { 92tmpdir_t::~tmpdir_t() {
76 assert(!dir.empty()); 93 assert(!dir.empty());
77 if(rmdir(dir.c_str())) { 94 if(rmdir(dir.c_str())) {
78 syslog(LOG_WARNING,"Failed to remove '%s' directory",dir.c_str()); 95 syslog(LOG_WARNING,"Failed to remove '%s' directory",dir.c_str());
79 } 96 }
80} 97}
81 98
82std::string tmpdir_t::get_file(const std::string& f) { 99std::string tmpdir_t::get_file(const std::string& f) {