QuotedCoding.cs :  » Bloggers » dasBlog » Lesnikowski » Pawel » Mail » Pop3 » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » dasBlog 
dasBlog » Lesnikowski » Pawel » Mail » Pop3 » QuotedCoding.cs
using System;
using System.Text;
using System.Text.RegularExpressions;

namespace Lesnikowski.Pawel.Mail.Pop3{
  /// <summary>
  /// Summary description for Coding.
  /// </summary>
  public abstract class QuotedCoding
  {
    /// <summary>
    /// zwraca tablice bajtow
    /// zamienia 3 znaki np '=A9' na odp wartosc.
    /// zamienia '_' na znak 32
    /// </summary>
    /// <param name="s">Kupis_Pawe=B3</param>
    /// <returns>Kupis Pawe</returns>
    public static byte[] GetByteArray(string s)
    {
            // clemensv -- added check
      if ( s == null || s.Length == 0 )
                return new byte[0];
            
            byte[] buffer=new byte[s.Length];


      int bufferPosition=0;
      for(int i=0;i<s.Length;i++)
      {
                if (s[i]=='=' )
                {
                    if ( i<(s.Length-2) )
                    {
                        if ( s[i+1]=='\r' && s[i+2]=='\n')
                        {
                            bufferPosition--;
                        }
                        else
                        {
                            buffer[bufferPosition]=System.Convert.ToByte(s.Substring(i+1,2),16);
                        }
                        i+=2;
                    }
                    else
                    {
                        break;
                    }
                }
                else if (s[i]=='_')
                {
                    buffer[bufferPosition]=32;
                }
                else
                {
                    buffer[bufferPosition]=(byte)s[i];
                }
        bufferPosition++;
      }
      byte[] newArray=new byte[bufferPosition];
      Array.Copy(buffer,newArray,bufferPosition);
      return newArray;
    }

    /// <summary>
    /// Decoduje string "=?iso-8859-2?Q?Kupis_Pawe=B3?=" 
    /// lub zakodowany base64
    /// na poprawny
    /// </summary>
    /// <param name="s">"=?iso-8859-2?Q?Kupis_Pawe=B3?="</param>
    /// <returns>Kupis Pawe</returns>
    public static string DecodeOne(string charset, string cmd, string subject )
    {
            byte[] bArray;

      if (cmd=="Q") //querystring
            {
                bArray=GetByteArray(subject);
            }
            else if (cmd=="B")//base64
            {
                bArray=Convert.FromBase64String(subject);
            }
            else
            {
                return subject;
            }
      Encoding encoding=Encoding.GetEncoding(charset); 
      return encoding.GetString(bArray);
    }

    private static readonly Regex splitterRegex = new Regex(@"(?<unencoded>((?!=\?).)*)?(?:=\?(?<charset>.*?)\?(?<cmd>\w)\?(?<subject>.*?)\?=)?",RegexOptions.Compiled);
    /// <summary>
    /// decoduje string zamienia wpisy (=?...?=) na odp wartosci
    /// </summary>
    /// <param name="s">"ala i =?iso-8859-2?Q?Kupis_Pawe=B3?= ma kota"</param>
    /// <returns>"ala i Pawe Kupis ma kota"</returns>
    public static string Decode(string s)
    {
      StringBuilder retString=new StringBuilder();
            foreach( Match m in splitterRegex.Matches(s) )
            {
                if ( m.Groups["unencoded"].Success )
                {
                     retString.Append(m.Groups["unencoded"].Value);
                }
                if ( m.Groups["charset"].Success && m.Groups["cmd"].Success && m.Groups["subject"].Success )
                {
                     retString.Append(DecodeOne(m.Groups["charset"].Value,m.Groups["cmd"].Value,m.Groups["subject"].Value));
                }
            }
            return retString.ToString();
    }

  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.