7 #ifndef _MIMETIC_CODEC_BASE64_H_
8 #define _MIMETIC_CODEC_BASE64_H_
9 #include <mimetic/circular_buffer.h>
10 #include <mimetic/codec/codec_base.h>
11 #include <mimetic/codec/codec_chain.h>
19 enum { LF = 0xA, CR = 0xD, NL =
'\n' };
20 enum { default_maxlen = 76 };
21 enum { eq_sign = 100 };
22 static const char sEncTable[];
23 static const char sDecTable[];
24 static const int sDecTableSz;
26 class Encoder;
class Decoder;
27 typedef Encoder encoder_type;
28 typedef Decoder decoder_type;
38 enum { pad_idx = 64 };
43 template<
typename OutIt>
44 inline void writeBuf(OutIt& out)
46 int pad_count = 3 - m_cidx;
49 idx[0] = m_ch[0] >> 2;
53 idx[1] = (((m_ch[0] & 3) << 4) | (m_ch[1] >> 4));
54 idx[2] = ((m_ch[1] & 0xf) << 2) | (m_ch[2] >> 6);
55 idx[3] = m_ch[2] & 0x3f;
58 idx[1] = (((m_ch[0] & 3) << 4) | (m_ch[1] >> 4));
59 idx[2] = (m_ch[1] & 0xf) << 2 ;
63 idx[1] = (m_ch[0] & 3) << 4;
64 idx[2] = idx[3] = pad_idx;
67 for(
int i = 0; i < 4; ++i)
69 *out = sEncTable[ idx[i] ]; ++out;
70 if(m_maxlen && ++m_pos > m_maxlen)
86 : m_cidx(0), m_pos(1), m_maxlen(maxlen)
90 const char*
name()
const {
return "Base64"; }
94 template<
typename InIt,
typename OutIt>
95 void process(InIt bit, InIt eit, OutIt out)
97 for(; bit != eit; ++bit)
99 m_ch[m_cidx++] = (char_type)*bit;
120 template<
typename OutIt>
131 template<
typename OutIt>
149 template<
typename OutIt>
150 inline void writeBuf(OutIt& out)
160 m_ch[2] = m_ch[3] = eq_sign;
168 *out = (m_ch[0] << 2 | ((m_ch[1] >> 4) & 0x3) ); ++out;
169 if(m_ch[2] == eq_sign)
return;
170 *out = (m_ch[1] << 4 | ((m_ch[2] >> 2) & 0xF) ); ++out;
171 if(m_ch[3] == eq_sign)
return;
172 *out = (m_ch[2] << 6 | m_ch[3]); ++out;
181 const char*
name()
const {
return "Base64"; }
186 template<
typename InIt,
typename OutIt>
187 inline void process(InIt bit, InIt eit, OutIt out)
191 for(; bit != eit; ++bit)
194 if(c > sDecTableSz || sDecTable[c] == -1)
196 m_ch[m_cidx++] = sDecTable[c];
218 template<
typename OutIt>
221 if(c > sDecTableSz || sDecTable[c] == -1)
223 m_ch[m_cidx++] = sDecTable[c];
231 template<
typename OutIt>
void process(char_type c, OutIt &out)
Definition: base64.h:121
Encoder(int maxlen=default_maxlen)
Definition: base64.h:85
const char * name() const
Definition: base64.h:90
void process(char_type c, OutIt &out)
Definition: base64.h:219
Base class for buffered codecs.
Definition: codec_base.h:47
void flush(OutIt &out)
Definition: base64.h:132
Decoder()
Definition: base64.h:176
Base64 encoder.
Definition: base64.h:36
double codeSizeMultiplier() const
Definition: base64.h:80
void process(InIt bit, InIt eit, OutIt out)
Definition: base64.h:187
Base64 decoder.
Definition: base64.h:144
void flush(OutIt &out)
Definition: base64.h:232
const char * name() const
Definition: base64.h:181
void process(InIt bit, InIt eit, OutIt out)
Definition: base64.h:95