author | Michael Krelin <hacker@klever.net> | 2007-11-21 12:09:06 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2007-11-21 12:09:06 (UTC) |
commit | d31def2b6dc0fb5c4a17918d608d9f5107372a74 (patch) (unidiff) | |
tree | a52663a3db03f75d647e6dfea5fe173cdd37d528 /lib/util.cc | |
parent | f0877a6af785a36ef769114816c71a838295d902 (diff) | |
download | libopkele-d31def2b6dc0fb5c4a17918d608d9f5107372a74.zip libopkele-d31def2b6dc0fb5c4a17918d608d9f5107372a74.tar.gz libopkele-d31def2b6dc0fb5c4a17918d608d9f5107372a74.tar.bz2 |
keep compiler happy and check more error conditions
Thanks Marcus Rueckert for pointing this out
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | lib/util.cc | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/lib/util.cc b/lib/util.cc index 94f6f53..94e09ed 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -1,162 +1,163 @@ | |||
1 | #include <errno.h> | 1 | #include <errno.h> |
2 | #include <cassert> | 2 | #include <cassert> |
3 | #include <vector> | 3 | #include <vector> |
4 | #include <string> | 4 | #include <string> |
5 | #include <openssl/bio.h> | 5 | #include <openssl/bio.h> |
6 | #include <openssl/evp.h> | 6 | #include <openssl/evp.h> |
7 | #include <curl/curl.h> | 7 | #include <curl/curl.h> |
8 | #include "opkele/util.h" | 8 | #include "opkele/util.h" |
9 | #include "opkele/exception.h" | 9 | #include "opkele/exception.h" |
10 | 10 | ||
11 | namespace opkele { | 11 | namespace opkele { |
12 | using namespace std; | 12 | using namespace std; |
13 | 13 | ||
14 | namespace util { | 14 | namespace util { |
15 | 15 | ||
16 | /* | 16 | /* |
17 | * base64 | 17 | * base64 |
18 | */ | 18 | */ |
19 | string encode_base64(const void *data,size_t length) { | 19 | string encode_base64(const void *data,size_t length) { |
20 | BIO *b64 = 0, *bmem = 0; | 20 | BIO *b64 = 0, *bmem = 0; |
21 | try { | 21 | try { |
22 | b64 = BIO_new(BIO_f_base64()); | 22 | b64 = BIO_new(BIO_f_base64()); |
23 | if(!b64) | 23 | if(!b64) |
24 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); | 24 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); |
25 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 25 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
26 | bmem = BIO_new(BIO_s_mem()); | 26 | bmem = BIO_new(BIO_s_mem()); |
27 | BIO_set_flags(b64,BIO_CLOSE); | 27 | BIO_set_flags(b64,BIO_CLOSE); |
28 | if(!bmem) | 28 | if(!bmem) |
29 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); | 29 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); |
30 | BIO_push(b64,bmem); | 30 | BIO_push(b64,bmem); |
31 | if(BIO_write(b64,data,length)!=length) | 31 | if(((size_t)BIO_write(b64,data,length))!=length) |
32 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); | 32 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); |
33 | BIO_flush(b64); | 33 | if(BIO_flush(b64)!=1) |
34 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); | ||
34 | char *rvd; | 35 | char *rvd; |
35 | long rvl = BIO_get_mem_data(bmem,&rvd); | 36 | long rvl = BIO_get_mem_data(bmem,&rvd); |
36 | string rv(rvd,rvl); | 37 | string rv(rvd,rvl); |
37 | BIO_free_all(b64); | 38 | BIO_free_all(b64); |
38 | return rv; | 39 | return rv; |
39 | }catch(...) { | 40 | }catch(...) { |
40 | if(b64) BIO_free_all(b64); | 41 | if(b64) BIO_free_all(b64); |
41 | throw; | 42 | throw; |
42 | } | 43 | } |
43 | } | 44 | } |
44 | 45 | ||
45 | void decode_base64(const string& data,vector<unsigned char>& rv) { | 46 | void decode_base64(const string& data,vector<unsigned char>& rv) { |
46 | BIO *b64 = 0, *bmem = 0; | 47 | BIO *b64 = 0, *bmem = 0; |
47 | rv.clear(); | 48 | rv.clear(); |
48 | try { | 49 | try { |
49 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); | 50 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); |
50 | if(!bmem) | 51 | if(!bmem) |
51 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); | 52 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); |
52 | b64 = BIO_new(BIO_f_base64()); | 53 | b64 = BIO_new(BIO_f_base64()); |
53 | if(!b64) | 54 | if(!b64) |
54 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); | 55 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); |
55 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 56 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
56 | BIO_push(b64,bmem); | 57 | BIO_push(b64,bmem); |
57 | unsigned char tmp[512]; | 58 | unsigned char tmp[512]; |
58 | size_t rb = 0; | 59 | size_t rb = 0; |
59 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) | 60 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) |
60 | rv.insert(rv.end(),tmp,&tmp[rb]); | 61 | rv.insert(rv.end(),tmp,&tmp[rb]); |
61 | BIO_free_all(b64); | 62 | BIO_free_all(b64); |
62 | }catch(...) { | 63 | }catch(...) { |
63 | if(b64) BIO_free_all(b64); | 64 | if(b64) BIO_free_all(b64); |
64 | throw; | 65 | throw; |
65 | } | 66 | } |
66 | } | 67 | } |
67 | 68 | ||
68 | /* | 69 | /* |
69 | * big numerics | 70 | * big numerics |
70 | */ | 71 | */ |
71 | 72 | ||
72 | BIGNUM *base64_to_bignum(const string& b64) { | 73 | BIGNUM *base64_to_bignum(const string& b64) { |
73 | vector<unsigned char> bin; | 74 | vector<unsigned char> bin; |
74 | decode_base64(b64,bin); | 75 | decode_base64(b64,bin); |
75 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); | 76 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); |
76 | if(!rv) | 77 | if(!rv) |
77 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); | 78 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); |
78 | return rv; | 79 | return rv; |
79 | } | 80 | } |
80 | 81 | ||
81 | BIGNUM *dec_to_bignum(const string& dec) { | 82 | BIGNUM *dec_to_bignum(const string& dec) { |
82 | BIGNUM *rv = 0; | 83 | BIGNUM *rv = 0; |
83 | if(!BN_dec2bn(&rv,dec.c_str())) | 84 | if(!BN_dec2bn(&rv,dec.c_str())) |
84 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 85 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
85 | return rv; | 86 | return rv; |
86 | } | 87 | } |
87 | 88 | ||
88 | string bignum_to_base64(const BIGNUM *bn) { | 89 | string bignum_to_base64(const BIGNUM *bn) { |
89 | vector<unsigned char> bin(BN_num_bytes(bn)+1); | 90 | vector<unsigned char> bin(BN_num_bytes(bn)+1); |
90 | unsigned char *binptr = &(bin.front())+1; | 91 | unsigned char *binptr = &(bin.front())+1; |
91 | int l = BN_bn2bin(bn,binptr); | 92 | int l = BN_bn2bin(bn,binptr); |
92 | if(l && (*binptr)&0x80){ | 93 | if(l && (*binptr)&0x80){ |
93 | (*(--binptr)) = 0; ++l; | 94 | (*(--binptr)) = 0; ++l; |
94 | } | 95 | } |
95 | return encode_base64(binptr,l); | 96 | return encode_base64(binptr,l); |
96 | } | 97 | } |
97 | 98 | ||
98 | /* | 99 | /* |
99 | * w3c times | 100 | * w3c times |
100 | */ | 101 | */ |
101 | 102 | ||
102 | string time_to_w3c(time_t t) { | 103 | string time_to_w3c(time_t t) { |
103 | struct tm tm_t; | 104 | struct tm tm_t; |
104 | if(!gmtime_r(&t,&tm_t)) | 105 | if(!gmtime_r(&t,&tm_t)) |
105 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 106 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
106 | char rv[25]; | 107 | char rv[25]; |
107 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) | 108 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) |
108 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); | 109 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); |
109 | return rv; | 110 | return rv; |
110 | } | 111 | } |
111 | 112 | ||
112 | time_t w3c_to_time(const string& w) { | 113 | time_t w3c_to_time(const string& w) { |
113 | struct tm tm_t; | 114 | struct tm tm_t; |
114 | memset(&tm_t,0,sizeof(tm_t)); | 115 | memset(&tm_t,0,sizeof(tm_t)); |
115 | if( | 116 | if( |
116 | sscanf( | 117 | sscanf( |
117 | w.c_str(), | 118 | w.c_str(), |
118 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 119 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
119 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 120 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
120 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec | 121 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec |
121 | ) != 6 ) | 122 | ) != 6 ) |
122 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 123 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
123 | tm_t.tm_mon--; | 124 | tm_t.tm_mon--; |
124 | tm_t.tm_year-=1900; | 125 | tm_t.tm_year-=1900; |
125 | time_t rv = mktime(&tm_t); | 126 | time_t rv = mktime(&tm_t); |
126 | if(rv==(time_t)-1) | 127 | if(rv==(time_t)-1) |
127 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 128 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); |
128 | return rv; | 129 | return rv; |
129 | } | 130 | } |
130 | 131 | ||
131 | /* | 132 | /* |
132 | * | 133 | * |
133 | */ | 134 | */ |
134 | 135 | ||
135 | string url_encode(const string& str) { | 136 | string url_encode(const string& str) { |
136 | char * t = curl_escape(str.c_str(),str.length()); | 137 | char * t = curl_escape(str.c_str(),str.length()); |
137 | if(!t) | 138 | if(!t) |
138 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); | 139 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); |
139 | string rv(t); | 140 | string rv(t); |
140 | curl_free(t); | 141 | curl_free(t); |
141 | return rv; | 142 | return rv; |
142 | } | 143 | } |
143 | 144 | ||
144 | string long_to_string(long l) { | 145 | string long_to_string(long l) { |
145 | char rv[32]; | 146 | char rv[32]; |
146 | int r=snprintf(rv,sizeof(rv),"%ld",l); | 147 | int r=snprintf(rv,sizeof(rv),"%ld",l); |
147 | if(r<0 || r>=sizeof(rv)) | 148 | if(r<0 || r>=(int)sizeof(rv)) |
148 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); | 149 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); |
149 | return rv; | 150 | return rv; |
150 | } | 151 | } |
151 | 152 | ||
152 | long string_to_long(const string& s) { | 153 | long string_to_long(const string& s) { |
153 | char *endptr = 0; | 154 | char *endptr = 0; |
154 | long rv = strtol(s.c_str(),&endptr,10); | 155 | long rv = strtol(s.c_str(),&endptr,10); |
155 | if((!endptr) || endptr==s.c_str()) | 156 | if((!endptr) || endptr==s.c_str()) |
156 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); | 157 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); |
157 | return rv; | 158 | return rv; |
158 | } | 159 | } |
159 | 160 | ||
160 | } | 161 | } |
161 | 162 | ||
162 | } | 163 | } |