internal unsafe BaseCodePageEncoding(int codepage, int dataCodePage) : base((codepage == 0) ? Win32Native.GetACP() : codepage) { this.bFlagDataTable = true; this.pCodePage = null; this.dataTableCodePage = dataCodePage; this.LoadCodePageTables(); }
internal static unsafe int GetCodePageByteSize(int codePage) { CodePageHeader *headerPtr = FindCodePage(codePage); if (headerPtr == null) { return(0); } return(headerPtr->ByteCount); }
private unsafe void LoadCodePageTables() { CodePageHeader *headerPtr = FindCodePage(this.dataTableCodePage); if (headerPtr == null) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_NoCodepageData", new object[] { this.CodePage })); } this.pCodePage = headerPtr; this.LoadManagedCodePage(); }
[System.Security.SecurityCritical] // auto-generated internal static unsafe int GetCodePageByteSize(int codePage) { // Get our code page info CodePageHeader *pCodePage = FindCodePage(codePage); // If null return 0 if (pCodePage == null) { return(0); } Contract.Assert(pCodePage->ByteCount == 1 || pCodePage->ByteCount == 2, "[BaseCodePageEncoding] Code page (" + codePage + ") has invalid byte size (" + pCodePage->ByteCount + ") in table"); // Return what it says for byte count return(pCodePage->ByteCount); }
[System.Security.SecurityCritical] // auto-generated private unsafe void LoadCodePageTables() { CodePageHeader *pCodePage = FindCodePage(dataTableCodePage); // Make sure we have one if (pCodePage == null) { // Didn't have one throw new NotSupportedException( Environment.GetResourceString("NotSupported_NoCodepageData", CodePage)); } // Remember our code page this.pCodePage = pCodePage; // We had it, so load it LoadManagedCodePage(); }
[System.Security.SecurityCritical] // auto-generated private static unsafe CodePageHeader *FindCodePage(int codePage) { // We'll have to loop through all of the m_pCodePageIndex[] items to find our code page, this isn't // binary or anything so its not monsterously fast. for (int i = 0; i < m_pCodePageFileHeader->CodePageCount; i++) { CodePageIndex *pCodePageIndex = (&(m_pCodePageFileHeader->CodePages)) + i; if (pCodePageIndex->CodePage == codePage) { // Found it! CodePageHeader *pCodePage = (CodePageHeader *)((byte *)m_pCodePageFileHeader + pCodePageIndex->Offset); return(pCodePage); } } // Couldn't find it return(null); }
// We have a managed code page entry, so load our tables // SBCS data section looks like: // // char[256] - what each byte maps to in unicode. No support for surrogates. 0 is undefined code point // (except 0 for byte 0 is expected to be a real 0) // // byte/char* - Data for best fit (unicode->bytes), again no best fit for Unicode // 1st WORD is Unicode // of 1st character position // Next bytes are best fit byte for that position. Position is incremented after each byte // byte < 0x20 means skip the next n positions. (Where n is the byte #) // byte == 1 means that next word is another unicode code point # // byte == 0 is unknown. (doesn't override initial WCHAR[256] table! protected override unsafe void LoadManagedCodePage() { Debug.Assert(m_codePageHeader?.Length > 0); fixed(byte *pBytes = &m_codePageHeader[0]) { 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); Unsafe.InitBlockUnaligned(pNativeMemory, 0, (uint)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[0]) { 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; } }
[System.Security.SecurityCritical] // auto-generated private unsafe void LoadCodePageTables() { CodePageHeader* pCodePage = FindCodePage(dataTableCodePage); // Make sure we have one if (pCodePage == null) { // Didn't have one throw new NotSupportedException( Environment.GetResourceString("NotSupported_NoCodepageData", CodePage)); } // Remember our code page this.pCodePage = pCodePage; // We had it, so load it LoadManagedCodePage(); }
internal unsafe BaseCodePageEncoding(SerializationInfo info, StreamingContext context) : base(0) { this.bFlagDataTable = true; this.pCodePage = null; throw new ArgumentNullException("this"); }
private unsafe void LoadCodePageTables() { CodePageHeader* headerPtr = FindCodePage(this.dataTableCodePage); if (headerPtr == null) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_NoCodepageData", new object[] { this.CodePage })); } this.pCodePage = headerPtr; this.LoadManagedCodePage(); }