Debian Codegraph @

mcs/class/I18N/CJK/CP51932.cs

raw symbols SCIP
1 /*
2 * CP51932.cs - Japanese EUC-JP code page.
3 *
4 * It is based on CP932.cs from Portable.NET
5 *
6 * Author:
7 * Atsushi Enomoto <[email protected]>
8 *
9 * Below are original (CP932.cs) copyright lines
10 *
11 * (C)2004 Novell Inc.
12 *
13 * Copyright (c) 2002 Southern Storm Software, Pty Ltd
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining
16 * a copy of this software and associated documentation files (the "Software"),
17 * to deal in the Software without restriction, including without limitation
18 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 * and/or sell copies of the Software, and to permit persons to whom the
20 * Software is furnished to do so, subject to the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be included
23 * in all copies or substantial portions of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
26 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 * OTHER DEALINGS IN THE SOFTWARE.
32 */
33
34 /*
35
36 jis.table is generated from uni2tab.c, which requires CP932.TXT and
37 Unihan.txt as input files. However, it is non-normative and in Japan it is
38 contains many problem.
39
40 FIXME: Some characters such as 0xFF0B (wide "plus") are missing in
41 that table.
42 */
43
44 /*
45 0x00-0x1F, 0x7F : control characters
46 0x20-0x7E : ASCII
47 0xA1A1-0xFEFE : Kanji (precisely, both bytes contain only A1-FE)
48 0x8EA1-0x8EDF : half-width Katakana
49 0x8FA1A1-0x8FFEFE : Complemental Kanji
50
51 */
52
53 namespace I18N.CJK
54 {
55
56 using System;
57 using System.Text;
58 using I18N.Common;
59
60 #if DISABLE_UNSAFE
61 using MonoEncoder = I18N.Common.MonoSafeEncoder;
62 using MonoEncoding = I18N.Common.MonoSafeEncoding;
63 #endif
64
65 [Serializable]
66 public class CP51932 : MonoEncoding
67 {
68 // Magic number used by Windows for the EUC-JP code page.
69 private const int EUC_JP_CODE_PAGE = 51932;
70
71 // Constructor.
72 public CP51932 () : base (EUC_JP_CODE_PAGE, 932)
73 {
74 }
75
76 #if !DISABLE_UNSAFE
77 public unsafe override int GetByteCountImpl (char* chars, int count)
78 {
79 return new CP51932Encoder (this).GetByteCountImpl (chars, count, true);
80 }
81
82 public unsafe override int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount)
83 {
84 return new CP51932Encoder (this).GetBytesImpl (chars, charCount, bytes, byteCount, true);
85 }
86 #else
87 public override int GetByteCount (char [] chars, int index, int length)
88 {
89 return new CP51932Encoder (this).GetByteCount (chars, index, length, true);
90 }
91
92 public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
93 {
94 return new CP51932Encoder (this).GetBytes (chars, charIndex, charCount, bytes, byteIndex, true);
95 }
96 #endif
97
98 public override int GetCharCount (byte [] bytes, int index, int count)
99 {
100 return new CP51932Decoder ().GetCharCount (
101 bytes, index, count, true);
102 }
103
104 public override int GetChars (
105 byte [] bytes, int byteIndex, int byteCount,
106 char [] chars, int charIndex)
107 {
108 return new CP51932Decoder ().GetChars (bytes,
109 byteIndex, byteCount, chars, charIndex, true);
110 }
111
112 // Get the maximum number of bytes needed to encode a
113 // specified number of characters.
114 public override int GetMaxByteCount(int charCount)
115 {
116 if(charCount < 0)
117 {
118 throw new ArgumentOutOfRangeException
119 ("charCount",
120 Strings.GetString("ArgRange_NonNegative"));
121 }
122 return charCount * 3;
123 }
124
125 // Get the maximum number of characters needed to decode a
126 // specified number of bytes.
127 public override int GetMaxCharCount(int byteCount)
128 {
129 if(byteCount < 0)
130 {
131 throw new ArgumentOutOfRangeException
132 ("byteCount",
133 Strings.GetString ("ArgRange_NonNegative"));
134 }
135 return byteCount;
136 }
137
138 public override Encoder GetEncoder ()
139 {
140 return new CP51932Encoder (this);
141 }
142
143 public override Decoder GetDecoder ()
144 {
145 return new CP51932Decoder ();
146 }
147
148 #if !ECMA_COMPAT
149
150 // Get the mail body name for this encoding.
151 public override String BodyName {
152 get { return "euc-jp"; }
153 }
154
155 // Get the human-readable name for this encoding.
156 public override String EncodingName {
157 get { return "Japanese (EUC)"; }
158 }
159
160 // Get the mail agent header name for this encoding.
161 public override String HeaderName {
162 get { return "euc-jp"; }
163 }
164
165 // Determine if this encoding can be displayed in a Web browser.
166 public override bool IsBrowserDisplay {
167 get { return true; }
168 }
169
170 // Determine if this encoding can be saved from a Web browser.
171 public override bool IsBrowserSave {
172 get { return true; }
173 }
174
175 // Determine if this encoding can be displayed in a mail/news agent.
176 public override bool IsMailNewsDisplay {
177 get { return true; }
178 }
179
180 // Determine if this encoding can be saved from a mail/news agent.
181 public override bool IsMailNewsSave {
182 get { return true; }
183 }
184
185 // Get the IANA-preferred Web name for this encoding.
186 public override String WebName {
187 get { return "euc-jp"; }
188 }
189 } // CP51932
190 #endif // !ECMA_COMPAT
191
192 public class CP51932Encoder : MonoEncoder
193 {
194 public CP51932Encoder (MonoEncoding encoding)
195 : base (encoding)
196 {
197 }
198
199 #if !DISABLE_UNSAFE
200 // Get the number of bytes needed to encode a character buffer.
201 public unsafe override int GetByteCountImpl (
202 char* chars, int count, bool refresh)
203 {
204 // Determine the length of the final output.
205 int index = 0;
206 int length = 0;
207 int ch, value;
208 byte [] cjkToJis = JISConvert.Convert.cjkToJis;
209 byte [] extraToJis = JISConvert.Convert.extraToJis;
210
211 while (count > 0) {
212 ch = chars [index++];
213 --count;
214 ++length;
215 if (ch < 0x0080) {
216 // Character maps to itself.
217 continue;
218 } else if (ch < 0x0100) {
219 // Check for special Latin 1 characters that
220 // can be mapped to double-byte code points.
221 if(ch == 0x00A2 || ch == 0x00A3 || ch == 0x00A7 ||
222 ch == 0x00A8 || ch == 0x00AC || ch == 0x00B0 ||
223 ch == 0x00B1 || ch == 0x00B4 || ch == 0x00B6 ||
224 ch == 0x00D7 || ch == 0x00F7)
225 {
226 ++length;
227 }
228 } else if (ch >= 0x0391 && ch <= 0x0451) {
229 // Greek subset characters.
230 ++length;
231 } else if (ch >= 0x2010 && ch <= 0x9FA5) {
232 // This range contains the bulk of the CJK set.
233 value = (ch - 0x2010) * 2;
234 value = ((int) (cjkToJis[value])) | (((int)(cjkToJis[value + 1])) << 8);
235 if(value >= 0x0100)
236 ++length;
237 } else if(ch >= 0xFF01 && ch < 0xFF60) {
238 // This range contains extra characters.
239 value = (ch - 0xFF01) * 2;
240 value = ((int)(extraToJis[value])) |
241 (((int)(extraToJis[value + 1])) << 8);
242 if(value >= 0x0100)
243 ++length;
244 } else if(ch >= 0xFF60 && ch <= 0xFFA0) {
245 ++length; // half-width kana
246 }
247 }
248
249 // Return the length to the caller.
250 return length;
251 }
252
253 // Get the bytes that result from encoding a character buffer.
254 public unsafe override int GetBytesImpl (
255 char* chars, int charCount, byte* bytes, int byteCount, bool refresh)
256 {
257 int charIndex = 0;
258 int byteIndex = 0;
259 int end = charCount;
260
261 // Convert the characters into their byte form.
262 int posn = byteIndex;
263 int byteLength = byteCount;
264 int ch, value;
265
266 byte[] cjkToJis = JISConvert.Convert.cjkToJis;
267 byte[] greekToJis = JISConvert.Convert.greekToJis;
268 byte[] extraToJis = JISConvert.Convert.extraToJis;
269
270 for (int i = charIndex; i < end; i++, charCount--) {
271 ch = chars [i];
272 if (posn >= byteLength) {
273 throw new ArgumentException (Strings.GetString ("Arg_InsufficientSpace"), "bytes");
274 }
275
276 if (ch < 0x0080) {
277 // Character maps to itself.
278 bytes[posn++] = (byte)ch;
279 continue;
280 } else if (ch >= 0x0391 && ch <= 0x0451) {
281 // Greek subset characters.
282 value = (ch - 0x0391) * 2;
283 value = ((int)(greekToJis[value])) |
284 (((int)(greekToJis[value + 1])) << 8);
285 } else if (ch >= 0x2010 && ch <= 0x9FA5) {
286 // This range contains the bulk of the CJK set.
287 value = (ch - 0x2010) * 2;
288 value = ((int) (cjkToJis[value])) |
289 (((int)(cjkToJis[value + 1])) << 8);
290 } else if (ch >= 0xFF01 && ch <= 0xFF60) {
291 // This range contains extra characters,
292 // including half-width katakana.
293 value = (ch - 0xFF01) * 2;
294 value = ((int) (extraToJis [value])) |
295 (((int) (extraToJis [value + 1])) << 8);
296 } else if (ch >= 0xFF60 && ch <= 0xFFA0) {
297 value = ch - 0xFF60 + 0x8EA0;
298 } else {
299 // Invalid character.
300 value = 0;
301 }
302
303 if (value == 0) {
304 HandleFallback (
305 chars, ref i, ref charCount,
306 bytes, ref posn, ref byteCount, null);
307 } else if (value < 0x0100) {
308 bytes [posn++] = (byte) value;
309 } else if ((posn + 1) >= byteLength) {
310 throw new ArgumentException (Strings.GetString ("Arg_InsufficientSpace"), "bytes");
311 } else if (value < 0x8000) {
312 // general 2byte glyph/kanji
313 value -= 0x0100;
314 bytes [posn++] = (byte) (value / 0x5E + 0xA1);
315 bytes [posn++] = (byte) (value % 0x5E + 0xA1);
316 //Console.WriteLine ("{0:X04}", ch);
317 continue;
318 }
319 else
320 {
321 // half-width kana
322 bytes [posn++] = 0x8E;
323 bytes [posn++] = (byte) (value - 0x8E00);
324 }
325 }
326
327 // Return the final length to the caller.
328 return posn - byteIndex;
329 }
330 #else
331 // Get the number of bytes needed to encode a character buffer.
332 public override int GetByteCount(char[] chars, int index, int count, bool flush)
333 {
334 // Determine the length of the final output.
335 int length = 0;
336 int ch, value;
337 byte[] cjkToJis = JISConvert.Convert.cjkToJis;
338 byte[] extraToJis = JISConvert.Convert.extraToJis;
339
340 while (count > 0)
341 {
342 ch = chars[index++];
343 --count;
344 ++length;
345 if (ch < 0x0080)
346 {
347 // Character maps to itself.
348 continue;
349 }
350 else if (ch < 0x0100)
351 {
352 // Check for special Latin 1 characters that
353 // can be mapped to double-byte code points.
354 if (ch == 0x00A2 || ch == 0x00A3 || ch == 0x00A7 ||
355 ch == 0x00A8 || ch == 0x00AC || ch == 0x00B0 ||
356 ch == 0x00B1 || ch == 0x00B4 || ch == 0x00B6 ||
357 ch == 0x00D7 || ch == 0x00F7)
358 {
359 ++length;
360 }
361 }
362 else if (ch >= 0x0391 && ch <= 0x0451)
363 {
364 // Greek subset characters.
365 ++length;
366 }
367 else if (ch >= 0x2010 && ch <= 0x9FA5)
368 {
369 // This range contains the bulk of the CJK set.
370 value = (ch - 0x2010) * 2;
371 value = ((int)(cjkToJis[value])) | (((int)(cjkToJis[value + 1])) << 8);
372 if (value >= 0x0100)
373 ++length;
374 }
375 else if (ch >= 0xFF01 && ch < 0xFF60)
376 {
377 // This range contains extra characters.
378 value = (ch - 0xFF01) * 2;
379 value = ((int)(extraToJis[value])) |
380 (((int)(extraToJis[value + 1])) << 8);
381 if (value >= 0x0100)
382 ++length;
383 }
384 else if (ch >= 0xFF60 && ch <= 0xFFA0)
385 {
386 ++length; // half-width kana
387 }
388 }
389
390 // Return the length to the caller.
391 return length;
392 }
393
394 // Get the bytes that result from encoding a character buffer.
395 public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush)
396 {
397 // Convert the characters into their byte form.
398 int posn = byteIndex;
399 int byteLength = bytes.Length;
400 int byteCount = bytes.Length;
401 int end = charIndex + charCount;
402 int ch, value;
403
404 byte[] cjkToJis = JISConvert.Convert.cjkToJis;
405 byte[] greekToJis = JISConvert.Convert.greekToJis;
406 byte[] extraToJis = JISConvert.Convert.extraToJis;
407
408 for (int i = charIndex; i < end; i++, charCount--)
409 {
410 ch = chars[i];
411 if (posn >= byteLength)
412 {
413 throw new ArgumentException(Strings.GetString("Arg_InsufficientSpace"), "bytes");
414 }
415
416 if (ch < 0x0080)
417 {
418 // Character maps to itself.
419 bytes[posn++] = (byte)ch;
420 continue;
421 }
422 else if (ch >= 0x0391 && ch <= 0x0451)
423 {
424 // Greek subset characters.
425 value = (ch - 0x0391) * 2;
426 value = ((int)(greekToJis[value])) |
427 (((int)(greekToJis[value + 1])) << 8);
428 }
429 else if (ch >= 0x2010 && ch <= 0x9FA5)
430 {
431 // This range contains the bulk of the CJK set.
432 value = (ch - 0x2010) * 2;
433 value = ((int)(cjkToJis[value])) |
434 (((int)(cjkToJis[value + 1])) << 8);
435 }
436 else if (ch >= 0xFF01 && ch <= 0xFF60)
437 {
438 // This range contains extra characters,
439 // including half-width katakana.
440 value = (ch - 0xFF01) * 2;
441 value = ((int)(extraToJis[value])) |
442 (((int)(extraToJis[value + 1])) << 8);
443 }
444 else if (ch >= 0xFF60 && ch <= 0xFFA0)
445 {
446 value = ch - 0xFF60 + 0x8EA0;
447 }
448 else
449 {
450 // Invalid character.
451 value = 0;
452 }
453
454 if (value == 0)
455 {
456 HandleFallback (chars, ref i, ref charCount,
457 bytes, ref posn, ref byteCount, null);
458 }
459 else if (value < 0x0100)
460 {
461 bytes[posn++] = (byte)value;
462 }
463 else if ((posn + 1) >= byteLength)
464 {
465 throw new ArgumentException(Strings.GetString("Arg_InsufficientSpace"), "bytes");
466 }
467 else if (value < 0x8000)
468 {
469 // general 2byte glyph/kanji
470 value -= 0x0100;
471 bytes[posn++] = (byte)(value / 0x5E + 0xA1);
472 bytes[posn++] = (byte)(value % 0x5E + 0xA1);
473 //Console.WriteLine ("{0:X04}", ch);
474 continue;
475 }
476 else
477 {
478 // half-width kana
479 bytes[posn++] = 0x8E;
480 bytes[posn++] = (byte)(value - 0x8E00);
481 }
482 }
483
484 // Return the final length to the caller.
485 return posn - byteIndex;
486 }
487 #endif
488 } // CP51932Encoder
489
490 internal class CP51932Decoder : DbcsEncoding.DbcsDecoder
491 {
492 public CP51932Decoder ()
493 : base (null)
494 {
495 }
496
497 int last_count, last_bytes;
498
499 // Get the number of characters needed to decode a byte buffer.
500 public override int GetCharCount (byte [] bytes, int index, int count)
501 {
502 return GetCharCount (bytes, index, count, false);
503 }
504
505 public override
506 int GetCharCount (byte [] bytes, int index, int count, bool refresh)
507 {
508 CheckRange (bytes, index, count);
509
510 // Determine the total length of the converted string.
511 int value = 0;
512 byte[] table0208 = JISConvert.Convert.jisx0208ToUnicode;
513 byte[] table0212 = JISConvert.Convert.jisx0212ToUnicode;
514 int length = 0;
515 int byteval = 0;
516 int last = last_count;
517
518 while (count > 0) {
519 byteval = bytes [index++];
520 --count;
521 if (last == 0) {
522 if (byteval == 0x8F) {
523 // SS3: One-time triple-byte sequence should follow.
524 last = byteval;
525 } else if (byteval <= 0x7F) {
526 // Ordinary ASCII/Latin1/Control character.
527 length++;
528 } else if (byteval == 0x8E) {
529 // SS2: One-time double-byte sequence should follow.
530 last = byteval;
531 } else if (byteval >= 0xA1 && byteval <= 0xFE) {
532 // First byte in a double-byte sequence.
533 last = byteval;
534 } else {
535 // Invalid first byte.
536 length++;
537 }
538 }
539 else if (last == 0x8E) {
540 // SS2 (One-time double-byte sequence)
541 if (byteval >= 0xA1 && byteval <= 0xDF) {
542 length++;
543 } else {
544 // Invalid second byte.
545 length++;
546 }
547 last =0;
548 }
549 else if (last == 0x8F) {
550 // SS3: 3-byte character
551 // FIXME: not supported (I don't think iso-2022-jp has)
552 last = byteval;
553 }
554 else
555 {
556 // Second byte in a double-byte sequence.
557 value = (last - 0xA1) * 0x5E;
558 last = 0;
559 if (byteval >= 0xA1 && byteval <= 0xFE)
560 {
561 value += (byteval - 0xA1);
562 }
563 else
564 {
565 // Invalid second byte.
566 last = 0;
567 length++;
568 continue;
569 }
570
571 value *= 2;
572 value = ((int) (table0208 [value]))
573 | (((int) (table0208 [value + 1])) << 8);
574 if (value == 0)
575 value = ((int) (table0212 [value]))
576 | (((int) (table0212 [value + 1])) << 8);
577 if (value != 0)
578 length++;
579 else
580 length++;
581 }
582 }
583
584 // seems like .NET 2.0 adds \u30FB for insufficient
585 // byte seuqence (for Japanese \u30FB makes sense).
586 if (refresh && last != 0)
587 length++;
588 else
589 last_count = last;
590
591 // Return the final length to the caller.
592 return length;
593 }
594
595 public override int GetChars (byte[] bytes, int byteIndex,
596 int byteCount, char[] chars,
597 int charIndex)
598 {
599 return GetChars (bytes, byteIndex, byteCount, chars, charIndex, false);
600 }
601
602 public override
603 int GetChars (byte[] bytes, int byteIndex,
604 int byteCount, char[] chars,
605 int charIndex, bool refresh)
606 {
607 CheckRange (bytes, byteIndex, byteCount, chars, charIndex);
608
609 // Decode the bytes in the buffer.
610 int posn = charIndex;
611 int charLength = chars.Length;
612 int byteval, value;
613 int last = last_bytes;
614 byte[] table0208 = JISConvert.Convert.jisx0208ToUnicode;
615 byte[] table0212 = JISConvert.Convert.jisx0212ToUnicode;
616
617 while (byteCount > 0) {
618 byteval = bytes [byteIndex++];
619 --byteCount;
620 if (last == 0) {
621 if (byteval == 0x8F) {
622 // SS3 (One-time triple-byte sequence) should follow.
623 last = byteval;
624 } else if (byteval <= 0x7F) {
625 // Ordinary ASCII/Latin1/Control character.
626 if (posn >= charLength)
627 throw Insufficient ();
628 chars [posn++] = (char) byteval;
629 } else if (byteval == 0x8E) {
630 // SS2 (One-time double-byte sequence) should follow.
631 last = byteval;
632 } else if (byteval >= 0xA1 && byteval <= 0xFE) {
633 // First byte in a double-byte sequence.
634 last = byteval;
635 } else {
636 // Invalid first byte.
637 if (posn >= charLength)
638 throw Insufficient ();
639 chars [posn++] = '\u30FB';
640 }
641 }
642 else if (last == 0x8E) {
643 // SS2 (One-time double-byte sequence)
644 if (byteval >= 0xA1 && byteval <= 0xDF) {
645 value = ((byteval - 0x40) |
646 (last + 0x71) << 8);
647 if (posn >= charLength)
648 throw Insufficient ();
649 chars [posn++] = (char) value;
650 } else {
651 // Invalid second byte.
652 if (posn >= charLength)
653 throw Insufficient ();
654 chars [posn++] = '\u30FB';
655 }
656 last =0;
657 }
658 else if (last == 0x8F) {
659 // SS3: 3-byte character
660 // FIXME: not supported (I don't think iso-2022-jp has)
661 last = byteval;
662 }
663 else
664 {
665 // Second byte in a double-byte sequence.
666 value = (last - 0xA1) * 0x5E;
667 last = 0;
668 if (byteval >= 0xA1 && byteval <= 0xFE)
669 {
670 value += (byteval - 0xA1);
671 }
672 else
673 {
674 // Invalid second byte.
675 last = 0;
676 if (posn >= charLength)
677 throw Insufficient ();
678 chars [posn++] = '\u30FB';
679 continue;
680 }
681
682 value *= 2;
683 value = ((int) (table0208 [value]))
684 | (((int) (table0208 [value + 1])) << 8);
685 if (value == 0)
686 value = ((int) (table0212 [value]))
687 | (((int) (table0212 [value + 1])) << 8);
688 if (posn >= charLength)
689 throw Insufficient ();
690 if (value != 0)
691 chars [posn++] = (char)value;
692 else
693 chars [posn++] = '\u30FB';
694 }
695 }
696
697 if (refresh && last != 0) {
698 // seems like .NET 2.0 adds \u30FB for insufficient
699 // byte seuqence (for Japanese \u30FB makes sense).
700 if (posn >= charLength)
701 throw Insufficient ();
702 chars [posn++] = '\u30FB';
703 }
704 else
705 last_bytes = last;
706
707 // Return the final length to the caller.
708 return posn - charIndex;
709 }
710
711 Exception Insufficient ()
712 {
713 throw new ArgumentException
714 (Strings.GetString
715 ("Arg_InsufficientSpace"), "chars");
716 }
717 }; // class CP51932Decoder
718
719 [Serializable]
720 public class ENCeuc_jp : CP51932
721 {
722 public ENCeuc_jp () : base() {}
723
724 }; // class ENCeucjp
725
726 }; // namespace I18N.CJK