-rw-r--r-- | lib/util.cc | 21 |
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 | |||
@@ -61,155 +61,170 @@ namespace opkele { | |||
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; |