//If the source data contains '\ 0', the result will be truncated QString Base64Converter::revert(const QString& text) { QByteArray byte_array = text.toLocal8Bit(); if (byte_array.size()%4 != 0) { return QObject::tr("Invalid base64 string length"); } const unsigned char* from_str = (const unsigned char*)byte_array.constData(); int to_str_len = byte_array.size() / 4 * 3; unsigned char* to_str = new unsigned char[to_str_len + 1]; to_str[to_str_len] = '\0'; int totallen = 0; for(int i = 0; i<byte_array.size()/4; i++) { int len = decodeblock( &from_str[i*4], &to_str[i*3]); totallen += len; if (len == 0) { LOG_ERROR(QObject::tr("Invalid base64 code")); delete[] to_str; return ""; } } QString result = QString::fromLocal8Bit((char*)to_str); delete[] to_str; return result; }
std::string b64decode(InputIterator begin, InputIterator end) { unsigned char in[4], out[3]; unsigned char tmp; int i, len; std::string databuf; while( begin != end ) { len = 0; for( i = 0; i < 4; i++ ) { if( begin != end ) { tmp = static_cast<unsigned char>( *begin++ ); if (tmp != '=') { in[i] = tmp; len++; } else { in[i] = 0; } } else { in[i] = 0; } } if( len ) { decodeblock( in, out, len ); for( i = 0; i < 3; i++ ) { databuf.push_back(out[i]); } } } return databuf; }