Inheritance: ISerializable, IObjectReference
Exemple #1
0
            public Decoder(CodePageEncoding encoding)
            {
                if (encoding.CodePage == 50229)
                {
                    throw new NotSupportedException(Environment.GetResourceString("NotSupported_CodePage50229"));
                }

                this.encoding = encoding;
                lastByte      = -1;
                temp          = new byte[2];
            }
        /*
         * //ENCODINGFALLBACK
         * internal static Encoding CreateDefaultEncoding(EncodingFallback encodingFallback) {
         *  Encoding enc;
         *  int codePage = Win32Native.GetACP();
         *
         *  // For US English, we can save some startup working set by not calling
         *  // GetEncoding(int codePage) since JITting GetEncoding will force us to load
         *  // all the Encoding classes for ASCII, UTF7 & UTF8, & UnicodeEncoding.
         *  if (codePage == 1252) {
         *      enc = new CodePageEncoding(codePage, encodingFallback);
         *  }
         *  else
         *      enc = GetEncoding(codePage);
         *  return (enc);
         * }
         */
        internal static Encoding CreateDefaultEncoding()
        {
            Encoding enc;
            int      codePage = Win32Native.GetACP();

            // For US English, we can save some startup working set by not calling
            // GetEncoding(int codePage) since JITting GetEncoding will force us to load
            // all the Encoding classes for ASCII, UTF7 & UTF8, & UnicodeEncoding.
            if (codePage == 1252)
            {
                enc = new CodePageEncoding(codePage);
            }
            else
            {
                enc = GetEncoding(codePage);
            }
            return(enc);
        }
Exemple #3
0
        private static Encoding GetEncodingRare(int codepage)
        {
            BCLDebug.Assert(codepage != 0 && codepage != 1200 && codepage != 1201 && codepage != 65001, "This code page (" + codepage + " isn't supported by GetEncodingRare!");
            Encoding result;

            switch (codepage)
            {
            case 1:
            case 2:
            case 3:
            case 42:
                // NOTENOTE                     :
                // Win32 also allows the following special code page values.  We won't allow them except in the
                // CP_ACP case.
                // #define CP_ACP                    0           // default to ANSI code page
                // #define CP_OEMCP                  1           // default to OEM  code page
                // #define CP_MACCP                  2           // default to MAC  code page
                // #define CP_THREAD_ACP             3           // current thread's ANSI code page
                // #define CP_SYMBOL                 42          // SYMBOL translations
                throw new ArgumentException(String.Format(Environment.GetResourceString("Argument_CodepageNotSupported"), codepage), "codepage");

            case 65000:
                result = UTF7;
                break;

            case 20127:
                result = ASCII;
                break;

            case ENC50229:
                throw new NotSupportedException(Environment.GetResourceString("NotSupported_CodePage50229"));

            default:
                result = new CodePageEncoding(codepage);
                break;
            }
            return(result);
        }
        /*
         * //ENCODINGFALLBACK
         * internal unsafe int CallUnicodeToBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, int byteCount, out bool usedDefaultChar) {
         *  //
         *  // For MLang, there is no way to detect if default characters are used during the conversion.
         *  // So always set used default char to true.
         *  //
         *  usedDefaultChar = true;
         *  return (nativeUnicodeToBytes(m_pIMLangConvertCharsetFromUnicode, chars, charIndex, charCount, bytes, byteIndex, byteCount));
         * }
         *
         * internal unsafe int CallBytesToUnicode(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, int charCount) {
         *  return (nativeBytesToUnicode(m_pIMLangConvertCharsetToUnicode, bytes, byteIndex, out byteCount, chars, charIndex, charCount));
         * }
         */

        public override int GetCharCount(byte[] bytes, int index, int count)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes",
                                                Environment.GetResourceString("ArgumentNull_Array"));
            }
            if (index < 0 || count < 0)
            {
                throw new ArgumentOutOfRangeException((index < 0 ? "index" : "count"),
                                                      Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (bytes.Length - index < count)
            {
                throw new ArgumentOutOfRangeException("bytes",
                                                      Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
            }

            int charCount = 0;
            int result;
            int dwMode = 0;

            if (count < 3 && IsISCIICodePage(CodePage))
            {
                // Because of the way that MLang handles DLL-based code page, we will always fail (and get AgrumentException as a result)
                // when the byteCount is less than 3.
                // Therfore, let's call Win32 to convert these bytes directly.
                result = CodePageEncoding.BytesToUnicodeNative(CodePage, bytes, index, count, null, 0, 0);
            }
            else
            {
                result = nativeBytesToUnicode(CodePage, bytes, index, out count, null, 0, charCount, ref dwMode);
            }

            return(result);
        }
Exemple #5
0
 internal static Encoding CreateDefaultEncoding() {
     Encoding enc;
     int codePage = Win32Native.GetACP();
     
     // For US English, we can save some startup working set by not calling
     // GetEncoding(int codePage) since JITting GetEncoding will force us to load
     // all the Encoding classes for ASCII, UTF7 & UTF8, & UnicodeEncoding.
     if (codePage == 1252) {
         enc = new CodePageEncoding(codePage);
     }
     else
         enc = GetEncoding(codePage);
     return (enc);
 }
Exemple #6
0
 private static Encoding GetEncodingRare(int codepage)
 {
     BCLDebug.Assert(codepage != 0 && codepage != 1200 && codepage != 1201 && codepage != 65001, "This code page ("+codepage+" isn't supported by GetEncodingRare!");
     Encoding result;
     switch (codepage) {
     case 1:
     case 2:
     case 3:
     case 42:
         // NOTENOTE                     :
         // Win32 also allows the following special code page values.  We won't allow them except in the 
         // CP_ACP case.
         // #define CP_ACP                    0           // default to ANSI code page
         // #define CP_OEMCP                  1           // default to OEM  code page
         // #define CP_MACCP                  2           // default to MAC  code page
         // #define CP_THREAD_ACP             3           // current thread's ANSI code page
         // #define CP_SYMBOL                 42          // SYMBOL translations
         throw new ArgumentException(String.Format(Environment.GetResourceString("Argument_CodepageNotSupported"), codepage), "codepage");
     case 65000:
         result = UTF7;
         break;
     case 20127:
         result = ASCII;
         break;
     case ENC50229:
         throw new NotSupportedException(Environment.GetResourceString("NotSupported_CodePage50229"));
     default:
         result = new CodePageEncoding(codepage);
         break;
     }
     return result;
 }
Exemple #7
0
 /// <include file='doc\Encoding.uex' path='docs/doc[@for="Encoding.GetEncoding1"]/*' />
 public static Encoding GetEncoding(int codepage)
 {
     //
     // NOTENOTE: If you add a new encoding that can be get by codepage, be sure to
     // add the corresponding item in EncodingTable.
     // Otherwise, the code below will throw exception when trying to call 
     // EncodingTable.GetDataItem().
     //
     if (codepage < 0 || codepage > 65535) {
         throw new ArgumentOutOfRangeException(
             "codepage", String.Format(Environment.GetResourceString("ArgumentOutOfRange_Range"), 
             0, 65535));
     }
     Encoding result = null;
     if (encodings != null)
         result = (Encoding)encodings[codepage];
     if (result == null) {
         lock (typeof(Encoding)) {
             if (encodings == null) encodings = new Hashtable();
             if ((result = (Encoding)encodings[codepage])!=null) {
                 return result;
             }
             // Special case the commonly used Encoding classes here, then call
             // GetEncodingRare to avoid loading classes like MLangCodePageEncoding
             // and ASCIIEncoding.  ASP.NET uses UTF-8 & ISO-8859-1.
             switch (codepage) {
             case 0:
                 result = CreateDefaultEncoding();
                 break;
             case 1200:
                 result = Unicode;
                 break;
             case 1201:
                 result = BigEndianUnicode;
                 break;
             case 1252:
             case ISO_8859_1:
                 result = new CodePageEncoding(codepage);
                 break;
             case 65001:
                 result = UTF8;
                 break;
             default:
                 result = GetEncodingRare(codepage);
                 break;
             }
             encodings.Add(codepage, result);
         }
         
     }
     return result;
 }
        private static Encoding GetEncodingRare(int codepage)
        {
            BCLDebug.Assert(codepage != 0 && codepage != 1200 && codepage != 1201 && codepage != 65001, "This code page (" + codepage + " isn't supported by GetEncodingRare!");
            Encoding result;

            switch (codepage)
            {
            case 1:
            case 2:
            case 3:
            case 42:
                // NOTENOTE YSLin:
                // Win32 also allows the following special code page values.  We won't allow them except in the
                // CP_ACP case.
                // #define CP_ACP                    0           // default to ANSI code page
                // #define CP_OEMCP                  1           // default to OEM  code page
                // #define CP_MACCP                  2           // default to MAC  code page
                // #define CP_THREAD_ACP             3           // current thread's ANSI code page
                // #define CP_SYMBOL                 42          // SYMBOL translations
                throw new ArgumentException(String.Format(Environment.GetResourceString("Argument_CodepageNotSupported"), codepage), "codepage");

            case 65000:
                result = UTF7;
                break;

            case 20127:
                result = ASCII;
                break;

            case ISO_8859_1:
                result = Latin1;
                break;

            case ISOKorean:
                result = new MLangCodePageEncoding(ISOKorean, ISOKorean_SIZE);
                break;

            case ChineseSimp:
                result = new MLangCodePageEncoding(ChineseSimp, ChineseSimp_SIZE);
                break;

            case ISCIIAsseme:
            case ISCIIBengali:
            case ISCIIDevanagari:
            case ISCIIGujarathi:
            case ISCIIKannada:
            case ISCIIMalayalam:
            case ISCIIOriya:
            case ISCIIPanjabi:
            case ISCIITamil:
            case ISCIITelugu:
                result = new MLangCodePageEncoding(codepage, ISCII_SIZE);
                break;

            case ISO2022JP:
                result = new MLangCodePageEncoding(ISO2022JP, ISO2022JP_SIZE);
                break;

            case ENC50221:
                result = new MLangCodePageEncoding(ENC50221, ENC50221_SIZE);
                break;

            case ENC50222:
                result = new MLangCodePageEncoding(ENC50222, ENC50222_SIZE);
                break;

            case ENC50227:
                result = new MLangCodePageEncoding(ENC50227, ENC50227_SIZE);
                break;

            case EUCJP:
                result = new MLangCodePageEncoding(EUCJP, EUCJP_SIZE);
                break;

            case GB18030:
                result = new GB18030Encoding();
                break;

            case EUCCN:
                result = new MLangCodePageEncoding(EUCCN, EUCCN_SIZE);
                break;

            case ENC50229:
                throw new NotSupportedException(Environment.GetResourceString("NotSupported_CodePage50229"));

            default:
                result = new CodePageEncoding(codepage);
                break;
            }
            return(result);
        }
        // Returns an Encoding object for a given code page.
        //
        // YSLin Make this internal for now until we decide the final spec of EncodingFallback

        /*
         * //ENCODINGFALLBACK
         * internal static Encoding GetEncoding(int codepage, EncodingFallback encodingFallback)
         * {
         *  //
         *  // NOTENOTE: If you add a new encoding that can be get by codepage, be sure to
         *  // add the corresponding item in EncodingTable.
         *  // Otherwise, the code below will throw exception when trying to call
         *  // EncodingTable.GetDataItem().
         *  //
         *  if (codepage < 0 || codepage > 65535) {
         *      throw new ArgumentOutOfRangeException(
         *          "codepage", String.Format(Environment.GetResourceString("ArgumentOutOfRange_Range"),
         *          0, 65535));
         *  }
         *  Encoding result = null;
         *  if (encodings != null)
         *      result = (Encoding)encodings[codepage];
         *  if (result == null) {
         *      lock (typeof(Encoding)) {
         *          if (encodings == null) encodings = new Hashtable();
         *          if ((result = (Encoding)encodings[codepage])!=null) {
         *              return result;
         *          }
         *          switch (codepage) {
         *          case 0:
         *              if (encodingFallback != null) {
         *                  return (CreateDefaultEncoding(encodingFallback));
         *              }
         *              result = Default;
         *              break;
         *          case 1:
         *          case 2:
         *          case 3:
         *          case 42:
         *              // NOTENOTE YSLin:
         *              // Win32 also allows the following special code page values.  We won't allow them except in the
         *              // CP_ACP case.
         *              // #define CP_ACP                    0           // default to ANSI code page
         *              // #define CP_OEMCP                  1           // default to OEM  code page
         *              // #define CP_MACCP                  2           // default to MAC  code page
         *              // #define CP_THREAD_ACP             3           // current thread's ANSI code page
         *              // #define CP_SYMBOL                 42          // SYMBOL translations
         *              throw new ArgumentException(String.Format(Environment.GetResourceString("Argument_CodepageNotSupported"), codepage), "codepage");
         *           case 1200:
         *              result = Unicode;
         *              break;
         *          case 1201:
         *              result = BigEndianUnicode;
         *              break;
         *          case 65000:
         *              result = UTF7;
         *              break;
         *          case 65001:
         *              result = UTF8;
         *              break;
         *          case 20127:
         *              result = ASCII;
         *              break;
         *          case ISOKorean:
         *          case ChineseSimp:
         *          case ISCIIAsseme:
         *          case ISCIIBengali:
         *          case ISCIIDevanagari:
         *          case ISCIIGujarathi:
         *          case ISCIIKannada:
         *          case ISCIIMalayalam:
         *          case ISCIIOriya:
         *          case ISCIIPanjabi:
         *          case ISCIITamil:
         *          case ISCIITelugu:
         *          case ISO2022JP:
         *              if (encodingFallback != null) {
         *                  return (new MLangCodePageEncoding(codepage, encodingFallback));
         *              }
         *              result = new MLangCodePageEncoding(codepage);
         *              break;
         *          case EUCJP:
         *              result = new EUCJPEncoding();
         *              break;
         *          default:
         *              if (encodingFallback != null) {
         *                  return (new CodePageEncoding(codepage, encodingFallback));
         *              }
         *              result = new CodePageEncoding(codepage);
         *  break;
         *          }
         *          encodings.Add(codepage, result);
         *      }
         *
         *  }
         *  return result;
         * }
         */


        /// <include file='doc\Encoding.uex' path='docs/doc[@for="Encoding.GetEncoding1"]/*' />
        public static Encoding GetEncoding(int codepage)
        {
            //
            // NOTENOTE: If you add a new encoding that can be get by codepage, be sure to
            // add the corresponding item in EncodingTable.
            // Otherwise, the code below will throw exception when trying to call
            // EncodingTable.GetDataItem().
            //
            if (codepage < 0 || codepage > 65535)
            {
                throw new ArgumentOutOfRangeException(
                          "codepage", String.Format(Environment.GetResourceString("ArgumentOutOfRange_Range"),
                                                    0, 65535));
            }
            Encoding result = null;

            if (encodings != null)
            {
                result = (Encoding)encodings[codepage];
            }
            if (result == null)
            {
                lock (typeof(Encoding)) {
                    if (encodings == null)
                    {
                        encodings = new Hashtable();
                    }
                    if ((result = (Encoding)encodings[codepage]) != null)
                    {
                        return(result);
                    }
                    // Special case the commonly used Encoding classes here, then call
                    // GetEncodingRare to avoid loading classes like MLangCodePageEncoding
                    // and ASCIIEncoding.  ASP.NET uses UTF-8 & ISO-8859-1.
                    switch (codepage)
                    {
                    case 0:
                        result = CreateDefaultEncoding();
                        break;

                    case 1200:
                        result = Unicode;
                        break;

                    case 1201:
                        result = BigEndianUnicode;
                        break;

                    case 1252:
                        result = new CodePageEncoding(codepage);
                        break;

                    case 65001:
                        result = UTF8;
                        break;

                    default:
                        result = GetEncodingRare(codepage);
                        break;
                    }
                    encodings.Add(codepage, result);
                }
            }
            return(result);
        }
Exemple #10
0
        static Console() {
            // For console apps, the console handles are set to values like 3, 7, 
            // and 11 OR if you've been created via CreateProcess, possibly -1
            // or 0.  -1 is definitely invalid, while 0 is probably invalid.
            // Also note each handle can independently be bad or good.
            // For Windows apps, the console handles are set to values like 3, 7, 
            // and 11 but are invalid handles - you may not write to them.  However,
            // you can still spawn a Windows app via CreateProcess and read stdout
            // and stderr.
            // So, we always need to check each handle independently for validity
            // by trying to write or read to it, unless it is -1.

            
            // Delay initialize Console.In until someone uses it.
            
            // Set up Console.Out
            Encoding outEnc = null;
            Stream s = OpenStandardOutput(_DefaultConsoleBufferSize);
            if (s == Stream.Null) {
#if _DEBUG
                if (CheckOutputDebug())
                    _out = MakeDebugOutputTextWriter("Console.Out: ");
                else
#endif
                    _out = TextWriter.Synchronized(StreamWriter.Null);
            }
            else {
                // To avoid loading about 7 classes, don't call Encoding.GetEncoding(int)
                outEnc = new CodePageEncoding(GetConsoleOutputCPNative());
                StreamWriter stdout = new StreamWriter(s, outEnc, _DefaultConsoleBufferSize);
                stdout.AutoFlush = true;
                stdout.Closable = !IsStreamAConsole(Win32Native.STD_OUTPUT_HANDLE);
                _out = TextWriter.Synchronized(stdout);
            }

            // Set up Console.Error
            s = OpenStandardError(_DefaultConsoleBufferSize);
            if (s == Stream.Null) {
#if _DEBUG
                if (CheckOutputDebug())
                    _error = MakeDebugOutputTextWriter("Console.Error: ");
                else
#endif
                    _error = TextWriter.Synchronized(StreamWriter.Null);
            }
            else {
                if (outEnc == null)
                    outEnc = new CodePageEncoding(GetConsoleOutputCPNative());
                StreamWriter stderr = new StreamWriter(s, outEnc, _DefaultConsoleBufferSize);
                stderr.AutoFlush = true;
                stderr.Closable = !IsStreamAConsole(Win32Native.STD_ERROR_HANDLE);
                _error = TextWriter.Synchronized(stderr);
            }
        }
Exemple #11
0
            public Decoder(CodePageEncoding encoding) {
                if (encoding.CodePage==50229) {
                    throw new NotSupportedException(Environment.GetResourceString("NotSupported_CodePage50229"));
                }

                this.encoding = encoding;
                lastByte = -1;
                temp = new byte[2];
            }
        public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
        {
            if (bytes == null || chars == null)
            {
                throw new ArgumentNullException((bytes == null ? "bytes" : "chars"),
                                                Environment.GetResourceString("ArgumentNull_Array"));
            }
            if (byteIndex < 0 || byteCount < 0)
            {
                throw new ArgumentOutOfRangeException((byteIndex < 0 ? "byteIndex" : "byteCount"),
                                                      Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (bytes.Length - byteIndex < byteCount)
            {
                throw new ArgumentOutOfRangeException("bytes",
                                                      Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
            }
            if (charIndex < 0 || charIndex > chars.Length)
            {
                throw new ArgumentOutOfRangeException("charIndex",
                                                      Environment.GetResourceString("ArgumentOutOfRange_Index"));
            }
            if (byteCount == 0)
            {
                return(0);
            }
            // There are cases that a call to nativeBytesToUnicode() may generate an empty string.  For example, if
            // bytes only contain a lead byte.
            // Therefore, we should allow charIndex to be equal to chars.Length.

            int charCount = chars.Length - charIndex;

            int result;
            int dwMode = 0;

            if (byteCount < 3 && IsISCIICodePage(CodePage))
            {
                if (charIndex == chars.Length)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_ConversionOverflow"));
                }
                // Because of the way that MLang handles DLL-based code page, we will always fail (and get AgrumentException as a result)
                // when the byteCount is less than 3.
                // Therfore, let's call Win32 to convert these bytes directly.
                result = CodePageEncoding.BytesToUnicodeNative(CodePage, bytes, byteIndex, byteCount,
                                                               chars, charIndex, chars.Length - charIndex);
                if (result == 0)
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_ConversionOverflow"));
                }
            }
            else
            {
                result = nativeBytesToUnicode(
                    CodePage,
                    bytes, byteIndex, out byteCount,
                    chars, charIndex, charCount, ref dwMode);
            }

            return(result);
        }