-rw-r--r-- | lib/util.cc | 1 |
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,66 +1,67 @@ | |||
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 | ||
11 | namespace opkele { | 12 | namespace 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()); |
51 | if(!bmem) | 52 | if(!bmem) |
52 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); | 53 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); |
53 | b64 = BIO_new(BIO_f_base64()); | 54 | b64 = BIO_new(BIO_f_base64()); |
54 | if(!b64) | 55 | if(!b64) |
55 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); | 56 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); |
56 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 57 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
57 | BIO_push(b64,bmem); | 58 | BIO_push(b64,bmem); |
58 | unsigned char tmp[512]; | 59 | unsigned char tmp[512]; |
59 | size_t rb = 0; | 60 | size_t rb = 0; |
60 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) | 61 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) |
61 | rv.insert(rv.end(),tmp,&tmp[rb]); | 62 | rv.insert(rv.end(),tmp,&tmp[rb]); |
62 | BIO_free_all(b64); | 63 | BIO_free_all(b64); |
63 | }catch(...) { | 64 | }catch(...) { |
64 | if(b64) BIO_free_all(b64); | 65 | if(b64) BIO_free_all(b64); |
65 | throw; | 66 | throw; |
66 | } | 67 | } |