7 static const char* alfabet
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
9 string
base64_encode ( const string
& sInput
)
11 unsigned char x
=0, topbit
=0;
20 if ( x
<= sInput
.length() ) v
+= sInput
[x
-1];
24 if ( x
> sInput
.length() && !v
)
26 sOutput
+= alfabet
[(v
>> topbit
) & 63];
27 v
&= (1 << topbit
) - 1;
28 } while ( x
< sInput
.length() || v
);
29 int eq
= (8 - (sOutput
.length() % 4)) % 4;
35 string
base64_decode ( const string
& sInput
)
37 unsigned char x
=0, topbit
=0;
38 int v
=0, inlen
= sInput
.length();
40 while ( inlen
&& sInput
[inlen
-1] == '=' )
48 if ( x
<= inlen
) v
+= (strchr(alfabet
, sInput
[x
-1]) - alfabet
);
52 if ( x
> inlen
&& !v
)
54 sOutput
+= (char)((v
>> topbit
) & 255);
55 v
&= ((1 << topbit
) - 1);
56 } while ( x
<= inlen
|| v
);