author | Michael Krelin <hacker@klever.net> | 2008-06-25 21:37:52 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-06-25 21:37:52 (UTC) |
commit | 026a467b0e1db2d90ad1b4d0be6511c777da39e7 (patch) (unidiff) | |
tree | 0c853e8df4b5c6cfa8c31e6092c05692c8aeddbb /lib/util.cc | |
parent | 903f7b8108cdb9d1340267f456f23815ead5e807 (diff) | |
download | libopkele-026a467b0e1db2d90ad1b4d0be6511c777da39e7.zip libopkele-026a467b0e1db2d90ad1b4d0be6511c777da39e7.tar.gz libopkele-026a467b0e1db2d90ad1b4d0be6511c777da39e7.tar.bz2 |
gave up the most stupid bug contest award
Thanks to compiler for spotting it.
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | lib/util.cc | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/lib/util.cc b/lib/util.cc index 3e7f3aa..b702291 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -80,260 +80,260 @@ namespace opkele { | |||
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 | time_t w3c_to_time(const string& w) { | 125 | time_t w3c_to_time(const string& w) { |
126 | int fraction; | 126 | int fraction; |
127 | struct tm tm_t; | 127 | struct tm tm_t; |
128 | memset(&tm_t,0,sizeof(tm_t)); | 128 | memset(&tm_t,0,sizeof(tm_t)); |
129 | if( ( | 129 | if( ( |
130 | sscanf( | 130 | sscanf( |
131 | w.c_str(), | 131 | w.c_str(), |
132 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 132 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
133 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 133 | &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 | 134 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec |
135 | ) != 6 | 135 | ) != 6 |
136 | ) && ( | 136 | ) && ( |
137 | sscanf( | 137 | sscanf( |
138 | w.c_str(), | 138 | w.c_str(), |
139 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", | 139 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", |
140 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 140 | &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, | 141 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, |
142 | &fraction | 142 | &fraction |
143 | ) != 7 | 143 | ) != 7 |
144 | ) ) | 144 | ) ) |
145 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 145 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
146 | tm_t.tm_mon--; | 146 | tm_t.tm_mon--; |
147 | tm_t.tm_year-=1900; | 147 | tm_t.tm_year-=1900; |
148 | time_t rv = mktime(&tm_t); | 148 | time_t rv = mktime(&tm_t); |
149 | if(rv==(time_t)-1) | 149 | if(rv==(time_t)-1) |
150 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 150 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); |
151 | return rv-timezone; | 151 | return rv-timezone; |
152 | } | 152 | } |
153 | 153 | ||
154 | /* | 154 | /* |
155 | * | 155 | * |
156 | */ | 156 | */ |
157 | 157 | ||
158 | static inline bool isrfc3986unreserved(int c) { | 158 | static inline bool isrfc3986unreserved(int c) { |
159 | if(c<'-') return false; | 159 | if(c<'-') return false; |
160 | if(c<='.') return true; | 160 | if(c<='.') return true; |
161 | if(c<'0') return false; if(c<='9') return true; | 161 | if(c<'0') return false; if(c<='9') return true; |
162 | if(c<'A') return false; if(c<='Z') return true; | 162 | if(c<'A') return false; if(c<='Z') return true; |
163 | if(c<'_') return false; | 163 | if(c<'_') return false; |
164 | if(c=='_') return true; | 164 | if(c=='_') return true; |
165 | if(c<'a') return false; if(c<='z') return true; | 165 | if(c<'a') return false; if(c<='z') return true; |
166 | if(c=='~') return true; | 166 | if(c=='~') return true; |
167 | return false; | 167 | return false; |
168 | } | 168 | } |
169 | 169 | ||
170 | struct __url_encoder : public unary_function<char,void> { | 170 | struct __url_encoder : public unary_function<char,void> { |
171 | public: | 171 | public: |
172 | string& rv; | 172 | string& rv; |
173 | 173 | ||
174 | __url_encoder(string& r) : rv(r) { } | 174 | __url_encoder(string& r) : rv(r) { } |
175 | 175 | ||
176 | result_type operator()(argument_type c) { | 176 | result_type operator()(argument_type c) { |
177 | if(isrfc3986unreserved(c)) | 177 | if(isrfc3986unreserved(c)) |
178 | rv += c; | 178 | rv += c; |
179 | else{ | 179 | else{ |
180 | char tmp[4]; | 180 | char tmp[4]; |
181 | snprintf(tmp,sizeof(tmp),"%%%02X", | 181 | snprintf(tmp,sizeof(tmp),"%%%02X", |
182 | (c&0xff)); | 182 | (c&0xff)); |
183 | rv += tmp; | 183 | rv += tmp; |
184 | } | 184 | } |
185 | } | 185 | } |
186 | }; | 186 | }; |
187 | 187 | ||
188 | string url_encode(const string& str) { | 188 | string url_encode(const string& str) { |
189 | string rv; | 189 | string rv; |
190 | for_each(str.begin(),str.end(), | 190 | for_each(str.begin(),str.end(), |
191 | __url_encoder(rv)); | 191 | __url_encoder(rv)); |
192 | return rv; | 192 | return rv; |
193 | } | 193 | } |
194 | 194 | ||
195 | string url_decode(const string& str) { | 195 | string url_decode(const string& str) { |
196 | string rv; | 196 | string rv; |
197 | back_insert_iterator<string> ii(rv); | 197 | back_insert_iterator<string> ii(rv); |
198 | for(string::const_iterator i=str.begin(),ie=str.end(); | 198 | for(string::const_iterator i=str.begin(),ie=str.end(); |
199 | i!=ie;++i) { | 199 | i!=ie;++i) { |
200 | switch(*i) { | 200 | switch(*i) { |
201 | case '+': | 201 | case '+': |
202 | *(ii++) = ' '; break; | 202 | *(ii++) = ' '; break; |
203 | case '%': | 203 | case '%': |
204 | ++i; | 204 | ++i; |
205 | static char tmp[3] = {0,0,0}; | 205 | static char tmp[3] = {0,0,0}; |
206 | if(i==ie) | 206 | if(i==ie) |
207 | throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string"); | 207 | throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string"); |
208 | tmp[0] == *(i++); | 208 | tmp[0] = *(i++); |
209 | if(i==ie) | 209 | if(i==ie) |
210 | throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string"); | 210 | throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string"); |
211 | tmp[1] == *i; | 211 | tmp[1] = *i; |
212 | if(!(isxdigit(tmp[0]) && isxdigit(tmp[1]))) | 212 | if(!(isxdigit(tmp[0]) && isxdigit(tmp[1]))) |
213 | throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string"); | 213 | throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string"); |
214 | *(ii++) = strtol(tmp,0,16); | 214 | *(ii++) = strtol(tmp,0,16); |
215 | break; | 215 | break; |
216 | default: | 216 | default: |
217 | *(ii++) = *i; break; | 217 | *(ii++) = *i; break; |
218 | } | 218 | } |
219 | } | 219 | } |
220 | return rv; | 220 | return rv; |
221 | } | 221 | } |
222 | 222 | ||
223 | string attr_escape(const string& str) { | 223 | string attr_escape(const string& str) { |
224 | static const char *unsafechars = "<>&\n\"'"; | 224 | static const char *unsafechars = "<>&\n\"'"; |
225 | string rv; | 225 | string rv; |
226 | string::size_type p=0; | 226 | string::size_type p=0; |
227 | while(true) { | 227 | while(true) { |
228 | string::size_type us = str.find_first_of(unsafechars,p); | 228 | string::size_type us = str.find_first_of(unsafechars,p); |
229 | if(us==string::npos) { | 229 | if(us==string::npos) { |
230 | if(p!=str.length()) | 230 | if(p!=str.length()) |
231 | rv.append(str,p,str.length()-p); | 231 | rv.append(str,p,str.length()-p); |
232 | return rv; | 232 | return rv; |
233 | } | 233 | } |
234 | rv.append(str,p,us-p); | 234 | rv.append(str,p,us-p); |
235 | rv += "&#"; | 235 | rv += "&#"; |
236 | rv += long_to_string((long)str[us]); | 236 | rv += long_to_string((long)str[us]); |
237 | rv += ';'; | 237 | rv += ';'; |
238 | p = us+1; | 238 | p = us+1; |
239 | } | 239 | } |
240 | } | 240 | } |
241 | 241 | ||
242 | string long_to_string(long l) { | 242 | string long_to_string(long l) { |
243 | char rv[32]; | 243 | char rv[32]; |
244 | int r=snprintf(rv,sizeof(rv),"%ld",l); | 244 | int r=snprintf(rv,sizeof(rv),"%ld",l); |
245 | if(r<0 || r>=(int)sizeof(rv)) | 245 | if(r<0 || r>=(int)sizeof(rv)) |
246 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); | 246 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); |
247 | return rv; | 247 | return rv; |
248 | } | 248 | } |
249 | 249 | ||
250 | long string_to_long(const string& s) { | 250 | long string_to_long(const string& s) { |
251 | char *endptr = 0; | 251 | char *endptr = 0; |
252 | long rv = strtol(s.c_str(),&endptr,10); | 252 | long rv = strtol(s.c_str(),&endptr,10); |
253 | if((!endptr) || endptr==s.c_str()) | 253 | if((!endptr) || endptr==s.c_str()) |
254 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); | 254 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); |
255 | return rv; | 255 | return rv; |
256 | } | 256 | } |
257 | 257 | ||
258 | /* | 258 | /* |
259 | * Normalize URL according to the rules, described in rfc 3986, section 6 | 259 | * Normalize URL according to the rules, described in rfc 3986, section 6 |
260 | * | 260 | * |
261 | * - uppercase hex triplets (e.g. %ab -> %AB) | 261 | * - uppercase hex triplets (e.g. %ab -> %AB) |
262 | * - lowercase scheme and host | 262 | * - lowercase scheme and host |
263 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, | 263 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, |
264 | * that is - [:alpha:][:digit:]._~- | 264 | * that is - [:alpha:][:digit:]._~- |
265 | * - remove dot segments | 265 | * - remove dot segments |
266 | * - remove empty and default ports | 266 | * - remove empty and default ports |
267 | * - if there's no path component, add '/' | 267 | * - if there's no path component, add '/' |
268 | */ | 268 | */ |
269 | string rfc_3986_normalize_uri(const string& uri) { | 269 | string rfc_3986_normalize_uri(const string& uri) { |
270 | string rv; | 270 | string rv; |
271 | string::size_type ns = uri.find_first_not_of(data::_whitespace_chars); | 271 | string::size_type ns = uri.find_first_not_of(data::_whitespace_chars); |
272 | if(ns==string::npos) | 272 | if(ns==string::npos) |
273 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); | 273 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); |
274 | string::size_type colon = uri.find(':',ns); | 274 | string::size_type colon = uri.find(':',ns); |
275 | if(colon==string::npos) | 275 | if(colon==string::npos) |
276 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); | 276 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); |
277 | transform( | 277 | transform( |
278 | uri.begin()+ns, uri.begin()+colon+1, | 278 | uri.begin()+ns, uri.begin()+colon+1, |
279 | back_inserter(rv), ::tolower ); | 279 | back_inserter(rv), ::tolower ); |
280 | bool s; | 280 | bool s; |
281 | string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1; | 281 | string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1; |
282 | if(ul <= (colon+3)) | 282 | if(ul <= (colon+3)) |
283 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); | 283 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); |
284 | if(uri[colon+1]!='/' || uri[colon+2]!='/') | 284 | if(uri[colon+1]!='/' || uri[colon+2]!='/') |
285 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); | 285 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); |
286 | if(rv=="http:") | 286 | if(rv=="http:") |
287 | s = false; | 287 | s = false; |
288 | else if(rv=="https:") | 288 | else if(rv=="https:") |
289 | s = true; | 289 | s = true; |
290 | else{ | 290 | else{ |
291 | /* TODO: support more schemes. e.g. xri. How do we normalize | 291 | /* TODO: support more schemes. e.g. xri. How do we normalize |
292 | * xri? | 292 | * xri? |
293 | */ | 293 | */ |
294 | rv.append(uri,colon+1,ul-colon-1); | 294 | rv.append(uri,colon+1,ul-colon-1); |
295 | return rv; | 295 | return rv; |
296 | } | 296 | } |
297 | rv += "//"; | 297 | rv += "//"; |
298 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); | 298 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); |
299 | if(interesting==string::npos) { | 299 | if(interesting==string::npos) { |
300 | transform( | 300 | transform( |
301 | uri.begin()+colon+3,uri.begin()+ul, | 301 | uri.begin()+colon+3,uri.begin()+ul, |
302 | back_inserter(rv), ::tolower ); | 302 | back_inserter(rv), ::tolower ); |
303 | rv += '/'; return rv; | 303 | rv += '/'; return rv; |
304 | } | 304 | } |
305 | transform( | 305 | transform( |
306 | uri.begin()+colon+3,uri.begin()+interesting, | 306 | uri.begin()+colon+3,uri.begin()+interesting, |
307 | back_inserter(rv), ::tolower ); | 307 | back_inserter(rv), ::tolower ); |
308 | bool qf = false; | 308 | bool qf = false; |
309 | char ic = uri[interesting]; | 309 | char ic = uri[interesting]; |
310 | if(ic==':') { | 310 | if(ic==':') { |
311 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); | 311 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); |
312 | const char *nptr = uri.data()+interesting+1; | 312 | const char *nptr = uri.data()+interesting+1; |
313 | char *eptr = 0; | 313 | char *eptr = 0; |
314 | long port = strtol(nptr,&eptr,10); | 314 | long port = strtol(nptr,&eptr,10); |
315 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { | 315 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { |
316 | char tmp[8]; | 316 | char tmp[8]; |
317 | snprintf(tmp,sizeof(tmp),":%ld",port); | 317 | snprintf(tmp,sizeof(tmp),":%ld",port); |
318 | rv += tmp; | 318 | rv += tmp; |
319 | } | 319 | } |
320 | if(ni==string::npos) { | 320 | if(ni==string::npos) { |
321 | rv += '/'; return rv; | 321 | rv += '/'; return rv; |
322 | } | 322 | } |
323 | interesting = ni; | 323 | interesting = ni; |
324 | }else if(ic!='/') { | 324 | }else if(ic!='/') { |
325 | rv += '/'; rv += ic; | 325 | rv += '/'; rv += ic; |
326 | qf = true; | 326 | qf = true; |
327 | ++interesting; | 327 | ++interesting; |
328 | } | 328 | } |
329 | string::size_type n = interesting; | 329 | string::size_type n = interesting; |
330 | char tmp[3] = { 0,0,0 }; | 330 | char tmp[3] = { 0,0,0 }; |
331 | stack<string::size_type> psegs; psegs.push(rv.length()); | 331 | stack<string::size_type> psegs; psegs.push(rv.length()); |
332 | string pseg; | 332 | string pseg; |
333 | for(;n<ul;) { | 333 | for(;n<ul;) { |
334 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); | 334 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); |
335 | if(unsafe==string::npos) { | 335 | if(unsafe==string::npos) { |
336 | pseg.append(uri,n,ul-n-1); n = ul-1; | 336 | pseg.append(uri,n,ul-n-1); n = ul-1; |
337 | }else{ | 337 | }else{ |
338 | pseg.append(uri,n,unsafe-n); | 338 | pseg.append(uri,n,unsafe-n); |
339 | n = unsafe; | 339 | n = unsafe; |