/// <summary> /// Tries to parse the given input buffer into a new <see cref="MimeKit.Header"/> instance. /// </summary> /// <returns><c>true</c>, if the header was successfully parsed, <c>false</c> otherwise.</returns> /// <param name="options">The parser options to use.</param> /// <param name="buffer">The input buffer.</param> /// <param name="startIndex">The starting index of the input buffer.</param> /// <param name="length">The number of bytes in the input buffer to parse.</param> /// <param name="header">The parsed header.</param> /// <exception cref="System.ArgumentNullException"> /// <para><paramref name="options"/> is <c>null</c>.</para> /// <para>-or-</para> /// <para><paramref name="buffer"/> is <c>null</c>.</para> /// </exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="startIndex"/> and <paramref name="length"/> do not specify /// a valid range in the byte array. /// </exception> public static bool TryParse(ParserOptions options, byte[] buffer, int startIndex, int length, out Header header) { if (options == null) { throw new ArgumentNullException("options"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } if (startIndex < 0 || startIndex > buffer.Length) { throw new ArgumentOutOfRangeException("startIndex"); } if (length < 0 || startIndex + length > buffer.Length) { throw new ArgumentOutOfRangeException("length"); } unsafe { fixed(byte *inptr = buffer) { return(TryParse(options.Clone(), inptr + startIndex, length, true, out header)); } } }
/// <summary> /// Gets the header value using the specified charset. /// </summary> /// <remarks> /// <para>If the raw header value does not properly encode non-ASCII text, the decoder /// will fall back to a default charset encoding. Sometimes, however, this /// default charset fallback is wrong and the mail client may wish to override /// that default charset on a per-header basis.</para> /// <para>By using this method, the client is able to override the fallback charset /// on a per-header basis.</para> /// </remarks> /// <returns>The value.</returns> /// <param name="charset">Charset.</param> public string GetValue(Encoding charset) { if (charset == null) { throw new ArgumentNullException("charset"); } var options = Options.Clone(); options.CharsetEncoding = charset; return(Unfold(Rfc2047.DecodeText(options, RawValue))); }
/// <summary> /// Tries to parse the given text into a new <see cref="MimeKit.Header"/> instance. /// </summary> /// <returns><c>true</c>, if the header was successfully parsed, <c>false</c> otherwise.</returns> /// <param name="options">The parser options to use.</param> /// <param name="text">The text to parse.</param> /// <param name="header">The parsed header.</param> /// <exception cref="System.ArgumentNullException"> /// <para><paramref name="options"/> is <c>null</c>.</para> /// <para>-or-</para> /// <para><paramref name="text"/> is <c>null</c>.</para> /// </exception> public static bool TryParse(ParserOptions options, string text, out Header header) { if (options == null) { throw new ArgumentNullException("options"); } if (text == null) { throw new ArgumentNullException("text"); } var buffer = Encoding.UTF8.GetBytes(text); unsafe { fixed(byte *inptr = buffer) { return(TryParse(options.Clone(), inptr, buffer.Length, true, out header)); } } }
/// <summary> /// Tries to parse the given text into a new <see cref="MimeKit.Header"/> instance. /// </summary> /// <remarks> /// Parses a header from the specified text. /// </remarks> /// <returns><c>true</c>, if the header was successfully parsed, <c>false</c> otherwise.</returns> /// <param name="options">The parser options to use.</param> /// <param name="text">The text to parse.</param> /// <param name="header">The parsed header.</param> /// <exception cref="System.ArgumentNullException"> /// <para><paramref name="options"/> is <c>null</c>.</para> /// <para>-or-</para> /// <para><paramref name="text"/> is <c>null</c>.</para> /// </exception> public static bool TryParse (ParserOptions options, string text, out Header header) { if (options == null) throw new ArgumentNullException ("options"); if (text == null) throw new ArgumentNullException ("text"); var buffer = Encoding.UTF8.GetBytes (text); unsafe { fixed (byte *inptr = buffer) { return TryParse (options.Clone (), inptr, buffer.Length, true, out header); } } }
/// <summary> /// Tries to parse the given input buffer into a new <see cref="MimeKit.Header"/> instance. /// </summary> /// <remarks> /// Parses a header from the supplied buffer starting at the given index /// and spanning across the specified number of bytes. /// </remarks> /// <returns><c>true</c>, if the header was successfully parsed, <c>false</c> otherwise.</returns> /// <param name="options">The parser options to use.</param> /// <param name="buffer">The input buffer.</param> /// <param name="startIndex">The starting index of the input buffer.</param> /// <param name="length">The number of bytes in the input buffer to parse.</param> /// <param name="header">The parsed header.</param> /// <exception cref="System.ArgumentNullException"> /// <para><paramref name="options"/> is <c>null</c>.</para> /// <para>-or-</para> /// <para><paramref name="buffer"/> is <c>null</c>.</para> /// </exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// <paramref name="startIndex"/> and <paramref name="length"/> do not specify /// a valid range in the byte array. /// </exception> public static bool TryParse (ParserOptions options, byte[] buffer, int startIndex, int length, out Header header) { if (options == null) throw new ArgumentNullException ("options"); if (buffer == null) throw new ArgumentNullException ("buffer"); if (startIndex < 0 || startIndex > buffer.Length) throw new ArgumentOutOfRangeException ("startIndex"); if (length < 0 || length > (buffer.Length - startIndex)) throw new ArgumentOutOfRangeException ("length"); unsafe { fixed (byte* inptr = buffer) { return TryParse (options.Clone (), inptr + startIndex, length, true, out header); } } }
/// <summary> /// Tries to parse the given text into a new <see cref="MimeKit.Header"/> instance. /// </summary> /// <remarks> /// Parses a header from the specified text. /// </remarks> /// <returns><c>true</c>, if the header was successfully parsed, <c>false</c> otherwise.</returns> /// <param name="options">The parser options to use.</param> /// <param name="text">The text to parse.</param> /// <param name="header">The parsed header.</param> /// <exception cref="System.ArgumentNullException"> /// <para><paramref name="options"/> is <c>null</c>.</para> /// <para>-or-</para> /// <para><paramref name="text"/> is <c>null</c>.</para> /// </exception> public static bool TryParse (ParserOptions options, string text, out Header header) { ParseUtils.ValidateArguments (options, text); var buffer = Encoding.UTF8.GetBytes (text); unsafe { fixed (byte *inptr = buffer) { return TryParse (options.Clone (), inptr, buffer.Length, true, out header); } } }
/// <summary> /// Tries to parse the given input buffer into a new <see cref="MimeKit.Header"/> instance. /// </summary> /// <remarks> /// Parses a header from the specified buffer. /// </remarks> /// <returns><c>true</c>, if the header was successfully parsed, <c>false</c> otherwise.</returns> /// <param name="options">The parser options to use.</param> /// <param name="buffer">The input buffer.</param> /// <param name="header">The parsed header.</param> /// <exception cref="System.ArgumentNullException"> /// <para><paramref name="options"/> is <c>null</c>.</para> /// <para>-or-</para> /// <para><paramref name="buffer"/> is <c>null</c>.</para> /// </exception> public static bool TryParse (ParserOptions options, byte[] buffer, out Header header) { ParseUtils.ValidateArguments (options, buffer); unsafe { fixed (byte* inptr = buffer) { return TryParse (options.Clone (), inptr, buffer.Length, true, out header); } } }
/// <summary> /// Sets the stream to parse. /// </summary> /// <param name="options">The parser options.</param> /// <param name="stream">The stream to parse.</param> /// <param name="format">The format of the stream.</param> /// <param name="persistent"><c>true</c> if the stream is persistent; otherwise <c>false</c>.</param> /// <remarks> /// <para>If <paramref name="persistent"/> is <c>true</c> and <paramref name="stream"/> is seekable, then /// the <see cref="MimeParser"/> will not copy the content of <see cref="MimePart"/>s into memory. Instead, /// it will use a <see cref="MimeKit.IO.BoundStream"/> to reference a substream of <paramref name="stream"/>. /// This has the potential to not only save mmeory usage, but also improve <see cref="MimeParser"/> /// performance.</para> /// <para>It should be noted, however, that disposing <paramref name="stream"/> will make it impossible /// for <see cref="ContentObject"/> to read the content.</para> /// </remarks> /// <exception cref="System.ArgumentNullException"> /// <para><paramref name="options"/> is <c>null</c>.</para> /// <para>-or-</para> /// <para><paramref name="stream"/> is <c>null</c>.</para> /// </exception> public void SetStream(ParserOptions options, Stream stream, MimeFormat format, bool persistent) { if (options == null) throw new ArgumentNullException ("options"); if (stream == null) throw new ArgumentNullException ("stream"); this.persistent = persistent && stream.CanSeek; this.options = options.Clone (); this.format = format; this.stream = stream; inputIndex = inputStart; inputEnd = inputStart; mboxMarkerOffset = 0; mboxMarkerLength = 0; offset = stream.CanSeek ? stream.Position : 0; headers.Clear (); headerOffset = 0; headerIndex = 0; bounds.Clear (); if (format == MimeFormat.Mbox) { bounds.Add (Boundary.CreateMboxBoundary ()); mboxMarkerBuffer = new byte[ReadAheadSize]; state = MimeParserState.MboxMarker; } else { state = MimeParserState.Initialized; } }