summaryrefslogtreecommitdiffabout
path: root/lib/util.cc
authorMichael Krelin <hacker@klever.net>2008-01-20 21:08:05 (UTC)
committer Michael Krelin <hacker@klever.net>2008-01-20 21:08:05 (UTC)
commit9bfb6fadf71c46bf4cb5adabba0c96c32e84c1bc (patch) (unidiff)
tree702473142242e80538c4801cc379ec98fba199dd /lib/util.cc
parent395a126cbf59b7a50f44da3096b68bab412ab33d (diff)
downloadlibopkele-9bfb6fadf71c46bf4cb5adabba0c96c32e84c1bc.zip
libopkele-9bfb6fadf71c46bf4cb5adabba0c96c32e84c1bc.tar.gz
libopkele-9bfb6fadf71c46bf4cb5adabba0c96c32e84c1bc.tar.bz2
the whole library rewritten
Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (limited to 'lib/util.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/util.cc71
1 files changed, 69 insertions, 2 deletions
diff --git a/lib/util.cc b/lib/util.cc
index a9b9bed..54d6535 100644
--- a/lib/util.cc
+++ b/lib/util.cc
@@ -1,316 +1,383 @@
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 <openssl/bio.h> 8#include <openssl/bio.h>
9#include <openssl/evp.h> 9#include <openssl/evp.h>
10#include <openssl/hmac.h>
10#include <curl/curl.h> 11#include <curl/curl.h>
11#include "opkele/util.h" 12#include "opkele/util.h"
12#include "opkele/exception.h" 13#include "opkele/exception.h"
13 14
15#include <config.h>
16#ifdef HAVE_DEMANGLE
17# include <cxxabi.h>
18#endif
19
14namespace opkele { 20namespace opkele {
15 using namespace std; 21 using namespace std;
16 22
17 namespace util { 23 namespace util {
18 24
19 /* 25 /*
20 * base64 26 * base64
21 */ 27 */
22 string encode_base64(const void *data,size_t length) { 28 string encode_base64(const void *data,size_t length) {
23 BIO *b64 = 0, *bmem = 0; 29 BIO *b64 = 0, *bmem = 0;
24 try { 30 try {
25 b64 = BIO_new(BIO_f_base64()); 31 b64 = BIO_new(BIO_f_base64());
26 if(!b64) 32 if(!b64)
27 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); 33 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder");
28 BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); 34 BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
29 bmem = BIO_new(BIO_s_mem()); 35 bmem = BIO_new(BIO_s_mem());
30 BIO_set_flags(b64,BIO_CLOSE); 36 BIO_set_flags(b64,BIO_CLOSE);
31 if(!bmem) 37 if(!bmem)
32 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); 38 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer");
33 BIO_push(b64,bmem); 39 BIO_push(b64,bmem);
34 if(((size_t)BIO_write(b64,data,length))!=length) 40 if(((size_t)BIO_write(b64,data,length))!=length)
35 throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); 41 throw exception_openssl(OPKELE_CP_ "failed to BIO_write()");
36 if(BIO_flush(b64)!=1) 42 if(BIO_flush(b64)!=1)
37 throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); 43 throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()");
38 char *rvd; 44 char *rvd;
39 long rvl = BIO_get_mem_data(bmem,&rvd); 45 long rvl = BIO_get_mem_data(bmem,&rvd);
40 string rv(rvd,rvl); 46 string rv(rvd,rvl);
41 BIO_free_all(b64); 47 BIO_free_all(b64);
42 return rv; 48 return rv;
43 }catch(...) { 49 }catch(...) {
44 if(b64) BIO_free_all(b64); 50 if(b64) BIO_free_all(b64);
45 throw; 51 throw;
46 } 52 }
47 } 53 }
48 54
49 void decode_base64(const string& data,vector<unsigned char>& rv) { 55 void decode_base64(const string& data,vector<unsigned char>& rv) {
50 BIO *b64 = 0, *bmem = 0; 56 BIO *b64 = 0, *bmem = 0;
51 rv.clear(); 57 rv.clear();
52 try { 58 try {
53 bmem = BIO_new_mem_buf((void*)data.data(),data.size()); 59 bmem = BIO_new_mem_buf((void*)data.data(),data.size());
54 if(!bmem) 60 if(!bmem)
55 throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); 61 throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()");
56 b64 = BIO_new(BIO_f_base64()); 62 b64 = BIO_new(BIO_f_base64());
57 if(!b64) 63 if(!b64)
58 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); 64 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder");
59 BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); 65 BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
60 BIO_push(b64,bmem); 66 BIO_push(b64,bmem);
61 unsigned char tmp[512]; 67 unsigned char tmp[512];
62 size_t rb = 0; 68 size_t rb = 0;
63 while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) 69 while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0)
64 rv.insert(rv.end(),tmp,&tmp[rb]); 70 rv.insert(rv.end(),tmp,&tmp[rb]);
65 BIO_free_all(b64); 71 BIO_free_all(b64);
66 }catch(...) { 72 }catch(...) {
67 if(b64) BIO_free_all(b64); 73 if(b64) BIO_free_all(b64);
68 throw; 74 throw;
69 } 75 }
70 } 76 }
71 77
72 /* 78 /*
73 * big numerics 79 * big numerics
74 */ 80 */
75 81
76 BIGNUM *base64_to_bignum(const string& b64) { 82 BIGNUM *base64_to_bignum(const string& b64) {
77 vector<unsigned char> bin; 83 vector<unsigned char> bin;
78 decode_base64(b64,bin); 84 decode_base64(b64,bin);
79 BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); 85 BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0);
80 if(!rv) 86 if(!rv)
81 throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); 87 throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()");
82 return rv; 88 return rv;
83 } 89 }
84 90
85 BIGNUM *dec_to_bignum(const string& dec) { 91 BIGNUM *dec_to_bignum(const string& dec) {
86 BIGNUM *rv = 0; 92 BIGNUM *rv = 0;
87 if(!BN_dec2bn(&rv,dec.c_str())) 93 if(!BN_dec2bn(&rv,dec.c_str()))
88 throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); 94 throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()");
89 return rv; 95 return rv;
90 } 96 }
91 97
92 string bignum_to_base64(const BIGNUM *bn) { 98 string bignum_to_base64(const BIGNUM *bn) {
93 vector<unsigned char> bin(BN_num_bytes(bn)+1); 99 vector<unsigned char> bin(BN_num_bytes(bn)+1);
94 unsigned char *binptr = &(bin.front())+1; 100 unsigned char *binptr = &(bin.front())+1;
95 int l = BN_bn2bin(bn,binptr); 101 int l = BN_bn2bin(bn,binptr);
96 if(l && (*binptr)&0x80){ 102 if(l && (*binptr)&0x80){
97 (*(--binptr)) = 0; ++l; 103 (*(--binptr)) = 0; ++l;
98 } 104 }
99 return encode_base64(binptr,l); 105 return encode_base64(binptr,l);
100 } 106 }
101 107
102 /* 108 /*
103 * w3c times 109 * w3c times
104 */ 110 */
105 111
106 string time_to_w3c(time_t t) { 112 string time_to_w3c(time_t t) {
107 struct tm tm_t; 113 struct tm tm_t;
108 if(!gmtime_r(&t,&tm_t)) 114 if(!gmtime_r(&t,&tm_t))
109 throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); 115 throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()");
110 char rv[25]; 116 char rv[25];
111 if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) 117 if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t))
112 throw failed_conversion(OPKELE_CP_ "failed to strftime()"); 118 throw failed_conversion(OPKELE_CP_ "failed to strftime()");
113 return rv; 119 return rv;
114 } 120 }
115 121
116 time_t w3c_to_time(const string& w) { 122 time_t w3c_to_time(const string& w) {
117 int fraction; 123 int fraction;
118 struct tm tm_t; 124 struct tm tm_t;
119 memset(&tm_t,0,sizeof(tm_t)); 125 memset(&tm_t,0,sizeof(tm_t));
120 if( ( 126 if( (
121 sscanf( 127 sscanf(
122 w.c_str(), 128 w.c_str(),
123 "%04d-%02d-%02dT%02d:%02d:%02dZ", 129 "%04d-%02d-%02dT%02d:%02d:%02dZ",
124 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, 130 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday,
125 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec 131 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec
126 ) != 6 132 ) != 6
127 ) && ( 133 ) && (
128 sscanf( 134 sscanf(
129 w.c_str(), 135 w.c_str(),
130 "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", 136 "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
131 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, 137 &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday,
132 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, 138 &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec,
133 &fraction 139 &fraction
134 ) != 7 140 ) != 7
135 ) ) 141 ) )
136 throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); 142 throw failed_conversion(OPKELE_CP_ "failed to sscanf()");
137 tm_t.tm_mon--; 143 tm_t.tm_mon--;
138 tm_t.tm_year-=1900; 144 tm_t.tm_year-=1900;
139 time_t rv = mktime(&tm_t); 145 time_t rv = mktime(&tm_t);
140 if(rv==(time_t)-1) 146 if(rv==(time_t)-1)
141 throw failed_conversion(OPKELE_CP_ "failed to mktime()"); 147 throw failed_conversion(OPKELE_CP_ "failed to mktime()");
142 return rv-timezone; 148 return rv-timezone;
143 } 149 }
144 150
145 /* 151 /*
146 * 152 *
147 */ 153 */
148 154
149 string url_encode(const string& str) { 155 string url_encode(const string& str) {
150 char * t = curl_escape(str.c_str(),str.length()); 156 char * t = curl_escape(str.c_str(),str.length());
151 if(!t) 157 if(!t)
152 throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); 158 throw failed_conversion(OPKELE_CP_ "failed to curl_escape()");
153 string rv(t); 159 string rv(t);
154 curl_free(t); 160 curl_free(t);
155 return rv; 161 return rv;
156 } 162 }
157 163
158 string long_to_string(long l) { 164 string long_to_string(long l) {
159 char rv[32]; 165 char rv[32];
160 int r=snprintf(rv,sizeof(rv),"%ld",l); 166 int r=snprintf(rv,sizeof(rv),"%ld",l);
161 if(r<0 || r>=(int)sizeof(rv)) 167 if(r<0 || r>=(int)sizeof(rv))
162 throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); 168 throw failed_conversion(OPKELE_CP_ "failed to snprintf()");
163 return rv; 169 return rv;
164 } 170 }
165 171
166 long string_to_long(const string& s) { 172 long string_to_long(const string& s) {
167 char *endptr = 0; 173 char *endptr = 0;
168 long rv = strtol(s.c_str(),&endptr,10); 174 long rv = strtol(s.c_str(),&endptr,10);
169 if((!endptr) || endptr==s.c_str()) 175 if((!endptr) || endptr==s.c_str())
170 throw failed_conversion(OPKELE_CP_ "failed to strtol()"); 176 throw failed_conversion(OPKELE_CP_ "failed to strtol()");
171 return rv; 177 return rv;
172 } 178 }
173 179
174 /* 180 /*
175 * Normalize URL according to the rules, described in rfc 3986, section 6 181 * Normalize URL according to the rules, described in rfc 3986, section 6
176 * 182 *
177 * - uppercase hex triplets (e.g. %ab -> %AB) 183 * - uppercase hex triplets (e.g. %ab -> %AB)
178 * - lowercase scheme and host 184 * - lowercase scheme and host
179 * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, 185 * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3,
180 * that is - [:alpha:][:digit:]._~- 186 * that is - [:alpha:][:digit:]._~-
181 * - remove dot segments 187 * - remove dot segments
182 * - remove empty and default ports 188 * - remove empty and default ports
183 * - if there's no path component, add '/' 189 * - if there's no path component, add '/'
184 */ 190 */
185 string rfc_3986_normalize_uri(const string& uri) { 191 string rfc_3986_normalize_uri(const string& uri) {
186 static const char *whitespace = " \t\r\n"; 192 static const char *whitespace = " \t\r\n";
187 string rv; 193 string rv;
188 string::size_type ns = uri.find_first_not_of(whitespace); 194 string::size_type ns = uri.find_first_not_of(whitespace);
189 if(ns==string::npos) 195 if(ns==string::npos)
190 throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); 196 throw bad_input(OPKELE_CP_ "Can't normalize empty URI");
191 string::size_type colon = uri.find(':',ns); 197 string::size_type colon = uri.find(':',ns);
192 if(colon==string::npos) 198 if(colon==string::npos)
193 throw bad_input(OPKELE_CP_ "No scheme specified in URI"); 199 throw bad_input(OPKELE_CP_ "No scheme specified in URI");
194 transform( 200 transform(
195 uri.begin()+ns, uri.begin()+colon+1, 201 uri.begin()+ns, uri.begin()+colon+1,
196 back_inserter(rv), ::tolower ); 202 back_inserter(rv), ::tolower );
197 bool s; 203 bool s;
198 string::size_type ul = uri.find_last_not_of(whitespace)+1; 204 string::size_type ul = uri.find_last_not_of(whitespace)+1;
199 if(ul <= (colon+3)) 205 if(ul <= (colon+3))
200 throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); 206 throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered");
201 if(uri[colon+1]!='/' || uri[colon+2]!='/') 207 if(uri[colon+1]!='/' || uri[colon+2]!='/')
202 throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); 208 throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component");
203 if(rv=="http:") 209 if(rv=="http:")
204 s = false; 210 s = false;
205 else if(rv=="https:") 211 else if(rv=="https:")
206 s = true; 212 s = true;
207 else{ 213 else{
208 /* TODO: support more schemes. 214 /* TODO: support more schemes. e.g. xri. How do we normalize
209 * e.g. xri. How do we normalize
210 * xri? 215 * xri?
211 */ 216 */
212 rv.append(uri,colon+1,ul-colon-1); 217 rv.append(uri,colon+1,ul-colon-1);
213 return rv; 218 return rv;
214 } 219 }
215 rv += "//"; 220 rv += "//";
216 string::size_type interesting = uri.find_first_of(":/#?",colon+3); 221 string::size_type interesting = uri.find_first_of(":/#?",colon+3);
217 if(interesting==string::npos) { 222 if(interesting==string::npos) {
218 transform( 223 transform(
219 uri.begin()+colon+3,uri.begin()+ul, 224 uri.begin()+colon+3,uri.begin()+ul,
220 back_inserter(rv), ::tolower ); 225 back_inserter(rv), ::tolower );
221 rv += '/'; return rv; 226 rv += '/'; return rv;
222 } 227 }
223 transform( 228 transform(
224 uri.begin()+colon+3,uri.begin()+interesting, 229 uri.begin()+colon+3,uri.begin()+interesting,
225 back_inserter(rv), ::tolower ); 230 back_inserter(rv), ::tolower );
226 bool qf = false; 231 bool qf = false;
227 char ic = uri[interesting]; 232 char ic = uri[interesting];
228 if(ic==':') { 233 if(ic==':') {
229 string::size_type ni = uri.find_first_of("/#?%",interesting+1); 234 string::size_type ni = uri.find_first_of("/#?%",interesting+1);
230 const char *nptr = uri.data()+interesting+1; 235 const char *nptr = uri.data()+interesting+1;
231 char *eptr = 0; 236 char *eptr = 0;
232 long port = strtol(nptr,&eptr,10); 237 long port = strtol(nptr,&eptr,10);
233 if( (port>0) && (port<65535) && port!=(s?443:80) ) { 238 if( (port>0) && (port<65535) && port!=(s?443:80) ) {
234 char tmp[8]; 239 char tmp[8];
235 snprintf(tmp,sizeof(tmp),":%ld",port); 240 snprintf(tmp,sizeof(tmp),":%ld",port);
236 rv += tmp; 241 rv += tmp;
237 } 242 }
238 if(ni==string::npos) { 243 if(ni==string::npos) {
239 rv += '/'; return rv; 244 rv += '/'; return rv;
240 } 245 }
241 interesting = ni; 246 interesting = ni;
242 }else if(ic!='/') { 247 }else if(ic!='/') {
243 rv += '/'; rv += ic; 248 rv += '/'; rv += ic;
244 qf = true; 249 qf = true;
245 ++interesting; 250 ++interesting;
246 } 251 }
247 string::size_type n = interesting; 252 string::size_type n = interesting;
248 char tmp[3] = { 0,0,0 }; 253 char tmp[3] = { 0,0,0 };
249 stack<string::size_type> psegs; psegs.push(rv.length()); 254 stack<string::size_type> psegs; psegs.push(rv.length());
250 string pseg; 255 string pseg;
251 for(;n<ul;) { 256 for(;n<ul;) {
252 string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); 257 string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n);
253 if(unsafe==string::npos) { 258 if(unsafe==string::npos) {
254 pseg.append(uri,n,ul-n-1); n = ul-1; 259 pseg.append(uri,n,ul-n-1); n = ul-1;
255 }else{ 260 }else{
256 pseg.append(uri,n,unsafe-n); 261 pseg.append(uri,n,unsafe-n);
257 n = unsafe; 262 n = unsafe;
258 } 263 }
259 char c = uri[n++]; 264 char c = uri[n++];
260 if(c=='%') { 265 if(c=='%') {
261 if((n+1)>=ul) 266 if((n+1)>=ul)
262 throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); 267 throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character");
263 tmp[0] = uri[n++]; 268 tmp[0] = uri[n++];
264 tmp[1] = uri[n++]; 269 tmp[1] = uri[n++];
265 if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) 270 if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) ))
266 throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); 271 throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized");
267 int cc = strtol(tmp,0,16); 272 int cc = strtol(tmp,0,16);
268 if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) 273 if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) )
269 pseg += cc; 274 pseg += cc;
270 else{ 275 else{
271 pseg += '%'; 276 pseg += '%';
272 pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); 277 pseg += toupper(tmp[0]); pseg += toupper(tmp[1]);
273 } 278 }
274 }else if(qf) { 279 }else if(qf) {
275 rv += pseg; rv += c; 280 rv += pseg; rv += c;
276 pseg.clear(); 281 pseg.clear();
277 }else if(n>=ul || strchr("?/#",c)) { 282 }else if(n>=ul || strchr("?/#",c)) {
278 if(pseg.empty() || pseg==".") { 283 if(pseg.empty() || pseg==".") {
279 }else if(pseg=="..") { 284 }else if(pseg=="..") {
280 if(psegs.size()>1) { 285 if(psegs.size()>1) {
281 rv.resize(psegs.top()); psegs.pop(); 286 rv.resize(psegs.top()); psegs.pop();
282 } 287 }
283 }else{ 288 }else{
284 psegs.push(rv.length()); 289 psegs.push(rv.length());
285 if(c!='/') { 290 if(c!='/') {
286 pseg += c; 291 pseg += c;
287 qf = true; 292 qf = true;
288 } 293 }
289 rv += '/'; rv += pseg; 294 rv += '/'; rv += pseg;
290 } 295 }
291 if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { 296 if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) {
292 rv += '/'; 297 rv += '/';
293 if(n<ul) 298 if(n<ul)
294 qf = true; 299 qf = true;
295 }else if(strchr("?#",c)) { 300 }else if(strchr("?#",c)) {
296 if(psegs.size()==1 && psegs.top()==rv.length()) 301 if(psegs.size()==1 && psegs.top()==rv.length())
297 rv += '/'; 302 rv += '/';
298 if(pseg.empty()) 303 if(pseg.empty())
299 rv += c; 304 rv += c;
300 qf = true; 305 qf = true;
301 } 306 }
302 pseg.clear(); 307 pseg.clear();
303 }else{ 308 }else{
304 pseg += c; 309 pseg += c;
305 } 310 }
306 } 311 }
307 if(!pseg.empty()) { 312 if(!pseg.empty()) {
308 if(!qf) rv += '/'; 313 if(!qf) rv += '/';
309 rv += pseg; 314 rv += pseg;
310 } 315 }
311 return rv; 316 return rv;
312 } 317 }
313 318
319 string& strip_uri_fragment_part(string& u) {
320 string::size_type q = u.find('?'), f = u.find('#');
321 if(q==string::npos) {
322 if(f!=string::npos)
323 u.erase(f);
324 }else{
325 if(f!=string::npos) {
326 if(f<q)
327 u.erase(f,q-f);
328 else
329 u.erase(f);
330 }
331 }
332 return u;
333 }
334
335 string abi_demangle(const char *mn) {
336#ifndef HAVE_DEMANGLE
337 return mn;
338#else /* !HAVE_DEMANGLE */
339 int dstat;
340 char *demangled = abi::__cxa_demangle(mn,0,0,&dstat);
341 if(dstat)
342 return mn;
343 string rv = demangled;
344 free(demangled);
345 return rv;
346#endif /* !HAVE_DEMANGLE */
347 }
348
349 string base64_signature(const assoc_t& assoc,const basic_openid_message& om) {
350 const string& slist = om.get_field("signed");
351 string kv;
352 string::size_type p=0;
353 while(true) {
354 string::size_type co = slist.find(',',p);
355 string f = (co==string::npos)
356 ?slist.substr(p):slist.substr(p,co-p);
357 kv += f;
358 kv += ':';
359 kv += om.get_field(f);
360 kv += '\n';
361 if(co==string::npos) break;
362 p = co+1;
363 }
364 const secret_t& secret = assoc->secret();
365 const EVP_MD *evpmd;
366 const string& at = assoc->assoc_type();
367 if(at=="HMAC-SHA256")
368 evpmd = EVP_sha256();
369 else if(at=="HMAC-SHA1")
370 evpmd = EVP_sha1();
371 else
372 throw unsupported(OPKELE_CP_ "unknown association type");
373 unsigned int md_len = 0;
374 unsigned char *md = HMAC(evpmd,
375 &(secret.front()),secret.size(),
376 (const unsigned char*)kv.data(),kv.length(),
377 0,&md_len);
378 return encode_base64(md,md_len);
379 }
380
314 } 381 }
315 382
316} 383}