12 static const char dictionary[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
17 : _sb(sb), delim(c), delim_w(w), col(0) {
18 LOG(
"base64::ostreambuf (streambuf*)");
23 LOG(
"base64::ostreambuf::reset");
29 LOG(
"base64::ostreambuf::sync");
30 return _sb->pubsync();
33 static inline void encode(
const char* in,
char* out) {
34 LOG(
"base64::encode");
35 static const unsigned int hi6 = ((1 << 6) -1 ) << 2;
36 static const unsigned int lo2 = (1 << 2) - 1;
37 static const unsigned int hi4 = ((1 << 4) - 1) << 4;
38 static const unsigned int lo4 = (1 << 4) - 1;
39 static const unsigned int hi2 = ((1 << 2) - 1) << 6;
40 static const unsigned int lo6 = (1 << 6) - 1;
43 out[1] =
dictionary[(lo2 & in[0]) << 4 | (hi4 & in[1]) >> 4];
44 out[2] =
dictionary[(lo4 & in[1]) << 2 | (hi2 & in[2]) >> 6];
53 if (
delim_w > 0 && rcol <= len) {
54 LOG(
"\t" << rcol <<
" columns to padding");
55 ret +=
_sb->sputn(buf, rcol);
57 ret +=
_sb->sputn(buf + rcol, len - rcol);
59 LOG(
"\tcol = " << col);
62 ret +=
_sb->sputn(buf,len);
72 LOG(
"base64::ostreambuf::overflow (" << c <<
")\n\t[" << *
buf << *(
buf+1) << *(
buf+2) <<
"]");
79 *pptr() = static_cast <
char >(
c);
86 LOG(
"base64::~ostreambuf");
94 for(
int i=0; i < av; ++i) {
116 static const int window =
'Z'-
'A' + 1 ;
124 if (d >= 0 && d < window) {
129 if (d >= 0 && d < window) {
134 if (d >= 0 && d < 10) {
135 ret = d + 2 * window;
151 LOG(
"base64::index (" << c <<
") [unknown]");
159 static inline void decode(
const char* in,
char* out) {
160 LOG(
"base64::decode");
161 static const unsigned char hi2 = ((1 << 2) - 1) << 4;
162 static const unsigned char lo4 = ((1 << 4) - 1);
163 static const unsigned char hi4 = ((1 << 4) - 1) << 2;
164 static const unsigned char lo2 = ((1 << 2) - 1);
168 for (
int i=0; i < 4; ++i) {
169 _in[i] =
index(in[i]);
174 c |= (_in[1] & hi2) >> 4;
177 c = (_in[1] & lo4) << 4;
178 c |= (_in[2] & hi4) >> 2;
181 c = (_in[2] & lo2) << 6;
187 : _sb(sb), end(false), delim(d), delim_w(d_w), col(0) {
188 LOG(
"base64::istreambuf (streambuf*)");
193 LOG(
"base64::istreambuf::underflow");
196 LOG(
"\tattempt to read from an ended stream");
203 LOG(
"\texpecting delimiter at " << av);
204 int ret =
_sb->sgetn(enc, 5);
205 LOG(
"\tenc=" << enc);
207 LOG(
"\tread " << ret <<
" but wanted " << 5);
212 if (
delim != enc[av]) {
213 LOG(
"\texpected delimiter " <<
delim <<
" but got " << enc[av]);
220 for (
int i=av; i < 4; ++i) {
226 int ret =
_sb->sgetn(enc, 4);
256 LOG(
"base64::~istreambuf");
int sync()
flush as much data as possible (overloaded from streambuf)
void reset()
reset input buffer
debugging/logging support
ostreambuf(std::streambuf *sb, unsigned int width=76, char delimiter='\n')
construct using a streambuf
static void encode(const char *in, char *out)
~ostreambuf()
closes the base64 stream
static char index(char c)
int write(const char *buf, size_t len)
Takes care of inserting delimiters every delim_w characters.
general base64 decoding errors
static void decode(const char *in, char *out)
istreambuf(std::streambuf *sb, unsigned int width=76, char delimiter='\n')
construct using a streambuf
int underflow()
requests that input buffer be reloaded (overloaded from streambuf)
int overflow(int c)
write a character that surpasses buffer end (overloaded from streambuf)
C++ streambuf interface to encode/decode base64 data.
std::streamsize available() const
remaining characters in the buffer
~istreambuf()
closes the base64 stream
static const char dictionary[]
exception related to base64 encode/decode, xstream::base64 namespace