summaryrefslogtreecommitdiffabout
path: root/lib/util.cc
Unidiff
Diffstat (limited to 'lib/util.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/util.cc1
1 files changed, 1 insertions, 0 deletions
diff --git a/lib/util.cc b/lib/util.cc
index 94e09ed..26be66a 100644
--- a/lib/util.cc
+++ b/lib/util.cc
@@ -1,50 +1,51 @@
1#include <errno.h> 1#include <errno.h>
2#include <cassert> 2#include <cassert>
3#include <cstring>
3#include <vector> 4#include <vector>
4#include <string> 5#include <string>
5#include <openssl/bio.h> 6#include <openssl/bio.h>
6#include <openssl/evp.h> 7#include <openssl/evp.h>
7#include <curl/curl.h> 8#include <curl/curl.h>
8#include "opkele/util.h" 9#include "opkele/util.h"
9#include "opkele/exception.h" 10#include "opkele/exception.h"
10 11
11namespace opkele { 12namespace opkele {
12 using namespace std; 13 using namespace std;
13 14
14 namespace util { 15 namespace util {
15 16
16 /* 17 /*
17 * base64 18 * base64
18 */ 19 */
19 string encode_base64(const void *data,size_t length) { 20 string encode_base64(const void *data,size_t length) {
20 BIO *b64 = 0, *bmem = 0; 21 BIO *b64 = 0, *bmem = 0;
21 try { 22 try {
22 b64 = BIO_new(BIO_f_base64()); 23 b64 = BIO_new(BIO_f_base64());
23 if(!b64) 24 if(!b64)
24 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); 25 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder");
25 BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); 26 BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
26 bmem = BIO_new(BIO_s_mem()); 27 bmem = BIO_new(BIO_s_mem());
27 BIO_set_flags(b64,BIO_CLOSE); 28 BIO_set_flags(b64,BIO_CLOSE);
28 if(!bmem) 29 if(!bmem)
29 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); 30 throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer");
30 BIO_push(b64,bmem); 31 BIO_push(b64,bmem);
31 if(((size_t)BIO_write(b64,data,length))!=length) 32 if(((size_t)BIO_write(b64,data,length))!=length)
32 throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); 33 throw exception_openssl(OPKELE_CP_ "failed to BIO_write()");
33 if(BIO_flush(b64)!=1) 34 if(BIO_flush(b64)!=1)
34 throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); 35 throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()");
35 char *rvd; 36 char *rvd;
36 long rvl = BIO_get_mem_data(bmem,&rvd); 37 long rvl = BIO_get_mem_data(bmem,&rvd);
37 string rv(rvd,rvl); 38 string rv(rvd,rvl);
38 BIO_free_all(b64); 39 BIO_free_all(b64);
39 return rv; 40 return rv;
40 }catch(...) { 41 }catch(...) {
41 if(b64) BIO_free_all(b64); 42 if(b64) BIO_free_all(b64);
42 throw; 43 throw;
43 } 44 }
44 } 45 }
45 46
46 void decode_base64(const string& data,vector<unsigned char>& rv) { 47 void decode_base64(const string& data,vector<unsigned char>& rv) {
47 BIO *b64 = 0, *bmem = 0; 48 BIO *b64 = 0, *bmem = 0;
48 rv.clear(); 49 rv.clear();
49 try { 50 try {
50 bmem = BIO_new_mem_buf((void*)data.data(),data.size()); 51 bmem = BIO_new_mem_buf((void*)data.data(),data.size());