Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GB18030Encoding.cs « CJK « I18N « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f62121e81f042353e77e6952e5f6df720654cf65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//
// GB18030Encoding.cs
//
// Author:
//	Atsushi Enomoto  <atsushi@ximian.com>
//
using System;
using System.Reflection;
using System.Text;
using I18N.Common;

#if DISABLE_UNSAFE
using MonoEncoder = I18N.Common.MonoSafeEncoder;
using MonoEncoding = I18N.Common.MonoSafeEncoding;
#endif

namespace I18N.CJK
{
	[Serializable]
	internal class ENCgb18030 : GB18030Encoding
	{
		public ENCgb18030 (): base () {}
	}

	[Serializable]
	public class CP54936 : GB18030Encoding { }

	[Serializable]
	public class GB18030Encoding : MonoEncoding
	{
		// Constructor.
		public GB18030Encoding ()
			: base (54936, 936)
		{
		}

		public override string EncodingName {
			get { return "Chinese Simplified (GB18030)"; }
		}

		public override string HeaderName {
			get { return "GB18030"; }
		}

		public override string BodyName {
			get { return "GB18030"; }
		}

		public override string WebName {
			get { return "GB18030"; }
		}

		public override bool IsMailNewsDisplay {
			get { return true; }
		}

		public override bool IsMailNewsSave {
			get { return true; }
		}

		public override bool IsBrowserDisplay {
			get { return true; }
		}

		public override bool IsBrowserSave {
			get { return true; }
		}

		public override int GetMaxByteCount (int len)
		{
			// non-GB2312 characters in \u0080 - \uFFFF
			return len * 4;
		}

		public override int GetMaxCharCount (int len)
		{
			return len;
		}

#if !DISABLE_UNSAFE
		public unsafe override int GetByteCountImpl (char* chars, int count)
		{
			return new GB18030Encoder (this).GetByteCountImpl (chars, count, true);
		}

		public unsafe override int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount)
		{
			return new GB18030Encoder (this).GetBytesImpl (chars, charCount, bytes, byteCount, true);
		}
#else
		public override int GetByteCount (char [] chars, int index, int length)
		{
			return new GB18030Encoder (this).GetByteCount (chars, index, length, true);
		}

		public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
		{
			return new GB18030Encoder (this).GetBytes (chars, charIndex, charCount, bytes, byteIndex, true);
		}
#endif

		public override int GetCharCount (byte [] bytes, int start, int len)
		{
			return new GB18030Decoder ().GetCharCount (bytes, start, len);
		}

		public override int GetChars (byte [] bytes, int byteIdx, int srclen, char [] chars, int charIdx)
		{
			return new GB18030Decoder ().GetChars (bytes, byteIdx, srclen, chars, charIdx);
		}

		public override Encoder GetEncoder ()
		{
			return new GB18030Encoder (this);
		}

		public override Decoder GetDecoder ()
		{
			return new GB18030Decoder ();
		}
	}

	class GB18030Decoder : DbcsEncoding.DbcsDecoder
	{
		static DbcsConvert gb2312 = DbcsConvert.Gb2312;
		// for now incomplete block is not supported - should we?
		// int incomplete1 = -1, incomplete2 = -1, incomplete3 = -1;

		public GB18030Decoder ()
			: base (null)
		{
		}

		public override int GetCharCount (byte [] bytes, int start, int len)
		{
			CheckRange (bytes, start, len);

			int end = start + len;
			int ret = 0;
			while (start < end) {
				if (bytes [start] < 0x80) {
					ret++;
					start++;
					continue;
				}
				else if (bytes [start] == 0x80) {
					// Euro sign - actually it is obsolete,
					// now it's just reserved but not used
					ret++;
					start++;
					continue;
				}
				else if (bytes [start] == 0xFF) {
					// invalid data - fill '?'
					ret++;
					start++;
					continue;
				}
				else if (start + 1 >= end) {
//					incomplete1 = bytes [start];
//					incomplete2 = -1;
//					incomplete3 = -1;
					ret++;
					break; // incomplete tail.
				}

				byte second = bytes [start + 1];
				if (second == 0x7F || second == 0xFF) {
					// invalid data
					ret++;
					start += 2;
					continue;
				}
				else if (0x30 <= second && second <= 0x39) {
					// UCS mapping
					if (start + 3 >= end) {
						// incomplete tail.
//						incomplete1 = bytes [start];
//						incomplete2 = bytes [start + 1];
//						if (start + 3 == end)
//							incomplete3 = bytes [start + 2];
						ret += start + 3 == end ? 3 : 2;
						break;
					}
					long value = GB18030Source.FromGBX (bytes, start);
					if (value < 0) {
						// invalid data.
						ret++;
						start -= (int) value;
					} else if (value >= 0x10000) {
						// UTF16 surrogate
						ret += 2;
						start += 4;
					} else {
						// UTF16 BMP
						ret++;
						start+= 4;
					}
				} else {
					// GB2312 mapping
					start += 2;
					ret++;
				}
			}
			return ret;
		}

		public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
		{
			CheckRange (bytes, byteIndex, byteCount, chars, charIndex);

			int byteEnd = byteIndex + byteCount;
			int charStart = charIndex;

			while (byteIndex < byteEnd) {
				if (bytes [byteIndex] < 0x80) {
					chars [charIndex++] = (char) bytes [byteIndex++];
					continue;
				}
				else if (bytes [byteIndex] == 0x80) {
					// Euro sign - actually it is obsolete,
					// now it's just reserved but not used
					chars [charIndex++] = '\u20AC';
					byteIndex++;
					continue;
				}
				else if (bytes [byteIndex] == 0xFF) {
					// invalid data - fill '?'
					chars [charIndex++] = '?';
					byteIndex++;
					continue;
				}
				else if (byteIndex + 1 >= byteEnd) {
					//incomplete1 = bytes [byteIndex++];
					//incomplete2 = -1;
					//incomplete3 = -1;
					break; // incomplete tail.
				}

				byte second = bytes [byteIndex + 1];
				if (second == 0x7F || second == 0xFF) {
					// invalid data
					chars [charIndex++] = '?';
					byteIndex += 2;
				}
				else if (0x30 <= second && second <= 0x39) {
					// UCS mapping
					if (byteIndex + 3 >= byteEnd) {
						// incomplete tail.
						//incomplete1 = bytes [byteIndex];
						//incomplete2 = bytes [byteIndex + 1];
						//if (byteIndex + 3 == byteEnd)
						//	incomplete3 = bytes [byteIndex + 2];
						break;
					}
					long value = GB18030Source.FromGBX (bytes, byteIndex);
					if (value < 0) {
						// invalid data.
						chars [charIndex++] = '?';
						byteIndex -= (int) value;
					} else if (value >= 0x10000) {
						// UTF16 surrogate
						value -= 0x10000;
						chars [charIndex++] = (char) (value / 0x400 + 0xD800);
						chars [charIndex++] = (char) (value % 0x400 + 0xDC00);
						byteIndex += 4;
					} else {
						// UTF16 BMP
						chars [charIndex++] = (char) value;
						byteIndex += 4;
					}
				} else {
					byte first = bytes [byteIndex];
					int ord = ((first - 0x81) * 191 + second - 0x40) * 2;
					char c1 = ord < 0 || ord >= gb2312.n2u.Length ?
						'\0' : (char) (gb2312.n2u [ord] + gb2312.n2u [ord + 1] * 256);
					if (c1 == 0)
						chars [charIndex++] = '?';
					else
						chars [charIndex++] = c1;
					byteIndex += 2;
				}
			}

			return charIndex - charStart;
		}
	}

	class GB18030Encoder : MonoEncoder
	{
		static DbcsConvert gb2312 = DbcsConvert.Gb2312;

		public GB18030Encoder (MonoEncoding owner)
			: base (owner)
		{
		}

		char incomplete_byte_count;
		char incomplete_bytes;

#if !DISABLE_UNSAFE
		public unsafe override int GetByteCountImpl (char* chars, int count, bool refresh)
		{
			int start = 0;
			int end = count;
			int ret = 0;
			while (start < end) {
				char ch = chars [start];
				if (ch < 0x80) {
					// ASCII
					ret++;
					start++;
					continue;
				} else if (Char.IsSurrogate (ch)) {
					// Surrogate
					if (start + 1 == end) {
						incomplete_byte_count = ch;
						start++;
					} else {
						ret += 4;
						start += 2;
					}
					continue;
				}

				if (ch < 0x80 || ch == 0xFF) {
					// ASCII
					ret++;
					start++;
					continue;
				}

				byte b1 = gb2312.u2n [((int) ch) * 2 + 1];
				byte b2 = gb2312.u2n [((int) ch) * 2];
				if (b1 != 0 && b2 != 0) {
					// GB2312
					ret += 2;
					start++;
					continue;
				}

				// non-GB2312
				long value = GB18030Source.FromUCS (ch);
				if (value < 0)
					ret++; // invalid(?)
				else
					ret += 4;
				start++;
			}

			if (refresh) {
				if (incomplete_byte_count != char.MinValue)
					ret++;
				incomplete_byte_count = char.MinValue;
			}
			return ret;
		}

		public unsafe override int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount, bool refresh)
		{
			int charIndex = 0;
			int byteIndex = 0;

			int charEnd = charIndex + charCount;
			int byteStart = byteIndex;
			char ch = incomplete_bytes;

			while (charIndex < charEnd) {
				if (incomplete_bytes == char.MinValue)
					ch = chars [charIndex++];
				else
					incomplete_bytes = char.MinValue;

				if (ch < 0x80) {
					// ASCII
					bytes [byteIndex++] = (byte) ch;
					continue;
				} else if (Char.IsSurrogate (ch)) {
					// Surrogate
					if (charIndex == charEnd) {
						incomplete_bytes = ch;
						break; // incomplete
					}
					char ch2 = chars [charIndex++];
					if (!Char.IsSurrogate (ch2)) {
						// invalid surrogate
						HandleFallback (
							chars, ref charIndex, ref charCount,
							bytes, ref byteIndex, ref byteCount, null);
						continue;
					}
					int cp = (ch - 0xD800) * 0x400 + ch2 - 0xDC00;
					GB18030Source.Unlinear (bytes + byteIndex, GB18030Source.FromUCSSurrogate (cp));
					byteIndex += 4;
					continue;
				}


				if (ch <= 0x80 || ch == 0xFF) {
					// Character maps to itself
					bytes [byteIndex++] = (byte) ch;
					continue;
				}

				byte b1 = gb2312.u2n [((int) ch) * 2 + 1];
				byte b2 = gb2312.u2n [((int) ch) * 2];
				if (b1 != 0 && b2 != 0) {
					bytes [byteIndex++] = b1;
					bytes [byteIndex++] = b2;
					continue;
				}

				long value = GB18030Source.FromUCS (ch);
				if (value < 0)
					bytes [byteIndex++] = 0x3F; // invalid(?)
				else {
					// non-GB2312
					GB18030Source.Unlinear (bytes + byteIndex, value);
					byteIndex += 4;
				}
			}

			if (refresh) {
				if (incomplete_bytes != char.MinValue)
					bytes [byteIndex++] = 0x3F; // incomplete
				incomplete_bytes = char.MinValue;
			}

			return byteIndex - byteStart;
		}
#else

		public override int GetByteCount(char[] chars, int index, int count, bool refresh)
		{
			int start = 0;
			int end = count;
			int ret = 0;
			while (start < end)
			{
				char ch = chars[start];
				if (ch < 0x80)
				{
					// ASCII
					ret++;
					start++;
					continue;
				}
				else if (Char.IsSurrogate(ch))
				{
					// Surrogate
					if (start + 1 == end)
					{
						incomplete_byte_count = ch;
						start++;
					}
					else
					{
						ret += 4;
						start += 2;
					}
					continue;
				}

				if (ch < 0x80 || ch == 0xFF)
				{
					// ASCII
					ret++;
					start++;
					continue;
				}

				byte b1 = gb2312.u2n[((int)ch) * 2 + 1];
				byte b2 = gb2312.u2n[((int)ch) * 2];
				if (b1 != 0 && b2 != 0)
				{
					// GB2312
					ret += 2;
					start++;
					continue;
				}

				// non-GB2312
				long value = GB18030Source.FromUCS(ch);
				if (value < 0)
					ret++; // invalid(?)
				else
					ret += 4;
				start++;
			}

			if (refresh)
			{
				if (incomplete_byte_count != char.MinValue)
					ret++;
				incomplete_byte_count = char.MinValue;
			}
			return ret;
		}

		public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool refresh)
		{
			int byteCount = bytes.Length;
			int charEnd = charIndex + charCount;
			int byteStart = byteIndex;
			char ch = incomplete_bytes;

			while (charIndex < charEnd)
			{
				if (incomplete_bytes == char.MinValue)
					ch = chars[charIndex++];
				else
					incomplete_bytes = char.MinValue;

				if (ch < 0x80)
				{
					// ASCII
					bytes[byteIndex++] = (byte)ch;
					continue;
				}
				else if (Char.IsSurrogate(ch))
				{
					// Surrogate
					if (charIndex == charEnd)
					{
						incomplete_bytes = ch;
						break; // incomplete
					}
					char ch2 = chars[charIndex++];
					if (!Char.IsSurrogate(ch2))
					{
						// invalid surrogate
						HandleFallback (chars, ref charIndex, ref charCount,
							bytes, ref byteIndex, ref byteCount, null);
						continue;
					}
					int cp = (ch - 0xD800) * 0x400 + ch2 - 0xDC00;
					GB18030Source.Unlinear(bytes,  byteIndex, GB18030Source.FromUCSSurrogate(cp));
					byteIndex += 4;
					continue;
				}


				if (ch <= 0x80 || ch == 0xFF)
				{
					// Character maps to itself
					bytes[byteIndex++] = (byte)ch;
					continue;
				}

				byte b1 = gb2312.u2n[((int)ch) * 2 + 1];
				byte b2 = gb2312.u2n[((int)ch) * 2];
				if (b1 != 0 && b2 != 0)
				{
					bytes[byteIndex++] = b1;
					bytes[byteIndex++] = b2;
					continue;
				}

				long value = GB18030Source.FromUCS(ch);
				if (value < 0)
					bytes[byteIndex++] = 0x3F; // invalid(?)
				else
				{
					// non-GB2312
					GB18030Source.Unlinear(bytes, byteIndex, value);
					byteIndex += 4;
				}
			}

			if (refresh)
			{
				if (incomplete_bytes != char.MinValue)
					bytes[byteIndex++] = 0x3F; // incomplete
				incomplete_bytes = char.MinValue;
			}

			return byteIndex - byteStart;
		}
#endif
	}
}