-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGB2312Encoding.cs
269 lines (235 loc) · 7.97 KB
/
GB2312Encoding.cs
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
//
// RYTUnZipper
//
// Created by wang.ping on 10/19/12.
// Copyright 2011 RYTong. All rights reserved.
//
using System;
using System.IO;
using System.Text;
using System.Windows;
namespace System.Text
{
public sealed class GB2312Encoding : Encoding
{
private const char LEAD_BYTE_CHAR = '\uFFFE';
private static GB2312Encoding instance = new GB2312Encoding();
public static GB2312Encoding Instance
{
get
{
return instance;
}
}
private GB2312Encoding()
{
if (!BitConverter.IsLittleEndian)
throw new PlatformNotSupportedException("Not supported big endian platform.");
}
public override int GetByteCount(char[] chars, int index, int count)
{
int byteCount = 0;
ushort u;
char c;
for (int i = 0; i < count; index++, byteCount++, i++)
{
c = chars[index];
u = Map.UnicodeToGB2312(c);
if (u > 0xff)
byteCount++;
}
return byteCount;
}
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
{
int byteCount = 0;
ushort u;
char c;
for (int i = 0; i < charCount; charIndex++, byteIndex++, byteCount++, i++)
{
c = chars[charIndex];
u = Map.UnicodeToGB2312(c);
if (u == 0 && c != 0)
{
bytes[byteIndex] = 0x3f; // 0x3f == '?'
}
else if (u < 0x100)
{
bytes[byteIndex] = (byte)u;
}
else
{
bytes[byteIndex] = (byte)((u >> 8) & 0xff);
byteIndex++;
byteCount++;
bytes[byteIndex] = (byte)(u & 0xff);
}
}
return byteCount;
}
public override int GetCharCount(byte[] bytes, int index, int count)
{
return GetCharCount(bytes, index, count, null);
}
private int GetCharCount(byte[] bytes, int index, int count, GB2312Decoder decoder)
{
int charCount = 0;
ushort u;
char c;
for (int i = 0; i < count; index++, charCount++, i++)
{
u = 0;
if (decoder != null && decoder.pendingByte != 0)
{
u = decoder.pendingByte;
decoder.pendingByte = 0;
}
u = (ushort)(u << 8 | bytes[index]);
c = Map.GB2312ToUnicode(u);
if (c == LEAD_BYTE_CHAR)
{
if (i < count - 1)
{
index++;
i++;
}
else if (decoder != null)
{
decoder.pendingByte = bytes[index];
return charCount;
}
}
}
return charCount;
}
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
{
return GetChars(bytes, byteIndex, byteCount, chars, charIndex, null);
}
private int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, GB2312Decoder decoder)
{
int charCount = 0;
ushort u;
char c;
for (int i = 0; i < byteCount; byteIndex++, charIndex++, charCount++, i++)
{
u = 0;
if (decoder != null && decoder.pendingByte != 0)
{
u = decoder.pendingByte;
decoder.pendingByte = 0;
}
u = (ushort)(u << 8 | bytes[byteIndex]);
c = Map.GB2312ToUnicode(u);
if (c == LEAD_BYTE_CHAR)
{
if (i < byteCount - 1)
{
byteIndex++;
i++;
u = (ushort)(u << 8 | bytes[byteIndex]);
c = Map.GB2312ToUnicode(u);
}
else if (decoder == null)
{
c = '\0';
}
else
{
decoder.pendingByte = bytes[byteIndex];
return charCount;
}
}
if (c == 0 && u != 0)
chars[charIndex] = '?';
else
chars[charIndex] = c;
}
return charCount;
}
public override int GetMaxByteCount(int charCount)
{
if (charCount < 0)
throw new ArgumentOutOfRangeException("charCount");
long count = charCount + 1;
count *= 2;
if (count > int.MaxValue)
throw new ArgumentOutOfRangeException("charCount");
return (int)count;
}
public override int GetMaxCharCount(int byteCount)
{
if (byteCount < 0)
throw new ArgumentOutOfRangeException("byteCount");
long count = byteCount + 3;
if (count > int.MaxValue)
throw new ArgumentOutOfRangeException("byteCount");
return (int)count;
}
public override Decoder GetDecoder()
{
return new GB2312Decoder(this);
}
public override string WebName
{
get
{
return "gb2312";
}
}
private static class Map
{
private static ushort[] _gb2312ToUnicode = null;
private static ushort[] _unicodeToGb2312 = null;
static Map()
{
_gb2312ToUnicode = new ushort[0x10000];
_unicodeToGb2312 = new ushort[0x10000];
var assembly = typeof(GB2312Encoding).Assembly;
var resource = assembly.GetManifestResourceStream("System.Text.GB2312Encoding.gb2312.bin");
if (resource != null)
{
using (BinaryReader reader = new BinaryReader(resource))
{
for (int i = 0; i < 0xffff; i++)
{
ushort u = reader.ReadUInt16();
_unicodeToGb2312[i] = u;
}
for (int i = 0; i < 0xffff; i++)
{
ushort u = reader.ReadUInt16();
_gb2312ToUnicode[i] = u;
}
}
}
}
public static char GB2312ToUnicode(ushort code)
{
return (char)_gb2312ToUnicode[code];
}
public static ushort UnicodeToGB2312(char ch)
{
return _unicodeToGb2312[ch];
}
}
private sealed class GB2312Decoder : Decoder
{
private GB2312Decoder() { }
private GB2312Encoding _encoding = null;
public GB2312Decoder(GB2312Encoding encoding)
{
_encoding = encoding;
}
public override int GetCharCount(byte[] bytes, int index, int count)
{
return _encoding.GetCharCount(bytes, index, count, this);
}
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
{
return _encoding.GetChars(bytes, byteIndex, byteCount, chars, charIndex, this);
}
public byte pendingByte;
}
}
}