]> git.zarvox.org Git - shareboard.git/blob - base64.cpp
Implement image insertion and placement, tool selection, and eraser.
[shareboard.git] / base64.cpp
1
2 #include <QString>
3 #include <QByteArray>
4 #include <QDebug>
5 #include <Qt>
6
7 static QString keystring = QString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
8
9 QString toBase64(QByteArray binary) {
10         QString retval;
11         int i;
12         int pad = binary.size() % 3;
13         for(i = 0; i < binary.size(); i+=3) {
14                 quint8 a = 0;
15                 quint8 b = 0;
16                 quint8 c = 0;
17                 a = binary[i];
18                 retval.append(keystring[(a & 0xfc) >> 2 ]);
19                 if( i+1 < binary.size()) {
20                         b = binary[i+1];
21                         quint8 idex = ((a & 0x03) << 4) |  ((b & 0xf0) >> 4);
22                         retval.append(keystring[idex]);
23                         if (i+2 == binary.size()) {
24                                 retval.append(keystring[(b & 0x0f) << 2]);
25                         }
26                 } else {
27                         retval.append(keystring[(a & 0x03) << 4]);
28                         break;
29                 }
30                 if( i+2 < binary.size()) {
31                         c = binary[i+2];
32                         quint8 idex = ((b & 0x0f) << 2) | ((c & 0xc0) >> 6);
33                         retval.append(keystring[idex]);
34                         retval.append(keystring[c & 0x3f]);
35                 }
36         }
37         if(pad == 1)
38                 retval.append("==");
39         if(pad == 2)
40                 retval.append("=");
41         return retval;
42 }
43
44 QByteArray fromBase64(QString text) {
45         QByteArray retval;
46         if( text.size() % 4 != 0)
47                 return retval;
48         for(int i = 0; i < text.size(); i+=4) {
49                 quint8 idex1 = static_cast<quint8>(keystring.indexOf(text[i])); // Something between 0 and 63
50                 quint8 idex2 = static_cast<quint8>(keystring.indexOf(text[i+1]));
51                 quint8 idex3 = static_cast<quint8>(keystring.indexOf(text[i+2]));
52                 quint8 idex4 = static_cast<quint8>(keystring.indexOf(text[i+3]));
53                 retval.append( ((idex1 << 2) | ((idex2 & 0x30) >> 4))) ;
54                 if(text[i+2] == '=')
55                         return retval;
56                 retval.append( ((idex2 & 0x0f) << 4) | ((idex3 & 0x3c) >> 2)  ) ;
57                 if(text[i+3] == '=')
58                         return retval;
59                 retval.append( ((idex3 & 0x03) << 6) | (idex4)  ) ;
60
61         }
62         return retval;
63 }
64
65 /*
66  * Yay, unit tests.
67 int main(void) {
68         QByteArray test = QByteArray("Texas!");
69         qDebug() << test;
70         qDebug() << toBase64(test);
71         qDebug() << fromBase64(toBase64(test));
72         return 0;
73 }
74 */