| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
using System; |
| 8 |
using System.Globalization; |
| 9 |
using System.Reflection; |
| 10 |
using System.Text; |
| 11 |
using System.Runtime.InteropServices; |
| 12 |
#if BUILD_GENERATOR |
| 13 |
using System.IO; |
| 14 |
using System.Xml; |
| 15 |
#endif |
| 16 |
|
| 17 |
namespace I18N.CJK |
| 18 |
{ |
| 19 |
internal unsafe class GB18030Source |
| 20 |
{ |
| 21 |
class GB18030Map |
| 22 |
{ |
| 23 |
public readonly int UStart; |
| 24 |
public readonly int UEnd; |
| 25 |
public readonly long GStart; |
| 26 |
public readonly long GEnd; |
| 27 |
public readonly bool Dummy; |
| 28 |
|
| 29 |
public GB18030Map ( |
| 30 |
int ustart, int uend, long gstart, long gend, bool dummy) |
| 31 |
{ |
| 32 |
this.UStart = ustart; |
| 33 |
this.UEnd = uend; |
| 34 |
this.GStart = gstart; |
| 35 |
this.GEnd = gend; |
| 36 |
this.Dummy = dummy; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
private GB18030Source () |
| 41 |
{ |
| 42 |
} |
| 43 |
|
| 44 |
static readonly byte *gbx2uni; |
| 45 |
static readonly byte *uni2gbx; |
| 46 |
static readonly int gbx2uniSize, uni2gbxSize; |
| 47 |
|
| 48 |
static GB18030Source () |
| 49 |
{ |
| 50 |
MethodInfo mi = typeof (Assembly).GetMethod ( |
| 51 |
"GetManifestResourceInternal", |
| 52 |
BindingFlags.NonPublic | BindingFlags.Instance); |
| 53 |
|
| 54 |
int size = 0; |
| 55 |
Module mod = null; |
| 56 |
IntPtr ret = IntPtr.Zero; |
| 57 |
|
| 58 |
if (mi != null) |
| 59 |
{ |
| 60 |
ret = (IntPtr)mi.Invoke( |
| 61 |
Assembly.GetExecutingAssembly(), |
| 62 |
new object[] { "gb18030.table", size, mod }); |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
|
| 67 |
using (var ms = Assembly.GetExecutingAssembly() |
| 68 |
.GetManifestResourceStream("gb18030.table")) |
| 69 |
{ |
| 70 |
var len = (int)ms.Length; |
| 71 |
byte* buf = (byte*)Marshal.AllocHGlobal(sizeof(byte) * len); |
| 72 |
|
| 73 |
for (int i = 0; i < len; i++) |
| 74 |
buf[i] = (byte)ms.ReadByte(); |
| 75 |
|
| 76 |
ret = (IntPtr)buf; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if (ret != IntPtr.Zero) { |
| 81 |
gbx2uni = (byte*) ((void*) ret); |
| 82 |
gbx2uniSize = |
| 83 |
(gbx2uni [0] << 24) + (gbx2uni [1] << 16) + |
| 84 |
(gbx2uni [2] << 8) + (gbx2uni [3]); |
| 85 |
gbx2uni += 4; |
| 86 |
uni2gbx = gbx2uni + gbx2uniSize; |
| 87 |
uni2gbxSize = |
| 88 |
(uni2gbx [0] << 24) + (uni2gbx [1] << 16) + |
| 89 |
(uni2gbx [2] << 8) + (uni2gbx [3]); |
| 90 |
uni2gbx += 4; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
static readonly long gbxBase = |
| 95 |
FromGBXRaw (0x81, 0x30, 0x81, 0x30, false); |
| 96 |
static readonly long gbxSuppBase = |
| 97 |
FromGBXRaw (0x90, 0x30, 0x81, 0x30, false); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
static readonly GB18030Map [] ranges = new GB18030Map [] { |
| 102 |
|
| 103 |
new GB18030Map (0x0452, 0x200F, FromGBXRaw (0x81, 0x30, 0xD3, 0x30, false), FromGBXRaw (0x81, 0x36, 0xA5, 0x31, false), false), |
| 104 |
|
| 105 |
new GB18030Map (0x2643, 0x2E80, FromGBXRaw (0x81, 0x37, 0xA8, 0x39, false), FromGBXRaw (0x81, 0x38, 0xFD, 0x38, false), false), |
| 106 |
|
| 107 |
new GB18030Map (0x361B, 0x3917, FromGBXRaw (0x82, 0x30, 0xA6, 0x33, false), FromGBXRaw (0x82, 0x30, 0xF2, 0x37, false), false), |
| 108 |
|
| 109 |
new GB18030Map (0x3CE1, 0x4055, FromGBXRaw (0x82, 0x31, 0xD4, 0x38, false), FromGBXRaw (0x82, 0x32, 0xAF, 0x32, false), false), |
| 110 |
|
| 111 |
new GB18030Map (0x4160, 0x4336, FromGBXRaw (0x82, 0x32, 0xC9, 0x37, false), FromGBXRaw (0x82, 0x32, 0xF8, 0x37, false), false), |
| 112 |
|
| 113 |
new GB18030Map (0x44D7, 0x464B, FromGBXRaw (0x82, 0x33, 0xA3, 0x39, false), FromGBXRaw (0x82, 0x33, 0xC9, 0x31, false), false), |
| 114 |
|
| 115 |
new GB18030Map (0x478E, 0x4946, FromGBXRaw (0x82, 0x33, 0xE8, 0x38, false), FromGBXRaw (0x82, 0x34, 0x96, 0x38, false), false), |
| 116 |
|
| 117 |
new GB18030Map (0x49B8, 0x4C76, FromGBXRaw (0x82, 0x34, 0xA1, 0x31, false), FromGBXRaw (0x82, 0x34, 0xE7, 0x33, false), false), |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
new GB18030Map (0x4E00, 0x9FA5, 0, 0, true), |
| 122 |
|
| 123 |
new GB18030Map (0x9FA6, 0xD7FF, FromGBXRaw (0x82, 0x35, 0x8F, 0x33, false), FromGBXRaw (0x83, 0x36, 0xC7, 0x38, false), false), |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
new GB18030Map (0xD800, 0xE76B, 0, 0, true), |
| 128 |
|
| 129 |
|
| 130 |
new GB18030Map (0xE865, 0xF92B, FromGBXRaw (0x83, 0x36, 0xD0, 0x30, false), FromGBXRaw (0x84, 0x30, 0x85, 0x34, false), false), |
| 131 |
|
| 132 |
new GB18030Map (0xFA2A, 0xFE2F, FromGBXRaw (0x84, 0x30, 0x9C, 0x38, false), FromGBXRaw (0x84, 0x31, 0x85, 0x37, false), false), |
| 133 |
|
| 134 |
new GB18030Map (0xFFE6, 0xFFFF, FromGBXRaw (0x84, 0x31, 0xA2, 0x34, false), FromGBXRaw (0x84, 0x31, 0xA4, 0x39, false), false), |
| 135 |
}; |
| 136 |
|
| 137 |
public static void Unlinear (byte [] bytes, int start, long gbx) |
| 138 |
{ |
| 139 |
fixed (byte* bptr = bytes) { |
| 140 |
Unlinear (bptr + start, gbx); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
public static unsafe void Unlinear (byte* bytes, long gbx) |
| 145 |
{ |
| 146 |
bytes [3] = (byte) (gbx % 10 + 0x30); |
| 147 |
gbx /= 10; |
| 148 |
bytes [2] = (byte) (gbx % 126 + 0x81); |
| 149 |
gbx /= 126; |
| 150 |
bytes [1] = (byte) (gbx % 10 + 0x30); |
| 151 |
gbx /= 10; |
| 152 |
bytes [0] = (byte) (gbx + 0x81); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
public static long FromGBX (byte [] bytes, int start) |
| 157 |
{ |
| 158 |
byte b1 = bytes [start]; |
| 159 |
byte b2 = bytes [start + 1]; |
| 160 |
byte b3 = bytes [start + 2]; |
| 161 |
byte b4 = bytes [start + 3]; |
| 162 |
if (b1 < 0x81 || b1 == 0xFF) |
| 163 |
return -1; |
| 164 |
if (b2 < 0x30 || b2 > 0x39) |
| 165 |
return -2; |
| 166 |
if (b3 < 0x81 || b3 == 0xFF) |
| 167 |
return -3; |
| 168 |
if (b4 < 0x30 || b4 > 0x39) |
| 169 |
return -4; |
| 170 |
if (b1 >= 0x90) |
| 171 |
return FromGBXRaw (b1, b2, b3, b4, true); |
| 172 |
long linear = FromGBXRaw (b1, b2, b3, b4, false); |
| 173 |
|
| 174 |
long rawOffset = 0; |
| 175 |
long startIgnore = 0; |
| 176 |
|
| 177 |
for (int i = 0; i < ranges.Length; i++) { |
| 178 |
GB18030Map m = ranges [i]; |
| 179 |
if (linear < m.GStart) |
| 180 |
return ToUcsRaw ((int) (linear |
| 181 |
- startIgnore + rawOffset)); |
| 182 |
if (linear <= m.GEnd) |
| 183 |
return linear - gbxBase - m.GStart |
| 184 |
+ m.UStart; |
| 185 |
if (m.GStart != 0) { |
| 186 |
rawOffset += m.GStart - startIgnore; |
| 187 |
startIgnore = m.GEnd + 1; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
return -4; |
| 194 |
} |
| 195 |
|
| 196 |
public static long FromUCSSurrogate (int cp) |
| 197 |
{ |
| 198 |
return cp + gbxSuppBase; |
| 199 |
} |
| 200 |
|
| 201 |
public static long FromUCS (int cp) |
| 202 |
{ |
| 203 |
long rawOffset = 0; |
| 204 |
long startIgnore = 0x80; |
| 205 |
for (int i = 0; i < ranges.Length; i++) { |
| 206 |
GB18030Map m = ranges [i]; |
| 207 |
if (cp < m.UStart) |
| 208 |
return ToGbxRaw ((int) (cp |
| 209 |
- startIgnore + rawOffset)); |
| 210 |
if (cp <= m.UEnd) |
| 211 |
return cp - m.UStart + m.GStart; |
| 212 |
if (m.GStart != 0) { |
| 213 |
rawOffset += m.UStart - startIgnore; |
| 214 |
startIgnore = m.UEnd + 1; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
return -1; |
| 220 |
} |
| 221 |
|
| 222 |
static long FromGBXRaw ( |
| 223 |
byte b1, byte b2, byte b3, byte b4, bool supp) |
| 224 |
{ |
| 225 |
|
| 226 |
return (((b1 - (supp ? 0x90 : 0x81)) * 10 + |
| 227 |
(b2 - 0x30)) * 126 + |
| 228 |
(b3 - 0x81)) * 10 + |
| 229 |
b4 - 0x30 + (supp ? 0x10000 : 0); |
| 230 |
} |
| 231 |
|
| 232 |
static int ToUcsRaw (int idx) |
| 233 |
{ |
| 234 |
return gbx2uni [idx * 2] * 0x100 + |
| 235 |
gbx2uni [idx * 2 + 1]; |
| 236 |
} |
| 237 |
|
| 238 |
static long ToGbxRaw (int idx) |
| 239 |
{ |
| 240 |
if (idx < 0 || idx * 2 + 1 >= uni2gbxSize) |
| 241 |
return -1; |
| 242 |
return gbxBase + uni2gbx [idx * 2] * 0x100 + uni2gbx [idx * 2 + 1]; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
#if BUILD_GENERATOR |
| 247 |
public static void Main () |
| 248 |
{ |
| 249 |
new GB18030Source ().Run (); |
| 250 |
} |
| 251 |
|
| 252 |
byte [] uni2gbxMap; |
| 253 |
byte [] gbx2uniMap; |
| 254 |
|
| 255 |
void Run () |
| 256 |
{ |
| 257 |
int ustart = 0x80; |
| 258 |
long gstart = 0; |
| 259 |
int ucount = 0; |
| 260 |
long gcount = 0; |
| 261 |
bool skip = false; |
| 262 |
for (int i = 0; i < ranges.Length; i++) { |
| 263 |
GB18030Map m = ranges [i]; |
| 264 |
if (!skip) { |
| 265 |
|
| 266 |
ucount += m.UStart - ustart; |
| 267 |
} |
| 268 |
if (m.GStart != 0) |
| 269 |
gcount += m.GStart - gstart; |
| 270 |
skip = m.GStart == 0; |
| 271 |
ustart = m.UEnd + 1; |
| 272 |
if (m.GStart != 0) |
| 273 |
gstart = m.GEnd + 1; |
| 274 |
} |
| 275 |
|
| 276 |
Console.Error.WriteLine ("Total UCS codepoints: {0} ({1:X04})", ucount, ucount); |
| 277 |
Console.Error.WriteLine ("Total GBX codepoints: {0} ({1:X04})", gcount, gcount); |
| 278 |
|
| 279 |
uni2gbxMap = new byte [ucount * 2]; |
| 280 |
gbx2uniMap = new byte [gcount * 2]; |
| 281 |
|
| 282 |
XmlDocument doc = new XmlDocument (); |
| 283 |
doc.XmlResolver = null; |
| 284 |
doc.Load ("gb-18030-2000.xml"); |
| 285 |
foreach (XmlElement e in doc.SelectNodes ( |
| 286 |
"/characterMapping/assignments/a")) |
| 287 |
AddMap (e); |
| 288 |
|
| 289 |
using (FileStream fs = File.Create ("gb18030.table")) { |
| 290 |
byte [] size = new byte [4]; |
| 291 |
for (int i = 0, len = gbx2uniMap.Length; |
| 292 |
i < 4; i++, len >>= 8) |
| 293 |
size [3 - i] = (byte) (len % 0x100); |
| 294 |
fs.Write (size, 0, 4); |
| 295 |
fs.Write (gbx2uniMap, 0, gbx2uniMap.Length); |
| 296 |
fs.Write (uni2gbxMap, 0, uni2gbxMap.Length); |
| 297 |
} |
| 298 |
Console.WriteLine ("done."); |
| 299 |
} |
| 300 |
|
| 301 |
void AddMap (XmlElement e) |
| 302 |
{ |
| 303 |
int u = int.Parse (e.GetAttribute ("u"), |
| 304 |
NumberStyles.HexNumber); |
| 305 |
byte [] b = new byte [4]; |
| 306 |
int idx = 0; |
| 307 |
foreach (string s in e.GetAttribute ("b").Split (' ')) |
| 308 |
b [idx++] = |
| 309 |
byte.Parse (s, NumberStyles.HexNumber); |
| 310 |
if (idx != 4) |
| 311 |
return; |
| 312 |
|
| 313 |
AddMap (u, b); |
| 314 |
} |
| 315 |
|
| 316 |
void AddMap (int u, byte [] b) |
| 317 |
{ |
| 318 |
int gbx = (int) (FromGBXRaw ( |
| 319 |
b [0], b [1], b [2], b [3], false) - gbxBase); |
| 320 |
if (u > 0x10000 || gbx > 0x10000) |
| 321 |
throw new Exception (String.Format ( |
| 322 |
"should not happen: {0:X04} {1:X04}", |
| 323 |
u, gbx)); |
| 324 |
|
| 325 |
int uidx = IndexForUcs (u); |
| 326 |
|
| 327 |
uni2gbxMap [uidx * 2] = (byte) (gbx / 0x100); |
| 328 |
uni2gbxMap [uidx * 2 + 1] = (byte) (gbx % 0x100); |
| 329 |
|
| 330 |
int gidx = IndexForGbx (gbx); |
| 331 |
|
| 332 |
gbx2uniMap [gidx * 2] = (byte) (u / 0x100); |
| 333 |
gbx2uniMap [gidx * 2 + 1] = (byte) (u % 0x100); |
| 334 |
} |
| 335 |
|
| 336 |
static int IndexForUcs (int ucs) |
| 337 |
{ |
| 338 |
int start = 0x80; |
| 339 |
int count = 0; |
| 340 |
bool skip = false; |
| 341 |
for (int i = 0; i < ranges.Length; i++) { |
| 342 |
GB18030Map m = ranges [i]; |
| 343 |
if (!skip) { |
| 344 |
if (ucs < m.UStart) |
| 345 |
return count + ucs - start; |
| 346 |
count += m.UStart - start; |
| 347 |
} |
| 348 |
skip = m.GStart == 0; |
| 349 |
start = m.UEnd + 1; |
| 350 |
} |
| 351 |
return -1; |
| 352 |
} |
| 353 |
|
| 354 |
static int IndexForGbx (int gbx) |
| 355 |
{ |
| 356 |
long start = 0; |
| 357 |
long count = 0; |
| 358 |
for (int i = 0; i < ranges.Length; i++) { |
| 359 |
GB18030Map m = ranges [i]; |
| 360 |
if (m.GStart == 0) |
| 361 |
continue; |
| 362 |
if (gbx < m.GStart) |
| 363 |
return (int) (count + gbx - start); |
| 364 |
count += m.GStart - start; |
| 365 |
start = m.GEnd + 1; |
| 366 |
} |
| 367 |
return -1; |
| 368 |
} |
| 369 |
|
| 370 |
#endif |
| 371 |
|
| 372 |
|
| 373 |
} |
| 374 |
|
| 375 |
} |