Exemplo n.º 1
0
 private static long WriteToken(byte[] token, int tokenOffset, MimeStringLength tokenLength, System.IO.Stream stream, ref MimeStringLength currentLineLength, ref LineBuffer lineBuffer, ref bool autoAddedLastLWSP, bool allowUTF8)
 {
     var num1 = 0L;
     var flag1 = token != null && tokenLength.InChars == 1 && MimeScan.IsFWSP(token[tokenOffset]);
     if (!flag1 && currentLineLength.InChars + lineBuffer.Length.InChars + tokenLength.InChars > 78 && lineBuffer.LengthTillLastLWSP.InBytes >= 0) {
         if (lineBuffer.LengthTillLastLWSP.InBytes > 0) {
             stream.Write(lineBuffer.Bytes, 0, lineBuffer.LengthTillLastLWSP.InBytes);
             num1 += lineBuffer.LengthTillLastLWSP.InBytes;
             currentLineLength.IncrementBy(lineBuffer.LengthTillLastLWSP);
         }
         if (currentLineLength.InBytes > 0)
             num1 += Header.WriteLineEnd(stream, ref currentLineLength);
         if (autoAddedLastLWSP) {
             autoAddedLastLWSP = false;
             lineBuffer.LengthTillLastLWSP.IncrementBy(1);
         }
         if (lineBuffer.LengthTillLastLWSP.InBytes != lineBuffer.Length.InBytes) {
             System.Buffer.BlockCopy(lineBuffer.Bytes, lineBuffer.LengthTillLastLWSP.InBytes, lineBuffer.Bytes, 0, lineBuffer.Length.InBytes - lineBuffer.LengthTillLastLWSP.InBytes);
             lineBuffer.Length.DecrementBy(lineBuffer.LengthTillLastLWSP);
             if (lineBuffer.Length.InBytes > 0 && MimeScan.IsFWSP(lineBuffer.Bytes[0]))
                 lineBuffer.LengthTillLastLWSP.SetAs(0);
             else
                 lineBuffer.LengthTillLastLWSP.SetAs(-1);
         } else {
             lineBuffer.Length.SetAs(0);
             lineBuffer.LengthTillLastLWSP.SetAs(-1);
         }
         var flag2 = false;
         if (lineBuffer.Length.InBytes > 0) {
             if (!MimeScan.IsFWSP(lineBuffer.Bytes[0]))
                 flag2 = true;
         } else if (!flag1)
             flag2 = true;
         if (flag2) {
             stream.Write(LineStartWhitespace, 0, LineStartWhitespace.Length);
             num1 += LineStartWhitespace.Length;
             currentLineLength.IncrementBy(LineStartWhitespace.Length);
         }
     }
     if (currentLineLength.InBytes + lineBuffer.Length.InBytes + tokenLength.InBytes > 998) {
         if (lineBuffer.Length.InBytes > 0) {
             stream.Write(lineBuffer.Bytes, 0, lineBuffer.Length.InBytes);
             num1 += lineBuffer.Length.InBytes;
             currentLineLength.IncrementBy(lineBuffer.Length);
             lineBuffer.Length.SetAs(0);
             autoAddedLastLWSP = false;
             lineBuffer.LengthTillLastLWSP.SetAs(-1);
         }
         if (token != null) {
             while (currentLineLength.InBytes + tokenLength.InBytes > 998) {
                 var num2 = System.Math.Max(0, 998 - currentLineLength.InBytes);
                 var countInChars = 0;
                 var num3 = 0;
                 if (allowUTF8) {
                     var num4 = 0;
                     while (num4 < tokenLength.InBytes) {
                         var num5 = token[tokenOffset + num4];
                         var bytesUsed = 1;
                         if (num5 >= 128 && !MimeScan.IsUTF8NonASCII(token, tokenOffset + num4, tokenOffset + tokenLength.InBytes, out bytesUsed))
                             bytesUsed = 1;
                         if (num3 + bytesUsed <= num2) {
                             ++countInChars;
                             num3 += bytesUsed;
                             num4 += bytesUsed;
                         } else
                             break;
                     }
                 } else {
                     countInChars = num2;
                     num3 = num2;
                 }
                 stream.Write(token, tokenOffset, num3);
                 var num6 = num1 + num3;
                 currentLineLength.IncrementBy(countInChars, num3);
                 tokenOffset += num3;
                 tokenLength.DecrementBy(countInChars, num3);
                 num1 = num6 + Header.WriteLineEnd(stream, ref currentLineLength);
                 if (!flag1) {
                     stream.Write(LineStartWhitespace, 0, LineStartWhitespace.Length);
                     num1 += LineStartWhitespace.Length;
                     currentLineLength.IncrementBy(LineStartWhitespace.Length);
                 }
             }
         }
     }
     if (token != null) {
         System.Buffer.BlockCopy(token, tokenOffset, lineBuffer.Bytes, lineBuffer.Length.InBytes, tokenLength.InBytes);
         if (flag1 && (lineBuffer.Length.InBytes == 0 || !MimeScan.IsFWSP(lineBuffer.Bytes[lineBuffer.Length.InBytes - 1]))) {
             autoAddedLastLWSP = false;
             lineBuffer.LengthTillLastLWSP.SetAs(lineBuffer.Length);
         }
         lineBuffer.Length.IncrementBy(tokenLength);
     }
     return num1;
 }
Exemplo n.º 2
0
 internal static long WriteLines(MimeStringList lines, System.IO.Stream stream)
 {
     if (lines.Count == 0) {
         var currentLineLength = new MimeStringLength(0);
         return Header.WriteLineEnd(stream, ref currentLineLength);
     }
     var num = 0L;
     for (var index = 0; index < lines.Count; ++index) {
         int offset;
         int count;
         var data = lines[index].GetData(out offset, out count);
         if (count != 0) {
             if (!MimeScan.IsLWSP(data[offset])) {
                 stream.Write(MimeString.Space, 0, MimeString.Space.Length);
                 num += MimeString.Space.Length;
             }
             stream.Write(data, offset, count);
             num += count;
         }
         var currentLineLength = new MimeStringLength(0);
         num += Header.WriteLineEnd(stream, ref currentLineLength);
     }
     return num;
 }
Exemplo n.º 3
0
 internal override long WriteTo(System.IO.Stream stream, EncodingOptions encodingOptions, MimeOutputFilter filter, ref MimeStringLength currentLineLength, ref byte[] scratchBuffer)
 {
     var nameLength = this.WriteName(stream, ref scratchBuffer);
     currentLineLength.IncrementBy((int) nameLength);
     var merge = false;
     if (!this.IsDirty && this.RawLength != 0) {
         if (this.IsProtected) {
             var num = nameLength + Header.WriteLines(lines, stream);
             currentLineLength.SetAs(0);
             return num;
         }
         if (!this.IsHeaderLineTooLong(nameLength, out merge)) {
             var num = nameLength + Header.WriteLines(lines, stream);
             currentLineLength.SetAs(0);
             return num;
         }
     }
     var mimeStringList = lines;
     if (merge)
         mimeStringList = Header.MergeLines(mimeStringList);
     return nameLength + Header.QuoteAndFold(stream, mimeStringList, 4026531840U, false, mimeStringList.Length > 0, encodingOptions.AllowUTF8, 0, ref currentLineLength, ref scratchBuffer) +
            Header.WriteLineEnd(stream, ref currentLineLength);
 }
Exemplo n.º 4
0
 internal static long QuoteAndFold(
     System.IO.Stream stream, MimeStringList fragments, uint inputMask, bool quoteOutput, bool addSpaceAtStart, bool allowUTF8, int lastLineReserve, ref MimeStringLength currentLineLength, ref byte[] scratchBuffer)
 {
     var num = 0L;
     var lineBuffer = new LineBuffer();
     lineBuffer.Length = new MimeStringLength(0);
     lineBuffer.LengthTillLastLWSP = new MimeStringLength(-1);
     if (scratchBuffer == null || scratchBuffer.Length < 998)
         scratchBuffer = new byte[998];
     lineBuffer.Bytes = scratchBuffer;
     var token = quoteOutput ? MimeScan.Token.Spec | MimeScan.Token.Fwsp : MimeScan.Token.Fwsp;
     var autoAddedLastLWSP = false;
     if (addSpaceAtStart && currentLineLength.InBytes != 0) {
         num += Header.WriteToken(Space, 0, new MimeStringLength(1), stream, ref currentLineLength, ref lineBuffer, ref autoAddedLastLWSP, allowUTF8);
         autoAddedLastLWSP = true;
     }
     if (quoteOutput)
         num += Header.WriteToken(DoubleQuote, 0, new MimeStringLength(1), stream, ref currentLineLength, ref lineBuffer, ref autoAddedLastLWSP, allowUTF8);
     for (var index = 0; index < fragments.Count; ++index) {
         var mimeString = fragments[index];
         var offset = 0;
         var count = 0;
         var data = mimeString.GetData(out offset, out count);
         if (((int) mimeString.Mask & (int) inputMask) != 0) {
             do {
                 var characterCount = 0;
                 var nextOf = MimeScan.FindNextOf(token, data, offset, count, out characterCount, allowUTF8);
                 if (nextOf > 0) {
                     num += Header.WriteToken(data, offset, new MimeStringLength(characterCount, nextOf), stream, ref currentLineLength, ref lineBuffer, ref autoAddedLastLWSP, allowUTF8);
                     offset += nextOf;
                     count -= nextOf;
                 }
                 if (count != 0) {
                     switch (data[offset]) {
                         case 34:
                         case 92:
                             if (((int) mimeString.Mask & -536870913) != 0) {
                                 num += Header.WriteToken(
                                     new byte[2] {
                                         92,
                                         data[offset]
                                     },
                                     0,
                                     new MimeStringLength(2),
                                     stream,
                                     ref currentLineLength,
                                     ref lineBuffer,
                                     ref autoAddedLastLWSP,
                                     allowUTF8);
                                 ++offset;
                                 --count;
                                 goto label_15;
                             }
                             break;
                     }
                     num += Header.WriteToken(
                         new byte[1] {
                             data[offset]
                         },
                         0,
                         new MimeStringLength(1),
                         stream,
                         ref currentLineLength,
                         ref lineBuffer,
                         ref autoAddedLastLWSP,
                         allowUTF8);
                     ++offset;
                     --count;
                 }
                 label_15:
                 ;
             } while (count != 0);
         }
     }
     if (quoteOutput)
         num += Header.WriteToken(DoubleQuote, 0, new MimeStringLength(1), stream, ref currentLineLength, ref lineBuffer, ref autoAddedLastLWSP, allowUTF8);
     if (lastLineReserve > 0)
         num += Header.WriteToken(null, 0, new MimeStringLength(lastLineReserve), stream, ref currentLineLength, ref lineBuffer, ref autoAddedLastLWSP, allowUTF8);
     if (lineBuffer.Length.InBytes > 0) {
         stream.Write(lineBuffer.Bytes, 0, lineBuffer.Length.InBytes);
         num += lineBuffer.Length.InBytes;
         currentLineLength.IncrementBy(lineBuffer.Length);
     }
     return num;
 }
Exemplo n.º 5
0
 internal static long WriteLineEnd(System.IO.Stream stream, ref MimeStringLength currentLineLength)
 {
     var num1 = 0L;
     stream.Write(MimeString.CrLf, 0, MimeString.CrLf.Length);
     var num2 = num1 + MimeString.CrLf.Length;
     currentLineLength.SetAs(0);
     return num2;
 }
Exemplo n.º 6
0
 internal static long WriteDateHeaderValue(System.IO.Stream stream, System.DateTime utcDateTime, System.TimeSpan timeZoneOffset, ref MimeStringLength currentLineLength)
 {
     var num1 = 0L;
     var characterCount = 0;
     var buffer = DateHeader.FormatValue(utcDateTime, timeZoneOffset, out characterCount);
     stream.Write(MimeString.Space, 0, MimeString.Space.Length);
     var num2 = num1 + MimeString.Space.Length;
     currentLineLength.IncrementBy(MimeString.Space.Length);
     long num3;
     if (buffer != null) {
         stream.Write(buffer, 0, buffer.Length);
         num3 = num2 + buffer.Length;
         currentLineLength.IncrementBy(characterCount, buffer.Length);
     } else {
         stream.Write(MimeString.CommentInvalidDate, 0, MimeString.CommentInvalidDate.Length);
         num3 = num2 + MimeString.CommentInvalidDate.Length;
         currentLineLength.IncrementBy(MimeString.CommentInvalidDate.Length);
     }
     return num3 + Header.WriteLineEnd(stream, ref currentLineLength);
 }
Exemplo n.º 7
0
 internal override long WriteTo(System.IO.Stream stream, EncodingOptions encodingOptions, MimeOutputFilter filter, ref MimeStringLength currentLineLength, ref byte[] scratchBuffer)
 {
     var num1 = this.WriteName(stream, ref scratchBuffer);
     currentLineLength.IncrementBy((int) num1);
     if (!this.IsDirty && this.RawLength != 0 && this.IsProtected) {
         var num2 = Header.WriteLines(this.Lines, stream);
         var num3 = num1 + num2;
         currentLineLength.SetAs(0);
         return num3;
     }
     if (!parsed)
         this.Parse();
     var num4 = num1 + DateHeader.WriteDateHeaderValue(stream, utcDateTime, timeZoneOffset, ref currentLineLength);
     currentLineLength.SetAs(0);
     return num4;
 }
Exemplo n.º 8
0
 public void WriteHeaderValue(System.DateTime value)
 {
     this.AssertOpen();
     if (headerValueWritten)
         throw new System.InvalidOperationException(Resources.Strings.CannotWriteHeaderValueMoreThanOnce);
     if (lastHeader == null)
         throw new System.InvalidOperationException(Resources.Strings.CannotWriteHeaderValueHere);
     headerValueWritten = true;
     var timeZoneOffset = System.TimeSpan.Zero;
     var utcDateTime = value.ToUniversalTime();
     if (value.Kind != System.DateTimeKind.Utc)
         timeZoneOffset = System.TimeZoneInfo.Local.GetUtcOffset(value);
     Header.WriteName(shimStream, lastHeader.Name, ref scratchBuffer);
     var currentLineLength = new MimeStringLength(0);
     DateHeader.WriteDateHeaderValue(shimStream, utcDateTime, timeZoneOffset, ref currentLineLength);
     lastHeader = null;
 }
Exemplo n.º 9
0
 internal override long WriteValue(System.IO.Stream stream, EncodingOptions encodingOptions, MimeOutputFilter filter, ref MimeStringLength currentLineLength, ref byte[] scratchBuffer)
 {
     if (disp.Length == 0)
         disp = "attachment";
     return base.WriteValue(stream, encodingOptions, filter, ref currentLineLength, ref scratchBuffer);
 }
Exemplo n.º 10
0
 internal override long WriteTo(System.IO.Stream stream, EncodingOptions encodingOptions, MimeOutputFilter filter, ref MimeStringLength currentLineLength, ref byte[] scratchBuffer)
 {
     var nameLength = this.WriteName(stream, ref scratchBuffer);
     currentLineLength.IncrementBy((int) nameLength);
     MimeStringList mimeStringList;
     if (this.RawLength == 0 && decodedValue != null && decodedValue.Length != 0)
         mimeStringList = this.GetEncodedValue(encodingOptions, ValueEncodingStyle.Normal);
     else if ((EncodingFlags.ForceReencode & encodingOptions.EncodingFlags) != EncodingFlags.None) {
         this.ForceParse();
         mimeStringList = this.GetEncodedValue(encodingOptions, ValueEncodingStyle.Normal);
     } else {
         var merge = false;
         if (!this.IsDirty && this.RawLength != 0) {
             if (this.IsProtected) {
                 var num = nameLength + Header.WriteLines(this.Lines, stream);
                 currentLineLength.SetAs(0);
                 return num;
             }
             if (!this.IsHeaderLineTooLong(nameLength, out merge)) {
                 var num = nameLength + Header.WriteLines(this.Lines, stream);
                 currentLineLength.SetAs(0);
                 return num;
             }
         }
         mimeStringList = this.Lines;
         if (merge)
             mimeStringList = Header.MergeLines(mimeStringList);
     }
     return nameLength + Header.QuoteAndFold(stream, mimeStringList, 4026531840U, false, mimeStringList.Length > 0, encodingOptions.AllowUTF8, 0, ref currentLineLength, ref scratchBuffer) +
            Header.WriteLineEnd(stream, ref currentLineLength);
 }
Exemplo n.º 11
0
 public void SetAs(MimeStringLength value)
 {
     this.InChars = value.InChars;
     this.InBytes = value.InBytes;
 }
Exemplo n.º 12
0
 public void IncrementBy(MimeStringLength count)
 {
     this.InChars += count.InChars;
     this.InBytes += count.InBytes;
 }