-rw-r--r-- | noncore/unsupported/mail2/libmail/miscfunctions.cpp | 3 |
1 files changed, 1 insertions, 2 deletions
diff --git a/noncore/unsupported/mail2/libmail/miscfunctions.cpp b/noncore/unsupported/mail2/libmail/miscfunctions.cpp index 0edbfa8..c81b101 100644 --- a/noncore/unsupported/mail2/libmail/miscfunctions.cpp +++ b/noncore/unsupported/mail2/libmail/miscfunctions.cpp | |||
@@ -1,295 +1,294 @@ | |||
1 | #include <qdatetime.h> | 1 | #include <qdatetime.h> |
2 | 2 | ||
3 | #include <stdlib.h> | 3 | #include <stdlib.h> |
4 | #include <stdio.h> | 4 | #include <stdio.h> |
5 | #include <time.h> | 5 | #include <time.h> |
6 | 6 | ||
7 | #include <openssl/md5.h> | ||
8 | |||
9 | #include "miscfunctions.h" | 7 | #include "miscfunctions.h" |
8 | #include "md5.h" | ||
10 | 9 | ||
11 | QString MiscFunctions::encodeQPrintable(const QString &src) | 10 | QString MiscFunctions::encodeQPrintable(const QString &src) |
12 | { | 11 | { |
13 | // TODO: implent encodeQPrintable | 12 | // TODO: implent encodeQPrintable |
14 | return src; | 13 | return src; |
15 | } | 14 | } |
16 | 15 | ||
17 | QString MiscFunctions::decodeQPrintable(const QString &src) | 16 | QString MiscFunctions::decodeQPrintable(const QString &src) |
18 | { | 17 | { |
19 | QString out; | 18 | QString out; |
20 | 19 | ||
21 | for (unsigned int i = 0; i <= src.length(); i++) { | 20 | for (unsigned int i = 0; i <= src.length(); i++) { |
22 | if (src[i] == '=') { | 21 | if (src[i] == '=') { |
23 | if (src[i+1] == "\n") { | 22 | if (src[i+1] == "\n") { |
24 | i += 1; | 23 | i += 1; |
25 | } else { | 24 | } else { |
26 | QString temp = QString("%1%2").arg(src[i+1]).arg(src[i+2]); | 25 | QString temp = QString("%1%2").arg(src[i+1]).arg(src[i+2]); |
27 | int number = temp.toInt(0, 16); | 26 | int number = temp.toInt(0, 16); |
28 | 27 | ||
29 | out += QChar(number); | 28 | out += QChar(number); |
30 | i += 2; | 29 | i += 2; |
31 | } | 30 | } |
32 | } else { | 31 | } else { |
33 | out += src[i]; | 32 | out += src[i]; |
34 | } | 33 | } |
35 | } | 34 | } |
36 | return out; | 35 | return out; |
37 | } | 36 | } |
38 | 37 | ||
39 | QString MiscFunctions::encodeBase64(const QString &src) | 38 | QString MiscFunctions::encodeBase64(const QString &src) |
40 | { | 39 | { |
41 | char *dataPtr = (char *) src.latin1(); | 40 | char *dataPtr = (char *) src.latin1(); |
42 | int len = src.length(); | 41 | int len = src.length(); |
43 | int count = 0; | 42 | int count = 0; |
44 | QString temp = ""; | 43 | QString temp = ""; |
45 | 44 | ||
46 | while (len > 0) { | 45 | while (len > 0) { |
47 | if (len < 3) { | 46 | if (len < 3) { |
48 | encodeBase64Base(dataPtr, &temp, len); | 47 | encodeBase64Base(dataPtr, &temp, len); |
49 | len = 0; | 48 | len = 0; |
50 | } else { | 49 | } else { |
51 | encodeBase64Base(dataPtr, &temp, 3); | 50 | encodeBase64Base(dataPtr, &temp, 3); |
52 | len -= 3; | 51 | len -= 3; |
53 | dataPtr += 3; | 52 | dataPtr += 3; |
54 | count += 4; | 53 | count += 4; |
55 | } | 54 | } |
56 | if (count > 72) { | 55 | if (count > 72) { |
57 | count = 0; | 56 | count = 0; |
58 | temp += "\n"; | 57 | temp += "\n"; |
59 | } | 58 | } |
60 | } | 59 | } |
61 | 60 | ||
62 | return temp; | 61 | return temp; |
63 | } | 62 | } |
64 | 63 | ||
65 | void MiscFunctions::encodeBase64Base(char *src, QString *dest, int len) | 64 | void MiscFunctions::encodeBase64Base(char *src, QString *dest, int len) |
66 | { | 65 | { |
67 | QString temp; | 66 | QString temp; |
68 | uchar c; | 67 | uchar c; |
69 | uchar bufOut[4]; | 68 | uchar bufOut[4]; |
70 | 69 | ||
71 | bufOut[0] = src[0]; | 70 | bufOut[0] = src[0]; |
72 | bufOut[0] >>= 2; | 71 | bufOut[0] >>= 2; |
73 | 72 | ||
74 | bufOut[1] = src[0]; | 73 | bufOut[1] = src[0]; |
75 | bufOut[1] = bufOut[1] & (1 + 2); | 74 | bufOut[1] = bufOut[1] & (1 + 2); |
76 | bufOut[1] <<= 4; | 75 | bufOut[1] <<= 4; |
77 | if (len > 1) c = src[1]; | 76 | if (len > 1) c = src[1]; |
78 | else c = 0; | 77 | else c = 0; |
79 | 78 | ||
80 | c = c & (16 + 32 + 64 + 128); | 79 | c = c & (16 + 32 + 64 + 128); |
81 | c >>= 4; | 80 | c >>= 4; |
82 | bufOut[1] = bufOut[1] | c; | 81 | bufOut[1] = bufOut[1] | c; |
83 | 82 | ||
84 | bufOut[2] = src[1]; | 83 | bufOut[2] = src[1]; |
85 | bufOut[2] = bufOut[2] & (1 + 2 + 4 + 8); | 84 | bufOut[2] = bufOut[2] & (1 + 2 + 4 + 8); |
86 | bufOut[2] <<= 2; | 85 | bufOut[2] <<= 2; |
87 | if (len > 2) c = src[2]; | 86 | if (len > 2) c = src[2]; |
88 | else c = 0; | 87 | else c = 0; |
89 | 88 | ||
90 | c >>= 6; | 89 | c >>= 6; |
91 | bufOut[2] = bufOut[2] | c; | 90 | bufOut[2] = bufOut[2] | c; |
92 | 91 | ||
93 | bufOut[3] = src[2]; | 92 | bufOut[3] = src[2]; |
94 | bufOut[3] = bufOut[3] & (1 + 2 + 4 + 8 + 16 + 32); | 93 | bufOut[3] = bufOut[3] & (1 + 2 + 4 + 8 + 16 + 32); |
95 | 94 | ||
96 | if (len == 1) { | 95 | if (len == 1) { |
97 | bufOut[2] = 64; | 96 | bufOut[2] = 64; |
98 | bufOut[3] = 64; | 97 | bufOut[3] = 64; |
99 | } | 98 | } |
100 | if (len == 2) { | 99 | if (len == 2) { |
101 | bufOut[3] = 64; | 100 | bufOut[3] = 64; |
102 | } | 101 | } |
103 | for (int x = 0; x < 4; x++) { | 102 | for (int x = 0; x < 4; x++) { |
104 | if (bufOut[x] <= 25) | 103 | if (bufOut[x] <= 25) |
105 | bufOut[x] += (uint) 'A'; | 104 | bufOut[x] += (uint) 'A'; |
106 | else if (bufOut[x] >= 26 && bufOut[x] <= 51) | 105 | else if (bufOut[x] >= 26 && bufOut[x] <= 51) |
107 | bufOut[x] += (uint) 'a' - 26; | 106 | bufOut[x] += (uint) 'a' - 26; |
108 | else if (bufOut[x] >= 52 && bufOut[x] <= 61) | 107 | else if (bufOut[x] >= 52 && bufOut[x] <= 61) |
109 | bufOut[x] += (uint) '0' - 52; | 108 | bufOut[x] += (uint) '0' - 52; |
110 | else if (bufOut[x] == 62) | 109 | else if (bufOut[x] == 62) |
111 | bufOut[x] = '+'; | 110 | bufOut[x] = '+'; |
112 | else if (bufOut[x] == 63) | 111 | else if (bufOut[x] == 63) |
113 | bufOut[x] = '/'; | 112 | bufOut[x] = '/'; |
114 | else if (bufOut[x] == 64) | 113 | else if (bufOut[x] == 64) |
115 | bufOut[x] = '='; | 114 | bufOut[x] = '='; |
116 | 115 | ||
117 | dest->append(bufOut[x]); | 116 | dest->append(bufOut[x]); |
118 | } | 117 | } |
119 | } | 118 | } |
120 | 119 | ||
121 | QString MiscFunctions::decodeBase64(const QString &src) | 120 | QString MiscFunctions::decodeBase64(const QString &src) |
122 | { | 121 | { |
123 | char plain[4]; | 122 | char plain[4]; |
124 | char *destPtr; | 123 | char *destPtr; |
125 | QByteArray buffer; | 124 | QByteArray buffer; |
126 | uint bufCount = 0, pos = 0, decodedCount, x; | 125 | uint bufCount = 0, pos = 0, decodedCount, x; |
127 | 126 | ||
128 | buffer.resize(src.length() * 3 / 4); | 127 | buffer.resize(src.length() * 3 / 4); |
129 | destPtr = buffer.data(); | 128 | destPtr = buffer.data(); |
130 | 129 | ||
131 | while (pos < src.length()) { | 130 | while (pos < src.length()) { |
132 | decodedCount = 4; | 131 | decodedCount = 4; |
133 | x = 0; | 132 | x = 0; |
134 | while ((x < 4) && (pos < src.length())) { | 133 | while ((x < 4) && (pos < src.length())) { |
135 | plain[x] = src[pos].latin1(); | 134 | plain[x] = src[pos].latin1(); |
136 | pos++; | 135 | pos++; |
137 | if (plain[x] == '\r' || plain[x] == '\n' || plain[x] == ' ') | 136 | if (plain[x] == '\r' || plain[x] == '\n' || plain[x] == ' ') |
138 | x--; | 137 | x--; |
139 | x++; | 138 | x++; |
140 | } | 139 | } |
141 | if (x > 1) { | 140 | if (x > 1) { |
142 | decodedCount = decodeBase64Base(plain, destPtr); | 141 | decodedCount = decodeBase64Base(plain, destPtr); |
143 | destPtr += decodedCount; | 142 | destPtr += decodedCount; |
144 | bufCount += decodedCount; | 143 | bufCount += decodedCount; |
145 | } | 144 | } |
146 | } | 145 | } |
147 | 146 | ||
148 | buffer.resize(bufCount); | 147 | buffer.resize(bufCount); |
149 | return QString(buffer); | 148 | return QString(buffer); |
150 | } | 149 | } |
151 | 150 | ||
152 | int MiscFunctions::decodeBase64Base(char *src, char *bufOut) | 151 | int MiscFunctions::decodeBase64Base(char *src, char *bufOut) |
153 | { | 152 | { |
154 | char c, z; | 153 | char c, z; |
155 | char li[4]; | 154 | char li[4]; |
156 | int processed; | 155 | int processed; |
157 | 156 | ||
158 | for (int x = 0; x < 4; x++) { | 157 | for (int x = 0; x < 4; x++) { |
159 | c = src[x]; | 158 | c = src[x]; |
160 | 159 | ||
161 | if ( (int) c >= 'A' && (int) c <= 'Z') | 160 | if ( (int) c >= 'A' && (int) c <= 'Z') |
162 | li[x] = (int) c - (int) 'A'; | 161 | li[x] = (int) c - (int) 'A'; |
163 | if ( (int) c >= 'a' && (int) c <= 'z') | 162 | if ( (int) c >= 'a' && (int) c <= 'z') |
164 | li[x] = (int) c - (int) 'a' + 26; | 163 | li[x] = (int) c - (int) 'a' + 26; |
165 | if ( (int) c >= '0' && (int) c <= '9') | 164 | if ( (int) c >= '0' && (int) c <= '9') |
166 | li[x] = (int) c - (int) '0' + 52; | 165 | li[x] = (int) c - (int) '0' + 52; |
167 | if (c == '+') | 166 | if (c == '+') |
168 | li[x] = 62; | 167 | li[x] = 62; |
169 | if (c == '/') | 168 | if (c == '/') |
170 | li[x] = 63; | 169 | li[x] = 63; |
171 | } | 170 | } |
172 | 171 | ||
173 | processed = 1; | 172 | processed = 1; |
174 | bufOut[0] = (char) li[0] & (32+16+8+4+2+1); | 173 | bufOut[0] = (char) li[0] & (32+16+8+4+2+1); |
175 | bufOut[0] <<= 2; | 174 | bufOut[0] <<= 2; |
176 | z = li[1] >> 4; | 175 | z = li[1] >> 4; |
177 | bufOut[0] = bufOut[0] | z; | 176 | bufOut[0] = bufOut[0] | z; |
178 | 177 | ||
179 | if (src[2] != '=') { | 178 | if (src[2] != '=') { |
180 | bufOut[1] = (char) li[1] & (8+4+2+1); | 179 | bufOut[1] = (char) li[1] & (8+4+2+1); |
181 | bufOut[1] <<= 4; | 180 | bufOut[1] <<= 4; |
182 | z = li[2] >> 2; | 181 | z = li[2] >> 2; |
183 | bufOut[1] = bufOut[1] | z; | 182 | bufOut[1] = bufOut[1] | z; |
184 | processed++; | 183 | processed++; |
185 | 184 | ||
186 | if (src[3] != '=') { | 185 | if (src[3] != '=') { |
187 | bufOut[2] = (char) li[2] & (2+1); | 186 | bufOut[2] = (char) li[2] & (2+1); |
188 | bufOut[2] <<= 6; | 187 | bufOut[2] <<= 6; |
189 | z = li[3]; | 188 | z = li[3]; |
190 | bufOut[2] = bufOut[2] | z; | 189 | bufOut[2] = bufOut[2] | z; |
191 | processed++; | 190 | processed++; |
192 | } | 191 | } |
193 | } | 192 | } |
194 | return processed; | 193 | return processed; |
195 | } | 194 | } |
196 | 195 | ||
197 | QString MiscFunctions::uniqueString() | 196 | QString MiscFunctions::uniqueString() |
198 | { | 197 | { |
199 | QString uniqueString = QDate::currentDate().toString(); | 198 | QString uniqueString = QDate::currentDate().toString(); |
200 | uniqueString += QTime::currentTime().toString(); | 199 | uniqueString += QTime::currentTime().toString(); |
201 | uniqueString += QString("%1").arg(rand()); | 200 | uniqueString += QString("%1").arg(rand()); |
202 | 201 | ||
203 | unsigned char md[16]; | 202 | unsigned char md[16]; |
204 | 203 | ||
205 | MD5_CTX ctx; | 204 | MD5_CTX ctx; |
206 | MD5_Init(&ctx); | 205 | MD5_Init(&ctx); |
207 | MD5_Update(&ctx, (unsigned char *)uniqueString.latin1(), uniqueString.length()); | 206 | MD5_Update(&ctx, (unsigned char *)uniqueString.latin1(), uniqueString.length()); |
208 | MD5_Final(md, &ctx); | 207 | MD5_Final(md, &ctx); |
209 | 208 | ||
210 | char hash[16]; | 209 | char hash[16]; |
211 | for (unsigned int i = 0; i < sizeof(md); i++) | 210 | for (unsigned int i = 0; i < sizeof(md); i++) |
212 | sprintf(hash + 2 * i, "%02x", md[i]); | 211 | sprintf(hash + 2 * i, "%02x", md[i]); |
213 | 212 | ||
214 | return hash; | 213 | return hash; |
215 | } | 214 | } |
216 | 215 | ||
217 | QString MiscFunctions::rfcDate() | 216 | QString MiscFunctions::rfcDate() |
218 | { | 217 | { |
219 | time_t t = time(NULL); | 218 | time_t t = time(NULL); |
220 | tm *time = localtime(&t); | 219 | tm *time = localtime(&t); |
221 | QString pm, tzh, tzm, ths, tms, tss; | 220 | QString pm, tzh, tzm, ths, tms, tss; |
222 | 221 | ||
223 | time->tm_gmtoff < 0 ? pm = "-" : pm = "+"; | 222 | time->tm_gmtoff < 0 ? pm = "-" : pm = "+"; |
224 | int h = abs(time->tm_gmtoff) / 3600; | 223 | int h = abs(time->tm_gmtoff) / 3600; |
225 | int m = (abs(time->tm_gmtoff) - h * 3600) / 60; | 224 | int m = (abs(time->tm_gmtoff) - h * 3600) / 60; |
226 | h < 10 ? tzh = QString("0%1").arg(h) : tzh = QString("%1").arg(h); | 225 | h < 10 ? tzh = QString("0%1").arg(h) : tzh = QString("%1").arg(h); |
227 | m < 10 ? tzm = QString("0%1").arg(m) : tzm = QString("%1").arg(m); | 226 | m < 10 ? tzm = QString("0%1").arg(m) : tzm = QString("%1").arg(m); |
228 | 227 | ||
229 | int th = time->tm_hour; | 228 | int th = time->tm_hour; |
230 | int tm = time->tm_min; | 229 | int tm = time->tm_min; |
231 | int ts = time->tm_sec; | 230 | int ts = time->tm_sec; |
232 | th < 10 ? ths = QString("0%1").arg(th) : ths = QString("%1").arg(th); | 231 | th < 10 ? ths = QString("0%1").arg(th) : ths = QString("%1").arg(th); |
233 | tm < 10 ? tms = QString("0%1").arg(tm) : tms = QString("%1").arg(tm); | 232 | tm < 10 ? tms = QString("0%1").arg(tm) : tms = QString("%1").arg(tm); |
234 | ts < 10 ? tss = QString("0%1").arg(ts) : tss = QString("%1").arg(ts); | 233 | ts < 10 ? tss = QString("0%1").arg(ts) : tss = QString("%1").arg(ts); |
235 | 234 | ||
236 | QString month = QDate().monthName(time->tm_mon + 1); | 235 | QString month = QDate().monthName(time->tm_mon + 1); |
237 | QString dayna = QDate().dayName(time->tm_wday); | 236 | QString dayna = QDate().dayName(time->tm_wday); |
238 | QString tzone = pm + tzh + tzm; | 237 | QString tzone = pm + tzh + tzm; |
239 | 238 | ||
240 | return QString("%1, %2 %3 %4 %5:%6:%7 %8") | 239 | return QString("%1, %2 %3 %4 %5:%6:%7 %8") |
241 | .arg(dayna) | 240 | .arg(dayna) |
242 | .arg(time->tm_mday) | 241 | .arg(time->tm_mday) |
243 | .arg(month) | 242 | .arg(month) |
244 | .arg(time->tm_year + 1900) | 243 | .arg(time->tm_year + 1900) |
245 | .arg(ths) | 244 | .arg(ths) |
246 | .arg(tms) | 245 | .arg(tms) |
247 | .arg(tss) | 246 | .arg(tss) |
248 | .arg(tzone); | 247 | .arg(tzone); |
249 | } | 248 | } |
250 | 249 | ||
251 | QString MiscFunctions::smtpAuthCramMd5(const QString &data, const QString &key) | 250 | QString MiscFunctions::smtpAuthCramMd5(const QString &data, const QString &key) |
252 | { | 251 | { |
253 | MD5_CTX context; | 252 | MD5_CTX context; |
254 | unsigned char k_ipad[65]; | 253 | unsigned char k_ipad[65]; |
255 | unsigned char k_opad[65]; | 254 | unsigned char k_opad[65]; |
256 | unsigned char tk[16]; | 255 | unsigned char tk[16]; |
257 | unsigned char digest[16]; | 256 | unsigned char digest[16]; |
258 | unsigned char *key_int = (unsigned char *)key.latin1(); | 257 | unsigned char *key_int = (unsigned char *)key.latin1(); |
259 | char hash[33]; | 258 | char hash[33]; |
260 | 259 | ||
261 | if (key.length() > 64) { | 260 | if (key.length() > 64) { |
262 | MD5_CTX tctx; | 261 | MD5_CTX tctx; |
263 | MD5_Init(&tctx); | 262 | MD5_Init(&tctx); |
264 | MD5_Update(&tctx, key_int, sizeof(key_int)); | 263 | MD5_Update(&tctx, key_int, sizeof(key_int)); |
265 | MD5_Final(tk, &tctx); | 264 | MD5_Final(tk, &tctx); |
266 | 265 | ||
267 | key_int = tk; | 266 | key_int = tk; |
268 | } | 267 | } |
269 | 268 | ||
270 | bzero(k_ipad, sizeof k_ipad); | 269 | bzero(k_ipad, sizeof k_ipad); |
271 | bzero(k_opad, sizeof k_opad); | 270 | bzero(k_opad, sizeof k_opad); |
272 | bcopy(key_int, k_ipad, sizeof(key_int)); | 271 | bcopy(key_int, k_ipad, sizeof(key_int)); |
273 | bcopy(key_int, k_opad, sizeof(key_int)); | 272 | bcopy(key_int, k_opad, sizeof(key_int)); |
274 | 273 | ||
275 | for (int i = 0; i < 64; i++) { | 274 | for (int i = 0; i < 64; i++) { |
276 | k_ipad[i] ^= 0x36; | 275 | k_ipad[i] ^= 0x36; |
277 | k_opad[i] ^= 0x5c; | 276 | k_opad[i] ^= 0x5c; |
278 | } | 277 | } |
279 | 278 | ||
280 | MD5_Init(&context); | 279 | MD5_Init(&context); |
281 | MD5_Update(&context, k_ipad, 64); | 280 | MD5_Update(&context, k_ipad, 64); |
282 | MD5_Update(&context, (unsigned char *)data.latin1(), data.length()); | 281 | MD5_Update(&context, (unsigned char *)data.latin1(), data.length()); |
283 | MD5_Final(digest, &context); | 282 | MD5_Final(digest, &context); |
284 | 283 | ||
285 | MD5_Init(&context); | 284 | MD5_Init(&context); |
286 | MD5_Update(&context, k_opad, 64); | 285 | MD5_Update(&context, k_opad, 64); |
287 | MD5_Update(&context, digest, 16); | 286 | MD5_Update(&context, digest, 16); |
288 | MD5_Final(digest, &context); | 287 | MD5_Final(digest, &context); |
289 | 288 | ||
290 | for (unsigned int i = 0; i < sizeof(digest); i++) | 289 | for (unsigned int i = 0; i < sizeof(digest); i++) |
291 | sprintf (hash + 2 * i, "%02x", digest[i]); | 290 | sprintf (hash + 2 * i, "%02x", digest[i]); |
292 | 291 | ||
293 | return hash; | 292 | return hash; |
294 | } | 293 | } |
295 | 294 | ||