System.Text.SBCSCodePageEncoding.LoadManagedCodePage C# (CSharp) Method

LoadManagedCodePage() private method

private LoadManagedCodePage ( ) : void
return void
        protected unsafe override void LoadManagedCodePage()
        {
            fixed (byte* pBytes = m_codePageHeader)
            {
                CodePageHeader* pCodePage = (CodePageHeader*)pBytes;
                // Should be loading OUR code page
                Debug.Assert(pCodePage->CodePage == dataTableCodePage,
                    "[SBCSCodePageEncoding.LoadManagedCodePage]Expected to load data table code page");

                // Make sure we're really a 1 byte code page
                if (pCodePage->ByteCount != 1)
                    throw new NotSupportedException(SR.Format(SR.NotSupported_NoCodepageData, CodePage));

                // Remember our unknown bytes & chars
                _byteUnknown = (byte)pCodePage->ByteReplace;
                _charUnknown = pCodePage->UnicodeReplace;

                // Get our mapped section 65536 bytes for unicode->bytes, 256 * 2 bytes for bytes->unicode
                // Plus 4 byte to remember CP # when done loading it. (Don't want to get IA64 or anything out of alignment)
                const int UnicodeToBytesMappingSize = 65536;
                const int BytesToUnicodeMappingSize = 256 * 2;
                const int CodePageNumberSize = 4;
                int bytesToAllocate = UnicodeToBytesMappingSize + BytesToUnicodeMappingSize + CodePageNumberSize + iExtraBytes;
                byte* pNativeMemory = GetNativeMemory(bytesToAllocate);
                ZeroMemAligned(pNativeMemory, bytesToAllocate);

                char* mapBytesToUnicode = (char*)pNativeMemory;
                byte* mapUnicodeToBytes = (byte*)(pNativeMemory + 256 * 2);

                // Need to read our data file and fill in our section.
                // WARNING: Multiple code pieces could do this at once (so we don't have to lock machine-wide)
                //          so be careful here.  Only stick legal values in here, don't stick temporary values.

                // Read our data file and set mapBytesToUnicode and mapUnicodeToBytes appropriately
                // First table is just all 256 mappings

                byte[] buffer = new byte[256 * sizeof(char)];
                lock (s_streamLock)
                {
                    s_codePagesEncodingDataStream.Seek(m_firstDataWordOffset, SeekOrigin.Begin);
                    s_codePagesEncodingDataStream.Read(buffer, 0, buffer.Length);
                }

                fixed (byte* pBuffer = buffer)
                {
                    char* pTemp = (char*)pBuffer;
                    for (int b = 0; b < 256; b++)
                    {
                        // Don't want to force 0's to map Unicode wrong.  0 byte == 0 unicode already taken care of
                        if (pTemp[b] != 0 || b == 0)
                        {
                            mapBytesToUnicode[b] = pTemp[b];

                            if (pTemp[b] != UNKNOWN_CHAR)
                                mapUnicodeToBytes[pTemp[b]] = (byte)b;
                        }
                        else
                        {
                            mapBytesToUnicode[b] = UNKNOWN_CHAR;
                        }
                    }
                }

                _mapBytesToUnicode = mapBytesToUnicode;
                _mapUnicodeToBytes = mapUnicodeToBytes;
            }
        }