<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />

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.Runtime.InteropServices; 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(); public static EncodingProvider Instance => s_singleton; 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 num = 0; return null; } [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 static ref T GetNonNullPinnableReference<T>(T[] array) where T : struct { return ref MemoryMarshal.GetArrayDataReference(array); } public override IEnumerable<EncodingInfo> GetEncodings() { return BaseCodePageEncoding.GetEncodings(this); } } }