0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

simple base64::encode

This commit is contained in:
Eliot Horowitz 2009-09-30 23:08:33 -04:00
parent c9bbecda69
commit fdd776a50a
4 changed files with 129 additions and 1 deletions

View File

@ -256,7 +256,8 @@ if GetOption( "extrapath" ) is not None:
# ------ SOURCE FILE SETUP -----------
commonFiles = Split( "stdafx.cpp buildinfo.cpp db/jsobj.cpp db/json.cpp db/commands.cpp db/lasterror.cpp db/nonce.cpp db/queryutil.cpp shell/mongo.cpp" )
commonFiles += [ "util/background.cpp" , "util/mmap.cpp" , "util/sock.cpp" , "util/util.cpp" , "util/message.cpp" , "util/assert_util.cpp" , "util/httpclient.cpp" , "util/md5main.cpp" ]
commonFiles += [ "util/background.cpp" , "util/mmap.cpp" , "util/sock.cpp" , "util/util.cpp" , "util/message.cpp" ,
"util/assert_util.cpp" , "util/httpclient.cpp" , "util/md5main.cpp" , "util/base64.cpp" ]
commonFiles += Glob( "util/*.c" )
commonFiles += Split( "client/connpool.cpp client/dbclient.cpp client/model.cpp" )
commonFiles += [ "scripting/engine.cpp" ]

View File

@ -20,6 +20,7 @@
#include "stdafx.h"
#include "dbtests.h"
#include "../util/base64.h"
namespace BasicTests {
@ -47,6 +48,18 @@ namespace BasicTests {
}
};
class Base64Tests {
public:
void run(){
base64::testAlphabet();
ASSERT_EQUALS( "ZWxp" , base64::encode( "eli" , 3 ) );
ASSERT_EQUALS( "ZWxpb3Rz" , base64::encode( "eliots" , 6 ) );
ASSERT_EQUALS( "ZQ==" , base64::encode( "e" , 1 ) );
ASSERT_EQUALS( "ZWw=" , base64::encode( "el" , 2 ) );
}
};
class All : public Suite {
public:
All() : Suite( "basic" ){
@ -54,6 +67,7 @@ namespace BasicTests {
void setupTests(){
add< Rarely >();
add< Base64Tests >();
}
} myall;

86
util/base64.cpp Normal file
View File

@ -0,0 +1,86 @@
// util/base64.cpp
/**
* Copyright (C) 2008 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
namespace mongo {
namespace base64 {
const char * alphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"+/";
void testAlphabet(){
assert( strlen( alphabet ) == 64 );
for ( int i=0; i<26; i++ )
assert( alphabet[i] == toupper( alphabet[i+26] ) );
}
void encode( stringstream& ss , const char * data , int size ){
for ( int i=0; i<size; i+=3 ){
int left = size - i;
const char * start = data + i;
// byte 0
ss << alphabet[start[0]>>2];
// byte 1
char temp = ( start[0] & 0x3 ) << 4;
if ( left == 1 ){
ss << alphabet[temp];
break;
}
temp |= start[1] >> 4;
ss << alphabet[temp];
// byte 2
temp = ( start[1] & 0xF ) << 2;
if ( left == 2 ){
ss << alphabet[temp];
break;
}
temp |= start[2] >> 6;
ss << alphabet[temp];
// byte 3
ss << alphabet[start[2] & 0x3f];
}
int mod = size % 3;
if ( mod == 1 ){
ss << "==";
}
else if ( mod == 2 ){
ss << "=";
}
}
string encode( const char * data , int size ){
stringstream ss;
encode( ss , data ,size );
return ss.str();
}
}
}

27
util/base64.h Normal file
View File

@ -0,0 +1,27 @@
// util/base64.h
/**
* Copyright (C) 2008 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
namespace mongo {
namespace base64 {
void encode( stringstream& ss , void * data , int size );
string encode( const char * data , int size );
void testAlphabet();
}
}