]> git.zarvox.org Git - shareboard.git/commitdiff
Implement base64 encoding and decoding, so we can serialize images.
authorDrew Fisher <drew.m.fisher@gmail.com>
Thu, 7 Oct 2010 20:28:50 +0000 (13:28 -0700)
committerDrew Fisher <drew.m.fisher@gmail.com>
Thu, 7 Oct 2010 20:28:50 +0000 (13:28 -0700)
base64.cpp [new file with mode: 0644]
base64.h [new file with mode: 0644]
shareboard.pro

diff --git a/base64.cpp b/base64.cpp
new file mode 100644 (file)
index 0000000..a4fa948
--- /dev/null
@@ -0,0 +1,74 @@
+
+#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;
+}
+*/
diff --git a/base64.h b/base64.h
new file mode 100644 (file)
index 0000000..b57ddb6
--- /dev/null
+++ b/base64.h
@@ -0,0 +1,9 @@
+#ifndef __BASE64_H__
+#define __BASE64_H__
+#include <QString>
+#include <QByteArray>
+
+QString toBase64(QByteArray binary);
+QByteArray fromBase64(QString text);
+
+#endif // __BASE64_H__
index 7a3909b225fb540702e8bb96833b52c72583f3de..8ffb5819aa5ac5ae4b0b9794bd363535733aa7f0 100644 (file)
@@ -10,5 +10,5 @@ DEPENDPATH += .
 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