System.Text.EUCJPEncoding.CleanUpBytes C# (CSharp) Method

CleanUpBytes() protected method

protected CleanUpBytes ( int &bytes ) : bool
bytes int
return bool
        protected override bool CleanUpBytes(ref int bytes)
        {
            if (bytes >= 0x100)
            {
                // map extended char (0xfa40-0xfc4b) to a special range
                // (ported from mlang)
                if (bytes >= 0xfa40 && bytes <= 0xfc4b)
                {
                    if (bytes >= 0xfa40 && bytes <= 0xfa5b)
                    {
                        if (bytes <= 0xfa49)
                            bytes = bytes - 0x0b51;
                        else if (bytes >= 0xfa4a && bytes <= 0xfa53)
                            bytes = bytes - 0x072f6;
                        else if (bytes >= 0xfa54 && bytes <= 0xfa57)
                            bytes = bytes - 0x0b5b;
                        else if (bytes == 0xfa58)
                            bytes = 0x878a;
                        else if (bytes == 0xfa59)
                            bytes = 0x8782;
                        else if (bytes == 0xfa5a)
                            bytes = 0x8784;
                        else if (bytes == 0xfa5b)
                            bytes = 0x879a;
                    }
                    else if (bytes >= 0xfa5c && bytes <= 0xfc4b)
                    {
                        byte tc = unchecked((byte)bytes);
                        if (tc < 0x5c)
                            bytes = bytes - 0x0d5f;
                        else if (tc >= 0x80 && tc <= 0x9B)
                            bytes = bytes - 0x0d1d;
                        else
                            bytes = bytes - 0x0d1c;
                    }
                }

                // Convert 932 code page to 20932 like code page range
                // (also ported from mlang)
                byte bLead = unchecked((byte)(bytes >> 8));
                byte bTrail = unchecked((byte)bytes);

                bLead -= ((bLead > (byte)0x9f) ? (byte)0xb1 : (byte)0x71);
                bLead = (byte)((bLead << 1) + 1);
                if (bTrail > (byte)0x9e)
                {
                    bTrail -= (byte)0x7e;
                    bLead++;
                }
                else
                {
                    if (bTrail > (byte)0x7e)
                        bTrail--;
                    bTrail -= (byte)0x1f;
                }

                bytes = ((int)bLead) << 8 | (int)bTrail | 0x8080;

                // Don't step out of our allocated lead byte area.
                // All DBCS lead and trail bytes should be >= 0xa1 and <= 0xfe
                if ((bytes & 0xFF00) < 0xa100 || (bytes & 0xFF00) > 0xfe00 ||
                    (bytes & 0xFF) < 0xa1 || (bytes & 0xFF) > 0xfe)
                    return false;
                // WARNING: Our funky mapping allows illegal values, which we continue to use
                // for compatibility purposes.
            }
            else
            {
                // For 51932 1/2 Katakana gets a 0x8E lead byte
                // Adjust 1/2 Katakana
                if (bytes >= 0xa1 && bytes <= 0xdf)
                {
                    bytes |= 0x8E00;
                    return true;
                }

                // 0x81-0x9f and 0xe0-0xfc CP 932
                // 0x8e and 0xa1-0xfe      CP 20932 (we don't use 8e though)
                // b0-df is 1/2 Katakana
                // So 81-9f & e0-fc are 932 lead bytes, a1-fe are our lead bytes
                // so ignore everything above 0x80 except 0xa0 and 0xff
                if (bytes >= 0x81 && bytes != 0xa0 && bytes != 0xff)
                {
                    // We set different lead bytes later, so just return false
                    return false;
                }
            }

            return true;
        }