195 } |
195 } |
196 return NULL; |
196 return NULL; |
197 } |
197 } |
198 |
198 |
199 |
199 |
200 |
200 char* util_base64decode(char *in) { |
201 char* util_base64decode(char* in) { |
201 int len = 0; |
|
202 return util_base64decode_len(in, &len); |
|
203 } |
|
204 |
|
205 char* util_base64decode_len(char* in, int *outlen) { |
202 size_t len = strlen(in); |
206 size_t len = strlen(in); |
203 char *out = calloc(1, len); |
207 char *out = calloc(1, len); |
204 |
208 |
205 BIO* b = BIO_new_mem_buf(in, len); |
209 BIO* b = BIO_new_mem_buf(in, len); |
206 BIO *d = BIO_new(BIO_f_base64()); |
210 BIO *d = BIO_new(BIO_f_base64()); |
207 BIO_set_flags(d, BIO_FLAGS_BASE64_NO_NL); |
211 BIO_set_flags(d, BIO_FLAGS_BASE64_NO_NL); |
208 b = BIO_push(d, b); |
212 b = BIO_push(d, b); |
209 |
213 |
210 BIO_read(b, out, len); |
214 *outlen = BIO_read(b, out, len); |
211 BIO_free_all(b); |
215 BIO_free_all(b); |
212 |
216 |
|
217 return out; |
|
218 } |
|
219 |
|
220 char* util_base64encode(char *in, size_t len) { |
|
221 BIO *b; |
|
222 BIO *e; |
|
223 BUF_MEM *mem; |
|
224 |
|
225 e = BIO_new(BIO_f_base64()); |
|
226 b = BIO_new(BIO_s_mem()); |
|
227 |
|
228 e = BIO_push(e, b); |
|
229 BIO_write(e, in, len); |
|
230 BIO_flush(e); |
|
231 |
|
232 BIO_get_mem_ptr(e, &mem); |
|
233 char *out = malloc(mem->length); |
|
234 memcpy(out, mem->data, mem->length -1); |
|
235 out[mem->length - 1] = '\0'; |
|
236 |
|
237 BIO_free_all(e); |
|
238 |
213 return out; |
239 return out; |
214 } |
240 } |
215 |
241 |
216 /* |
242 /* |
217 * gets a substring from 0 to the appearance of the token |
243 * gets a substring from 0 to the appearance of the token |