This commit is contained in:
Elena.Subbotina
2023-09-06 18:31:10 +03:00
parent 1194fcb012
commit ab2eb4a19b
4 changed files with 147 additions and 40 deletions

View File

@ -72,6 +72,7 @@ public:
bool isDocFormatFile (const std::wstring & fileName);
bool isXlsFormatFile (const std::wstring & fileName);
bool isOleObjectFile (POLE::Storage* storage);
bool isDocFormatFile (POLE::Storage *storage);
bool isXlsFormatFile (POLE::Storage *storage);
bool isPptFormatFile (POLE::Storage *storage);

View File

@ -34,7 +34,7 @@
#include "../DesktopEditor/common/Directory.h"
#include "../OfficeUtils/src/OfficeUtils.h"
#include "../DesktopEditor/xml/include/xmlutils.h"
#include "../OOXML/Base/Base.h"
//#if defined FILE_FORMAT_CHECKER_WITH_MACRO
// #include "../MsBinaryFile/PptFile/Main/PPTFormatLib.h"
//#endif
@ -45,6 +45,49 @@
#define MIN_SIZE_BUFFER 4096
#define MAX_SIZE_BUFFER 102400
std::string ReadStringFromOle(POLE::Stream* stream, unsigned int max_size)
{
std::string result;
if (!stream) return result;
_UINT32 cch = 0;
if (4 != stream->read((BYTE*)&cch, 4)) return result;
unsigned char* stringBytes = new unsigned char[max_size];
if (!stringBytes) return result;
if (cch > max_size)
{
//error ... skip to 0
unsigned int pos_orinal = stream->tell();
unsigned int pos = 0;
stream->read(stringBytes, max_size);
while (pos < max_size)
{
if (stringBytes[pos] == 0)
break;
pos++;
}
stream->seek(pos_orinal + pos - 1);
}
else
{
if (cch > 0)
{
//dont read the terminating zero
cch = stream->read(stringBytes, cch);
result = std::string((char*)stringBytes, cch);
}
}
RELEASEARRAYOBJECTS(stringBytes);
//skip the terminating zero of the Unicode string
stream->seek(stream->tell() + 2);
return result;
}
bool COfficeFileFormatChecker::isRtfFormatFile(unsigned char* pBuffer,int dwBytes)
{
if (pBuffer == NULL) return false;
@ -182,7 +225,61 @@ bool COfficeFileFormatChecker::isPdfFormatFile (unsigned char* pBuffer,int dwByt
return false;
}
bool COfficeFileFormatChecker::isDocFormatFile (POLE::Storage * storage)
bool COfficeFileFormatChecker::isOleObjectFile(POLE::Storage* storage)
{
if (storage == NULL) return false;
POLE::Stream streamOle(storage, L"Ole");
if (false == streamOle.fail())
{
std::string UserType, ClipboardFormat, Program;
POLE::Stream streamCompObject(storage, L"CompObj");
if (false == streamCompObject.fail())
{
streamCompObject.seek(28); // skip Header
unsigned int sz_obj = streamCompObject.size() - streamCompObject.tell();
if (sz_obj > 4)
{
UserType = ReadStringFromOle(&streamCompObject, sz_obj);
sz_obj = streamCompObject.size() - streamCompObject.tell();
if (sz_obj > 4)
ClipboardFormat = ReadStringFromOle(&streamCompObject, sz_obj);
sz_obj = streamCompObject.size() - streamCompObject.tell();
if (sz_obj > 4)
Program = ReadStringFromOle(&streamCompObject, sz_obj);
}
return true;
}
else
{
POLE::Stream streamLinkInfo(storage, L"LinkInfo");
if (false == streamLinkInfo.fail())
{
short cch = 0;
if (2 == streamLinkInfo.read((BYTE*)&cch, 2))
{
unsigned char* str = new unsigned char[cch];
cch = streamLinkInfo.read(str, cch);
ClipboardFormat = std::string((char*)str, cch);
RELEASEARRAYOBJECTS(str);
streamLinkInfo.seek(streamLinkInfo.tell() + 6);
//skip ...
}
return true;
}
}
}
return false;
}
bool COfficeFileFormatChecker::isDocFormatFile (POLE::Storage * storage)
{
if (storage == NULL) return false;
@ -458,7 +555,11 @@ bool COfficeFileFormatChecker::isOfficeFile(const std::wstring & _fileName)
POLE::Storage storage(fileName.c_str());
if (storage.open())
{
if ( isDocFormatFile(&storage) )
if (isOleObjectFile(&storage))
{
return false;
}
else if ( isDocFormatFile(&storage) )
{
//nFileType внутри
return true;

View File

@ -533,8 +533,8 @@ namespace SimpleTypes
case 5:this->m_eValue = fontfamilyDecorative; break;
default:this->m_eValue = fontfamilyNotApplicable;
}
return this->m_eValue;
}
return this->m_eValue;
}
std::wstring CFontFamily::ToString () const
@ -2409,9 +2409,9 @@ namespace SimpleTypes
switch(this->m_eValue)
{
case layoutNone_ : return L"none";
case layoutOverlapping: return L"banner";
case layoutOverlapping: return L"overlapping";
case layoutBanner :
default : return L"overlapping";
default : return L"banner";
}
}

View File

@ -250,46 +250,51 @@ namespace PPTX
std::wstring sProgID = m_sProgId.get_value_or(L"");
//test xls ole_file for convert to xlsx
COfficeFileFormatChecker checker;
if (ole_file.IsInit() && (checker.isXlsFormatFile(ole_file->filename().GetPath()) ||
checker.isDocFormatFile(ole_file->filename().GetPath())))
if (ole_file.IsInit())
{
std::wstring sTemp = ole_file->filename().GetDirectory();
std::wstring sResultOoxmlDir = sTemp + FILE_SEPARATOR_STR + _T("ooxml_unpacked");
NSDirectory::CreateDirectory(sResultOoxmlDir);
COfficeFileFormatChecker checker;
checker.isOfficeFile(ole_file->filename().GetPath());
bool bMacro = true;
_UINT32 nRes = 0;
std::wstring ooxml_file;
if (checker.nFileType == AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLS)
if (checker.nFileType == AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLS ||
checker.nFileType == AVS_OFFICESTUDIO_FILE_DOCUMENT_DOC)
{
nRes = ConvertXls2Xlsx(ole_file->filename().GetPath(), sResultOoxmlDir, L"", L"", sTemp, 0, bMacro);
std::wstring sTemp = ole_file->filename().GetDirectory();
ooxml_file = ole_file->filename().GetPath() + (bMacro ? L".xlsm" : L".xlsx");
}
else if (checker.nFileType == AVS_OFFICESTUDIO_FILE_DOCUMENT_DOC)
{
COfficeDocFile docFile;
docFile.m_sTempFolder = ole_file->filename().GetDirectory();
std::wstring sResultOoxmlDir = sTemp + FILE_SEPARATOR_STR + _T("ooxml_unpacked");
NSDirectory::CreateDirectory(sResultOoxmlDir);
nRes = docFile.LoadFromFile(ole_file->filename().GetPath(), sResultOoxmlDir, L"", bMacro);
bool bMacro = true;
_UINT32 nRes = 0;
std::wstring ooxml_file;
ooxml_file = ole_file->filename().GetPath() + (bMacro ? L".docm" : L".docx");
}
if (0 == nRes)
{
COfficeUtils oCOfficeUtils(NULL);
nRes = (S_OK == oCOfficeUtils.CompressFileOrDirectory(sResultOoxmlDir, ooxml_file)) ? nRes : S_FALSE;
}
NSDirectory::DeleteDirectory(sResultOoxmlDir);
if (0 == nRes && false == ooxml_file.empty())
{
ole_file->set_MsPackage(true);
ole_file->set_filename(ooxml_file, false);
if (checker.nFileType == AVS_OFFICESTUDIO_FILE_SPREADSHEET_XLS)
{
nRes = ConvertXls2Xlsx(ole_file->filename().GetPath(), sResultOoxmlDir, L"", L"", sTemp, 0, bMacro);
ooxml_file = ole_file->filename().GetPath() + (bMacro ? L".xlsm" : L".xlsx");
}
else if (checker.nFileType == AVS_OFFICESTUDIO_FILE_DOCUMENT_DOC)
{
COfficeDocFile docFile;
docFile.m_sTempFolder = ole_file->filename().GetDirectory();
nRes = docFile.LoadFromFile(ole_file->filename().GetPath(), sResultOoxmlDir, L"", bMacro);
ooxml_file = ole_file->filename().GetPath() + (bMacro ? L".docm" : L".docx");
}
if (0 == nRes)
{
COfficeUtils oCOfficeUtils(NULL);
nRes = (S_OK == oCOfficeUtils.CompressFileOrDirectory(sResultOoxmlDir, ooxml_file)) ? nRes : S_FALSE;
}
NSDirectory::DeleteDirectory(sResultOoxmlDir);
if (0 == nRes && false == ooxml_file.empty())
{
ole_file->set_MsPackage(true);
ole_file->set_filename(ooxml_file, false);
}
}
}