-rw-r--r-- | include/opkele/util.h | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/include/opkele/util.h b/include/opkele/util.h index edc1859..085c9e6 100644 --- a/include/opkele/util.h +++ b/include/opkele/util.h | |||
@@ -1,133 +1,143 @@ | |||
1 | #ifndef __OPKELE_UTIL_H | 1 | #ifndef __OPKELE_UTIL_H |
2 | #define __OPKELE_UTIL_H | 2 | #define __OPKELE_UTIL_H |
3 | 3 | ||
4 | #include <time.h> | 4 | #include <time.h> |
5 | #include <string> | 5 | #include <string> |
6 | #include <vector> | 6 | #include <vector> |
7 | #include <openssl/bn.h> | 7 | #include <openssl/bn.h> |
8 | #include <openssl/dh.h> | 8 | #include <openssl/dh.h> |
9 | 9 | ||
10 | namespace opkele { | 10 | namespace opkele { |
11 | using std::string; | 11 | using std::string; |
12 | using std::vector; | 12 | using std::vector; |
13 | 13 | ||
14 | /** | 14 | /** |
15 | * @brief opkele utils namespace | 15 | * @brief opkele utils namespace |
16 | */ | 16 | */ |
17 | namespace util { | 17 | namespace util { |
18 | 18 | ||
19 | /** | 19 | /** |
20 | * Convenience class encapsulating SSL BIGNUM object for the purpose of | 20 | * Convenience class encapsulating SSL BIGNUM object for the purpose of |
21 | * automatical freeing. | 21 | * automatical freeing. |
22 | */ | 22 | */ |
23 | class bignum_t { | 23 | class bignum_t { |
24 | public: | 24 | public: |
25 | BIGNUM *_bn; | 25 | BIGNUM *_bn; |
26 | 26 | ||
27 | bignum_t() : _bn(0) { } | 27 | bignum_t() : _bn(0) { } |
28 | bignum_t(BIGNUM *bn) : _bn(bn) { } | 28 | bignum_t(BIGNUM *bn) : _bn(bn) { } |
29 | ~bignum_t() throw() { if(_bn) BN_free(_bn); } | 29 | ~bignum_t() throw() { if(_bn) BN_free(_bn); } |
30 | 30 | ||
31 | bignum_t& operator=(BIGNUM *bn) { if(_bn) BN_free(_bn); _bn = bn; return *this; } | 31 | bignum_t& operator=(BIGNUM *bn) { if(_bn) BN_free(_bn); _bn = bn; return *this; } |
32 | 32 | ||
33 | operator const BIGNUM*(void) const { return _bn; } | 33 | operator const BIGNUM*(void) const { return _bn; } |
34 | operator BIGNUM*(void) { return _bn; } | 34 | operator BIGNUM*(void) { return _bn; } |
35 | }; | 35 | }; |
36 | /** | 36 | /** |
37 | * Convenience clas encapsulating SSL DH object for the purpose of | 37 | * Convenience clas encapsulating SSL DH object for the purpose of |
38 | * automatic freeing. | 38 | * automatic freeing. |
39 | */ | 39 | */ |
40 | class dh_t { | 40 | class dh_t { |
41 | public: | 41 | public: |
42 | DH *_dh; | 42 | DH *_dh; |
43 | 43 | ||
44 | dh_t() : _dh(0) { } | 44 | dh_t() : _dh(0) { } |
45 | dh_t(DH *dh) : _dh(dh) { } | 45 | dh_t(DH *dh) : _dh(dh) { } |
46 | ~dh_t() throw() { if(_dh) DH_free(_dh); } | 46 | ~dh_t() throw() { if(_dh) DH_free(_dh); } |
47 | 47 | ||
48 | dh_t& operator=(DH *dh) { if(_dh) DH_free(_dh); _dh = dh; return *this; } | 48 | dh_t& operator=(DH *dh) { if(_dh) DH_free(_dh); _dh = dh; return *this; } |
49 | 49 | ||
50 | operator const DH*(void) const { return _dh; } | 50 | operator const DH*(void) const { return _dh; } |
51 | operator DH*(void) { return _dh; } | 51 | operator DH*(void) { return _dh; } |
52 | 52 | ||
53 | DH* operator->() { return _dh; } | 53 | DH* operator->() { return _dh; } |
54 | const DH* operator->() const { return _dh; } | 54 | const DH* operator->() const { return _dh; } |
55 | }; | 55 | }; |
56 | 56 | ||
57 | /** | 57 | /** |
58 | * Convert base64-encoded SSL BIGNUM to internal representation. | 58 | * Convert base64-encoded SSL BIGNUM to internal representation. |
59 | * @param b64 base64-encoded number | 59 | * @param b64 base64-encoded number |
60 | * @return SSL BIGNUM | 60 | * @return SSL BIGNUM |
61 | * @throw failed_conversion in case of error | 61 | * @throw failed_conversion in case of error |
62 | */ | 62 | */ |
63 | BIGNUM *base64_to_bignum(const string& b64); | 63 | BIGNUM *base64_to_bignum(const string& b64); |
64 | /** | 64 | /** |
65 | * Convert decimal representation to SSL BIGNUM. | 65 | * Convert decimal representation to SSL BIGNUM. |
66 | * @param dec decimal representation | 66 | * @param dec decimal representation |
67 | * @return resulting BIGNUM | 67 | * @return resulting BIGNUM |
68 | * @throw failed_conversion in case of error | 68 | * @throw failed_conversion in case of error |
69 | */ | 69 | */ |
70 | BIGNUM *dec_to_bignum(const string& dec); | 70 | BIGNUM *dec_to_bignum(const string& dec); |
71 | /** | 71 | /** |
72 | * Convert SSL BIGNUM data to base64 encoded string. | 72 | * Convert SSL BIGNUM data to base64 encoded string. |
73 | * @param bn BIGNUM | 73 | * @param bn BIGNUM |
74 | * @return base64encoded string | 74 | * @return base64encoded string |
75 | */ | 75 | */ |
76 | string bignum_to_base64(const BIGNUM *bn); | 76 | string bignum_to_base64(const BIGNUM *bn); |
77 | 77 | ||
78 | /** | 78 | /** |
79 | * Convert internal time representation to w3c format | 79 | * Convert internal time representation to w3c format |
80 | * @param t internal representation | 80 | * @param t internal representation |
81 | * @return w3c time | 81 | * @return w3c time |
82 | * @throw failed_conversion in case of error | 82 | * @throw failed_conversion in case of error |
83 | */ | 83 | */ |
84 | string time_to_w3c(time_t t); | 84 | string time_to_w3c(time_t t); |
85 | /** | 85 | /** |
86 | * Convert W3C time representation to internal time_t | 86 | * Convert W3C time representation to internal time_t |
87 | * @param w w3c representation | 87 | * @param w w3c representation |
88 | * @return converted time | 88 | * @return converted time |
89 | * @throw failed_conversion in case of error | 89 | * @throw failed_conversion in case of error |
90 | */ | 90 | */ |
91 | time_t w3c_to_time(const string& w); | 91 | time_t w3c_to_time(const string& w); |
92 | 92 | ||
93 | /** | 93 | /** |
94 | * Encode string to the representation suitable for using in URL. | 94 | * Encode string to the representation suitable for using in URL. |
95 | * @param str string to encode | 95 | * @param str string to encode |
96 | * @return encoded string | 96 | * @return encoded string |
97 | * @throw failed_conversion in case of failure | 97 | * @throw failed_conversion in case of failure |
98 | */ | 98 | */ |
99 | string url_encode(const string& str); | 99 | string url_encode(const string& str); |
100 | 100 | ||
101 | /** | 101 | /** |
102 | * Convert number to string | 102 | * Convert number to string |
103 | * @param l number | 103 | * @param l number |
104 | * @return string representation | 104 | * @return string representation |
105 | * @throw failed_conversion in case of failure | 105 | * @throw failed_conversion in case of failure |
106 | */ | 106 | */ |
107 | string long_to_string(long l); | 107 | string long_to_string(long l); |
108 | /** | 108 | /** |
109 | * Convert string to number | 109 | * Convert string to number |
110 | * @param s string, containing the number | 110 | * @param s string, containing the number |
111 | * @return the number | 111 | * @return the number |
112 | * @throw failed_conversion in case of failure | 112 | * @throw failed_conversion in case of failure |
113 | */ | 113 | */ |
114 | long string_to_long(const string& s); | 114 | long string_to_long(const string& s); |
115 | 115 | ||
116 | /** | 116 | /** |
117 | * Encode binary data using base64. | 117 | * Encode binary data using base64. |
118 | * @param data pointer to binary data | 118 | * @param data pointer to binary data |
119 | * @param length length of data | 119 | * @param length length of data |
120 | * @return encoded data | 120 | * @return encoded data |
121 | */ | 121 | */ |
122 | string encode_base64(const void *data,size_t length); | 122 | string encode_base64(const void *data,size_t length); |
123 | /** | 123 | /** |
124 | * Decode binary data from base64 representation. | 124 | * Decode binary data from base64 representation. |
125 | * @param data base64-encoded data | 125 | * @param data base64-encoded data |
126 | * @param rv container for decoded binary | 126 | * @param rv container for decoded binary |
127 | */ | 127 | */ |
128 | void decode_base64(const string& data,vector<unsigned char>& rv); | 128 | void decode_base64(const string& data,vector<unsigned char>& rv); |
129 | |||
130 | /** | ||
131 | * Normalize http(s) URI according to RFC3986, section 6. URI is | ||
132 | * expected to have scheme: in front of it. | ||
133 | * @param uri URI | ||
134 | * @return normalized URI | ||
135 | * @throw not_implemented in case of non-httpi(s) URI | ||
136 | * @throw bad_input in case of malformed URI | ||
137 | */ | ||
138 | string rfc_3986_normalize_uri(const string& uri); | ||
129 | } | 139 | } |
130 | 140 | ||
131 | } | 141 | } |
132 | 142 | ||
133 | #endif /* __OPKELE_UTIL_H */ | 143 | #endif /* __OPKELE_UTIL_H */ |