|  | @@ -0,0 +1,402 @@
 | 
	
		
			
				|  |  | +/*
 | 
	
		
			
				|  |  | + * ASCIIEncoding.cs - Implementation of the "System.Text.ASCIIEncoding" class.
 | 
	
		
			
				|  |  | + *
 | 
	
		
			
				|  |  | + * Copyright (C) 2001  Southern Storm Software, Pty Ltd.
 | 
	
		
			
				|  |  | + *
 | 
	
		
			
				|  |  | + * This program is free software; you can redistribute it and/or modify
 | 
	
		
			
				|  |  | + * it under the terms of the GNU General Public License as published by
 | 
	
		
			
				|  |  | + * the Free Software Foundation; either version 2 of the License, or
 | 
	
		
			
				|  |  | + * (at your option) any later version.
 | 
	
		
			
				|  |  | + *
 | 
	
		
			
				|  |  | + * This program is distributed in the hope that it will be useful,
 | 
	
		
			
				|  |  | + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
	
		
			
				|  |  | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
	
		
			
				|  |  | + * GNU General Public License for more details.
 | 
	
		
			
				|  |  | + *
 | 
	
		
			
				|  |  | + * You should have received a copy of the GNU General Public License
 | 
	
		
			
				|  |  | + * along with this program; if not, write to the Free Software
 | 
	
		
			
				|  |  | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +using System;
 | 
	
		
			
				|  |  | +using System.Text;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +namespace Renci.SshNet.Common
 | 
	
		
			
				|  |  | +{
 | 
	
		
			
				|  |  | +    public partial class ASCIIEncoding : Encoding
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        // Magic number used by Windows for "ASCII".
 | 
	
		
			
				|  |  | +        internal const int ASCII_CODE_PAGE = 20127;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        private static ASCIIEncoding _current = new ASCIIEncoding();
 | 
	
		
			
				|  |  | +        /// <summary>
 | 
	
		
			
				|  |  | +        /// Gets the current encoding.
 | 
	
		
			
				|  |  | +        /// </summary>
 | 
	
		
			
				|  |  | +        public static ASCIIEncoding Current
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            get
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                return _current;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /// <summary>
 | 
	
		
			
				|  |  | +        /// Calculates the number of bytes produced by encoding a set of characters from the specified character array.
 | 
	
		
			
				|  |  | +        /// </summary>
 | 
	
		
			
				|  |  | +        /// <param name="chars">The character array containing the set of characters to encode.</param>
 | 
	
		
			
				|  |  | +        /// <param name="index">The index of the first character to encode.</param>
 | 
	
		
			
				|  |  | +        /// <param name="count">The number of characters to encode.</param>
 | 
	
		
			
				|  |  | +        /// <returns>
 | 
	
		
			
				|  |  | +        /// The number of bytes produced by encoding the specified characters.
 | 
	
		
			
				|  |  | +        /// </returns>
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentNullException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="chars"/> is null. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentOutOfRangeException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="index"/> or <paramref name="count"/> is less than zero.-or- <paramref name="index"/> and <paramref name="count"/> do not denote a valid range in <paramref name="chars"/>. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.Text.EncoderFallbackException">A fallback occurred (see Understanding Encodings for complete explanation)-and-<see cref="P:System.Text.Encoding.EncoderFallback"/> is set to <see cref="T:System.Text.EncoderExceptionFallback"/>.</exception>
 | 
	
		
			
				|  |  | +        public override int GetByteCount(char[] chars, int index, int count)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            if (chars == null)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentNullException("chars");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (index < 0 || index > chars.Length)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("index");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (count < 0 || count > (chars.Length - index))
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("count");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            return count;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /// <summary>
 | 
	
		
			
				|  |  | +        /// Gets the byte count.
 | 
	
		
			
				|  |  | +        /// </summary>
 | 
	
		
			
				|  |  | +        /// <param name="chars">The chars.</param>
 | 
	
		
			
				|  |  | +        /// <returns></returns>
 | 
	
		
			
				|  |  | +        public override int GetByteCount(string chars)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            if (chars == null)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentNullException("chars");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            return chars.Length;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /// <summary>
 | 
	
		
			
				|  |  | +        /// Encodes a set of characters from the specified character array into the specified byte array.
 | 
	
		
			
				|  |  | +        /// </summary>
 | 
	
		
			
				|  |  | +        /// <param name="chars">The character array containing the set of characters to encode.</param>
 | 
	
		
			
				|  |  | +        /// <param name="charIndex">The index of the first character to encode.</param>
 | 
	
		
			
				|  |  | +        /// <param name="charCount">The number of characters to encode.</param>
 | 
	
		
			
				|  |  | +        /// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
 | 
	
		
			
				|  |  | +        /// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
 | 
	
		
			
				|  |  | +        /// <returns>
 | 
	
		
			
				|  |  | +        /// The actual number of bytes written into <paramref name="bytes"/>.
 | 
	
		
			
				|  |  | +        /// </returns>
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentNullException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="chars"/> is null.-or- <paramref name="bytes"/> is null. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentOutOfRangeException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="charIndex"/> or <paramref name="charCount"/> or <paramref name="byteIndex"/> is less than zero.-or- <paramref name="charIndex"/> and <paramref name="charCount"/> do not denote a valid range in <paramref name="chars"/>.-or- <paramref name="byteIndex"/> is not a valid index in <paramref name="bytes"/>. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="bytes"/> does not have enough capacity from <paramref name="byteIndex"/> to the end of the array to accommodate the resulting bytes. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.Text.EncoderFallbackException">A fallback occurred (see Understanding Encodings for complete explanation)-and-<see cref="P:System.Text.Encoding.EncoderFallback"/> is set to <see cref="T:System.Text.EncoderExceptionFallback"/>.</exception>
 | 
	
		
			
				|  |  | +        public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            if (chars == null)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentNullException("chars");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (bytes == null)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentNullException("bytes");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (charIndex < 0 || charIndex > chars.Length)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("charIndex");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (charCount < 0 || charCount > (chars.Length - charIndex))
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("charCount");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (byteIndex < 0 || byteIndex > bytes.Length)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("byteIndex");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if ((bytes.Length - byteIndex) < charCount)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentException();
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            int count = charCount;
 | 
	
		
			
				|  |  | +            char ch;
 | 
	
		
			
				|  |  | +            while (count-- > 0)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                ch = chars[charIndex++];
 | 
	
		
			
				|  |  | +                if (ch < (char)0x80)
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    bytes[byteIndex++] = (byte)ch;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                else
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    bytes[byteIndex++] = (byte)'?';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            return charCount;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /// <summary>
 | 
	
		
			
				|  |  | +        /// Encodes a set of characters from the specified <see cref="T:System.String"/> into the specified byte array.
 | 
	
		
			
				|  |  | +        /// </summary>
 | 
	
		
			
				|  |  | +        /// <param name="s">The <see cref="T:System.String"/> containing the set of characters to encode.</param>
 | 
	
		
			
				|  |  | +        /// <param name="charIndex">The index of the first character to encode.</param>
 | 
	
		
			
				|  |  | +        /// <param name="charCount">The number of characters to encode.</param>
 | 
	
		
			
				|  |  | +        /// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
 | 
	
		
			
				|  |  | +        /// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
 | 
	
		
			
				|  |  | +        /// <returns>
 | 
	
		
			
				|  |  | +        /// The actual number of bytes written into <paramref name="bytes"/>.
 | 
	
		
			
				|  |  | +        /// </returns>
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentNullException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="s"/> is null.-or- <paramref name="bytes"/> is null. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentOutOfRangeException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="charIndex"/> or <paramref name="charCount"/> or <paramref name="byteIndex"/> is less than zero.-or- <paramref name="charIndex"/> and <paramref name="charCount"/> do not denote a valid range in <paramref name="chars"/>.-or- <paramref name="byteIndex"/> is not a valid index in <paramref name="bytes"/>. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="bytes"/> does not have enough capacity from <paramref name="byteIndex"/> to the end of the array to accommodate the resulting bytes. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.Text.EncoderFallbackException">A fallback occurred (see Understanding Encodings for complete explanation)-and-<see cref="P:System.Text.Encoding.EncoderFallback"/> is set to <see cref="T:System.Text.EncoderExceptionFallback"/>.</exception>
 | 
	
		
			
				|  |  | +        public override int GetBytes(String s, int charIndex, int charCount, byte[] bytes, int byteIndex)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            if (s == null)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentNullException("s");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (bytes == null)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentNullException("bytes");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (charIndex < 0 || charIndex > s.Length)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("charIndex");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (charCount < 0 || charCount > (s.Length - charIndex))
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("charCount");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (byteIndex < 0 || byteIndex > bytes.Length)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("byteIndex");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if ((bytes.Length - byteIndex) < charCount)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentException();
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            int count = charCount;
 | 
	
		
			
				|  |  | +            char ch;
 | 
	
		
			
				|  |  | +            while (count-- > 0)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                ch = s[charIndex++];
 | 
	
		
			
				|  |  | +                if (ch < (char)0x80)
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    bytes[byteIndex++] = (byte)ch;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                else
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    bytes[byteIndex++] = (byte)'?';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            return charCount;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /// <summary>
 | 
	
		
			
				|  |  | +        /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.
 | 
	
		
			
				|  |  | +        /// </summary>
 | 
	
		
			
				|  |  | +        /// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
 | 
	
		
			
				|  |  | +        /// <param name="index">The index of the first byte to decode.</param>
 | 
	
		
			
				|  |  | +        /// <param name="count">The number of bytes to decode.</param>
 | 
	
		
			
				|  |  | +        /// <returns>
 | 
	
		
			
				|  |  | +        /// The number of characters produced by decoding the specified sequence of bytes.
 | 
	
		
			
				|  |  | +        /// </returns>
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentNullException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="bytes"/> is null. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentOutOfRangeException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="index"/> or <paramref name="count"/> is less than zero.-or- <paramref name="index"/> and <paramref name="count"/> do not denote a valid range in <paramref name="bytes"/>. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.Text.DecoderFallbackException">A fallback occurred (see Understanding Encodings for complete explanation)-and-<see cref="P:System.Text.Encoding.DecoderFallback"/> is set to <see cref="T:System.Text.DecoderExceptionFallback"/>.</exception>
 | 
	
		
			
				|  |  | +        public override int GetCharCount(byte[] bytes, int index, int count)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            if (bytes == null)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentNullException("bytes");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (index < 0 || index > bytes.Length)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("index");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (count < 0 || count > (bytes.Length - index))
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("count");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            return count;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /// <summary>
 | 
	
		
			
				|  |  | +        /// Decodes a sequence of bytes from the specified byte array into the specified character array.
 | 
	
		
			
				|  |  | +        /// </summary>
 | 
	
		
			
				|  |  | +        /// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
 | 
	
		
			
				|  |  | +        /// <param name="byteIndex">The index of the first byte to decode.</param>
 | 
	
		
			
				|  |  | +        /// <param name="byteCount">The number of bytes to decode.</param>
 | 
	
		
			
				|  |  | +        /// <param name="chars">The character array to contain the resulting set of characters.</param>
 | 
	
		
			
				|  |  | +        /// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
 | 
	
		
			
				|  |  | +        /// <returns>
 | 
	
		
			
				|  |  | +        /// The actual number of characters written into <paramref name="chars"/>.
 | 
	
		
			
				|  |  | +        /// </returns>
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentNullException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="bytes"/> is null.-or- <paramref name="chars"/> is null. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentOutOfRangeException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="byteIndex"/> or <paramref name="byteCount"/> or <paramref name="charIndex"/> is less than zero.-or- <paramref name="byteindex"/> and <paramref name="byteCount"/> do not denote a valid range in <paramref name="bytes"/>.-or- <paramref name="charIndex"/> is not a valid index in <paramref name="chars"/>. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="chars"/> does not have enough capacity from <paramref name="charIndex"/> to the end of the array to accommodate the resulting characters. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.Text.DecoderFallbackException">A fallback occurred (see Understanding Encodings for complete explanation)-and-<see cref="P:System.Text.Encoding.DecoderFallback"/> is set to <see cref="T:System.Text.DecoderExceptionFallback"/>.</exception>
 | 
	
		
			
				|  |  | +        public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            if (bytes == null)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentNullException("bytes");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (chars == null)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentNullException("chars");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (byteIndex < 0 || byteIndex > bytes.Length)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("byteIndex");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (byteCount < 0 || byteCount > (bytes.Length - byteIndex))
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("byteCount");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (charIndex < 0 || charIndex > chars.Length)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("charIndex");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if ((chars.Length - charIndex) < byteCount)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentException();
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            int count = byteCount;
 | 
	
		
			
				|  |  | +            byte ch;
 | 
	
		
			
				|  |  | +            while (count-- > 0)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                ch = bytes[byteIndex++];
 | 
	
		
			
				|  |  | +                if (ch < 0x80)
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    chars[charIndex++] = (char)ch;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                else
 | 
	
		
			
				|  |  | +                {
 | 
	
		
			
				|  |  | +                    chars[charIndex++] = '?';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            return byteCount;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /// <summary>
 | 
	
		
			
				|  |  | +        /// Calculates the maximum number of bytes produced by encoding the specified number of characters.
 | 
	
		
			
				|  |  | +        /// </summary>
 | 
	
		
			
				|  |  | +        /// <param name="charCount">The number of characters to encode.</param>
 | 
	
		
			
				|  |  | +        /// <returns>
 | 
	
		
			
				|  |  | +        /// The maximum number of bytes produced by encoding the specified number of characters.
 | 
	
		
			
				|  |  | +        /// </returns>
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentOutOfRangeException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="charCount"/> is less than zero. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.Text.EncoderFallbackException">A fallback occurred (see Understanding Encodings for complete explanation)-and-<see cref="P:System.Text.Encoding.EncoderFallback"/> is set to <see cref="T:System.Text.EncoderExceptionFallback"/>.</exception>
 | 
	
		
			
				|  |  | +        public override int GetMaxByteCount(int charCount)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            if (charCount < 0)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("charCount");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            return charCount;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /// <summary>
 | 
	
		
			
				|  |  | +        /// Calculates the maximum number of characters produced by decoding the specified number of bytes.
 | 
	
		
			
				|  |  | +        /// </summary>
 | 
	
		
			
				|  |  | +        /// <param name="byteCount">The number of bytes to decode.</param>
 | 
	
		
			
				|  |  | +        /// <returns>
 | 
	
		
			
				|  |  | +        /// The maximum number of characters produced by decoding the specified number of bytes.
 | 
	
		
			
				|  |  | +        /// </returns>
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentOutOfRangeException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="byteCount"/> is less than zero. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.Text.DecoderFallbackException">A fallback occurred (see Understanding Encodings for complete explanation)-and-<see cref="P:System.Text.Encoding.DecoderFallback"/> is set to <see cref="T:System.Text.DecoderExceptionFallback"/>.</exception>
 | 
	
		
			
				|  |  | +        public override int GetMaxCharCount(int byteCount)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            if (byteCount < 0)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("byteCount");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            return byteCount;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /// <summary>
 | 
	
		
			
				|  |  | +        /// Decodes a sequence of bytes from the specified byte array into a string.
 | 
	
		
			
				|  |  | +        /// </summary>
 | 
	
		
			
				|  |  | +        /// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
 | 
	
		
			
				|  |  | +        /// <param name="index">The index of the first byte to decode.</param>
 | 
	
		
			
				|  |  | +        /// <param name="count">The number of bytes to decode.</param>
 | 
	
		
			
				|  |  | +        /// <returns>
 | 
	
		
			
				|  |  | +        /// A <see cref="T:System.String"/> containing the results of decoding the specified sequence of bytes.
 | 
	
		
			
				|  |  | +        /// </returns>
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentException">The byte array contains invalid Unicode code points.</exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentNullException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="bytes"/> is null. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.ArgumentOutOfRangeException">
 | 
	
		
			
				|  |  | +        ///   <paramref name="index"/> or <paramref name="count"/> is less than zero.-or- <paramref name="index"/> and <paramref name="count"/> do not denote a valid range in <paramref name="bytes"/>. </exception>
 | 
	
		
			
				|  |  | +        ///   
 | 
	
		
			
				|  |  | +        /// <exception cref="T:System.Text.DecoderFallbackException">A fallback occurred (see Understanding Encodings for complete explanation)-and-<see cref="P:System.Text.Encoding.DecoderFallback"/> is set to <see cref="T:System.Text.DecoderExceptionFallback"/>.</exception>
 | 
	
		
			
				|  |  | +        public override String GetString(byte[] bytes, int index, int count)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            if (bytes == null)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentNullException("bytes");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (index < 0 || index > bytes.Length)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("index");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (count < 0 || count > (bytes.Length - index))
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                throw new ArgumentOutOfRangeException("count");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (count == 0)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                return String.Empty;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            char[] chars = new char[count];
 | 
	
		
			
				|  |  | +            int posn = 0;
 | 
	
		
			
				|  |  | +            while (count-- > 0)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                chars[posn++] = (char)(bytes[index++]);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            return new string(chars);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }; 
 | 
	
		
			
				|  |  | +}
 |