1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System.Collections.Generic;
6 using System.Diagnostics;
7 using System.Globalization;
12 internal static class EncodingTable
14 // Return a list of all EncodingInfo objects describing all of our encodings
15 internal static EncodingInfo[] GetEncodings()
17 EncodingInfo[] arrayEncodingInfo = new EncodingInfo[s_encodingDataTableItems.Length];
19 for (int i = 0; i < s_encodingDataTableItems.Length; i++)
21 CodePageDataItem dataItem = s_encodingDataTableItems[i];
23 arrayEncodingInfo[i] = new EncodingInfo(dataItem.CodePage, dataItem.WebName,
24 SR.GetResourceString(dataItem.DisplayNameResourceKey));
27 return arrayEncodingInfo;
30 internal static int GetCodePageFromName(string name)
34 throw new ArgumentNullException(nameof(name));
38 if (s_encodingDataTable.TryGetValue(name, out codePage))
43 // The encoding name is not valid.
44 throw new ArgumentException(
46 CultureInfo.CurrentCulture,
47 SR.Argument_EncodingNotSupported, name), nameof(name));
50 internal static CodePageDataItem GetCodePageDataItem(int codepage)
52 CodePageDataItem item;
57 item = s_encodingDataTableItems[0];
60 item = s_encodingDataTableItems[1];
63 item = s_encodingDataTableItems[2];
66 item = s_encodingDataTableItems[3];
69 item = s_encodingDataTableItems[4];
72 item = s_encodingDataTableItems[5];
75 item = s_encodingDataTableItems[6];
78 item = s_encodingDataTableItems[7];
85 Debug.Assert(item == null || item.CodePage == codepage, "item.CodePage needs to equal the specified codepage");
92 static EncodingTable()
95 s_encodingDataTable.Count == EncodingTableCapacity,
96 string.Format(CultureInfo.InvariantCulture,
97 "EncodingTable s_encodingDataTable's initial capacity (EncodingTableCapacity) is incorrect.{0}Expected (s_encodingDataTable.Count): {1}, Actual (EncodingTableCapacity): {2}",
99 s_encodingDataTable.Count,
100 EncodingTableCapacity));
104 // NOTE: the following two lists were taken from ~\src\classlibnative\nls\encodingdata.cpp
105 // and should be kept in sync with those lists
107 private const int EncodingTableCapacity = 42;
108 private readonly static Dictionary<string, ushort> s_encodingDataTable =
109 new Dictionary<string, ushort>(EncodingTableCapacity, StringComparer.OrdinalIgnoreCase)
111 { "ANSI_X3.4-1968", 20127 },
112 { "ANSI_X3.4-1986", 20127 },
116 { "csASCII", 20127 },
117 { "csISOLatin1", 28591 },
118 { "csUnicode11UTF7", 65000 },
121 { "ISO-10646-UCS-2", 1200 },
122 { "iso-8859-1", 28591 },
123 { "iso-ir-100", 28591 },
124 { "iso-ir-6", 20127 },
125 { "ISO646-US", 20127 },
126 { "iso8859-1", 28591 },
127 { "ISO_646.irv:1991", 20127 },
128 { "iso_8859-1", 28591 },
129 { "iso_8859-1:1987", 28591 },
134 { "unicode-1-1-utf-7", 65000 },
135 { "unicode-1-1-utf-8", 65001 },
136 { "unicode-2-0-utf-7", 65000 },
137 { "unicode-2-0-utf-8", 65001 },
138 // People get confused about the FFFE here. We can't change this because it'd break existing apps.
139 // This has been this way for a long time, including in Mlang.
140 // Big Endian, BOM seems backwards, think of the BOM in little endian order.
141 { "unicodeFFFE", 1201},
143 { "us-ascii", 20127 },
148 { "UTF-32BE", 12001 },
149 { "UTF-32LE", 12000 },
152 { "x-unicode-1-1-utf-7", 65000 },
153 { "x-unicode-1-1-utf-8", 65001 },
154 { "x-unicode-2-0-utf-7", 65000 },
155 { "x-unicode-2-0-utf-8", 65001 },
158 // redeclaring these constants here for readability below
159 private const uint MIMECONTF_MAILNEWS = Encoding.MIMECONTF_MAILNEWS;
160 private const uint MIMECONTF_BROWSER = Encoding.MIMECONTF_BROWSER;
161 private const uint MIMECONTF_SAVABLE_MAILNEWS = Encoding.MIMECONTF_SAVABLE_MAILNEWS;
162 private const uint MIMECONTF_SAVABLE_BROWSER = Encoding.MIMECONTF_SAVABLE_BROWSER;
164 // keep this array sorted by code page, so the order is consistent for GetEncodings()
165 // Remember to update GetCodePageDataItem() if this list is updated
166 private readonly static CodePageDataItem[] s_encodingDataTableItems = new[]
168 new CodePageDataItem(1200, 1200, "utf-16", MIMECONTF_SAVABLE_BROWSER), // "Unicode"
169 new CodePageDataItem(1201, 1200, "utf-16BE", 0), // Big Endian, old FFFE BOM seems backwards, think of the BOM in little endian order.
170 new CodePageDataItem(12000, 1200, "utf-32", 0), // "Unicode (UTF-32)"
171 new CodePageDataItem(12001, 1200, "utf-32BE", 0), // "Unicode (UTF-32 Big Endian)"
172 new CodePageDataItem(20127, 1252, "us-ascii", MIMECONTF_MAILNEWS | MIMECONTF_SAVABLE_MAILNEWS), // "US-ASCII"
173 new CodePageDataItem(28591, 1252, "iso-8859-1", MIMECONTF_MAILNEWS | MIMECONTF_BROWSER | MIMECONTF_SAVABLE_MAILNEWS | MIMECONTF_SAVABLE_BROWSER), // "Western European (ISO)"
174 new CodePageDataItem(65000, 1200, "utf-7", MIMECONTF_MAILNEWS | MIMECONTF_SAVABLE_MAILNEWS), // "Unicode (UTF-7)"
175 new CodePageDataItem(65001, 1200, "utf-8", MIMECONTF_MAILNEWS | MIMECONTF_BROWSER | MIMECONTF_SAVABLE_MAILNEWS | MIMECONTF_SAVABLE_BROWSER), // "Unicode (UTF-8)"