DecodeQP.cs
上传用户:hncsjykj
上传日期:2022-08-09
资源大小:461k
文件大小:5k
源码类别:

Email客户端

开发平台:

C#

  1. /******************************************************************************
  2. Copyright 2003-2004 Hamid Qureshi and Unruled Boy 
  3. OpenPOP.Net is free software; you can redistribute it and/or modify
  4. it under the terms of the Lesser GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. OpenPOP.Net is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. Lesser GNU General Public License for more details.
  11. You should have received a copy of the Lesser GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  14. /*******************************************************************************/
  15. /*
  16. *Name: OpenPOP.MIMEParser.DecodeQP
  17. *Function: Decoding Quoted-Printable text
  18. *Author: coollzh(coollzh@hotmail.com)
  19. *Created: 2003/8
  20. *Modified: 2004/3/29 12:33 GMT+8
  21. *Description:
  22. *Changes:
  23. */
  24. using System;
  25. using System.Text;
  26. using System.Globalization;
  27. namespace OpenPOP.MIMEParser
  28. {
  29. /// <summary>
  30. /// Decoding Quoted-Printable text
  31. /// 
  32. /// </summary>
  33. public class DecodeQP
  34. {
  35. public DecodeQP()
  36. {
  37. }
  38. /// <summary>
  39. /// Decoding Quoted-Printable string
  40. /// </summary>
  41. /// <param name="Hexstring">Quoted-Printable encoded string</param>
  42. /// <param name="encode">encoding method</param>
  43. /// <returns>decoded string</returns>
  44. public static string ConvertHexToString(string Hexstring,string Encoding)
  45. {
  46. try
  47. {return ConvertHexToString(Hexstring, System.Text.Encoding.GetEncoding(Encoding));}
  48. catch
  49. {return ConvertHexContent(Hexstring);}
  50. }
  51. /// <summary>
  52. /// Decoding Quoted-Printable string
  53. /// </summary>
  54. /// <param name="Hexstring">Quoted-Printable encoded string</param>
  55. /// <param name="encode">encoding method</param>
  56. /// <returns>decoded string</returns>
  57. public static string ConvertHexToString(string Hexstring,Encoding encode)
  58. {
  59. try
  60. {
  61. if(Hexstring==null||Hexstring.Equals("")) return "";
  62. if(Hexstring.StartsWith("=")) Hexstring=Hexstring.Substring(1);
  63. string[] aHex= Hexstring.Split(new char[1]{'='});
  64. byte[] abyte = new Byte[aHex.Length];
  65. for(int i=0;i<abyte.Length;i++)
  66. {
  67. // Console.WriteLine(aHex[i]);
  68. abyte[i] =(byte) int.Parse(aHex[i],NumberStyles.HexNumber);
  69. }
  70. return encode.GetString(abyte);
  71. }
  72. catch
  73. {
  74. return Hexstring;
  75. }
  76. }
  77. /// <summary>
  78. /// Decoding Quoted-Printable string at a position
  79. /// </summary>
  80. /// <param name="Hexstring">Quoted-Printable encoded string</param>
  81. /// <param name="encode">encoding method, "Default" is suggested</param>
  82. /// <param name="nStart">position to start, normally 0</param>
  83. /// <returns>decoded string</returns>
  84. public static string ConvertHexContent(string Hexstring,Encoding encode,long nStart)
  85. {
  86. if(nStart>=Hexstring.Length) return Hexstring;
  87. //to hold string to be decoded
  88. StringBuilder sbHex = new  StringBuilder();
  89. sbHex.Append("");
  90. //to hold decoded string
  91. StringBuilder sbEncoded = new StringBuilder();
  92. sbEncoded.Append("");
  93. //wether we reach Quoted-Printable string
  94. bool isBegin = false;
  95. string temp;
  96. int i = (int)nStart;
  97. while(i<Hexstring.Length )
  98. {
  99. //init next loop
  100. sbHex.Remove(0,sbHex.Length);
  101. isBegin = false;
  102. int count=0;
  103. while(i<Hexstring.Length )
  104. {
  105. temp = Hexstring.Substring(i,1);//before reaching Quoted-Printable string, one char at a time
  106. if(temp.StartsWith("=")) 
  107. {
  108. temp = Hexstring.Substring(i,3);//get 3 chars
  109. if(temp.EndsWith("rn"))//return char
  110. {
  111. if(isBegin&& (count % 2==0))
  112. break;
  113. // sbEncoded.Append("");
  114. i=i+3;
  115. }
  116. else if(!temp.EndsWith("3D"))
  117. {
  118. sbHex.Append(temp);
  119. isBegin = true;//we reach Quoted-Printable string, put it into buffer
  120. i=i+3;
  121. count++;
  122. }
  123. else //if it ends with 3D, it is "="
  124. {
  125. if(isBegin&& (count % 2==0)) //wait until even items to handle all character sets
  126. break;
  127. sbEncoded.Append("=");
  128. i=i+3;
  129. }
  130. }
  131. else
  132. {
  133. if(isBegin)//we have got the how Quoted-Printable string, break it
  134. break;
  135. sbEncoded.Append(temp);//not Quoted-Printable string, put it into buffer
  136. i++;
  137. }
  138. }
  139. //decode Quoted-Printable string
  140. sbEncoded.Append(ConvertHexToString(sbHex.ToString(),encode)); 
  141. }
  142. return sbEncoded.ToString();
  143. }
  144. /// <summary>
  145. /// Decoding Quoted-Printable string using default encoding and begin at 0
  146. /// </summary>
  147. /// <param name="Hexstring">Quoted-Printable encoded string</param>
  148. /// <returns>decoded string</returns>
  149. public static string ConvertHexContent(string Hexstring)
  150. {
  151. if(Hexstring==null || Hexstring.Equals("")) return Hexstring;
  152. return ConvertHexContent(Hexstring,Encoding.Default,0);
  153. }
  154. }
  155. }