39 using namespace KMime;
43 class UUDecoder :
public Decoder
46 uchar mAnnouncedOctetCount;
47 uchar mCurrentOctetCount;
49 bool mLastWasCRLF : 1;
51 uint mIntoBeginLine : 3;
53 uint mIntoEndLine : 2;
55 void searchForBegin(
const char* &scursor,
const char *
const send );
59 UUDecoder(
bool withCRLF=
false )
60 :
Decoder( withCRLF ), mStepNo( 0 ),
61 mAnnouncedOctetCount( 0 ), mCurrentOctetCount( 0 ),
62 mOutbits( 0 ), mLastWasCRLF( true ),
63 mSawBegin( false ), mIntoBeginLine( 0 ),
64 mSawEnd( false ), mIntoEndLine( 0 ) {}
67 virtual ~UUDecoder() {}
69 bool decode(
const char* &scursor,
const char *
const send,
70 char* &dcursor,
const char *
const dend );
72 bool finish(
char* &dcursor,
const char *
const dend )
73 { Q_UNUSED( dcursor ); Q_UNUSED( dend );
return true; }
83 return new UUDecoder( withCRLF );
90 void UUDecoder::searchForBegin(
const char* &scursor,
const char *
const send )
92 static const char begin[] =
"begin\n";
93 static const uint beginLength = 5;
95 assert( !mSawBegin || mIntoBeginLine > 0 );
97 while ( scursor != send ) {
98 uchar ch = *scursor++;
99 if ( ch == begin[mIntoBeginLine] ) {
100 if ( mIntoBeginLine < beginLength ) {
103 if ( mIntoBeginLine == beginLength ) {
112 }
else if ( mSawBegin ) {
115 kWarning() <<
"UUDecoder: garbage before \"begin\", resetting parser";
124 static inline uchar uuDecode( uchar c )
130 bool UUDecoder::decode(
const char* &scursor,
const char *
const send,
131 char* &dcursor,
const char *
const dend )
134 if ( !mSawBegin || mIntoBeginLine != 0 ) {
135 searchForBegin( scursor, send );
136 }
else if ( mSawEnd ) {
142 while ( dcursor != dend && scursor != send ) {
143 uchar ch = *scursor++;
147 if ( mIntoEndLine > 0 ) {
148 static const char end[] =
"end";
149 static const uint endLength = 3;
151 if ( ch == end[mIntoEndLine] ) {
153 if ( mIntoEndLine == endLength ) {
160 kWarning() <<
"UUDecoder: invalid line octet count looks like \"end\" (mIntoEndLine ="
161 << mIntoEndLine <<
")!";
171 if ( mLastWasCRLF ) {
173 mLastWasCRLF =
false;
174 mCurrentOctetCount = 0;
179 }
else if ( ch > 0x60 ) {
181 }
else if ( ch >
' ' ) {
182 mAnnouncedOctetCount = uuDecode( ch );
183 }
else if ( ch ==
'\n' ) {
193 }
else if ( ch >
' ' ) {
194 value = uuDecode( ch );
195 }
else if ( ch ==
'\n' ) {
205 mOutbits = value << 2;
208 if ( mCurrentOctetCount < mAnnouncedOctetCount ) {
209 *dcursor++ = (char)( mOutbits | value >> 4 );
211 ++mCurrentOctetCount;
212 mOutbits = value << 4;
215 if ( mCurrentOctetCount < mAnnouncedOctetCount ) {
216 *dcursor++ = (char)( mOutbits | value >> 2 );
218 ++mCurrentOctetCount;
219 mOutbits = value << 6;
222 if ( mCurrentOctetCount < mAnnouncedOctetCount ) {
223 *dcursor++ = (char)( mOutbits | value );
225 ++mCurrentOctetCount;
231 mStepNo = ( mStepNo + 1 ) % 4;
234 kWarning( mCurrentOctetCount == mAnnouncedOctetCount + 1 )
235 <<
"UUDecoder: mismatch between announced ("
236 << mAnnouncedOctetCount <<
") and actual line octet count!";
241 return scursor == send;
This file is part of the API for handling MIME data and defines a uuencode Codec class.
Stateful CTE decoder class.
A class representing the UUEncode codec.
Decoder * makeDecoder(bool withCRLF=false) const
Encoder * makeEncoder(bool withCRLF=false) const