-rw-r--r-- | lib/util.cc | 67 |
1 files changed, 56 insertions, 11 deletions
diff --git a/lib/util.cc b/lib/util.cc index d78b5e0..d9abca7 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -1,113 +1,158 @@ | |||
1 | #include <errno.h> | 1 | #include <errno.h> |
2 | #include <cassert> | 2 | #include <cassert> |
3 | #include <vector> | 3 | #include <vector> |
4 | #include <string> | 4 | #include <string> |
5 | #include <mimetic/mimetic.h> | 5 | #include <openssl/bio.h> |
6 | #include <openssl/evp.h> | ||
6 | #include <curl/curl.h> | 7 | #include <curl/curl.h> |
7 | #include "opkele/util.h" | 8 | #include "opkele/util.h" |
8 | #include "opkele/exception.h" | 9 | #include "opkele/exception.h" |
9 | 10 | ||
10 | namespace opkele { | 11 | namespace opkele { |
11 | using namespace std; | 12 | using namespace std; |
12 | 13 | ||
13 | namespace util { | 14 | namespace util { |
14 | 15 | ||
15 | /* | 16 | /* |
17 | * base64 | ||
18 | */ | ||
19 | string encode_base64(const void *data,size_t length) { | ||
20 | BIO *b64 = 0, *bmem = 0; | ||
21 | try { | ||
22 | b64 = BIO_new(BIO_f_base64()); | ||
23 | if(!b64) | ||
24 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); | ||
25 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | ||
26 | bmem = BIO_new(BIO_s_mem()); | ||
27 | BIO_set_flags(b64,BIO_CLOSE); | ||
28 | if(!bmem) | ||
29 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); | ||
30 | BIO_push(b64,bmem); | ||
31 | if(BIO_write(b64,data,length)!=length) | ||
32 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); | ||
33 | BIO_flush(b64); | ||
34 | char *rvd; | ||
35 | long rvl = BIO_get_mem_data(bmem,&rvd); | ||
36 | string rv(rvd,rvl); | ||
37 | BIO_free_all(b64); | ||
38 | return rv; | ||
39 | }catch(...) { | ||
40 | if(b64) BIO_free_all(b64); | ||
41 | throw; | ||
42 | } | ||
43 | } | ||
44 | |||
45 | void decode_base64(const string& data,vector<unsigned char>& rv) { | ||
46 | BIO *b64 = 0, *bmem = 0; | ||
47 | rv.clear(); | ||
48 | try { | ||
49 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); | ||
50 | if(!bmem) | ||
51 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); | ||
52 | b64 = BIO_new(BIO_f_base64()); | ||
53 | if(!b64) | ||
54 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); | ||
55 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | ||
56 | BIO_push(b64,bmem); | ||
57 | unsigned char tmp[512]; | ||
58 | size_t rb = 0; | ||
59 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) | ||
60 | rv.insert(rv.end(),tmp,&tmp[rb]); | ||
61 | BIO_free_all(b64); | ||
62 | }catch(...) { | ||
63 | if(b64) BIO_free_all(b64); | ||
64 | throw; | ||
65 | } | ||
66 | } | ||
67 | |||
68 | /* | ||
16 | * big numerics | 69 | * big numerics |
17 | */ | 70 | */ |
18 | 71 | ||
19 | BIGNUM *base64_to_bignum(const string& b64) { | 72 | BIGNUM *base64_to_bignum(const string& b64) { |
20 | vector<unsigned char> bin; | 73 | vector<unsigned char> bin; |
21 | mimetic::Base64::Decoder b; | 74 | decode_base64(b64,bin); |
22 | mimetic::decode( | ||
23 | b64.begin(),b64.end(), b, | ||
24 | back_insert_iterator<vector<unsigned char> >(bin) ); | ||
25 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); | 75 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); |
26 | if(!rv) | 76 | if(!rv) |
27 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); | 77 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); |
28 | return rv; | 78 | return rv; |
29 | } | 79 | } |
30 | 80 | ||
31 | BIGNUM *dec_to_bignum(const string& dec) { | 81 | BIGNUM *dec_to_bignum(const string& dec) { |
32 | BIGNUM *rv = 0; | 82 | BIGNUM *rv = 0; |
33 | if(!BN_dec2bn(&rv,dec.c_str())) | 83 | if(!BN_dec2bn(&rv,dec.c_str())) |
34 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 84 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
35 | return rv; | 85 | return rv; |
36 | } | 86 | } |
37 | 87 | ||
38 | string bignum_to_base64(const BIGNUM *bn) { | 88 | string bignum_to_base64(const BIGNUM *bn) { |
39 | vector<unsigned char> bin(BN_num_bytes(bn)); | 89 | vector<unsigned char> bin(BN_num_bytes(bn)); |
40 | int l = BN_bn2bin(bn,&(bin.front())); | 90 | int l = BN_bn2bin(bn,&(bin.front())); |
41 | string rv; | 91 | return encode_base64(&(bin.front()),l); |
42 | mimetic::Base64::Encoder b(0); | ||
43 | mimetic::encode( | ||
44 | bin.begin(),bin.begin()+l, b, | ||
45 | back_insert_iterator<string>(rv) ); | ||
46 | return rv; | ||
47 | } | 92 | } |
48 | 93 | ||
49 | /* | 94 | /* |
50 | * w3c times | 95 | * w3c times |
51 | */ | 96 | */ |
52 | 97 | ||
53 | string time_to_w3c(time_t t) { | 98 | string time_to_w3c(time_t t) { |
54 | struct tm tm_t; | 99 | struct tm tm_t; |
55 | if(!gmtime_r(&t,&tm_t)) | 100 | if(!gmtime_r(&t,&tm_t)) |
56 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 101 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
57 | char rv[25]; | 102 | char rv[25]; |
58 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) | 103 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) |
59 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); | 104 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); |
60 | return rv; | 105 | return rv; |
61 | } | 106 | } |
62 | 107 | ||
63 | time_t w3c_to_time(const string& w) { | 108 | time_t w3c_to_time(const string& w) { |
64 | struct tm tm_t; | 109 | struct tm tm_t; |
65 | memset(&tm_t,0,sizeof(tm_t)); | 110 | memset(&tm_t,0,sizeof(tm_t)); |
66 | if( | 111 | if( |
67 | sscanf( | 112 | sscanf( |
68 | w.c_str(), | 113 | w.c_str(), |
69 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 114 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
70 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 115 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
71 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec | 116 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec |
72 | ) != 6 ) | 117 | ) != 6 ) |
73 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 118 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
74 | tm_t.tm_mon--; | 119 | tm_t.tm_mon--; |
75 | tm_t.tm_year-=1900; | 120 | tm_t.tm_year-=1900; |
76 | time_t rv = mktime(&tm_t); | 121 | time_t rv = mktime(&tm_t); |
77 | if(rv==(time_t)-1) | 122 | if(rv==(time_t)-1) |
78 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 123 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); |
79 | return rv; | 124 | return rv; |
80 | } | 125 | } |
81 | 126 | ||
82 | /* | 127 | /* |
83 | * | 128 | * |
84 | */ | 129 | */ |
85 | 130 | ||
86 | string url_encode(const string& str) { | 131 | string url_encode(const string& str) { |
87 | char * t = curl_escape(str.c_str(),str.length()); | 132 | char * t = curl_escape(str.c_str(),str.length()); |
88 | if(!t) | 133 | if(!t) |
89 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); | 134 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); |
90 | string rv(t); | 135 | string rv(t); |
91 | curl_free(t); | 136 | curl_free(t); |
92 | return rv; | 137 | return rv; |
93 | } | 138 | } |
94 | 139 | ||
95 | string long_to_string(long l) { | 140 | string long_to_string(long l) { |
96 | char rv[32]; | 141 | char rv[32]; |
97 | int r=snprintf(rv,sizeof(rv),"%ld",l); | 142 | int r=snprintf(rv,sizeof(rv),"%ld",l); |
98 | if(r<0 || r>=sizeof(rv)) | 143 | if(r<0 || r>=sizeof(rv)) |
99 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); | 144 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); |
100 | return rv; | 145 | return rv; |
101 | } | 146 | } |
102 | 147 | ||
103 | long string_to_long(const string& s) { | 148 | long string_to_long(const string& s) { |
104 | char *endptr = 0; | 149 | char *endptr = 0; |
105 | long rv = strtol(s.c_str(),&endptr,10); | 150 | long rv = strtol(s.c_str(),&endptr,10); |
106 | if((!endptr) || endptr==s.c_str()) | 151 | if((!endptr) || endptr==s.c_str()) |
107 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); | 152 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); |
108 | return rv; | 153 | return rv; |
109 | } | 154 | } |
110 | 155 | ||
111 | } | 156 | } |
112 | 157 | ||
113 | } | 158 | } |