-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,163 +1,164 @@ | |||
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 | } |
67 | } | 68 | } |
68 | 69 | ||
69 | /* | 70 | /* |
70 | * big numerics | 71 | * big numerics |
71 | */ | 72 | */ |
72 | 73 | ||
73 | BIGNUM *base64_to_bignum(const string& b64) { | 74 | BIGNUM *base64_to_bignum(const string& b64) { |
74 | vector<unsigned char> bin; | 75 | vector<unsigned char> bin; |
75 | decode_base64(b64,bin); | 76 | decode_base64(b64,bin); |
76 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); | 77 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); |
77 | if(!rv) | 78 | if(!rv) |
78 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); | 79 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); |
79 | return rv; | 80 | return rv; |
80 | } | 81 | } |
81 | 82 | ||
82 | BIGNUM *dec_to_bignum(const string& dec) { | 83 | BIGNUM *dec_to_bignum(const string& dec) { |
83 | BIGNUM *rv = 0; | 84 | BIGNUM *rv = 0; |
84 | if(!BN_dec2bn(&rv,dec.c_str())) | 85 | if(!BN_dec2bn(&rv,dec.c_str())) |
85 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 86 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
86 | return rv; | 87 | return rv; |
87 | } | 88 | } |
88 | 89 | ||
89 | string bignum_to_base64(const BIGNUM *bn) { | 90 | string bignum_to_base64(const BIGNUM *bn) { |
90 | vector<unsigned char> bin(BN_num_bytes(bn)+1); | 91 | vector<unsigned char> bin(BN_num_bytes(bn)+1); |
91 | unsigned char *binptr = &(bin.front())+1; | 92 | unsigned char *binptr = &(bin.front())+1; |
92 | int l = BN_bn2bin(bn,binptr); | 93 | int l = BN_bn2bin(bn,binptr); |
93 | if(l && (*binptr)&0x80){ | 94 | if(l && (*binptr)&0x80){ |
94 | (*(--binptr)) = 0; ++l; | 95 | (*(--binptr)) = 0; ++l; |
95 | } | 96 | } |
96 | return encode_base64(binptr,l); | 97 | return encode_base64(binptr,l); |
97 | } | 98 | } |
98 | 99 | ||
99 | /* | 100 | /* |
100 | * w3c times | 101 | * w3c times |
101 | */ | 102 | */ |
102 | 103 | ||
103 | string time_to_w3c(time_t t) { | 104 | string time_to_w3c(time_t t) { |
104 | struct tm tm_t; | 105 | struct tm tm_t; |
105 | if(!gmtime_r(&t,&tm_t)) | 106 | if(!gmtime_r(&t,&tm_t)) |
106 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 107 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
107 | char rv[25]; | 108 | char rv[25]; |
108 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) | 109 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) |
109 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); | 110 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); |
110 | return rv; | 111 | return rv; |
111 | } | 112 | } |
112 | 113 | ||
113 | time_t w3c_to_time(const string& w) { | 114 | time_t w3c_to_time(const string& w) { |
114 | struct tm tm_t; | 115 | struct tm tm_t; |
115 | memset(&tm_t,0,sizeof(tm_t)); | 116 | memset(&tm_t,0,sizeof(tm_t)); |
116 | if( | 117 | if( |
117 | sscanf( | 118 | sscanf( |
118 | w.c_str(), | 119 | w.c_str(), |
119 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 120 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
120 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 121 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
121 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec | 122 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec |
122 | ) != 6 ) | 123 | ) != 6 ) |
123 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 124 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
124 | tm_t.tm_mon--; | 125 | tm_t.tm_mon--; |
125 | tm_t.tm_year-=1900; | 126 | tm_t.tm_year-=1900; |
126 | time_t rv = mktime(&tm_t); | 127 | time_t rv = mktime(&tm_t); |
127 | if(rv==(time_t)-1) | 128 | if(rv==(time_t)-1) |
128 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 129 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); |
129 | return rv; | 130 | return rv; |
130 | } | 131 | } |
131 | 132 | ||
132 | /* | 133 | /* |
133 | * | 134 | * |
134 | */ | 135 | */ |
135 | 136 | ||
136 | string url_encode(const string& str) { | 137 | string url_encode(const string& str) { |
137 | char * t = curl_escape(str.c_str(),str.length()); | 138 | char * t = curl_escape(str.c_str(),str.length()); |
138 | if(!t) | 139 | if(!t) |
139 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); | 140 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); |
140 | string rv(t); | 141 | string rv(t); |
141 | curl_free(t); | 142 | curl_free(t); |
142 | return rv; | 143 | return rv; |
143 | } | 144 | } |
144 | 145 | ||
145 | string long_to_string(long l) { | 146 | string long_to_string(long l) { |
146 | char rv[32]; | 147 | char rv[32]; |
147 | int r=snprintf(rv,sizeof(rv),"%ld",l); | 148 | int r=snprintf(rv,sizeof(rv),"%ld",l); |
148 | if(r<0 || r>=(int)sizeof(rv)) | 149 | if(r<0 || r>=(int)sizeof(rv)) |
149 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); | 150 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); |
150 | return rv; | 151 | return rv; |
151 | } | 152 | } |
152 | 153 | ||
153 | long string_to_long(const string& s) { | 154 | long string_to_long(const string& s) { |
154 | char *endptr = 0; | 155 | char *endptr = 0; |
155 | long rv = strtol(s.c_str(),&endptr,10); | 156 | long rv = strtol(s.c_str(),&endptr,10); |
156 | if((!endptr) || endptr==s.c_str()) | 157 | if((!endptr) || endptr==s.c_str()) |
157 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); | 158 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); |
158 | return rv; | 159 | return rv; |
159 | } | 160 | } |
160 | 161 | ||
161 | } | 162 | } |
162 | 163 | ||
163 | } | 164 | } |