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

ISO2022JP.cs « CJK « I18N « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fea787bfb90185ba05e192ea7ae46aefc7b18b45 (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
using System;
using System.Text;
using I18N.Common;

namespace I18N.CJK
{
	// FIXME:
	// find out what is the difference between 50220, 50221 and 50222.

	[Serializable]
	public class CP50220 : ISO2022JPEncoding
	{
		public CP50220 ()
			: base (50220, false, false)
		{
		}

		public override string EncodingName {
			get { return "Japanese (JIS)"; }
		}
	}

	[Serializable]
	public class CP50221 : ISO2022JPEncoding
	{
		public CP50221 ()
			: base (50221, true, false)
		{
		}

		public override string EncodingName {
			get { return "Japanese (JIS-Allow 1 byte Kana)"; }
		}
	}

	[Serializable]
	public class CP50222 : ISO2022JPEncoding
	{
		public CP50222 ()
			: base (50222, true, true)
		{
		}

		public override string EncodingName {
			get { return "Japanese (JIS-Allow 1 byte Kana - SO/SI)"; }
		}
	}

	[Serializable]
	public class ISO2022JPEncoding : MonoEncoding
	{
		static JISConvert convert = JISConvert.Convert;

		public ISO2022JPEncoding (int codePage, bool allow1ByteKana, bool allowShiftIO)
			: base (codePage)
		{
			this.allow_1byte_kana = allow1ByteKana;
			this.allow_shift_io = allowShiftIO;
		}

		readonly bool allow_1byte_kana, allow_shift_io;

		public override string BodyName {
			get { return "iso-2022-jp"; }
		}

		public override string HeaderName {
			get { return "iso-2022-jp"; }
		}

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

		public override int GetMaxByteCount (int charCount)
		{
			// ESC w ESC s ESC w ... (even number) ESC s
			return charCount / 2 * 5 + 4;
		}

		public override int GetMaxCharCount (int byteCount)
		{
			// no escape sequence
			return byteCount;
		}

		public override int GetByteCount (char [] chars, int charIndex, int charCount)
		{
			return new ISO2022JPEncoder (this, allow_1byte_kana, allow_shift_io).GetByteCount (chars, charIndex, charCount, true);
		}

		public unsafe override int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount)
		{
			return new ISO2022JPEncoder (this, allow_1byte_kana, allow_shift_io).GetBytesImpl (chars, charCount, bytes, byteCount, true);
		}

		public override int GetCharCount (byte [] bytes, int index, int count)
		{
			return new ISO2022JPDecoder (allow_1byte_kana, allow_shift_io).GetCharCount (bytes, index, count);
		}

		public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
		{
			return new ISO2022JPDecoder (allow_1byte_kana, allow_shift_io).GetChars (bytes, byteIndex, byteCount, chars, charIndex);
		}
	}

	internal enum ISO2022JPMode {
		ASCII,
		JISX0208,
		JISX0201
	}

	internal class ISO2022JPEncoder : MonoEncoding.MonoEncoder
	{
		static JISConvert convert = JISConvert.Convert;

		readonly bool allow_1byte_kana, allow_shift_io;

		ISO2022JPMode m = ISO2022JPMode.ASCII;
		bool shifted_in;

		public ISO2022JPEncoder (MonoEncoding owner, bool allow1ByteKana, bool allowShiftIO)
			: base (owner)
		{
			this.allow_1byte_kana = allow1ByteKana;
			this.allow_shift_io = allowShiftIO;
		}

		public override int GetByteCount (char [] chars, int charIndex, int charCount, bool flush)
		{
			int end = charIndex + charCount;
			int value;
			int byteCount = 0;

			for (int i = charIndex; i < end; i++) {
				char ch = chars [i];
				// When half-kana is not allowed and it is
				// actually in the input, convert to full width
				// kana.
				if (!allow_1byte_kana &&
					ch >= 0xFF60 && ch <= 0xFFA0)
					ch = full_width_map [ch - 0xFF60];

				if (ch >= 0x2010 && ch <= 0x9FA5)
				{
					if (shifted_in) {
						shifted_in = false;
						byteCount++; // shift_out
					}
					if (m != ISO2022JPMode.JISX0208)
						byteCount += 3;
					m = ISO2022JPMode.JISX0208;
					// This range contains the bulk of the CJK set.
					value = (ch - 0x2010) * 2;
					value = ((int)(convert.cjkToJis[value])) |
							(((int)(convert.cjkToJis[value + 1])) << 8);
				} else if (ch >= 0xFF01 && ch <= 0xFF60) {
					if (shifted_in) {
						shifted_in = false;
						byteCount++;
					}
					if (m != ISO2022JPMode.JISX0208)
						byteCount += 3;
					m = ISO2022JPMode.JISX0208;

					// This range contains extra characters,
					value = (ch - 0xFF01) * 2;
					value = ((int)(convert.extraToJis[value])) |
							(((int)(convert.extraToJis[value + 1])) << 8);
				} else if(ch >= 0xFF60 && ch <= 0xFFA0) {
					if (allow_shift_io) {
						if (!shifted_in) {
							byteCount++;
							shifted_in = true;
						}
					}
					else if (m != ISO2022JPMode.JISX0201) {
						byteCount += 3;
						m = ISO2022JPMode.JISX0201;
					}
					value = ch - 0xFF60 + 0xA0;
				} else if (ch < 128) {
					if (shifted_in) {
						shifted_in = false;
						byteCount++;
					}
					if (m != ISO2022JPMode.ASCII)
						byteCount += 3;
					m = ISO2022JPMode.ASCII;
					value = (int) ch;
				} else
					// skip non-convertible character
					continue;

				if (value > 0x100)
					byteCount += 2;
				else
					byteCount++;
			}
			// must end in ASCII mode
			if (flush) {
				if (shifted_in) {
					shifted_in = false;
					byteCount++;
				}
				if (m != ISO2022JPMode.ASCII)
					byteCount += 3;
				m = ISO2022JPMode.ASCII;
			}
			return byteCount;
		}

		// returns false if it failed to add required ESC.
		private unsafe void SwitchMode (byte* bytes, ref int byteIndex,
			ref int byteCount, ref ISO2022JPMode cur, ISO2022JPMode next)
		{
			if (cur == next)
				return;

			if (byteCount <= 3)
				throw new ArgumentOutOfRangeException ("Insufficient byte buffer.");
			bytes [byteIndex++] = 0x1B;
			bytes [byteIndex++] = (byte) (next == ISO2022JPMode.JISX0208 ? 0x24 : 0x28);
			bytes [byteIndex++] = (byte) (next == ISO2022JPMode.JISX0201 ? 0x49 : 0x42);
			cur = next;
		}

		static readonly char [] full_width_map = new char [] {
			'\0', '\u3002', '\u300C', '\u300D', '\u3001', '\u30FB', // to nakaguro
			'\u30F2', '\u30A1', '\u30A3', '\u30A5', '\u30A7', '\u30A9', '\u30E3', '\u30E5', '\u30E7', '\u30C3', // to small tsu
			'\u30FC', '\u30A2', '\u30A4', '\u30A6', '\u30A8', '\u30AA', // A-O
			'\u30AB', '\u30AD', '\u30AF', '\u30B1', '\u30B3',
			'\u30B5', '\u30B7', '\u30B9', '\u30BB', '\u30BD',
			'\u30BF', '\u30C1', '\u30C4', '\u30C6', '\u30C8',
			'\u30C9', '\u30CA', '\u30CB', '\u30CC', '\u30CD',
			'\u30CF', '\u30D2', '\u30D5', '\u30D8', '\u30DB',
			'\u30DE', '\u30DF', '\u30E0', '\u30E1', '\u30E2',
			'\u30E4', '\u30E6', '\u30E8', // Ya-Yo
			'\u30E9', '\u30EA', '\u30EB', '\u30EC', '\u30ED',
			'\u30EF', '\u30F1', '\u30F3', '\u309B', '\u309C'};

		public unsafe override int GetBytesImpl (
			char* chars, int charCount,
			byte* bytes, int byteCount, bool flush)
		{
			int charIndex = 0;
			int byteIndex = 0;
#if NET_2_0
			EncoderFallbackBuffer buffer = null;
#endif

			int start = byteIndex;
			int end = charIndex + charCount;
			int value;

			for (int i = charIndex; i < end; i++, charCount--) {
				char ch = chars [i];

				// When half-kana is not allowed and it is
				// actually in the input, convert to full width
				// kana.
				if (!allow_1byte_kana &&
					ch >= 0xFF60 && ch <= 0xFFA0)
					ch = full_width_map [ch - 0xFF60];

				if (ch >= 0x2010 && ch <= 0x9FA5)
				{
					if (shifted_in) {
						bytes [byteIndex++] = 0x0F;
						shifted_in = false;
						byteCount--;
					}
					switch (m) {
					case ISO2022JPMode.JISX0208:
						break;
					default:
						SwitchMode (bytes, ref byteIndex, ref byteCount, ref m, ISO2022JPMode.JISX0208);
						break;
					}
					// This range contains the bulk of the CJK set.
					value = (ch - 0x2010) * 2;
					value = ((int)(convert.cjkToJis[value])) |
							(((int)(convert.cjkToJis[value + 1])) << 8);
				} else if (ch >= 0xFF01 && ch <= 0xFF60) {
					if (shifted_in) {
						bytes [byteIndex++] = 0x0F;
						shifted_in = false;
						byteCount--;
					}
					switch (m) {
					case ISO2022JPMode.JISX0208:
						break;
					default:
						SwitchMode (bytes, ref byteIndex, ref byteCount, ref m, ISO2022JPMode.JISX0208);
						break;
					}

					// This range contains extra characters,
					value = (ch - 0xFF01) * 2;
					value = ((int)(convert.extraToJis[value])) |
							(((int)(convert.extraToJis[value + 1])) << 8);
				} else if (ch >= 0xFF60 && ch <= 0xFFA0) {
					// disallowed half-width kana is
					// already converted to full-width kana
					// so here we don't have to consider it.

					if (allow_shift_io) {
						if (!shifted_in) {
							bytes [byteIndex++] = 0x0E;
							shifted_in = true;
							byteCount--;
						}
					} else {
						switch (m) {
						case ISO2022JPMode.JISX0201:
							break;
						default:
							SwitchMode (bytes, ref byteIndex, ref byteCount, ref m, ISO2022JPMode.JISX0201);
							break;
						}
					}
					value = ch - 0xFF40;
				} else if (ch < 128) {
					if (shifted_in) {
						bytes [byteIndex++] = 0x0F;
						shifted_in = false;
						byteCount--;
					}
					SwitchMode (bytes, ref byteIndex, ref byteCount, ref m, ISO2022JPMode.ASCII);
					value = (int) ch;
				} else {
#if NET_2_0
					HandleFallback (
						chars, ref i, ref charCount,
						bytes, ref byteIndex, ref byteCount);
#endif
					// skip non-convertible character
					continue;
				}

//Console.WriteLine ("{0:X04} : {1:x02} {2:x02}", v, (int) v / 94 + 33, v % 94 + 33);
				if (value > 0x100) {
					value -= 0x0100;
					bytes [byteIndex++] = (byte) (value / 94 + 33);
					bytes [byteIndex++] = (byte) (value % 94 + 33);
					byteCount -= 2;
				}
				else {
					bytes [byteIndex++] = (byte) value;
					byteCount--;
				}
			}
			if (flush) {
				// must end in ASCII mode
				if (shifted_in) {
					bytes [byteIndex++] = 0x0F;
					shifted_in = false;
					byteCount--;
				}
				if (m != ISO2022JPMode.ASCII)
					SwitchMode (bytes, ref byteIndex, ref byteCount, ref m, ISO2022JPMode.ASCII);
			}
			return byteIndex - start;
		}

#if NET_2_0
		public override void Reset ()
		{
			m = ISO2022JPMode.ASCII;
			shifted_in = false;
		}
#endif
	}

	internal class ISO2022JPDecoder : Decoder
	{
		static JISConvert convert = JISConvert.Convert;

		readonly bool allow_shift_io;
		ISO2022JPMode m = ISO2022JPMode.ASCII;
		bool shifted_in;

		public ISO2022JPDecoder (bool allow1ByteKana, bool allowShiftIO)
		{
			this.allow_shift_io = allowShiftIO;
		}

		// GetCharCount
		public override int GetCharCount (byte [] bytes, int index, int count)
		{
			int ret = 0;

			int end = index + count;
			for (int i = index; i < end; i++) {
				if (allow_shift_io) {
					switch (bytes [i]) {
					case 0x0F:
						shifted_in = false;
						continue;
					case 0x0E:
						shifted_in = true;
						continue;
					}
				}
				if (bytes [i] != 0x1B) {
					if (!shifted_in && m == ISO2022JPMode.JISX0208) {
						if (i + 1 == end)
							break; // incomplete head of wide char
						else
							ret++;
						i++; // 2 byte char
					}
					else
						ret++; // half-kana or ASCII
				} else {
					if (i + 2 >= end)
						break; // incomplete escape sequence
					i++;
					bool wide = false;
					if (bytes [i] == 0x24)
						wide = true;
					else if (bytes [i] == 0x28)
						wide = false;
					else
						throw new ArgumentException ("Unexpected ISO-2022-JP escape sequence.");
					i++;
					if (bytes [i] == 0x42)
						m = wide ? ISO2022JPMode.JISX0208 : ISO2022JPMode.ASCII;
					else if (bytes [i] == 0x49)
						m = ISO2022JPMode.JISX0201;
					else
						throw new ArgumentException (String.Format ("Unexpected ISO-2022-JP escape sequence. Ended with 0x{0:X04}", bytes [i]));
				}
			}
			return ret;
		}

		private int ToChar (int value)
		{
			value <<= 1;
			return value + 1 >= convert.jisx0208ToUnicode.Length ?
				-1 :
				((int) (convert.jisx0208ToUnicode [value])) |
					(((int) (convert.jisx0208ToUnicode [value + 1])) << 8);
		}

		public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
		{
			int start = charIndex;
			int end = byteIndex + byteCount;
			for (int i = byteIndex; i < end && charIndex < chars.Length; i++) {
				if (allow_shift_io) {
					switch (bytes [i]) {
					case 0x0F:
						shifted_in = false;
						continue;
					case 0x0E:
						shifted_in = true;
						continue;
					}
				}

				if (bytes [i] != 0x1B) {
					if (shifted_in || m == ISO2022JPMode.JISX0201) {
						// half-kana
						if (bytes [i] < 0x60)
							chars [charIndex++] = (char) (bytes [i] + 0xFF40);
						else
							// invalid
							chars [charIndex++] = '?';
					}
					else if (m == ISO2022JPMode.JISX0208) {
						if (i + 1 == end)
							break; // incomplete head of wide char

						// am so lazy, so reusing jis2sjis
						int s1 = ((bytes [i] - 1) >> 1) + ((bytes [i] <= 0x5e) ? 0x71 : 0xb1);
						int s2 = bytes [i + 1] + (((bytes [i] & 1) != 0) ? 0x20 : 0x7e);
						int v = (s1 - 0x81) * 0xBC;
						v += s2 - 0x41;

						int ch = ToChar (v);
						if (ch < 0)
							chars [charIndex++] = '?';
						else
							chars [charIndex++] = (char) ch;
						i++;
					}
					// LAMESPEC: actually this should not
					// be allowed when 1byte-kana is not
					// allowed, but MS.NET seems to allow
					// it in any mode.
					else if (bytes [i] > 0xA0 && bytes [i] < 0xE0) // half-width Katakana
						chars [charIndex++] = (char) (bytes [i] - 0xA0 + 0xFF60);
					else
						chars [charIndex++] = (char) bytes [i];
					continue;
				} else {
					if (i + 2 >= end)
						break; // incomplete escape sequence
					i++;
					bool wide = false;
					if (bytes [i] == 0x24)
						wide = true;
					else if (bytes [i] == 0x28)
						wide = false;
					else
						throw new ArgumentException ("Unexpected ISO-2022-JP escape sequence.");
					i++;
					if (bytes [i] == 0x42)
						m = wide ? ISO2022JPMode.JISX0208 : ISO2022JPMode.ASCII;
					else if (bytes [i] == 0x49)
						m = ISO2022JPMode.JISX0201;
					else
						throw new ArgumentException (String.Format ("Unexpected ISO-2022-JP escape sequence. Ended with 0x{0:X04}", bytes [i]));
				}
			}

			return charIndex - start;
		}

#if NET_2_0
		public override void Reset ()
		{
			m = ISO2022JPMode.ASCII;
			shifted_in = false;
		}
#endif
	}

	[Serializable]
	public class ENCiso_2022_jp : CP50220
	{
		public ENCiso_2022_jp () : base() {}

	}; // class ENCiso_2022_jp
}