From: Drew Fisher Date: Thu, 7 Oct 2010 20:28:50 +0000 (-0700) Subject: Implement base64 encoding and decoding, so we can serialize images. X-Git-Url: http://git.zarvox.org/login?a=commitdiff_plain;h=65963cf96b801fc8d4322de69d142ca03d16b712;p=shareboard.git Implement base64 encoding and decoding, so we can serialize images. --- diff --git a/base64.cpp b/base64.cpp new file mode 100644 index 0000000..a4fa948 --- /dev/null +++ b/base64.cpp @@ -0,0 +1,74 @@ + +#include +#include +#include +#include + +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(keystring.indexOf(text[i])); // Something between 0 and 63 + quint8 idex2 = static_cast(keystring.indexOf(text[i+1])); + quint8 idex3 = static_cast(keystring.indexOf(text[i+2])); + quint8 idex4 = static_cast(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 index 0000000..b57ddb6 --- /dev/null +++ b/base64.h @@ -0,0 +1,9 @@ +#ifndef __BASE64_H__ +#define __BASE64_H__ +#include +#include + +QString toBase64(QByteArray binary); +QByteArray fromBase64(QString text); + +#endif // __BASE64_H__ diff --git a/shareboard.pro b/shareboard.pro index 7a3909b..8ffb581 100644 --- a/shareboard.pro +++ b/shareboard.pro @@ -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