protected unsafe override void LoadManagedCodePage()
{
// Use base code page loading algorithm.
iExtraBytes = (GBLast4ByteCode + 1) * 2 + 0x10000 / 8;
// Load most of our code page
base.LoadManagedCodePage();
// Point to our new data sections
byte* pNativeMemory = (byte*)safeNativeMemoryHandle.DangerousGetHandle();
mapUnicodeTo4BytesFlags = pNativeMemory + 65536 * 2 * 2;
map4BytesToUnicode = (char*)(pNativeMemory + 65536 * 2 * 2 + 0x10000 / 8);
// Once we've done our base LoadManagedCodePage, we'll have to add our fixes
char unicodeCount = (char)0;
ushort count4Byte = 0;
for (int index = 0; index < _tableUnicodeToGBDiffs.Length; index++)
{
ushort data = _tableUnicodeToGBDiffs[index];
// Check high bit
if ((data & 0x8000) != 0)
{
// Make be exact value
if (data > 0x9000 && data != 0xD1A6)
{
// It was an exact value (gb18040[data] = unicode)
mapBytesToUnicode[data] = unicodeCount;
mapUnicodeToBytes[unicodeCount] = data;
unicodeCount++;
}
else
{
// It was a CP 936 compatible data, that table's already loaded, just increment our pointer
unicodeCount += unchecked((char)(data & 0x7FFF));
}
}
else
{
// It was GB 18030 4 byte data, next <data> characters are 4 byte sequences.
while (data > 0)
{
Debug.Assert(count4Byte <= GBLast4ByteCode,
"[GB18030Encoding.LoadManagedCodePage] Found too many 4 byte codes in data table.");
// Set the 4 byte -> Unicode value
map4BytesToUnicode[count4Byte] = unicodeCount;
// Set the unicode -> 4 bytes value, including flag that its a 4 byte sequence
mapUnicodeToBytes[unicodeCount] = count4Byte;
// Set the flag saying its a 4 byte sequence
mapUnicodeTo4BytesFlags[unicodeCount / 8] |= unchecked((byte)(1 << (unicodeCount % 8)));
unicodeCount++;
count4Byte++;
data--;
}
}
}
// unicodeCount should've wrapped back to 0
Debug.Assert(unicodeCount == 0,
"[GB18030Encoding.LoadManagedCodePage] Expected unicodeCount to wrap around to 0 as all chars were processed");
// We should've read in GBLast4ByteCode 4 byte sequences
Debug.Assert(count4Byte == GBLast4ByteCode + 1,
"[GB18030Encoding.LoadManagedCodePage] Expected 0x99FB to be last 4 byte offset, found 0x" + count4Byte.ToString("X4", CultureInfo.InvariantCulture));
}