轉載自http://www.firstsolver.com/wordpress/?p=406
Base64.h
幫助
源代碼
/* ----------------------------------------------------------
* 文件名稱:Base64.h
*
* 作者:秦建輝
*
* MSN:[email protected]
* QQ:36748897
*
* 博客:http://www.firstsolver.com/wordpress/
*
* 開發環境:
* Qt Creator 2.6.0
* Qt Library 4.8.4
* Ubuntu 12.10
*
* 版本歷史:
* V1.0 2012年12月07日
* 實現Base64編解碼
------------------------------------------------------------ */
#ifndef BASE64_H
#define BASE64_H
#include <QString>
class Base64
{
public:
/*
* 功能:靜態成員函數,將字節數組轉換爲Base64編碼字符串
* 參數說明:
* binaryData:要轉換的字節數組
* 返回值:
* 轉換後得到的Base64編碼字符串
* 異常拋出:
* 無
* 說明:
* 空說明列表指出函數不拋出任何異常
* 如果一個函數聲明沒有指定異常說明,則該函數可以拋出任意類型的異常
*/
static QString encode(const QByteArray & binaryData) throw();
/*
* 功能:靜態成員函數,將Base64編碼字符串解碼爲字節數組
* 參數說明:
* base64String:要轉換的Base64編碼字符串
* 返回值:
* 解碼後得到的字節數組
* 異常拋出:
* 拋出整型異常
* -1:數據錯誤
* 說明:
* 字符串中允許任意的空白字符、回車換行符、連字符
*/
static QByteArray decode(const QString & base64String) throw(int);
};
#endif // BASE64_H
#include "Base64.h"
#include <QDataStream>
static const char* DATA_BIN2ASCII = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// Base64編碼,該函數不拋出任何異常
QString Base64::encode(const QByteArray & binaryData) throw()
{
if(binaryData.isEmpty())return QString();
QString sb;
int index = 0;
for(int i = binaryData.length(); i > 0; i -= 3)
{ // 將3字節數據轉換成4個ASCII字符
if(i >= 3)
{
int b0 = binaryData[index++] & 0xFF;
int b1 = binaryData[index++] & 0xFF;
int b2 = binaryData[index++] & 0xFF;
sb.append(DATA_BIN2ASCII[b0 >> 2]);
sb.append(DATA_BIN2ASCII[((b0 << 4) | (b1 >> 4)) & 0x3F]);
sb.append(DATA_BIN2ASCII[((b1 << 2) | (b2 >> 6)) & 0x3F]);
sb.append(DATA_BIN2ASCII[b2 & 0x3F]);
}
else
{
int b0 = binaryData[index++] & 0xFF;
int b1;
if(i == 2)
b1 = binaryData[index++] & 0xFF;
else
b1 = 0;
sb.append(DATA_BIN2ASCII[b0 >> 2]);
sb.append(DATA_BIN2ASCII[((b0 << 4) | (b1 >> 4)) & 0x3F]);
if(i == 1)
sb.append('='); // 填充字符
else
sb.append(DATA_BIN2ASCII[(b1 << 2) & 0x3F]);
sb.append('='); // 填充字符
}
}
return sb;
}
#define B64_EOLN 0xF0 // 換行\n
#define B64_CR 0xF1 // 回車\r
#define B64_EOF 0xF2 // 連字符-
#define B64_WS 0xE0 // 跳格或者空格(\t、space)
#define B64_ERROR 0xFF // 錯誤字符
#define B64_NOT_BASE64 0xF3 // 空白字符、回車換行字符、連字符
static const quint8 DATA_ASCII2BIN[128] = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE0,0xF0,0xFF,0xFF,0xF1,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xE0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x3E,0xFF,0xF2,0xFF,0x3F,
0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0xFF,0xFF,0xFF,0x00,0xFF,0xFF,
0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,
0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0xFF,0xFF,0xFF,0xFF,0xFF
};
// Base64解碼,該函數拋出整型異常
QByteArray Base64::decode(const QString & base64String) throw(int)
{
if(base64String.isEmpty()) return QByteArray();
int length = base64String.length(); // 字符串長度
QByteArray ba; // 字節數組
QDataStream out(&ba, QIODevice::WriteOnly); // 寫入數據流
int index = 0;
int previous = 0;
for(int i = 0; i < length; i++)
{
int charValue = base64String[i].unicode(); // 字符的Unicode編碼
if(charValue >= 0x80) throw(-1); // 數據錯誤
int orderValue = DATA_ASCII2BIN[charValue];
if(orderValue == B64_ERROR) throw(-1); // 數據錯誤
// 跳過空白字符、回車換行字符、連字符
if((orderValue | 0x13) == B64_NOT_BASE64)
continue;
if(index == 0)
{
if(charValue == '=') throw(-1); // 數據錯誤
previous = orderValue;
index++;
}
else if(index == 1)
{
if(charValue == '=') throw(-1); // 數據錯誤
out << static_cast<quint8>((previous << 2) | (orderValue >> 4));
previous = orderValue;
index++;
}
else if(index == 2)
{
if(charValue == '=')
{ // 簡化處理,不再判斷第二個'='
index = 0;
break;
}
out << static_cast<quint8>((previous << 4) | (orderValue >> 2));
previous = orderValue;
index++;
}
else
{
index = 0;
if(charValue == '=')break;
out << static_cast<quint8>((previous << 6) | orderValue);
}
} // End for
if(index == 0) // 有效字符個數必須是4的倍數
return ba;
else
throw(-1); // 數據錯誤
}