| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 | /* * 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{    /// <summary>    /// Represents an ASCII character encoding of Unicode characters.    /// </summary>    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="bytes"/>.-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);        }    }; }
 |