#if !FEATURE_ENCODING_ASCII using System; using System.Text; namespace Renci.SshNet.Common { /// /// Implementation of ASCII Encoding /// public class ASCIIEncoding : Encoding { private readonly char _fallbackChar; private static readonly char[] ByteToChar; static ASCIIEncoding() { if (ByteToChar == null) { ByteToChar = new char[128]; var ch = '\0'; for (byte i = 0; i < 128; i++) { ByteToChar[i] = ch++; } } } /// /// Initializes a new instance of the class. /// public ASCIIEncoding() { _fallbackChar = '?'; } /// /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. /// /// The character array containing the set of characters to encode. /// The index of the first character to encode. /// The number of characters to encode. /// /// The number of bytes produced by encoding the specified characters. /// /// is null. /// or is less than zero.-or- and do not denote a valid range in . public override int GetByteCount(char[] chars, int index, int count) { return count; } /// /// Encodes a set of characters from the specified character array into the specified byte array. /// /// The character array containing the set of characters to encode. /// The index of the first character to encode. /// The number of characters to encode. /// The byte array to contain the resulting sequence of bytes. /// The index at which to start writing the resulting sequence of bytes. /// /// The actual number of bytes written into . /// /// is null.-or- is null. /// or or is less than zero.-or- and do not denote a valid range in .-or- is not a valid index in . /// does not have enough capacity from to the end of the array to accommodate the resulting bytes. public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) { for (var i = 0; i < charCount && i < chars.Length; i++) { var b = (byte)chars[i + charIndex]; if (b > 127) b = (byte) _fallbackChar; bytes[i + byteIndex] = b; } return charCount; } /// /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. /// /// The byte array containing the sequence of bytes to decode. /// The index of the first byte to decode. /// The number of bytes to decode. /// /// The number of characters produced by decoding the specified sequence of bytes. /// /// is null. /// or is less than zero.-or- and do not denote a valid range in . public override int GetCharCount(byte[] bytes, int index, int count) { return count; } /// /// Decodes a sequence of bytes from the specified byte array into the specified character array. /// /// The byte array containing the sequence of bytes to decode. /// The index of the first byte to decode. /// The number of bytes to decode. /// The character array to contain the resulting set of characters. /// The index at which to start writing the resulting set of characters. /// /// The actual number of characters written into . /// /// is null.-or- is null. /// or or is less than zero.-or- and do not denote a valid range in .-or- is not a valid index in . /// does not have enough capacity from to the end of the array to accommodate the resulting characters. public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { for (var i = 0; i < byteCount; i++) { var b = bytes[i + byteIndex]; char ch; if (b > 127) { ch = _fallbackChar; } else { ch = ByteToChar[b]; } chars[i + charIndex] = ch; } return byteCount; } /// /// Calculates the maximum number of bytes produced by encoding the specified number of characters. /// /// The number of characters to encode. /// /// The maximum number of bytes produced by encoding the specified number of characters. /// /// is less than zero. public override int GetMaxByteCount(int charCount) { if (charCount < 0) throw new ArgumentOutOfRangeException("charCount", "Non-negative number required."); return charCount + 1; } /// /// Calculates the maximum number of characters produced by decoding the specified number of bytes. /// /// The number of bytes to decode. /// /// The maximum number of characters produced by decoding the specified number of bytes. /// /// is less than zero. public override int GetMaxCharCount(int byteCount) { if (byteCount < 0) throw new ArgumentOutOfRangeException("byteCount", "Non-negative number required."); return byteCount; } } } #endif // !FEATURE_ENCODING_ASCII