22 static const std::string alphabet64(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" );
23 static const char pad =
'=';
24 static const char np = (char)std::string::npos;
25 static char table64vals[] =
27 62, np, np, np, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, np, np, np, np, np,
28 np, np, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
29 18, 19, 20, 21, 22, 23, 24, 25, np, np, np, np, np, np, 26, 27, 28, 29, 30, 31,
30 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
33 inline char table64(
unsigned char c )
35 return ( c < 43 || c > 122 ) ? np : table64vals[c-43];
38 const std::string
encode64(
const std::string& input )
42 const std::string::size_type length = input.length();
44 encoded.reserve( length * 2 );
46 for( std::string::size_type i = 0; i < length; ++i )
48 c =
static_cast<char>( ( input[i] >> 2 ) & 0x3f );
49 encoded += alphabet64[c];
51 c =
static_cast<char>( ( input[i] << 4 ) & 0x3f );
53 c =
static_cast<char>( c |
static_cast<char>( ( input[i] >> 4 ) & 0x0f ) );
54 encoded += alphabet64[c];
58 c =
static_cast<char>( ( input[i] << 2 ) & 0x3c );
60 c =
static_cast<char>( c |
static_cast<char>( ( input[i] >> 6 ) & 0x03 ) );
61 encoded += alphabet64[c];
71 c =
static_cast<char>( input[i] & 0x3f );
72 encoded += alphabet64[c];
83 const std::string
decode64(
const std::string& input )
86 const std::string::size_type length = input.length();
89 decoded.reserve( length );
91 for( std::string::size_type i = 0; i < length; ++i )
93 c = table64(input[i]);
95 d = table64(input[i]);
96 c =
static_cast<char>( ( c << 2 ) | ( ( d >> 4 ) & 0x3 ) );
104 c = table64(input[i]);
105 d =
static_cast<char>( ( ( d << 4 ) & 0xf0 ) | ( ( c >> 2 ) & 0xf ) );
115 d = table64(input[i]);
116 c =
static_cast<char>( ( ( c << 6 ) & 0xc0 ) | d );