Exemple #1
0
        [System.Security.SecurityCritical]  // auto-generated
        private unsafe bool FindCodePage(int codePage)
        {
            Debug.Assert(m_codePageHeader != null && m_codePageHeader.Length == CODEPAGE_HEADER_SIZE, "m_codePageHeader expected to match in size the struct CodePageHeader");

            // Loop through all of the m_pCodePageIndex[] items to find our code page
            byte[] codePageIndex = new byte[sizeof(CodePageIndex)];

            lock (s_streamLock)
            {
                // seek to the first CodePageIndex entry
                s_codePagesEncodingDataStream.Seek(CODEPAGE_DATA_FILE_HEADER_SIZE, SeekOrigin.Begin);

                int codePagesCount;
                fixed(byte *pBytes = &s_codePagesDataHeader[0])
                {
                    CodePageDataFileHeader *pDataHeader = (CodePageDataFileHeader *)pBytes;

                    codePagesCount = pDataHeader->CodePageCount;
                }

                fixed(byte *pBytes = &codePageIndex[0])
                {
                    CodePageIndex *pCodePageIndex = (CodePageIndex *)pBytes;

                    for (int i = 0; i < codePagesCount; i++)
                    {
                        s_codePagesEncodingDataStream.Read(codePageIndex, 0, codePageIndex.Length);

                        if (pCodePageIndex->CodePage == codePage)
                        {
                            // Found it!
                            long position = s_codePagesEncodingDataStream.Position;
                            s_codePagesEncodingDataStream.Seek((long)pCodePageIndex->Offset, SeekOrigin.Begin);
                            s_codePagesEncodingDataStream.Read(m_codePageHeader, 0, m_codePageHeader.Length);
                            m_firstDataWordOffset = (int)s_codePagesEncodingDataStream.Position; // stream now pointing to the codepage data

                            if (i == codePagesCount - 1)                                         // last codepage
                            {
                                m_dataSize = (int)(s_codePagesEncodingDataStream.Length - pCodePageIndex->Offset - m_codePageHeader.Length);
                            }
                            else
                            {
                                // Read Next codepage data to get the offset and then calculate the size
                                s_codePagesEncodingDataStream.Seek(position, SeekOrigin.Begin);
                                int currentOffset = pCodePageIndex->Offset;
                                s_codePagesEncodingDataStream.Read(codePageIndex, 0, codePageIndex.Length);
                                m_dataSize = pCodePageIndex->Offset - currentOffset - m_codePageHeader.Length;
                            }

                            return(true);
                        }
                    }
                }
            }

            // Couldn't find it
            return(false);
        }
        internal static unsafe EncodingInfo [] GetEncodings(CodePagesEncodingProvider provider)
        {
            lock (s_streamLock)
            {
                s_codePagesEncodingDataStream.Seek(CODEPAGE_DATA_FILE_HEADER_SIZE, SeekOrigin.Begin);

                int codePagesCount;
                fixed(byte *pBytes = &s_codePagesDataHeader[0])
                {
                    CodePageDataFileHeader *pDataHeader = (CodePageDataFileHeader *)pBytes;

                    codePagesCount = pDataHeader->CodePageCount;
                }

                EncodingInfo [] encodingInfoList = new EncodingInfo[codePagesCount];

                CodePageIndex codePageIndex  = default;
                Span <byte>   pCodePageIndex = new Span <byte>(&codePageIndex, Unsafe.SizeOf <CodePageIndex>());

                for (int i = 0; i < codePagesCount; i++)
                {
                    s_codePagesEncodingDataStream.Read(pCodePageIndex);

                    string codePageName;
                    switch (codePageIndex.CodePage)
                    {
                    // Fixup some encoding names.
                    case 950:   codePageName = "big5"; break;

                    case 10002: codePageName = "x-mac-chinesetrad"; break;

                    case 20833: codePageName = "x-ebcdic-koreanextended"; break;

                    default:    codePageName = new string(&codePageIndex.CodePageName); break;
                    }

                    string?resourceName = EncodingNLS.GetLocalizedEncodingNameResource(codePageIndex.CodePage);
                    string?displayName  = null;

                    if (resourceName != null && resourceName.StartsWith("Globalization_cp_", StringComparison.OrdinalIgnoreCase))
                    {
                        displayName = SR.GetResourceString(resourceName);
                    }

                    encodingInfoList[i] = new EncodingInfo(provider, codePageIndex.CodePage, codePageName, displayName ?? codePageName);
                }

                return(encodingInfoList);
            }
        }
Exemple #3
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static unsafe int GetCodePageByteSize(int codePage)
        {
            // Loop through all of the m_pCodePageIndex[] items to find our code page
            byte[] codePageIndex = new byte[sizeof(CodePageIndex)];

            lock (s_streamLock)
            {
                // seek to the first CodePageIndex entry
                s_codePagesEncodingDataStream.Seek(CODEPAGE_DATA_FILE_HEADER_SIZE, SeekOrigin.Begin);

                int codePagesCount;
                fixed(byte *pBytes = &s_codePagesDataHeader[0])
                {
                    CodePageDataFileHeader *pDataHeader = (CodePageDataFileHeader *)pBytes;

                    codePagesCount = pDataHeader->CodePageCount;
                }

                fixed(byte *pBytes = &codePageIndex[0])
                {
                    CodePageIndex *pCodePageIndex = (CodePageIndex *)pBytes;

                    for (int i = 0; i < codePagesCount; i++)
                    {
                        s_codePagesEncodingDataStream.Read(codePageIndex, 0, codePageIndex.Length);

                        if (pCodePageIndex->CodePage == codePage)
                        {
                            Debug.Assert(pCodePageIndex->ByteCount == 1 || pCodePageIndex->ByteCount == 2,
                                         "[BaseCodePageEncoding] Code page (" + codePage + ") has invalid byte size (" + pCodePageIndex->ByteCount + ") in table");
                            // Return what it says for byte count
                            return(pCodePageIndex->ByteCount);
                        }
                    }
                }
            }

            // Couldn't find it
            return(0);
        }