5 // Alan Tam Siu Lung (Tam@SiuLung.com)
14 internal class CP950
: DbcsEncoding
16 // Magic number used by Windows for the Big5 code page.
17 private const int BIG5_CODE_PAGE
= 950;
20 public CP950() : base(BIG5_CODE_PAGE
) {
21 convert
= Big5Convert
.Convert
;
24 // Get the bytes that result from encoding a character buffer.
25 public override int GetBytes(char[] chars
, int charIndex
, int charCount
,
26 byte[] bytes
, int byteIndex
)
29 base.GetBytes(chars
, charIndex
, charCount
, bytes
, byteIndex
);
30 int origIndex
= byteIndex
;
31 while (charCount
-- > 0) {
32 char c
= chars
[charIndex
++];
33 if (c
<= 0x80 || c
== 0xFF) { // ASCII
34 bytes
[byteIndex
++] = (byte)c
;
37 byte b1
= convert
.u2n
[((int)c
) * 2 + 1];
38 byte b2
= convert
.u2n
[((int)c
) * 2];
39 if (b1
== 0 && b2
== 0) {
40 bytes
[byteIndex
++] = (byte)'?';
42 bytes
[byteIndex
++] = b1
;
43 bytes
[byteIndex
++] = b2
;
46 return byteIndex
- origIndex
;
49 // Get the characters that result from decoding a byte buffer.
50 public override int GetChars(byte[] bytes
, int byteIndex
, int byteCount
,
51 char[] chars
, int charIndex
)
54 base.GetChars(bytes
, byteIndex
, byteCount
, chars
, charIndex
);
55 int origIndex
= charIndex
;
57 while (byteCount
-- > 0) {
58 int b
= bytes
[byteIndex
++];
60 if (b
<= 0x80 || b
== 0xFF) { // ASCII
61 chars
[charIndex
++] = (char)b
;
63 } else if (b
< 0xA1 || b
>= 0xFA) {
70 int ord
= ((lastByte
- 0xA1) * 191 + b
- 0x40) * 2;
71 char c1
= (char)(convert
.n2u
[ord
] + convert
.n2u
[ord
+ 1] * 256);
73 chars
[charIndex
++] = '?';
75 chars
[charIndex
++] = c1
;
78 return charIndex
- origIndex
;
81 // Get a decoder that handles a rolling Big5 state.
82 public override Decoder
GetDecoder()
84 return new CP950Decoder(convert
);
87 // Get the mail body name for this encoding.
88 public override String BodyName
90 get { return "big5"; }
93 // Get the human-readable name for this encoding.
94 public override String EncodingName
96 get { return "Chinese Traditional (Big5)"; }
99 // Get the mail agent header name for this encoding.
100 public override String HeaderName
102 get { return "big5"; }
105 // Get the IANA-preferred Web name for this encoding.
106 public override String WebName
108 get { return "big5"; }
112 // Get the Windows code page represented by this object.
113 public override int WindowsCodePage
115 get { return BIG5_PAGE; }
119 // Decoder that handles a rolling Big5 state.
120 private sealed class CP950Decoder
: DbcsDecoder
123 public CP950Decoder(DbcsConvert convert
) : base(convert
) {}
125 public override int GetChars(byte[] bytes
, int byteIndex
, int byteCount
,
126 char[] chars
, int charIndex
)
128 base.GetChars(bytes
, byteIndex
, byteCount
, chars
, charIndex
);
129 int origIndex
= charIndex
;
130 while (byteCount
-- > 0) {
131 int b
= bytes
[byteIndex
++];
133 if (b
<= 0x80 || b
== 0xFF) { // ASCII
134 chars
[charIndex
++] = (char)b
;
136 } else if (b
< 0xA1 || b
>= 0xFA) {
143 int ord
= ((lastByte
- 0xA1) * 191 + b
- 0x40) * 2;
144 char c1
= (char)(convert
.n2u
[ord
] + convert
.n2u
[ord
+ 1] * 256);
146 chars
[charIndex
++] = '?';
148 chars
[charIndex
++] = c1
;
152 return charIndex
- origIndex
;
157 internal class ENCbig5
: CP950