00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #pragma once
00019
00020 namespace mongo {
00021 namespace base64 {
00022
00023 class Alphabet {
00024 public:
00025 Alphabet()
00026 : encode((unsigned char*)
00027 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00028 "abcdefghijklmnopqrstuvwxyz"
00029 "0123456789"
00030 "+/")
00031 , decode(new unsigned char[257]) {
00032 memset( decode.get() , 0 , 256 );
00033 for ( int i=0; i<64; i++ ) {
00034 decode[ encode[i] ] = i;
00035 }
00036
00037 test();
00038 }
00039 void test() {
00040 assert( strlen( (char*)encode ) == 64 );
00041 for ( int i=0; i<26; i++ )
00042 assert( encode[i] == toupper( encode[i+26] ) );
00043 }
00044
00045 char e( int x ) {
00046 return encode[x&0x3f];
00047 }
00048
00049 private:
00050 const unsigned char * encode;
00051 public:
00052 boost::scoped_array<unsigned char> decode;
00053 };
00054
00055 extern Alphabet alphabet;
00056
00057
00058 void encode( stringstream& ss , const char * data , int size );
00059 string encode( const char * data , int size );
00060 string encode( const string& s );
00061
00062 void decode( stringstream& ss , const string& s );
00063 string decode( const string& s );
00064
00065
00066 void testAlphabet();
00067 }
00068 }