FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix Default/OEM encoding behavior PowerShell Core · PowerShell/PowerShell@9580b4a · GitHub

Commit 9580b4a

Browse files
committed
Fix Default/OEM encoding behavior PowerShell Core
1 parent 9efbf56 commit 9580b4a

3 files changed

Lines changed: 37 additions & 11 deletions

File tree

‎src/System.Management.Automation/namespaces/FileSystemProvider.cs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7569,8 +7569,7 @@ private static Encoding GetEncodingFromEnum(FileSystemCmdletProviderEncoding typ
75697569

75707570
case FileSystemCmdletProviderEncoding.Oem:
75717571
{
7572-
uint oemCP = NativeMethods.GetOEMCP();
7573-
encoding = System.Text.Encoding.GetEncoding((int)oemCP);
7572+
encoding = ClrFacade.GetOEMEncoding();
75747573
}
75757574
break;
75767575

‎src/System.Management.Automation/utils/ClrFacade.cs‎

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -530,17 +530,26 @@ internal static object[] GetCustomAttributes<T>(Assembly assembly)
530530
#region Encoding
531531

532532
/// <summary>
533-
/// Facade for Encoding.Default
533+
/// Facade for getting Encoding.Default
534534
/// </summary>
535535
internal static Encoding GetDefaultEncoding()
536536
{
537537
if (s_defaultEncoding == null)
538538
{
539-
#if CORECLR // Encoding.Default is not in CoreCLR
540-
// As suggested by CoreCLR team (tarekms), use latin1 (ISO-8859-1, CodePage 28591) as the default encoding.
541-
// We will revisit this if it causes any failures when running tests on Core PS.
542-
s_defaultEncoding = Encoding.GetEncoding(28591);
543-
#else
539+
#if CORECLR
540+
541+
#if UNUX // PowerShell Core on Unix
542+
s_defaultEncoding = Encoding.GetEncoding(65001);
543+
#else // PowerShell Core on Windows
544+
uint aCp = NativeMethods.GetACP();
545+
if (s_oemEncoding == null)
546+
{
547+
Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
548+
}
549+
s_defaultEncoding = Encoding.GetEncoding((int)aCp);
550+
#endif
551+
552+
#else // Windows PowerShell
544553
s_defaultEncoding = Encoding.Default;
545554
#endif
546555
}
@@ -555,10 +564,20 @@ internal static Encoding GetOEMEncoding()
555564
{
556565
if (s_oemEncoding == null)
557566
{
558-
#if CORECLR // The OEM code page '437' is not supported by CoreCLR.
559-
// Use the default encoding (ISO-8859-1, CodePage 28591) as the OEM encoding in OneCore powershell.
567+
#if CORECLR
568+
569+
#if UNUX // PowerShell Core on Unix
560570
s_oemEncoding = GetDefaultEncoding();
561-
#else
571+
#else // PowerShell Core on Windows
572+
uint oemCp = NativeMethods.GetOEMCP();
573+
if (s_defaultEncoding == null)
574+
{
575+
Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
576+
}
577+
s_oemEncoding = Encoding.GetEncoding((int)oemCp);
578+
#endif
579+
580+
#else // Windows PowerShell
562581
uint oemCp = NativeMethods.GetOEMCP();
563582
s_oemEncoding = Encoding.GetEncoding((int)oemCp);
564583
#endif
@@ -1033,6 +1052,12 @@ private static class NativeMethods
10331052
[DllImport(PinvokeDllNames.GetOEMCPDllName, SetLastError = false, CharSet = CharSet.Unicode)]
10341053
internal static extern uint GetOEMCP();
10351054

1055+
/// <summary>
1056+
/// Pinvoke for GetACP to get the Windows operating system code page.
1057+
/// </summary>
1058+
[DllImport(PinvokeDllNames.GetACPDllName, SetLastError = false, CharSet = CharSet.Unicode)]
1059+
internal static extern uint GetACP();
1060+
10361061
public const int S_OK = 0x00000000;
10371062

10381063
/// <summary>

‎src/System.Management.Automation/utils/PInvokeDllNames.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal static class PinvokeDllNames
1717
internal const string QueryDosDeviceDllName = "api-ms-win-core-file-l1-1-0.dll"; /*1*/
1818
internal const string CreateSymbolicLinkDllName = "api-ms-win-core-file-l2-1-0.dll"; /*2*/
1919
internal const string GetOEMCPDllName = "api-ms-win-core-localization-l1-2-0.dll"; /*3*/
20+
internal const string GetACPDllName = "api-ms-win-core-localization-l1-2-0.dll"; /*3*/
2021
internal const string DeviceIoControlDllName = "api-ms-win-core-io-l1-1-0.dll"; /*4*/
2122
internal const string CreateFileDllName = "api-ms-win-core-file-l1-1-0.dll"; /*5*/
2223
internal const string DeleteFileDllName = "api-ms-win-core-file-l1-1-0.dll"; /*6*/
@@ -139,6 +140,7 @@ internal static class PinvokeDllNames
139140
internal const string QueryDosDeviceDllName = "kernel32.dll"; /*1*/
140141
internal const string CreateSymbolicLinkDllName = "kernel32.dll"; /*2*/
141142
internal const string GetOEMCPDllName = "kernel32.dll"; /*3*/
143+
internal const string GetACPDllName = "kernel32.dll"; /*3*/
142144
internal const string DeviceIoControlDllName = "kernel32.dll"; /*4*/
143145
internal const string CreateFileDllName = "kernel32.dll"; /*5*/
144146
internal const string DeleteFileDllName = "kernel32.dll"; /*6*/

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL