diff --git a/Common/OfficeFileFormatChecker.h b/Common/OfficeFileFormatChecker.h index 2ba9c642e9..de6e31a80c 100644 --- a/Common/OfficeFileFormatChecker.h +++ b/Common/OfficeFileFormatChecker.h @@ -72,7 +72,7 @@ private: bool isMS_OFFCRYPTOFormatFile (POLE::Storage * storage); bool isRtfFormatFile (unsigned char* pBuffer,int dwBytes); - bool isHtmlFormatFile (unsigned char* pBuffer,int dwBytes); + bool isHtmlFormatFile (unsigned char* pBuffer,int dwBytes, bool testCloseTag); bool isPdfFormatFile (unsigned char* pBuffer,int dwBytes); bool isBinaryDoctFormatFile (unsigned char* pBuffer,int dwBytes); diff --git a/Common/OfficeFileFormatChecker2.cpp b/Common/OfficeFileFormatChecker2.cpp index 7bcf3cfbb0..9540f79faf 100644 --- a/Common/OfficeFileFormatChecker2.cpp +++ b/Common/OfficeFileFormatChecker2.cpp @@ -29,11 +29,13 @@ * terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode * */ -#include #include "OfficeFileFormatChecker.h" + #include "../DesktopEditor/common/File.h" #include "../OfficeUtils/src/OfficeUtils.h" + #include "3dParty/pole/pole.h" +#include #define MIN_SIZE_BUFFER 4096 #define MAX_SIZE_BUFFER 102400 @@ -47,22 +49,40 @@ bool COfficeFileFormatChecker::isRtfFormatFile(unsigned char* pBuffer,int dwByte return false; } -bool COfficeFileFormatChecker::isHtmlFormatFile(unsigned char* pBuffer,int dwBytes) +bool COfficeFileFormatChecker::isHtmlFormatFile(unsigned char* pBuffer, int dwBytes, bool testCloseTag) { if (pBuffer == NULL) return false; - bool tagOpen = false; - //Html File is XML-file with rootElement - html - for (int i = 0; i < dwBytes - 4 && i < 100; i++) - { - if (0x3C == pBuffer[i]) - tagOpen = true; - else if (0x3E == pBuffer[i]) - tagOpen = false; - else if (tagOpen && (0x48 == pBuffer[i] || 0x68 == pBuffer[i]) && (0x54 == pBuffer[i + 1] || 0x74 == pBuffer[i + 1]) - && (0x4d == pBuffer[i + 2] || 0x6d == pBuffer[i + 2]) && (0x4c == pBuffer[i + 3] || 0x6c == pBuffer[i + 3])) - return true; - } + bool tagOpen = false; + + if (testCloseTag) + { + for (int i = 0; i < dwBytes - 6 ; i++) + { + if ((0x3C == pBuffer[i]) && (0x2F == pBuffer[i +1]) && (0x48 == pBuffer[i + 2] || 0x68 == pBuffer[i + 2]) + && (0x54 == pBuffer[i + 3] || 0x74 == pBuffer[i + 3]) + && (0x4d == pBuffer[i + 4] || 0x6d == pBuffer[i + 4]) + && (0x4c == pBuffer[i + 5] || 0x6c == pBuffer[i + 5])) + { + return true; + } + } + } + else + { + for (int i = 0; i < dwBytes - 4 && i < 100; i++) + { + if (0x3C == pBuffer[i]) + tagOpen = true; + else if (0x3E == pBuffer[i]) + tagOpen = false; + else if (tagOpen && (0x48 == pBuffer[i] || 0x68 == pBuffer[i]) && (0x54 == pBuffer[i + 1] || 0x74 == pBuffer[i + 1]) + && (0x4d == pBuffer[i + 2] || 0x6d == pBuffer[i + 2]) && (0x4c == pBuffer[i + 3] || 0x6c == pBuffer[i + 3])) + { + return true; + } + } + } return false; } @@ -222,9 +242,8 @@ bool COfficeFileFormatChecker::isOfficeFile(const std::wstring & fileName) if (!buffer){file.CloseFile();return false;} DWORD dwReadBytes = 0; - file.ReadFile(buffer,MIN_SIZE_BUFFER,dwReadBytes); + file.ReadFile(buffer, MIN_SIZE_BUFFER, dwReadBytes); int sizeRead = (int)dwReadBytes; - file.CloseFile(); if ( isRtfFormatFile(buffer,sizeRead) ) { @@ -250,15 +269,27 @@ bool COfficeFileFormatChecker::isOfficeFile(const std::wstring & fileName) { nFileType = AVS_OFFICESTUDIO_FILE_CROSSPLATFORM_DJVU; } - else if (isHtmlFormatFile(buffer,sizeRead) ) + else if (isHtmlFormatFile(buffer,sizeRead, false)) { - nFileType = AVS_OFFICESTUDIO_FILE_DOCUMENT_HTML; + long fileSize = file.GetFileSize(); + if (fileSize > MIN_SIZE_BUFFER) + { + file.SeekFile(fileSize - MIN_SIZE_BUFFER); + file.ReadFile(buffer, MIN_SIZE_BUFFER, dwReadBytes); + int sizeRead = (int)dwReadBytes; + } + if (isHtmlFormatFile(buffer,sizeRead, true)) + { + nFileType = AVS_OFFICESTUDIO_FILE_DOCUMENT_HTML; + } } else if (isFB2FormatFile(buffer,sizeRead) ) { nFileType = AVS_OFFICESTUDIO_FILE_DOCUMENT_FB2; } ///////////////////////////////////////////////////////////////////////// + file.CloseFile(); + if (buffer)delete []buffer; buffer = NULL; }