Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
OSEncoding.cs
Go to the documentation of this file.
1namespace System.Text;
2
3internal sealed class OSEncoding : Encoding
4{
5 private readonly int _codePage;
6
7 private string _encodingName;
8
9 public override string EncodingName
10 {
11 get
12 {
13 if (_encodingName == null)
14 {
15 _encodingName = "Codepage - " + _codePage;
16 }
17 return _encodingName;
18 }
19 }
20
21 public override string WebName => EncodingName;
22
23 internal OSEncoding(int codePage)
24 : base(codePage)
25 {
26 _codePage = codePage;
27 }
28
29 public unsafe override int GetByteCount(char[] chars, int index, int count)
30 {
31 if (chars == null)
32 {
34 }
35 if (index < 0 || count < 0)
36 {
38 }
39 if (chars.Length - index < count)
40 {
42 }
43 if (count == 0)
44 {
45 return 0;
46 }
47 fixed (char* ptr = chars)
48 {
49 return WideCharToMultiByte(_codePage, ptr + index, count, null, 0);
50 }
51 }
52
53 public unsafe override int GetByteCount(string s)
54 {
55 if (s == null)
56 {
57 throw new ArgumentNullException("s");
58 }
59 if (s.Length == 0)
60 {
61 return 0;
62 }
63 fixed (char* pChars = s)
64 {
65 return WideCharToMultiByte(_codePage, pChars, s.Length, null, 0);
66 }
67 }
68
69 public unsafe override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
70 {
71 if (s == null || bytes == null)
72 {
73 throw new ArgumentNullException((s == null) ? "s" : "bytes", System.SR.ArgumentNull_Array);
74 }
75 if (charIndex < 0 || charCount < 0)
76 {
77 throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", System.SR.ArgumentOutOfRange_NeedNonNegNum);
78 }
79 if (s.Length - charIndex < charCount)
80 {
82 }
83 if (byteIndex < 0 || byteIndex > bytes.Length)
84 {
86 }
87 if (charCount == 0)
88 {
89 return 0;
90 }
91 if (bytes.Length == 0)
92 {
94 }
95 fixed (char* ptr = s)
96 {
97 fixed (byte* ptr2 = &bytes[0])
98 {
100 }
101 }
102 }
103
104 public unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
105 {
106 if (chars == null || bytes == null)
107 {
108 throw new ArgumentNullException((chars == null) ? "chars" : "bytes", System.SR.ArgumentNull_Array);
109 }
110 if (charIndex < 0 || charCount < 0)
111 {
112 throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", System.SR.ArgumentOutOfRange_NeedNonNegNum);
113 }
114 if (chars.Length - charIndex < charCount)
115 {
117 }
118 if (byteIndex < 0 || byteIndex > bytes.Length)
119 {
121 }
122 if (charCount == 0)
123 {
124 return 0;
125 }
126 if (bytes.Length == 0)
127 {
129 }
130 fixed (char* ptr = chars)
131 {
132 fixed (byte* ptr2 = &bytes[0])
133 {
134 return WideCharToMultiByte(_codePage, ptr + charIndex, charCount, ptr2 + byteIndex, bytes.Length - byteIndex);
135 }
136 }
137 }
138
139 public unsafe override int GetCharCount(byte[] bytes, int index, int count)
140 {
141 if (bytes == null)
142 {
144 }
145 if (index < 0 || count < 0)
146 {
147 throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", System.SR.ArgumentOutOfRange_NeedNonNegNum);
148 }
149 if (bytes.Length - index < count)
150 {
152 }
153 if (count == 0)
154 {
155 return 0;
156 }
157 fixed (byte* ptr = bytes)
158 {
159 return MultiByteToWideChar(_codePage, ptr + index, count, null, 0);
160 }
161 }
162
163 public unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
164 {
165 if (bytes == null || chars == null)
166 {
167 throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", System.SR.ArgumentNull_Array);
168 }
169 if (byteIndex < 0 || byteCount < 0)
170 {
171 throw new ArgumentOutOfRangeException((byteIndex < 0) ? "byteIndex" : "byteCount", System.SR.ArgumentOutOfRange_NeedNonNegNum);
172 }
173 if (bytes.Length - byteIndex < byteCount)
174 {
176 }
177 if (charIndex < 0 || charIndex > chars.Length)
178 {
180 }
181 if (byteCount == 0)
182 {
183 return 0;
184 }
185 if (chars.Length == 0)
186 {
188 }
189 fixed (byte* ptr = bytes)
190 {
191 fixed (char* ptr2 = &chars[0])
192 {
193 return MultiByteToWideChar(_codePage, ptr + byteIndex, byteCount, ptr2 + charIndex, chars.Length - charIndex);
194 }
195 }
196 }
197
198 public override int GetMaxByteCount(int charCount)
199 {
200 if (charCount < 0)
201 {
203 }
204 long num = (long)charCount * 14L;
205 if (num > int.MaxValue)
206 {
208 }
209 return (int)num;
210 }
211
212 public override int GetMaxCharCount(int byteCount)
213 {
214 if (byteCount < 0)
215 {
217 }
218 long num = byteCount * 4;
219 if (num > int.MaxValue)
220 {
222 }
223 return (int)num;
224 }
225
226 public override Encoder GetEncoder()
227 {
228 return new OSEncoder(this);
229 }
230
231 public override Decoder GetDecoder()
232 {
233 switch (CodePage)
234 {
235 case 932:
236 case 936:
237 case 949:
238 case 950:
239 case 1361:
240 case 10001:
241 case 10002:
242 case 10003:
243 case 10008:
244 case 20000:
245 case 20001:
246 case 20002:
247 case 20003:
248 case 20004:
249 case 20005:
250 case 20261:
251 case 20932:
252 case 20936:
253 case 51949:
254 return new DecoderDBCS(this);
255 default:
256 return base.GetDecoder();
257 }
258 }
259
260 internal unsafe static int WideCharToMultiByte(int codePage, char* pChars, int count, byte* pBytes, int byteCount)
261 {
262 int num = global::Interop.Kernel32.WideCharToMultiByte((uint)codePage, 0u, pChars, count, pBytes, byteCount, IntPtr.Zero, IntPtr.Zero);
263 if (num <= 0)
264 {
266 }
267 return num;
268 }
269
270 internal unsafe static int MultiByteToWideChar(int codePage, byte* pBytes, int byteCount, char* pChars, int count)
271 {
272 int num = global::Interop.Kernel32.MultiByteToWideChar((uint)codePage, 0u, pBytes, byteCount, pChars, count);
273 if (num <= 0)
274 {
276 }
277 return num;
278 }
279}
static string ArgumentOutOfRange_Index
Definition SR.cs:30
static string Argument_EncodingConversionOverflowBytes
Definition SR.cs:84
static string Argument_InvalidCharSequenceNoIndex
Definition SR.cs:92
static string ArgumentOutOfRange_IndexCount
Definition SR.cs:80
static string ArgumentOutOfRange_GetCharCountOverflow
Definition SR.cs:90
static string Argument_EncodingConversionOverflowChars
Definition SR.cs:86
static string ArgumentOutOfRange_GetByteCountOverflow
Definition SR.cs:88
static string ArgumentOutOfRange_IndexCountBuffer
Definition SR.cs:78
static string ArgumentNull_Array
Definition SR.cs:24
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
Definition SR.cs:7
virtual int CodePage
Definition Encoding.cs:515
override Decoder GetDecoder()
unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
unsafe override int GetByteCount(string s)
Definition OSEncoding.cs:53
readonly int _codePage
Definition OSEncoding.cs:5
override int GetMaxCharCount(int byteCount)
override int GetMaxByteCount(int charCount)
unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
static unsafe int MultiByteToWideChar(int codePage, byte *pBytes, int byteCount, char *pChars, int count)
unsafe override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
Definition OSEncoding.cs:69
static unsafe int WideCharToMultiByte(int codePage, char *pChars, int count, byte *pBytes, int byteCount)
unsafe override int GetByteCount(char[] chars, int index, int count)
Definition OSEncoding.cs:29
override Encoder GetEncoder()
unsafe override int GetCharCount(byte[] bytes, int index, int count)
override string WebName
Definition OSEncoding.cs:21
override string EncodingName
Definition OSEncoding.cs:10
OSEncoding(int codePage)
Definition OSEncoding.cs:23
static readonly IntPtr Zero
Definition IntPtr.cs:18