Update contrib.
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
19 #include <bafl/qpcodec.h>
23 @param aSrcString source string
24 @param rDestString Destination string
25 @return KErrNone if decode is complete
26 @return KErrCorrupt if string is not QuotedPrintable compliant
28 EXPORT_C TInt QuotedPrintableCodec::Decode( const TDesC8& aSrcString, TDes8& rDestString )
32 TInt KPanicInvalidSMTPLine = 6;
33 _LIT(KDllName,"MultipartParser");
36 const TUint8 KImcvSP = ' ';
37 const TUint8 KImcvCR = '\r';
38 const TUint8 KImcvLF = '\n';
39 const TUint8 KImcvTab = '\t';
40 const TUint8 KImcvEquals = '=';
41 TUint8 qpCharacter = KImcvEquals;
43 TInt error = KErrNone;
45 __ASSERT_DEBUG(aSrcString.Length(), User::Panic( KDllName ,KPanicInvalidSMTPLine));
47 rDestString = KNullDesC8;
49 TPtrC8 source( aSrcString.Ptr(), aSrcString.Length() );
50 const TUint8* pSource = source.Ptr();
51 const TUint8* pEnd = pSource+aSrcString.Length();
53 // find out if this is a blank line, if so then we'll add a paragraph delimiter instead
54 // assume it's blank and then look for non-blank characters
55 // avoid the CRLF at the end of the line (we know it's there thanks to the assertion above)
57 TBool blankLine = ETrue;
58 while (pSource < pEnd-2)
60 if (*pSource!=KImcvSP && *pSource!=KImcvTab)
70 rDestString.Copy( aSrcString );
78 pSource = source.Ptr(); // reset to start of source data
79 const TUint8 zero = '0';
80 const TUint8 alphaAdjust = 55; // 'A' is ascii 65 so we need to subtract 55 from
81 // alphabetical hex digits to get their numeric value
82 while( pSource < pEnd )
84 if (*pSource != qpCharacter )
86 // Quoted character or Attachment bound, just bung it on & move to the next one
89 rDestString.Append( *pSource );
91 else // check for encoded character
93 // start looking at the next two characters, if they are there.
95 if ( pSource+2 < pEnd )
99 // check for '=' at EOL => this is a soft break, so remove it
100 if (*pSource != KImcvCR)
102 if(*pSource != KImcvLF)
104 // now decode hex value into ASCII code : hi-order bits come first
105 hiBits = (TUint8)(0x0F & (IsDigit( *pSource ) ? (TUint8)(*pSource-zero) : (TUint8)(*pSource-alphaAdjust)));
107 loBits = (TUint8)(0x0F & (IsDigit( *pSource ) ? (TUint8)(*pSource-zero) : (TUint8)(*pSource-alphaAdjust)));
108 asciiValue = (TUint8)( (hiBits<<4) + loBits);
109 // bung the character thus formed onto the decoded string
110 rDestString.Append( asciiValue );
111 // *ptr++ = asciiValue;
118 if(*pSource != KImcvLF)
122 rDestString.Append( *pSource );
124 rDestString.Append( *pSource );
132 // copy the rest of the data & use up the input string in the process.
134 while (pSource < pEnd)
136 error=KErrCorrupt; // not QP compliant
137 //*ptr++ = *pSource++;
139 rDestString.Append( *pSource );
143 } // check for '=' char
145 pSource++; // next source charactery
148 rDestString.SetLength(outputLength);
155 @param aChar charcter to be check
156 @return ETrue if passed charcter is digit
157 @return EFalse if passed charcter is not digit
159 TBool QuotedPrintableCodec::IsDigit( TChar aChar )
161 return ( (aChar >= '0') && (aChar <= '9') );