Ejemplo n.º 1
0
        internal static long QuoteAndFold(Stream stream, MimeStringList fragments, uint inputMask, bool quoteOutput, bool addSpaceAtStart, bool allowUTF8, int lastLineReserve, ref MimeStringLength currentLineLength, ref byte[] scratchBuffer)
        {
            long num = 0L;

            Header.LineBuffer lineBuffer = default(Header.LineBuffer);
            lineBuffer.Length             = new MimeStringLength(0);
            lineBuffer.LengthTillLastLWSP = new MimeStringLength(-1);
            if (scratchBuffer == null || scratchBuffer.Length < 998)
            {
                scratchBuffer = new byte[998];
            }
            lineBuffer.Bytes = scratchBuffer;
            MimeScan.Token token = quoteOutput ? (MimeScan.Token.Spec | MimeScan.Token.Fwsp) : MimeScan.Token.Fwsp;
            bool           flag  = false;

            if (addSpaceAtStart && currentLineLength.InBytes != 0)
            {
                num += Header.WriteToken(Header.Space, 0, new MimeStringLength(1), stream, ref currentLineLength, ref lineBuffer, ref flag, allowUTF8);
                flag = true;
            }
            if (quoteOutput)
            {
                num += Header.WriteToken(Header.DoubleQuote, 0, new MimeStringLength(1), stream, ref currentLineLength, ref lineBuffer, ref flag, allowUTF8);
            }
            for (int i = 0; i < fragments.Count; i++)
            {
                MimeString mimeString = fragments[i];
                int        num2       = 0;
                int        num3       = 0;
                byte[]     data       = mimeString.GetData(out num2, out num3);
                if ((mimeString.Mask & inputMask) != 0U)
                {
                    do
                    {
                        int valueInChars = 0;
                        int num4         = MimeScan.FindNextOf(token, data, num2, num3, out valueInChars, allowUTF8);
                        if (num4 > 0)
                        {
                            num  += Header.WriteToken(data, num2, new MimeStringLength(valueInChars, num4), stream, ref currentLineLength, ref lineBuffer, ref flag, allowUTF8);
                            num2 += num4;
                            num3 -= num4;
                        }
                        if (num3 != 0)
                        {
                            byte b = data[num2];
                            if ((b == 34 || b == 92) && (mimeString.Mask & 3758096383U) != 0U)
                            {
                                num += Header.WriteToken(new byte[]
                                {
                                    92,
                                    data[num2]
                                }, 0, new MimeStringLength(2), stream, ref currentLineLength, ref lineBuffer, ref flag, allowUTF8);
                                num2++;
                                num3--;
                            }
                            else
                            {
                                num += Header.WriteToken(new byte[]
                                {
                                    data[num2]
                                }, 0, new MimeStringLength(1), stream, ref currentLineLength, ref lineBuffer, ref flag, allowUTF8);
                                num2++;
                                num3--;
                            }
                        }
                    }while (num3 != 0);
                }
            }
            if (quoteOutput)
            {
                num += Header.WriteToken(Header.DoubleQuote, 0, new MimeStringLength(1), stream, ref currentLineLength, ref lineBuffer, ref flag, allowUTF8);
            }
            if (lastLineReserve > 0)
            {
                num += Header.WriteToken(null, 0, new MimeStringLength(lastLineReserve), stream, ref currentLineLength, ref lineBuffer, ref flag, allowUTF8);
            }
            if (lineBuffer.Length.InBytes > 0)
            {
                stream.Write(lineBuffer.Bytes, 0, lineBuffer.Length.InBytes);
                num += (long)lineBuffer.Length.InBytes;
                currentLineLength.IncrementBy(lineBuffer.Length);
            }
            return(num);
        }