Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
GB18030Encoding.cs
Go to the documentation of this file.
1namespace System.Text;
2
3internal sealed class GB18030Encoding : DBCSCodePageEncoding
4{
5 internal sealed class GB18030Decoder : System.Text.DecoderNLS
6 {
7 internal short bLeftOver1 = -1;
8
9 internal short bLeftOver2 = -1;
10
11 internal short bLeftOver3 = -1;
12
13 internal short bLeftOver4 = -1;
14
15 internal override bool HasState => bLeftOver1 >= 0;
16
17 internal GB18030Decoder(EncodingNLS encoding)
18 : base(encoding)
19 {
20 }
21
22 public override void Reset()
23 {
24 bLeftOver1 = -1;
25 bLeftOver2 = -1;
26 bLeftOver3 = -1;
27 bLeftOver4 = -1;
28 if (m_fallbackBuffer != null)
29 {
31 }
32 }
33 }
34
35 internal unsafe char* map4BytesToUnicode = null;
36
37 internal unsafe byte* mapUnicodeTo4BytesFlags = null;
38
39 private readonly ushort[] _tableUnicodeToGBDiffs = new ushort[439]
40 {
41 32896, 36, 32769, 2, 32770, 7, 32770, 5, 32769, 31,
42 32769, 8, 32770, 6, 32771, 1, 32770, 4, 32770, 3,
43 32769, 1, 32770, 1, 32769, 4, 32769, 17, 32769, 7,
44 32769, 15, 32769, 24, 32769, 3, 32769, 4, 32769, 29,
45 32769, 98, 32769, 1, 32769, 1, 32769, 1, 32769, 1,
46 32769, 1, 32769, 1, 32769, 1, 32769, 28, 43199, 87,
47 32769, 15, 32769, 101, 32769, 1, 32771, 13, 32769, 183,
48 32785, 1, 32775, 7, 32785, 1, 32775, 55, 32769, 14,
49 32832, 1, 32769, 7102, 32769, 2, 32772, 1, 32770, 2,
50 32770, 7, 32770, 9, 32769, 1, 32770, 1, 32769, 5,
51 32769, 112, 41699, 86, 32769, 1, 32769, 3, 32769, 12,
52 32769, 10, 32769, 62, 32780, 4, 32778, 22, 32772, 2,
53 32772, 110, 32769, 6, 32769, 1, 32769, 3, 32769, 4,
54 32769, 2, 32772, 2, 32769, 1, 32769, 1, 32773, 2,
55 32769, 5, 32772, 5, 32769, 10, 32769, 3, 32769, 5,
56 32769, 13, 32770, 2, 32772, 6, 32770, 37, 32769, 3,
57 32769, 11, 32769, 25, 32769, 82, 32769, 333, 32778, 10,
58 32808, 100, 32844, 4, 32804, 13, 32783, 3, 32771, 10,
59 32770, 16, 32770, 8, 32770, 8, 32770, 3, 32769, 2,
60 32770, 18, 32772, 31, 32770, 2, 32769, 54, 32769, 1,
61 32769, 2110, 65104, 2, 65108, 3, 65111, 2, 65112, 65117,
62 10, 65118, 15, 65131, 2, 65134, 3, 65137, 4, 65139,
63 2, 65140, 65141, 3, 65145, 14, 65156, 293, 43402, 43403,
64 43404, 43405, 43406, 43407, 43408, 43409, 43410, 43411, 43412, 43413,
65 4, 32772, 1, 32787, 5, 32770, 2, 32777, 20, 43401,
66 2, 32851, 7, 32772, 2, 32854, 5, 32771, 6, 32805,
67 246, 32778, 7, 32769, 113, 32769, 234, 32770, 12, 32771,
68 2, 32769, 34, 32769, 9, 32769, 2, 32770, 2, 32769,
69 113, 65110, 43, 65109, 298, 65114, 111, 65116, 11, 65115,
70 765, 65120, 85, 65119, 96, 65122, 65125, 14, 65123, 147,
71 65124, 218, 65128, 287, 65129, 113, 65130, 885, 65135, 264,
72 65136, 471, 65138, 116, 65144, 4, 65143, 43, 65146, 248,
73 65147, 373, 65149, 20, 65148, 193, 65152, 5, 65153, 82,
74 65154, 16, 65155, 441, 65157, 50, 65158, 2, 65159, 4,
75 65160, 65161, 1, 65162, 65163, 20, 65165, 3, 65164, 22,
76 65167, 65166, 703, 65174, 39, 65171, 65172, 65173, 65175, 65170,
77 111, 65176, 65177, 65178, 65179, 65180, 65181, 65182, 148, 65183,
78 81, 53670, 14426, 36716, 1, 32859, 1, 32798, 13, 32801,
79 1, 32771, 5, 32769, 7, 32769, 4, 32770, 4, 32770,
80 8, 32769, 7, 32769, 16, 32770, 14, 32769, 4295, 32769,
81 76, 32769, 27, 32769, 81, 32769, 9, 32769, 26, 32772,
82 1, 32769, 1, 32770, 3, 32769, 6, 32771, 1, 32770,
83 2, 32771, 1030, 32770, 1, 32786, 4, 32778, 1, 32772,
84 1, 32782, 1, 32772, 149, 32862, 129, 32774, 26
85 };
86
87 internal unsafe GB18030Encoding()
88 : base(54936, 936, System.Text.EncoderFallback.ReplacementFallback, System.Text.DecoderFallback.ReplacementFallback)
89 {
90 }
91
92 protected unsafe override void LoadManagedCodePage()
93 {
94 iExtraBytes = 87032;
95 base.LoadManagedCodePage();
96 byte* ptr = (byte*)(void*)safeNativeMemoryHandle.DangerousGetHandle();
97 mapUnicodeTo4BytesFlags = ptr + 262144;
98 map4BytesToUnicode = (char*)(ptr + 262144 + 8192);
99 char c = '\0';
100 ushort num = 0;
101 for (int i = 0; i < _tableUnicodeToGBDiffs.Length; i++)
102 {
103 ushort num2 = _tableUnicodeToGBDiffs[i];
104 if ((num2 & 0x8000u) != 0)
105 {
106 if (num2 > 36864 && num2 != 53670)
107 {
108 mapBytesToUnicode[(int)num2] = c;
109 mapUnicodeToBytes[(int)c] = num2;
110 c = (char)(c + 1);
111 }
112 else
113 {
114 c = (char)(c + (ushort)(num2 & 0x7FFF));
115 }
116 continue;
117 }
118 while (num2 > 0)
119 {
120 map4BytesToUnicode[(int)num] = c;
121 mapUnicodeToBytes[(int)c] = num;
122 byte* num3 = mapUnicodeTo4BytesFlags + c / 8;
123 *num3 |= (byte)(1 << c % 8);
124 c = (char)(c + 1);
125 num++;
126 num2--;
127 }
128 }
129 }
130
131 internal unsafe bool Is4Byte(char charTest)
132 {
133 byte b = mapUnicodeTo4BytesFlags[charTest / 8];
134 if (b != 0)
135 {
136 return (b & (1 << charTest % 8)) != 0;
137 }
138 return false;
139 }
140
141 public unsafe override int GetByteCount(char* chars, int count, System.Text.EncoderNLS encoder)
142 {
143 return GetBytes(chars, count, null, 0, encoder);
144 }
145
146 public unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, System.Text.EncoderNLS encoder)
147 {
148 char c = '\0';
149 if (encoder != null)
150 {
151 c = encoder.charLeftOver;
152 }
153 EncodingByteBuffer encodingByteBuffer = new EncodingByteBuffer(this, encoder, bytes, byteCount, chars, charCount);
154 while (true)
155 {
156 if (encodingByteBuffer.MoreData)
157 {
158 char nextChar = encodingByteBuffer.GetNextChar();
159 if (c != 0)
160 {
161 if (!char.IsLowSurrogate(nextChar))
162 {
163 encodingByteBuffer.MovePrevious(bThrow: false);
164 if (encodingByteBuffer.Fallback(c))
165 {
166 c = '\0';
167 continue;
168 }
169 c = '\0';
170 }
171 else
172 {
173 int num = (c - 55296 << 10) + (nextChar - 56320);
174 byte b = (byte)(num % 10 + 48);
175 num /= 10;
176 byte b2 = (byte)(num % 126 + 129);
177 num /= 126;
178 byte b3 = (byte)(num % 10 + 48);
179 num /= 10;
180 c = '\0';
181 if (encodingByteBuffer.AddByte((byte)(num + 144), b3, b2, b))
182 {
183 c = '\0';
184 continue;
185 }
186 encodingByteBuffer.MovePrevious(bThrow: false);
187 }
188 }
189 else if (nextChar <= '\u007f')
190 {
191 if (encodingByteBuffer.AddByte((byte)nextChar))
192 {
193 continue;
194 }
195 }
196 else
197 {
198 if (char.IsHighSurrogate(nextChar))
199 {
200 c = nextChar;
201 continue;
202 }
203 if (char.IsLowSurrogate(nextChar))
204 {
205 if (encodingByteBuffer.Fallback(nextChar))
206 {
207 continue;
208 }
209 }
210 else
211 {
212 ushort num2 = mapUnicodeToBytes[(int)nextChar];
213 if (Is4Byte(nextChar))
214 {
215 byte b4 = (byte)(num2 % 10 + 48);
216 num2 /= 10;
217 byte b5 = (byte)(num2 % 126 + 129);
218 num2 /= 126;
219 byte b6 = (byte)(num2 % 10 + 48);
220 num2 /= 10;
221 if (encodingByteBuffer.AddByte((byte)(num2 + 129), b6, b5, b4))
222 {
223 continue;
224 }
225 }
226 else if (encodingByteBuffer.AddByte((byte)(num2 >> 8), (byte)(num2 & 0xFFu)))
227 {
228 continue;
229 }
230 }
231 }
232 }
233 if ((encoder != null && !encoder.MustFlush) || c <= '\0')
234 {
235 break;
236 }
237 encodingByteBuffer.Fallback(c);
238 c = '\0';
239 }
240 if (encoder != null)
241 {
242 if (bytes != null)
243 {
244 encoder.charLeftOver = c;
245 }
246 encoder.m_charsUsed = encodingByteBuffer.CharsUsed;
247 }
248 return encodingByteBuffer.Count;
249 }
250
251 internal bool IsGBLeadByte(short ch)
252 {
253 if (ch >= 129)
254 {
255 return ch <= 254;
256 }
257 return false;
258 }
259
260 internal bool IsGBTwoByteTrailing(short ch)
261 {
262 if (ch < 64 || ch > 126)
263 {
264 if (ch >= 128)
265 {
266 return ch <= 254;
267 }
268 return false;
269 }
270 return true;
271 }
272
273 internal bool IsGBFourByteTrailing(short ch)
274 {
275 if (ch >= 48)
276 {
277 return ch <= 57;
278 }
279 return false;
280 }
281
282 internal int GetFourBytesOffset(short offset1, short offset2, short offset3, short offset4)
283 {
284 return (offset1 - 129) * 10 * 126 * 10 + (offset2 - 48) * 126 * 10 + (offset3 - 129) * 10 + offset4 - 48;
285 }
286
287 public unsafe override int GetCharCount(byte* bytes, int count, System.Text.DecoderNLS baseDecoder)
288 {
289 return GetChars(bytes, count, null, 0, baseDecoder);
290 }
291
292 public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int charCount, System.Text.DecoderNLS baseDecoder)
293 {
294 GB18030Decoder gB18030Decoder = (GB18030Decoder)baseDecoder;
295 EncodingCharBuffer encodingCharBuffer = new EncodingCharBuffer(this, gB18030Decoder, chars, charCount, bytes, byteCount);
296 short num = -1;
297 short num2 = -1;
298 short num3 = -1;
299 short num4 = -1;
300 if (gB18030Decoder != null && gB18030Decoder.bLeftOver1 != -1)
301 {
302 num = gB18030Decoder.bLeftOver1;
303 num2 = gB18030Decoder.bLeftOver2;
304 num3 = gB18030Decoder.bLeftOver3;
305 num4 = gB18030Decoder.bLeftOver4;
306 while (num != -1)
307 {
308 if (!IsGBLeadByte(num))
309 {
310 if (num <= 127)
311 {
312 if (!encodingCharBuffer.AddChar((char)num))
313 {
314 break;
315 }
316 }
317 else if (!encodingCharBuffer.Fallback((byte)num))
318 {
319 break;
320 }
321 num = num2;
322 num2 = num3;
323 num3 = num4;
324 num4 = -1;
325 continue;
326 }
327 while (num2 == -1 || (IsGBFourByteTrailing(num2) && num4 == -1))
328 {
329 if (!encodingCharBuffer.MoreData)
330 {
331 if (gB18030Decoder.MustFlush)
332 {
333 break;
334 }
335 if (chars != null)
336 {
337 gB18030Decoder.bLeftOver1 = num;
338 gB18030Decoder.bLeftOver2 = num2;
339 gB18030Decoder.bLeftOver3 = num3;
340 gB18030Decoder.bLeftOver4 = num4;
341 }
342 gB18030Decoder.m_bytesUsed = encodingCharBuffer.BytesUsed;
343 return encodingCharBuffer.Count;
344 }
345 if (num2 == -1)
346 {
347 num2 = encodingCharBuffer.GetNextByte();
348 }
349 else if (num3 == -1)
350 {
351 num3 = encodingCharBuffer.GetNextByte();
352 }
353 else
354 {
355 num4 = encodingCharBuffer.GetNextByte();
356 }
357 }
358 if (IsGBTwoByteTrailing(num2))
359 {
360 int num5 = num << 8;
361 num5 |= (byte)num2;
362 if (!encodingCharBuffer.AddChar(mapBytesToUnicode[num5], 2))
363 {
364 break;
365 }
366 num = -1;
367 num2 = -1;
368 }
369 else if (IsGBFourByteTrailing(num2) && IsGBLeadByte(num3) && IsGBFourByteTrailing(num4))
370 {
371 int fourBytesOffset = GetFourBytesOffset(num, num2, num3, num4);
372 if (fourBytesOffset <= 39419)
373 {
374 if (!encodingCharBuffer.AddChar(map4BytesToUnicode[fourBytesOffset], 4))
375 {
376 break;
377 }
378 }
379 else if (fourBytesOffset >= 189000 && fourBytesOffset <= 1237575)
380 {
381 fourBytesOffset -= 189000;
382 if (!encodingCharBuffer.AddChar((char)(55296 + fourBytesOffset / 1024), (char)(56320 + fourBytesOffset % 1024), 4))
383 {
384 break;
385 }
386 }
387 else if (!encodingCharBuffer.Fallback((byte)num, (byte)num2, (byte)num3, (byte)num4))
388 {
389 break;
390 }
391 num = -1;
392 num2 = -1;
393 num3 = -1;
394 num4 = -1;
395 }
396 else
397 {
398 if (!encodingCharBuffer.Fallback((byte)num))
399 {
400 break;
401 }
402 num = num2;
403 num2 = num3;
404 num3 = num4;
405 num4 = -1;
406 }
407 }
408 }
409 while (encodingCharBuffer.MoreData)
410 {
411 byte nextByte = encodingCharBuffer.GetNextByte();
412 if (nextByte <= 127)
413 {
414 if (!encodingCharBuffer.AddChar((char)nextByte))
415 {
416 break;
417 }
418 }
419 else if (IsGBLeadByte(nextByte))
420 {
421 if (encodingCharBuffer.MoreData)
422 {
423 byte nextByte2 = encodingCharBuffer.GetNextByte();
424 if (IsGBTwoByteTrailing(nextByte2))
425 {
426 int num6 = nextByte << 8;
427 num6 |= nextByte2;
428 if (!encodingCharBuffer.AddChar(mapBytesToUnicode[num6], 2))
429 {
430 break;
431 }
432 }
433 else if (IsGBFourByteTrailing(nextByte2))
434 {
435 if (encodingCharBuffer.EvenMoreData(2))
436 {
437 byte nextByte3 = encodingCharBuffer.GetNextByte();
438 byte nextByte4 = encodingCharBuffer.GetNextByte();
439 if (IsGBLeadByte(nextByte3) && IsGBFourByteTrailing(nextByte4))
440 {
441 int fourBytesOffset2 = GetFourBytesOffset(nextByte, nextByte2, nextByte3, nextByte4);
442 if (fourBytesOffset2 <= 39419)
443 {
444 if (!encodingCharBuffer.AddChar(map4BytesToUnicode[fourBytesOffset2], 4))
445 {
446 break;
447 }
448 }
449 else if (fourBytesOffset2 >= 189000 && fourBytesOffset2 <= 1237575)
450 {
451 fourBytesOffset2 -= 189000;
452 if (!encodingCharBuffer.AddChar((char)(55296 + fourBytesOffset2 / 1024), (char)(56320 + fourBytesOffset2 % 1024), 4))
453 {
454 break;
455 }
456 }
457 else if (!encodingCharBuffer.Fallback(nextByte, nextByte2, nextByte3, nextByte4))
458 {
459 break;
460 }
461 }
462 else
463 {
464 encodingCharBuffer.AdjustBytes(-3);
465 if (!encodingCharBuffer.Fallback(nextByte))
466 {
467 break;
468 }
469 }
470 continue;
471 }
472 if (gB18030Decoder != null && !gB18030Decoder.MustFlush)
473 {
474 if (chars != null)
475 {
476 num = nextByte;
477 num2 = nextByte2;
478 num3 = (short)((!encodingCharBuffer.MoreData) ? (-1) : encodingCharBuffer.GetNextByte());
479 num4 = -1;
480 }
481 break;
482 }
483 if (!encodingCharBuffer.Fallback(nextByte, nextByte2))
484 {
485 break;
486 }
487 }
488 else
489 {
490 encodingCharBuffer.AdjustBytes(-1);
491 if (!encodingCharBuffer.Fallback(nextByte))
492 {
493 break;
494 }
495 }
496 continue;
497 }
498 if (gB18030Decoder != null && !gB18030Decoder.MustFlush)
499 {
500 if (chars != null)
501 {
502 num = nextByte;
503 num2 = -1;
504 num3 = -1;
505 num4 = -1;
506 }
507 break;
508 }
509 if (!encodingCharBuffer.Fallback(nextByte))
510 {
511 break;
512 }
513 }
514 else if (!encodingCharBuffer.Fallback(nextByte))
515 {
516 break;
517 }
518 }
519 if (gB18030Decoder != null)
520 {
521 if (chars != null)
522 {
523 gB18030Decoder.bLeftOver1 = num;
524 gB18030Decoder.bLeftOver2 = num2;
525 gB18030Decoder.bLeftOver3 = num3;
526 gB18030Decoder.bLeftOver4 = num4;
527 }
528 gB18030Decoder.m_bytesUsed = encodingCharBuffer.BytesUsed;
529 }
530 return encodingCharBuffer.Count;
531 }
532
533 public override int GetMaxByteCount(int charCount)
534 {
535 if (charCount < 0)
536 {
538 }
539 long num = (long)charCount + 1L;
540 if (base.EncoderFallback.MaxCharCount > 1)
541 {
542 num *= base.EncoderFallback.MaxCharCount;
543 }
544 num *= 4;
545 if (num > int.MaxValue)
546 {
548 }
549 return (int)num;
550 }
551
552 public override int GetMaxCharCount(int byteCount)
553 {
554 if (byteCount < 0)
555 {
557 }
558 long num = (long)byteCount + 3L;
559 if (base.DecoderFallback.MaxCharCount > 1)
560 {
561 num *= base.DecoderFallback.MaxCharCount;
562 }
563 if (num > int.MaxValue)
564 {
566 }
567 return (int)num;
568 }
569
570 public override Decoder GetDecoder()
571 {
572 return new GB18030Decoder(this);
573 }
574}
static string ArgumentOutOfRange_GetCharCountOverflow
Definition SR.cs:90
static string ArgumentOutOfRange_GetByteCountOverflow
Definition SR.cs:88
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
Definition SR.cs:7
DecoderFallbackBuffer m_fallbackBuffer
Definition DecoderNLS.cs:17
unsafe bool AddByte(byte b, int moreBytesExpected)
Definition Encoding.cs:271
unsafe void MovePrevious(bool bThrow)
Definition Encoding.cs:305
unsafe bool AddChar(char ch, int numBytes)
Definition Encoding.cs:145
bool Fallback(byte fallbackByte)
Definition Encoding.cs:180
unsafe void AdjustBytes(int count)
Definition Encoding.cs:166
unsafe override int GetByteCount(char *chars, int count, System.Text.EncoderNLS encoder)
int GetFourBytesOffset(short offset1, short offset2, short offset3, short offset4)
unsafe override void LoadManagedCodePage()
override Decoder GetDecoder()
override int GetMaxCharCount(int byteCount)
readonly ushort[] _tableUnicodeToGBDiffs
unsafe byte * mapUnicodeTo4BytesFlags
unsafe bool Is4Byte(char charTest)
unsafe override int GetCharCount(byte *bytes, int count, System.Text.DecoderNLS baseDecoder)
unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, System.Text.EncoderNLS encoder)
unsafe override int GetChars(byte *bytes, int byteCount, char *chars, int charCount, System.Text.DecoderNLS baseDecoder)
override int GetMaxByteCount(int charCount)