[Dnsmail-cvs] dnsmail MIME.cs,1.2,1.3
Brought to you by:
ethem
From: Ethem E. <et...@us...> - 2006-08-07 08:15:39
|
Update of /cvsroot/dnsmail/dnsmail In directory sc8-pr-cvs12.sourceforge.net:/tmp/cvs-serv18842/dnsmail Modified Files: MIME.cs Log Message: QPEncode optimised. Index: MIME.cs =================================================================== RCS file: /cvsroot/dnsmail/dnsmail/MIME.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MIME.cs 4 Aug 2006 09:52:19 -0000 1.2 --- MIME.cs 7 Aug 2006 08:15:36 -0000 1.3 *************** *** 17,23 **** public sealed class MIME { ! private MIME() {} ! private static readonly byte[] crlf = Encoding.ASCII.GetBytes("\r\n"); ! private static readonly byte[] eq_crlf = Encoding.ASCII.GetBytes("=\r\n"); private static readonly byte[] eq_crlf_crlf = Encoding.ASCII.GetBytes("=\r\n\r\n"); private static readonly char[] hexChars = --- 17,23 ---- public sealed class MIME { ! private MIME() { } ! private static readonly byte[] crlf = Encoding.ASCII.GetBytes("\r\n"); ! private static readonly byte[] eq_crlf = Encoding.ASCII.GetBytes("=\r\n"); private static readonly byte[] eq_crlf_crlf = Encoding.ASCII.GetBytes("=\r\n\r\n"); private static readonly char[] hexChars = *************** *** 32,42 **** #region encodemap ! private const byte NUL = 0x00; // don't encode ! private const byte ESC = 0x01; // always encode ! private const byte SPC = 0x02; // horizontal whitespace ! private const byte BOL = 0x03; // encode if at beginning of line ! private const byte CR = 0x04; // carriage return ! private const byte LF = 0x05; // linefeed ! private static readonly byte[] encodemap = { ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, // 000-007 --- 32,42 ---- #region encodemap ! private const byte NUL = 0x00; // don't encode ! private const byte ESC = 0x01; // always encode ! private const byte SPC = 0x02; // horizontal whitespace ! private const byte BOL = 0x03; // encode if at beginning of line ! private const byte CR = 0x04; // carriage return ! private const byte LF = 0x05; // linefeed ! private static readonly byte[] encodemap = { ESC, ESC, ESC, ESC, ESC, ESC, ESC, ESC, // 000-007 *************** *** 81,87 **** private const byte DNUL = 0x7F; private const byte DESC = 0x7E; ! private const byte DCR = 0x7D; ! private const byte DLF = 0x7C; ! private static readonly byte[] decodemap = { DNUL, DNUL, DNUL, DNUL, DNUL, DNUL, DNUL, DNUL, // 000-007 --- 81,87 ---- private const byte DNUL = 0x7F; private const byte DESC = 0x7E; ! private const byte DCR = 0x7D; ! private const byte DLF = 0x7C; ! private static readonly byte[] decodemap = { DNUL, DNUL, DNUL, DNUL, DNUL, DNUL, DNUL, DNUL, // 000-007 *************** *** 134,192 **** return null; - byte b, c; byte lastByte = NUL; bool hadWhitespace = false; int column = 0, buflen = originalBuffer.Length; MemoryStream ms = new MemoryStream(); ! try { ! for(int i = 0; i < buflen; i++) { ! b = originalBuffer[i]; ! c = encodemap[b]; ! ! if (c == NUL) ! { ! ms.WriteByte(b); ! column++; ! hadWhitespace = false; ! } ! else if (c == SPC) ! { ! ms.WriteByte(b); ! column++; ! hadWhitespace = true; ! } ! else if (c == LF && lastByte == CR) ! { ! // ignore ! } ! else if (c == CR || c == LF) { ! if (hadWhitespace) ! { ! ms.Write(eq_crlf_crlf, 0, eq_crlf_crlf.Length); hadWhitespace = false; ! } ! else ! ms.Write(crlf, 0, crlf.Length); ! column ^= column; ! } ! else if ((c == BOL) && (column != 0) && ((flags & SmtpEncode.Dot) != SmtpEncode.None)) ! { ! ms.WriteByte(b); ! column++; ! hadWhitespace = false; ! } ! else ! { ! ms.Write(new byte[] { (byte)'=', hex[0x0f & (b >> 4)], hex[0x0f & b] }, 0, 3); ! column += 3; ! hadWhitespace = false; } lastByte = c; // ASSERT: Debug.Assert(column <= 76); ! if (column >= 73) { ms.Write(eq_crlf, 0, eq_crlf.Length); --- 134,201 ---- return null; byte lastByte = NUL; bool hadWhitespace = false; int column = 0, buflen = originalBuffer.Length; MemoryStream ms = new MemoryStream(); ! try { ! for (int i = 0; i < buflen; i++) { ! byte b = originalBuffer[i]; ! byte c = encodemap[b]; ! switch (c) { ! case NUL: ! ms.WriteByte(b); ! column++; hadWhitespace = false; ! break; ! case SPC: ! ms.WriteByte(b); ! column++; ! hadWhitespace = true; ! break; ! ! default: ! if (c != LF || lastByte != CR) ! { ! switch (c) ! { ! case CR: ! case LF: ! if (hadWhitespace) ! { ! ms.Write(eq_crlf_crlf, 0, eq_crlf_crlf.Length); ! hadWhitespace = false; ! } ! else ! { ! ms.Write(crlf, 0, crlf.Length); ! } ! column ^= column; ! break; ! ! default: ! if ((c == BOL) && (column != 0) && ((flags & SmtpEncode.Dot) != SmtpEncode.None)) ! { ! ms.WriteByte(b); ! column++; ! hadWhitespace = false; ! } ! else ! { ! ms.Write(new byte[] { (byte)'=', hex[0x0f & (b >> 4)], hex[0x0f & b] }, 0, 3); ! column += 3; ! hadWhitespace = false; ! } ! break; ! } ! } ! break; } lastByte = c; // ASSERT: Debug.Assert(column <= 76); ! if (column >= 73) { ms.Write(eq_crlf, 0, eq_crlf.Length); *************** *** 203,207 **** return ret; } ! catch { return null; --- 212,216 ---- return ret; } ! catch { return null; *************** *** 210,216 **** { ms.SetLength(0L); ! ms.Capacity=0; ms.Close(); ! ms=null; } } --- 219,225 ---- { ms.SetLength(0L); ! ms.Capacity = 0; ms.Close(); ! ms = null; } } *************** *** 227,235 **** byte[] token = new byte[3]; int tokenLength = 0, i = 0; ! try { while (i < buflen) { ! if (tokenLength == 0) { int j = i; --- 236,244 ---- byte[] token = new byte[3]; int tokenLength = 0, i = 0; ! try { while (i < buflen) { ! if (tokenLength == 0) { int j = i; *************** *** 241,245 **** break; } ! byte b = decodedBuffer[i++]; byte c = decodemap[b]; --- 250,254 ---- break; } ! byte b = decodedBuffer[i++]; byte c = decodemap[b]; *************** *** 255,265 **** if (n1 == DCR || n1 == DLF) { // "=\r", "=\n", or "=\r\n" ! if (n1 == DLF || n2 != DLF) { ! if (n2 == DESC) { token[0] = token[2]; tokenLength = 1; ! } else { --- 264,274 ---- if (n1 == DCR || n1 == DLF) { // "=\r", "=\n", or "=\r\n" ! if (n1 == DLF || n2 != DLF) { ! if (n2 == DESC) { token[0] = token[2]; tokenLength = 1; ! } else { *************** *** 283,294 **** catch { ! ret = null; } finally { ms.SetLength(0L); ! ms.Capacity=0; ms.Close(); ! ms=null; } } --- 292,303 ---- catch { ! ret = null; } finally { ms.SetLength(0L); ! ms.Capacity = 0; ms.Close(); ! ms = null; } } *************** *** 301,310 **** internal static String QEncode(byte[] bufferOriginal, string charSet) { ! if (bufferOriginal == null)return String.Empty; int bufferLen = bufferOriginal.Length; ! if (bufferLen == 0) return String.Empty; ! if (charSet==null||charSet==string.Empty) charSet = Erle.DnsMail.MailInfo.DefaultCharset; ! try { int nRead = 0; StringBuilder sb = new StringBuilder(); --- 310,319 ---- internal static String QEncode(byte[] bufferOriginal, string charSet) { ! if (bufferOriginal == null) return String.Empty; int bufferLen = bufferOriginal.Length; ! if (bufferLen == 0) return String.Empty; ! if (charSet == null || charSet == string.Empty) charSet = Erle.DnsMail.MailInfo.DefaultCharset; ! try { int nRead = 0; StringBuilder sb = new StringBuilder(); *************** *** 319,330 **** continue; } ! sb.Append( '=' ); ! sb.Append( hexChars[(ch >> 4) & 0x0f] ); ! sb.Append( hexChars[ch & 0x0f] ); } sb.Append("?="); return sb.ToString(); } ! catch { return String.Empty; --- 328,339 ---- continue; } ! sb.Append('='); ! sb.Append(hexChars[(ch >> 4) & 0x0f]); ! sb.Append(hexChars[ch & 0x0f]); } sb.Append("?="); return sb.ToString(); } ! catch { return String.Empty; *************** *** 340,357 **** internal static String BEncode(byte[] bufferOriginal, string charSet) { ! if (bufferOriginal == null)return String.Empty; int bufferLen = bufferOriginal.Length; ! if (bufferLen == 0) return String.Empty; ! if (charSet==null||charSet==string.Empty) charSet = Erle.DnsMail.MailInfo.DefaultCharset; ! try { StringBuilder sb = new StringBuilder(); sb.Append("=?" + charSet + "?B?"); ! sb.Append(Convert.ToBase64String(bufferOriginal)); sb.Append("?="); return sb.ToString(); } ! catch { return String.Empty; --- 349,366 ---- internal static String BEncode(byte[] bufferOriginal, string charSet) { ! if (bufferOriginal == null) return String.Empty; int bufferLen = bufferOriginal.Length; ! if (bufferLen == 0) return String.Empty; ! if (charSet == null || charSet == string.Empty) charSet = Erle.DnsMail.MailInfo.DefaultCharset; ! try { StringBuilder sb = new StringBuilder(); sb.Append("=?" + charSet + "?B?"); ! sb.Append(Convert.ToBase64String(bufferOriginal)); sb.Append("?="); return sb.ToString(); } ! catch { return String.Empty; *************** *** 364,366 **** #endregion } ! }; \ No newline at end of file --- 373,375 ---- #endregion } ! }; |