Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

miscfunctions.cpp

Go to the documentation of this file.
00001 #include <qdatetime.h>
00002 
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005 #include <time.h>
00006 
00007 #include "miscfunctions.h"
00008 #include "md5.h"
00009 
00010 QString MiscFunctions::encodeQPrintable(const QString &src)
00011 {
00012         // TODO: implent encodeQPrintable
00013         return src;
00014 }
00015 
00016 QString MiscFunctions::decodeQPrintable(const QString &src)
00017 {
00018         QString out;
00019 
00020         for (unsigned int i = 0; i <= src.length(); i++) {
00021                 if (src[i] == '=') {
00022                         if (src[i+1] == "\n") {
00023                                 i += 1;
00024                         } else {
00025                                 QString temp = QString("%1%2").arg(src[i+1]).arg(src[i+2]);
00026                                 int number = temp.toInt(0, 16);
00027 
00028                                 out += QChar(number);
00029                                 i += 2;
00030                         }
00031                 } else {
00032                         out += src[i];
00033                 }
00034         }
00035         return out;
00036 }
00037 
00038 QString MiscFunctions::encodeBase64(const QString &src)
00039 {
00040         char *dataPtr = (char *) src.latin1();
00041         int len = src.length();
00042         int count = 0;
00043         QString temp = "";
00044 
00045         while (len > 0) {
00046                 if (len < 3) {
00047                         encodeBase64Base(dataPtr, &temp, len);
00048                         len = 0;
00049                 } else {
00050                         encodeBase64Base(dataPtr, &temp, 3);
00051                         len -= 3;
00052                         dataPtr += 3;
00053                         count += 4;
00054                 }
00055                 if (count > 72) {
00056                         count = 0;
00057                         temp += "\n";
00058                 }
00059         }
00060 
00061         return temp;    
00062 }
00063 
00064 void MiscFunctions::encodeBase64Base(char *src, QString *dest, int len)
00065 {
00066         QString temp;
00067         uchar c;
00068         uchar bufOut[4];
00069 
00070         bufOut[0] = src[0];
00071         bufOut[0] >>= 2;
00072 
00073         bufOut[1] = src[0];
00074         bufOut[1] = bufOut[1] & (1 + 2);
00075         bufOut[1] <<= 4;
00076         if (len > 1) c = src[1];
00077         else c = 0;
00078 
00079         c = c & (16 + 32 + 64 + 128);
00080         c >>= 4;
00081         bufOut[1] = bufOut[1] | c;
00082 
00083         bufOut[2] = src[1];
00084         bufOut[2] = bufOut[2] & (1 + 2 + 4 + 8);
00085         bufOut[2] <<= 2;
00086         if (len > 2) c = src[2];
00087         else c = 0;
00088 
00089         c >>= 6;
00090         bufOut[2] = bufOut[2] | c;
00091 
00092         bufOut[3] = src[2];
00093         bufOut[3] = bufOut[3] & (1 + 2 + 4 + 8 + 16 + 32);
00094 
00095         if (len == 1) {
00096                 bufOut[2] = 64;
00097                 bufOut[3] = 64;
00098         }
00099         if (len == 2) {
00100                 bufOut[3] = 64;
00101         }
00102         for (int x = 0; x < 4; x++) {
00103                 if (bufOut[x] <= 25)
00104                         bufOut[x] += (uint) 'A';
00105                 else if (bufOut[x] >= 26 && bufOut[x] <= 51)
00106                         bufOut[x] += (uint) 'a' - 26;
00107                 else if (bufOut[x] >= 52 && bufOut[x] <= 61)
00108                         bufOut[x] += (uint) '0' - 52;
00109                 else if (bufOut[x] == 62)
00110                         bufOut[x] = '+';
00111                 else if (bufOut[x] == 63)
00112                         bufOut[x] = '/';
00113                 else if (bufOut[x] == 64)
00114                         bufOut[x] = '=';
00115 
00116                 dest->append(bufOut[x]);
00117         }
00118 }
00119 
00120 QString MiscFunctions::decodeBase64(const QString &src)
00121 {
00122         char plain[4];
00123         char *destPtr;
00124         QByteArray buffer;
00125         uint bufCount = 0, pos = 0, decodedCount, x;
00126         
00127         buffer.resize(src.length() * 3 / 4);
00128         destPtr = buffer.data();
00129         
00130         while (pos < src.length()) {
00131                 decodedCount = 4;
00132                 x = 0;
00133                 while ((x < 4) && (pos < src.length())) {
00134                         plain[x] = src[pos].latin1();
00135                         pos++;
00136                         if (plain[x] == '\r' || plain[x] == '\n' || plain[x] == ' ')
00137                                 x--;
00138                         x++;
00139                 }
00140                 if (x > 1) {
00141                         decodedCount = decodeBase64Base(plain, destPtr);
00142                         destPtr += decodedCount;
00143                         bufCount += decodedCount;
00144                 }
00145         }
00146         
00147         buffer.resize(bufCount);
00148         return QString(buffer);
00149 }
00150 
00151 int MiscFunctions::decodeBase64Base(char *src, char *bufOut)
00152 {
00153         char c, z;
00154         char li[4];
00155         int processed;
00156 
00157         for (int x = 0; x < 4; x++) {
00158                 c = src[x];
00159         
00160                 if ( (int) c >= 'A' && (int) c <= 'Z')
00161                         li[x] = (int) c - (int) 'A';
00162                 if ( (int) c >= 'a' && (int) c <= 'z')
00163                         li[x] = (int) c - (int) 'a' + 26;
00164                 if ( (int) c >= '0' && (int) c <= '9')
00165                         li[x] = (int) c - (int) '0' + 52;
00166                 if (c == '+')
00167                         li[x] = 62;
00168                 if (c == '/')
00169                         li[x] = 63;
00170         }
00171         
00172         processed = 1;
00173         bufOut[0] = (char) li[0] & (32+16+8+4+2+1);
00174         bufOut[0] <<= 2;
00175         z = li[1] >> 4;
00176         bufOut[0] = bufOut[0] | z;
00177                 
00178         if (src[2] != '=') {
00179                 bufOut[1] = (char) li[1] & (8+4+2+1);
00180                 bufOut[1] <<= 4;
00181                 z = li[2] >> 2;
00182                 bufOut[1] = bufOut[1] | z;
00183                 processed++;
00184 
00185                 if (src[3] != '=') {
00186                         bufOut[2] = (char) li[2] & (2+1);
00187                         bufOut[2] <<= 6;
00188                         z = li[3];
00189                         bufOut[2] = bufOut[2] | z;
00190                         processed++;
00191                 }
00192         }
00193         return processed;
00194 }
00195 
00196 QString MiscFunctions::uniqueString()
00197 {
00198         QString uniqueString = QDate::currentDate().toString();
00199         uniqueString        += QTime::currentTime().toString();
00200         uniqueString        += QString("%1").arg(rand());
00201 
00202         unsigned char md[16];
00203 
00204         MD5_CTX ctx;
00205         MD5_Init(&ctx);
00206         MD5_Update(&ctx, (unsigned char *)uniqueString.latin1(), uniqueString.length());
00207         MD5_Final(md, &ctx);
00208 
00209         char hash[16];
00210         for (unsigned int i = 0; i < sizeof(md); i++)
00211                 sprintf(hash + 2 * i, "%02x", md[i]);
00212 
00213         return hash;
00214 }
00215 
00216 QString MiscFunctions::rfcDate()
00217 {
00218         time_t t = time(NULL);
00219         tm *time = localtime(&t);
00220         QString pm, tzh, tzm, ths, tms, tss;
00221 
00222         time->tm_gmtoff < 0 ? pm = "-" : pm = "+";
00223         int h = abs(time->tm_gmtoff) / 3600;
00224         int m = (abs(time->tm_gmtoff) - h * 3600) / 60;
00225         h < 10 ? tzh = QString("0%1").arg(h) : tzh = QString("%1").arg(h);
00226         m < 10 ? tzm = QString("0%1").arg(m) : tzm = QString("%1").arg(m);
00227 
00228         int th = time->tm_hour;
00229         int tm = time->tm_min;
00230         int ts = time->tm_sec;
00231         th < 10 ? ths = QString("0%1").arg(th) : ths = QString("%1").arg(th);
00232         tm < 10 ? tms = QString("0%1").arg(tm) : tms = QString("%1").arg(tm);
00233         ts < 10 ? tss = QString("0%1").arg(ts) : tss = QString("%1").arg(ts);
00234 
00235         QString month = QDate().monthName(time->tm_mon + 1);
00236         QString dayna = QDate().dayName(time->tm_wday);
00237         QString tzone = pm + tzh + tzm;
00238 
00239         return QString("%1, %2 %3 %4 %5:%6:%7 %8")
00240                 .arg(dayna)
00241                 .arg(time->tm_mday)
00242                 .arg(month)
00243                 .arg(time->tm_year + 1900)
00244                 .arg(ths)
00245                 .arg(tms)
00246                 .arg(tss)
00247                 .arg(tzone);
00248 }
00249 
00250 QString MiscFunctions::smtpAuthCramMd5(const QString &data, const QString &key)
00251 {
00252         MD5_CTX context;
00253         unsigned char k_ipad[65];
00254         unsigned char k_opad[65];
00255         unsigned char tk[16];
00256         unsigned char digest[16];
00257         unsigned char *key_int = (unsigned char *)key.latin1();
00258         char hash[33];
00259 
00260         if (key.length() > 64) {
00261                 MD5_CTX tctx;
00262                 MD5_Init(&tctx);
00263                 MD5_Update(&tctx, key_int, sizeof(key_int));
00264                 MD5_Final(tk, &tctx);
00265 
00266                 key_int = tk;
00267         }       
00268 
00269         bzero(k_ipad, sizeof k_ipad);
00270         bzero(k_opad, sizeof k_opad);
00271         bcopy(key_int, k_ipad, sizeof(key_int));
00272         bcopy(key_int, k_opad, sizeof(key_int));
00273 
00274         for (int i = 0; i < 64; i++) {
00275                 k_ipad[i] ^= 0x36;
00276                 k_opad[i] ^= 0x5c;
00277         }
00278 
00279         MD5_Init(&context);
00280         MD5_Update(&context, k_ipad, 64);
00281         MD5_Update(&context, (unsigned char *)data.latin1(), data.length());
00282         MD5_Final(digest, &context);
00283 
00284         MD5_Init(&context);
00285         MD5_Update(&context, k_opad, 64);
00286         MD5_Update(&context, digest, 16);
00287         MD5_Final(digest, &context);
00288 
00289         for (unsigned int i = 0; i < sizeof(digest); i++)
00290                 sprintf (hash + 2 * i, "%02x", digest[i]);
00291 
00292         return hash;
00293 }
00294 

Generated on Sat Nov 5 16:18:08 2005 for OPIE by  doxygen 1.4.2