CodePagesEncodingProvider
Provides access to an encoding provider for code pages that otherwise are available only in the desktop .NET Framework.
                using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
namespace System.Text
{
    [System.Runtime.CompilerServices.NullableContext(1)]
    [System.Runtime.CompilerServices.Nullable(0)]
    public sealed class CodePagesEncodingProvider : EncodingProvider
    {
        private static readonly EncodingProvider s_singleton = new CodePagesEncodingProvider();
        private readonly Dictionary<int, Encoding> _encodings = new Dictionary<int, Encoding>();
        private readonly ReaderWriterLockSlim _cacheLock = new ReaderWriterLockSlim();
        private const int ISCIIAssemese = 57006;
        private const int ISCIIBengali = 57003;
        private const int ISCIIDevanagari = 57002;
        private const int ISCIIGujarathi = 57010;
        private const int ISCIIKannada = 57008;
        private const int ISCIIMalayalam = 57009;
        private const int ISCIIOriya = 57007;
        private const int ISCIIPanjabi = 57011;
        private const int ISCIITamil = 57004;
        private const int ISCIITelugu = 57005;
        private const int ISOKorean = 50225;
        private const int ChineseHZ = 52936;
        private const int ISO2022JP = 50220;
        private const int ISO2022JPESC = 50221;
        private const int ISO2022JPSISO = 50222;
        private const int ISOSimplifiedCN = 50227;
        private const int EUCJP = 51932;
        private const int CodePageMacGB2312 = 10008;
        private const int CodePageMacKorean = 10003;
        private const int CodePageGB2312 = 20936;
        private const int CodePageDLLKorean = 20949;
        private const int GB18030 = 54936;
        private const int DuplicateEUCCN = 51936;
        private const int EUCKR = 51949;
        private const int EUCCN = 936;
        private const int ISO_8859_8I = 38598;
        private const int ISO_8859_8_Visual = 28598;
        public static EncodingProvider Instance => s_singleton;
        private static int SystemDefaultCodePage {
            get {
                if (!global::Interop.Kernel32.TryGetACPCodePage(out int codePage))
                    return 0;
                return codePage;
            }
        }
        internal CodePagesEncodingProvider()
        {
        }
        [System.Runtime.CompilerServices.NullableContext(2)]
        public override Encoding GetEncoding(int codepage)
        {
            if (codepage < 0 || codepage > 65535)
                return null;
            if (codepage != 0) {
                Encoding value = null;
                _cacheLock.EnterUpgradeableReadLock();
                try {
                    if (!_encodings.TryGetValue(codepage, out value)) {
                        switch (BaseCodePageEncoding.GetCodePageByteSize(codepage)) {
                        case 1:
                            value = new SBCSCodePageEncoding(codepage);
                            break;
                        case 2:
                            value = new DBCSCodePageEncoding(codepage);
                            break;
                        default:
                            value = GetEncodingRare(codepage);
                            if (value == null)
                                return null;
                            break;
                        }
                        _cacheLock.EnterWriteLock();
                        try {
                            if (!_encodings.TryGetValue(codepage, out Encoding value2)) {
                                _encodings.Add(codepage, value);
                                return value;
                            }
                            return value2;
                        } finally {
                            _cacheLock.ExitWriteLock();
                        }
                    }
                    return value;
                } finally {
                    _cacheLock.ExitUpgradeableReadLock();
                }
            }
            int systemDefaultCodePage = SystemDefaultCodePage;
            if (systemDefaultCodePage == 0)
                return null;
            return GetEncoding(systemDefaultCodePage);
        }
        [return: System.Runtime.CompilerServices.Nullable(2)]
        public override Encoding GetEncoding(string name)
        {
            int codePageFromName = System.Text.EncodingTable.GetCodePageFromName(name);
            if (codePageFromName == 0)
                return null;
            return GetEncoding(codePageFromName);
        }
        private static Encoding GetEncodingRare(int codepage)
        {
            Encoding result = null;
            if (codepage <= 51932) {
                switch (codepage) {
                case 10008:
                    result = new DBCSCodePageEncoding(10008, 20936);
                    goto IL_011a;
                case 10003:
                    result = new DBCSCodePageEncoding(10003, 20949);
                    goto IL_011a;
                case 50220:
                case 50221:
                case 50222:
                case 50225:
                    break;
                case 50227:
                    goto IL_00ea;
                case 51932:
                    result = new EUCJPEncoding();
                    goto IL_011a;
                case 38598:
                    result = new SBCSCodePageEncoding(codepage, 28598);
                    goto IL_011a;
                default:
                    goto IL_011a;
                }
                goto IL_00e1;
            }
            if (codepage <= 51949) {
                switch (codepage) {
                case 51936:
                    break;
                case 51949:
                    result = new DBCSCodePageEncoding(codepage, 20949);
                    goto IL_011a;
                default:
                    goto IL_011a;
                }
                goto IL_00ea;
            }
            switch (codepage) {
            case 54936:
                goto IL_00d9;
            case 52936:
                goto IL_00e1;
            }
            if ((uint)(codepage - 57002) <= 9)
                result = new ISCIIEncoding(codepage);
            goto IL_011a;
            IL_00e1:
            result = new ISO2022Encoding(codepage);
            goto IL_011a;
            IL_00ea:
            result = new DBCSCodePageEncoding(codepage, 936);
            goto IL_011a;
            IL_011a:
            return result;
            IL_00d9:
            result = new GB18030Encoding();
            goto IL_011a;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        internal unsafe static ref T GetNonNullPinnableReference<T>(T[] array) where T : struct
        {
            if (array.Length == 0)
                return ref Unsafe.AsRef<T>((void*)1);
            return ref array[0];
        }
    }
}