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