From ae1ecd9c786911f9f1f0242f0f7d702b3e5dfeba Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Sat, 24 Dec 2011 15:33:26 -0500 Subject: bulk move of code to src/ SERVER-4551 --- src/mongo/util/base64.cpp | 109 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/mongo/util/base64.cpp (limited to 'src/mongo/util/base64.cpp') diff --git a/src/mongo/util/base64.cpp b/src/mongo/util/base64.cpp new file mode 100644 index 00000000000..aff06e26126 --- /dev/null +++ b/src/mongo/util/base64.cpp @@ -0,0 +1,109 @@ +// util/base64.cpp + + +/* Copyright 2009 10gen Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "pch.h" +#include "base64.h" + +namespace mongo { + namespace base64 { + + Alphabet alphabet; + + void encode( stringstream& ss , const char * data , int size ) { + for ( int i=0; i>2); + + // byte 1 + unsigned char temp = ( start[0] << 4 ); + if ( left == 1 ) { + ss << alphabet.e(temp); + break; + } + temp |= ( ( start[1] >> 4 ) & 0xF ); + ss << alphabet.e(temp); + + // byte 2 + temp = ( start[1] & 0xF ) << 2; + if ( left == 2 ) { + ss << alphabet.e(temp); + break; + } + temp |= ( ( start[2] >> 6 ) & 0x3 ); + ss << alphabet.e(temp); + + // byte 3 + ss << alphabet.e(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(); + } + + string encode( const string& s ) { + return encode( s.c_str() , s.size() ); + } + + + void decode( stringstream& ss , const string& s ) { + uassert( 10270 , "invalid base64" , s.size() % 4 == 0 ); + const unsigned char * data = (const unsigned char*)s.c_str(); + int size = s.size(); + + unsigned char buf[3]; + for ( int i=0; i> 4 ) & 0x3 ); + buf[1] = ( ( alphabet.decode[start[1]] << 4 ) & 0xF0 ) | ( ( alphabet.decode[start[2]] >> 2 ) & 0xF ); + buf[2] = ( ( alphabet.decode[start[2]] << 6 ) & 0xC0 ) | ( ( alphabet.decode[start[3]] & 0x3F ) ); + + int len = 3; + if ( start[3] == '=' ) { + len = 2; + if ( start[2] == '=' ) { + len = 1; + } + } + ss.write( (const char*)buf , len ); + } + } + + string decode( const string& s ) { + stringstream ss; + decode( ss , s ); + return ss.str(); + } + + } +} + -- cgit v1.2.1