00001
00012
00013
00015
00016 #include "Base64Encoder.h"
00017 #include <sstream>
00019
00021
00022 const unsigned char Base64Encoder::lookupTable[65] =
00023
00024 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00025 "abcdefghijklmnopqrstuvwxyz"
00026 "0123456789+/";
00027
00028 const unsigned char Base64Encoder::lookdownTable[256] = {
00029
00030 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00031 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00032 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00033 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00034 0, 0, 0, 62,0, 0, 0, 63,52,53,
00035 54,55,56,57,58,59,60,61,0, 0,
00036 0, 0, 0, 0, 0, 0, 1, 2, 3, 4,
00037 5, 6, 7, 8, 9,10,11,12,13,14,
00038 15,16,17,18,19,20,21,22,23,24,
00039 25,0, 0, 0, 0, 0, 0, 26,27,28,
00040 29,30,31,32,33,34,35,36,37,38,
00041 39,40,41,42,43,44,45,46,47,48,
00042 49,50,51,0, 0, 0, 0, 0, 0, 0,
00043 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00044 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00045 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00046 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00047 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00048 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00049 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00050 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00051 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00052 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00053 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00054 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00055 0, 0, 0, 0, 0, 0
00056 };
00057
00058 const int Base64Encoder::g_KsizeofBase64Buffer = 3;
00059
00060 Base64Encoder::Base64Encoder()
00061 {
00062
00063 }
00064
00065 Base64Encoder::~Base64Encoder()
00066 {
00067
00068 }
00069
00070 std::string Base64Encoder::encodeValue( void* value, unsigned long sizeofValue )
00071 {
00072 std::ostringstream retVal;
00073 unsigned char* data = reinterpret_cast<unsigned char*>(value);
00074
00075 if ( sizeofValue > 2 )
00076 for ( unsigned int i = 0; i < sizeofValue-2; i += 3 )
00077 {
00078 retVal << lookupTable[ *data >> 2 & 0x3f ];
00079 retVal << lookupTable[ ((*data << 4) & 0x3f) | ((*(data+1) >> 4) & 0xf) ]; data++;
00080 retVal << lookupTable[ ((*data << 2) & 0x3f) | ((*(data+1) >> 6) & 0x3) ]; data++;
00081 retVal << lookupTable[ *data++ & 0x3f ];
00082 }
00083
00084 int leftOver = sizeofValue % 3;
00085 if ( leftOver > 0 )
00086 {
00087 retVal << lookupTable[ *data >> 2 & 0x3f ];
00088
00089 if ( leftOver > 1 )
00090 {
00091 retVal << lookupTable[ ((*data << 4) & 0x3f) | ((*(data+1) >> 4) & 0xf) ];
00092 retVal << lookupTable[ ((*++data << 2) & 0x3f) ];
00093 }
00094 else
00095 {
00096 retVal << lookupTable[ (*data << 4) & 0x3f ];
00097 retVal << '=';
00098 }
00099 retVal << '=';
00100 }
00101 return retVal.str();
00102 }
00103
00104
00105
00106 unsigned char* Base64Encoder::decodeValue( const std::string& value, unsigned long& sizeofValue )
00107 {
00108 register int k = 0;
00109 register int strSize = value.length();
00110 register unsigned char*retval = new unsigned char[ sizeof(unsigned char) * strSize ];
00111 int i;
00112
00113 union {
00114 unsigned char bytes[4];
00115 unsigned long data;
00116 } triad;
00117 triad.data = 0;
00118
00119 for ( i = 0; i < strSize ; i++ )
00120 {
00121 triad.data <<= 6;
00122 triad.data |= lookdownTable[value[i]] ;
00123
00124 if ( ( i != 0 ) && ( ( ( i + 1 ) % 4 ) == 0 ) )
00125 {
00126 retval[k++] = triad.bytes[2];
00127 retval[k++] = triad.bytes[1];
00128 retval[k++] = triad.bytes[0];
00129 }
00130 }
00131 i--;
00132 while ( value[i--] == '=' )
00133 k--;
00134
00135 sizeofValue = k;
00136 return retval;
00137 }