30 #include <boost/array.hpp>
31 #include <boost/bimap.hpp>
32 #include <boost/assign.hpp>
45 uint_a_char_bimap
TRADUCCIO = boost::assign::list_of<uint_a_char_bimap::relation>
120 if (s.length() % 3 == 1) {
125 else if (s.length() % 3 == 2) {
129 assert( 0 == (s.length() % 3) );
131 r.reserve(static_cast<size_t>(s.length() * 1.34));
132 typedef bitset<sizeof(int)*8> bset;
133 for (
auto it=s.begin(); it != s.end(); it+=3) {
135 const unsigned char a = *it,
138 typedef unsigned int uint;
139 const unsigned long abc = a<<16 | b<<8 | c;
140 const uint x1 =
static_cast<uint
>( abc & 0x00003f),
141 x2 = static_cast<uint>((abc & 0x000fc0) >> 6),
142 x3 = static_cast<uint>((abc & 0x03f000) >> 12),
143 x4 = static_cast<uint>((abc & 0xfc0000) >> 18);
144 r.push_back(TRADUCCIO.left.at(x4));
145 r.push_back(TRADUCCIO.left.at(x3));
146 r.push_back(TRADUCCIO.left.at(x2));
147 r.push_back(TRADUCCIO.left.at(x1));
150 for (
auto rit = r.rbegin(); rit != r.rbegin()+padding; ++rit) {
158 if ( valids == 0 ) {
return ""; }
160 unsigned long bytes = 0;
161 for (
size_t idx=0; idx<valids; ++idx) {
162 bytes = bytes << 6 | TRADUCCIO.right.at(bloc[idx]);
164 const size_t offset_ajust = (4-valids)*6;
165 bytes = bytes << offset_ajust;
166 assert( 0 == (bytes & 0xFF000000) );
167 const char d3 = bytes & 0x0000FF,
168 d2 = (bytes & 0x00FF00) >> 8,
169 d1 = (bytes & 0xFF0000) >> 16;
176 switch (offset_ajust) {
178 r.erase(r.length()-1);
181 r.erase(r.length()-2);
184 r.erase(r.length()-3);
194 s.reserve(S.length());
197 for (
auto it=S.begin(); it!=S.end(); ++it) {
199 TRADUCCIO.right.at(*it);
202 catch (
const std::out_of_range &) {
207 r.reserve(s.length());
208 boost::array<char,4> bloc;
211 for (
auto it=s.begin(); it != s.end(); ++it) {
219 const size_t resta = (s.length() % 4);