internal static HeaderId GetHeaderId(string name, bool validateArgument) { if (name == null) { if (validateArgument) { throw new ArgumentNullException("name"); } return(HeaderId.Unknown); } else { if (name.Length != 0) { if (validateArgument) { for (int i = 0; i < name.Length; i++) { if (name[i] >= '\u0080' || !MimeScan.IsField((byte)name[i])) { throw new ArgumentException(Strings.InvalidHeaderName(name, i), "name"); } } } HeaderNameIndex headerNameIndex = Header.LookupName(name); return(MimeData.headerNames[(int)headerNameIndex].publicHeaderId); } if (validateArgument) { throw new ArgumentException("Header name cannot be an empty string", "name"); } return(HeaderId.Unknown); } }
private void ParseEscapedString(bool save, ref MimeStringList outStr, out bool singleByte) { bool flag = this.bytes[this.position] == 27; if (save) { outStr.AppendFragment(new MimeString(this.bytes, this.position, 1, 536870912U)); } this.position++; if (flag && !this.ParseEscapeSequence(save, ref outStr)) { singleByte = true; return; } singleByte = false; do { int num = MimeScan.ScanJISString(this.bytes, this.position, this.end - this.position, ref singleByte); if (save && num != 0) { outStr.AppendFragment(new MimeString(this.bytes, this.position, num, 536870912U)); } this.position += num; }while (!singleByte && this.ParseNextLine()); if (!flag && this.position != this.end && this.bytes[this.position] == 15) { if (save) { outStr.AppendFragment(new MimeString(this.bytes, this.position, 1, 536870912U)); } this.position++; } }
public MimeString ParseToken(MimeScan.Token token) { MimeStringList mimeStringList = default(MimeStringList); while (this.position != this.end || this.ParseNextLine()) { int num = 0; int num2 = MimeScan.FindEndOf(token, this.bytes, this.position, this.end - this.position, out num, this.allowUTF8); if (num2 == 0) { break; } mimeStringList.AppendFragment(new MimeString(this.bytes, this.position, num2)); this.position += num2; } if (mimeStringList.Count == 0) { return(default(MimeString)); } if (mimeStringList.Count == 1) { return(mimeStringList[0]); } byte[] sz = mimeStringList.GetSz(); return(new MimeString(sz, 0, sz.Length)); }
internal static long WriteLines(MimeStringList lines, Stream stream) { if (lines.Count == 0) { MimeStringLength mimeStringLength = new MimeStringLength(0); return(Header.WriteLineEnd(stream, ref mimeStringLength)); } long num = 0L; for (int i = 0; i < lines.Count; i++) { int num2; int num3; byte[] data = lines[i].GetData(out num2, out num3); if (num3 != 0) { if (!MimeScan.IsLWSP(data[num2])) { stream.Write(MimeString.Space, 0, MimeString.Space.Length); num += (long)MimeString.Space.Length; } stream.Write(data, num2, num3); num += (long)num3; } MimeStringLength mimeStringLength2 = new MimeStringLength(0); num += Header.WriteLineEnd(stream, ref mimeStringLength2); } return(num); }
public void ParseSkipToNextDelimiterByte(byte delimiter) { MimeStringList mimeStringList = default(MimeStringList); for (;;) { if (this.position != this.end) { byte b = this.bytes[this.position]; if (b == delimiter) { break; } if (b == 34) { this.ParseQString(false, ref mimeStringList, true); } else if (b == 40) { this.ParseComment(false, false, ref mimeStringList, true); } else { this.position++; this.ParseCFWS(false, ref mimeStringList, true); int num = 0; this.position += MimeScan.FindEndOf(MimeScan.Token.Atom, this.bytes, this.position, this.end - this.position, out num, this.allowUTF8); } } else if (!this.ParseNextLine()) { return; } } }
// Token: 0x0600032A RID: 810 RVA: 0x000120A0 File Offset: 0x000102A0 private bool FindBoundary(byte[] data, int line, int nextNL, out int nextBoundaryLevel, out bool nextBoundaryEnd) { while (nextNL > line && MimeScan.IsLWSP(data[nextNL - 1])) { nextNL--; } uint num = ByteString.ComputeCrc(data, line, nextNL - line); bool flag; if (this.currentLevel.IsBoundary(data, line, nextNL - line, (long)((ulong)num), out flag)) { nextBoundaryLevel = this.parseStackTop; nextBoundaryEnd = flag; return(true); } for (int i = this.parseStackTop - 1; i >= 0; i--) { if (this.parseStack[i].IsBoundary(data, line, nextNL - line, (long)((ulong)num), out flag)) { nextBoundaryLevel = i; nextBoundaryEnd = flag; return(true); } } nextBoundaryLevel = -1; nextBoundaryEnd = false; return(false); }
internal bool IsHeaderLineTooLong(long nameLength, out bool merge) { int num = 0; bool result = false; merge = false; for (int i = 0; i < this.lines.Count; i++) { int num2; int num3; byte[] data = this.lines[i].GetData(out num2, out num3); bool flag = MimeScan.IsLWSP(data[num2]); if (num != 0 && !flag) { result = true; merge = true; } num += num3; if (i == 0 && (long)num3 + nameLength + 1L > 998L) { result = true; } } return(result); }
// Token: 0x06000467 RID: 1127 RVA: 0x00019318 File Offset: 0x00017518 public static int FindEndOf(MimeScan.Token token, byte[] bytes, int start, int length, out int characterCount, bool allowUTF8) { int num = start - 1; int num2 = start + length; characterCount = 0; while (++num < num2) { if ((short)(MimeScan.Dictionary[(int)bytes[num]] & token) != 0) { characterCount++; } else { if (!allowUTF8 || bytes[num] < 128) { break; } int num3 = 0; if (!MimeScan.IsUTF8NonASCII(bytes, num, num2, out num3) || (short)((MimeScan.Token.Token | MimeScan.Token.Atom) & token) == 0) { break; } num += num3 - 1; characterCount++; } } return(num - start); }
// Token: 0x06000469 RID: 1129 RVA: 0x000193E4 File Offset: 0x000175E4 public static int FindNextOf(MimeScan.Token token, byte[] bytes, int start, int length, out int characterCount, bool allowUTF8) { int num = start - 1; int num2 = start + length; characterCount = 0; while (++num < num2 && (short)(MimeScan.Dictionary[(int)bytes[num]] & token) == 0) { if (allowUTF8 && bytes[num] >= 128) { int num3 = 0; if (MimeScan.IsUTF8NonASCII(bytes, num, num2, out num3)) { if ((short)((MimeScan.Token.Token | MimeScan.Token.Atom) & token) == 0) { num += num3 - 1; characterCount++; continue; } break; } } characterCount++; } return(num - start); }
private void ParseRawFragment(ref bool whitespaceOnly) { while (!this.iterator.Eof && 61 != this.iterator.Pick()) { int num = MimeScan.SkipLwsp(this.iterator.Bytes, this.iterator.Offset, this.iterator.Length); if (num != 0) { this.iterator.Get(num); } if (this.iterator.Eof) { break; } if (61 == this.iterator.Pick()) { return; } num = MimeScan.SkipToLwspOrEquals(this.iterator.Bytes, this.iterator.Offset, this.iterator.Length); if (num != 0) { whitespaceOnly = false; this.iterator.Get(num); } } }
public void ParseParameterValue(ref MimeStringList value, ref bool goodValue, bool handleISO2022) { MimeStringList mimeStringList = default(MimeStringList); goodValue = true; while (this.position != this.end || this.ParseNextLine()) { byte b = this.bytes[this.position]; if (b == 34) { value.Reset(); mimeStringList.Reset(); this.ParseQString(true, ref value, handleISO2022); return; } if (b == 40 || MimeScan.IsLWSP(b)) { this.ParseCFWS(true, ref mimeStringList, handleISO2022); } else { if (b == 59) { return; } int num = this.position; do { int num2 = 1; if (!MimeScan.IsToken(b)) { if (this.allowUTF8 && b >= 128) { if (!MimeScan.IsUTF8NonASCII(this.bytes, this.position, this.end, out num2)) { num2 = 1; goodValue = false; } } else { goodValue = false; } } this.position += num2; if (this.position == this.end) { break; } b = this.bytes[this.position]; }while (b != 59 && b != 40 && !MimeScan.IsLWSP(b)); value.TakeOverAppend(ref mimeStringList); value.AppendFragment(new MimeString(this.bytes, num, this.position - num)); } } }
public void ParseQString(bool save, ref MimeStringList phrase, bool handleISO2022) { bool flag = false; if (save) { phrase.AppendFragment(new MimeString(this.bytes, this.position, 1, 268435456U)); } this.position++; bool flag2 = true; for (;;) { int num = MimeScan.ScanQuotedString(this.bytes, this.position, this.end - this.position, handleISO2022, ref flag); if (num != 0) { if (save) { phrase.AppendFragment(new MimeString(this.bytes, this.position, num)); } this.position += num; } if (this.position != this.end) { if (this.bytes[this.position] == 14 || this.bytes[this.position] == 27) { this.ParseEscapedString(save, ref phrase, out flag2); } else { if (save) { phrase.AppendFragment(new MimeString(this.bytes, this.position, 1, 268435456U)); } this.position++; if (this.bytes[this.position - 1] == 34) { break; } flag = true; } } else if (!this.ParseNextLine()) { goto Block_8; } } return; Block_8: if (save && flag2) { phrase.AppendFragment(new MimeString(MimeString.DoubleQuote, 0, MimeString.DoubleQuote.Length, 268435456U)); } }
// Token: 0x0600030A RID: 778 RVA: 0x000111C4 File Offset: 0x0000F3C4 private int EncodeRfc2231Segment(byte[] source, int sourceIndex, int maxValueLength, EncodingOptions encodingOptions) { byte[] array = new byte[maxValueLength * 4]; int count = 0; int num = 0; while (sourceIndex < source.Length) { int i = 1; byte b = source[sourceIndex]; bool flag = true; if (b >= 128) { if (encodingOptions.AllowUTF8 && MimeScan.IsUTF8NonASCII(source, sourceIndex, source.Length, out i)) { flag = false; } } else if (!MimeScan.IsSegmentEncodingRequired(b)) { flag = false; } if (flag) { if (num + 3 > maxValueLength) { break; } array[count++] = 37; array[count++] = MimeParameter.OctetEncoderMap[b >> 4]; array[count++] = MimeParameter.OctetEncoderMap[(int)(b & 15)]; num += 3; this.valueEncoded = true; sourceIndex++; } else { if (num + 1 > maxValueLength) { break; } while (i > 0) { array[count++] = source[sourceIndex++]; i--; } num++; } } this.valueFragments.AppendFragment(new MimeString(array, 0, count)); return(sourceIndex); }
public static bool IsHeaderNameValid(string name) { if (string.IsNullOrEmpty(name)) { return(false); } for (int i = 0; i < name.Length; i++) { if (name[i] >= '\u0080' || !MimeScan.IsField((byte)name[i])) { return(false); } } return(true); }
// Token: 0x0600019F RID: 415 RVA: 0x00007578 File Offset: 0x00005778 private bool IsQuotingRequired(MimeString mimeStr, bool allowUTF8) { AddressItem.WriteState writeState = AddressItem.WriteState.Begin; MimeString mimeString = new MimeString(AddressItem.WordBreakBytes, 0, AddressItem.WordBreakBytes.Length); int num; int num2; byte[] data = mimeStr.GetData(out num, out num2); while (num2 != 0) { switch (writeState) { case AddressItem.WriteState.Begin: { int num3 = 0; int num4 = MimeScan.FindEndOf(MimeScan.Token.Atom, data, num, num2, out num3, allowUTF8); if (num4 == 0) { if (num2 <= 3 || num != 0 || !mimeString.HasPrefixEq(data, 0, 3)) { return(true); } num += 3; num2 -= 3; writeState = AddressItem.WriteState.Begin; } else { num += num4; num2 -= num4; writeState = AddressItem.WriteState.Atom; } break; } case AddressItem.WriteState.Atom: if ((num2 < 2 || data[num] != 32) && (num2 < 1 || data[num] != 46)) { return(true); } num++; num2--; writeState = AddressItem.WriteState.Begin; break; } } return(false); }
// Token: 0x060001A1 RID: 417 RVA: 0x00007684 File Offset: 0x00005884 internal string QuoteString(string inputString) { StringBuilder stringBuilder = new StringBuilder(inputString.Length + 2); stringBuilder.Append("\""); foreach (char c in inputString) { if (c < '\u0080' && MimeScan.IsEscapingRequired((byte)c)) { stringBuilder.Append("\\"); } stringBuilder.Append(c); } stringBuilder.Append("\""); return(stringBuilder.ToString()); }
// Token: 0x06000251 RID: 593 RVA: 0x0000AEB8 File Offset: 0x000090B8 internal static bool IsEncodingRequired(string value, bool allowUTF8) { if (string.IsNullOrEmpty(value)) { return(false); } char[] array = new char[1]; int num = 0; foreach (char c in value) { if (c < '\u0080') { if (MimeScan.IsEncodingRequired((byte)c)) { return(true); } if (MimeScan.IsLWSP((byte)c)) { num = 0; } else { num++; } } else { if (!allowUTF8) { return(true); } array[0] = c; num += ByteString.StringToBytesCount(new string(array), allowUTF8); } if (998 < num + 1) { return(true); } } return(false); }
public void ParseWhitespace(bool save, ref MimeStringList phrase) { for (;;) { int num = MimeScan.SkipLwsp(this.bytes, this.position, this.end - this.position); if (save && num != 0) { phrase.AppendFragment(new MimeString(this.bytes, this.position, num)); } this.position += num; if (this.position != this.end) { break; } if (!this.ParseNextLine()) { return; } } }
public void ParseComment(bool save, bool saveInnerOnly, ref MimeStringList comment, bool handleISO2022) { int num = 1; bool flag = false; int num2 = 0; if (save && !saveInnerOnly) { comment.AppendFragment(new MimeString(this.bytes, this.position, 1)); } this.position++; for (;;) { int num3 = MimeScan.ScanComment(this.bytes, this.position, this.end - this.position, handleISO2022, ref num, ref flag); if (num3 != 0) { if (save) { if (num == 0 && saveInnerOnly) { num2 = 1; } comment.AppendFragment(new MimeString(this.bytes, this.position, num3 - num2)); } this.position += num3; if (num == 0) { break; } } if (this.position != this.end && (this.bytes[this.position] == 14 || this.bytes[this.position] == 27)) { bool flag2; this.ParseEscapedString(save, ref comment, out flag2); } else if (!this.ParseNextLine()) { return; } } }
public void ParseCFWS(bool save, ref MimeStringList phrase, bool handleISO2022) { for (;;) { int num = MimeScan.SkipLwsp(this.bytes, this.position, this.end - this.position); if (save && num != 0) { phrase.AppendFragment(new MimeString(this.bytes, this.position, num)); } this.position += num; if (this.position != this.end) { if (this.bytes[this.position] != 40) { break; } this.ParseComment(save, false, ref phrase, handleISO2022); } else if (!this.ParseNextLine()) { break; } } }
public bool ParseToDelimiter(bool ignoreNextByte, bool separateWithWhitespace, ref MimeStringList phrase) { bool result = false; int num = ignoreNextByte ? 1 : 0; for (;;) { int num2 = 0; num += MimeScan.FindEndOf(MimeScan.Token.Atom, this.bytes, this.position + num, this.end - this.position - num, out num2, this.allowUTF8); if (num != 0) { result = true; if (phrase.Length != 0 && separateWithWhitespace) { if (this.position == this.start || this.bytes[this.position - 1] != 32) { phrase.AppendFragment(ValueParser.SpaceLine); } else { this.position--; num++; } } separateWithWhitespace = false; phrase.AppendFragment(new MimeString(this.bytes, this.position, num)); this.position += num; } if (this.position != this.end || !this.ParseNextLine()) { break; } num = 0; } return(result); }
public static int ParseToken(MimeString str, out int characterCount, bool allowUTF8) { return(MimeScan.FindEndOf(MimeScan.Token.Token, str.Data, str.Offset, str.Length, out characterCount, allowUTF8)); }
private bool ParseEncodedWord(string lastEncodedWordCharsetName, string lastEncodedWordLanguage, ref byte[] byteBuffer, out string encodedWordCharsetName, out string encodedWordLanguage, out byte bOrQ, out ValuePosition encodedWordContentStart, out ValuePosition encodedWordContentEnd) { encodedWordCharsetName = null; encodedWordLanguage = null; bOrQ = 0; ValuePosition valuePosition = default(ValuePosition); encodedWordContentEnd = valuePosition; encodedWordContentStart = (encodedWordContentEnd = valuePosition); int num = this.iterator.Get(); if (this.iterator.Get() != 63) { return(false); } if (byteBuffer == null) { byteBuffer = ScratchPad.GetByteBuffer(Math.Max(this.maxCharsetNameLength + 1, Math.Min(1024, this.iterator.TotalLength))); } int num2 = -1; int i; for (i = 0; i < this.maxCharsetNameLength + 1; i++) { num = this.iterator.Get(); if (!ValueDecoder.Is2047Token((byte)num)) { break; } byteBuffer[i] = (byte)num; if (num2 == -1 && num == 42) { num2 = i; } } if (i == this.maxCharsetNameLength + 1 || num != 63 || i == 0 || num2 == 0) { return(false); } num = this.iterator.Get(); bOrQ = ((num == 66 || num == 98) ? 66 : ((num == 81 || num == 113) ? 81 : 0)); if (bOrQ == 0 || this.iterator.Get() != 63) { return(false); } if (num2 != -1) { int num3 = num2 + 1; int num4 = i - (num2 + 1); i = num2; if (num4 != 0) { if (lastEncodedWordLanguage != null && num4 == lastEncodedWordLanguage.Length) { int num5 = 0; while (num5 < num4 && lastEncodedWordLanguage[num5] == (char)byteBuffer[num3 + num5]) { num5++; } if (num5 != num4) { encodedWordLanguage = ByteString.BytesToString(byteBuffer, num3, num4, false); } else { encodedWordLanguage = lastEncodedWordLanguage; } } else { encodedWordLanguage = ByteString.BytesToString(byteBuffer, num3, num4, false); } } } if (lastEncodedWordCharsetName != null && i == lastEncodedWordCharsetName.Length) { int num6 = 0; while (num6 < i && lastEncodedWordCharsetName[num6] == (char)byteBuffer[num6]) { num6++; } if (num6 != i) { encodedWordCharsetName = ByteString.BytesToString(byteBuffer, 0, i, false); } else { encodedWordCharsetName = lastEncodedWordCharsetName; } } else { encodedWordCharsetName = ByteString.BytesToString(byteBuffer, 0, i, false); } encodedWordContentStart = this.iterator.CurrentPosition; bool flag = false; for (;;) { encodedWordContentEnd = this.iterator.CurrentPosition; num = this.iterator.Get(); if (num == -1) { break; } if (MimeScan.IsLWSP((byte)num)) { flag = true; } else { if (num == 63) { num = this.iterator.Get(); if (num == -1) { return(false); } if (num == 61) { return(true); } this.iterator.Unget(); if (bOrQ != 81) { return(false); } } else if (num == 61 && flag) { num = this.iterator.Get(); if (num == -1) { return(false); } if (num == 63) { goto Block_33; } this.iterator.Unget(); } flag = false; } } return(false); Block_33: this.iterator.Unget(); this.iterator.Unget(); return(false); }
// Token: 0x0600046A RID: 1130 RVA: 0x00019454 File Offset: 0x00017654 public static int SkipLwsp(byte[] bytes, int offset, int length) { int num = 0; return(MimeScan.FindEndOf(MimeScan.Token.Lwsp, bytes, offset, length, out num, false)); }
// Token: 0x06000245 RID: 581 RVA: 0x00009F60 File Offset: 0x00008160 public static bool IsValidSmtpAddress(string address, bool checkLength, out int domainStart, bool allowUTF8 = false) { if (string.IsNullOrEmpty(address)) { domainStart = -1; return(false); } if (checkLength && address.Length > 571 && (address.Length > 1860 || !MimeAddressParser.IsEncapsulatedX400Address(address))) { domainStart = -1; return(false); } int num = 0; MimeAddressParser.ValidationStage validationStage = MimeAddressParser.ValidationStage.BEGIN; while (num < address.Length && validationStage != MimeAddressParser.ValidationStage.ERROR) { char c = address[num]; num++; switch (validationStage) { case MimeAddressParser.ValidationStage.BEGIN: if ((c < '\u0080' && MimeScan.IsAtom((byte)c)) || (c >= '\u0080' && allowUTF8)) { validationStage = MimeAddressParser.ValidationStage.LOCAL; continue; } if (c == '\\') { validationStage = MimeAddressParser.ValidationStage.LOCAL_ESC; continue; } if (c == '"') { validationStage = MimeAddressParser.ValidationStage.LOCAL_DQS; continue; } goto IL_1B6; case MimeAddressParser.ValidationStage.LOCAL: if (c == '@') { goto IL_182; } if (c == '.') { validationStage = MimeAddressParser.ValidationStage.LOCAL_DOT; continue; } break; case MimeAddressParser.ValidationStage.LOCAL_DOT: break; case MimeAddressParser.ValidationStage.LOCAL_DQS: if (c == '"') { validationStage = MimeAddressParser.ValidationStage.LOCAL_DQS_END; continue; } if (c == '\\') { validationStage = MimeAddressParser.ValidationStage.LOCAL_DQS_ESC; continue; } if ((c < '\u0080' && '\r' != c && '\n' != c && '\\' != c && '"' != c) || (c >= '\u0080' && allowUTF8)) { validationStage = MimeAddressParser.ValidationStage.LOCAL_DQS; continue; } goto IL_1B6; case MimeAddressParser.ValidationStage.LOCAL_ESC: if (c < '\u0080' || (c >= '\u0080' && allowUTF8)) { validationStage = MimeAddressParser.ValidationStage.LOCAL; continue; } goto IL_1B6; case MimeAddressParser.ValidationStage.LOCAL_DQS_ESC: if (c < '\u0080' || (c >= '\u0080' && allowUTF8)) { validationStage = MimeAddressParser.ValidationStage.LOCAL_DQS; continue; } goto IL_1B6; case MimeAddressParser.ValidationStage.LOCAL_DQS_END: if (c == '@') { goto IL_182; } goto IL_1B6; case MimeAddressParser.ValidationStage.DOMAIN: goto IL_182; default: goto IL_1B6; } if ((c < '\u0080' && MimeScan.IsAtom((byte)c)) || (c >= '\u0080' && allowUTF8)) { validationStage = MimeAddressParser.ValidationStage.LOCAL; continue; } if (c == '\\') { validationStage = MimeAddressParser.ValidationStage.LOCAL_ESC; continue; } IL_1B6: validationStage = MimeAddressParser.ValidationStage.ERROR; continue; IL_182: if (checkLength && num - 1 > 315 && (num - 1 > 1604 || !MimeAddressParser.IsEncapsulatedX400Address(address))) { domainStart = -1; return(false); } if (MimeAddressParser.IsValidDomain(address, num, checkLength, allowUTF8)) { domainStart = num; return(true); } goto IL_1B6; } domainStart = -1; return(false); }
// Token: 0x06000324 RID: 804 RVA: 0x0001178C File Offset: 0x0000F98C public MimeToken Parse(byte[] data, int start, int end, bool flush) { int num = start + this.currentOffset; int num2 = start + this.lineOffset; switch (this.state) { case MimeParser.ParseState.Headers: { bool flag = false; int num3 = ByteString.IndexOf(data, 10, num, end - num); if (num3 == -1) { num3 = end; } if (num3 == end) { if ((end - start <= 998 && !flush) || (!flush && end - start <= 999 && data[end - 1] == 13)) { this.currentOffset = end - start; return(new MimeToken(MimeTokenId.None, 0)); } } else if (num3 == start || data[num3 - 1] != 13) { this.compliance |= MimeComplianceStatus.BareLinefeedInHeader; flag = true; } else { num3--; } this.headerNameLength = 0; this.headerDataOffset = 0; int num4; if (num3 - start > 998) { this.compliance |= MimeComplianceStatus.InvalidWrapping; this.currentOffset = num3 - (start + 998); this.lineOffset = num2 - (start + 998); num3 = start + 998; num4 = 0; } else { this.currentOffset = 0; this.lineOffset = ((num3 == end) ? (num2 - num3) : 0); num4 = ((num3 == end) ? 0 : (flag ? 1 : 2)); } if (num3 == start) { this.state = MimeParser.ParseState.EndOfHeaders; this.lastTokenLength = num4; return(new MimeToken(MimeTokenId.EndOfHeaders, this.lastTokenLength)); } if (num4 != 0 && num3 + num4 < end && data[num3 + num4] != 32 && data[num3 + num4] != 9) { this.headerComplete = true; } else { this.headerComplete = false; } if (!this.firstHeader && (num2 < start || data[num2] == 32 || data[num2] == 9)) { this.lastTokenLength = num3 + num4 - start; return(new MimeToken(MimeTokenId.HeaderContinuation, this.lastTokenLength)); } this.firstHeader = false; int num5 = 0; this.headerNameLength = MimeScan.FindEndOf(MimeScan.Token.Field, data, start, num3 - start, out num5, false); if (this.headerNameLength == 0) { this.compliance |= MimeComplianceStatus.InvalidHeader; this.lastTokenLength = num3 + num4 - start; return(new MimeToken(MimeTokenId.Header, this.lastTokenLength)); } int num6 = start + this.headerNameLength; if (num6 == num3 || data[num6] != 58) { num6 += MimeScan.SkipLwsp(data, num6, num3 - num6); if (num6 == num3 || data[num6] != 58) { this.headerNameLength = 0; if (this.mime && (this.parseStackTop > 0 || this.currentLevel.ContentType == MajorContentType.Multipart) && num3 - num2 > 2 && data[num2] == 45 && data[num2 + 1] == 45 && this.FindBoundary(data, num2, num3, out this.nextBoundaryLevel, out this.nextBoundaryEnd)) { this.compliance |= MimeComplianceStatus.MissingBodySeparator; if (this.nextBoundaryLevel != this.parseStackTop) { this.compliance |= MimeComplianceStatus.MissingBoundary; } this.lineOffset = 0; this.currentOffset = num3 - start; this.state = MimeParser.ParseState.EndOfHeaders; return(new MimeToken(MimeTokenId.EndOfHeaders, 0)); } this.compliance |= MimeComplianceStatus.InvalidHeader; this.lastTokenLength = num3 + num4 - start; return(new MimeToken(MimeTokenId.Header, this.lastTokenLength)); } } this.headerDataOffset = num6 + 1 - start; this.lastTokenLength = num3 + num4 - start; return(new MimeToken(MimeTokenId.Header, this.lastTokenLength)); } case MimeParser.ParseState.EndOfHeaders: this.CheckMimeConstraints(); if (this.mime && this.parseEmbeddedMessages && this.currentLevel.ContentType == MajorContentType.MessageRfc822 && !this.currentLevel.StreamMode) { this.PushLevel(false); this.lastTokenLength = 0; return(new MimeToken(MimeTokenId.EmbeddedStart, 0)); } this.state = MimeParser.ParseState.Body; break; case MimeParser.ParseState.Body: break; default: return(new MimeToken(MimeTokenId.EndOfFile, 0)); } return(this.ParseBody(data, start, end, flush, num2, num)); }
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); }
private static long WriteToken(byte[] token, int tokenOffset, MimeStringLength tokenLength, Stream stream, ref MimeStringLength currentLineLength, ref Header.LineBuffer lineBuffer, ref bool autoAddedLastLWSP, bool allowUTF8) { long num = 0L; bool flag = token != null && tokenLength.InChars == 1 && MimeScan.IsFWSP(token[tokenOffset]); if (!flag && 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); num += (long)lineBuffer.LengthTillLastLWSP.InBytes; currentLineLength.IncrementBy(lineBuffer.LengthTillLastLWSP); } if (currentLineLength.InBytes > 0) { num += Header.WriteLineEnd(stream, ref currentLineLength); } if (autoAddedLastLWSP) { autoAddedLastLWSP = false; lineBuffer.LengthTillLastLWSP.IncrementBy(1); } if (lineBuffer.LengthTillLastLWSP.InBytes != lineBuffer.Length.InBytes) { 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); } bool flag2 = false; if (lineBuffer.Length.InBytes > 0) { if (!MimeScan.IsFWSP(lineBuffer.Bytes[0])) { flag2 = true; } } else if (!flag) { flag2 = true; } if (flag2) { stream.Write(Header.LineStartWhitespace, 0, Header.LineStartWhitespace.Length); num += (long)Header.LineStartWhitespace.Length; currentLineLength.IncrementBy(Header.LineStartWhitespace.Length); } } if (currentLineLength.InBytes + lineBuffer.Length.InBytes + tokenLength.InBytes > 998) { if (lineBuffer.Length.InBytes > 0) { stream.Write(lineBuffer.Bytes, 0, lineBuffer.Length.InBytes); num += (long)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) { int num2 = Math.Max(0, 998 - currentLineLength.InBytes); int num3 = 0; int num4 = 0; if (allowUTF8) { int num5; for (int i = 0; i < tokenLength.InBytes; i += num5) { byte b = token[tokenOffset + i]; num5 = 1; if (b >= 128 && !MimeScan.IsUTF8NonASCII(token, tokenOffset + i, tokenOffset + tokenLength.InBytes, out num5)) { num5 = 1; } if (num4 + num5 > num2) { break; } num3++; num4 += num5; } } else { num3 = num2; num4 = num2; } stream.Write(token, tokenOffset, num4); num += (long)num4; currentLineLength.IncrementBy(num3, num4); tokenOffset += num4; tokenLength.DecrementBy(num3, num4); num += Header.WriteLineEnd(stream, ref currentLineLength); if (!flag) { stream.Write(Header.LineStartWhitespace, 0, Header.LineStartWhitespace.Length); num += (long)Header.LineStartWhitespace.Length; currentLineLength.IncrementBy(Header.LineStartWhitespace.Length); } } } } if (token != null) { Buffer.BlockCopy(token, tokenOffset, lineBuffer.Bytes, lineBuffer.Length.InBytes, tokenLength.InBytes); if (flag && (lineBuffer.Length.InBytes == 0 || !MimeScan.IsFWSP(lineBuffer.Bytes[lineBuffer.Length.InBytes - 1]))) { autoAddedLastLWSP = false; lineBuffer.LengthTillLastLWSP.SetAs(lineBuffer.Length); } lineBuffer.Length.IncrementBy(tokenLength); } return(num); }
public static int ParseToken(string value, int currentOffset, bool allowUTF8) { return(MimeScan.FindEndOf(MimeScan.Token.Token, value, currentOffset, allowUTF8)); }
private static void ParseValue(MimeStringList list, out DateTime utcDateTime, out TimeSpan timeZoneOffset) { MimeStringList mimeStringList = default(MimeStringList); ValueParser valueParser = new ValueParser(list, false); DateHeader.ParseStage parseStage = DateHeader.ParseStage.DayOfWeek; int[] array = new int[8]; byte b = 32; while (parseStage != DateHeader.ParseStage.Count) { valueParser.ParseCFWS(false, ref mimeStringList, true); byte b2 = valueParser.ParseGet(); if (b2 == 0) { break; } if (!MimeScan.IsToken(b2) || b2 == 45 || b2 == 43) { b = b2; valueParser.ParseCFWS(false, ref mimeStringList, true); } else { if (MimeScan.IsDigit(b2)) { if (parseStage == DateHeader.ParseStage.DayOfWeek) { parseStage = DateHeader.ParseStage.DayOfMonth; } if (parseStage == DateHeader.ParseStage.Second && b != 58) { parseStage = DateHeader.ParseStage.Zone; } int num = 0; do { num++; array[(int)parseStage] *= 10; array[(int)parseStage] += (int)(b2 - 48); b2 = valueParser.ParseGet(); }while (b2 != 0 && MimeScan.IsDigit(b2)); if (b2 != 0) { valueParser.ParseUnget(); } if (parseStage == DateHeader.ParseStage.Year && num <= 2) { array[(int)parseStage] += ((array[(int)parseStage] < 30) ? 2000 : 1900); } if (parseStage == DateHeader.ParseStage.Zone && num <= 2) { array[(int)parseStage] *= 100; } if (parseStage == DateHeader.ParseStage.Zone && b == 45) { array[(int)parseStage] = -array[(int)parseStage]; } parseStage++; } else if (MimeScan.IsAlpha(b2)) { valueParser.ParseUnget(); MimeString mimeString = valueParser.ParseToken(MimeScan.Token.Alpha); if (parseStage == DateHeader.ParseStage.DayOfWeek) { parseStage = DateHeader.ParseStage.DayOfMonth; } else if (parseStage == DateHeader.ParseStage.Month) { array[(int)parseStage] = DateHeader.MapMonthToInt(mimeString.Data, mimeString.Offset, mimeString.Length); parseStage = DateHeader.ParseStage.Year; } else if (parseStage >= DateHeader.ParseStage.Second) { if (mimeString.Length == 2 && 65 == DateHeader.MakeUpper(mimeString[0]) && 77 == DateHeader.MakeUpper(mimeString[1])) { if (array[4] == 12) { array[4] = 0; } parseStage = DateHeader.ParseStage.Zone; } else if (mimeString.Length == 2 && 80 == DateHeader.MakeUpper(mimeString[0]) && 77 == DateHeader.MakeUpper(mimeString[1])) { if (array[4] < 12) { array[4] += 12; } parseStage = DateHeader.ParseStage.Zone; } else { array[7] = (int)DateHeader.MapZoneToInt(mimeString.Data, mimeString.Offset, mimeString.Length); parseStage = DateHeader.ParseStage.Count; } } } b = 32; } } if (parseStage > DateHeader.ParseStage.Year) { int num2 = array[3]; int num3 = array[2]; int num4 = array[1]; int num5 = array[4]; int num6 = array[5]; int num7 = array[6]; if (num5 == 23 && num6 == 59 && num7 == 60) { num7 = 59; } if (num2 >= 1900 && num2 <= 9999 && num3 >= 1 && num3 <= 12 && num4 >= 1 && num4 <= DateTime.DaysInMonth(num2, num3) && num5 >= 0 && num5 < 24 && num6 >= 0 && num6 < 60 && num7 >= 0 && num7 < 60) { try { utcDateTime = new DateTime(num2, num3, num4, num5, num6, num7, 0, DateTimeKind.Utc); goto IL_319; } catch (ArgumentException) { utcDateTime = DateHeader.minDateTime; goto IL_319; } } utcDateTime = DateHeader.minDateTime; } else { utcDateTime = DateHeader.minDateTime; } IL_319: if (parseStage != DateHeader.ParseStage.Count || !(utcDateTime > DateHeader.minDateTime)) { timeZoneOffset = TimeSpan.Zero; return; } int num8 = array[7] / 100; int num9 = array[7] % 100; if (num8 > 99 || num8 < -99) { num8 = 0; num9 = 0; } if (num9 > 59 || num9 < -59) { num9 = 0; } timeZoneOffset = new TimeSpan(num8, num9, 0); if (utcDateTime.Ticks >= timeZoneOffset.Ticks && DateTime.MaxValue.Ticks >= utcDateTime.Ticks - timeZoneOffset.Ticks) { utcDateTime -= timeZoneOffset; return; } utcDateTime = DateHeader.minDateTime; timeZoneOffset = TimeSpan.Zero; }