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>
20 enum { LF = 0xA, CR = 0xD, NL =
'\n' };
21 enum { default_maxlen = 76 };
22 enum { eq_sign = 100 };
23 static const char sEncTable[];
24 static const signed char sDecTable[];
25 static const int sDecTableSz;
27 class Encoder;
class Decoder;
28 typedef Encoder encoder_type;
29 typedef Decoder decoder_type;
39 enum { pad_idx = 64 };
44 template<
typename OutIt>
45 inline void writeBuf(OutIt& out)
47 int pad_count = 3 - m_cidx;
50 idx[0] = m_ch[0] >> 2;
54 idx[1] = (((m_ch[0] & 3) << 4) | (m_ch[1] >> 4));
55 idx[2] = ((m_ch[1] & 0xf) << 2) | (m_ch[2] >> 6);
56 idx[3] = m_ch[2] & 0x3f;
59 idx[1] = (((m_ch[0] & 3) << 4) | (m_ch[1] >> 4));
60 idx[2] = (m_ch[1] & 0xf) << 2 ;
64 idx[1] = (m_ch[0] & 3) << 4;
65 idx[2] = idx[3] = pad_idx;
68 for(
int i = 0; i < 4; ++i)
70 *out = sEncTable[ idx[i] ]; ++out;
71 if(m_maxlen && ++m_pos > m_maxlen)
87 : m_cidx(0), m_pos(1), m_maxlen(maxlen)
89 memset(&m_ch, 0,
sizeof(m_ch));
92 const char*
name()
const {
return "Base64"; }
96 template<
typename InIt,
typename OutIt>
97 void process(InIt bit, InIt eit, OutIt out)
99 for(; bit != eit; ++bit)
101 m_ch[m_cidx++] = (char_type)*bit;
122 template<
typename OutIt>
133 template<
typename OutIt>
151 template<
typename OutIt>
152 inline void writeBuf(OutIt& out)
162 m_ch[2] = m_ch[3] = eq_sign;
170 *out = (m_ch[0] << 2 | ((m_ch[1] >> 4) & 0x3) ); ++out;
171 if(m_ch[2] == eq_sign)
return;
172 *out = (m_ch[1] << 4 | ((m_ch[2] >> 2) & 0xF) ); ++out;
173 if(m_ch[3] == eq_sign)
return;
174 *out = (m_ch[2] << 6 | m_ch[3]); ++out;
183 const char*
name()
const {
return "Base64"; }
188 template<
typename InIt,
typename OutIt>
189 inline void process(InIt bit, InIt eit, OutIt out)
193 for(; bit != eit; ++bit)
196 if(c >= sDecTableSz || sDecTable[c] == -1)
198 m_ch[m_cidx++] = sDecTable[c];
220 template<
typename OutIt>
223 if(c >= sDecTableSz || sDecTable[c] == -1)
225 m_ch[m_cidx++] = sDecTable[c];
233 template<
typename OutIt>
Base64 decoder.
Definition base64.h:147
void process(char_type c, OutIt &out)
Definition base64.h:221
const char * name() const
Definition base64.h:183
void process(InIt bit, InIt eit, OutIt out)
Definition base64.h:189
Decoder()
Definition base64.h:178
void flush(OutIt &out)
Definition base64.h:234
Base64 encoder.
Definition base64.h:38
void process(char_type c, OutIt &out)
Definition base64.h:123
double codeSizeMultiplier() const
Definition base64.h:81
const char * name() const
Definition base64.h:92
void process(InIt bit, InIt eit, OutIt out)
Definition base64.h:97
void flush(OutIt &out)
Definition base64.h:134
Encoder(int maxlen=default_maxlen)
Definition base64.h:86
Base class for buffered codecs.
Definition codec_base.h:48