--- /dev/null
+
+#include <QString>
+#include <QByteArray>
+#include <QDebug>
+#include <Qt>
+
+static QString keystring = QString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
+
+QString toBase64(QByteArray binary) {
+ QString retval;
+ int i;
+ int pad = binary.size() % 3;
+ for(i = 0; i < binary.size(); i+=3) {
+ quint8 a = 0;
+ quint8 b = 0;
+ quint8 c = 0;
+ a = binary[i];
+ retval.append(keystring[(a & 0xfc) >> 2 ]);
+ if( i+1 < binary.size()) {
+ b = binary[i+1];
+ quint8 idex = ((a & 0x03) << 4) | ((b & 0xf0) >> 4);
+ retval.append(keystring[idex]);
+ if (i+2 == binary.size()) {
+ retval.append(keystring[(b & 0x0f) << 2]);
+ }
+ } else {
+ retval.append(keystring[(a & 0x03) << 4]);
+ break;
+ }
+ if( i+2 < binary.size()) {
+ c = binary[i+2];
+ quint8 idex = ((b & 0x0f) << 2) | ((c & 0xc0) >> 6);
+ retval.append(keystring[idex]);
+ retval.append(keystring[c & 0x3f]);
+ }
+ }
+ if(pad == 1)
+ retval.append("==");
+ if(pad == 2)
+ retval.append("=");
+ return retval;
+}
+
+QByteArray fromBase64(QString text) {
+ QByteArray retval;
+ if( text.size() % 4 != 0)
+ return retval;
+ for(int i = 0; i < text.size(); i+=4) {
+ quint8 idex1 = static_cast<quint8>(keystring.indexOf(text[i])); // Something between 0 and 63
+ quint8 idex2 = static_cast<quint8>(keystring.indexOf(text[i+1]));
+ quint8 idex3 = static_cast<quint8>(keystring.indexOf(text[i+2]));
+ quint8 idex4 = static_cast<quint8>(keystring.indexOf(text[i+3]));
+ retval.append( ((idex1 << 2) | ((idex2 & 0x30) >> 4))) ;
+ if(text[i+2] == '=')
+ return retval;
+ retval.append( ((idex2 & 0x0f) << 4) | ((idex3 & 0x3c) >> 2) ) ;
+ if(text[i+3] == '=')
+ return retval;
+ retval.append( ((idex3 & 0x03) << 6) | (idex4) ) ;
+
+ }
+ return retval;
+}
+
+/*
+ * Yay, unit tests.
+int main(void) {
+ QByteArray test = QByteArray("Texas!");
+ qDebug() << test;
+ qDebug() << toBase64(test);
+ qDebug() << fromBase64(toBase64(test));
+ return 0;
+}
+*/
INCLUDEPATH += .
# Input
-HEADERS += mainwindow.h action.h connectwidget.h connectionmanager.h shareboard.h shareboardcanvas.h
-SOURCES += main.cpp mainwindow.cpp action.cpp connectwidget.cpp connectionmanager.cpp shareboard.cpp shareboardcanvas.cpp
+HEADERS += mainwindow.h action.h connectwidget.h connectionmanager.h shareboard.h shareboardcanvas.h base64.h
+SOURCES += main.cpp mainwindow.cpp action.cpp connectwidget.cpp connectionmanager.cpp shareboard.cpp shareboardcanvas.cpp base64.cpp