7 #ifndef _MIMETIC_CODEC_QP_H_
8 #define _MIMETIC_CODEC_QP_H_
13 #include <mimetic/libconfig.h>
14 #include <mimetic/utils.h>
15 #include <mimetic/circular_buffer.h>
16 #include <mimetic/codec/codec_base.h>
17 #include <mimetic/codec/codec_chain.h>
25 enum { LF = 0xA, CR = 0xD, NL = LF, TAB = 9, SP = 32 };
26 enum { default_maxlen = 76 };
47 size_t m_pos, m_maxlen;
49 circular_buffer<char_type> m_cbuf;
51 template<
typename OutIt>
52 void hardLineBrk(OutIt& out)
57 template<
typename OutIt>
58 void softLineBrk(OutIt& out)
63 template<
typename OutIt>
64 void write(char_type ch, OutIt& out)
66 bool is_last_ch = m_cbuf.empty();
67 if(!is_last_ch && m_pos == m_maxlen)
72 template<
typename OutIt>
73 void writeHex(char_type ch, OutIt& out)
75 static char_type hexc[] =
77 '0',
'1',
'2',
'3',
'4',
'5' ,
'6',
'7',
'8',
'9',
78 'A',
'B',
'C',
'D',
'E',
'F'
80 bool is_last_ch = m_cbuf.empty();
81 if(m_pos + (is_last_ch ? 1 : 2) >= m_maxlen)
85 *out = hexc[ch >> 4]; ++out;
86 *out = hexc[ch & 0xf]; ++out;
89 template<
typename OutIt>
90 void encodeChar(char_type c, OutIt& out)
92 int cnt = m_cbuf.count();
101 if(cnt>=4 && m_cbuf.compare(0,4,
"rom "))
108 if(!cnt || sTb[ m_cbuf[0] ] == newline)
121 if(m_binary || !cnt || sTb[ m_cbuf[0] ] == newline)
130 if(cnt && m_cbuf[0] == (c == CR ? LF : CR))
136 if(!m_binary) m_binary = 1;
161 : m_pos(1), m_maxlen(default_maxlen),
162 m_binary(isBinary), m_cbuf(laBufSz)
166 const char*
name()
const {
return "Quoted-Printable"; }
183 template<
typename InIt,
typename OutIt>
186 for(; bit != eit; ++bit)
203 template<
typename OutIt>
206 m_cbuf.push_back(ic);
207 if(m_cbuf.count() < laBufSz)
209 char_type c = m_cbuf.front();
216 template<
typename OutIt>
220 while(!m_cbuf.empty())
236 enum { laBufSz = 80 };
246 size_t m_pos, m_maxlen;
252 template<
typename OutIt>
253 void hardLineBrk(OutIt& out)
const
257 template<
typename OutIt>
258 void write(char_type ch, OutIt& out)
const
262 bool isnl(char_type c)
const
264 return (c == CR || c == LF);
266 template<
typename OutIt>
267 void flushPrev(OutIt& out)
269 copy(m_prev.begin(), m_prev.end(), out);
272 int hex_to_int(char_type c)
const
274 if( c >=
'0' && c <=
'9')
return c -
'0';
275 else if( c >=
'A' && c <=
'F')
return c -
'A' + 10;
276 else if( c >=
'a' && c <=
'f')
return c -
'a' + 10;
279 bool ishex(char_type c)
const
281 return (c >=
'0' && c <=
'9') ||
282 (c >=
'A' && c <=
'F') ||
283 (c >=
'a' && c <=
'f');
285 template<
typename OutIt>
286 void decodeChar(char_type c, OutIt& out)
298 m_state = sWaitingChar;
301 m_state = sWaitingChar;
313 if(m_prev.length() > 1)
317 m_state = sWaitingChar;
319 m_state = sWaitingFirstHex;
323 case sWaitingFirstHex:
329 m_state = sWaitingChar;
333 m_state = sWaitingSecondHex;
336 case sWaitingSecondHex:
343 assert(m_prev.length());
344 last = m_prev[m_prev.length()-1];
345 oc = hex_to_int(last) << 4 |
350 m_state = sWaitingChar;
358 int len = m_prev.length();
359 if(!len || m_prev[0] !=
'=')
362 m_state = sWaitingChar;
364 is2Ch = (c == (m_nl == CR ? LF : CR));
378 }
else if(c ==
'=') {
385 if(c < 32 && c != TAB)
400 : m_state(sWaitingChar), m_nl(0)
404 const char*
name()
const {
return "Quoted-Printable"; }
421 template<
typename InIt,
typename OutIt>
424 for(;bit != eit; ++bit)
425 decodeChar(*bit, out);
442 template<
typename OutIt>
450 template<
typename OutIt>
466 int len = m_prev.length();
476 write(m_prev[1], out);
void process(char_type ic, OutIt &out)
Definition: qp.h:204
void process(char_type ic, OutIt &out)
Definition: qp.h:443
size_t maxlen()
Definition: qp.h:406
size_t maxlen()
Definition: qp.h:168
Encoder(bool isBinary=false)
Definition: qp.h:160
quoted-printable decoder
Definition: qp.h:234
Base class for buffered codecs.
Definition: codec_base.h:47
const char * name() const
Definition: qp.h:404
double codeSizeMultiplier() const
Definition: qp.h:147
const char * name() const
Definition: qp.h:166
void maxlen(size_t i)
Definition: qp.h:414
void maxlen(size_t i)
Definition: qp.h:176
void process(InIt bit, InIt eit, OutIt out)
Definition: qp.h:422
quoted-printable encoder
Definition: qp.h:44
void process(InIt bit, InIt eit, OutIt out)
Definition: qp.h:184
void flush(OutIt &out)
Definition: qp.h:217
void flush(OutIt &out)
Definition: qp.h:451
Decoder()
Definition: qp.h:399