static string DecodeRfc2231(out Encoding encoding, ref Decoder decoder, HexDecoder hex, byte[] text, int startIndex, int count, bool flush) { int endIndex = startIndex + count; int index = startIndex; string charset; // Note: decoder is only null if this is the first segment if (decoder == null) { if (TryGetCharset(text, ref index, endIndex, out charset)) { try { encoding = CharsetUtils.GetEncoding(charset, "?"); decoder = (Decoder)encoding.GetDecoder(); } catch (NotSupportedException) { encoding = Encoding.GetEncoding(28591); // iso-8859-1 decoder = (Decoder)encoding.GetDecoder(); } } else { // When no charset is specified, it should be safe to assume US-ASCII... // but we all know what assume means, right?? encoding = Encoding.GetEncoding(28591); // iso-8859-1 decoder = (Decoder)encoding.GetDecoder(); } } else { encoding = null; } int length = endIndex - index; var decoded = new byte[hex.EstimateOutputLength(length)]; // hex decode... length = hex.Decode(text, index, length, decoded); int outLength = decoder.GetCharCount(decoded, 0, length, flush); var output = new char[outLength]; outLength = decoder.GetChars(decoded, 0, length, output, 0, flush); return(new string (output, 0, outLength)); }
internal static char[] ConvertToUnicode(Encoding encoding, byte[] input, int startIndex, int length, out int charCount) { var decoder = encoding.GetDecoder(); int count = decoder.GetCharCount(input, startIndex, length, true); var output = new char[count]; charCount = decoder.GetChars(input, startIndex, length, output, 0, true); return(output); }
/// <summary> /// Initializes a new instance of the <see cref="MimeKit.IO.Filters.CharsetFilter"/> class. /// </summary> /// <remarks> /// Creates a new <see cref="CharsetFilter"/> to convert text from the specified /// source encoding into the target charset encoding. /// </remarks> /// <param name="sourceEncoding">Source encoding.</param> /// <param name="targetEncoding">Target encoding.</param> /// <exception cref="System.ArgumentNullException"> /// <para><paramref name="sourceEncoding"/> is <c>null</c>.</para> /// <para>-or-</para> /// <para><paramref name="targetEncoding"/> is <c>null</c>.</para> /// </exception> public CharsetFilter(Encoding sourceEncoding, Encoding targetEncoding) { if (sourceEncoding == null) { throw new ArgumentNullException(nameof(sourceEncoding)); } if (targetEncoding == null) { throw new ArgumentNullException(nameof(targetEncoding)); } SourceEncoding = sourceEncoding; TargetEncoding = targetEncoding; decoder = (Decoder)SourceEncoding.GetDecoder(); encoder = (Encoder)TargetEncoding.GetEncoder(); }
/// <summary> /// Initializes a new instance of the <see cref="MimeKit.IO.Filters.CharsetFilter"/> class. /// </summary> /// <remarks> /// Creates a new <see cref="CharsetFilter"/> to convert text from the specified /// source encoding into the target charset encoding. /// </remarks> /// <param name="sourceEncoding">Source encoding.</param> /// <param name="targetEncoding">Target encoding.</param> /// <exception cref="System.ArgumentNullException"> /// <para><paramref name="sourceEncoding"/> is <c>null</c>.</para> /// <para>-or-</para> /// <para><paramref name="targetEncoding"/> is <c>null</c>.</para> /// </exception> public CharsetFilter (Encoding sourceEncoding, Encoding targetEncoding) { if (sourceEncoding == null) throw new ArgumentNullException ("sourceEncoding"); if (targetEncoding == null) throw new ArgumentNullException ("targetEncoding"); SourceEncoding = sourceEncoding; TargetEncoding = targetEncoding; decoder = (Decoder) SourceEncoding.GetDecoder (); encoder = (Encoder) TargetEncoding.GetEncoder (); }
internal static char[] ConvertToUnicode (Encoding encoding, byte[] input, int startIndex, int length, out int charCount) { var decoder = encoding.GetDecoder (); int count = decoder.GetCharCount (input, startIndex, length, true); char[] output = new char[count]; charCount = decoder.GetChars (input, startIndex, length, output, 0, true); return output; }