示例#1
0
文件: base.cs 项目: bacome/imapclient
        public bool GetToken(cCharset pCharset, byte?pEncodedIntro, byte?pRepresentsSpace, out string rString)
        {
            var lBookmark = Position;

            if (!GetToken(pCharset, pEncodedIntro, pRepresentsSpace, out cByteList lBytes))
            {
                rString = null;
                return(false);
            }

            rString = cTools.UTF8BytesToString(lBytes);
            return(true);
        }
示例#2
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);
 }
示例#3
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);
        }
示例#4
0
        private bool ZTryAsMailbox(string pString, char?pDelimiter, cCharset pCharset, out cCommandPart rCommandPart, out string rEncodedMailboxPath)
        {
            if (pString.Equals(cMailboxName.InboxString, StringComparison.InvariantCultureIgnoreCase))
            {
                rCommandPart        = cCommandPart.Inbox;
                rEncodedMailboxPath = cMailboxName.InboxString;
            }

            cTextCommandPart    lText;
            cLiteralCommandPart lLiteral;

            if (ZTryAsBytesInCharset(pString, pCharset, false, out lText))
            {
                rCommandPart        = lText;
                rEncodedMailboxPath = pString;
                return(true);
            }

            if (UTF8Enabled)
            {
                if (ZTryAsQuotedUTF8(pString, false, out lText))
                {
                    rCommandPart        = lText;
                    rEncodedMailboxPath = pString;
                    return(true);
                }

                rCommandPart        = null;
                rEncodedMailboxPath = null;
                return(false);
            }

            cByteList lBytes;

            if (pDelimiter == null)
            {
                lBytes = cModifiedUTF7.Encode(pString);
            }
            else
            {
                char lDelimiterChar = pDelimiter.Value;
                byte lDelimiterByte = (byte)lDelimiterChar;

                string[] lSegments = pString.Split(lDelimiterChar);

                lBytes = new cByteList();
                bool lFirst = true;

                foreach (string lSegment in lSegments)
                {
                    if (lFirst)
                    {
                        lFirst = false;
                    }
                    else
                    {
                        lBytes.Add(lDelimiterByte);
                    }
                    lBytes.AddRange(cModifiedUTF7.Encode(lSegment));
                }
            }

            if (ZTryAsBytesInCharset(lBytes, pCharset, false, false, out lText) || ZTryAsQuotedASCII(lBytes, false, false, out lText))
            {
                rCommandPart        = lText;
                rEncodedMailboxPath = cTools.ASCIIBytesToString(lBytes);
                return(true);
            }

            if (ZTryAsASCIILiteral(lBytes, false, out lLiteral))
            {
                rCommandPart        = lLiteral;
                rEncodedMailboxPath = cTools.ASCIIBytesToString(lBytes);
                return(true);
            }

            rCommandPart        = null;
            rEncodedMailboxPath = null;
            return(false);
        }
示例#5
0
文件: base.cs 项目: bacome/imapclient
        public bool GetToken(cCharset pCharset, byte?pEncodedIntro, byte?pRepresentsSpace, out cByteList rBytes, int pMinLength = 1, int pMaxLength = int.MaxValue)
        {
            if (Position.AtEnd)
            {
                rBytes = null; return(false);
            }
            if (Position.BytesLine.Literal)
            {
                rBytes = null; return(false);
            }

            var lBookmark1 = Position;

            rBytes = new cByteList();

            while (true)
            {
                byte lByte = Position.BytesLine[Position.Byte];

                if (lByte == pEncodedIntro)
                {
                    var lBookmark2 = Position;

                    ZAdvance(ref Position);

                    if (ZGetHexEncodedByte(out var lByteFromHex))
                    {
                        rBytes.Add(lByteFromHex);
                        if (Position.AtEnd || Position.BytesLine.Literal || rBytes.Count == pMaxLength)
                        {
                            break;
                        }
                        continue;
                    }

                    Position = lBookmark2;
                }

                if (lByte == pRepresentsSpace)
                {
                    rBytes.Add(cASCII.SPACE);
                }
                else
                {
                    if (!pCharset.Contains(lByte))
                    {
                        break;
                    }
                    rBytes.Add(lByte);
                }

                ZAdvance(ref Position);
                if (Position.AtEnd || Position.BytesLine.Literal || rBytes.Count == pMaxLength)
                {
                    break;
                }
            }

            if (rBytes.Count < pMinLength)
            {
                Position = lBookmark1; rBytes = null; return(false);
            }

            return(true);
        }