Files
mongo/util/base64.cpp

110 lines
3.3 KiB
C++
Raw Normal View History

2009-09-30 23:08:33 -04:00
// 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.
*/
2009-09-30 23:08:33 -04:00
2010-04-27 15:27:52 -04:00
#include "pch.h"
2010-03-11 12:24:05 -05:00
#include "base64.h"
2009-09-30 23:08:33 -04:00
namespace mongo {
namespace base64 {
2011-01-04 00:40:41 -05:00
2010-03-11 12:24:05 -05:00
Alphabet alphabet;
2009-09-30 23:32:28 -04:00
2011-01-04 00:40:41 -05:00
void encode( stringstream& ss , const char * data , int size ) {
for ( int i=0; i<size; i+=3 ) {
2009-09-30 23:08:33 -04:00
int left = size - i;
const unsigned char * start = (const unsigned char*)data + i;
2011-01-04 00:40:41 -05:00
2009-09-30 23:08:33 -04:00
// byte 0
2009-10-01 01:26:19 -04:00
ss << alphabet.e(start[0]>>2);
2011-01-04 00:40:41 -05:00
2009-09-30 23:08:33 -04:00
// byte 1
unsigned char temp = ( start[0] << 4 );
2011-01-04 00:40:41 -05:00
if ( left == 1 ) {
2009-10-01 01:26:19 -04:00
ss << alphabet.e(temp);
2009-09-30 23:08:33 -04:00
break;
}
2009-10-01 01:26:19 -04:00
temp |= ( ( start[1] >> 4 ) & 0xF );
ss << alphabet.e(temp);
2009-09-30 23:08:33 -04:00
// byte 2
temp = ( start[1] & 0xF ) << 2;
2011-01-04 00:40:41 -05:00
if ( left == 2 ) {
2009-10-01 01:26:19 -04:00
ss << alphabet.e(temp);
2009-09-30 23:08:33 -04:00
break;
}
2009-10-01 01:26:19 -04:00
temp |= ( ( start[2] >> 6 ) & 0x3 );
ss << alphabet.e(temp);
2009-09-30 23:08:33 -04:00
// byte 3
2009-10-01 01:26:19 -04:00
ss << alphabet.e(start[2] & 0x3f);
2009-09-30 23:08:33 -04:00
}
int mod = size % 3;
2011-01-04 00:40:41 -05:00
if ( mod == 1 ) {
2009-09-30 23:08:33 -04:00
ss << "==";
}
2011-01-04 00:40:41 -05:00
else if ( mod == 2 ) {
2009-09-30 23:08:33 -04:00
ss << "=";
}
}
2011-01-04 00:40:41 -05:00
string encode( const char * data , int size ) {
2009-09-30 23:08:33 -04:00
stringstream ss;
encode( ss , data ,size );
return ss.str();
}
2011-01-04 00:40:41 -05:00
string encode( const string& s ) {
2009-09-30 23:32:28 -04:00
return encode( s.c_str() , s.size() );
}
2011-01-04 00:40:41 -05:00
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();
2009-09-30 23:32:28 -04:00
int size = s.size();
2011-01-04 00:40:41 -05:00
unsigned char buf[3];
2011-01-04 00:40:41 -05:00
for ( int i=0; i<size; i+=4) {
const unsigned char * start = data + i;
2009-10-01 01:26:19 -04:00
buf[0] = ( ( alphabet.decode[start[0]] << 2 ) & 0xFC ) | ( ( alphabet.decode[start[1]] >> 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 ) );
2011-01-04 00:40:41 -05:00
2009-10-01 09:49:17 -04:00
int len = 3;
2011-01-04 00:40:41 -05:00
if ( start[3] == '=' ) {
2009-10-01 09:49:17 -04:00
len = 2;
2011-01-04 00:40:41 -05:00
if ( start[2] == '=' ) {
2009-10-01 09:49:17 -04:00
len = 1;
}
}
ss.write( (const char*)buf , len );
2009-09-30 23:32:28 -04:00
}
}
2011-01-04 00:40:41 -05:00
string decode( const string& s ) {
2009-09-30 23:32:28 -04:00
stringstream ss;
decode( ss , s );
return ss.str();
}
2009-09-30 23:08:33 -04:00
}
}