reactos/rosapps/games/ArchBlackmann/base64.cpp
Royce Mitchell III 089686cac2 ArchBlackmann irc bot - works in ReactOS - happy hacking
svn path=/trunk/; revision=12289
2004-12-22 18:25:29 +00:00

58 lines
1.1 KiB
C++

// base64.cpp
#include "base64.h"
using std::string;
static const char* alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
string base64_encode ( const string& sInput )
{
unsigned char x=0, topbit=0;
int v=0;
string sOutput;
do
{
if ( topbit < 6 )
{
x++;
v <<= 8;
if ( x <= sInput.length() ) v += sInput[x-1];
topbit += 8;
}
topbit -= 6;
if ( x > sInput.length() && !v )
break;
sOutput += alfabet[(v >> topbit) & 63];
v &= (1 << topbit) - 1;
} while ( x < sInput.length() || v );
int eq = (8 - (sOutput.length() % 4)) % 4;
while ( eq-- )
sOutput += '=';
return sOutput;
}
string base64_decode ( const string& sInput )
{
unsigned char x=0, topbit=0;
int v=0, inlen = sInput.length();
string sOutput;
while ( inlen && sInput[inlen-1] == '=' )
inlen--;
do
{
while ( topbit < 8 )
{
x++;
v <<= 6;
if ( x <= inlen ) v += (strchr(alfabet, sInput[x-1]) - alfabet);
topbit += 6;
}
topbit -= 8;
if ( x > inlen && !v )
break;
sOutput += (char)((v >> topbit) & 255);
v &= ((1 << topbit) - 1);
} while ( x <= inlen || v );
return sOutput;
}