29 const char * character_map =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
33 g_uint32_t G::Base64::numeric(
char c )
35 return static_cast<g_uint32_t
>(
static_cast<unsigned char>(c) ) ;
38 void G::Base64::accumulate_8( g_uint32_t & n , std::string::const_iterator & p ,
39 std::string::const_iterator end ,
int & i )
41 char c = p == end ?
'\0' : *p ;
51 size_t G::Base64::hi_6( g_uint32_t n )
53 return (n >> 18U) & 0x3F ;
56 void G::Base64::generate_6( g_uint32_t & n ,
int & i , std::string & result )
58 char c = i-- >= 0 ? character_map[hi_6(n)] : pad ;
59 result.append( 1U , c ) ;
65 return encode( s_in ,
"\015\012" ) ;
72 for( std::string::const_iterator p = s_in.begin() ; p != s_in.end() ; blocks++ )
74 if( blocks && (blocks % 19U) == 0U )
75 result.append( eol ) ;
79 accumulate_8( n , p , s_in.end() , i ) ;
80 accumulate_8( n , p , s_in.end() , i ) ;
81 accumulate_8( n , p , s_in.end() , i ) ;
82 generate_6( n , i , result ) ;
83 generate_6( n , i , result ) ;
84 generate_6( n , i , result ) ;
85 generate_6( n , i , result ) ;
93 char G::Base64::to_char( g_uint32_t n )
95 return static_cast<char>(
static_cast<unsigned char>(n)) ;
98 size_t G::Base64::index(
char c ,
bool & error )
100 const char * p = std::strchr( character_map , c ) ;
101 error = error || !c || !p ;
105 return static_cast<size_t>( p - character_map ) ;
108 size_t G::Base64::accumulate_6( g_uint32_t & n ,
char c_in ,
int & n_out ,
bool & error )
113 n |= index(c_in,error) ;
116 return c_in !=
'\0' ;
119 g_uint32_t G::Base64::hi_8( g_uint32_t n )
121 return (n >> 16U) & 0xff ;
124 void G::Base64::generate_8( g_uint32_t & n ,
int & n_out , std::string & result )
127 result.append( 1U , to_char(hi_8(n)) ) ;
134 std::string result = decode( s , error ) ;
143 for(
const char * p = s.c_str() ; *p ; )
145 if( *p ==
'\r' || *p ==
'\n' )
154 i += accumulate_6( n , p[i] , n_out , error ) ;
155 i += accumulate_6( n , p[i] , n_out , error ) ;
156 i += accumulate_6( n , p[i] , n_out , error ) ;
157 i += accumulate_6( n , p[i] , n_out , error ) ;
159 generate_8( n , n_out , result ) ;
160 generate_8( n , n_out , result ) ;
161 generate_8( n , n_out , result ) ;
169 G_IGNORE_RETURN(std::string) decode( s , error ) ;
static bool valid(const std::string &)
Returns true if the string can be decoded.
static std::string decode(const std::string &)
Decodes the given string.
static std::string encode(const std::string &s, const std::string &line_break)
Encodes the given string.