29 const char * character_map =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
32 std::string encode(
const std::string & s ,
const std::string & eol ) ;
33 std::string decode(
const std::string & s ,
bool & error ) ;
34 bool valid(
const std::string & ) ;
36 g_uint32_t numeric(
char c ) ;
37 void accumulate_8( g_uint32_t & n , std::string::const_iterator & , std::string::const_iterator ,
int & ) ;
38 size_t hi_6( g_uint32_t n ) ;
39 void generate_6( g_uint32_t & n ,
int & i , std::string & result ) ;
40 char to_char( g_uint32_t n ) ;
41 size_t index(
char c ,
bool & error ) ;
42 void accumulate_6( g_uint32_t & n ,
const std::string & ,
size_t & ,
size_t & ,
bool & error ) ;
43 g_uint32_t hi_8( g_uint32_t n ) ;
44 void generate_8( g_uint32_t & n ,
size_t & i , std::string & result ,
bool & error ) ;
49 return encode( s_in ,
"\015\012" ) ;
54 return imp::encode( s_in , eol ) ;
60 std::string result = imp::decode( s , error ) ;
68 return imp::valid( s ) ;
73 g_uint32_t imp::numeric(
char c )
75 return static_cast<g_uint32_t
>(
static_cast<unsigned char>(c) ) ;
78 void imp::accumulate_8( g_uint32_t & n , std::string::const_iterator & p ,
79 std::string::const_iterator end ,
int & i )
81 char c = p == end ?
'\0' : *p ;
91 size_t imp::hi_6( g_uint32_t n )
93 return (n >> 18U) & 0x3F ;
96 void imp::generate_6( g_uint32_t & n ,
int & i , std::string & result )
98 char c = i-- >= 0 ? character_map[hi_6(n)] : pad ;
99 result.append( 1U , c ) ;
103 std::string imp::encode(
const std::string & s_in ,
const std::string & eol )
107 for( std::string::const_iterator p = s_in.begin() ; p != s_in.end() ; blocks++ )
109 if( blocks && (blocks % 19U) == 0U )
110 result.append( eol ) ;
114 accumulate_8( n , p , s_in.end() , i ) ;
115 accumulate_8( n , p , s_in.end() , i ) ;
116 accumulate_8( n , p , s_in.end() , i ) ;
117 generate_6( n , i , result ) ;
118 generate_6( n , i , result ) ;
119 generate_6( n , i , result ) ;
120 generate_6( n , i , result ) ;
126 char imp::to_char( g_uint32_t n )
128 return static_cast<char>(
static_cast<unsigned char>(n)) ;
131 size_t imp::index(
char c ,
bool & error )
133 const char * p = std::strchr( character_map , c ) ;
134 error = error || !c || !p ;
138 return static_cast<size_t>( p - character_map ) ;
141 void imp::accumulate_6( g_uint32_t & n ,
const std::string & s ,
size_t & i ,
size_t & bits ,
bool & error )
144 if( i == s.length() )
147 else if( s.at(i) == pad )
153 n |= index( s.at(i++) , error ) ;
158 g_uint32_t imp::hi_8( g_uint32_t n )
160 return (n >> 16U) & 0xff ;
163 void imp::generate_8( g_uint32_t & n ,
size_t & bits , std::string & result ,
bool & error )
168 result.append( 1U , to_char(hi_8(n)) ) ;
177 std::string imp::decode(
const std::string & s ,
bool & error )
181 for(
const char * p = s.c_str() ; p[i] ; )
183 if( *p ==
'\r' || *p ==
'\n' )
192 accumulate_6( n , s , i , bits , error ) ;
193 accumulate_6( n , s , i , bits , error ) ;
194 accumulate_6( n , s , i , bits , error ) ;
195 accumulate_6( n , s , i , bits , error ) ;
196 if( bits < 8U ) error = true ;
197 generate_8( n , bits , result , error ) ;
198 generate_8( n , bits , result , error ) ;
199 generate_8( n , bits , result , error ) ;
204 bool imp::valid(
const std::string & s )
207 std::string result = decode( s , error ) ;
208 G_IGNORE_VARIABLE(result) ;
static bool valid(const std::string &)
Returns true if the string can be decoded without any strict errors.
static std::string decode(const std::string &, bool strict=false)
Decodes the given string.
static std::string encode(const std::string &s, const std::string &line_break)
Encodes the given string.