Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BaseCodePageEncoding.cs
Go to the documentation of this file.
2using System.IO;
7
8namespace System.Text;
9
11{
12 [StructLayout(LayoutKind.Explicit)]
13 internal struct CodePageDataFileHeader
14 {
15 [FieldOffset(0)]
16 internal char TableName;
17
18 [FieldOffset(32)]
19 internal ushort Version;
20
21 [FieldOffset(40)]
22 internal short CodePageCount;
23
24 [FieldOffset(42)]
25 internal short unused1;
26 }
27
28 [StructLayout(LayoutKind.Explicit, Pack = 2)]
29 internal struct CodePageIndex
30 {
31 [FieldOffset(0)]
32 internal char CodePageName;
33
34 [FieldOffset(32)]
35 internal short CodePage;
36
37 [FieldOffset(34)]
38 internal short ByteCount;
39
40 [FieldOffset(36)]
41 internal int Offset;
42 }
43
44 [StructLayout(LayoutKind.Explicit)]
45 internal struct CodePageHeader
46 {
47 [FieldOffset(0)]
48 internal char CodePageName;
49
50 [FieldOffset(32)]
51 internal ushort VersionMajor;
52
53 [FieldOffset(34)]
54 internal ushort VersionMinor;
55
56 [FieldOffset(36)]
57 internal ushort VersionRevision;
58
59 [FieldOffset(38)]
60 internal ushort VersionBuild;
61
62 [FieldOffset(40)]
63 internal short CodePage;
64
65 [FieldOffset(42)]
66 internal short ByteCount;
67
68 [FieldOffset(44)]
69 internal char UnicodeReplace;
70
71 [FieldOffset(46)]
72 internal ushort ByteReplace;
73 }
74
75 protected int dataTableCodePage;
76
77 protected int iExtraBytes;
78
79 protected char[] arrayUnicodeBestFit;
80
81 protected char[] arrayBytesBestFit;
82
83 private static readonly byte[] s_codePagesDataHeader = new byte[44];
84
85 protected static Stream s_codePagesEncodingDataStream = GetEncodingDataStream("codepages.nlp");
86
87 protected static readonly object s_streamLock = new object();
88
89 protected byte[] m_codePageHeader = new byte[48];
90
91 protected int m_firstDataWordOffset;
92
93 protected int m_dataSize;
94
96
105
112
117
119 {
122 {
123 return;
124 }
125 fixed (byte* ptr = &codePageDataFileHeader[0])
126 {
128 char* ptr3 = &ptr2->TableName;
129 for (int i = 0; i < 16; i++)
130 {
132 }
133 ushort* ptr4 = &ptr2->Version;
134 for (int j = 0; j < 4; j++)
135 {
137 }
138 ptr2->CodePageCount = BinaryPrimitives.ReverseEndianness(ptr2->CodePageCount);
139 }
140 }
141
142 internal unsafe static void ReadCodePageIndex(Stream stream, byte[] codePageIndex)
143 {
144 stream.Read(codePageIndex, 0, codePageIndex.Length);
146 {
147 return;
148 }
149 fixed (byte* ptr = &codePageIndex[0])
150 {
152 char* ptr3 = &ptr2->CodePageName;
153 for (int i = 0; i < 16; i++)
154 {
156 }
157 ptr2->CodePage = BinaryPrimitives.ReverseEndianness(ptr2->CodePage);
158 ptr2->ByteCount = BinaryPrimitives.ReverseEndianness(ptr2->ByteCount);
159 ptr2->Offset = BinaryPrimitives.ReverseEndianness(ptr2->Offset);
160 }
161 }
162
163 internal unsafe static void ReadCodePageHeader(Stream stream, byte[] codePageHeader)
164 {
165 stream.Read(codePageHeader, 0, codePageHeader.Length);
167 {
168 return;
169 }
170 fixed (byte* ptr = &codePageHeader[0])
171 {
173 char* ptr3 = &ptr2->CodePageName;
174 for (int i = 0; i < 16; i++)
175 {
177 }
178 ptr2->VersionMajor = BinaryPrimitives.ReverseEndianness(ptr2->VersionMajor);
179 ptr2->VersionMinor = BinaryPrimitives.ReverseEndianness(ptr2->VersionMinor);
180 ptr2->VersionRevision = BinaryPrimitives.ReverseEndianness(ptr2->VersionRevision);
181 ptr2->VersionBuild = BinaryPrimitives.ReverseEndianness(ptr2->VersionBuild);
182 ptr2->CodePage = BinaryPrimitives.ReverseEndianness(ptr2->CodePage);
183 ptr2->ByteCount = BinaryPrimitives.ReverseEndianness(ptr2->ByteCount);
184 ptr2->UnicodeReplace = (char)BinaryPrimitives.ReverseEndianness(ptr2->UnicodeReplace);
185 ptr2->ByteReplace = BinaryPrimitives.ReverseEndianness(ptr2->ByteReplace);
186 }
187 }
188
199
208
209 private unsafe bool FindCodePage(int codePage)
210 {
211 byte[] array = new byte[sizeof(CodePageIndex)];
213 {
215 int codePageCount;
216 fixed (byte* ptr = &s_codePagesDataHeader[0])
217 {
220 }
221 fixed (byte* ptr3 = &array[0])
222 {
224 for (int i = 0; i < codePageCount; i++)
225 {
227 if (ptr4->CodePage == codePage)
228 {
229 long position = s_codePagesEncodingDataStream.Position;
230 s_codePagesEncodingDataStream.Seek(ptr4->Offset, SeekOrigin.Begin);
233 if (i == codePageCount - 1)
234 {
235 m_dataSize = (int)(s_codePagesEncodingDataStream.Length - ptr4->Offset - m_codePageHeader.Length);
236 }
237 else
238 {
239 s_codePagesEncodingDataStream.Seek(position, SeekOrigin.Begin);
240 int offset = ptr4->Offset;
242 m_dataSize = ptr4->Offset - offset - m_codePageHeader.Length;
243 }
244 return true;
245 }
246 }
247 }
248 }
249 return false;
250 }
251
252 internal unsafe static int GetCodePageByteSize(int codePage)
253 {
254 byte[] array = new byte[sizeof(CodePageIndex)];
256 {
258 int codePageCount;
259 fixed (byte* ptr = &s_codePagesDataHeader[0])
260 {
263 }
264 fixed (byte* ptr3 = &array[0])
265 {
267 for (int i = 0; i < codePageCount; i++)
268 {
270 if (ptr4->CodePage == codePage)
271 {
272 return ptr4->ByteCount;
273 }
274 }
275 }
276 }
277 return 0;
278 }
279
280 protected abstract void LoadManagedCodePage();
281
282 protected unsafe byte* GetNativeMemory(int iSize)
283 {
284 if (safeNativeMemoryHandle == null)
285 {
286 byte* ptr = (byte*)(void*)Marshal.AllocHGlobal(iSize);
288 }
289 return (byte*)(void*)safeNativeMemoryHandle.DangerousGetHandle();
290 }
291
292 protected abstract void ReadBestFitTable();
293
295 {
296 if (arrayUnicodeBestFit == null)
297 {
299 }
300 return arrayUnicodeBestFit;
301 }
302
304 {
305 if (arrayBytesBestFit == null)
306 {
308 }
309 return arrayBytesBestFit;
310 }
311
312 internal void CheckMemorySection()
313 {
315 {
317 }
318 }
319
321 {
322 stream.Read(codePageIndex);
324 {
325 return;
326 }
327 fixed (byte* ptr = &codePageIndex[0])
328 {
330 char* ptr3 = &ptr2->CodePageName;
331 for (int i = 0; i < 16; i++)
332 {
334 }
335 ptr2->CodePage = BinaryPrimitives.ReverseEndianness(ptr2->CodePage);
336 ptr2->ByteCount = BinaryPrimitives.ReverseEndianness(ptr2->ByteCount);
337 ptr2->Offset = BinaryPrimitives.ReverseEndianness(ptr2->Offset);
338 }
339 }
340
342 {
344 {
346 int codePageCount;
347 fixed (byte* ptr = &s_codePagesDataHeader[0])
348 {
351 }
355 for (int i = 0; i < codePageCount; i++)
356 {
358 string text = codePageIndex.CodePage switch
359 {
360 950 => "big5",
361 10002 => "x-mac-chinesetrad",
362 20833 => "x-ebcdic-koreanextended",
363 _ => new string(&codePageIndex.CodePageName),
364 };
366 string text2 = null;
367 if (localizedEncodingNameResource != null && localizedEncodingNameResource.StartsWith("Globalization_cp_", StringComparison.OrdinalIgnoreCase))
368 {
370 }
371 array[i] = new EncodingInfo(provider, codePageIndex.CodePage, text, text2 ?? text);
372 }
373 return array;
374 }
375 }
376}
static readonly bool IsLittleEndian
static sbyte ReverseEndianness(sbyte value)
static IntPtr AllocHGlobal(int cb)
Definition Marshal.cs:625
static string NotSupported_NoCodepageData
Definition SR.cs:1688
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string GetResourceString(string resourceKey)
Definition SR.cs:101
Definition SR.cs:7
BaseCodePageEncoding(int codepage, int dataCodePage, EncoderFallback enc, DecoderFallback dec)
BaseCodePageEncoding(int codepage, int dataCodePage)
static unsafe EncodingInfo[] GetEncodings(CodePagesEncodingProvider provider)
static readonly byte[] s_codePagesDataHeader
static unsafe void ReadCodePageHeader(Stream stream, byte[] codePageHeader)
static unsafe void ReadCodePageIndex(Stream stream, byte[] codePageIndex)
static unsafe void ReadCodePageDataFileHeader(Stream stream, byte[] codePageDataFileHeader)
static unsafe void ReadCodePageIndex(Stream stream, Span< byte > codePageIndex)
unsafe byte * GetNativeMemory(int iSize)
static unsafe int GetCodePageByteSize(int codePage)
unsafe bool FindCodePage(int codePage)
static Stream GetEncodingDataStream(string tableName)
static string GetLocalizedEncodingNameResource(int codePage)
virtual int CodePage
Definition Encoding.cs:515
void GetObjectData(SerializationInfo info, StreamingContext context)
static readonly IntPtr Zero
Definition IntPtr.cs:18