-rw-r--r-- | lib/util.cc | 25 |
1 files changed, 0 insertions, 25 deletions
diff --git a/lib/util.cc b/lib/util.cc index 1e7335c..d78b5e0 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -1,138 +1,113 @@ | |||
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 <mimetic/mimetic.h> |
6 | #include <curl/curl.h> | 6 | #include <curl/curl.h> |
7 | #include "opkele/util.h" | 7 | #include "opkele/util.h" |
8 | #include "opkele/exception.h" | 8 | #include "opkele/exception.h" |
9 | 9 | ||
10 | namespace opkele { | 10 | namespace opkele { |
11 | using namespace std; | 11 | using namespace std; |
12 | 12 | ||
13 | namespace util { | 13 | namespace util { |
14 | 14 | ||
15 | /* | 15 | /* |
16 | * big numerics | 16 | * big numerics |
17 | */ | 17 | */ |
18 | 18 | ||
19 | BIGNUM *base64_to_bignum(const string& b64) { | 19 | BIGNUM *base64_to_bignum(const string& b64) { |
20 | vector<unsigned char> bin; | 20 | vector<unsigned char> bin; |
21 | mimetic::Base64::Decoder b; | 21 | mimetic::Base64::Decoder b; |
22 | mimetic::decode( | 22 | mimetic::decode( |
23 | b64.begin(),b64.end(), b, | 23 | b64.begin(),b64.end(), b, |
24 | back_insert_iterator<vector<unsigned char> >(bin) ); | 24 | back_insert_iterator<vector<unsigned char> >(bin) ); |
25 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); | 25 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); |
26 | if(!rv) | 26 | if(!rv) |
27 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); | 27 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); |
28 | return rv; | 28 | return rv; |
29 | } | 29 | } |
30 | 30 | ||
31 | BIGNUM *dec_to_bignum(const string& dec) { | 31 | BIGNUM *dec_to_bignum(const string& dec) { |
32 | BIGNUM *rv = 0; | 32 | BIGNUM *rv = 0; |
33 | if(!BN_dec2bn(&rv,dec.c_str())) | 33 | if(!BN_dec2bn(&rv,dec.c_str())) |
34 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 34 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
35 | return rv; | 35 | return rv; |
36 | } | 36 | } |
37 | 37 | ||
38 | string bignum_to_base64(const BIGNUM *bn) { | 38 | string bignum_to_base64(const BIGNUM *bn) { |
39 | vector<unsigned char> bin(BN_num_bytes(bn)); | 39 | vector<unsigned char> bin(BN_num_bytes(bn)); |
40 | int l = BN_bn2bin(bn,&(bin.front())); | 40 | int l = BN_bn2bin(bn,&(bin.front())); |
41 | string rv; | 41 | string rv; |
42 | mimetic::Base64::Encoder b(0); | 42 | mimetic::Base64::Encoder b(0); |
43 | mimetic::encode( | 43 | mimetic::encode( |
44 | bin.begin(),bin.begin()+l, b, | 44 | bin.begin(),bin.begin()+l, b, |
45 | back_insert_iterator<string>(rv) ); | 45 | back_insert_iterator<string>(rv) ); |
46 | return rv; | 46 | return rv; |
47 | } | 47 | } |
48 | 48 | ||
49 | /* | 49 | /* |
50 | * w3c times | 50 | * w3c times |
51 | */ | 51 | */ |
52 | 52 | ||
53 | string time_to_w3c(time_t t) { | 53 | string time_to_w3c(time_t t) { |
54 | struct tm tm_t; | 54 | struct tm tm_t; |
55 | if(!gmtime_r(&t,&tm_t)) | 55 | if(!gmtime_r(&t,&tm_t)) |
56 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 56 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
57 | char rv[25]; | 57 | char rv[25]; |
58 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) | 58 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) |
59 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); | 59 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); |
60 | return rv; | 60 | return rv; |
61 | } | 61 | } |
62 | 62 | ||
63 | time_t w3c_to_time(const string& w) { | 63 | time_t w3c_to_time(const string& w) { |
64 | struct tm tm_t; | 64 | struct tm tm_t; |
65 | memset(&tm_t,0,sizeof(tm_t)); | 65 | memset(&tm_t,0,sizeof(tm_t)); |
66 | if( | 66 | if( |
67 | sscanf( | 67 | sscanf( |
68 | w.c_str(), | 68 | w.c_str(), |
69 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 69 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
70 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 70 | &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 | 71 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec |
72 | ) != 6 ) | 72 | ) != 6 ) |
73 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 73 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
74 | tm_t.tm_mon--; | 74 | tm_t.tm_mon--; |
75 | tm_t.tm_year-=1900; | 75 | tm_t.tm_year-=1900; |
76 | time_t rv = mktime(&tm_t); | 76 | time_t rv = mktime(&tm_t); |
77 | if(rv==(time_t)-1) | 77 | if(rv==(time_t)-1) |
78 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 78 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); |
79 | return rv; | 79 | return rv; |
80 | } | 80 | } |
81 | 81 | ||
82 | /* | 82 | /* |
83 | * | 83 | * |
84 | */ | 84 | */ |
85 | 85 | ||
86 | string canonicalize_url(const string& url) { | ||
87 | string rv = url; | ||
88 | // strip leading and trailing spaces | ||
89 | string::size_type i = rv.find_first_not_of(" \t\r\n"); | ||
90 | if(i==string::npos) | ||
91 | throw bad_input(OPKELE_CP_ "empty URL"); | ||
92 | if(i) | ||
93 | rv.erase(0,i); | ||
94 | i = rv.find_last_not_of(" \t\r\n"); | ||
95 | assert(i!=string::npos); | ||
96 | if(i<(rv.length()-1)) | ||
97 | rv.erase(i+1); | ||
98 | // add missing http:// | ||
99 | i = rv.find("://"); | ||
100 | if(i==string::npos) { // primitive. but do we need more? | ||
101 | rv.insert(0,"http://"); | ||
102 | i = sizeof("http://")-1; | ||
103 | }else{ | ||
104 | i += sizeof("://")-1; | ||
105 | } | ||
106 | if(rv.find('/',i)==string::npos) | ||
107 | rv += '/'; | ||
108 | return rv; | ||
109 | } | ||
110 | |||
111 | string url_encode(const string& str) { | 86 | string url_encode(const string& str) { |
112 | char * t = curl_escape(str.c_str(),str.length()); | 87 | char * t = curl_escape(str.c_str(),str.length()); |
113 | if(!t) | 88 | if(!t) |
114 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); | 89 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); |
115 | string rv(t); | 90 | string rv(t); |
116 | curl_free(t); | 91 | curl_free(t); |
117 | return rv; | 92 | return rv; |
118 | } | 93 | } |
119 | 94 | ||
120 | string long_to_string(long l) { | 95 | string long_to_string(long l) { |
121 | char rv[32]; | 96 | char rv[32]; |
122 | int r=snprintf(rv,sizeof(rv),"%ld",l); | 97 | int r=snprintf(rv,sizeof(rv),"%ld",l); |
123 | if(r<0 || r>=sizeof(rv)) | 98 | if(r<0 || r>=sizeof(rv)) |
124 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); | 99 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); |
125 | return rv; | 100 | return rv; |
126 | } | 101 | } |
127 | 102 | ||
128 | long string_to_long(const string& s) { | 103 | long string_to_long(const string& s) { |
129 | char *endptr = 0; | 104 | char *endptr = 0; |
130 | long rv = strtol(s.c_str(),&endptr,10); | 105 | long rv = strtol(s.c_str(),&endptr,10); |
131 | if((!endptr) || endptr==s.c_str()) | 106 | if((!endptr) || endptr==s.c_str()) |
132 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); | 107 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); |
133 | return rv; | 108 | return rv; |
134 | } | 109 | } |
135 | 110 | ||
136 | } | 111 | } |
137 | 112 | ||
138 | } | 113 | } |