// Token: 0x06000256 RID: 598 RVA: 0x0000B710 File Offset: 0x00009910 private static void CalculateMethodAndChunkSize_Utf8(bool allowQEncoding, ByteEncoder.Tables.CharClasses unsafeCharClassesForQEncoding, Encoding encoding, string value, int valueOffset, int encodedWordSpace, out byte method, out int chunkSize) { int num = encodedWordSpace / 4 * 3; int num2 = 0; int num3 = 0; int num4 = 0; int num5 = 0; int num6 = valueOffset; bool flag = false; bool flag2 = false; while (num6 != value.Length && (!flag || !flag2)) { int num7 = 1; int num8 = 1; int num9 = 1; char c = value[num6++]; if (MimeCommon.QEncodingRequired(c, unsafeCharClassesForQEncoding)) { if (c > '\u007f') { num9++; if (c > '߿') { num9++; if (MimeCommon.IsAnySurrogate(c) && MimeCommon.IsHighSurrogate(c) && num6 != value.Length && MimeCommon.IsLowSurrogate(value[num6])) { num6++; num7++; num9++; } } } num8 = num9 * 3; } flag = (flag || num5 + num9 > num); flag2 = (flag2 || num3 + num8 > encodedWordSpace); if (!flag) { num5 += num9; num4 += num7; } if (!flag2) { num3 += num8; num2 += num7; } } if (num2 >= num4 && num3 < num5 + 3) { chunkSize = num2; method = 81; return; } chunkSize = MimeCommon.AdjustChunkSize(encoding, num4, value, valueOffset, num); method = 66; }
// Token: 0x06000254 RID: 596 RVA: 0x0000B4B0 File Offset: 0x000096B0 private static void CalculateMethodAndChunkSize_Sbcs(bool allowQEncoding, ByteEncoder.Tables.CharClasses unsafeCharClassesForQEncoding, Encoding encoding, string value, int valueOffset, int encodedWordSpace, out byte method, out int chunkSize) { int num = encodedWordSpace / 4 * 3; int num2 = Math.Min(num, value.Length - valueOffset); if (num2 != value.Length - valueOffset && MimeCommon.IsHighSurrogate(value[valueOffset + num2 - 1]) && MimeCommon.IsLowSurrogate(value[valueOffset + num2])) { num2--; } int num3 = (num2 + 2) / 3 * 4; if (!allowQEncoding) { chunkSize = MimeCommon.AdjustChunkSize(encoding, num2, value, valueOffset, num); method = 66; return; } int num4 = 0; int num5 = 0; int num6 = valueOffset; while (num6 != value.Length && num5 < encodedWordSpace) { int num7 = 1; int num8 = 1; char ch = value[num6++]; if (MimeCommon.QEncodingRequired(ch, unsafeCharClassesForQEncoding)) { num8 = 3; if (MimeCommon.IsHighSurrogate(ch) && num6 != value.Length && MimeCommon.IsLowSurrogate(value[num6])) { num6++; num7++; num8 = 6; } } if (num5 + num8 > encodedWordSpace) { break; } num5 += num8; num4 += num7; if (num5 > num3 && num4 <= num2) { break; } } if (num4 >= num2 && num5 < num3 + 3) { chunkSize = num4; method = 81; return; } chunkSize = MimeCommon.AdjustChunkSize(encoding, num2, value, valueOffset, num); method = 66; }
// Token: 0x0600025A RID: 602 RVA: 0x0000B9AC File Offset: 0x00009BAC private static int AdjustChunkSize(Encoding encoding, int chunkSize, string value, int valueOffset, int targetBytes) { int i = MimeCommon.GetByteCount(encoding, value, valueOffset, chunkSize); if (i > targetBytes) { int num = chunkSize * targetBytes / i; if (num < chunkSize) { if (MimeCommon.IsLowSurrogate(value[valueOffset + num]) && MimeCommon.IsHighSurrogate(value[valueOffset + num - 1])) { num--; } chunkSize = num; i = MimeCommon.GetByteCount(encoding, value, valueOffset, chunkSize); } while (i > targetBytes) { num = chunkSize * targetBytes / i; if (num < chunkSize) { chunkSize = num; } else { chunkSize--; } if (MimeCommon.IsLowSurrogate(value[valueOffset + chunkSize]) && MimeCommon.IsHighSurrogate(value[valueOffset + chunkSize - 1])) { chunkSize--; } i = MimeCommon.GetByteCount(encoding, value, valueOffset, chunkSize); } } if (i < targetBytes - 2 && i != 0) { int num2 = 0; while (valueOffset + chunkSize < value.Length && num2++ < 3) { int num3 = 1; if (MimeCommon.IsHighSurrogate(value[valueOffset + chunkSize]) && valueOffset + chunkSize + 1 < value.Length && MimeCommon.IsLowSurrogate(value[valueOffset + chunkSize + 1])) { num3++; } i = MimeCommon.GetByteCount(encoding, value, valueOffset, chunkSize + num3); if (i > targetBytes) { break; } chunkSize += num3; } } return(chunkSize); }
// Token: 0x06000259 RID: 601 RVA: 0x0000B934 File Offset: 0x00009B34 private static void CalculateMethodAndChunkSize_Mbcs(bool allowQEncoding, ByteEncoder.Tables.CharClasses unsafeCharClassesForQEncoding, Encoding encoding, string value, int valueOffset, int encodedWordSpace, out byte method, out int chunkSize) { int num = encodedWordSpace / 4 * 3; chunkSize = Math.Min(num, value.Length - valueOffset); if (chunkSize < value.Length - valueOffset && MimeCommon.IsLowSurrogate(value[valueOffset + chunkSize]) && MimeCommon.IsHighSurrogate(value[valueOffset + chunkSize - 1])) { chunkSize--; } chunkSize = MimeCommon.AdjustChunkSize(encoding, chunkSize, value, valueOffset, num); method = 66; }
// Token: 0x06000252 RID: 594 RVA: 0x0000AF40 File Offset: 0x00009140 internal static MimeStringList EncodeValue(string value, EncodingOptions encodingOptions, ValueEncodingStyle style) { if (string.IsNullOrEmpty(value)) { return(MimeStringList.Empty); } if (!MimeCommon.IsEncodingRequired(value, encodingOptions.AllowUTF8)) { return(new MimeStringList(new MimeString(value))); } MimeStringList result = default(MimeStringList); Charset encodingCharset; if (encodingOptions.CharsetName != null) { encodingCharset = encodingOptions.GetEncodingCharset(); } else { OutboundCodePageDetector outboundCodePageDetector = new OutboundCodePageDetector(); outboundCodePageDetector.AddText(value); int codePage = outboundCodePageDetector.GetCodePage(); if (!Charset.TryGetCharset(codePage, out encodingCharset)) { encodingCharset = MimeCommon.DefaultEncodingOptions.GetEncodingCharset(); } } ByteEncoder.Tables.CharClasses charClasses = ByteEncoder.Tables.CharClasses.QEncode; if (style == ValueEncodingStyle.Phrase) { charClasses |= ByteEncoder.Tables.CharClasses.QPhraseUnsafe; } else if (style == ValueEncodingStyle.Comment) { charClasses |= ByteEncoder.Tables.CharClasses.QCommentUnsafe; } bool allowQEncoding = false; MimeCommon.CalculateMethodAndChunkSize calculateMethodAndChunkSize; if (encodingCharset.Kind == CodePageKind.Sbcs) { calculateMethodAndChunkSize = MimeCommon.calculateMethodAndChunkSizeSbcs; if (encodingCharset.AsciiSupport >= CodePageAsciiSupport.Fine) { allowQEncoding = true; } } else if (encodingCharset.Kind == CodePageKind.Dbcs) { calculateMethodAndChunkSize = MimeCommon.calculateMethodAndChunkSizeDbcs; if (encodingCharset.AsciiSupport >= CodePageAsciiSupport.Fine) { allowQEncoding = true; } } else if (encodingCharset.CodePage == 65001) { calculateMethodAndChunkSize = MimeCommon.calculateMethodAndChunkSizeUtf8; allowQEncoding = true; } else if (encodingCharset.CodePage == 1200 || encodingCharset.CodePage == 1201) { calculateMethodAndChunkSize = MimeCommon.calculateMethodAndChunkSizeUnicode16; } else if (encodingCharset.CodePage == 12000 || encodingCharset.CodePage == 12001) { calculateMethodAndChunkSize = MimeCommon.calculateMethodAndChunkSizeUnicode32; } else { calculateMethodAndChunkSize = MimeCommon.calculateMethodAndChunkSizeMbcs; } int num = 75; int num2 = 7 + encodingCharset.Name.Length; int num3 = num - num2; if (num3 < 32) { num = num2 + 32; num3 = 32; } byte[] byteBuffer = ScratchPad.GetByteBuffer(num3); Encoding encoding = encodingCharset.GetEncoding(); byte[] array = new byte[5 + encodingCharset.Name.Length]; int num4 = 0; array[num4++] = 61; array[num4++] = 63; num4 += ByteString.StringToBytes(encodingCharset.Name, array, num4, false); array[num4++] = 63; array[num4++] = 88; array[num4++] = 63; MimeString mimeString = new MimeString(array); int num5 = 0; byte[] array2 = null; int num6 = 0; int num7 = num3 / 4; while (num5 != value.Length) { byte b; int num8; calculateMethodAndChunkSize(allowQEncoding, charClasses, encoding, value, num5, num3, out b, out num8); int bytes; int num10; for (;;) { for (;;) { try { bytes = encoding.GetBytes(value, num5, num8, byteBuffer, 0); } catch (ArgumentException) { if (num8 < 2) { throw; } num8 -= ((num8 > 10) ? 3 : 1); if (MimeCommon.IsLowSurrogate(value[num5 + num8]) && MimeCommon.IsHighSurrogate(value[num5 + num8 - 1])) { num8--; } break; } if (bytes == 0) { goto IL_424; } if (array2 == null || array2.Length - num6 < num + 1) { if (array2 != null) { result.Append(new MimeString(array2, 0, num6)); num6 = 0; } int val = ((value.Length - num5) / num8 + 1) * (num + 1); int num9 = Math.Min(val, 4096 / (num + 1) * (num + 1)); array2 = new byte[num9]; } num10 = num6; if (result.Count > 0 || num10 > 0) { array2[num10++] = 32; } num10 += mimeString.CopyTo(array2, num10); array2[num10 - 2] = b; if (b != 81) { goto IL_3F5; } int num11 = num10; int num12 = 0; while (num12 < bytes && num10 - num11 + 1 <= num3) { byte b2 = byteBuffer[num12]; if (MimeCommon.QEncodingRequired((char)b2, charClasses)) { if (num10 - num11 + 3 > num3) { break; } array2[num10++] = 61; array2[num10++] = ByteEncoder.NibbleToHex[b2 >> 4]; array2[num10++] = ByteEncoder.NibbleToHex[(int)(b2 & 15)]; } else { if (b2 == 32) { b2 = 95; } array2[num10++] = b2; } num12++; } if (num12 == bytes) { goto IL_408; } if (num8 < 2) { goto Block_26; } num8 -= ((num8 > 10) ? 3 : 1); if (MimeCommon.IsLowSurrogate(value[num5 + num8]) && MimeCommon.IsHighSurrogate(value[num5 + num8 - 1])) { num8--; } } } IL_424: num5 += num8; continue; Block_26: throw new InvalidOperationException("unexpected thing just happened"); IL_408: array2[num10++] = 63; array2[num10++] = 61; num6 = num10; goto IL_424; IL_3F5: num10 += MimeCommon.Base64EncodeChunk(byteBuffer, 0, bytes, array2, num10); goto IL_408; } if (array2 != null) { result.Append(new MimeString(array2, 0, num6)); } return(result); }