예제 #1
0
        private static bool ZTryAsQuotedUTF8(string pString, bool pSecret, out cTextCommandPart rResult)
        {
            if (pString == null)
            {
                rResult = null; return(false);
            }

            var lEncBytes = Encoding.UTF8.GetBytes(pString);

            var lBytes = new cByteList(lEncBytes.Length + 2);

            lBytes.Add(cASCII.DQUOTE);

            foreach (byte lByte in lEncBytes)
            {
                if (lByte == cASCII.NUL || lByte == cASCII.CR || lByte == cASCII.LF)
                {
                    rResult = null; return(false);
                }
                if (lByte == cASCII.DQUOTE || lByte == cASCII.BACKSL)
                {
                    lBytes.Add(cASCII.BACKSL);
                }
                lBytes.Add(lByte);
            }

            lBytes.Add(cASCII.DQUOTE);

            rResult = new cTextCommandPart(lBytes, pSecret);
            return(true);
        }
예제 #2
0
        private static bool ZTryAsQuotedASCII(string pString, bool pSecret, out cTextCommandPart rResult)
        {
            if (pString == null)
            {
                rResult = null; return(false);
            }

            var lBytes = new cByteList(pString.Length + 2);

            lBytes.Add(cASCII.DQUOTE);

            foreach (char lChar in pString)
            {
                if (lChar == cChar.NUL || lChar == cChar.CR || lChar == cChar.LF || lChar > cChar.DEL)
                {
                    rResult = null; return(false);
                }
                if (lChar == '"' || lChar == '\\')
                {
                    lBytes.Add(cASCII.BACKSL);
                }
                lBytes.Add((byte)lChar);
            }

            lBytes.Add(cASCII.DQUOTE);

            rResult = new cTextCommandPart(lBytes, pSecret);
            return(true);
        }
예제 #3
0
        private static bool ZTryAsQuotedASCII(IList <byte> pBytes, bool pSecret, bool pEncoded, out cTextCommandPart rResult)
        {
            if (pBytes == null)
            {
                rResult = null; return(false);
            }

            var lBytes = new cByteList(pBytes.Count + 2);

            lBytes.Add(cASCII.DQUOTE);

            foreach (byte lByte in pBytes)
            {
                if (lByte == cASCII.NUL || lByte == cASCII.CR || lByte == cASCII.LF || lByte > cASCII.DEL)
                {
                    rResult = null; return(false);
                }
                if (lByte == cASCII.DQUOTE || lByte == cASCII.BACKSL)
                {
                    lBytes.Add(cASCII.BACKSL);
                }
                lBytes.Add(lByte);
            }

            lBytes.Add(cASCII.DQUOTE);

            rResult = new cTextCommandPart(lBytes, pSecret, pEncoded);
            return(true);
        }
예제 #4
0
 public static bool TryAsUTF8String(string pString, out cTextCommandPart rResult)
 {
     if (pString == null)
     {
         rResult = null; return(false);
     }
     return(ZTryAsQuotedUTF8(pString, false, out rResult));
 }
예제 #5
0
        private static void _Tests_MailboxName(string pMailboxPath, bool pExpectFail, cTrace.cContext pParentContext)
        {
            var lContext = pParentContext.NewMethod(nameof(cMailboxName), nameof(_Tests_MailboxName), pMailboxPath);

            cCommandPartFactory lFactory;
            cCommandPart        lCommandPart;
            cBytesCursor        lCursor;
            IList <byte>        lEncodedMailboxPath;
            cMailboxName        lMailboxName;

            lFactory = new cCommandPartFactory(false, null);

            if (!lFactory.TryAsMailbox(pMailboxPath, '/', out lCommandPart, out _))
            {
                throw new cTestsException($"mailboxname conversion failed on '{pMailboxPath}'");
            }
            cTextCommandPart lTCP = lCommandPart as cTextCommandPart;

            lCursor = new cBytesCursor(lTCP.Bytes);
            lCursor.GetAString(out lEncodedMailboxPath);

            if (cMailboxName.TryConstruct(lEncodedMailboxPath, cASCII.SLASH, false, out lMailboxName))
            {
                if (pExpectFail)
                {
                    throw new cTestsException($"mailboxname construction succeeded on '{pMailboxPath}' and it shouldn't have");
                }
            }
            else
            {
                if (!pExpectFail)
                {
                    throw new cTestsException($"mailboxname construction failed on '{pMailboxPath}' and it shouldn't have");
                }
                return;
            }

            if (lMailboxName.Path != pMailboxPath)
            {
                throw new cTestsException($"mailboxname conversion failed on '{pMailboxPath}' -> {lTCP.Bytes} -> '{lMailboxName}'", lContext);
            }

            lFactory = new cCommandPartFactory(true, null);
            lFactory.TryAsMailbox(pMailboxPath, '/', out lCommandPart, out _);
            lTCP    = lCommandPart as cTextCommandPart;
            lCursor = new cBytesCursor(lTCP.Bytes);
            lCursor.GetAString(out lEncodedMailboxPath);
            cMailboxName.TryConstruct(lEncodedMailboxPath, cASCII.SLASH, true, out lMailboxName);
            if (lMailboxName.Path != pMailboxPath)
            {
                throw new cTestsException($"mailboxname conversion failed on '{pMailboxPath}' -> {lTCP.Bytes} -> '{lMailboxName}'", lContext);
            }
        }
예제 #6
0
 public static bool TryAsCharsetName(string pString, out cTextCommandPart rResult)
 {
     // rfc 3501 says a charset can be an astring; rfc 5256 says a charset can be an atom or a quoted string
     //  I'm going with the latter here ...
     //
     if (TryAsAtom(pString, out rResult))
     {
         return(true);
     }
     if (ZTryAsQuotedASCII(pString, false, out rResult))
     {
         return(true);
     }
     return(false);
 }
예제 #7
0
 private static bool ZTryAsBytesInCharset(IList <byte> pBytes, cCharset pCharset, bool pSecret, bool pEncoded, out cTextCommandPart rResult)
 {
     if (pBytes == null || pBytes.Count == 0)
     {
         rResult = null; return(false);
     }
     foreach (byte lByte in pBytes)
     {
         if (!pCharset.Contains(lByte))
         {
             rResult = null; return(false);
         }
     }
     rResult = new cTextCommandPart(pBytes, pSecret, pEncoded);
     return(true);
 }
예제 #8
0
        private static bool ZTryAsBytesInCharset(string pString, cCharset pCharset, bool pSecret, out cTextCommandPart rResult)
        {
            if (string.IsNullOrEmpty(pString))
            {
                rResult = null; return(false);
            }

            var lBytes = new cByteList(pString.Length);

            foreach (char lChar in pString)
            {
                if (!pCharset.Contains(lChar))
                {
                    rResult = null; return(false);
                }
                lBytes.Add((byte)lChar);
            }

            rResult = new cTextCommandPart(lBytes, pSecret);
            return(true);
        }
예제 #9
0
        public cCommandPartFactory(bool pUTF8Enabled, Encoding pEncoding)
        {
            UTF8Enabled = pUTF8Enabled;

            if (pUTF8Enabled)
            {
                CharsetName = null;
            }
            else
            {
                Encoding = pEncoding;
                if (pEncoding == null)
                {
                    CharsetName = null;
                }
                else if (!TryAsCharsetName(pEncoding.WebName, out CharsetName))
                {
                    throw new ArgumentOutOfRangeException(nameof(pEncoding));
                }
            }
        }
예제 #10
0
 public static bool TryAsAtom(string pString, out cTextCommandPart rResult) => ZTryAsBytesInCharset(pString, cCharset.Atom, false, out rResult);