Наследование: BaseCodePageEncoding
        public override Encoding GetEncoding(int codepage)
        {
            if (codepage < 0 || codepage > 65535)
                return null;

            if (codepage == 0)
            {
                // Retrieve the system default non-unicode code page if possible, or return null,
                // giving the rest of the EncodingProviders a chance to return a default.
                int systemDefaultCodePage = SystemDefaultCodePage;
                return systemDefaultCodePage != 0 ?
                    GetEncoding(systemDefaultCodePage) :
                    null;
            }

            Encoding result = null;

            _cacheLock.EnterUpgradeableReadLock();
            try
            {
                if (_encodings.TryGetValue(codepage, out result))
                    return result;

                int i = BaseCodePageEncoding.GetCodePageByteSize(codepage);

                if (i == 1)
                {
                    result = new SBCSCodePageEncoding(codepage);
                }
                else if (i == 2)
                {
                    result = new DBCSCodePageEncoding(codepage);
                }
                else
                {
                    result = GetEncodingRare(codepage);
                    if (result == null)
                        return null;
                }

                _cacheLock.EnterWriteLock();
                try
                {
                    Encoding cachedEncoding;
                    if (_encodings.TryGetValue(codepage, out cachedEncoding))
                        return cachedEncoding;

                    _encodings.Add(codepage, result);
                }
                finally
                {
                    _cacheLock.ExitWriteLock();
                }
            }
            finally
            {
                _cacheLock.ExitUpgradeableReadLock();
            }

            return result;
        }
        public static Encoding GetEncoding(int codepage)
        {
            if ((codepage < 0) || (codepage > 0xffff))
            {
                throw new ArgumentOutOfRangeException("codepage", Environment.GetResourceString("ArgumentOutOfRange_Range", new object[] { 0, 0xffff }));
            }
            Encoding unicode = null;
            if (encodings != null)
            {
                unicode = (Encoding) encodings[codepage];
            }
            if (unicode == null)
            {
                lock (InternalSyncObject)
                {
                    if (encodings == null)
                    {
                        encodings = new Hashtable();
                    }
                    unicode = (Encoding) encodings[codepage];
                    if (unicode != null)
                    {
                        return unicode;
                    }
                    switch (codepage)
                    {
                        case 0:
                            unicode = Default;
                            break;

                        case 1:
                        case 2:
                        case 3:
                        case 0x2a:
                            throw new ArgumentException(Environment.GetResourceString("Argument_CodepageNotSupported", new object[] { codepage }), "codepage");

                        case 0x4b0:
                            unicode = Unicode;
                            break;

                        case 0x4b1:
                            unicode = BigEndianUnicode;
                            break;

                        case 0x6faf:
                            unicode = Latin1;
                            break;

                        case 0xfde9:
                            unicode = UTF8;
                            break;

                        case 0x4e4:
                            unicode = new SBCSCodePageEncoding(codepage);
                            break;

                        case 0x4e9f:
                            unicode = ASCII;
                            break;

                        default:
                            unicode = GetEncodingCodePage(codepage);
                            if (unicode == null)
                            {
                                unicode = GetEncodingRare(codepage);
                            }
                            break;
                    }
                    encodings.Add(codepage, unicode);
                }
            }
            return unicode;
        }
Пример #3
0
        [System.Security.SecurityCritical]  // auto-generated
        private static Encoding GetEncodingRare(int codepage)
        {
            Contract.Assert(codepage != 0 && codepage != 1200 && codepage != 1201 && codepage != 65001,
                "[Encoding.GetEncodingRare]This code page (" + codepage + ") isn't supported by GetEncodingRare!");
            Encoding result;
            switch (codepage)
            {
                case CodePageUTF7:              // 65000
                    result = UTF7;
                    break;
                case CodePageUTF32:             // 12000
                    result = UTF32;
                    break;
                case CodePageUTF32BE:           // 12001
                    result = new UTF32Encoding(true, true);
                    break;
                case ISCIIAssemese:
                case ISCIIBengali:
                case ISCIIDevanagari:
                case ISCIIGujarathi:
                case ISCIIKannada:
                case ISCIIMalayalam:
                case ISCIIOriya:
                case ISCIIPanjabi:
                case ISCIITamil:
                case ISCIITelugu:
                    result = new ISCIIEncoding(codepage);
                    break;
                // GB2312-80 uses same code page for 20936 and mac 10008
                case CodePageMacGB2312:
          //     case CodePageGB2312:
          //        result = new DBCSCodePageEncoding(codepage, EUCCN);
                    result = new DBCSCodePageEncoding(CodePageMacGB2312, CodePageGB2312);
                    break;

                // Mac Korean 10003 and 20949 are the same
                case CodePageMacKorean:
                    result = new DBCSCodePageEncoding(CodePageMacKorean, CodePageDLLKorean);
                    break;
                // GB18030 Code Pages
                case GB18030:
                    result = new GB18030Encoding();
                    break;
                // ISO2022 Code Pages
                case ISOKorean:
            //    case ISOSimplifiedCN
                case ChineseHZ:
                case ISO2022JP:         // JIS JP, full-width Katakana mode (no half-width Katakana)
                case ISO2022JPESC:      // JIS JP, esc sequence to do Katakana.
                case ISO2022JPSISO:     // JIS JP with Shift In/ Shift Out Katakana support
                    result = new ISO2022Encoding(codepage);
                    break;
                // Duplicate EUC-CN (51936) just calls a base code page 936,
                // so does ISOSimplifiedCN (50227), which's gotta be broken
                case DuplicateEUCCN:
                case ISOSimplifiedCN:
                    result = new DBCSCodePageEncoding(codepage, EUCCN);    // Just maps to 936
                    break;
                case EUCJP:
                    result = new EUCJPEncoding();
                    break;
                case EUCKR:
                    result = new DBCSCodePageEncoding(codepage, CodePageDLLKorean);    // Maps to 20949
                    break;
                case ENC50229:
                    throw new NotSupportedException(Environment.GetResourceString("NotSupported_CodePage50229"));
                case ISO_8859_8I:
                    result = new SBCSCodePageEncoding(codepage, ISO_8859_8_Visual);        // Hebrew maps to a different code page
                    break;
                default:
                    // Not found, already tried codepage table code pages in GetEncoding()
                    throw new NotSupportedException(
                        Environment.GetResourceString("NotSupported_NoCodepageData", codepage));
            }
            return result;
        }
        private static Encoding GetEncodingRare(int codepage)
        {
            Encoding result = null;

            switch (codepage)
            {
                case ISCIIAssemese:
                case ISCIIBengali:
                case ISCIIDevanagari:
                case ISCIIGujarathi:
                case ISCIIKannada:
                case ISCIIMalayalam:
                case ISCIIOriya:
                case ISCIIPanjabi:
                case ISCIITamil:
                case ISCIITelugu:
                    result = new ISCIIEncoding(codepage);
                    break;
                // GB2312-80 uses same code page for 20936 and mac 10008
                case CodePageMacGB2312:
                    //     case CodePageGB2312:
                    //        result = new DBCSCodePageEncoding(codepage, EUCCN);
                    result = new DBCSCodePageEncoding(CodePageMacGB2312, CodePageGB2312);
                    break;

                // Mac Korean 10003 and 20949 are the same
                case CodePageMacKorean:
                    result = new DBCSCodePageEncoding(CodePageMacKorean, CodePageDLLKorean);
                    break;
                // GB18030 Code Pages
                case GB18030:
                    result = new GB18030Encoding();
                    break;
                // ISO2022 Code Pages
                case ISOKorean:
                //    case ISOSimplifiedCN
                case ChineseHZ:
                case ISO2022JP:         // JIS JP, full-width Katakana mode (no half-width Katakana)
                case ISO2022JPESC:      // JIS JP, esc sequence to do Katakana.
                case ISO2022JPSISO:     // JIS JP with Shift In/ Shift Out Katakana support
                    result = new ISO2022Encoding(codepage);
                    break;
                // Duplicate EUC-CN (51936) just calls a base code page 936,
                // so does ISOSimplifiedCN (50227), which's gotta be broken
                case DuplicateEUCCN:
                case ISOSimplifiedCN:
                    result = new DBCSCodePageEncoding(codepage, EUCCN);    // Just maps to 936
                    break;
                case EUCJP:
                    result = new EUCJPEncoding();
                    break;
                case EUCKR:
                    result = new DBCSCodePageEncoding(codepage, CodePageDLLKorean);    // Maps to 20949
                    break;
                case ISO_8859_8I:
                    result = new SBCSCodePageEncoding(codepage, ISO_8859_8_Visual);        // Hebrew maps to a different code page
                    break;
            }
            return result;
        }
Пример #5
0
        [System.Security.SecurityCritical]  // auto-generated
        private static Encoding CreateDefaultEncoding()
        {
            Encoding enc;

#if FEATURE_CODEPAGES_FILE            
            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 SBCSCodePageEncoding(codePage);
            else
                enc = GetEncoding(codePage);
#else // FEATURE_CODEPAGES_FILE            

            // For silverlight we use UTF8 since ANSI isn't available
            enc = UTF8;

#endif //FEATURE_CODEPAGES_FILE            

            return (enc);
        }
Пример #6
0
        [System.Security.SecuritySafeCritical]  // auto-generated
#endif
        public static Encoding GetEncoding(int codepage)
        {
            Encoding result = EncodingProvider.GetEncodingFromProvider(codepage);
            if (result != null)
                return result;

            //
            // NOTE: 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", Environment.GetResourceString("ArgumentOutOfRange_Range",
                        0, 65535));
            }

            Contract.EndContractBlock();

            // Our Encoding

            // See if we have a hash table with our encoding in it already.
            if (encodings != null) {
                result = (Encoding)encodings[codepage];
            }

            if (result == null)
            {
                // Don't conflict with ourselves
                lock (InternalSyncObject)
                {
                    // Need a new hash table
                    // in case another thread beat us to creating the Dictionary
                    if (encodings == null) {
                        encodings = new Hashtable();
                    }

                    // Double check that we don't have one in the table (in case another thread beat us here)
                    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 CodePageDefault:                   // 0, default code page
                            result = Encoding.Default;
                            break;
                        case CodePageUnicode:                   // 1200, Unicode
                            result = Unicode;
                            break;
                        case CodePageBigEndian:                 // 1201, big endian unicode
                            result = BigEndianUnicode;
                            break;
#if FEATURE_CODEPAGES_FILE                            
                        case CodePageWindows1252:               // 1252, Windows
                            result = new SBCSCodePageEncoding(codepage);
                            break;
#else

#if FEATURE_UTF7
                            // on desktop, UTF7 is handled by GetEncodingRare.
                            // On Coreclr, we handle this directly without bringing GetEncodingRare, so that we get real UTF-7 encoding.
                        case CodePageUTF7:                      // 65000, UTF7
                            result = UTF7;
                            break;
#endif 

#if FEATURE_UTF32        
                        case CodePageUTF32:             // 12000
                            result = UTF32;
                            break;
                        case CodePageUTF32BE:           // 12001
                            result = new UTF32Encoding(true, true);
                            break;
#endif

#endif
                        case CodePageUTF8:                      // 65001, UTF8
                            result = UTF8;
                            break;

                        // These are (hopefully) not very common, but also shouldn't slow us down much and make default
                        // case able to handle more code pages by calling GetEncodingCodePage
                        case CodePageNoOEM:             // 1
                        case CodePageNoMac:             // 2
                        case CodePageNoThread:          // 3
                        case CodePageNoSymbol:          // 42
                            // 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(Environment.GetResourceString(
                                "Argument_CodepageNotSupported", codepage), "codepage");
#if FEATURE_ASCII
                        // Have to do ASCII and Latin 1 first so they don't get loaded as code pages
                        case CodePageASCII:             // 20127
                            result = ASCII;
                            break;
#endif
#if FEATURE_LATIN1
                        case ISO_8859_1:                // 28591
                            result = Latin1;
                            break;
#endif                      
                        default:
                        {
#if FEATURE_CODEPAGES_FILE
                            // 1st assume its a code page.
                            result = GetEncodingCodePage(codepage);
                            if (result == null)
                                result = GetEncodingRare(codepage);
                            break;
#else
                            // Is it a valid code page?
                            if (EncodingTable.GetCodePageDataItem(codepage) == null)
                            {
                                throw new NotSupportedException(
                                    Environment.GetResourceString("NotSupported_NoCodepageData", codepage));
                            }

                            result = UTF8;
                            break;
#endif // FEATURE_CODEPAGES_FILE
                        }
                    }
                    encodings.Add(codepage, result);
                }

            }
            return result;
        }
Пример #7
0
        public override Encoding GetEncoding(int codepage)
        {
            if (codepage < 0 || codepage > 65535)
            {
                return(null);
            }

            if (codepage == 0)
            {
                // Retrieve the system default non-unicode code page if possible, or return null,
                // giving the rest of the EncodingProviders a chance to return a default.
                int systemDefaultCodePage = SystemDefaultCodePage;
                return(systemDefaultCodePage != 0 ?
                       GetEncoding(systemDefaultCodePage) :
                       null);
            }

            Encoding result = null;

            _cacheLock.EnterUpgradeableReadLock();
            try
            {
                if (_encodings.TryGetValue(codepage, out result))
                {
                    return(result);
                }

                int i = BaseCodePageEncoding.GetCodePageByteSize(codepage);

                if (i == 1)
                {
                    result = new SBCSCodePageEncoding(codepage);
                }
                else if (i == 2)
                {
                    result = new DBCSCodePageEncoding(codepage);
                }
                else
                {
                    result = GetEncodingRare(codepage);
                    if (result == null)
                    {
                        return(null);
                    }
                }

                _cacheLock.EnterWriteLock();
                try
                {
                    Encoding cachedEncoding;
                    if (_encodings.TryGetValue(codepage, out cachedEncoding))
                    {
                        return(cachedEncoding);
                    }

                    _encodings.Add(codepage, result);
                }
                finally
                {
                    _cacheLock.ExitWriteLock();
                }
            }
            finally
            {
                _cacheLock.ExitUpgradeableReadLock();
            }

            return(result);
        }
Пример #8
0
        private static Encoding GetEncodingRare(int codepage)
        {
            Encoding result = null;

            switch (codepage)
            {
            case ISCIIAssemese:
            case ISCIIBengali:
            case ISCIIDevanagari:
            case ISCIIGujarathi:
            case ISCIIKannada:
            case ISCIIMalayalam:
            case ISCIIOriya:
            case ISCIIPanjabi:
            case ISCIITamil:
            case ISCIITelugu:
                result = new ISCIIEncoding(codepage);
                break;

            // GB2312-80 uses same code page for 20936 and mac 10008
            case CodePageMacGB2312:
                //     case CodePageGB2312:
                //        result = new DBCSCodePageEncoding(codepage, EUCCN);
                result = new DBCSCodePageEncoding(CodePageMacGB2312, CodePageGB2312);
                break;

            // Mac Korean 10003 and 20949 are the same
            case CodePageMacKorean:
                result = new DBCSCodePageEncoding(CodePageMacKorean, CodePageDLLKorean);
                break;

            // GB18030 Code Pages
            case GB18030:
                result = new GB18030Encoding();
                break;

            // ISO2022 Code Pages
            case ISOKorean:
            //    case ISOSimplifiedCN
            case ChineseHZ:
            case ISO2022JP:             // JIS JP, full-width Katakana mode (no half-width Katakana)
            case ISO2022JPESC:          // JIS JP, esc sequence to do Katakana.
            case ISO2022JPSISO:         // JIS JP with Shift In/ Shift Out Katakana support
                result = new ISO2022Encoding(codepage);
                break;

            // Duplicate EUC-CN (51936) just calls a base code page 936,
            // so does ISOSimplifiedCN (50227), which's gotta be broken
            case DuplicateEUCCN:
            case ISOSimplifiedCN:
                result = new DBCSCodePageEncoding(codepage, EUCCN);        // Just maps to 936
                break;

            case EUCJP:
                result = new EUCJPEncoding();
                break;

            case EUCKR:
                result = new DBCSCodePageEncoding(codepage, CodePageDLLKorean);        // Maps to 20949
                break;

            case ISO_8859_8I:
                result = new SBCSCodePageEncoding(codepage, ISO_8859_8_Visual);            // Hebrew maps to a different code page
                break;
            }
            return(result);
        }
Пример #9
0
        public static Encoding GetEncoding(int codepage)
        {
            Encoding result = EncodingProvider.GetEncodingFromProvider(codepage);
            if (result != null)
                return result;

            //
            // NOTE: 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(
                    nameof(codepage), Environment.GetResourceString("ArgumentOutOfRange_Range",
                        0, 65535));
            }

            Contract.EndContractBlock();

            // Our Encoding

            // See if the encoding is cached in a static field.
            switch (codepage)
            {
                case CodePageDefault: return Default;            // 0
                case CodePageUnicode: return Unicode;            // 1200
                case CodePageBigEndian: return BigEndianUnicode; // 1201
                case CodePageUTF32: return UTF32;                // 12000
                case CodePageUTF32BE: return BigEndianUTF32;     // 12001
                case CodePageUTF7: return UTF7;                  // 65000
                case CodePageUTF8: return UTF8;                  // 65001
                case CodePageASCII: return ASCII;                // 20127
                case ISO_8859_1: return Latin1;                  // 28591

                // We don't allow the following special code page values that Win32 allows.
                case CodePageNoOEM:                              // 1 CP_OEMCP
                case CodePageNoMac:                              // 2 CP_MACCP
                case CodePageNoThread:                           // 3 CP_THREAD_ACP
                case CodePageNoSymbol:                           // 42 CP_SYMBOL
                    throw new ArgumentException(Environment.GetResourceString(
                        "Argument_CodepageNotSupported", codepage), nameof(codepage));
            }

#if FEATURE_CODEPAGES_FILE
            object key = codepage; // Box once

            // See if we have a hash table with our encoding in it already.
            if (encodings != null) {
                result = (Encoding)encodings[key];
            }

            if (result == null)
            {
                // Don't conflict with ourselves
                lock (InternalSyncObject)
                {
                    // Need a new hash table
                    // in case another thread beat us to creating the Dictionary
                    if (encodings == null) {
                        encodings = new Hashtable();
                    }

                    // Double check that we don't have one in the table (in case another thread beat us here)
                    if ((result = (Encoding)encodings[key]) != null)
                        return result;

                    if (codepage == CodePageWindows1252)
                    {
                        result = new SBCSCodePageEncoding(codepage);
                    }
                    else
                    {
                        result = GetEncodingCodePage(codepage) ?? GetEncodingRare(codepage);
                    }

                    Debug.Assert(result != null, "result != null");

                    encodings.Add(key, result);
                }
            }
            return result;
#else
            // Is it a valid code page?
            if (EncodingTable.GetCodePageDataItem(codepage) == null)
            {
                throw new NotSupportedException(
                    Environment.GetResourceString("NotSupported_NoCodepageData", codepage));
            }

            return UTF8;
#endif // FEATURE_CODEPAGES_FILE
        }
Пример #10
0
        private static Encoding CreateDefaultEncoding()
        {
            // defaultEncoding should be null if we get here, but we can't
            // assert that in case another thread beat us to the initialization

            Encoding enc;

#if FEATURE_CODEPAGES_FILE            
            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 SBCSCodePageEncoding(codePage);
            else
                enc = GetEncoding(codePage);
#else // FEATURE_CODEPAGES_FILE            

            // For silverlight we use UTF8 since ANSI isn't available
            enc = UTF8;

#endif // FEATURE_CODEPAGES_FILE

            // This method should only ever return one Encoding instance
            return Interlocked.CompareExchange(ref defaultEncoding, enc, null) ?? enc;
        }
Пример #11
0
        private 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 SBCSCodePageEncoding(codePage);
            else
                enc = GetEncoding(codePage);

            return (enc);
        }