/usr/include/mimetic/codec/qp.h is in libmimetic-dev 0.9.8-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | /***************************************************************************
copyright : (C) 2002-2008 by Stefano Barbato
email : stefano@codesink.org
$Id: qp.h,v 1.20 2008-10-07 11:06:26 tat Exp $
***************************************************************************/
#ifndef _MIMETIC_CODEC_QP_H_
#define _MIMETIC_CODEC_QP_H_
#include <iostream>
#include <string>
#include <sstream>
#include <cassert>
#include <mimetic/libconfig.h>
#include <mimetic/utils.h>
#include <mimetic/circular_buffer.h>
#include <mimetic/codec/codec_base.h>
#include <mimetic/codec/codec_chain.h>
namespace mimetic
{
class QP
{
friend class test_qp;
enum { LF = 0xA, CR = 0xD, NL = LF, TAB = 9, SP = 32 };
enum { default_maxlen = 76 };
enum {
printable, /* print as-is */
tab, /* print if !isBinary */
sp, /* ' ' */
newline, /* cr or lf; encode if isBinary*/
binary, /* rest of the ascii map */
unsafe /* "!\"#$@[]\\^`{}|~" */
};
static char sTb[256];
public:
/// quoted-printable encoder
/*!
\sa encode decode
*/
class Encoder: public buffered_codec, public chainable_codec<Encoder>
{
enum { laBufSz = 5 }; // look-ahead buffer
size_t m_pos, m_maxlen;
bool m_binary;
circular_buffer<char_type> m_cbuf;
template<typename OutIt>
void hardLineBrk(OutIt& out)
{
*out = NL; ++out;
m_pos = 1;
}
template<typename OutIt>
void softLineBrk(OutIt& out)
{
*out = '='; ++out;
hardLineBrk(out);
}
template<typename OutIt>
void write(char_type ch, OutIt& out)
{
bool is_last_ch = m_cbuf.empty();
if(!is_last_ch && m_pos == m_maxlen)
softLineBrk(out);
*out = ch; ++out;
m_pos++;
}
template<typename OutIt>
void writeHex(char_type ch, OutIt& out)
{
static char_type hexc[] =
{
'0', '1', '2', '3', '4', '5' ,'6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'
};
bool is_last_ch = m_cbuf.empty();
if(m_pos + (is_last_ch ? 1 : 2) >= m_maxlen)
softLineBrk(out);
// write out =HH
*out = '='; ++out;
*out = hexc[ch >> 4]; ++out;
*out = hexc[ch & 0xf]; ++out;
m_pos += 3;
}
template<typename OutIt>
void encodeChar(char_type c, OutIt& out)
{
int cnt = m_cbuf.count();
switch(sTb[c])
{
case printable:
if(m_pos == 1)
{
switch(c)
{
case 'F': // hex enc on "^From .*"
if(cnt>=4 && m_cbuf.compare(0,4,"rom "))
{
writeHex(c,out);
return;
}
break;
case '.': // hex encode if "^.[\r\n]" or on eof
if(!cnt || sTb[ m_cbuf[0] ] == newline)
{
writeHex(c,out);
return;
}
break;
}
}
write(c,out);
break;
case tab:
case sp:
// on binary encoding, or last input ch or newline
if(m_binary || !cnt || sTb[ m_cbuf[0] ] == newline)
writeHex(c,out);
else
write(c,out);
break;
case newline:
if(m_binary)
writeHex(c, out);
else {
if(cnt && m_cbuf[0] == (c == CR ? LF : CR))
m_cbuf.pop_front(); // eat it
hardLineBrk(out);
}
break;
case binary:
if(!m_binary) m_binary = 1; // switch to binary mode
writeHex(c, out);
break;
case unsafe:
writeHex(c, out);
break;
}
}
public:
/*! return the multiplier of the required (max) size of the output buffer
* when encoding */
double codeSizeMultiplier() const
{
// worse case is *3 but we'll use the (euristic) average value of 1.5.
// this may decrease performance when encoding messages with many
// non-ASCII (> 127) characters
return 1.5;
}
/*!
Constructor
\param isBinary if true all space and newline characters will be
treated like binary chars and will be hex encoded (useful if you
want to encode a binary file).
*/
Encoder(bool isBinary = false)
: m_pos(1), m_maxlen(default_maxlen),
m_binary(isBinary), m_cbuf(laBufSz)
{
}
/*! Returns the name of the codec ("Quoted-Printable") */
const char* name() const { return "Quoted-Printable"; }
/*! Returns the max line length */
size_t maxlen()
{
return m_maxlen;
}
/*!
Set the max line length. No more then \p i chars will be
printed on one line.
*/
void maxlen(size_t i)
{
m_maxlen = i;
}
/*!
Encodes [\p bit,\p eit) and write any encoded char to \p out.
*/
template<typename InIt, typename OutIt>
void process(InIt bit, InIt eit, OutIt out)
{
for(; bit != eit; ++bit)
process(*bit, out);
flush(out);
}
/*!
Encodes \p ic and write any encoded output char to \p out.
\warning You must call flush() when all chars have been
processed by the encode funcion.
\n
\code
while( (c = getchar()) != EOF )
qp.process(c, out);
qp.flush();
\endcode
\n
\sa flush()
*/
template<typename OutIt>
void process(char_type ic, OutIt& out)
{
m_cbuf.push_back(ic);
if(m_cbuf.count() < laBufSz)
return;
char_type c = m_cbuf.front();
m_cbuf.pop_front();
encodeChar(c, out);
}
/*!
Write to \p out any buffered encoded char.
*/
template<typename OutIt>
void flush(OutIt& out)
{
char_type c;
while(!m_cbuf.empty())
{
c = m_cbuf.front();
m_cbuf.pop_front();
encodeChar(c, out);
}
}
};
/// quoted-printable decoder
/*!
\sa encode decode
*/
class Decoder: public buffered_codec, public chainable_codec<Encoder>
{
enum { laBufSz = 80 }; // look-ahead buffer
enum {
sWaitingChar,
sAfterEq,
sWaitingFirstHex,
sWaitingSecondHex,
sBlank,
sNewline,
sOtherChar
};
size_t m_pos, m_maxlen;
int m_state, m_nl;
std::string m_prev;
template<typename OutIt>
void hardLineBrk(OutIt& out) const
{
*out = NL; ++out;
}
template<typename OutIt>
void write(char_type ch, OutIt& out) const
{
*out = ch; ++out;
}
bool isnl(char_type c) const
{
return (c == CR || c == LF);
}
template<typename OutIt>
void flushPrev(OutIt& out)
{
copy(m_prev.begin(), m_prev.end(), out);
m_prev.clear();
}
int hex_to_int(char_type c) const
{
if( c >= '0' && c <='9') return c - '0';
else if( c >= 'A' && c <='F') return c - 'A' + 10;
else if( c >= 'a' && c <='f') return c - 'a' + 10;
else return 0;
}
bool ishex(char_type c) const
{
return (c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'F') ||
(c >= 'a' && c <= 'f');
}
template<typename OutIt>
void decodeChar(char_type c, OutIt& out)
{
for(;;)
{
switch(m_state)
{
case sBlank:
if(isblank(c))
m_prev.append(1,c);
else if(isnl(c)) {
// soft linebrk & ignore trailing blanks
m_prev.clear();
m_state = sWaitingChar;
} else {
flushPrev(out);
m_state = sWaitingChar;
continue;
}
return;
case sAfterEq:
if(isblank(c))
m_prev.append(1,c);
else if(isnl(c)) {
// soft linebrk
m_state = sNewline;
continue;
} else {
if(m_prev.length() > 1)
{
// there're blanks after =
flushPrev(out);
m_state = sWaitingChar;
} else
m_state = sWaitingFirstHex;
continue;
}
return;
case sWaitingFirstHex:
if(!ishex(c))
{
// malformed: =[not-hexch]
flushPrev(out);
write(c, out);
m_state = sWaitingChar;
return;
} else {
m_prev.append(1,c);
m_state = sWaitingSecondHex;
}
return;
case sWaitingSecondHex:
if(!ishex(c))
{ // malformed (=[hexch][not-hexch])
flushPrev(out);
write(c, out);
} else {
char_type oc, last;
assert(m_prev.length());
last = m_prev[m_prev.length()-1];
oc = hex_to_int(last) << 4 |
hex_to_int(c) ;
write(oc,out);
m_prev.clear();
}
m_state = sWaitingChar;
return;
case sNewline:
if(m_nl == 0)
{
m_nl = c;
return;
} else {
int len = m_prev.length();
if(!len || m_prev[0] != '=')
hardLineBrk(out);
m_prev.clear();
m_state = sWaitingChar;
bool is2Ch;
is2Ch = (c == (m_nl == CR ? LF : CR));
m_nl = 0;
if(is2Ch)
return;
continue;
}
case sWaitingChar:
if(isblank(c))
{
m_state = sBlank;
continue;
} else if(isnl(c)) {
m_state = sNewline;
continue;
} else if(c == '=') {
m_state = sAfterEq;
m_prev.append(1, c);
return;
} else {
// WARNING: NOT ignoring chars > 126
// as suggested in rfc2045 6.7 note 4
if(c < 32 && c != TAB)
{
// malformed, CTRL ch found
// ignore (rfc2045 6.7 note 4)
return;
}
write(c,out);
}
return;
}
}
}
public:
/*! Constructor */
Decoder()
: m_state(sWaitingChar), m_nl(0)
{
}
/*! Returns the name of the codec ("Quoted-Printable") */
const char* name() const { return "Quoted-Printable"; }
/*! Returns the max line length */
size_t maxlen()
{
return m_maxlen;
}
/*!
Set the max line length. No more then \p i chars will be
printed on one line.
*/
void maxlen(size_t i)
{
m_maxlen = i;
}
/*!
Decodes [\p bit,\p eit) and write any decoded char to \p out.
*/
template<typename InIt, typename OutIt>
void process(InIt bit, InIt eit, OutIt out)
{
for(;bit != eit; ++bit)
decodeChar(*bit, out);
flush(out);
}
/*!
Decodes \p ic and write any decoded output char to \p out.
\warning You must call flush() when all chars have been
processed by the code(...) funcion.
\n
\code
while( (c = getchar()) != EOF )
qp.process(c, out);
qp.flush();
\endcode
\n
\sa flush()
*/
template<typename OutIt>
void process(char_type ic, OutIt& out)
{
decodeChar(ic, out);
}
/*!
Write to \p out any buffered decoded char.
*/
template<typename OutIt>
void flush(OutIt& out)
{
/* m_prev can be (regex):
empty:
ok
'=' :
malformed, '=' is last stream char, print as is
(rfc2045 6.7 note 3)
'=[a-zA-Z]'
malformed, print as is
(rfc2045 6.7 note 2)
'= +'
malformed, just print '=' and ignore trailing
blanks (rfc2045 6.7 (3) )
*/
int len = m_prev.length();
if(len)
{
if(len == 1)
{
/* malformed if m_prev[0] == '=' */
write('=', out);
} else {
write('=', out);
if(m_prev[1] != ' ')
write(m_prev[1], out);
}
} else if(m_nl != 0) // stream ends with newline
hardLineBrk(out);
}
};
};
} // namespace
#endif
|