Important alert: (current site time 5/18/2013 11:17:19 AM EDT)
 

VB icon

QP Decode

Email
Submitted on: 10/23/2012 1:14:49 PM
By: J.A. Coutts 
Level: Intermediate
User Rating: By 1 Users
Compatibility: VB 6.0
Views: 2785
 
     The following code is based on code submitted by: AndrComm http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=3113&lngWId=1 His code is designed to decode quoted-printable text transmitted in an email, and is straight forward and relatively quick. However, I ran into a number of complications when attempting to use it.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: QP Decode
' Description:The following code is based on code submitted by: AndrComm
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=3113&lngWId=1
His code is designed to decode quoted-printable text transmitted 
in an email, and is straight forward and relatively quick. However, 
I ran into a number of complications when attempting to use it.
' By: J.A. Coutts
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=74601&lngWId=1'for details.'**************************************

The first problem was with the line:
	sTemp = Replace(sTemp, "=", "")
It removes any remaining "=" signs, which is not required by RFC 2045 and 
causes complications. It can safely be removed.
The next problem was with the line:
	For i = 32 To 10 Step -1
Codes 0 to 9 were separated from codes 10 to 32 because of the number of 
digits involved. Unfortunately, that left out the single digit hex codes A 
to F (10 to 15). I replaced both of these routines with a single routine.
The next problem encountered was with what they call dot-stuffing. With 
the SMTP protocol, a single dot on a line by itself signifies the end of 
the message. When the message lines are reduced to 76 characters, there is 
a possibility that a dot could be left on a line by itself. To prevent 
this, they add an extra dot if the line begins with a dot. Code was added 
to remove this extra dot.
I have no idea why, but some vendors add multiple "=" signs before and 
after the boundary string as part of the actual string. The last "=" 
sign in this string gets interpreted as a soft end-of-line. I added code 
to preserve this string.
Everyone seems to have their own solution to dealing with the single dot 
problem. One vendor replaced all dots with quoted-printable characters. 
Codes 33 to 127 do not require coding, but I had to add code to deal with 
this bizarre situation.
Public Function DecodeQP(ByRef StrToDecode As String) As String
	Dim sTemp As String
	Dim N%
	sTemp = StrToDecode
	'Check for dot stuffing at beginning of line
	If InStr(sTemp, vbCrLf & "..") <> 0 Then
		sTemp = Replace(sTemp, vbCrLf & "..", vbCrLf & ".")
	End If
	'Substitute temporary code for "==" chars at end of line
	If InStr(1, sTemp, "==" & vbCrLf) <> 0 Then
		sTemp = Replace(sTemp, "==" & vbCrLf, "==" & Chr$(255) & Chr$(254))
		'Delete soft end of lines
		sTemp = Replace(sTemp, "=" & vbCrLf, "")
		'Restore original "==" signs
		sTemp = Replace(sTemp, "==" & Chr$(255) & Chr$(254), "==" & vbCrLf)
	Else 'Just delete soft end of lines
		sTemp = Replace(sTemp, "=" & vbCrLf, "")
	End If
	'Restore non ASCII chars
	For N% = 255 To 127 Step -1
		If InStr(1, sTemp, "=" & Hex$(N%)) <> 0 Then
			sTemp = Replace(sTemp, "=" & Hex$(N%), Chr$(N%))
		End If
	Next N%
	'Restore periods
	If InStr(1, sTemp, "=2E") <> 0 Then
		sTemp = Replace(sTemp, "=2E", ".")
	End If
	'Substitute temporary code for "=" signs
	If InStr(1, sTemp, "=" & Hex$(61)) <> 0 Then _
		sTemp = Replace(sTemp, "=" & Hex$(61), Chr$(255) & Chr$(254))
	'Restore control codes
	For N% = 32 To 0 Step -1
		If InStr(1, sTemp, "=" & Right$("0" & Hex$(N%), 2)) <> 0 Then
			sTemp = Replace(sTemp, "=" & Right$("0" & Hex$(N%), 2), Chr$(N%))
		End If
	Next N%
	'sTemp = Replace(sTemp, "=", "") 'Replace remaining "=" signs
	sTemp = Replace(sTemp, Chr$(255) & Chr$(254), "=") 'Restore original "=" signs
	DecodeQP = sTemp
End Function


Other 13 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.