summaryrefslogtreecommitdiffabout
path: root/lib
authorMichael Krelin <hacker@klever.net>2008-06-29 16:08:01 (UTC)
committer Michael Krelin <hacker@klever.net>2008-06-29 16:11:44 (UTC)
commit12837594b705ad10fdadfd0ba1bfc2249b3b1264 (patch) (unidiff)
treee0502e678b09668ec59828237ebf5972014d0ce0 /lib
parent362678728b8232c9490e14ba14ff323d9a92d6be (diff)
downloadlibopkele-12837594b705ad10fdadfd0ba1bfc2249b3b1264.zip
libopkele-12837594b705ad10fdadfd0ba1bfc2249b3b1264.tar.gz
libopkele-12837594b705ad10fdadfd0ba1bfc2249b3b1264.tar.bz2
Fixed w3c to unix timestamp conversion for FreeBSD
Thanks to Göran Löwkrantz for pointing both to the problem and possible solution. Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (limited to 'lib') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/util.cc21
1 files changed, 18 insertions, 3 deletions
diff --git a/lib/util.cc b/lib/util.cc
index d979502..a46ba2a 100644
--- a/lib/util.cc
+++ b/lib/util.cc
@@ -1,279 +1,294 @@
1#include <errno.h> 1#include <errno.h>
2#include <cassert> 2#include <cassert>
3#include <cctype> 3#include <cctype>
4#include <cstring> 4#include <cstring>
5#include <vector> 5#include <vector>
6#include <string> 6#include <string>
7#include <stack> 7#include <stack>
8#include <algorithm> 8#include <algorithm>
9#include <openssl/bio.h> 9#include <openssl/bio.h>
10#include <openssl/evp.h> 10#include <openssl/evp.h>
11#include <openssl/sha.h> 11#include <openssl/sha.h>
12#include <openssl/hmac.h> 12#include <openssl/hmac.h>
13#include <opkele/util.h> 13#include <opkele/util.h>
14#include <opkele/exception.h> 14#include <opkele/exception.h>
15#include <opkele/data.h> 15#include <opkele/data.h>
16#include <opkele/debug.h> 16#include <opkele/debug.h>
17 17
18#include <config.h> 18#include <config.h>
19#ifdef HAVE_DEMANGLE 19#ifdef HAVE_DEMANGLE
20# include <cxxabi.h> 20# include <cxxabi.h>
21#endif 21#endif
22 22
23namespace opkele { 23namespace opkele {
24 using namespace std; 24 using namespace std;
25 25
26 namespace util { 26 namespace util {
27 27
28 /* 28 /*
29 * base64 29 * base64
30 */ 30 */
31 string encode_base64(const void *data,size_t length) { 31 string encode_base64(const void *data,size_t length) {
32 BIO *b64 = 0, *bmem = 0; 32 BIO *b64 = 0, *bmem = 0;
33 try { 33 try {
34 b64 = BIO_new(BIO_f_base64()); 34 b64 = BIO_new(BIO_f_base64());
35 if(!b64) 35 if(!b64)
36 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); 36 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder");
37 BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); 37 BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
38 bmem = BIO_new(BIO_s_mem()); 38 bmem = BIO_new(BIO_s_mem());
39 BIO_set_flags(b64,BIO_CLOSE); 39 BIO_set_flags(b64,BIO_CLOSE);
40 if(!bmem) 40 if(!bmem)
41 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); 41 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer");
42 BIO_push(b64,bmem); 42 BIO_push(b64,bmem);
43 if(((size_t)BIO_write(b64,data,length))!=length) 43 if(((size_t)BIO_write(b64,data,length))!=length)
44 throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); 44 throw exception_openssl(OPKELE_CP_ "failed to BIO_write()");
45 if(BIO_flush(b64)!=1) 45 if(BIO_flush(b64)!=1)
46 throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); 46 throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()");
47 char *rvd; 47 char *rvd;
48 long rvl = BIO_get_mem_data(bmem,&rvd); 48 long rvl = BIO_get_mem_data(bmem,&rvd);
49 string rv(rvd,rvl); 49 string rv(rvd,rvl);
50 BIO_free_all(b64); 50 BIO_free_all(b64);
51 return rv; 51 return rv;
52 }catch(...) { 52 }catch(...) {
53 if(b64) BIO_free_all(b64); 53 if(b64) BIO_free_all(b64);
54 throw; 54 throw;
55 } 55 }
56 } 56 }
57 57
58 void decode_base64(const string& data,vector<unsigned char>& rv) { 58 void decode_base64(const string& data,vector<unsigned char>& rv) {
59 BIO *b64 = 0, *bmem = 0; 59 BIO *b64 = 0, *bmem = 0;
60 rv.clear(); 60 rv.clear();
61 try { 61 try {
62 bmem = BIO_new_mem_buf((void*)data.data(),data.size()); 62 bmem = BIO_new_mem_buf((void*)data.data(),data.size());
63 if(!bmem) 63 if(!bmem)
64 throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); 64 throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()");
65 b64 = BIO_new(BIO_f_base64()); 65 b64 = BIO_new(BIO_f_base64());
66 if(!b64) 66 if(!b64)
67 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); 67 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder");
68 BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); 68 BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
69 BIO_push(b64,bmem); 69 BIO_push(b64,bmem);
70 unsigned char tmp[512]; 70 unsigned char tmp[512];
71 size_t rb = 0; 71 size_t rb = 0;
72 while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) 72 while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0)
73 rv.insert(rv.end(),tmp,&tmp[rb]); 73 rv.insert(rv.end(),tmp,&tmp[rb]);
74 BIO_free_all(b64); 74 BIO_free_all(b64);
75 }catch(...) { 75 }catch(...) {
76 if(b64) BIO_free_all(b64); 76 if(b64) BIO_free_all(b64);
77 throw; 77 throw;
78 } 78 }
79 } 79 }
80 80
81 /* 81 /*
82 * big numerics 82 * big numerics
83 */ 83 */
84 84
85 BIGNUM *base64_to_bignum(const string& b64) { 85 BIGNUM *base64_to_bignum(const string& b64) {
86 vector<unsigned char> bin; 86 vector<unsigned char> bin;
87 decode_base64(b64,bin); 87 decode_base64(b64,bin);
88 BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); 88 BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0);
89 if(!rv) 89 if(!rv)
90 throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); 90 throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()");
91 return rv; 91 return rv;
92 } 92 }
93 93
94 BIGNUM *dec_to_bignum(const string& dec) { 94 BIGNUM *dec_to_bignum(const string& dec) {
95 BIGNUM *rv = 0; 95 BIGNUM *rv = 0;
96 if(!BN_dec2bn(&rv,dec.c_str())) 96 if(!BN_dec2bn(&rv,dec.c_str()))
97 throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); 97 throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()");
98 return rv; 98 return rv;
99 } 99 }
100 100
101 string bignum_to_base64(const BIGNUM *bn) { 101 string bignum_to_base64(const BIGNUM *bn) {
102 vector<unsigned char> bin(BN_num_bytes(bn)+1); 102 vector<unsigned char> bin(BN_num_bytes(bn)+1);
103 unsigned char *binptr = &(bin.front())+1; 103 unsigned char *binptr = &(bin.front())+1;
104 int l = BN_bn2bin(bn,binptr); 104 int l = BN_bn2bin(bn,binptr);
105 if(l && (*binptr)&0x80){ 105 if(l && (*binptr)&0x80){
106 (*(--binptr)) = 0; ++l; 106 (*(--binptr)) = 0; ++l;
107 } 107 }
108 return encode_base64(binptr,l); 108 return encode_base64(binptr,l);
109 } 109 }
110 110
111 /* 111 /*
112 * w3c times 112 * w3c times
113 */ 113 */
114 114
115 string time_to_w3c(time_t t) { 115 string time_to_w3c(time_t t) {
116 struct tm tm_t; 116 struct tm tm_t;
117 if(!gmtime_r(&t,&tm_t)) 117 if(!gmtime_r(&t,&tm_t))
118 throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); 118 throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()");
119 char rv[25]; 119 char rv[25];
120 if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) 120 if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t))
121 throw failed_conversion(OPKELE_CP_ "failed to strftime()"); 121 throw failed_conversion(OPKELE_CP_ "failed to strftime()");
122 return rv; 122 return rv;
123 } 123 }
124 124
125#ifndef HAVE_TIMEGM
126 static time_t timegm(struct tm *t) {
127 char *tz = getenv("TZ");
128 setenv("TZ","",1); tzset();
129 time_t rv = mktime(t);
130 if(tz)
131 setenv("TZ",tz,1);
132 else
133 unsetenv("TZ");
134 tzset();
135 return rv;
136 }
137 #define timegm opkele::util::timegm
138#endif /* HAVE_TIMEGM */
139
125 time_t w3c_to_time(const string& w) { 140 time_t w3c_to_time(const string& w) {
126 int fraction; 141 int fraction;
127 struct tm tm_t; 142 struct tm tm_t;
128 memset(&tm_t,0,sizeof(tm_t)); 143 memset(&tm_t,0,sizeof(tm_t));
129 if( ( 144 if( (
130 sscanf( 145 sscanf(
131 w.c_str(), 146 w.c_str(),
132 "%04d-%02d-%02dT%02d:%02d:%02dZ", 147 "%04d-%02d-%02dT%02d:%02d:%02dZ",
133 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, 148 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday,
134 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec 149 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec
135 ) != 6 150 ) != 6
136 ) && ( 151 ) && (
137 sscanf( 152 sscanf(
138 w.c_str(), 153 w.c_str(),
139 "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", 154 "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
140 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, 155 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday,
141 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, 156 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec,
142 &fraction 157 &fraction
143 ) != 7 158 ) != 7
144 ) ) 159 ) )
145 throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); 160 throw failed_conversion(OPKELE_CP_ "failed to sscanf()");
146 tm_t.tm_mon--; 161 tm_t.tm_mon--;
147 tm_t.tm_year-=1900; 162 tm_t.tm_year-=1900;
148 time_t rv = mktime(&tm_t); 163 time_t rv = timegm(&tm_t);
149 if(rv==(time_t)-1) 164 if(rv==(time_t)-1)
150 throw failed_conversion(OPKELE_CP_ "failed to mktime()"); 165 throw failed_conversion(OPKELE_CP_ "failed to gmtime()");
151 return rv-timezone; 166 return rv;
152 } 167 }
153 168
154 /* 169 /*
155 * 170 *
156 */ 171 */
157 172
158 static inline bool isrfc3986unreserved(int c) { 173 static inline bool isrfc3986unreserved(int c) {
159 if(c<'-') return false; 174 if(c<'-') return false;
160 if(c<='.') return true; 175 if(c<='.') return true;
161 if(c<'0') return false; if(c<='9') return true; 176 if(c<'0') return false; if(c<='9') return true;
162 if(c<'A') return false; if(c<='Z') return true; 177 if(c<'A') return false; if(c<='Z') return true;
163 if(c<'_') return false; 178 if(c<'_') return false;
164 if(c=='_') return true; 179 if(c=='_') return true;
165 if(c<'a') return false; if(c<='z') return true; 180 if(c<'a') return false; if(c<='z') return true;
166 if(c=='~') return true; 181 if(c=='~') return true;
167 return false; 182 return false;
168 } 183 }
169 184
170 struct __url_encoder : public unary_function<char,void> { 185 struct __url_encoder : public unary_function<char,void> {
171 public: 186 public:
172 string& rv; 187 string& rv;
173 188
174 __url_encoder(string& r) : rv(r) { } 189 __url_encoder(string& r) : rv(r) { }
175 190
176 result_type operator()(argument_type c) { 191 result_type operator()(argument_type c) {
177 if(isrfc3986unreserved(c)) 192 if(isrfc3986unreserved(c))
178 rv += c; 193 rv += c;
179 else{ 194 else{
180 char tmp[4]; 195 char tmp[4];
181 snprintf(tmp,sizeof(tmp),"%%%02X", 196 snprintf(tmp,sizeof(tmp),"%%%02X",
182 (c&0xff)); 197 (c&0xff));
183 rv += tmp; 198 rv += tmp;
184 } 199 }
185 } 200 }
186 }; 201 };
187 202
188 string url_encode(const string& str) { 203 string url_encode(const string& str) {
189 string rv; 204 string rv;
190 for_each(str.begin(),str.end(), 205 for_each(str.begin(),str.end(),
191 __url_encoder(rv)); 206 __url_encoder(rv));
192 return rv; 207 return rv;
193 } 208 }
194 209
195 string url_decode(const string& str) { 210 string url_decode(const string& str) {
196 string rv; 211 string rv;
197 back_insert_iterator<string> ii(rv); 212 back_insert_iterator<string> ii(rv);
198 for(string::const_iterator i=str.begin(),ie=str.end(); 213 for(string::const_iterator i=str.begin(),ie=str.end();
199 i!=ie;++i) { 214 i!=ie;++i) {
200 switch(*i) { 215 switch(*i) {
201 case '+': 216 case '+':
202 *(ii++) = ' '; break; 217 *(ii++) = ' '; break;
203 case '%': 218 case '%':
204 ++i; 219 ++i;
205 static char tmp[3] = {0,0,0}; 220 static char tmp[3] = {0,0,0};
206 if(i==ie) 221 if(i==ie)
207 throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string"); 222 throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string");
208 tmp[0] = *(i++); 223 tmp[0] = *(i++);
209 if(i==ie) 224 if(i==ie)
210 throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string"); 225 throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string");
211 tmp[1] = *i; 226 tmp[1] = *i;
212 if(!(isxdigit(tmp[0]) && isxdigit(tmp[1]))) 227 if(!(isxdigit(tmp[0]) && isxdigit(tmp[1])))
213 throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string"); 228 throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string");
214 *(ii++) = (char)strtol(tmp,0,16); 229 *(ii++) = (char)strtol(tmp,0,16);
215 break; 230 break;
216 default: 231 default:
217 *(ii++) = *i; break; 232 *(ii++) = *i; break;
218 } 233 }
219 } 234 }
220 return rv; 235 return rv;
221 } 236 }
222 237
223 string attr_escape(const string& str) { 238 string attr_escape(const string& str) {
224 static const char *unsafechars = "<>&\n\"'"; 239 static const char *unsafechars = "<>&\n\"'";
225 string rv; 240 string rv;
226 string::size_type p=0; 241 string::size_type p=0;
227 while(true) { 242 while(true) {
228 string::size_type us = str.find_first_of(unsafechars,p); 243 string::size_type us = str.find_first_of(unsafechars,p);
229 if(us==string::npos) { 244 if(us==string::npos) {
230 if(p!=str.length()) 245 if(p!=str.length())
231 rv.append(str,p,str.length()-p); 246 rv.append(str,p,str.length()-p);
232 return rv; 247 return rv;
233 } 248 }
234 rv.append(str,p,us-p); 249 rv.append(str,p,us-p);
235 rv += "&#"; 250 rv += "&#";
236 rv += long_to_string((long)str[us]); 251 rv += long_to_string((long)str[us]);
237 rv += ';'; 252 rv += ';';
238 p = us+1; 253 p = us+1;
239 } 254 }
240 } 255 }
241 256
242 string long_to_string(long l) { 257 string long_to_string(long l) {
243 char rv[32]; 258 char rv[32];
244 int r=snprintf(rv,sizeof(rv),"%ld",l); 259 int r=snprintf(rv,sizeof(rv),"%ld",l);
245 if(r<0 || r>=(int)sizeof(rv)) 260 if(r<0 || r>=(int)sizeof(rv))
246 throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); 261 throw failed_conversion(OPKELE_CP_ "failed to snprintf()");
247 return rv; 262 return rv;
248 } 263 }
249 264
250 long string_to_long(const string& s) { 265 long string_to_long(const string& s) {
251 char *endptr = 0; 266 char *endptr = 0;
252 long rv = strtol(s.c_str(),&endptr,10); 267 long rv = strtol(s.c_str(),&endptr,10);
253 if((!endptr) || endptr==s.c_str()) 268 if((!endptr) || endptr==s.c_str())
254 throw failed_conversion(OPKELE_CP_ "failed to strtol()"); 269 throw failed_conversion(OPKELE_CP_ "failed to strtol()");
255 return rv; 270 return rv;
256 } 271 }
257 272
258 /* 273 /*
259 * Normalize URL according to the rules, described in rfc 3986, section 6 274 * Normalize URL according to the rules, described in rfc 3986, section 6
260 * 275 *
261 * - uppercase hex triplets (e.g. %ab -> %AB) 276 * - uppercase hex triplets (e.g. %ab -> %AB)
262 * - lowercase scheme and host 277 * - lowercase scheme and host
263 * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, 278 * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3,
264 * that is - [:alpha:][:digit:]._~- 279 * that is - [:alpha:][:digit:]._~-
265 * - remove dot segments 280 * - remove dot segments
266 * - remove empty and default ports 281 * - remove empty and default ports
267 * - if there's no path component, add '/' 282 * - if there's no path component, add '/'
268 */ 283 */
269 string rfc_3986_normalize_uri(const string& uri) { 284 string rfc_3986_normalize_uri(const string& uri) {
270 string rv; 285 string rv;
271 string::size_type ns = uri.find_first_not_of(data::_whitespace_chars); 286 string::size_type ns = uri.find_first_not_of(data::_whitespace_chars);
272 if(ns==string::npos) 287 if(ns==string::npos)
273 throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); 288 throw bad_input(OPKELE_CP_ "Can't normalize empty URI");
274 string::size_type colon = uri.find(':',ns); 289 string::size_type colon = uri.find(':',ns);
275 if(colon==string::npos) 290 if(colon==string::npos)
276 throw bad_input(OPKELE_CP_ "No scheme specified in URI"); 291 throw bad_input(OPKELE_CP_ "No scheme specified in URI");
277 transform( 292 transform(
278 uri.begin()+ns, uri.begin()+colon+1, 293 uri.begin()+ns, uri.begin()+colon+1,
279 back_inserter(rv), ::tolower ); 294 back_inserter(rv), ::tolower );