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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
/*
kmime_codec_qp.cpp
This file is part of KMime, the KDE internet mail/usenet news message library.
Copyright (c) 2002 Marc Mutz <mutz@kde.org>
KMime is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
KMime is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this library with any edition of
the TQt library by Trolltech AS, Norway (or with modified versions
of TQt that use the same license as TQt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
TQt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#include "kmime_codec_qp.h"
#include "kmime_util.h"
#include <kdebug.h>
#include <cassert>
using namespace KMime;
namespace KMime {
// some helpful functions:
static inline char binToHex( uchar value ) {
if ( value > 9 )
return value + 'A' - 10;
else
return value + '0';
}
static inline uchar highNibble( uchar ch ) {
return ch >> 4;
}
static inline uchar lowNibble( uchar ch ) {
return ch & 0xF;
}
static inline bool keep( uchar ch ) {
// no CTLs, except HT and not '?'
return !( ((ch < ' ') && (ch != '\t')) || (ch == '?') );
}
//
// QuotedPrintableCodec
//
class QuotedPrintableEncoder : public Encoder {
char mInputBuffer[16];
uchar mCurrentLineLength; // 0..76
uchar mAccu;
uint mInputBufferReadCursor : 4; // 0..15
uint mInputBufferWriteCursor : 4; // 0..15
enum {
Never, AtBOL, Definitely
} mAccuNeedsEncoding : 2;
bool mSawLineEnd : 1;
bool mSawCR : 1;
bool mFinishing : 1;
bool mFinished : 1;
protected:
friend class QuotedPrintableCodec;
QuotedPrintableEncoder( bool withCRLF=false )
: Encoder( withCRLF ), mCurrentLineLength(0), mAccu(0),
mInputBufferReadCursor(0), mInputBufferWriteCursor(0),
mAccuNeedsEncoding(Never),
mSawLineEnd(false), mSawCR(false), mFinishing(false),
mFinished(false) {}
bool needsEncoding( uchar ch ) {
return ( (ch > '~') || ((ch < ' ') && (ch != '\t')) || (ch == '=') );
}
bool needsEncodingAtEOL( uchar ch ) {
return ( ch == ' ' || ch == '\t' );
}
bool needsEncodingAtBOL( uchar ch ) {
return ( ch == 'F' || ch == '.' || ch == '-' );
}
bool fillInputBuffer( const char* & scursor, const char * const send );
bool processNextChar();
void createOutputBuffer( char* & dcursor, const char * const dend );
public:
virtual ~QuotedPrintableEncoder() {}
bool encode( const char* & scursor, const char * const send,
char* & dcursor, const char * const dend );
bool finish( char* & dcursor, const char * const dend );
};
class QuotedPrintableDecoder : public Decoder {
const char mEscapeChar;
char mBadChar;
/** @p accu holds the msb nibble of the hexchar or zero. */
uchar mAccu;
/** @p insideHexChar is true iff we're inside an hexchar (=XY).
Together with @ref mAccu, we can build this states:
@li @p insideHexChar == @p false:
normal text
@li @p insideHexChar == @p true, @p mAccu == 0:
saw the leading '='
@li @p insideHexChar == @p true, @p mAccu != 0:
saw the first nibble '=X'
*/
const bool mTQEncoding;
bool mInsideHexChar;
bool mFlushing;
bool mExpectLF;
bool mHaveAccu;
protected:
friend class QuotedPrintableCodec;
friend class Rfc2047TQEncodingCodec;
friend class Rfc2231EncodingCodec;
QuotedPrintableDecoder( bool withCRLF=false,
bool aTQEncoding=false, char aEscapeChar='=' )
: Decoder( withCRLF ),
mEscapeChar(aEscapeChar),
mBadChar(0),
mAccu(0),
mTQEncoding(aTQEncoding),
mInsideHexChar(false),
mFlushing(false),
mExpectLF(false),
mHaveAccu(false) {}
public:
virtual ~QuotedPrintableDecoder() {}
bool decode( const char* & scursor, const char * const send,
char* & dcursor, const char * const dend );
// ### really no finishing needed???
bool finish( char* &, const char * const ) { return true; }
};
class Rfc2047TQEncodingEncoder : public Encoder {
uchar mAccu;
uchar mStepNo;
const char mEscapeChar;
bool mInsideFinishing : 1;
protected:
friend class Rfc2047TQEncodingCodec;
friend class Rfc2231EncodingCodec;
Rfc2047TQEncodingEncoder( bool withCRLF=false, char aEscapeChar='=' )
: Encoder( withCRLF ),
mAccu(0), mStepNo(0), mEscapeChar( aEscapeChar ),
mInsideFinishing( false )
{
// else an optimization in ::encode might break.
assert( aEscapeChar == '=' || aEscapeChar == '%' );
}
// this code assumes that isEText( mEscapeChar ) == false!
bool needsEncoding( uchar ch ) {
if ( ch > 'z' ) return true; // {|}~ DEL and 8bit chars need
if ( !isEText( ch ) ) return true; // all but a-zA-Z0-9!/*+- need, too
if ( mEscapeChar == '%' && ( ch == '*' || ch == '/' ) )
return true; // not allowed in rfc2231 encoding
return false;
}
public:
virtual ~Rfc2047TQEncodingEncoder() {}
bool encode( const char* & scursor, const char * const send,
char* & dcursor, const char * const dend );
bool finish( char* & dcursor, const char * const dend );
};
// this doesn't access any member variables, so it can be defined static
// but then we can't call it from virtual functions
static int QuotedPrintableDecoder_maxDecodedSizeFor( int insize, bool withCRLF ) {
// all chars unencoded:
int result = insize;
// but maybe all of them are \n and we need to make them \r\n :-o
if ( withCRLF )
result += insize;
// there might be an accu plus escape
result += 2;
return result;
}
Encoder * QuotedPrintableCodec::makeEncoder( bool withCRLF ) const {
return new QuotedPrintableEncoder( withCRLF );
}
Decoder * QuotedPrintableCodec::makeDecoder( bool withCRLF ) const {
return new QuotedPrintableDecoder( withCRLF );
}
int QuotedPrintableCodec::maxDecodedSizeFor( int insize, bool withCRLF ) const {
return QuotedPrintableDecoder_maxDecodedSizeFor(insize, withCRLF);
}
Encoder * Rfc2047TQEncodingCodec::makeEncoder( bool withCRLF ) const {
return new Rfc2047TQEncodingEncoder( withCRLF );
}
Decoder * Rfc2047TQEncodingCodec::makeDecoder( bool withCRLF ) const {
return new QuotedPrintableDecoder( withCRLF, true );
}
int Rfc2047TQEncodingCodec::maxDecodedSizeFor( int insize, bool withCRLF ) const {
return QuotedPrintableDecoder_maxDecodedSizeFor(insize, withCRLF);
}
Encoder * Rfc2231EncodingCodec::makeEncoder( bool withCRLF ) const {
return new Rfc2047TQEncodingEncoder( withCRLF, '%' );
}
Decoder * Rfc2231EncodingCodec::makeDecoder( bool withCRLF ) const {
return new QuotedPrintableDecoder( withCRLF, true, '%' );
}
int Rfc2231EncodingCodec::maxDecodedSizeFor( int insize, bool withCRLF ) const {
return QuotedPrintableDecoder_maxDecodedSizeFor(insize, withCRLF);
}
/********************************************************/
/********************************************************/
/********************************************************/
bool QuotedPrintableDecoder::decode( const char* & scursor, const char * const send,
char* & dcursor, const char * const dend ) {
if ( mWithCRLF )
kdWarning() << "CRLF output for decoders isn't yet supported!" << endl;
while ( scursor != send && dcursor != dend ) {
if ( mFlushing ) {
// we have to flush chars in the aftermath of an decoding
// error. The way to request a flush is to
// - store the offending character in mBadChar and
// - set mFlushing to true.
// The supported cases are (H: hexchar, X: bad char):
// =X, =HX, CR
// mBadChar is only written out if it is not by itself illegal in
// quoted-printable (e.g. CTLs, 8Bits).
// A fast way to suppress mBadChar output is to set it to NUL.
if ( mInsideHexChar ) {
// output '='
*dcursor++ = mEscapeChar;
mInsideHexChar = false;
} else if ( mHaveAccu ) {
// output the high nibble of the accumulator:
*dcursor++ = binToHex( highNibble( mAccu ) );
mHaveAccu = false;
mAccu = 0;
} else {
// output mBadChar
assert( mAccu == 0 );
if ( mBadChar ) {
if ( ((mBadChar >= '>') && (mBadChar <= '~')) ||
((mBadChar >= '!') && (mBadChar <= '<')) )
*dcursor++ = mBadChar;
mBadChar = 0;
}
mFlushing = false;
}
continue;
}
assert( mBadChar == 0 );
uchar ch = *scursor++;
uchar value = 255;
if ( mExpectLF && ch != '\n' ) {
kdWarning() << "QuotedPrintableDecoder: "
"illegally formed soft linebreak or lonely CR!" << endl;
mInsideHexChar = false;
mExpectLF = false;
assert( mAccu == 0 );
}
if ( mInsideHexChar ) {
// next char(s) represent nibble instead of itself:
if ( ch <= '9' ) {
if ( ch >= '0' ) {
value = ch - '0';
} else {
switch ( ch ) {
case '\r':
mExpectLF = true;
break;
case '\n':
// soft line break, but only if mAccu is NUL.
if ( !mHaveAccu ) {
mExpectLF = false;
mInsideHexChar = false;
break;
}
// else fall through
default:
kdWarning() << "QuotedPrintableDecoder: "
"illegally formed hex char! Outputting verbatim." << endl;
mBadChar = ch;
mFlushing = true;
}
continue;
}
} else { // ch > '9'
if ( ch <= 'F' ) {
if ( ch >= 'A' ) {
value = 10 + ch - 'A';
} else { // [:-@]
mBadChar = ch;
mFlushing = true;
continue;
}
} else { // ch > 'F'
if ( ch <= 'f' && ch >= 'a' ) {
value = 10 + ch - 'a';
} else {
mBadChar = ch;
mFlushing = true;
continue;
}
}
}
assert( value < 16 );
assert( mBadChar == 0 );
assert( !mExpectLF );
if ( mHaveAccu ) {
*dcursor++ = char( mAccu | value );
mAccu = 0;
mHaveAccu = false;
mInsideHexChar = false;
} else {
mHaveAccu = true;
mAccu = value << 4;
}
} else { // not mInsideHexChar
if ( ((ch <= '~') && (ch >= ' ')) || (ch == '\t') ) {
if ( ch == mEscapeChar ) {
mInsideHexChar = true;
} else if ( mTQEncoding && ch == '_' ) {
*dcursor++ = char(0x20);
} else {
*dcursor++ = char(ch);
}
} else if ( ch == '\n' ) {
*dcursor++ = '\n';
mExpectLF = false;
} else if ( ch == '\r' ) {
mExpectLF = true;
} else {
kdWarning() << "QuotedPrintableDecoder: " << ch <<
" illegal character in input stream! Ignoring." << endl;
}
}
}
return (scursor == send);
}
bool QuotedPrintableEncoder::fillInputBuffer( const char* & scursor,
const char * const send ) {
// Don't read more if there's still a tail of a line in the buffer:
if ( mSawLineEnd )
return true;
// Read until the buffer is full or we have found CRLF or LF (which
// don't end up in the input buffer):
for ( ; ( mInputBufferWriteCursor + 1 ) % 16 != mInputBufferReadCursor
&& scursor != send ; mInputBufferWriteCursor++ ) {
char ch = *scursor++;
if ( ch == '\r' ) {
mSawCR = true;
} else if ( ch == '\n' ) {
// remove the CR from the input buffer (if any) and return that
// we found a line ending:
if ( mSawCR ) {
mSawCR = false;
assert( mInputBufferWriteCursor != mInputBufferReadCursor );
mInputBufferWriteCursor--;
}
mSawLineEnd = true;
return true; // saw CRLF or LF
} else {
mSawCR = false;
}
mInputBuffer[ mInputBufferWriteCursor ] = ch;
}
mSawLineEnd = false;
return false; // didn't see a line ending...
}
bool QuotedPrintableEncoder::processNextChar() {
// If we process a buffer which doesn't end in a line break, we
// can't process all of it, since the next chars that will be read
// could be a line break. So we empty the buffer only until a fixed
// number of chars is left (except when mFinishing, which means that
// the data doesn't end in newline):
const int minBufferFillWithoutLineEnd = 4;
assert( mOutputBufferCursor == 0 );
int bufferFill = int(mInputBufferWriteCursor) - int(mInputBufferReadCursor) ;
if ( bufferFill < 0 )
bufferFill += 16;
assert( bufferFill >=0 && bufferFill <= 15 );
if ( !mFinishing && !mSawLineEnd &&
bufferFill < minBufferFillWithoutLineEnd )
return false;
// buffer is empty, return false:
if ( mInputBufferReadCursor == mInputBufferWriteCursor )
return false;
// Real processing goes here:
mAccu = mInputBuffer[ mInputBufferReadCursor++ ];
if ( needsEncoding( mAccu ) ) // always needs encoding or
mAccuNeedsEncoding = Definitely;
else if ( ( mSawLineEnd || mFinishing ) // needs encoding at end of line
&& bufferFill == 1 // or end of buffer
&& needsEncodingAtEOL( mAccu ) )
mAccuNeedsEncoding = Definitely;
else if ( needsEncodingAtBOL( mAccu ) )
mAccuNeedsEncoding = AtBOL;
else
// never needs encoding
mAccuNeedsEncoding = Never;
return true;
}
// Outputs processed (verbatim or hex-encoded) chars and inserts soft
// line breaks as necessary. Depends on processNextChar's directions
// on whether or not to encode the current char, and whether or not
// the current char is the last one in it's input line:
void QuotedPrintableEncoder::createOutputBuffer( char* & dcursor,
const char * const dend )
{
const int maxLineLength = 76; // rfc 2045
assert( mOutputBufferCursor == 0 );
bool lastOneOnThisLine = mSawLineEnd
&& mInputBufferReadCursor == mInputBufferWriteCursor;
int neededSpace = 1;
if ( mAccuNeedsEncoding == Definitely)
neededSpace = 3;
// reserve space for the soft hyphen (=)
if ( !lastOneOnThisLine )
neededSpace++;
if ( mCurrentLineLength > maxLineLength - neededSpace ) {
// current line too short, insert soft line break:
write( '=', dcursor, dend );
writeCRLF( dcursor, dend );
mCurrentLineLength = 0;
}
if ( (Never == mAccuNeedsEncoding) ||
((AtBOL == mAccuNeedsEncoding) && (mCurrentLineLength != 0)) ) {
write( mAccu, dcursor, dend );
mCurrentLineLength++;
} else {
write( '=', dcursor, dend );
write( binToHex( highNibble( mAccu ) ), dcursor, dend );
write( binToHex( lowNibble( mAccu ) ), dcursor, dend );
mCurrentLineLength += 3;
}
}
bool QuotedPrintableEncoder::encode( const char* & scursor, const char * const send,
char* & dcursor, const char * const dend )
{
// support probing by the caller:
if ( mFinishing ) return true;
while ( scursor != send && dcursor != dend ) {
if ( mOutputBufferCursor && !flushOutputBuffer( dcursor, dend ) )
return (scursor == send);
assert( mOutputBufferCursor == 0 );
// fill input buffer until eol has been reached or until the
// buffer is full, whatever comes first:
fillInputBuffer( scursor, send );
if ( processNextChar() )
// there was one...
createOutputBuffer( dcursor, dend );
else if ( mSawLineEnd &&
mInputBufferWriteCursor == mInputBufferReadCursor ) {
// load a hard line break into output buffer:
writeCRLF( dcursor, dend );
// signal fillInputBuffer() we are ready for the next line:
mSawLineEnd = false;
mCurrentLineLength = 0;
} else
// we are supposedly finished with this input block:
break;
}
// make sure we write as much as possible and don't stop _writing_
// just because we have no more _input_:
if ( mOutputBufferCursor ) flushOutputBuffer( dcursor, dend );
return (scursor == send);
} // encode
bool QuotedPrintableEncoder::finish( char* & dcursor,
const char * const dend ) {
mFinishing = true;
if ( mFinished )
return flushOutputBuffer( dcursor, dend );
while ( dcursor != dend ) {
if ( mOutputBufferCursor && !flushOutputBuffer( dcursor, dend ) )
return false;
assert( mOutputBufferCursor == 0 );
if ( processNextChar() )
// there was one...
createOutputBuffer( dcursor, dend );
else if ( mSawLineEnd &&
mInputBufferWriteCursor == mInputBufferReadCursor ) {
// load a hard line break into output buffer:
writeCRLF( dcursor, dend );
mSawLineEnd = false;
mCurrentLineLength = 0;
} else {
mFinished = true;
return flushOutputBuffer( dcursor, dend );
}
}
return mFinished && !mOutputBufferCursor;
} // finish
bool Rfc2047TQEncodingEncoder::encode( const char* & scursor, const char * const send,
char* & dcursor, const char * const dend )
{
if ( mInsideFinishing ) return true;
while ( scursor != send && dcursor != dend ) {
uchar value;
switch ( mStepNo ) {
case 0:
// read the next char and decide if and how do encode:
mAccu = *scursor++;
if ( !needsEncoding( mAccu ) ) {
*dcursor++ = char(mAccu);
} else if ( mEscapeChar == '=' && mAccu == 0x20 ) {
// shortcut encoding for 0x20 (latin-1/us-ascii SPACE)
// (not for rfc2231 encoding)
*dcursor++ = '_';
} else {
// needs =XY encoding - write escape char:
*dcursor++ = mEscapeChar;
mStepNo = 1;
}
continue;
case 1:
// extract hi-nibble:
value = highNibble(mAccu);
mStepNo = 2;
break;
case 2:
// extract lo-nibble:
value = lowNibble(mAccu);
mStepNo = 0;
break;
default: assert( 0 );
}
// and write:
*dcursor++ = binToHex( value );
}
return (scursor == send);
} // encode
#include <tqstring.h>
bool Rfc2047TQEncodingEncoder::finish( char* & dcursor, const char * const dend ) {
mInsideFinishing = true;
// write the last bits of mAccu, if any:
while ( mStepNo != 0 && dcursor != dend ) {
uchar value;
switch ( mStepNo ) {
case 1:
// extract hi-nibble:
value = highNibble(mAccu);
mStepNo = 2;
break;
case 2:
// extract lo-nibble:
value = lowNibble(mAccu);
mStepNo = 0;
break;
default: assert( 0 );
}
// and write:
*dcursor++ = binToHex( value );
}
return mStepNo == 0;
}
} // namespace KMime
|