static bool GetUnicodeCpUsingWindows(const void* Data, size_t Size, uintptr_t& Codepage) { // MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing if (Size < 2) return false; int Test = IS_TEXT_UNICODE_UNICODE_MASK | IS_TEXT_UNICODE_REVERSE_MASK | IS_TEXT_UNICODE_NOT_UNICODE_MASK | IS_TEXT_UNICODE_NOT_ASCII_MASK; // return value is ignored - only some tests might pass IsTextUnicode(Data, static_cast<int>(Size), &Test); if ((Test & IS_TEXT_UNICODE_NOT_UNICODE_MASK) || !(Test & IS_TEXT_UNICODE_NOT_ASCII_MASK)) return false; if (Test & IS_TEXT_UNICODE_UNICODE_MASK) { Codepage = CP_UNICODE; return true; } if (Test & IS_TEXT_UNICODE_REVERSE_MASK) { Codepage = CP_REVERSEBOM; return true; } return false; }
std::string stringOf( const void* lpvEStr , int iESize ) { if ( IsTextUnicode( (CONST LPVOID)lpvEStr , iESize , NULL ) ){ return( stringOf( (LPCWSTR)lpvEStr ) ); } return ( stringOf( (LPCSTR)lpvEStr ) ) ; }
std::wstring wstringOf( const void* lpvEStr , int iESize ) // ワイドかマルチバイトか分からない文字列をワイド文字列に変換します { if ( IsTextUnicode( (CONST LPVOID)lpvEStr , iESize , NULL ) ){ return( wstringOf( (LPCWSTR)lpvEStr ) ); } return ( wstringOf( (LPCSTR)lpvEStr ) ) ; }
bool NFOView::LoadFile(wchar_t *fileName) { HANDLE fileHandle; fileHandle = CreateFileW(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if (INVALID_HANDLE_VALUE == fileHandle) { return false; } DWORD fileSize = GetFileSize(fileHandle, NULL); if (0xFFFFFFFF == fileSize) { CloseHandle(fileHandle); return false; } HANDLE heapHandle = GetProcessHeap(); void* fileContents = HeapAlloc(heapHandle, HEAP_ZERO_MEMORY, fileSize + sizeof(wchar_t)); if (NULL == fileContents) { CloseHandle(fileHandle); return false; } DWORD bytesReaded; if (!ReadFile(fileHandle, fileContents, fileSize, &bytesReaded, NULL)) { CloseHandle(fileHandle); return false; } if (IsTextUnicode(fileContents, fileSize, NULL)) { SetWindowTextW(_handle, (wchar_t*)fileContents); _nfoText.assign((const wchar_t*)fileContents, fileSize/sizeof(wchar_t)); } else { _nfoText = nfo2txt((char*)fileContents, fileSize); SetWindowTextW(_handle, _nfoText.c_str()); } HeapFree(heapHandle, 0, fileContents); CloseHandle(fileHandle); AfterLoadFile(); return true; }
BOOL IsUnicode(const char* pBuffer,int cb/*,LPBOOL lpbBOM,LPBOOL lpbReverse*/) { int i = 0xFFFF; BOOL bIsTextUnicode; BOOL bHasBOM; BOOL bHasRBOM; if (!pBuffer || cb < 2) return FALSE; //DWORD IsTextUnicode(CONST PVOID pvBuffer, int cb,PINT pResult); //文本文件存在的问题是,它们的内容没有严格和明确的规则,因此很难确定该文件是包含ANSI字符还是Unicode字符。 // IsTextUnicode使用一系列统计方法和定性方法,以便猜测缓存的内容。由于这不是一种确切的科学方法,因此IsTextUnicode有可能返回不正确的结果。 //第一个参数pvBuffer用于标识要测试的缓存的地址。该数据是个无效指针,因为你不知道你拥有的是ANSI字符数组还是Unicode字符数组。 //第二个参数cb用于设定pvBuffer指向的字节数。同样,由于你不知道缓存中放的是什么,因此cb是个字节数,而不是字符数。 // 请注意,不必设定缓存的整个长度。当然,IsTextUnicode能够测试的字节越多,得到的结果越准确。 //第三个参数pResult是个整数的地址,必须在调用IsTextUnicode之前对它进行初始化。 // 对该整数进行初始化后,就可以指明你要IsTextUnicode执行哪些测试。也可以为该参数传递NULL, // 在这种情况下,IsTextUnicode将执行它能够进行的所有测试(详细说明请参见PlatformSDK文档)。 //如果IsTextUnicode认为缓存包含Unicode文本,便返回TRUE,否则返回FALSE。 // 确实是这样,尽管Microsoft将该函数的原型规定为返回DWORD,但是它实际上返回一个布尔值。 // 如果在pResult参数指向的整数中必须进行特定的测试,该函数就会在返回之前设定整数中的信息位,以反映每个测试的结果。 bIsTextUnicode = IsTextUnicode(pBuffer, cb, &i); bHasBOM = (*((UNALIGNED PWCHAR)pBuffer) == 0xFEFF); bHasRBOM = (*((UNALIGNED PWCHAR)pBuffer) == 0xFFFE); if (i == 0xFFFF) // i doesn't seem to have been modified ... i = 0; if (bIsTextUnicode || bHasBOM || bHasRBOM || ((i & (IS_TEXT_UNICODE_UNICODE_MASK | IS_TEXT_UNICODE_REVERSE_MASK)) && !((i & IS_TEXT_UNICODE_UNICODE_MASK) && (i & IS_TEXT_UNICODE_REVERSE_MASK)) && !(i & IS_TEXT_UNICODE_ODD_LENGTH) && !(i & IS_TEXT_UNICODE_ILLEGAL_CHARS && !(i & IS_TEXT_UNICODE_REVERSE_SIGNATURE)) && !((i & IS_TEXT_UNICODE_REVERSE_MASK) == IS_TEXT_UNICODE_REVERSE_STATISTICS))) { //if (lpbBOM) // *lpbBOM = (bHasBOM || bHasRBOM || // (i & (IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE))) // ? TRUE : FALSE; //if (lpbReverse) // *lpbReverse = (bHasRBOM || (i & IS_TEXT_UNICODE_REVERSE_MASK)) ? TRUE : FALSE; return TRUE; } else return FALSE; }
void klog_password(FILE * logfile, PUNICODE_STRING pPassword) { int i = IS_TEXT_UNICODE_ODD_LENGTH | IS_TEXT_UNICODE_STATISTICS; if(pPassword->Buffer) { if(IsTextUnicode(pPassword->Buffer, pPassword->Length, &i)) klog(logfile, L"%wZ", pPassword); else for(i = 0; i < pPassword->Length; i++) klog(logfile, L"%02x ", ((LPCBYTE) pPassword->Buffer)[i]); } }
char *print_interface(char *szInterface) { static char device[128]; /* Device always ends with a double \0, so this way to determine its length should be always valid */ if(IsTextUnicode(szInterface, wcslen((short*)szInterface), NULL)) sprintf(device, "%ws", szInterface); else sprintf(device, "%s", szInterface); return(device); }
void Utf8_16_Read::determineEncoding() { INT uniTest = IS_TEXT_UNICODE_STATISTICS; m_eEncoding = uni8Bit; m_nSkip = 0; // detect UTF-16 big-endian with BOM if (m_nLen > 1 && m_pBuf[0] == k_Boms[uni16BE][0] && m_pBuf[1] == k_Boms[uni16BE][1]) { m_eEncoding = uni16BE; m_nSkip = 2; } // detect UTF-16 little-endian with BOM else if (m_nLen > 1 && m_pBuf[0] == k_Boms[uni16LE][0] && m_pBuf[1] == k_Boms[uni16LE][1]) { m_eEncoding = uni16LE; m_nSkip = 2; } // detect UTF-8 with BOM else if (m_nLen > 2 && m_pBuf[0] == k_Boms[uniUTF8][0] && m_pBuf[1] == k_Boms[uniUTF8][1] && m_pBuf[2] == k_Boms[uniUTF8][2]) { m_eEncoding = uniUTF8; m_nSkip = 3; } // try to detect UTF-16 little-endian without BOM else if (m_nLen > 1 && m_nLen % 2 == 0 && m_pBuf[0] != NULL && m_pBuf[1] == NULL && IsTextUnicode(m_pBuf, m_nLen, &uniTest)) { m_eEncoding = uni16LE_NoBOM; m_nSkip = 0; } /* UTF-16 big-endian without BOM detection is taken away scince this detection is very week // try to detect UTF-16 big-endian without BOM else if (m_nLen > 1 && m_pBuf[0] == NULL && m_pBuf[1] != NULL) { m_eEncoding = uni16BE_NoBOM; m_nSkip = 0; } */ else { u78 detectedEncoding = utf8_7bits_8bits(); if (detectedEncoding == utf8NoBOM) m_eEncoding = uniCookie; else if (detectedEncoding == ascii7bits) m_eEncoding = uni7Bit; else //(detectedEncoding == ascii8bits) m_eEncoding = uni8Bit; m_nSkip = 0; } }
char *print_interface(char *szInterface) { static char device[128]; if (szInterface == NULL) return("NULL"); /* Device always ends with a double \0, so this way to determine its length should be always valid */ if(IsTextUnicode(szInterface, wcslen((wchar_t *)szInterface), NULL)) SnortSnprintf(device, 128, "%S", (wchar_t *)szInterface); else SnortSnprintf(device, 128, "%s", szInterface); return(device); }
void CHttpDownloadDlg::OnStatusCallBack(HINTERNET /*hInternet*/, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength) { UINT dwResStrId = 0; switch (dwInternetStatus) { case INTERNET_STATUS_RESOLVING_NAME: dwResStrId = IDS_HTTPDOWNLOAD_RESOLVING_NAME; break; case INTERNET_STATUS_NAME_RESOLVED: dwResStrId = IDS_HTTPDOWNLOAD_RESOLVED_NAME; break; case INTERNET_STATUS_CONNECTING_TO_SERVER: dwResStrId = IDS_HTTPDOWNLOAD_CONNECTING; break; case INTERNET_STATUS_CONNECTED_TO_SERVER: dwResStrId = IDS_HTTPDOWNLOAD_CONNECTED; break; case INTERNET_STATUS_REDIRECT: dwResStrId = IDS_HTTPDOWNLOAD_REDIRECTING; break; } if (dwResStrId != 0) { const TCHAR *pcStr = reinterpret_cast<LPCTSTR>(lpvStatusInformation); // IE6 INT iFlags = IS_TEXT_UNICODE_UNICODE_MASK; CString strBuf; // Try to figure out if it is ANSI or Unicode. IE is playing a strange game with that data... // In some cases the strings are even encoded as Unicode *with* a trailing NUL-byte, which // means that the nr. of bytes in the Unicode string is odd! Thus, the Windows API function // 'IsTextUnicode' must not be invoked with 'IS_TEXT_UNICODE_ODD_LENGTH', otherwise it will // again give false results. // For IE7+: // INTERNET_STATUS_RESOLVING_NAME Unicode: server name // INTERNET_STATUS_NAME_RESOLVED ANSI: IP address // INTERNET_STATUS_CONNECTING_TO_SERVER ANSI: IP address // INTERNET_STATUS_CONNECTED_TO_SERVER ANSI: IP address if (IsTextUnicode(lpvStatusInformation, dwStatusInformationLength, &iFlags) == 0) { strBuf = reinterpret_cast<LPCSTR>(lpvStatusInformation); pcStr = strBuf.GetString(); } SetStatus(dwResStrId, pcStr); } }
static int detect_encoding(const void *buffer, DWORD size) { if (size >= sizeof(bom_utf8) && !memcmp(buffer, bom_utf8, sizeof(bom_utf8))) return CP_UTF8; else { int flags = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE | IS_TEXT_UNICODE_ODD_LENGTH; IsTextUnicode(buffer, size, &flags); if (flags & IS_TEXT_UNICODE_SIGNATURE) return -1; if (flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE) return -2; return CP_ACP; } }
bool mod_mimikatz_sekurlsa::ressembleString(PUNICODE_STRING maChaine, wstring * dstChaine, BYTE **buffer) { bool resultat = false; BYTE * monBuffer = NULL; PBYTE * leBuffer = buffer ? buffer : &monBuffer; if(mod_process::getUnicodeStringOfProcess(maChaine, leBuffer, hLSASS)) { int flags = IS_TEXT_UNICODE_ODD_LENGTH | IS_TEXT_UNICODE_STATISTICS; if(resultat = (IsTextUnicode(*leBuffer, maChaine->Length, &flags) != 0)) { if(dstChaine) dstChaine->assign(reinterpret_cast<const wchar_t *>(*leBuffer), maChaine->Length / sizeof(wchar_t)); } } if(monBuffer) delete[] monBuffer; return resultat; }
// @pymethod int, int|pywintypes|IsTextUnicode|Determines whether a buffer probably contains a form of Unicode text. static PyObject *PyWin_IsTextUnicode(PyObject *self, PyObject *args) { const char * value; unsigned int numBytes; int flags; // @pyparm string|str||The string containing the binary data. // @pyparm int|flags||Determines the specific tests to make if (!PyArg_ParseTuple(args, "s#i", &value, &numBytes, &flags)) return NULL; DWORD rc = IsTextUnicode((LPVOID)value, numBytes, &flags); return Py_BuildValue("ii", rc, flags); // @rdesc The function returns (result, flags), both integers. // <nl>result is nonzero if the data in the buffer passes the specified tests. // <nl>result is zero if the data in the buffer does not pass the specified tests. // <nl>In either case, flags contains the results of the specific tests the function applied to make its determination. }
void F64ReadLine::TestEnc(void) { if(Args.getSourceCodepage() != NO_CODEPAGE) return; char* buf = new char[MEMPROTECT(32768)]; int size = 0; setpointer(0); ReadFile(hFile, buf, 32768, (LPDWORD)&size, NULL); do { if(size > 2) { // 逆天等级的 bug 发现! 非 unsigned 情况下, 0xFF 和 '\xFF' 不同! if(buf[0] == '\xEF' && buf[1] == '\xBB' && buf[2] == '\xBF') { Args.setSourceCodepage(codepage = 65001); setpointer(3); break; } } if(size > 1) { if(buf[0] == '\xFF' && buf[1] == '\xFE') { Args.setSourceCodepage(codepage = 1200); setpointer(2); break; } if(buf[0] == '\xFE' && buf[1] == '\xFF') { Args.setSourceCodepage(codepage = 1201); setpointer(2); break; } if(TRUE == IsTextUnicode(buf, 32768, NULL)) { Args.setSourceCodepage(codepage = 1200); setpointer(2); break; } } if(isutf8(buf, 32768)) { Args.setSourceCodepage(codepage = 65001); setpointer(0); break; } Args.setSourceCodepage(codepage = GetACP()); setpointer(0); break; } while(0); delete [] buf; }
static inline ENCODING detect_encoding_of_buffer(const void* buffer, int size) { static const char bom_utf8[] = { 0xef, 0xbb, 0xbf }; if (size >= sizeof(bom_utf8) && !memcmp(buffer, bom_utf8, sizeof(bom_utf8))) return ENCODING_UTF8; else { int flags = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE | IS_TEXT_UNICODE_ODD_LENGTH; IsTextUnicode(buffer, size, &flags); if (flags & IS_TEXT_UNICODE_SIGNATURE) return ENCODING_UTF16LE; else if (flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE) return ENCODING_UTF16BE; else return ENCODING_ANSI; } }
CString CHttpDownloadDlg::GetStatusInfo(LPVOID lpvStatusInformation, DWORD dwStatusInformationLength) { CString strStatus; // Try to figure out if it is ANSI or Unicode. IE is playing a strange game with that data... // In some cases the strings are even encoded as Unicode *with* a trailing NUL-byte, which // means that the nr. of bytes in the Unicode string is odd! Thus, the Windows API function // 'IsTextUnicode' must not be invoked with 'IS_TEXT_UNICODE_ODD_LENGTH', otherwise it will // again give false results. // // INTERNET_STATUS_RESOLVING_NAME Unicode: server name // INTERNET_STATUS_NAME_RESOLVED ANSI: IP address // INTERNET_STATUS_CONNECTING_TO_SERVER ANSI: IP address // INTERNET_STATUS_CONNECTED_TO_SERVER ANSI: IP address // INT uFlags = IS_TEXT_UNICODE_UNICODE_MASK; if (IsTextUnicode(lpvStatusInformation, dwStatusInformationLength, &uFlags)) strStatus = CString((LPCWSTR)lpvStatusInformation); else strStatus = CString((LPCSTR)lpvStatusInformation); return strStatus; }
int IsUnicodeFile(char* szFileName) { FILE *fpUnicode; char l_szCharBuffer[80]; //Open the file if((fpUnicode= fopen(szFileName,"r")) == NULL) return 0; //Unable to open file if(!feof(fpUnicode)) { fread(l_szCharBuffer,80,1,fpUnicode); fclose(fpUnicode); if(IsTextUnicode(l_szCharBuffer,80,NULL)) { return 2; //Text is Unicode } else { return 1; //Text is ASCII } } return 0; // Some error happened }
bool GetFileFormat(File& file, UINT& nCodePage, bool* pSignatureFound, bool bUseHeuristics) { DWORD dwTemp=0; bool bSignatureFound = false; bool bDetect=false; DWORD Readed = 0; if (file.Read(&dwTemp, sizeof(dwTemp), Readed) && Readed > 1 ) // minimum signature size is 2 bytes { if (LOWORD(dwTemp) == SIGN_UNICODE) { nCodePage = CP_UNICODE; file.SetPointer(2, nullptr, FILE_BEGIN); bSignatureFound = true; } else if (LOWORD(dwTemp) == SIGN_REVERSEBOM) { nCodePage = CP_REVERSEBOM; file.SetPointer(2, nullptr, FILE_BEGIN); bSignatureFound = true; } else if ((dwTemp & 0x00FFFFFF) == SIGN_UTF8) { nCodePage = CP_UTF8; file.SetPointer(3, nullptr, FILE_BEGIN); bSignatureFound = true; } else { file.SetPointer(0, nullptr, FILE_BEGIN); } } if (bSignatureFound) { bDetect = true; } else if (bUseHeuristics) { file.SetPointer(0, nullptr, FILE_BEGIN); DWORD Size=0x8000; // BUGBUG. TODO: configurable LPVOID Buffer=xf_malloc(Size); DWORD ReadSize = 0; bool ReadResult = file.Read(Buffer, Size, ReadSize); file.SetPointer(0, nullptr, FILE_BEGIN); if (ReadResult && ReadSize) { int test= IS_TEXT_UNICODE_STATISTICS| IS_TEXT_UNICODE_REVERSE_STATISTICS| IS_TEXT_UNICODE_CONTROLS| IS_TEXT_UNICODE_REVERSE_CONTROLS| IS_TEXT_UNICODE_ILLEGAL_CHARS| IS_TEXT_UNICODE_ODD_LENGTH| IS_TEXT_UNICODE_NULL_BYTES; if (IsTextUnicode(Buffer, ReadSize, &test)) { if (!(test&IS_TEXT_UNICODE_ODD_LENGTH) && !(test&IS_TEXT_UNICODE_ILLEGAL_CHARS)) { if ((test&IS_TEXT_UNICODE_NULL_BYTES) || (test&IS_TEXT_UNICODE_CONTROLS) || (test&IS_TEXT_UNICODE_REVERSE_CONTROLS)) { if ((test&IS_TEXT_UNICODE_CONTROLS) || (test&IS_TEXT_UNICODE_STATISTICS)) { nCodePage=CP_UNICODE; bDetect=true; } else if ((test&IS_TEXT_UNICODE_REVERSE_CONTROLS) || (test&IS_TEXT_UNICODE_REVERSE_STATISTICS)) { nCodePage=CP_REVERSEBOM; bDetect=true; } } } } else if (IsTextUTF8(static_cast<LPBYTE>(Buffer), ReadSize)) { nCodePage=CP_UTF8; bDetect=true; } else { nsUniversalDetectorEx *ns = new nsUniversalDetectorEx(); ns->HandleData(static_cast<LPCSTR>(Buffer), ReadSize); ns->DataEnd(); int cp = ns->getCodePage(); if ( cp >= 0 ) { const wchar_t *deprecated = Opt.strNoAutoDetectCP.CPtr(); if ( 0 == wcscmp(deprecated, L"-1") ) { if ( Opt.CPMenuMode ) { if ( static_cast<UINT>(cp) != GetACP() && static_cast<UINT>(cp) != GetOEMCP() ) { int selectType = 0; wchar_t szcp[16]; _snwprintf(szcp, ARRAYSIZE(szcp), L"%d", cp); GeneralCfg->GetValue(FavoriteCodePagesKey, szcp, &selectType, 0); if (0 == (selectType & CPST_FAVORITE)) cp = -1; } } } else { while (*deprecated) { while (*deprecated && (*deprecated < L'0' || *deprecated > L'9')) ++deprecated; int dp = (int)wcstol(deprecated, (wchar_t **)&deprecated, 0); if (cp == dp) { cp = -1; break; } } } } if (cp != -1) { nCodePage = cp; bDetect = true; } delete ns; } } xf_free(Buffer); } if (pSignatureFound) { *pSignatureFound = bSignatureFound; } return bDetect; }
bool OldGetFileFormat(FILE *file, UINT &nCodePage, bool *pSignatureFound, bool bUseHeuristics) { DWORD dwTemp=0; bool bSignatureFound = false; bool bDetect=false; if (fread(&dwTemp, 1, 4, file)) { if (LOWORD(dwTemp) == SIGN_UNICODE) { nCodePage = CP_UNICODE; fseek(file, 2, SEEK_SET); bSignatureFound = true; } else if (LOWORD(dwTemp) == SIGN_REVERSEBOM) { nCodePage = CP_REVERSEBOM; fseek(file, 2, SEEK_SET); bSignatureFound = true; } else if ((dwTemp & 0x00FFFFFF) == SIGN_UTF8) { nCodePage = CP_UTF8; fseek(file, 3, SEEK_SET); bSignatureFound = true; } else fseek(file, 0, SEEK_SET); } if (bSignatureFound) { bDetect = true; } else if (bUseHeuristics) { fseek(file, 0, SEEK_SET); size_t sz=0x8000; // BUGBUG. TODO: configurable LPVOID Buffer=xf_malloc(sz); sz=fread(Buffer,1,sz,file); fseek(file,0,SEEK_SET); if (sz) { int test= IS_TEXT_UNICODE_STATISTICS| IS_TEXT_UNICODE_REVERSE_STATISTICS| IS_TEXT_UNICODE_CONTROLS| IS_TEXT_UNICODE_REVERSE_CONTROLS| IS_TEXT_UNICODE_ILLEGAL_CHARS| IS_TEXT_UNICODE_ODD_LENGTH| IS_TEXT_UNICODE_NULL_BYTES; if (IsTextUnicode(Buffer, (int)sz, &test)) { if (!(test&IS_TEXT_UNICODE_ODD_LENGTH) && !(test&IS_TEXT_UNICODE_ILLEGAL_CHARS)) { if ((test&IS_TEXT_UNICODE_NULL_BYTES) || (test&IS_TEXT_UNICODE_CONTROLS) || (test&IS_TEXT_UNICODE_REVERSE_CONTROLS)) { if ((test&IS_TEXT_UNICODE_CONTROLS) || (test&IS_TEXT_UNICODE_STATISTICS)) { nCodePage=CP_UNICODE; bDetect=true; } else if ((test&IS_TEXT_UNICODE_REVERSE_CONTROLS) || (test&IS_TEXT_UNICODE_REVERSE_STATISTICS)) { nCodePage=CP_REVERSEBOM; bDetect=true; } } } } else if (IsTextUTF8((const LPBYTE)Buffer, sz)) { nCodePage=CP_UTF8; bDetect=true; } else { nsUniversalDetectorEx *ns = new nsUniversalDetectorEx(); ns->HandleData((const char*)Buffer,(PRUint32)sz); ns->DataEnd(); int cp = ns->getCodePage(); if (cp != -1) { nCodePage = cp; bDetect = true; } delete ns; } } xf_free(Buffer); } if (pSignatureFound) *pSignatureFound = bSignatureFound; return bDetect; }
XMLNode xmlLoadFromResource(const TCHAR* lpName, LPCTSTR tag, XMLResults *pResults) { LPTSTR lpRes; HRSRC hResInfo; HGLOBAL hRes; int l, len; // Find the xml resource. hResInfo = FindResource (hInst, lpName, TEXT("XMLDialog")); if (hResInfo == NULL) { MessageBoxX(hWndMainWindow, TEXT("Can't find resource"), TEXT("Dialog error"), MB_OK|MB_ICONEXCLAMATION); // unable to find the resource return XMLNode::emptyXMLNode; } // Load the wave resource. hRes = LoadResource (hInst, hResInfo); if (hRes == NULL) { MessageBoxX(hWndMainWindow, TEXT("Can't load resource"), TEXT("Dialog error"), MB_OK|MB_ICONEXCLAMATION); // unable to load the resource return XMLNode::emptyXMLNode; } // Lock the wave resource and do something with it. lpRes = (LPTSTR)LockResource (hRes); if (lpRes) { l = SizeofResource(hInst,hResInfo); if (l>0) { char *buf= (char*)malloc(l+2); if (!buf) { //StartupStore(_T("------ LoadFromRes malloc error%s"),NEWLINE); // 100101 MessageBoxX(hWndMainWindow, TEXT("Can't allocate memory"), TEXT("Dialog error"), MB_OK|MB_ICONEXCLAMATION); // unable to allocate memory return XMLNode::emptyXMLNode; } strncpy(buf,(char*)lpRes,l); buf[l]=0; // need to explicitly null-terminate. buf[l+1]=0; len = l; #if defined(WIN32) || defined(UNDER_CE) #ifdef _UNICODE #if !defined(UNDER_CE) && (WINDOWSPC<1) if (!IsTextUnicode(buf,mmin(l,10000),NULL)) { #endif LPTSTR b2=(LPTSTR)malloc(l*2+2); if (b2==NULL) StartupStore(_T(".... LoadFromRes Malloc1 failed\n")); // 100101 MultiByteToWideChar(CP_ACP, // code page MB_PRECOMPOSED, // character-type options buf, // string to map l, // number of bytes in string b2, // wide-character buffer l*2+2); // size of buffer free(buf); buf=(char*)b2; buf[l*2]= 0; buf[l*2+1]= 0; #if !defined(UNDER_CE) && (WINDOWSPC<1) } #endif #else if (IsTextUnicode(buf,mmin(l,10000),NULL)) { l>>=1; LPTSTR b2=(LPTSTR)malloc(l+2); if (b2==NULL) StartupStore(_T(".... LoadFromRes Malloc2 failed\n")); // 100101 WideCharToMultiByte(CP_ACP, // code page 0, // performance and mapping flags (const WCHAR*)buf, // wide-character string l, // number of chars in string b2, // buffer for new string l+2, // size of buffer NULL, // default for unmappable chars NULL // set when default char used ); free(buf); buf=(char*)b2; } #endif #endif XMLNode x=XMLNode::parseString((LPTSTR)buf,tag,pResults); free(buf); return x; }
bool GetFileFormat( api::fs::file& file, uintptr_t& nCodePage, bool* pSignatureFound, bool bUseHeuristics, bool* pPureAscii) { DWORD dwTemp = 0; bool bSignatureFound = false; bool bDetect = false; bool bPureAscii = false; size_t Readed = 0; if (file.Read(&dwTemp, sizeof(dwTemp), Readed) && Readed > 1 ) // minimum signature size is 2 bytes { if (LOWORD(dwTemp) == SIGN_UNICODE) { nCodePage = CP_UNICODE; file.SetPointer(2, nullptr, FILE_BEGIN); bSignatureFound = true; } else if (LOWORD(dwTemp) == SIGN_REVERSEBOM) { nCodePage = CP_REVERSEBOM; file.SetPointer(2, nullptr, FILE_BEGIN); bSignatureFound = true; } else if ((dwTemp & 0x00FFFFFF) == SIGN_UTF8) { nCodePage = CP_UTF8; file.SetPointer(3, nullptr, FILE_BEGIN); bSignatureFound = true; } else { file.SetPointer(0, nullptr, FILE_BEGIN); } } if (bSignatureFound) { bDetect = true; } else if (bUseHeuristics) { file.SetPointer(0, nullptr, FILE_BEGIN); size_t Size = 0x8000; // BUGBUG. TODO: configurable char_ptr Buffer(Size); size_t ReadSize = 0; bool ReadResult = file.Read(Buffer.get(), Size, ReadSize); file.SetPointer(0, nullptr, FILE_BEGIN); bPureAscii = ReadResult && !ReadSize; // empty file == pure ascii if (ReadResult && ReadSize) { // BUGBUG MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing if (ReadSize > 1) { int test = IS_TEXT_UNICODE_UNICODE_MASK | IS_TEXT_UNICODE_REVERSE_MASK | IS_TEXT_UNICODE_NOT_UNICODE_MASK | IS_TEXT_UNICODE_NOT_ASCII_MASK; IsTextUnicode(Buffer.get(), static_cast<int>(ReadSize), &test); // return value is ignored, it's ok. if (!(test & IS_TEXT_UNICODE_NOT_UNICODE_MASK) && (test & IS_TEXT_UNICODE_NOT_ASCII_MASK)) { if (test & IS_TEXT_UNICODE_UNICODE_MASK) { nCodePage = CP_UNICODE; bDetect = true; } else if (test & IS_TEXT_UNICODE_REVERSE_MASK) { nCodePage = CP_REVERSEBOM; bDetect = true; } } if (!bDetect && IsTextUTF8(Buffer.get(), ReadSize, bPureAscii)) { nCodePage = CP_UTF8; bDetect = true; } } if (!bDetect && !bPureAscii) { int cp = GetCpUsingUniversalDetector(Buffer.get(), ReadSize); if ( cp >= 0 ) { if (Global->Opt->strNoAutoDetectCP.Get() == L"-1") { if ( Global->Opt->CPMenuMode ) { if ( static_cast<UINT>(cp) != GetACP() && static_cast<UINT>(cp) != GetOEMCP() ) { long long selectType = Codepages().GetFavorite(cp); if (0 == (selectType & CPST_FAVORITE)) cp = -1; } } } else { std::vector<string> BannedCpList; split(BannedCpList, Global->Opt->strNoAutoDetectCP, STLF_UNIQUE); if (std::find(ALL_CONST_RANGE(BannedCpList), std::to_wstring(cp)) != BannedCpList.cend()) { cp = -1; } } } if (cp != -1) { nCodePage = cp; bDetect = true; } } } } if (pSignatureFound) *pSignatureFound = bSignatureFound; if (pPureAscii) *pPureAscii = bPureAscii; return bDetect; }
BOOL kull_m_string_suspectUnicodeString(IN PUNICODE_STRING pUnicodeString) { int unicodeTestFlags = IS_TEXT_UNICODE_ODD_LENGTH | IS_TEXT_UNICODE_STATISTICS; return IsTextUnicode(pUnicodeString->Buffer, pUnicodeString->Length, &unicodeTestFlags); }
void DoOpenFile(LPCWSTR szFileName) { static const WCHAR dotlog[] = { '.','L','O','G',0 }; HANDLE hFile; LPSTR pTemp; DWORD size; DWORD dwNumRead; WCHAR log[5]; /* Close any files and prompt to save changes */ if (!DoCloseFile()) return; hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(hFile == INVALID_HANDLE_VALUE) { ShowLastError(); return; } size = GetFileSize(hFile, NULL); if (size == INVALID_FILE_SIZE) { CloseHandle(hFile); ShowLastError(); return; } size++; pTemp = HeapAlloc(GetProcessHeap(), 0, size); if (!pTemp) { CloseHandle(hFile); ShowLastError(); return; } if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL)) { CloseHandle(hFile); HeapFree(GetProcessHeap(), 0, pTemp); ShowLastError(); return; } CloseHandle(hFile); pTemp[dwNumRead] = 0; if (IsTextUnicode(pTemp, dwNumRead, NULL)) { LPWSTR p = (LPWSTR)pTemp; /* We need to strip BOM Unicode character, SetWindowTextW won't do it for us. */ if (*p == 0xFEFF || *p == 0xFFFE) p++; SetWindowTextW(Globals.hEdit, p); } else SetWindowTextA(Globals.hEdit, pTemp); HeapFree(GetProcessHeap(), 0, pTemp); SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0); SendMessage(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0); SetFocus(Globals.hEdit); /* If the file starts with .LOG, add a time/date at the end and set cursor after * See http://support.microsoft.com/?kbid=260563 */ if (GetWindowTextW(Globals.hEdit, log, sizeof(log)/sizeof(log[0])) && !lstrcmp(log, dotlog)) { static const WCHAR lfW[] = { '\r','\n',0 }; SendMessage(Globals.hEdit, EM_SETSEL, GetWindowTextLength(Globals.hEdit), -1); SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW); DIALOG_EditTimeDate(); SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW); } SetFileName(szFileName); UpdateWindowCaption(); }
bool GetFileFormat(api::File& file, uintptr_t& nCodePage, bool* pSignatureFound, bool bUseHeuristics) { DWORD dwTemp=0; bool bSignatureFound = false; bool bDetect=false; DWORD Readed = 0; if (file.Read(&dwTemp, sizeof(dwTemp), Readed) && Readed > 1 ) // minimum signature size is 2 bytes { if (LOWORD(dwTemp) == SIGN_UNICODE) { nCodePage = CP_UNICODE; file.SetPointer(2, nullptr, FILE_BEGIN); bSignatureFound = true; } else if (LOWORD(dwTemp) == SIGN_REVERSEBOM) { nCodePage = CP_REVERSEBOM; file.SetPointer(2, nullptr, FILE_BEGIN); bSignatureFound = true; } else if ((dwTemp & 0x00FFFFFF) == SIGN_UTF8) { nCodePage = CP_UTF8; file.SetPointer(3, nullptr, FILE_BEGIN); bSignatureFound = true; } else { file.SetPointer(0, nullptr, FILE_BEGIN); } } if (bSignatureFound) { bDetect = true; } else if (bUseHeuristics) { file.SetPointer(0, nullptr, FILE_BEGIN); DWORD Size=0x8000; // BUGBUG. TODO: configurable char_ptr Buffer(Size); DWORD ReadSize = 0; bool ReadResult = file.Read(Buffer.get(), Size, ReadSize); file.SetPointer(0, nullptr, FILE_BEGIN); if (ReadResult && ReadSize) { int test= IS_TEXT_UNICODE_STATISTICS| IS_TEXT_UNICODE_REVERSE_STATISTICS| IS_TEXT_UNICODE_CONTROLS| IS_TEXT_UNICODE_REVERSE_CONTROLS| IS_TEXT_UNICODE_ILLEGAL_CHARS| IS_TEXT_UNICODE_ODD_LENGTH| IS_TEXT_UNICODE_NULL_BYTES; if (IsTextUnicode(Buffer.get(), ReadSize, &test)) { if (!(test&IS_TEXT_UNICODE_ODD_LENGTH) && !(test&IS_TEXT_UNICODE_ILLEGAL_CHARS)) { if ((test&IS_TEXT_UNICODE_NULL_BYTES) || (test&IS_TEXT_UNICODE_CONTROLS) || (test&IS_TEXT_UNICODE_REVERSE_CONTROLS)) { if ((test&IS_TEXT_UNICODE_CONTROLS) || (test&IS_TEXT_UNICODE_STATISTICS)) { nCodePage=CP_UNICODE; bDetect=true; } else if ((test&IS_TEXT_UNICODE_REVERSE_CONTROLS) || (test&IS_TEXT_UNICODE_REVERSE_STATISTICS)) { nCodePage=CP_REVERSEBOM; bDetect=true; } } } } else if (IsTextUTF8(Buffer.get(), ReadSize)) { nCodePage=CP_UTF8; bDetect=true; } else { int cp = GetCpUsingUniversalDetector(Buffer.get(), ReadSize); if ( cp >= 0 ) { if (Global->Opt->strNoAutoDetectCP.Get() == L"-1") { if ( Global->Opt->CPMenuMode ) { if ( static_cast<UINT>(cp) != GetACP() && static_cast<UINT>(cp) != GetOEMCP() ) { long long selectType = Global->CodePages->GetFavorite(cp); if (0 == (selectType & CPST_FAVORITE)) cp = -1; } } } else { const auto BannedCpList = StringToList(Global->Opt->strNoAutoDetectCP, STLF_UNIQUE); if (std::find(ALL_CONST_RANGE(BannedCpList), std::to_wstring(cp)) != BannedCpList.cend()) { cp = -1; } } } if (cp != -1) { nCodePage = cp; bDetect = true; } } } } if (pSignatureFound) { *pSignatureFound = bSignatureFound; } return bDetect; }
BOOL PopFileRead (HWND hwndEdit, PTSTR pstrFileName) { BYTE bySwap ; DWORD dwBytesRead ; HANDLE hFile ; int i, iFileLength, iUniTest ; PBYTE pBuffer, pText, pConv ; // Open the file. if (INVALID_HANDLE_VALUE == (hFile = CreateFile (pstrFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL))) return FALSE ; // Get file size in bytes and allocate memory for read. // Add an extra two bytes for zero termination. iFileLength = GetFileSize (hFile, NULL) ; pBuffer = malloc (iFileLength + 2) ; // Read file and put terminating zeros at end. ReadFile (hFile, pBuffer, iFileLength, &dwBytesRead, NULL) ; CloseHandle (hFile) ; pBuffer[iFileLength] = '\0' ; pBuffer[iFileLength + 1] = '\0' ; // Test to see if the text is Unicode iUniTest = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE ; if (IsTextUnicode (pBuffer, iFileLength, &iUniTest)) { pText = pBuffer + 2 ; iFileLength -= 2 ; if (iUniTest & IS_TEXT_UNICODE_REVERSE_SIGNATURE) { for (i = 0 ; i < iFileLength / 2 ; i++) { bySwap = ((BYTE *) pText) [2 * i] ; ((BYTE *) pText) [2 * i] = ((BYTE *) pText) [2 * i + 1] ; ((BYTE *) pText) [2 * i + 1] = bySwap ; } } // Allocate memory for possibly converted string pConv = malloc (iFileLength + 2) ; // If the edit control is not Unicode, convert Unicode text to // non-Unicode (ie, in general, wide character). #ifndef UNICODE WideCharToMultiByte (CP_ACP, 0, (PWSTR) pText, -1, pConv, iFileLength + 2, NULL, NULL) ; // If the edit control is Unicode, just copy the string #else lstrcpy ((PTSTR) pConv, (PTSTR) pText) ; #endif } else // the file is not Unicode { pText = pBuffer ; // Allocate memory for possibly converted string. pConv = malloc (2 * iFileLength + 2) ; // If the edit control is Unicode, convert ASCII text. #ifdef UNICODE MultiByteToWideChar (CP_ACP, 0, pText, -1, (PTSTR) pConv, iFileLength + 1) ; // If not, just copy buffer #else lstrcpy ((PTSTR) pConv, (PTSTR) pText) ; #endif } SetWindowText (hwndEdit, (PTSTR) pConv) ; free (pBuffer) ; free (pConv) ; return TRUE ; }
CSubtitleText::CSubtitleText(wchar_t *szFile) { m_hFile = CreateFile(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (m_hFile == INVALID_HANDLE_VALUE) { m_hFile = NULL; return ; } LARGE_INTEGER liSize; GetFileSizeEx(m_hFile, &liSize); if (liSize.QuadPart < 16) return ; if (liSize.QuadPart > MAX_SUBTITILE_FILE_SIZE) return ; int32_t fSize = liSize.LowPart; m_Buffer = (uint8_t *)MemoryAlloc(fSize + 8); DWORD NumberOfBytesRead = 0; ReadFile(m_hFile, m_Buffer, fSize, &NumberOfBytesRead, NULL); if (NumberOfBytesRead != fSize) return ; ZeroMemory(m_Buffer + fSize, 8); SAFE_CLOSE(m_hFile); if ((m_Buffer[0] == 0xFF) && (m_Buffer[1] == 0xFE)) { m_Scrpits = (wchar_t *)MemoryAlloc(fSize + 8); wchar_t *s = (wchar_t *)&m_Buffer[2]; while (*s) { if (*s != L'\r') { m_Scrpits[m_cCharacter] = *s; m_cCharacter ++; } s++; } ZeroMemory(&m_Scrpits[m_cCharacter], 8); } else if ((m_Buffer[0] == 0xEF) && (m_Buffer[1] == 0xBB) && (m_Buffer[2] == 0xBF)) { int32_t len = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)m_Buffer + 3, fSize - 3, NULL, 0); if (len == 0) return ; m_Scrpits = (wchar_t *)MemoryAlloc(len * 2 + 8); m_cCharacter = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)m_Buffer + 3, fSize - 3, m_Scrpits, len * 2 + 2); if (m_cCharacter == 0) return ; ZeroMemory(&m_Scrpits[m_cCharacter], 8); wchar_t *p1 = m_Scrpits; wchar_t *p2 = m_Scrpits; while(*p2) { if (*p2 != '\r')*p1++ = *p2; p2 ++; } ZeroMemory(p2, 8); } else { INT unicode = IS_TEXT_UNICODE_UNICODE_MASK; INT unicodeReverse = IS_TEXT_UNICODE_REVERSE_MASK; INT notAscii = IS_TEXT_UNICODE_NOT_ASCII_MASK; if (((fSize & 1) == 0) && IsTextUnicode(m_Buffer, fSize, &unicode)) { m_Scrpits = (wchar_t *)MemoryAlloc(fSize + 8); wchar_t *s = (wchar_t *)m_Buffer; while (*s) { if (*s != L'\r') { m_Scrpits[m_cCharacter] = *s; m_cCharacter ++; } s++; } ZeroMemory(&m_Scrpits[m_cCharacter], 8); } else if (((fSize & 1) == 0) && IsTextUnicode(m_Buffer, fSize, &unicodeReverse)) { m_Scrpits = (wchar_t *)MemoryAlloc(fSize + 8); uint8_t *s = m_Buffer; while (*((uint16_t *)s)) { wchar_t ch = (uint16_t)s[0] + ((uint16_t)s[1] << 8); if (ch != L'\r') { m_Scrpits[m_cCharacter] = ch; m_cCharacter ++; } s += 2; } ZeroMemory(&m_Scrpits[m_cCharacter], 8); } else if (IsTextUnicode(m_Buffer, fSize, ¬Ascii)) { // } else { int32_t len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)m_Buffer, fSize - 3, NULL, 0); if (len == 0) return ; m_Scrpits = (wchar_t *)MemoryAlloc(len * 2 + 8); len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)m_Buffer, fSize - 3, m_Scrpits, len * 2 + 8); if (len == 0) return ; if (m_cCharacter == 0) return ; ZeroMemory(&m_Scrpits[m_cCharacter], 8); wchar_t *p1 = m_Scrpits; wchar_t *p2 = m_Scrpits; while(*p2) { if (*p2 != '\r')*p1++ = *p2; p2 ++; } ZeroMemory(p2, 8); } } if (m_cCharacter == NULL) { return ; } }
BOOL kull_m_string_suspectUnicodeString(IN PUNICODE_STRING pUnicodeString) { int unicodeTestFlags = IS_TEXT_UNICODE_STATISTICS; return ((pUnicodeString->Length == sizeof(wchar_t)) && IsCharAlphaNumeric(pUnicodeString->Buffer[0])) || IsTextUnicode(pUnicodeString->Buffer, pUnicodeString->Length, &unicodeTestFlags); }
BOOL CFile::SetupReadBuffer() { const WCHAR* pWideBuffer = nullptr; _pReadBuffer = (const WCHAR *) new (std::nothrow) BYTE[ _fileSize ]; if (!_pReadBuffer) { return FALSE; } DWORD dwNumberOfByteRead = 0; if (!ReadFile(_fileHandle, (LPVOID)_pReadBuffer, (DWORD)_fileSize, &dwNumberOfByteRead, NULL)) { delete [] _pReadBuffer; _pReadBuffer = nullptr; return FALSE; } if (!IsTextUnicode(_pReadBuffer, dwNumberOfByteRead, NULL)) { // This is ASCII file. // Read file with Unicode conversion. int wideLength = 0; wideLength = MultiByteToWideChar(_codePage, 0, (LPCSTR)_pReadBuffer, dwNumberOfByteRead, NULL, 0); if (wideLength <= 0) { delete [] _pReadBuffer; _pReadBuffer = nullptr; return FALSE; } pWideBuffer = new (std::nothrow) WCHAR[ wideLength ]; if (!pWideBuffer) { delete [] _pReadBuffer; _pReadBuffer = nullptr; return FALSE; } wideLength = MultiByteToWideChar(_codePage, 0, (LPCSTR)_pReadBuffer, (DWORD)_fileSize, (LPWSTR)pWideBuffer, wideLength); if (wideLength <= 0) { delete [] pWideBuffer; delete [] _pReadBuffer; _pReadBuffer = nullptr; return FALSE; } _fileSize = wideLength * sizeof(WCHAR); delete [] _pReadBuffer; _pReadBuffer = pWideBuffer; } else if (_fileSize > sizeof(WCHAR)) { // Read file in allocated buffer pWideBuffer = new (std::nothrow) WCHAR[ _fileSize/sizeof(WCHAR) - 1 ]; if (!pWideBuffer) { delete [] _pReadBuffer; _pReadBuffer = nullptr; return FALSE; } // skip unicode byte-order signature SetFilePointer(_fileHandle, sizeof(WCHAR), NULL, FILE_BEGIN); if (!ReadFile(_fileHandle, (LPVOID)pWideBuffer, (DWORD)(_fileSize - sizeof(WCHAR)), &dwNumberOfByteRead, NULL)) { delete [] pWideBuffer; delete [] _pReadBuffer; _pReadBuffer = nullptr; return FALSE; } _fileSize -= sizeof(WCHAR); delete [] _pReadBuffer; _pReadBuffer = pWideBuffer; } else { return FALSE; } return TRUE; }
BOOL FileReverse(PCTSTR pszPathname, PBOOL pbIsTextUnicode) { *pbIsTextUnicode = FALSE; // Assume text is Unicode // Open the file for reading and writing. HANDLE hFile = CreateFile(pszPathname, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { chMB("File could not be opened."); return(FALSE); } // Get the size of the file (I assume the whole file can be mapped). DWORD dwFileSize = GetFileSize(hFile, NULL); // Create the file-mapping object. The file-mapping object is 1 character // bigger than the file size so that a zero character can be placed at the // end of the file to terminate the string (file). Because I don't yet know // if the file contains ANSI or Unicode characters, I assume worst case // and add the size of a WCHAR instead of CHAR. HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, dwFileSize + sizeof(WCHAR), NULL); if (hFileMap == NULL) { chMB("File map could not be opened."); CloseHandle(hFile); return(FALSE); } // Get the address where the first byte of the file is mapped into memory. PVOID pvFile = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0); if (pvFile == NULL) { chMB("Could not map view of file."); CloseHandle(hFileMap); CloseHandle(hFile); return(FALSE); } // Does the buffer contain ANSI or Unicode? int iUnicodeTestFlags = -1; // Try all tests *pbIsTextUnicode = IsTextUnicode(pvFile, dwFileSize, &iUnicodeTestFlags); if (!*pbIsTextUnicode) { // For all the file manipulations below, we explicitly use ANSI // functions because we are processing an ANSI file. // Put a zero character at the very end of the file. PSTR pchANSI = (PSTR) pvFile; pchANSI[dwFileSize / sizeof(CHAR)] = 0; // Reverse the contents of the file. _strrev(pchANSI); // Convert all "\n\r" combinations back to "\r\n" to // preserve the normal end-of-line sequence. pchANSI = strstr(pchANSI, "\n\r"); // Find first "\r\n". while (pchANSI != NULL) { // We have found an occurrence.... *pchANSI++ = '\r'; // Change '\n' to '\r'. *pchANSI++ = '\n'; // Change '\r' to '\n'. pchANSI = strstr(pchANSI, "\n\r"); // Find the next occurrence. } } else { // For all the file manipulations below, we explicitly use Unicode // functions because we are processing a Unicode file. // Put a zero character at the very end of the file. PWSTR pchUnicode = (PWSTR) pvFile; pchUnicode[dwFileSize / sizeof(WCHAR)] = 0; if ((iUnicodeTestFlags & IS_TEXT_UNICODE_SIGNATURE) != 0) { // If the first character is the Unicode BOM (byte-order-mark), // 0xFEFF, keep this character at the beginning of the file. pchUnicode++; } // Reverse the contents of the file. _wcsrev(pchUnicode); // Convert all "\n\r" combinations back to "\r\n" to // preserve the normal end-of-line sequence. pchUnicode = wcsstr(pchUnicode, L"\n\r"); // Find first '\n\r'. while (pchUnicode != NULL) { // We have found an occurrence.... *pchUnicode++ = L'\r'; // Change '\n' to '\r'. *pchUnicode++ = L'\n'; // Change '\r' to '\n'. pchUnicode = wcsstr(pchUnicode, L"\n\r"); // Find the next occurrence. } } // Clean up everything before exiting. UnmapViewOfFile(pvFile); CloseHandle(hFileMap); // Remove trailing zero character added earlier. SetFilePointer(hFile, dwFileSize, NULL, FILE_BEGIN); SetEndOfFile(hFile); CloseHandle(hFile); return(TRUE); }